Пример #1
0
def test_anonymise_directory():
    temp_anon_filepath = label_dicom_filepath_as_anonymised(temp_filepath)
    try:
        makedirs(temp_dirpath, exist_ok=True)
        copyfile(test_filepath, temp_filepath)
        assert not is_anonymised_directory(temp_dirpath)

        # Test file deletion
        anonymise_directory(temp_dirpath,
                            delete_original_files=False,
                            anonymise_filenames=False)
        # # File should be anonymised but not dir, since original file
        # # is still present.
        assert is_anonymised_file(temp_anon_filepath)
        assert exists(temp_filepath)
        assert not is_anonymised_directory(temp_dirpath)

        remove_file(temp_anon_filepath)
        anonymise_directory(temp_dirpath,
                            delete_original_files=True,
                            anonymise_filenames=False)
        # # File and dir should be anonymised since original file should
        # # have been deleted.
        assert is_anonymised_file(temp_anon_filepath)
        assert not exists(temp_filepath)
        assert is_anonymised_directory(temp_dirpath)

    finally:
        remove_file(temp_anon_filepath)
        remove_dir(temp_dirpath)
Пример #2
0
def test_anonymise_file():
    assert not is_anonymised_file(test_filepath)

    try:
        # Private tag handling
        anon_private_filepath = anonymise_file(test_filepath,
                                               delete_private_tags=False)
        assert not is_anonymised_file(anon_private_filepath,
                                      ignore_private_tags=False)
        assert is_anonymised_file(anon_private_filepath,
                                  ignore_private_tags=True)

        anon_private_filepath = anonymise_file(test_filepath,
                                               delete_private_tags=True)
        assert is_anonymised_file(anon_private_filepath,
                                  ignore_private_tags=False)

        # Filename is anonymised?
        assert basename(anon_private_filepath) == test_anon_basename

        # Deletion of original file
        temp_basename = "{}_{}.dcm".format(
            '.'.join(test_filepath.split('.')[:-1]), uuid4())

        temp_filepath = pjoin(dirname(test_filepath), temp_basename)
        copyfile(test_filepath, temp_filepath)

        anon_filepath_orig = anonymise_file(temp_filepath,
                                            delete_original_file=True)
        assert is_anonymised_file(anon_filepath_orig)
        assert not exists(temp_filepath)

        # Preservation of filename if desired
        expected_filepath = "{}_Anonymised.dcm".format('.'.join(
            test_filepath.split('.')[:-1]))
        anon_filepath_pres = anonymise_file(test_filepath,
                                            anonymise_filename=False)
        assert anon_filepath_pres == expected_filepath

    finally:
        remove_file(temp_filepath)
        remove_file(anon_private_filepath)
        remove_file(anon_filepath_orig)
        remove_file(anon_filepath_pres)
Пример #3
0
def _check_is_anonymised_dataset_file_and_dir(
    ds, tmp_path, anon_is_expected=True, ignore_private_tags=False
):
    temp_filepath = str(tmp_path / "test.dcm")

    try:
        ds.is_little_endian = True
        ds.is_implicit_VR = True
        ds.file_meta = TEST_FILE_META
        ds.save_as(temp_filepath, write_like_original=False)

        if anon_is_expected:
            assert is_anonymised_dataset(ds, ignore_private_tags)
            assert is_anonymised_file(temp_filepath, ignore_private_tags)
            assert is_anonymised_directory(tmp_path, ignore_private_tags)
        else:
            assert not is_anonymised_dataset(ds, ignore_private_tags)
            assert not is_anonymised_file(temp_filepath, ignore_private_tags)
            assert not is_anonymised_directory(tmp_path, ignore_private_tags)
    finally:
        remove_file(temp_filepath)
Пример #4
0
def test_anonymise_file():
    assert not is_anonymised_file(TEST_FILEPATH)
    temp_basename = "{}_{}.dcm".format(".".join(TEST_FILEPATH.split(".")[:-1]), uuid4())

    temp_filepath = pjoin(dirname(TEST_FILEPATH), temp_basename)
    anon_private_filepath = ""
    anon_filepath_orig = ""
    anon_filepath_pres = ""

    try:
        # Private tag handling
        anon_private_filepath = anonymise_file(TEST_FILEPATH, delete_private_tags=False)
        assert not is_anonymised_file(anon_private_filepath, ignore_private_tags=False)
        assert is_anonymised_file(anon_private_filepath, ignore_private_tags=True)

        anon_private_filepath = anonymise_file(TEST_FILEPATH, delete_private_tags=True)
        assert is_anonymised_file(anon_private_filepath, ignore_private_tags=False)

        # Filename is anonymised?
        assert basename(anon_private_filepath) == TEST_ANON_BASENAME

        # Deletion of original file
        copyfile(TEST_FILEPATH, temp_filepath)

        anon_filepath_orig = anonymise_file(temp_filepath, delete_original_file=True)
        assert is_anonymised_file(anon_filepath_orig)
        assert not exists(temp_filepath)

        # Preservation of filename if desired
        expected_filepath = "{}_Anonymised.dcm".format(
            ".".join(TEST_FILEPATH.split(".")[:-1])
        )
        anon_filepath_pres = anonymise_file(TEST_FILEPATH, anonymise_filename=False)
        assert anon_filepath_pres == expected_filepath

    finally:
        remove_file(temp_filepath)
        remove_file(anon_private_filepath)
        remove_file(anon_filepath_orig)
        remove_file(anon_filepath_pres)
Пример #5
0
def _check_is_anonymised_dataset_file_and_dir(ds,
                                              anon_is_expected=True,
                                              ignore_private_tags=False):
    try:
        makedirs(temp_dirpath, exist_ok=True)
        ds.is_little_endian = True
        ds.is_implicit_VR = True
        ds.file_meta = file_meta
        ds.save_as(temp_filepath, write_like_original=False)

        if anon_is_expected:
            assert is_anonymised_dataset(ds, ignore_private_tags)
            assert is_anonymised_file(temp_filepath, ignore_private_tags)
            assert is_anonymised_directory(temp_dirpath, ignore_private_tags)
        else:
            assert not is_anonymised_dataset(ds, ignore_private_tags)
            assert not is_anonymised_file(temp_filepath, ignore_private_tags)
            assert not is_anonymised_directory(temp_dirpath,
                                               ignore_private_tags)
    finally:
        remove_file(temp_filepath)
        remove_dir(temp_dirpath)
Пример #6
0
def test_anonymise_cli():

    try:
        makedirs(temp_dirpath, exist_ok=True)
        copyfile(test_filepath, temp_filepath)
        temp_anon_filepath = pjoin(temp_dirpath, test_anon_basename)

        # Basic file anonymisation
        assert not is_anonymised_file(temp_filepath)
        assert not exists(temp_anon_filepath)

        anon_file_command = ('pymedphys dicom anonymise'.split() +
                             [temp_filepath])
        try:
            subprocess.check_call(anon_file_command)
            assert is_anonymised_file(temp_anon_filepath)
            assert exists(temp_filepath)
        finally:
            remove_file(temp_anon_filepath)

        # File anonymisation - preserve filenames
        assert not is_anonymised_file(temp_filepath)

        expected_anon_filepath = label_dicom_filepath_as_anonymised(
            temp_filepath)
        assert not exists(expected_anon_filepath)

        anon_file_pres_command = ('pymedphys dicom anonymise -f'.split() +
                                  [temp_filepath])
        try:
            subprocess.check_call(anon_file_pres_command)
            assert is_anonymised_file(expected_anon_filepath)
            assert exists(temp_filepath)
        finally:
            remove_file(expected_anon_filepath)

        # File anonymisation - clear values
        assert not is_anonymised_file(temp_filepath)
        assert not exists(temp_anon_filepath)

        anon_file_clear_command = ('pymedphys dicom anonymise -c'.split() +
                                   [temp_filepath])
        try:
            subprocess.check_call(anon_file_clear_command)
            assert is_anonymised_file(temp_anon_filepath)
            assert pydicom.dcmread(temp_anon_filepath).PatientName == ''
            assert exists(temp_filepath)
        finally:
            remove_file(temp_anon_filepath)

        # File anonymisation - leave keywords unchanged
        assert not is_anonymised_file(temp_filepath)
        assert not exists(temp_anon_filepath)

        anon_file_keep_command = ('pymedphys dicom anonymise'.split() +
                                  [temp_filepath] + '-k PatientName'.split())
        try:
            subprocess.check_call(anon_file_keep_command)
            assert not is_anonymised_file(temp_anon_filepath)
            ds = pydicom.dcmread(temp_anon_filepath)
            ds.PatientName = "Anonymous"
            assert is_anonymised_dataset(ds)
            assert exists(temp_filepath)
        finally:
            remove_file(temp_anon_filepath)

        # File anonymisation - private tag handling
        assert not is_anonymised_file(temp_filepath)
        assert not exists(temp_anon_filepath)

        anon_file_private_command = ('pymedphys dicom anonymise -p'.split() +
                                     [temp_filepath])
        try:
            subprocess.check_call(anon_file_private_command)
            assert not is_anonymised_file(temp_anon_filepath)
            assert is_anonymised_file(temp_anon_filepath,
                                      ignore_private_tags=True)
            assert exists(temp_filepath)
        finally:
            remove_file(temp_anon_filepath)

        # TODO: File anonymisation - unknown tag handling
        # # Calling a subprocess reloads BaselineDicomDictionary...

        # Basic dir anonymisation
        assert not is_anonymised_directory(DATA_DIR)
        assert not exists(temp_anon_filepath)

        anon_dir_command = ('pymedphys dicom anonymise'.split() +
                            [temp_dirpath])
        try:
            subprocess.check_call(anon_dir_command)
            assert is_anonymised_file(temp_anon_filepath)
            assert exists(temp_filepath)
        finally:
            remove_file(temp_anon_filepath)
    finally:
        remove_file(temp_filepath)
        remove_dir(temp_dirpath)
Пример #7
0
def test_anonymise_cli(tmp_path):

    temp_filepath = str(tmp_path / "test.dcm")
    try:
        copyfile(TEST_FILEPATH, temp_filepath)
        temp_anon_filepath = str(tmp_path / TEST_ANON_BASENAME)
        # Basic file anonymisation
        assert not is_anonymised_file(temp_filepath)
        assert not exists(temp_anon_filepath)

        anon_file_command = "pymedphys dicom anonymise".split() + [temp_filepath]
        try:
            subprocess.check_call(anon_file_command)
            assert is_anonymised_file(temp_anon_filepath)
            assert exists(temp_filepath)
        finally:
            remove_file(temp_anon_filepath)

        # File anonymisation - preserve filenames
        assert not is_anonymised_file(temp_filepath)

        expected_anon_filepath = label_dicom_filepath_as_anonymised(temp_filepath)
        assert not exists(expected_anon_filepath)

        anon_file_pres_command = "pymedphys dicom anonymise -f".split() + [
            temp_filepath
        ]
        try:
            subprocess.check_call(anon_file_pres_command)
            assert is_anonymised_file(expected_anon_filepath)
            assert exists(temp_filepath)
        finally:
            remove_file(expected_anon_filepath)

        # File anonymisation - clear values
        assert not is_anonymised_file(temp_filepath)
        assert not exists(temp_anon_filepath)

        temp_cleared_anon_filepath = str(tmp_path / TEST_ANON_BASENAME)

        anon_file_clear_command = "pymedphys dicom anonymise -c".split() + [
            temp_filepath
        ]
        try:
            subprocess.check_call(anon_file_clear_command)
            assert is_anonymised_file(temp_cleared_anon_filepath)
            assert pydicom.dcmread(temp_cleared_anon_filepath).PatientName == ""
            assert exists(temp_filepath)
        finally:
            remove_file(temp_cleared_anon_filepath)

        # File anonymisation - leave keywords unchanged
        assert not is_anonymised_file(temp_filepath)
        assert not exists(temp_anon_filepath)

        anon_file_keep_command = (
            "pymedphys dicom anonymise".split()
            + [temp_filepath]
            + "-k PatientName".split()
        )
        try:
            subprocess.check_call(anon_file_keep_command)
            assert not is_anonymised_file(temp_anon_filepath)
            ds = pydicom.dcmread(temp_anon_filepath)
            ds.PatientName = "Anonymous"
            assert is_anonymised_dataset(ds)
            assert exists(temp_filepath)
        finally:
            remove_file(temp_anon_filepath)

        # File anonymisation - private tag handling
        assert not is_anonymised_file(temp_filepath)
        assert not exists(temp_anon_filepath)

        anon_file_private_command = "pymedphys dicom anonymise -p".split() + [
            temp_filepath
        ]
        try:
            subprocess.check_call(anon_file_private_command)
            assert not is_anonymised_file(temp_anon_filepath)
            assert is_anonymised_file(temp_anon_filepath, ignore_private_tags=True)
            assert exists(temp_filepath)
        finally:
            remove_file(temp_anon_filepath)

        # TODO: File anonymisation - unknown tag handling
        # # Calling a subprocess reloads BASELINE_DICOM_DICT...

        # Basic dir anonymisation
        assert not is_anonymised_directory(tmp_path)
        assert not exists(temp_anon_filepath)

        anon_dir_command = "pymedphys dicom anonymise".split() + [str(tmp_path)]
        try:
            subprocess.check_call(anon_dir_command)
            assert is_anonymised_file(temp_anon_filepath)
            assert exists(temp_filepath)
        finally:
            remove_file(temp_anon_filepath)
    finally:
        remove_file(temp_filepath)