Esempio n. 1
0
def test_read_labspec():

    ramandir = Path('ramandata')
    scp.info_(ramandir)

    A = scp.read_labspec('Activation.txt', directory=ramandir)
    A.plot()

    A = scp.read_labspec('532nm-191216-Si 200mu.txt', directory=ramandir)
    A.plot()

    A = scp.read_labspec('serie190214-1.txt', directory=ramandir)
    A.plot(colorbar=True)
    A.plot_map(colorbar=True)

    A = scp.read_labspec('SMC1-Initial RT.txt', directory=ramandir)
    A.plot()

    B = scp.read(protocol='labspec', directory=ramandir)

    # this pack all spectra of the subdir directory
    B = scp.read_dir(directory=ramandir / 'subdir')
    B.plot()

    scp.show()
Esempio n. 2
0
def test_basecor_multivariate(IR_dataset_2D):
    dataset = IR_dataset_2D[5]

    basc = BaselineCorrection(dataset)
    s = basc([6000., 3500.], [1800., 1500.],
             method='multivariate',
             interpolation='pchip')
    s.plot()
    s = basc([6000., 3500.], [1800., 1500.],
             method='multivariate',
             interpolation='polynomial')
    s.plot(clear=False, color='red')

    dataset = IR_dataset_2D[:15]

    basc = BaselineCorrection(dataset)
    s = basc([6000., 3500.], [1800., 1500.],
             method='multivariate',
             interpolation='pchip')
    s.plot()
    s = basc([6000., 3500.], [1800., 1500.],
             method='multivariate',
             interpolation='polynomial')
    s.plot(cmap='copper')
    show()
Esempio n. 3
0
def test_read_topspin():
    # Open a dialog for selecting a Topspin directory
    A = scp.read_topspin(directory=nmrdir)
    assert A.name == 'topspin_2d expno:1 procno:1 (SER)'

    # A.plot()
    A = scp.read_topspin(nmrdir / 'exam2d_HC/3/pdata/1/2rr')
    A.plot_map()

    # Select a TOPSPIN spectra using the full name
    B = scp.read_topspin(nmrdir / 'topspin_1d/1/fid')
    assert str(B) == 'NDDataset: [complex128] unitless (size: 12411)'

    C = scp.read_topspin(nmrdir / 'topspin_1d/1/pdata/1/1r')
    assert str(C) == 'NDDataset: [complex128] unitless (size: 16384)'
    C.plot_map()

    # Select a TOPSPIN spectra using the full name
    B = scp.read_topspin(nmrdir / 'topspin_2d/1/ser')
    assert str(B) == 'NDDataset: [quaternion] unitless (shape: (y:96, x:948))'
    B.plot_surface()

    C = scp.read_topspin(nmrdir / 'topspin_2d/1/pdata/1/2rr')
    assert str(
        C) == 'NDDataset: [quaternion] unitless (shape: (y:1024, x:2048))'
    C.plot_image()

    # alternative syntax

    D = scp.read_topspin(nmrdir / 'topspin_2d', expno=1, procno=1)
    assert D == C

    scp.show()
Esempio n. 4
0
def test_autosub(IR_dataset_2D):
    dataset = IR_dataset_2D

    ranges = [5000., 5999.], [1940., 1820.]

    s1 = dataset.copy()
    ref = s1[-1].squeeze()

    dataset.plot_stack()
    ref.plot(clear=False, linewidth=2., 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()
Esempio n. 5
0
def test_plot2D_as_3D():
    data = NDDataset.read_matlab(
        os.path.join('matlabdata', 'als2004dataset.MAT'))

    X = data[0]
    # X.plot_3D()

    X.plot_surface()

    X.set_coordset(y=Coord(title='elution time'), x=Coord(title='wavenumbers'))
    X.title = 'intensity'
    X.plot_surface()

    X.plot_surface(colorbar=True)

    show()

    pass
Esempio n. 6
0
def test_basecor_sequential(IR_dataset_2D):
    dataset = IR_dataset_2D[5]
    basc = BaselineCorrection(dataset)

    s = basc([6000., 3500.], [2200., 1500.],
             method='sequential',
             interpolation='pchip')
    s.plot()

    s1 = basc([6000., 3500.], [2200., 1500.],
              method='sequential',
              interpolation='polynomial')
    s1.plot(clear=False, color='red')

    dataset = IR_dataset_2D[5]  # with LinearCoord
    basc = BaselineCorrection(dataset)

    s2 = basc([6000., 3500.], [2200., 1500.],
              method='sequential',
              interpolation='pchip')
    assert_dataset_almost_equal(s, s2, decimal=5)
    s2.plot(clear=False, color='green')

    s3 = basc([6000., 3500.], [2200., 1500.],
              method='sequential',
              interpolation='polynomial')
    assert_dataset_almost_equal(s1, s3, decimal=5)
    s3.plot(clear=False, color='cyan')

    show()

    dataset = IR_dataset_2D[:15]
    basc = BaselineCorrection(dataset)
    s = basc([6000., 3500.], [2200., 1500.],
             method='sequential',
             interpolation='pchip')
    s.plot()
    s = basc([6000., 3500.], [2200., 1500.],
             method='sequential',
             interpolation='polynomial')
    s.plot(cmap='copper')

    show()
Esempio n. 7
0
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., 3500.], [1800., 1500.],
             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., 3500.], [1800., 1500.],
             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., 2.e3], [3.0e4, 3.3e4]],
                dim='y',
                interpolation='polynomial',
                order=1,
                method='sequential')
    blc.corrected.plot()
    scp.show()

    # 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.:20., :])
    blc.compute(*[[10., 11.], [19., 20.]],
                dim='y',
                interpolation='polynomial',
                order=1,
                method='sequential')
    blc.corrected.T.plot()
    scp.show()
Esempio n. 8
0
def test_plot2D():
    A = NDDataset.read_omnic('irdata/nh4y-activation.spg')
    A.y -= A.y[0]
    A.y.to('hour', inplace=True)
    A.y.title = u'Aquisition time'
    A.copy().plot_stack()
    A.copy().plot_stack(data_transposed=True)
    A.copy().plot_image(style=['sans', 'paper'], fontsize=9)

    # use preferences
    prefs = A.preferences
    prefs.reset()
    prefs.image.cmap = 'magma'
    prefs.font.size = 10
    prefs.font.weight = 'bold'
    prefs.axes.grid = True
    A.plot()
    A.plot(style=['sans', 'paper', 'grayscale'], colorbar=False)
    show()
    pass
Esempio n. 9
0
def test_plot2D():
    A = NDDataset.read_omnic("irdata/nh4y-activation.spg")
    A.y -= A.y[0]
    A.y.to("hour", inplace=True)
    A.y.title = "Acquisition time"
    A.copy().plot_stack()
    A.copy().plot_stack(data_transposed=True)
    A.copy().plot_image(style=["sans", "paper"], fontsize=9)

    # use preferences
    prefs = A.preferences
    prefs.reset()
    prefs.image.cmap = "magma"
    prefs.font.size = 10
    prefs.font.weight = "bold"
    prefs.axes.grid = True
    A.plot()
    A.plot(style=["sans", "paper", "grayscale"], colorbar=False)

    show()
Esempio n. 10
0
def test_plot2D_as_3D():
    data = NDDataset.read_matlab(
        os.path.join("matlabdata", "als2004dataset.MAT"))

    X = data[0]

    X.plot_surface()

    X.set_coordset(
        y=Coord(title="elution time", units="s"),
        x=Coord(title="wavenumbers", units="cm^-1"),
    )
    X.title = "intensity"
    X.plot_surface()

    X.plot_surface(colorbar=True)

    show()

    pass
Esempio n. 11
0
def test_ab_nmr(NMR_dataset_1D):
    dataset = NMR_dataset_1D.copy()
    dataset /= dataset.real.data.max()  # nromalize

    dataset.em(10. * ur.Hz, inplace=True)
    dataset = dataset.fft(tdeff=8192, size=2**15)
    dataset = dataset[150.0:-150.] + 1.

    dataset.plot()

    transf = dataset.copy()
    transfab = transf.ab(window=.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()
Esempio n. 12
0
def test_fit_single_dataset(IR_dataset_2D, script):
    dataset = IR_dataset_2D[54, 3700.:3400.]

    f1 = Fit(dataset, script, silent=True)
    f1.run(maxiter=10, every=1)
    #    assert_approx_equal(dataset.model_A, -116.40475, significant=4)
    #    assert_approx_equal(f1.fp['width_line_2'], 195.7273, significant=4)
    dataset.plot(plot_model=True)

    dataset2 = dataset.copy() * 2.34
    f2 = Fit(dataset2, script, silent=True)
    f2.run(maxiter=10, every=1)

    dataset2.plot(plot_model=True)

    assert_approx_equal(dataset2.model_A, 272.3309560470805, significant=4)
    assert_approx_equal(f2.fp['width_line_2'], 195.7273, significant=4)

    f2 = Fit(dataset2, script, silent=False)
    f2.run(maxiter=1000, every=1)

    dataset2.plot(plot_model=True)
    show()
Esempio n. 13
0
def test_ndmath_and_api_methods(IR_dataset_1D, IR_dataset_2D):
    # CREATION _LIKE METHODS
    # ----------------------

    # from a list
    x = [1, 2, 3]

    # _like as an API method
    ds = NDDataset(x).full_like(2.5, title='empty')
    ds = scp.full_like(x, 2)
    assert np.all(ds.data == np.full((3, ), 2))
    assert ds.implements('NDDataset')

    # _like as a classmethod
    ds = NDDataset.full_like(x, 2)
    assert np.all(ds.data == np.full((3, ), 2))
    assert ds.implements('NDDataset')

    # _like as an instance method
    ds = NDDataset(x).full_like(2)
    assert np.all(ds.data == np.full((3, ), 2))
    assert ds.implements('NDDataset')

    # _like as an instance method
    ds = NDDataset(x).empty_like(title='empty')
    assert ds.implements('NDDataset')
    assert ds.title == 'empty'

    # from an array
    x = np.array([1, 2, 3])

    ds = NDDataset(x).full_like(2)
    assert np.all(ds.data == np.full((3, ), 2))
    assert ds.implements('NDDataset')

    # from a NDArray subclass with units
    x = NDDataset([1, 2, 3], units='km')
    ds = scp.full_like(x, 2)
    assert np.all(ds.data == np.full((3, ), 2))
    assert ds.implements('NDDataset')
    assert ds.units == ur.km

    ds1 = scp.full_like(ds, np.nan, dtype=np.double, units='m')
    assert ds1.units == Unit('m')

    # change of units is forced
    ds2 = scp.full_like(ds, 2, dtype=np.double, units='s')
    assert ds2.units == ur.s

    # other like creation functions
    nd = scp.empty_like(ds, dtype=np.double, units='m')
    assert str(nd) == 'NDDataset: [float64] m (size: 3)'
    assert nd.dtype == np.dtype(np.double)

    nd = scp.zeros_like(ds, dtype=np.double, units='m')
    assert str(nd) == 'NDDataset: [float64] m (size: 3)'
    assert np.all(nd.data == np.zeros((3, )))

    nd = scp.ones_like(ds, dtype=np.double, units='m')
    assert str(nd) == 'NDDataset: [float64] m (size: 3)'
    assert np.all(nd.data == np.ones((3, )))

    # FULL
    # ----

    ds = NDDataset.full((6, ), 0.1)
    assert ds.size == 6
    assert str(ds) == 'NDDataset: [float64] unitless (size: 6)'

    # ZEROS
    # -----

    ds = NDDataset.zeros((6, ), units='km')
    assert ds.size == 6
    assert str(ds) == 'NDDataset: [float64] km (size: 6)'

    # ONES
    # ----

    ds = NDDataset.ones((6, ))
    ds = scp.full((6, ), 0.1)
    assert ds.size == 6
    assert str(ds) == 'NDDataset: [float64] unitless (size: 6)'

    ds = NDDataset.ones((6, ), units='absorbance', dtype='complex128')
    assert ds.size == 3
    assert str(ds) == 'NDDataset: [complex128] a.u. (size: 3)'
    assert ds[0].data == 1. + 1.j

    # LINSPACE
    # --------

    c2 = Coord.linspace(1, 20, 200, units='m', name='mycoord')
    assert c2.name == 'mycoord'
    assert c2.size == 200
    assert c2[-1].data == 20
    assert c2[0].values == Quantity(1, 'm')

    # ARANGE
    # -------

    c3 = Coord.arange(1, 20.0001, 1, units='s', name='mycoord')
    assert c3.name == 'mycoord'
    assert c3.size == 20
    assert c3[-1].data == 20
    assert c3[0].values == Quantity(1, 's')

    # EYE
    # ----

    ds1 = scp.NDDataset.eye(2, dtype=int)
    assert str(ds1) == 'NDDataset: [float64] unitless (shape: (y:2, x:2))'
    ds = scp.eye(3, k=1, units='km')
    assert (ds.data == np.eye(3, k=1)).all()
    assert ds.units == ur.km

    # IDENTITY
    # --------

    ds = scp.identity(3, units='km')
    assert (ds.data == np.identity(3, )).all()
    assert ds.units == ur.km

    # RANDOM
    # ------

    ds = scp.random((3, 3), units='km')
    assert str(ds) == 'NDDataset: [float64] km (shape: (y:3, x:3))'

    # adding coordset
    c1 = Coord.linspace(1, 20, 200, units='m', name='axe_x')
    ds = scp.random((200, ), units='km', coordset=scp.CoordSet(x=c1))

    # DIAGONAL
    # --------

    # extract diagonal
    nd = scp.full((2, 2), 0.5, units='s', title='initial')
    assert str(nd) == "NDDataset: [float64] s (shape: (y:2, x:2))"
    ndd = scp.diagonal(nd, title='diag')
    assert str(ndd) == 'NDDataset: [float64] s (size: 2)'
    assert ndd.units == Unit('s')

    cx = scp.Coord([0, 1])
    cy = scp.Coord([2, 5])
    nd = NDDataset.full((2, 2),
                        0.5,
                        units='s',
                        coordset=scp.CoordSet(cx, cy),
                        title='initial')
    assert str(nd) == "NDDataset: [float64] s (shape: (y:2, x:2))"
    ndd = nd.diagonal(title='diag2')
    assert str(ndd) == 'NDDataset: [float64] s (size: 2)'
    assert ndd.units == Unit('s')
    assert ndd.title == 'diag2'

    cx = scp.Coord([0, 1, 2])
    cy = scp.Coord([2, 5])
    nd = NDDataset.full((2, 3),
                        0.5,
                        units='s',
                        coordset=scp.CoordSet(x=cx, y=cy),
                        title='initial')
    assert str(nd) == "NDDataset: [float64] s (shape: (y:2, x:3))"
    ndd = nd.diagonal(title='diag3')
    assert str(ndd) == 'NDDataset: [float64] s (size: 2)'
    assert ndd.units == Unit('s')
    assert ndd.title == 'diag3'
    assert_array_equal(nd.x.data[:ndd.x.size], ndd.x.data)

    ndd = nd.diagonal(title='diag4', dim='y')
    assert str(ndd) == 'NDDataset: [float64] s (size: 2)'
    assert ndd.units == Unit('s')
    assert ndd.title == 'diag4'
    assert_array_equal(nd.y.data[:ndd.y.size], ndd.y.data)

    # DIAG
    # ----

    ref = NDDataset(np.diag((3, 3.4, 2.3)), units='m', title='something')

    # Three forms should return the same NDDataset
    ds = scp.diag((3, 3.4, 2.3), units='m', title='something')
    assert_dataset_equal(ds, ref)

    ds = NDDataset.diag((3, 3.4, 2.3), units='m', title='something')
    assert_dataset_equal(ds, ref)

    ds = NDDataset((3, 3.4, 2.3)).diag(units='m', title='something')
    assert_dataset_equal(ds, ref)

    # and this too
    ds1 = NDDataset((3, 3.4, 2.3), units='s', title='another')

    ds = scp.diag(ds1, units='m', title='something')
    assert_dataset_equal(ds, ref)

    ds = ds1.diag(units='m', title='something')
    assert_dataset_equal(ds, ref)

    # BOOL : ALL and ANY
    # ------------------

    ds = NDDataset([[True, False], [True, True]])
    b = np.all(ds)
    assert not b

    b = scp.all(ds)
    assert not b

    b = ds.all()
    assert not b

    b = NDDataset.any(ds)
    assert b

    b = ds.all(dim='y')
    assert_array_equal(b, np.array([True, False]))

    b = ds.any(dim='y')
    assert_array_equal(b, np.array([True, True]))

    # ARGMAX, MAX
    # -----------

    nd1 = IR_dataset_1D.copy()
    nd1[1290.:890.] = MASKED
    assert nd1.is_masked
    assert str(nd1) == 'NDDataset: [float64] a.u. (size: 5549)'

    idx = nd1.argmax()
    assert idx == 3122

    mx = nd1.max()
    # alternative
    mx = scp.max(nd1)
    mx = NDDataset.max(nd1)
    assert mx == Quantity(3.8080601692199707, 'absorbance')

    mxk = nd1.max(keepdims=True)
    assert isinstance(mxk, NDDataset)
    assert str(mxk) == 'NDDataset: [float64] a.u. (size: 1)'
    assert mxk.values == mx

    # test on a 2D NDDataset
    nd2 = IR_dataset_2D.copy()
    nd2[:, 1290.:890.] = MASKED

    mx = nd2.max()  # no axis specified
    assert mx == Quantity(3.8080601692199707, 'absorbance')
    mxk = nd2.max(keepdims=True)
    assert str(mxk) == 'NDDataset: [float64] a.u. (shape: (y:1, x:1))'

    nd2m = nd2.max('y')  # axis selected
    ax = nd2m.plot()
    nd2[0].plot(ax=ax, clear=False)
    scp.show()

    nd2m2 = nd2.max('x')  # axis selected
    nd2m2.plot()
    scp.show()

    nd2m = nd2.max('y', keepdims=True)
    assert nd2m.shape == (1, 5549)

    nd2m = nd2.max('x', keepdims=True)
    assert nd2m.shape == (55, 1)

    mx = nd2.min()  # no axis specified
    assert mx == Quantity(-0.022955093532800674, 'absorbance')
    mxk = nd2.min(keepdims=True)
    assert str(mxk) == 'NDDataset: [float64] a.u. (shape: (y:1, x:1))'

    nd2m = nd2.min('y')  # axis selected
    ax = nd2m.plot()
    nd2[0].plot(ax=ax, clear=False)
    scp.show()

    nd2m2 = nd2.min('x')  # axis selected
    nd2m2.plot()
    scp.show()

    nd2m = nd2.min('y', keepdims=True)
    assert nd2m.shape == (1, 5549)

    nd2m = nd2.min('x', keepdims=True)
    assert nd2m.shape == (55, 1)

    # CLIP
    # ----
    nd3 = nd2 - 2.
    assert nd3.units == nd2.units
    nd3c = nd3.clip(-.5, 1.)
    assert nd3c.max().m == 1.
    assert nd3c.min().m == -.5

    # COORDMIN AND COORDMAX
    # ---------------------
    cm = nd2.coordmin()
    assert np.around(cm['x'], 3) == Quantity(1290.165, 'cm^-1')

    cm = nd2.coordmin(dim='y')
    assert cm.size == 1

    cm = nd2.coordmax(dim='y')
    assert cm.size == 1

    cm = nd2.coordmax(dim='x')
    assert cm.size == 1

    # ABS
    # ----
    nd2a = scp.abs(nd2)
    mxa = nd2a.min()
    assert mxa > 0

    nd2a = NDDataset.abs(nd2)
    mxa = nd2a.min()
    assert mxa > 0

    nd2a = np.abs(nd2)
    mxa = nd2a.min()
    assert mxa > 0

    ndd = NDDataset([1., 2. + 1j, 3.])
    val = np.abs(ndd)

    val = ndd[1] * 1.2 - 10.
    val = np.abs(val)

    # FROMFUNCTION
    # ------------
    # 1D
    def func1(t, v):
        d = v * t
        return d

    time = Coord.linspace(
        0,
        9,
        10,
    )
    distance = NDDataset.fromfunction(func1, v=134, coordset=CoordSet(t=time))
    assert distance.dims == ['t']
    assert_array_equal(distance.data, np.fromfunction(func1, (10, ), v=134))

    time = Coord.linspace(0, 90, 10, units='min')
    distance = NDDataset.fromfunction(func1,
                                      v=Quantity(134, 'km/hour'),
                                      coordset=CoordSet(t=time))
    assert distance.dims == ['t']
    assert_array_equal(distance.data,
                       np.fromfunction(func1, (10, ), v=134) * 10 / 60)

    # 2D
    def func2(x, y):
        d = x + 1 / y
        return d

    c0 = Coord.linspace(0, 9, 3)
    c1 = Coord.linspace(10, 20, 2)

    # implicit ordering of coords (y,x)
    distance = NDDataset.fromfunction(func2, coordset=CoordSet(c1, c0))
    assert distance.shape == (2, 3)
    assert distance.dims == ['y', 'x']

    # or equivalent
    distance = NDDataset.fromfunction(func2, coordset=[c1, c0])
    assert distance.shape == (2, 3)
    assert distance.dims == ['y', 'x']

    # explicit ordering of coords (y,x)  #
    distance = NDDataset.fromfunction(func2, coordset=CoordSet(u=c0, v=c1))

    assert distance.shape == (2, 3)
    assert distance.dims == ['v', 'u']
    assert distance[0, 2].data == distance.u[2].data + 1. / distance.v[0].data

    # with units
    def func3(x, y):
        d = x + y
        return d

    c0u = Coord.linspace(0, 9, 3, units='km')
    c1u = Coord.linspace(10, 20, 2, units='m')
    distance = NDDataset.fromfunction(func3, coordset=CoordSet(u=c0u, v=c1u))

    assert distance.shape == (2, 3)
    assert distance.dims == ['v', 'u']
    assert distance[0, 2].values == distance.u[2].values + distance.v[0].values

    c0u = Coord.linspace(0, 9, 3, units='km')
    c1u = Coord.linspace(10, 20, 2, units='m^-1')
    distance = NDDataset.fromfunction(func2, coordset=CoordSet(u=c0u, v=c1u))

    assert distance.shape == (2, 3)
    assert distance.dims == ['v', 'u']
    assert distance[
        0, 2].values == distance.u[2].values + 1. / distance.v[0].values

    # FROMITER
    # --------
    iterable = (x * x for x in range(5))
    nit = scp.fromiter(iterable, float, units='km')
    assert str(nit) == 'NDDataset: [float64] km (size: 5)'
    assert_array_equal(nit.data, np.array([0, 1, 4, 9, 16]))

    # MEAN, AVERAGE
    # -----
    nd = IR_dataset_2D.copy()
    m = scp.mean(nd)
    assert m == Quantity(np.mean(nd.data), "absorbance")

    m = scp.average(nd)
    assert m == Quantity(np.average(nd.data), "absorbance")

    mx = scp.mean(nd, keepdims=True)
    assert mx.shape == (1, 1)

    mxd = scp.mean(nd, dim='y')
    assert str(mxd) == 'NDDataset: [float64] a.u. (size: 5549)'
    assert str(mxd.x) == 'LinearCoord: [float64] cm^-1 (size: 5549)'
Esempio n. 14
0
def test_1D():
    dataset = NDDataset.read_omnic(
        os.path.join(prefs.datadir, 'irdata', 'nh4y-activation.spg'))

    # get first spectrum
    nd0 = dataset[0]

    # plot generic
    nd0.plot()

    # nd0.plot(output=os.path.join(figures_dir, 'IR_dataset_1D'),
    #          savedpi=150)
    #
    # # plot generic style
    # nd0.plot(style='poster',
    #          output=os.path.join(figures_dir, 'IR_dataset_1D_poster'),
    #          savedpi=150)
    #
    # # check that style reinit to default
    # nd0.plot(output='IR_dataset_1D', savedpi=150)
    # # try:
    # #     assert same_images('IR_dataset_1D.png',
    # #                        os.path.join(figures_dir, 'IR_dataset_1D.png'))
    # # except AssertionError:
    # #     os.remove('IR_dataset_1D.png')
    # #     raise AssertionError('comparison fails')
    # # os.remove('IR_dataset_1D.png')
    #
    # # try other type of plots
    # nd0.plot_pen()
    # nd0[:, ::100].plot_scatter()
    # nd0.plot_lines()
    # nd0[:, ::100].plot_bar()
    #
    # show()
    #
    # # multiple
    # d = dataset[:, ::100]
    # datasets = [d[0], d[10], d[20], d[50], d[53]]
    # labels = ['sample {}'.format(label) for label in
    #           ["S1", "S10", "S20", "S50", "S53"]]
    #
    # # plot multiple
    # plot_multiple(method='scatter',
    #               datasets=datasets, labels=labels, legend='best',
    #               output=os.path.join(figures_dir,
    #                                   'multiple_IR_dataset_1D_scatter'),
    #               savedpi=150)
    #
    # # plot mupltiple with style
    # plot_multiple(method='scatter', style='sans',
    #               datasets=datasets, labels=labels, legend='best',
    #               output=os.path.join(figures_dir,
    #                                   'multiple_IR_dataset_1D_scatter_sans'),
    #               savedpi=150)
    #
    # # check that style reinit to default
    # plot_multiple(method='scatter',
    #               datasets=datasets, labels=labels, legend='best',
    #               output='multiple_IR_dataset_1D_scatter',
    #               savedpi=150)
    # try:
    #     assert same_images('multiple_IR_dataset_1D_scatter',
    #                        os.path.join(figures_dir,
    #                                     'multiple_IR_dataset_1D_scatter'))
    # except AssertionError:
    #     os.remove('multiple_IR_dataset_1D_scatter.png')
    #     raise AssertionError('comparison fails')
    # os.remove('multiple_IR_dataset_1D_scatter.png')

    # plot 1D column
    col = dataset[:, 3500.]  # note the indexing using wavenumber!
    _ = col.plot_scatter()

    show()
Esempio n. 15
0
def test_MCRALS():
    """Test MCRALS with synthetic data"""

    def gaussian(x, h, c, w, noise):
        return h * (np.exp(-1 / 2 * ((x.data - c) / w) ** 2)) + noise * np.random.randn(
            len(x)
        )  # a gaussian with added noise

    def expon(t, c0, l, noise):
        return c0 * (np.exp(l * t.data)) + noise * np.random.randn(len(t.data))

    def get_C(C):
        return C

    n_PS = 2  # number of pure species
    n_t = 10
    n_wl = 10
    h = [1, 1]
    c = [250, 750]
    w = [100, 100]
    noise_spec = [0.0, 0.0]
    noise_conc = [0.0, 0.0]

    c0 = [10, 1]
    l = np.array([-2, 2]) * 1e-2

    t_c = Coord(np.arange(0, 100, 100 / n_t), title="time", units="s")
    wl_c = Coord(np.arange(0, 1000, 1000 / n_wl), title="wavelength", units="nm")
    PS_c = Coord(
        range(n_PS),
        title="species",
        units=None,
        labels=["PS#" + str(i) for i in range(n_PS)],
    )

    St = NDDataset.zeros((n_PS, len(wl_c)), coordset=(PS_c, wl_c))
    C = NDDataset.zeros((len(t_c), n_PS), coordset=(t_c, PS_c))

    St0 = NDDataset.zeros((n_PS, len(wl_c)), coordset=(PS_c, wl_c))
    C0 = NDDataset.zeros((len(t_c), n_PS), coordset=(t_c, PS_c))

    for i, id in enumerate((0, 1)):
        C.data[:, i] = expon(t_c, c0[id], l[id], noise_conc[id])
        St.data[i, :] = gaussian(wl_c, h[id], c[id], w[id], noise_spec[id])

        C0.data[:, i] = expon(t_c, c0[id], l[id], 0)
        St0.data[i, :] = gaussian(wl_c, h[id], c[id], w[id], 0)

    D = dot(C, St)
    D.title = "intensity"

    #############################
    # Test normal functioning

    # guess = C0
    mcr = MCRALS(D, C0, tol=30.0)

    assert "converged !" in mcr.log[-15:]

    # test attributes
    for attr in [
        "log",
        "logs",
        "params",
        "Chard",
        "Stsoft",
        "St",
        "C",
        "extOutput",
        "fixedC",
        "X",
    ]:
        assert hasattr(mcr, attr)

    # test plot
    mcr.plotmerit()
    show()

    # test diverging
    mcr = MCRALS(
        D,
        C0,
        monoIncConc=[0, 1],
        monoIncTol=1.0,
        unimodSpec=[0, 1],
        normSpec="euclid",
        closureConc=[0, 1],
        closureMethod="constantSum",
        maxdiv=1,
    )
    assert "Stop ALS optimization" in mcr.log[-40:]

    # guess = C0, hard modeling
    mcr = MCRALS(D, C0, hardConc=[0, 1], getConc=get_C, argsGetConc=(C0,), tol=30.0)
    assert "converged !" in mcr.log[-15:]

    # guess = C, test with deprecated parameters
    # and few other parameters set to non-default values to improve coverage
    mcr = MCRALS(
        D,
        C0,
        # deprecated:
        unimodMod="strict",
        unimodTol=1.1,
        verbose=True,
        # other parameters set to non-default values
        monoIncConc=[0],
        monoDecConc=[1],
        closureConc=[0, 1],
        nonnegConc=None,
        unimodConc=None,
        unimodSpec="all",
        nonnegSpec=None,
        normSpec="max",
        maxit=1,
    )
    set_loglevel("WARN")

    # guess = C0.data, test with other parameters
    mcr = MCRALS(
        D,
        C0.data,
        normSpec="euclid",
        closureConc=[0, 1],
        closureMethod="constantSum",
        maxit=1,
    )
    assert "Convergence criterion ('tol')" in mcr.log[-100:]

    # guess = St as ndarray
    mcr = MCRALS(D, St0.data, tol=15.0)
    assert "converged !" in mcr.log[-15:]

    #########################
    # Text exceptions

    # guess with wrong number of dimensions
    try:
        mcr = MCRALS(
            D,
            np.random.rand(n_t - 1, n_PS),
        )
    except ValueError as e:
        assert e.args[0] == "the dimensions of guess do not match the data"

    # guess with wrong nonnegConc parameter
    try:
        mcr = MCRALS(D, C, nonnegConc=[2])
    except ValueError as e:
        assert "please check nonnegConc" in e.args[0]
    try:
        mcr = MCRALS(D, C, nonnegConc=[0, 1, 1])
    except ValueError as e:
        assert "please check nonnegConc" in e.args[0]

    # guess with wrong unimodConc parameter
    try:
        mcr = MCRALS(D, C, unimodConc=[2])
    except ValueError as e:
        assert "please check unimodConc" in e.args[0]
    try:
        mcr = MCRALS(D, C, unimodConc=[0, 1, 1])
    except ValueError as e:
        assert "please check unimodConc" in e.args[0]

    # wrong closureTarget
    try:
        mcr = MCRALS(D, C, closureTarget=[0, 1, 1])
    except ValueError as e:
        assert "please check closureTarget" in e.args[0]

    # wrong hardC_to_C_idx
    try:
        mcr = MCRALS(D, C, hardC_to_C_idx=[2])
    except ValueError as e:
        assert "please check hardC_to_C_idx" in e.args[0]
    try:
        mcr = MCRALS(D, C, hardC_to_C_idx=[0, 1, 1])
    except ValueError as e:
        assert "please check hardC_to_C_idx" in e.args[0]

    # wrong unimodSpec
    try:
        mcr = MCRALS(D, C, unimodSpec=[2])
    except ValueError as e:
        assert "please check unimodSpec" in e.args[0]
    try:
        mcr = MCRALS(D, C, unimodSpec=[0, 1, 1])
    except ValueError as e:
        assert "please check unimodSpec" in e.args[0]

    # wrong nonnegSpec
    try:
        mcr = MCRALS(D, C, nonnegSpec=[2])
    except ValueError as e:
        assert "please check nonnegSpec" in e.args[0]
    try:
        mcr = MCRALS(D, C, nonnegSpec=[0, 1, 1])
    except ValueError as e:
        assert "please check nonnegSpec" in e.args[0]
Esempio n. 16
0
# -*- coding: utf-8 -*-
# flake8: noqa
# ======================================================================================================================
#  Copyright (©) 2015-2020 LCS - Laboratoire Catalyse et Spectrochimie, Caen, France.                                  =
#  CeCILL-B FREE SOFTWARE LICENSE AGREEMENT - See full LICENSE agreement in the root directory                         =
# ======================================================================================================================
"""
Loading Bruker OPUS files
============================================

Here we load an experimental Bruker OPUS files and plot it.

"""

import spectrochempy as scp

Z = scp.read_opus(['test.0000', 'test.0001', 'test.0002', 'test.0003'],
                  directory='irdata/OPUS')
print(Z)

# %%
# plot it

Z.plot()

scp.show(
)  # uncomment to show plot if needed (not necessary in jupyter notebook)