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

ForwardGridder

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

ForwardGridder

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

tuple

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

tuple

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

tuple

get_x_index(x: float) int

get the x index of the grid cell that physically contains “x”

Parameters

x (float) –

Returns

x_index

Return type

int

get_y_index(y: float) int

get the y index of the grid cell that physically contains “x”

Parameters

y (float) –

Returns

y_index

Return type

int

get_z_index(z: float) int

get the y index of the grid cell that physically contains “z”

Parameters

z (float) –

Returns

z_index

Return type

int

get_x_index_fraction(x: float) float

get the fractional x index of “x” within the 3D grid image

Parameters

x (float) –

Returns

x_index

Return type

float

get_y_index_fraction(y: float) float

get the fractional y index of “y” within the 3D grid image

Parameters

y (float) –

Returns

y_index

Return type

float

get_z_index_fraction(z: float) float

get the fractional z index of “z” within the 3D grid image

Parameters

z (float) –

Returns

z_index

Return type

float

get_x_value(x_index: float) float

return the x value of the grid cell of index x_index

Parameters

x_index (int) –

Returns

x

Return type

float

get_y_value(y_index: int) float

return the y value of the grid cell of index y_index

Parameters

y_index (int) –

Returns

y

Return type

float

get_z_value(z_index: int) float

return the z value of the grid cell of index z_index

Parameters

z_index (int) –

Returns

z

Return type

float

get_x_grd_value(x: float) float

return the x value of the grid cell that contains x

Parameters

x (float) –

Returns

x_value of grid cell

Return type

float

get_y_grd_value(y: float) float

return the y value of the grid cell that contains y

Parameters

y (float) –

Returns

y_value of grid cell

Return type

float

get_z_grd_value(z: float) float

return the z value of the grid cell that contains z

Parameters

z (float) –

Returns

z_value of grid cell

Return type

float

get_extent_x() list

return x extend (useful for plotting)

get_extent_y() list

return y extend (useful for plotting)

get_extent_z() list

return z extend (useful for plotting)

get_x_coordinates() list

return valid x grid coordinates as list

get_y_coordinates() list

return valid y grid coordinates as list

get_z_coordinates() list

return valid z grid coordinates as list