def test_3d(self): a = numpy.array([x * 3 for x in range(24)]).reshape((3, 2, 4)) expected = [ ((0, 0, 0), 0), ((0, 0, 1), 3), ((0, 0, 2), 6), ((0, 0, 3), 9), ((0, 1, 0), 12), ((0, 1, 1), 15), ((0, 1, 2), 18), ((0, 1, 3), 21), ((1, 0, 0), 24), ((1, 0, 1), 27), ((1, 0, 2), 30), ((1, 0, 3), 33), ((1, 1, 0), 36), ((1, 1, 1), 39), ((1, 1, 2), 42), ((1, 1, 3), 45), ((2, 0, 0), 48), ((2, 0, 1), 51), ((2, 0, 2), 54), ((2, 0, 3), 57), ((2, 1, 0), 60), ((2, 1, 1), 63), ((2, 1, 2), 66), ((2, 1, 3), 69), ] actual = list(utils.ndenumerate(a)) self.assertEqual(expected, actual)
def serialize(self, data): """ :param data: A sequence of data where each datum has the following attributes: * matrix: N-dimensional numpy array containing the disaggregation histogram. * dim_labels: A list of strings which label the dimensions of a given histogram. For example, for a Magnitude-Distance-Epsilon histogram, we would expect `dim_labels` to be ``['Mag', 'Dist', 'Eps']``. * poe: The disaggregation Probability of Exceedance level for which these results were produced. * iml: Intensity measure level, interpolated from the source hazard curve at the given ``poe``. """ with open(self.path, 'w') as fh: root = etree.Element('nrml', nsmap=openquake.nrmllib.SERIALIZE_NS_MAP) diss_matrices = etree.SubElement(root, 'disaggMatrices') _set_metadata(diss_matrices, self.metadata, _ATTR_MAP) transform = lambda val: ', '.join([str(x) for x in val]) _set_metadata(diss_matrices, self.metadata, self.BIN_EDGE_ATTR_MAP, transform=transform) for result in data: diss_matrix = etree.SubElement(diss_matrices, 'disaggMatrix') # Check that we have bin edges defined for each dimension label # (mag, dist, lon, lat, eps, TRT) for label in result.dim_labels: bin_edge_attr = self.DIM_LABEL_TO_BIN_EDGE_MAP.get(label) assert self.metadata.get(bin_edge_attr) is not None, ( "Writer is missing '%s' metadata" % bin_edge_attr ) result_type = ','.join(result.dim_labels) diss_matrix.set('type', result_type) dims = ','.join([str(x) for x in result.matrix.shape]) diss_matrix.set('dims', dims) diss_matrix.set('poE', str(result.poe)) diss_matrix.set('iml', str(result.iml)) for idxs, value in utils.ndenumerate(result.matrix): prob = etree.SubElement(diss_matrix, 'prob') index = ','.join([str(x) for x in idxs]) prob.set('index', index) prob.set('value', str(value)) fh.write(etree.tostring( root, pretty_print=True, xml_declaration=True, encoding='UTF-8'))
def test_1d(self): a = numpy.array([x * 3 for x in range(4)]) expected = [ ((0, ), 0), ((1, ), 3), ((2, ), 6), ((3, ), 9), ] actual = list(utils.ndenumerate(a)) self.assertEqual(expected, actual)
def test_1d(self): a = numpy.array([x * 3 for x in range(4)]) expected = [ ((0,), 0), ((1,), 3), ((2,), 6), ((3,), 9), ] actual = list(utils.ndenumerate(a)) self.assertEqual(expected, actual)
def test_4d(self): a = numpy.array([x * 3 for x in range(48)]).reshape((2, 2, 3, 4)) expected = [ ((0, 0, 0, 0), 0), ((0, 0, 0, 1), 3), ((0, 0, 0, 2), 6), ((0, 0, 0, 3), 9), ((0, 0, 1, 0), 12), ((0, 0, 1, 1), 15), ((0, 0, 1, 2), 18), ((0, 0, 1, 3), 21), ((0, 0, 2, 0), 24), ((0, 0, 2, 1), 27), ((0, 0, 2, 2), 30), ((0, 0, 2, 3), 33), ((0, 1, 0, 0), 36), ((0, 1, 0, 1), 39), ((0, 1, 0, 2), 42), ((0, 1, 0, 3), 45), ((0, 1, 1, 0), 48), ((0, 1, 1, 1), 51), ((0, 1, 1, 2), 54), ((0, 1, 1, 3), 57), ((0, 1, 2, 0), 60), ((0, 1, 2, 1), 63), ((0, 1, 2, 2), 66), ((0, 1, 2, 3), 69), ((1, 0, 0, 0), 72), ((1, 0, 0, 1), 75), ((1, 0, 0, 2), 78), ((1, 0, 0, 3), 81), ((1, 0, 1, 0), 84), ((1, 0, 1, 1), 87), ((1, 0, 1, 2), 90), ((1, 0, 1, 3), 93), ((1, 0, 2, 0), 96), ((1, 0, 2, 1), 99), ((1, 0, 2, 2), 102), ((1, 0, 2, 3), 105), ((1, 1, 0, 0), 108), ((1, 1, 0, 1), 111), ((1, 1, 0, 2), 114), ((1, 1, 0, 3), 117), ((1, 1, 1, 0), 120), ((1, 1, 1, 1), 123), ((1, 1, 1, 2), 126), ((1, 1, 1, 3), 129), ((1, 1, 2, 0), 132), ((1, 1, 2, 1), 135), ((1, 1, 2, 2), 138), ((1, 1, 2, 3), 141), ] actual = list(utils.ndenumerate(a)) self.assertEqual(expected, actual)
def serialize(self, data): """ :param data: A sequence of data where each datum has the following attributes: * matrix: N-dimensional numpy array containing the disaggregation histogram. * dim_labels: A list of strings which label the dimensions of a given histogram. For example, for a Magnitude-Distance-Epsilon histogram, we would expect `dim_labels` to be ``['Mag', 'Dist', 'Eps']``. * poe: The disaggregation Probability of Exceedance level for which these results were produced. * iml: Intensity measure level, interpolated from the source hazard curve at the given ``poe``. """ with open(self.path, 'w') as fh: root = etree.Element('nrml', nsmap=openquake.nrmllib.SERIALIZE_NS_MAP) diss_matrices = etree.SubElement(root, 'disaggMatrices') _set_metadata(diss_matrices, self.metadata, _ATTR_MAP) transform = lambda val: ', '.join([str(x) for x in val]) _set_metadata(diss_matrices, self.metadata, self.BIN_EDGE_ATTR_MAP, transform=transform) for result in data: diss_matrix = etree.SubElement(diss_matrices, 'disaggMatrix') # Check that we have bin edges defined for each dimension label # (mag, dist, lon, lat, eps, TRT) for label in result.dim_labels: bin_edge_attr = self.DIM_LABEL_TO_BIN_EDGE_MAP.get(label) assert self.metadata.get(bin_edge_attr) is not None, ( "Writer is missing '%s' metadata" % bin_edge_attr) result_type = ','.join(result.dim_labels) diss_matrix.set('type', result_type) dims = ','.join([str(x) for x in result.matrix.shape]) diss_matrix.set('dims', dims) diss_matrix.set('poE', str(result.poe)) diss_matrix.set('iml', str(result.iml)) for idxs, value in utils.ndenumerate(result.matrix): prob = etree.SubElement(diss_matrix, 'prob') index = ','.join([str(x) for x in idxs]) prob.set('index', index) prob.set('value', str(value)) fh.write( etree.tostring(root, pretty_print=True, xml_declaration=True, encoding='UTF-8'))