def test_calc_bright_pass(self):
        """ Test _calc_brightness """
        #        from pathos.pools import ProcessPool as Pool
        #        pool = Pool()
        bf = BushFire()
        firms = bf._clean_firms_csv(TEST_FIRMS)
        firms['datenum'].values[100] = 7000
        firms = bf._firms_cons_days(firms)
        firms = bf._firms_clustering(firms, DEF_CENTROIDS[1] / 2 / 15, 15)
        bf._firms_event(2, firms.cons_id.values, firms.clus_id.values,
                        firms.event_id.values, firms.iter_ev.values,
                        firms.datenum.values)
        firms.latitude[8169] = firms.loc[16]['latitude']
        firms.longitude[8169] = firms.loc[16]['longitude']
        bf._calc_brightness(firms, DEF_CENTROIDS[0], DEF_CENTROIDS[1])
        bf.check()

        self.assertEqual(bf.tag.haz_type, 'BF')
        self.assertEqual(bf.tag.description, '')
        self.assertEqual(bf.units, 'K')
        self.assertEqual(bf.centroids.size, 19454)
        self.assertTrue(np.allclose(bf.event_id, np.arange(1, 8)))
        self.assertEqual(bf.event_name, ['1', '2', '3', '4', '5', '6', '7'])
        self.assertTrue(bf.frequency.size, 0)
        self.assertTrue(isinstance(bf.intensity, sparse.csr_matrix))
        self.assertTrue(isinstance(bf.fraction, sparse.csr_matrix))
        self.assertEqual(bf.intensity.shape, (7, 19454))
        self.assertEqual(bf.fraction.shape, (7, 19454))
        self.assertEqual(bf.fraction.max(), 1.0)
        self.assertAlmostEqual(bf.intensity[0, 16618],
                               firms.loc[100].brightness)
        self.assertAlmostEqual(
            bf.intensity[1, 123],
            max(firms.loc[6721].brightness, firms.loc[6722].brightness))
        self.assertAlmostEqual(bf.intensity[2, :].max(),
                               firms.loc[8105].brightness)
        self.assertAlmostEqual(bf.intensity[4, 19367],
                               firms.loc[2879].brightness)
        self.assertAlmostEqual(bf.intensity[5, 10132],
                               firms.loc[8176].brightness)
        self.assertAlmostEqual(
            bf.intensity[6, 10696],
            max(firms.loc[9322].brightness, firms.loc[9324].brightness))
        self.assertAlmostEqual(
            bf.intensity[6, 10697],
            max(firms.loc[9321].brightness, firms.loc[9323].brightness))
        self.assertEqual((bf.intensity[0, :] > 0).sum(), 1)
        self.assertEqual((bf.intensity[1, :] > 0).sum(), 424)
        self.assertEqual((bf.intensity[2, :] > 0).sum(), 1)
        self.assertEqual((bf.intensity[3, :] > 0).sum(), 1112)
        self.assertEqual((bf.intensity[4, :] > 0).sum(), 1)
        self.assertEqual((bf.intensity[5, :] > 0).sum(), 264)
        self.assertEqual((bf.intensity[6, :] > 0).sum(), 2)
    def test_firms_event_pass(self):
        """ Test _firms_event """
        bf = BushFire()
        firms_ori = bf._clean_firms_csv(TEST_FIRMS)
        firms_ori['datenum'].values[100] = 7000
        firms_ori = bf._firms_cons_days(firms_ori)
        firms_ori = bf._firms_clustering(firms_ori, 0.375 / 2 / 15, 15)
        firms = firms_ori.copy()
        bf._firms_event(2, firms.cons_id.values, firms.clus_id.values,
                        firms.event_id.values, firms.iter_ev.values,
                        firms.datenum.values)
        self.assertEqual(len(firms), 9325)
        self.assertTrue(np.allclose(np.unique(firms.event_id), np.arange(7)))
        self.assertTrue(np.allclose(firms.event_id[-4:], np.ones(4) * 6))
        self.assertEqual(firms.event_id.values[100], 0)
        self.assertEqual(firms.event_id.values[4255], 1)
        self.assertEqual(firms.event_id.values[8105], 2)
        self.assertEqual(firms.event_id.values[0], 3)
        self.assertEqual(firms.event_id.values[2], 3)
        self.assertEqual(firms.event_id.values[4254], 3)
        self.assertEqual(firms.event_id.values[2879], 4)
        self.assertEqual(firms.event_id.values[9320], 5)

        self.assertFalse(firms.iter_ev.any())

        for ev_id in np.unique(firms.event_id):
            if np.unique(firms[firms.event_id == ev_id].cons_id).size != 1:
                self.assertEqual(1, 0)
            if np.unique(firms[firms.event_id == ev_id].clus_id).size != 1:
                self.assertEqual(1, 0)

        for col_name in firms.columns:
            if col_name not in ('event_id', 'acq_date', 'confidence',
                                'instrument', 'satellite', 'iter_ev'):
                self.assertTrue(
                    np.allclose(firms[col_name].values,
                                firms_ori[col_name].values))
            elif col_name not in ('event_id', 'iter_ev'):
                for elem_l, elem_r in zip(firms[col_name].values,
                                          firms_ori[col_name].values):
                    self.assertEqual(elem_l, elem_r)
 def test_iter_events_pass(self):
     """ Test identification of events """
     ori_thres = BushFire.days_thres
     bf = BushFire()
     firms = bf._clean_firms_csv(TEST_FIRMS)
     firms['datenum'].values[100] = 7000
     i_iter = 0
     BushFire.days_thres = 3
     while firms.iter_ev.any():
         # Compute cons_id: consecutive events in current iteration
         bf._firms_cons_days(firms)
         # Compute clus_id: cluster identifier inside cons_id
         bf._firms_clustering(firms, 0.375, 15)
         # compute event_id
         BushFire.days_thres = 2
         bf._firms_event(BushFire.days_thres, firms.cons_id.values,
                         firms.clus_id.values, firms.event_id.values,
                         firms.iter_ev.values, firms.datenum.values)
         i_iter += 1
     self.assertEqual(i_iter, 2)
     BushFire.days_thres = ori_thres
    def test_hist_event_one_pass(self):
        bf = BushFire()
        firms = {
            'latitude': [
                -38.104, -38.104, -38.104, -38.104, -38.104, -38.093, -38.095,
                -37.433, -37.421, -37.423, -37.45, -38.104, -38.104, -38.104,
                -38.095, -37.45, -38.045, -38.047, -38.036, -37.983, -37.978,
                -37.979, -37.981, -37.45, -37.431, -37.421, -37.423, -38.104
            ],
            'longitude': [
                146.388, 146.388, 146.388, 146.388, 146.388, 146.397, 146.386,
                142.43, 142.442, 142.428, 145.361, 146.388, 146.388, 146.388,
                146.397, 145.361, 146.416, 146.404, 146.413, 146.33, 146.311,
                146.299, 146.288, 145.361, 142.445, 142.442, 142.428, 146.388
            ],
            'brightness': [
                400, 10, 316.5, 150, 500, 312.6, 312.7, 324.4, 373.6, 359.6,
                312.9, 100, 400, 500, 300, 250, 100, 150, 300, 400, 250, 300,
                332, 450, 200, 150, 400, 100
            ],
            'acq_date': [
                '2008-03-08', '2008-03-08', '2006-01-24', '2006-01-24',
                '2006-01-24', '2006-01-24', '2006-01-24', '2006-01-24',
                '2006-01-24', '2006-01-24', '2006-01-24', '2006-01-25',
                '2006-01-25', '2006-01-28', '2006-01-28', '2006-01-30',
                '2006-03-06', '2006-03-06', '2006-03-06', '2006-03-06',
                '2007-01-24', '2007-01-24', '2007-01-24', '2007-01-24',
                '2007-01-24', '2008-03-08', '2008-03-08', '2008-03-08'
            ]
        }

        firms, description = TestReaderDF._read_firms_synth(firms)

        self.assertEqual(firms['latitude'][0], -38.104)
        self.assertEqual(firms['longitude'][0], 146.388)
        self.assertEqual(firms['latitude'].iloc[-1], -38.104)
        self.assertEqual(firms['longitude'].iloc[-1], 146.388)
        self.assertFalse(firms['datenum'][0] == firms['datenum'][9])

        firms = bf._firms_cons_days(firms)

        self.assertEqual(firms['cons_id'][0], 0)
        self.assertEqual(firms['cons_id'][7], 0)
        self.assertEqual(firms['cons_id'][8], 0)
        self.assertEqual(firms['cons_id'][9], 0)
        self.assertEqual(firms['cons_id'][10], 0)
        self.assertEqual(firms['cons_id'][11], 1)
        self.assertEqual(firms['cons_id'][13], 1)
        self.assertEqual(firms['cons_id'][15], 2)
        self.assertEqual(firms['cons_id'][21], 3)
        self.assertEqual(firms['cons_id'][26], 4)

        centroids, res_data = bf._centroids_creation(firms, 1)

        firms = bf._firms_clustering(firms, res_data)

        self.assertEqual(max((firms['clus_id'][:10]).values), 2)
        self.assertEqual(max((firms['clus_id'][11:13]).values), 0)

        firms = bf._firms_event(firms)

        self.assertEqual(max((firms['event_id'][:10]).values), 3)
        self.assertEqual(firms['event_id'][11], 4)
        self.assertEqual(firms['event_id'][12], 4)
        self.assertEqual(firms['event_id'][13], 5)

        # add brightness matrix
        bf._calc_brightness(firms, centroids)

        self.assertEqual(bf.intensity[0, 1172], 500)
        self.assertEqual(bf.intensity[0, 4714], 312.7)
        self.assertEqual(bf.intensity[0, 4717], 312.6)
        self.assertEqual(bf.intensity[1, 227620], 312.9)
        self.assertEqual(bf.intensity[2, 232658], 324.4)
        self.assertEqual(bf.intensity[2, 236200], 359.6)
        self.assertEqual(bf.intensity[2, 237385], 373.6)
        self.assertEqual(bf.intensity[3, 1172], 500)
        self.assertEqual(bf.intensity[3, 4717], 300)
        self.assertEqual(bf.intensity[4, 227620], 250)

        # add probabilistic event
        ev_id = 1
        bf._random_bushfire_one_event(ev_id, 1)
Esempio n. 5
0
    def test_random_brightness_one_pass(self):
        bf = BushFire()
        firms, description = bf._read_firms_csv(TEST_FIRMS)

        firms = bf._clean_firms_csv(firms)

        self.assertEqual(firms['latitude'][0], 36.46245)
        self.assertEqual(firms['longitude'][0], -121.8989)
        self.assertEqual(firms['latitude'].iloc[-1], 36.17266)
        self.assertEqual(firms['longitude'].iloc[-1], -121.61211000000002)

        centroids, res_data = bf._centroids_creation(firms, 1)
        firms = bf._firms_cons_days(firms)

        self.assertEqual(firms['cons_id'][9320], 0)
        self.assertEqual(firms['cons_id'][9321], 1)

        firms = bf._firms_clustering(firms, res_data)

        self.assertEqual(firms['clus_id'][1621], 0)
        self.assertEqual(firms['clus_id'][1622], 1)
        self.assertEqual(firms['clus_id'][1623], 2)

        firms = bf._firms_event(firms)

        self.assertEqual(firms['event_id'][1621], 1)
        self.assertEqual(firms['event_id'][1622], 2)
        self.assertEqual(firms['event_id'][1623], 3)
        self.assertEqual(firms['event_id'][9319], 3)
        self.assertEqual(firms['event_id'][9320], 4)

        bf._calc_brightness(firms, centroids)
        bf.check()

        self.assertEqual(bf.tag.haz_type, 'BF')
        self.assertEqual(bf.tag.description, '')
        self.assertEqual(bf.units, 'K')
        self.assertEqual(bf.centroids.size, 78090)
        self.assertEqual(bf.event_id.size, 5)
        self.assertEqual(bf.event_id[0], 1)
        self.assertEqual(bf.event_name, ['1.0', '2.0', '3.0', '4.0', '5.0'])
        self.assertTrue(np.array_equal(bf.frequency, np.array([1, 1, 1, 1, 1])))
        self.assertTrue(isinstance(bf.intensity, sparse.csr_matrix))
        self.assertTrue(isinstance(bf.fraction, sparse.csr_matrix))
        self.assertEqual(bf.intensity.shape, (5, 78090))
        self.assertEqual(bf.fraction.shape, (5, 78090))

        bf._area_one_year(2016)

        ev_id = 3
        area_hull_one_event = bf._hull_burned_area(ev_id)

        self.assertAlmostEqual(area_hull_one_event, 60421, delta = 500)

        prob_event = bf._random_bushfire_one_event(ev_id, 1)
        prob_event.check()

        self.assertEqual(prob_event.tag.haz_type, 'BF')
        self.assertEqual(prob_event.tag.description, '')
        self.assertEqual(prob_event.units, 'K')
        self.assertEqual(prob_event.centroids.size, 78090)
        self.assertEqual(prob_event.event_id.size, 1)
        self.assertEqual(prob_event.event_id[0], 1)
        self.assertEqual(prob_event.event_name, ['3_gen1'])
        self.assertTrue(np.array_equal(prob_event.frequency, np.array([1])))
        self.assertTrue(isinstance(prob_event.intensity, sparse.csr_matrix))
        self.assertTrue(isinstance(prob_event.fraction, sparse.csr_matrix))
        self.assertEqual(prob_event.intensity.shape, (1, 78090))
        self.assertEqual(prob_event.fraction.shape, (1, 78090))

        self.assertAlmostEqual(prob_event.intensity.nonzero()[0].size, 3928, delta = 10)

        self.assertAlmostEqual(bf.intensity[ev_id - 1].data.mean(),prob_event.intensity[0].data.mean(), delta = 2)
        self.assertAlmostEqual(bf.intensity[ev_id - 1].data.std(), prob_event.intensity[0].data.std(), delta = 2)