index

gridkit.index.validate_index(func)[source]

Decorator to convert the index argument of a function to a GridIndex object.

gridkit.index.concat(grid_ids: List | Tuple)[source]

Concatenate supplied GridIndex instances into one. Duplicate indices are allowed. If duplicates are not desired for your usecase, consider calling GridIndex.unique() after concatenating.

Parameters:

grid_ids – A list of GridIndex isntances to concatenate

class gridkit.index.GridIndex(index)[source]

Bases: object

Index to be used with grid classes. These GridIndex class instances contain the references to the grid cells by ID (idx, idy). Several methods are implemented to treat the indices as sets, where one (idx, idy) pair is considered one unit, one cell-ID. The GridIndex class also allows for generic operators such as: (+, -, , /, //, *, %, >=, <=, >, <)

Parameters:

index (Union[numpy.ndarray, list, tuple, GridIndex]) – The index containing the cell-id. This is assumed to either be a single (idx, idy) pair or a list, tuple or ndarray containing multiple of such pairs.

Raises:

ValueError – When the shape of the index does not match the expected shape of (2,) or (N,2) where N is the number of cells, a ValuError is raised.

classmethod from_index_1d(combined)[source]

Turn 1d-view into GridIndex

property x

The X-component of the cell-IDs

property y

The Y-component of the cell-IDs

property shape
unique(**kwargs)[source]

The unique IDs contained in the index. Remove duplicate IDs.

Parameters:

**kwargs – The keyword arguments to pass to numpy.unique

sort()[source]

Sort the grid indices. Multidimentional indices are not supported. Ravels the indices if indices are multidimentional.

The indices are sorted first by x, then by y.

Returns:

The sorted ids

Return type:

GridIndex

Examples

>>> from gridkit.index import GridIndex
>>> unsorted_ids = GridIndex([[1,1],[0,1],[1,0],[0,0]])
>>> sorted_ids = unsorted_ids.sort()
>>> sorted_ids.index
array([[0, 0],
       [0, 1],
       [1, 0],
       [1, 1]], dtype=int32)
intersection(other)[source]

The intersection of two GridIndex instances. Keep the IDs contained in both.

Parameters:

other (GridIndex) – The GridIndex instance to compare with

difference(other)[source]

The difference of two GridIndex instances. Keep the IDs contained in self that are not in other.

Parameters:

other (GridIndex) – The GridIndex instance to compare with

isdisjoint(other)[source]

True if none of the IDs in self are in other. False if any ID in self is also in other.

Parameters:

other (GridIndex) – The GridIndex instance to compare with

issubset(other)[source]

True if all of the IDs in self are in other. False if not all IDs in self is also in other.

Parameters:

other (GridIndex) – The GridIndex instance to compare with

issuperset(other)[source]

True if all of the IDs in other are in self. False if not all IDs in other is also in self.

Parameters:

other (GridIndex) – The GridIndex instance to compare with

property index_1d

Turn index based on x,y into a single integer. Assumes x and y are 32-bit integers

ravel()[source]

Flatten a nd-index

Examples

>>> from gridkit.index import GridIndex
>>> import numpy
>>> index = GridIndex(numpy.arange(2*3*2).reshape(2,3,2))
>>> index.index
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5]],

       [[ 6,  7],
        [ 8,  9],
        [10, 11]]])
>>> flat_index = index.ravel()
>>> flat_index.index
array([[ 0,  1],
       [ 2,  3],
       [ 4,  5],
       [ 6,  7],
       [ 8,  9],
       [10, 11]])
Returns:

A flattened copy of te index

Return type:

GridIndex

append(index, in_place=False)[source]

Add cell ids to the end of the current index. This updates the .index attribute in-place as would an append on a python list.

Parameters:
  • index (GridIndex) – The cell_ids to append to the current index

  • in_place (bool (optional, default False )) – Updates the index of self if True. Returns a copy if False. Note: This does not improve performance as you might expect form a true in-place operation. The copy is made regardless since the data stored in the GridIndex is based on a numpy array and not on a Python List. The in-place option is for convenience only, not performance.

Return type:

None

Examples

>>> cell_ids = GridIndex([0,1])
>>> cell_ids.index
array([0, 1])
>>> result = cell_ids.append([-5,9])
>>> result.index
array([[ 0,  1],
       [-5,  9]])

Alternatively, by specifying in_place=True the original object can be updated. As noted at the in_place parameter description, this does not result in performance gains.

>>> cell_ids.append([-5,9], in_place=True)
<gridkit.index.GridIndex object at ...>
>>> cell_ids.index
array([[ 0,  1],
       [-5,  9]])
delete(index, in_place=False)[source]

Remove all instances of ‘item’ from self.

Parameters:
  • index (GridIndex) – The cell ids to remove from self

  • in_place (bool (optional, default False )) – Updates the index of self if True. Returns a copy if False. Note: This does not improve performance as you might expect form a true in-place operation. The copy is made regardless since the data stored in the GridIndex is based on a numpy array and not on a Python List. The in-place option is for convenience only, not performance.

Returns:

The new index where the supplied ids were removed

Return type:

GridIndex

Examples

Alternatively, by specifying in_place=True the original object can be updated. As noted at the in_place parameter description, this does not result in performance gains.

copy()[source]

Return an immutable copy of self.