def _verify_loss_map(self, path, expected_data): namespaces = dict(nrml=xml.NRML_NS, gml=xml.GML_NS) root = etree.parse(path) actual_lm_data = [] lm_nodes = root.xpath('.//nrml:LMNode', namespaces=namespaces) # sanity check: there should be only 3 loss map nodes: self.assertEqual(3, len(lm_nodes)) for node in lm_nodes: node_data = dict() [pos] = node.xpath('.//gml:pos', namespaces=namespaces) node_data['pos'] = pos.text [loss] = node.xpath('./nrml:loss', namespaces=namespaces) node_data['asset'] = loss.get('assetRef') [mean] = loss.xpath('./nrml:mean', namespaces=namespaces) [stddev] = loss.xpath('./nrml:stdDev', namespaces=namespaces) node_data['mean'] = float(mean.text) node_data['stddev'] = float(stddev.text) actual_lm_data.append(node_data) helpers.assertDeepAlmostEqual(self, sorted(expected_data), sorted(actual_lm_data), places=self.LOSSMAP_PRECISION)
def test_bug_932765(self): # Test to directly address this bug: # https://bugs.launchpad.net/openquake/+bug/932765 # To make this test work, we basically have to define a list of epsilon # limits which has a different length from all of the others. eps_limits = [-0.5, +0.5, +1.5] subset_name = 'MagDistEpsPMF' file_name = '%s.dat' % subset_name target_path = os.path.join(self.tempdir, '%s_932765.hdf5' % subset_name) # load in the test data data = self.read_data_file( file_name, [self.NMAG - 1, self.NDIST - 1, self.NEPS - 1]) # chop the test data to match what we expected with the shortened # epsilon limits we've defined (see above) expected_data = data[:, :, 0:2] disagg_subsets.extract_subsets(666, self.SITE, self.full_matrix_path, self.LATITUDE_BIN_LIMITS, self.LONGITUDE_BIN_LIMITS, self.MAGNITUDE_BIN_LIMITS, eps_limits, self.DISTANCE_BIN_LIMITS, target_path, [subset_name]) actual = h5py.File(target_path, 'r')[subset_name].value helpers.assertDeepAlmostEqual(self, expected_data, actual)
def _test_pmf(self, name, result_shape): target_path = os.path.join(self.tempdir, '%s.hdf5' % name) disagg_subsets.extract_subsets( 111, self.SITE, self.full_matrix_path, self.LATITUDE_BIN_LIMITS, self.LONGITUDE_BIN_LIMITS, self.MAGNITUDE_BIN_LIMITS, self.EPSILON_BIN_LIMITS, self.DISTANCE_BIN_LIMITS, target_path, [name]) expected_result = self.read_data_file('%s.dat' % name, result_shape) result = h5py.File(target_path, 'r')[name].value helpers.assertDeepAlmostEqual(self, expected_result, result)
def test_compute_beta_dist(self): # expected beta distributions provided by Vitor expected_beta_distributions = [ [1.0000000, 1.0000000, 1.0000000, 1.0000000, 1.0000000], [0.9895151, 0.9999409, 1.0000000, 1.0000000, 1.0000000], [0.9175720, 0.9981966, 0.9999997, 1.0000000, 1.0000000], [0.7764311, 0.9887521, 0.9999922, 1.0000000, 1.0000000], [0.6033381, 0.9633258, 0.9999305, 1.0000000, 1.0000000], [0.4364471, 0.9160514, 0.9996459, 1.0000000, 1.0000000], [0.2975979, 0.8460938, 0.9987356, 1.0000000, 1.0000000], [0.1931667, 0.7574557, 0.9964704, 1.0000000, 1.0000000], [0.1202530, 0.6571491, 0.9917729, 0.9999999, 1.0000000], [0.0722091, 0.5530379, 0.9832939, 0.9999997, 1.0000000], [0.0420056, 0.4521525, 0.9695756, 0.9999988, 1.0000000], [0.0130890, 0.2790107, 0.9213254, 0.9999887, 1.0000000], [0.0037081, 0.1564388, 0.8409617, 0.9999306, 1.0000000], [0.0009665, 0.0805799, 0.7311262, 0.9996882, 1.0000000], [0.0002335, 0.0384571, 0.6024948, 0.9988955, 1.0000000], [0.0000526, 0.0171150, 0.4696314, 0.9967629, 1.0000000], [0.0000022, 0.0027969, 0.2413923, 0.9820831, 1.0000000], [0.0000001, 0.0003598, 0.0998227, 0.9364072, 1.0000000], [0.0000000, 0.0000367, 0.0334502, 0.8381920, 0.9999995], [0.0000000, 0.0000030, 0.0091150, 0.6821293, 0.9999959], [0.0000000, 0.0000002, 0.0020162, 0.4909782, 0.9999755], [0.0000000, 0.0000000, 0.0000509, 0.1617086, 0.9995033], [0.0000000, 0.0000000, 0.0000005, 0.0256980, 0.9945488], [0.0000000, 0.0000000, 0.0000000, 0.0016231, 0.9633558], [0.0000000, 0.0000000, 0.0000000, 0.0000288, 0.8399534], [0.0000000, 0.0000000, 0.0000000, 0.0000001, 0.5409583], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.3413124], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.1589844], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0421052], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0027925], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0000000] ] vuln_function = shapes.VulnerabilityFunction(self.imls, self.mean_loss_ratios, self.covs) # steps = 5 loss_ratios = _generate_loss_ratios(vuln_function, 5) lrem = numpy.empty((len(loss_ratios), vuln_function.imls.size), float) for col, _ in enumerate(vuln_function): for row, loss_ratio in enumerate(loss_ratios): lrem[row][col] = BetaDistribution.survival_function( loss_ratio, col=col, vf=vuln_function) helpers.assertDeepAlmostEqual(self, expected_beta_distributions, lrem, delta=0.0005)
def _test_pmf(self, name, result_shape): target_path = os.path.join(self.tempdir, '%s.hdf5' % name) disagg_subsets.extract_subsets( 111, self.SITE, self.full_matrix_path, self.LATITUDE_BIN_LIMITS, self.LONGITUDE_BIN_LIMITS, self.MAGNITUDE_BIN_LIMITS, self.EPSILON_BIN_LIMITS, self.DISTANCE_BIN_LIMITS, target_path, [name] ) expected_result = self.read_data_file('%s.dat' % name, result_shape) result = h5py.File(target_path, 'r')[name].value helpers.assertDeepAlmostEqual(self, expected_result, result)
def test_compute_bcr_in_the_classical_psha_calculator(self): self._compute_risk_classical_psha_setup() helpers.delete_profile(self.job) bcr_config = helpers.demo_file('benefit_cost_ratio/config.gem') job_profile, params, sections = engine.import_job_profile( bcr_config, self.job) # We need to adjust a few of the parameters for this test: job_profile.imls = [ 0.005, 0.007, 0.0098, 0.0137, 0.0192, 0.0269, 0.0376, 0.0527, 0.0738, 0.103, 0.145, 0.203, 0.284, 0.397, 0.556, 0.778] params['ASSET_LIFE_EXPECTANCY'] = '50' job_profile.asset_life_expectancy = 50 params['REGION_VERTEX'] = '0.0, 0.0, 0.0, 2.0, 2.0, 2.0, 2.0, 0.0' job_profile.region = GEOSGeometry(shapes.polygon_ewkt_from_coords( params['REGION_VERTEX'])) job_profile.save() job_ctxt = engine.JobContext( params, self.job_id, sections=sections, oq_job_profile=job_profile) calculator = classical_core.ClassicalRiskCalculator(job_ctxt) [input] = models.inputs4job(self.job.id, input_type="exposure") emdl = input.model() if not emdl: emdl = models.ExposureModel( owner=self.job.owner, input=input, description="c-psha test exposure model", category="c-psha power plants", stco_unit="watt", stco_type="aggregated", reco_unit="joule", reco_type="aggregated") emdl.save() assets = emdl.exposuredata_set.filter(asset_ref="rubcr") if not assets: asset = models.ExposureData(exposure_model=emdl, taxonomy="ID", asset_ref="rubcr", stco=1, reco=123.45, site=GEOSGeometry("POINT(1.0 1.0)")) asset.save() Block.from_kvs(self.job_id, self.block_id) calculator.compute_risk(self.block_id) result_key = kvs.tokens.bcr_block_key(self.job_id, self.block_id) res = kvs.get_value_json_decoded(result_key) expected_result = {'bcr': 0.0, 'eal_original': 0.003032, 'eal_retrofitted': 0.003032} helpers.assertDeepAlmostEqual( self, res, [[[1, 1], [[expected_result, "rubcr"]]]])
def test_compute_beta_dist(self): # expected beta distributions provided by Vitor expected_beta_distributions = [ [1.0000000, 1.0000000, 1.0000000, 1.0000000, 1.0000000], [0.9895151, 0.9999409, 1.0000000, 1.0000000, 1.0000000], [0.9175720, 0.9981966, 0.9999997, 1.0000000, 1.0000000], [0.7764311, 0.9887521, 0.9999922, 1.0000000, 1.0000000], [0.6033381, 0.9633258, 0.9999305, 1.0000000, 1.0000000], [0.4364471, 0.9160514, 0.9996459, 1.0000000, 1.0000000], [0.2975979, 0.8460938, 0.9987356, 1.0000000, 1.0000000], [0.1931667, 0.7574557, 0.9964704, 1.0000000, 1.0000000], [0.1202530, 0.6571491, 0.9917729, 0.9999999, 1.0000000], [0.0722091, 0.5530379, 0.9832939, 0.9999997, 1.0000000], [0.0420056, 0.4521525, 0.9695756, 0.9999988, 1.0000000], [0.0130890, 0.2790107, 0.9213254, 0.9999887, 1.0000000], [0.0037081, 0.1564388, 0.8409617, 0.9999306, 1.0000000], [0.0009665, 0.0805799, 0.7311262, 0.9996882, 1.0000000], [0.0002335, 0.0384571, 0.6024948, 0.9988955, 1.0000000], [0.0000526, 0.0171150, 0.4696314, 0.9967629, 1.0000000], [0.0000022, 0.0027969, 0.2413923, 0.9820831, 1.0000000], [0.0000001, 0.0003598, 0.0998227, 0.9364072, 1.0000000], [0.0000000, 0.0000367, 0.0334502, 0.8381920, 0.9999995], [0.0000000, 0.0000030, 0.0091150, 0.6821293, 0.9999959], [0.0000000, 0.0000002, 0.0020162, 0.4909782, 0.9999755], [0.0000000, 0.0000000, 0.0000509, 0.1617086, 0.9995033], [0.0000000, 0.0000000, 0.0000005, 0.0256980, 0.9945488], [0.0000000, 0.0000000, 0.0000000, 0.0016231, 0.9633558], [0.0000000, 0.0000000, 0.0000000, 0.0000288, 0.8399534], [0.0000000, 0.0000000, 0.0000000, 0.0000001, 0.5409583], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.3413124], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.1589844], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0421052], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0027925], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0000000]] vuln_function = shapes.VulnerabilityFunction(self.imls, self.mean_loss_ratios, self.covs) # steps = 5 loss_ratios = _generate_loss_ratios(vuln_function, 5) lrem = numpy.empty((len(loss_ratios), vuln_function.imls.size), float) for col, _ in enumerate(vuln_function): for row, loss_ratio in enumerate(loss_ratios): lrem[row][col] = BetaDistribution.survival_function(loss_ratio, col=col, vf=vuln_function) helpers.assertDeepAlmostEqual(self, expected_beta_distributions, lrem, delta=0.0005)
def test_compute_bcr(self): cfg_path = helpers.demo_file( 'probabilistic_event_based_risk/config.gem') helpers.delete_profile(self.job) job_profile, params, sections = engine.import_job_profile( cfg_path, self.job) job_profile.calc_mode = 'event_based_bcr' job_profile.interest_rate = 0.05 job_profile.asset_life_expectancy = 50 job_profile.region = GEOSGeometry(shapes.polygon_ewkt_from_coords( '0.0, 0.0, 0.0, 2.0, 2.0, 2.0, 2.0, 0.0')) job_profile.region_grid_spacing = 0.1 job_profile.maximum_distance = 200.0 job_profile.gmf_random_seed = None job_profile.save() params.update(dict(CALCULATION_MODE='Event Based BCR', INTEREST_RATE='0.05', ASSET_LIFE_EXPECTANCY='50', MAXIMUM_DISTANCE='200.0', REGION_VERTEX=('0.0, 0.0, 0.0, 2.0, ' '2.0, 2.0, 2.0, 0.0'), REGION_GRID_SPACING='0.1')) job_ctxt = engine.JobContext( params, self.job_id, sections=sections, oq_job_profile=job_profile) calculator = eb_core.EventBasedRiskCalculator(job_ctxt) self.block_id = 7 SITE = shapes.Site(1.0, 1.0) block = Block(self.job_id, self.block_id, (SITE, )) block.to_kvs() location = GEOSGeometry(SITE.point.to_wkt()) asset = models.ExposureData(exposure_model=self.emdl, taxonomy="ID", asset_ref=22.61, stco=1, reco=123.45, site=location) asset.save() calculator.compute_risk(self.block_id) result_key = kvs.tokens.bcr_block_key(self.job_id, self.block_id) result = kvs.get_value_json_decoded(result_key) expected_result = {'bcr': 0.0, 'eal_original': 0.0, 'eal_retrofitted': 0.0} helpers.assertDeepAlmostEqual( self, [[[1, 1], [[expected_result, "22.61"]]]], result)
def test_compute_lrem_using_beta_distribution(self): # expected lrem provided by Vitor expected_beta_distributions = [ [1.0000000, 1.0000000, 1.0000000, 1.0000000, 1.0000000], [0.9895151, 0.9999409, 1.0000000, 1.0000000, 1.0000000], [0.9175720, 0.9981966, 0.9999997, 1.0000000, 1.0000000], [0.7764311, 0.9887521, 0.9999922, 1.0000000, 1.0000000], [0.6033381, 0.9633258, 0.9999305, 1.0000000, 1.0000000], [0.4364471, 0.9160514, 0.9996459, 1.0000000, 1.0000000], [0.2975979, 0.8460938, 0.9987356, 1.0000000, 1.0000000], [0.1931667, 0.7574557, 0.9964704, 1.0000000, 1.0000000], [0.1202530, 0.6571491, 0.9917729, 0.9999999, 1.0000000], [0.0722091, 0.5530379, 0.9832939, 0.9999997, 1.0000000], [0.0420056, 0.4521525, 0.9695756, 0.9999988, 1.0000000], [0.0130890, 0.2790107, 0.9213254, 0.9999887, 1.0000000], [0.0037081, 0.1564388, 0.8409617, 0.9999306, 1.0000000], [0.0009665, 0.0805799, 0.7311262, 0.9996882, 1.0000000], [0.0002335, 0.0384571, 0.6024948, 0.9988955, 1.0000000], [0.0000526, 0.0171150, 0.4696314, 0.9967629, 1.0000000], [0.0000022, 0.0027969, 0.2413923, 0.9820831, 1.0000000], [0.0000001, 0.0003598, 0.0998227, 0.9364072, 1.0000000], [0.0000000, 0.0000367, 0.0334502, 0.8381920, 0.9999995], [0.0000000, 0.0000030, 0.0091150, 0.6821293, 0.9999959], [0.0000000, 0.0000002, 0.0020162, 0.4909782, 0.9999755], [0.0000000, 0.0000000, 0.0000509, 0.1617086, 0.9995033], [0.0000000, 0.0000000, 0.0000005, 0.0256980, 0.9945488], [0.0000000, 0.0000000, 0.0000000, 0.0016231, 0.9633558], [0.0000000, 0.0000000, 0.0000000, 0.0000288, 0.8399534], [0.0000000, 0.0000000, 0.0000000, 0.0000001, 0.5409583], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.3413124], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.1589844], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0421052], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0027925], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0000000] ] vuln_function = shapes.VulnerabilityFunction(self.imls, self.mean_loss_ratios, self.covs, "BT") lrem = _compute_lrem(vuln_function, 5) helpers.assertDeepAlmostEqual(self, expected_beta_distributions, lrem, delta=0.0005)
def test_compute_lrem_using_beta_distribution(self): # expected lrem provided by Vitor expected_beta_distributions = [ [1.0000000, 1.0000000, 1.0000000, 1.0000000, 1.0000000], [0.9895151, 0.9999409, 1.0000000, 1.0000000, 1.0000000], [0.9175720, 0.9981966, 0.9999997, 1.0000000, 1.0000000], [0.7764311, 0.9887521, 0.9999922, 1.0000000, 1.0000000], [0.6033381, 0.9633258, 0.9999305, 1.0000000, 1.0000000], [0.4364471, 0.9160514, 0.9996459, 1.0000000, 1.0000000], [0.2975979, 0.8460938, 0.9987356, 1.0000000, 1.0000000], [0.1931667, 0.7574557, 0.9964704, 1.0000000, 1.0000000], [0.1202530, 0.6571491, 0.9917729, 0.9999999, 1.0000000], [0.0722091, 0.5530379, 0.9832939, 0.9999997, 1.0000000], [0.0420056, 0.4521525, 0.9695756, 0.9999988, 1.0000000], [0.0130890, 0.2790107, 0.9213254, 0.9999887, 1.0000000], [0.0037081, 0.1564388, 0.8409617, 0.9999306, 1.0000000], [0.0009665, 0.0805799, 0.7311262, 0.9996882, 1.0000000], [0.0002335, 0.0384571, 0.6024948, 0.9988955, 1.0000000], [0.0000526, 0.0171150, 0.4696314, 0.9967629, 1.0000000], [0.0000022, 0.0027969, 0.2413923, 0.9820831, 1.0000000], [0.0000001, 0.0003598, 0.0998227, 0.9364072, 1.0000000], [0.0000000, 0.0000367, 0.0334502, 0.8381920, 0.9999995], [0.0000000, 0.0000030, 0.0091150, 0.6821293, 0.9999959], [0.0000000, 0.0000002, 0.0020162, 0.4909782, 0.9999755], [0.0000000, 0.0000000, 0.0000509, 0.1617086, 0.9995033], [0.0000000, 0.0000000, 0.0000005, 0.0256980, 0.9945488], [0.0000000, 0.0000000, 0.0000000, 0.0016231, 0.9633558], [0.0000000, 0.0000000, 0.0000000, 0.0000288, 0.8399534], [0.0000000, 0.0000000, 0.0000000, 0.0000001, 0.5409583], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.3413124], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.1589844], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0421052], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0027925], [0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0000000]] vuln_function = shapes.VulnerabilityFunction( self.imls, self.mean_loss_ratios, self.covs, "BT") lrem = _compute_lrem(vuln_function, 5) helpers.assertDeepAlmostEqual( self, expected_beta_distributions, lrem, delta=0.0005)
def test_multiple_matrices(self): target_path = os.path.join(self.tempdir, 'multiple.hdf5') pmfs = { 'MagDistEpsPMF': ('MagDistEpsPMF.dat', [self.NMAG - 1, self.NDIST - 1, self.NEPS - 1]), 'LatLonPMF': ('LatLonPMF.dat', [self.NLAT - 1, self.NLON - 1]), FULL_DISAGG_MATRIX: (self.FULL_MATRIX_DATA, self.FULL_MATRIX_SHAPE) } disagg_subsets.extract_subsets(112, self.SITE, self.full_matrix_path, self.LATITUDE_BIN_LIMITS, self.LONGITUDE_BIN_LIMITS, self.MAGNITUDE_BIN_LIMITS, self.EPSILON_BIN_LIMITS, self.DISTANCE_BIN_LIMITS, target_path, pmfs.keys()) result = h5py.File(target_path, 'r') for name, (datafile, shape) in pmfs.items(): expected_result = self.read_data_file(datafile, shape) helpers.assertDeepAlmostEqual(self, expected_result, result[name])
def test_multiple_matrices(self): target_path = os.path.join(self.tempdir, 'multiple.hdf5') pmfs = { 'MagDistEpsPMF': ('MagDistEpsPMF.dat', [self.NMAG - 1, self.NDIST - 1, self.NEPS - 1]), 'LatLonPMF': ('LatLonPMF.dat', [self.NLAT - 1, self.NLON - 1]), disagg.FULL_DISAGG_MATRIX: (self.FULL_MATRIX_DATA, self.FULL_MATRIX_SHAPE) } disagg_subsets.extract_subsets( 112, self.SITE, self.full_matrix_path, self.LATITUDE_BIN_LIMITS, self.LONGITUDE_BIN_LIMITS, self.MAGNITUDE_BIN_LIMITS, self.EPSILON_BIN_LIMITS, self.DISTANCE_BIN_LIMITS, target_path, pmfs.keys() ) result = h5py.File(target_path, 'r') for name, (datafile, shape) in pmfs.items(): expected_result = self.read_data_file(datafile, shape) helpers.assertDeepAlmostEqual(self, expected_result, result[name])
def test_write_uhs_data(self): # Test object type to use instead of `UhSpectrumData`; # this is a little more light-weight # First, set up all the test data: Data = namedtuple('Data', 'realization, sa_values, location') points = [ Point(0.0, 0.0), Point(1.0, 0.0), Point(0.0, 1.0), Point(1.0, 1.0), ] # A single 2D matrix for each location/point sa_test_values = [ # each row repsents a realization, # while the contents of each row is an array of SA values [[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 10.0, 11.0, 12.0]], [[13.0, 14.0, 15.0, 16.0], [17.0, 18.0, 19.0, 20.0], [21.0, 22.0, 23.0, 24.0]], [[25.0, 26.0, 27.0, 28.0], [29.0, 30.0, 31.0, 32.0], [33.0, 34.0, 35.0, 36.0]], [[37.0, 38.0, 39.0, 40.0], [41.0, 42.0, 43.0, 44.0], [45.0, 46.0, 47.0, 48.0]], ] uhs_data = [] for i, pt in enumerate(points): for j, sa_values in enumerate(sa_test_values[i]): uhs_data.append(Data(j, sa_values, pt)) # Done setting up the test data. # Now, create the empty file: target_dir = tempfile.mkdtemp() try: poe = 0.05 n_rlz = 3 # rows n_periods = 4 # columns ds_names = [uhs_export._point_to_ds_name(p) for p in points] # As a robustness test, reverse the order of the ds_names. # It should not matter when we're creating the file (since the # structure of the file is basically a dict of 2D matrices). the_file = uhs_export.touch_result_hdf5_file( target_dir, poe, ds_names[::-1], n_rlz, n_periods) # Finally, call the function under test with our list of fake # `UhSpectrumData` objects. uhs_export.write_uhs_data(the_file, uhs_data) # Now read the file and check the contents: with h5py.File(the_file, 'r') as h5_file: for i, ds in enumerate(ds_names): helpers.assertDeepAlmostEqual( self, sa_test_values[i], h5_file[ds].value) finally: shutil.rmtree(target_dir)
def test_write_uhs_data(self): # Test object type to use instead of `UhSpectrumData`; # this is a little more light-weight # First, set up all the test data: Data = namedtuple('Data', 'realization, sa_values, location') points = [ Point(0.0, 0.0), Point(1.0, 0.0), Point(0.0, 1.0), Point(1.0, 1.0), ] # A single 2D matrix for each location/point sa_test_values = [ # each row repsents a realization, # while the contents of each row is an array of SA values [[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 10.0, 11.0, 12.0]], [[13.0, 14.0, 15.0, 16.0], [17.0, 18.0, 19.0, 20.0], [21.0, 22.0, 23.0, 24.0]], [[25.0, 26.0, 27.0, 28.0], [29.0, 30.0, 31.0, 32.0], [33.0, 34.0, 35.0, 36.0]], [[37.0, 38.0, 39.0, 40.0], [41.0, 42.0, 43.0, 44.0], [45.0, 46.0, 47.0, 48.0]], ] uhs_data = [] for i, pt in enumerate(points): for j, sa_values in enumerate(sa_test_values[i]): uhs_data.append(Data(j, sa_values, pt)) # Done setting up the test data. # Now, create the empty file: target_dir = tempfile.mkdtemp() try: poe = 0.05 n_rlz = 3 # rows n_periods = 4 # columns ds_names = [uhs_export._point_to_ds_name(p) for p in points] # As a robustness test, reverse the order of the ds_names. # It should not matter when we're creating the file (since the # structure of the file is basically a dict of 2D matrices). the_file = uhs_export.touch_result_hdf5_file( target_dir, poe, ds_names[::-1], n_rlz, n_periods) # Finally, call the function under test with our list of fake # `UhSpectrumData` objects. uhs_export.write_uhs_data(the_file, uhs_data) # Now read the file and check the contents: with h5py.File(the_file, 'r') as h5_file: for i, ds in enumerate(ds_names): helpers.assertDeepAlmostEqual(self, sa_test_values[i], h5_file[ds].value) finally: shutil.rmtree(target_dir)