Skip to content

Commit

Permalink
Feature/order samples unsequa (#766)
Browse files Browse the repository at this point in the history
* Allow to set loglevel

* Add method to sort samples

* Update CHANGELOG.md

* Add advanced examples for unsequa

* Remove logging control

* Update changelog

* Remove tipo

* Clarify docstring

* Correct docstring

* Update t.o.c.

* Remove unecessary output prints

* Remove linter issues

* Update changelog

---------

Co-authored-by: Chahan Kropf <[email protected]>
  • Loading branch information
chahank and Chahan Kropf authored Aug 24, 2023
1 parent 2d6130c commit a21abe3
Show file tree
Hide file tree
Showing 6 changed files with 731 additions and 1,711 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Removed:
- Added method `Exposures.centroids_total_value` to replace the functionality of `Exposures.affected_total_value`. This method is temporary and deprecated. [#702](https://github.com/CLIMADA-project/climada_python/pull/702)
- New method `climada.util.api_client.Client.purge_cache`: utility function to remove outdated files from the local file system to free disk space.
([#737](https://github.com/CLIMADA-project/climada_python/pull/737))
- Added advanced examples in unsequa tutorial for coupled input variables and for handling efficiently the loading of multiple large files [#766](https://github.com/CLIMADA-project/climada_python/pull/766)

### Changed

Expand Down
5 changes: 4 additions & 1 deletion climada/engine/unsequa/calc_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,10 @@ def _sample_parallel_iterator(self, samples, chunksize, **kwargs):
suitable for methods _map_impact_calc and _map_costben_calc
"""
return zip(_chunker(samples, chunksize), *(itertools.repeat(item) for item in kwargs.values()))
return zip(
_chunker(samples, chunksize),
*(itertools.repeat(item) for item in kwargs.values())
)

def _chunker(seq, size):
for pos in range(0, len(seq), size):
Expand Down
14 changes: 8 additions & 6 deletions climada/engine/unsequa/calc_cost_benefit.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ class CalcCostBenefit(Calc):
----------
value_unit : str
Unit of the exposures value
haz_input_var : climada.engine.uncertainty.input_var.InputVar
haz_input_var : InputVar or Hazard
Present Hazard uncertainty variable
ent_input_var : climada.engine.uncertainty.input_var.InputVar
ent_input_var : InputVar or Entity
Present Entity uncertainty variable
haz_unc_fut_Var: climada.engine.uncertainty.input_var.InputVar
haz_unc_fut_Var: InputVar or Hazard
Future Hazard uncertainty variable
ent_fut_input_var : climada.engine.uncertainty.input_var.InputVar
ent_fut_input_var : InputVar or Entity
Future Entity uncertainty variable
_input_var_names : tuple(str)
Names of the required uncertainty variables
Expand Down Expand Up @@ -179,15 +179,17 @@ def uncertainty(self,
See Also
--------
climada.engine.cost_benefit:
Compute risk and adptation option cost benefits.
compute risk and adptation option cost benefits.
"""

if unc_sample.samples_df.empty:
raise ValueError("No sample was found. Please create one first" +
"using UncImpact.make_sample(N)")

chunksize = np.ceil(unc_sample.samples_df.shape[0] / processes).astype(int) if chunksize is None else chunksize
chunksize = np.ceil(
unc_sample.samples_df.shape[0] / processes
).astype(int) if chunksize is None else chunksize

samples_df = unc_sample.samples_df.copy(deep=True)
unit = self.value_unit
Expand Down
14 changes: 8 additions & 6 deletions climada/engine/unsequa/calc_impact.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

LOGGER = logging.getLogger(__name__)


class CalcImpact(Calc):
"""
Impact uncertainty caclulation class.
Expand All @@ -57,11 +56,11 @@ class CalcImpact(Calc):
Compute eai_exp or not
value_unit : str
Unit of the exposures value
exp_input_var : climada.engine.uncertainty.input_var.InputVar
exp_input_var : InputVar or Exposures
Exposure uncertainty variable
impf_input_var : climada.engine.uncertainty.input_var.InputVar
impf_input_var : InputVar if ImpactFuncSet
Impact function set uncertainty variable
haz_input_var: climada.engine.uncertainty.input_var.InputVar
haz_input_var: InputVar or Hazard
Hazard uncertainty variable
_input_var_names : tuple(str)
Names of the required uncertainty input variables
Expand Down Expand Up @@ -180,15 +179,18 @@ def uncertainty(self,
See Also
--------
climada.engine.impact: Compute risk.
climada.engine.impact:
compute impact and risk.
"""

if unc_sample.samples_df.empty:
raise ValueError("No sample was found. Please create one first"
"using UncImpact.make_sample(N)")

chunksize = np.ceil(unc_sample.samples_df.shape[0] / processes).astype(int) if chunksize is None else chunksize
chunksize = np.ceil(
unc_sample.samples_df.shape[0] / processes
).astype(int) if chunksize is None else chunksize

samples_df = unc_sample.samples_df.copy(deep=True)

Expand Down
18 changes: 18 additions & 0 deletions climada/engine/unsequa/unc_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ def __init__(self, samples_df, unit=None):
self.samples_df = samples_df
self.unit = unit

def order_samples(self, by_parameters):
"""
Function to sort the samples dataframe.
Note: the unc_output.samples_df is ordered inplace.
Parameters
----------
by_parameters : list[string]
List of the uncertainty parameters to sort by (ordering in list is kept)
Returns
-------
None.
"""
self.samples_df.sort_values(by=by_parameters, inplace=True, axis=0)

def get_samples_df(self):
return getattr(self, 'samples_df')

Expand Down
Loading

0 comments on commit a21abe3

Please sign in to comment.