{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\nimport os\nimport pydicom as dicom\nfrom tqdm.notebook import trange, tqdm","metadata":{"execution":{"iopub.status.busy":"2022-07-16T21:12:27.362006Z","iopub.execute_input":"2022-07-16T21:12:27.362506Z","iopub.status.idle":"2022-07-16T21:12:27.824528Z","shell.execute_reply.started":"2022-07-16T21:12:27.362395Z","shell.execute_reply":"2022-07-16T21:12:27.823194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = [\"Abdomen\",\n         \"Ankle\",\n         \"Cervical Spine\",\n         \"Chest\",\n         \"Clavicles\",\n         \"Elbow\",\n         \"Feet\",\n         \"Finger\",\n         \"Forearm\",\n         \"Hand\",\n         \"Hip\",\n         \"Knee\",\n         \"Lower Leg\",\n         \"Lumbar Spine\",\n         \"Others\",\n         \"Pelvis\",\n         \"Shoulder\",\n         \"Sinus\",\n         \"Skull\",\n         \"Thigh\",\n         \"Thoracic Spine\",\n         \"Wrist\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T21:12:27.826872Z","iopub.execute_input":"2022-07-16T21:12:27.827969Z","iopub.status.idle":"2022-07-16T21:12:27.835342Z","shell.execute_reply.started":"2022-07-16T21:12:27.827920Z","shell.execute_reply":"2022-07-16T21:12:27.834017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = pd.read_csv(\"../input/unifesp-x-ray-body-part-classifier/train.csv\")\ntrain_ds[\"Name\"] = [label[int(x.split(\" \")[0])] for x in train_ds[\"Target\"]]\ntrain_ds.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T21:12:27.836759Z","iopub.execute_input":"2022-07-16T21:12:27.837189Z","iopub.status.idle":"2022-07-16T21:12:27.884844Z","shell.execute_reply.started":"2022-07-16T21:12:27.837146Z","shell.execute_reply":"2022-07-16T21:12:27.883969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Labels distribustion**","metadata":{}},{"cell_type":"code","source":"ax = train_ds['Name'].value_counts().plot(kind='bar',\n                                    figsize=(14,8))\nax.set_xlabel(\"Category\")\nax.set_ylabel(\"N\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T21:12:27.889138Z","iopub.execute_input":"2022-07-16T21:12:27.889957Z","iopub.status.idle":"2022-07-16T21:12:28.204245Z","shell.execute_reply.started":"2022-07-16T21:12:27.889918Z","shell.execute_reply":"2022-07-16T21:12:28.203001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def find_all(name, path=\"../input/unifesp-x-ray-body-part-classifier/train/train/\"):\n    result = []\n    for root, dirs, files in os.walk(path):\n        if name in files:\n            result.append(os.path.join(root, name))\n    if len(result) == 1:\n        return result[0]\n    elif len(result) == 0:\n        print(f'not found : {name}')\n    else :\n        print(\"More that one pic\")\n        print(result)\n\nfind_all(\"1.2.826.0.1.3680043.8.498.11373167214629703280194450289801864281-c.dcm\")","metadata":{"execution":{"iopub.status.busy":"2022-07-16T21:12:28.205859Z","iopub.execute_input":"2022-07-16T21:12:28.206345Z","iopub.status.idle":"2022-07-16T21:12:37.313292Z","shell.execute_reply.started":"2022-07-16T21:12:28.206298Z","shell.execute_reply":"2022-07-16T21:12:37.312018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"string = './test '\nfolder = \"train\"\nstring = string + f'./{folder} '\nfor lab in label:\n    string = string + f'./{folder}/\"{lab}\" '\nstring","metadata":{"execution":{"iopub.status.busy":"2022-07-16T21:12:37.314843Z","iopub.execute_input":"2022-07-16T21:12:37.315516Z","iopub.status.idle":"2022-07-16T21:12:37.323348Z","shell.execute_reply.started":"2022-07-16T21:12:37.315457Z","shell.execute_reply":"2022-07-16T21:12:37.322350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir $string","metadata":{"execution":{"iopub.status.busy":"2022-07-16T21:12:37.324648Z","iopub.execute_input":"2022-07-16T21:12:37.325284Z","iopub.status.idle":"2022-07-16T21:12:38.101157Z","shell.execute_reply.started":"2022-07-16T21:12:37.325235Z","shell.execute_reply":"2022-07-16T21:12:38.099801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls train","metadata":{"execution":{"iopub.status.busy":"2022-07-16T21:12:38.103233Z","iopub.execute_input":"2022-07-16T21:12:38.104358Z","iopub.status.idle":"2022-07-16T21:12:38.867070Z","shell.execute_reply.started":"2022-07-16T21:12:38.104303Z","shell.execute_reply":"2022-07-16T21:12:38.865548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image, ImageOps\n\n\ndef padding(img, expected_size):\n    desired_size = expected_size\n    delta_width = desired_size - img.size[0]\n    delta_height = desired_size - img.size[1]\n    pad_width = delta_width // 2\n    pad_height = delta_height // 2\n    padding = (pad_width, pad_height, delta_width - pad_width, delta_height - pad_height)\n    return ImageOps.expand(img, padding)\n\n\ndef resize_with_padding(img, expected_size):\n    img.thumbnail((expected_size[0], expected_size[1]))\n    # print(img.size)\n    delta_width = expected_size[0] - img.size[0]\n    delta_height = expected_size[1] - img.size[1]\n    pad_width = delta_width // 2\n    pad_height = delta_height // 2\n    padding = (pad_width, pad_height, delta_width - pad_width, delta_height - pad_height)\n    return ImageOps.expand(img, padding)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T21:12:38.869665Z","iopub.execute_input":"2022-07-16T21:12:38.870134Z","iopub.status.idle":"2022-07-16T21:12:38.882134Z","shell.execute_reply.started":"2022-07-16T21:12:38.870085Z","shell.execute_reply":"2022-07-16T21:12:38.881143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Plots : pre-processing**","metadata":{}},{"cell_type":"code","source":"for i in trange(10):#len(train_ds)):\n    print(\"#\"*15)\n    target = train_ds[\"Target\"].iloc[i]\n    if target.split(\" \")[0] != target[:-1]:\n        print(\"Wrong label : \")\n        print(target+\"#\"*5)\n        continue\n    file = train_ds[\"SOPInstanceUID\"].iloc[i]\n    print(f' label : {label[int(target)]}')\n    file_path = find_all(f'{file}-c.dcm')\n    #print(file_path)\n\n    ds = dicom.dcmread(file_path)\n    \n    normalized = ( ds.pixel_array - np.mean(ds.pixel_array) ) / np.std(ds.pixel_array)\n    mat  = ( normalized + 1 ) /2\n    img = Image.fromarray(np.uint8(mat * 255) , 'L')\n    print(\"Raw\")\n    plt.imshow(ds.pixel_array)\n    plt.show()\n    print(\"Normalized\")\n    plt.imshow(img)\n    plt.show()\n    padded = resize_with_padding(img,(256,256))\n    print(\"Normalized / padded 256x256\")\n    plt.imshow(padded)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T21:12:38.885474Z","iopub.execute_input":"2022-07-16T21:12:38.886071Z","iopub.status.idle":"2022-07-16T21:13:44.713334Z","shell.execute_reply.started":"2022-07-16T21:12:38.886039Z","shell.execute_reply":"2022-07-16T21:13:44.712128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Save training set**","metadata":{}},{"cell_type":"code","source":"for i in trange(len(train_ds)):\n    target = train_ds[\"Target\"].iloc[i]\n    if target.split(\" \")[0] != target[:-1]:\n        continue\n    file = train_ds[\"SOPInstanceUID\"].iloc[i]\n    file_path = find_all(f'{file}-c.dcm')\n\n    ds = dicom.dcmread(file_path)\n    \n    normalized = ( ds.pixel_array - np.mean(ds.pixel_array) ) / np.std(ds.pixel_array)\n    mat  = ( normalized + 1 ) /2\n    img = Image.fromarray(np.uint8(mat * 255) , 'L')\n    padded = resize_with_padding(img,(256,256))\n    padded.save(f'./train/{label[int(target)]}/{i}.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T21:18:02.295222Z","iopub.execute_input":"2022-07-16T21:18:02.295797Z","iopub.status.idle":"2022-07-16T21:18:32.111691Z","shell.execute_reply.started":"2022-07-16T21:18:02.295754Z","shell.execute_reply":"2022-07-16T21:18:32.110098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Save testing set**","metadata":{}},{"cell_type":"code","source":"test_ds = pd.read_csv(\"../input/unifesp-x-ray-body-part-classifier/sample_submission.csv\")\ntest_ds.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T21:18:38.586696Z","iopub.execute_input":"2022-07-16T21:18:38.587495Z","iopub.status.idle":"2022-07-16T21:18:38.604850Z","shell.execute_reply.started":"2022-07-16T21:18:38.587456Z","shell.execute_reply":"2022-07-16T21:18:38.603789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in trange(len(test_ds)):\n    file = test_ds[\"SOPInstanceUID\"].iloc[i]\n    file_path = find_all(f'{file}-c.dcm',path=\"../input/unifesp-x-ray-body-part-classifier/test/test/\")\n\n    ds = dicom.dcmread(file_path)\n    \n    normalized = ( ds.pixel_array - np.mean(ds.pixel_array) ) / np.std(ds.pixel_array)\n    mat  = ( normalized + 1 ) /2\n    img = Image.fromarray(np.uint8(mat * 255) , 'L')\n    padded = resize_with_padding(img,(256,256))\n    padded.save(f'./test/{i}.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T21:18:39.065661Z","iopub.execute_input":"2022-07-16T21:18:39.066032Z","iopub.status.idle":"2022-07-16T21:18:57.196749Z","shell.execute_reply.started":"2022-07-16T21:18:39.066003Z","shell.execute_reply":"2022-07-16T21:18:57.195342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"note that the name of the image is the index i of the dataframe in sample_submission.csv","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}