55from typing import Callable , Mapping , Optional , Tuple , Union , Any , Dict
66from numbers import Number
77
8- import numpy as np
98import pandas as pd
109from scipy .optimize import Bounds , LinearConstraint , NonlinearConstraint
1110
@@ -29,14 +28,19 @@ class Input:
2928 data : pandas.Dataframe
3029 The data to compare computed impacts to. Index: Event IDs matching the IDs of
3130 ``hazard``. Columns: Arbitrary columns.
32- cost_func : Callable
33- Function that takes an ``Impact`` object and a ``pandas.Dataframe`` as argument
34- and returns a single number. The optimization algorithm will try to minimize this
35- number. See this module for a suggestion of cost functions.
36- impact_func_gen : Callable
31+ impact_func_creator : Callable
3732 Function that takes the parameters as keyword arguments and returns an impact
3833 function set. This will be called each time the optimization algorithm updates
3934 the parameters.
35+ impact_to_dataframe : Callable
36+ Function that takes an impact object as input and transforms its data into a
37+ pandas.DataFrame that is compatible with the format of :py:attr:`data`.
38+ The return value of this function will be passed to the :py:attr`cost_func`
39+ as first argument.
40+ cost_func : Callable
41+ Function that takes two ``pandas.Dataframe`` objects and returns the scalar
42+ "cost" between them. The optimization algorithm will try to minimize this
43+ number.
4044 bounds : Mapping (str, {Bounds, tuple(float, float)}), optional
4145 The bounds for the parameters. Keys: parameter names. Values:
4246 ``scipy.minimize.Bounds`` instance or tuple of minimum and maximum value.
@@ -58,8 +62,9 @@ class Input:
5862 hazard : Hazard
5963 exposure : Exposures
6064 data : pd .DataFrame
61- cost_func : Callable [[Impact , pd .DataFrame ], Number ]
62- impact_func_gen : Callable [..., ImpactFuncSet ]
65+ impact_func_creator : Callable [..., ImpactFuncSet ]
66+ impact_to_dataframe : Callable [[Impact ], pd .DataFrame ]
67+ cost_func : Callable [[pd .DataFrame , pd .DataFrame ], Number ]
6368 bounds : Optional [Mapping [str , Union [Bounds , Tuple [Number , Number ]]]] = None
6469 constraints : Optional [Union [ConstraintType , list [ConstraintType ]]] = None
6570 impact_calc_kwds : Mapping [str , Any ] = field (
@@ -104,7 +109,7 @@ class Optimizer(ABC):
104109
105110 input : Input
106111
107- def _target_func (self , impact : Impact , data : pd .DataFrame ) -> Number :
112+ def _target_func (self , impact : pd . DataFrame , data : pd .DataFrame ) -> Number :
108113 """Target function for the optimizer
109114
110115 The default version of this function simply returns the value of the cost
@@ -123,13 +128,13 @@ def _target_func(self, impact: Impact, data: pd.DataFrame) -> Number:
123128 """
124129 return self .input .cost_func (impact , data )
125130
126- def _kwargs_to_impact_func_gen (self , * _ , ** kwargs ) -> Dict [str , Any ]:
131+ def _kwargs_to_impact_func_creator (self , * _ , ** kwargs ) -> Dict [str , Any ]:
127132 """Define how the parameters to :py:meth:`_opt_func` must be transformed
128133
129134 Optimizers may implement different ways of representing the parameters (e.g.,
130135 key-value pairs, arrays, etc.). Depending on this representation, the parameters
131136 must be transformed to match the syntax of the impact function generator used,
132- see :py:attr:`Input.impact_func_gen `.
137+ see :py:attr:`Input.impact_func_creator `.
133138
134139 In this default version, the method simply returns its keyword arguments as
135140 mapping. Override this method if the optimizer used *does not* represent
@@ -162,14 +167,15 @@ def _opt_func(self, *args, **kwargs) -> Number:
162167 -------
163168 Target function value for the given arguments
164169 """
165- params = self ._kwargs_to_impact_func_gen (* args , ** kwargs )
166- impf_set = self .input .impact_func_gen (** params )
170+ params = self ._kwargs_to_impact_func_creator (* args , ** kwargs )
171+ impf_set = self .input .impact_func_creator (** params )
167172 impact = ImpactCalc (
168173 exposures = self .input .exposure ,
169174 impfset = impf_set ,
170175 hazard = self .input .hazard ,
171176 ).impact (** self .input .impact_calc_kwds )
172- return self ._target_func (impact , self .input .data )
177+ impact_df = self .input .impact_to_dataframe (impact )
178+ return self ._target_func (impact_df , self .input .data )
173179
174180 @abstractmethod
175181 def run (self , ** opt_kwargs ) -> Output :
0 commit comments