Skip to content

Commit

Permalink
added modalities, num rtstruct and pixel size to metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
fishingguy456 committed Jun 3, 2022
1 parent ae09827 commit 51634e6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
7 changes: 6 additions & 1 deletion examples/autotest.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ def process_one_subject(self, subject_id):
print(subject_id, " start")

metadata = {}
subject_modalities = set()
num_rtstructs = 0
for i, colname in enumerate(self.output_streams):
modality = colname.split("_")[0]

subject_modalities.add(modality)
# Taking modality pairs if it exists till _{num}
output_stream = ("_").join([item for item in colname.split("_") if item.isnumeric()==False])

Expand Down Expand Up @@ -154,6 +156,7 @@ def process_one_subject(self, subject_id):

print(subject_id, " SAVED DOSE")
elif modality == "RTSTRUCT":
num_rtstructs += 1
#For RTSTRUCT, you need image or PT
structure_set = read_results[i]
conn_to = output_stream.split("_")[-1]
Expand Down Expand Up @@ -220,6 +223,8 @@ def process_one_subject(self, subject_id):

print(subject_id, " SAVED PET")
#Saving all the metadata in multiple text files
metadata["Modalities"] = list(subject_modalities)
metadata["numRTSTRUCTs"] = num_rtstructs
with open(pathlib.Path(self.output_directory,".temp",f'{subject_id}.pkl').as_posix(),'wb') as f:
pickle.dump(metadata,f)
return
Expand Down
6 changes: 5 additions & 1 deletion imgtools/io/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
from typing import Optional, List
from collections import namedtuple
import json
import copy

import pandas as pd
import SimpleITK as sitk
Expand Down Expand Up @@ -128,6 +128,10 @@ def read_dicom_series(path: str,
metadata["RescaleType"] = str(dicom_data.RescaleType)
if hasattr(dicom_data, 'RescaleSlope'):
metadata["RescaleSlope"] = str(dicom_data.RescaleSlope)
if hasattr(dicom_data, 'PixelSpacing') and hasattr(dicom_data, 'SliceThickness'):
pixel_size = copy.copy(dicom_data.PixelSpacing)
pixel_size.append(dicom_data.SliceThickness)
metadata["PixelSize"] = tuple(pixel_size)

return CTMRScan(reader.Execute(), metadata)

Expand Down
5 changes: 5 additions & 0 deletions imgtools/modules/dose.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os, pathlib
import warnings
import copy

from typing import Dict, Optional, TypeVar

Expand Down Expand Up @@ -77,6 +78,10 @@ def from_dicom_rtdose(cls, path):
metadata["RescaleType"] = str(df.RescaleType)
if hasattr(df, 'RescaleSlope'):
metadata["RescaleSlope"] = str(df.RescaleSlope)
if hasattr(df, 'PixelSpacing') and hasattr(df, 'SliceThickness'):
pixel_size = copy.copy(df.PixelSpacing)
pixel_size.append(df.SliceThickness)
metadata["PixelSize"] = tuple(pixel_size)

return cls(img_dose, df)

Expand Down
5 changes: 5 additions & 0 deletions imgtools/modules/pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from matplotlib import pyplot as plt
import SimpleITK as sitk
from pydicom import dcmread
import copy

T = TypeVar('T')

Expand Down Expand Up @@ -100,6 +101,10 @@ def from_dicom_pet(cls, path,series_id=None,type="SUV"):
metadata["RescaleType"] = str(pet.RescaleType)
if hasattr(pet, 'RescaleSlope'):
metadata["RescaleSlope"] = str(pet.RescaleSlope)
if hasattr(pet, 'PixelSpacing') and hasattr(pet, 'SliceThickness'):
pixel_size = copy.copy(pet.PixelSpacing)
pixel_size.append(pet.SliceThickness)
metadata["PixelSize"] = tuple(pixel_size)

return cls(img_pet, df, factor, calc, metadata)
# return cls(img_pet, df, factor, calc)
Expand Down
7 changes: 6 additions & 1 deletion imgtools/modules/structureset.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
from warnings import warn
from typing import Dict, List, Optional, Union, Tuple, Set, TypeVar
import copy

import numpy as np
import SimpleITK as sitk
Expand Down Expand Up @@ -36,7 +37,7 @@ def from_dicom_rtstruct(cls, rtstruct_path: str) -> 'StructureSet':

metadata = {}
if hasattr(rtstruct, 'StructureSetROISequence'):
metadata["num_ROIs"] = str(len(rtstruct.StructureSetROISequence))
metadata["numROIs"] = str(len(rtstruct.StructureSetROISequence))

# Number of Slices is avg. number slice?
if hasattr(rtstruct, 'NumberofSlices'):
Expand Down Expand Up @@ -66,6 +67,10 @@ def from_dicom_rtstruct(cls, rtstruct_path: str) -> 'StructureSet':
metadata["RescaleType"] = str(rtstruct.RescaleType)
if hasattr(rtstruct, 'RescaleSlope'):
metadata["RescaleSlope"] = str(rtstruct.RescaleSlope)
if hasattr(rtstruct, 'PixelSpacing') and hasattr(rtstruct, 'SliceThickness'):
pixel_size = copy.copy(rtstruct.PixelSpacing)
pixel_size.append(rtstruct.SliceThickness)
metadata["PixelSize"] = tuple(pixel_size)

return cls(roi_points, metadata)
# return cls(roi_points)
Expand Down

0 comments on commit 51634e6

Please sign in to comment.