def validate_dataset(*, dataset_file, reference_file, dataset_group, reference_group): try: dataset_file = ismrmrd.File(dataset_file, 'r') except OSError as e: return Failure, "Failed to read dataset file '{}'".format(dataset_file) try: reference_file = ismrmrd.File(reference_file, 'r') except OSError as e: return Failure, "Failed to read reference file '{}'".format( reference_file) header = dataset_file[dataset_group].header ref_header = reference_file[reference_group].header if not dataset_file[dataset_group].header == reference_file[ reference_group].header: import deepdiff diff = deepdiff.diff.DeepDiff(header, ref_header) print(diff.pretty()) return Failure, "Dataset header did not match reference header" for attribute in ['acquisitions', 'waveforms', 'images']: dataset = getattr(dataset_file[dataset_group], attribute) or [] reference = getattr(reference_file[reference_group], attribute) or [] if not list(dataset) == list(reference): return Failure, "Dataset {attr} did not match reference {attr}".format( attr=attribute) return None, "Dataset matched reference"
def test_file_cannot_write_data_on_image(): filename = os.path.join(temp_dir, "file.h5") with ismrmrd.File(filename) as file: dataset = file['dataset'] dataset.images = random_images(1) with ismrmrd.File(filename) as file: dataset = file['dataset'] dataset.acquisitions = random_acquisitions(10)
def test_file_can_access_random_acquisition(): filename = os.path.join(temp_dir, "acquisitions.h5") acquisitions = list(random_acquisitions(256)) with ismrmrd.File(filename) as file: dataset = file['dataset'] dataset.acquisitions = acquisitions with ismrmrd.File(filename) as file: dataset = file['dataset'] assert acquisitions[255] == dataset.acquisitions[255]
def test_file_can_read_and_write_headers(): filename = os.path.join(temp_dir, "file.h5") header = ismrmrd.xsd.CreateFromDocument(example_header) with ismrmrd.File(filename) as file: dataset = file['dataset'] dataset.header = header with ismrmrd.File(filename) as file: dataset = file['dataset'] assert header.toxml() == dataset.header.toxml()
def test_file_can_read_and_write_waveforms(): filename = os.path.join(temp_dir, "waveforms.h5") waveforms = list(random_waveforms(10)) with ismrmrd.File(filename) as file: dataset = file['dataset'] dataset.waveforms = waveforms with ismrmrd.File(filename) as file: dataset = file['dataset'] for a, b in zip(waveforms, dataset.waveforms): assert a == b
def test_file_can_read_random_image(): filename = os.path.join(temp_dir, "images.h5") images = list(random_images(10)) with ismrmrd.File(filename) as file: imageset = file['dataset/image_1'] imageset.images = images with ismrmrd.File(filename) as file: imageset = file['dataset/image_1'] assert images[8] == imageset.images[8]
def test_file_can_read_image_slice(): filename = os.path.join(temp_dir, "images.h5") images = list(random_images(10)) with ismrmrd.File(filename) as file: imageset = file['dataset/image_1'] imageset.images = images with ismrmrd.File(filename) as file: imageset = file['dataset/image_1'] for a, b in zip(images[5:10], imageset.images[5:10]): assert a == b
def test_file_can_list_keys(): filename = os.path.join(temp_dir, "keys.h5") with ismrmrd.File(filename) as file: dataset = file['dataset'] dataset.acquisitions = random_acquisitions(10) dataset2 = file['dataset2'] dataset2.acquisitions = random_acquisitions(10) with ismrmrd.File(filename) as file: assert (file.keys() == {'dataset', 'dataset2'})
def test_file_can_read_and_write_acquisitions(): filename = os.path.join(temp_dir, "acquisitions.h5") acquisitions = list(random_acquisitions(10)) with ismrmrd.File(filename) as file: dataset = file['dataset'] dataset.acquisitions = acquisitions with ismrmrd.File(filename) as file: dataset = file['dataset'] for a, b in zip(acquisitions, dataset.acquisitions): assert a == b
def test_file_can_write_random_acquisition(): filename = os.path.join(temp_dir, "acquisitions.h5") acquisitions = list(random_acquisitions(256)) acquisition = create_random_acquisition() with ismrmrd.File(filename) as file: dataset = file['dataset'] dataset.acquisitions = acquisitions dataset.acquisitions[200] = acquisition with ismrmrd.File(filename) as file: dataset = file['dataset'] assert acquisition == dataset.acquisitions[200]
def test_file_can_access_random_acquisition_slice(): filename = os.path.join(temp_dir, "acquisitions.h5") acquisitions = list(random_acquisitions(256)) with ismrmrd.File(filename) as file: dataset = file['dataset'] dataset.acquisitions = acquisitions with ismrmrd.File(filename) as file: dataset = file['dataset'] for a, b in zip(acquisitions[250:255], dataset.acquisitions[250:255]): assert a == b
def test_file_can_write_random_image(): filename = os.path.join(temp_dir, "images.h5") image = create_random_image() with ismrmrd.File(filename) as file: imageset = file['dataset/image_1'] imageset.images = random_images(10) imageset.images[6] = image with ismrmrd.File(filename) as file: imageset = file['dataset/image_1'] assert image == imageset.images[6]
def test_file_can_write_random_acquisition_slice(): filename = os.path.join(temp_dir, "acquisitions.h5") acquisitions = list(random_acquisitions(256)) slice = list(random_acquisitions(3)) with ismrmrd.File(filename) as file: dataset = file['dataset'] dataset.acquisitions = acquisitions dataset.acquisitions[150:153] = slice with ismrmrd.File(filename) as file: dataset = file['dataset'] for a, b in zip(slice, dataset.acquisitions[150:153]): assert a == b
def test_file_can_rewrite_data_and_images(): filename = os.path.join(temp_dir, "file.h5") with ismrmrd.File(filename) as file: dataset = file['dataset'] dataset.acquisitions = random_acquisitions(20) dataset.acquisitions = random_acquisitions(10) dataset.waveforms = random_waveforms(10) dataset.waveforms = random_waveforms(5) dataset.acquisitions = random_acquisitions(5) with ismrmrd.File(filename) as file: imageset = file['dataset/images'] imageset.images = random_images(1) imageset.images = random_images(2) imageset.images = random_images(3)
def test_file_returns_none_when_no_acquisitions_present(): filename = os.path.join(temp_dir, "acquisitions.h5") acquisitions = list(random_acquisitions(10)) with ismrmrd.File(filename) as file: dataset = file['dataset'] assert not dataset.has_acquisitions() assert dataset.acquisitions is None dataset.acquisitions = acquisitions with ismrmrd.File(filename) as file: dataset = file['dataset'] assert dataset.has_acquisitions() assert dataset.acquisitions is not None
def test_file_cannot_write_mismatched_slice(): filename = os.path.join(temp_dir, "acquisitions.h5") acquisitions = list(random_acquisitions(256)) slice = list(random_acquisitions(3)) with ismrmrd.File(filename) as file: dataset = file['dataset'] dataset.acquisitions = acquisitions dataset.acquisitions[150:155] = slice
def test_file_can_list_contained_images(): filename = os.path.join(temp_dir, "find_file.h5") with ismrmrd.File(filename) as file: dataset = file['dataset'] dataset.acquisitions = random_acquisitions(10) imageset = file['dataset/image_1'] imageset.images = random_images(1) imageset = file['dataset/image_2'] imageset.images = random_images(2) imageset = file['dataset/nested/image_3'] imageset.images = [create_random_image(3)] with ismrmrd.File(filename) as file: assert (file.find_images() == { 'dataset/image_1', 'dataset/image_2', 'dataset/nested/image_3' }) assert (file.find_data() == {'dataset'})
def __init__(self, parent, file_name): super().__init__(parent) self.tree = QtWidgets.QTreeWidget(self) self.tree.setHeaderHidden(True) self.tree.itemClicked.connect(lambda widget, _: self.set_viewer(widget.container, widget.viewer)) FileWidget.__populate_tree(self.tree, ismrmrd.File(file_name, mode='r')) self.viewer = QtWidgets.QListWidget(self) self.addWidget(self.tree) self.addWidget(self.viewer) self.__balance()
def test_file_can_delete_acquisitions(): filename = os.path.join(temp_dir, "acquisitions.h5") with ismrmrd.File(filename) as file: dataset = file['dataset'] assert dataset.acquisitions is None del dataset.acquisitions assert dataset.acquisitions is None dataset.acquisitions = random_acquisitions(10) assert dataset.acquisitions is not None del dataset.acquisitions assert dataset.acquisitions is None
def validate_image_header(*, output_file, reference_file, output_group, reference_group): def equals(): return lambda out, ref: out == ref def approx(threshold=1e-6): return lambda out, ref: abs(out - ref) <= threshold def ignore(): return lambda out, ref: True def each(rule): return lambda out, ref: all( rule(out, ref) for out, ref in itertools.zip_longest(out, ref)) header_rules = { 'version': equals(), 'data_type': equals(), 'flags': equals(), 'measurement_uid': equals(), 'matrix_size': each(equals()), 'field_of_view': each(approx()), 'channels': equals(), 'position': each(approx()), 'read_dir': each(approx()), 'phase_dir': each(approx()), 'slice_dir': each(approx()), 'patient_table_position': each(approx()), 'average': equals(), 'slice': equals(), 'contrast': equals(), 'phase': equals(), 'repetition': equals(), 'set': equals(), 'acquisition_time_stamp': ignore(), 'physiology_time_stamp': each(ignore()), 'image_type': equals(), 'image_index': equals(), 'image_series_index': ignore(), 'user_int': each(equals()), 'user_float': each(approx()), 'attribute_string_len': ignore() } def check_image_header(output, reference): if not output: raise RuntimeError("Missing output") if not reference: raise RuntimeError("Missing reference") output = output.getHead() reference = reference.getHead() for attribute, rule in header_rules.items(): if not rule(getattr(output, attribute), getattr(reference, attribute)): print(output) print(reference) raise RuntimeError( "Image header '{}' does not match reference. [index {}, series {}]" .format(attribute, output.image_index, output.image_series_index)) try: with ismrmrd.File(output_file, 'r') as output_file: with ismrmrd.File(reference_file, 'r') as reference_file: output_images = output_file[output_group].images or [] reference_images = reference_file[reference_group].images or [] for output_image, reference_image in itertools.zip_longest( output_images, reference_images): check_image_header(output_image, reference_image) except OSError as e: return Failure, str(e) except RuntimeError as e: return Failure, str(e) return None, "Output headers matched reference"