| 1 |
|
| 2 |
) |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import numpy as np |
| 10 |
|
| 11 |
|
| 12 |
class DataSet: |
| 13 |
""" |
| 14 |
Data IO object for DataArray. |
| 15 |
""" |
| 16 |
|
| 17 |
def __array__(self): |
| 18 |
return self._read_data()[:] |
| 19 |
|
| 20 |
def __getitem__(self, index): |
| 21 |
return self._read_data(index) |
| 22 |
|
| 23 |
def __setitem__(self, index, value): |
| 24 |
self._write_data(value, index) |
| 25 |
|
| 26 |
def __len__(self): |
| 27 |
return self.len() |
| 28 |
|
| 29 |
def __iter__(self): |
| 30 |
for idx in range(self.len()): |
| 31 |
yield self[idx] |
| 32 |
|
| 33 |
def len(self): |
| 34 |
""" |
| 35 |
Length of the first dimension. Equivalent to `DataSet.shape[0]`. |
| 36 |
|
| 37 |
:type: int or long |
| 38 |
""" |
| 39 |
return self.shape[0] |
| 40 |
|
| 41 |
@property |
| 42 |
def shape(self): |
| 43 |
""" |
| 44 |
:type: tuple of data array dimensions. |
| 45 |
""" |
| 46 |
return self.data_extent |
| 47 |
|
| 48 |
@property |
| 49 |
def size(self): |
| 50 |
""" |
| 51 |
Number of elements in the DataSet, i.e. the product of the |
| 52 |
elements in :attr:`~nixio.data_array.DataSet.shape`. |
| 53 |
|
| 54 |
:type: int |
| 55 |
""" |
| 56 |
return np.prod(self.shape) |
| 57 |
|
| 58 |
@property |
| 59 |
def dtype(self): |
| 60 |
""" |
| 61 |
:type: :class:`numpy.dtype` object holding type information about |
| 62 |
the data stored in the DataSet. |
| 63 |
""" |
| 64 |
return np.dtype(self._get_dtype()) |
| 65 |
|
| 66 |
def write_direct(self, data): |
| 67 |
""" |
| 68 |
Directly write all of ``data`` to the |
| 69 |
:class:`~nixio.data_array.DataSet`. The supplied data must be a |
| 70 |
:class:`numpy.ndarray` that matches the DataSet's shape and must have |
| 71 |
C-style contiguous memory layout (see :attr:`numpy.ndarray.flags` and |
| 72 |
:class:`~numpy.ndarray` for more information). |
| 73 |
|
| 74 |
:param data: The array which contents is being written |
| 75 |
:type data: :class:`numpy.ndarray` |
| 76 |
""" |
| 77 |
self._write_data(data) |
| 78 |
|
| 79 |
def read_direct(self, data): |
| 80 |
""" |
| 81 |
Directly read all data stored in the :class:`~nixio.data_array.DataSet` |
| 82 |
into ``data``. The supplied data must be a :class:`numpy.ndarray` that |
| 83 |
matches the DataSet's shape, must have C-style contiguous memory layout |
| 84 |
and must be writeable (see :attr:`numpy.ndarray.flags` and |
| 85 |
:class:`~numpy.ndarray` for more information). |
| 86 |
|
| 87 |
:param data: The array where data is being read into |
| 88 |
:type data: :class:`numpy.ndarray` |
| 89 |
""" |
| 90 |
data[:] = self._read_data() |
| 91 |
|
| 92 |
def append(self, data, axis=0): |
| 93 |
""" |
| 94 |
Append ``data`` to the DataSet along the ``axis`` specified. |
| 95 |
|
| 96 |
:param data: The data to append. Shape must agree except for the |
| 97 |
specified axis |
| 98 |
:param axis: Along which axis to append the data to |
| 99 |
""" |
| 100 |
data = np.ascontiguousarray(data) |
| 101 |
if len(self.shape) != len(data.shape): |
| 102 |
raise ValueError( |
| 103 |
"Data and DataArray must have the same dimensionality" |
| 104 |
) |
| 105 |
|
| 106 |
if any([s != ds for i, (s, ds) in |
| 107 |
enumerate(zip(self.shape, data.shape)) if i != axis]): |
| 108 |
raise ValueError("Shape of data and shape of DataArray must match " |
| 109 |
"in all dimension but axis!") |
| 110 |
|
| 111 |
offset = tuple(0 if i != axis else x for i, x in enumerate(self.shape)) |
| 112 |
count = data.shape |
| 113 |
enlarge = tuple(self.shape[i] + (0 if i != axis else x) |
| 114 |
for i, x in enumerate(data.shape)) |
| 115 |
self.data_extent = enlarge |
| 116 |
slc = tuple(slice(o, c+o) for o, c in zip(offset, count)) |
| 117 |
self._write_data(data, slc) |
| 118 |
|
| 119 |
def _write_data(self, data, slc=None): |
| 120 |
dataset = self._h5group.get_dataset("data") |
| 121 |
dataset.write_data(data, slc) |
| 122 |
|
| 123 |
def _read_data(self, slc=None): |
| 124 |
return self._h5group.get_dataset("data").read_data(slc) |
| 125 |
|
| 126 |
@property |
| 127 |
def data_extent(self): |
| 128 |
""" |
| 129 |
The size of the data. |
| 130 |
|
| 131 |
:type: tuple of int |
| 132 |
""" |
| 133 |
dataset = self._h5group.get_dataset("data") |
| 134 |
return dataset.shape |
| 135 |
|
| 136 |
@data_extent.setter |
| 137 |
def data_extent(self, extent): |
| 138 |
dataset = self._h5group.get_dataset("data") |
| 139 |
dataset.shape = extent |
| 140 |
|
| 141 |
@property |
| 142 |
def data_type(self): |
| 143 |
""" |
| 144 |
The data type of the data stored in the DataArray. This is a read only |
| 145 |
property. |
| 146 |
|
| 147 |
:type: nixio.DataType |
| 148 |
""" |
| 149 |
return self._get_dtype() |
| 150 |
|
| 151 |
def _get_dtype(self): |
| 152 |
dataset = self._h5group.get_dataset("data") |
| 153 |
return dataset.dtype |