Beispiel #1
0
    def test_get_patient_age_with_field(self, dicom_generator):
        age = '10Y'
        filename, dataset = dicom_generator(PatientAge=age)

        dcm = Dicom(filename, dcm=dataset)

        assert dcm._patient_age() == age
Beispiel #2
0
    def test_get_patient_age_with_birth_date(self, dicom_generator):
        filename, dataset = dicom_generator(PatientBirthDate='20180101',
                                            StudyDate='20190701')

        dcm = Dicom(filename, dcm=dataset)

        assert dcm._patient_age() == '001Y'
Beispiel #3
0
    def test_sort_test(self, dicom_generator, tmpdir, capsys):
        filename, dicom = dicom_generator(
            SeriesDescription='desc',
            SeriesNumber=1,
        )
        dcm = Dicom(filename, dcm=dicom)

        # Base Directory
        root = str(tmpdir)

        # Subdirectories
        directory = [
            '%(SeriesDescription)s',
            '%(SeriesDescription)s',
        ]

        # Filename format
        file_format = '%(ImageType)s'

        dcm.sort(root, directory, file_format, keep_original=False, test=True)

        destination = tmpdir.join('desc_Series0001').join('desc_Series0001')
        destination = destination.join('Unknown')

        # The original is unmodified
        assert os.path.exists(filename)

        # The file was not actually copied
        assert os.path.exists(str(destination)) is False

        captured = capsys.readouterr()
        assert captured.out == str(destination) + '\n'
Beispiel #4
0
    def test_sort_discard_original(self, dicom_generator, tmpdir):
        filename, dicom = dicom_generator(
            SeriesDescription='desc',
            SeriesNumber=1,
        )
        dcm = Dicom(filename, dcm=dicom)

        # Base Directory
        root = str(tmpdir)

        # Subdirectories
        directory = [
            '%(SeriesDescription)s',
            '%(SeriesDescription)s',
        ]

        # Filename format
        file_format = '%(ImageType)s'

        dcm.sort(root, directory, file_format, keep_original=False)

        destination = tmpdir.join('desc_Series0001').join('desc_Series0001')
        destination = destination.join('Unknown')

        assert os.path.exists(filename) is False
        assert os.path.exists(str(destination))
Beispiel #5
0
    def test_get_item_override_value(self, dicom_generator):
        filename, dicom = dicom_generator()
        dcm = Dicom(filename, dcm=dicom)

        dcm.set_anonymization_rules({'Field': 'Value'})

        assert dcm['Field'] == 'Value'
Beispiel #6
0
    def test_get_image_type_3drecon(self, dicom_generator):
        filename, dataset = dicom_generator(ImageType=['CSA 3D EDITOR', ''])

        dcm = Dicom(filename, dcm=dataset)

        assert dcm._image_type() == '3DRecon'
        assert dataset.InstanceNumber == dataset.SeriesNumber
Beispiel #7
0
    def test_get_series_description(self, dicom_generator):
        filename, dataset = dicom_generator(SeriesDescription=' My Series',
                                            SeriesNumber=1)

        dcm = Dicom(filename, dcm=dataset)

        assert dcm._series_description() == 'My Series_Series0001'
Beispiel #8
0
    def test_get_series_description_no_description(self, dicom_generator):
        filename, dicom = dicom_generator(SeriesNumber=1)
        del dicom.SeriesDescription

        dcm = Dicom(filename, dcm=dicom)

        assert dcm._series_description() == 'Series0001'
Beispiel #9
0
    def test_sort_inplace(self, dicom_generator, tmpdir):
        filename, dicom = dicom_generator(
            'image.dcm',
            SeriesDescription='desc',
            SeriesNumber=1,
        )
        dcm = Dicom(filename, dcm=dicom)

        # Base Directory
        root = str(tmpdir)

        # Subdirectories
        directory = None

        # Filename format
        file_format = '%(ImageType)s'

        dcm.sort(
            root,
            directory,
            file_format,
            rootdir=[os.path.dirname(filename)],
        )

        destination = str(tmpdir.join('image.dcm'))

        assert os.path.exists(destination)
Beispiel #10
0
    def test_get_destination_bad_directory_attr(self, dicom_generator):
        filename, dicom = dicom_generator(
            'image.dcm',
            PatientName='name',
            SeriesDescription='desc',
            SeriesNumber=1,
        )

        dcm = Dicom(filename, dcm=dicom)

        # Base Directory
        root = '/my/base/directory'

        # Sub-directories
        directory = [
            '%(Invalid)s',  # Invalid attribute
            '%(SeriesDescription)s %(PatientName)s'
        ]

        # Filename format
        filename = '%(SeriesNumber)d file'

        dest = dcm.get_destination(root, directory, filename)

        expected = '/my/base/directory/UNKNOWN/desc_Series0001 name/1 file'
        assert dest == expected
Beispiel #11
0
    def test_anonymization_invalid_input(self, dicom_generator):
        filename, dataset = dicom_generator()
        dcm = Dicom(filename, dcm=dataset)

        with pytest.raises(Exception) as excinfo:
            dcm.set_anonymization_rules('')

        assert excinfo.value.args[0] == 'Anon rules must be a dictionary'
Beispiel #12
0
    def test_anonymize_birthdate(self, dicom_generator):
        date = '20191101'
        filename, dataset = dicom_generator(PatientBirthDate='20200101')
        dcm = Dicom(filename, dcm=dataset)

        dcm.set_anonymization_rules({'PatientBirthDate': date})

        assert dcm.overrides['PatientBirthDate'] == date
        assert dcm['PatientBirthDate'] == date
Beispiel #13
0
    def test_anonymize_age(self, dicom_generator):
        filename, dataset = dicom_generator(PatientAge='030Y',
                                            PatientBirthDate='19890201',
                                            StudyDate='20190301')

        dcm = Dicom(filename, dcm=dataset)

        dcm.set_anonymization_rules({'PatientBirthDate': ''})

        assert dcm['PatientAge'] == '030Y'
        assert dcm['PatientBirthDate'] == '19890101'
Beispiel #14
0
    def test_anonymization(self, dicom_generator):
        filename, dataset = dicom_generator()
        dcm = Dicom(filename, dcm=dataset)

        adict = {'Key': 'Value'}

        dcm.set_anonymization_rules(adict)

        assert dcm.anonymization_lookup == adict
        assert dcm.overrides['Key'] == 'Value'
        assert dcm['Key'] == 'Value'
Beispiel #15
0
    def test_constructor_without_dicom(self, dicom_generator, mocker):
        func = mocker.patch.object(pydicom, 'read_file', return_value='dicom')

        filename, _ = dicom_generator()
        dcm = Dicom(filename)

        func.assert_called_once_with(filename)
        assert dcm.dicom == 'dicom'
Beispiel #16
0
    def test_constructor_with_dicom(self, dicom_generator, mocker):
        func = mocker.patch.object(pydicom, 'read_file', return_value='dicom')

        filename, dataset = dicom_generator()

        dcm = Dicom(filename, dcm=dataset)

        func.assert_not_called()
        assert dcm.dicom == dataset
Beispiel #17
0
    def test_create_default_overrides(self, dicom_generator):
        filename, dataset = dicom_generator()
        dcm = Dicom(filename, dcm=dataset)

        assert 'ImageType' in dcm.default_overrides
        assert 'FileExtension' in dcm.default_overrides
        assert 'SeriesDescription' in dcm.default_overrides

        assert dcm.overrides == dcm.default_overrides
Beispiel #18
0
    def test_sort_anonymize(self, dicom_generator, tmpdir):
        filename, dicom = dicom_generator(
            PatientName='TO^BE^REMOVED',
            SeriesDescription='desc',
            SeriesNumber=1,
        )
        dcm = Dicom(filename, dcm=dicom)

        # Overwrite the PatientName
        dcm.set_anonymization_rules({'PatientName': 'ANON'})

        # Base Directory
        root = str(tmpdir)

        # Subdirectories
        directory = [
            '%(PatientName)s',
            '%(SeriesDescription)s',
        ]

        # Filename format
        file_format = '%(ImageType)s'

        dcm.sort(root, directory, file_format, keep_original=False)

        destination = tmpdir.join('ANON').join('desc_Series0001')
        destination = str(destination.join('Unknown'))

        assert os.path.exists(destination)

        # Ensure the DICOM was modified
        newdcm = pydicom.read_file(destination)

        assert newdcm.PatientName == 'ANON'
Beispiel #19
0
    def test_sort_anonymize_invalid_field(self, dicom_generator, tmpdir):
        filename, dicom = dicom_generator(
            PatientName='TO^BE^REMOVED',
            SeriesDescription='desc',
            SeriesNumber=1,
        )
        dcm = Dicom(filename, dcm=dicom)

        # Overwrite the PatientID (this field does not exist)
        dcm.set_anonymization_rules({'PatientID': 'ANON'})

        # Base Directory
        root = str(tmpdir)

        # Subdirectories
        directory = [
            '%(PatientID)s',
            '%(SeriesDescription)s',
        ]

        # Filename format
        file_format = '%(ImageType)s'

        dcm.sort(root, directory, file_format, keep_original=False)

        destination = tmpdir.join('ANON').join('desc_Series0001')
        destination = str(destination.join('Unknown'))

        assert os.path.exists(destination)

        # Make sure the field was not added to the new DICOM
        newdcm = pydicom.read_file(destination)

        assert 'PatientID' not in newdcm
Beispiel #20
0
    def test_get_destination(self, dicom_generator):
        filename, dicom = dicom_generator(
            PatientName='name',
            SeriesDescription='desc',
            SeriesNumber=1,
        )

        dcm = Dicom(filename, dcm=dicom)

        # Base Directory
        root = '/my/base/directory'

        # Sub-directories
        directory = ['%(ImageType)s', '%(SeriesDescription)s %(PatientName)s']

        # Filename format
        filename = '%(SeriesNumber)d file'

        dest = dcm.get_destination(root, directory, filename)

        expected = '/my/base/directory/Unknown/desc_Series0001 name/1 file'
        assert dest == expected
Beispiel #21
0
    def test_get_destination_bad_filename_attr(self, dicom_generator):
        filename, dicom = dicom_generator(
            'image.dcm',
            PatientName='name',
            SeriesDescription='desc',
            SeriesNumber=1,
        )

        dcm = Dicom(filename, dcm=dicom)

        # Base Directory
        root = '/my/base/directory'

        # Sub-directories
        directory = ['%(ImageType)s', '%(SeriesDescription)s %(PatientName)s']

        # Filename format which uses attributes that are not defined
        filename = '%(Invalid)d file'

        dest = dcm.get_destination(root, directory, filename)
        expected = '/my/base/directory/Unknown/desc_Series0001 name/image.dcm'

        assert dest == expected
Beispiel #22
0
    def test_is_anonymous(self, dicom_generator):
        filename, dataset = dicom_generator()
        dcm = Dicom(filename, dcm=dataset)

        assert dcm.is_anonymous() is False

        # Set anonimization rules
        dcm.set_anonymization_rules({'PatientName': 'ANON'})

        assert dcm.is_anonymous() is True
Beispiel #23
0
    def test_anonymize_empty_birthdate(self, dicom_generator):
        filename, dataset = dicom_generator(
            PatientBirthDate='20170601',
            StudyDate='20180201',
        )
        dcm = Dicom(filename, dcm=dataset)

        dcm.set_anonymization_rules({'PatientBirthDate': ''})

        # Maintains the patient age but uses January 01
        assert dcm.overrides['PatientBirthDate'] == '20180101'
        assert dcm['PatientBirthDate'] == '20180101'

        # 1-day Old
        dataset.PatientBirthDate = '20180131'

        dcm.set_anonymization_rules({'PatientBirthDate': ''})
        assert dcm.overrides['PatientBirthDate'] == '20180101'
        assert dcm['PatientBirthDate'] == '20180101'
Beispiel #24
0
    def test_get_image_type_magnitude(self, dicom_generator):
        filename, dataset = dicom_generator(ImageType=['FFE', 'M', 'JUNK'])

        dcm = Dicom(filename, dcm=dataset)

        assert dcm._image_type() == 'Mag'
Beispiel #25
0
    def test_get_image_type_phase(self, dicom_generator):
        filename, dataset = dicom_generator(ImageType=['P', 'BLAH'])

        dcm = Dicom(filename, dcm=dataset)

        assert dcm._image_type() == 'Phase'
Beispiel #26
0
    def test_get_image_type_phoenix(self, dicom_generator):
        filename, dataset = dicom_generator(ImageType=['CSA REPORT', ''])

        dcm = Dicom(filename, dcm=dataset)

        assert dcm._image_type() == 'Phoenix'
Beispiel #27
0
    def test_get_file_extension_empty(self, dicom_generator):
        filename, _ = dicom_generator('image')

        dcm = Dicom(filename)
        assert dcm._file_extension() == ''
Beispiel #28
0
    def test_get_item_dicom_attribute(self, dicom_generator):
        filename, dicom = dicom_generator(PatientName='Suever')
        dcm = Dicom(filename, dcm=dicom)

        assert dcm['PatientName'] == 'Suever'
Beispiel #29
0
    def test_get_image_type_no_type(self, dicom_generator):
        filename, dataset = dicom_generator()

        dcm = Dicom(filename, dcm=dataset)

        assert dcm._image_type() == 'Unknown'
Beispiel #30
0
    def test_get_image_type_generic(self, dicom_generator):
        filename, dataset = dicom_generator(ImageType=['A', 'B'])

        dcm = Dicom(filename, dcm=dataset)

        assert dcm._image_type() == 'Image'