From f94f0025e471b90ee2574ca8f557db1c8fd0a462 Mon Sep 17 00:00:00 2001 From: "Pui Man (Mannie) Kam" Date: Wed, 2 Nov 2022 16:23:59 +0100 Subject: [PATCH] Feature/init hazard tag (#570) * update __init__ in tc_tracks.py * update tc_tracks.py * update tc_tracks.py * remove trailing white space * update __init__ for hazard.tag * und changes on tc_tracks * engine.impact: proper hazard.tag initialization * cosmetics * set hazard_type de facto mandatory (de jure still optonal) Co-authored-by: emanuel-schmid --- climada/engine/impact.py | 14 +++++++------- climada/engine/unsequa/calc_cost_benefit.py | 4 ++-- climada/hazard/base.py | 3 +-- climada/hazard/tag.py | 7 +++++-- climada/hazard/test/test_tag.py | 6 ++---- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/climada/engine/impact.py b/climada/engine/impact.py index 36fcdeeca..d08a9447d 100755 --- a/climada/engine/impact.py +++ b/climada/engine/impact.py @@ -57,7 +57,7 @@ class Impact(): ---------- tag : dict dictionary of tags of exposures, impact functions set and - hazard: {'exp': Tag(), 'impf_set': Tag(), 'haz': TagHazard()} + hazard: {'exp': Tag(), 'impf_set': Tag(), 'haz': TagHaz()} event_id : np.array id (>0) of each hazard event event_name : list @@ -137,7 +137,7 @@ def __init__(self, matrix num_events x num_exp with impacts. tag : dict, optional dictionary of tags of exposures, impact functions set and - hazard: {'exp': Tag(), 'impf_set': Tag(), 'haz': TagHazard()} + hazard: {'exp': Tag(), 'impf_set': Tag(), 'haz': TagHaz()} """ self.tag = tag or {} @@ -955,10 +955,10 @@ def from_excel(cls, file_name): LOGGER.info('Reading %s', file_name) dfr = pd.read_excel(file_name) imp =cls() - imp.tag['haz'] = TagHaz() - imp.tag['haz'].haz_type = dfr['tag_hazard'][0] - imp.tag['haz'].file_name = dfr['tag_hazard'][1] - imp.tag['haz'].description = dfr['tag_hazard'][2] + imp.tag['haz'] = TagHaz( + haz_type = dfr['tag_hazard'][0], + file_name = dfr['tag_hazard'][1], + description = dfr['tag_hazard'][2]) imp.tag['exp'] = Tag() imp.tag['exp'].file_name = dfr['tag_exposure'][0] imp.tag['exp'].description = dfr['tag_exposure'][1] @@ -1397,7 +1397,7 @@ class ImpactFreqCurve(): tag : dict = field(default_factory=dict) """dictionary of tags of exposures, impact functions set and - hazard: {'exp': Tag(), 'impf_set': Tag(), 'haz': TagHazard()}""" + hazard: {'exp': Tag(), 'impf_set': Tag(), 'haz': TagHaz()}""" return_per : np.array = np.array([]) """return period""" diff --git a/climada/engine/unsequa/calc_cost_benefit.py b/climada/engine/unsequa/calc_cost_benefit.py index ef1985c1e..e6b895670 100644 --- a/climada/engine/unsequa/calc_cost_benefit.py +++ b/climada/engine/unsequa/calc_cost_benefit.py @@ -23,11 +23,11 @@ import logging import time - from functools import partial -import pandas as pd from typing import Optional, Union +import pandas as pd + from climada.engine.cost_benefit import CostBenefit from climada.engine.unsequa import Calc, InputVar, UncCostBenefitOutput from climada.util import log_level diff --git a/climada/hazard/base.py b/climada/hazard/base.py index 526f381be..96aea6abe 100644 --- a/climada/hazard/base.py +++ b/climada/hazard/base.py @@ -189,8 +189,7 @@ def __init__(self, haz_type='', pool=None): >>> haz = Hazard.from_mat(HAZ_DEMO_MAT, 'demo') """ - self.tag = TagHazard() - self.tag.haz_type = haz_type + self.tag = TagHazard(haz_type=haz_type) self.units = '' self.centroids = Centroids() # following values are defined for each event diff --git a/climada/hazard/tag.py b/climada/hazard/tag.py index 32f086e9e..15abb8758 100644 --- a/climada/hazard/tag.py +++ b/climada/hazard/tag.py @@ -39,12 +39,15 @@ class Tag(object): description(s) of the data """ - def __init__(self, haz_type='', file_name='', description=''): + def __init__(self, + haz_type: str = '', + file_name: str = '', + description: str = ''): """Initialize values. Parameters ---------- - haz_type : str, optional + haz_type : str acronym of the hazard type (e.g. 'TC'). file_name : str or list(str), optional file name(s) to read diff --git a/climada/hazard/test/test_tag.py b/climada/hazard/test/test_tag.py index a036ae2e0..fcac66a2e 100644 --- a/climada/hazard/test/test_tag.py +++ b/climada/hazard/test/test_tag.py @@ -61,15 +61,13 @@ def test_equal_same(self): def test_append_empty(self): """Appends an other tag correctly.""" tag1 = TagHazard('TC', 'file_name1.mat', 'dummy file 1') - tag2 = TagHazard() - tag2.haz_type = 'TC' + tag2 = TagHazard(haz_type='TC') tag1.append(tag2) self.assertEqual('file_name1.mat', tag1.file_name) self.assertEqual('dummy file 1', tag1.description) - tag1 = TagHazard() - tag1.haz_type = 'TC' + tag1 = TagHazard('TC') tag2 = TagHazard('TC', 'file_name1.mat', 'dummy file 1') tag1.append(tag2)