{"cells":[{"metadata":{"trusted":true,"_uuid":"2043566544d842f8ce98c724a10ae34a05e347f3"},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nimport pydicom\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7a75d3b90491aaa7088f2f9a5dffae1287cd2feb"},"cell_type":"code","source":"path = '../input/'\n\nclass_path = path + 'stage_1_detailed_class_info.csv'\n\nlabel_path = path + 'stage_1_train_labels.csv'\n\ntrain_img_path = path + 'stage_1_train_images/'\n\ntest_img_path = path + 'stage_1_test_images'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f45007a49286228f6e6e3a5404d450225d9a6e36"},"cell_type":"code","source":"label = pd.read_csv(label_path)\nclasses = pd.read_csv(class_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ea42a0fe1c7c387eaccf687b107bd2aec812cf10"},"cell_type":"code","source":"print(sum(label['Target']==1)/label.shape[0])  # data imbalance","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f18b58ecee373839c8ceb808c1e3f1f461865ba7"},"cell_type":"code","source":"sum(label['Target']==1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a874cce958ec14caf3f3dd2129df2965446487e6"},"cell_type":"code","source":"class_set = set(classes['class'])  # Lung Opacity = 1, otherwise = 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9b0d17f5aaa87bad96b32f50873ff5321ddaaa2f"},"cell_type":"code","source":"sum(classes['class'] == 'Lung Opacity') == sum(label['Target']==1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d42c72aa6f1e1d3d34966c8cbc595b11e6e320b8"},"cell_type":"code","source":"train_img = []\nfor root, dirs, files in os.walk(train_img_path):\n    for name in files:\n        if name[-3:] == 'dcm':\n            train_img.append(name)\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6837bb0a51fc0fc774d7af7e9b44f0f71d4635f6"},"cell_type":"code","source":"def plot_img_bbox(img, boxes_df):\n    # plot the image and bboxes\n    # Bounding boxes are defined as follows: x-min y-min width height\n    fig, a = plt.subplots(1,1)\n    fig.set_size_inches(10,10)\n    a.imshow(img, cmap = 'gray')\n    for index, row in boxes_df.iterrows():\n        x, y, width, height  = row['x'], row['y'], row['width'], row['height']\n        rect = patches.Rectangle((x, y),\n                                 width, height,\n                                 linewidth = 2,\n                                 edgecolor = 'r',\n                                 facecolor = 'none')\n\n        # Draw the bounding box on top of the image\n        a.add_patch(rect)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a039bb3d173ff8a3824871dc814cc0f1cbbd0182"},"cell_type":"code","source":"def look_img(img_class=None, pID=None):\n    if pID == None:\n        if img_class == None:\n            pID = classes['patientId'].sample(1).iloc[0]\n            img_class = classes[classes['patientId']==pID]['class']\n        else:\n            pID = classes[classes['class'] == img_class]['patientId'].sample(1).iloc[0]\n    dicom = pydicom.dcmread(train_img_path+pID+'.dcm')\n    # get the image pixels\n    img = dicom.pixel_array\n    # get the bboxes, each box is one row\n    boxes_df = label[label['patientId'] == pID]\n    plot_img_bbox(img, boxes_df)\n    # get age and gender\n    age = int(dicom.PatientAge)\n    gender = dicom.PatientSex\n    print(img_class, '\\n', age, gender, pID)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0b67d1b84b376d51e267831ac9206d2d365f8cd1"},"cell_type":"code","source":"for cls in class_set:\n    for i in range(5):\n        look_img(cls)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9dd0bcec7c773a2b583f38dcd2560515c74f5fd6"},"cell_type":"code","source":"# check how many bbox max in a img\nfrom collections import Counter\nc = Counter(label['patientId'])\nsort = sorted(c.items(), key=lambda item: item[1], reverse=True)\nsort","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9f091b2c5f651496c5b7e5c1981a637dd290baa"},"cell_type":"code","source":"look_img(pID = sort[0][0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ceebec6a70557eb233160aa57f5138eb8780ac93"},"cell_type":"code","source":"look_img(pID = sort[15][0])","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}