Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Fabian Blumenroehr committed Sep 14, 2022
1 parent be301aa commit 32a4b11
Showing 1 changed file with 13 additions and 48 deletions.
61 changes: 13 additions & 48 deletions NEPMetadataMapping/dicomReader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pydicom
import re
import logging
from datetime import datetime

# This class instantiates the class object of a dicom file, which contains the key-value pairs of the fiel metadata.
class DicomReader:
Expand All @@ -15,64 +16,24 @@ def __init__(self, dicomFile, studyName, seriesName):

self.studyName=studyName
self.seriesName=seriesName
self.studyDateTime=None
self.mainDict={}
self.subDict=self.dataset(self.dicomFile)
self.subDict=self.pydicomObjects(self.dicomFile)
self.__dict__.update(self.mainDict)
self.studyDateTime = datetime.strptime(self.studyDateTime, '%Y%m%d %H%M%S').isoformat()
self.__dict__.pop("mainDict")
self.__dict__.pop("subDict")
self.__dict__.pop("dicomFile")
self.__dict__.pop("pixelData")

#This method parses the pydicom.Dataset objects created by the pyDicom module to retrieve the primitive data types
#print(self.__dict__)
#This method parses the pydicom.Dataset and pydicom.Sequence objects created by the pyDicom module to retrieve the primitive data types
# and separate the attributes.
def dataset(self, dataset):
def pydicomObjects(self, dataset):
subDict={}
subList=[]
for i in dataset:
if isinstance(i, pydicom.Dataset):
subList.append(self.dataset(i))
elif isinstance(i.value, pydicom.Sequence):
name=i.name.split()
if len(name)==1:
name=name[0].lower()
else:
subname=""
for j in name[1:]:
subname+=j.capitalize()

name=name[0].lower() + subname

name=re.sub('[^A-Za-z0-9]+', '', name)
subDict[name]=self.sequence(i.value)
else:
name=i.name.split()
if len(name)==1:
name=name[0].lower()
else:
subname=""
for j in name[1:]:
subname+=j.capitalize()

name=name[0].lower() + subname

name=re.sub('[^A-Za-z0-9]+', '', name)

subDict[name]=i.value
self.mainDict[name]=i.value

if len(subList)>0:
return subList
else:
return subDict

#This method parses the pydicom.Sequence objects created by the pyDicom module to retrieve the primitive data types
# and separate the attributes
def sequence(self, sequence):
subDict={}
subList=[]
for i in sequence:
if isinstance(i, pydicom.Dataset):
subList.append(self.dataset(i))
subList.append(self.pydicomObjects(i))
elif isinstance(i.value, pydicom.Sequence):
name=i.name.split()
if len(name)==1:
Expand All @@ -85,7 +46,7 @@ def sequence(self, sequence):
name=name[0].lower() + subname

name=re.sub('[^A-Za-z0-9]+', '', name)
subDict[name]=self.sequence(i.value)
subDict[name]=self.pydicomObjects(i.value)
else:
name=i.name.split()
if len(name)==1:
Expand All @@ -98,6 +59,10 @@ def sequence(self, sequence):
name=name[0].lower() + subname

name=re.sub('[^A-Za-z0-9]+', '', name)
if name == "studyDate":
self.studyDateTime=i.value+" "
if name == "studyTime":
self.studyDateTime += i.value
subDict[name]=i.value
self.mainDict[name]=i.value

Expand Down

0 comments on commit 32a4b11

Please sign in to comment.