Skip to content

Commit

Permalink
ENH Support numpy 2 (#524)
Browse files Browse the repository at this point in the history
* DEP Use longdouble as numpy 2 removed longfloat

Fixes #515

* 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

* DOC Add numpy 2.0 support to changelog
  • Loading branch information
drewejohnson authored Jun 18, 2024
1 parent 07b0b2e commit 729678a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Changelog
======

* Depletion reader settings can be provided at construction - :pull:`516`
* Support ``numpy`` 2.0 - :pull:`524`

.. _v0.10.1:

Expand Down
14 changes: 7 additions & 7 deletions src/serpentTools/parsers/depmatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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

Expand Down
7 changes: 3 additions & 4 deletions src/serpentTools/utils/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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():
Expand Down
8 changes: 4 additions & 4 deletions tests/test_depmtx.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down
30 changes: 15 additions & 15 deletions tests/test_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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.
"""


Expand All @@ -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.
"""


Expand All @@ -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.
"""


Expand All @@ -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.
"""


Expand Down

0 comments on commit 729678a

Please sign in to comment.