def test_append_all_pass(self): """Test _append_all function.""" haz_1 = Hazard('TC') haz_1.tag.file_name = 'file1.mat' haz_1.tag.description = 'Description 1' haz_1.centroids = Centroids() haz_1.centroids.set_lat_lon(np.array([1, 3, 5]), np.array([2, 4, 6])) haz_1.event_id = np.array([1]) haz_1.event_name = ['ev1'] haz_1.date = np.array([1]) haz_1.orig = np.array([True]) haz_1.frequency = np.array([1.0]) haz_1.fraction = sparse.csr_matrix([[0.02, 0.03, 0.04]]) haz_1.intensity = sparse.csr_matrix([[0.2, 0.3, 0.4]]) haz_1.units = 'm/s' haz_2 = Hazard('TC') haz_2.tag.file_name = 'file2.mat' haz_2.tag.description = 'Description 2' haz_2.centroids = Centroids() haz_2.centroids.set_lat_lon(np.array([1, 3, 5]), np.array([2, 4, 6])) haz_2.event_id = np.array([1]) haz_2.event_name = ['ev2'] haz_2.date = np.array([2]) haz_2.orig = np.array([False]) haz_2.frequency = np.array([1.0]) haz_2.fraction = sparse.csr_matrix([[1.02, 1.03, 1.04]]) haz_2.intensity = sparse.csr_matrix([[1.2, 1.3, 1.4]]) haz_2.units = 'm/s' haz = Hazard('TC') haz._append_all([haz_1, haz_2]) hres_frac = sparse.csr_matrix([[0.02, 0.03, 0.04], \ [1.02, 1.03, 1.04]]) hres_inten = sparse.csr_matrix([[0.2, 0.3, 0.4], \ [1.2, 1.3, 1.4]]) self.assertTrue(sparse.isspmatrix_csr(haz.intensity)) self.assertTrue( np.array_equal(haz.intensity.todense(), hres_inten.todense())) self.assertTrue(sparse.isspmatrix_csr(haz.fraction)) self.assertTrue( np.array_equal(haz.fraction.todense(), hres_frac.todense())) self.assertEqual(haz.units, haz_2.units) self.assertTrue(np.array_equal(haz.frequency, np.array([1.0, 1.0]))) self.assertTrue(np.array_equal(haz.orig, np.array([True, False]))) self.assertTrue(np.array_equal(haz.date, np.array([1, 2]))) self.assertTrue(np.array_equal(haz.event_id, np.array([1, 2]))) self.assertTrue(haz.event_name, ['ev1', 'ev2']) self.assertTrue( np.array_equal(haz.centroids.coord, haz_1.centroids.coord)) self.assertTrue( np.array_equal(haz.centroids.coord, haz_2.centroids.coord)) self.assertTrue(haz.tag, 'file_1.mat + file_2.mat') self.assertTrue(haz.tag, 'Description 1 + Description 2')
def test_write_fraction_pass(self): """Test write_raster with fraction""" haz_fl = Hazard('FL') haz_fl.event_id = np.array([1]) haz_fl.date = np.array([1]) haz_fl.frequency = np.array([1]) haz_fl.orig = np.array([1]) haz_fl.event_name = ['1'] haz_fl.intensity = sparse.csr_matrix(np.array([0.5, 0.2, 0.1])) haz_fl.fraction = sparse.csr_matrix(np.array([0.5, 0.2, 0.1]) / 2) haz_fl.centroids.set_lat_lon(np.array([1, 2, 3]), np.array([1, 2, 3])) haz_fl.check() haz_fl.write_raster(os.path.join(DATA_DIR, 'test_write_hazard.tif'), intensity=False) haz_read = Hazard('FL') haz_read.set_raster( [os.path.join(DATA_DIR, 'test_write_hazard.tif')], files_fraction=[os.path.join(DATA_DIR, 'test_write_hazard.tif')]) self.assertEqual(haz_read.intensity.shape, (1, 9)) self.assertEqual(haz_read.fraction.shape, (1, 9)) self.assertTrue( np.allclose(np.unique(np.array(haz_read.fraction.toarray())), np.array([0.0, 0.05, 0.1, 0.25]))) self.assertTrue( np.allclose(np.unique(np.array(haz_read.intensity.toarray())), np.array([0.0, 0.05, 0.1, 0.25])))
def test_all_different_extend(self): """Append totally different hazard.""" haz1 = dummy_hazard() haz2 = Hazard('TC') haz2.tag.file_name = 'file2.mat' haz2.tag.description = 'Description 2' haz2.centroids = Centroids() haz2.centroids.set_lat_lon(np.array([7, 9, 11]), np.array([8, 10, 12])) haz2.event_id = np.array([5, 6, 7, 8]) haz2.event_name = ['ev5', 'ev6', 'ev7', 'ev8'] haz2.frequency = np.array([0.9, 0.75, 0.75, 0.22]) haz2.fraction = sparse.csr_matrix([[0.2, 0.3, 0.4], \ [0.1, 0.1, 0.1], \ [0.3, 0.1, 0.9], \ [0.3, 0.2, 0.8]]) haz2.intensity = sparse.csr_matrix([[0.2, 3.3, 6.4], \ [1.1, 0.1, 1.01], \ [8.3, 4.1, 4.0], \ [9.3, 9.2, 1.7]]) haz2.date = np.ones((4,)) haz2.orig = np.ones((4,)) haz2.units = 'm/s' haz1.append(haz2) haz1.check() # expected values haz1_orig = dummy_hazard() exp_inten = np.zeros((8, 6)) exp_inten[0:4, 0:3] = haz1_orig.intensity.todense() exp_inten[4:8, 3:6] = haz2.intensity.todense() exp_frac = np.zeros((8, 6)) exp_frac[0:4, 0:3] = haz1_orig.fraction.todense() exp_frac[4:8, 3:6] = haz2.fraction.todense() self.assertEqual(haz1.event_id.size, 8) self.assertTrue(sparse.isspmatrix_csr(haz1.intensity)) self.assertTrue(sparse.isspmatrix_csr(haz1.fraction)) for i_ev in range(haz1.event_id.size): self.assertTrue(any((haz1.intensity[i_ev].todense() == exp_inten).all(1))) self.assertTrue(any((haz1.fraction[i_ev].todense() == exp_frac).all(1))) self.assertTrue(haz1.event_name[i_ev] in haz1_orig.event_name + haz2.event_name) self.assertTrue(haz1.date[i_ev] in np.append(haz1_orig.date, haz2.date)) self.assertTrue(haz1.orig[i_ev] in np.append(haz1_orig.orig, haz2.orig)) self.assertTrue(haz1.event_id[i_ev] in np.append(haz1_orig.event_id, haz2.event_id)) self.assertTrue(haz1.frequency[i_ev] in np.append(haz1_orig.frequency, haz2.frequency)) self.assertEqual(haz1.centroids.size, 6) self.assertEqual(haz1_orig.units, haz1.units) self.assertEqual(haz1.tag.file_name, \ [haz1_orig.tag.file_name, haz2.tag.file_name]) self.assertEqual(haz1.tag.haz_type, haz1_orig.tag.haz_type) self.assertEqual(haz1.tag.description, \ [haz1_orig.tag.description, haz2.tag.description])
def _event_probabilistic(self, ev_id, i_ens): """ Append a row in the intensity matrix for the probabilistic event. The brightness value of the probabilistic event are randomly selected in the intensity matrix corresponding to the historical event. Fill in the hazard file for the probabilistic event. Parameters: ev_id: id of the selected historical event i_ens (int): number of the generated probabilistic event Returns: new_haz (Hazard): new hazard corresponding to one probabilistic event """ LOGGER.debug('Brightness probabilistic event.') # Probabilistic event ev_proba = np.argwhere(self.centroids.burned >= 1) ev_proba_uni = (ev_proba[:, 0]-1) * self.centroids.nb_centr_lon + ev_proba[:, 1] # Append a row to the intensity matrix with the brightness values of the # probabilistic event # The brightness value is chosen randomly from the brightness values of # the historical event. new_haz = Hazard(HAZ_TYPE) new_haz.intensity = sparse.lil_matrix(np.zeros(len(self.centroids.id))) for _, ev_prob in enumerate(ev_proba_uni): bright_proba = np.random.choice(self.intensity[ev_id -1].data) new_haz.intensity[0, ev_prob] = bright_proba new_haz.intensity = new_haz.intensity.tocsr() # Hazard new_haz.tag = TagHazard(HAZ_TYPE) new_haz.units = 'K' # Kelvin units brightness new_haz.centroids = self.centroids new_haz.event_id = np.ones(1, int) new_haz.frequency = np.ones(1, float) new_haz.event_name = [str(ev_id) + '_gen' + str(i_ens)] new_haz.date = np.array([self.date[int(ev_id-1)]]) new_haz.orig = np.zeros(1, bool) # Following values are defined for each event and centroid new_haz.fraction = new_haz.intensity.copy() new_haz.fraction.data.fill(1.0) return new_haz
def test_read_write_vector_pass(self): """Test write_raster: Hazard from vector data""" haz_fl = Hazard('FL') haz_fl.event_id = np.array([1]) haz_fl.date = np.array([1]) haz_fl.frequency = np.array([1]) haz_fl.orig = np.array([1]) haz_fl.event_name = ['1'] haz_fl.intensity = sparse.csr_matrix(np.array([0.5, 0.2, 0.1])) haz_fl.fraction = sparse.csr_matrix(np.array([0.5, 0.2, 0.1]) / 2) haz_fl.centroids.set_lat_lon(np.array([1, 2, 3]), np.array([1, 2, 3])) haz_fl.check() haz_fl.write_raster(DATA_DIR.joinpath('test_write_hazard.tif')) haz_read = Hazard('FL') haz_read.set_raster([DATA_DIR.joinpath('test_write_hazard.tif')]) self.assertEqual(haz_read.intensity.shape, (1, 9)) self.assertTrue( np.allclose(np.unique(np.array(haz_read.intensity.toarray())), np.array([0.0, 0.1, 0.2, 0.5])))
def dummy_hazard(): hazard = Hazard('TC') hazard.tag.file_name = 'file1.mat' hazard.tag.description = 'Description 1' hazard.centroids = Centroids() hazard.centroids.set_lat_lon(np.array([1, 3, 5]), np.array([2, 4, 6])) hazard.event_id = np.array([1, 2, 3, 4]) hazard.event_name = ['ev1', 'ev2', 'ev3', 'ev4'] hazard.date = np.array([1, 2, 3, 4]) hazard.orig = np.array([True, False, False, True]) hazard.frequency = np.array([0.1, 0.5, 0.5, 0.2]) hazard.fraction = sparse.csr_matrix([[0.02, 0.03, 0.04], \ [0.01, 0.01, 0.01], \ [0.3, 0.1, 0.0], \ [0.3, 0.2, 0.0]]) hazard.intensity = sparse.csr_matrix([[0.2, 0.3, 0.4], \ [0.1, 0.1, 0.01], \ [4.3, 2.1, 1.0], \ [5.3, 0.2, 1.3]]) hazard.units = 'm/s' return hazard
def _event_probabilistic(self, ev_idx, i_ens, centr_burned): """ Define synthetic hazard from randomly burned centroids. Parameters: ev_idx (int): the selected historical event i_ens (int): number of the generated probabilistic event centr_burned (np.array): array containing burned centroids Returns: new_haz (Hazard) """ LOGGER.debug('Brightness probabilistic event.') # The brightness values are chosen randomly at every burned centroids # from the brightness values of the historical event ev_proba_uni = centr_burned.nonzero()[0] * self.centroids.shape[1] + \ centr_burned.nonzero()[1] new_haz = Hazard(HAZ_TYPE) new_haz.intensity = sparse.lil_matrix(np.zeros((1, self.centroids.size))) for ev_prob in ev_proba_uni: new_haz.intensity[0, ev_prob] = np.random.choice( \ self.intensity[ev_idx, :].data) new_haz.intensity = new_haz.intensity.tocsr() # Hazard new_haz.tag = TagHazard(HAZ_TYPE) new_haz.units = 'K' # Kelvin units brightness new_haz.centroids = self.centroids new_haz.event_id = np.ones(1, int) new_haz.frequency = np.ones(1, float) new_haz.event_name = [str(ev_idx+1) + '_gen' + str(i_ens+1)] new_haz.date = np.array([self.date[ev_idx]], int) new_haz.orig = np.zeros(1, bool) # Following values are defined for each event and centroid new_haz.fraction = new_haz.intensity.copy() new_haz.fraction.data.fill(1.0) return new_haz