Skip to content

Commit

Permalink
ENH: Support passing depletion reader settings at construction (#516)
Browse files Browse the repository at this point in the history
* enh: support metadataKeys through depletion reader init

* enh: support passing materials to DepletionReader init

* enh: support passing processTotal to DepletionReader

* dev: move io import next to std lib in test_depletion.py

* enh: support passing materialVariables to DepletionReader construction

* DOC: Add versionadded directive to DepletionReader init settings

* DOC: Start 0.11.0 changelog with depletion reader constructor changes
  • Loading branch information
drewejohnson authored Jun 12, 2024
1 parent 3260a70 commit e7d97bf
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
7 changes: 7 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
Changelog
=========

.. _v0.11.0:

0.11.0
======

* Depletion reader settings can be provided at construction - :pull:`516`

.. _v0.10.1:

:release-tag:`0.10.1`
Expand Down
53 changes: 50 additions & 3 deletions src/serpentTools/parsers/depletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,37 @@ class DepletionReader(DepPlotMixin, MaterialReader):
----------
filePath: str
path to the depletion file
materials : list of string, optional
Names and patterns to use when determining if a material found
in the file should be processed or not. ``["fuel"]`` will
result in only the material exactly named ``fuel`` to be processed.
But ``["fuel.*"]`` is a pattern that will match and collect any
material that begins with ``fuel``. If not provided,
pulls from ``depletion.materials`` setting.
.. versionadded:: 0.11.0
materialVariables : list of string, optional
Names of variables to pull for each processed material. List can
contain zero or more entries like ``"ADENS"``, ``"MDENS"``, and
``"INH_TOX"``. If not provided, pulls from
``depletion.materialVariables`` setting.
.. versionadded:: 0.11.0
metadataKeys : list of string, optional
Metadata fields to pull from the file. List can contain
zero or more of the following strings: ``"ZAI"``, ``"NAMES"``,
``"DAYS",`` and ``"BU"`` (case sensitive). If not provided,
pulls from ``depletion.metadataKeys`` setting
.. versionadded:: 0.11.0
processTotal : bool, optional
Flag to process the ``TOTAL`` data section or not. If not given,
pulls from the ``depletion.processTotal`` setting.
.. versionadded:: 0.11.0
Attributes
----------
Expand All @@ -170,6 +201,7 @@ class DepletionReader(DepPlotMixin, MaterialReader):
of this reader
"""

docAttrs = """materials: dict
Dictionary with material names as keys and the corresponding
:py:class:`~serpentTools.objects.materials.DepletedMaterial` class
Expand All @@ -178,9 +210,24 @@ class DepletionReader(DepPlotMixin, MaterialReader):
Dictionary with file-wide data names as keys and the
corresponding data, e.g. ``'zai'``: [list of zai numbers]"""

def __init__(self, filePath):
MaterialReader.__init__(self, filePath, 'depletion')
patterns = self.settings['materials'] or ['.*']
def __init__(
self,
filePath,
materials=None,
materialVariables=None,
metadataKeys=None,
processTotal=None,
):
MaterialReader.__init__(self, filePath, "depletion")
if metadataKeys is not None:
self.settings["metadataKeys"] = metadataKeys
if materials is not None:
self.settings["materials"] = materials
if processTotal is not None:
self.settings["processTotal"] = processTotal
if materialVariables is not None:
self.settings["materialVariables"] = materialVariables
patterns = self.settings["materials"] or [".*"]
# match all materials if nothing given
self._matPatterns = [re.compile(mat) for mat in patterns]
DepPlotMixin.__init__(self)
Expand Down
42 changes: 41 additions & 1 deletion tests/test_depletion.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Test the depletion file."""

from unittest import TestCase
from os import remove
from io import BytesIO

from numpy import array
from numpy.testing import assert_equal
import pytest

from io import BytesIO
from serpentTools.data import getFile
from serpentTools.settings import rc
from serpentTools.parsers.depletion import (
Expand Down Expand Up @@ -434,3 +436,41 @@ def constructExpectedVarName(material, variable):


del DepMatlabExportHelper


def test_defaultSettings():
"""Test behavior of settings if no additional arguments given"""
r = DepletionReader(DEP_FILE_PATH)
assert r.settings["metadataKeys"] == ["ZAI", "NAMES", "DAYS", "BU"]
assert r.settings["materials"] == []
assert r.settings["processTotal"]
assert r.settings["materialVariables"] == []


def test_simpleOverloadSettings():
"""Test overwritting some settings and the behavior"""
base = DepletionReader(DEP_FILE_PATH)
base.read()
alt = DepletionReader(
DEP_FILE_PATH,
materials=["f.*"],
materialVariables=["ADENS", "INH_TOX"],
metadataKeys=["ZAI"],
processTotal=False,
)
assert not alt.settings["processTotal"]
alt.read()
assert set(alt.metadata) == {
"zai",
}
assert alt.zais == base.zais
assert set(alt.materials) == {
"fuel",
}
fuel = alt["fuel"]
assert set(fuel.data) == {"adens", "inhTox"}
assert fuel["adens"] == pytest.approx(base["fuel"]["adens"])
assert fuel["inhTox"] == pytest.approx(base["fuel"]["inhTox"])
assert alt.days is None
assert alt.burnup is None
assert alt.names is None

0 comments on commit e7d97bf

Please sign in to comment.