triangularmap.tmap.ArrayTMap

class triangularmap.tmap.ArrayTMap(n, value, *args, **kwargs)[source]

Bases: TMap

Create a TMap of size n using a NumPy array initialised with value as underlying storage. The array is created with numpy.full. The first argument (the shape of the array) is computed via size_from_n() (if n is a tuple of integers this will also be a tuple with only the first element being changed; see below); the second argument is value; *args and **kwargs are passed on (except for linearise_blocks, see below).

Parameters:
  • n – int or tuple of int; if n is a single integer it specifies the size (width) of the map; if n is a tuple of integers, the first element specifies the size of the map and the remaining ones specify the shape of the values stored in the map (i.e. value_shape).

  • value – value to initialise the underlying array with numpy.full

  • args – additional arguments passed on to numpy.full

  • kwargs – additional arguments passed on to numpy.full; if linearise_blocks is in kwargs this will be removed first and instead directly passed on to initialise the TMap.

Public Data Attributes:

Inherited from TMap

n

Size of the triangular map, i.e., the width at the triangle base.

arr

Underlying storage container.

value_shape

Shape of the values stored in the map.

linearise_blocks

lslice

A convenience property that uses get_lslice() and set_lslice() as getter and setter, respectively.

dslice

A convenience property that uses get_dslice() and set_dslice() as getter and setter, respectively.

sslice

A convenience property that uses get_sslice() and set_sslice() as getter and setter, respectively.

eslice

A convenience property that uses get_eslice() and set_eslice() as getter and setter, respectively.

sblock

A convenience property that uses get_sblock() and set_sblock() as getter and setter, respectively.

eblock

A convenience property that uses get_eblock() and set_eblock() as getter and setter, respectively.

Public Methods:

__new__(cls, n, value, *args, **kwargs)

Create a TMap of size n using a NumPy array initialised with value as underlying storage.

Inherited from TMap

size_from_n(n)

Calculate the size N of the underlying 1D array for a given width n of the triangular map: N = n (n + 1)) / 2.

n_from_size(n)

Calculate width n of the map given the size N of the underlying 1D array: n = (\sqrt{8 * N + 1} - 1) / 2.

get_reindex_from_start_end_to_top_down(n)

For a map of width n, get an index array of length n (n + 1)) / 2 to reindex from start-end order to top-down order.

reindex_from_start_end_to_top_down(arr)

Reindex arr from start-end order to top-down order.

get_reindex_from_top_down_to_start_end(n)

For a map of width n, get an index array of length n (n + 1)) / 2 to reindex from top-down order to start-end order.

reindex_from_top_down_to_start_end(arr)

Reindex arr from top-down order to start-end order.

__init__(arr[, linearise_blocks, _n])

depth(start, end)

Compute the depth d corresponding to (start, end): d = n - (end - start).

level(*args)

Compute the level from depth or (start, end).

linear_from_start_end(start, end)

Compute the linear index (in the underlying 1D array) corresponding to a (start, end) pair.

__getitem__(item)

Get the item corresponding to (start, end) or the sub-map corresponding to slice start:end.

__setitem__(key, value)

Set the item corresponding to (start, end).

copy()

Copy the map.

top([depth])

Return the sub-map corresponding to the top-most levels.

linear_start_end_from_level(level)

Compute the linear 1D start and end index corresponding to all values in the respective level of the map.

get_lslice(level)

Slice the map at the given level, returning a view of the values.

set_lslice(level, value)

get_dslice(depth)

Slice the map at the given depth, returning a view of the values.

set_dslice(depth, value)

get_sslice(item)

Return a slice for the given start index.

set_sslice(key, value)

Like get_sslice but set value instead of returning values.

get_eslice(item)

Return a slice for the given end index.

set_eslice(key, value)

Like get_eslice but set value instead of returning values.

get_sblock(item)

Return a block of sslices down from the specified level.

set_sblock(key, value)

Like get_sblock but set value.

get_eblock(item)

Return a block of eslices down from the specified level.

set_eblock(key, value)

Like get_sblock but set value.

flatten([order])

Return map in linear order.

__repr__()

Return repr(self).

__str__()

Return a string representation of the map, consisting of consecutive dslices.

pretty([cut, str_func, detach_pytorch, scf, ...])

Pretty-print a triangular map.

Private Data Attributes:

Inherited from TMap

_flatten_regex

Private Methods:

Inherited from TMap

_to_int(i)

Convert i to integer, no matter whether it is a single number or a numpy array.

_unpack_item(item)

_is_pytorch()

_check(start, end)

Check whether 0 <= start < end < n.

_end_indices_for_sslice(start)

Compute the end indices corresponding to a slice at the give start index.

_start_indices_for_eslice(end)

Compute the start indices corresponding to a slice at the give end index.

_get_sblock_index(item)

_get_eblock_index(item)