Skip to content

Commit e519b94

Browse files
committed
Fix Python PointFilter HDF5 deserialization and next-event estimator type
PointFilter lacked from_hdf5() and get_pandas_dataframe() overrides. The base Filter.from_hdf5() passed a flat numpy array to PointFilter.__init__ which expects Sequence[tuple], causing TypeError. The base get_pandas_dataframe() used np.repeat on the nested tuple structure, causing ValueError. Also add 'next-event' to ESTIMATOR_TYPES so the Tally.estimator setter accepts the value read from statepoint files. Fixes: - Add PointFilter.from_hdf5() that reconstructs (pos, r0) tuples from flat HDF5 bins array. - Add PointFilter.get_pandas_dataframe() that formats detector positions as readable string labels. - Add 'next-event' to ESTIMATOR_TYPES set in tallies.py.
1 parent 6c9e2f5 commit e519b94

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

openmc/filter.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,28 @@ def bins(self, bins):
834834
cv.check_length(f'bins[{i}][0]', item[0], 3, 3)
835835
cv.check_type(f'bins[{i}][1]', item[1], Real)
836836
self._bins = bins
837+
838+
@classmethod
839+
def from_hdf5(cls, group, **kwargs):
840+
filter_id = int(group.name.split('/')[-1].lstrip('filter '))
841+
flat = group['bins'][()]
842+
# Reconstruct tuple structure: every 4 values = (x, y, z, r0)
843+
bins = []
844+
for i in range(0, len(flat), 4):
845+
pos = (float(flat[i]), float(flat[i+1]), float(flat[i+2]))
846+
r0 = float(flat[i+3])
847+
bins.append((pos, r0))
848+
out = cls(bins, filter_id=filter_id)
849+
out._num_bins = group['n_bins'][()]
850+
return out
851+
852+
def get_pandas_dataframe(self, data_size, stride, **kwargs):
853+
import pandas as pd
854+
labels = [f"({p[0]}, {p[1]}, {p[2]}) R0={r}" for (p, r) in self.bins]
855+
filter_bins = np.repeat(labels, stride)
856+
tile_factor = data_size // len(filter_bins)
857+
filter_bins = np.tile(filter_bins, tile_factor)
858+
return pd.DataFrame({self.short_name.lower(): filter_bins})
837859

838860
def to_xml_element(self):
839861
"""Return XML Element representing the Filter.

openmc/tallies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
_FILTER_CLASSES = (openmc.Filter, openmc.CrossFilter, openmc.AggregateFilter)
3737

3838
# Valid types of estimators
39-
ESTIMATOR_TYPES = {'tracklength', 'collision', 'analog'}
39+
ESTIMATOR_TYPES = {'tracklength', 'collision', 'analog', 'next-event'}
4040

4141

4242
class Tally(IDManagerMixin):

0 commit comments

Comments
 (0)