Skip to content

Commit

Permalink
Merge pull request #1033 from vasole/nnma
Browse files Browse the repository at this point in the history
[ROI Imaging] Support NNMA on stacks of images
  • Loading branch information
vasole authored Sep 26, 2023
2 parents 03f559a + 95b9b04 commit cb6775d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
39 changes: 7 additions & 32 deletions PyMca5/PyMcaMath/mva/NNMAModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -384,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:
Expand All @@ -407,17 +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
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
new_images.shape = ncomponents, r, c
return new_images, values, new_vectors

if __name__ == "__main__":
Expand Down
24 changes: 19 additions & 5 deletions PyMca5/PyMcaPlugins/NNMAStackPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit cb6775d

Please sign in to comment.