{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import glob, pylab, pandas as pd\nimport pydicom, numpy as np\nfrom os import listdir\nfrom os.path import isfile, join\nimport matplotlib.pylab as plt\nimport os\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import sys\nprint(sys.version)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow\nimport tensorflow.keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cd /kaggle/input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"current_dir='/kaggle/input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/'\n\ntraining_dir=os.path.join(current_dir, 'stage_2_train')\ndf=pd.read_csv(\"/kaggle/input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train.csv\")\nprint(len(os.listdir(training_dir)))\n# print((os.listdir(training_dir)[0]))\n# ID_b83c56d9b.dcm\n\ntest_dir= os.path.join(current_dir, 'stage_2_test')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_dir=os.path.join(training_dir, \"\")\ndf['ID'] = df['ID'].str.split(\"_\", n = 3, expand = True)[1]\ndef append_ext2(fn):\n    return \"ID_\"+fn+\".dcm\"\n\ndf[\"ID\"]=df[\"ID\"].apply(append_ext2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(new_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df1=df.loc[lambda x: x.index % 6 == 0, \"ID\"]\ndf1.reset_index(inplace = True, drop = True)\ndf2=pd.DataFrame(df.loc[lambda x: x.index % 6 == 0, \"Label\"])\ndf2.reset_index(inplace = True, drop = True)\ndf2.rename(columns = {'Label': \"type1\"}, inplace=True)\ndf3=pd.DataFrame(df.loc[lambda x: x.index % 6 == 1, \"Label\"])\ndf3.reset_index(inplace = True, drop = True)\ndf3.rename(columns = {'Label': \"type2\"}, inplace=True)\ndf4=pd.DataFrame(df.loc[lambda x: x.index % 6 == 2, \"Label\"])\ndf4.reset_index(inplace = True, drop = True)\ndf4.rename(columns = {'Label': \"type3\"}, inplace=True)\ndf5=pd.DataFrame(df.loc[lambda x: x.index % 6 == 3, \"Label\"])\ndf5.reset_index(inplace = True, drop = True)\ndf5.rename(columns = {'Label': \"type4\"}, inplace=True)\ndf6=pd.DataFrame(df.loc[lambda x: x.index % 6 == 4, \"Label\"])\ndf6.reset_index(inplace = True, drop = True)\ndf6.rename(columns = {'Label': \"type5\"}, inplace=True)\ndf7=pd.DataFrame(df.loc[lambda x: x.index % 6 == 5, \"Label\"])\ndf7.reset_index(inplace = True, drop = True)\ndf7.rename(columns = {'Label': \"type6\"}, inplace=True)\n\ndf8 = pd.concat([df1, df2, df3, df4, df5, df6, df7], axis=1, sort=False)\ndf8.rename(columns = {'ID': \"Id\"}, inplace=True)\ndf8.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df8.to_csv('/kaggle/working/train_processed.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final=pd.read_csv(\"/kaggle/working/train_processed.csv\")\ndf_final.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def window_image(img, window_center,window_width, intercept, slope):\n\n    img = (img*slope +intercept)\n    img_min = window_center - window_width//2\n    img_max = window_center + window_width//2\n    img[img<img_min] = img_min\n    img[img>img_max] = img_max\n    return img \n\ndef get_first_of_dicom_field_as_int(x):\n    #get x[0] as in int is x is a 'pydicom.multival.MultiValue', otherwise get int(x)\n    if type(x) == pydicom.multival.MultiValue:\n        return int(x[0])\n    else:\n        return int(x)\n    \ndef get_windowing(data):\n    dicom_fields = [data[('0028','1050')].value, #window center\n                    data[('0028','1051')].value, #window width\n                    data[('0028','1052')].value, #intercept\n                    data[('0028','1053')].value] #slope\n    return [get_first_of_dicom_field_as_int(x) for x in dicom_fields]\n\ndef full(file):\n    ds=pydicom.dcmread(file)\n    image = ds.pixel_array\n    window_center , window_width, intercept, slope = get_windowing(ds)\n    image_windowed = window_image(image, window_center, window_width, intercept, slope)\n    \n    return image_windowed","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen=ImageDataGenerator(rescale=1./255, preprocessing_function=full)\ntrain_generator=train_datagen.flow_from_dataframe(dataframe=df_final, directory= new_dir,\n                                                  x_col=\"Id\",\n                                                  y_col=['type1', 'type2', 'type3', 'type4', 'type5', 'type6'], \n                                                  class_mode=\"other\", has_ext=False, target_size=(128, 128), \n                                                  batch_size=128, shuffle = True)\n#new_dir=/kaggle/input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train/\nfor i in train_generator:\n    idx = (train_generator.batch_index - 1) * train_generator.batch_size\n    print(train_generator.filenames[idx : idx + train_generator.batch_size])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}