Skip to content

Commit e635b1b

Browse files
committed
new function to remove exposures
1 parent 68f7984 commit e635b1b

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

climada/entity/exposures/base.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,37 @@ def read(self, files, descriptions='', var_names=None):
173173
for file, desc, var in zip(all_files, desc_list, var_list):
174174
self.append(Exposures._read_one(file, desc, var))
175175

176+
def remove(self, exp_id):
177+
"""Remove one exposure with given id.
178+
179+
Parameters:
180+
exp_id (list(int) or int): exposure ids.
181+
"""
182+
if not isinstance(exp_id, list):
183+
exp_id = [exp_id]
184+
try:
185+
pos_del = []
186+
for one_id in exp_id:
187+
pos_del.append(np.argwhere(self.id == one_id)[0][0])
188+
except IndexError:
189+
LOGGER.info('No exposure with id %s.', exp_id)
190+
return
191+
self.coord = np.delete(self.coord, pos_del, axis=0)
192+
self.value = np.delete(self.value, pos_del)
193+
self.impact_id = np.delete(self.impact_id, pos_del)
194+
self.id = np.delete(self.id, pos_del)
195+
if self.deductible.size:
196+
self.deductible = np.delete(self.deductible, pos_del)
197+
if self.cover.size:
198+
self.cover = np.delete(self.cover, pos_del)
199+
if self.category_id.size:
200+
self.category_id = np.delete(self.category_id, pos_del)
201+
if self.region_id.size:
202+
self.region_id = np.delete(self.region_id, pos_del)
203+
old_assigned = self.assigned.copy()
204+
for key, val in old_assigned.items():
205+
self.assigned[key] = np.delete(val, pos_del)
206+
176207
def append(self, exposures):
177208
"""Check and append variables of input Exposures to current Exposures.
178209
@@ -276,6 +307,12 @@ def coord(self, value):
276307
else:
277308
self._coord = value
278309

310+
@property
311+
def size(self):
312+
""" Get longitude from coord array """
313+
self.check()
314+
return self.value.size
315+
279316
@staticmethod
280317
def _read_one(file_name, description='', var_names=None):
281318
"""Read one file and fill attributes.

climada/entity/exposures/test/test_base.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,36 @@ def test_read_incompatible_fail(self):
220220
self.assertIn('Append not possible. Different reference years.', \
221221
cm.output[0])
222222

223+
class TestRemove(unittest.TestCase):
224+
"""Check read function with several files"""
225+
226+
def test_read_one_pass(self):
227+
"""Remove an exposure of existing id."""
228+
expo = good_exposures()
229+
expo.remove(1)
230+
expo.check()
231+
self.assertEqual(expo.size, 2)
232+
233+
def test_read_two_pass(self):
234+
"""Remove an exposure of existing id."""
235+
expo = good_exposures()
236+
expo.remove([1,3])
237+
expo.check()
238+
self.assertEqual(expo.size, 1)
239+
240+
def test_read_three_pass(self):
241+
"""Remove an exposure of existing id."""
242+
expo = good_exposures()
243+
expo.remove([1,3, 2])
244+
expo.check()
245+
self.assertEqual(expo.size, 0)
246+
247+
def test_read_wrong_one_pass(self):
248+
"""Remove exposure that does not exist."""
249+
expo = good_exposures()
250+
expo.remove(5)
251+
self.assertEqual(expo.size, 3)
252+
223253
class TestChecker(unittest.TestCase):
224254
"""Test loading funcions from the Exposures class"""
225255
def test_check_wrongValue_fail(self):
@@ -323,4 +353,5 @@ def test_check_wrongId_fail(self):
323353
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestAppend))
324354
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestReadParallel))
325355
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestConstructor))
356+
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestRemove))
326357
unittest.TextTestRunner(verbosity=2).run(TESTS)

0 commit comments

Comments
 (0)