diff --git a/PyMca5/PyMcaPhysics/xrf/FastXRFLinearFit.py b/PyMca5/PyMcaPhysics/xrf/FastXRFLinearFit.py index cbda75325..cdbf57735 100644 --- a/PyMca5/PyMcaPhysics/xrf/FastXRFLinearFit.py +++ b/PyMca5/PyMcaPhysics/xrf/FastXRFLinearFit.py @@ -736,7 +736,12 @@ def _fitLstSqReduced(self, data=None, sliceChan=None, mcaIndex=None, @staticmethod def _fitDtypeResult(data): if data.dtype not in [numpy.float32, numpy.float64]: - if data.itemsize < 5: + if hasattr(data, "itemsize"): + if data.itemsize < 5: + return numpy.float32 + else: + return numpy.float64 + elif (data.nbytes / data.size) < 5: return numpy.float32 else: return numpy.float64 diff --git a/PyMca5/__init__.py b/PyMca5/__init__.py index 55fe55837..34c1c9f74 100644 --- a/PyMca5/__init__.py +++ b/PyMca5/__init__.py @@ -27,7 +27,7 @@ __contact__ = "sole@esrf.fr" __license__ = "MIT" __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France" -__version__ = "5.8.8" +__version__ = "5.8.9" import os import sys diff --git a/PyMca5/tests/FastXRFLinearFitTest.py b/PyMca5/tests/FastXRFLinearFitTest.py new file mode 100644 index 000000000..6b807634b --- /dev/null +++ b/PyMca5/tests/FastXRFLinearFitTest.py @@ -0,0 +1,174 @@ +# /*########################################################################## +# +# The PyMca X-Ray Fluorescence Toolkit +# +# Copyright (c) 2023 European Synchrotron Radiation Facility +# +# This file is part of the PyMca X-ray Fluorescence Toolkit developed at +# the ESRF. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +#############################################################################*/ +__author__ = "V.A. Sole" +__contact__ = "sole@esrf.eu" +__license__ = "MIT" +__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France" +import unittest +import sys +import os +import numpy +import tempfile +import shutil +from PyMca5.tests import XrfData +from PyMca5.PyMcaPhysics.xrf import FastXRFLinearFit +from PyMca5.PyMcaPhysics.xrf.XRFBatchFitOutput import OutputBuffer + +try: + import h5py + + HAS_H5PY = True +except ImportError: + HAS_H5PY = False + +class testFastXRFLinearFit(unittest.TestCase): + _rtolLegacy = 1e-5 + + def setUp(self): + self.path = tempfile.mkdtemp(prefix="pymca") + super(testFastXRFLinearFit, self).setUp() + + def tearDown(self): + shutil.rmtree(self.path) + + @unittest.skipUnless(HAS_H5PY, "h5py not installed") + def testCommand(self): + from PyMca5.PyMcaIO import HDF5Stack1D + + # generate the data + data, livetime = XrfData.generateXRFData() + configuration = XrfData.generateXRFConfig() + configuration["fit"]["stripalgorithm"] = 1 + + # create HDF5 file + fname = os.path.join(self.path, "FastXRF.h5") + h5 = h5py.File(fname, "w") + h5["/data"] = data + h5["/data_int32"] = (data * 1000).astype(numpy.int32) + h5.flush() + h5.close() + + fastFit = FastXRFLinearFit.FastXRFLinearFit() + fastFit.setFitConfiguration(configuration) + + outputDir = None + outputRoot = "" + fileEntry = "" + fileProcess = "" + refit = None + filepattern = None + begin = None + end = None + increment = None + backend = None + weight = 0 + tif = 0 + edf = 0 + csv = 0 + h5 = 1 + dat = 0 + concentrations = 0 + diagnostics = 0 + debug = 0 + overwrite = 1 + multipage = 0 + + outbuffer = OutputBuffer( + outputDir=outputDir, + outputRoot=outputRoot, + fileEntry=fileEntry, + fileProcess=fileProcess, + diagnostics=diagnostics, + tif=tif, + edf=edf, + csv=csv, + h5=h5, + dat=dat, + multipage=multipage, + overwrite=overwrite, + ) + + # test standard reading + scanlist = None + selection = {"y": "/data"} + dataStack = HDF5Stack1D.HDF5Stack1D([fname], selection, scanlist=scanlist) + with outbuffer.saveContext(): + fastFit.fitMultipleSpectra( + y=dataStack, + weight=weight, + refit=refit, + concentrations=concentrations, + outbuffer=outbuffer, + ) + # test dynamic reading + h5 = h5py.File(fname, "r") + with outbuffer.saveContext(): + fastFit.fitMultipleSpectra( + y=h5["/data"], + weight=weight, + refit=refit, + concentrations=concentrations, + outbuffer=outbuffer, + ) + # test dynamic reading of integer data + with outbuffer.saveContext(): + fastFit.fitMultipleSpectra( + y=h5["/data_int32"], + weight=weight, + refit=refit, + concentrations=concentrations, + outbuffer=outbuffer, + ) + + h5.close() + h5 = None + +def getSuite(auto=True): + testSuite = unittest.TestSuite() + if auto: + testSuite.addTest( + unittest.TestLoader().loadTestsFromTestCase(testFastXRFLinearFit) + ) + else: + # use a predefined order + testSuite.addTest(testPyMcaBatch("testCommand")) + return testSuite + + +def test(auto=False): + return unittest.TextTestRunner(verbosity=2).run(getSuite(auto=auto)) + + +if __name__ == "__main__": + if len(sys.argv) > 1: + auto = False + else: + auto = True + result = test(auto) + sys.exit(not result.wasSuccessful()) diff --git a/PyMca5/tests/__init__.py b/PyMca5/tests/__init__.py index e352fe6a8..d9af85781 100644 --- a/PyMca5/tests/__init__.py +++ b/PyMca5/tests/__init__.py @@ -2,10 +2,10 @@ # # The PyMca X-Ray Fluorescence Toolkit # -# Copyright (c) 2004-2019 European Synchrotron Radiation Facility +# Copyright (c) 2004-2023 European Synchrotron Radiation Facility # # This file is part of the PyMca X-ray Fluorescence Toolkit developed at -# the ESRF by the Software group. +# the ESRF. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -26,13 +26,14 @@ # THE SOFTWARE. # #############################################################################*/ -__author__ = "V. Armando Sole - ESRF Data Analysis" +__author__ = "V. Armando Sole" __contact__ = "sole@esrf.fr" __license__ = "MIT" __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France" import os from PyMca5.tests.ConfigDictTest import test as testConfigDict +from PyMca5.tests.DataTest import test as testDataTest from PyMca5.tests.EdfFileTest import test as testEdfFile from PyMca5.tests.ROIBatchTest import test as testROIBatch from PyMca5.tests.ElementsTest import test as testElements @@ -44,6 +45,7 @@ from PyMca5.tests.McaStackViewTest import test as testMcaStackView from PyMca5.tests.NexusUtilsTest import test as testNexusUtils from PyMca5.tests.StackInfoTest import test as testStackInfo +from PyMca5.tests.FastXRFLinearFitTest import test as testFastXRFLinearFit def testAll(): from PyMca5.tests.TestAll import main as testAll diff --git a/changelog.txt b/changelog.txt index fbafaaf45..c5ed3b4ab 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,8 @@ +VERSION 5.8.9 +------------- + +- FastXRF. Restore compatibility with dynamically loaded HDF5 data + VERSION 5.8.8 -------------