ForwardGridder
This modules provides a class to create 3D numpy grids from scatter points using a selection of forward mapping functions.
Example usage
import numpy as np
# import this module
from themachinethatgoesping.gridding.forwardgridder import ForwardGridder
# test date
size=1000000
sx = np.random.random(size)*100
sy = np.random.random(size)*100
sz = np.random.random(size)*100
sv = np.random.random(size)*1
# create new grid (auto determine min/max x,y,z)
grd_res = 1
gridder = ForwardGridder.from_data(grd_res,sx,sy,sz)
#create 3D numpy array using block mean (fast)
ival, iweight = gridder.interpolate_block_mean(sx,sy,sz,sv)
#ival: sum per voxel
#iweight: num per voxel
#create 3D numpy array using weighted mean (slower)
ival, iweight = gridder.interpolate_weighted_mean(sx,sy,sz,sv)
#compuate total value
iavg = np.zeros(ival.shape)
iavg[ival > 0] = ival[iweight > 0] / iweight[iweight > 0]
Class: ForwardGridder
- class ForwardGridder(xres: float, yres: float, zres: float, min_x: float, max_x: float, min_y: float, max_y: float, min_z: float, max_z: float, xbase: float = 0.0, ybase: float = 0.0, zbase: float = 0.0)
Simple class to generate 3D grids (images) and interpolate xyz data onto a grid using simple forward mapping algorithms. (e.g. block mean, weighted mean interpolation)
- __init__(xres: float, yres: float, zres: float, min_x: float, max_x: float, min_y: float, max_y: float, min_z: float, max_z: float, xbase: float = 0.0, ybase: float = 0.0, zbase: float = 0.0)
Initialize forward gridder class using grid parameters.
- Parameters
xres (float) – x resolution of the grid
yres (float) – y resolution of the grid
zres (float) – z resolution of the grid
min_x (float) – smallest x value that must be contained within the grid
max_x (float) – largest x value that must be contained within the grid
min_y (float) – smallest y value that must be contained within the grid
max_y (float) – largest y value that must be contained within the grid
min_z (float) – smallest z value that must be contained within the grid
max_z (float) – largest z value that must be contained within the grid
xbase (float, optional) – x base position of the grid, by default 0.0
ybase (float, optional) – y base position of the grid, by default 0.0
zbase (float, optional) – z base position of the grid, by default 0.0
- classmethod from_data(res: float, sx: Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], sy: Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], sz: Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]])
Create gridder with resolution “res” xmin,xmax,ymin,ymax,zmin,zmax are determined to exactly contain the given data vectors (sx,sy,sz)
- Parameters
res (float) – x,y and z resolution of the grid
sx (ArrayLike) – array with x data
sy (ArrayLike) – array with y data
sz (ArrayLike) – array with z data
- Returns
ForwardGridder object
- Return type
- classmethod from_res(res: float, min_x: float, max_x: float, min_y: float, max_y: float, min_z: float, max_z: float)
Create gridder setting xres,yres and zres to “res”
- Parameters
res (float) – x,y and z resolution of the grid
min_x (float) – smallest x value that must be contained within the grid
max_x (float) – largest x value that must be contained within the grid
min_y (float) – smallest y value that must be contained within the grid
max_y (float) – smallest y value that must be contained within the grid
min_z (float) – smallest z value that must be contained within the grid
max_z (float) – smallest z value that must be contained within the grid
- Returns
ForwardGridder object
- Return type
- get_empty_grd_images() tuple
create empty num and sum grid images
- Returns
image_values: summed value for each grid position image_weights: weights for each grid position
- Return type
(image_values, image_weights)
- interpolate_block_mean(sx: Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], sy: Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], sz: Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], s_val: Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], image_values: Optional[ndarray] = None, image_weights: Optional[ndarray] = None, skip_invalid: bool = True) tuple
interpolate 3D points onto 3d images using block mean interpolation
- Parameters
sx (ArrayLike) – x values
sy (ArrayLike) – y values
sz (ArrayLike) – z values
s_val (ArrayLike) – amplitudes / volume backscattering coefficients
image_values (np.ndarray, optional) – Image with values. If None a new image will be created. Dimensions must fit the internal nx,ny,nz
image_weights (np.ndarray, optional) – Image with weights. If None a new image will be created. Dimensions must fit the internal nx,ny,nz
skip_invalid (bool, optional) – skip values that exceed border_xmin, _xmax, _ymin, _ymax, _zmin, _zmax. Otherwise throw exception by default True
- Returns
image_values, image_weights
- Return type
- interpolate_weighted_mean(sx: Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], sy: Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], sz: Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], s_val: Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], image_values: Optional[ndarray] = None, image_weights: Optional[ndarray] = None, skip_invalid: bool = True)
interpolate 3D points onto 3d images using weighted mean interpolation
- Parameters
sx (ArrayLike) – x values
sy (ArrayLike) – y values
sz (ArrayLike) – z values
s_val (ArrayLike) – amplitudes / volume backscattering coefficients
image_values (np.ndarray, optional) – Image with values. If None a new image will be created. Dimensions must fit the internal nx,ny,nz
image_weights (np.ndarray, optional) – Image with weights. If None a new image will be created. Dimensions must fit the internal nx,ny,nz
skip_invalid (bool, optional) – skip values that exceed border_xmin, _xmax, _ymin, _ymax, _zmin, _zmax. Otherwise throw exception by default True
- Returns
image_values, image_weights
- Return type
- static get_minmax(sx: Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], sy: Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], sz: Union[_SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]]) tuple
returns the min/max value of three lists (same size). Sometimes faster than separate numpy functions because it only loops once.
- Parameters
sx (ArrayLike) – 1D array with x positions (same size)
sy (ArrayLike) – 1D array with x positions (same size)
sz (ArrayLike) – 1D array with x positions (same size)
- Returns
with (xmin,xmax,ymin,ymax,zmin,zmax)
- Return type