|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 4 | +# |
| 5 | +# Copyright (C) 2014-2017 GEM Foundation and G. Weatherill |
| 6 | +# |
| 7 | +# OpenQuake is free software: you can redistribute it and/or modify it |
| 8 | +# under the terms of the GNU Affero General Public License as published |
| 9 | +# by the Free Software Foundation, either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# OpenQuake is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU Affero General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Affero General Public License |
| 18 | +# along with OpenQuake. If not, see <http://www.gnu.org/licenses/>. |
| 19 | +""" |
| 20 | +Tests for generation of data for trellis plots |
| 21 | +""" |
| 22 | +import unittest |
| 23 | +import os |
| 24 | +import json |
| 25 | +import numpy as np |
| 26 | +import smtk.trellis.trellis_plots as trpl |
| 27 | +import smtk.trellis.configure as rcfg |
| 28 | + |
| 29 | + |
| 30 | +BASE_DATA_PATH = os.path.join(os.path.dirname(__file__), "data") |
| 31 | + |
| 32 | + |
| 33 | +class BaseTrellisTest(unittest.TestCase): |
| 34 | + """ |
| 35 | + This core test is designed to run a series of trellis plot calculations |
| 36 | + and ensure compatibility with previously generated results |
| 37 | + """ |
| 38 | + |
| 39 | + TEST_FILE = None |
| 40 | + |
| 41 | + def setUp(self): |
| 42 | + self.imts = ["PGA", "SA(0.2)", "SA(2.0)", "SA(3.0)"] |
| 43 | + self.periods = [0.05, 0.075, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, |
| 44 | + 0.17, 0.18, 0.19, 0.20, 0.22, 0.24, 0.26, 0.28, 0.30, |
| 45 | + 0.32, 0.34, 0.36, 0.38, 0.40, 0.42, 0.44, 0.46, 0.48, |
| 46 | + 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, |
| 47 | + 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, |
| 48 | + 3.0, 4.001, 5.0, 7.5, 10.0] |
| 49 | + |
| 50 | + self.gsims = ["AkkarBommer2010", "CauzziFaccioli2008", |
| 51 | + "ChiouYoungs2008", "ZhaoEtAl2006Asc", "AkkarEtAlRjb2014", |
| 52 | + "BindiEtAl2014Rjb", "CauzziEtAl2014", "DerrasEtAl2014", |
| 53 | + "AbrahamsonEtAl2014", "BooreEtAl2014", "ChiouYoungs2014", |
| 54 | + "CampbellBozorgnia2014", "KothaEtAl2016Italy", |
| 55 | + "KothaEtAl2016Other", "KothaEtAl2016Turkey", |
| 56 | + "ZhaoEtAl2016Asc", "BindiEtAl2017Rjb"] |
| 57 | + |
| 58 | + |
| 59 | +class DistanceTrellisTest(BaseTrellisTest): |
| 60 | + TEST_FILE = "test_distance_imt_trellis.json" |
| 61 | + |
| 62 | + def compare_jsons(self, old, new): |
| 63 | + """ |
| 64 | + Compares the json data from file with the new data from trellis plot |
| 65 | + """ |
| 66 | + # Check x-labels are the same |
| 67 | + self.assertEqual(old["xlabel"], new["xlabel"]) |
| 68 | + # Check x-values are the same |
| 69 | + np.testing.assert_array_almost_equal(old["xvalues"], new["xvalues"], 7) |
| 70 | + for imt in old["figures"]: |
| 71 | + self.assertEqual(old["figures"][imt]["ylabel"], |
| 72 | + new["figures"][imt]["ylabel"]) |
| 73 | + for gsim in old["figures"][imt]["yvalues"]: |
| 74 | + np.testing.assert_array_almost_equal( |
| 75 | + old["figures"][imt]["yvalues"][gsim], |
| 76 | + new["figures"][imt]["yvalues"][gsim], 7) |
| 77 | + |
| 78 | + def _run_trellis(self, rupture): |
| 79 | + """ |
| 80 | + Executes the trellis plotting - for mean |
| 81 | + """ |
| 82 | + return trpl.DistanceIMTTrellis.from_rupture_model(rupture, |
| 83 | + self.gsims, |
| 84 | + self.imts, |
| 85 | + distance_type="rrup") |
| 86 | + |
| 87 | + def test_distance_imt_trellis(self): |
| 88 | + """ |
| 89 | + Tests the DistanceIMT trellis data generation |
| 90 | + """ |
| 91 | + reference = json.load(open( |
| 92 | + os.path.join(BASE_DATA_PATH, self.TEST_FILE), "r")) |
| 93 | + # Setup rupture |
| 94 | + rupture = rcfg.GSIMRupture(6.5, 60., 1.5, |
| 95 | + hypocentre_location=(0.5, 0.5)) |
| 96 | + _ = rupture.get_target_sites_line(250.0, 1.0, 800.0) |
| 97 | + # Get trellis calculations |
| 98 | + trl = self._run_trellis(rupture) |
| 99 | + # Parse the json formatted string to a dictionary string |
| 100 | + results = json.loads(trl.to_json()) |
| 101 | + # Compare the two dictionaries |
| 102 | + self.compare_jsons(reference, results) |
| 103 | + |
| 104 | + |
| 105 | +class DistanceSigmaTrellisTest(DistanceTrellisTest): |
| 106 | + TEST_FILE = "test_distance_sigma_imt_trellis.json" |
| 107 | + |
| 108 | + def _run_trellis(self, rupture): |
| 109 | + """ |
| 110 | + Executes the trellis plotting - for standard deviation |
| 111 | + """ |
| 112 | + return trpl.DistanceSigmaIMTTrellis.from_rupture_model( |
| 113 | + rupture, |
| 114 | + self.gsims, |
| 115 | + self.imts, |
| 116 | + distance_type="rrup") |
| 117 | + |
| 118 | + |
| 119 | +class MagnitudeTrellisTest(BaseTrellisTest): |
| 120 | + TEST_FILE = "test_magnitude_imt_trellis.json" |
| 121 | + |
| 122 | + def _run_trellis(self, magnitudes, distance, properties): |
| 123 | + """ |
| 124 | + Executes the trellis plotting - for mean |
| 125 | + """ |
| 126 | + return trpl.MagnitudeIMTTrellis.from_rupture_model(properties, |
| 127 | + magnitudes, |
| 128 | + distance, |
| 129 | + self.gsims, |
| 130 | + self.imts) |
| 131 | + |
| 132 | + def compare_jsons(self, old, new): |
| 133 | + """ |
| 134 | + Compares the json data from file with the new data from trellis plot |
| 135 | + """ |
| 136 | + self.assertEqual(old["xlabel"], new["xlabel"]) |
| 137 | + np.testing.assert_array_almost_equal(old["xvalues"], new["xvalues"], 7) |
| 138 | + for imt in old["figures"]: |
| 139 | + for gsim in old["figures"][imt]: |
| 140 | + if gsim == u"ylabel": |
| 141 | + self.assertEqual(old["figures"][imt][gsim], |
| 142 | + new["figures"][imt][gsim]) |
| 143 | + else: |
| 144 | + np.testing.assert_array_almost_equal( |
| 145 | + old["figures"][imt][gsim], |
| 146 | + new["figures"][imt][gsim], 7) |
| 147 | + |
| 148 | + def test_magnitude_imt_trellis(self): |
| 149 | + """ |
| 150 | + Tests the MagnitudeIMT trellis data generation |
| 151 | + """ |
| 152 | + reference = json.load(open( |
| 153 | + os.path.join(BASE_DATA_PATH, self.TEST_FILE), "r")) |
| 154 | + magnitudes = np.arange(4., 8.1, 0.1) |
| 155 | + distance = 20. |
| 156 | + properties = {"dip": 60.0, "rake": -90.0, "aspect": 1.5, "ztor": 0.0, |
| 157 | + "vs30": 800.0, "backarc": False, "z1pt0": 50.0, |
| 158 | + "z2pt5": 1.0, "line_azimuth": 90.0} |
| 159 | + trl = self._run_trellis(magnitudes, distance, properties) |
| 160 | + results = json.loads(trl.to_json()) |
| 161 | + self.compare_jsons(reference, results) |
| 162 | + |
| 163 | + |
| 164 | +class MagnitudeSigmaTrellisTest(MagnitudeTrellisTest): |
| 165 | + TEST_FILE = "test_magnitude_sigma_imt_trellis.json" |
| 166 | + |
| 167 | + def _run_trellis(self, magnitudes, distance, properties): |
| 168 | + """ |
| 169 | + Executes the trellis plotting - for standard deviation |
| 170 | + """ |
| 171 | + return trpl.MagnitudeSigmaIMTTrellis.from_rupture_model(properties, |
| 172 | + magnitudes, |
| 173 | + distance, |
| 174 | + self.gsims, |
| 175 | + self.imts) |
| 176 | + |
| 177 | + |
| 178 | +class MagnitudeDistanceSpectraTrellisTest(BaseTrellisTest): |
| 179 | + TEST_FILE = "test_magnitude_distance_spectra_trellis.json" |
| 180 | + |
| 181 | + def compare_jsons(self, old, new): |
| 182 | + """ |
| 183 | + Compares the MagnitudeDistanceSpectra jsons |
| 184 | + """ |
| 185 | + self.assertEqual(old["xlabel"], new["xlabel"]) |
| 186 | + self.assertEqual(old["ylabel"], new["ylabel"]) |
| 187 | + np.testing.assert_array_almost_equal(old["xvalues"], new["xvalues"], 7) |
| 188 | + for key in old["figures"]: |
| 189 | + self.assertAlmostEqual(old["figures"][key]["magnitude"], |
| 190 | + new["figures"][key]["magnitude"], 7) |
| 191 | + self.assertAlmostEqual(old["figures"][key]["distance"], |
| 192 | + new["figures"][key]["distance"], 7) |
| 193 | + self.assertEqual(old["figures"][key]["row"], |
| 194 | + new["figures"][key]["row"]) |
| 195 | + self.assertEqual(old["figures"][key]["column"], |
| 196 | + new["figures"][key]["column"]) |
| 197 | + for gsim in old["figures"][key]["yvalues"]: |
| 198 | + old_vals = np.array(old["figures"][key]["yvalues"][gsim]) |
| 199 | + new_vals = np.array(new["figures"][key]["yvalues"][gsim]) |
| 200 | + if old_vals.dtype == "O": |
| 201 | + # Has None Values - compare element by element |
| 202 | + for old_val, new_val in zip(old_vals, new_vals): |
| 203 | + if old_val and new_val: |
| 204 | + self.assertAlmostEqual(old_val, new_val, 7) |
| 205 | + else: |
| 206 | + self.assertEqual(old_val, new_val) |
| 207 | + else: |
| 208 | + np.testing.assert_array_almost_equal(old_vals, new_vals, 7) |
| 209 | + |
| 210 | + def _run_trellis(self, magnitudes, distances, properties): |
| 211 | + """ |
| 212 | + Executes the trellis plotting - for mean |
| 213 | + """ |
| 214 | + return trpl.MagnitudeDistanceSpectraTrellis.from_rupture_model( |
| 215 | + properties, magnitudes, distances, self.gsims, self.periods, |
| 216 | + distance_type="rrup") |
| 217 | + |
| 218 | + def test_magnitude_distance_spectra_trellis(self): |
| 219 | + """ |
| 220 | + Tests the MagnitudeDistanceSpectra Trellis data generation |
| 221 | + """ |
| 222 | + reference = json.load(open( |
| 223 | + os.path.join(BASE_DATA_PATH, self.TEST_FILE), "r")) |
| 224 | + properties = {"dip": 60.0, "rake": -90.0, "aspect": 1.5, "ztor": 0.0, |
| 225 | + "vs30": 800.0, "backarc": False, "z1pt0": 50.0, |
| 226 | + "z2pt5": 1.0} |
| 227 | + magnitudes = [4.0, 5.0, 6.0, 7.0] |
| 228 | + distances = [5., 20., 50., 150.0] |
| 229 | + trl = self._run_trellis(magnitudes, distances, properties) |
| 230 | + results = json.loads(trl.to_json()) |
| 231 | + self.compare_jsons(reference, results) |
| 232 | + |
| 233 | + |
| 234 | +class MagnitudeDistanceSpectraSigmaTrellisTest( |
| 235 | + MagnitudeDistanceSpectraTrellisTest): |
| 236 | + TEST_FILE = "test_magnitude_distance_spectra_sigma_trellis.json" |
| 237 | + |
| 238 | + def _run_trellis(self, magnitudes, distances, properties): |
| 239 | + """ |
| 240 | + Executes the trellis plotting - for standard deviation |
| 241 | + """ |
| 242 | + return trpl.MagnitudeDistanceSpectraSigmaTrellis.from_rupture_model( |
| 243 | + properties, magnitudes, distances, self.gsims, self.periods, |
| 244 | + distance_type="rrup") |
0 commit comments