Skip to content

Commit

Permalink
Feature/init hazard tag (#570)
Browse files Browse the repository at this point in the history
* 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 <schmide@ethz.ch>
  • Loading branch information
2 people authored and Simona Meiler committed Nov 22, 2022
1 parent 2f7ecad commit f94f002
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
14 changes: 7 additions & 7 deletions climada/engine/impact.py
Expand Up @@ -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
Expand Down Expand Up @@ -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 {}
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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"""
Expand Down
4 changes: 2 additions & 2 deletions climada/engine/unsequa/calc_cost_benefit.py
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions climada/hazard/base.py
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions climada/hazard/tag.py
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions climada/hazard/test/test_tag.py
Expand Up @@ -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)
Expand Down

0 comments on commit f94f002

Please sign in to comment.