Repository: CORE-GATECH-GROUP/serpent-tools
Pull request #293 (2019-03-13 11:45:35)

API Deprecate DepletionReader.saveAsMatlab for toMatlab
This PR deprecates the `saveAsMatlab` method for the DepletionReader. By this, warnings are placed when the method is called, alerting the user that `toMatlab` is preferred.
A `deprecated` message is also added to the docstring for documentation.

Tests are modified to reflect this change, by only testing toMatlab, and removing tests that excluded metadata. toMatlab writes all the data present on the object.

Will update the changelog


Patch:
diff --git a/docs/changelog.rst b/docs/changelog.rst
index cdd4492..48ec77b 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -17,6 +17,8 @@ Next
 * Direct ``toMatlab`` methods for |SensitivityReader|, |DepmtxReader|
   |DepletionReader|, |DetectorReader|, |HistoryReader|, and |Detector| objects.
 * Overhaul, reorganization, and cleanup of documentation
+* |DepletionReader| ``saveAsMatlab`` in favor of
+  :meth:`~serpentTools.DepletionReader.toMatlab`
 
 .. _v0.6.2:
 
@@ -29,7 +31,7 @@ Next
 * Add :mod:`serpentTools.io` module for converting objects to
   other data types. Currently a general function for converting
   |toMatlab-short|
-* |DetectorReader| and |Detector| objects can be writen to 
+* |DetectorReader| and |Detector| objects can be written to 
   MATLAB files using |toMatlab-full|
 * |resultReader| can plot data using
   :meth:`~serpentTools.ResultsReader.plot`
diff --git a/serpentTools/parsers/depletion.py b/serpentTools/parsers/depletion.py
index 98379a0..39f9827 100644
--- a/serpentTools/parsers/depletion.py
+++ b/serpentTools/parsers/depletion.py
@@ -15,7 +15,7 @@ from serpentTools.parsers.base import MaterialReader
 from serpentTools.objects.materials import DepletedMaterial
 from serpentTools.utils.core import deconvertVariableName
 from serpentTools.messages import (
-    warning, debug, error, SerpentToolsException,
+    warning, debug, error, SerpentToolsException, deprecated,
 )
 from serpentTools.utils import (
     getKeyMatchingShapes,
@@ -366,12 +366,16 @@ class DepletionReader(DepPlotMixin, MaterialReader):
             lower, upper, 'burnup')
         return similar
 
+    @deprecated("toMatlab")
     def saveAsMatlab(self, fileP, reconvert=True, metadata=None,
                      append=True, format='5', longNames=True,
                      compress=True, oned='row'):
         """
         Write a binary MATLAB file from the contents of this reader
 
+        .. deprecated:: 0.7.0
+            Use :meth:`~serpentTools.DepletionReader.toMatlab`
+
         Parameters
         ----------
         fileP: str or file-like object
@@ -410,12 +414,10 @@ class DepletionReader(DepPlotMixin, MaterialReader):
         :func:`scipy.io.savemat`
         """
         if metadata is not None:
+            warn("metadata argument is deprecated. All metadata written",
+                 FutureWarning)
             # need this path to perserve selecting not to write
             # metadata
-            from scipy.io import savemat
-            data = self._gather_matlab(reconvert, metadata)
-            return savemat(fileP, data, append, format, longNames,
-                           compress, oned)
         converter = MatlabConverter(self, fileP)
         return converter.convert(reconvert, append, format, longNames,
                                  compress, oned)
@@ -428,9 +430,6 @@ class DepletionReader(DepPlotMixin, MaterialReader):
         output file types
         """
 
-        if metadata is not None:
-            warn("version 0.7.0+ will not use metadata argument and write "
-                 "all by default.", FutureWarning)
         # set these here to reduce number of if/elses
 
         if reconvert:
@@ -438,12 +437,8 @@ class DepletionReader(DepPlotMixin, MaterialReader):
         else:
             converter = prepToMatlab
 
-        # remove for v0.7.0
-        if metadata and self.metadata:
-            data = {k.upper() if reconvert else k: v
-                    for k, v in iteritems(self.metadata)}
-        else:
-            data = {}
+        data = {k.upper() if reconvert else k: v
+                for k, v in iteritems(self.metadata)}
 
         for matName, material in iteritems(self.materials):
             for varName, varData in iteritems(material.data):


Test patch:
diff --git a/serpentTools/tests/test_depletion.py b/serpentTools/tests/test_depletion.py
index 268ea3e..bdcc57a 100644
--- a/serpentTools/tests/test_depletion.py
+++ b/serpentTools/tests/test_depletion.py
@@ -1,11 +1,11 @@
 """Test the depletion file."""
-from unittest import TestCase, skipIf
+from unittest import TestCase
 from os import remove
 
 from numpy import array
 from numpy.testing import assert_equal
 
-from six import iteritems
+from six import iteritems, BytesIO
 
 from serpentTools.data import getFile
 from serpentTools.settings import rc
@@ -13,8 +13,7 @@ from serpentTools.parsers.depletion import (
     DepletionReader, getMaterialNameAndVariable, getMatlabVarName,
     prepToMatlab, deconvert,
 )
-from serpentTools.utils import checkScipy
-from serpentTools.tests.utils import LoggerMixin
+from serpentTools.tests.utils import LoggerMixin, MatlabTesterHelper
 
 
 DEP_FILE = 'ref_dep.m'
@@ -262,37 +261,26 @@ class DepletionUtilTester(TestCase):
         self.assertEqual(self.BAD_0, getMatlabVarName(self.VAR_CHUNK[0]))
 
 
-class DepMatlabExportHelper(TestCase):
+class DepMatlabExportHelper(MatlabTesterHelper):
     """
     Class that tests the saveAsMatlab method for DepletionReaders
     """
 
-    MAT_FILE_TEMPLATE = "dep_c{}_m{}.mat"
-
     @classmethod
     def setUpClass(cls):
         cls.reader = DepletionReader(DEP_FILE_PATH)
         cls.reader.read()
 
-    def setUp(self):
-        self.MAT_FILE = self.MAT_FILE_TEMPLATE.format(
-            1 if self.RECONVERT else 0,
-            1 if self.SAVE_METADATA else 0,
-        )
-
-    @skipIf(not checkScipy(), "scipy not installed")
-    def test_saveMatlab(self):
+    def test_toMatlab(self):
         """
         Verify that the reader can write data to a .mat file
         """
         from scipy.io import loadmat
-        self.reader.saveAsMatlab(self.MAT_FILE, self.RECONVERT,
-                                 self.SAVE_METADATA)
-        loaded = loadmat(self.MAT_FILE)
+        stream = BytesIO()
+        self.reader.toMatlab(stream, self.RECONVERT)
+        loaded = loadmat(stream)
         self.check(loaded)
-        if self.SAVE_METADATA:
-            self.checkMetadata(loaded)
-        remove(self.MAT_FILE)
+        self.checkMetadata(loaded)
 
     def check(self, loaded):
         """Check the contents of the data loaded from the .mat file"""
@@ -313,8 +301,10 @@ class DepMatlabExportHelper(TestCase):
                     err_msg="{} {}".format(materialName, variableName))
 
     def checkMetadata(self, loaded):
-        """Check the contents of the metadata loaded from mat file"""
-        pass
+        for key, data in iteritems(self.reader.metadata):
+            expected = key.upper() if self.RECONVERT else key
+            assert expected in loaded, (
+                "Missing {} from metadata".format(expected))
 
     def constructExpectedVarName(self, material, variable):
         """
@@ -323,7 +313,7 @@ class DepMatlabExportHelper(TestCase):
         raise NotImplementedError
 
 
-class DepMatlabExportConverter(object):
+class DepMatlabExportConverter(DepMatlabExportHelper):
     """Mixin class that converts variables back to orgiginal form"""
 
     RECONVERT = True
@@ -333,7 +323,7 @@ class DepMatlabExportConverter(object):
         return deconvert(material, variable)
 
 
-class DepMatlabExportNoConverter(object):
+class DepMatlabExportNoConverter(DepMatlabExportHelper):
     """Mixin class that does not convert to original form"""
 
     RECONVERT = False
@@ -343,48 +333,6 @@ class DepMatlabExportNoConverter(object):
         return prepToMatlab(material, variable)
 
 
-class DepMatlabExportMetadataTester(DepMatlabExportHelper):
-    """Class that sends metadata to the matlab file and checks"""
-
-    SAVE_METADATA = True
-
-    def checkMetadata(self, loaded):
-        for key, data in iteritems(self.reader.metadata):
-            expected = key.upper() if self.RECONVERT else key
-            assert expected in loaded, (
-                "Missing {} from metadata".format(expected))
-
-
-class DepMatlabExportWithoutMetadataTester(DepMatlabExportHelper):
-    """Class that does not send metadata to the matlab file and checks"""
-
-    SAVE_METADATA = False
-
-    def checkMetadata(self, loaded):
-        for key, data in iteritems(self.reader.metadata):
-            assert key not in loaded, (
-                "{} from metadata present and should not be".format(key))
-
-
-class DepMatlabExportConvWithMetadata(
-        DepMatlabExportConverter, DepMatlabExportMetadataTester):
-    """Class that saves a matlab file with converted names and metadata"""
-    pass
-
-
-class DepMatlabExportConvWithoutMetadata(
-        DepMatlabExportConverter, DepMatlabExportWithoutMetadataTester):
-    """Class that saves a matlab file with converted names and no metadata"""
-    pass
-
-
-class DepMatlabExportNoConvWithMetadata(
-        DepMatlabExportNoConverter, DepMatlabExportMetadataTester):
-    """Class that saves a matlab file without converted names and metadata"""
-    pass
-
-
-del DepMatlabExportMetadataTester, DepMatlabExportWithoutMetadataTester
 del DepMatlabExportHelper