def test_assign_pass(self): """Check that attribute `assigned` is correctly set.""" np_rand = np.random.RandomState(123456789) haz = Hazard.from_raster([HAZ_DEMO_FL], haz_type='FL', window=Window(10, 20, 50, 60)) haz.raster_to_vector() ncentroids = haz.centroids.size exp = Exposures(crs=haz.centroids.crs) # some are matching exactly, some are geographically close exp.gdf['longitude'] = np.concatenate([ haz.centroids.lon, haz.centroids.lon + 0.001 * (-0.5 + np_rand.rand(ncentroids)) ]) exp.gdf['latitude'] = np.concatenate([ haz.centroids.lat, haz.centroids.lat + 0.001 * (-0.5 + np_rand.rand(ncentroids)) ]) expected_result = np.concatenate( [np.arange(ncentroids), np.arange(ncentroids)]) # make sure that it works for both float32 and float64 for test_dtype in [np.float64, np.float32]: haz.centroids.lat = haz.centroids.lat.astype(test_dtype) haz.centroids.lon = haz.centroids.lon.astype(test_dtype) exp.assign_centroids(haz) self.assertEqual(exp.gdf.shape[0], len(exp.gdf[INDICATOR_CENTR + 'FL'])) np.testing.assert_array_equal( exp.gdf[INDICATOR_CENTR + 'FL'].values, expected_result)
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 = Centroids.from_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'), intensity=False) haz_read = Hazard.from_raster( [DATA_DIR.joinpath('test_write_hazard.tif')], files_fraction=[DATA_DIR.joinpath('test_write_hazard.tif')], haz_type='FL') 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_assign_raster_same_pass(self): """Test assign_centroids with raster hazard""" exp = Exposures.from_raster(HAZ_DEMO_FL, window=Window(10, 20, 50, 60)) exp.check() haz = Hazard.from_raster([HAZ_DEMO_FL], haz_type='FL', window=Window(10, 20, 50, 60)) exp.assign_centroids(haz) np.testing.assert_array_equal(exp.gdf[INDICATOR_CENTR + 'FL'].values, np.arange(haz.centroids.size, dtype=int))
def test_read_write_raster_pass(self): """Test write_raster: Hazard from raster data""" haz_fl = Hazard.from_raster([HAZ_DEMO_FL]) haz_fl.tag.haz_type = 'FL' haz_fl.check() self.assertEqual(haz_fl.intensity.shape, (1, 1032226)) self.assertEqual(haz_fl.intensity.min(), -9999.0) self.assertAlmostEqual(haz_fl.intensity.max(), 4.662774085998535) haz_fl.write_raster(DATA_DIR.joinpath('test_write_hazard.tif')) haz_read = Hazard.from_raster( [DATA_DIR.joinpath('test_write_hazard.tif')]) haz_fl.tag.haz_type = 'FL' self.assertTrue( np.allclose(haz_fl.intensity.toarray(), haz_read.intensity.toarray())) self.assertEqual( np.unique(np.array(haz_fl.fraction.toarray())).size, 2)
def test_assign_large_hazard_subset_pass(self): """Test assign_centroids with raster hazard""" exp = Exposures.from_raster(HAZ_DEMO_FL, window=Window(10, 20, 50, 60)) exp.gdf.latitude[[0, 1]] = exp.gdf.latitude[[1, 0]] exp.gdf.longitude[[0, 1]] = exp.gdf.longitude[[1, 0]] exp.check() haz = Hazard.from_raster([HAZ_DEMO_FL], haz_type='FL') haz.raster_to_vector() exp.assign_centroids(haz) assigned_centroids = haz.centroids.select(sel_cen=exp.gdf[INDICATOR_CENTR + 'FL'].values) np.testing.assert_array_equal(assigned_centroids.lat, exp.gdf.latitude) np.testing.assert_array_equal(assigned_centroids.lon, exp.gdf.longitude)
def test_read_raster_pool_pass(self): """Test from_raster constructor with pool""" from pathos.pools import ProcessPool as Pool pool = Pool() haz_fl = Hazard.from_raster([HAZ_DEMO_FL], haz_type='FL', pool=pool) haz_fl.check() self.assertEqual(haz_fl.intensity.shape, (1, 1032226)) self.assertEqual(haz_fl.intensity.min(), -9999.0) self.assertAlmostEqual(haz_fl.intensity.max(), 4.662774085998535) pool.close() pool.join()