Fitting#
- elecboltz.fit_model(x_data: Mapping[str, Sequence], y_data: Mapping[str, Sequence], init_params: Mapping, bounds: Mapping, x_shift: Mapping = None, x_normalize: Mapping = None, save_path: str = None, save_label: str = None, worker_percentage: float = 0.0, **kwargs)#
Convenience function to set up and run a fitting routine.
This uses
scipy.optimize.differential_evolutionto perform a global fit. The optimizer is hard-coded, because the callback functions are highly specific to each optimizer. Saves the results to the specified path.- Parameters:
x_data (Mapping[str, Sequence]) – The independent variable data (e.g. field). The name of the variable is mapped to the data, e.g.
{'field': [0, 1, 2]}.y_data (Mapping[str, Sequence]) – The dependent variable data (e.g. conductivity). The name of the variable is mapped to the data, e.g.
{'sigma_xx': [1.1, 2.4, 3.8]}. The name of each variable must start with “sigma” or “rho” (for conductivity or resistivity, respectively), and you can add a suffix to specify the component (e.g."sigma_xx","rho_xy").init_params (Mapping) – Initial parameters for the fitting routine, and also other parameters for initiallizing the classes. This is passed through
easy_paramsto theBandStructureandConductivity.bounds (Mapping) – Bounds for the fitting parameters. This mapping has the same structure as
init_params, but only containing the variables that are to be fitted, and their values in the mapping must be a collection of the form (min, max).x_shift (Mapping, optional) – If provided, the y values will be shifted by the y value at this x point. The mapping must have the same structure as
x_data, but with single values instead of arrays as the values.x_normalize (Mapping, optional) – If provided, the y values will be normalized by the y value at this x point. Note that shifting will always be done before normalization. The mapping must have the same structure as
x_data, but with single values instead of arrays as the values. Note that shifts are applied before normalization.save_path (str, optional) – The directory where the fitting results will be saved. If not provided, results will not be saved.
save_label (str, optional) – Label of the results. If not provided, will be set to
f"y_label_x_label". Ify_label` or ``x_labelare collections of string, they will be joined with an underscore.worker_percentage (float, optional) – The percentage of available workers to use for parallel computation. If set to 0, it will not be used for setting the number of workers. The number of workers can also be set by the workers keyword argument of differential evolution.
log_format (str, optional) – The format for logging parameter values.
**kwargs (dict, optional) – Additional keyword arguments passed to scipy.optimize.differential_evolution.
- class elecboltz.fit.FittingRoutine(init_params: Mapping, save_path: str = None, save_label: str = 'fit', update_keys: Collection[str] = None, print_log: bool = True)#
This convenience class automatically sets up a fitting routine.
Stores the necessary data and generates the functions used in This includes handling the residual computation, parameter update, logging the fitting progress, and saving the results to a file.
- Parameters:
init_params (Mapping) – Initial parameters for the fitting routine, and also other parameters for initiallizing the classes. This is passed through
easy_paramsto theBandStructureandConductivity.save_path (str, optional) – The directory where the fitting logs will be saved. If not provided, logs will not be saved.
save_label (str, optional) – Label of the results.
update_keys (Collection[str], optional) – The “flattened” keys of the parameters that will be updated during the fitting. See
extract_keysfor more information. Used for updating theparamsattribute and logging. If not provided,paramswill not be updated and the parameter names will not be mentioned in the log.print_log (bool, optional) – If True, the fitting progress will be printed to the console.
- params#
The current parameters dictionary.
- Type:
Mapping
- save_path#
The directory where the fitting logs will be saved.
- Type:
Path
- save_label#
The label of the results.
- Type:
Path
- update_keys#
The “flattened” keys of the parameters that will be updated during the fitting. This is used for updating the params attribute and logging.
- Type:
Collection[str]
- iteration#
The current iteration number.
- Type:
int
- last_time#
The timestamp of the last fitting iteration.
- Type:
float
- total_time#
The total time spent on the fitting routine.
- Type:
float
- log(param_values, convergence: float = None)#
Log the current fitting iteration and parameters.
- Parameters:
param_values (Sequence) – The current values of the parameters.
convergence (float, optional) – The convergence value of the fitting routine. 0.0 means no convergence, 1.0 means perfect convergence. If not provided, it will not be logged.
- residual(param_values: ~collections.abc.Sequence, param_keys: ~collections.abc.Sequence[str], x_data: ~collections.abc.Mapping[str, ~collections.abc.Sequence], y_data: ~collections.abc.Mapping[str, ~collections.abc.Sequence], x_shift: ~collections.abc.Mapping = None, x_normalize: ~collections.abc.Mapping = None, y_shift: ~collections.abc.Mapping = None, y_normalize: ~collections.abc.Mapping = None, cond_obj: ~elecboltz.conductivity.Conductivity = None, loss: ~typing.Callable = <function FittingRoutine.<lambda>>)#
Compute the residual for the given parameters and data.
- Parameters:
param_values (Sequence) – The values of the parameters to update.
param_keys (Sequence[str]) – The keys of the parameters to update.
x_data (Mapping[str, Sequence]) – The independent variable data (e.g. field). The name of the variable is mapped to the data, e.g.
{'field': [0, 1, 2]}.y_data (Mapping[str, Sequence]) – The dependent variable data (e.g. conductivity). The name of the variable is mapped to the data, e.g.
{'sigma_xx': [1.1, 2.4, 3.8]}.x_shift (Mapping, optional) – If provided, the y values will be shifted by the y value at this x point.
x_normalize (Mapping, optional) – If provided, the y values will be normalized by the y value at this x point. Note that shifts are applied before normalization.
y_shift (float, optional) – The y values will be normalized to this value (if
x_normalizeis provided).y_normalize (float, optional) – The y values will be shifted to this value (if
x_shiftis provided).squared (bool, optional) – If True, the residual is computed as the mean squared error. If False, it returns the mean absolute difference.
cond_obj (Conductivity, optional) – If provided, the fitter will try to salvage the calculations already done from that object.