<jupyter_start><jupyter_text>Demo of NMF-SO and NMF-ARD-SO  [1] <NAME>, <NAME>, <NAME>, <NAME>, <NAME>, <NAME>, <NAME>, "Sparse Modeling of EELS and EDX Spectral Imaging Data by Nonnegative Matrix Factorization", Ultramicroscopy, Vol.170, p.43-59, 2016.<jupyter_code>%matplotlib inline
import numpy as np
import scipy.io as sio
from libnmf import NMF, NMF_SO, NMF_ARD_SO<jupyter_output><empty_output><jupyter_text>Generate a synthetic dataset with noise<jupyter_code>#load theoretical data of Mn3O4 without noise
mat_dict = sio.loadmat('mn3o4_f2.mat')
ximage = mat_dict['datar']
del(mat_dict)

# focusing channel
n_ch = np.arange(37-1,116);
ximage = ximage[:,:,n_ch];

# # of pixels along x and y axis, # of EELS channels
xdim, ydim, Nch = ximage.shape

#  generating pahtom data by adding gaussian noise
X = np.reshape(ximage, (xdim*ydim, Nch))
scale_spect = np.max(X)
s2_noise = 0.1  #noise variance
X = X + np.random.randn(xdim*ydim, Nch) * s2_noise * scale_spect;
X = (X + np.abs(X))/2;
scale_X = np.mean(X)
X = X / scale_X<jupyter_output><empty_output><jupyter_text>Basic NMF<jupyter_code># define and training model
nmf = NMF(n_components=2, reps=3, max_itr=100)
nmf.fit(X, num_xy=(xdim,ydim), channel_vals=n_ch, unit_name='Channel')
nmf.imshow_component(figsize=(6, 3))  # for 2D spactrum (Spectrum Imaging) dataset
nmf.plot_spectra(figsize=(6,3)) # plot decomposed spectra
nmf.plot_object_fun(figsize=(6,3)) # plot learnig curve (object function)<jupyter_output><empty_output><jupyter_text>NMF-SO<jupyter_code># define and training model
nmf_so = NMF_SO(n_components=2, wo=0.05, reps=3, max_itr=100)
nmf_so.fit(X, num_xy=(xdim,ydim), channel_vals=n_ch, unit_name='Channel')
nmf_so.imshow_component(figsize=(6, 3))  # for 2D spactrum (Spectrum Imaging) dataset
nmf_so.plot_spectra(figsize=(6,3)) # plot decomposed spectra
nmf_so.plot_object_fun(figsize=(6,3)) # plot learnig curve (object function)<jupyter_output><empty_output><jupyter_text>NMF-ARD-SO<jupyter_code># define and training model
nmf_ard = NMF_ARD_SO(n_components=9, wo=0.05, reps=3, max_itr=100)
nmf_ard.fit(X, num_xy=(xdim,ydim), channel_vals=n_ch)
nmf_ard.plot_ard()  # plot learning curve with component intensities
nmf_ard.imshow_component(figsize=(6, 3))  # for 2D spactrum (Spectrum Imaging) dataset
nmf_ard.plot_spectra(figsize=(6,3)) # plot decomposed spectra
nmf_ard.plot_object_fun(figsize=(6,3)) # #plot learnig curve (object function)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13357843137254902, 'fraction_numerical': 0.028594771241830064, 'mean_word_length': 7.273648648648648, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '265522', 'license': 'MIT', 'n_tokens_mistral': 1026, 'n_tokens_neox': 980, 'n_words': 211, 'path': 'python/demo_libnmf.ipynb', 'repo_name': 'MotokiShiga/stem-nmf', 'size': '2500'}