NMF analysis example

Import the spectrochempy API package

import spectrochempy as scp

Prepare the dataset to NMF factorize

Here we use a FTIR dataset corresponding the dehydration of a NH4Y zeolite and recorded in the OMNIC format.

dataset = scp.read_omnic("irdata/nh4y-activation.spg")

Mask some columns (features) which correspond to saturated parts of the spectra. Note that we use float number for defining the limits for masking as coordinates (integer numbers would mean point index and s would lead t incorrect results)

dataset[:, 882.0:1280.0] = scp.MASKED

Make sure all data are positive. For this we use the math functionalities of NDDataset objects (min function to find the minimum value of the dataset and the - operator for subtrating this value to all spectra of the dataset.

dataset -= dataset.min()

Plot it for a visual check

_ = dataset.plot()

Create a NMF object

As argument of the object constructor we define log_level to "INFO" to obtain verbose output during fit, and we set the number of component to use at 4.

model = scp.NMF(n_components=4, log_level="INFO")

Fit the model

_ = model.fit(dataset)

Get the results

The concentration \(C\) and the transposed matrix of spectra \(S^T\) can be obtained as follow

C = model.transform()
St = model.components

Plot results

_ = C.T.plot(title="Concentration", colormap=None, legend=C.k.labels)
m = St.ptp()
for i in range(St.shape[0]):
    St.data[i] -= i * m / 2
ax = St.plot(title="Components", colormap=None, legend=St.k.labels)
_ = ax.set_yticks([])

Uncomment the following line to display all figures when running the script directly with Python.

# scp.show()

Total running time of the script: ( 0 minutes 0.000 seconds)