예제 #1
0
    def _helper_recursive_glob(self, paths):
        """Given a combined list of files and directories, return a
        combined list of sorted and unique fully-qualified filenames,
        consisting of the supplied filenames and a recursive search
        through all supplied directories.
        """

        # we'll use this to keep all filenames unique
        files_dict = {}
        d = gdcm.Directory()

        for path in paths:
            if os.path.isdir(path):
                # we have to cast path to str (it's usually unicode)
                # else the gdcm wrappers error on "bad number of
                # arguments to overloaded function"
                d.Load(str(path), True)
                # fromkeys creates a new dictionary with GetFilenames
                # as keys; then update merges this dictionary with the
                # existing files_dict
                normed = [os.path.normpath(i) for i in d.GetFilenames()]
                files_dict.update(dict.fromkeys(normed, 1))

            elif os.path.isfile(path):
                files_dict[os.path.normpath(path)] = 1

        # now sort everything
        filenames = files_dict.keys()
        filenames.sort()

        return filenames
예제 #2
0
def get_series_dict(dirpath):
    directory = gdcm.Directory()
    loaded = directory.Load(dirpath)
    if not loaded: return {}
    scanner = gdcm.Scanner()
    seruid_tag = gdcm.Tag(0x0020, 0x000e)
    scanner.AddTag(seruid_tag)
    scanned = scanner.Scan(directory.GetFilenames())
    if not scanned: return {}
    uids = scanner.GetValues()
    series_dict = {}
    for uid in uids:
        series_dict[uid] = scanner.GetAllFilenamesFromTagToValue(
            seruid_tag, uid)
    return series_dict
예제 #3
0
def TestScan(dirname, recursive=False):
    # Check the dirname is indeed a directory
    system = gdcm.System()
    if not system.FileIsDirectory(dirname):
        print "Need a directory"
        sys.exit(1)

    # Retrieve all the files within that dir (recursively?)
    d = gdcm.Directory()
    nfiles = d.Load(dirname, recursive)
    print "done retrieving all the", nfiles, "files"

    s = gdcm.Scanner()
    t1 = gdcm.Tag(0x0020, 0x000d)  # VR::UI
    t2 = gdcm.Tag(0x0020, 0x000e)  # VR::UI
    t3 = gdcm.Tag(0x0028, 0x0011)  # VR::US
    # Some fun tags, with dual VR:
    t4 = gdcm.Tag(0x0028, 0x0106)  # VR::US_SS
    t5 = gdcm.Tag(0x0028, 0x0107)  # VR::US_SS
    s.AddTag(t1)
    s.AddTag(t2)
    s.AddTag(t3)
    s.AddTag(t4)
    s.AddTag(t5)
    b = s.Scan(d.GetFilenames())
    if not b:
        print "Scanner failed"
        sys.exit(1)

    # Raw Values found:
    values = s.GetValues()
    print "Values found for all tags are:"
    print values

    # get the main super-map :
    mappings = s.GetMappings()

    #file1 = d.GetFilenames()[0];
    #print file1
    #m1 = s.GetMapping( file1 )
    #print m1
    #print dir(m1)

    #for k,v in m1.iteritems():
    #  print "item", k,v

    # For each file get the value for tag t1:
    for f in d.GetFilenames():
        print "Working on:", f
        mapping = s.GetMapping(f)
        pttv = gdcm.PythonTagToValue(mapping)
        # reset iterator to start position
        pttv.Start()
        # iterate until the end:
        while (not pttv.IsAtEnd()):
            # get current value for tag and associated value:
            # if tag was not found, then it was simply not added to the internal std::map
            # Warning value can be None
            tag = pttv.GetCurrentTag()
            value = pttv.GetCurrentValue()
            print tag, "->", value
            # increment iterator
            pttv.Next()
예제 #4
0
    reader.SetNumberOfScalarComponents(numberOfScalarComponents)


if __name__ == "__main__":
    try:
        filename = os.sys.argv[1]
    except:
        # failure
        print "Need a filename"
        sys.exit(1)

    # setup reader
    r = vtkgdcm.vtkGDCMThreadedImageReader()
    r.FileLowerLeftOn()
    #dir = vtkDirectory()
    dir = gdcm.Directory()

    # Did user pass in a directory:
    system = gdcm.System()
    if system.FileIsDirectory(filename):
        nfiles = dir.Load(filename)
        files = dir.GetFilenames()
        # Need to construct full path out of the simple filename
        fullpath = vtkStringArray()
        for file in files:
            fullpath.InsertNextValue(file)
        r.SetFileNames(fullpath)
        assert fullpath.GetNumberOfValues()  # Need at least one file
        ExecuteInformation(r, fullpath.GetValue(0),
                           fullpath.GetNumberOfValues())
        r.AddObserver("ProgressEvent", PrintProgress)
예제 #5
0
    def scanDir(self, strPath, s):
        d = gdcm.Directory()
        nfiles = d.Load(strPath)
        if (nfiles == 0):
            sys.exit(-1)
            # No DICOM files in the directory

        filenames = d.GetFilenames()
        qDebug("The number of files to scan is " + str(len(filenames)))

        # Define the set of tags we are interested in, may need more
        t1 = gdcm.Tag(0x10, 0x20)
        # Patient ID
        t2 = gdcm.Tag(0x10, 0x10)
        # Patient Name
        t3 = gdcm.Tag(0x20, 0x10)
        # Study ID
        t4 = gdcm.Tag(0x20, 0x0d)
        # Study Instance UID
        t5 = gdcm.Tag(0x20, 0x0e)
        # Series Instance UID
        t6 = gdcm.Tag(0x20, 0x11)
        # Series Number
        t7 = gdcm.Tag(0x28, 0x08)
        # Number of Frames
        t8 = gdcm.Tag(0x20, 0x32)
        # Image Position
        t10 = gdcm.Tag(0x28, 0x30)
        # Pixel Spacing
        t11 = gdcm.Tag(0x20, 0x37)
        # Image Orientation Patient
        t12 = gdcm.Tag(0x28, 0x02)
        # Samples per pixel
        t13 = gdcm.Tag(0x28, 0x04)
        # Photometric Interpretation
        t14 = gdcm.Tag(0x28, 0x10)
        # Rows
        t15 = gdcm.Tag(0x28, 0x11)
        # Column
        t16 = gdcm.Tag(0x28, 0x101)
        # BitStored
        t17 = gdcm.Tag(0x02, 0x02)
        # Media Storage SOP Class UID
        t18 = gdcm.Tag(0x02, 0x03)
        # Media Storage SOP Instance UID
        t19 = gdcm.Tag(0x02, 0x10)
        # Transfer Syntax
        t20 = gdcm.Tag(0x08, 0x16)
        # SOP Class UID
        t21 = gdcm.Tag(0x08, 0x18)
        # SOP Instance UID
        t22 = gdcm.Tag(0x5200, 0x9229)
        # Shared functional group
        t23 = gdcm.Tag(0x5200, 0x9230)
        # Per frame functional group
        t24 = gdcm.Tag(0x0028, 0x1050)
        # WindowCenter
        t25 = gdcm.Tag(0x0028, 0x1051)
        # WindowWidth
        t26 = gdcm.Tag(0x0028, 0x1052)
        # Rescale Intercept
        t27 = gdcm.Tag(0x0028, 0x1053)
        # Rescale Slope
        t28 = gdcm.Tag(0x0028, 0x1054)
        # Rescale Type
        t29 = gdcm.Tag(0x0010, 0x0030)
        # PatientBirthDate
        t30 = gdcm.Tag(0x0010, 0x0040)
        # PatientSex
        t31 = gdcm.Tag(0x0008, 0x0020)
        # Study Date
        t32 = gdcm.Tag(0x0008, 0x1030)
        # Study Description

        s.AddTag(t1)
        s.AddTag(t2)
        s.AddTag(t3)
        s.AddTag(t4)
        s.AddTag(t5)
        s.AddTag(t6)
        s.AddTag(t7)
        s.AddTag(t8)
        s.AddTag(t10)
        s.AddTag(t11)
        s.AddTag(t12)
        s.AddTag(t13)
        s.AddTag(t14)
        s.AddTag(t15)
        s.AddTag(t16)
        s.AddTag(t17)
        s.AddTag(t18)
        s.AddTag(t19)
        s.AddTag(t20)
        s.AddTag(t21)
        s.AddTag(t22)
        s.AddTag(t23)
        s.AddTag(t29)
        s.AddTag(t30)
        s.AddTag(t31)
        s.AddTag(t32)

        b = s.Scan(filenames)

        # if no files in this directory
        dicomfiles = []
        if (not b):
            qDebug("Empty directory")
            return dicomfiles

        study_list = []
        series_list = []
        series_count = {}
        image_count = {}

        for aFile in filenames:
            if (s.IsKey(aFile)):  # existing DICOM file
                # qDebug("Scan "+aFile)
                is_multiframe = 0

                pttv = gdcm.PythonTagToValue(s.GetMapping(aFile))
                pttv.Start()
                patient_DOB = ""
                patient_sex = ""
                study_description = ""

                # iterate until the end:
                while (not pttv.IsAtEnd()):

                    # get current value for tag and associated value:
                    # if tag was not found, then it was simply not added to the internal std::map
                    # Warning value can be None
                    tag = pttv.GetCurrentTag()
                    value = pttv.GetCurrentValue()

                    if (tag == t1):
                        #print ("PatientID->",value)
                        patient_id = value
                    elif (tag == t2):
                        #print ("PatientName->",value)
                        patient_name = value
                    elif (tag == t29):
                        # print ("PatientBirthDate->",value)
                        patient_DOB = value
                    elif (tag == t30):
                        patient_sex = value
                    elif (tag == t3):
                        # print ("StudyID->",value)
                        study_id = value
                    elif (tag == t4):
                        studyinstance_uid = value
                    elif (tag == t31):
                        # print ("StudyDate->",value)
                        study_date = value
                    elif (tag == t32):
                        study_description = value
                    elif (tag == t6):
                        series_num = value
                        # print ("SeriesNum->",value)
                    elif (tag == t5):
                        # print ("SeriesInstanceUID->",value)
                        seriesinstance_uid = value
                    elif (tag == t7):
                        # print ("NumberOfFrame->",value)
                        if (int(value) > 1):
                            is_multiframe = int(value)
                        else:
                            is_multiframe = 0
                    elif (tag == t19):
                        # print("Transfer Syntax->",value)
                        pass
                    elif (tag == t20):
                        # print("SOP Class UID->",value)
                        sopclass_uid = value
                        #sop_ClassName = sopclass_uid.GetName()
                    elif (tag == t21):
                        # print("SOP Instance UID->",value)
                        sopinstance_uid = value
                    # increment iterator
                    pttv.Next()

                # new StudyInstanceUID
                if (studyinstance_uid not in series_count.keys()):
                    # Add to the study_list
                    study_list.append([
                        patient_id, patient_name, patient_DOB, patient_sex,
                        study_id, studyinstance_uid, study_date,
                        study_description, 0
                    ])
                    # Add count
                    series_count[studyinstance_uid] = 0

                # new SeriesInstanceUID
                if (seriesinstance_uid not in image_count.keys()):
                    # Add to the series_list
                    series_list.append([
                        study_id, studyinstance_uid, seriesinstance_uid,
                        series_num, sopclass_uid, sopinstance_uid, 0
                    ])
                    # Add count
                    image_count[seriesinstance_uid] = 0
                    series_count[studyinstance_uid] += 1

                if (is_multiframe == 0):
                    image_count[seriesinstance_uid] += 1
                else:
                    image_count[seriesinstance_uid] += is_multiframe

        # print(series_count)
        # print(image_count)

        # for each study_list items update series_count from series_count(studyinstance_uid)
        for study in study_list:
            study[8] = series_count[study[5]]

        # for each series_list items update images_count from image_count(seriesinstance_uid)
        for series in series_list:
            series[6] = image_count[series[2]]

        #print(study_list)
        #print(series_list)

        return study_list, series_list
예제 #6
0
import gdcm
import sys


def PrintProgress(object, event):
    assert event == "ProgressEvent"
    print "Progress:", object.GetProgress()


def MySort(ds1, ds2):
    # compare ds1
    return False


if __name__ == "__main__":

    dirname = sys.argv[1]
    d = gdcm.Directory()
    d.Load(dirname)

    print d

    sorter = gdcm.Sorter()
    sorter.SetSortFunction(MySort)
    #sorter.AddObserver( "ProgressEvent", PrintProgress )
    sorter.Sort(d.GetFilenames())

    print "Sorter:"
    print sorter