def test_on_map(self): gmap = RasterMap(refloat_halflife=6, bitmap_array=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection()) # hours assert gmap.on_map((0., 0., 0.)) assert gmap.on_map((55.0, 0., 0.)) is False
def test_land_cross_array(self): """ test a few LEs """ gmap = RasterMap(refloat_halflife=6, raster=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection()) # one left to right # one right to left # one diagonal upper left to lower right # one diagonal upper right to lower left spill = sample_sc_release(4) spill['positions'] = np.array( ((5.0, 5.0, 0.), (15.0, 5.0, 0.), (0., 0., 0.), (19.0, 0., 0.)), dtype=np.float64) spill['next_positions'] = np.array( ((15.0, 5.0, 0.), (5.0, 5.0, 0.), (10.0, 5.0, 0.), (0., 9.0, 0.)), dtype=np.float64) gmap.beach_elements(spill) assert np.array_equal(spill['next_positions'], ((10.0, 5.0, 0.), (10.0, 5.0, 0.), (10.0, 5.0, 0.), (10.0, 4.0, 0.))) assert np.array_equal(spill['last_water_positions'], ((9.0, 5.0, 0.), (11.0, 5.0, 0.), (9.0, 4.0, 0.), (11.0, 4.0, 0.))) assert np.alltrue(spill['status_codes'] == oil_status.on_land)
def test_on_land(self): gmap = RasterMap(refloat_halflife=6, bitmap_array=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection()) # hours assert gmap.on_land((10, 3, 0)) == 1 assert gmap.on_land((9, 3, 0)) == 0 assert gmap.on_land((11, 3, 0)) == 0
def test_land_cross(self): """ try a single LE that should be crossing land """ gmap = RasterMap(refloat_halflife=6, bitmap_array=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection()) # hours spill = sample_sc_release(1) spill['positions'] = np.array(((5.0, 5.0, 0.), ), dtype=np.float64) spill['next_positions'] = np.array(((15.0, 5.0, 0.), ), dtype=np.float64) spill['status_codes'] = np.array((oil_status.in_water, ), dtype=status_code_type) gmap.beach_elements(spill) assert np.array_equal(spill['next_positions'][0], (10.0, 5.0, 0.)) assert np.array_equal(spill['last_water_positions'][0], (9.0, 5.0, 0.)) assert spill['status_codes'][0] == oil_status.on_land
def test_starts_on_land(self): """ try a single LE that starts on land it last water position should be the same point. """ gmap = RasterMap(refloat_halflife=6, bitmap_array=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection()) # hours spill = sample_sc_release(1) spill['positions'] = np.array(((10.0, 5.0, 0.), ), dtype=np.float64) spill['last_water_positions'] = np.array(((0.0, 0.0, 0.), ), dtype=np.float64) spill['next_positions'] = np.array(((15.0, 5.0, 0.), ), dtype=np.float64) spill['status_codes'] = np.array((oil_status.in_water, ), dtype=status_code_type) gmap.beach_elements(spill) ## next position gets set to land location assert np.array_equal(spill['next_positions'][0], (10.0, 5.0, 0.)) assert np.array_equal(spill['last_water_positions'][0], (10.0, 5.0, 0.)) assert spill['status_codes'][0] == oil_status.on_land
def test_some_cross_array(self): """ test a few LEs """ gmap = RasterMap(refloat_halflife=6, bitmap_array=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection()) # hours # one left to right # one right to left # diagonal that doesn't hit # diagonal that does hit spill = sample_sc_release(4) spill['positions'] = np.array(((5.0, 5.0, 0.), (15.0, 5.0, 0.), (0., 0., 0.), (19.0, 0., 0.)), dtype=np.float64) spill['next_positions'] = np.array(((9.0, 5.0, 0.), (11.0, 5.0, 0.), (9.0, 9.0, 0.), (0., 9.0, 0.)), dtype=np.float64) gmap.beach_elements(spill) assert np.array_equal(spill['next_positions'], ((9.0, 5.0, 0.), (11.0, 5.0, 0.), (9.0, 9.0, 0.), (10.0, 4.0, 0.))) # just the beached ones assert np.array_equal((spill['last_water_positions'])[3:], ((11.0, 4.0, 0.), )) assert np.array_equal((spill['status_codes'])[3:], (oil_status.on_land, ))
def test_land_cross_array(self): """ test a few LEs """ gmap = RasterMap(refloat_halflife=6, bitmap_array=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection()) # hours # one left to right # one right to left # one diagonal upper left to lower right # one diagonal upper right to lower left spill = sample_sc_release(4) spill['positions'] = np.array(((5.0, 5.0, 0.), (15.0, 5.0, 0.), (0., 0., 0.), (19.0, 0., 0.)), dtype=np.float64) spill['next_positions'] = np.array(((15.0, 5.0, 0.), (5.0, 5.0, 0.), (10.0, 5.0, 0.), (0., 9.0, 0.)), dtype=np.float64) gmap.beach_elements(spill) assert np.array_equal(spill['next_positions'], ((10.0, 5.0, 0.), (10.0, 5.0, 0.), (10.0, 5.0, 0.), (10.0, 4.0, 0.))) assert np.array_equal(spill['last_water_positions'], ((9.0, 5.0, 0.), (11.0, 5.0, 0.), (9.0, 4.0, 0.), (11.0, 4.0, 0.))) assert np.alltrue(spill['status_codes'] == oil_status.on_land)
def test_starts_on_land(self): """ try a single LE that starts on land it last water position should be the same point. """ gmap = RasterMap(refloat_halflife=6, raster=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection()) spill = sample_sc_release(1) spill['positions'] = np.array(((10.0, 5.0, 0.), ), dtype=np.float64) spill['last_water_positions'] = np.array(((0.0, 0.0, 0.), ), dtype=np.float64) spill['next_positions'] = np.array(((15.0, 5.0, 0.), ), dtype=np.float64) spill['status_codes'] = np.array((oil_status.in_water, ), dtype=status_code_type) gmap.beach_elements(spill) # next position gets set to land location assert np.array_equal(spill['next_positions'][0], (10.0, 5.0, 0.)) assert np.array_equal(spill['last_water_positions'][0], (10.0, 5.0, 0.)) assert spill['status_codes'][0] == oil_status.on_land
def test_spillable_area2(self): # a test with a polygon spillable area poly = ((5, 2), (15, 2), (15, 10), (10, 10), (10, 5)) gmap = RasterMap(refloat_halflife=6, bitmap_array=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection(), spillable_area=[poly]) # hours # cases that are spillable assert gmap.allowable_spill_position((11.0, 3.0, 0.)) assert gmap.allowable_spill_position((14.0, 9.0, 0.)) # in polygon, but on land: assert not gmap.allowable_spill_position((11.0, 6.0, 0.)) # outside polygon, on land: assert not gmap.allowable_spill_position((8.0, 6.0, 0.)) # outside polygon, off land: assert not gmap.allowable_spill_position((3.0, 3.0, 0.))
def test_on_map(self): gmap = RasterMap(refloat_halflife=6, bitmap_array=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection()) # hours # making sure the gmap is set up right assert gmap.on_map((100.0, 1., 0.)) is False assert gmap.on_map((0., 1., 0.))
def test_on_land(self): gmap = RasterMap(refloat_halflife=6, bitmap_array=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection()) # hours print 'testing a land point:', (10, 6, 0.) print gmap.on_land((10, 6, 0.)) assert gmap.on_land((10, 6, 0.)) # right in the middle print 'testing a water point:' assert not gmap.on_land((19.0, 11.0, 0.))
def test_on_land(self): gmap = RasterMap(refloat_halflife=6, bitmap_array=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection()) # right in the middle print 'testing a land point:', (10, 6, 0.), gmap.on_land((10, 6, 0.)) assert gmap.on_land((10, 6, 0.)) print 'testing a water point:' assert not gmap.on_land((19.0, 11.0, 0.))
def test_save_as_image(self, dump): """ only tests that it doesn't crash -- you need to look at the image to see if it's right """ rmap = RasterMap(refloat_halflife=6, bitmap_array=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30),(50, -30)), projection=NoProjection()) rmap.save_as_image(os.path.join(dump, 'raster_map_image.png')) assert True
def test_some_off_map(self): """ Test LEs that go off the map should get off_map flag - no longer setting to_be_removed flag. map simply sets the off_maps flag. """ gmap = RasterMap(refloat_halflife=6, bitmap_array=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection()) # hours spill = sample_sc_release(8) spill['positions'] = np.array(( (45.0, 25.0, 0.), (45.0, 25.0, 0.), (45.0, -25.0, 0.), (45.0, -25.0, 0.), (-45.0, -25.0, 0.), (-45.0, -25.0, 0.), (-45.0, 25.0, 0.), (-45.0, 25.0, 0.), ), dtype=np.float64) spill['next_positions'] = np.array(( # off # still on # off # still on # off # still on # off # still on (55.0, 25.0, 0.), (49.0, 25.0, 0.), (45.0, -35.0, 0.), (45.0, -29.0, 0.), (-55.0, -25.0, 0.), (-49.0, -25.0, 0.), (-45.0, 35.0, 0.), (-45.0, 29.0, 0.), ), dtype=np.float64) gmap.beach_elements(spill) off = np.ones(4,) * oil_status.off_maps # off = np.ones(4) * oil_status.to_be_removed assert np.array_equal(spill['status_codes'][0::2], off) on = np.ones(4) * oil_status.in_water assert np.array_equal(spill['status_codes'][1::2], on)
def test_spillable_area(self): # anywhere not on land is spillable... # in this case gmap = RasterMap(refloat_halflife=6, bitmap_array=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection()) # right in the middle of land print 'testing a land point:' assert not gmap.allowable_spill_position((10, 6, 0.)) print 'testing a water point:' assert gmap.allowable_spill_position((19.0, 11.0, 0.))
def test_outside_raster(self): """ test LEs starting form outside the raster bounds """ gmap = RasterMap(refloat_halflife=6, bitmap_array=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection()) # hours # one left to right # one right to left # diagonal that doesn't hit # diagonal that does hit # spill = gnome.spill.Spill(num_LEs=4) spill = sample_sc_release(4) spill['positions'] = np.array(((30.0, 5.0, 0.), (-5.0, 5.0, 0.), (5.0, -5.0, 0.), (-5.0, -5.0, 0.)), dtype=np.float64) # outside right # outside left # outside top # outside upper left spill['next_positions'] = np.array(((15.0, 5.0, 0.), (5.0, 5.0, 0.), (5.0, 15.0, 0.), (25.0, 15.0, 0.)), dtype=np.float64) gmap.beach_elements(spill) assert np.array_equal(spill['next_positions'], ((15.0, 5.0, 0.), (5.0, 5.0, 0.), (5.0, 15.0, 0.), (10.0, 5.0, 0.))) # just the beached ones assert np.array_equal((spill['last_water_positions'])[3:], ((9.0, 4.0, 0.), )) assert np.array_equal((spill['status_codes'])[3:], (oil_status.on_land, ))
class TestRefloat: """ only tests the refloat_elements interface and functionality for borderline cases like all elements in water, refloat_halflife = 0 A raster map with only water is used, but since there isn't a land check, this is irrelevant """ # make time_step = refloat_halflife so 50% probability of refloat time_step = 3600. # land/water irrelevant for this test map = RasterMap(refloat_halflife=time_step / 3600., raster=np.zeros((20, 12), dtype=np.uint8), projection=NoProjection(), map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30))) num_les = 1000 spill = sample_sc_release(num_les) orig_pos = np.random.uniform(0, num_les, spill['positions'].shape) last_water = (1., 2., 0.) spill['positions'][:] = orig_pos spill['last_water_positions'] += last_water def reset(self): self.spill['positions'][:] = self.orig_pos self.spill['last_water_positions'][:] = self.last_water self.map.refloat_halflife = self.time_step / 3600. def test_all_elementsinwater(self): """ all elements in water so do nothing """ self.reset() # reset _state self.spill['status_codes'][:] = oil_status.in_water self.map.refloat_elements(self.spill, self.time_step) assert np.all(self.spill['positions'] == self.orig_pos) assert np.all(self.spill['status_codes'] == oil_status.in_water) def test_refloat_halflife_0(self): """ refloat_halflife is 0 so refloat all elements on land """ self.reset() self.map.refloat_halflife = 0 self.spill['status_codes'][5:] = oil_status.on_land self.map.refloat_elements(self.spill, self.time_step) assert np.all((self.spill['positions'])[:5] == self.orig_pos[:5]) assert np.all((self.spill['positions'])[5:] == self.last_water) def test_refloat_halflife_negative(self): """ refloat_halflife is test_refloat_halflife_negative: this should mean totally sticky --no refloat """ self.reset() self.map.refloat_halflife = -1 self.spill['status_codes'][5:] = oil_status.on_land orig_status_codes = self.spill['status_codes'].copy() self.map.refloat_elements(self.spill, self.time_step) assert np.all((self.spill['positions']) == self.orig_pos) assert np.all(self.spill['status_codes'] == orig_status_codes) def test_refloat_some_onland(self): """ refloat elements on land based on probability """ self.reset() self.spill['status_codes'][:] = oil_status.in_water self.map.refloat_halflife = 3 * self.time_step / 3600. # say 500 out of 1000 are on_land, and we expect about 50% of these # to refloat # initial 25% LEs on_land, last 25% of LEs on_land init_ix = int(round(.25 * self.num_les)) last_ix = self.num_les - (int(round(.5 * self.num_les)) - init_ix) ix = range(init_ix) # choose first 25% of indices ix.extend(range(last_ix, self.num_les, 1)) # last 25% of indices ix = np.asarray(ix) self.spill['status_codes'][ix] = oil_status.on_land self.map.refloat_elements(self.spill, self.time_step) expected = (round( 1. - .5**(self.time_step / self.map.refloat_halflife * 3600.), 2) * 100) actual = (np.count_nonzero( self.spill['status_codes'][ix] == oil_status.in_water) / (self.num_les / 2) * 100) print('Expect {0}% refloat, actual refloated: {1}%'.format( expected, actual)) # ensure some of the elements that were on land are back on water assert np.count_nonzero( self.spill['status_codes'][ix] == oil_status.in_water) > 0 refloat_ix = ix[np.where( self.spill['status_codes'][ix] == oil_status.in_water)[0]] assert np.all(self.spill['positions'][refloat_ix] == self.last_water) # refloated elements assert np.all(self.spill['status_codes'][refloat_ix] == oil_status.in_water) # status is back in water # ensure elements that were in_water are not changed # these are original values that are not refloated mask = np.array([i not in refloat_ix for i in range(self.num_les)], dtype=bool) assert np.all( self.spill['positions'][mask, :] == self.orig_pos[mask, :])
def test__off_raster(self): """ test the _on_raster method """ # overkill for just the raster.. rmap = RasterMap(refloat_halflife=6, raster=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection()) # the corners assert not rmap._off_raster((0, 0)) assert not rmap._off_raster((19, 0)) assert not rmap._off_raster((19, 11)) assert not rmap._off_raster((0, 11)) # in the middle somewhere assert not rmap._off_raster((10, 6)) # just off the edges assert rmap._off_raster((-1, 0)) assert rmap._off_raster((19, -1)) assert rmap._off_raster((20, 11)) assert rmap._off_raster((0, 12)) # way off -- just for the heck of it. assert rmap._off_raster((-1000, -2000)) assert rmap._off_raster((1000, 2000))
def test__off_bitmap(self): """ test the _on_bitmap method """ # overkill for just the bitmap.. rmap = RasterMap(refloat_halflife=6, bitmap_array=self.raster, map_bounds=((-50, -30), (-50, 30), (50, 30), (50, -30)), projection=NoProjection()) # the corners assert not rmap._off_bitmap((0, 0)) assert not rmap._off_bitmap((19, 0)) assert not rmap._off_bitmap((19, 11)) assert not rmap._off_bitmap((0, 11)) # in the middle somewhere assert not rmap._off_bitmap((10, 6)) # just off the edges assert rmap._off_bitmap((-1, 0)) assert rmap._off_bitmap((19, -1)) assert rmap._off_bitmap((20, 11)) assert rmap._off_bitmap((0, 12)) # way off -- just for the heck of it. assert rmap._off_bitmap((-1000, -2000)) assert rmap._off_bitmap((1000, 2000))