Skip to content

Commit

Permalink
Replace dictionary by MetaData class
Browse files Browse the repository at this point in the history
  • Loading branch information
lamyj committed May 16, 2016
1 parent 6b6849a commit 11d991d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
20 changes: 11 additions & 9 deletions src/cli/dicom2nifti
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ def convert(dicom, destination, dtype, pretty_print, zip):
nifti_data = dicomifier.dicom_to_nifti.convert(dicom_data_sets, dtype)

stacks_count = {}
for nifti_image, nifti_meta_data in nifti_data:
series_instance_uid = nifti_meta_data["SeriesInstanceUID"][0]
for image, meta_data in nifti_data:
series_instance_uid = meta_data["SeriesInstanceUID"][0]

stacks_count.setdefault(series_instance_uid, 0)
stacks_count[series_instance_uid] += 1

directories = {}

for nifti_image, nifti_meta_data in nifti_data:
study_instance_uid = nifti_meta_data["StudyInstanceUID"]
series_instance_uid = nifti_meta_data["SeriesInstanceUID"][0]
for image, meta_data in nifti_data:
study_instance_uid = meta_data["StudyInstanceUID"][0]
series_instance_uid = meta_data["SeriesInstanceUID"][0]

study_description = nifti_meta_data.get("StudyDescription", "")
series_description = nifti_meta_data.get("SeriesDescription", "")
study_description = meta_data.get("StudyDescription", [""])[0]
series_description = meta_data.get("SeriesDescription", [""])[0]

study_directory, _ = directories.setdefault(
study_instance_uid, [1+len(directories), {}])
Expand Down Expand Up @@ -99,12 +99,14 @@ def convert(dicom, destination, dtype, pretty_print, zip):
suffix = ".nii"
if zip:
suffix += ".gz"
nifti_image.save(destination_root+suffix)
image.save(destination_root+suffix)

kwargs = {}
if pretty_print:
kwargs = {"sort_keys": True, "indent": 4}
json.dump(nifti_meta_data, open(destination_root+".json", "w"), **kwargs)
json.dump(
meta_data, open(destination_root+".json", "w"),
cls=dicomifier.MetaData.JSONEncoder, **kwargs)

if __name__ == "__main__":
sys.exit(main())
8 changes: 5 additions & 3 deletions src/python/dicomifier/dicom_to_nifti/convert.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import print_function
import logging
import itertools

import numpy
Expand All @@ -11,11 +11,13 @@ def convert(dicom_data_sets, dtype, pretty_print=False):
nifti_data = []

stacks = get_stacks(dicom_data_sets)
logging.info(
"Found {} stack{}".format(len(stacks), "s" if len(stacks)>1 else ""))
for key, data_sets in stacks.items():
sort(data_sets)

nifti_image = image.get_nifti_image(data_sets, dtype)
nifti_meta_data = meta_data.get_nifti_meta_data(data_sets, key)
nifti_image = image.get_image(data_sets, dtype)
nifti_meta_data = meta_data.get_meta_data(data_sets, key)

nifti_data.append((nifti_image, nifti_meta_data))

Expand Down
2 changes: 1 addition & 1 deletion src/python/dicomifier/dicom_to_nifti/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import numpy
import odil

def get_nifti_image(data_sets, dtype):
def get_image(data_sets, dtype):
pixel_data = [get_pixel_data(data_set) for data_set in data_sets]
pixel_data = numpy.asarray(pixel_data, dtype=dtype)

Expand Down
41 changes: 24 additions & 17 deletions src/python/dicomifier/dicom_to_nifti/meta_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@

import itertools

import numpy
import odil

def get_nifti_meta_data(data_sets, key):
from .. import MetaData

def get_meta_data(data_sets, key):
""" Return the merged meta-data from the DICOM data sets in the NIfTI+JSON
format: a dictionary keyed by the DICOM keyword (or the string
representation of the tag if no keyword is found) and valued by the
Value (or InlineBinary) of the DICOM elements.
"""

meta_data = [convert_meta_data(x) for x in data_sets]

named_key = []
Expand All @@ -21,18 +30,26 @@ def get_nifti_meta_data(data_sets, key):
except odil.Exception as e:
pass
named_key.append(name)

return merge_meta_data(meta_data, named_key)

def convert_meta_data(data_set):
""" Convert the meta-data from DICOM data sets to the NIfTI+JSON format.
"""

meta_data = {}

skipped = [
# Stored in the NIfTI image
"Rows", "Columns",
"ImageOrientationPatient", "ImagePositionPatient", "PixelSpacing",
# Useless in the NIfTI world (?)
"SOPInstanceUID",
# Implicit with the NIfTI data type
"PixelRepresentation", "HighBit", "BitsStored", "BitsAllocated",
# Stored in the NIfTI image
"PixelData",
# PixelValueTransformation sequence is applied on the image
"PixelValueTransformationSequence",
"SmallestImagePixelValue", "LargestImagePixelValue",
]
Expand Down Expand Up @@ -64,34 +81,24 @@ def convert_meta_data(data_set):
return meta_data

def merge_meta_data(data_sets, key):
""" Merge the meta-data of DICOM data sets if they are equal.
"""

merged = {}

tags = set(itertools.chain(*[x.keys() for x in data_sets]))
for tag in tags:
multiplicity = "unknown"
try:
multiplicity = odil.registry.public_dictionary[
getattr(odil.registry, tag)].vm
except:
# Nothing is known about the multiplicity
pass

merged_value = None
if tag in key:
if multiplicity == "1":
merged_value = data_sets[0][tag]
else:
merged_value = data_sets[0][tag]
merged_value = data_sets[0][tag]
else:
merged_value = []
for data_set in data_sets:
value = data_set.get(tag, None)
if value is not None and multiplicity == "1":
value = value[0]
merged_value.append(value)

if all(x == merged_value[0] for x in merged_value):
merged_value = merged_value[0]
merged[tag] = merged_value

return merged
return MetaData(merged)

0 comments on commit 11d991d

Please sign in to comment.