General extrapolation/interpolation models (models)#

Classes:

SymFuncBase(beta)

Base class to define a sympy function for user defined derivatives.

Lambdify(exprs, args, *[, lambdify_kws])

Create list-like of python functions from list-like of expressions.

SymDerivBase(func[, args, expand, ...])

Base class for working with recursive derivatives in expansions.

SymMinusLog()

Class to compute derivatives of Y = -log(<X>).

Derivatives(funcs, *[, exprs, args])

Class to wrap functions calculating derivatives to specified order.

ExtrapModel(alpha0, data, derivatives[, ...])

Apply taylor series extrapolation.

StateCollection(states, *[, kws])

Sequence of models.

PiecewiseStateCollection(states, *[, kws])

Provide methods for Piecewise state collection.

ExtrapWeightedModel(states, *[, kws])

Weighted extrapolation model.

InterpModel(states, *[, kws])

Interpolation model.

InterpModelPiecewise(states, *[, kws])

Apposed to the multiple model InterpModel, perform a piecewise interpolation.

PerturbModel(alpha0, data[, alpha_name])

Perturbation model.

MBARModel(states, *[, kws])

Sadly, this doesn't work as beautifully.

Functions:

factory_minus_log()

Create lambdified version of SymMinusLog

taylor_series_norm(order[, order_dim])

taylor_series_coefficients = derivs * taylor_series_norm.

xr_weights_minkowski(deltas[, m, dim])

Minkowski weights DataArray.

class thermoextrap.models.SymFuncBase(beta)[source]#

Bases: Function

Base class to define a sympy function for user defined derivatives.

Methods:

deriv_args()

Symbol arguments of function.

fdiff([argindex])

Derivative of function.

doit([deep])

Generic doit method (See doit()).

eval(beta)

Evaluate function.

tcall(beta)

Typed interface to cls(...)

classmethod deriv_args()[source]#

Symbol arguments of function.

This is used by Data class to create a ‘lambdfied’ callable function.

fdiff(argindex=1)[source]#

Derivative of function. This will be used by thermoextrap.models.SymDerivBase.

doit(deep=False, **hints)[source]#

Generic doit method (See doit()).

classmethod eval(beta)[source]#

Evaluate function.

Other than trivial cases where the function returns a number, Function should return None. The heavy lifting is deferred to doit()

classmethod tcall(beta)[source]#

Typed interface to cls(…)

This also allows us to call with named keyword arguments, etc.

class thermoextrap.models.Lambdify(exprs, args, *, lambdify_kws=NOTHING)[source]#

Bases: object

Create list-like of python functions from list-like of expressions.

Parameters:
  • exprs (sequence of Function) – array of sympy expressions to lambdify

  • args (sequence of Symbol) – array of symbols which will be in args of the resulting function

  • lambdify_kws (dict) – Extra arguments to lambdify(). Note that by default we set cse = True, dummify = True by default.

class thermoextrap.models.SymDerivBase(func, args=None, expand=True, post_func=None, beta=None)[source]#

Bases: object

Base class for working with recursive derivatives in expansions.

Parameters:
  • func (Function) – Function to differentiate. This should (most likely) be an instance of thermoextrap.models.SymFuncBase

  • args (sequence of Symbol) – Arguments to func

  • expand (bool) – If True, apply expand()

  • post_func (str or callable()) – Transformation of base function. For example, post_fuc = -sympy.log is equivalent to passing minus_log=True If a string, then apply the following standard functions

    • minus_log : post_func = -sympy.log

    • pow_i : post_func = lambda f: pow(f, i). E.g., pow_2 => pow(f, 2)

Methods:

doit([simplify, expand])

Indexer for Derivatives with doit() applied

lambdify([args, simplify, expand])

Indexer of lambdified functions

doit(simplify=False, expand=False)[source]#

Indexer for Derivatives with doit() applied

lambdify(args=None, simplify=False, expand=False, **lambdify_kws)[source]#

Indexer of lambdified functions

See also

doit, Lambdify

class thermoextrap.models.SymMinusLog[source]#

Bases: object

Class to compute derivatives of Y = -log(<X>).

thermoextrap.models.factory_minus_log()[source]#

Create lambdified version of SymMinusLog

class thermoextrap.models.Derivatives(funcs, *, exprs=None, args=None)[source]#

Bases: MyAttrsMixin

Class to wrap functions calculating derivatives to specified order.

Parameters:
  • funcs (sequence of callable()) – funcs[i](*args) gives the ith derivative

  • exprs (sequence of Expr, optional) – expressions corresponding to the funcs Mostly for debugging purposes.

Attributes:

funcs

Sequence of callable functions

exprs

Sequence of sympy expressions, optional

args

Arguments

Methods:

derivs(data, *[, order, minus_log, ...])

Calculate derivatives for orders range(0, order+1).

coefs(data, *[, order, minus_log, order_dim])

Alias to self.derivs(..., norm=True).

from_sympy(exprs, args)

Create object from list of sympy functions.

funcs#

Sequence of callable functions

exprs#

Sequence of sympy expressions, optional

args#

Arguments

derivs(data, *, order=None, minus_log=False, order_dim='order', concat_kws=None, norm=False)[source]#

Calculate derivatives for orders range(0, order+1).

Parameters:
  • data (object) – Data object. If passed, use args=data.deriv_args

  • order (int, optional) – If pass data and order is None, then order=data.order Otherwise, must mass order

  • minus_log (bool, default False) – If True, apply transform for Y = -log(<X>)

  • order_dim (str, default 'order') – If None, output will be a list If order_dim is a string, then apply xarray.concat to output To yield a single DataArray

  • concat_kws (dict, optional) – extra arguments to xarray.concat

  • norm (bool, default False) – If true, then normalize derivatives by 1/n!, where n is the order of the derivative. That is, transform derivatives to taylor series coefficients See also taylor_series_norm

Returns:

output (list of xarray.DataArray) – See above for nature of output

coefs(data, *, order=None, minus_log=False, order_dim='order', **kwargs)[source]#

Alias to self.derivs(…, norm=True).

See also

derivs

classmethod from_sympy(exprs, args)[source]#

Create object from list of sympy functions.

Parameters:
Returns:

output (object)

thermoextrap.models.taylor_series_norm(order, order_dim='order')[source]#

taylor_series_coefficients = derivs * taylor_series_norm.

class thermoextrap.models.ExtrapModel(alpha0, data, derivatives, order=None, *, minus_log=False, alpha_name='alpha')[source]#

Bases: MyAttrsMixin, Generic[DataT]

Apply taylor series extrapolation.

Attributes:

data

Data object

derivatives

Derivatives object

minus_log

Whether to apply X <- -log(X).

alpha_name

Name of alpha

Methods:

__call__(*args, **kwargs)

Call self as a function.

predict(alpha, *[, order, order_dim, ...])

Calculate taylor series at values "alpha".

resample(sampler, **kws)

Create new object with resampled data.

data#

Data object

derivatives#

Derivatives object

minus_log#

Whether to apply X <- -log(X).

alpha_name#

Name of alpha

__call__(*args, **kwargs)[source]#

Call self as a function.

predict(alpha, *, order=None, order_dim='order', cumsum=False, no_sum=False, minus_log=None, alpha_name=None, dalpha_coords='dalpha', alpha0_coords=True)[source]#

Calculate taylor series at values “alpha”.

Parameters:
  • alpha (float or sequence of DataArray) – Value of alpha to evaluate expansion at.

  • order (int, optional) – Optional order to perform expansion to.

  • order_dim (str, default "order") – Name of dimension for new order dimension, if created.

  • cumsum (bool, default False) – If True, perform a cumsum on output for all orders. Otherwise, to total sum.

  • no_sum (bool, default False) – If True, do not sum the results. Useful if manually performing any math with series.

  • minus_log (bool, default False) – If True, transform expansion to Y = - log(X).

  • alpha_name (str, optional) – Name to apply to created alpha dimension.

  • dalpha_coords (str, default "dalpha") – Name of coordinate dalpha = alpha - alpha0.

  • alpha0_coords (str or bool, default True) – If True, add alpha_name + “0” to the coordinates of the results. If str, use this as the alpha0 coordinate names.

Returns:

output (DataArray or Dataset)

resample(sampler, **kws)[source]#

Create new object with resampled data.

class thermoextrap.models.StateCollection(states, *, kws=NOTHING)[source]#

Bases: MyAttrsMixin, Sequence[SupportsModelDataT_co], Generic[DataT, SupportsModelDataT_co]

Sequence of models.

Parameters:
  • states (list) – list of states to consider Note that some subclasses require this list to be sorted

  • kws (Mapping, optional) – additional key word arguments to keep internally in self.kws

Methods:

predict(alpha, **kwargs)

Prediction routine.

coefs([order])

Coefficients.

resample(sampler, **kws)

Resample underlying models.

map(func, *args, **kwargs)

Apply a function to elements self.

map_concat(func, *args[, concat_dim, concat_kws])

Apply function and concat output.

append(states[, sort, key])

Create new object with states appended to self.states.

predict(alpha, **kwargs)[source]#

Prediction routine.

To be implemented by subclasses.

coefs(order=None, **kwargs)[source]#

Coefficients.

To be implemented by subclass

resample(sampler, **kws)[source]#

Resample underlying models.

If pass in a single sampler, use it for all states. For example, to resample all states with some nrep, use .resample(sampler={"nrep": nrep}). Note that the if you pass a single mapping, the mapping will be passed to each state resample method, which will in turn create unique sample for each state. To specify a different sampler for each state, pass in a sequence of sampler.

map(func, *args, **kwargs)[source]#

Apply a function to elements self. out = [func(s, *args, **kwargs) for s in self].

map_concat(func, *args, concat_dim=None, concat_kws=None, **kwargs)[source]#

Apply function and concat output.

defaults to concat with dim=pd.Index(self.alpha0, name=self.alpha_name)

append(states, sort=True, key=None, **kws)[source]#

Create new object with states appended to self.states.

Parameters:
  • states (list) – states to append to self.states

  • sort (bool, default True) – if true, sort states by key alpha0

  • key (callable(), optional) – callable function to use as key if sort=True. Default is lambda x: x.alpha0 see sorted function

  • kws (dict) – extra arguments to sorted

Returns:

out (object) – same type as self with new states added to states list

thermoextrap.models.xr_weights_minkowski(deltas, m=20, dim='state')[source]#

Minkowski weights DataArray.

class thermoextrap.models.PiecewiseStateCollection(states, *, kws=NOTHING)[source]#

Bases: StateCollection[DataT, SupportsModelDataT]

Provide methods for Piecewise state collection.

class thermoextrap.models.ExtrapWeightedModel(states, *, kws=NOTHING)[source]#

Bases: PiecewiseStateCollection[DataT, ExtrapModel[DataT]], Generic[DataT]

Weighted extrapolation model.

Parameters:
  • states (sequence of ExtrapModel) – Extrap models to consider.

  • kws (Mapping, optional) – additional key word arguments to keep internally in self.kws

Methods:

predict(alpha, *[, order, order_dim, ...])

predict(alpha, *, order=None, order_dim='order', cumsum=False, minus_log=None, alpha_name=None, method=None, bounded=False, **kwargs)[source]#
Parameters:

method ({None, 'between', 'nearest'}) – method to select which models are chosen to predict value for given value of alpha.

  • None or between: use states such that state[i].alpha0 <= alpha < states[i+1] if alpha < state[0].alpha0 use first two states if alpha > states[-1].alpha0 use last two states

  • nearest: use two states with minimum abs(state[k].alpha0 - alpha)

Notes

This requires that states are ordered in ascending alpha0 order

class thermoextrap.models.InterpModel(states, *, kws=NOTHING)[source]#

Bases: StateCollection[DataT, SupportsModelDerivsDataT]

Interpolation model.

Parameters:
  • states (list) – list of states to consider Note that some subclasses require this list to be sorted

  • kws (Mapping, optional) – additional key word arguments to keep internally in self.kws

Methods:

coefs([order, order_dim, minus_log])

Coefficients.

predict(alpha, *[, order, order_dim, ...])

Prediction routine.

coefs(order=None, *, order_dim='porder', minus_log=None, **kwargs)[source]#

Coefficients.

To be implemented by subclass

predict(alpha, *, order=None, order_dim='porder', minus_log=False, alpha_name=None, **kwargs)[source]#

Prediction routine.

To be implemented by subclasses.

class thermoextrap.models.InterpModelPiecewise(states, *, kws=NOTHING)[source]#

Bases: PiecewiseStateCollection[DataT, SupportsModelDerivsDataT]

Apposed to the multiple model InterpModel, perform a piecewise interpolation.

Parameters:
  • states (list) – list of states to consider Note that some subclasses require this list to be sorted

  • kws (Mapping, optional) – additional key word arguments to keep internally in self.kws

Methods:

predict(alpha, *[, order, order_dim, ...])

predict(alpha, *, order=None, order_dim='porder', minus_log=False, alpha_name=None, method=None, bounded=False, **kwargs)[source]#
Parameters:

alpha (float or sequence of float)

class thermoextrap.models.PerturbModel(alpha0, data, alpha_name='alpha')[source]#

Bases: MyAttrsMixin, Generic[DataT]

Perturbation model.

class thermoextrap.models.MBARModel(states, *, kws=NOTHING)[source]#

Bases: StateCollection[DataArray, Any]

Sadly, this doesn’t work as beautifully.

Parameters:
  • states (list) – list of states to consider Note that some subclasses require this list to be sorted

  • kws (Mapping, optional) – additional key word arguments to keep internally in self.kws

Methods:

predict(alpha, *[, alpha_name])

Prediction routine.

resample(*args, **kwargs)

Resample underlying models.

predict(alpha, *, alpha_name=None, **kwargs)[source]#

Prediction routine.

To be implemented by subclasses.

resample(*args, **kwargs)[source]#

Resample underlying models.

If pass in a single sampler, use it for all states. For example, to resample all states with some nrep, use .resample(sampler={"nrep": nrep}). Note that the if you pass a single mapping, the mapping will be passed to each state resample method, which will in turn create unique sample for each state. To specify a different sampler for each state, pass in a sequence of sampler.