def test_NNMF(): w1 = scp.NDDataset([[1, 2, 3], [4, 5, 6]]) h1 = scp.NDDataset([[1, 2], [3, 4], [5, 6]]) w2 = scp.NDDataset([[1, 1, 1], [4, 4, 4]]) h2 = scp.NDDataset([[1, 1], [3, 3], [5, 5]]) v = scp.dot(w1, h1) nnmf = scp.NNMF(v, w2, h2, tol=0.0001, maxtime=60, maxiter=100, verbose=True) scp.info_("------") scp.info_(nnmf.C) scp.info_("------") scp.info_(nnmf.St) assert_array_almost_equal(nnmf.C.data, np.array([[1.4, 2.1, 2.9], [4.4, 5.2, 6.0]]), decimal=1) assert_array_almost_equal(nnmf.St.data, [[0.8, 1.9], [2.9, 3.9], [5.1, 5.9]], decimal=1)
def test_bugs_units_change(): # check for bug on transmittance conversion X = scp.NDDataset([0.0, 0.3, 1.3, 5.0], units="absorbance") # A to T X1 = X.to("transmittance") assert_array_equal(X1.data, 10 ** -np.array([0.0, 0.3, 1.3, 5.0]) * 100) assert X1.title == "transmittance" # T to abs T X2 = X1.to("absolute_transmittance") assert_array_equal(X2.data, 10 ** -np.array([0.0, 0.3, 1.3, 5.0])) assert X2.title == "transmittance" # A to abs T X2b = X.to("absolute_transmittance") assert_array_equal(X2b, X2) assert X2b.title == "transmittance" # abs T to T X3 = X2.to("transmittance") assert_array_equal(X3, X1) assert X3.title == "transmittance" # T to A X4 = X3.to("absorbance") assert_array_almost_equal(X4, X) assert X4.title == "absorbance" # abs T to A X5 = X2.to("absorbance") assert_array_almost_equal(X5, X) assert X5.title == "absorbance"
def test_nmr_1D_apodization(NMR_dataset_1D): dataset = NMR_dataset_1D.copy() dataset /= dataset.real.data.max() # normalize lb = 0 arr, apod = dataset.em(lb=lb, retapod=True) # arr and dataset should be equal as no em was applied assert_equal(dataset.data, arr.data) lb = 0.0 gb = 0.0 arr, apod = dataset.gm(lb=lb, gb=gb, retapod=True) # arr and dataset should be equal as no em was applied assert_equal(dataset.data, arr.data) lb = 100 arr, apod = dataset.em(lb=lb, retapod=True) # arr and dataset should not be equal as inplace=False assert not np.array_equal(dataset.data, arr.data) assert_array_almost_equal(apod[1], 0.9987, decimal=4) # inplace=True dataset.plot(xlim=(0.0, 6000.0)) dataset.em(lb=100.0 * ur.Hz, inplace=True) dataset.plot(c="r", data_only=True, clear=False) # successive call dataset.em(lb=200.0 * ur.Hz, inplace=True) dataset.plot(c="g", data_only=True, clear=False) dataset = NMR_dataset_1D.copy() dataset.plot() dataset.em(100.0 * ur.Hz, inplace=True) dataset.plot(c="r", data_only=True, clear=False) # first test gm dataset = NMR_dataset_1D.copy() dataset /= dataset.real.data.max() # normalize dataset.plot(xlim=(0.0, 6000.0)) dataset, apod = dataset.gm(lb=-100.0 * ur.Hz, gb=100.0 * ur.Hz, retapod=True) dataset.plot(c="b", data_only=True, clear=False) apod.plot(c="r", data_only=True, clear=False) show()
def test_compare_scikit_learn(): X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]) pcas = sklPCA(n_components=2) pcas.fit(X) pca = PCA(NDDataset(X)) pca.printev(n_pc=2) assert_array_almost_equal(pca.sv.data, pcas.singular_values_) assert_array_almost_equal(pca.ev_ratio.data, pcas.explained_variance_ratio_ * 100.) dataset = NDDataset.read('irdata/nh4y-activation.spg') X = dataset.data pcas = sklPCA(n_components=5) pcas.fit(X) dataset = X.copy() pca = PCA(NDDataset(dataset)) pca.printev(n_pc=5) assert_array_almost_equal(pca.sv.data[:5], pcas.singular_values_[:5], 4) assert_array_almost_equal(pca.ev_ratio.data[:5], pcas.explained_variance_ratio_[:5] * 100., 4)
def test_nddataset_coords_indexer(): dx = np.random.random((10, 100, 10)) coord0 = np.linspace(4000, 1000, 10) coord1 = np.linspace(0, 60, 10) # wrong length coord2 = np.linspace(20, 30, 10) with pytest.raises(ValueError): # wrong length da = scp.NDDataset( dx, coordset=[coord0, coord1, coord2], title="absorbance", coordtitles=["wavelength", "time-on-stream", "temperature"], coordunits=["cm^-1", "s", "K"], ) coord1 = np.linspace(0, 60, 100) da = scp.NDDataset( dx, coordset=[coord0, coord1, coord2], title="absorbance", coordtitles=["wavelength", "time-on-stream", "temperature"], coordunits=["cm^-1", "s", "K"], ) assert da.shape == (10, 100, 10) coords = da.coordset assert len(coords) == 3 assert_array_almost_equal( da.coordset[2].data, coord0, decimal=2, err_msg="get axis by index " "failed" ) # we use almost as SpectroChemPy round the coordinate numbers assert_array_almost_equal( da.coordset["wavelength"].data, coord0, decimal=2, err_msg="get axis by title failed", ) assert_array_almost_equal( da.coordset["time-on-stream"].data, coord1, decimal=4, err_msg="get axis by title failed", ) assert_array_almost_equal(da.coordset["temperature"].data, coord2, decimal=3) da.coordset["temperature"] += 273.15 * ur.K assert_array_almost_equal( da.coordset["temperature"].data, coord2 + 273.15, decimal=3 )
def test_compare_scikit_learn(): try: import_optional_dependency("scikit-learn") except ImportError: return from sklearn.decomposition import PCA as sklPCA X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]) pcas = sklPCA(n_components=2) pcas.fit(X) pca = PCA(NDDataset(X)) pca.printev(n_pc=2) assert_array_almost_equal(pca._sv.data, pcas.singular_values_) assert_array_almost_equal(pca.ev_ratio.data, pcas.explained_variance_ratio_ * 100.0) dataset = NDDataset.read("irdata/nh4y-activation.spg") X1 = dataset.copy().data pcas = sklPCA(n_components=5, svd_solver="full") pcas.fit(X1) X2 = NDDataset(dataset.copy()) pca = PCA(X2) pca.printev(n_pc=5) assert_array_almost_equal(pca._sv.data[:5], pcas.singular_values_[:5], 4) assert_array_almost_equal(pca.ev_ratio.data[:5], pcas.explained_variance_ratio_[:5] * 100.0, 4) show()