Example #1
0
def test_nddataset_slicing_by_index(ref_ds, ds1):
    da = ds1
    ref = ref_ds
    # case where the index is an integer: the selection is by index starting
    # at zero
    assert da.shape == ref.shape
    coords = da.coordset
    plane0 = da[0]
    # should return a dataset of with dimension x of size 1
    assert type(plane0) == type(da)
    assert plane0.ndim == 3
    assert plane0.shape == (1, 100, 3)
    assert plane0.size == 300
    assert plane0.dims == ['z', 'y', 'x']
    assert_dataset_equal(plane0.z, da.z[0])
    da1 = plane0.squeeze()
    assert da1.shape == (100, 3)
    assert da1.dims == ['y', 'x']
    plane1 = da[:, 0]
    assert type(plane1) == type(da)
    assert plane1.ndim == 3
    assert plane1.shape == (10, 1, 3)
    assert plane1.size == 30
    assert plane1.dims == ['z', 'y', 'x']
    da2 = plane1.squeeze()
    assert da2.dims == ['z', 'x']
    assert_dataset_almost_equal(da2.z, coords[-1], decimal=2)  # remember
    # coordinates
    # are ordered by name!
    assert_dataset_almost_equal(da2.x, coords[0])
    # another selection
    row0 = plane0[:, 0]
    assert type(row0) == type(da)
    assert row0.shape == (1, 1, 3)
    # and again selection
    element = row0[..., 0]
    assert type(element) == type(da)
    assert element.dims == ['z', 'y', 'x']
    # squeeze
    row1 = row0.squeeze()
    assert row1.mask == scp.NOMASK
    row1[0] = scp.MASKED
    assert row1.dims == ['x']
    assert row1.shape == (3,)
    assert row1.mask.shape == (3,)
    element = row1[..., 0]
    assert element.x == coords[0][0]
    # now a slicing in multi direction
    da[1:4, 10:40:2, :2]
    # now again a slicing in multi direction (full selection)
    da[:, :, -2]
    # now again a slicing in multi direction (ellipsis)
    da[..., -1]
    da[-1, ...]
Example #2
0
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()
Example #3
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()
Example #4
0
def test_compare(IR_dataset_1D, simple_project):
    # dataset comparison

    nd1 = IR_dataset_1D.copy()
    nd2 = nd1.copy()

    testing.assert_dataset_equal(nd1, nd2)

    nd3 = nd1.copy()
    nd3.title = 'ddd'

    with testing.raises(AssertionError):
        testing.assert_dataset_equal(nd1, nd3)

    nd4 = nd1.copy()
    nd4.data += 0.001

    with testing.raises(AssertionError):
        testing.assert_dataset_equal(nd1, nd4)

    testing.assert_dataset_almost_equal(nd1, nd4, decimal=3)

    with testing.raises(AssertionError):
        testing.assert_dataset_almost_equal(nd1, nd4, decimal=4)

    # project comparison

    proj1 = simple_project.copy()
    proj1.name = 'PROJ1'
    proj2 = proj1.copy()
    proj2.name = 'PROJ2'

    testing.assert_project_equal(proj1, proj2)

    proj3 = proj2.copy()
    proj3.add_script(Script(content='print()', name='just_a_try'))

    with testing.raises(AssertionError):
        testing.assert_project_equal(proj1, proj3)
Example #5
0
def test_read_write_jdx(IR_dataset_2D):
    X = IR_dataset_2D[:10, :100]

    # write
    f = X.write_jdx('nh4y-activation.jdx')

    # read
    Y = NDDataset.read_jdx(f)

    assert_dataset_almost_equal(X, Y, decimal=2)

    # delete
    f.unlink()

    # write
    f = X.write_jdx()
    assert f.stem == X.name

    Y = NDDataset.read_jcamp(f, name='xxx')
    assert Y.name == 'xxx'

    f.unlink()
def test_compare_dataset(IR_dataset_1D):
    # dataset comparison

    nd1 = IR_dataset_1D.copy()
    nd2 = nd1.copy()

    testing.assert_dataset_equal(nd1, nd2)

    nd3 = nd1.copy()
    nd3.title = "ddd"

    with testing.raises(AssertionError):
        testing.assert_dataset_equal(nd1, nd3)

    nd4 = nd1.copy()
    nd4.data += 0.001

    with testing.raises(AssertionError):
        testing.assert_dataset_equal(nd1, nd4)

    testing.assert_dataset_almost_equal(nd1, nd4, decimal=3)

    with testing.raises(AssertionError):
        testing.assert_dataset_almost_equal(nd1, nd4, decimal=4)
Example #7
0
def test_concatenate(IR_dataset_2D):
    dataset = IR_dataset_2D

    # print(dataset)
    s1 = dataset[:, -10:]
    s2 = dataset[:, :-10]

    # specify axis
    dim = 'x'
    s = concatenate(s1, s2, dims=dim)
    assert s.units == s1.units
    assert s.shape[-1] == (s1.shape[-1] + s2.shape[-1])
    assert s.x.size == (s1.x.size + s2.x.size)
    assert s.x != dataset.x
    s = s.sort(dims=dim, descend=True)  #
    assert_dataset_almost_equal(s.x, Coord(dataset.x, linear=False), decimal=3)

    # default concatenation in the last dimensions
    s = concatenate(s1, s2)
    assert s.units == s1.units
    assert s.shape[-1] == (s1.shape[-1] + s2.shape[-1])
    assert s.x.size == (s1.x.size + s2.x.size)
    assert s.x != dataset.x
    s = s.sort(descend=True)  #
    assert_dataset_almost_equal(s.x, Coord(dataset.x, linear=False), decimal=3)

    s1 = dataset[:10]
    s2 = dataset[20:]
    # check with derived units
    s1.to(ur.m, force=True)
    s2.to(ur.dm, force=True)
    s = concatenate(s1, s2, dim=0)
    assert s.units == s1.units
    assert s.shape[0] == (s1.shape[0] + s2.shape[0])
    assert s.y.size == (s1.y.size + s2.y.size)
    s = s.sort(dim='y')
    s.plot()

    # second syntax
    s = s1.concatenate(s2, dim=0)
    assert s.units == s1.units
    assert s.shape[0] == (s1.shape[0] + s2.shape[0])
    assert s.y.size == (s1.y.size + s2.y.size)

    # third syntax
    s = concatenate((s1, s2), dim=0)
    assert s.units == s1.units
    assert s.shape[0] == (s1.shape[0] + s2.shape[0])
    assert s.y.size == (s1.y.size + s2.y.size)

    # concatenation in the first dimenson using stack
    s = stack(s1, s2)
    assert s.units == s1.units
    assert s.shape[0] == (s1.shape[0] + s2.shape[0])
    assert s.y.size == (s1.y.size + s2.y.size)

    # Stacking of datasets:
    # for nDimensional datasets (with the same shape), a new dimension is added
    ss = concatenate(s.copy(), s.copy(), force_stack=True)  # make a copy of s
    # (dataset cannot be concatenated to itself!)
    assert ss.shape == (2, 45, 5549)

    # If one of the dimensions is of size one, then this dimension is removed before stacking
    s0 = s[0]
    s1 = s[1]
    ss = s0.concatenate(s1, force_stack=True)
    assert s0.shape == (1, 5549)
    assert ss.shape == (2, 5549)

    # stack squeezed nD dataset
    s0 = s[0].copy().squeeze()
    assert s0.shape == (5549, )
    s1 = s[1].squeeze()
    assert s1.shape == (5549, )
    ss = concatenate(s0, s1, force_stack=True)
    assert ss.shape == (2, 5549)
def test_concatenate(IR_dataset_2D):
    dataset = IR_dataset_2D
    dim = "x"
    # print(dataset)
    s = dataset
    s1 = dataset[:, -10:]
    s2 = dataset[:, :-10]

    # specify axis
    s = concatenate(s1, s2, dims=dim)
    assert s.units == s1.units
    assert s.shape[-1] == (s1.shape[-1] + s2.shape[-1])
    assert s.x.size == (s1.x.size + s2.x.size)
    assert s.x != dataset.x
    s = s.sort(dims=dim, descend=True)  #
    assert_dataset_almost_equal(s.x, Coord(dataset.x, linear=False), decimal=3)

    # default concatenation in the last dimension
    s = concatenate(s1, s2)
    assert s.units == s1.units
    assert s.shape[-1] == (s1.shape[-1] + s2.shape[-1])
    assert s.x.size == (s1.x.size + s2.x.size)
    assert s.x != dataset.x
    s = s.sort(descend=True)  #
    assert_dataset_almost_equal(s.x, Coord(dataset.x, linear=False), decimal=3)

    s1 = dataset[:10]
    s2 = dataset[20:]
    # check with derived units
    s1.to(ur.m, force=True)
    s2.to(ur.dm, force=True)
    s = concatenate(s1, s2, dims=0)
    assert s.units == s1.units
    assert s.shape[0] == (s1.shape[0] + s2.shape[0])
    assert s.y.size == (s1.y.size + s2.y.size)
    s = s.sort(dim="y")

    # second syntax
    s = s1.concatenate(s2, dims=0)
    assert s.units == s1.units
    assert s.shape[0] == (s1.shape[0] + s2.shape[0])
    assert s.y.size == (s1.y.size + s2.y.size)

    # third syntax
    s = concatenate((s1, s2), dims=0)
    assert s.units == s1.units
    assert s.shape[0] == (s1.shape[0] + s2.shape[0])
    assert s.y.size == (s1.y.size + s2.y.size)

    # coordset
    coord_2 = Coord(np.log(s.y.data), title="log_time")
    s.set_coordset(y=[s.y, coord_2], x=s.x)
    s1 = s[:2]
    s2 = s[-5:]
    s12 = concatenate(s1, s2, axis=0)
    assert (s2["y"].labels[1] == s12["y"].labels[1][-5:]).all()

    # authors
    s0 = s[0]
    s1 = s[1]
    s0.author = "sdqe65g4rf"
    s2 = concatenate(s0, s1)
    assert "sdqe65g4rf" in s2.author and s1.author in s2.author

    # titles
    s0.title = "new_title"
    assert concatenate(s0, s1).title == "new_title"

    # incompatible dimensions
    s0 = scp.NDDataset(np.zeros((10, 100)))
    s1 = scp.NDDataset(np.zeros((10, 100)))
    with pytest.raises(DimensionsCompatibilityError):
        s0.concatenate(s1[0].squeeze())

    with pytest.raises(DimensionsCompatibilityError):
        s0.concatenate(s1[:, :50], axis=0)

    # incompatible units
    s0 = scp.NDDataset(np.zeros((10, 100)), units="V")
    s1 = scp.NDDataset(np.zeros((10, 100)), units="A")
    with pytest.raises(UnitsCompatibilityError):
        scp.concatenate(s0, s1)

    s1 = scp.NDDataset(np.ones((10, 100)), units="mV")
    s01 = scp.concatenate(s0, s1)
    assert s01.data[-1, -1] == 0.001

    # -------------------
    # Stack

    # concatenation using stack
    s1 = dataset[:10]
    s2 = dataset[-10:]
    s = stack(s1, s2)
    assert s.units == s1.units
    assert s.shape == (2, s1.shape[0], s1.shape[1])
    assert s.y.size == s1.y.size
    assert s.x.size == s1.x.size

    with pytest.warns(DeprecationWarning):
        concatenate(s1, s2, force_stack=True)

    # If one of the dimensions is of size one, then this dimension is NOT removed before stacking
    s0 = dataset[0]
    s1 = dataset[1]
    ss = stack(s0, s1)
    assert s0.shape == (1, 5549)
    assert ss.shape == (2, s1.shape[0], s1.shape[1])

    # # stack squeezed nD dataset
    s0 = dataset[0].copy().squeeze()
    assert s0.shape == (5549,)
    s1 = dataset[1].squeeze()
    assert s1.shape == (5549,)
    s = stack(s0, s1)
    assert s.shape == (2, 5549)

    # # stack squeezed nD dataset
    s2 = s1[0:100]
    with pytest.raises(DimensionsCompatibilityError):
        s = stack(s0, s2)