{"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":"#!pip install fastbook torch==1.8.1 pyarrow pydicom kornia opencv-python scikit-image","metadata":{"execution":{"iopub.status.busy":"2022-03-18T08:35:51.422358Z","iopub.execute_input":"2022-03-18T08:35:51.422638Z","iopub.status.idle":"2022-03-18T08:35:51.427158Z","shell.execute_reply.started":"2022-03-18T08:35:51.422604Z","shell.execute_reply":"2022-03-18T08:35:51.426394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --user torch==1.9.0 torchvision==0.10.0 torchaudio==0.9.0 torchtext==0.10.0 pyarrow pydicom kornia opencv-python scikit-image","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:51:56.652035Z","iopub.execute_input":"2022-03-18T09:51:56.652735Z","iopub.status.idle":"2022-03-18T09:52:04.166641Z","shell.execute_reply.started":"2022-03-18T09:51:56.652697Z","shell.execute_reply":"2022-03-18T09:52:04.165807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.basics import *\nfrom fastai.callback.all import *\nfrom fastai.vision.all import *\nfrom fastai.medical.imaging import *\n\nimport pydicom","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:04.168969Z","iopub.execute_input":"2022-03-18T09:52:04.169257Z","iopub.status.idle":"2022-03-18T09:52:04.176767Z","shell.execute_reply.started":"2022-03-18T09:52:04.16922Z","shell.execute_reply":"2022-03-18T09:52:04.176045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ../input","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:04.177902Z","iopub.execute_input":"2022-03-18T09:52:04.178549Z","iopub.status.idle":"2022-03-18T09:52:04.846678Z","shell.execute_reply.started":"2022-03-18T09:52:04.178511Z","shell.execute_reply":"2022-03-18T09:52:04.845697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_csv = Path(\"../input/meduni-ich-labels-sample/gesamt_labels.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:04.850235Z","iopub.execute_input":"2022-03-18T09:52:04.850466Z","iopub.status.idle":"2022-03-18T09:52:04.855201Z","shell.execute_reply.started":"2022-03-18T09:52:04.850437Z","shell.execute_reply":"2022-03-18T09:52:04.854487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_labels_all = pd.read_csv(path_csv)","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:04.85676Z","iopub.execute_input":"2022-03-18T09:52:04.857188Z","iopub.status.idle":"2022-03-18T09:52:04.876297Z","shell.execute_reply.started":"2022-03-18T09:52:04.857138Z","shell.execute_reply":"2022-03-18T09:52:04.875631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_labels_all","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:04.877488Z","iopub.execute_input":"2022-03-18T09:52:04.877726Z","iopub.status.idle":"2022-03-18T09:52:04.892441Z","shell.execute_reply.started":"2022-03-18T09:52:04.87769Z","shell.execute_reply":"2022-03-18T09:52:04.891665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# hier können Daten von individuellen Studenten gefiltert werden\n# falls ihr ALLE Bilder nehmen wollt: setzt die student_id auf eine negative Zahl (zB -1)\nstudent_ids= [-1]\n\n# mask = (df_labels_all[\"Student_ID\"] == student_ids\nmask = df_labels_all[\"Student_ID\"].isin(student_ids)\n\ndf_labels = df_labels_all[mask]\nn = len(df_labels)\nif n:\n    print(f\"Studenten Nr {student_ids} => {len(df_labels)} Labels\\n\")\n    df_labels\n\nelse:\n    df_labels = df_labels_all\n    print(f\"Studenten Nr {student_ids} => keine Labels gefunden\")\n    print(f\"daher nehmen wir alle verfügbaren Labels: {len(df_labels)} Labels\\n\")","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:04.893846Z","iopub.execute_input":"2022-03-18T09:52:04.894102Z","iopub.status.idle":"2022-03-18T09:52:04.902821Z","shell.execute_reply.started":"2022-03-18T09:52:04.89407Z","shell.execute_reply":"2022-03-18T09:52:04.902064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hier könnt ihr angeben wie die verwendeten Labels für das Neuronale Netz heißen sollen\n# zB\n# Positiv/Negativ\n# Blutung/keine Blutung\n\nlabel_mapping = {\n    0: \"Keine Blutung\",\n    1: \"Gehirnblutung\",\n}\n\nlabel2digit = {\n    v: k for (k, v) in label_mapping.items()\n}","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:04.903955Z","iopub.execute_input":"2022-03-18T09:52:04.904772Z","iopub.status.idle":"2022-03-18T09:52:04.912638Z","shell.execute_reply.started":"2022-03-18T09:52:04.904735Z","shell.execute_reply":"2022-03-18T09:52:04.911796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_data = Path(\"../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train/\")\nassert path_data.exists()","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:04.91386Z","iopub.execute_input":"2022-03-18T09:52:04.914654Z","iopub.status.idle":"2022-03-18T09:52:04.92433Z","shell.execute_reply.started":"2022-03-18T09:52:04.914617Z","shell.execute_reply":"2022-03-18T09:52:04.923649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fastai überprüft normalerweise ob die Dateien wirklich korrekt sind\n# das macht das Laden der DICOMs ziemlich langsam\n# zudem verwenden wir hier ein professionell erstelltes Datenset\n# daher beschleunigen wir das Laden der Daten indem wir hier die eingebauten Funktionen von fastai durch unsere eigenen ersetzen\n# zudem bauen wir ein Limit ein falls wir nicht alle DICOMs laden wollen\n\ndef get_files(path, extensions=None, folders=None, followlinks=True, limit=None):\n    \"Get all the files in `path` with optional `extensions`, optionally with `recurse`, only in `folders`, if specified.\"\n    path = Path(path)\n    folders=L(folders)\n    extensions = setify(extensions)\n    extensions = {e.lower() for e in extensions}    \n    \n#   f = [o.name for o in os.scandir(path) if o.is_file()]  # hier überprüft das Original ob es wirklich Dateien sind\n    f = [o.name for o in os.scandir(path)]\n    if limit:\n        f = f[:limit]\n            \n    res = _get_files(path, f, extensions)\n    \n    return L(res)\n\n\ndef _get_files(p, fs, extensions=None):\n    p = Path(p)\n    res = [p/f for f in fs if not f.startswith('.')\n           and ((not extensions) or f'.{f.split(\".\")[-1].lower()}' in extensions)]\n    return res\n\n\ndef get_dicom_files(path, folders=None, limit=None):\n    \"Get dicom files in `path` recursively, only in `folders`, if specified.\"\n    return get_files(path, extensions=[\".dcm\",\".dicom\"], folders=folders, limit=limit)","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:04.926539Z","iopub.execute_input":"2022-03-18T09:52:04.927144Z","iopub.status.idle":"2022-03-18T09:52:04.936965Z","shell.execute_reply.started":"2022-03-18T09:52:04.927106Z","shell.execute_reply":"2022-03-18T09:52:04.936315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\n# das laden aller DICOMs dauert trotzdem etwas\ndicoms = get_dicom_files(path_data, limit=None)","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:05.142209Z","iopub.execute_input":"2022-03-18T09:52:05.142477Z","iopub.status.idle":"2022-03-18T09:52:27.0498Z","shell.execute_reply.started":"2022-03-18T09:52:05.142448Z","shell.execute_reply":"2022-03-18T09:52:27.049063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicoms","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:27.051782Z","iopub.execute_input":"2022-03-18T09:52:27.05228Z","iopub.status.idle":"2022-03-18T09:52:27.058553Z","shell.execute_reply.started":"2022-03-18T09:52:27.052242Z","shell.execute_reply":"2022-03-18T09:52:27.05779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = dicoms[1].dcmread()","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:27.059922Z","iopub.execute_input":"2022-03-18T09:52:27.060195Z","iopub.status.idle":"2022-03-18T09:52:27.079122Z","shell.execute_reply.started":"2022-03-18T09:52:27.060142Z","shell.execute_reply":"2022-03-18T09:52:27.078475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:27.080993Z","iopub.execute_input":"2022-03-18T09:52:27.081311Z","iopub.status.idle":"2022-03-18T09:52:27.297892Z","shell.execute_reply.started":"2022-03-18T09:52:27.081276Z","shell.execute_reply":"2022-03-18T09:52:27.297162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class BrainWindow(PILBase):\n    _open_args = {}\n    _tensor_cls = TensorDicom\n    _show_args = TensorDicom._show_args\n    \n    @classmethod\n    def create(cls, fn:(Path,str,bytes), mode=None) -> None:\n        if isinstance(fn,bytes):\n            im = pydicom.dcmread(pydicom.filebase.DicomBytesIO(fn))\n        elif isinstance(fn,(Path,str)):\n            im = Path(fn).dcmread()\n            \n        scaled = np.array(im.windowed(l=40, w=80).numpy()) * 255\n        scaled = scaled.astype(np.uint8)\n        \n        return cls(Image.fromarray(scaled))","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:27.298828Z","iopub.execute_input":"2022-03-18T09:52:27.299092Z","iopub.status.idle":"2022-03-18T09:52:27.306691Z","shell.execute_reply.started":"2022-03-18T09:52:27.299049Z","shell.execute_reply":"2022-03-18T09:52:27.306045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ich_datablock = DataBlock(\n    blocks=(ImageBlock(cls=BrainWindow), CategoryBlock),\n    get_x=lambda df: (path_data/f\"{df[0]}\").with_suffix(\".dcm\"),\n    get_y=lambda df:label_mapping[df[1]],\n    batch_tfms=[*aug_transforms(size=224), Normalize.from_stats(*imagenet_stats)],\n    splitter=RandomSplitter(valid_pct=0.2, seed=42)\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:27.30789Z","iopub.execute_input":"2022-03-18T09:52:27.30876Z","iopub.status.idle":"2022-03-18T09:52:30.052362Z","shell.execute_reply.started":"2022-03-18T09:52:27.308722Z","shell.execute_reply":"2022-03-18T09:52:30.051602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataloader = ich_datablock.dataloaders(df_labels[[\"Datei\", \"Label\"]].values, num_workers=1) #, bs=64)","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:30.05375Z","iopub.execute_input":"2022-03-18T09:52:30.053992Z","iopub.status.idle":"2022-03-18T09:52:30.279592Z","shell.execute_reply.started":"2022-03-18T09:52:30.053958Z","shell.execute_reply":"2022-03-18T09:52:30.278678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size_train = len(dataloader.train) * dataloader.train.bs\nsize_valid = len(dataloader.valid) * dataloader.valid.bs\n\nprint(f\"Trainingsdatenset enthält {size_train} dicoms\")\nprint(f\"Validierungsdatenset enthält {size_valid} dicoms\")","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:30.281003Z","iopub.execute_input":"2022-03-18T09:52:30.281782Z","iopub.status.idle":"2022-03-18T09:52:30.287981Z","shell.execute_reply.started":"2022-03-18T09:52:30.28174Z","shell.execute_reply":"2022-03-18T09:52:30.287189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataloader.train.show_batch()","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:30.289303Z","iopub.execute_input":"2022-03-18T09:52:30.290012Z","iopub.status.idle":"2022-03-18T09:52:32.501657Z","shell.execute_reply.started":"2022-03-18T09:52:30.289973Z","shell.execute_reply":"2022-03-18T09:52:32.500987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learner = cnn_learner(dataloader, resnet34, metrics=accuracy)","metadata":{"execution":{"iopub.status.busy":"2022-03-18T10:04:56.430094Z","iopub.execute_input":"2022-03-18T10:04:56.43066Z","iopub.status.idle":"2022-03-18T10:04:56.845385Z","shell.execute_reply.started":"2022-03-18T10:04:56.430622Z","shell.execute_reply":"2022-03-18T10:04:56.844649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# hier könnt ihr einstellen wie viele Epochen ihr trainieren wollt\n# (am besten zuerst mit 1 Epoche ausprobieren um Zeit einschätzen zu können)\nlearner.fine_tune(5)","metadata":{"execution":{"iopub.status.busy":"2022-03-18T10:04:58.237316Z","iopub.execute_input":"2022-03-18T10:04:58.237573Z","iopub.status.idle":"2022-03-18T10:10:06.929898Z","shell.execute_reply.started":"2022-03-18T10:04:58.237543Z","shell.execute_reply":"2022-03-18T10:10:06.92904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = Interpretation.from_learner(learner)","metadata":{"execution":{"iopub.status.busy":"2022-03-18T10:10:06.931872Z","iopub.execute_input":"2022-03-18T10:10:06.932316Z","iopub.status.idle":"2022-03-18T10:10:16.639025Z","shell.execute_reply.started":"2022-03-18T10:10:06.932275Z","shell.execute_reply":"2022-03-18T10:10:16.638211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fastai's plot_top_losses Funktion ist momentan kaputt, darum hier unsere eigene Version\ndef plot_top_losses_fix(interp, k, largest=True, **kwargs):\n        losses, idx = interp.top_losses(k, largest)\n        \n        if not isinstance(interp.inputs, tuple): \n            interp.inputs = (interp.inputs,)\n            \n        if isinstance(interp.inputs[0], Tensor):\n            inps = tuple(o[idx] for o in interp.inputs)\n            \n        else:\n            inps = interp.dl.create_batch(interp.dl.before_batch([tuple(o[i] for o in interp.inputs) for i in idx]))\n            \n        b = inps + tuple(o[idx] for o in (interp.targs if is_listy(interp.targs) else (interp.targs,)))\n        \n        x,y,its = interp.dl._pre_show_batch(b, max_n=k)\n        \n        b_out = inps + tuple(o[idx] for o in (interp.decoded if is_listy(interp.decoded) else (interp.decoded,)))\n        \n        x1,y1,outs = interp.dl._pre_show_batch(b_out, max_n=k)\n        \n        if its is not None:\n            #plot_top_losses(x, y, its, outs.itemgot(slice(len(inps), None)), L(self.preds).itemgot(idx), losses,  **kwargs)\n            plot_top_losses(x, y, its, outs.itemgot(slice(len(inps), None)), interp.preds[idx], losses,  **kwargs)\n        #TODO: figure out if this is needed\n        #its None means that a batch knows how to show itself as a whole, so we pass x, x1\n        #else: show_results(x, x1, its, ctxs=ctxs, max_n=max_n, **kwargs)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-18T10:10:16.640678Z","iopub.execute_input":"2022-03-18T10:10:16.640924Z","iopub.status.idle":"2022-03-18T10:10:16.653824Z","shell.execute_reply.started":"2022-03-18T10:10:16.640886Z","shell.execute_reply":"2022-03-18T10:10:16.653068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# zeig Bilder wo Vorhersagen \"am meisten falsch\" waren\nplot_top_losses_fix(interp, 9, figsize=(15, 15))","metadata":{"execution":{"iopub.status.busy":"2022-03-18T08:49:40.461264Z","iopub.execute_input":"2022-03-18T08:49:40.461564Z","iopub.status.idle":"2022-03-18T08:49:41.792481Z","shell.execute_reply.started":"2022-03-18T08:49:40.461477Z","shell.execute_reply":"2022-03-18T08:49:41.791679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_test_set = Path(\"../input/meduni-ich-labels-sample/test_fixed.csv\")\nassert path_test_set.exists()","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:52:35.170357Z","iopub.execute_input":"2022-03-18T09:52:35.17078Z","iopub.status.idle":"2022-03-18T09:52:35.178142Z","shell.execute_reply.started":"2022-03-18T09:52:35.170741Z","shell.execute_reply":"2022-03-18T09:52:35.177451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !cat {path_test_set} | sed -E \"s/(dcm);([01])/\\1:\\2/\" | sed \"s/;//g\" | sed 's/\"//g' | sed -E \":a;N;$!ba;s/dcm\\n/dcm:/g\"","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:46:17.32236Z","iopub.execute_input":"2022-03-18T09:46:17.322649Z","iopub.status.idle":"2022-03-18T09:46:17.327238Z","shell.execute_reply.started":"2022-03-18T09:46:17.322618Z","shell.execute_reply":"2022-03-18T09:46:17.326526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_testset = pd.read_csv(path_test_set, header=None, sep=\";\")\ndf_testset.columns = [\"Datei\", \"Label\"]\ndf_testset.set_index(\"Datei\", drop=False, inplace=True)\ndf_testset.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:55:41.396747Z","iopub.execute_input":"2022-03-18T09:55:41.397014Z","iopub.status.idle":"2022-03-18T09:55:41.411117Z","shell.execute_reply.started":"2022-03-18T09:55:41.396983Z","shell.execute_reply":"2022-03-18T09:55:41.410124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_test_dataset(n=200):\n    return list(df_testset[\"Datei\"])\n\n#     return list(df_testset.sample(n)[\"Datei\"])\n#     return list(df_labels[\"Datei\"])[:200]","metadata":{"execution":{"iopub.status.busy":"2022-03-18T10:03:30.492239Z","iopub.execute_input":"2022-03-18T10:03:30.492684Z","iopub.status.idle":"2022-03-18T10:03:30.496215Z","shell.execute_reply.started":"2022-03-18T10:03:30.492648Z","shell.execute_reply":"2022-03-18T10:03:30.495552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# anzahl_test_dicoms = 20\n\ntest_set = get_test_dataset(anzahl_test_dicoms)\nlen(test_set)","metadata":{"execution":{"iopub.status.busy":"2022-03-18T10:13:06.042678Z","iopub.execute_input":"2022-03-18T10:13:06.042943Z","iopub.status.idle":"2022-03-18T10:13:06.049742Z","shell.execute_reply.started":"2022-03-18T10:13:06.042912Z","shell.execute_reply":"2022-03-18T10:13:06.049046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set[:5]","metadata":{"execution":{"iopub.status.busy":"2022-03-18T10:03:39.897541Z","iopub.execute_input":"2022-03-18T10:03:39.897795Z","iopub.status.idle":"2022-03-18T10:03:39.904135Z","shell.execute_reply.started":"2022-03-18T10:03:39.897766Z","shell.execute_reply":"2022-03-18T10:03:39.903415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(path_data/test_set[0]).dcmread().show()","metadata":{"execution":{"iopub.status.busy":"2022-03-18T10:02:12.792558Z","iopub.execute_input":"2022-03-18T10:02:12.792824Z","iopub.status.idle":"2022-03-18T10:02:13.025672Z","shell.execute_reply.started":"2022-03-18T10:02:12.792795Z","shell.execute_reply":"2022-03-18T10:02:13.024955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learner.predict(path_data/test_set[0])","metadata":{"execution":{"iopub.status.busy":"2022-03-18T10:02:16.685682Z","iopub.execute_input":"2022-03-18T10:02:16.685945Z","iopub.status.idle":"2022-03-18T10:02:16.746114Z","shell.execute_reply.started":"2022-03-18T10:02:16.685916Z","shell.execute_reply":"2022-03-18T10:02:16.745424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_ground_truth = pd.read_csv(\"../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:54:08.890359Z","iopub.execute_input":"2022-03-18T09:54:08.89063Z","iopub.status.idle":"2022-03-18T09:54:08.894608Z","shell.execute_reply.started":"2022-03-18T09:54:08.8906Z","shell.execute_reply":"2022-03-18T09:54:08.89363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mask = df_ground_truth[\"ID\"].str.endswith(\"any\")\n# df_ground_truth = df_ground_truth[mask]\n# f\"Labels gefunden für {len(df_ground_truth)} dicoms\"","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:54:37.547699Z","iopub.execute_input":"2022-03-18T09:54:37.548249Z","iopub.status.idle":"2022-03-18T09:54:37.551683Z","shell.execute_reply.started":"2022-03-18T09:54:37.548212Z","shell.execute_reply":"2022-03-18T09:54:37.550663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_ground_truth[\"fname\"] = df_ground_truth[\"ID\"].str.rsplit(\"_\", 1, expand=True)[0]\n# df_ground_truth.set_index(\"fname\", inplace=True)\n# df_ground_truth.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-18T09:54:35.713465Z","iopub.execute_input":"2022-03-18T09:54:35.71372Z","iopub.status.idle":"2022-03-18T09:54:35.717581Z","shell.execute_reply.started":"2022-03-18T09:54:35.713691Z","shell.execute_reply":"2022-03-18T09:54:35.716442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def compare(fname):\n    fname = Path(fname).stem\n    dicom = (path_data/fname).with_suffix(\".dcm\")\n    prediction = learner.predict(dicom)\n    y_hat = label2digit[prediction[0]]\n    y = df_testset.loc[fname + \".dcm\", \"Label\"]\n    return (y, y_hat)","metadata":{"execution":{"iopub.status.busy":"2022-03-18T10:13:12.065583Z","iopub.execute_input":"2022-03-18T10:13:12.065925Z","iopub.status.idle":"2022-03-18T10:13:12.074058Z","shell.execute_reply.started":"2022-03-18T10:13:12.065879Z","shell.execute_reply":"2022-03-18T10:13:12.073344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"compare('ID_b08fb0feb.dcm')","metadata":{"execution":{"iopub.status.busy":"2022-03-18T10:13:12.261126Z","iopub.execute_input":"2022-03-18T10:13:12.26161Z","iopub.status.idle":"2022-03-18T10:13:12.333815Z","shell.execute_reply.started":"2022-03-18T10:13:12.261575Z","shell.execute_reply":"2022-03-18T10:13:12.333178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Statistische Auswertung","metadata":{}},{"cell_type":"code","source":"result = {\n    \"FP\": 0,  # false positive\n    \"TP\": 0,  # true positive\n    \"FN\": 0,  # false negative\n    \"TN\": 0,  # true negative\n}\n\nfor file in test_set:\n    y, y_hat = compare(file)\n    \n    if y and y_hat:\n        result[\"TP\"] = result[\"TP\"] + 1\n    elif (not y) and (not y_hat):\n        result[\"TN\"] = result[\"TN\"] + 1\n    elif y and (not y_hat):\n        result[\"FN\"] = result[\"FN\"] + 1\n    elif (not y) and y_hat:\n        result[\"FP\"] = result[\"FP\"] + 1","metadata":{"execution":{"iopub.status.busy":"2022-03-18T10:16:43.634824Z","iopub.execute_input":"2022-03-18T10:16:43.635092Z","iopub.status.idle":"2022-03-18T10:16:46.369381Z","shell.execute_reply.started":"2022-03-18T10:16:43.635062Z","shell.execute_reply":"2022-03-18T10:16:46.368753Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result","metadata":{"execution":{"iopub.status.busy":"2022-03-18T10:16:52.445408Z","iopub.execute_input":"2022-03-18T10:16:52.445668Z","iopub.status.idle":"2022-03-18T10:16:52.450496Z","shell.execute_reply.started":"2022-03-18T10:16:52.44564Z","shell.execute_reply":"2022-03-18T10:16:52.449834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\nconfusion_matrix = [\n    [result[\"TP\"], result[\"FP\"]],\n    [result[\"FN\"], result[\"TN\"]]\n]\n\ndf_cm = pd.DataFrame(confusion_matrix,\n                     index=[\"NN pos\", \"NN neg\"], \n                     columns=[\"real pos\", \"real neg\"]\n                    )\n\nplt.figure(figsize = (5,5))  # hier könnt ihr die Größe ändern\nsns.set(font_scale=2)\nsns.heatmap(df_cm, annot=True, fmt=\"d\")","metadata":{"execution":{"iopub.status.busy":"2022-03-18T10:16:54.72527Z","iopub.execute_input":"2022-03-18T10:16:54.725765Z","iopub.status.idle":"2022-03-18T10:16:55.033622Z","shell.execute_reply.started":"2022-03-18T10:16:54.725723Z","shell.execute_reply":"2022-03-18T10:16:55.03298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}