{"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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor 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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/osic-pulmonary-fibrosis-progression/train.csv')\ntest=pd.read_csv('/kaggle/input/osic-pulmonary-fibrosis-progression/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai2.basics import *\nfrom fastai2.callback.all import *\nfrom fastai2.vision.all import *\nfrom fastai2.medical.imaging import *\n\nimport pydicom\nimport seaborn as sns\n\nimport numpy as np\nimport pandas as pd\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pip install fastai2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"source = Path(\"/kaggle/input/osic-pulmonary-fibrosis-progression/\")\nfiles = os.listdir(source)\nprint(files)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = source/'train'\ntrain_files = get_dicom_files(train)\ntrain_files","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"patient1 = train_files[7]\ndimg = dcmread(patient1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dimg","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_one_patient(file):\n    \"\"\" function to view patient image and choosen tags within the head of the DICOM\"\"\"\n    pat = dcmread(file)\n    print(f'patient Name: {pat.PatientName}')\n    print(f'Patient ID: {pat.PatientID}')\n    #print(f'Patient age: {pat.PatientAge}')\n    print(f'Patient Sex: {pat.PatientSex}')\n    print(f'Body part: {pat.BodyPartExamined}')\n    trans = Transform(Resize(256))\n    dicom_create = PILDicom.create(file)\n    dicom_transform = trans(dicom_create)\n    return show_image(dicom_transform)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_one_patient(patient1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from pydicom.pixel_data_handlers.util import convert_color_space","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"arr = dimg.pixel_array\nconvert = convert_color_space( arr,'RGB', 'RGB')\nshow_image(convert)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"px = dimg.pixels.flatten()\nplt.hist(px, color='c')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(source/'train.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Plot 3 comparisons\ndef plot_comparison3(df, feature, feature1, feature2):\n    \"Plot 3 comparisons from a dataframe\"\n    fig, (ax1, ax2, ax3) = plt.subplots(1,3, figsize = (16, 4))\n    s1 = sns.countplot(df[feature], ax=ax1)\n    s1.set_title(feature)\n    s2 = sns.countplot(df[feature1], ax=ax2)\n    s2.set_title(feature1)\n    s3 = sns.countplot(df[feature2], ax=ax3)\n    s3.set_title(feature2)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_comparison3(df, 'Sex', 'Age', 'SmokingStatus')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Plot 1 comparisons\ndef plot_comparison1(df, feature):\n    \"Plot 1 comparisons from a dataframe\"\n    fig, (ax1) = plt.subplots(1,1, figsize = (16, 4))\n    s1 = sns.countplot(df[feature], ax=ax1)\n    s1.set_title(feature)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_comparison1(df, 'Percent')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"eda_df = df[['Sex','Age','SmokingStatus','Percent','FVC']]\neda_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\nedaa_df = eda_df.apply(lambda col: le.fit_transform(col.astype(str)), axis=0, result_type='expand')\nedaa_df.head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.set(style=\"whitegrid\")\nsns.set_context(\"paper\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.pairplot(eda_df, hue=\"FVC\", height=5, aspect=2, palette='gist_rainbow_r')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"get_x = lambda x:source/'train'/f'{x[0]}.dcm'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"get_y=ColReader('FVC')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batch_tfms = aug_transforms(flip_vert=True, max_lighting=0.1, max_zoom=1.05, max_warp=0.)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class PILDicom2(PILBase):\n    _open_args,_tensor_cls,_show_args = {},TensorDicom,TensorDicom._show_args\n    @classmethod\n    def create(cls, fn:(Path,str,bytes), mode=None)->None:\n        \"Open a `DICOM file` from path `fn` or bytes `fn` and load it as a `PIL Image`\"\n        dimg = dcmread(fn)\n        arr = dimg.pixel_array; convert = convert_color_space(arr,'YBR_FULL_422', 'RGB')\n        im = Image.fromarray(convert)\n        im.load()\n        im = im._new(im.im)\n        return cls(im.convert(mode) if mode else im)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"blocks = (ImageBlock(cls=PILDicom2), CategoryBlock)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ct = DataBlock(blocks=blocks,\n                   get_x=get_x,\n                   splitter=RandomSplitter(),\n                   item_tfms=Resize(128),\n                   get_y=ColReader('target'),\n                   batch_tfms=batch_tfms)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls = ct.dataloaders(df.sample(100), bs=4)","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}