From 4fa6532a7d5f016e319e5540012bf4de63298dc7 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 18 Jun 2024 09:19:36 -0700 Subject: [PATCH 1/3] DEP Use longdouble as numpy 2 removed longfloat Fixes #515 --- src/serpentTools/parsers/depmatrix.py | 14 ++++++------- tests/test_depmtx.py | 8 +++---- tests/test_engines.py | 30 +++++++++++++-------------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/serpentTools/parsers/depmatrix.py b/src/serpentTools/parsers/depmatrix.py index 3bab5313..a87f3f3e 100644 --- a/src/serpentTools/parsers/depmatrix.py +++ b/src/serpentTools/parsers/depmatrix.py @@ -3,7 +3,7 @@ """ import re -from numpy import empty, empty_like, longfloat, zeros +from numpy import empty, empty_like, longdouble, zeros from serpentTools.parsers.base import BaseReader, SparseReaderMixin from serpentTools.messages import SerpentToolsException @@ -98,7 +98,7 @@ def _read(self): match = self._getMatch(line, NDENS_REGEX, 'n0 vector') line = _parseIsoBlock(stream, tempN0, match, line, NDENS_REGEX) numIso = len(tempN0) - self.n0 = empty(numIso, dtype=longfloat) + self.n0 = empty(numIso, dtype=longdouble) for index in range(numIso): self.n0[index] = tempN0.pop(index) @@ -120,13 +120,13 @@ def _read(self): _parseIsoBlock(stream, self.n1, match, line, NDENS_REGEX) def __processDenseMatrix(self, stream, matrixSize): - self.depmtx = zeros(matrixSize, dtype=longfloat) + self.depmtx = zeros(matrixSize, dtype=longdouble) line = stream.readline() match = self._getMatch(line, DEPMTX_REGEX, 'depletion matrix') while match: row, col = [int(xx) - 1 for xx in match.groups()[:2]] - value = longfloat(match.groups()[2]) - self.depmtx[row, col] = longfloat(value) + value = longdouble(match.groups()[2]) + self.depmtx[row, col] = longdouble(value) line = stream.readline() match = DEPMTX_REGEX.search(line) return line @@ -136,11 +136,11 @@ def __processSparseMatrix(self, stream, matrixSize): from serpentTools.parsers.base import CSCStreamProcessor cscProcessor = CSCStreamProcessor( - stream, DEPMTX_REGEX, longfloat) + stream, DEPMTX_REGEX, longdouble) line = cscProcessor.process() self.depmtx = csc_matrix( (cscProcessor.data[:, 0], cscProcessor.indices, - cscProcessor.indptr), dtype=longfloat, shape=matrixSize) + cscProcessor.indptr), dtype=longdouble, shape=matrixSize) return line diff --git a/tests/test_depmtx.py b/tests/test_depmtx.py index 9a45efb2..f764fb1f 100644 --- a/tests/test_depmtx.py +++ b/tests/test_depmtx.py @@ -1,6 +1,6 @@ from unittest import TestCase -from numpy import array, longfloat, subtract, matrix +from numpy import array, longdouble, subtract, matrix from numpy.testing import assert_array_equal from serpentTools.data import getFile @@ -92,7 +92,7 @@ class DepmtxTestHelper(TestCase): 2.108367055387284252194156353166E-07, 9.841801952371811001202615665146E-12, 8.052266563002563381135156316475E-16, - ], dtype=longfloat) + ], dtype=longdouble) REF_ZAI = array([ -1, 10010, 10020, 10030, 20030, 20040, 30060, 30070, 40090, 50100, 50110, 60120, 70140, 70150, 80160, 80170, 561380, 561400, 581380, @@ -179,7 +179,7 @@ class DepmtxTestHelper(TestCase): 2.108922382983142802931309477132E-07, 9.823435767284427578223342980928E-12, 8.030354784613487489140522453118E-16, - ], dtype=longfloat) + ], dtype=longdouble) # Values for comparing depletion matrix # Taking the first and last N_MTX values @@ -236,7 +236,7 @@ class DepmtxTestHelper(TestCase): 2.166500765256806589294628619590E-14, 8.673595631793918327440743302798E-12, -3.205752291225075667717126458572E-09, - ], dtype=longfloat) + ], dtype=longdouble) def test_vectors_deltaT(self): """Verify the isotopics are stored correctly.""" diff --git a/tests/test_engines.py b/tests/test_engines.py index fe1fdeed..64998579 100644 --- a/tests/test_engines.py +++ b/tests/test_engines.py @@ -12,7 +12,7 @@ import re from numpy.testing import assert_array_equal, assert_allclose -from numpy import longfloat, array, dtype +from numpy import longdouble, array, dtype from serpentTools.parsers.base import CSCStreamProcessor @@ -90,9 +90,9 @@ class FloatProcessor(object): datatype = float -class LongFloatProcessor(object): +class longdoubleProcessor(object): """Class that stores data as long floats.""" - datatype = longfloat + datatype = longdouble class CaseOneStringFloatTester( @@ -102,10 +102,10 @@ class CaseOneStringFloatTester( """ -class CaseOneStringLongfloatTester( - CSCStreamTester, CaseOneStringBasedProcessor, LongFloatProcessor): +class CaseOneStringlongdoubleTester( + CSCStreamTester, CaseOneStringBasedProcessor, longdoubleProcessor): """ - Class that reads the first case with string-regex and stores longfloats. + Class that reads the first case with string-regex and stores longdoubles. """ @@ -116,10 +116,10 @@ class CaseTwoStringFloatTester( """ -class CaseTwoStringLongfloatTester( - CSCStreamTester, CaseTwoStringBasedProcessor, LongFloatProcessor): +class CaseTwoStringlongdoubleTester( + CSCStreamTester, CaseTwoStringBasedProcessor, longdoubleProcessor): """ - Class that reads the second case with string-regex and stores longfloats. + Class that reads the second case with string-regex and stores longdoubles. """ @@ -130,10 +130,10 @@ class CaseOneRegexFloatTester( """ -class CaseOneRegexLongfloatTester( - CSCStreamTester, CaseOneRegexBasedProcessor, LongFloatProcessor): +class CaseOneRegexlongdoubleTester( + CSCStreamTester, CaseOneRegexBasedProcessor, longdoubleProcessor): """ - Class that reads the first case with compiled regex and stores longfloats. + Class that reads the first case with compiled regex and stores longdoubles. """ @@ -144,10 +144,10 @@ class CaseTwoRegexFloatTester( """ -class CaseTwoRegexLongfloatTester( - CSCStreamTester, CaseTwoRegexBasedProcessor, LongFloatProcessor): +class CaseTwoRegexlongdoubleTester( + CSCStreamTester, CaseTwoRegexBasedProcessor, longdoubleProcessor): """ - Class that reads the second case with compiled regex and stores longfloats. + Class that reads the second case with compiled regex and stores longdoubles. """ From 25b764cf61bd27c2419b227682917aeb8e808a96 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 18 Jun 2024 09:29:19 -0700 Subject: [PATCH 2/3] DEP Use numpy.char over numpy.core.defchararray The latter produces the following deprecation warning: > DeprecationWarning: numpy.core.defchararray is deprecated and has been renamed to numpy._core.defchararray. The numpy._core namespace contains private NumPy internals and its use is discouraged, as NumPy internals can change without warning in any release. In practice, most real-world usage of numpy.core is to access functionality in the public NumPy API. If that is the case, use the public NumPy API. If not, you are using NumPy internals. If you would still like to access an internal attribute, use numpy._core.defchararray.equal. from numpy.core.defchararray import equal as charEqual Eventually we should start using `numpy.strings` once we move to `numpy>=2.0` since `numpy.char` is deprecated and will not receive updates. From https://numpy.org/doc/stable/reference/routines.char.html#module-numpy.char > This submodule is considered legacy and will no longer receive updates. This could also mean it will be removed in future NumPy versions. The string operations in this module, as well as the numpy.char.chararray class, are planned to be deprecated in the future. Use numpy.strings instead --- src/serpentTools/utils/compare.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/serpentTools/utils/compare.py b/src/serpentTools/utils/compare.py index c9c8841a..336c97b2 100644 --- a/src/serpentTools/utils/compare.py +++ b/src/serpentTools/utils/compare.py @@ -3,10 +3,9 @@ """ from collections.abc import Iterable -from numpy.core.defchararray import equal as charEqual from numpy import ( fabs, zeros_like, ndarray, array, greater, multiply, subtract, - equal, asarray, + equal, asarray, char ) from serpentTools.messages import ( @@ -190,8 +189,8 @@ def directCompare(obj0, obj1, lower, upper): def _directCompareIdentical(obj0, obj1): """Compare arrays that should be identical""" # special case for strings - if obj0.dtype.name[:3] == 'str': - compArray = charEqual(obj0, obj1) + if obj0.dtype.name.startswith("str"): + compArray = char.equal(obj0, obj1) else: compArray = equal(obj0, obj1) if compArray.all(): From 47756dff059602a9026c92938f5be488b9f6a29b Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 18 Jun 2024 09:40:29 -0700 Subject: [PATCH 3/3] DOC Add numpy 2.0 support to changelog --- docs/changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 644db99a..b013778a 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -12,6 +12,7 @@ Changelog ====== * Depletion reader settings can be provided at construction - :pull:`516` +* Support ``numpy`` 2.0 - :pull:`524` .. _v0.10.1: