def test_bush_fire_all_rnd_pass(self): bf = BushFire() centr_res_factor = 1 bf.set_bush_fire(TEST_FIRMS, centr_res_factor, seed = 8) bf.set_proba_all_event(ens_size = 3) 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, 20) self.assertEqual(bf.event_id[0], 1) self.assertEqual(bf.event_name, ['1.0', '2.0', '3.0', '4.0', '5.0', '1.0_gen0', '1.0_gen1', '1.0_gen2', '2.0_gen0', '2.0_gen1', '2.0_gen2', '3.0_gen0', '3.0_gen1', '3.0_gen2', '4.0_gen0', '4.0_gen1', '4.0_gen2', '5.0_gen0', '5.0_gen1', '5.0_gen2']) self.assertTrue(np.array_equal(bf.frequency, np.array([0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25]))) self.assertTrue(isinstance(bf.intensity, sparse.csr_matrix)) self.assertTrue(isinstance(bf.fraction, sparse.csr_matrix)) self.assertEqual(bf.intensity.shape, (20, 78090)) self.assertEqual(bf.fraction.shape, (20, 78090))
def test_bush_fire_rnd_one_pass(self): bf = BushFire() centr_res_factor = 1 bf.set_bush_fire(TEST_FIRMS, centr_res_factor, seed = 8) bf_haz = bf._set_proba_one_event(ev_id = 3, ens_size = 3) prob_haz = BushFire() prob_haz._append_all(bf_haz) bf.append(prob_haz) 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, 8) self.assertEqual(bf.event_id[0], 1) self.assertEqual(bf.event_name, ['1.0', '2.0', '3.0', '4.0', '5.0', '3_gen0', '3_gen1', '3_gen2']) self.assertTrue(np.array_equal(bf.frequency, np.array([1, 1, 1, 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, (8, 78090)) self.assertEqual(bf.fraction.shape, (8, 78090))
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_bush_fire_one_pass(self): bf = BushFire() centr_res_factor = 1 bf.set_bush_fire(TEST_FIRMS, centr_res_factor, seed = 8) 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))
def test_pool_pass(self): """ Test reproducibility with pool """ from pathos.pools import ProcessPool as Pool pool = Pool() bf = BushFire(pool) bf.set_hist_events(TEST_FIRMS, 1 / 2) bf.set_proba_events(ens_size=2, seed=9) bf.check() self.assertEqual(bf.size, 9 * 3) orig = np.zeros(27, bool) orig[:9] = True self.assertTrue(np.allclose(bf.orig, orig)) self.assertTrue( np.allclose( bf.date[:9], np.array([ 736190, 736218, 736167, 736180, 736221, 736245, 736221, 736225, 736228 ]))) self.assertTrue(np.allclose(bf.date[9:11], np.array([736190, 736190]))) self.assertTrue(np.allclose(bf.date[11:13], np.array([736218, 736218]))) self.assertTrue(np.allclose(bf.date[-2:], np.array([736228, 736228]))) self.assertEqual(bf.event_name[:9], ['1', '2', '3', '4', '5', '6', '7', '8', '9']) self.assertEqual(bf.event_name[9:11], ['1_gen1', '1_gen2']) self.assertEqual(bf.event_name[-2:], ['9_gen1', '9_gen2']) self.assertAlmostEqual(bf.fraction.max(), 1.0) self.assertAlmostEqual(bf.fraction.min(), 0.0) self.assertAlmostEqual(bf.intensity[26, 9994], 301.5) self.assertAlmostEqual(bf.intensity[26, 9995], 326.7) self.assertAlmostEqual(bf.intensity[26, 10138], 326.7) self.assertAlmostEqual(bf.intensity[26, 10281], 326.7) self.assertEqual(bf.intensity[26, :].nonzero()[0].size, 4) self.assertEqual(bf.intensity.nonzero()[0].size, 5517) self.assertEqual(bf.intensity.shape, (27, 19872)) self.assertEqual(bf.fraction.shape, (27, 19872))
def test_hist_pass(self): """ Test set_hist_events """ bf = BushFire() bf.set_hist_events(TEST_FIRMS, centr_res_factor=1 / 2) bf.check() self.assertEqual(bf.tag.haz_type, 'BF') self.assertEqual(bf.units, 'K') self.assertTrue(np.allclose(bf.event_id, np.arange(1, 10))) self.assertTrue( np.allclose( bf.date, np.array([ 736190, 736218, 736167, 736180, 736221, 736245, 736221, 736225, 736228 ]))) self.assertTrue(np.allclose(bf.orig, np.ones(9, bool))) self.assertEqual(bf.event_name, ['1', '2', '3', '4', '5', '6', '7', '8', '9']) self.assertTrue(np.allclose(bf.frequency, np.ones(9))) self.assertEqual(bf.intensity.shape, (9, 19872)) self.assertEqual(bf.fraction.shape, (9, 19872)) self.assertEqual(bf.intensity[1, :].nonzero()[1][0], 13052) self.assertEqual(bf.intensity[3, :].nonzero()[1][0], 56) self.assertEqual(bf.intensity[5, :].nonzero()[1][0], 8975) self.assertEqual(bf.intensity[5, :].nonzero()[1][1], 8976) self.assertEqual(bf.intensity[5, :].nonzero()[1].size, 2) self.assertEqual(bf.intensity[6, :].nonzero()[1][0], 9555) self.assertEqual(bf.intensity[6, :].nonzero()[1].size, 1) self.assertEqual(bf.intensity[7, :].nonzero()[1][0], 8682) self.assertEqual(bf.intensity[7, :].nonzero()[1].size, 3) self.assertAlmostEqual(bf.intensity[0, 19003], 317.6) self.assertAlmostEqual(bf.intensity[2, 2008], 0.0) self.assertAlmostEqual(bf.intensity[2, 9409], 350.9) self.assertAlmostEqual(bf.intensity[7, 8683], 307.9) self.assertAlmostEqual(bf.fraction.max(), 1.0) self.assertAlmostEqual(bf.fraction.min(), 0.0)
def test_centr_synth_pass(self): """ Test probabilistic set_proba_events """ centroids = def_ori_centroids(-121.92908999999999, 35.66364, -120.96468, 36.59146, 1 / 2) bf = BushFire() bf.set_hist_events(TEST_FIRMS, centroids=centroids) bf.set_proba_events(ens_size=2, seed=8) bf.check() self.assertEqual(bf.size, 9 * 3) orig = np.zeros(27, bool) orig[:9] = True self.assertTrue(np.allclose(bf.orig, orig)) self.assertTrue( np.allclose( bf.date[:9], np.array([ 736190, 736218, 736167, 736180, 736221, 736245, 736221, 736225, 736228 ]))) self.assertTrue(np.allclose(bf.date[9:11], np.array([736190, 736190]))) self.assertTrue(np.allclose(bf.date[11:13], np.array([736218, 736218]))) self.assertTrue(np.allclose(bf.date[-2:], np.array([736228, 736228]))) self.assertEqual(bf.event_name[:9], ['1', '2', '3', '4', '5', '6', '7', '8', '9']) self.assertEqual(bf.event_name[9:11], ['1_gen1', '1_gen2']) self.assertEqual(bf.event_name[-2:], ['9_gen1', '9_gen2']) self.assertEqual(bf.intensity.shape, (27, 19454)) self.assertEqual(bf.fraction.shape, (27, 19454)) self.assertAlmostEqual(bf.intensity[26, 10128], 301.5) self.assertAlmostEqual(bf.intensity[26, 10269], 301.5) self.assertAlmostEqual(bf.intensity[26, 10271], 295.4) self.assertEqual(bf.intensity[26, :].nonzero()[0].size, 3) self.assertEqual(bf.intensity.nonzero()[0].size, 5445) self.assertAlmostEqual(bf.fraction.max(), 1.0) self.assertAlmostEqual(bf.fraction.min(), 0.0)
def test_centr_hist_pass(self): """ Test set_hist_events """ centroids = def_ori_centroids(-121.92908999999999, 35.66364, -120.96468, 36.59146, 1 / 2) bf = BushFire() bf.set_hist_events(TEST_FIRMS, centroids=centroids) bf.check() self.assertEqual(bf.tag.haz_type, 'BF') self.assertEqual(bf.units, 'K') self.assertTrue(np.allclose(bf.event_id, np.arange(1, 10))) self.assertTrue( np.allclose( bf.date, np.array([ 736190, 736218, 736167, 736180, 736221, 736245, 736221, 736225, 736228 ]))) self.assertTrue(np.allclose(bf.orig, np.ones(9, bool))) self.assertEqual(bf.event_name, ['1', '2', '3', '4', '5', '6', '7', '8', '9']) self.assertTrue(np.allclose(bf.frequency, np.ones(9))) self.assertEqual(bf.intensity.shape, (9, 19454)) self.assertEqual(bf.fraction.shape, (9, 19454)) self.assertEqual(bf.intensity[1, :].nonzero()[1][0], 6764) self.assertEqual(bf.intensity[3, :].nonzero()[1][0], 19367) self.assertEqual(bf.intensity[5, :].nonzero()[1][0], 10696) self.assertEqual(bf.intensity[5, :].nonzero()[1][1], 10697) self.assertEqual(bf.intensity[6, :].nonzero()[1][0], 10132) self.assertEqual(bf.intensity[7, :].nonzero()[1][0], 10834) self.assertAlmostEqual(bf.intensity[0, 3257], 299.1) self.assertAlmostEqual(bf.intensity[2, 13650], 367.0) self.assertAlmostEqual(bf.intensity[2, 17328], 357.9) self.assertAlmostEqual(bf.intensity[7, 10834], 336.7) self.assertAlmostEqual(bf.fraction.max(), 1.0) self.assertAlmostEqual(bf.fraction.min(), 0.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)