Example #1
0
    def test_writes_a_single_result(self):
        data = [(shapes.Site(-122.5000, 37.5000), 
                {   "IDmodel": "MMI_3_1",
                    "investigationTimeSpan": 50.0,
                    "endBranchLabel": "3_1",
                    "IML": [5.0000e-03, 7.0000e-03, 1.3700e-02, 
                    1.9200e-02, 2.6900e-02, 3.7600e-02, 5.2700e-02, 
                    7.3800e-02, 9.8000e-02, 1.0300e-01, 1.4500e-01, 
                    2.0300e-01, 2.8400e-01, 3.9700e-01, 5.5600e-01, 
                    7.7800e-01, 1.0900e+00, 1.5200e+00, 2.1300e+00],
                    "saPeriod": 0.1,
                    "saDamping": 1.0,
                    "IMT": "PGA",
                    "poE": [9.8728e-01, 9.8266e-01, 9.4957e-01, 
                    9.0326e-01, 8.1956e-01, 6.9192e-01, 5.2866e-01, 
                    3.6143e-01, 2.4231e-01, 2.2452e-01, 1.2831e-01, 
                    7.0352e-02, 3.6060e-02, 1.6579e-02, 6.4213e-03, 
                    2.0244e-03, 4.8605e-04, 8.1752e-05, 7.3425e-06]})]

        path = test.test_output_file(TEST_FILE_SINGLE_RESULT)
        self._remove_and_write_file(path)

        self.writer.serialize(data)
        self._is_xml_valid(path)
        
        self.assertTrue(XML_METADATA in self._result_as_string(path))
Example #2
0
    def test_writes_multiple_results_with_different_branch_levels(self):
        data = [(shapes.Site(-122.5000, 37.5000), 
                 {  "IDmodel": "MMI_3_1",
                    "investigationTimeSpan": 50.0,
                    "endBranchLabel": "3_1",
                    "IML": [5.0, 6.0, 7.0],
                    "saPeriod": 0.1,
                    "saDamping": 1.0,
                    "IMT": "PGA",
                    "poE": [0.1, 0.2, 0.3]}),
                (shapes.Site(-122.5000, 37.5000), 
                 {  "IDmodel": "MMI_3_1",
                    "investigationTimeSpan": 50.0,
                    "endBranchLabel": "3_2",
                    "IML": [5.0, 6.0, 7.0],
                    "saPeriod": 0.1,
                    "saDamping": 1.0,
                    "IMT": "PGA",
                    "poE": [0.1, 0.2, 0.3]}),
                (shapes.Site(-122.4000, 37.5000), 
                 {  "IDmodel": "MMI_3_1",
                    "investigationTimeSpan": 50.0,
                    "endBranchLabel": "3_2",
                    "IML": [8.0, 9.0, 10.0],
                    "saPeriod": 0.1,
                    "saDamping": 1.0,
                    "IMT": "PGA",
                    "poE": [0.1, 0.2, 0.3]})]

        path = test.test_output_file(TEST_FILE_MULTIPLE_DIFFERENT_BRANCHES)
        self._remove_and_write_file(path)

        self.writer.serialize(data)
        self._is_xml_valid(path)
Example #3
0
    def test_writes_the_config_only_once(self):
        data = [(shapes.Site(-122.5000, 37.5000), 
                 {      "IDmodel": "MMI_3_1",
                        "investigationTimeSpan": 50.0,
                        "endBranchLabel": "3_1",
                        "IML": [5.0, 6.0, 7.0],
                        "saPeriod": 0.1,
                        "saDamping": 1.0,
                        "IMT": "PGA",
                        "poE": [0.1, 0.2, 0.3]}),
                (shapes.Site(-122.4000, 37.5000), 
                 {      "IDmodel": "MMI_3_1",
                        "investigationTimeSpan": 50.0,
                        "endBranchLabel": "3_2",
                        "IML": [5.0, 6.0, 7.0],
                        "saPeriod": 0.1,
                        "saDamping": 1.0,
                        "IMT": "PGA",
                        "poE": [0.4, 0.5, 0.6]})
                ]
        
        path = test.test_output_file(TEST_FILE_CONFIG_ONCE)
        self._remove_and_write_file(path)

        self.writer.serialize(data)
        self._is_xml_valid(path)
Example #4
0
    def test_geotiff_generation_and_metadata_validation(self):
        """Create a GeoTIFF, and check if it has the
        correct metadata."""
        path = test.test_output_file(GEOTIFF_FILENAME_WITHOUT_NUMBER)
        smallregion = shapes.Region.from_coordinates(TEST_REGION_SMALL)
        gwriter = geotiff.GeoTiffFile(path, smallregion.grid)
        gwriter.close()

        self._assert_geotiff_metadata_is_correct(path, smallregion)
Example #5
0
    def test_geotiff_generation_with_number_in_filename(self):
        """Create a GeoTIFF with a number in its filename. This
        test has been written because it has been reported that numbers in the
        filename do not work."""
        path = test.test_output_file(GEOTIFF_FILENAME_WITH_NUMBER)
        smallregion = shapes.Region.from_coordinates(TEST_REGION_SMALL)
        gwriter = geotiff.GeoTiffFile(path, smallregion.grid)
        gwriter.close()

        self._assert_geotiff_metadata_is_correct(path, smallregion)
Example #6
0
    def test_serializes_gmf(self):
        path = test.test_output_file(GMF_NORUPTURE_TEST_FILE)
        writer = hazard_output.GMFXMLWriter(path)
        writer.serialize(GMF_NORUPTURE_TEST_DATA)

        check_data = {}
        reader = hazard_parser.GMFReader(path)
        for curr_site, curr_attribute in reader:
            check_data[curr_site] = curr_attribute

        self.assertEqual(check_data, GMF_NORUPTURE_TEST_DATA)
Example #7
0
    def test_geotiff_generation_colorscale(self):
        """Check RGB geotiff generation with colorscale for GMF."""
        path = test.test_output_file(GEOTIFF_FILENAME_COLORSCALE)
        asymmetric_region = shapes.Region.from_coordinates(
            TEST_REGION_LARGE_ASYMMETRIC)
        gwriter = geotiff.GMFGeoTiffFile(path, asymmetric_region.grid, 
            discrete=False)

        reference_raster = numpy.zeros((asymmetric_region.grid.rows, 
                                        asymmetric_region.grid.columns),
                                       dtype=numpy.float)
        self._fill_rasters(asymmetric_region, gwriter, reference_raster, 
            self._colorscale_fill)
        gwriter.close()
Example #8
0
    def setUp(self):
        self.path = test.test_output_file(LOSS_XML_OUTPUT_FILE)
        self.ratio_path = test.test_output_file(LOSS_RATIO_XML_OUTPUT_FILE)
        # self.schema_path = os.path.join(test.SCHEMA_DIR, LOSS_SCHEMA_FILE)
        self.schema_path = NRML_SCHEMA_PATH_OLD

        # Build up some sample loss curves here

        first_site = shapes.Site(10.0, 10.0)
        second_site = shapes.Site(10.0, 20.0)
        first_curve = TEST_CURVE
        second_curve = first_curve
        first_asset = {"AssetID" : "1711"}
        second_asset = {"AssetID" : "1712"}

        # Then serialize them to XML
        loss_curves = [(first_site, (first_curve, first_asset)), 
                        (second_site, (second_curve, second_asset))] 

        xml_writer = risk_output.LossCurveXMLWriter(self.path)
        xml_writer.serialize(loss_curves)
        
        xml_writer = risk_output.LossRatioCurveXMLWriter(self.ratio_path)
        xml_writer.serialize(loss_curves)
Example #9
0
    def test_writes_multiple_results_with_statistics(self):
        data = [(shapes.Site(-122.5000, 37.5000), 
                 {
                    "nrml_id": "nrml_instance_1",
                    "hazres_id": "hazard_result_0001",
                    "hcfield_id": "hazard_field_one",
                    "hcnode_id": "the_hazard_node_1000",
                    "IDmodel": "foo",
                    "investigationTimeSpan": 50.0,
                    "statistics": "quantile",
                    "quantileValue": "0.5",
                    "IML": [5.0000e-03, 7.0000e-03, 1.3700e-02, 
                    1.9200e-02, 2.6900e-02, 3.7600e-02, 5.2700e-02, 
                    7.3800e-02, 9.8000e-02, 1.0300e-01, 1.4500e-01, 
                    2.0300e-01, 2.8400e-01, 3.9700e-01, 5.5600e-01, 
                    7.7800e-01, 1.0900e+00, 1.5200e+00, 2.1300e+00],
                    "saPeriod": 0.1,
                    "saDamping": 1.0,
                    "IMT": "PGA",
                    "poE": [9.8728e-01, 9.8266e-01, 9.4957e-01, 
                    9.0326e-01, 8.1956e-01, 6.9192e-01, 5.2866e-01, 
                    3.6143e-01, 2.4231e-01, 2.2452e-01, 1.2831e-01, 
                    7.0352e-02, 3.6060e-02, 1.6579e-02, 6.4213e-03, 
                    2.0244e-03, 4.8605e-04, 8.1752e-05, 7.3425e-06]}),
                (shapes.Site(-122.4000, 37.5000), 
                 {  "IDmodel": "foo",
                    "investigationTimeSpan": 50.0,
                    "statistics": "quantile",
                    "quantileValue": "0.5",
                    "IML": [5.0000e-03, 7.0000e-03, 1.3700e-02, 
                    1.9200e-02, 2.6900e-02, 3.7600e-02, 5.2700e-02, 
                    7.3800e-02, 9.8000e-02, 1.0300e-01, 1.4500e-01, 
                    2.0300e-01, 2.8400e-01, 3.9700e-01, 5.5600e-01, 
                    7.7800e-01, 1.0900e+00, 1.5200e+00, 2.1300e+00],
                    "saPeriod": 0.1,
                    "saDamping": 1.0,
                    "IMT": "PGA",
                    "poE": [9.8784e-01, 9.8405e-01, 9.5719e-01, 
                    9.1955e-01, 8.5019e-01, 7.4038e-01, 5.9153e-01, 
                    4.2626e-01, 2.9755e-01, 2.7731e-01, 1.6218e-01, 
                    8.8035e-02, 4.3499e-02, 1.9065e-02, 7.0442e-03, 
                    2.1300e-03, 4.9498e-04, 8.1768e-05, 7.3425e-06]})]

        path = test.test_output_file(TEST_FILE_STATISTICS)
        self._remove_and_write_file(path)

        self.writer.serialize(data)
        self._is_xml_valid(path)
Example #10
0
    def test_geotiff_generation_nondefault_colorscale(self):
        """Check RGB geotiff generation with colorscale for GMF. Use
        alternative colorscale."""
        path = test.test_output_file(GEOTIFF_FILENAME_NONDEFAULT_COLORSCALE)
        asymmetric_region = shapes.Region.from_coordinates(
            TEST_REGION_LARGE_ASYMMETRIC)

        gwriter = geotiff.GMFGeoTiffFile(path, asymmetric_region.grid,
            iml_list=None, discrete=False, colormap='gmt-green-red')

        reference_raster = numpy.zeros((asymmetric_region.grid.rows, 
                                        asymmetric_region.grid.columns),
                                       dtype=numpy.float)
        self._fill_rasters(asymmetric_region, gwriter, reference_raster, 
            self._colorscale_cuts_fill)
        gwriter.close()
Example #11
0
    def test_geotiff_generation_discrete_colorscale(self):
        """Check RGB geotiff generation with colorscale for GMF. Use
        discrete colorscale based on IML values, with default IML."""
        path = test.test_output_file(GEOTIFF_FILENAME_DISCRETE_COLORSCALE)
        asymmetric_region = shapes.Region.from_coordinates(
            TEST_REGION_LARGE_ASYMMETRIC)

        gwriter = geotiff.GMFGeoTiffFile(path, asymmetric_region.grid,
            iml_list=None, discrete=True, colormap='gmt-seis')

        reference_raster = numpy.zeros((asymmetric_region.grid.rows, 
                                        asymmetric_region.grid.columns),
                                       dtype=numpy.float)
        self._fill_rasters(asymmetric_region, gwriter, reference_raster, 
            self._colorscale_cuts_fill)
        gwriter.close()
Example #12
0
    def test_geotiff_generation_and_simple_raster_validation(self):
        """Create a GeoTIFF and assign values to the raster nodes according
        to a simple function. Then check if the raster values have been set
        correctly."""
        path = test.test_output_file(GEOTIFF_FILENAME_SQUARE_REGION)
        squareregion = shapes.Region.from_coordinates(TEST_REGION_SQUARE)
        gwriter = geotiff.GeoTiffFile(path, squareregion.grid)
        
        reference_raster = numpy.zeros((squareregion.grid.rows, 
                                        squareregion.grid.columns), 
                                       dtype=numpy.float)
        self._fill_rasters(squareregion, gwriter, reference_raster, 
            self._trivial_fill)
        gwriter.close()

        self._assert_geotiff_metadata_and_raster_is_correct(path, 
            squareregion, GEOTIFF_USED_CHANNEL_IDX, reference_raster)
Example #13
0
    def test_geotiff_generation_initialize_raster(self):
        """Create a GeoTIFF and initialize the raster to a given value. Then
        check through metadata if it has been done correctly. We check the 
        minumum and maximum values of the band, which are expected to have
        the value of the raster nodes."""
        path = test.test_output_file(GEOTIFF_FILENAME_WITH_NUMBER)
        smallregion = shapes.Region.from_coordinates(TEST_REGION_SMALL)
        gwriter = geotiff.GeoTiffFile(path, smallregion.grid, 
                                      GEOTIFF_TEST_PIXEL_VALUE)
        gwriter.close()

        self._assert_geotiff_metadata_is_correct(path, smallregion)

        # assert that all raster pixels have the desired value
        self._assert_geotiff_band_min_max_values(path,
            GEOTIFF_USED_CHANNEL_IDX, 
            GEOTIFF_TEST_PIXEL_VALUE, 
            GEOTIFF_TEST_PIXEL_VALUE)
Example #14
0
    def test_geotiff_generation_asymmetric_pattern(self):
        """Create a GeoTIFF and assign values to the raster nodes according
        to a simple function. Use a somewhat larger, non-square region for 
        that. Then check if the raster values have been set correctly."""
        path = test.test_output_file(GEOTIFF_FILENAME_LARGE_ASYMMETRIC_REGION)
        asymmetric_region = shapes.Region.from_coordinates(
            TEST_REGION_LARGE_ASYMMETRIC)
        gwriter = geotiff.GeoTiffFile(path, asymmetric_region.grid)

        reference_raster = numpy.zeros((asymmetric_region.grid.rows, 
                                        asymmetric_region.grid.columns),
                                       dtype=numpy.float)
        self._fill_rasters(asymmetric_region, gwriter, reference_raster, 
            self._trivial_fill)
        gwriter.close()

        self._assert_geotiff_metadata_and_raster_is_correct(path, 
            asymmetric_region, GEOTIFF_USED_CHANNEL_IDX, reference_raster)
Example #15
0
    def test_geotiff_loss_ratio_map_colorscale(self):
        path = test.test_output_file(GEOTIFF_LOSS_RATIO_MAP_COLORSCALE)
        asymmetric_region = shapes.Region.from_coordinates(
            TEST_REGION_LARGE_ASYMMETRIC ) 

        gwriter = geotiff.LossMapGeoTiffFile(path, asymmetric_region.grid, 
            normalize=True)
        reference_raster = numpy.zeros((asymmetric_region.grid.rows, 
                                        asymmetric_region.grid.columns),
                                       dtype=numpy.float)

        color_fill = lambda x, y:  (x * y) / 50
        self._fill_rasters(asymmetric_region, gwriter, reference_raster, 
            color_fill)
        gwriter.close()

        self._assert_geotiff_metadata_is_correct(path, asymmetric_region)
        self._assert_geotiff_band_min_max_values(path, 
            GEOTIFF_USED_CHANNEL_IDX, 0, 240)
Example #16
0
    def test_writes_multiple_results_with_one_branch_level(self):
        data = [(shapes.Site(-122.5000, 37.5000), 
                 {  "IDmodel": "MMI_3_1",
                    "investigationTimeSpan": 50.0,
                    "endBranchLabel": "3_1",
                    "IML": [5.0000e-03, 7.0000e-03, 1.3700e-02, 
                    1.9200e-02, 2.6900e-02, 3.7600e-02, 5.2700e-02, 
                    7.3800e-02, 9.8000e-02, 1.0300e-01, 1.4500e-01, 
                    2.0300e-01, 2.8400e-01, 3.9700e-01, 5.5600e-01, 
                    7.7800e-01, 1.0900e+00, 1.5200e+00, 2.1300e+00],
                    "saPeriod": 0.1,
                    "saDamping": 1.0,
                    "IMT": "PGA",
                    "poE": [9.8728e-01, 9.8266e-01, 9.4957e-01, 
                    9.0326e-01, 8.1956e-01, 6.9192e-01, 5.2866e-01, 
                    3.6143e-01, 2.4231e-01, 2.2452e-01, 1.2831e-01, 
                    7.0352e-02, 3.6060e-02, 1.6579e-02, 6.4213e-03, 
                    2.0244e-03, 4.8605e-04, 8.1752e-05, 7.3425e-06]}),
                (shapes.Site(-122.4000, 37.5000), 
                 {  "IDmodel": "MMI_3_1",
                    "investigationTimeSpan": 50.0,
                    "endBranchLabel": "3_1",
                    "IML": [5.0000e-03, 7.0000e-03, 1.3700e-02, 
                    1.9200e-02, 2.6900e-02, 3.7600e-02, 5.2700e-02, 
                    7.3800e-02, 9.8000e-02, 1.0300e-01, 1.4500e-01, 
                    2.0300e-01, 2.8400e-01, 3.9700e-01, 5.5600e-01, 
                    7.7800e-01, 1.0900e+00, 1.5200e+00, 2.1300e+00],
                    "saPeriod": 0.1,
                    "saDamping": 1.0,
                    "IMT": "PGA",
                    "poE": [9.8784e-01, 9.8405e-01, 9.5719e-01, 
                    9.1955e-01, 8.5019e-01, 7.4038e-01, 5.9153e-01, 
                    4.2626e-01, 2.9755e-01, 2.7731e-01, 1.6218e-01, 
                    8.8035e-02, 4.3499e-02, 1.9065e-02, 7.0442e-03, 
                    2.1300e-03, 4.9498e-04, 8.1768e-05, 7.3425e-06]})]

        path = test.test_output_file(TEST_FILE_MULTIPLE_ONE_BRANCH)
        self._remove_and_write_file(path)

        self.writer.serialize(data)
        self._is_xml_valid(path)
Example #17
0
    def test_geotiff_generation_discrete_colorscale_custom_bins(self):
        """Check RGB geotiff generation with colorscale for GMF. Use
        discrete colorscale based on IML values, with custom IML."""
        path = test.test_output_file(
            GEOTIFF_FILENAME_DISCRETE_CUSTOMBIN_COLORSCALE)
        asymmetric_region = shapes.Region.from_coordinates(
            TEST_REGION_LARGE_ASYMMETRIC)

        iml_list = [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, 1.09, 1.52, 2.13]

        gwriter = geotiff.GMFGeoTiffFile(path, asymmetric_region.grid,
            iml_list=iml_list, discrete=True, colormap='matlab-polar')

        reference_raster = numpy.zeros((asymmetric_region.grid.rows, 
                                        asymmetric_region.grid.columns),
                                       dtype=numpy.float)
        self._fill_rasters(asymmetric_region, gwriter, reference_raster, 
            self._colorscale_cuts_fill)
        gwriter.close()
Example #18
0
    def test_geotiff_generation_explicit_colorscale_bins(self):
        """Check RGB geotiff generation with colorscale for GMF. Limits 
        and bins of colorscale are explicitly given."""
        path = test.test_output_file(
            GEOTIFF_FILENAME_EXPLICIT_COLORSCALE_BINS)
        asymmetric_region = shapes.Region.from_coordinates(
            TEST_REGION_LARGE_ASYMMETRIC)

        for test_number, test_list in enumerate(([0.9, 0.95, 1.0, 1.05], 
                                                 None)):
        
            curr_path = "%s.%s.tiff" % (path[0:-5], test_number)
            gwriter = geotiff.GMFGeoTiffFile(curr_path, 
                asymmetric_region.grid, iml_list=test_list, discrete=False)

            reference_raster = numpy.zeros((asymmetric_region.grid.rows, 
                                            asymmetric_region.grid.columns),
                                           dtype=numpy.float)
            self._fill_rasters(asymmetric_region, gwriter, reference_raster, 
                self._colorscale_cuts_fill)
            gwriter.close()
Example #19
0
    def test_loss_curve_plot_generation_multiple_sites(self):
        """Create SVG plots for loss curves read from an NRML file. The
        file contains data for several sites. 
        For each site, a separate SVG file is created."""

        path = test.test_output_file(LOSS_CURVE_PLOT_FILENAME)
        loss_curve_path = test.test_file(LOSS_CURVE_PLOT_INPUTFILE)

        plotter = curve.RiskCurvePlotter(path, loss_curve_path, mode='loss',
            curve_title="This is a test loss curve")

        # delete expected output files, if existing
        for svg_file in plotter.filenames():
            if os.path.isfile(svg_file):
                os.remove(svg_file)

        plotter.plot(autoscale_y=True)

        # assert that for each site in the NRML file an SVG has been created
        for svg_file in plotter.filenames():
            self.assertTrue(os.path.getsize(svg_file) > 0)
Example #20
0
    def test_simple_curve_plot_generation(self):
        """Create an SVG plot of a single (hazard) curve for a single site
        from a dictionary."""

        test_site = shapes.Site(-122, 38)
        test_end_branch = '1_1'
        test_hc_data = {test_end_branch: 
                {'abscissa': [0.0, 1.0, 1.8],
                 'ordinate': [1.0, 0.5, 0.2],
                 'abscissa_property': 'PGA',
                 'ordinate_property': 'Probability of Exceedance',
                 'curve_title': 'Hazard Curve',
                 'Site': test_site}}

        path = test.test_output_file(HAZARDCURVE_PLOT_SIMPLE_FILENAME)
        plot = curve.CurvePlot(path)
        plot.write(test_hc_data)
        plot.close()

        # assert that file has been created and is not empty
        self.assertTrue(os.path.getsize(path) > 0)
        os.remove(path)
Example #21
0
    def test_hazardcurve_plot_generation_multiple_sites_multiple_curves(self):
        """Create SVG plots for hazard curves read from an NRML file. The
        file contains data for several sites, and several end branches of
        the logic tree. For each site, a separate SVG file is created."""

        path = test.test_output_file(HAZARDCURVE_PLOT_FILENAME)
        hazardcurve_path = test.test_file(HAZARDCURVE_PLOT_INPUTFILE)

        plotter = curve.HazardCurvePlotter(path, hazardcurve_path,
            curve_title='Example Hazard Curves')

        # delete expected output files, if existing
        for svg_file in plotter.filenames():
            if os.path.isfile(svg_file):
                os.remove(svg_file)

        plotter.plot()

        # assert that for each site in the NRML file an SVG has been created
        # and is not empty
        for svg_file in plotter.filenames():
            self.assertTrue(os.path.getsize(svg_file) > 0)
            os.remove(svg_file)
Example #22
0
    def _read_curves_inside_region(self, upper_left_cor, lower_right_cor):
        constraint = shapes.RegionConstraint.from_simple(
                upper_left_cor, lower_right_cor)

        reader = hazard_parser.NrmlFile(test.test_output_file(TEST_FILE))
        return reader.filter(constraint)
Example #23
0
 def test_raises_an_error_if_no_curve_is_serialized(self):
     path = test.test_output_file(TEST_FILE)
     self._remove_and_write_file(path)
     self.assertRaises(RuntimeError, self.writer.close)