shnitsel.analyze.generic#

Attributes#

Functions#

norm(da[, dim, keep_attrs])

Calculate the 2-norm of a DataArray, reducing/squeezing the dimension with name dim

center(da[, dim, keep_attrs])

Subtract the mean of a DataArray along a specified dimension.

subtract_combinations(da, dim[, add_labels])

Calculate all possible pairwise differences over a given dimension

keep_norming(da[, exclude])

"Function to calculate the norm of a variable across all dimensions except the ones denoted in exclude

replace_total(da, to_replace, value)

Replaces each occurence of to_replace in da with the corresponding element of value.

relativize(da, **sel)

Subtract the minimum of an xr.DataArray from all the array's elements

pwdists(atXYZ_source[, center_mean])

Compute pairwise distances and standardize it by removing the mean

Module Contents#

norm(da, dim='direction', keep_attrs=None)#

Calculate the 2-norm of a DataArray, reducing/squeezing the dimension with name dim

Parameters:
  • da (shnitsel.core.typedefs.DataArrayOrVar) – Array to calculate the norm of

  • dim (shnitsel.core.typedefs.DimName) – Dimension to calculate norm along (and therby reduce), by default ‘direction’

  • optional – Dimension to calculate norm along (and therby reduce), by default ‘direction’

  • keep_attrs (bool | str | None) – How to deal with attributes; passed to xr.apply_ufunc, by default None

  • optional – How to deal with attributes; passed to xr.apply_ufunc, by default None

Return type:

A DataArray with dimension dim reduced

center(da, dim='frame', keep_attrs=None)#

Subtract the mean of a DataArray along a specified dimension.

Parameters:
  • da (DataArray) – Input array to be centered.

  • dim (str, optional) – Dimension along which to compute the mean, by default ‘frame’.

  • keep_attrs (bool or str or None, optional) – How to handle attributes; passed to xr.apply_ufunc, by default None.

Returns:

Centered DataArray with the same dimensions as input.

Return type:

DataArray

subtract_combinations(da, dim, add_labels=False)#

Calculate all possible pairwise differences over a given dimension

Parameters:
  • da (xr.DataArray) – Input DataArray; must contain dimension dim with an associated coordinate.

  • dim (Dimname) – Dimension (of size $n$) to take pairwise differences over.

  • add_labels (bool, optional) – If True, label the pairwise differences based on the index of dim otherwise do not add labels, by default False.

Return type:

A DataArray with the dimension dim replaced by a dimension ‘`dim`comb’ of size $n(n-1)/2$

keep_norming(da, exclude=None)#

“Function to calculate the norm of a variable across all dimensions except the ones denoted in exclude

Used to obtain scalar representations of vector-values observables for plotting and other calculations where only the magnitude of the vector is of relevance.

Parameters:
  • da (xr.DataArray) – The data array to norm across all non-excluded dimensions

  • exclude (Collection[DimName] | None, optional) – The dimensions to exclude/retain. Defaults to [‘state’, ‘statecomb’, ‘frame’, ‘time’].

Returns:

The resulting, normed array

Return type:

xr.DataArray

Notes

The output of keep_norming is not necessarily >= 0; for example, if all dimensions in da are in exclude, the original object possibly containing negative values will be returned unaltered.

replace_total(da, to_replace, value)#

Replaces each occurence of to_replace in da with the corresponding element of value. Replacement must be total, i.e. every element of da must be in to_replace. This permits a change of dtype between to_replace and value. This function is based on the snippets at pydata/xarray#6377

Parameters:
  • da (xr.DataArray) – An xr.DataArray to replace values within

  • to_replace (np.ndarray | list) – Values to search for and replace – these should be sortable, i.e. each pair of elements must be comparable by <

  • value (np.ndarray | list) – Values with which to replace the found occurrences of to_replace – the dtype of this argument determines the dtype of the result

Return type:

An xr.DataArray with dtype matching value obtained from da by replacing occurrences of to_replace with the corresponding values in value

relativize(da, **sel)#

Subtract the minimum of an xr.DataArray from all the array’s elements

Parameters:
  • da (xr.DataArray) – The xr.DataArray from which to subtract the minimum

  • **sel – If keyword parameters are present, the reference minimum is picked from those elements that remain after running xarray.DataArray.sel() using the keyword parameters as arguments.

Return type:

The result of subtraction, with attrs intact.

pwdists(atXYZ_source, center_mean=False)#

Compute pairwise distances and standardize it by removing the mean and L2-normalization (if your features are vectors and you want magnitudes only, to lose directional info)

Parameters:
  • atXYZ (xr.DataArray | AtXYZ | xr.Datset | ShnitselDataset) – A DataArray containing the atomic positions; Alternatively a dataset, a trajectory or a frameset with the positional data to derive the pairwise distances from.

  • center_mean (bool, optional) – Subtract mean of calculated pairwise distances if True, by default False

  • atXYZ_source (shnitsel.core.typedefs.AtXYZ | xarray.Dataset | shnitsel.data.dataset_containers.shared.ShnitselDataset)

Returns:

  • A DataArray holding pairwise distance vectors with the same dimensions as

  • but with a descriptor dimension indexing the pairwise distances.

Return type:

xarray.DataArray

get_standardized_pairwise_dists#