| 1 |
|
| 2 |
) |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import warnings |
| 10 |
import numpy as np |
| 11 |
|
| 12 |
from .tag import BaseTag, SliceMode |
| 13 |
from .container import LinkContainer |
| 14 |
from .source_link_container import SourceLinkContainer |
| 15 |
from .data_array import DataArray |
| 16 |
from .data_view import DataView |
| 17 |
from .link_type import LinkType |
| 18 |
from .exceptions import (OutOfBounds, IncompatibleDimensions, |
| 19 |
UninitializedEntity) |
| 20 |
from .section import Section |
| 21 |
|
| 22 |
|
| 23 |
class MultiTag(BaseTag): |
| 24 |
|
| 25 |
@classmethod |
| 26 |
def create_new(cls, nixfile, nixparent, h5parent, name, type_, positions): |
| 27 |
newentity = super(MultiTag, cls).create_new(nixfile, nixparent, |
| 28 |
h5parent, name, type_) |
| 29 |
newentity.positions = positions |
| 30 |
return newentity |
| 31 |
|
| 32 |
@property |
| 33 |
def positions(self): |
| 34 |
""" |
| 35 |
The positions defined by the tag. This is a read-write property. |
| 36 |
|
| 37 |
:type: nixio.DataArray |
| 38 |
""" |
| 39 |
if "positions" not in self._h5group: |
| 40 |
raise RuntimeError("MultiTag.positions: DataArray not found!") |
| 41 |
return DataArray(self.file, self._parent, |
| 42 |
self._h5group.open_group("positions")) |
| 43 |
|
| 44 |
@positions.setter |
| 45 |
def positions(self, da): |
| 46 |
if da is None: |
| 47 |
raise TypeError("MultiTag.positions cannot be None.") |
| 48 |
if "positions" in self._h5group: |
| 49 |
del self._h5group["positions"] |
| 50 |
self._h5group.create_link(da, "positions") |
| 51 |
if self.file.auto_update_timestamps: |
| 52 |
self.force_updated_at() |
| 53 |
|
| 54 |
@property |
| 55 |
def extents(self): |
| 56 |
""" |
| 57 |
The extents defined by the tag. This is an optional read-write |
| 58 |
property and may be set to None. |
| 59 |
|
| 60 |
:type: nixio.DataArray or None |
| 61 |
""" |
| 62 |
if "extents" in self._h5group: |
| 63 |
return DataArray(self.file, self._parent, |
| 64 |
self._h5group.open_group("extents")) |
| 65 |
return None |
| 66 |
|
| 67 |
@extents.setter |
| 68 |
def extents(self, da): |
| 69 |
if da is None: |
| 70 |
del self._h5group["extents"] |
| 71 |
else: |
| 72 |
self._h5group.create_link(da, "extents") |
| 73 |
if self.file.auto_update_timestamps: |
| 74 |
self.force_updated_at() |
| 75 |
|
| 76 |
@property |
| 77 |
def references(self): |
| 78 |
""" |
| 79 |
A property containing all data arrays referenced by the tag. Referenced |
| 80 |
data arrays can be obtained by index or their id. References can be |
| 81 |
removed from the list, removing a referenced DataArray will not remove |
| 82 |
it from the file. New references can be added using the append method |
| 83 |
of the list. |
| 84 |
This is a read only attribute. |
| 85 |
|
| 86 |
:type: nixio.LinkContainer of nixio.DataArray |
| 87 |
""" |
| 88 |
if self._references is None: |
| 89 |
self._references = LinkContainer("references", self, DataArray, |
| 90 |
self._parent.data_arrays) |
| 91 |
return self._references |
| 92 |
|
| 93 |
def _calc_data_slices_mtag(self, data, index, stop_rule): |
| 94 |
positions = self.positions |
| 95 |
extents = self.extents |
| 96 |
if not positions or index >= positions.shape[0]: |
| 97 |
raise OutOfBounds("Index out of bounds of positions!") |
| 98 |
|
| 99 |
if extents and index >= extents.shape[0]: |
| 100 |
raise OutOfBounds("Index out of bounds of extents!") |
| 101 |
|
| 102 |
if extents and positions.data_extent != extents.data_extent: |
| 103 |
raise IncompatibleDimensions("Number of dimensions in position and extent do not match", |
| 104 |
"MultiTag._calc_data_slices_mtag") |
| 105 |
|
| 106 |
if len(positions.shape) == 1: |
| 107 |
|
| 108 |
|
| 109 |
positions = np.array([p for p in positions]) |
| 110 |
|
| 111 |
if extents and len(extents.shape) == 1: |
| 112 |
|
| 113 |
|
| 114 |
extents = np.array([e for e in extents]) |
| 115 |
|
| 116 |
position = positions[index] |
| 117 |
extent = None |
| 118 |
if extents is not None and len(extents) > 0: |
| 119 |
extent = extents[index] |
| 120 |
return self._calc_data_slices(data, position, extent, stop_rule) |
| 121 |
|
| 122 |
def retrieve_data(self, posidx, refidx): |
| 123 |
msg = ("Call to deprecated method MultiTag.retrieve_data. " |
| 124 |
"Use MultiTag.tagged_data instead.") |
| 125 |
warnings.warn(msg, category=DeprecationWarning) |
| 126 |
return self.tagged_data(posidx, refidx) |
| 127 |
|
| 128 |
def tagged_data(self, posidx, refidx, stop_rule=SliceMode.Exclusive): |
| 129 |
references = self.references |
| 130 |
positions = self.positions |
| 131 |
extents = self.extents |
| 132 |
if len(references) == 0: |
| 133 |
raise OutOfBounds("There are no references in this multitag!") |
| 134 |
|
| 135 |
if (posidx >= positions.data_extent[0] or |
| 136 |
extents and posidx >= extents.data_extent[0]): |
| 137 |
raise OutOfBounds("Index out of bounds of positions or extents!") |
| 138 |
|
| 139 |
ref = references[refidx] |
| 140 |
|
| 141 |
slices = self._calc_data_slices_mtag(ref, posidx, stop_rule) |
| 142 |
return DataView(ref, slices) |
| 143 |
|
| 144 |
def retrieve_feature_data(self, posidx, featidx): |
| 145 |
msg = ("Call to deprecated method MultiTag.retrieve_feature_data. " |
| 146 |
"Use MultiTag.feature_data instead.") |
| 147 |
warnings.warn(msg, category=DeprecationWarning) |
| 148 |
return self.feature_data(posidx, featidx) |
| 149 |
|
| 150 |
def feature_data(self, posidx, featidx, stop_rule=SliceMode.Exclusive): |
| 151 |
if len(self.features) == 0: |
| 152 |
msg = "There are no features associated with this tag!" |
| 153 |
raise OutOfBounds(msg) |
| 154 |
|
| 155 |
try: |
| 156 |
feat = self.features[featidx] |
| 157 |
except KeyError: |
| 158 |
feat = None |
| 159 |
for feature in self.features: |
| 160 |
if feature.data.name == featidx or feature.data.id == featidx: |
| 161 |
feat = feature |
| 162 |
break |
| 163 |
if feat is None: |
| 164 |
raise |
| 165 |
data = feat.data |
| 166 |
if data is None: |
| 167 |
raise UninitializedEntity() |
| 168 |
if feat.link_type == LinkType.Tagged: |
| 169 |
slices = self._calc_data_slices_mtag(data, posidx, stop_rule) |
| 170 |
if not self._slices_in_data(data, slices): |
| 171 |
raise OutOfBounds("Requested data slice out of the extent " |
| 172 |
"of the Feature!") |
| 173 |
return DataView(data, slices) |
| 174 |
if feat.link_type == LinkType.Indexed: |
| 175 |
if posidx > data.data_extent[0]: |
| 176 |
raise OutOfBounds("Position is larger than the data stored " |
| 177 |
"in the Feature!") |
| 178 |
slices = [slice(posidx, posidx + 1)] |
| 179 |
slices.extend(slice(0, stop) for stop in data.data_extent[1:]) |
| 180 |
|
| 181 |
if not self._slices_in_data(data, slices): |
| 182 |
msg = "Requested data slice out of the extent of the Feature!" |
| 183 |
raise OutOfBounds(msg) |
| 184 |
return DataView(data, slices) |
| 185 |
|
| 186 |
slices = tuple(slice(0, stop) for stop in data.data_extent) |
| 187 |
return DataView(data, slices) |
| 188 |
|
| 189 |
@property |
| 190 |
def sources(self): |
| 191 |
""" |
| 192 |
A property containing all Sources referenced by the MultiTag. Sources |
| 193 |
can be obtained by index or their id. Sources can be removed from the |
| 194 |
list, but removing a referenced Source will not remove it from the |
| 195 |
file. New Sources can be added using the append method of the list. |
| 196 |
This is a read only attribute. |
| 197 |
""" |
| 198 |
if self._sources is None: |
| 199 |
self._sources = SourceLinkContainer(self) |
| 200 |
return self._sources |
| 201 |
|
| 202 |
|
| 203 |
@property |
| 204 |
def metadata(self): |
| 205 |
""" |
| 206 |
|
| 207 |
Associated metadata of the entity. Sections attached to the entity via |
| 208 |
this attribute can provide additional annotations. This is an optional |
| 209 |
read-write property, and can be None if no metadata is available. |
| 210 |
|
| 211 |
:type: nixio.Section |
| 212 |
""" |
| 213 |
if "metadata" in self._h5group: |
| 214 |
return Section(self.file, None, self._h5group.open_group("metadata")) |
| 215 |
else: |
| 216 |
return None |
| 217 |
|
| 218 |
@metadata.setter |
| 219 |
def metadata(self, sect): |
| 220 |
if not isinstance(sect, Section): |
| 221 |
raise TypeError("{} is not of type Section".format(sect)) |
| 222 |
self._h5group.create_link(sect, "metadata") |
| 223 |
|
| 224 |
@metadata.deleter |
| 225 |
def metadata(self): |
| 226 |
if "metadata" in self._h5group: |
| 227 |
self._h5group.delete("metadata") |