Esempio n. 1
0
def test_coordset_del(coord0, coord1, coord2):
    coords = CoordSet(coord2, [coord0, coord0.copy()], coord1)

    assert (
        str(coords) == repr(coords) ==
        "CoordSet: [x:time-on-stream, y:[_1:wavenumber, _2:wavenumber], z:temperature]"
    )

    del coords["temperature"]
    assert (str(coords) == repr(coords) ==
            "CoordSet: [x:time-on-stream, y:[_1:wavenumber, _2:wavenumber]]")

    del coords.y["wavenumber"]
    assert (str(coords) == repr(coords) ==
            "CoordSet: [x:time-on-stream, y:[_2:wavenumber]]")

    coords = CoordSet(coord2, [coord0, coord0.copy()], coord1)
    del coords["wavenumber"]
    assert (str(coords) == repr(coords) ==
            "CoordSet: [x:time-on-stream, y:[_2:wavenumber], z:temperature]")

    coords = CoordSet(coord2, [coord0, coord0.copy()], coord1)
    del coords.y_2
    assert (str(coords) == repr(coords) ==
            "CoordSet: [x:time-on-stream, y:[_1:wavenumber], z:temperature]")

    coords = CoordSet(coord2, [coord0, coord0.copy()], coord1)
    del coords.y._1
    assert (str(coords) == repr(coords) ==
            "CoordSet: [x:time-on-stream, y:[_2:wavenumber], z:temperature]")
Esempio n. 2
0
def test_coordset_multicoord_for_a_single_dim():
    # normal coord (single numerical array for a axis)

    coord1 = NDArray(data=np.linspace(1000., 4000., 5),
                     labels='a b c d e'.split(),
                     mask=None,
                     units='cm^1',
                     title='wavelengths')

    coord0 = NDArray(data=np.linspace(20, 500, 5),
                     labels='very low-low-normal-high-very high'.split('-'),
                     mask=None,
                     units='K',
                     title='temperature')

    # pass as a list of coord -> this become a subcoordset
    coordsa = CoordSet([coord1, coord0])
    assert repr(
        coordsa
    ) == 'CoordSet: [x:[_1:wavelengths, _2:temperature]]'  # note the internal coordinates are not sorted
    assert not coordsa.is_same_dim
    assert coordsa.x.is_same_dim

    coordsb = coordsa.x

    # try to pass arguments, each being an coord
    coordsc = CoordSet(coord1, coord0)
    assert not coordsc.is_same_dim
    assert repr(coordsc) == 'CoordSet: [x:temperature, y:wavelengths]'

    # try to pass arguments where each are a coords
    coordsd = CoordSet(coordsa.x, coordsc)
    assert repr(
        coordsd
    ) == "CoordSet: [x:[_1:temperature, _2:wavelengths], y:[_1:wavelengths, _2:temperature]]"

    assert not coordsd.is_same_dim
    assert np.all([item.is_same_dim for item in coordsd])

    coordse = CoordSet(coordsb, coord1)
    assert repr(
        coordse
    ) == "CoordSet: [x:wavelengths, y:[_1:wavelengths, _2:temperature]]"

    assert not coordse.is_same_dim
    assert coordse('y').is_same_dim

    co = coordse('x')
    assert isinstance(co, Coord)

    co = coordse('y')
    assert isinstance(co, CoordSet)
    assert co.name == 'y'
    assert co.names == ['_1', '_2']
    assert co._1 == coord1  # no reordering for the sub-coordset

    co = coordse[-1:]
    assert isinstance(co, CoordSet)
    assert co[0].name == 'y'  # should keep the original name (solved)
    assert co[0]["_1"] == coord1
Esempio n. 3
0
def test_coordset_del(coord0, coord1, coord2):
    coords = CoordSet(coord2, [coord0, coord0.copy()], coord1)

    assert str(coords) == repr(
        coords
    ) == 'CoordSet: [x:time-on-stream, y:[_1:wavenumber, _2:wavenumber], z:temperature]'

    del coords['temperature']
    assert str(coords) == repr(
        coords
    ) == 'CoordSet: [x:time-on-stream, y:[_1:wavenumber, _2:wavenumber]]'

    del coords.y['wavenumber']
    assert str(coords) == repr(
        coords) == 'CoordSet: [x:time-on-stream, y:[_2:wavenumber]]'

    coords = CoordSet(coord2, [coord0, coord0.copy()], coord1)
    del coords['wavenumber']
    assert str(coords) == repr(
        coords
    ) == 'CoordSet: [x:time-on-stream, y:[_2:wavenumber], z:temperature]'

    coords = CoordSet(coord2, [coord0, coord0.copy()], coord1)
    del coords.y_2
    assert str(coords) == repr(
        coords
    ) == 'CoordSet: [x:time-on-stream, y:[_1:wavenumber], z:temperature]'

    coords = CoordSet(coord2, [coord0, coord0.copy()], coord1)
    del coords.y._1
    assert str(coords) == repr(
        coords
    ) == 'CoordSet: [x:time-on-stream, y:[_2:wavenumber], z:temperature]'
Esempio n. 4
0
def test_coordset_sizes(coord0, coord1):
    coords = CoordSet(coord0, coord1)
    assert coords.sizes == [coords.x.size, coords.y.size
                            ] == [coord1.size, coord0.size]

    coords = CoordSet([coord0, coord0.copy()], coord1)
    assert coords.sizes == [coords.x.size, coords.y.size
                            ] == [coord1.size, coord0.size]

    assert coord0.size != coord0[:7].size
    with pytest.raises(ValueError):
        coords = CoordSet([coord0, coord0[:7]], coord1)
Esempio n. 5
0
    def add_coordset(self, *coords, dims=None, **kwargs):
        """
        Add one or a set of coordinates from a dataset.

        Parameters
        ----------
        *coords : iterable
            Coordinates object(s).
        dims : list
            Name of the coordinates.
        **kwargs
            Optional keyword parameters passed to the coordset.
        """
        if not coords and not kwargs:
            # reset coordinates
            self._coordset = None
            return

        if self._coordset is None:
            # make the whole coordset at once
            self._coordset = CoordSet(*coords, dims=dims, **kwargs)
        else:
            # add one coordinate
            self._coordset._append(*coords, **kwargs)

        if self._coordset:
            # set a notifier to the updated traits of the CoordSet instance
            HasTraits.observe(self._coordset, self._dims_update, "_updated")
            # force it one time after this initialization
            self._coordset._updated = True
Esempio n. 6
0
def test_coordset_str_repr(coord0, coord1, coord2):
    coords = CoordSet(coord2, [coord0, coord0.copy()], coord1)

    assert str(coords) == repr(
        coords
    ) == 'CoordSet: [x:time-on-stream, y:[_1:wavenumber, _2:wavenumber], z:temperature]'
    assert repr(coords) == str(coords)
Esempio n. 7
0
def test_ndmath_absolute_of_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 = NDDataset(na0, dtype=quaternion)

    coords = CoordSet(np.linspace(-1, 1, 2), np.linspace(-10., 10., 3))
    assert nd.shape == (2, 3)
    nd.set_coordset(**coords)
    np.abs(nd)
Esempio n. 8
0
def dsm():
    # dataset with coords containing several axis and a mask

    coordmultiple = CoordSet(coord2_, coord2b_)
    return NDDataset(ref3d_data,
                     coordset=[coord0_, coord1_, coordmultiple],
                     mask=ref3d_mask,
                     title='absorbance',
                     units='absorbance').copy()
Esempio n. 9
0
    def __getitem__(self, items, **kwargs):

        saveditems = items

        # coordinate selection to test first
        if isinstance(items, str):
            try:
                return self._coordset[items]
            except Exception:
                pass

        # slicing
        new, items = super().__getitem__(items, return_index=True)

        if new is None:
            return None

        if self._coordset is not None:
            names = self._coordset.names  # all names of the current coordinates
            new_coords = [None] * len(names)
            for i, item in enumerate(items):
                # get the corresponding dimension name in the dims list
                name = self.dims[i]
                # get the corresponding index in the coordinate's names list
                idx = names.index(name)
                if self._coordset[idx].is_empty:
                    new_coords[idx] = Coord(None, name=name)
                elif isinstance(item, slice):
                    # add the slice on the corresponding coordinates on the dim to the new list of coordinates
                    if not isinstance(self._coordset[idx], CoordSet):
                        new_coords[idx] = self._coordset[idx][item]
                    else:
                        # we must slice all internal coordinates
                        newc = []
                        for c in self._coordset[idx]:
                            newc.append(c[item])
                        new_coords[idx] = CoordSet(*newc[::-1], name=name)
                        # we reverse to be sure
                        # the order will be  kept for internal coordinates
                        new_coords[idx]._default = self._coordset[
                            idx]._default  # set the same default coord
                        new_coords[idx]._is_same_dim = self._coordset[
                            idx]._is_same_dim

                elif isinstance(item, (np.ndarray, list)):
                    new_coords[idx] = self._coordset[idx][item]

            new.set_coordset(*new_coords, keepnames=True)

        new.history = f"Slice extracted: ({saveditems})"
        return new
Esempio n. 10
0
    def __setattr__(self, key, value):

        if key in DEFAULT_DIM_NAME:  # syntax such as ds.x, ds.y, etc...
            # Note the above test is important to avoid errors with traitlets
            # even if it looks redundant with the following
            if key in self.dims:
                if self._coordset is None:
                    # we need to create a coordset first
                    self.set_coordset(
                        dict((self.dims[i], None) for i in range(self.ndim)))
                idx = self._coordset.names.index(key)
                _coordset = self._coordset
                listcoord = False
                if isinstance(value, list):
                    listcoord = all(
                        [isinstance(item, Coord) for item in value])
                if listcoord:
                    _coordset[idx] = list(
                        CoordSet(value).to_dict().values())[0]
                    _coordset[idx].name = key
                    _coordset[idx]._is_same_dim = True
                elif isinstance(value, CoordSet):
                    if len(value) > 1:
                        value = CoordSet(value)
                    _coordset[idx] = list(value.to_dict().values())[0]
                    _coordset[idx].name = key
                    _coordset[idx]._is_same_dim = True
                elif isinstance(value, (Coord, LinearCoord)):
                    value.name = key
                    _coordset[idx] = value
                else:
                    _coordset[idx] = Coord(value, name=key)
                _coordset = self._valid_coordset(_coordset)
                self._coordset.set(_coordset)
            else:
                raise AttributeError(f"Coordinate `{key}` is not used.")
        else:
            super().__setattr__(key, value)
Esempio n. 11
0
def test_coordset_copy(coord0, coord1):
    coord2 = LinearCoord.linspace(200.,
                                  300.,
                                  3,
                                  units="K",
                                  title='temperature')

    coordsa = CoordSet(coord0, coord1, coord2)

    coordsb = coordsa.copy()
    assert coordsa == coordsb
    assert coordsa is not coordsb
    assert coordsa(1) == coordsb(1)
    assert coordsa(1).name == coordsb(1).name

    # copy
    coords = CoordSet(coord0, coord0.copy())
    coords1 = coords[:]
    assert coords is not coords1

    import copy
    coords2 = copy.deepcopy(coords)
    assert coords == coords2
Esempio n. 12
0
def test_coordset_get(coord0, coord1, coord2):
    coords = CoordSet(coord2, [coord0, coord0.copy()], coord1)

    coord = coords["temperature"]
    assert str(coord) == "Coord: [float64] K (size: 3)"
    assert coord.name == "z"

    coord = coords["wavenumber"]
    assert coord.name == "_1"

    coord = coords["y_2"]
    assert coord.name == "_2"

    coord = coords["_1"]
    assert coord.name == "_1"
Esempio n. 13
0
def test_coordset_get(coord0, coord1, coord2):
    coords = CoordSet(coord2, [coord0, coord0.copy()], coord1)

    coord = coords['temperature']
    assert str(coord) == 'Coord: [float64] K (size: 3)'
    assert coord.name == 'z'

    coord = coords['wavenumber']
    assert coord.name == '_1'

    coord = coords['y_2']
    assert coord.name == '_2'

    coord = coords['_1']
    assert coord.name == '_1'
Esempio n. 14
0
    def get_conc(self, n_pc=None):
        """
        Computes abstract concentration profile (first in - first out).

        Parameters
        ----------
        n_pc : int, optional, default:3
            Number of pure species for which the concentration profile must be
            computed.

        Returns
        --------
        concentrations
            Concentration profile.
        """
        M, K = self.f_ev.shape
        if n_pc is None:
            n_pc = K
        n_pc = min(K, n_pc)

        f = self.f_ev
        b = self.b_ev

        xcoord = Coord(range(n_pc), title="PS#")
        c = NDDataset(
            np.zeros((M, n_pc)),
            coordset=CoordSet(y=self._X.y, x=xcoord),
            name=f"C_EFA[{self._X.name}]",
            title="relative concentration",
            description="Concentration profile from EFA",
            history=f"{datetime.now(timezone.utc)}: created by spectrochempy",
        )
        if self._X.is_masked:
            masked_rows = np.all(self._X.mask, axis=-1)
        else:
            masked_rows = np.array([False] * M)

        for i in range(M):
            if masked_rows[i]:
                c[i] = MASKED
                continue
            c[i] = np.min((f.data[i, :n_pc], b.data[i, :n_pc][::-1]), axis=0)
        return c
Esempio n. 15
0
def test_coordset_call(coord0, coord1):
    coordsa = CoordSet(coord0, coord1)

    assert str(coordsa) == 'CoordSet: [x:time-on-stream, y:wavenumber]'
    a = coordsa(1, 0)
    assert a == coordsa

    b = coordsa(1)
    assert b == coord0  # reordering

    c = coordsa('x')
    assert c == coord1

    d = coordsa('time-on-stream')
    assert d == coord1

    with pytest.raises(KeyError):
        coordsa('x_a')  # do not exit

    coordsa('y_a')
Esempio n. 16
0
def test_coordset_call(coord0, coord1):
    coordsa = CoordSet(coord0, coord1)

    assert str(coordsa) == "CoordSet: [x:time-on-stream, y:wavenumber]"
    a = coordsa(1, 0)
    assert a == coordsa

    b = coordsa(1)
    assert b == coord0  # reordering

    c = coordsa("x")
    assert c == coord1

    d = coordsa("time-on-stream")
    assert d == coord1

    with pytest.raises(KeyError):
        coordsa("x_a")  # do not exit

    coordsa("y_a")
Esempio n. 17
0
        def item_to_attr(obj, dic):

            for key, val in dic.items():

                try:
                    if "readonly" in dic.keys() and key in [
                            "readonly", "name"
                    ]:
                        # case of the meta and preferences
                        pass

                    elif hasattr(obj, f"_{key}"):
                        # use the hidden attribute if it exists
                        key = f"_{key}"

                    if val is None:
                        pass

                    elif key in ["_meta", "_ranges", "_preferences"]:
                        setattr(obj, key, item_to_attr(getattr(obj, key), val))

                    elif key in ["_coordset"]:
                        _coords = []
                        for v in val["coords"]:
                            if "data" in v:
                                # coords
                                _coords.append(item_to_attr(Coord(), v))
                            elif "coords" in v:
                                # likely a coordset (multicoordinates)
                                if v["is_same_dim"]:
                                    _mcoords = []
                                    for mv in v["coords"]:
                                        if "data" in mv:
                                            # coords
                                            _mcoords.append(
                                                item_to_attr(Coord(), mv))
                                        else:
                                            # likely a linearcoord
                                            _mcoords.append(
                                                item_to_attr(
                                                    LinearCoord(), mv))
                                    cs = CoordSet(*_mcoords[::-1],
                                                  name=v["name"])
                                    _coords.append(cs)
                                else:
                                    raise ValueError(
                                        "Invalid : not a multicoordinate")
                            else:
                                # likely a linearcoord
                                _coords.append(item_to_attr(LinearCoord(), v))

                        coords = dict((c.name, c) for c in _coords)
                        obj.set_coordset(coords)
                        obj._name = val["name"]
                        obj._references = val["references"]

                    elif key in ["_datasets"]:
                        # datasets = [item_to_attr(NDDataset(name=k),
                        # v) for k, v in val.items()]
                        datasets = [
                            item_to_attr(NDDataset(), js) for js in val
                        ]
                        obj.datasets = datasets

                    elif key in ["_projects"]:
                        projects = [item_to_attr(Project(), js) for js in val]
                        obj.projects = projects

                    elif key in ["_scripts"]:
                        scripts = [item_to_attr(Script(), js) for js in val]
                        obj.scripts = scripts

                    elif key in ["_parent"]:
                        # automatically set
                        pass

                    else:
                        if isinstance(val, TYPE_BOOL) and key == "_mask":
                            val = np.bool_(val)
                        if isinstance(obj, NDDataset) and key == "_filename":
                            obj.filename = val  # This is a hack because for some reason fileame attribute is not
                            # found ????
                        else:
                            setattr(obj, key, val)

                except Exception as e:
                    raise TypeError(f"for {key} {e}")

            return obj
Esempio n. 18
0
def test_coordset_set(coord0, coord1, coord2):
    coords = CoordSet(coord2, [coord0, coord0.copy()], coord1)
    assert str(coords) == repr(
        coords
    ) == 'CoordSet: [x:time-on-stream, y:[_1:wavenumber, _2:wavenumber], z:temperature]'

    coords.set_titles('time', 'dddd', 'celcius')
    assert str(
        coords
    ) == 'CoordSet: [x:time, y:[_1:wavenumber, _2:wavenumber], z:celcius]'

    coords.set_titles(x='time', z='celcius', y_1='length')
    assert str(coords) == repr(
        coords
    ) == 'CoordSet: [x:time, y:[_1:length, _2:wavenumber], z:celcius]'

    coords.set_titles('t', ('l', 'g'), x='x')
    assert str(coords) == 'CoordSet: [x:x, y:[_1:l, _2:g], z:celcius]'

    coords.set_titles(('t', ('l', 'g')), z='z')
    assert str(coords) == 'CoordSet: [x:t, y:[_1:l, _2:g], z:z]'

    coords.set_titles()  # nothing happens
    assert str(coords) == 'CoordSet: [x:t, y:[_1:l, _2:g], z:z]'

    with pytest.raises(DimensionalityError):  # because units doesn't match
        coords.set_units(('km/s', ('s', 'm')), z='radian')

    coords.set_units(('km/s', ('s', 'm')), z='radian',
                     force=True)  # force change
    assert str(coords) == 'CoordSet: [x:t, y:[_1:l, _2:wavelength], z:z]'
    assert coords.y_1.units == ur('s')

    # set item

    coords['z'] = coord2
    assert str(
        coords) == 'CoordSet: [x:t, y:[_1:l, _2:wavelength], z:temperature]'

    coords['temperature'] = coord1
    assert str(
        coords) == 'CoordSet: [x:t, y:[_1:l, _2:wavelength], z:time-on-stream]'

    coords['y_2'] = coord2
    assert str(
        coords
    ) == 'CoordSet: [x:t, y:[_1:l, _2:temperature], z:time-on-stream]'

    coords['_1'] = coord2
    assert str(
        coords
    ) == 'CoordSet: [x:t, y:[_1:temperature, _2:temperature], z:time-on-stream]'

    coords['t'] = coord2
    assert str(
        coords
    ) == 'CoordSet: [x:temperature, y:[_1:temperature, _2:temperature], z:time-on-stream]'

    coord2.title = 'zaza'
    coords['temperature'] = coord2
    assert str(
        coords
    ) == 'CoordSet: [x:zaza, y:[_1:temperature, _2:temperature], z:time-on-stream]'

    coords['temperature'] = coord2
    assert str(
        coords
    ) == 'CoordSet: [x:zaza, y:[_1:zaza, _2:temperature], z:time-on-stream]'

    coords.set(coord1, coord0, coord2)
    assert str(coords) == 'CoordSet: [x:zaza, y:wavenumber, z:time-on-stream]'

    coords.z = coord0
    assert str(coords) == 'CoordSet: [x:zaza, y:wavenumber, z:wavenumber]'

    coords.zaza = coord0
    assert str(
        coords) == 'CoordSet: [x:wavenumber, y:wavenumber, z:wavenumber]'

    coords.wavenumber = coord2
    assert str(coords) == 'CoordSet: [x:zaza, y:wavenumber, z:wavenumber]'
Esempio n. 19
0
def test_coordset_implements(coord0, coord1):
    coordsa = CoordSet(coord0, coord1)

    assert coordsa.implements("CoordSet")
    assert coordsa.implements() == "CoordSet"
Esempio n. 20
0
def test_coordset_update(coord0, coord1):
    coords = CoordSet(coord0, coord1)

    coords.update(x=coord0)

    assert coords[1] == coords[0] == coord0
Esempio n. 21
0
def test_coordset_set(coord0, coord1, coord2):
    coords = CoordSet(coord2, [coord0, coord0.copy()], coord1)
    assert (
        str(coords) == repr(coords) ==
        "CoordSet: [x:time-on-stream, y:[_1:wavenumber, _2:wavenumber], z:temperature]"
    )

    coords.set_titles("time", "dddd", "celsius")
    assert (str(coords) ==
            "CoordSet: [x:time, y:[_1:wavenumber, _2:wavenumber], z:celsius]")

    coords.set_titles(x="time", z="celsius", y_1="length")
    assert (str(coords) == repr(coords) ==
            "CoordSet: [x:time, y:[_1:length, _2:wavenumber], z:celsius]")

    coords.set_titles("t", ("l", "g"), x="x")
    assert str(coords) == "CoordSet: [x:x, y:[_1:l, _2:g], z:celsius]"

    coords.set_titles(("t", ("l", "g")), z="z")
    assert str(coords) == "CoordSet: [x:t, y:[_1:l, _2:g], z:z]"

    coords.set_titles()  # nothing happens
    assert str(coords) == "CoordSet: [x:t, y:[_1:l, _2:g], z:z]"

    with pytest.raises(DimensionalityError):  # because units doesn't match
        coords.set_units(("km/s", ("s", "m")), z="radian")

    coords.set_units(("km/s", ("s", "m")), z="radian",
                     force=True)  # force change
    assert str(coords) == "CoordSet: [x:t, y:[_1:l, _2:wavelength], z:z]"
    assert coords.y_1.units == ur("s")

    # set item

    coords["z"] = coord2
    assert str(
        coords) == "CoordSet: [x:t, y:[_1:l, _2:wavelength], z:temperature]"

    coords["temperature"] = coord1
    assert str(
        coords) == "CoordSet: [x:t, y:[_1:l, _2:wavelength], z:time-on-stream]"

    coords["y_2"] = coord2
    assert str(
        coords
    ) == "CoordSet: [x:t, y:[_1:l, _2:temperature], z:time-on-stream]"

    coords["_1"] = coord2
    assert (
        str(coords) ==
        "CoordSet: [x:t, y:[_1:temperature, _2:temperature], z:time-on-stream]"
    )

    coords["t"] = coord2
    assert (
        str(coords) ==
        "CoordSet: [x:temperature, y:[_1:temperature, _2:temperature], z:time-on-stream]"
    )

    coord2.title = "zaza"
    coords["temperature"] = coord2
    assert (
        str(coords) ==
        "CoordSet: [x:zaza, y:[_1:temperature, _2:temperature], z:time-on-stream]"
    )

    coords["temperature"] = coord2
    assert (
        str(coords) ==
        "CoordSet: [x:zaza, y:[_1:zaza, _2:temperature], z:time-on-stream]")

    coords.set(coord1, coord0, coord2)
    assert str(coords) == "CoordSet: [x:zaza, y:wavenumber, z:time-on-stream]"

    coords.z = coord0
    assert str(coords) == "CoordSet: [x:zaza, y:wavenumber, z:wavenumber]"

    coords.zaza = coord0
    assert str(
        coords) == "CoordSet: [x:wavenumber, y:wavenumber, z:wavenumber]"

    coords.wavenumber = coord2
    assert str(coords) == "CoordSet: [x:zaza, y:wavenumber, z:wavenumber]"
Esempio n. 22
0
def test_coordset_implements(coord0, coord1):
    coordsa = CoordSet(coord0, coord1)

    assert coordsa.implements('CoordSet')
    assert coordsa.implements() == 'CoordSet'
Esempio n. 23
0
def test_coordset_init(coord0, coord1, coord2):
    coord3 = coord2.copy()
    coord3.title = 'titi'

    coordsa = CoordSet(coord0, coord3, coord2)  # First syntax

    assert coordsa.names == ['x', 'y',
                             'z']  # coordinates are sorted in the coordset

    coordsb = CoordSet(
        (coord0, coord3, coord2))  # second syntax with a tuple of coordinates
    assert coordsb.names == ['x', 'y', 'z']

    # but warning
    coordsa1 = CoordSet([
        coord0[:3], coord3[:3], coord2[:3]
    ])  # A list means that it is a sub-coordset (different meaning)
    assert coordsa1.names == ['x']
    assert coordsa1.x.names == ['_1', '_2', '_3']

    coordsc = CoordSet(x=coord2, y=coord3, z=coord0)  # third syntax
    assert coordsc.names == ['x', 'y', 'z']

    coordsc1 = CoordSet({'x': coord2, 'y': coord3, 'z': coord0})
    assert coordsc1.names == ['x', 'y', 'z']

    coordsd = CoordSet(coord3, x=coord2, y=coord3,
                       z=coord0)  # conflict (keyw replace args)
    assert coordsa == coordsb
    assert coordsa == coordsc
    assert coordsa == coordsd
    assert coordsa == coordsc1
    c = coordsa["x"]
    assert c == coord2
    c = coordsa["y"]
    assert c == coord3
    assert coordsa['wavenumber'] == coord0

    coord4 = copy(coord2)
    coordsc = CoordSet([coord1[:3], coord2[:3], coord4[:3]])
    assert coordsa != coordsc

    coordse = CoordSet(x=(coord1[:3], coord2[:3]), y=coord3,
                       z=coord0)  # coordset as coordinates
    assert coordse['x'].titles == CoordSet(coord1, coord2, sorted=False).titles
    assert coordse['x_2'] == coord2
    assert coordse['titi'] == coord3

    # iteration
    for coord in coordsa:
        assert isinstance(coord, Coord)

    for i, coord in enumerate(coordsa):
        assert isinstance(coord, Coord)

    assert repr(coord0) == 'Coord: [float64] cm^-1 (size: 10)'

    coords = CoordSet(coord0.copy(), coord0)

    assert repr(coords).startswith('CoordSet: [x:wavenumber, y:wavenumber]')

    with pytest.raises(ValueError):
        coords = CoordSet(2, 3)  # Coord in CoordSet cannot be simple scalar

    coords = CoordSet(x=coord2, y=coord3, z=None)
    assert coords.names == ['x', 'y', 'z']
    assert coords.z.is_empty

    coords = CoordSet(x=coord2, y=coord3, z=np.array((1, 2, 3)))
    assert coords.names == ['x', 'y', 'z']
    assert coords.z.size == 3

    with pytest.raises(KeyError):
        coords = CoordSet(x=coord2, y=coord3, fx=np.array(
            (1, 2, 3)))  # wrong key (must be a single char)

    with pytest.raises(ValueError):
        coords = CoordSet(x=coord2, y=coord3, z=3)  # wrong coordinate value

    # set a coordset from another one
    coords = CoordSet(**coordse)
    assert coordse.names == ['x', 'y', 'z']
    assert coords.names == ['x', 'y', 'z']
    assert coords == coordse

    # not recommended
    coords2 = CoordSet(
        *coordse)  # loose the names so the ordering may be different
    assert coords2.names == ['x', 'y', 'z']
    assert coords.x == coords2.z
Esempio n. 24
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)"
Esempio n. 25
0
def test_coordset_multicoord_for_a_single_dim():
    # normal coord (single numerical array for a axis)

    coord1 = Coord(
        data=np.linspace(1000.0, 4000.0, 5),
        labels="a b c d e".split(),
        mask=None,
        units="cm^1",
        title="wavelengths",
    )

    coord0 = Coord(
        data=np.linspace(20, 500, 5),
        labels="very low-low-normal-high-very high".split("-"),
        mask=None,
        units="K",
        title="temperature",
    )

    # pass as a list of coord -> this become a subcoordset
    coordsa = CoordSet([coord0, coord1])
    assert (repr(coordsa) == "CoordSet: [x:[_1:temperature, _2:wavelengths]]"
            )  # note the internal coordinates are not sorted
    assert not coordsa.is_same_dim
    assert coordsa.x.is_same_dim

    coordsb = coordsa.x

    # try to pass arguments, each being an coord
    coordsc = CoordSet(coord1, coord0)
    assert not coordsc.is_same_dim
    assert repr(coordsc) == "CoordSet: [x:temperature, y:wavelengths]"

    # try to pass arguments where each are a coords
    coordsd = CoordSet(coordsa.x, coordsc)
    assert (
        repr(coordsd) ==
        "CoordSet: [x:[_1:temperature, _2:wavelengths], y:[_1:temperature, _2:wavelengths]]"
    )

    assert not coordsd.is_same_dim
    assert np.all([item.is_same_dim for item in coordsd])

    coordse = CoordSet(coordsb, coord1)
    assert (repr(coordse) ==
            "CoordSet: [x:wavelengths, y:[_1:temperature, _2:wavelengths]]")

    assert not coordse.is_same_dim
    assert coordse("y").is_same_dim

    co = coordse("x")
    assert isinstance(co, Coord)

    co = coordse("y")
    assert isinstance(co, CoordSet)
    assert co.name == "y"
    assert co.names == ["_1", "_2"]
    assert co._1 == coord0

    co = coordse[-1:]
    assert isinstance(co, CoordSet)
    assert co[0].name == "y"  # should keep the original name (solved)
    assert co[0]["_1"] == coord0