Skip to content

Commit

Permalink
Remove Tag attribute from ImpactFuncSet (#753)
Browse files Browse the repository at this point in the history
* impact_func_set.ImpactFuncSet: remove tag attribute
* climada.entity.entity_def: remove ImactFuncSet description arrgument
* climada.entity.test.test_entity: adapt to removed tag
* doc: remove tag from ImpactFuncSet tutorial
  • Loading branch information
emanuel-schmid authored Jul 13, 2023
1 parent dd70ca4 commit 4e01450
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 54 deletions.
4 changes: 2 additions & 2 deletions climada/entity/entity_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def from_mat(cls, file_name, description=''):
return cls(
exposures=Exposures.from_mat(file_name),
disc_rates=DiscRates.from_mat(file_name),
impact_func_set=ImpactFuncSet.from_mat(file_name, description),
impact_func_set=ImpactFuncSet.from_mat(file_name),
measure_set=MeasureSet.from_mat(file_name, description)
)

Expand Down Expand Up @@ -131,7 +131,7 @@ def from_excel(cls, file_name, description=''):
exp.tag = Tag(file_name=file_name, description=description)

dr = DiscRates.from_excel(file_name)
impf_set = ImpactFuncSet.from_excel(file_name, description)
impf_set = ImpactFuncSet.from_excel(file_name)
meas_set = MeasureSet.from_excel(file_name, description)

return cls(
Expand Down
21 changes: 5 additions & 16 deletions climada/entity/impact_funcs/impact_func_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import xlsxwriter

from climada.entity.impact_funcs.base import ImpactFunc
from climada.util.tag import Tag
import climada.util.plot as u_plot
import climada.util.hdf5_handler as u_hdf5

Expand Down Expand Up @@ -68,17 +67,14 @@ class ImpactFuncSet:
Attributes
----------
tag : climada.util.tag.Tag
information about the source data
_data : dict
contains ImpactFunc classes. It's not suppossed to be
directly accessed. Use the class methods instead.
"""

def __init__(
self,
impact_funcs: Optional[Iterable[ImpactFunc]] = None,
tag: Optional[Tag] = None
impact_funcs: Optional[Iterable[ImpactFunc]] = None
):
"""Initialization.
Expand All @@ -88,8 +84,6 @@ def __init__(
----------
impact_funcs : iterable of ImpactFunc, optional
An iterable (list, set, array, ...) of ImpactFunc.
tag : climada.util.tag.Tag, optional
The entity tag of this object.
Examples
--------
Expand All @@ -108,15 +102,12 @@ def __init__(
"""
# TODO: Automatically check this object if impact_funcs is not None.
self.clear()
if tag is not None:
self.tag = tag
if impact_funcs is not None:
for impf in impact_funcs:
self.append(impf)

def clear(self):
"""Reinitialize attributes."""
self.tag = Tag()
self._data = dict() # {hazard_type : {id:ImpactFunc}}

def append(self, func):
Expand Down Expand Up @@ -312,8 +303,6 @@ def extend(self, impact_funcs):
self.__dict__ = copy.deepcopy(impact_funcs.__dict__)
return

self.tag.append(impact_funcs.tag)

new_func = impact_funcs.get_func()
for _, vul_dict in new_func.items():
for _, vul in vul_dict.items():
Expand Down Expand Up @@ -361,7 +350,7 @@ def plot(self, haz_type=None, fun_id=None, axis=None, **kwargs):
return axis

@classmethod
def from_excel(cls, file_name, description='', var_names=None):
def from_excel(cls, file_name, var_names=None):
"""Read excel file following template and store variables.
Parameters
Expand All @@ -381,7 +370,7 @@ def from_excel(cls, file_name, description='', var_names=None):
var_names = DEF_VAR_EXCEL
dfr = pd.read_excel(file_name, var_names['sheet_name'])

imp_func_set = cls(tag=Tag(str(file_name), description))
imp_func_set = cls()
imp_func_set._fill_dfr(dfr, var_names)
return imp_func_set

Expand All @@ -392,7 +381,7 @@ def read_excel(self, *args, **kwargs):
self.__dict__ = ImpactFuncSet.from_excel(*args, **kwargs).__dict__

@classmethod
def from_mat(cls, file_name, description='', var_names=None):
def from_mat(cls, file_name, var_names=None):
"""Read MATLAB file generated with previous MATLAB CLIMADA version.
Parameters
Expand Down Expand Up @@ -470,7 +459,7 @@ def _get_hdf5_str(imp, idxs, file_name, var_name):
except KeyError as err:
raise KeyError("Not existing variable: %s" % str(err)) from err

return cls(impact_funcs, Tag(str(file_name), description))
return cls(impact_funcs)

def read_mat(self, *args, **kwargs):
"""This function is deprecated, use ImpactFuncSet.from_mat instead."""
Expand Down
26 changes: 4 additions & 22 deletions climada/entity/impact_funcs/test/test_imp_fun_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import numpy as np

from climada import CONFIG
from climada.entity.impact_funcs.impact_func_set import ImpactFuncSet, ImpactFunc, Tag
from climada.entity.impact_funcs.impact_func_set import ImpactFuncSet, ImpactFunc
from climada.util.constants import ENT_TEMPLATE_XLS, ENT_DEMO_TODAY

ENT_TEST_MAT = CONFIG.exposures.test_data.dir().joinpath('demo_today.mat')
Expand All @@ -33,7 +33,6 @@ def test_attributes_all(self):
"""All attributes are defined"""
imp_fun = ImpactFuncSet()
vulner_1 = ImpactFunc("TC", "2")
self.assertTrue(hasattr(imp_fun, 'tag'))
self.assertTrue(hasattr(imp_fun, '_data'))
self.assertTrue(hasattr(vulner_1, 'haz_type'))
self.assertTrue(hasattr(vulner_1, 'name'))
Expand Down Expand Up @@ -301,16 +300,13 @@ def test_extend_to_empty_same(self):
"""Extend ImpactFuncSet to empty one."""
imp_fun = ImpactFuncSet()
imp_fun_add = ImpactFuncSet(
(ImpactFunc("TC", 1), ImpactFunc("TC", 3), ImpactFunc("FL", 3)),
Tag('file1.txt'))
(ImpactFunc("TC", 1), ImpactFunc("TC", 3), ImpactFunc("FL", 3)))
imp_fun.extend(imp_fun_add)
imp_fun.check()

self.assertEqual(imp_fun.size(), 3)
self.assertEqual(imp_fun.size('TC'), 2)
self.assertEqual(imp_fun.size('FL'), 1)
self.assertEqual(imp_fun.tag.file_name, imp_fun_add.tag.file_name)
self.assertEqual(imp_fun.tag.description, imp_fun_add.tag.description)

def test_extend_equal_same(self):
"""Extend the same ImpactFuncSet. The inital ImpactFuncSet is obtained."""
Expand Down Expand Up @@ -353,8 +349,7 @@ class TestReaderMat(unittest.TestCase):
def test_demo_file_pass(self):
"""Read demo excel file"""
# Read demo mat file
description = 'One single file.'
imp_funcs = ImpactFuncSet.from_mat(ENT_TEST_MAT, description)
imp_funcs = ImpactFuncSet.from_mat(ENT_TEST_MAT)

# Check results
n_funcs = 2
Expand Down Expand Up @@ -419,9 +414,6 @@ def test_demo_file_pass(self):
self.assertEqual(imp_funcs._data[hazard][second_id].paa[0], 0)
self.assertEqual(imp_funcs._data[hazard][second_id].paa[8], 1)

# general information
self.assertEqual(imp_funcs.tag.file_name, [str(ENT_TEST_MAT)])
self.assertEqual(imp_funcs.tag.description, [description])

class TestReaderExcel(unittest.TestCase):
"""Test reader functionality of the imp_funcsFuncsExcel class"""
Expand All @@ -430,8 +422,7 @@ def test_demo_file_pass(self):
"""Read demo excel file"""
# Read demo excel file

description = 'One single file.'
imp_funcs = ImpactFuncSet.from_excel(ENT_DEMO_TODAY, description)
imp_funcs = ImpactFuncSet.from_excel(ENT_DEMO_TODAY)

# Check results
n_funcs = 2
Expand Down Expand Up @@ -496,10 +487,6 @@ def test_demo_file_pass(self):
self.assertEqual(imp_funcs._data[hazard][second_id].paa[0], 0)
self.assertEqual(imp_funcs._data[hazard][second_id].paa[8], 1)

# general information
self.assertEqual(imp_funcs.tag.file_name, [str(ENT_DEMO_TODAY)])
self.assertEqual(imp_funcs.tag.description, [description])

def test_template_file_pass(self):
"""Read template excel file"""
imp_funcs = ImpactFuncSet.from_excel(ENT_TEMPLATE_XLS)
Expand All @@ -516,8 +503,6 @@ def test_write_read_pass(self):
"""Write + read excel file"""

imp_funcs = ImpactFuncSet()
imp_funcs.tag = Tag(file_name='No file name',
description='test writer')

idx = 1
name = 'code 1'
Expand Down Expand Up @@ -562,9 +547,6 @@ def test_write_read_pass(self):

imp_res = ImpactFuncSet.from_excel(file_name)

self.assertEqual(imp_res.tag.file_name, [str(file_name)])
self.assertEqual(imp_res.tag.description, [])

# first function
for fun_haz, fun_dict in imp_res.get_func().items():
for fun_id, fun in fun_dict.items():
Expand Down
4 changes: 1 addition & 3 deletions climada/entity/test/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,13 @@ def test_from_mat(self):
self.assertEqual(entity_mat.exposures.tag.file_name, [str(ENT_TEST_MAT)])
self.assertIsInstance(entity_mat.disc_rates, DiscRates)
self.assertEqual(entity_mat.measures.tag.file_name, [str(ENT_TEST_MAT)])
self.assertEqual(entity_mat.impact_funcs.tag.file_name, [str(ENT_TEST_MAT)])
self.assertTrue(isinstance(entity_mat.impact_funcs, ImpactFuncSet))

def test_from_excel(self):
"""Read entity from an xls file following the template."""
entity_xls = Entity.from_excel(ENT_TEMPLATE_XLS)
self.assertEqual(entity_xls.exposures.tag.file_name, [str(ENT_TEMPLATE_XLS)])
self.assertEqual(entity_xls.measures.tag.file_name, [str(ENT_TEMPLATE_XLS)])
self.assertEqual(entity_xls.impact_funcs.tag.file_name,
[str(ENT_TEMPLATE_XLS)])

class TestCheck(unittest.TestCase):
"""Test entity checker."""
Expand Down
11 changes: 0 additions & 11 deletions doc/tutorial/climada_entity_ImpactFuncSet.ipynb

Large diffs are not rendered by default.

0 comments on commit 4e01450

Please sign in to comment.