{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install fastai2 -q","execution_count":null,"outputs":[]},{"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\n#Load the dependancies\nfrom fastai2.basics import *\nfrom fastai2.callback.all import *\nfrom fastai2.vision.all import *\nfrom fastai2.medical.imaging import *\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport torch\nimport torch.nn as nn\nfrom torchvision.models import resnet18\nimport pydicom.pixel_data_handlers.gdcm_handler as gdcm_handler \n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\nimport pydicom\nimport os\nfrom torch.utils.data import DataLoader, Dataset\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# There seem to be a bunch of dicom files where the pixels are corrupted","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df=pd.read_csv('../input/osic-pulmonary-fibrosis-progression/train.csv')\ntest_df=pd.read_csv('../input/osic-pulmonary-fibrosis-progression/test.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Ive extracted the good and bad files into dictionaries of lists: key=subject, value=[list of filenames]","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import gc\n\ngood_file_dict={}\nbad_file_dict={}\ni=0\nfor subject in train_df.Patient.unique():\n    subject_path='../input/osic-pulmonary-fibrosis-progression/train/' +subject\n    all_subject_files = os.listdir(subject_path)\n    good_file_list=[]\n    bad_file_list=[]\n    for file in all_subject_files:\n        try:\n            im=dcmread(os.path.join(subject_path, file)).pixels\n            good_file_list.append(file)\n        except ValueError:\n            bad_file_list.append(os.path.join(subject_path, file))\n            continue\n        except RuntimeError:\n            bad_file_list.append(os.path.join(subject_path, file))\n            continue\n    good_file_dict[subject]=good_file_list\n    bad_file_dict[subject]=bad_file_list\n    i+=1\n    print(i/len(train_df.Patient.unique())*100,'%')\n\n\ni=0\nfor subject in test_df.Patient.unique():\n    subject_path='../input/osic-pulmonary-fibrosis-progression/test/' +subject\n    all_subject_files = os.listdir(subject_path)\n    good_file_list=[]\n    bad_file_list=[]\n    for file in all_subject_files:\n        try:\n            im=dcmread(os.path.join(subject_path, file)).pixels\n            good_file_list.append(file)\n        except ValueError:\n            bad_file_list.append(os.path.join(subject_path, file))\n            continue\n        except RuntimeError:\n            bad_file_list.append(os.path.join(subject_path, file))\n            continue\n    good_file_dict[subject]=good_file_list\n    bad_file_dict[subject]=bad_file_list\n    i+=1\n    print(i/len(test_df.Patient.unique())*100,'%')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"good_file_dict['ID00007637202177411956430']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pickle\n\nwith open('good_files.pickle', 'wb') as handle:\n    pickle.dump(good_file_dict, handle)\n\nwith open('good_files.pickle', 'rb') as handle:\n    file_dict = pickle.load(handle)\n    \nwith open('bad_files.pickle', 'wb') as handle:\n    pickle.dump(bad_file_dict, handle)","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}