forked from h5py/h5py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dims.py
184 lines (147 loc) · 5.21 KB
/
dims.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# This file is part of h5py, a Python interface to the HDF5 library.
#
# http://www.h5py.org
#
# Copyright 2008-2013 Andrew Collette and contributors
#
# License: Standard 3-clause BSD; see "license.txt" for full license terms
# and contributor agreement.
"""
Implements support for HDF5 dimension scales.
"""
from __future__ import absolute_import
from .. import h5ds
from . import base
from .base import phil, with_phil
from .dataset import Dataset
class DimensionProxy(base.CommonStateObject):
"""
Represents an HDF5 "dimension".
"""
@property
@with_phil
def label(self):
""" Get or set the dimension scale label """
#return h5ds.get_label(self._id, self._dimension)
# Produces a segfault for a non-existent label (Fixed in hdf5-1.8.8).
# Here is a workaround:
try:
dset = Dataset(self._id)
return self._d(dset.attrs['DIMENSION_LABELS'][self._dimension])
except (KeyError, IndexError):
return ''
@label.setter
@with_phil
def label(self, val):
# pylint: disable=missing-docstring
h5ds.set_label(self._id, self._dimension, self._e(val))
@with_phil
def __init__(self, id_, dimension):
self._id = id_
self._dimension = dimension
@with_phil
def __hash__(self):
return hash((type(self), self._id, self._dimension))
@with_phil
def __eq__(self, other):
return hash(self) == hash(other)
@with_phil
def __iter__(self):
for k in self.keys():
yield k
@with_phil
def __len__(self):
return h5ds.get_num_scales(self._id, self._dimension)
@with_phil
def __getitem__(self, item):
if isinstance(item, int):
scales = []
h5ds.iterate(self._id, self._dimension, scales.append, 0)
return Dataset(scales[item])
else:
def f(dsid):
""" Iterate over scales to find a matching name """
if h5ds.get_scale_name(dsid) == self._e(item):
return dsid
res = h5ds.iterate(self._id, self._dimension, f, 0)
if res is None:
raise KeyError(item)
return Dataset(res)
def attach_scale(self, dset):
""" Attach a scale to this dimension.
Provide the Dataset of the scale you would like to attach.
"""
with phil:
h5ds.attach_scale(self._id, dset.id, self._dimension)
def detach_scale(self, dset):
""" Remove a scale from this dimension.
Provide the Dataset of the scale you would like to remove.
"""
with phil:
h5ds.detach_scale(self._id, dset.id, self._dimension)
def items(self):
""" Get a list of (name, Dataset) pairs with all scales on this
dimension.
"""
with phil:
scales = []
# H5DSiterate raises an error if there are no dimension scales,
# rather than iterating 0 times. See #483.
if len(self) > 0:
h5ds.iterate(self._id, self._dimension, scales.append, 0)
return [
(self._d(h5ds.get_scale_name(x)), Dataset(x))
for x in scales
]
def keys(self):
""" Get a list of names for the scales on this dimension. """
with phil:
return [key for (key, _) in self.items()]
def values(self):
""" Get a list of Dataset for scales on this dimension. """
with phil:
return [val for (_, val) in self.items()]
@with_phil
def __repr__(self):
if not self._id:
return "<Dimension of closed HDF5 dataset>"
return ('<"%s" dimension %d of HDF5 dataset at %s>'
% (self.label, self._dimension, id(self._id)))
class DimensionManager(base.MappingHDF5, base.CommonStateObject):
"""
Represents a collection of dimension associated with a dataset.
Like AttributeManager, an instance of this class is returned when
accessing the ".dims" property on a Dataset.
"""
@with_phil
def __init__(self, parent):
""" Private constructor.
"""
self._id = parent.id
@with_phil
def __getitem__(self, index):
""" Return a Dimension object
"""
if index > len(self) - 1:
raise IndexError('Index out of range')
return DimensionProxy(self._id, index)
@with_phil
def __len__(self):
""" Number of dimensions associated with the dataset. """
return len(Dataset(self._id).shape)
@with_phil
def __iter__(self):
""" Iterate over the dimensions. """
for i in range(len(self)):
yield self[i]
@with_phil
def __repr__(self):
if not self._id:
return "<Dimensions of closed HDF5 dataset>"
return "<Dimensions of HDF5 object at %s>" % id(self._id)
def create_scale(self, dset, name=''):
""" Create a new dimension, from an initial scale.
Provide the dataset and a name for the scale.
"""
with phil:
h5ds.set_scale(dset.id, self._e(name))