darsia.Image#
- class Image(img, transformations=None, **kwargs)[source]#
Bases:
objectAn image with physical meaning.
Wraps a NumPy array together with its physical extent, acquisition time and coordinate system, so that regions, patches and measurements can be addressed in metres and seconds.
ScalarImageandOpticalImageare convenience specialisations.Initialise a physical space-time image.
Supports scalar and vector-valued 2d/3d/4d images, including single time slices and time series. The
scalarflag records whether the range is stored in an extra axis;seriesrecords whether a time axis is present.- Parameters:
- img
numpy.ndarray Data array with
space_dim + time_dim + range_dimaxes.- transformations
listofcallable(),optional Reduction and correction routines, applied in order.
- **kwargs
Options controlling the attributes below; the defaults target conventional optical images.
- img
- Attributes:
- dim
int Dimensionality of the physical space.
- scalarbool
Whether the data is scalar-valued (no extra range axis).
- seriesbool
Whether the array carries a time axis.
- indexing
str Axis indexing of the first
dimentries.- img
numpy.ndarray The (space-time) image array.
- date
list Absolute time of each slice.
- time
list Relative time of each slice.
- dim
Examples
data = np.zeros((Nx, Ny, Nz, Nt, Nd), dtype=float) image = darsia.Image(data, scalar=False, series=True, dim=3)
Methods
append(image[, offset])Append other image to current image.
astype(data_type)For scalar data types, change the data type of the data array.
copy()Copy constructor.
eval(point[, interpolation])Evaluate the image array at a given point location.
geometry()Generate (flat) geometry object corresponding to the image.
img_as(data_type)Change data type via skimage.
integral()Integrate the image over its spatial dimensions.
metadata()Return all metadata required to initiate an image via keyword arguments.
reset_origin([return_image])Reset origin and coordinatesystem.
Pick date of first image in a series as reference date.
resize(cx[, cy])roi(roi)Extraction of spatial subregion using a darsia.ROI object.
save(path[, verbose])Save image to file.
set_time([time])Setter for time array.
Return all metadata connected to dimensions of image and voxels.
show([title, duration, mode])Show image using matplotlib.pyplots or plotly built-in methods.
show_matplotlib([title, duration])Show the image using
matplotlib.pyplot.show_plain([title])Show image using matplotlib.pyplots in plain mode.
show_plotly([title, duration])Show the image using
plotly(often faster than matplotlib).slice(cut, axis)Extract of spatial slice.
subregion(roi)Extraction of spatial subregion.
time_interval(indices)Extraction of temporal subregion, only for space-time images.
time_slice(time_index)Extraction of single time slice.
to_vtk(path[, name])Save image to file in vtk format.
update_metadata([meta])Update metadata of image.
update_reference_time(reference)Update reference time.
Attributes
Physical coordinate system with equipped transformation from voxel to Cartesian space.
Physical domain.
Data type of the (current) image array.
Number of voxels in each dimension.
Cartesian coordinate of the corner opposite to origin.
Shape of the image array, incl.
Spatial resolution, i.e., number of voxels.
Size of each voxel in each direction, ordered as indexing.
Data array.
Original dtype at construction of the object.
Dimension of the spatial domain.
Indexing of each axis in context of matrix indexing (ijk) or Cartesian coordinates (xyz).
Dimension in the directions corresponding to the indexings.
Name of image, e.g., used to describe the origin.
Cartesian coordinates associated to the [0,0,0] voxel (after applying transformations), using Cartesian indexing.
Flag controlling whether the data array corresponds to a time series.
Dimensionality of the image in temporal sense.
Number of time points.
Time in datetime format.
Reference date (for defining relative time).
Relative time in scalar format (in seconds).
Flag controlling whether the data array is scalar, i.e., it does not use an extra axis to encode the range.
Dimensionality of the image in data sense.
Number of entries for each data entry.
- append(image, offset=None)[source]#
Append other image to current image. Makes in particular a non-space-time image to a space-time image.
- astype(data_type)[source]#
For scalar data types, change the data type of the data array. For Image data types, cast the entire image.
- eval(point, interpolation='nearest')[source]#
Evaluate the image array at a given point location.
- Parameters:
- point
Voxel,Coordinate,VoxelArray,orCoordinateArray Point(s) at which to evaluate the image. Coordinates are converted to voxels if necessary.
- interpolation
str Interpolation mode. Either
"nearest"(default, rounds to nearest voxel) or"linear"(bilinear/trilinear interpolation viascipy.interpolate.RegularGridInterpolator). Voxel inputs (Voxel/VoxelArray) always use nearest-neighbor regardless of this parameter.
- point
- Returns:
np.ndarrayImage value(s) at the given point(s). For a single point, returns a scalar or 1D array depending on whether the image is scalar or non-scalar. For multiple points, returns an array of corresponding values.
- Example::
import darsia
import numpy as np
arr = np.arange(12, dtype=float).reshape(3, 4)
image = darsia.ScalarImage(arr, dimensions=[1, 1], space_dim=2)
# Default nearest-voxel evaluation (existing behaviour)
image.eval(darsia.Coordinate([0.6, 0.5]))
# Bilinear interpolation at a sub-voxel coordinate
image.eval(darsia.Coordinate([0.625, 0.5]), interpolation=”linear”) # 8.0
- geometry()[source]#
Generate (flat) geometry object corresponding to the image.
- Returns:
darsia.GeometryGeometry object.
- integral()[source]#
Integrate the image over its spatial dimensions.
- Returns:
floatIntegral of the image over space.
- metadata()[source]#
Return all metadata required to initiate an image via keyword arguments.
- Returns:
dictMetadata with keys equal to all keywords arguments.
- roi(roi)[source]#
Extraction of spatial subregion using a darsia.ROI object.
- Parameters:
- roi
darsia.ROI Region of interest, defining a box in space.
- roi
- Returns:
ImageImage with restricted spatial domain.
- save(path, verbose=True)[source]#
Save image to file.
Store array and metadata in single file.
NOTE: Keywords are compatible with imread_from_npz.
- Parameters:
- path
Path Full path to image. Use ending “npz”.
- path
- shape_metadata()[source]#
Return all metadata connected to dimensions of image and voxels.
Useful to define darsia.Geometry from darsia.Image.
- Returns:
dictMetadata with keys to instantiate a Geometry.
- show(title='', duration=None, mode='matplotlib', **kwargs)[source]#
Show image using matplotlib.pyplots or plotly built-in methods. The latter often is faster.
- show_matplotlib(title='', duration=None, **kwargs)[source]#
Show the image using
matplotlib.pyplot.- Parameters:
- title
str Title in the displayed window.
- duration
int,optional Display duration in seconds.
- **kwargs
Additional display options:
threshold (float) threshold for displaying 3d images relative (bool) whether the threshold is relative view (str) "scatter" or "voxel" (3d only; "voxel" is slower) side_view (str) "scatter" or "voxel" (3d only) surpress_2d (bool) whether 2d images are displayed surpress_3d (bool) whether 3d images are displayed (default True, since slow) delay (bool) delay the display, to show several images at once
- title
- show_plain(title='', **kwargs)[source]#
Show image using matplotlib.pyplots in plain mode.
NOTE: Only applicable for 2d images, which are not scalar. The image array is plotted without any additional information and modifications.
- Parameters:
- title
str Title in the displayed window.
- **kwargs
Additional arguments passed to show_matplotlib or show_plotly.
- title
- show_plotly(title='', duration=None, **kwargs)[source]#
Show the image using
plotly(often faster than matplotlib).- Parameters:
- title
str Title in the displayed window.
- duration
int,optional Display duration in seconds.
- **kwargs
Additional display options:
threshold (float) threshold for displaying 3d images relative (bool) whether the threshold is relative view (str) "scatter" or "voxel" (3d only; "voxel" is slower) side_view (str) "scatter" or "voxel" (3d only) surpress_2d (bool) whether 2d images are displayed surpress_3d (bool) whether 3d images are displayed
- title
- subregion(roi)[source]#
Extraction of spatial subregion.
- Parameters:
- roi
tupleofslices,VoxelArray,orCoordinateArray Voxel intervals in all dimensions, or points in space, in Cartesian coordinates, uniquely defining a box, i.e., at least space_dim points. Type decides interpretation.
- roi
- Returns:
ImageImage with restricted spatial domain.
- time_interval(indices)[source]#
Extraction of temporal subregion, only for space-time images.
- Parameters:
- indices
slice Time interval in terms of indices.
- indices
- Returns:
ImageImage with restricted temporal domain.
- Raises:
ValueErrorIf image is not a time series.
ValueErrorIf indices is not a slice.
- to_vtk(path, name=None)[source]#
Save image to file in vtk format.
- Parameters:
- path
Path Full path to image, without ending.
- name
str,optional Name of the data. Defaults to None.
- path
- update_metadata(meta=None, **kwargs)[source]#
Update metadata of image.
- Parameters:
- meta
dict Metadata to be updated, with keys corresponding to self.metadata().
- **kwargs
Additional keyword arguments to be updated.
- meta
- update_reference_time(reference)[source]#
Update reference time. Modifies the relative time.
reference (datetime or float): reference date or relative reference time (in seconds)
- property coordinatesystem#
Physical coordinate system with equipped transformation from voxel to Cartesian space.
NOTE: The definition of CoordinateSystem implicitly requires several attributes to be defined. Therefore, we need to define the CoordinateSystem after defining the spatial attributes, also implicitly defined as properties.
- Returns:
CoordinateSystemPhysical coordinate system.
- date#
Time in datetime format.
- dimensions#
Dimension in the directions corresponding to the indexings.
- property domain#
Physical domain.
- Returns:
tupleCollection of coordinates in matrix indexing defining domain.
- property dtype#
Data type of the (current) image array.
- Returns:
np.dtypeData type of the image array.
- img#
Data array.
- indexing#
Indexing of each axis in context of matrix indexing (ijk) or Cartesian coordinates (xyz).
- name#
Name of image, e.g., used to describe the origin.
- property num_voxels#
Number of voxels in each dimension.
- Returns:
listNumber of voxels in each dimension.
- property opposite_corner#
Cartesian coordinate of the corner opposite to origin.
- Returns:
CoordinateCartesian coordinate of the corner opposite to origin.
- origin#
Cartesian coordinates associated to the [0,0,0] voxel (after applying transformations), using Cartesian indexing.
- original_dtype#
Original dtype at construction of the object.
- range_dim#
Dimensionality of the image in data sense.
- range_num#
Number of entries for each data entry.
- reference_date#
Reference date (for defining relative time).
- scalar#
Flag controlling whether the data array is scalar, i.e., it does not use an extra axis to encode the range.
- series#
Flag controlling whether the data array corresponds to a time series.
- property shape#
Shape of the image array, incl. time and data dimension.
- Returns:
tupleShape of the image array.
- space_dim#
Dimension of the spatial domain.
- time#
Relative time in scalar format (in seconds).
- time_dim#
Dimensionality of the image in temporal sense.
- time_num#
Number of time points.