Skip to content

Commit

Permalink
Feature/init entity def (#560)
Browse files Browse the repository at this point in the history
* Add typing (init already up to date).

* Update init entity and main tutorial

* Add typing import

* Correct typo measures to measure_set

* main-climada tutorieal: undo re-plotting

* PEP8: change import order

Co-authored-by: Chahan Kropf <chahan.kropf@posteo.com>
Co-authored-by: emanuel-schmid <schmide@ethz.ch>
Co-authored-by: Emanuel Schmid <51439563+emanuel-schmid@users.noreply.github.com>
  • Loading branch information
4 people authored Oct 27, 2022
1 parent 38b3945 commit 513c1f5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 55 deletions.
23 changes: 11 additions & 12 deletions climada/engine/unsequa/input_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,13 +795,13 @@ def _meas_set_unc_dict(bounds_cost):

def _ent_unc_func(EN, ET, EL, IFi, IL, MDD, PAA, CO, DR, bounds_noise,
impf_set_list, haz_id_dict, disc_rate, exp_list, meas_set):
ent = Entity()
ent.exposures = _exp_uncfunc(EN, ET, EL, exp_list, bounds_noise)
ent.impact_funcs = _impfset_uncfunc(IFi, MDD, PAA, IL, impf_set_list=impf_set_list,

exposures = _exp_uncfunc(EN, ET, EL, exp_list, bounds_noise)
impact_func_set = _impfset_uncfunc(IFi, MDD, PAA, IL, impf_set_list=impf_set_list,
haz_id_dict=haz_id_dict)
ent.measures = _meas_set_uncfunc(CO, meas_set=meas_set)
ent.disc_rates = _disc_uncfunc(DR, disc_rate)
return ent
measure_set = _meas_set_uncfunc(CO, meas_set=meas_set)
disc_rates = _disc_uncfunc(DR, disc_rate)
return Entity(exposures, disc_rates, impact_func_set, measure_set)

def _ent_unc_dict(bounds_totval, bounds_noise, bounds_impfi, bounds_mdd,
bounds_paa, n_impf_set, bounds_disc, bounds_cost, n_exp):
Expand All @@ -813,13 +813,12 @@ def _ent_unc_dict(bounds_totval, bounds_noise, bounds_impfi, bounds_mdd,

def _entfut_unc_func(EN, EG, EL, IFi, IL, MDD, PAA, CO, bounds_noise,
impf_set_list, haz_id_dict, exp_list, meas_set):
ent = Entity()
ent.exposures = _exp_uncfunc(EN=EN, ET=EG, EL=EL, exp_list=exp_list, bounds_noise=bounds_noise)
ent.impact_funcs = _impfset_uncfunc(IFi, MDD, PAA, IL, impf_set_list=impf_set_list,
exposures = _exp_uncfunc(EN=EN, ET=EG, EL=EL, exp_list=exp_list, bounds_noise=bounds_noise)
impact_funcs = _impfset_uncfunc(IFi, MDD, PAA, IL, impf_set_list=impf_set_list,
haz_id_dict=haz_id_dict)
ent.measures = _meas_set_uncfunc(CO, meas_set=meas_set)
ent.disc_rates = DiscRates() #Disc rate of future entity ignored in cost_benefit.calc()
return ent
measures = _meas_set_uncfunc(CO, meas_set=meas_set)
disc_rates = DiscRates() #Disc rate of future entity ignored in cost_benefit.calc()
return Entity(exposures, disc_rates, impact_funcs, measures)

def _entfut_unc_dict(bounds_impfi, bounds_mdd,
bounds_paa, n_impf_set, bounds_eg, bounds_noise,
Expand Down
16 changes: 11 additions & 5 deletions climada/entity/entity_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
__all__ = ['Entity']

import logging
from typing import Optional
import pandas as pd

from climada.entity.tag import Tag
Expand All @@ -32,16 +33,16 @@

LOGGER = logging.getLogger(__name__)

class Entity(object):
class Entity:
"""Collects exposures, impact functions, measures and discount rates.
Default values set when empty constructor.
Attributes
----------
exposures : Exposures
exposures
impact_funcs : ImpactFucs
impact functions
impact_funcs : ImpactFuncSet
impact functions set
measures : MeasureSet
measures
disc_rates : DiscRates
Expand All @@ -50,8 +51,13 @@ class Entity(object):
Default file from configuration file
"""

def __init__(self, exposures=None, disc_rates=None,
impact_func_set=None, measure_set=None):
def __init__(
self,
exposures: Optional[Exposures] = None,
disc_rates: Optional[DiscRates] = None,
impact_func_set: Optional[ImpactFuncSet] = None,
measure_set: Optional[MeasureSet] = None
):
"""
Initialize entity
Expand Down
92 changes: 54 additions & 38 deletions doc/tutorial/1_main_climada.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -481,20 +481,7 @@
"\n",
"As with Hazard objects, Entities can be read from files or created through code. The Excel template can be found in `climada_python/data/system/entity_template.xlsx`.\n",
"\n",
"In this tutorial we will create an Exposure object using the LitPop economic exposure module, and load a pre-defined wind damage function.\n",
"\n",
"First we create an empty Entity object:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from climada.entity import Entity\n",
"\n",
"ent = Entity()"
"In this tutorial we will create an Exposure object using the LitPop economic exposure module, and load a pre-defined wind damage function."
]
},
{
Expand All @@ -514,7 +501,7 @@
"4. Choose in the drop-down menus: Temporal: single year, FileFormat: GeoTiff, Resolution: 30 seconds. Click “2020” and then \"create download\".\n",
"5. Copy the file \"gpw_v4_population_count_rev11_2020_30_sec.tif\" into the folder \"~/climada/data\". (Or you can run the block once to find the right path in the error message)\n",
"\n",
"Now we can create an economic Exposure dataset for Puerto Rico, add it to our Entity, and plot it here:"
"Now we can create an economic Exposure dataset for Puerto Rico."
]
},
{
Expand Down Expand Up @@ -591,8 +578,6 @@
"exp_litpop = LitPop.from_countries('Puerto Rico', res_arcsec = 120) # We'll go lower resolution than default to keep it simple\n",
"exp_litpop.set_geometry_points() # Set geodataframe geometries from lat lon data\n",
"\n",
"ent.exposures = exp_litpop\n",
"\n",
"exp_litpop.plot_hexbin(pop_name=True, linewidth=4, buffer=0.1);"
]
},
Expand Down Expand Up @@ -656,9 +641,7 @@
"outputs": [],
"source": [
"imp_fun_set = ImpactFuncSet()\n",
"imp_fun_set.append(imp_fun)\n",
"\n",
"ent.impact_funcs = imp_fun_set"
"imp_fun_set.append(imp_fun)"
]
},
{
Expand Down Expand Up @@ -687,8 +670,7 @@
}
],
"source": [
"ent.exposures.gdf['impf_TC'] = 1\n",
"ent.check()"
"exp_litpop.gdf['impf_TC'] = 1"
]
},
{
Expand Down Expand Up @@ -909,16 +891,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"It shows there are now very few events at the 5-year return period - the new building codes removed most of these from the event set. Finally we add the measure set to our Entity."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"ent.measures = meas_set"
"It shows there are now very few events at the 5-year return period - the new building codes removed most of these from the event set."
]
},
{
Expand Down Expand Up @@ -959,9 +932,7 @@
"disc.years = np.arange(1950, 2101)\n",
"disc.rates = np.ones(disc.years.size) * 0.02\n",
"disc.check()\n",
"disc.plot()\n",
"\n",
"ent.disc_rates = disc"
"disc.plot()"
]
},
{
Expand All @@ -971,6 +942,46 @@
"We are now ready to move to the last part of the CLIMADA model for Impact and Cost Benefit analyses."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Define Entity\n",
"\n",
"We are now ready to define our Entity object that contains the exposures, impact functions, discount rates and measures."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'Optional' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/var/folders/r5/6rbkr9r16mg86237m11wqn500000gn/T/ipykernel_65266/1555313014.py\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mclimada\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mentity\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mEntity\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/Documents/Climada/climada_python/climada/entity/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mdisc_rates\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mmeasures\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 26\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mentity_def\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/Documents/Climada/climada_python/climada/entity/entity_def.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[0mLOGGER\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlogging\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetLogger\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m__name__\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 34\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 35\u001b[0;31m \u001b[0;32mclass\u001b[0m \u001b[0mEntity\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 36\u001b[0m \"\"\"Collects exposures, impact functions, measures and discount rates.\n\u001b[1;32m 37\u001b[0m \u001b[0mDefault\u001b[0m \u001b[0mvalues\u001b[0m \u001b[0mset\u001b[0m \u001b[0mwhen\u001b[0m \u001b[0mempty\u001b[0m \u001b[0mconstructor\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/Documents/Climada/climada_python/climada/entity/entity_def.py\u001b[0m in \u001b[0;36mEntity\u001b[0;34m()\u001b[0m\n\u001b[1;32m 53\u001b[0m def __init__(\n\u001b[1;32m 54\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 55\u001b[0;31m \u001b[0mexposures\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mOptional\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mExposures\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 56\u001b[0m \u001b[0mdisc_rates\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mOptional\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mDiscRates\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 57\u001b[0m \u001b[0mimpact_func_set\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mOptional\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mImpactFuncSet\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'Optional' is not defined"
]
}
],
"source": [
"from climada.entity import Entity\n",
"\n",
"ent = Entity(\n",
" exposures=exp_litpop,\n",
" disc_rates=disc,\n",
" impact_func_set=imp_fun_set,\n",
" measures_set=meas_set\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1249,9 +1260,9 @@
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3.8.13 ('climada_320')",
"language": "python",
"name": "conda-env-climada_311-py"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -1263,7 +1274,12 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
"version": "3.8.13"
},
"vscode": {
"interpreter": {
"hash": "0d8f20e4ce91d520e7feec912e0986dcdc154072031c954af0101af347b335d9"
}
}
},
"nbformat": 4,
Expand Down

0 comments on commit 513c1f5

Please sign in to comment.