From a42d953f0f9b58f0df7eb2a9bfbe0fb08702d1fc Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Mon, 3 Jun 2024 13:03:15 -0700 Subject: [PATCH 1/7] enh: support metadataKeys through depletion reader init --- src/serpentTools/parsers/depletion.py | 14 +++++++++++--- tests/test_depletion.py | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/serpentTools/parsers/depletion.py b/src/serpentTools/parsers/depletion.py index 18ade582..2ab4f766 100644 --- a/src/serpentTools/parsers/depletion.py +++ b/src/serpentTools/parsers/depletion.py @@ -147,6 +147,11 @@ class DepletionReader(DepPlotMixin, MaterialReader): ---------- filePath: str path to the depletion file + 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 Attributes ---------- @@ -170,6 +175,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 @@ -178,9 +184,11 @@ 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, metadataKeys=None): + MaterialReader.__init__(self, filePath, "depletion") + if metadataKeys is not None: + self.settings["metadataKeys"] = metadataKeys + patterns = self.settings["materials"] or [".*"] # match all materials if nothing given self._matPatterns = [re.compile(mat) for mat in patterns] DepPlotMixin.__init__(self) diff --git a/tests/test_depletion.py b/tests/test_depletion.py index 4e2c5d7f..8595bc25 100644 --- a/tests/test_depletion.py +++ b/tests/test_depletion.py @@ -1,4 +1,5 @@ """Test the depletion file.""" + from unittest import TestCase from os import remove @@ -434,3 +435,25 @@ 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"] + + +def test_simpleOverloadSettings(): + """Test overwritting some settings and the behavior""" + base = DepletionReader(DEP_FILE_PATH) + base.read() + alt = DepletionReader(DEP_FILE_PATH, metadataKeys=["ZAI"]) + alt.read() + assert set(alt.metadata) == { + "zai", + } + assert alt.zais == base.zais + assert set(alt.materials) == set(base.materials) + assert alt.days is None + assert alt.burnup is None + assert alt.names is None From e889d833e2cdad6e85e689ba8c0a914c470e3be9 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Mon, 3 Jun 2024 13:15:37 -0700 Subject: [PATCH 2/7] enh: support passing materials to DepletionReader init --- src/serpentTools/parsers/depletion.py | 11 ++++++++++- tests/test_depletion.py | 9 +++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/serpentTools/parsers/depletion.py b/src/serpentTools/parsers/depletion.py index 2ab4f766..1325d2a6 100644 --- a/src/serpentTools/parsers/depletion.py +++ b/src/serpentTools/parsers/depletion.py @@ -147,6 +147,13 @@ 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. metadataKeys : list of string, optional Metadata fields to pull from the file. List can contain zero or more of the following strings: ``"ZAI"``, ``"NAMES"``, @@ -184,10 +191,12 @@ 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, metadataKeys=None): + def __init__(self, filePath, materials=None, metadataKeys=None): MaterialReader.__init__(self, filePath, "depletion") if metadataKeys is not None: self.settings["metadataKeys"] = metadataKeys + if materials is not None: + self.settings["materials"] = materials patterns = self.settings["materials"] or [".*"] # match all materials if nothing given self._matPatterns = [re.compile(mat) for mat in patterns] diff --git a/tests/test_depletion.py b/tests/test_depletion.py index 8595bc25..15701e89 100644 --- a/tests/test_depletion.py +++ b/tests/test_depletion.py @@ -441,19 +441,24 @@ 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"] == [] def test_simpleOverloadSettings(): """Test overwritting some settings and the behavior""" base = DepletionReader(DEP_FILE_PATH) base.read() - alt = DepletionReader(DEP_FILE_PATH, metadataKeys=["ZAI"]) + alt = DepletionReader( + DEP_FILE_PATH, + materials=["f.*"], + metadataKeys=["ZAI"], + ) alt.read() assert set(alt.metadata) == { "zai", } assert alt.zais == base.zais - assert set(alt.materials) == set(base.materials) + assert set(alt.materials) == {"fuel", "total"} assert alt.days is None assert alt.burnup is None assert alt.names is None From 7f454a7e84cb714673dc3f2bf569f3c6240fe8d8 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Mon, 3 Jun 2024 13:28:37 -0700 Subject: [PATCH 3/7] enh: support passing processTotal to DepletionReader --- src/serpentTools/parsers/depletion.py | 9 ++++++++- tests/test_depletion.py | 7 ++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/serpentTools/parsers/depletion.py b/src/serpentTools/parsers/depletion.py index 1325d2a6..5362e20a 100644 --- a/src/serpentTools/parsers/depletion.py +++ b/src/serpentTools/parsers/depletion.py @@ -159,6 +159,9 @@ class DepletionReader(DepPlotMixin, MaterialReader): zero or more of the following strings: ``"ZAI"``, ``"NAMES"``, ``"DAYS",`` and ``"BU"`` (case sensitive). If not provided, pulls from ``depletion.metadataKeys`` setting + processTotal : bool, optional + Flag to process the ``TOTAL`` data section or not. If not given, + pulls from the ``depletion.processTotal`` setting. Attributes ---------- @@ -191,12 +194,16 @@ 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, materials=None, metadataKeys=None): + def __init__( + self, filePath, materials=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 patterns = self.settings["materials"] or [".*"] # match all materials if nothing given self._matPatterns = [re.compile(mat) for mat in patterns] diff --git a/tests/test_depletion.py b/tests/test_depletion.py index 15701e89..b39b3f40 100644 --- a/tests/test_depletion.py +++ b/tests/test_depletion.py @@ -442,6 +442,7 @@ def test_defaultSettings(): r = DepletionReader(DEP_FILE_PATH) assert r.settings["metadataKeys"] == ["ZAI", "NAMES", "DAYS", "BU"] assert r.settings["materials"] == [] + assert r.settings["processTotal"] def test_simpleOverloadSettings(): @@ -452,13 +453,17 @@ def test_simpleOverloadSettings(): DEP_FILE_PATH, materials=["f.*"], 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", "total"} + assert set(alt.materials) == { + "fuel", + } assert alt.days is None assert alt.burnup is None assert alt.names is None From ce492b30b64373d8c6f3399c94cf68c69a46e5c6 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Mon, 3 Jun 2024 13:32:31 -0700 Subject: [PATCH 4/7] dev: move io import next to std lib in test_depletion.py --- tests/test_depletion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_depletion.py b/tests/test_depletion.py index b39b3f40..cada745c 100644 --- a/tests/test_depletion.py +++ b/tests/test_depletion.py @@ -2,11 +2,11 @@ from unittest import TestCase from os import remove +from io import BytesIO from numpy import array from numpy.testing import assert_equal -from io import BytesIO from serpentTools.data import getFile from serpentTools.settings import rc from serpentTools.parsers.depletion import ( From 081323f6ef4804246881975dec7ea3b79da266b4 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Mon, 3 Jun 2024 13:38:11 -0700 Subject: [PATCH 5/7] enh: support passing materialVariables to DepletionReader construction --- src/serpentTools/parsers/depletion.py | 14 +++++++++++++- tests/test_depletion.py | 7 +++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/serpentTools/parsers/depletion.py b/src/serpentTools/parsers/depletion.py index 5362e20a..2b0c1d96 100644 --- a/src/serpentTools/parsers/depletion.py +++ b/src/serpentTools/parsers/depletion.py @@ -154,6 +154,11 @@ class DepletionReader(DepPlotMixin, MaterialReader): But ``["fuel.*"]`` is a pattern that will match and collect any material that begins with ``fuel``. If not provided, pulls from ``depletion.materials`` setting. + 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. metadataKeys : list of string, optional Metadata fields to pull from the file. List can contain zero or more of the following strings: ``"ZAI"``, ``"NAMES"``, @@ -195,7 +200,12 @@ class DepletionReader(DepPlotMixin, MaterialReader): corresponding data, e.g. ``'zai'``: [list of zai numbers]""" def __init__( - self, filePath, materials=None, metadataKeys=None, processTotal=None + self, + filePath, + materials=None, + materialVariables=None, + metadataKeys=None, + processTotal=None, ): MaterialReader.__init__(self, filePath, "depletion") if metadataKeys is not None: @@ -204,6 +214,8 @@ def __init__( 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] diff --git a/tests/test_depletion.py b/tests/test_depletion.py index cada745c..de56a251 100644 --- a/tests/test_depletion.py +++ b/tests/test_depletion.py @@ -6,6 +6,7 @@ from numpy import array from numpy.testing import assert_equal +import pytest from serpentTools.data import getFile from serpentTools.settings import rc @@ -443,6 +444,7 @@ def test_defaultSettings(): assert r.settings["metadataKeys"] == ["ZAI", "NAMES", "DAYS", "BU"] assert r.settings["materials"] == [] assert r.settings["processTotal"] + assert r.settings["materialVariables"] == [] def test_simpleOverloadSettings(): @@ -452,6 +454,7 @@ def test_simpleOverloadSettings(): alt = DepletionReader( DEP_FILE_PATH, materials=["f.*"], + materialVariables=["ADENS", "INH_TOX"], metadataKeys=["ZAI"], processTotal=False, ) @@ -464,6 +467,10 @@ def test_simpleOverloadSettings(): 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 From a1aebfde9d7b3a13fec5d5cd61b34d4126ac801e Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 11 Jun 2024 11:03:00 -0700 Subject: [PATCH 6/7] DOC: Add versionadded directive to DepletionReader init settings --- src/serpentTools/parsers/depletion.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/serpentTools/parsers/depletion.py b/src/serpentTools/parsers/depletion.py index 2b0c1d96..f30ff960 100644 --- a/src/serpentTools/parsers/depletion.py +++ b/src/serpentTools/parsers/depletion.py @@ -154,20 +154,31 @@ class DepletionReader(DepPlotMixin, MaterialReader): 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 ---------- days : numpy.ndarray or None From f2b928221e5926a6bb3926db8efc632332f18308 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 11 Jun 2024 11:03:21 -0700 Subject: [PATCH 7/7] DOC: Start 0.11.0 changelog with depletion reader constructor changes --- docs/changelog.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index ef0dd7f9..644db99a 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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`