-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test autopipeline and modalities, solved some autopipeline bugs…
…, read_dicom_series and pet now supports series_id Former-commit-id: 03f9957
- Loading branch information
Showing
9 changed files
with
192 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...CA ORL FDG TEP POS TX-94629/1.000000-RTstructCTsim-PETPET-CT-87625/1-1.dcm.REMOVED.git-id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
b75896dfda8ce5f47a6086c99b9099690502266a |
1 change: 1 addition & 0 deletions
1
...B.0OrophCBTRTID derived StudyInstanceUID.-94629/Pinnacle POI-41418/1-1.dcm.REMOVED.git-id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dd2bcf36be02340b1220b43c1f7ab4bb1180dfc0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import os | ||
from posixpath import dirname | ||
import shutil | ||
import warnings | ||
from multiprocessing import cpu_count | ||
|
||
import numpy as np | ||
import SimpleITK as sitk | ||
import pytest | ||
import nrrd | ||
import pandas as pd | ||
|
||
from imgtools.autopipeline import AutoPipeline | ||
|
||
@pytest.fixture | ||
def dataset_path(): | ||
curr_path=("/").join(os.getcwd().split("/")[:-1]) | ||
input_path = curr_path+ "/examples/data_test" | ||
output_path = curr_path+ "/tests/" | ||
return input_path,output_path | ||
|
||
@pytest.mark.parametrize("modalities",["PT","CT,RTDOSE","CT,RTSTRUCT,RTDOSE","CT,RTSTRUCT,RTDOSE,PT"]) | ||
def test_pipeline(dataset_path,modalities): | ||
input_path,output_path = dataset_path | ||
n_jobs = 2 | ||
output_path_mod = output_path + "temp_folder_" + ("_").join(modalities.split(",")) | ||
#Initialize pipeline for the current setting | ||
pipeline = AutoPipeline(input_path,output_path_mod,modalities,n_jobs=n_jobs) | ||
#Run for different modalities | ||
comp_path = os.path.join(output_path_mod, "dataset.csv") | ||
if n_jobs > 1 or n_jobs == -1: # == Parallel Processing == | ||
pipeline.run() | ||
elif n_jobs == 1: # == Series (Single-core) Processing == | ||
subject_ids = pipeline._get_loader_subject_ids() | ||
for subject_id in subject_ids: | ||
pipeline.process_one_subject(subject_id) | ||
pipeline.graph.to_csv(comp_path) | ||
|
||
#Check if the crawl and edges exist | ||
crawl_path = ("/").join(input_path.split("/")[:-1]) + "/imgtools_" + input_path.split("/")[-1] + ".csv" | ||
json_path = ("/").join(input_path.split("/")[:-1]) + "/imgtools_" + input_path.split("/")[-1] + ".json" | ||
edge_path = ("/").join(input_path.split("/")[:-1]) + "/imgtools_" + input_path.split("/")[-1] + "_edges.csv" | ||
assert os.path.exists(crawl_path) & os.path.exists(edge_path), "this breaks because there was no crawler output" | ||
|
||
#for the test example, there are 6 files and 4 connections | ||
crawl_data = pd.read_csv(crawl_path,index_col = 0) | ||
edge_data = pd.read_csv(edge_path) | ||
assert (len(crawl_data)==7) & (len(edge_data)==4), "this breaks because there was some error in crawling or while making the edge table" | ||
|
||
#Check if the dataset.csv is having the correct number of components and has all the fields | ||
comp_table = pd.read_csv(comp_path) | ||
assert len(comp_table)==1, "this breaks because there is some error in making components, check datagraph.parser" | ||
|
||
#Check the nrrd files | ||
if modalities=="PT": | ||
path_pet = output_path_mod + "/pet/" + os.listdir(output_path_mod+"/pet")[0] | ||
dicom,_ = nrrd.read(path_pet) | ||
assert dicom.shape[-1] == int(crawl_data.loc[crawl_data["modality"]=="PT","instances"].values[0]) | ||
elif modalities=="CT,RTDOSE": | ||
path_ct = output_path_mod + "/image/" + os.listdir(output_path_mod+"/image")[0] | ||
path_dose = output_path_mod + "/dose/" + os.listdir(output_path_mod+"/dose")[0] | ||
dicom_ct,_ = nrrd.read(path_ct) | ||
dicom_dose,_ = nrrd.read(path_dose) | ||
assert dicom_ct.shape == dicom_dose.shape | ||
elif modalities=="CT,RTSTRUCT,RTDOSE": | ||
path_ct = output_path_mod + "/image/" + os.listdir(output_path_mod+"/image")[0] | ||
path_dose = output_path_mod + "/dose/" + os.listdir(output_path_mod+"/dose")[0] | ||
path_str = output_path_mod + "/mask_ct/" + os.listdir(output_path_mod+"/mask_ct")[0] | ||
dicom_ct,_ = nrrd.read(path_ct) | ||
dicom_dose,_ = nrrd.read(path_dose) | ||
dicom_str,_ = nrrd.read(path_str) | ||
#ensure they are in same physical space | ||
assert dicom_ct.shape == dicom_dose.shape == dicom_str.shape[1:] | ||
else: | ||
path_ct = output_path_mod + "/image/" + os.listdir(output_path_mod+"/image")[0] | ||
path_dose = output_path_mod + "/dose/" + os.listdir(output_path_mod+"/dose")[0] | ||
path_ctstr = output_path_mod + "/mask_ct/" + os.listdir(output_path_mod+"/mask_ct")[0] | ||
path_ptstr = output_path_mod + "/mask_pt/" + os.listdir(output_path_mod+"/mask_pt")[0] | ||
path_pet = output_path_mod + "/pet/" + os.listdir(output_path_mod+"/pet")[0] | ||
dicom_ct,_ = nrrd.read(path_ct) | ||
dicom_dose,_ = nrrd.read(path_dose) | ||
dicom_ctstr,_ = nrrd.read(path_ctstr) | ||
dicom_ptstr,_ = nrrd.read(path_ptstr) | ||
dicom_pet,_ = nrrd.read(path_pet) | ||
#ensure they are in same physical space | ||
assert dicom_ct.shape == dicom_dose.shape == dicom_ctstr.shape[1:] == dicom_ptstr.shape[1:] == dicom_pet.shape | ||
os.remove(crawl_path) | ||
os.remove(json_path) | ||
os.remove(edge_path) | ||
shutil.rmtree(output_path_mod) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
''' | ||
This code is for testing functioning of different modalities | ||
''' | ||
|
||
|
||
import os | ||
from posixpath import dirname | ||
import shutil | ||
import warnings | ||
from multiprocessing import cpu_count | ||
|
||
import numpy as np | ||
import SimpleITK as sitk | ||
import pytest | ||
import pydicom | ||
|
||
from imgtools.io import read_dicom_auto | ||
from imgtools.ops import StructureSetToSegmentation, ImageAutoOutput, Resample | ||
from imgtools.pipeline import Pipeline | ||
|
||
@pytest.fixture | ||
def modalities_path(): | ||
path = {} | ||
path["CT"] = "../examples/data_test/patient_1/08-27-1885-CA ORL FDG TEP POS TX-94629/3.000000-Merged-06362" | ||
path["RTSTRUCT"] = "../examples/data_test/patient_1/08-27-1885-OrophCB.0OrophCBTRTID derived StudyInstanceUID.-94629/Pinnacle POI-41418" | ||
path["RTDOSE"] = "../examples/data_test/patient_1/08-27-1885-OrophCB.0OrophCBTRTID derived StudyInstanceUID.-94629/11376" | ||
path["PT"] = "../examples/data_test/patient_1/08-27-1885-CA ORL FDG TEP POS TX-94629/532790.000000-LOR-RAMLA-44600" | ||
return path | ||
|
||
@pytest.mark.parametrize("modalities", ["CT", "RTSTRUCT","RTDOSE","PT"]) | ||
def test_modalities(modalities,modalities_path): | ||
path = modalities_path | ||
if modalities!="RTSTRUCT": | ||
#Checks for dimensions | ||
img = read_dicom_auto(path["CT"]) | ||
dcm = pydicom.dcmread(os.path.join(path[modalities],os.listdir(path[modalities])[0])).pixel_array | ||
instances = len(os.listdir(path[modalities])) | ||
dicom = read_dicom_auto(path[modalities]) | ||
if instances>1: #For comparing CT and PT modalities | ||
assert dcm.shape == (dicom.GetHeight(),dicom.GetWidth()) | ||
assert instances == dicom.GetDepth() | ||
else: #For comparing RTDOSE modalties | ||
assert dcm.shape == (dicom.GetDepth(),dicom.GetHeight(),dicom.GetWidth()) | ||
if modalities=="PT": | ||
dicom = dicom.resample_pet(img) | ||
assert dicom.GetSize()==img.GetSize() | ||
if modalities=="RTDOSE": | ||
dicom = dicom.resample_dose(img) | ||
assert dicom.GetSize()==img.GetSize() | ||
else: | ||
img = read_dicom_auto(path["CT"]) | ||
struc = read_dicom_auto(path[modalities]) | ||
make_binary_mask = StructureSetToSegmentation(roi_names=[], continuous=False) | ||
mask = make_binary_mask(struc, img) | ||
A = sitk.GetArrayFromImage(mask) | ||
assert len(A.shape)==4 | ||
assert A.shape[0:3]==(img.GetDepth(),img.GetHeight(),img.GetWidth()) |