15. varray
— Working with variable width tables.¶
Mesh type geometries use tables of integer data to store the connectivity between different geometric entities. The basic connectivity table in a Mesh with elements of the same type is a table of constant width: the number of nodes connected to each element is constant. However, the inverse table (the elements connected to each node) does not have a constant width.
Tables of constant width can conveniently be stored as a 2D array, allowing fast indexing by row and/or column number. A variable width table can be stored (using arrays) in two ways:
- as a 2D array, with a width equal to the maximal row length. Unused positions in the row are then filled with an invalid value (-1).
- as a 1D array, storing a simple concatenation of the rows. An additional array then stores the position in that array of the first element of each row.
In pyFormex, variable width tables were initially stored as 2D arrays: a remnant of the author’s past FORTRAN experience. With a growing professional use of pyFormex involving ever larger models, it became clear that there was a large memory and speed penalty related to the use of 2D arrays with lots of unused entries. This is illustrated in the following table, obtained on the inversion of a connectivity table of 10000 rows and 25 columns. The table shows the memory size of the inverse table, the time needed to compute it, and the time to compute both tables. The latter involves an extra conversion of the stored array to the other data type.
Stored as: | 2D (ndarray) | 1D (Varray) | 1D (Varray) |
---|---|---|---|
Rows are sorted: | yes | yes | no |
Memory size | 450000 | 250000 | 250000 |
Time to create table | 128 ms | 49 ms | 25ms |
Time to create both | 169 ms | 82 ms | 57ms |
The memory and speed gains of using the Varray are important. The 2D array can even be faster generated by first creating the 1D array, and then converting that to 2D. Not sorting the entries in the Varray provides a further gain. The Varray class defined below therefore does not sort the rows by default, but provides methods to sort them when needed.
15.1. Classes defined in module varray¶
-
class
varray.
Varray
(data=[], ind=None)[source]¶ A variable width 2D integer array
This class provides an efficient way to store tables of nonnegative integers when the rows of the table may have different length.
For large tables this may allow an important memory saving compared to a rectangular array where the non-existent entries are filled by some special value. Data in the Varray are stored as a single 1D array, containing the concatenation of all rows. An index is kept with the start position of each row in the 1D array.
Parameters: - data –
Data to initialize to a new Varray object. This can either of:
- another Varray instance: a shallow copy of the Varray is created.
- a list of lists of integers. Each item in the list contains one row of the table.
- a 2D ndarray of integer type. The nonnegative numbers on each row constitute the data for that row.
- a 1D array or list of integers, containing the concatenation of the rows. The second argument ind specifies the indices of the first element of each row.
- a 1D array or list of integers, containing the concatenation of the rows obtained by prepending each row with the row length. The caller should make sure these 1D data are consistent.
- ind (1-dim int array_like, optional) – This is only used when data is a pure concatenation of all rows.
It holds the position in data of the first element of each row.
Its length is equal to the number of rows (nrows) or nrows+1.
It is a non-decreasing series of integer values, starting with 0.
If it has
nrows+1
entries, the last value is equal to the total number of elements in data. This last value may be omitted, and will then be added automatically. Note that two subsequent elements may be equal, corresponding with an empty row.
Attributes
-
shape
¶ The combined (
nrows
,``width``) values.Type: tuple of two ints
Examples
Create a Varray is by default printed in user-friendly format:
>>> Va = Varray([[0],[1,2],[0,2,4],[0,2]]) >>> Va Varray([[0], [1, 2], [0, 2, 4], [0, 2]])
The Varray prints in a user-friendly format:
>>> print(Va) Varray (4,3) [0] [1 2] [0 2 4] [0 2] <BLANKLINE>
Other initialization methods resulting in the same Varray:
>>> Vb = Varray(Va) >>> print(str(Vb) == str(Va)) True >>> Vb = Varray(np.array([[-1,-1,0],[-1,1,2],[0,2,4],[-1,0,2]])) >>> print(str(Vb) == str(Va)) True >>> Vc = Varray([0,1,2,0,2,4,0,2],at.cumsum([0,1,2,3,2])) >>> print(str(Vc) == str(Va)) True >>> Vd = Varray([1,0, 2,1,2, 3,0,2,4, 2,0,2]) >>> print(str(Vd) == str(Va)) True
Show info about the Varray
>>> print(Va.nrows, Va.width, Va.shape) 4 3 (4, 3) >>> print(Va.size, Va.lengths) 8 [1 2 3 2]
Indexing: The data for any row can be obtained by simple indexing:
>>> print(Va[1]) [1 2]
This is equivalent with
>>> print(Va.row(1)) [1 2]
>>> print(Va.row(-1)) [0 2]
Change elements:
>>> Va[1][0] = 3 >>> print(Va[1]) [3 2]
Full row can be changed with matching length:
>>> Va[1] = [1, 2] >>> print(Va[1]) [1 2]
Negative indices are allowed:
Extracted columns are filled with -1 values where needed
>>> print(Va.col(1)) [-1 2 2 2]
Select takes multiple rows using indices or bool:
>>> print(Va.select([1,3])) Varray (2,2) [1 2] [0 2] <BLANKLINE> >>> print(Va.select(Va.lengths==2)) Varray (2,2) [1 2] [0 2] <BLANKLINE>
Iterator: A Varray provides its own iterator:
>>> for row in Va: ... print(row) [0] [1 2] [0 2 4] [0 2]
>>> print(Varray()) Varray (0,0) <BLANKLINE>
>>> L,R = Va.sameLength() >>> print(L) [1 2 3] >>> print(R) [array([0]), array([1, 3]), array([2])] >>> for a in Va.split(): ... print(a) [[0]] [[1 2] [0 2]] [[0 2 4]]
-
lengths
¶ Return the length of all rows of the Varray
-
nrows
Return the number of rows in the Varray
-
size
Return the total number of elements in the Varray
-
shape
Return a tuple with the number of rows and maximum row length
-
col
(i)[source]¶ Return the data for column i
This always returns a list of length nrows. For rows where the column index i is missing, a value -1 is returned.
-
select
(sel)[source]¶ Select some rows from the Varray.
Parameters: sel (iterable of ints or bools) – Specifies the row(s) to be selected. If type is int, the values are the row numbers. If type is bool, the length of the iterable should be exactly self.nrows
; the positions where the value is True are the rows to be returned.Returns: Varray object – A Varray with only the selected rows. Examples
>>> Va = Varray([[0],[1,2],[0,2,4],[0,2]]) >>> Va.select((1,3)) Varray([[1, 2], [0, 2]]) >>> Va.select((False,True,False,True)) Varray([[1, 2], [0, 2]])
-
index
(sel)[source]¶ Convert a selector to an index.
Parameters: sel (iterable of ints or bools) – Specifies the elements of the Varray to be selected. If type is int, the values are the index numbers in the flat array. If type is bool, the length of the iterable should be exactly self.size
; the positions where the value is True will be returned.Returns: int array – The selected element numbers. Examples
>>> Va = Varray([[0],[1,2],[0,2,4],[0,2]]) >>> Va.index((1,3,5,7)) array([1, 3, 5, 7]) >>> Va.index((False,True,False,True,False,True,False,True)) array([1, 3, 5, 7])
-
rowindex
(sel)[source]¶ Return the rowindex for the elements flagged by selector sel.
sel is either a list of element numbers or a bool array with length self.size
-
colindex
(sel)[source]¶ Return the column index for the elements flagged by selector sel.
sel is either a list of element numbers or a bool array with length self.size
-
where
(sel)[source]¶ Return row and column index of the selected elements
sel is either a list of element numbers or a bool array with length self.size
Returns a 2D array where the first column is the row index and the second column the corresponding column index of an element selected by sel
-
sorted
()[source]¶ Returns a sorted Varray.
Returns a Varray with the same entries but where each row is sorted.
This returns a copy of the data, and leaves the original unchanged.
See also
sort()
for sorting the rows inplace.
-
removeFlat
(ind)[source]¶ Remove the nelement with flat index i
Parameters: ind (int or int :term:) – Index in the flat data of the element(s) to remove. Returns: Varray – A Varray with the element(s) ind removed. Examples
>>> Va = Varray([[0],[1,2],[0,2,4],[0,2]]) >>> Va.removeFlat(3) Varray([[0], [1, 2], [2, 4], [0, 2]]) >>> Va.removeFlat([0,2,7]) Varray([[], [1], [0, 2, 4], [0]])
-
sort
()[source]¶ Sort the Varray inplace.
Sorting a Varray sorts the elements in each row. The sorting is done inplace.
See also
sorted()
for sorting the rows without changing the original.
-
toArray
()[source]¶ Convert the Varray to a 2D array.
Returns a 2D array with shape (self.nrows,self.width), containing the row data of the Varray. Rows which are shorter than width are padded at the start with values -1.
-
sameLength
()[source]¶ Groups the rows according to their length.
Returns a tuple of two lists (lengths,rows):
- lengths: the sorted unique row lengths,
- rows: the indices of the rows having the corresponding length.
-
split
()[source]¶ Split the Varray into 2D arrays.
Returns a list of 2D arrays with the same number of columns and the indices in the original Varray.
-
inverse
(expand=False)[source]¶ Return the inverse of a Varray.
The inverse of a Varray is again a Varray. Values k on a row i will become values i on row k. The number of data in both Varrays is thus the same.
The inverse of the inverse is equal to the original. Two Varrays are equal if they have the same number of rows and all rows contain the same numbers, independent of their order.
Example:
>>> a = Varray([[0,1],[2,0],[1,2],[4]]) >>> b = a.inverse() >>> c = b.inverse() >>> print(a,b,c) Varray (4,2) [0 1] [2 0] [1 2] [4] Varray (5,2) [0 1] [0 2] [1 2] [] [3] Varray (4,2) [0 1] [0 2] [1 2] [4] <BLANKLINE>
- data –
15.2. Functions defined in module varray¶
-
varray.
inverseIndex
(ind, sort=False, expand=False)[source]¶ Create the inverse of a 2D index array.
Parameters:
- ind: a Varray or a 2D index array. A 2D index array is a 2D integer array where only nonnegative values are significant and negative values are silently ignored. While in most cases all values in a row are unique, this is not a requirement. Degenerate elements may have the same node number appearing multiple times in the same row.
- sort: bool. If True, rows are sorted.
- expand: bool. If True, an
numpy.ndarray
is returned.
Returns the inverse index, as a Varray (default) or as an ndarray (if expand is True). If sort is True, rows are sorted.
Example
>>> a = inverseIndex([[0,1],[0,2],[1,2],[0,3]]) >>> print(a) Varray (4,3) [0 1 3] [0 2] [1 2] [3] <BLANKLINE>