{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import os\nimport cv2\nimport glob\nimport pydicom\nimport numpy as np\nimport pandas as pd\nimport torch\nimport torch.optim as optim\nfrom albumentations import Compose, ShiftScaleRotate, Resize, CenterCrop, HorizontalFlip, RandomBrightnessContrast, Normalize\nfrom albumentations.pytorch import ToTensor\nfrom torch.utils.data import Dataset\nfrom tqdm import tqdm_notebook as tqdm\nfrom matplotlib import pyplot as plt\nfrom torchvision import transforms","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = '../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/'\npath_trn = path+'stage_2_train'\npath_tst = path+'stage_2_test'\n\npath_dfs = '../input/rsnakerneldataframe/'\npath_df_sample = path_dfs+'df_sample.fth'\npath_df_trn = path_dfs+'df_trn.fth'\npath_df_tst = path_dfs+'fns_tst.fth'\npath_df_lbls = path_dfs+'labels.fth'\npath_bins = path_dfs+'bins.pkl'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_trn = pd.read_feather(path_df_trn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_lbls = pd.read_feather(path_df_lbls)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"complete_df = df_trn.join(df_lbls.set_index('ID'), 'SOPInstanceUID')\nassert not len(complete_df[complete_df['any'].isna()])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"complete_df = complete_df.assign(pct_cut = pd.cut(complete_df.img_pct_window, [0,0.02,0.03,0.05,0.1,0.3,1]))\ncomplete_df.drop(complete_df.query('img_pct_window<0.02').index, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_lbl = complete_df.query('any==True')\nlen(df_lbl)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_nonlbl = complete_df.query('any==False').sample(len(df_lbl))\nlen(df_nonlbl)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"complete_df = pd.concat([df_lbl,df_nonlbl])\nlen(complete_df)\ndel(df_nonlbl, df_lbl)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_samp_idx = complete_df.sample(frac=0.05)\ndf_samp_idx.to_csv('df_samp_idx.csv')\ndel(complete_df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#img_file = os.path.join(path_trn, list(map(filename, df_samp.fname))[0]+'.dcm')\nfor index, row in df_samp_idx.iterrows():\n    img_file = df_samp_idx.fname[index]\n    dcm_name = img_file.rsplit(\"/\")[-1].rsplit(\".\")[0]\n    img_name = pydicom.dcmread(img_file)\n    img_name = img_name.pixel_array\n    cv2.imwrite(dcm_name + '.png', img_name) # write png image os.path.join(path , 'waka.jpg')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}