Analysis (erlab.analysis
)¶
Various functions for data analysis.
Modules¶
Utilities for curve fitting. |
|
Functions related to masking. |
|
Macros for correlation analysis. |
|
Fermi edge fitting. |
|
Various image processing functions including tools for visualizing dispersive features. |
|
Utilities for interpolation. |
|
Momentum conversion functions. |
|
Transformations. |
|
- erlab.analysis.correct_with_edge(darr, modelresult, shift_coords=True, plot=False, plot_kw=None, **shift_kwargs)[source]¶
Corrects the given data array
darr
with the given values or fit result.- Parameters:
darr (DataArray) – The input data array to be corrected.
modelresult (ModelResult | ndarray[Any, dtype[floating]] | Callable) – The model result that contains the fermi edge information. It can be an instance of
lmfit.model.ModelResult
, a numpy array containing the edge position at each angle, or a callable function that takes an array of angles and returns the corresponding energy value.shift_coords (bool) – If
True
, the coordinates of the output data will be changed so that the output contains all the values of the original data. IfFalse
, the coordinates and shape of the original data will be retained, and only the data will be shifted. Defaults toFalse
.plot (bool) – Whether to plot the original and corrected data arrays. Defaults to
False
.plot_kw (dict | None) – Additional keyword arguments for the plot. Defaults to
None
.**shift_kwargs – Additional keyword arguments to
erlab.analysis.utilities.shift
.
- Returns:
corrected – The edge corrected data.
- Return type:
- erlab.analysis.mask_with_hex_bz(kxymap, a=3.54, rotate=0.0, invert=False)[source]¶
Return map masked with a hexagonal BZ.
- erlab.analysis.polygon_mask(vertices, x, y, invert=False)[source]¶
Create a mask based on a polygon defined by its vertices.
- Parameters:
vertices (ndarray[Any, dtype[float64]]) – The vertices of the polygon. The shape should be (N, 2), where N is the number of vertices.
x (ndarray[Any, dtype[float64]]) – The x-coordinates of the grid points.
y (ndarray[Any, dtype[float64]]) – The y-coordinates of the grid points.
invert (bool) – If
True
, invert the mask (i.e., setTrue
where the polygon is outside andFalse
where it is inside). Default isFalse
.
- Returns:
mask – The mask array with shape
(len(x), len(y))
. The mask containsTrue
where the polygon is inside andFalse
where it is outside (or vice versa ifinvert
isTrue
).- Return type:
ndarray
Note
This function uses the
erlab.analysis.mask.polygon.bounded_side_bool
to determine whether a point is inside or outside the polygon.Example
>>> vertices = np.array([[0.2, 0.2], [0.2, 0.8], [0.8, 0.8], [0.8, 0.2]]) >>> x = np.linspace(0, 1, 5) >>> y = np.linspace(0, 1, 5) >>> polygon_mask(vertices, x, y) array([[False, False, False, False, False], [False, True, True, True, False], [False, True, True, True, False], [False, True, True, True, False], [False, False, False, False, False]])
- erlab.analysis.polygon_mask_points(vertices, x, y, invert=False)[source]¶
Compute a mask indicating whether points are inside or outside a polygon.
- Parameters:
vertices (ndarray[Any, dtype[float64]]) – The vertices of the polygon. The shape should be (N, 2), where N is the number of vertices.
x (ndarray[Any, dtype[float64]]) – The x-coordinates of the points.
y (ndarray[Any, dtype[float64]]) – The y-coordinates of the points.
invert (bool) – If
True
, invert the mask (i.e., setTrue
where the polygon is outside andFalse
where it is inside). Default isFalse
.
- Returns:
mask – A boolean array of shape
(len(x),)
indicating whether each point is inside or outside the polygon.- Return type:
ndarray
- Raises:
ValueError – If the lengths of x and y are not equal.
Notes
This function uses the
erlab.analysis.mask.polygon.bounded_side_bool
to determine whether a point is inside or outside the polygon.
- erlab.analysis.shift(darr, shift, along, shift_coords=False, **shift_kwargs)[source]¶
Shifts the values of a DataArray along a single dimension.
The shift is applied using
scipy.ndimage.shift
with the specified keyword arguments. Linear interpolation is used by default.- Parameters:
darr (DataArray) – The array to shift.
shift (float | DataArray) – The amount of shift to be applied along the specified dimension. If
shift
is a DataArray, different shifts can be applied to different coordinates. The dimensions ofshift
must be a subset of the dimensions ofdarr
. For more information, see the note below. Ifshift
is afloat
, the same shift is applied to all values along dimensionalong
. This is equivalent to providing a 0-dimensional DataArray.along (str) – Name of the dimension along which the shift is applied.
shift_coords (bool) – If
True
, the coordinates of the output data will be changed so that the output contains all the values of the original data. IfFalse
, the coordinates and shape of the original data will be retained, and only the data will be shifted. Defaults toFalse
.**shift_kwargs – Additional keyword arguments passed onto
scipy.ndimage.shift
. Default values ofcval
andorder
are set tonp.nan
and1
respectively.
- Returns:
The shifted DataArray.
- Return type:
Note
All dimensions in
shift
must be a dimension indarr
.The
shift
array values are divided by the step size along thealong
dimension.NaN values in
shift
are treated as zero.
Example
>>> import xarray as xr >>> darr = xr.DataArray( ... np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).astype(float), dims=["x", "y"] ... ) >>> shift_arr = xr.DataArray([1, 0, 2], dims=["x"]) >>> shifted = erlab.analysis.utilities.shift(darr, shift_arr, along="y") >>> print(shifted) <xarray.DataArray (x: 3, y: 3)> Size: 72B nan 1.0 2.0 4.0 5.0 6.0 nan nan 7.0 Dimensions without coordinates: x, y
- erlab.analysis.slice_along_path(darr, vertices, step_size=None, dim_name='path', interp_kwargs=None, **vertices_kwargs)[source]¶
Interpolate a DataArray along a path defined by a sequence of vertices.
- Parameters:
darr (DataArray) – The data array to interpolate.
vertices (Mapping[Hashable, Sequence]) – Dictionary specifying the vertices of the path along which to interpolate the DataArray. The keys of the dictionary should correspond to the dimensions of the DataArray along which to interpolate.
step_size (float | None) – The step size to use for the interpolation. This determines the number of points along the path at which the data array will be interpolated. If None, the step size is determined automatically as the smallest step size of the coordinates along the dimensions of the vertices if all coordinates are evenly spaced. If there exists a dimension where the coordinates are not evenly spaced,
step_size
must be specified.dim_name (str) – The name of the new dimension that corresponds to the distance along the interpolated path. Default is “path”.
interp_kwargs (dict | None) – Additional keyword arguments passed to
xarray.DataArray.interp
.**vertices_kwargs – The keyword arguments form of
vertices
. One ofvertices
orvertices_kwargs
must be provided.
- Returns:
interpolated – New dataarray on the new coordinate.
- Return type:
DataArray
Examples
>>> import numpy as np >>> import xarray as xr >>> from erlab.analysis.interpolate import slice_along_path >>> x = np.linspace(0, 10, 11) >>> y = np.linspace(0, 10, 11) >>> z = np.linspace(0, 10, 11) >>> data = np.random.rand(11, 11, 11) >>> darr = xr.DataArray(data, coords={"x": x, "y": y, "z": z}, dims=["x", "y", "z"]) >>> vertices = {"x": [0, 5, 10], "y": [0, 5, 10], "z": [0, 5, 10]} >>> interp = slice_along_path(darr, vertices)
See also