Python reference

Generated from the docstrings of the installed module, which are the ones help(meshsearch) prints. Every member runs the same code the C++ reference documents, and the guide explains how they work together.

The signatures below are also what a type checker reads: the package is typed, and the stub it installs is generated from them. Where a member takes a point, x, y and z are real numbers; for a batch they are one-dimensional sequences or arrays of real numbers. A float64 or integer numpy array is used as it is, anything else is converted by numpy first (in queries; the constructor reads float64 in place and converts other dtypes). Where a member takes objects, index is an integer and indices a one-dimensional sequence or array of integers of any dtype, never cast. An argument that has a length selects the batched form, and the types keep the two forms apart: a number is never taken for a batch, nor an array for a single query. Complex numbers and arrays of objects are not real numbers and raise TypeError. Arrays are returned as numpy arrays of the element type shown.

The types in the signatures

To keep the signatures readable, the types of the arguments are written with eight shorthands. They are defined in the stub the package installs, meshsearch/_core.pyi, and nowhere else: they are names of the signatures, which help() prints as well, and not objects to import. A type checker reads through them to the definitions given here. The other types are numpy’s own: numpy.typing.NDArray[numpy.uint32] is a numpy array of uint32.

type _Real = float | numpy.floating | numpy.integer

A real number: a Python float or int, or a numpy floating or integer number.

type _Int = int | numpy.integer

An integer, for an index or a count: a Python int or a numpy integer.

type _RealArray = Sequence[_Real] | numpy.typing.NDArray[numpy.floating | numpy.integer]

Real numbers for a batch of queries: a sequence of them, a list or a tuple for instance, or a numpy array of a floating or integer dtype; one-dimensional.

type _IntArray = Sequence[_Int] | numpy.typing.NDArray[numpy.integer]

Integers for a batch of indices: a sequence of them, or a numpy array of an integer dtype; one-dimensional.

type _Column = Sequence[_Real] | numpy.typing.NDArray[numpy.floating | numpy.integer | numpy.bool_]

One coordinate of every point, for the constructor: as _RealArray, and a numpy array of bools as well.

type _Rows = Sequence[Sequence[_Real]] | numpy.typing.NDArray[numpy.floating | numpy.integer | numpy.bool_]

The points as rows (x, y, z), for the constructor: a sequence of triples of real numbers, or a numpy array of shape (N, 3) of a floating, integer or bool dtype.

type _Limits = Sequence[Sequence[_Real]] | numpy.typing.NDArray[numpy.floating | numpy.integer]

The box: three pairs (lo, hi) of real numbers, as nested sequences or as a numpy array of shape (3, 2).

type _Periodic = bool | numpy.bool_ | Sequence[bool | numpy.bool_] | numpy.typing.NDArray[numpy.bool_]

One bool for all three axes, or three bools as a sequence or a numpy array.

The grid

class meshsearch.MeshGrid(*args, **kwargs)

Uniform cubic mesh over a set of points, for neighbour queries.

Objects are identified by their position in the coordinate arrays, or by their row in pos. Indices are stable: remove_object leaves an index permanently invalid, add_object issues a new one, and indices are never reused. Every member taking an index raises meshsearch.IndexError on one that is negative, out of range or removed, except is_alive, which answers False.

Several threads may query one grid at the same time; add_object and remove_object require that no other thread use it.

Batched queries take any array-like for the coordinates: a float64 or integer array is used as it is, and anything else, a list or a float32 array for instance, is converted by numpy first.

A grid can be copied with copy.copy and copy.deepcopy; the copy is independent and answers every query as the original does. Grids are not picklable: for parallel work use threads, which share one grid, or rebuild the grid in each process from the coordinates.

__copy__(self) → meshsearch.MeshGrid

A copy of the grid, independent of it and answering every query as it does.

__deepcopy__(self, memo: object) → meshsearch.MeshGrid

A copy of the grid, independent of it and answering every query as it does.

__init__(self, x: _Column, y: _Column, z: _Column, cellsize: _Real, *, limits: _Limits | None = None, periodic: _Periodic = False) → None
__init__(self, pos: _Rows, cellsize: _Real, *, limits: _Limits | None = None, periodic: _Periodic = False) → None

Overloaded function.

  1. __init__(self, x: _Column, y: _Column, z: _Column, cellsize: _Real, *, limits: _Limits | None = None, periodic: _Periodic = False) -> None

Build the grid over the points x, y, z, one-dimensional arrays of equal length; object i is the point (x[i], y[i], z[i]). The grid copies the coordinates and keeps no reference to the arrays. A float64 C-contiguous array is read where it is; any other array-like of real numbers, a float32 array, a strided column or a list for instance, is converted first, which costs a temporary copy. For points held as rows, use MeshGrid(pos, cellsize) instead.

Coordinates, cellsize and every radius must be in the same unit. cellsize is the cell side, the one tuning parameter: for uniform data, start between 1.5 and 2.5 mean separations, the mean separation being (volume / number of points) ** (1/3); the guide discusses other choices.

limits, keyword-only, is the box ((xlo,xhi),(ylo,yhi),(zlo,zhi)), and must contain every point. When omitted, the box is fitted to the points with a small margin, and a query point outside their range may raise meshsearch.Error: give limits whenever queries can fall outside the data. Each non-periodic side of the box is grown symmetrically to a whole number of cells and always contains the limits; an axis of zero extent gets one cell.

periodic, keyword-only, is a bool for all three axes or a triple of bools; the default is False. A periodic axis requires limits, and its box is exactly them, with period L = hi - lo. Distances along it are minimum-image distances, the displacement taken in [-L/2, L/2), so each object is seen once and any radius is valid, +inf included. Coordinates are not wrapped. The periods must be integer multiples of the shortest one, within a relative 1e-12. All cells share one side, never larger than cellsize; the cellsize property gives it.

Raises meshsearch.Error if the arrays differ in length or hold more than 4294967295 points; if a coordinate is not finite or lies outside limits; if cellsize is not finite and positive, or so small that 1/cellsize overflows; if limits is not 3x2 (a flat sequence of numbers is a TypeError), not finite, or has lo > hi; if there are no points and no limits; if the box is so large that its squared diagonal overflows, or the grid would need more than 2147483647 cells along an axis; and, for a periodic axis, if limits is missing, the period is below 2**-1000 or the limits too large to shift by it, or the period is not an integer multiple of the shortest. TypeError if periodic is neither a bool nor three bools, or the coordinates are not arrays of real numbers, or cellsize or limits are not real numbers, complex numbers included; meshsearch.Error if the coordinates are not one-dimensional. MemoryError if the grid cannot be allocated.

  1. __init__(self, pos: _Rows, cellsize: _Real, *, limits: _Limits | None = None, periodic: _Periodic = False) -> None

Build the grid over the points pos, an array of shape (N, 3): object i is the point pos[i] = (x, y, z). The grid copies the coordinates and keeps no reference to the array, and is the grid MeshGrid(pos[:, 0], pos[:, 1], pos[:, 2], cellsize) builds, with the same results in the same order. A float64 C-contiguous array, the usual layout of an (N, 3) numpy array, is read where it is; any other array-like of real numbers of that shape, a float32 or a Fortran-ordered array or a list of triples for instance, is converted first, which costs a temporary copy.

cellsize, limits and periodic are those of MeshGrid(x, y, z, cellsize), and so are the errors, except that the coordinates cannot differ in length: TypeError if pos is not an array of real numbers, meshsearch.Error if it is not of shape (N, 3).

__len__(self) → int

Number of objects in the grid, excluding removed ones.

__repr__(self) → str

A short description: the object count, cells per axis, cell side and periodic axes.

add_object(self, x: _Real, y: _Real, z: _Real) → int

Add an object and return its index, one past the highest ever issued. The box is not refitted, and memory is not reclaimed: to add many objects, build a new grid. Must not run while another thread uses the grid.

Raises meshsearch.Error if the point is outside the box, or if 4294967295 indices have already been issued.

close_objects(self, x: _Real, y: _Real, z: _Real, rmax: _Real, rmin: _Real = 0.0) → numpy.typing.NDArray[numpy.uint32]
close_objects(self, index: _Int, rmax: _Real, rmin: _Real = 0.0) → numpy.typing.NDArray[numpy.uint32]
close_objects(self, x: _RealArray, y: _RealArray, z: _RealArray, rmax: _Real, rmin: _Real = 0.0, *, workers: int = 1) → meshsearch.Batch
close_objects(self, indices: _IntArray, rmax: _Real, rmin: _Real = 0.0, *, workers: int = 1) → meshsearch.Batch

Overloaded function.

  1. close_objects(self, x: _Real, y: _Real, z: _Real, rmax: _Real, rmin: _Real = 0.0) -> numpy.typing.NDArray[numpy.uint32]

Objects at distance d from a point, with rmin <= d <= rmax, as a uint32 array whose order is not meaningful. The interval is closed at both ends, so an object at exactly either radius is returned, and rmin == rmax selects the objects at exactly that distance. rmax may be +inf. A ball reaching past the box is cut off at its faces, except along a periodic axis, where it wraps. An object lying on the point is at d = 0 and is returned.

Raises meshsearch.Error if the point is outside the box, if a radius is negative or NaN, or if rmin exceeds rmax.

  1. close_objects(self, index: _Int, rmax: _Real, rmin: _Real = 0.0) -> numpy.typing.NDArray[numpy.uint32]

Objects at distance d from an object, with rmin <= d <= rmax, as a uint32 array whose order is not meaningful; the interval is the point form’s. The query object is excluded by identity, not by distance: another object coincident with it is at d = 0 and is returned.

Raises meshsearch.IndexError if index is negative, out of range or removed; meshsearch.Error if a radius is negative or NaN, or if rmin exceeds rmax; TypeError if index is not an integer.

  1. close_objects(self, x: _RealArray, y: _RealArray, z: _RealArray, rmax: _Real, rmin: _Real = 0.0, *, workers: int = 1) -> meshsearch.Batch

Objects at distance d from each of an array of points, with rmin <= d <= rmax, as a meshsearch.Batch (indices, offsets) in compressed-row form: the results of query i are indices[offsets[i]:offsets[i+1]], in the order the single call returns them. indices is uint32 and offsets int64; offsets has n_query + 1 entries, starts at 0 and ends at len(indices). A query with no results gives an empty slice, never a missing entry. A large batch is answered in cell order internally, which is faster; the results are always in query order.

There is no out= argument: the length of indices is not known until the queries have run, so no array could be supplied in advance. count_objects gives the counts alone.

Raises meshsearch.Error, naming the first query that fails, where the single-point call would, and returns nothing partial; also if the arrays differ in length, or on the radii, even for an empty batch.

The GIL is released while the queries run, so other Python threads may run, and may query the same grid; none may modify it until the call returns.

workers, keyword-only, is the number of threads the batch may use: 1, the default, runs it in the calling thread; -1 uses every core available to the process (on Linux, its CPU affinity set). The results and their order do not depend on it. A batch too small to share uses fewer threads. Raises meshsearch.Error if workers is 0 or below -1, TypeError if it is not an integer.

  1. close_objects(self, indices: _IntArray, rmax: _Real, rmin: _Real = 0.0, *, workers: int = 1) -> meshsearch.Batch

Objects at distance d from each object of an array of indices, with rmin <= d <= rmax, each object excluded from its own answer, as a meshsearch.Batch (indices, offsets) in compressed-row form: the results of query i are indices[offsets[i]:offsets[i+1]], in the order the single call returns them. indices is uint32 and offsets int64; offsets has n_query + 1 entries, starts at 0 and ends at len(indices). A query with no results gives an empty slice, never a missing entry.

indices is a 1-D array-like of integers, such as the uint32 arrays the queries return. An array of any integer dtype is read as it is, never cast, so values are never wrapped: a negative value or one that is not a valid index raises meshsearch.IndexError. A list or tuple of integers is converted first. Anything that is not integers raises TypeError; anything that is not one-dimensional raises meshsearch.Error.

A large batch is answered in cell order internally, which is faster; the results are always in query order.

There is no out= argument: the length of indices is not known until the queries have run, so no array could be supplied in advance. count_objects gives the counts alone.

Raises meshsearch.IndexError, naming the first query that fails, on an index that is negative, out of range or removed, and returns nothing partial; meshsearch.Error on the radii, even for an empty array.

The GIL is released while the queries run, so other Python threads may run, and may query the same grid; none may modify it until the call returns.

workers, keyword-only, is the number of threads the batch may use: 1, the default, runs it in the calling thread; -1 uses every core available to the process (on Linux, its CPU affinity set). The results and their order do not depend on it. A batch too small to share uses fewer threads. Raises meshsearch.Error if workers is 0 or below -1, TypeError if it is not an integer.

contains(self, x: _Real, y: _Real, z: _Real) → bool
contains(self, x: _RealArray, y: _RealArray, z: _RealArray, *, out: numpy.typing.NDArray[numpy.bool_] | None = None) → numpy.typing.NDArray[numpy.bool_]

Overloaded function.

  1. contains(self, x: _Real, y: _Real, z: _Real) -> bool

True if a query at the point is accepted, that is, if the point lies in the box. False for a NaN coordinate. Raises nothing for any real coordinates; TypeError if one is not a real number.

  1. contains(self, x: _RealArray, y: _RealArray, z: _RealArray, *, out: numpy.typing.NDArray[numpy.bool_] | None = None) -> numpy.typing.NDArray[numpy.bool_]

For each of an array of points, whether a query at it is accepted, that is, whether it lies in the box: a bool array, in query order. False for a NaN coordinate; no point makes it raise.

out, keyword-only, is an array to write the result into instead of allocating one, and is then returned. It must already be exactly the result: a writeable, C-contiguous bool array of shape (n_query,). Nothing is converted: a wrong dtype, a read-only array or something that is not an array raises TypeError; a wrong shape or a non-contiguous array raises meshsearch.Error.

Raises meshsearch.Error if the arrays differ in length or are not one-dimensional; TypeError if they do not hold real numbers.

count_objects(self, x: _Real, y: _Real, z: _Real, rmax: _Real, rmin: _Real = 0.0) → int
count_objects(self, index: _Int, rmax: _Real, rmin: _Real = 0.0) → int
count_objects(self, x: _RealArray, y: _RealArray, z: _RealArray, rmax: _Real, rmin: _Real = 0.0, *, out: numpy.typing.NDArray[numpy.int64] | None = None, workers: int = 1) → numpy.typing.NDArray[numpy.int64]
count_objects(self, indices: _IntArray, rmax: _Real, rmin: _Real = 0.0, *, out: numpy.typing.NDArray[numpy.int64] | None = None, workers: int = 1) → numpy.typing.NDArray[numpy.int64]

Overloaded function.

  1. count_objects(self, x: _Real, y: _Real, z: _Real, rmax: _Real, rmin: _Real = 0.0) -> int

Number of objects close_objects would return for a point, counted without collecting them, as an int.

Raises meshsearch.Error as close_objects does: if the point is outside the box, if a radius is negative or NaN, or if rmin exceeds rmax.

  1. count_objects(self, index: _Int, rmax: _Real, rmin: _Real = 0.0) -> int

Number of objects close_objects would return for an object, which is excluded, counted without collecting them, as an int.

Raises meshsearch.IndexError if index is negative, out of range or removed; meshsearch.Error on the radii, as close_objects; TypeError if index is not an integer.

  1. count_objects(self, x: _RealArray, y: _RealArray, z: _RealArray, rmax: _Real, rmin: _Real = 0.0, *, out: numpy.typing.NDArray[numpy.int64] | None = None, workers: int = 1) -> numpy.typing.NDArray[numpy.int64]

For each of an array of points, the number of objects close_objects would return for it: an int64 array with one count per query, in query order. The indices are never collected, so this is cheaper than close_objects when only the counts are wanted. A large batch is answered in cell order internally, which is faster; the results are always in query order.

out, keyword-only, is an array to write the result into instead of allocating one, and is then returned. It must already be exactly the result: a writeable, C-contiguous int64 array of shape (n_query,). Nothing is converted: a wrong dtype, a read-only array or something that is not an array raises TypeError; a wrong shape or a non-contiguous array raises meshsearch.Error. out is checked before any query runs, and is not written to if the call raises.

Raises meshsearch.Error, naming the first query that fails, where the single-point call would, and returns nothing partial; also if the arrays differ in length, or on the radii, even for an empty batch.

The GIL is released while the queries run, so other Python threads may run, and may query the same grid; none may modify it until the call returns.

workers, keyword-only, is the number of threads the batch may use: 1, the default, runs it in the calling thread; -1 uses every core available to the process (on Linux, its CPU affinity set). The results and their order do not depend on it. A batch too small to share uses fewer threads. Raises meshsearch.Error if workers is 0 or below -1, TypeError if it is not an integer.

  1. count_objects(self, indices: _IntArray, rmax: _Real, rmin: _Real = 0.0, *, out: numpy.typing.NDArray[numpy.int64] | None = None, workers: int = 1) -> numpy.typing.NDArray[numpy.int64]

For each object of an array of indices, the number of objects close_objects would return for it, the object itself excluded: an int64 array with one count per index, in order.

indices is a 1-D array-like of integers, such as the uint32 arrays the queries return. An array of any integer dtype is read as it is, never cast, so values are never wrapped: a negative value or one that is not a valid index raises meshsearch.IndexError. A list or tuple of integers is converted first. Anything that is not integers raises TypeError; anything that is not one-dimensional raises meshsearch.Error.

A large batch is answered in cell order internally, which is faster; the results are always in query order.

out, keyword-only, is an array to write the result into instead of allocating one, and is then returned. It must already be exactly the result: a writeable, C-contiguous int64 array of shape (len(indices),). Nothing is converted: a wrong dtype, a read-only array or something that is not an array raises TypeError; a wrong shape or a non-contiguous array raises meshsearch.Error. out is checked before any query runs, and is not written to if the call raises.

Raises meshsearch.IndexError, naming the first query that fails, on an index that is negative, out of range or removed, and returns nothing partial; meshsearch.Error on the radii, even for an empty array.

The GIL is released while the queries run, so other Python threads may run, and may query the same grid; none may modify it until the call returns.

workers, keyword-only, is the number of threads the batch may use: 1, the default, runs it in the calling thread; -1 uses every core available to the process (on Linux, its CPU affinity set). The results and their order do not depend on it. A batch too small to share uses fewer threads. Raises meshsearch.Error if workers is 0 or below -1, TypeError if it is not an integer.

get_cell(self, x: _Real, y: _Real, z: _Real) → int

Linear index of the cell containing a point: (ix * ny + iy) * nz + iz, with (ix, iy, iz) from get_cell_coords and (nx, ny, nz) = n_cells.

Raises meshsearch.Error if the point is outside the box.

get_cell_coords(self, x: _Real, y: _Real, z: _Real) → tuple[int, int, int]

Cell coordinates (ix, iy, iz) of the cell containing a point.

Raises meshsearch.Error if the point is outside the box.

get_objects_in_cell(self, x: _Real, y: _Real, z: _Real) → numpy.typing.NDArray[numpy.uint32]

Objects in the cell containing a point, as a uint32 array.

Raises meshsearch.Error if the point is outside the box.

is_alive(self, index: _Int) → bool
is_alive(self, indices: _IntArray, *, out: numpy.typing.NDArray[numpy.bool_] | None = None) → numpy.typing.NDArray[numpy.bool_]

Overloaded function.

  1. is_alive(self, index: _Int) -> bool

True if index names an object still in the grid. A negative index, or one out of range or removed, is False: the one member taking an index that does not raise IndexError.

Raises TypeError if index is not an integer.

  1. is_alive(self, indices: _IntArray, *, out: numpy.typing.NDArray[numpy.bool_] | None = None) -> numpy.typing.NDArray[numpy.bool_]

For each index of an array-like of integers, whether it names an object still in the grid: a bool array, in order. As for a single index, one that is negative or out of range is False, not an error. An array of integers of any dtype is used as it is; a list or tuple is converted first. Anything that is not integers raises TypeError; anything that is not one-dimensional raises meshsearch.Error.

out, keyword-only, is an array to write the result into instead of allocating one, and is then returned. It must already be exactly the result: a writeable, C-contiguous bool array of shape (len(indices),). Nothing is converted: a wrong dtype, a read-only array or something that is not an array raises TypeError; a wrong shape or a non-contiguous array raises meshsearch.Error.

nearest_object(self, x: _Real, y: _Real, z: _Real) → int
nearest_object(self, index: _Int) → int
nearest_object(self, x: _RealArray, y: _RealArray, z: _RealArray, *, out: numpy.typing.NDArray[numpy.uint32] | None = None, workers: int = 1) → numpy.typing.NDArray[numpy.uint32]
nearest_object(self, indices: _IntArray, *, out: numpy.typing.NDArray[numpy.uint32] | None = None, workers: int = 1) → numpy.typing.NDArray[numpy.uint32]

Overloaded function.

  1. nearest_object(self, x: _Real, y: _Real, z: _Real) -> int

Index of the object nearest to a point. An object lying on the point is at distance 0 and is the answer; ties go to the lower index.

Raises meshsearch.Error if the point is outside the box or the grid holds no objects.

  1. nearest_object(self, index: _Int) -> int

Index of the object nearest to an object. The query object is excluded by identity, not by distance: another object coincident with it is at distance 0 and may be the answer.

Raises meshsearch.IndexError if index is negative, out of range or removed; meshsearch.Error if no other object remains; TypeError if index is not an integer.

  1. nearest_object(self, x: _RealArray, y: _RealArray, z: _RealArray, *, out: numpy.typing.NDArray[numpy.uint32] | None = None, workers: int = 1) -> numpy.typing.NDArray[numpy.uint32]

The object nearest to each of an array-like of points: a uint32 array with one index per query, in query order. A large batch is answered in cell order internally, which is faster; the results are always in query order.

out, keyword-only, is an array to write the result into instead of allocating one, and is then returned. It must already be exactly the result: a writeable, C-contiguous uint32 array of shape (n_query,). Nothing is converted: a wrong dtype, a read-only array or something that is not an array raises TypeError; a wrong shape or a non-contiguous array raises meshsearch.Error. out is checked before any query runs, and is not written to if the call raises.

Raises meshsearch.Error, naming the first query that fails, where the single-point call would, and returns nothing partial; also if the arrays differ in length, or if the grid is empty, even for an empty batch.

The GIL is released while the queries run, so other Python threads may run, and may query the same grid; none may modify it until the call returns.

workers, keyword-only, is the number of threads the batch may use: 1, the default, runs it in the calling thread; -1 uses every core available to the process (on Linux, its CPU affinity set). The results and their order do not depend on it. A batch too small to share uses fewer threads. Raises meshsearch.Error if workers is 0 or below -1, TypeError if it is not an integer.

  1. nearest_object(self, indices: _IntArray, *, out: numpy.typing.NDArray[numpy.uint32] | None = None, workers: int = 1) -> numpy.typing.NDArray[numpy.uint32]

The object nearest to each object of an array of indices, each excluded from its own answer: a uint32 array with one index per query, in query order.

indices is a 1-D array-like of integers, such as the uint32 arrays the queries return. An array of any integer dtype is read as it is, never cast, so values are never wrapped: a negative value or one that is not a valid index raises meshsearch.IndexError. A list or tuple of integers is converted first. Anything that is not integers raises TypeError; anything that is not one-dimensional raises meshsearch.Error.

A large batch is answered in cell order internally, which is faster; the results are always in query order.

out, keyword-only, is an array to write the result into instead of allocating one, and is then returned. It must already be exactly the result: a writeable, C-contiguous uint32 array of shape (len(indices),). Nothing is converted: a wrong dtype, a read-only array or something that is not an array raises TypeError; a wrong shape or a non-contiguous array raises meshsearch.Error. out is checked before any query runs, and is not written to if the call raises.

Raises meshsearch.IndexError, naming the first query that fails, on an index that is negative, out of range or removed, and returns nothing partial; meshsearch.Error if fewer than two objects remain, even for an empty array.

The GIL is released while the queries run, so other Python threads may run, and may query the same grid; none may modify it until the call returns.

workers, keyword-only, is the number of threads the batch may use: 1, the default, runs it in the calling thread; -1 uses every core available to the process (on Linux, its CPU affinity set). The results and their order do not depend on it. A batch too small to share uses fewer threads. Raises meshsearch.Error if workers is 0 or below -1, TypeError if it is not an integer.

nearest_objects(self, n: _Int, x: _Real, y: _Real, z: _Real) → numpy.typing.NDArray[numpy.uint32]
nearest_objects(self, n: _Int, index: _Int) → numpy.typing.NDArray[numpy.uint32]
nearest_objects(self, n: _Int, x: _RealArray, y: _RealArray, z: _RealArray, *, out: numpy.typing.NDArray[numpy.uint32] | None = None, workers: int = 1) → numpy.typing.NDArray[numpy.uint32]
nearest_objects(self, n: _Int, indices: _IntArray, *, out: numpy.typing.NDArray[numpy.uint32] | None = None, workers: int = 1) → numpy.typing.NDArray[numpy.uint32]

Overloaded function.

  1. nearest_objects(self, n: _Int, x: _Real, y: _Real, z: _Real) -> numpy.typing.NDArray[numpy.uint32]

The n objects nearest to a point, nearest first, as a uint32 array; ties by ascending index, so a tie at the last place keeps the lower indices. n = 0 returns an empty array.

Raises meshsearch.Error if the point is outside the box, or if n is negative or exceeds the number of objects.

  1. nearest_objects(self, n: _Int, index: _Int) -> numpy.typing.NDArray[numpy.uint32]

The n objects nearest to an object, nearest first; ties by ascending index. A tie at the last place keeps the lower indices. The query object is excluded by identity, not by distance: another object coincident with it is at distance 0 and is among them.

Raises meshsearch.IndexError if index is negative, out of range or removed; meshsearch.Error if n is negative or not below the number of objects; TypeError if index is not an integer.

  1. nearest_objects(self, n: _Int, x: _RealArray, y: _RealArray, z: _RealArray, *, out: numpy.typing.NDArray[numpy.uint32] | None = None, workers: int = 1) -> numpy.typing.NDArray[numpy.uint32]

The n objects nearest to each of an array of points, nearest first; ties by ascending index: a uint32 array of shape (n_query, n), rectangular because every query returns exactly n. A tie at the last place keeps the lower indices. A large batch is answered in cell order internally, which is faster; the results are always in query order.

out, keyword-only, is an array to write the result into instead of allocating one, and is then returned. It must already be exactly the result: a writeable, C-contiguous uint32 array of shape (n_query, n). Nothing is converted: a wrong dtype, a read-only array or something that is not an array raises TypeError; a wrong shape or a non-contiguous array raises meshsearch.Error. out is checked before any query runs, and is not written to if the call raises.

Raises meshsearch.Error, naming the first query that fails, where the single-point call would, and returns nothing partial; also if the arrays differ in length, or if n is negative or exceeds the number of objects, even for an empty batch, or if n_query * n exceeds what 64-bit sizes can count.

The GIL is released while the queries run, so other Python threads may run, and may query the same grid; none may modify it until the call returns.

workers, keyword-only, is the number of threads the batch may use: 1, the default, runs it in the calling thread; -1 uses every core available to the process (on Linux, its CPU affinity set). The results and their order do not depend on it. A batch too small to share uses fewer threads. Raises meshsearch.Error if workers is 0 or below -1, TypeError if it is not an integer.

  1. nearest_objects(self, n: _Int, indices: _IntArray, *, out: numpy.typing.NDArray[numpy.uint32] | None = None, workers: int = 1) -> numpy.typing.NDArray[numpy.uint32]

The n objects nearest to each object of an array of indices, nearest first, each object excluded from its own answer; ties by ascending index: a uint32 array of shape (len(indices), n).

indices is a 1-D array-like of integers, such as the uint32 arrays the queries return. An array of any integer dtype is read as it is, never cast, so values are never wrapped: a negative value or one that is not a valid index raises meshsearch.IndexError. A list or tuple of integers is converted first. Anything that is not integers raises TypeError; anything that is not one-dimensional raises meshsearch.Error.

A large batch is answered in cell order internally, which is faster; the results are always in query order.

out, keyword-only, is an array to write the result into instead of allocating one, and is then returned. It must already be exactly the result: a writeable, C-contiguous uint32 array of shape (len(indices), n). Nothing is converted: a wrong dtype, a read-only array or something that is not an array raises TypeError; a wrong shape or a non-contiguous array raises meshsearch.Error. out is checked before any query runs, and is not written to if the call raises.

Raises meshsearch.IndexError, naming the first query that fails, on an index that is negative, out of range or removed, and returns nothing partial; meshsearch.Error if n is negative or not below the number of objects, even for an empty array, or if len(indices) * n exceeds what 64-bit sizes can count.

The GIL is released while the queries run, so other Python threads may run, and may query the same grid; none may modify it until the call returns.

workers, keyword-only, is the number of threads the batch may use: 1, the default, runs it in the calling thread; -1 uses every core available to the process (on Linux, its CPU affinity set). The results and their order do not depend on it. A batch too small to share uses fewer threads. Raises meshsearch.Error if workers is 0 or below -1, TypeError if it is not an integer.

remove_object(self, index: _Int) → None

Remove an object. Its index stays invalid for the life of the grid, and its memory is not returned. Must not run while another thread uses the grid.

Raises meshsearch.IndexError if index is negative, out of range or already removed; TypeError if it is not an integer.

property cellsize

Cell side. The cellsize requested, unless an axis is periodic, where it is the largest side not above the request that fits a whole number of times into the shortest period.

property lims

The box as a 3x2 array: grown to whole cells on a non-periodic axis, exactly the limits given on a periodic one.

property n_cells

Cells per axis.

property periodic

Per axis, whether the box is periodic: a tuple of three bools.

The result of a batched close_objects

class meshsearch.Batch(indices, offsets)

What the batched close_objects returns: a named tuple (indices, offsets) in compressed-row form. The results of query i are indices[offsets[i]:offsets[i+1]].

indices

uint32 array: the indices found by every query, concatenated.

offsets

int64 array of n_query + 1 entries, starting at 0 and ending at len(indices).

The version

meshsearch.__version__: str

The version of the library the module was built from, a string such as "1.0.0"; the same as MESHSEARCH_VERSION in C++.

The command line

python -m meshsearch --cmake-dir prints the directory that holds the package’s meshsearchConfig.cmake, to pass to CMake as -Dmeshsearch_DIR. The guide’s page Using meshsearch from another package explains how a C++ project, or a Python package with a compiled module of its own, uses it.

Errors

exception meshsearch.Error

Bases: ValueError

Raised on invalid input: a point outside the box, an invalid argument, or a query the grid cannot answer.

exception meshsearch.IndexError

Bases: Error, IndexError

Raised on an index that is negative, out of range or names a removed object. Also a meshsearch.Error and a built-in IndexError.