{"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":"#common packages \nimport numpy as np\nimport os\nimport copy\nfrom math import *\nimport matplotlib.pyplot as plt\nfrom functools import reduce\nfrom glob import glob\n\n#reading in dicom files\nimport pydicom","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-20T16:39:40.758113Z","iopub.execute_input":"2021-07-20T16:39:40.758601Z","iopub.status.idle":"2021-07-20T16:39:41.133314Z","shell.execute_reply.started":"2021-07-20T16:39:40.758512Z","shell.execute_reply":"2021-07-20T16:39:41.132203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def check_data(path):\n    length = []\n    folders = glob(os.path.join(path,'*'))\n    for folder in folders:\n        mods = glob(os.path.join(folder, '*'))\n        if (len(mods) != 4):\n            print(mods)\n        for mod in mods:\n            length.append(len(glob(os.path.join(mod,'*.png'))))\n    return length","metadata":{"execution":{"iopub.status.busy":"2021-07-20T16:17:34.514483Z","iopub.execute_input":"2021-07-20T16:17:34.514919Z","iopub.status.idle":"2021-07-20T16:17:34.521118Z","shell.execute_reply.started":"2021-07-20T16:17:34.514879Z","shell.execute_reply":"2021-07-20T16:17:34.520147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = np.array(check_data('../input/rsna-miccai-png/test'))\ntrain = np.array(check_data('../input/rsna-miccai-png/train'))","metadata":{"execution":{"iopub.status.busy":"2021-07-20T16:17:34.522931Z","iopub.execute_input":"2021-07-20T16:17:34.5234Z","iopub.status.idle":"2021-07-20T16:18:51.961437Z","shell.execute_reply.started":"2021-07-20T16:17:34.523337Z","shell.execute_reply":"2021-07-20T16:18:51.960576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(train)","metadata":{"execution":{"iopub.status.busy":"2021-07-20T16:18:51.962778Z","iopub.execute_input":"2021-07-20T16:18:51.963098Z","iopub.status.idle":"2021-07-20T16:18:52.145526Z","shell.execute_reply.started":"2021-07-20T16:18:51.963064Z","shell.execute_reply":"2021-07-20T16:18:52.144593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(test)","metadata":{"execution":{"iopub.status.busy":"2021-07-20T16:18:52.14692Z","iopub.execute_input":"2021-07-20T16:18:52.147268Z","iopub.status.idle":"2021-07-20T16:18:52.282719Z","shell.execute_reply.started":"2021-07-20T16:18:52.147231Z","shell.execute_reply":"2021-07-20T16:18:52.281823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import animation, rc\nrc('animation', html='jshtml')\n\ndef create_animation(ims):\n    fig = plt.figure(figsize=(6, 6))\n    plt.axis('off')\n    im = plt.imshow(ims[0], cmap=\"gray\")\n\n    def animate_func(i):\n        im.set_array(ims[i])\n        return [im]\n\n    return animation.FuncAnimation(fig, animate_func, frames = len(ims), interval = 1000//24)\n\ndef get_index(path):\n    start = path.rfind('-')\n    end = path.rfind('.png')\n    return int(path[start+1: end])\n\ndef load_image(path):\n    ims = []\n    img_paths = glob(os.path.join(path, '*.png'))\n    img_paths = sorted(img_paths, key = lambda x: get_index(x))\n    for i in img_paths:\n        img = (plt.imread(i, ) * 255).astype(np.uint8)\n        ims.append(img)\n        print(img.max())\n    return np.asarray(ims)","metadata":{"execution":{"iopub.status.busy":"2021-07-20T16:42:38.902774Z","iopub.execute_input":"2021-07-20T16:42:38.903209Z","iopub.status.idle":"2021-07-20T16:42:38.914649Z","shell.execute_reply.started":"2021-07-20T16:42:38.903178Z","shell.execute_reply":"2021-07-20T16:42:38.913205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_image('../input/rsna-miccai-png/test/00001/FLAIR')\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2021-07-20T16:42:39.455263Z","iopub.execute_input":"2021-07-20T16:42:39.455638Z","iopub.status.idle":"2021-07-20T16:42:48.670151Z","shell.execute_reply.started":"2021-07-20T16:42:39.455596Z","shell.execute_reply":"2021-07-20T16:42:48.668971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install torchio","metadata":{"execution":{"iopub.status.busy":"2021-07-20T10:38:53.262654Z","iopub.execute_input":"2021-07-20T10:38:53.263027Z","iopub.status.idle":"2021-07-20T10:39:07.301338Z","shell.execute_reply.started":"2021-07-20T10:38:53.262997Z","shell.execute_reply":"2021-07-20T10:39:07.300171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import skimage.transform as skTrans","metadata":{"execution":{"iopub.status.busy":"2021-07-20T10:56:04.375473Z","iopub.execute_input":"2021-07-20T10:56:04.375816Z","iopub.status.idle":"2021-07-20T10:56:05.03202Z","shell.execute_reply.started":"2021-07-20T10:56:04.375784Z","shell.execute_reply":"2021-07-20T10:56:05.031207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result1 = skTrans.resize(images, (64,256,256), order=1, preserve_range=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-20T10:56:35.168308Z","iopub.execute_input":"2021-07-20T10:56:35.16866Z","iopub.status.idle":"2021-07-20T10:56:37.346981Z","shell.execute_reply.started":"2021-07-20T10:56:35.168627Z","shell.execute_reply":"2021-07-20T10:56:37.34612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_animation(result1)","metadata":{"execution":{"iopub.status.busy":"2021-07-20T10:56:59.073795Z","iopub.execute_input":"2021-07-20T10:56:59.074125Z","iopub.status.idle":"2021-07-20T10:57:01.327941Z","shell.execute_reply.started":"2021-07-20T10:56:59.074095Z","shell.execute_reply":"2021-07-20T10:57:01.326967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir test\n!mkdir train\n!git clone https://github.com/shigure3011/BrainTumorClassification.git\n%cd BrainTumorClassification","metadata":{"execution":{"iopub.status.busy":"2021-07-21T01:40:09.185043Z","iopub.execute_input":"2021-07-21T01:40:09.185424Z","iopub.status.idle":"2021-07-21T01:40:10.693202Z","shell.execute_reply.started":"2021-07-21T01:40:09.185327Z","shell.execute_reply":"2021-07-21T01:40:10.692279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile converter.py\nimport argparse, glob, os\nimport logging, pickle, bz2\n\nimport numpy as np\nimport skimage.transform as skTrans\nimport matplotlib.pyplot as plt\n\n\n# Config logging\nlogging.basicConfig(format=\"%(levelname)s - %(message)s\", level=logging.INFO)\n\n\n# Add parser\nparser = argparse.ArgumentParser()\n\nparser.add_argument('--img_dir', default='/content/drive/MyDrive/Data/test', type=str)\n\nparser.add_argument('--out_dir', default='/content/drive/MyDrive/BrainTumorClassification/test', type=str)\n\ndef shape(s):\n    try:\n        x, y, z = map(int, s.split(','))\n        return (x, y, z)\n    except:\n        raise argparse.ArgumentTypeError(\"Shape must have 3 values (C, H, W)\")\n\nparser.add_argument('--img_shape', default=(1,512,512), type=shape)\n\nparser.add_argument('--out_shape', default=(64,256,256), type=shape)\n\nargs = parser.parse_args()\n\n\n# Codes start here\ndef convert_to_pkl(img_dir: str, output_dir: str, img_shape: tuple, output_shape: tuple) -> None:\n    assert os.path.exists(img_dir), \"{} does not exist.\".format(img_dir)\n    Co, Ho, Wo = output_shape\n    print('Output shape: {}'.format(output_shape))\n\n    try:\n        assert os.path.exists(output_dir)\n    except AssertionError as e:\n        output_dir = img_dir\n        logging.error(\"{0} does not exist, save output to {1}\".format(output_dir, img_dir))\n    \n    assert all(i > 0 for i in output_shape), \"Output shape must have positive values.\"\n\n    cases = get_filenames(img_dir)\n    assert len(cases) > 0, \"{} is empty.\".format(img_dir)\n\n    n = len(cases)\n    for it in range(n):\n        case_dir = cases[it]\n        images = load_case(case_dir, img_shape, output_shape)\n        assert images.shape == (4, Co, Ho, Wo),\\\n            \"Case {0} has incorrect shape {1}, which is different from {2}\"\\\n            .format(case_dir, images.shape, (4, Co, Ho, Wo))\n\n        save_case(images, case_dir, output_dir)\n        print(\"[{1}/{2}] Saved {0}\".format(\n            os.path.join(output_dir, extract_folder_name(case_dir)), \n            it + 1, n\n        ))\n\n    logging.info('Converting completed.')\n\n\ndef get_filenames(path: str) -> list:\n    files = glob.glob(os.path.join(path, '*'))\n    return sorted(files)\n\n\ndef load_case(case_dir: str, img_shape: tuple, output_shape: tuple) -> np.ndarray:\n    modals = ['FLAIR', 'T1w', 'T1wCE', 'T2w']\n    Co, Ho, Wo = output_shape\n\n    images = np.array([], dtype=np.uint8).reshape(0, Co, Ho, Wo)\n\n    for modal in modals:\n        modal_path = os.path.join(case_dir, modal)\n        modal_img = load_modal(modal_path, img_shape, output_shape)\n        assert modal_img.shape == (1, Co, Ho, Wo),\\\n            \"Modal {0} has incorrect shape {1}, which is different from {2}\"\\\n            .format(modal_path, modal_img.shape, (1, Co, Ho, Wo))\n        images = np.concatenate((images, modal_img), axis = 0)\n\n    return images\n\n\ndef save_case(images: np.ndarray, case_dir: str, output_dir: str) -> None:\n    output_name = extract_folder_name(case_dir) + '.pkl.bz2'\n    output_path = os.path.join(output_dir, output_name)\n    \n    with bz2.open(output_path, 'wb') as f:\n        pickle.dump(images, f)\n\n\ndef load_modal(path: str, img_shape: tuple, output_shape: tuple) -> np.ndarray:\n    if not os.path.exists(path) or len(get_filenames(path)) == 0:\n        logging.info('{} does not contain images, paddad by zeros'.format(path))\n        return np.zeros(output_shape, dtype=np.uint8)[None, :]\n\n    images = load_images(path, img_shape)\n\n    assert len(images.shape) == 3,\\\n        \"Images {} must have shape of length 3\".format(path)\n\n    images = skTrans.resize(images, output_shape, order=1, preserve_range=True)\n    logging.info('Loaded {}'.format(path))\n    return images[None, :]\n\n\ndef load_images(path: str, img_shape: tuple) -> np.ndarray:\n    Ci, Hi, Wi = img_shape\n\n    imgs = np.array([], dtype=np.uint8).reshape(0, Hi, Wi)\n    img_paths = glob.glob(os.path.join(path, '*.png'))\n    img_paths = sorted(img_paths, key = lambda x: get_index(x))\n\n    for img_path in img_paths:\n        img = plt.imread(img_path)\n\n        if (Hi, Wi) != img.shape:\n            img = skTrans.resize(img, (Hi, Wi), order=1, preserve_range=True)\n        assert (Hi, Wi) == img.shape, \\\n            \"{0} has size {1}, which is different from {2}\".format(img_path, img.shape, (Wi, Hi))\n\n        img = transform(img)\n        imgs = np.concatenate((imgs, img), axis = 0)\n        logging.debug('Loaded {0} - Current shape {1}'.format(img_path, imgs.shape))\n\n    return imgs\n\n\ndef extract_folder_name(path: str)-> str:\n    return path[(path.rfind('/') + 1):]\n\n\ndef get_index(path):\n    start = path.rfind('-')\n    end = path.rfind('.png')\n    return int(path[start+1: end])\n\n\ndef transform(img: np.ndarray) -> np.ndarray:\n    output = (img * 255).astype(np.uint8)\n    return output[None, :]\n\n\nif __name__ == \"__main__\":\n    convert_to_pkl(args.img_dir, args.out_dir, args.img_shape, args.out_shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T01:40:51.486992Z","iopub.execute_input":"2021-07-21T01:40:51.487342Z","iopub.status.idle":"2021-07-21T01:40:51.496638Z","shell.execute_reply.started":"2021-07-21T01:40:51.487308Z","shell.execute_reply":"2021-07-21T01:40:51.495463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python converter.py --img_dir /kaggle/input/rsna-miccai-png/train --out_dir ../train","metadata":{"execution":{"iopub.status.busy":"2021-07-21T01:40:58.685281Z","iopub.execute_input":"2021-07-21T01:40:58.685699Z","iopub.status.idle":"2021-07-21T05:38:06.167581Z","shell.execute_reply.started":"2021-07-21T01:40:58.685664Z","shell.execute_reply":"2021-07-21T05:38:06.166562Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle, bz2","metadata":{"execution":{"iopub.status.busy":"2021-07-21T05:41:21.666298Z","iopub.execute_input":"2021-07-21T05:41:21.666751Z","iopub.status.idle":"2021-07-21T05:41:21.672316Z","shell.execute_reply.started":"2021-07-21T05:41:21.66671Z","shell.execute_reply":"2021-07-21T05:41:21.67145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom matplotlib import animation, rc\nrc('animation', html='jshtml')\n\ndef create_animation(ims):\n    fig = plt.figure(figsize=(6, 6))\n    plt.axis('off')\n    im = plt.imshow(ims[0], cmap=\"gray\")\n    \n    def animate_func(i):\n        im.set_array(ims[i])\n        return [im]\n\n    return animation.FuncAnimation(fig, animate_func, frames = len(ims), interval = 1000//24)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T05:41:22.17812Z","iopub.execute_input":"2021-07-21T05:41:22.178449Z","iopub.status.idle":"2021-07-21T05:41:22.184339Z","shell.execute_reply.started":"2021-07-21T05:41:22.178417Z","shell.execute_reply":"2021-07-21T05:41:22.183539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = pickle.load(bz2.open('../train/00772.pkl.bz2', 'rb'))","metadata":{"execution":{"iopub.status.busy":"2021-07-21T05:41:24.372018Z","iopub.execute_input":"2021-07-21T05:41:24.372328Z","iopub.status.idle":"2021-07-21T05:41:25.831678Z","shell.execute_reply.started":"2021-07-21T05:41:24.372299Z","shell.execute_reply":"2021-07-21T05:41:25.830826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_animation(a[0])","metadata":{"execution":{"iopub.status.busy":"2021-07-21T05:41:26.708128Z","iopub.execute_input":"2021-07-21T05:41:26.708458Z","iopub.status.idle":"2021-07-21T05:41:29.306416Z","shell.execute_reply.started":"2021-07-21T05:41:26.708425Z","shell.execute_reply":"2021-07-21T05:41:29.30546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r train.zip ../train","metadata":{"execution":{"iopub.status.busy":"2021-07-21T05:42:13.906928Z","iopub.execute_input":"2021-07-21T05:42:13.907275Z","iopub.status.idle":"2021-07-21T05:44:40.675852Z","shell.execute_reply.started":"2021-07-21T05:42:13.907242Z","shell.execute_reply":"2021-07-21T05:44:40.674884Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href=\"BrainTumorClassification/train.zip\">Download</a>","metadata":{}},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2021-07-21T05:46:13.532961Z","iopub.execute_input":"2021-07-21T05:46:13.533325Z","iopub.status.idle":"2021-07-21T05:46:14.171595Z","shell.execute_reply.started":"2021-07-21T05:46:13.533291Z","shell.execute_reply":"2021-07-21T05:46:14.170559Z"},"trusted":true},"execution_count":null,"outputs":[]}]}