def setUp(self): self.acquisition_metadata = create_complete_acquisition_meta_data_dictionary( ) self.device_metadata = create_complete_device_metadata_dictionary() self.pa_data = PAData(binary_time_series_data=np.random.random( (4, 200)), meta_data_acquisition=self.acquisition_metadata, meta_data_device=self.device_metadata) print("setUp")
def test_write_and_read_random_dictionary(self): device_dict = create_complete_device_metadata_dictionary() pa_data = PAData( binary_time_series_data=np.zeros([256, 2048]), meta_data_acquisition={ "test_int": 3, "test_float": 3.14, "test_string": "ipasc_test", "test_list": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], "test_nested_list": [1, 2, 5, { "key": [1, ["1", 1]] }] }, meta_data_device=device_dict) try: write_data("ipasc_test.hdf5", pa_data) test_data = load_data("ipasc_test.hdf5") except Exception as e: raise e finally: # clean up after ipasc_test if os.path.exists("ipasc_test.hdf5"): os.remove("ipasc_test.hdf5") def assertEqualsRecursive(a, b): if isinstance(a, dict): for item in a: self.assertTrue(item in a) self.assertTrue(item in b) if isinstance(a[item], dict): assertEqualsRecursive(a[item], b[item]) else: if isinstance(a[item], np.ndarray): self.assertTrue((a[item] == b[item]).all()) elif isinstance(a[item], list): for item1, item2 in zip(a[item], b[item]): assertEqualsRecursive(item1, item2) else: self.assertEqual(a[item], b[item]) elif isinstance(a, list): for item1, item2 in zip(a, b): assertEqualsRecursive(item1, item2) else: if isinstance(a, np.ndarray): self.assertTrue((a == b).all()) else: self.assertEqual(a, b) assertEqualsRecursive(pa_data.meta_data_acquisition, test_data.meta_data_acquisition) assertEqualsRecursive(pa_data.meta_data_device, test_data.meta_data_device) self.assertTrue((pa_data.binary_time_series_data == test_data.binary_time_series_data).all())
def load_data(path: str): """ TODO :param path: Path to an hdf5 file containing PAData. :return: PAData instance """ def recursively_load_dictionaries(file, in_file_path): """ TODO :param file: instance of an hdf5 File object :param in_file_path: Path inside the file object group structure :return: Dictionary instance """ data = {} for key, item in h5file[in_file_path].items(): if isinstance(item, h5py._hl.dataset.Dataset): if item[()] is not None: data[key] = item[()] else: data[key] = None elif isinstance(item, h5py._hl.group.Group): if key == "list": data = [None for x in item.keys()] for listkey in sorted(item.keys()): if isinstance(item[listkey], h5py._hl.dataset.Dataset): data[int(listkey)] = item[listkey][()] elif isinstance(item[listkey], h5py._hl.group.Group): data[int(listkey)] = recursively_load_dictionaries( file, in_file_path + key + "/" + listkey + "/") else: data[key] = recursively_load_dictionaries( file, in_file_path + key + "/") return data with h5py.File(path, "r") as h5file: binary_data = h5file["/binary_time_series_data"][()] pa_data = PAData(binary_data) pa_data.meta_data_acquisition = recursively_load_dictionaries( h5file, "/meta_data/") pa_data.meta_data_device = recursively_load_dictionaries( h5file, "/meta_data_device/") return pa_data
def test_check_a_complete_and_consistent_pa_data_instance(self): device_dict = create_complete_device_metadata_dictionary() acquisition_dict = create_complete_acquisition_meta_data_dictionary() pa_data = PAData(binary_time_series_data=np.zeros([256, 2048]), meta_data_acquisition=acquisition_dict, meta_data_device=device_dict) completeness_checker = CompletenessChecker(verbose=True) consistency_checker = ConsistencyChecker(verbose=True) assert completeness_checker.check_meta_data(pa_data.meta_data_acquisition) assert completeness_checker.check_meta_data_device(pa_data.meta_data_device) assert consistency_checker.check_binary(pa_data.binary_time_series_data) assert consistency_checker.check_meta_data(pa_data.meta_data_acquisition) assert consistency_checker.check_meta_data_device(pa_data.meta_data_device)
def test_check_a_complete_but_inconsistent_pa_data_instance(self): device_dict = create_complete_device_metadata_dictionary() for illuminator_tag in device_dict[MetadataDeviceTags.ILLUMINATORS.tag]: device_dict[MetadataDeviceTags.ILLUMINATORS.tag][illuminator_tag]\ [MetadataDeviceTags.PULSE_WIDTH.tag] = -0.1 acquisition_dict = create_complete_acquisition_meta_data_dictionary() acquisition_dict[MetadataAcquisitionTags.DIMENSIONALITY.tag] = "Wrong string" pa_data = PAData(binary_time_series_data=np.zeros([256, 2048]), meta_data_acquisition=acquisition_dict, meta_data_device=device_dict) completeness_checker = CompletenessChecker(verbose=True) consistency_checker = ConsistencyChecker(verbose=True) assert completeness_checker.check_meta_data(pa_data.meta_data_acquisition) assert completeness_checker.check_meta_data_device(pa_data.meta_data_device) assert consistency_checker.check_binary(pa_data.binary_time_series_data) assert consistency_checker.check_meta_data(pa_data.meta_data_acquisition) is False assert consistency_checker.check_meta_data_device(pa_data.meta_data_device) is False
def load_data(file_path, file_dictionary_path="/"): """ Loads a dictionary from an hdf5 file. The MIT License (MIT) Copyright (c) 2021 Computer Assisted Medical Interventions Group, DKFZ Copyright (c) 2021 VISION Lab, Cancer Research UK Cambridge Institute (CRUK CI) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. :param file_path: Path of the file to load the dictionary from. :param file_dictionary_path: Path in dictionary structure of hdf5 file to lo the dictionary in. :returns: Dictionary """ def recursively_load_dictionaries(file, path): """ Helper function which recursively loads data from the hdf5 group structure to a dictionary. :param file: hdf5 file instance to load the data from. :param path: Current group path in hdf5 file group structure. :returns: Dictionary """ dictionary = {} for key, item in h5file[path].items(): if isinstance(item, h5py._hl.dataset.Dataset): if item[()] is not None: dictionary[key] = item[()] if isinstance(dictionary[key], bytes): dictionary[key] = dictionary[key].decode("utf-8") elif isinstance(dictionary[key], np.bool_): dictionary[key] = bool(dictionary[key]) else: dictionary[key] = None elif isinstance(item, h5py._hl.group.Group): if key == "list": dictionary_list = [None for x in item.keys()] for listkey in sorted(item.keys()): print(listkey) if isinstance(item[listkey], h5py._hl.dataset.Dataset): dictionary_list[int(listkey)] = item[listkey][()] elif isinstance(item[listkey], h5py._hl.group.Group): dictionary_list[int( listkey)] = recursively_load_dictionaries( file, path + key + "/" + listkey + "/") dictionary = dictionary_list else: dictionary[key] = recursively_load_dictionaries( file, path + key + "/") return dictionary with h5py.File(file_path, "r") as h5file: binary_data = h5file["/binary_time_series_data"][()] pa_data = PAData(binary_data) pa_data.meta_data_acquisition = recursively_load_dictionaries( h5file, "/meta_data/") pa_data.meta_data_device = recursively_load_dictionaries( h5file, "/meta_data_device/") return pa_data
class MetaDataTest(TestCase): def setUp(self): self.acquisition_metadata = create_complete_acquisition_meta_data_dictionary( ) self.device_metadata = create_complete_device_metadata_dictionary() self.pa_data = PAData(binary_time_series_data=np.random.random( (4, 200)), meta_data_acquisition=self.acquisition_metadata, meta_data_device=self.device_metadata) print("setUp") def tearDown(self): print("tearDown") def test_get_general_information(self): assert self.pa_data.get_device_uuid() is not None assert self.pa_data.get_field_of_view() is not None assert self.pa_data.get_number_of_detectors() is not None assert self.pa_data.get_number_of_illuminators() is not None def test_get_illuminator_position(self): assert self.pa_data.get_illuminator_position() is not None assert self.pa_data.get_illuminator_position( list(self.pa_data.get_illuminator_ids())[0]) is not None assert self.pa_data.get_illuminator_position(0) is not None def test_get_illuminator_orientation(self): assert self.pa_data.get_illuminator_orientation() is not None assert self.pa_data.get_illuminator_orientation( list(self.pa_data.get_illuminator_ids())[0]) is not None assert self.pa_data.get_illuminator_orientation(0) is not None def test_get_illuminator_size(self): assert self.pa_data.get_illuminator_geometry() is not None assert self.pa_data.get_illuminator_geometry( list(self.pa_data.get_illuminator_ids())[0]) is not None assert self.pa_data.get_illuminator_geometry(0) is not None def test_get_wavelength_range(self): assert self.pa_data.get_wavelength_range() is not None assert self.pa_data.get_wavelength_range( list(self.pa_data.get_illuminator_ids())[0]) is not None assert self.pa_data.get_wavelength_range(0) is not None def test_get_energy_profile(self): assert self.pa_data.get_energy_profile() is not None assert self.pa_data.get_energy_profile( list(self.pa_data.get_illuminator_ids())[0]) is not None assert self.pa_data.get_energy_profile(0) is not None def test_get_stability_profile(self): assert self.pa_data.get_stability_profile() is not None assert self.pa_data.get_stability_profile( list(self.pa_data.get_illuminator_ids())[0]) is not None assert self.pa_data.get_stability_profile(0) is not None def test_get_pulse_duration(self): assert self.pa_data.get_pulse_width() is not None assert self.pa_data.get_pulse_width( list(self.pa_data.get_illuminator_ids())[0]) is not None assert self.pa_data.get_pulse_width(0) is not None def test_get_beam_intensity_profile(self): assert self.pa_data.get_beam_profile() is not None assert self.pa_data.get_beam_profile( list(self.pa_data.get_illuminator_ids())[0]) is not None assert self.pa_data.get_beam_profile(0) is not None def test_get_beam_divergence_angles(self): assert self.pa_data.get_beam_divergence() is not None assert self.pa_data.get_beam_divergence( list(self.pa_data.get_illuminator_ids())[0]) is not None assert self.pa_data.get_beam_divergence(0) is not None def test_get_detector_position(self): assert self.pa_data.get_detector_position() is not None assert self.pa_data.get_detector_position( list(self.pa_data.get_detector_ids())[0]) is not None assert self.pa_data.get_detector_position(0) is not None def test_get_detector_orientation(self): assert self.pa_data.get_detector_orientation() is not None assert self.pa_data.get_detector_orientation( list(self.pa_data.get_detector_ids())[0]) is not None assert self.pa_data.get_detector_orientation(0) is not None def test_get_detector_size(self): assert self.pa_data.get_detector_size() is not None assert self.pa_data.get_detector_size( list(self.pa_data.get_detector_ids())[0]) is not None assert self.pa_data.get_detector_size(0) is not None def test_get_frequency_response(self): assert self.pa_data.get_frequency_response() is not None assert self.pa_data.get_frequency_response( list(self.pa_data.get_detector_ids())[0]) is not None assert self.pa_data.get_frequency_response(0) is not None def test_get_angular_response(self): assert self.pa_data.get_angular_response() is not None assert self.pa_data.get_angular_response( list(self.pa_data.get_detector_ids())[0]) is not None assert self.pa_data.get_angular_response(0) is not None def test_aquisition_metadata(self): assert self.pa_data.get_encoding() is not None assert self.pa_data.get_compression() is not None assert self.pa_data.get_data_UUID() is not None assert self.pa_data.get_data_type() is not None assert self.pa_data.get_dimensionality() is not None assert self.pa_data.get_sizes() is not None assert self.pa_data.get_device_reference() is not None assert self.pa_data.get_pulse_laser_energy() is not None assert self.pa_data.get_time_stamps() is not None assert self.pa_data.get_wavelengths() is not None assert self.pa_data.get_time_gain_compensation() is not None assert self.pa_data.get_overall_gain() is not None assert self.pa_data.get_element_dependent_gain() is not None assert self.pa_data.get_temperature() is not None assert self.pa_data.get_coupling_agent() is not None assert self.pa_data.get_scanning_method() is not None assert self.pa_data.get_sampling_rate() is not None assert self.pa_data.get_frequency_filter() is not None