models module

Utils Pydantic model classes.

class CHAPBaseModel

Bases: BaseModel

Base CHAP configuration class implementing robust serialization tools.

dict(*args, **kwargs)
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_dump(*args, **kwargs)

Modify the Pydantic Basemodel call to model_dump to incorporate the private class attribute “_exclude” to exclude certain class fields from the model dump to dict

model_dump_json(*args, **kwargs)

Modify the Pydantic Basemodel call to model_dump_json to incorporate the private class attribute “_exclude” to exclude certain class fields from the model dump to json.

class Constant(*, model: Literal['constant'], parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None)] = [], prefix: str | None = '')

Bases: CHAPBaseModel

Class representing a Constant model component.

Variables:
  • model – The model component base name (a prefix will be added if multiple identical model components are added).

  • parameters – Function parameters, defaults to those auto generated from the function signature (excluding the independent variable), defaults to [].

  • prefix – The model prefix, defaults to ‘’.

model: Literal['constant']
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None), FieldInfo(annotation=NoneType, required=True, validate_default=True)]
prefix: str | None
class Exponential(*, model: Literal['exponential'], parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None)] = [], prefix: str | None = '')

Bases: CHAPBaseModel

Class representing an Exponential model component.

Variables:
  • model – The model component base name (a prefix will be added if multiple identical model components are added).

  • parameters – Function parameters, defaults to those auto generated from the function signature (excluding the independent variable), defaults to [].

  • prefix – The model prefix, defaults to ‘’.

model: Literal['exponential']
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None), FieldInfo(annotation=NoneType, required=True, validate_default=True)]
prefix: str | None
class Expression(*, model: Literal['expression'], expr: Annotated[str, StringConstraints(strip_whitespace=True, to_upper=None, to_lower=None, strict=None, min_length=1, max_length=None, pattern=None)], parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None)] = [], prefix: str | None = '')

Bases: CHAPBaseModel

Class representing an Expression model component.

Variables:
  • model – The model component base name (a prefix will be added if multiple identical model components are added).

  • expr – Mathematical expression to represent the model component.

  • parameters – Function parameters, defaults to those auto generated from the model expression (excluding the independent variable), defaults to [].

  • prefix – The model prefix, defaults to ‘’.

expr: Annotated[str, StringConstraints(strip_whitespace=True, to_upper=None, to_lower=None, strict=None, min_length=1, max_length=None, pattern=None)]
model: Literal['expression']
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None), FieldInfo(annotation=NoneType, required=True, validate_default=True)]
prefix: str | None
class FitConfig(*, code: Literal['lmfit', 'scipy'] = 'scipy', parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None)] = [], models: Annotated[list[Constant | Linear | Quadratic | Exponential | Gaussian | Lorentzian | Rectangle | Expression | Multipeak], Len(min_length=1, max_length=None)], method: Literal['leastsq', 'trf', 'dogbox', 'lm', 'least_squares'] = 'leastsq', rel_height_cutoff: Annotated[float, None, Interval(gt=0, ge=None, lt=1.0, le=None), None, AllowInfNan(allow_inf_nan=False)] | None = None, num_proc: Annotated[int, None, Interval(gt=0, ge=None, lt=None, le=None), None] = 1, plot: Annotated[bool, Strict(strict=True)] = False, print_report: Annotated[bool, Strict(strict=True)] = False, memfolder: str = 'joblib_memmap')

Bases: CHAPBaseModel

Class representing the configuration for the fit processor.

Variables:
  • code – Specifies is lmfit is used to perform the fit or if the scipy fit method is called directly, default to ‘lmfit’.

  • parameters – Fit model parameters in addition to those implicitly defined through the build-in model functions, defaults to []

  • models – The component(s) of the (composite) fit model.

  • rel_height_cutoff – Relative peak height cutoff for peak fitting (any peak with a height smaller than rel_height_cutoff times the maximum height of all peaks gets removed from the fit model).

  • num_proc – The number of processors used in fitting a map of data, defaults to 1.

  • plot – Weather a plot of the fit result is generated, defaults to False.

  • print_report – Weather to generate a fit result printout, defaults to False.

code: Literal['lmfit', 'scipy']
memfolder: str
method: Literal['leastsq', 'trf', 'dogbox', 'lm', 'least_squares']
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

models: Annotated[list[Constant | Linear | Quadratic | Exponential | Gaussian | Lorentzian | Rectangle | Expression | Multipeak], Len(min_length=1, max_length=None)]
num_proc: Annotated[int, None, Interval(gt=0, ge=None, lt=None, le=None), None]
parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None)]
plot: Annotated[bool, Strict(strict=True)]
print_report: Annotated[bool, Strict(strict=True)]
rel_height_cutoff: Annotated[float, None, Interval(gt=0, ge=None, lt=1.0, le=None), None, AllowInfNan(allow_inf_nan=False)] | None
classmethod validate_method(method, info)

Validate the specified method.

Parameters:
  • method (str) – The value of method to validate.

  • info (pydantic_core._pydantic_core.ValidationInfo) – Pydantic validator info object.

Returns:

Fit method.

Return type:

str

class FitParameter(*, name: Annotated[str, StringConstraints(strip_whitespace=True, to_upper=None, to_lower=None, strict=None, min_length=1, max_length=None, pattern=None)], value: Annotated[float, None, Interval(gt=None, ge=None, lt=None, le=None), None, AllowInfNan(allow_inf_nan=False)] | None = None, min: Annotated[float, None, Interval(gt=None, ge=None, lt=None, le=None), None, None] | None = -inf, max: Annotated[float, None, Interval(gt=None, ge=None, lt=None, le=None), None, None] | None = inf, vary: Annotated[bool, Strict(strict=True)] = True, expr: Annotated[str, StringConstraints(strip_whitespace=True, to_upper=None, to_lower=None, strict=None, min_length=1, max_length=None, pattern=None)] | None = None)

Bases: CHAPBaseModel

Class representing a specific fit parameter for the fit processor.

property default

Return the _default attribute.

expr: Annotated[str, StringConstraints(strip_whitespace=True, to_upper=None, to_lower=None, strict=None, min_length=1, max_length=None, pattern=None)] | None
property init_value

Return the _init_value attribute.

max: Annotated[float, None, Interval(gt=None, ge=None, lt=None, le=None), None, None] | None
min: Annotated[float, None, Interval(gt=None, ge=None, lt=None, le=None), None, None] | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_post_init(context: Any, /) None

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since that’s what pydantic-core passes when calling it.

Args:

self: The BaseModel instance. context: The context.

name: Annotated[str, StringConstraints(strip_whitespace=True, to_upper=None, to_lower=None, strict=None, min_length=1, max_length=None, pattern=None)]
property prefix

Return the _prefix attribute.

set(value=None, min=None, max=None, vary=None, expr=None)

Set or update FitParameter attributes.

Parameters:
  • value (float, optional) – Parameter value.

  • min (bool, optional) – Lower Parameter value bound. To remove the lower bound you must set min to numpy.inf.

  • max (bool, optional) – Upper Parameter value bound. To remove the lower bound you must set max to numpy.inf.

  • vary (bool, optional) – Whether the Parameter is varied during a fit.

  • expr (str, optional) – Mathematical expression used to constrain the value during the fit. To remove a constraint you must supply an empty string.

property stderr

Return the _stderr attribute.

classmethod validate_max(value)

Validate the specified max.

Parameters:

value (Union[float, None]) – Field value to validate (max).

Returns:

Upper bound of fit parameter.

Return type:

float

classmethod validate_min(value)

Validate the specified min.

Parameters:

value (Union[float, None]) – Field value to validate (min).

Returns:

Lower bound of fit parameter.

Return type:

float

value: Annotated[float, None, Interval(gt=None, ge=None, lt=None, le=None), None, AllowInfNan(allow_inf_nan=False)] | None
vary: Annotated[bool, Strict(strict=True)]
class Gaussian(*, model: Literal['gaussian'], parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None)] = [], prefix: str | None = '')

Bases: CHAPBaseModel

Class representing a Gaussian model component.

Variables:
  • model – The model component base name (a prefix will be added if multiple identical model components are added).

  • parameters – Function parameters, defaults to those auto generated from the function signature (excluding the independent variable), defaults to [].

  • prefix – The model prefix, defaults to ‘’.

model: Literal['gaussian']
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None), FieldInfo(annotation=NoneType, required=True, validate_default=True)]
prefix: str | None
class Linear(*, model: Literal['linear'], parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None)] = [], prefix: str | None = '')

Bases: CHAPBaseModel

Class representing a Linear model component.

Variables:
  • model – The model component base name (a prefix will be added if multiple identical model components are added).

  • parameters – Function parameters, defaults to those auto generated from the function signature (excluding the independent variable), defaults to [].

  • prefix – The model prefix, defaults to ‘’.

model: Literal['linear']
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None), FieldInfo(annotation=NoneType, required=True, validate_default=True)]
prefix: str | None
class Lorentzian(*, model: Literal['lorentzian'], parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None)] = [], prefix: str | None = '')

Bases: CHAPBaseModel

Class representing a Lorentzian model component.

Variables:
  • model – The model component base name (a prefix will be added if multiple identical model components are added).

  • parameters – Function parameters, defaults to those auto generated from the function signature (excluding the independent variable), defaults to [].

  • prefix – The model prefix, defaults to ‘’.

model: Literal['lorentzian']
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None), FieldInfo(annotation=NoneType, required=True, validate_default=True)]
prefix: str | None
class Multipeak(*, model: Literal['multipeak'], centers: Annotated[list[Annotated[float, None, Interval(gt=None, ge=None, lt=None, le=None), None, AllowInfNan(allow_inf_nan=False)]], Len(min_length=1, max_length=None)], centers_range: Annotated[float, None, Interval(gt=None, ge=None, lt=None, le=None), None, AllowInfNan(allow_inf_nan=False)] | None = None, fit_type: Literal['uniform', 'unconstrained'] | None = 'unconstrained', fwhm_min: Annotated[float, None, Interval(gt=None, ge=None, lt=None, le=None), None, AllowInfNan(allow_inf_nan=False)] | None = None, fwhm_max: Annotated[float, None, Interval(gt=None, ge=None, lt=None, le=None), None, AllowInfNan(allow_inf_nan=False)] | None = None, peak_models: Literal['gaussian', 'lorentzian'] = 'gaussian')

Bases: CHAPBaseModel

Class representing a multipeak model.

Variables:
  • model – The model component base name (a prefix will be added if multiple identical model components are added).

  • centers – Peak centers.

  • centers_range – Range of peak centers around their centers.

  • fit_type – Type of fit, defaults to ‘unconstrained’.

  • fwhm_min – Lower limit of the fwhm of the peaks.

  • fwhm_max – Upper limit of the fwhm of the peaks.

  • peak_models – Type of peaks, defaults to ‘gaussian’.

centers: Annotated[list[Annotated[float, None, Interval(gt=None, ge=None, lt=None, le=None), None, AllowInfNan(allow_inf_nan=False)]], Len(min_length=1, max_length=None)]
centers_range: Annotated[float, None, Interval(gt=None, ge=None, lt=None, le=None), None, AllowInfNan(allow_inf_nan=False)] | None
fit_type: Literal['uniform', 'unconstrained'] | None
fwhm_max: Annotated[float, None, Interval(gt=None, ge=None, lt=None, le=None), None, AllowInfNan(allow_inf_nan=False)] | None
fwhm_min: Annotated[float, None, Interval(gt=None, ge=None, lt=None, le=None), None, AllowInfNan(allow_inf_nan=False)] | None
model: Literal['multipeak']
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

peak_models: Literal['gaussian', 'lorentzian']
class Quadratic(*, model: Literal['quadratic'], parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None)] = [], prefix: str | None = '')

Bases: CHAPBaseModel

Class representing a Quadratic model component.

Variables:
  • model – The model component base name (a prefix will be added if multiple identical model components are added).

  • parameters – Function parameters, defaults to those auto generated from the function signature (excluding the independent variable), defaults to [].

  • prefix – The model prefix, defaults to ‘’.

model: Literal['quadratic']
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None), FieldInfo(annotation=NoneType, required=True, validate_default=True)]
prefix: str | None
class Rectangle(*, model: Literal['rectangle'], parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None)] = [], prefix: str | None = '')

Bases: CHAPBaseModel

Class representing a Rectangle model component.

Variables:
  • model – The model component base name (a prefix will be added if multiple identical model components are added).

  • parameters – Function parameters, defaults to those auto generated from the function signature (excluding the independent variable), defaults to [].

  • prefix – The model prefix, defaults to ‘’.

model: Literal['rectangle']
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

parameters: Annotated[list[FitParameter], Len(min_length=0, max_length=None), FieldInfo(annotation=NoneType, required=True, validate_default=True)]
prefix: str | None
constant(x, c=0.0)

Return a linear function.

constant(x, c) = c

Parameters:
  • x (numpy.ndarray) – The x-coordinates.

  • c (float) – The constant.

Returns:

The constant function results.

Type:

numpy.ndarray

exponential(x, amplitude=1.0, decay=1.0)

Return an exponential function.

exponential(x, amplitude, decay) = amplitude * exp(-x/decay)

gaussian(x, amplitude=1.0, center=0.0, sigma=1.0)

Return a 1-dimensional Gaussian function.

gaussian(x, amplitude, center, sigma) =

(amplitude/(s2pi*sigma)) * exp(-(x-center)**2 / (2*sigma**2))

linear(x, slope=1.0, intercept=0.0)

Return a linear function.

linear(x, slope, intercept) = slope * x + intercept

lorentzian(x, amplitude=1.0, center=0.0, sigma=1.0)

Return a 1-dimensional Lorentzian function.

lorentzian(x, amplitude, center, sigma) =

(amplitude/(1 + ((1.0*x-center)/sigma)**2)) / (pi*sigma)

quadratic(x, a=0.0, b=0.0, c=0.0)

Return a parabolic function.

parabolic(x, a, b, c) = a * x**2 + b * x + c

rectangle(x, amplitude=1.0, center1=0.0, sigma1=1.0, center2=1.0, sigma2=1.0, form='linear')

Return a rectangle function.

Starts at 0.0, rises to amplitude (at center1 with width sigma1), then drops to 0.0 (at center2 with width sigma2) with form: - ‘linear’ (default) = ramp_up + ramp_down - ‘atan’, ‘arctan’ = amplitude*(atan(arg1) + atan(arg2))/pi - ‘erf’ = amplitude*(erf(arg1) + erf(arg2))/2. - ‘logisitic’ = amplitude*[1 - 1/(1 + exp(arg1)) - 1/(1+exp(arg2))]

where arg1 = (x - center1)/sigma1 and arg2 = -(x - center2)/sigma2.

validate_parameters(parameters, info)

Validate the parameters.

Parameters:
  • parameters (list[FitParameter]) – Fit model parameters.

  • info (pydantic_core._pydantic_core.ValidationInfo) – Pydantic validator info object.

Returns:

List of fit model parameters.

Return type:

list[FitParameter]