Cannot get_slice()

Hello!
  I am trying to get a tally slice for certain energy bin. For this
purpose I do the following:

sp3d = StatePoint('statepoint.1000.h5')
tally3d = sp3d.get_tally(scores=['fission'])
energy3d = tally3d.get_slice(filters=['energy'],filter_bins=[(1,20)])

which prints the following error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.linux-x86_64/egg/openmc/tallies.py", line 2363, in get_slice
  File "build/bdist.linux-x86_64/egg/openmc/tallies.py", line 1084, in get_values
  File "build/bdist.linux-x86_64/egg/openmc/tallies.py", line 937, in get_filter_indices
  File "build/bdist.linux-x86_64/egg/openmc/tallies.py", line 780, in get_filter_index
  File "build/bdist.linux-x86_64/egg/openmc/filter.py", line 389, in get_bin_index
TypeError: 'int' object has no attribute '__getitem__'

Is it my mistake or is it a mistake in OpenMC ? (I use OpenMC v.0.7.1)

  Thank you!
    Vladimir

Vladimir,

Thanks for bringing this up - the “Tally.get_slice(…)” routine should work for this use case, but the documentation is not quite correct. I will submit a patch to our code repository shortly to correct this.

In the meantime, try the following instead:

energy3d = tally3d.get_slice(filters=[‘energy’],filter_bins=[((1,20),)])

I believe the only issue is that you must put the filter bins as a “tuple of tuples” which is not correctly documented. Each tuple corresponds to all of the bins to slice for the corresponding filter in the “filters” parameter list. I recognize that this can be confusing and appears a bit clunky, but it is intended to allow one to slice a tally across multiple filters with a single function call.

For example, if one could simultaneously slice off a tally for cell filter bins 1 and 2 and energy filter bins (0, 0.625e-6) and (1e-3, 20.) with the following code:

simultaneous_slice = tally.get_slice(filters=[‘cell’, ‘energy’], filter_bins=[(1, 2), ((0, 0.625e-6), (1.e-3, 20.))])

Let me know if this helps. I apologize for the confusing documentation and will try to correct this shortly.

Best,
Will

Vladimir,

See this patch to the documentation to reflect the appropriate methodology for tally slicing with energy filters:

https://github.com/mit-crpg/openmc/pull/568/files

Best,
Will

Yes, this works. Thank you!
  However, this way it works suspiciously slowly. For example, if I do
a simple python slice like

    fis3d = tally3d.get_slice(scores=['fission'])
    energy_slice = fis3d.mean[start_val:end_val:]

it works more than 10 times faster. I am not sure about the correctness
of such approach though.

  Vladimir

Yes there is a lot more overhead involved with the “slice” method than a direct access into the underlying NumPy array. Tally slicing is useful if you wish to encapsulate the sliced data within a Tally object so that you can easily index the data by filter and/or nuclide and/or score which is not as easy to do with a direct access. In addition, you can use any of Tally class’ method with a sliced tally (pandas dataframes, tally arithmetic, etc.). But if you are concerned with performance then I recommend using direct access. Also, we can likely can greatly improve the performance of the Tally class methods -including “slice” - with further development, but these are such new features that we’ve had little time to do performance optimizations.