5. Coding tutorial

When using the module the procedure can be split up into three steps: conversion, registration and evaluation.

5.1. Conversion

The conversion part of the module takes DICOM-files and converts them into nifty files. This is again split up into two parts: writing a dictionary and writing nifti files. The dictionary is written using the script conversion_dictwriter.py and is outputted as an excel file. The excel file is accessible by the user and also contains the flagged patients. It is important that the folder structure matches the paths given in the function. A structure as follows is thus required for all of the functions to work:

project directory
|-- data
|   |-- dicom
|   |   |-- dicom_file1.dcm
|   |   |-- ...
|   |-- misc_output
|   |   |-- filenames
|   |-- nifti
|-- dcm2reg

The dicom files thus has to be located in the folder named dicom.In order to run conversion_dictwriter.py the user has to has to supply the code with a startpatient and an endpatient i.e. how many patients the function should convert. A list of modalities to look for and which regions of interest that are of importance is also required. Furthermore, the function requires the amount of days allowed between the patient being scanned and the delineations being drawn: maxdelineationdelay. Lastly, the function requires the maximum allowed days between a primary study and secondary study: maxstudydelay.

 1# -*- coding: utf-8 -*-
 2import os
 3#from dicom_conversion import convertorv3_dictwriter_tutorial as dictwriter
 4from dicom_conversion.conversion_dictwriter import parse_args, main
 5
 6#Directories
 7cwd = os.getcwd()
 8sourcedir = os.path.join(os.path.dirname(cwd),"data","dicom")
 9miscdir = os.path.join(os.path.dirname(cwd),"data","misc_output")
10
11modalitylist = ["CT","PT","MR"]
12ROIs = ["mandible", "paro", "gtv", "brain", "chiasm", "cochlea", "esopha", "larynx", "lens", "lips", "optic", "oral", "spinal", "submand", "thyroid"]
13
14# startpatient, endpatient, sourcedir, targetdir, miscdir, modalitylist, ROIs, maxdelineationdelay, maxstudydelay
15parsed_args = parse_args(0,10,sourcedir, miscdir, modalitylist, ROIs, 21, 7)
16main(parsed_args)

The next part of the conversion is handled by the script conversion_filewriter.py. This function takes the excel file written by the previous function as input and from this converts to nifti files. The function requires a modalitylist and a list of regions of interest supplyed from the user. Furthermore, the user can specify which flagged patients the function should include when writing to nifti files. This is specified using the boolean variables: includered, includeorange, includeyellow. The nifti files will be outputted to the directory targetdir.

 1# -*- coding: utf-8 -*-
 2import os
 3from dicom_conversion import conversion_filewriter as filewriter
 4
 5#Directories
 6cwd = os.getcwd()
 7targetdir = os.path.join(os.path.dirname(cwd),"data","nifti")
 8miscdir = os.path.join(os.path.dirname(cwd),"data","misc_output")
 9
10modalitylist = ["CT","PT","MR"]
11ROIs = ["mandible", "paro", "gtv", "brain", "chiasm", "cochlea", "esopha", "larynx", "lens", "lips", "optic", "oral", "spinal", "submand", "thyroid"]
12
13#targetdir, miscdir, modalitylist, ROIs, includered, includeorange, includeyellow
14parsed_args = filewriter.parse_args(targetdir, miscdir, modalitylist, ROIs, False, False, False)
15filewriter.main(parsed_args)

5.2. Registration

The registration part of the module takes the converted nifti files as input and runs two different registrations. First, a rigid registration is run on all images and afterwards a deformable registration is run. The user needs to input how many images the registration function should run through. If the folder structure is set up correctly, the function will be able to find the needed directories. The registered images are outputted to the folder nifti_reg.

 1# -*- coding: utf-8 -*-
 2import os
 3from registration import register_rigid_bspline as registration
 4
 5#Directories
 6cwd = os.getcwd()
 7sourcedir = os.path.join(os.path.dirname(cwd),"data","nifti")
 8targetdir = os.path.join(os.path.dirname(cwd),"data","nifti_reg")
 9
10parsed_args = registration.parse_args(sourcedir,targetdir, 0, 10)
11registration.main(parsed_args)

5.3. Evaluation

The evaluation part of the module loads the registered pictures and evaluates by computing a metric. The user will need to supply the function with the amount of patients that it needs to evaluate. This is done via the variables startpatient and endpatient which is the first two arguements. The user is also able to select the margin of the croppoing box and the flagging_threshold using the variables margin and flagging. These are the last two arguments.

 1# -*- coding: utf-8 -*-
 2"""
 3Created on Wed May 25 14:41:13 2022
 4
 5@author: student
 6"""
 7
 8
 9import os
10from evaluation import evaluation
11
12#Directories
13cwd = os.getcwd()
14sourcedir = os.path.join(os.path.dirname(cwd),"data","nifti_reg")
15miscdir = os.path.join(os.path.dirname(cwd),"data","misc_output")
16
17#startpatient,endpatient,sourcedir,miscdir, margin, flaggingthreshold
18parsed_args = evaluation.parse_args(1,10,sourcedir,miscdir,3,0.5)
19evaluation.main(parsed_args)