def test_basecor_multivariate(IR_dataset_2D): dataset = IR_dataset_2D[5] basc = BaselineCorrection(dataset) s = basc([6000.0, 3500.0], [1800.0, 1500.0], method="multivariate", interpolation="pchip") s.plot() s = basc( [6000.0, 3500.0], [1800.0, 1500.0], method="multivariate", interpolation="polynomial", ) s.plot(clear=False, color="red") dataset = IR_dataset_2D[:15] basc = BaselineCorrection(dataset) s = basc([6000.0, 3500.0], [1800.0, 1500.0], method="multivariate", interpolation="pchip") s.plot() s = basc( [6000.0, 3500.0], [1800.0, 1500.0], method="multivariate", interpolation="polynomial", ) s.plot(cmap="copper") show()
def test_MCRALS(): data = NDDataset.read_matlab(os.path.join('matlabdata', 'als2004dataset.MAT'), transposed=True) X = data[-1] assert X.name == 'm1' X.set_coordset(y=np.arange(51), x=np.arange(96)) X.title = 'concentration' X.coordset.set_titles(y='spec coord.', x='elution time') X.plot(title='M1') guess = data[3] assert guess.name == 'spure' # spure guess.set_coordset(y=np.arange(4), x=np.arange(96)) guess.title = 'concentration' guess.coordset.set_titles(y='#components', x='elution time') guess.plot(title='spure') mcr = MCRALS(X, guess, verbose=True) mcr.C.T.plot(title='Concentration') mcr.St.plot(title='spectra') mcr.plotmerit() guess = EFA(X).get_conc(4) guess.plot(title='EFA guess') param = {'normSpec': 'euclid', 'maxit': 100} mcr2 = MCRALS(X, guess, param=param, verbose=False) mcr.plotmerit() assert 'converged !' in mcr2.logs[-15:] show()
def test_nmr_manual_1D_phasing(NMR_dataset_1D): dataset1D = NMR_dataset_1D.copy() dataset1D /= dataset1D.real.data.max() # normalize dataset1D.em(10. * ur.Hz) # inplace broadening transf = dataset1D.fft(tdeff=8192, size=2**15) # fft transf.plot() # plot) # manual phasing transfph = transf.pk(verbose=True) # by default pivot = 'auto' transfph.plot(xlim=(20, -20), clear=False, color='r') assert_array_equal(transfph.data, transf.data) # because phc0 already applied transfph3 = transf.pk(pivot=50, verbose=True) transfph3.plot(clear=False, color='r') not assert_array_equal(transfph3.data, transfph.data) # because phc0 already applied # transfph4 = transf.pk(pivot=100, phc0=40., verbose=True) transfph4.plot(xlim=(20, -20), clear=False, color='g') assert transfph4 != transfph transfph4 = transf.pk(pivot=100, verbose=True, inplace=True) (transfph4 - 10).plot(xlim=(20, -20), clear=False, color='r') show()
def test_nmr_auto_1D_phasing(): path = os.path.join(prefs.datadir, "nmrdata", "bruker", "tests", "nmr", "topspin_1d") ndd = NDDataset.read_topspin(path, expno=1, remove_digital_filter=True) ndd /= ndd.real.data.max() # normalize ndd.em(10.0 * ur.Hz, inplace=True) transf = ndd.fft(tdeff=8192, size=2**15) transf.plot(xlim=(20, -20), ls=":", color="k") transfph2 = transf.pk(verbose=True) transfph2.plot(xlim=(20, -20), clear=False, color="r") # automatic phasing transfph3 = transf.apk(verbose=True) (transfph3 - 1).plot(xlim=(20, -20), clear=False, color="b") transfph4 = transf.apk(algorithm="acme", verbose=True) (transfph4 - 2).plot(xlim=(20, -20), clear=False, color="g") transfph5 = transf.apk(algorithm="neg_peak", verbose=True) (transfph5 - 3).plot(xlim=(20, -20), clear=False, ls="-", color="r") transfph6 = transf.apk(algorithm="neg_area", verbose=True) (transfph6 - 4).plot(xlim=(20, -20), clear=False, ls="-.", color="m") transfph4 = transfph6.apk(algorithm="acme", verbose=True) (transfph4 - 6).plot(xlim=(20, -20), clear=False, color="b") show()
def test_nmr_auto_1D_phasing(): path = os.path.join(prefs.datadir, 'nmrdata', 'bruker', 'tests', 'nmr', 'topspin_1d') ndd = NDDataset.read_topspin(path, expno=1, remove_digital_filter=True) ndd /= ndd.real.data.max() # normalize ndd.em(10. * ur.Hz, inplace=True) transf = ndd.fft(tdeff=8192, size=2**15) transf.plot(xlim=(20, -20), ls=':', color='k') transfph2 = transf.pk(verbose=True) transfph2.plot(xlim=(20, -20), clear=False, color='r') # automatic phasing transfph3 = transf.apk(verbose=True) (transfph3 - 1).plot(xlim=(20, -20), clear=False, color='b') transfph4 = transf.apk(algorithm='acme', verbose=True) (transfph4 - 2).plot(xlim=(20, -20), clear=False, color='g') transfph5 = transf.apk(algorithm='neg_peak', verbose=True) (transfph5 - 3).plot(xlim=(20, -20), clear=False, ls='-', color='r') transfph6 = transf.apk(algorithm='neg_area', verbose=True) (transfph6 - 4).plot(xlim=(20, -20), clear=False, ls='-.', color='m') transfph4 = transfph6.apk(algorithm='acme', verbose=True) (transfph4 - 6).plot(xlim=(20, -20), clear=False, color='b') show()
def test_nmr_1D_show(NMR_dataset_1D): # test simple plot dataset = NMR_dataset_1D.copy() dataset.plot() show()
def test_IRIS(): X = NDDataset.read_omnic(os.path.join('irdata', 'CO@Mo_Al2O3.SPG')) p = [ 0.00300, 0.00400, 0.00900, 0.01400, 0.02100, 0.02600, 0.03600, 0.05100, 0.09300, 0.15000, 0.20300, 0.30000, 0.40400, 0.50300, 0.60200, 0.70200, 0.80100, 0.90500, 1.00400 ] X.coordset.update(y=Coord(p, title='pressure', units='torr')) # Using the `update` method is mandatory because it will preserve the name. # Indeed, setting using X.coordset[0] = Coord(...) fails unless name is specified: Coord(..., name='y') # set the optimization parameters, perform the analysis # and plot the results param = { 'epsRange': [-8, -1, 20], 'lambdaRange': [-7, -5, 3], 'kernel': 'langmuir' } X_ = X[:, 2250.:1950.] X_.plot() iris = IRIS(X_, param, verbose=True) f = iris.f X_hat = iris.reconstruct() iris.plotlcurve(scale='ln') f[0].plot(method='map', plottitle=True) X_hat[0].plot(plottitle=True) show()
def test_nmr_2D_imag_compare(NMR_dataset_2D): # plt.ion() dataset = NMR_dataset_2D.copy() dataset.plot() dataset.plot(imag=True, cmap='jet', data_only=True, alpha=.3, clear=False) # better not to replot a second colorbar show() pass
def test_nmr_fft_1D_our_Hz(NMR_dataset_1D): dataset1D = NMR_dataset_1D.copy() dataset1D /= dataset1D.real.data.max() # normalize LB = 10. * ur.Hz GB = 50. * ur.Hz dataset1D.gm(gb=GB, lb=LB) new = dataset1D.fft(size=32000, ppm=False) new.plot(xlim=[5000, -5000]) show()
def test_smooth(NMR_dataset_1D): dataset = NMR_dataset_1D.copy() dataset /= dataset.real.data.max() # normalize dataset = dataset.fft(tdeff=8192, size=2**15) + np.random.random(2**15) * 5.0 dataset.plot() s = dataset.smooth() s.plot(clear=False, color="r", xlim=[20, -20]) show()
def test_nmr_fft_1D(NMR_dataset_1D): dataset1D = NMR_dataset_1D.copy() dataset1D /= dataset1D.real.data.max() # normalize dataset1D.x.ito('s') new = dataset1D.fft(tdeff=8192, size=2**15) new.plot() new2 = new.ifft() dataset1D.plot() (new2 - 1.).plot(color='r', clear=False) show()
def test_nmr_1D_show_complex(NMR_dataset_1D): dataset = NMR_dataset_1D.copy() dataset.plot(xlim=(0., 25000.)) dataset.plot(imag=True, color='r', data_only=True, clear=False) # display the real and complex at the same time dataset.plot(show_complex=True, color='green', xlim=(0., 30000.), zlim=(-200., 200.)) show()
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_smooth_2D(IR_dataset_2D): dataset = IR_dataset_2D.copy() dataset /= dataset.real.data.max() # nromalize dataset += np.random.random(dataset.shape[-1]) * 0.02 s = dataset.smooth(length=21) (dataset + 0.25).plot(xlim=[4000, 3000]) s.plot(cmap="copper", clear=False, xlim=[4000, 3000]) s2 = s.smooth(length=21, dim="y") (s2 - 0.25).plot(cmap="jet", clear=False, xlim=[4000, 3000]) show()
def test_basecor_sequential(IR_dataset_2D): dataset = IR_dataset_2D[5] basc = BaselineCorrection(dataset) s = basc([6000.0, 3500.0], [2200.0, 1500.0], method="sequential", interpolation="pchip") s.plot() s1 = basc( [6000.0, 3500.0], [2200.0, 1500.0], method="sequential", interpolation="polynomial", ) s1.plot(clear=False, color="red") dataset = IR_dataset_2D[5] # with LinearCoord basc = BaselineCorrection(dataset) s2 = basc([6000.0, 3500.0], [2200.0, 1500.0], method="sequential", interpolation="pchip") assert_dataset_almost_equal(s, s2, decimal=5) s2.plot(clear=False, color="green") s3 = basc( [6000.0, 3500.0], [2200.0, 1500.0], method="sequential", interpolation="polynomial", ) assert_dataset_almost_equal(s1, s3, decimal=5) s3.plot(clear=False, color="cyan") dataset = IR_dataset_2D[:15] basc = BaselineCorrection(dataset) s = basc([6000.0, 3500.0], [2200.0, 1500.0], method="sequential", interpolation="pchip") s.plot() s = basc( [6000.0, 3500.0], [2200.0, 1500.0], method="sequential", interpolation="polynomial", ) s.plot(cmap="copper") show()
def _blcorrect_and_plot(self): slice_x = _str_to_slice(self._x_limits_control.value, self._X, "x") slice_y = _str_to_slice(self._y_limits_control.value, self._X, "y") self.original = self._X[slice_y, slice_x] ranges = _str_to_ranges(self._ranges_control.value) if self.original is not None: # slicing was OK # check that no range is outside coordinates new_ranges, changed = _update_ranges( _str_to_ranges(self._ranges_control.value), self.original.x.data) if changed: ranges = _round_ranges(new_ranges) self._ranges_control.value = _ranges_to_str(ranges) blc = BaselineCorrection(self.original) self.corrected = blc.compute( *ranges, interpolation=self._interpolation_selector.value, order=self._order_slider.value, method=self._method_selector.value, npc=self._npc_slider.value, ) self.baseline = self.original - self.corrected self._output.clear_output(True) with self._output: axes = multiplot( [ concatenate(self.original, self.baseline, dims="y"), self.corrected, ], labels=["Original", "Corrected"], sharex=True, nrow=2, ncol=1, fig=self._fig, figsize=(8, 6), dpi=96, left=0.12, mpl_event=False, ) axes["axe11"].get_xaxis().set_visible(False) blc.show_regions(axes["axe21"]) self._fig = axes["axe21"].figure show() self._done = True
def test_autosub(IR_dataset_2D): dataset = IR_dataset_2D ranges = [5000.0, 5999.0], [1940.0, 1820.0] s1 = dataset.copy() ref = s1[-1].squeeze() dataset.plot_stack() ref.plot(clear=False, linewidth=2.0, color="r") s2 = dataset.copy() s3 = s2.autosub(ref, *ranges, dim=-1, method="vardiff", inplace=False) s3.plot() # inplace = False assert np.round(s2.data[-1, 0], 4) != 0.0000 assert np.round(s3.data[-1, 0], 4) == 0.0000 s3.name = "vardiff" s3.plot_stack() s4 = dataset.copy() s4.autosub(ref, *ranges, method="ssdiff", inplace=True) s4.name = "ssdiff, inplace" assert np.round(s4.data[-1, 0], 4) == 0.0000 s4.plot_stack() # true avoid blocking due to graphs s4 = dataset.copy() s = autosub(s4, ref, *ranges, method="ssdiff") assert np.round(s4.data[-1, 0], 4) != 0.0000 assert np.round(s.data[-1, 0], 4) == 0.0000 s.name = "ssdiff direct call" s.plot_stack() # s5 = dataset.copy() # ref2 = s5[:, 0].squeeze() # ranges2 = [0, 5], [45, 54] # TODO: not yet implemented # s6 = s5.autosub(ref2, *ranges2, dim='y', method='varfit', inplace=False) # s6.plot() show()
def test_issue_227(): # IR spectrum, we want to make a baseline correction on the absorbance vs. time axis: ir = scp.read("irdata/nh4y-activation.spg") # baseline correction along x blc = scp.BaselineCorrection(ir) s1 = blc([5999.0, 3500.0], [1800.0, 1500.0], method="multivariate", interpolation="pchip") # baseline correction the transposed data along x (now on axis 0) -> should produce the same results # baseline correction along axis -1 previuosly blc = scp.BaselineCorrection(ir.T) s2 = blc( [5999.0, 3500.0], [1800.0, 1500.0], dim="x", method="multivariate", interpolation="pchip", ) # compare assert_dataset_equal(s1, s2.T) ir.y = ir.y - ir[0].y irs = ir[:, 2000.0:2020.0] blc = scp.BaselineCorrection(irs) blc.compute(*[[0.0, 2.0e3], [3.0e4, 3.3e4]], dim="y", interpolation="polynomial", order=1, method="sequential") blc.corrected.plot() # MS profiles, we want to make a baseline correction on the ion current vs. time axis: ms = scp.read("msdata/ion_currents.asc", timestamp=False) blc = scp.BaselineCorrection(ms[10.0:20.0, :]) blc.compute(*[[10.0, 11.0], [19.0, 20.0]], dim="y", interpolation="polynomial", order=1, method="sequential") blc.corrected.T.plot() show()
def test_nmr_2D_em_y(NMR_dataset_2D): dataset = NMR_dataset_2D.copy() assert dataset.shape == (96, 948) dataset.plot_map() # plot original dataset = NMR_dataset_2D.copy() dataset.plot_map() dataset.em(lb=50. * ur.Hz, dim=0) assert dataset.shape == (96, 948) dataset.plot_map(cmap='copper', data_only=True, clear=False) # em on dim=x dataset = NMR_dataset_2D.copy() dataset.plot_map() dataset.em(lb=50. * ur.Hz, dim='y') assert dataset.shape == (96, 948) dataset.plot_map(cmap='copper', data_only=True, clear=False) # em on dim=x show()
def test_nmr_multiple_manual_1D_phasing(): path = os.path.join(prefs.datadir, 'nmrdata', 'bruker', 'tests', 'nmr', 'topspin_1d') ndd = NDDataset.read_topspin(path, expno=1, remove_digital_filter=True) ndd /= ndd.real.data.max() # normalize ndd.em(10. * ur.Hz) # inplace broadening transf = ndd.fft(tdeff=8192, size=2**15) transfph1 = transf.pk(verbose=True) transfph1.plot(xlim=(20, -20), color='k') transfph2 = transf.pk(verbose=True) transfph2.plot(xlim=(20, -20), clear=False, color='r') transfph3 = transf.pk(52.43836, -16.8366, verbose=True) transfph3.plot(xlim=(20, -20), clear=False, color='b') show()
def test_issue_375(): # minimal example n_pc = 3 color1, color2 = "b", "r" ratio = NDDataset([1, 2, 3]) cum = ratio.cumsum() ax1 = ratio.plot_bar(color=color1, title="Scree plot") assert len(ax1.lines) == 0, "no lines" assert len(ax1.patches) == 3, "bar present" ax2 = cum.plot_scatter(color=color2, pen=True, markersize=7.0, twinx=ax1) assert len(ax2.lines) == 1, "1 lines" assert len(ax2.patches) == 0, "no bar present on the second plot" # TODO: Don't know yet how to get the marker present. ax1.set_title("Scree plot") show()
def test_nmr_2D_em_x(NMR_dataset_2D): dataset = NMR_dataset_2D.copy() assert dataset.shape == (96, 948) dataset.plot_map() # plot original dataset = NMR_dataset_2D.copy() dataset.plot_map() dataset.em(lb=50.0 * ur.Hz, axis=-1) assert dataset.shape == (96, 948) dataset.plot_map(cmap="copper", data_only=True, clear=False) # em on dim=x dataset = NMR_dataset_2D.copy() dataset.plot_map() dataset.em(lb=50.0 * ur.Hz, dim="x") assert dataset.shape == (96, 948) dataset.plot_map(cmap="copper", data_only=True, clear=False) # em on dim=x show() pass
def test_nmr_multiple_auto_1D_phasing(): path = os.path.join(prefs.datadir, "nmrdata", "bruker", "tests", "nmr", "topspin_1d") ndd = NDDataset.read_topspin(path, expno=1, remove_digital_filter=True) ndd /= ndd.real.data.max() # normalize ndd.em(10.0 * ur.Hz) # inplace broadening transf = ndd.fft(tdeff=8192, size=2**15) transf.plot(xlim=(20, -20), ls=":", color="k") t1 = transf.apk(algorithm="neg_peak", verbose=True) (t1 - 5.0).plot(xlim=(20, -20), clear=False, color="b") t2 = t1.apk(algorithm="neg_area", verbose=True) (t2 - 10).plot(xlim=(20, -20), clear=False, ls="-.", color="m") t3 = t2.apk(algorithm="acme", verbose=True) (t3 - 15).plot(xlim=(20, -20), clear=False, color="r") show()
def test_nmr_multiple_auto_1D_phasing(): path = os.path.join(prefs.datadir, 'nmrdata', 'bruker', 'tests', 'nmr', 'topspin_1d') ndd = NDDataset.read_topspin(path, expno=1, remove_digital_filter=True) ndd /= ndd.real.data.max() # normalize ndd.em(10. * ur.Hz) # inplace broadening transf = ndd.fft(tdeff=8192, size=2**15) transf.plot(xlim=(20, -20), ls=':', color='k') t1 = transf.apk(algorithm='neg_peak', verbose=True) (t1 - 5.).plot(xlim=(20, -20), clear=False, color='b') t2 = t1.apk(algorithm='neg_area', verbose=True) (t2 - 10).plot(xlim=(20, -20), clear=False, ls='-.', color='m') t3 = t2.apk(algorithm='acme', verbose=True) (t3 - 15).plot(xlim=(20, -20), clear=False, color='r') show()
def test_EFA(IR_dataset_2D): ds = IR_dataset_2D.copy() # columns masking ds[:, 1230.0:920.0] = MASKED # do not forget to use float in slicing ds[:, 5900.0:5890.0] = MASKED # difference spectra ds -= ds[-1] # column masking for bad columns ds[10:12] = MASKED efa = EFA(ds) n_pc = 4 c = efa.get_conc(n_pc) c.T.plot() show()
def test_pca(): dataset = NDDataset.read("irdata/nh4y-activation.spg") # with masks dataset[:, 1240.0:920.0] = MASKED # do not forget to use float in slicing pca = PCA(dataset) assert str(pca)[:3] == "\nPC" # test alternative options pca = PCA(dataset, centered=False, standardized=True, scaled=True) assert str(pca)[:3] == "\nPC" _, LT = pca.reduce(n_pc="auto") assert LT.shape[0] == 15 dataset_hat = pca.reconstruct(n_pc=5) assert (dataset_hat - dataset).max() < 0.4 # test with observations < variables pca2 = PCA(dataset[:, 4000.0:3995.0]) _, LT2 = pca2.reduce(n_pc="auto") assert LT2.shape[0] == 5 # test text and plots outputs pca.printev(n_pc=5) pca.screeplot(n_pc=5) pca.screeplot(n_pc="auto") pca.scoreplot((1, 2)) pca.scoreplot(1, 2, 3) pca.plotmerit show()
def test_pca(): dataset = NDDataset.read('irdata/nh4y-activation.spg') # with masks dataset[:, 1240.0:920.0] = MASKED # do not forget to use float in slicing pca = PCA(dataset) pca.printev(n_pc=5) assert str(pca)[:3] == '\nPC' pca.screeplot(n_pc=0.999) pca.screeplot(n_pc='auto') pca.scoreplot((1, 2)) pca.scoreplot(1, 2, 3) show()
def test_ab_nmr(NMR_dataset_1D): dataset = NMR_dataset_1D.copy() dataset /= dataset.real.data.max() # nromalize dataset.em(10.0 * ur.Hz, inplace=True) dataset = dataset.fft(tdeff=8192, size=2**15) dataset = dataset[150.0:-150.0] + 1.0 dataset.plot() transf = dataset.copy() transfab = transf.ab(window=0.25) transfab.plot(clear=False, color="r") transf = dataset.copy() base = transf.ab(mode="poly", dryrun=True) transfab = transf - base transfab.plot(xlim=(150, -150), clear=False, color="b") base.plot(xlim=(150, -150), ylim=[-2, 10], clear=False, color="y") show()
def generate_fake(): from spectrochempy.utils import show # define properties of the spectra and concentration profiles # ---------------------------------------------------------------------------------------------------------------------- POS = (6000., 4000., 2000., 2500.) WIDTH = (6000., 1000., 600., 800.) AMPL = (100., 100., 20., 50.) def C1(t): return t * .05 + .01 # linear evolution of the baseline def C2(t): return np.exp(-t / .5) * .3 + .1 def C3(t): return np.exp(-t / 3.) * .7 def C4(t): return 1. - C2(t) - C3(t) spec = _make_spectra_matrix(POS, WIDTH, AMPL) spec.plot_stack(colorbar=False) conc = _make_concentrations_matrix(C1, C2, C3, C4) conc.plot_stack(colorbar=False) d = _generate_2D_spectra(conc, spec) # add some noise d.data = np.random.normal(d.data, .007 * d.data.max()) d.plot_stack() show() d.save('test_full2D') spec.save('test_spectra') conc.save('test_concentration')
def test_simplisma(): print("") data = NDDataset.read_matlab(os.path.join("matlabdata", "als2004dataset.MAT"), merge=False) print( "Dataset (Jaumot et al., Chemometr. Intell. Lab. 76 (2005) 101-110)):") print("") for mat in data: print(" " + mat.name, str(mat.shape)) ds = data[-1] assert ds.name == "m1" print("\n test simplisma on {}\n".format(ds.name)) pure = SIMPLISMA(ds, n_pc=20, tol=0.2, noise=3, verbose=True) pure.C.T.plot() pure.St.plot() pure.plotmerit() assert "3 29 29.0 0.0072 0.9981" in pure.logs show()