Beispiel #1
0
def test_nddataset_set_coordinates(nd2d, ds1):
    # set coordinates all together
    nd = nd2d.copy()
    ny, nx = nd.shape
    nd.set_coordset(x=np.arange(nx), y=np.arange(ny))
    assert nd.dims == ['y', 'x']
    assert nd.x == np.arange(nx)
    nd.transpose(inplace=True)
    assert nd.dims == ['x', 'y']
    assert nd.x == np.arange(nx)
    # set coordinates from tuple
    nd = nd2d.copy()
    ny, nx = nd.shape
    nd.set_coordset(np.arange(ny), np.arange(nx))
    assert nd.dims == ['y', 'x']
    assert nd.x == np.arange(nx)
    nd.transpose(inplace=True)
    assert nd.dims == ['x', 'y']
    assert nd.x == np.arange(nx)
    # set coordinate with one set to None: should work!
    # set coordinates from tuple
    nd = nd2d.copy()
    ny, nx = nd.shape
    nd.set_coordset(np.arange(ny), None)
    assert nd.dims == ['y', 'x']
    assert nd.y == np.arange(ny)
    assert nd.x.is_empty
    nd.transpose(inplace=True)
    assert nd.dims == ['x', 'y']
    assert nd.y == np.arange(ny)
    assert nd.x.is_empty
    assert nd.coordset == scp.CoordSet(np.arange(ny), None)
    nd = nd2d.copy()
    ny, nx = nd.shape
    nd.set_coordset(None, np.arange(nx))
    assert nd.dims == ['y', 'x']
    assert nd.x == np.arange(nx)
    assert nd.y.is_empty
    nd.set_coordset(y=np.arange(ny), x=None)
    # set up a single coordinates
    nd = nd2d.copy()
    ny, nx = nd.shape
    nd.x = np.arange(nx)
    nd.x = np.arange(nx)  # do it again - fix  a bug
    nd.set_coordtitles(y='intensity', x='time')
    assert repr(nd.coordset) == 'CoordSet: [x:time, y:intensity]'
    # validation
    with pytest.raises(ValueError):
        nd.x = np.arange(nx + 5)
    with pytest.raises(AttributeError):
        nd.z = None
    # set coordinates all together
    nd = nd2d.copy()
    ny, nx = nd.shape
    nd.coordset = scp.CoordSet(u=np.arange(nx), v=np.arange(ny))
    assert nd.dims != ['u', 'v']  # dims = ['y','x']
    # set dim names
    nd.dims = ['u', 'v']
    nd.set_coordset(**scp.CoordSet(u=np.arange(ny), v=np.arange(nx)))
    assert nd.dims == ['u', 'v']
Beispiel #2
0
def test_nddataset_coordset():
    # init coordinates set at NDDataset initialization
    dx = np.random.random((10, 7, 3))
    coord0 = np.arange(10)
    coord1 = np.arange(7)
    coord2 = np.arange(3) * 100.
    da = scp.NDDataset(dx, coordset=(coord0, coord1, coord2), title='absorbance',
                       coordtitles=['wavelength', 'time-on-stream', 'temperature'], coordunits=['cm^-1', 's', 'K'], )
    assert da.shape == (10, 7, 3)
    assert da.coordset.titles == ['temperature', 'time-on-stream', 'wavelength']
    assert da.coordset.names == ['x', 'y', 'z']
    assert da.coordunits == [ur.Unit('K'), ur.Unit('s'), ur.Unit('cm^-1')]
    # order of dims follow data shape, but not necessarily the coord list (
    # which is ordered by name)
    assert da.dims == ['z', 'y', 'x']
    assert da.coordset.names == sorted(da.dims)
    # transpose
    dat = da.T
    assert dat.dims == ['x', 'y', 'z']
    # dims changed but did not change coords order !
    assert dat.coordset.names == sorted(dat.dims)
    assert dat.coordtitles == da.coordset.titles
    assert dat.coordunits == da.coordset.units

    # too many coordinates
    cadd = scp.Coord(labels=['d%d' % i for i in range(6)])
    coordtitles = ['wavelength', 'time-on-stream', 'temperature']
    coordunits = ['cm^-1', 's', None]
    daa = scp.NDDataset(dx, coordset=[coord0, coord1, coord2, cadd, coord2.copy()], title='absorbance',
                        coordtitles=coordtitles, coordunits=coordunits, )
    assert daa.coordset.titles == coordtitles[::-1]
    assert daa.dims == ['z', 'y', 'x']
    # with a CoordSet
    c0, c1 = scp.Coord(labels=['d%d' % i for i in range(6)]), scp.Coord(data=[1, 2, 3, 4, 5, 6])
    cc = scp.CoordSet(c0, c1)
    cd = scp.CoordSet(x=cc, y=c1)
    ds = scp.NDDataset([1, 2, 3, 6, 8, 0], coordset=cd, units='m')
    assert ds.dims == ['x']
    assert ds.x == cc
    ds.history = 'essai: 1'
    ds.history = 'try:2'
    # wrong type
    with pytest.raises(TypeError):
        ds.coord[1.3]
    # extra coordinates
    with pytest.raises(AttributeError):
        ds.y
    # invalid_length
    coord1 = scp.Coord(np.arange(9), title='wavelengths')  # , units='m')
    coord2 = scp.Coord(np.arange(20), title='time')  # , units='s')
    with pytest.raises(ValueError):
        scp.NDDataset(np.random.random((10, 20)), coordset=(coord1, coord2))
Beispiel #3
0
def test_nddataset_multiple_axis(ref_ds, coord0, coord1, coord2, coord2b, dsm):  # dsm is defined in conftest
    ref = ref_ds
    da = dsm.copy()
    coordm = scp.CoordSet(coord2, coord2b)
    # check indexing
    assert da.shape == ref.shape
    coords = da.coordset
    assert len(coords) == 3
    assert coords['z'] == coord0
    assert da.z == coord0
    assert da.coordset['wavenumber'] == coord0
    assert da.wavenumber == coord0
    assert da['wavenumber'] == coord0
    # for multiple coordinates
    assert da.coordset['x'] == coordm
    assert da['x'] == coordm
    assert da.x == coordm
    # but we can also specify, which axis should be returned explicitely
    # by an index or a label
    assert da.coordset['x_1'] == coord2b
    assert da.coordset['x_2'] == coord2
    assert da.coordset['x'][1] == coord2
    assert da.coordset['x']._1 == coord2b
    assert da.x['_1'] == coord2b
    assert da['x_1'] == coord2b
    assert da.x_1 == coord2b
    x = da.coordset['x']
    assert x['temperature'] == coord2
    assert da.coordset['x']['temperature'] == coord2
    # even simpler we can specify any of the axis title and get it ...
    assert da.coordset['time-on-stream'] == coord1
    assert da.coordset['temperature'] == coord2
    da.coordset['magnetic field'] += 100 * ur.millitesla
    assert da.coordset['magnetic field'] == coord2b + 100 * ur.millitesla
Beispiel #4
0
def test_from_function_docstring():
    def func1(t, v):
        d = v * t
        return d

    time = scp.LinearCoord.arange(0, 60, 10, units="min")
    scp.fromfunction(func1,
                     v=scp.Quantity(134, "km/hour"),
                     coordset=scp.CoordSet(t=time))
def dsm():
    # dataset with coords containing several axis and a mask

    coordmultiple = scp.CoordSet(coord2_, coord2b_)
    return scp.NDDataset(
        ref3d_data,
        coordset=[coord0_, coord1_, coordmultiple],
        mask=ref3d_mask,
        title="absorbance",
        units="absorbance",
    ).copy()
Beispiel #6
0
def test_nddataset_complex_dataset_slicing_by_index():
    na0 = np.array([1. + 2.j, 2., 0., 0., -1.j, 1j] * 4)
    nd = scp.NDDataset(na0)
    assert nd.shape == (24,)
    assert nd.data.shape == (24,)
    coords = (np.linspace(-10., 10., 24),)
    nd.set_coordset(coords)
    x1 = nd.x.copy()
    nd.coordset = coords
    x2 = nd.x.copy()
    assert x1 == x2
    # slicing
    nd1 = nd[0]
    assert nd1.shape == (1,)
    assert nd1.data.shape == (1,)
    # slicing range
    nd2 = nd[1:6]
    assert nd2.shape == (5,)
    assert nd2.data.shape == (5,)
    na0 = na0.reshape(6, 4)
    nd = scp.NDDataset(na0)
    coords = scp.CoordSet(np.linspace(-10., 10., 6), np.linspace(-1., 1., 4))
    nd.set_coordset(**coords)
    assert nd.shape == (6, 4)
    assert nd.data.shape == (6, 4)
    nd.coordset = coords
    # slicing 2D
    nd1 = nd[0]
    assert nd1.shape == (1, 4)
    assert nd1.data.shape == (1, 4)
    # slicing range
    nd1 = nd[1:3]
    assert nd1.shape == (2, 4)
    assert nd1.data.shape == (2, 4)
    # slicing range
    nd1 = nd[1:3, 0:2]
    assert nd1.shape == (2, 2)
    assert nd1.data.shape == (2, 2)
    nd.set_complex()
    assert nd.shape == (6, 4)
    assert nd.data.shape == (6, 4)
Beispiel #7
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)"
Beispiel #8
0

# %%
def func(t, v, var):
    d = v * t + (np.random.rand(len(t)) - 0.5) * var
    d[0].data = 0.0
    return d


time = scp.LinearCoord.linspace(0, 10, 20, title="time", units="hour")
d = scp.NDDataset.fromfunction(
    func,
    v=100.0 * ur("km/hr"),
    var=60.0 * ur("km"),
    # extra arguments passed to the function v, var
    coordset=scp.CoordSet(t=time),
    name="mydataset",
    title="distance travelled",
)

# %% [markdown]
# Here is a plot of these data-points:

# %%
prefs = d.preferences
prefs.figure.figsize = (7, 3)
d.plot_scatter(markersize=7, mfc="red")

# %% [markdown]
# We want to fit a line through these data-points of equation
#
Beispiel #9
0
def test_nddataset_square_dataset_with_identical_coordinates():
    a = np.random.rand(3, 3)
    c = scp.Coord(np.arange(3) * .25, title='time', units='us')
    nd = scp.NDDataset(a, coordset=scp.CoordSet(x=c, y='x'))
    assert nd.x == nd.y
def test_nddataset_square_dataset_with_identical_coordinates():
    a = np.random.rand(3, 3)
    c = scp.Coord(np.arange(3) * 0.25, title="time", units="us")
    nd = scp.NDDataset(a, coordset=scp.CoordSet(x=c, y="x"))
    assert nd.x == nd.y
def test_nddataset_coordset():
    # init coordinates set at NDDataset initialization
    dx = np.random.random((10, 7, 3))
    coord0 = np.arange(10)
    coord1 = np.arange(7)
    coord2 = np.arange(3) * 100.0
    da = scp.NDDataset(
        dx,
        coordset=(coord0, coord1, coord2),
        title="absorbance",
        coordtitles=["wavelength", "time-on-stream", "temperature"],
        coordunits=["cm^-1", "s", "K"],
    )
    assert da.shape == (10, 7, 3)
    assert da.coordset.titles == ["temperature", "time-on-stream", "wavelength"]
    assert da.coordset.names == ["x", "y", "z"]
    assert da.coordunits == [ur.Unit("K"), ur.Unit("s"), ur.Unit("cm^-1")]
    # order of dims follow data shape, but not necessarily the coord list (
    # which is ordered by name)
    assert da.dims == ["z", "y", "x"]
    assert da.coordset.names == sorted(da.dims)
    # transpose
    dat = da.T
    assert dat.dims == ["x", "y", "z"]
    # dims changed but did not change coords order !
    assert dat.coordset.names == sorted(dat.dims)
    assert dat.coordtitles == da.coordset.titles
    assert dat.coordunits == da.coordset.units

    # too many coordinates
    cadd = scp.Coord(labels=["d%d" % i for i in range(6)])
    coordtitles = ["wavelength", "time-on-stream", "temperature"]
    coordunits = ["cm^-1", "s", None]
    daa = scp.NDDataset(
        dx,
        coordset=[coord0, coord1, coord2, cadd, coord2.copy()],
        title="absorbance",
        coordtitles=coordtitles,
        coordunits=coordunits,
    )
    assert daa.coordset.titles == coordtitles[::-1]
    assert daa.dims == ["z", "y", "x"]
    # with a CoordSet
    c0, c1 = scp.Coord(labels=["d%d" % i for i in range(6)]), scp.Coord(
        data=[1, 2, 3, 4, 5, 6]
    )
    cc = scp.CoordSet(c0, c1)
    cd = scp.CoordSet(x=cc, y=c1)
    ds = scp.NDDataset([1, 2, 3, 6, 8, 0], coordset=cd, units="m")
    assert ds.dims == ["x"]
    assert ds.x == cc
    ds.history = "essai: 1"
    ds.history = "try:2"
    # wrong type
    with pytest.raises(TypeError):
        ds.coord[1.3]
    # extra coordinates
    with pytest.raises(AttributeError):
        ds.y
    # invalid_length
    coord1 = scp.Coord(np.arange(9), title="wavelengths")  # , units='m')
    coord2 = scp.Coord(np.arange(20), title="time")  # , units='s')
    with pytest.raises(ValueError):
        scp.NDDataset(np.random.random((10, 20)), coordset=(coord1, coord2))