CHAP.utils.models

Utils Pydantic model classes.

Module Contents

Classes

FitParameter

Class representing a specific fit parameter for the fit processor.

Constant

Class representing a Constant model component.

Linear

Class representing a Linear model component.

Quadratic

Class representing a Quadratic model component.

Exponential

Class representing an Exponential model component.

Gaussian

Class representing a Gaussian model component.

Lorentzian

Class representing a Lorentzian model component.

Rectangle

Class representing a Rectangle model component.

Expression

Class representing an Expression model component.

Multipeak

FitConfig

Class representing the configuration for the fit processor.

Functions

constant

Return a linear function.

linear

Return a linear function.

quadratic

Return a parabolic function.

exponential

Return an exponential function.

gaussian

Return a 1-dimensional Gaussian function.

lorentzian

Return a 1-dimensional Lorentzian function.

rectangle

Return a rectangle function.

validate_parameters

Validate the parameters

Data

tiny

s2pi

models

model_classes

API

CHAP.utils.models.tiny

None

CHAP.utils.models.s2pi

‘sqrt(…)’

CHAP.utils.models.constant(x, c=0.0)

Return a linear function.

constant(x, c) = c

CHAP.utils.models.linear(x, slope=1.0, intercept=0.0)

Return a linear function.

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

CHAP.utils.models.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

CHAP.utils.models.exponential(x, amplitude=1.0, decay=1.0)

Return an exponential function.

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

CHAP.utils.models.gaussian(x, amplitude=1.0, center=0.0, sigma=1.0)

Return a 1-dimensional Gaussian function.

gaussian(x, amplitude, center, sigma) = (amplitude/(s2pisigma)) * exp(-(x-center)**2 / (2sigma**2))

CHAP.utils.models.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.0x-center)/sigma)**2)) / (pisigma)

CHAP.utils.models.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.

CHAP.utils.models.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]

class CHAP.utils.models.FitParameter

Bases: pydantic.BaseModel

Class representing a specific fit parameter for the fit processor.

name: constr(strip_whitespace=True, min_length=1)

None

value: Optional[confloat(allow_inf_nan=False)]

None

min: Optional[confloat()]

None

max: Optional[confloat()]

None

vary: pydantic.StrictBool

True

expr: Optional[constr(strip_whitespace=True, min_length=1)]

None

_default: float

‘PrivateAttr(…)’

_init_value: float

‘PrivateAttr(…)’

_prefix: str

‘PrivateAttr(…)’

_stderr: float

‘PrivateAttr(…)’

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

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

property default

Return the _default attribute.

property init_value

Return the _init_value attribute.

property prefix

Return the _prefix attribute.

property stderr

Return the _stderr 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.

class CHAP.utils.models.Constant

Bases: pydantic.BaseModel

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]

None

parameters: typing_extensions.Annotated[conlist(item_type=FitParameter), Field(validate_default=True)]

[]

prefix: Optional[str] = <Multiline-String>
_validate_parameters_parameters

‘(…)’

class CHAP.utils.models.Linear

Bases: pydantic.BaseModel

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]

None

parameters: typing_extensions.Annotated[conlist(item_type=FitParameter), Field(validate_default=True)]

[]

prefix: Optional[str] = <Multiline-String>
_validate_parameters_parameters

‘(…)’

class CHAP.utils.models.Quadratic

Bases: pydantic.BaseModel

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]

None

parameters: typing_extensions.Annotated[conlist(item_type=FitParameter), Field(validate_default=True)]

[]

prefix: Optional[str] = <Multiline-String>
_validate_parameters_parameters

‘(…)’

class CHAP.utils.models.Exponential

Bases: pydantic.BaseModel

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]

None

parameters: typing_extensions.Annotated[conlist(item_type=FitParameter), Field(validate_default=True)]

[]

prefix: Optional[str] = <Multiline-String>
_validate_parameters_parameters

‘(…)’

class CHAP.utils.models.Gaussian

Bases: pydantic.BaseModel

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]

None

parameters: typing_extensions.Annotated[conlist(item_type=FitParameter), Field(validate_default=True)]

[]

prefix: Optional[str] = <Multiline-String>
_validate_parameters_parameters

‘(…)’

class CHAP.utils.models.Lorentzian

Bases: pydantic.BaseModel

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]

None

parameters: typing_extensions.Annotated[conlist(item_type=FitParameter), Field(validate_default=True)]

[]

prefix: Optional[str] = <Multiline-String>
_validate_parameters_parameters

‘(…)’

class CHAP.utils.models.Rectangle

Bases: pydantic.BaseModel

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]

None

parameters: typing_extensions.Annotated[conlist(item_type=FitParameter), Field(validate_default=True)]

[]

prefix: Optional[str] = <Multiline-String>
_validate_parameters_parameters

‘(…)’

class CHAP.utils.models.Expression

Bases: pydantic.BaseModel

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 ''.

model: Literal[expression]

None

expr: constr(strip_whitespace=True, min_length=1)

None

parameters: typing_extensions.Annotated[conlist(item_type=FitParameter), Field(validate_default=True)]

[]

prefix: Optional[str] = <Multiline-String>
_validate_parameters_parameters

‘(…)’

class CHAP.utils.models.Multipeak

Bases: pydantic.BaseModel

model: Literal[multipeak]

None

centers: conlist(item_type=confloat(allow_inf_nan=False), min_length=1)

None

fit_type: Optional[Literal[uniform, unconstrained]]

‘unconstrained’

centers_range: Optional[confloat(allow_inf_nan=False)]

None

fwhm_min: Optional[confloat(allow_inf_nan=False)]

None

fwhm_max: Optional[confloat(allow_inf_nan=False)]

None

peak_models: Literal[gaussian, lorentzian]

‘gaussian’

CHAP.utils.models.models

None

CHAP.utils.models.model_classes

()

class CHAP.utils.models.FitConfig

Bases: pydantic.BaseModel

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 is '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), defaults to None.

  • 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]

‘scipy’

parameters: conlist(item_type=FitParameter)

[]

models: conlist(item_type=Constant | Linear | Quadratic | Exponential | Gaussian | Lorentzian | Rectangle | Expression | Multipeak, min_length=1)

None

method: Literal[leastsq, trf, dogbox, lm, least_squares]

‘leastsq’

rel_height_cutoff: Optional[confloat(gt=0, lt=1.0, allow_inf_nan=False)]

None

num_proc: conint(gt=0)

1

plot: pydantic.StrictBool

False

print_report: pydantic.StrictBool

False

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