From 5e27186ffe9c78dac74e2b2fbea72d44ce255189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=2E=20Armando=20Sol=C3=A9?= Date: Tue, 26 Sep 2023 14:55:37 +0200 Subject: [PATCH 1/3] Remove unused code. --- PyMca5/PyMcaMath/mva/NNMAModule.py | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/PyMca5/PyMcaMath/mva/NNMAModule.py b/PyMca5/PyMcaMath/mva/NNMAModule.py index d8c0eb101..e0736d30b 100644 --- a/PyMca5/PyMcaMath/mva/NNMAModule.py +++ b/PyMca5/PyMcaMath/mva/NNMAModule.py @@ -63,7 +63,7 @@ from numpy or sparse from scipy.sparse package - k -- number of componnets to estimate + k -- number of components to estimate Astart Xstart -- matrices to start iterations. Maybe None @@ -185,17 +185,6 @@ """ import numpy import logging -try: - import os - os.environ["MDP_DISABLE_SKLEARN"] = "yes" - import mdp - if mdp.__version__ >= '2.6': - MDP = True - else: - MDP = False -except Exception: - MDP = False - from . import py_nnma @@ -223,8 +212,8 @@ def nnma(stack, ncomponents, binning=None, mask=None, spectral_mask=None, function=None, eps=5e-5, verbose=VERBOSE, maxcount=1000, kmeans=False): - if kmeans and (not MDP): - raise ValueError("K Means not supported") + if kmeans: + raise ValueError("K Means not supported by this module") #I take the defaults for the other parameters param = dict(alpha=.1, tau=2, regul=1e-2, sparse_par=1e-1, psi=1e-3) if function is None: @@ -408,16 +397,6 @@ def nnma(stack, ncomponents, binning=None, new_vectors[i,:] = X[idx,:] values[i] = 100.*total_nnma_intensity[idx][0]/original_intensity new_images.shape = ncomponents + n_more, r, c - if kmeans: - classifier = mdp.nodes.KMeansClassifier(ncomponents) - for i in range(ncomponents): - classifier.train(new_vectors[i:i+1]) - k = 0 - for i in range(r): - for j in range(c): - spectrum = data[k:k+1,:] - new_images[-1, i,j] = classifier.label(spectrum)[0] - k += 1 return new_images, values, new_vectors if __name__ == "__main__": From 44c91f8602c98a78de027301d5a164c199aa8dde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=2E=20Armando=20Sol=C3=A9?= Date: Tue, 26 Sep 2023 15:52:10 +0200 Subject: [PATCH 2/3] Remove unused code. --- PyMca5/PyMcaMath/mva/NNMAModule.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/PyMca5/PyMcaMath/mva/NNMAModule.py b/PyMca5/PyMcaMath/mva/NNMAModule.py index e0736d30b..6c7a267c6 100644 --- a/PyMca5/PyMcaMath/mva/NNMAModule.py +++ b/PyMca5/PyMcaMath/mva/NNMAModule.py @@ -373,13 +373,9 @@ def nnma(stack, ncomponents, binning=None, original_intensity = numpy.sum(data) #final values - if kmeans: - n_more = 1 - else: - n_more = 0 - new_images = numpy.zeros((ncomponents + n_more, r*c), numpy.float32) - new_vectors = numpy.zeros((X.shape[0]+n_more, X.shape[1]), numpy.float32) - values = numpy.zeros((ncomponents+n_more,), numpy.float32) + new_images = numpy.zeros((ncomponents, r*c), numpy.float32) + new_vectors = numpy.zeros((X.shape[0], X.shape[1]), numpy.float32) + values = numpy.zeros((ncomponents,), numpy.float32) for i in range(ncomponents): idx = sorted_idx[i] if 1: @@ -396,7 +392,7 @@ def nnma(stack, ncomponents, binning=None, new_images[i, maskview] = numpy.sum(numpy.dot(Atmp, Xtmp), axis=1) new_vectors[i,:] = X[idx,:] values[i] = 100.*total_nnma_intensity[idx][0]/original_intensity - new_images.shape = ncomponents + n_more, r, c + new_images.shape = ncomponents, r, c return new_images, values, new_vectors if __name__ == "__main__": From 95b9b048e01e2ea795086bd94de118714003c736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=2E=20Armando=20Sol=C3=A9?= Date: Tue, 26 Sep 2023 15:53:48 +0200 Subject: [PATCH 3/3] Support stacks of images --- PyMca5/PyMcaPlugins/NNMAStackPlugin.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/PyMca5/PyMcaPlugins/NNMAStackPlugin.py b/PyMca5/PyMcaPlugins/NNMAStackPlugin.py index 76b5dfe2f..df920d2ed 100644 --- a/PyMca5/PyMcaPlugins/NNMAStackPlugin.py +++ b/PyMca5/PyMcaPlugins/NNMAStackPlugin.py @@ -138,8 +138,8 @@ def calculate(self): mcaIndex = stack.info.get('McaIndex') shape = stack.data.shape stack = None - if mcaIndex not in [-1, len(shape) - 1]: - raise IndexError("NNMA does not support stacks of images yet") + if mcaIndex not in [0, -1, len(shape) - 1]: + raise IndexError("NNMA only support stacks of images or spectra") return if self.configurationWidget is None: self.configurationWidget = NNMAParametersDialog(None, regions=True) @@ -225,9 +225,23 @@ def actualCalculation(self): self._status.setText(text) oldShape = stack.data.shape - result = function(stack, **ddict) - if stack.data.shape != oldShape: - stack.data.shape = oldShape + mcaIndex = stack.info.get('McaIndex') + if mcaIndex == 0: + # image stack. We need a copy + _logger.info("NNMAStackPlugin converting to stack of spectra") + data = numpy.zeros(oldShape[1:] + oldShape[0:1], dtype=numpy.float32) + data.shape = -1, oldShape[0] + for i in range(oldShape[0]): + tmpData = stack.data[i] + tmpData.shape = -1 + data[:, i] = tmpData + data.shape = oldShape[1:] + oldShape[0:1] + result = function(data, **ddict) + data = None + else: + result = function(stack, **ddict) + if stack.data.shape != oldShape: + stack.data.shape = oldShape return result def threadFinished(self):