Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in signal decomposition concerning navigation_mask #2677

Closed
w-markus opened this issue Mar 16, 2021 · 2 comments
Closed

Bug in signal decomposition concerning navigation_mask #2677

w-markus opened this issue Mar 16, 2021 · 2 comments

Comments

@w-markus
Copy link

Please run:

%matplotlib qt
import hyperspy.api as hs
import hyperspy
import numpy as np
import matplotlib
from matplotlib import pyplot as plt

# some fake data
e = np.arange(0,2048)
spectra = [np.exp(-((e-p)/100) ** 2) for p in [200,600,1400]]

data = np.zeros((192,256,2048))
comp = [(0,90,4,1,1), (90,100,1,4,0.8), (100,110,1,4,1.0),(110,120,1,4,1.2),(120,192,1,1,4)]
for b,t,c0,c1,c2 in comp:
    data[b:t] = spectra[0]*c0+spectra[1]*c1+spectra[2]*c2
data = np.random.poisson(lam=100*data)*1.0
signal = hs.signals.Signal1D(data, signal_type='EDS_TEM')
signal.axes_manager[-1].name = 'E'
signal.axes_manager['E'].units = 'eV'
signal.axes_manager['E'].scale = 1.0e1
signal.axes_manager['E'].offset = 0

signal.plot()

#--------------------------------------------------
# do NMF decomposition
# define a mask
mask = np.ones((192,256)).astype(bool)
mask[90:120] = False

# one would expect to use navigation_mask=mask
# but this leads to "The shape of `mask` must match the shape of the `navigation_shape`."
# using navigation_mask=mask.T does not give that error, but the result makes no sense
# see the resulting loadings plot

signal.decomposition(normalize_poissonian_noise=True, 
                     navigation_mask=mask.T, 
                     algorithm='NMF', 
                     output_dimension=3, 
                     centre=None, 
                     max_iter=20,
                     return_info=False)    

loadings = signal.get_decomposition_loadings()
loadings.plot()
@ericpre
Copy link
Member

ericpre commented Mar 16, 2021

Thanks for the example, I can reproduce.

Until this is fixed, you can use a signal as a mask instead of numpy array:

s2 = signal.sum(-1)
nav_mask = (s2 < 1.08E5)

signal.decomposition(normalize_poissonian_noise=True,
                     navigation_mask=nav_mask,
                     algorithm='NMF',
                     output_dimension=3,
                     centre=None,
                     max_iter=20,
                     return_info=False)

@ericpre
Copy link
Member

ericpre commented Mar 16, 2021

Should be fixed in #2679.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants