Added previous/next links to iterations. Some additional reorganization.
@@ -34,8 +34,7 @@ class Segment:
def parent(self):
if not self.iteration.prev:
return None
- return Segment(self.info['parent_id'],
- self.iteration.prev)
+ return Segment(self.info['parent_id'], self.iteration.prev)
@property
def children(self):
@@ -60,29 +59,25 @@ class Iteration:
self.number = number
self.dataset = dataset
+ @property
+ def group(self):
+ return self.dataset.iteration_group(self.number)
+
@property
def segment_info(self):
- return self.dataset.segment_info(self.number)
+ return self.group['seg_index']
@property
def pcoord(self):
- return self.dataset.pcoord(self.number)
+ return self.group['pcoord']
@property
def num_segments(self):
return self.segment_info.shape[0]
@property
- def prev(self):
- if self.number == 1:
- return None
- return self.dataset.iteration(self.number - 1)
-
- @property
- def next(self):
- if self.number == self.dataset.num_iterations:
- return None
- return self.dataset.iteration(self.number + 1)
+ def segment_weights(self):
+ return self.segment_info['weight']
@property
def segments(self):
@@ -94,6 +89,18 @@ class Iteration:
raise ValueError(f'segment index must be in {valid_range}')
return Segment(index, self)
+ @property
+ def prev(self):
+ if self.number == 1:
+ return None
+ return self.dataset.iteration(self.number - 1)
+
+ @property
+ def next(self):
+ if self.number == self.dataset.num_iterations:
+ return None
+ return self.dataset.iteration(self.number + 1)
+
def __eq__(self, other):
return self.number == other.number and self.dataset is other.dataset
@@ -104,16 +111,7 @@ class Iteration:
class WEDataset:
def __init__(self, h5filename):
- self.h5file = h5py.File(h5filename)
-
- def _iter_group(self, iteration_number):
- return self.h5file['iterations'][f'iter_{iteration_number:08d}']
-
- def segment_info(self, iteration_number):
- return self._iter_group(iteration_number)['seg_index']
-
- def pcoord(self, iteration_number):
- return self._iter_group(iteration_number)['pcoord']
+ self.h5file = h5py.File(h5filename, 'r')
@property
def num_iterations(self):
@@ -121,8 +119,7 @@ class WEDataset:
@property
def iterations(self):
- return (Iteration(iteration_number, self)
- for iteration_number in range(1, self.num_iterations + 1))
+ return (Iteration(n, self) for n in range(1, self.num_iterations + 1))
def iteration(self, number):
valid_range = range(1, self.num_iterations + 1)
@@ -130,6 +127,9 @@ class WEDataset:
raise ValueError(f'iteration number must be in {valid_range}')
return Iteration(number, self)
+ def iteration_group(self, number):
+ return self.h5file['iterations'][f'iter_{number:08d}']
+
def __len__(self):
return self.num_iterations
