Grid

class hcipy.field.Grid(coords, weights=None)

Bases: object

A set of points on some coordinate system.

Parameters
coordsCoords

The actual definition of the coordinate values.

weightsarray_like or None

The interval size, area, volume or hypervolume of each point, depending on the number of dimensions. If this is None (default), the weights will be attempted to be calculated on the fly when needed.

Attributes
coords

The coordinate values for each dimension.

Attributes Summary

delta

The spacing between points in regularly-spaced grid.

dims

The number of elements in each dimension for a separated grid.

is_regular

True if the grid is regularly-spaced, False otherwise.

is_separated

True if the grid is separated, False otherwise.

is_unstructured

True if the grid is unstructured, False otherwise.

ndim

The number of dimensions.

points

A list of points of this grid.

regular_coords

The tuple (delta, dims, zero) for a regularly-spaced grid.

separated_coords

A list of coordinates for each dimension in a separated grid.

shape

The shape of a reshaped numpy.ndarray using this grid.

size

The number of points in this grid.

weights

The interval size, area, volume or hypervolume of each point, depending on the number of dimensions.

zero

The zero point of a regularly-spaced grid.

Methods Summary

as_(system)

Convert the grid to the new coordinate system system.

closest_to(p)

Get the index of the point closest to point p.

copy()

Create a copy.

empty([tensor_shape, dtype])

Create an empty Field from this Grid.

from_dict(tree)

Make a Grid from a dictionary, previously created by to_dict().

is_(system)

Check if the coordinate system is system.

ones([tensor_shape, dtype])

Create a field of ones from this Grid.

reverse()

Reverse the order of the points in-place.

reversed()

Make a copy of the grid with the order of the points reversed.

rotate(angle[, axis])

Rotate the grid in-place.

rotated(angle[, axis])

A rotated copy of this grid.

scale(scale)

Scale the grid in-place.

scaled(scale)

A scaled copy of this grid.

shift(shift)

Shift the grid in-place.

shifted(shift)

A shifted copy of this grid.

subset(criterium)

Construct a subset of the current sampling, based on criterium.

to_dict()

Convert the object to a dictionary for serialization.

zeros([tensor_shape, dtype])

Create a field of zeros from this Grid.

Attributes Documentation

delta

The spacing between points in regularly-spaced grid.

Raises
ValueError

If the grid is not regular.

dims

The number of elements in each dimension for a separated grid.

Raises
ValueError

If the grid is not separated.

is_regular

True if the grid is regularly-spaced, False otherwise.

is_separated

True if the grid is separated, False otherwise.

is_unstructured

True if the grid is unstructured, False otherwise.

ndim

The number of dimensions.

points

A list of points of this grid.

This can be used for easier iteration:

for p in grid.points:
        print(p)
regular_coords

The tuple (delta, dims, zero) for a regularly-spaced grid.

Raises
ValueError

If the grid is not regular.

separated_coords

A list of coordinates for each dimension in a separated grid.

Raises
ValueError

If the grid is not separated.

shape

The shape of a reshaped numpy.ndarray using this grid.

Raises
ValueError

If the grid is not separated.

size

The number of points in this grid.

weights

The interval size, area, volume or hypervolume of each point, depending on the number of dimensions.

The weights are attempted to be calculated on the fly if not set. If this fails, a warning is emitted and all points will be given an equal weight of one.

zero

The zero point of a regularly-spaced grid.

Raises
ValueError

If the grid is not regular.

Methods Documentation

as_(system)

Convert the grid to the new coordinate system system.

If the grid is already in the right coordinate system, this function doesn’t do anything.

Parameters
systemstr

The name of the coordinate system to check for.

Returns
Grid

A new Grid in the required coordinate system.

Raises
ValueError

If the conversion to the coordinate system system isn’t known.

closest_to(p)

Get the index of the point closest to point p.

Point p is assumed to have the same coordinate system as the grid itself.

Parameters
parray_like

The point at which to search for.

Returns
int

The index of the closest point.

copy()

Create a copy.

empty(tensor_shape=None, dtype=None)

Create an empty Field from this Grid.

Parameters
tensor_shapearray_like or None

The shape of the tensors in the to be created field. If this is None, a scalar field will be created.

dtypedata-type

The numpy data-type with which to create the field.

Returns
Field

A empty field.

classmethod from_dict(tree)

Make a Grid from a dictionary, previously created by to_dict().

Parameters
treedictionary

The dictionary from which to make a new Grid object.

Returns
Grid

The created object.

Raises
ValueError

If the dictionary is not formatted correctly.

is_(system)

Check if the coordinate system is system.

Parameters
systemstr

The name of the coordinate system to check for.

Returns
bool

If the coordinate system of the grid is equal to system.

ones(tensor_shape=None, dtype=None)

Create a field of ones from this Grid.

Parameters
tensor_shapearray_like or None

The shape of the tensors in the to be created field. If this is None, a scalar field will be created.

dtypedata-type

The numpy data-type with which to create the field.

Returns
Field

A ones field.

reverse()

Reverse the order of the points in-place.

Returns
Grid

Itself to allow for chaining these transformations.

reversed()

Make a copy of the grid with the order of the points reversed.

Returns
Grid

The reversed grid.

rotate(angle, axis=None)

Rotate the grid in-place.

Parameters
anglescalar

The angle in radians.

axisndarray or None

The axis of rotation. For two-dimensional grids, it is ignored. For three-dimensional grids it is required.

Returns
Grid

Itself to allow for chaining these transformations.

rotated(angle, axis=None)

A rotated copy of this grid.

Parameters
anglescalar

The angle in radians.

axisndarray or None

The axis of rotation. For two-dimensional grids, it is ignored. For three-dimensional grids it is required.

Returns
Grid

The rotated grid.

scale(scale)

Scale the grid in-place.

Parameters
scalearray_like

The factor with which to scale the grid.

Returns
Grid

Itself to allow for chaining these transformations.

scaled(scale)

A scaled copy of this grid.

Parameters
scalearray_like

The factor with which to scale the grid.

Returns
Grid

The scaled grid.

shift(shift)

Shift the grid in-place.

Parameters
shiftarray_like

The amount with which to shift the grid.

Returns
Grid

Itself to allow for chaining these transformations.

shifted(shift)

A shifted copy of this grid.

Parameters
shiftarray_like

The amount with which to shift the grid.

Returns
Grid

The scaled grid.

subset(criterium)

Construct a subset of the current sampling, based on criterium.

Parameters
criteriumfunction or array_like

The criterium used to select points. A function will be evaluated for every point. Otherwise, this must be a boolean array of integer array, used for slicing the points.

Returns
Grid

A new grid with UnstructuredCoords that includes only the points for which the criterium was true.

to_dict()

Convert the object to a dictionary for serialization.

Returns
dictionary

The created dictionary.

zeros(tensor_shape=None, dtype=None)

Create a field of zeros from this Grid.

Parameters
tensor_shapearray_like or None

The shape of the tensors in the to be created field. If this is None, a scalar field will be created.

dtypedata-type

The numpy data-type with which to create the field.

Returns
Field

A zeros field.