def test_anonymise_directory(tmp_path): temp_filepath = tmp_path / "test.dcm" temp_anon_filepath = label_dicom_filepath_as_anonymised(temp_filepath) try: copyfile(TEST_FILEPATH, temp_filepath) assert not is_anonymised_directory(tmp_path) # Test file deletion anonymise_directory( tmp_path, 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(tmp_path) remove_file(temp_anon_filepath) anonymise_directory( tmp_path, 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(tmp_path) finally: remove_file(temp_anon_filepath)
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)
def test_anonymise_directory(tmp_path): temp_filepath = tmp_path / "test.dcm" temp_anon_filepath = label_dicom_filepath_as_anonymised(temp_filepath) temp_record_filepath = tmp_path / "test_record.dcm" temp_anon_record_filepath = label_dicom_filepath_as_anonymised(temp_record_filepath) try: copyfile(get_rtplan_test_file_path(), temp_filepath) assert not is_anonymised_directory(tmp_path) # Test file deletion anon_path_list = anonymise_directory( tmp_path, 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(tmp_path) assert anon_path_list is not None assert anon_path_list[0] == temp_anon_filepath remove_file(temp_anon_filepath) anon_path_list = anonymise_directory( tmp_path, 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(tmp_path) assert anon_path_list[0] == temp_anon_filepath # Test fail fast vs. fail at last # if the function fails fast, the specified removal # will not take place # if the function does not fail fail, the specified # removal will take place logging.warning("Testing fail fast") remove_file(temp_anon_filepath) copyfile(get_rtplan_test_file_path(), temp_filepath) copyfile(get_treatment_record_test_file_path(), temp_record_filepath) ds_record = pydicom.dcmread(temp_record_filepath, force=True) # deliberately add a DICOM element that is not in the current # dictionary ds_record.add_new([0x300A, 0x9999], "FL", [1.0, 1.0]) pydicom.dcmwrite(temp_record_filepath, ds_record) with pytest.raises((KeyError, ValueError)): anon_path_list = anonymise_directory( tmp_path, delete_original_files=True, anonymise_filenames=False, fail_fast=True, ) logging.warning(anon_path_list) assert exists(temp_filepath) with pytest.raises((KeyError, ValueError)): anon_path_list = anonymise_directory( tmp_path, delete_original_files=True, anonymise_filenames=False, fail_fast=False, ) logging.warning(anon_path_list) assert not exists(temp_filepath) finally: remove_file(temp_anon_filepath) remove_file(temp_anon_record_filepath)