Example #1
0
def test_ndcomplex_complex(ndarraycplx):
    nd = ndarraycplx.copy()

    ndr = nd.real
    assert_array_equal(ndr.data, nd.data.real)
    assert ndr.size == nd.size
    assert not ndr.is_complex
Example #2
0
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_nddataset_real_imag():
    na = np.array(
        [[1.0 + 2.0j, 2.0 + 0j], [1.3 + 2.0j, 2.0 + 0.5j], [1.0 + 4.2j, 2.0 + 3j]]
    )
    nd = scp.NDDataset(na)
    # in the last dimension
    assert_array_equal(nd.real, na.real)
    assert_array_equal(nd.imag, na.imag)
Example #4
0
def test_nddataset_binary_operation_with_other_1D():
    coord1 = Coord(np.linspace(0.0, 10.0, 10))
    coord2 = Coord(np.linspace(1.0, 5.5, 5))
    d1 = NDDataset(np.random.random((10, 5)), coordset=[coord1, coord2])
    d2 = d1[0]
    # this should work independently of the value of the coordinates on dimension y
    d3 = d1 - d2
    assert_array_equal(d3.data, d1.data - d2.data)
Example #5
0
def test_ndarray_sort():
    # labels and sort

    d0 = NDArray(
        np.linspace(4000, 1000, 10),
        labels="a b c d e f g h i j".split(),
        units="s",
        mask=False,
        title="wavelength",
    )

    assert d0.is_labeled

    d1 = d0._sort()
    assert d1.data[0] == 1000

    # check inplace
    d2 = d0._sort(inplace=True)
    assert d0.data[0] == 1000
    assert d2 is d0

    # check descend
    d0._sort(descend=True, inplace=True)
    assert d0.data[0] == 4000

    # check sort using label
    d3 = d0._sort(by="label", descend=True)
    assert d3.labels[0] == "j"

    # multilabels
    # add a row of labels to d0
    d0.labels = "bc cd de ef ab fg gh hi ja ij ".split()

    d1 = d0._sort()
    assert d1.data[0] == 1000
    assert_array_equal(d1.labels[0], ["j", "ij"])

    d1._sort(descend=True, inplace=True)
    assert d1.data[0] == 4000
    assert_array_equal(d1.labels[0], ["a", "bc"])

    d1 = d1._sort(by="label[1]", descend=True)
    assert np.all(d1.labels[0] == ["i", "ja"])

    # other way
    d2 = d1._sort(by="label", pos=1, descend=True)
    assert np.all(d2.labels[0] == d1.labels[0])

    d3 = d1.copy()
    d3._labels = None
    d3._sort(by="label", pos=1,
             descend=True)  # no label! generate a warning but no error
Example #6
0
def test_ndarray_slice_labels():
    # slicing only-label array

    d0 = NDArray(labels='a b c d e f g h i j'.split(), title='labelled')
    assert d0.is_labeled

    assert d0.ndim == 1
    assert d0.shape == (10, )
    assert d0[1].labels == ['b']
    assert d0[1].values == 'b'
    assert d0['b'].values == 'b'
    assert d0['c':'d'].shape == (2, )
    assert_array_equal(d0['c':'d'].values, np.array(['c', 'd']))
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"
Example #8
0
def test_nddataset_issue_29_mulitlabels():
    DS = scp.NDDataset(np.random.rand(3, 4))
    with pytest.raises(ValueError):
        # shape data and label mismatch
        DS.set_coordset(DS.y, scp.Coord(title='xaxis', units='s', data=[1, 2, 3, 4], labels=['a', 'b', 'c']))
    c = scp.Coord(title='xaxis', units='s', data=[1, 2, 3, 4], labels=['a', 'b', 'c', 'd'])
    DS.set_coordset(x=c)
    c = scp.Coord(title='xaxis', units='s', data=[1, 2, 3, 4], labels=[['a', 'c', 'b', 'd'], ['e', 'f', 'g', 'h']])
    d = DS.y
    DS.set_coordset(d, c)
    DS.x.labels = ['alpha', 'beta', 'omega', 'gamma']
    assert DS.x.labels.shape == (4, 3)
    # sort
    DS1 = DS.sort(axis=1, by='value', descend=True)
    assert_array_equal(DS1.x, [4, 3, 2, 1])
    # sort
    assert DS.dims == ['y', 'x']
    DS1 = DS.sort(dim='x', by='label', descend=False)
    assert_array_equal(DS1.x, [1, 3, 2, 4])
    DS1 = DS.sort(dim='x', by='label', pos=2, descend=False)
    assert_array_equal(DS1.x, [1, 2, 4, 3])
    DS.sort(dim='y')
    DS.y.labels = ['alpha', 'omega', 'gamma']
    DS2 = DS.sort(dim='y')
    assert_array_equal(DS2.y.labels, ['alpha', 'gamma', 'omega'])
Example #9
0
def test_nddataset_fancy_indexing():
    # numpy vs dataset
    rand = np.random.RandomState(42)
    x = rand.randint(100, size=10)

    # single value indexing

    dx = NDDataset(x)
    assert (dx[3].data, dx[7].data, dx[2].data) == (x[3], x[7], x[2])

    # slice indexing

    assert_array_equal(dx[3:7].data, x[3:7])

    # boolean indexingassert

    assert_array_equal(dx[x > 52].data, x[x > 52])

    # fancy indexing
    ind = [3, 7, 4]

    assert_array_equal(dx[ind].data, x[ind])

    ind = np.array([[3, 7], [4, 5]])

    assert_array_equal(dx[ind].data, x[ind])

    with RandomSeedContext(1234):
        a = np.random.random((3, 5)).round(1)
    c = (np.arange(3), np.arange(5))
    nd = NDDataset(a, coordset=c)

    a = nd[[1, 0, 2]]

    a = nd[np.array([1, 0])]
Example #10
0
def test_ndcomplex_swapdims_quaternion():
    np.random.seed(12345)
    d = np.random.random((4, 3)) * np.exp(.1j)

    d3 = NDComplexArray(d, units=ur.Hz, mask=[[False, True, False], [True, False, False]],
                        dtype=typequaternion)  # quaternion with units & mask

    assert d3.shape == (2, 3)
    assert d3._data.shape == (2, 3)
    assert d3.has_complex_dims
    assert d3.is_quaternion

    w, x, y, z = as_float_array(d3.data).T

    d4 = d3.swapdims(0, 1)

    assert d4.shape == (3, 2)
    assert d4._data.shape == (3, 2)
    assert d4.has_complex_dims
    assert d4.is_quaternion

    wt, yt, xt, zt = as_float_array(d4.data).T
    assert_array_equal(xt, x.T)
    assert_array_equal(yt, y.T)
    assert_array_equal(zt, z.T)
    assert_array_equal(wt, w.T)
Example #11
0
def test_nddataset_add_with_masks():
    # numpy masked arrays mask the result of binary operations if the
    # mask of either operand is set.
    # Does NDData?
    ndd1 = NDDataset(np.array([1, 2]))
    ndd2 = NDDataset(np.array([2, 1]))
    result = ndd1 + ndd2
    assert_array_equal(result.data, np.array([3, 3]))

    ndd1 = NDDataset(np.array([1, 2]), mask=np.array([True, False]))
    other_mask = ~ndd1.mask
    ndd2 = NDDataset(np.array([2, 1]), mask=other_mask)
    result = ndd1 + ndd2
    # The result should have all entries masked...
    assert result.mask.all()
Example #12
0
def test_nddataset_quaternion():
    na0 = np.array([[1., 2., 2., 0., 0., 0.], [1.3, 2., 2., 0.5, 1., 1.], [1, 4.2, 2., 3., 2., 2.],
                    [5., 4.2, 2., 3., 3., 3.]])
    nd = scp.NDDataset(na0)
    assert nd.shape == (4, 6)
    nd.dims = ['v', 'u']
    nd.set_coordset(v=np.linspace(-1, 1, 4), u=np.linspace(-10., 10., 6))
    nd.set_quaternion()
    # test swapdims
    nds = nd.swapdims(0, 1)
    assert_array_equal(nd.data.T, nds.data)
    assert nd.coordset[0] == nds.coordset[0]  # we do not swap the coords
    # test transpose
    nds = nd.T
    assert_array_equal(nd.data.T, nds.data)
    assert nd.coordset[0] == nds.coordset[0]
Example #13
0
def test_ndarray_sort():
    # labels and sort

    d0 = NDArray(np.linspace(4000, 1000, 10),
                 labels='a b c d e f g h i j'.split(),
                 units='s',
                 mask=False,
                 title='wavelength')

    assert d0.is_labeled

    d1 = d0._sort()
    assert (d1.data[0] == 1000)

    # check inplace
    d2 = d0._sort(inplace=True)
    assert (d0.data[0] == 1000)
    assert d2 is d0

    # check descend
    d0._sort(descend=True, inplace=True)
    assert (d0.data[0] == 4000)

    # check sort using label
    d3 = d0._sort(by='label', descend=True)
    assert (d3.labels[0] == 'j')

    # multilabels
    # add a row of labels to d0
    d0.labels = 'bc cd de ef ab fg gh hi ja ij '.split()

    d1 = d0._sort()
    assert (d1.data[0] == 1000)
    assert_array_equal(d1.labels[0], ['j', 'ij'])

    d1._sort(descend=True, inplace=True)
    assert (d1.data[0] == 4000)
    assert_array_equal(d1.labels[0], ['a', 'bc'])

    d1 = d1._sort(by='label[1]', descend=True)
    assert np.all(d1.labels[0] == ['i', 'ja'])

    # other way
    d2 = d1._sort(by='label', pos=1, descend=True)
    assert np.all(d2.labels[0] == d1.labels[0])
Example #14
0
def test_ndio_generic(NMR_dataset_1D):
    nmr = NMR_dataset_1D
    assert nmr.directory == nmrdatadir

    # save with the default filename or open a dialog if it doesn't exists
    # ------------------------------------------------------------------------
    # save with the default name (equivalent to save_as in this case)
    # as this file (IR_1D.scp)  doesn't yet exist a confirmation dialog is opened
    f = nmr.save()
    assert nmr.filename == f.name
    assert nmr.directory == nmrdatadir

    # load back this  file : the full path f is given so no dialog is opened
    nd = NDDataset.load(f)
    assert_dataset_equal(nd, nmr)

    # as it has been already saved, we should not get dialogs
    f = nd.save()
    assert nd.filename == "NMR_1D.scp"
    # return

    # now it opens a dialog and the name can be changed
    f1 = nmr.save_as()
    assert nmr.filename == f1.name

    # remove these files
    f.unlink()
    f1.unlink()

    # save in a specified directory
    nmr.save_as(irdatadir / "essai")  # save essai.scp
    assert nmr.directory == irdatadir
    assert nmr.filename == "essai.scp"
    (irdatadir / nmr.filename).unlink()

    # save in the current directory
    f = nmr.save_as(cwd / "essai")

    # try to load without extension specification (will first assume it is scp)
    dl = NDDataset.load("essai")
    # assert dl.directory == cwd
    assert_array_equal(dl.data, nmr.data)
    f.unlink()
Example #15
0
def test_nddataset_masked_array_input():
    a = np.random.randn(100)
    marr = np.ma.masked_where(a > 0, a)
    nd = scp.NDDataset(marr)
    # check that masks and data match
    assert_array_equal(nd.mask, marr.mask)
    assert_array_equal(nd.data, marr.data)
    # check that they are both by reference
    marr.mask[10] = ~marr.mask[10]
    marr.data[11] = 123456789
    assert_array_equal(nd.mask, marr.mask)
    assert_array_equal(nd.data, marr.data)
def test_coord_slicing():
    # slicing by index

    coord0 = Coord(data=np.linspace(4000, 1000, 10),
                   mask=None,
                   title="wavelength")

    assert coord0[0] == 4000.0

    coord1 = Coord(data=np.linspace(4000, 1000, 10),
                   units="cm^-1",
                   mask=None,
                   title="wavelength")
    c1 = coord1[0]
    assert isinstance(c1.values, Quantity)
    assert coord1[0].values == 4000.0 * (1.0 / ur.cm)

    # slicing with labels

    labs = list("abcdefghij")

    coord0 = Coord(
        data=np.linspace(4000, 1000, 10),
        labels=labs,
        units="cm^-1",
        mask=None,
        title="wavelength",
    )

    assert coord0[0].values == 4000.0 * (1.0 / ur.cm)
    assert isinstance(coord0[0].values, Quantity)

    assert coord0[2] == coord0["c"]
    assert coord0["c":"d"] == coord0[2:4]  # label included

    # slicing only-labels coordinates

    y = list("abcdefghij")
    a = Coord(labels=y, name="x")
    assert a.name == "x"
    assert isinstance(a.labels, np.ndarray)
    assert_array_equal(a.values, a.labels)
Example #17
0
def test_issue417():
    X = scp.read_omnic("irdata/nh4y-activation.spg")
    x = X - X[-1]

    print("--subtract with ds from read_omnic")
    print(f"mean x: {np.mean(x.data)}")
    print(f"var x: {np.var(x.data)}")
    print("")

    f = X.write("X.scp")
    X_r = scp.read("X.scp")
    f.unlink()

    assert_array_equal(X.data, X_r.data)
    assert_dataset_equal(X, X_r)
    assert_equal_units(X.units, X_r.units)
    assert_dataset_equal(X[-1], X_r[-1])

    x_r = X_r - X_r[-1]

    print("--subtract after write/read_scp")
    print(f"mean x_r: {np.mean(x_r.data)}")
    print(f"var x_r: {np.var(x_r.data)}")
    print("")

    x_r2 = X_r - X_r[-1].data
    print("--subtract with data field")
    print(f"mean x_r2: {np.mean(x_r2.data)}")
    print(f"var x_r2: {np.var(x_r2.data)}")

    assert_array_equal(x.data, x_r2.data)
    assert_array_equal(x.data, x_r.data)
    assert_dataset_equal(x, x_r)
Example #18
0
def test_coord_slicing():
    # slicing by index

    coord0 = Coord(data=np.linspace(4000, 1000, 10),
                   mask=None,
                   title='wavelength')

    assert coord0[0] == 4000.0

    coord1 = Coord(data=np.linspace(4000, 1000, 10),
                   units='cm^-1',
                   mask=None,
                   title='wavelength')
    c1 = coord1[0]
    assert isinstance(c1.values, Quantity)
    assert coord1[0].values == 4000.0 * (1. / ur.cm)

    # slicing with labels

    labs = list('abcdefghij')

    coord0 = Coord(data=np.linspace(4000, 1000, 10),
                   labels=labs,
                   units='cm^-1',
                   mask=None,
                   title='wavelength')

    assert coord0[0].values == 4000.0 * (1. / ur.cm)
    assert isinstance(coord0[0].values, Quantity)

    assert coord0[2] == coord0['c']
    assert coord0['c':'d'] == coord0[2:4]  # label included

    # slicing only-labels coordinates

    y = list('abcdefghij')
    a = Coord(labels=y, name='x')
    assert a.name == 'x'
    assert isinstance(a.labels, np.ndarray)
    assert_array_equal(a.values, a.labels)
def test_nddataset_real_imag_quaternion():
    na = np.array(
        [[1.0 + 2.0j, 2.0 + 0j, 1.3 + 2.0j], [2.0 + 0.5j, 1.0 + 4.2j, 2.0 + 3j]]
    )
    nd = scp.NDDataset(na)
    # in the last dimension
    assert_array_equal(nd.real, na.real)
    assert_array_equal(nd.imag, na.imag)
    # in another dimension
    nd.set_quaternion(inplace=True)
    assert nd.is_quaternion
    assert nd.shape == (1, 3)
    na = np.array(
        [
            [1.0 + 2.0j, 2.0 + 0j],
            [1.3 + 2.0j, 2.0 + 0.5j],
            [1.0 + 4.2j, 2.0 + 3j],
            [5.0 + 4.2j, 2.0 + 3j],
        ]
    )
    nd = scp.NDDataset(na)
    nd.set_quaternion(inplace=True)
    assert nd.is_quaternion
    assert_array_equal(nd.real.data, na[::2, :].real)
    nb = np.array(
        [
            [0.0 + 2.0j, 0.0 + 0j],
            [1.3 + 2.0j, 2.0 + 0.5j],
            [0.0 + 4.2j, 0.0 + 3j],
            [5.0 + 4.2j, 2.0 + 3j],
        ]
    )
    ndj = scp.NDDataset(nb, dtype=quaternion)
    assert nd.imag == ndj
Example #20
0
def test_ndarray_copy():
    d0 = NDArray(
        np.linspace(4000, 1000, 10),
        labels="a  b  c  d  e  f  g  h  i  j".split(),
        units="s",
        mask=False,
        title="wavelength",
    )
    d0[5] = MASKED

    d1 = d0.copy()
    assert d1 is not d0
    assert d1 == d0
    assert not (d1 != d0)
    assert d1.units == d0.units
    assert_array_equal(d1.labels, d0.labels)
    assert_array_equal(d1.mask, d0.mask)

    d0 = NDArray(
        np.linspace(4000, 1000, 10),
        labels=[
            "a  b  c  d  e  f  g  h  i  j".split(),
            "bc cd de ef ab fg gh hi ja ij".split(),
        ],
        units="s",
        mask=False,
        title="wavelength",
    )
    d0[5] = MASKED

    d1 = d0.copy()
    assert d1 is not d0
    assert d1 == d0
    assert d1.units == d0.units
    assert_array_equal(d1.labels, d0.labels)
    assert_array_equal(d1.mask, d0.mask)

    d2 = copy(d0)
    assert d2 == d0

    d3 = deepcopy(d0)
    assert d3 == d0
Example #21
0
def test_ndarray_copy():
    d0 = NDArray(np.linspace(4000, 1000, 10),
                 labels='a  b  c  d  e  f  g  h  i  j'.split(),
                 units='s',
                 mask=False,
                 title='wavelength')
    d0[5] = MASKED

    d1 = d0.copy()
    assert d1 is not d0
    assert d1 == d0
    assert d1.units == d0.units
    assert_array_equal(d1.labels, d0.labels)
    assert_array_equal(d1.mask, d0.mask)

    d0 = NDArray(np.linspace(4000, 1000, 10),
                 labels=[
                     'a  b  c  d  e  f  g  h  i  j'.split(),
                     'bc cd de ef ab fg gh hi ja ij'.split()
                 ],
                 units='s',
                 mask=False,
                 title='wavelength')
    d0[5] = MASKED

    d1 = d0.copy()
    assert d1 is not d0
    assert d1 == d0
    assert d1.units == d0.units
    assert_array_equal(d1.labels, d0.labels)
    assert_array_equal(d1.mask, d0.mask)

    d2 = copy(d0)
    assert d2 == d0

    d3 = deepcopy(d0)
    assert d3 == d0
def test_nddataset_issue_29_mulitlabels():
    DS = scp.NDDataset(np.random.rand(3, 4))
    with pytest.raises(ValueError):
        # shape data and label mismatch
        DS.set_coordset(
            DS.y,
            scp.Coord(
                title="xaxis", units="s", data=[1, 2, 3, 4], labels=["a", "b", "c"]
            ),
        )
    c = scp.Coord(
        title="xaxis", units="s", data=[1, 2, 3, 4], labels=["a", "b", "c", "d"]
    )
    DS.set_coordset(x=c)
    c = scp.Coord(
        title="xaxis",
        units="s",
        data=[1, 2, 3, 4],
        labels=[["a", "c", "b", "d"], ["e", "f", "g", "h"]],
    )
    d = DS.y
    DS.set_coordset(d, c)
    DS.x.labels = ["alpha", "beta", "omega", "gamma"]
    assert DS.x.labels.shape == (4, 3)
    # sort
    DS1 = DS.sort(axis=1, by="value", descend=True)
    assert_array_equal(DS1.x, [4, 3, 2, 1])
    # sort
    assert DS.dims == ["y", "x"]
    DS1 = DS.sort(dim="x", by="label", descend=False)
    assert_array_equal(DS1.x, [1, 3, 2, 4])
    DS1 = DS.sort(dim="x", by="label", pos=2, descend=False)
    assert_array_equal(DS1.x, [1, 2, 4, 3])
    DS.sort(dim="y")
    DS.y.labels = ["alpha", "omega", "gamma"]
    DS2 = DS.sort(dim="y")
    assert_array_equal(DS2.y.labels, ["alpha", "gamma", "omega"])
Example #23
0
def test_ndcomplex_init_quaternion_witharray():
    d = np.arange(24).reshape(3, 2, 4)

    d = as_quat_array(d)
    d0 = NDComplexArray(d)

    assert d0.shape == (3, 2)
    assert_array_equal(d0.real.data, [[0, 4], [8, 12], [16, 20]])

    d1 = NDComplexArray(d)
    d1 = d1.set_quaternion()
    assert_array_equal(d1.real.data, [[0, 4], [8, 12], [16, 20]])

    d1 = d0.swapdims(1, 0)
    assert d1.shape == (2, 3)
    assert_array_equal(d1.real.data, [[0, 8, 16], [4, 12, 20]])
    assert d1[0, 0].values == quaternion(0, 2, 1, 3)
def test_coord():
    # simple coords

    a = Coord([1, 2, 3], name="x")
    assert a.id is not None
    assert not a.is_empty
    assert not a.is_masked
    assert_array_equal(a.data, np.array([1, 2, 3]))
    assert not a.is_labeled
    assert a.units is None
    assert a.unitless
    debug_(a.meta)
    assert not a.meta
    assert a.name == "x"

    # set properties

    a.title = "xxxx"
    assert a.title == "xxxx"
    a.name = "y"
    assert a.name == "y"
    a.meta = None
    a.meta = {"val": 125}  # need to be an OrderedDic
    assert a.meta["val"] == 125

    # now with labels

    x = np.arange(10)
    y = list("abcdefghij")
    a = Coord(x, labels=y, title="processors", name="x")
    assert a.title == "processors"
    assert isinstance(a.data, np.ndarray)
    assert isinstance(a.labels, np.ndarray)

    # any kind of object can be a label

    assert a.labels.dtype == "O"
    # even an array
    a._labels[3] = x
    assert a._labels[3][2] == 2

    # coords can be defined only with labels

    y = list("abcdefghij")
    a = Coord(labels=y, title="processors")
    assert a.title == "processors"
    assert isinstance(a.labels, np.ndarray)
    assert_array_equal(a.values, a.labels)
    # any kind of object can be a label
    assert a.labels.dtype == "O"
    # even an array
    a._labels[3] = range(10)
    assert a._labels[3][2] == 2

    # coords with datetime

    from datetime import datetime

    x = np.arange(10)
    y = [datetime(2017, 6, 2 * (i + 1)) for i in x]

    a = Coord(x, labels=y, title="time")
    assert a.title == "time"
    assert isinstance(a.data, np.ndarray)
    assert isinstance(a.labels, np.ndarray)
    a._sort(by="label", descend=True)

    # but coordinates must be 1D

    with pytest.raises(ValueError):
        # should raise an error as coords must be 1D
        Coord(data=np.ones((2, 10)))

    # unitless coordinates

    coord0 = Coord(
        data=np.linspace(4000, 1000, 10),
        labels=list("abcdefghij"),
        mask=None,
        units=None,
        title="wavelength",
    )
    assert coord0.units is None
    assert coord0.data[0] == 4000.0
    assert repr(coord0) == "Coord: [float64] unitless (size: 10)"

    # dimensionless coordinates

    coord0 = Coord(
        data=np.linspace(4000, 1000, 10),
        labels=list("abcdefghij"),
        mask=None,
        units=ur.dimensionless,
        title="wavelength",
    )
    assert coord0.units.dimensionless
    assert coord0.units.scaling == 1.0
    assert coord0.data[0] == 4000.0
    assert repr(coord0) == "Coord: [float64] dimensionless (size: 10)"

    # scaled dimensionless coordinates

    coord0 = Coord(
        data=np.linspace(4000, 1000, 10),
        labels=list("abcdefghij"),
        mask=None,
        units="m/km",
        title="wavelength",
    )
    assert coord0.units.dimensionless
    assert (coord0.data[0] == 4000.0
            )  # <- displayed data to be multiplied by the scale factor
    assert repr(
        coord0) == "Coord: [float64] scaled-dimensionless (0.001) (size: 10)"

    coord0 = Coord(
        data=np.linspace(4000, 1000, 10),
        labels=list("abcdefghij"),
        mask=None,
        units=ur.m / ur.km,
        title="wavelength",
    )

    assert coord0.units.dimensionless
    assert (coord0.data[0] == 4000.0
            )  # <- displayed data to be multiplied by the scale factor
    assert repr(
        coord0) == "Coord: [float64] scaled-dimensionless (0.001) (size: 10)"

    coord0 = Coord(
        data=np.linspace(4000, 1000, 10),
        labels=list("abcdefghij"),
        mask=None,
        units="m^2/s",
        title="wavelength",
    )
    assert not coord0.units.dimensionless
    assert coord0.units.scaling == 1.0
    assert coord0.data[0] == 4000.0
    assert repr(coord0) == "Coord: [float64] m².s⁻¹ (size: 10)"

    # comparison

    coord0 = Coord(
        data=np.linspace(4000, 1000, 10),
        labels=list("abcdefghij"),
        mask=None,
        title="wavelength",
    )
    coord0b = Coord(
        data=np.linspace(4000, 1000, 10),
        labels="a b c d e f g h i j".split(),
        mask=None,
        title="wavelength",
    )
    coord1 = Coord(
        data=np.linspace(4000, 1000, 10),
        labels="a b c d e f g h i j".split(),
        mask=None,
        title="titi",
    )
    coord2 = Coord(
        data=np.linspace(4000, 1000, 10),
        labels="b c d e f g h i j a".split(),
        mask=None,
        title="wavelength",
    )
    coord3 = Coord(data=np.linspace(4000, 1000, 10),
                   labels=None,
                   mask=None,
                   title="wavelength")

    assert coord0 == coord0b
    assert coord0 != coord1  # different title
    assert coord0 != coord2  # different labels
    assert coord0 != coord3  # one coord has no label

    # init from another coord

    coord0 = Coord(
        data=np.linspace(4000, 1000, 10),
        labels=list("abcdefghij"),
        units="s",
        mask=None,
        title="wavelength",
    )

    coord1 = Coord(coord0)
    assert coord1._data is coord0._data
    coord1 = Coord(coord0, copy=True)
    assert coord1._data is not coord0._data
    assert_array_equal(coord1._data, coord0._data)
    assert isinstance(coord0, Coord)
    assert isinstance(coord1, Coord)

    # sort

    coord0 = Coord(
        data=np.linspace(4000, 1000, 10),
        labels=list("abcdefghij"),
        units="s",
        mask=None,
        title="wavelength",
    )
    assert coord0.is_labeled
    ax = coord0._sort()
    assert ax.data[0] == 1000
    coord0._sort(descend=True, inplace=True)
    assert coord0.data[0] == 4000
    ax1 = coord0._sort(by="label", descend=True)
    assert ax1.labels[0] == "j"

    # copy

    coord0 = Coord(
        data=np.linspace(4000, 1000, 10),
        labels=list("abcdefghij"),
        units="s",
        mask=None,
        title="wavelength",
    )

    coord1 = coord0.copy()
    assert coord1 is not coord0

    assert_array_equal(coord1.data, coord0.data)
    assert_array_equal(coord1.labels, coord0.labels)
    assert coord1.units == coord0.units

    coord2 = copy(coord0)
    assert coord2 is not coord0

    assert_array_equal(coord2.data, coord0.data)
    assert_array_equal(coord2.labels, coord0.labels)
    assert coord2.units == coord0.units

    # automatic reversing for wavenumbers

    coord0 = Coord(data=np.linspace(4000, 1000, 10),
                   units="cm^-1",
                   mask=None,
                   title="wavenumbers")
    assert coord0.reversed

    assert not coord0.is_complex
    assert not coord0.is_empty
    assert coord0.T == coord0
    assert_array_equal(coord0.masked_data, coord0.data)
Example #25
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.0 + 1.0j

    # 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.0:890.0] = 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.0:890.0] = 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)

    nd2m2 = nd2.max("x")  # axis selected
    nd2m2.plot()

    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)

    nd2m2 = nd2.min("x")  # axis selected
    nd2m2.plot()

    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.0
    assert nd3.units == nd2.units
    nd3c = nd3.clip(-0.5, 1.0)
    assert nd3c.max().m == 1.0
    assert nd3c.min().m == -0.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.0, 2.0 + 1j, 3.0])
    val = np.abs(ndd)

    val = ndd[1] * 1.2 - 10.0
    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.0 / 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.0 / 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.shape == ()
    assert m == Quantity(np.mean(nd.data), "absorbance")

    m = scp.average(nd)
    assert m.shape == ()
    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⁻¹ (size: 5549)"

    # ----
    nd2 = NDDataset([[0, 1, 2], [3, 4, 5]])  # no coord (check issues

    m = scp.mean(nd2)

    assert m.shape == ()
    assert m == np.mean(nd2.data)
    assert m == 2.5

    m = scp.mean(nd2, keepdims=True)
    assert m.shape == (1, 1)
    assert m.data == [[2.5]]

    m = scp.mean(nd2, dim="y")
    assert m.shape == (3, )
    assert_array_equal(m.data, [1.5, 2.5, 3.5])
    assert str(m) == "NDDataset: [float64] unitless (size: 3)"

    m = scp.mean(nd2, dim=0, keepdims=True)
    assert m.shape == (1, 3)
    assert_array_equal(m.data, [[1.5, 2.5, 3.5]])
    assert str(m) == "NDDataset: [float64] unitless (shape: (y:1, x:3))"

    m = nd2.mean(dim="y")
    assert m.shape == (3, )
    assert_array_equal(m.data, [1.5, 2.5, 3.5])
    assert str(m) == "NDDataset: [float64] unitless (size: 3)"
Example #26
0
def test_ndarray_init(refarray, refmask, ndarray, ndarraymask):
    # initialisation with null array

    d0 = NDArray(description='testing ndarray')

    assert d0.implements('NDArray')
    assert d0.implements() == 'NDArray'

    assert isinstance(d0, NDArray)

    assert d0.is_empty
    assert d0.shape == ()
    assert d0.id.startswith('NDArray')
    assert d0.name == d0.id
    assert d0.title == '<untitled>'
    assert d0.ndim == 0
    assert d0.size is None
    assert not d0.is_masked
    assert d0.dtype is None
    assert d0.unitless
    assert not d0.dims
    assert not d0.meta
    assert hash(d0) is not None
    assert (repr(d0) == 'NDArray: empty (size: 0)')

    # assignement to basic write allowed properties

    d0.data = [1, 2, 3]  # put some data
    assert_array_equal(d0.data, np.array([1, 2, 3]))
    assert d0.dtype in TYPE_INTEGER
    assert d0.date.date() == datetime.now(timezone.utc).date()
    d0.date = datetime(2005, 10, 12)
    d0.date = "25/12/2025"
    assert d0.date == datetime(2025, 12, 25, 0, 0)
    d0.name = 'xxxx'
    assert d0.name == 'xxxx'
    d0.title = 'yyyy'
    assert d0.title == "yyyy"
    d0.meta = []
    d0.meta.something = "a_value"
    assert d0.meta.something == "a_value"
    assert d0[1].value == 2  # only a single element so we get a squeezed array
    d0.units = 'absorbance'
    assert d0.units == ur.absorbance
    assert d0[2] == 3 * ur.absorbance
    assert d0.dims == ['x']

    # initialisation with a scalar quantity

    d1 = NDArray(25)
    assert d1.data == np.array(25)
    assert d1.data.dtype in TYPE_INTEGER

    d1 = NDArray(13. * ur.tesla)
    assert d1.data == np.array(13.)
    assert d1.data.dtype in TYPE_FLOAT
    assert d1.shape == ()
    assert d1.ndim == 0
    assert not d1.dims
    assert d1.units == 'tesla'
    assert d1.values == 13. * ur.tesla

    # initialisation with a 1D array  quantity

    d2 = NDArray([13.] * ur.tesla)
    assert d2.data == np.array([13.])
    assert d2.shape == (1, )
    assert d2.ndim == 1
    assert d2.dims == ['x']
    assert d2.units == 'tesla'
    assert d2.values == 13. * ur.tesla

    # initialisation with a 1D vector quantity

    d3 = NDArray([[13., 20.]] * ur.tesla)
    assert_array_equal(d3.data, np.array([[13., 20.]]))
    assert d3.shape == (1, 2)
    assert d3.ndim == 2
    assert d3.dims == ['y', 'x']
    assert d3.units == 'tesla'

    # initialisation with a sequence

    d4 = NDArray((2, 3, 4))
    assert d4.shape == (3, )
    assert d4.size == 3
    assert d4.dims == ['x']
    assert not d4.is_masked

    # initialization with an array

    d5 = NDArray(refarray)
    assert d5.shape == refarray.shape
    assert d5.size == refarray.size
    assert not d5.is_masked

    # initialization with an NDArray object

    d6 = NDArray(ndarraymask)
    assert d6.title == '<untitled>'
    assert d6.shape == refarray.shape
    assert d6.dims == ['y', 'x']
    assert d6.size == refarray.size
    assert_array_equal(d6.data, refarray)
    assert d6._data is ndarraymask._data  # by default we do not copy
    # d6.data and ndarraym ask.data are however different due to the addition of un offset
    assert d6.is_masked
    assert_array_equal(d6.mask, refmask)
    assert d6.mask is ndarraymask.mask  # no copy by default

    # initialization with an NDArray object with copy
    d7 = NDArray(ndarraymask, copy=True)
    assert_array_equal(d7.data, refarray)
    assert d7.data is not ndarraymask.data  # by default we do not copy
    assert_array_equal(d7.mask, refmask)
    assert d7.mask is not ndarraymask.mask  # no copy by default

    # initialisation with a sequence and a mask

    d0mask = NDArray([2, 3, 4, 5], mask=[1, 0, 0, 0], dtype='int64')
    assert d0mask.shape == (4, )
    assert d0mask.is_masked
    assert d0mask.mask.shape == d0mask.shape

    # initialisation with a sequence and a mask

    d1mask = NDArray([2., 3., 4., 5.1], mask=[1, 0, 0, 0])
    assert d1mask.shape == (4, )
    assert d1mask.is_masked
    assert d1mask.mask.shape == d1mask.shape

    # dtype specified

    d8 = NDArray(ndarraymask, desc='with mask', dtype=np.int64)
    assert d8.shape == refarray.shape
    assert d8.data.dtype == np.int64
    assert d8.dims == ['y', 'x']
    assert d8.title == '<untitled>'
    assert d8.description == 'with mask'
    assert d8.desc == d8.description
    assert len(ndarraymask.history) == 1  # one line already in
    assert len(d8.history) == 2  # copy added

    # intialisation with only labels

    d9 = NDArray(labels='a b c d e f g h i j'.split(), title='labeled')
    assert d9.is_labeled

    # changing dims name
    d11 = NDArray(labels='a b c d e f g h i j'.split(),
                  title='labeled',
                  dims=['q'],
                  author='Blake',
                  history='Created from scratch')
    assert d11.dims == ['q']
    assert d11.author == 'Blake'

    assert '[  a   b ...   i   j]' in d11._repr_html_()  # comparison
Example #27
0
def test_coord():
    # simple coords

    a = Coord([1, 2, 3], name='x')
    assert a.id is not None
    assert not a.is_empty
    assert not a.is_masked
    assert_array_equal(a.data, np.array([1, 2, 3]))
    assert not a.is_labeled
    assert a.units is None
    assert a.unitless
    debug_(a.meta)
    assert not a.meta
    assert a.name == 'x'

    # set properties

    a.title = 'xxxx'
    assert a.title == 'xxxx'
    a.name = 'y'
    assert a.name == 'y'
    a.meta = None
    a.meta = {'val': 125}  # need to be an OrderedDic
    assert a.meta['val'] == 125

    # now with labels

    x = np.arange(10)
    y = list('abcdefghij')
    a = Coord(x, labels=y, title='processors', name='x')
    assert a.title == 'processors'
    assert isinstance(a.data, np.ndarray)
    assert isinstance(a.labels, np.ndarray)

    # any kind of object can be a label

    assert a.labels.dtype == 'O'
    # even an array
    a._labels[3] = x
    assert a._labels[3][2] == 2

    # coords can be defined only with labels

    y = list('abcdefghij')
    a = Coord(labels=y, title='processors')
    assert a.title == 'processors'
    assert isinstance(a.labels, np.ndarray)
    assert_array_equal(a.values, a.labels)
    # any kind of object can be a label
    assert a.labels.dtype == 'O'
    # even an array
    a._labels[3] = range(10)
    assert a._labels[3][2] == 2

    # coords with datetime

    from datetime import datetime
    x = np.arange(10)
    y = [datetime(2017, 6, 2 * (i + 1)) for i in x]

    a = Coord(x, labels=y, title='time')
    assert a.title == 'time'
    assert isinstance(a.data, np.ndarray)
    assert isinstance(a.labels, np.ndarray)
    a._sort(by='label', descend=True)

    # but coordinates must be 1D

    with pytest.raises(ValueError):
        # should raise an error as coords must be 1D
        Coord(data=np.ones((2, 10)))

    # unitless coordinates

    coord0 = Coord(data=np.linspace(4000, 1000, 10),
                   labels=list('abcdefghij'),
                   mask=None,
                   units=None,
                   title='wavelength')
    assert coord0.units is None
    assert coord0.data[0] == 4000.
    assert repr(coord0) == 'Coord: [float64] unitless (size: 10)'

    # dimensionless coordinates

    coord0 = Coord(data=np.linspace(4000, 1000, 10),
                   labels=list('abcdefghij'),
                   mask=None,
                   units=ur.dimensionless,
                   title='wavelength')
    assert coord0.units.dimensionless
    assert coord0.units.scaling == 1.
    assert coord0.data[0] == 4000.
    assert repr(coord0) == 'Coord: [float64]  (size: 10)'

    # scaled dimensionless coordinates

    coord0 = Coord(data=np.linspace(4000, 1000, 10),
                   labels=list('abcdefghij'),
                   mask=None,
                   units='m/km',
                   title='wavelength')
    assert coord0.units.dimensionless
    assert coord0.data[
        0] == 4000.  # <- displayed data to be multiplied by the scale factor
    assert repr(
        coord0) == 'Coord: [float64] scaled-dimensionless (0.001) (size: 10)'

    coord0 = Coord(data=np.linspace(4000, 1000, 10),
                   labels=list('abcdefghij'),
                   mask=None,
                   units=ur.m / ur.km,
                   title='wavelength')

    assert coord0.units.dimensionless
    assert coord0.data[
        0] == 4000.  # <- displayed data to be multiplied by the scale factor
    assert repr(
        coord0) == 'Coord: [float64] scaled-dimensionless (0.001) (size: 10)'

    coord0 = Coord(data=np.linspace(4000, 1000, 10),
                   labels=list('abcdefghij'),
                   mask=None,
                   units="m^2/s",
                   title='wavelength')
    assert not coord0.units.dimensionless
    assert coord0.units.scaling == 1.
    assert coord0.data[0] == 4000.
    assert repr(coord0) == 'Coord: [float64] m^2.s^-1 (size: 10)'

    # comparison

    coord0 = Coord(data=np.linspace(4000, 1000, 10),
                   labels=list('abcdefghij'),
                   mask=None,
                   title='wavelength')
    coord0b = Coord(data=np.linspace(4000, 1000, 10),
                    labels='a b c d e f g h i j'.split(),
                    mask=None,
                    title='wavelength')
    coord1 = Coord(data=np.linspace(4000, 1000, 10),
                   labels='a b c d e f g h i j'.split(),
                   mask=None,
                   title='titi')
    coord2 = Coord(data=np.linspace(4000, 1000, 10),
                   labels='b c d e f g h i j a'.split(),
                   mask=None,
                   title='wavelength')
    coord3 = Coord(data=np.linspace(4000, 1000, 10),
                   labels=None,
                   mask=None,
                   title='wavelength')

    assert coord0 == coord0b
    assert coord0 != coord1  # different title
    assert coord0 != coord2  # different labels
    assert coord0 != coord3  # one coord has no label

    # init from another coord

    coord0 = Coord(data=np.linspace(4000, 1000, 10),
                   labels=list('abcdefghij'),
                   units='s',
                   mask=None,
                   title='wavelength')

    coord1 = Coord(coord0)
    assert coord1._data is coord0._data
    coord1 = Coord(coord0, copy=True)
    assert coord1._data is not coord0._data
    assert_array_equal(coord1._data, coord0._data)
    assert isinstance(coord0, Coord)
    assert isinstance(coord1, Coord)

    # sort

    coord0 = Coord(data=np.linspace(4000, 1000, 10),
                   labels=list('abcdefghij'),
                   units='s',
                   mask=None,
                   title='wavelength')
    assert coord0.is_labeled
    ax = coord0._sort()
    assert (ax.data[0] == 1000)
    coord0._sort(descend=True, inplace=True)
    assert (coord0.data[0] == 4000)
    ax1 = coord0._sort(by='label', descend=True)
    assert (ax1.labels[0] == 'j')

    # copy

    coord0 = Coord(data=np.linspace(4000, 1000, 10),
                   labels=list('abcdefghij'),
                   units='s',
                   mask=None,
                   title='wavelength')

    coord1 = coord0.copy()
    assert coord1 is not coord0

    assert_array_equal(coord1.data, coord0.data)
    assert_array_equal(coord1.labels, coord0.labels)
    assert coord1.units == coord0.units

    coord2 = copy(coord0)
    assert coord2 is not coord0

    assert_array_equal(coord2.data, coord0.data)
    assert_array_equal(coord2.labels, coord0.labels)
    assert coord2.units == coord0.units

    # automatic reversing for wavenumbers

    coord0 = Coord(data=np.linspace(4000, 1000, 10),
                   units='cm^-1',
                   mask=None,
                   title='wavenumbers')
    assert coord0.reversed
Example #28
0
def test_ndarray_methods(refarray, ndarray, ndarrayunit):
    ref = refarray
    nd = ndarray.copy()
    assert nd.data.size == ref.size
    assert nd.shape == ref.shape
    assert nd.size == ref.size
    assert nd.ndim == 2
    assert nd.data[1, 1] == ref[1, 1]
    assert nd.dims == ['y', 'x']
    assert nd.unitless  # no units
    assert not nd.dimensionless  # no unit so dimensionless has no sense

    with catch_warnings() as w:
        # try to change to an array with units
        nd.to('m')  # should not change anything (but raise a warning)
        assert w[-1].category == SpectroChemPyWarning

    assert nd.unitless

    nd.units = 'm'
    assert nd.units == ur.meter

    nd1 = nd.to('km')
    assert nd.units != ur.kilometer  # not inplace
    assert nd1.units == ur.kilometer
    nd.ito('m')
    assert nd.units == ur.meter

    # change of units - ok if it can be casted to the current one

    nd.units = 'cm'

    # cannot change to incompatible units

    with pytest.raises(TypeError):
        nd.units = 'radian'

    # we can force them

    nd.ito('radian', force=True)

    # check dimensionless and scaling

    assert 1 * nd.units == 1. * ur.dimensionless
    assert nd.units.dimensionless
    assert nd.dimensionless
    with raises(DimensionalityError):
        nd1 = nd1.ito('km/s')  # should raise an error
    nd.units = 'm/km'
    assert nd.units.dimensionless
    assert nd.units.scaling == 0.001
    nd.to(1 * ur.m, force=True)
    assert nd.dims == ['y', 'x']

    # check units compatibility

    nd.ito('m', force=True)
    nd2 = ndarray.copy()
    assert nd2.dims == ['y', 'x']
    nd2.units = 'km'
    assert nd.is_units_compatible(nd2)
    nd2.ito('radian', force=True)
    assert not nd.is_units_compatible(nd2)

    # check masking

    assert not nd.is_masked
    repr(nd)
    assert repr(nd).startswith('NDArray: ')
    nd[0] = MASKED
    assert nd.is_masked
    assert nd.dims == ['y', 'x']

    # check len and size

    assert len(nd) == ref.shape[0]
    assert nd.shape == ref.shape
    assert nd.size == ref.size
    assert nd.ndim == 2
    assert nd.dims == ['y', 'x']

    # a vector is a 1st rank tensor. Internally (it will always be represented
    # as a 1D matrix.

    v = NDArray([[1., 2., 3.]])
    assert v.ndim == 2
    assert v.shape == (1, 3)
    assert v.dims == ['y', 'x']
    assert_array_equal(v.data, np.array([[1., 2., 3.]]))

    vt = v.transpose()
    assert vt.shape == (3, 1)
    assert vt.dims == ['x', 'y']
    assert_array_equal(vt.data, np.array([[1.], [2.], [3.]]))

    # test repr

    nd = ndarrayunit.copy()
    h, w = ref.shape
    assert nd.__repr__(
    ) == f"NDArray: [float64] m.s^-1 (shape: (y:{h}, x:{w}))"
    nd[1] = MASKED
    assert nd.is_masked

    # test repr_html
    assert '<table style=\'background:transparent\'>' in nd._repr_html_()

    # test iterations

    nd = ndarrayunit.copy()
    nd[1] = MASKED

    # force units to change

    np.random.seed(12345)
    ndd = NDArray(data=np.random.random((3, 3)),
                  mask=[[True, False, False], [False, True, False],
                        [False, False, True]],
                  units='meters')

    with raises(Exception):
        ndd.to('second')
    ndd.to('second', force=True)

    # swapdims

    np.random.seed(12345)
    d = np.random.random((4, 3))
    d3 = NDArray(d,
                 units=ur.Hz,
                 mask=[[False, True, False], [False, True, False],
                       [False, True, False], [True, False,
                                              False]])  # with units & mask
    assert d3.shape == (4, 3)
    assert d3._data.shape == (4, 3)
    assert d3.dims == ['y', 'x']
    d4 = d3.swapdims(0, 1)
    assert d4.dims == ['x', 'y']
    assert d4.shape == (3, 4)
    assert d4._data.shape == (3, 4)

    # test iter
    for i, item in enumerate(ndd):
        assert item == ndd[i]

    ndz = NDArray()
    assert not list(item for item in ndz)

    assert str(ndz) == repr(ndz) == 'NDArray: empty (size: 0)'
Example #29
0
def test_ndarray_slicing(refarray, ndarray):
    ref = refarray
    nd = ndarray.copy()
    assert not nd.is_masked
    assert nd.dims == ['y', 'x']

    # slicing is different in scpy than with numpy. We always return
    # unsqueezed dimensions, except for array of size 1, which are considered as scalar

    nd1 = nd[0, 0]
    assert_equal(nd1.data, nd.data[0:1, 0:1])
    assert nd1 is not nd[0, 0]
    assert nd1.ndim == 2  # array not reduced
    assert nd1.size == 1
    assert nd1.shape == (1, 1)
    assert isinstance(nd1, NDArray)
    assert isinstance(nd1.data, np.ndarray)
    assert isinstance(nd1.values, TYPE_FLOAT)

    nd1b, id = nd.__getitem__((0, 0), return_index=True)
    assert nd1b == nd1

    nd1a = nd[0, 0:2]
    assert_equal(nd1a.data, nd.data[0:1, 0:2])
    assert nd1a is not nd[0, 0:2]
    assert nd1a.ndim == 2
    assert nd1a.size == 2
    assert nd1a.shape == (1, 2)
    assert isinstance(nd1a, NDArray)
    assert nd1a.dims == ['y', 'x']

    # returning none if empty when slicing
    nd1b = nd[11:, 11:]
    assert nd1b is None

    # nd has been changed, restore it before continuing
    nd = ndarray.copy()

    nd2 = nd[7:10]
    assert_equal(nd2.data, nd.data[7:10])
    assert not nd.is_masked

    nd3 = nd2[1]
    assert nd3.shape == (1, ref.shape[1])
    assert nd3.dims == ['y', 'x']

    nd4 = nd2[:, 1]
    assert nd4.shape == (3, 1)
    assert nd4.dims == ['y', 'x']

    # squezzing
    nd5 = nd4.squeeze()
    assert nd5.shape == (3, )
    assert nd5.dims == ['y']

    # set item
    nd[1] = 2.
    assert nd[1, 0] == 2

    # set item mask
    nd[1] = MASKED
    assert nd.is_masked

    # boolean indexing
    nd = ndarray.copy()
    nd[nd.data > 0]

    # fancy indexing
    df = nd.data[[-1, 1]]

    ndf = nd[[-1, 1]]
    assert_array_equal(ndf.data, df)

    ndf = nd[[
        -1, 1
    ], INPLACE]  # TODO: check utility of this (I remember it should be related to setitem)
    assert_array_equal(ndf.data, df)

    # use with selection from other numpy functions
    am = np.argmax(nd.data, axis=1)
    assert_array_equal(am, np.array([7, 3]))
    amm = nd.data[..., am]
    assert_array_equal(nd[..., am].data, amm)
Example #30
0
def test_nddataset_sorting(ds1):  # ds1 is defined in conftest
    dataset = ds1[:3, :3, 0].copy()
    dataset.sort(inplace=True, dim='z')
    labels = np.array(list('abc'))
    assert_array_equal(dataset.coordset['z'].labels, labels)
    # nochange because the  axis is naturally iversed to force it
    # we need to specify descend
    dataset.sort(inplace=True, descend=False, dim='z')  # order value in increasing order
    labels = np.array(list('cba'))
    assert_array_equal(dataset.coordset['z'].labels, labels)
    dataset.sort(inplace=True, dim='z')
    new = dataset.copy()
    new = new.sort(descend=False, inplace=False, dim='z')
    assert_array_equal(new.data, dataset.data[::-1])
    assert (new[0, 0] == dataset[-1, 0])
    assert_array_equal(new.coordset['z'].labels, labels)
    assert_array_equal(new.coordset["z"].data, dataset.coordset['z'].data[::-1])
    # check for another dimension
    dataset = ds1.copy()
    new = ds1.copy()
    new.sort(dim='y', inplace=True, descend=False)
    assert_array_equal(new.data, dataset.data)
    assert (new[0, 0, 0] == dataset[0, 0, 0])
    new = dataset.copy()
    new.sort(dim='y', inplace=True, descend=True)
    assert_array_equal(new.data, dataset.data[:, ::-1, :])
    assert (new[0, -1, 0] == dataset[0, 0, 0])