BaseMeasurements#
- class abtem.measurements.BaseMeasurements(array, ensemble_axes_metadata, metadata)[source]#
Bases:
ArrayObject,EqualityMixin,CopyMixinBase class for all measurement types.
- Parameters:
array (ndarray) – Array containing data of type float or complex.
ensemble_axes_metadata (list of AxisMetadata, optional) – Metadata associated with an ensemble axis.
metadata (dict, optional) – A dictionary defining simulation metadata.
Methods
__init__(array, ensemble_axes_metadata, metadata)abs()Calculates the absolute value of a complex-valued measurement.
apply_func(func, **kwargs)Apply a function to the array object.
apply_transform(transform[, max_batch])Transform the wave functions by a given transformation.
compute([progress_bar, profiler, ...])Turn a lazy *ab*TEM object into its in-memory equivalent.
copy()Make a copy.
copy_to_device(device)Copy array to specified device.
ensemble_blocks([chunks])Split the ensemble into an array of smaller ensembles.
ensure_lazy([chunks])Creates an equivalent lazy version of the array object.
expand_dims([axis, axis_metadata])Expand the shape of the array object.
from_array_and_metadata(array, axes_metadata)Creates array object from a given array and metadata.
from_zarr(url[, chunks])Read wave functions from a hdf5 file.
generate_blocks([chunks])Generate chunks of the ensemble.
get_from_metadata(name[, broadcastable])get_items(items[, keepdims])Index the array and the corresponding axes metadata.
imag()Returns the imaginary part of a complex-valued measurement.
Calculates the squared norm of a complex-valued measurement.
lazy([chunks])max([axis, keepdims, split_every])Maximum of array object over one or more axes.
mean([axis, keepdims, split_every])Mean of array object over one or more axes.
min([axis, keepdims, split_every])Minmimum of array object over one or more axes.
Rechunk to remove chunks across the base dimensions.
normalize_ensemble([scale, shift])Normalize the ensemble by shifting ad scaling each member.
phase()Calculates the phase of a complex-valued measurement.
poisson_noise([dose_per_area, total_dose, ...])Add Poisson noise (i.e. shot noise) to a measurement corresponding to the provided 'total_dose' (per measurement if applied to an ensemble) or 'dose_per_area' (not applicable for single measurements).
real()Returns the real part of a complex-valued measurement.
rechunk(chunks, **kwargs)Rechunk dask array.
Calculates the mean of an ensemble measurement (e.g. of frozen phonon configurations).
relative_difference(other[, min_relative_tol])Calculates the relative difference with respect to another compatible measurement.
set_ensemble_axes_metadata(axes_metadata, axis)Sets the axes metadata of an ensemble axis.
show(*args, **kwargs)Documented in subclasses
squeeze([axis])Remove axes of length one from array object.
std([axis, keepdims, split_every])Standard deviation of array object over one or more axes.
sum([axis, keepdims, split_every])Sum of array object over one or more axes.
to_cpu()Move the array to the host memory from an arbitrary source array.
Convert ArrayObject to a xarray DataArray.
to_gpu([device])Move the array from the host memory to a gpu.
to_hyperspy([transpose])Convert ArrayObject to a Hyperspy signal.
to_measurement_ensemble()to_tiff(filename, **kwargs)Write data to a tiff file.
to_zarr(url[, compute, overwrite])Write data to a zarr file.
Attributes
Underlying array describing the array object.
List of AxisMetadata.
List of AxisMetadata of the base axes.
Number of base dimensions.
Shape of the base axes of the underlying array.
The device where the array is stored.
Datatype of array.
List of AxisMetadata of the ensemble axes.
Number of ensemble dimensions.
Shape of the ensemble axes of the underlying array.
True if array is complex.
True if array is lazy.
Metadata describing the measurement.
Shape of the underlying array.
- apply_func(func, **kwargs)#
Apply a function to the array object. The function must take an array as its first argument, only the array is modified, the metadata is not changed. The function is applied lazily if the array object is lazy.
- Parameters:
func (callable) – Function to apply to the array object.
kwargs – Additional keyword arguments passed to the function.
- Returns:
array_object – The array object with the function applied.
- Return type:
ArrayObject or subclass of ArrayObject
- apply_transform(transform, max_batch='auto')#
Transform the wave functions by a given transformation.
- Parameters:
transform (ArrayObjectTransform) – The array object transformation to apply.
max_batch (int, optional) – The number of wave functions in each chunk of the Dask array. If ‘auto’ (default), the batch size is automatically chosen based on the abtem user configuration settings “dask.chunk-size” and “dask.chunk-size-gpu”.
- Returns:
transformed_array_object – The transformed array object.
- Return type:
- property array: ndarray | Array#
Underlying array describing the array object.
- property axes_metadata: AxesMetadataList#
List of AxisMetadata.
- abstract property base_axes_metadata: list#
List of AxisMetadata of the base axes.
- property base_dims: int#
Number of base dimensions.
- property base_shape: tuple[int, ...]#
Shape of the base axes of the underlying array.
- compute(progress_bar=None, profiler=False, resource_profiler=False, **kwargs)#
Turn a lazy *ab*TEM object into its in-memory equivalent.
- Parameters:
progress_bar (bool) – Display a progress bar in the terminal or notebook during computation. The progress bar is only displayed with a local scheduler.
profiler (bool) – Return Profiler class used to profile Dask’s execution at the task level. Only execution with a local scheduler is profiled.
resource_profiler (bool) – Return ResourceProfiler class used to profile Dask’s execution at the resource level.
kwargs – Additional keyword arguments passed to dask.compute.
- Return type:
Union[Self,tuple[Self,tuple]]
- copy()#
Make a copy.
- Return type:
Self
- copy_to_device(device)#
Copy array to specified device.
- Parameters:
device (str)
- Returns:
object_on_device
- Return type:
- property device: str#
The device where the array is stored.
- property dtype: dtype#
Datatype of array.
- property ensemble_axes_metadata: list[AxisMetadata]#
List of AxisMetadata of the ensemble axes.
- ensemble_blocks(chunks=None)#
Split the ensemble into an array of smaller ensembles.
- Parameters:
chunks (iterable of tuples) – Block sizes along each dimension.
- Return type:
Array
- property ensemble_dims: int#
Number of ensemble dimensions.
- property ensemble_shape: tuple[int, ...]#
Shape of the ensemble axes of the underlying array.
- ensure_lazy(chunks='auto')#
Creates an equivalent lazy version of the array object.
- Parameters:
chunks (int or tuple or str) – How to chunk the array. See dask.array.from_array.
- Returns:
lazy_array_object – Lazy version of the array object.
- Return type:
ArrayObject or subclass of ArrayObject
- expand_dims(axis=None, axis_metadata=None)#
Expand the shape of the array object.
- Parameters:
axis (int or tuple of ints) – Position in the expanded axes where the new axis (or axes) is placed.
axis_metadata (AxisMetadata or List of AxisMetadata, optional) – The axis metadata describing the expanded axes. Default is UnknownAxis.
- Returns:
expanded – View of array object with the number of dimensions increased.
- Return type:
ArrayObject or subclass of ArrayObject
- abstract classmethod from_array_and_metadata(array, axes_metadata, metadata=None)[source]#
Creates array object from a given array and metadata.
- Parameters:
array (ndarray) – Array defining the array object.
axes_metadata (list of AxesMetadata) – Axis metadata for each axis. The axis metadata must be compatible with the shape of the array.
metadata (
Optional[dict]) – A dictionary defining the metadata of the array object.
- Returns:
array_object – The array object.
- Return type:
ArrayObject or subclass of ArrayObject
- classmethod from_zarr(url, chunks='auto')#
Read wave functions from a hdf5 file.
- Return type:
Self
- urlstr
Location of the data, typically a path to a local file. A URL can also include a protocol specifier like s3:// for remote data.
- chunkstuple of ints or tuples of ints
Passed to dask.array.from_array(), allows setting the chunks on initialisation, if the chunking scheme in the on-disc dataset is not optimal for the calculations to follow.
- generate_blocks(chunks=1)#
Generate chunks of the ensemble.
- Parameters:
chunks (iterable of tuples) – Block sizes along each dimension.
- Return type:
Generator[tuple[tuple[int,...],tuple[slice,...],ndarray],None,None]
- get_items(items, keepdims=False)#
Index the array and the corresponding axes metadata. Only ensemble axes can be indexed.
- Parameters:
items (int or tuple of int or slice) – The array is indexed according to this.
keepdims (bool, optional) – If True, all ensemble axes are left in the result as dimensions with size one. Default is False.
- Returns:
indexed_array – The indexed array object.
- Return type:
ArrayObject or subclass of ArrayObject
- property is_complex: bool#
True if array is complex.
- property is_lazy: bool#
True if array is lazy.
- max(axis=None, keepdims=False, split_every=2)#
Maximum of array object over one or more axes. Only ensemble axes can be reduced.
- Parameters:
axis (int or tuple of ints, optional) – Axis or axes along which a maxima are calculated. The default is to compute the mean of the flattened array. If this is a tuple of ints, the maxima are calculated over multiple axes. The indicated axes must be ensemble axes.
keepdims (bool, optional) – If True, the reduced axes are left in the result as dimensions with size one. Default is False.
split_every (int) – Only used for lazy arrays. See dask.array.reductions.
- Returns:
reduced_array – The reduced array object.
- Return type:
ArrayObject or subclass of ArrayObject
- mean(axis=None, keepdims=False, split_every=2)#
Mean of array object over one or more axes. Only ensemble axes can be reduced.
- Parameters:
axis (int or tuple of ints, optional) – Axis or axes along which a means are calculated. The default is to compute the mean of the flattened array. If this is a tuple of ints, the mean is calculated over multiple axes. The indicated axes must be ensemble axes.
keepdims (bool, optional) – If True, the reduced axes are left in the result as dimensions with size one. Default is False.
split_every (int) – Only used for lazy arrays. See dask.array.reductions.
- Returns:
reduced_array – The reduced array object.
- Return type:
ArrayObject or subclass of ArrayObject
- property metadata: dict#
Metadata describing the measurement.
- min(axis=None, keepdims=False, split_every=2)#
Minmimum of array object over one or more axes. Only ensemble axes can be reduced.
- Parameters:
axis (int or tuple of ints, optional) – Axis or axes along which a minima are calculated. The default is to compute the mean of the flattened array. If this is a tuple of ints, the minima are calculated over multiple axes. The indicated axes must be ensemble axes.
keepdims (bool, optional) – If True, the reduced axes are left in the result as dimensions with size one. Default is False.
split_every (int) – Only used for lazy arrays. See dask.array.reductions.
- Returns:
reduced_array – The reduced array object.
- Return type:
ArrayObject or subclass of ArrayObject
- no_base_chunks()#
Rechunk to remove chunks across the base dimensions.
- normalize_ensemble(scale='max', shift='mean')[source]#
Normalize the ensemble by shifting ad scaling each member.
- Parameters:
scale ({'max', 'min', 'sum', 'mean', 'ptp'})
shift ({'max', 'min', 'sum', 'mean', 'ptp'})
- Returns:
normalized_measurements
- Return type:
BaseMeasurements or subclass of _BaseMeasurement
- poisson_noise(dose_per_area=None, total_dose=None, samples=1, seed=None)[source]#
Add Poisson noise (i.e. shot noise) to a measurement corresponding to the provided ‘total_dose’ (per measurement if applied to an ensemble) or ‘dose_per_area’ (not applicable for single measurements).
- Parameters:
dose_per_area (float, sequence of float, optional) – The irradiation dose [electrons per Å:sup:2]. May be given as a single value or as a sequence of values for each ensemble member.
total_dose (float, optional) – The irradiation dose per diffraction pattern.
samples (int, optional) – The number of samples to draw from a Poisson distribution. If this is greater than 1, an additional ensemble axis will be added to the measurement.
seed (int, optional) – Seed the random number generator.
- Returns:
noisy_measurement – The noisy measurement.
- Return type:
BaseMeasurements or subclass of _BaseMeasurement
- rechunk(chunks, **kwargs)#
Rechunk dask array.
- Return type:
- chunksint or tuple or str
How to rechunk the array. See dask.array.rechunk.
- kwargs :
Additional keyword arguments passes to dask.array.rechunk.
- reduce_ensemble()[source]#
Calculates the mean of an ensemble measurement (e.g. of frozen phonon configurations).
- Return type:
Self
- relative_difference(other, min_relative_tol=0.0)[source]#
Calculates the relative difference with respect to another compatible measurement.
- Parameters:
other (BaseMeasurements) – Measurement to which the difference is calculated.
min_relative_tol (float) – Avoids division by zero errors by defining a minimum value of the divisor in the relative difference.
- Returns:
difference – The relative difference as a measurement of the same type.
- Return type:
- set_ensemble_axes_metadata(axes_metadata, axis)#
Sets the axes metadata of an ensemble axis.
- Parameters:
axes_metadata (AxisMetadata) – The new axis metadata.
axis (int) – The axis to set.
- Return type:
Self
- property shape: tuple[int, ...]#
Shape of the underlying array.
- squeeze(axis=None)#
Remove axes of length one from array object.
- Parameters:
axis (int or tuple of ints, optional) – Selects a subset of the entries of length one in the shape.
- Returns:
squeezed – The input array object, but with all or a subset of the dimensions of length 1 removed.
- Return type:
ArrayObject or subclass of ArrayObject
- std(axis=None, keepdims=False, split_every=2)#
Standard deviation of array object over one or more axes. Only ensemble axes can be reduced.
- Parameters:
axis (int or tuple of ints, optional) – Axis or axes along which a standard deviations are calculated. The default is to compute the mean of the flattened array. If this is a tuple of ints, the standard deviations are calculated over multiple axes. The indicated axes must be ensemble axes.
keepdims (bool, optional) – If True, the reduced axes are left in the result as dimensions with size one. Default is False.
split_every (int) – Only used for lazy arrays. See dask.array.reductions.
- Returns:
reduced_array – The reduced array object.
- Return type:
ArrayObject or subclass of ArrayObject
- sum(axis=None, keepdims=False, split_every=2)#
Sum of array object over one or more axes. Only ensemble axes can be reduced.
- Parameters:
axis (int or tuple of ints, optional) – Axis or axes along which a sums are performed. The default is to compute the mean of the flattened array. If this is a tuple of ints, the sum is performed over multiple axes. The indicated axes must be ensemble axes.
keepdims (bool, optional) – If True, the reduced axes are left in the result as dimensions with size one. Default is False.
split_every (int) – Only used for lazy arrays. See dask.array.reductions.
- Returns:
reduced_array – The reduced array object.
- Return type:
ArrayObject or subclass of ArrayObject
- to_cpu()#
Move the array to the host memory from an arbitrary source array.
- Return type:
Self
- to_data_array()#
Convert ArrayObject to a xarray DataArray. Requires xarray to be installed.
- Returns:
The converted xarray DataArray.
- Return type:
xarray.DataArray
- Raises:
ImportError – If xarray is not installed.
- to_gpu(device='gpu')#
Move the array from the host memory to a gpu.
- Return type:
Self
- to_hyperspy(transpose=True)#
Convert ArrayObject to a Hyperspy signal.
- Parameters:
transpose (bool, optional) – If True, transpose the base axes of the array before converting to a Hyperspy signal. Default is True.
- Returns:
signal – The converted Hyperspy signal.
- Return type:
Hyperspy signal
- Raises:
ImportError – If Hyperspy is not installed.
RuntimeError – If the number of base dimensions is not 1 or 2.
Notes
This method requires Hyperspy to be installed. You can find more information about Hyperspy at https://hyperspy.org.
- to_tiff(filename, **kwargs)#
Write data to a tiff file.
- Parameters:
filename (str) – The filename of the file to write.
kwargs – Keyword arguments passed to tifffile.imwrite.
- to_zarr(url, compute=True, overwrite=False, **kwargs)#
Write data to a zarr file.
- Parameters:
url (str) – Location of the data, typically a path to a local file. A URL can also include a protocol specifier like s3:// for remote data.
compute (bool) – If true compute immediately; return dask.delayed.Delayed otherwise.
overwrite (bool) – If given array already exists, overwrite=False will cause an error, where overwrite=True will replace the existing data.
kwargs – Keyword arguments passed to dask.array.to_zarr.