erlab.interactive.imagetool.slicer

Helper functions for fast slicing xarray.DataArray objects.

Classes

ArraySlicer(xarray_obj)

Internal class used to slice a xarray.DataArray rapidly.

class erlab.interactive.imagetool.slicer.ArraySlicer(xarray_obj)[source]

Bases: QObject

Internal class used to slice a xarray.DataArray rapidly.

Computes binned line and image profiles from multiple cursors. Also handles the data indices and the number of bins for each cursor. Automatic conversion of non-uniform dimensions are also handled here.

Parameters:

xarray_obj (xr.DataArray) – A xarray.DataArray with up to 4 dimensions.

Signals:
  • sigIndexChanged(int, tuple)

  • sigBinChanged(int, tuple)

  • sigCursorCountChanged(int)

  • sigShapeChanged()

Note

The original intent of this class was a xarray accessor. This is why ArraySlicer does not depend on a ImageSlicerArea but rather on the underlying xarray.DataArray. Originally, when loading a different array, a different instance of ArraySlicer had to be created. This was a terrible design choice since it messed up signals every time the instance was replaced. Hence, the behaviour was modified (23/06/19) so that the underlying xarray.DataArray of ArraySlicer could be swapped. As a consequence, each instance of ImageSlicerArea now corresponds to exactly one instance of ArraySlicer, regardless of the data. In the future, ArraySlicer might be changed so that it relies on its one-to-one correspondence with ImageSlicerArea for the signals.

add_cursor(like_cursor=-1, update=True)[source]
array_rect(i=None, j=None)[source]
center_cursor(cursor, update=True)[source]
clear_cache()[source]
clear_dim_cache(include_vals=False)[source]
clear_val_cache(include_vals=False)[source]
extract_avg_slice(cursor, axis)[source]
get_binned(cursor)[source]
get_bins(cursor)[source]
get_index(cursor, axis)[source]
get_indices(cursor)[source]
get_value(cursor, axis, uniform=False)[source]
get_values(cursor, uniform=False)[source]
index_of_value(axis, value, uniform=False)[source]
isel_args(cursor, disp, int_if_one=False)[source]
isel_code(cursor, disp)[source]
point_value(cursor, binned=True)[source]
qsel_args(cursor, disp)[source]
qsel_code(cursor, disp)[source]
remove_cursor(index, update=True)[source]
reset_property_cache(propname)[source]
set_array(xarray_obj, validate=True, reset=False)[source]
set_bin(cursor, axis, value, update=True)[source]
set_bins(cursor, value, update=True)[source]
set_index(cursor, axis, value, update=True)[source]
set_indices(cursor, value, update=True)[source]
set_value(cursor, axis, value, update=True, uniform=False)[source]
set_values(cursor, value, update=True)[source]
slice_with_coord(cursor, disp)[source]
span_bounds(cursor, axis)[source]
step_index(cursor, axis, value, update=True)[source]
swap_axes(ax1, ax2)[source]
static validate_array(data)[source]

Validate a given xarray.DataArray.

If data has two momentum axes (kx and ky), set them (and eV if exists) as the first two (or three) dimensions. Then, checks the data for non-uniform coordinates, which are converted to indices. Finally, converts the coordinates to C-contiguous float32. If input data values neither float32 nor float64, a conversion to float64 is attempted.

Parameters:

data (xarray.DataArray) – Input array with at least two dimensions.

Returns:

The converted data.

Return type:

xarray.DataArray

value_of_index(axis, value, uniform=False)[source]
values_of_dim(dim)[source]

Fast equivalent of self._obj[dim].values.

Returns the cached pointer of the underlying coordinate array, achieving a ~80x speedup. This should work most of the time since we only assume floating point values, but does require further testing. May break for future versions of pandas or xarray. See Notes.

Parameters:

dim (Hashable) – Name of the dimension to get the values from.

Return type:

numpy.ndarray

Notes

Looking at the implementation, I think this may return a pandas array in some cases, but I’m not sure so I’ll just leave it this way. When something breaks, replacing with self._obj._coords[dim]._data.array.array._ndarray may do the trick.

xslice(cursor, disp)[source]
property absnanmax: float
property absnanmin: float
property coords: tuple[ndarray[Any, dtype[float32]], ...]
property coords_uniform: tuple[ndarray[Any, dtype[float32]], ...]
property data_vals_T: npt.NDArray[np.floating]
property incs: tuple[float32, ...]
property incs_uniform: tuple[float32, ...]
property limits: tuple[float, float]

Return the global minima and maxima of the data.

property lims: tuple[tuple[float32, float32], ...]
property lims_uniform: tuple[tuple[float32, float32], ...]
property n_cursors: int

The number of cursors.

property nanmax: float
property nanmin: float