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_same_centroids_extend(self):
        """Append hazard with same centroids, different events."""
        haz1 = dummy_hazard()
        haz2 = Hazard('TC')
        haz2.tag.file_name = 'file2.mat'
        haz2.tag.description = 'Description 2'

        haz2.centroids = haz1.centroids
        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.units = 'm/s'

        haz1.append(haz2)
        haz1.check()

        # expected values
        haz1_orig = dummy_hazard()
        exp_inten = np.zeros((8, 3))
        exp_inten[0:4, 0:3] = haz1_orig.intensity.todense()
        exp_inten[4:8, 0:3] = haz2.intensity.todense()
        exp_frac = np.zeros((8, 3))
        exp_frac[0:4, 0:3] = haz1_orig.fraction.todense()
        exp_frac[4:8, 0:3] = 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, 3)
        self.assertTrue(
            np.array_equal(haz1.centroids.coord, haz2.centroids.coord))
        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])
Exemple #3
0
    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])))
Exemple #4
0
 def test_degenerate_pass(self):
     """ Test degenerate call. """
     haz = Hazard('TC')
     haz.read_mat(HAZ_TEST_MAT)
     return_period = np.array([25, 50, 100, 250])
     haz.intensity = sparse.csr.csr_matrix(np.zeros(haz.intensity.shape))
     inten_stats = haz.local_exceedance_inten(return_period)
     self.assertTrue(np.array_equal(inten_stats, np.zeros((4, 100))))
Exemple #5
0
    def test_same_events_append(self):
        """Append hazard with same events (and diff centroids).
        Events are appended with all new centroids columns. """
        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 = haz1.event_id
        haz2.event_name = haz1.event_name.copy()
        haz2.frequency = haz1.frequency
        haz2.date = haz1.date
        haz2.fraction = sparse.csr_matrix([[0.22, 0.32, 0.44], \
                                           [0.11, 0.11, 0.11], \
                                           [0.32, 0.11, 0.99], \
                                           [0.32, 0.22, 0.88]])
        haz2.intensity = sparse.csr_matrix([[0.22, 3.33, 6.44], \
                                            [1.11, 0.11, 1.11], \
                                            [8.33, 4.11, 4.4], \
                                            [9.33, 9.22, 1.77]])
        haz2.units = 'm/s'

        haz1.append(haz2)

        # expected values
        haz1_ori = dummy_hazard()
        res_inten = sparse.lil_matrix(np.zeros((8, 6)))
        res_inten[0:4, 0:3] = haz1_ori.intensity
        res_inten[4:, 3:] = haz2.intensity

        res_frac = sparse.lil_matrix(np.zeros((8, 6)))
        res_frac[0:4, 0:3] = haz1_ori.fraction
        res_frac[4:, 3:] = haz2.fraction

        self.assertTrue(np.array_equal(res_inten.todense(),
                                       haz1.intensity.todense()))
        self.assertTrue(sparse.isspmatrix_csr(haz1.intensity))
        self.assertTrue(np.array_equal(res_frac.todense(), \
                                       haz1.fraction.todense()))
        self.assertTrue(sparse.isspmatrix_csr(haz1.fraction))
        self.assertEqual(haz1.event_name,
                         haz1_ori.event_name + haz2.event_name)
        self.assertTrue(np.array_equal(haz1.date,
                                       np.append(haz1_ori.date, haz2.date)))
        self.assertTrue(np.array_equal(haz1.orig,
                                       np.append(haz1_ori.orig, haz2.orig)))
        self.assertTrue(np.array_equal(haz1.event_id, np.arange(1,9)))
        self.assertTrue(np.array_equal(haz1.frequency,
                                       np.append(haz1_ori.frequency, haz2.frequency)))
        self.assertEqual(haz1_ori.units, haz1.units)

        self.assertEqual(haz1.tag.file_name, \
                         [haz1_ori.tag.file_name, haz2.tag.file_name])
        self.assertEqual(haz1.tag.haz_type, haz1_ori.tag.haz_type)
        self.assertEqual(haz1.tag.description, \
                         [haz1_ori.tag.description, haz2.tag.description])
Exemple #6
0
    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
Exemple #7
0
    def test_same_events_same(self):
        """Append hazard with same events and diff centroids. After removing
        duplicate events, initial events are obtained with 0 intensity and
        fraction in new appended centroids."""
        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 = haz1.event_id
        haz2.event_name = haz1.event_name
        haz2.frequency = haz1.frequency
        haz2.date = haz1.date
        haz2.fraction = sparse.csr_matrix([[0.22, 0.32, 0.44], \
                                           [0.11, 0.11, 0.11], \
                                           [0.32, 0.11, 0.99], \
                                           [0.32, 0.22, 0.88]])
        haz2.intensity = sparse.csr_matrix([[0.22, 3.33, 6.44], \
                                            [1.11, 0.11, 1.11], \
                                            [8.33, 4.11, 4.4], \
                                            [9.33, 9.22, 1.77]])
        haz2.units = 'm/s'

        haz1.append(haz2)
        haz1.remove_duplicates()
        haz1.check()

        # expected values
        haz_res = dummy_hazard()
        haz_res.intensity = sparse.hstack([haz_res.intensity, \
            sparse.lil_matrix((haz_res.intensity.shape[0], 3))], format='lil').tocsr()
        haz_res.fraction = sparse.hstack([haz_res.fraction, \
            sparse.lil_matrix((haz_res.fraction.shape[0], 3))], format='lil').tocsr()
        self.assertTrue(np.array_equal(haz_res.intensity.todense(), \
                                       haz1.intensity.todense()))
        self.assertTrue(sparse.isspmatrix_csr(haz1.intensity))
        self.assertTrue(np.array_equal(haz_res.fraction.todense(), \
                                       haz1.fraction.todense()))
        self.assertTrue(sparse.isspmatrix_csr(haz1.fraction))
        self.assertEqual(haz1.event_name, haz_res.event_name)
        self.assertTrue(np.array_equal(haz1.date, haz_res.date))
        self.assertTrue(np.array_equal(haz1.orig, haz_res.orig))
        self.assertTrue(np.array_equal(haz1.event_id, \
                                       haz_res.event_id))
        self.assertTrue(np.array_equal(haz1.frequency, haz_res.frequency))
        self.assertEqual(haz_res.units, haz1.units)

        self.assertEqual(haz1.tag.file_name, \
                         [haz_res.tag.file_name, haz2.tag.file_name])
        self.assertEqual(haz1.tag.haz_type, haz_res.tag.haz_type)
        self.assertEqual(haz1.tag.description, \
                         [haz_res.tag.description, haz2.tag.description])
Exemple #8
0
    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
Exemple #9
0
    def good_hazard():
        """Define well a hazard"""
        haz = Hazard('TC')
        haz.centroids = Centroids()
        haz.centroids.set_lat_lon(np.array([1, 3]), np.array([2, 3]))
        haz.centroids.region_id = np.array([1, 2])
        haz.event_id = np.array([1, 2, 3])
        haz.event_name = ['A', 'B', 'C']
        haz.frequency = np.array([1, 2, 3])
        # events x centroids
        haz.intensity = sparse.csr_matrix([[1, 2], [1, 2], [1, 2]])
        haz.fraction = sparse.csr_matrix([[1, 2], [1, 2], [1, 2]])

        return haz
Exemple #10
0
    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])))
Exemple #11
0
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
        basin_shp = MultiPolygon([basin_shp])
        haz_pos.set_raster(files_intensity=[trend_file],
                           files_fraction=[trend_file],
                           band=[1],
                           geometry=basin_shp)
        haz_neg.set_raster(files_intensity=[trend_file],
                           files_fraction=[trend_file],
                           band=[1],
                           geometry=basin_shp)

    dis_map_pos = np.greater(haz_pos.intensity.todense(), 0)
    new_trends = dis_map_pos.astype(int)

    haz_pos.fraction = sp.sparse.csr_matrix(new_trends)
    haz_pos.intensity = sp.sparse.csr_matrix(new_trends)

    dis_map_neg = np.less(haz_neg.fraction.todense(), 0)

    new_trends_neg = dis_map_neg.astype(int)

    haz_neg.fraction = sp.sparse.csr_matrix(new_trends_neg)
    haz_neg.intensity = sp.sparse.csr_matrix(new_trends_neg)

    if new_trends.sum() > new_trends_neg.sum():
        dis_reg_geo = 1
    elif new_trends.sum() < new_trends_neg.sum():
        dis_reg_geo = -1
    else:
        dis_reg_geo = 0