{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import os\n\nimport numpy as np\nimport pandas as pd\nimport pydicom\n\nfrom skimage.measure import label,regionprops\nfrom skimage.segmentation import clear_border\nfrom tqdm.notebook import tqdm \nfrom multiprocessing import Pool\n\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class Detector:\n    def __call__(self, x):\n        raise NotImplementedError('Abstract') \n\nclass ThrDetector(Detector):\n    def __init__(self, thr=-400):\n        self.thr = thr\n        \n    def __call__(self, x):\n        x = pydicom.dcmread(x)\n        img = x.pixel_array\n        img = (img + x.RescaleIntercept) / x.RescaleSlope\n        img = img < self.thr\n        \n        img = clear_border(img)\n        img = label(img)\n        areas = [r.area for r in regionprops(img)]\n        areas.sort()\n        if len(areas) > 2:\n            for region in regionprops(img):\n                if region.area < areas[-2]:\n                    for coordinates in region.coords:                \n                        img[coordinates[0], coordinates[1]] = 0\n        img = img > 0\n        return np.int32(img)\n  \n\nclass Integral:\n    def __init__(self, detector: Detector):\n        self.detector = detector\n    \n    def __call__(self, xs):\n        raise NotImplementedError('Abstract')\n        \n\nclass MeanIntegral(Integral):\n    def __call__(self, xs):\n        with Pool(4) as p:\n            masks = p.map(self.detector, xs) \n        return np.mean(masks)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/osic-pulmonary-fibrosis-progression/train.csv')\ntrain_data = {}\nfor p in train.Patient.values:\n    train_data[p] = os.listdir(f'../input/osic-pulmonary-fibrosis-progression/train/{p}/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"keys = [k for k in list(train_data.keys()) if k not in ['ID00011637202177653955184', 'ID00052637202186188008618']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"integral = MeanIntegral(ThrDetector()) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"volume = {}\nfor k in tqdm(keys, total=len(keys)):\n    x = []\n    for i in train_data[k]:\n        x.append(f'../input/osic-pulmonary-fibrosis-progression/train/{k}/{i}') \n    volume[k] = integral(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for k in tqdm(train.Patient.values):\n    if k in ['ID00011637202177653955184', 'ID00052637202186188008618']:\n        continue\n    train.loc[train.Patient == k,'v'] = volume[k]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(10, 10))\n\nplt.plot(train.v, train.FVC, '.')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(10, 10))\n\nplt.plot(train.v, train.Percent, '.')","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}