Skip to content

Commit 69ca410

Browse files
committed
expose metadata in properties that read from hidden _metadata property
1 parent 9018f3f commit 69ca410

File tree

1 file changed

+19
-36
lines changed

1 file changed

+19
-36
lines changed

funlib/persistence/arrays/array.py

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ class Array(Freezable):
5858
"""
5959

6060
data: da.Array
61-
_voxel_size: Coordinate
62-
_offset: Coordinate
63-
_axis_names: list[str]
64-
_units: list[str]
6561
adapter: Adapter
6662

6763
def __init__(
@@ -76,18 +72,14 @@ def __init__(
7672
):
7773
self.data = da.from_array(data, chunks=chunks)
7874
self._uncollapsed_dims = [True for _ in self.data.shape]
79-
self.voxel_size = (
80-
voxel_size if voxel_size is not None else (1,) * len(data.shape)
81-
)
82-
self.offset = offset if offset is not None else (0,) * len(data.shape)
83-
self.axis_names = (
84-
axis_names
85-
if axis_names is not None
86-
else tuple(f"c{i}^" for i in range(self.channel_dims))
87-
+ tuple(f"d{i}" for i in range(self.voxel_size.dims))
88-
)
89-
self.units = units if units is not None else ("",) * self.voxel_size.dims
9075
self._source_data = data
76+
self._metadata = MetaData(
77+
offset=Coordinate(offset) if offset is not None else None,
78+
voxel_size=Coordinate(voxel_size) if voxel_size is not None else None,
79+
axis_names=list(axis_names) if axis_names is not None else None,
80+
units=list(units) if units is not None else None,
81+
shape=self._source_data.shape,
82+
)
9183

9284
if adapter is not None:
9385
self.apply_adapter(adapter)
@@ -99,22 +91,13 @@ def __init__(
9991

10092
self.validate()
10193

102-
@property
103-
def metadata(self) -> MetaData:
104-
return MetaData(
105-
offset=self._offset,
106-
voxel_size=self._voxel_size,
107-
axis_names=self._axis_names,
108-
units=self._units,
109-
)
110-
11194
@property
11295
def chunk_shape(self) -> Coordinate:
11396
return Coordinate(self.data.chunksize)
11497

11598
def uncollapsed_dims(self, physical: bool = False) -> list[bool]:
11699
if physical:
117-
return self._uncollapsed_dims[-self._voxel_size.dims :]
100+
return self._uncollapsed_dims[-self._metadata.voxel_size.dims :]
118101
else:
119102
return self._uncollapsed_dims
120103

@@ -123,54 +106,54 @@ def offset(self) -> Coordinate:
123106
"""Get the offset of this array in world units."""
124107
return Coordinate(
125108
[
126-
self._offset[ii]
109+
self._metadata.offset[ii]
127110
for ii, uncollapsed in enumerate(self.uncollapsed_dims(physical=True))
128111
if uncollapsed
129112
]
130113
)
131114

132115
@offset.setter
133116
def offset(self, offset: Iterable[int]) -> None:
134-
self._offset = Coordinate(offset)
117+
self._metadata.offset = Coordinate(offset)
135118

136119
@property
137120
def voxel_size(self) -> Coordinate:
138121
"""Get the size of a voxel in world units."""
139122
return Coordinate(
140123
[
141-
self._voxel_size[ii]
124+
self._metadata.voxel_size[ii]
142125
for ii, uncollapsed in enumerate(self.uncollapsed_dims(physical=True))
143126
if uncollapsed
144127
]
145128
)
146129

147130
@voxel_size.setter
148131
def voxel_size(self, voxel_size: Iterable[int]) -> None:
149-
self._voxel_size = Coordinate(voxel_size)
132+
self._metadata.voxel_size = Coordinate(voxel_size)
150133

151134
@property
152135
def units(self) -> list[str]:
153136
return [
154-
self._units[ii]
137+
self._metadata.units[ii]
155138
for ii, uncollapsed in enumerate(self.uncollapsed_dims(physical=True))
156139
if uncollapsed
157140
]
158141

159142
@units.setter
160143
def units(self, units: list[str]) -> None:
161-
self._units = list(units)
144+
self._metadata.units = list(units)
162145

163146
@property
164147
def axis_names(self) -> list[str]:
165148
return [
166-
self._axis_names[ii]
149+
self._metadata.axis_names[ii]
167150
for ii, uncollapsed in enumerate(self.uncollapsed_dims(physical=False))
168151
if uncollapsed
169152
]
170153

171154
@axis_names.setter
172155
def axis_names(self, axis_names):
173-
self._axis_names = list(axis_names)
156+
self._metadata.axis_names = list(axis_names)
174157

175158
@property
176159
def roi(self):
@@ -438,9 +421,9 @@ def __index(self, coordinate):
438421
return index
439422

440423
def validate(self):
441-
self.metadata.validate()
442-
assert len(self._axis_names) == len(self._source_data.shape), (
443-
f"Axis names must be provided for every dimension. Got ({self._axis_names}) "
424+
self._metadata.validate()
425+
assert len(self.axis_names) == len(self._source_data.shape), (
426+
f"Axis names must be provided for every dimension. Got ({self.axis_names}) "
444427
f"but expected {len(self.shape)} to match the data shape: {self.shape}"
445428
)
446429
if self.chunk_shape is not None:

0 commit comments

Comments
 (0)