Example #1
0
def test_log():
    # test log
    set_loglevel("WARNING")
    assert get_loglevel() == 30
    warning_('Ok, this is nicely executing!')
    set_loglevel(10)
    assert get_loglevel() == 10
Example #2
0
def test_logger(caplog):
    logger = logging.getLogger("SpectroChemPy")
    logger.propagate = True
    caplog.set_level(DEBUG)

    # We can set the level using strings
    set_loglevel("DEBUG")
    assert logger.handlers[0].level == INFO  # DEBUG only on the file
    assert logger.handlers[1].level == DEBUG

    set_loglevel(WARNING)
    assert logger.handlers[0].level == WARNING
    assert logger.handlers[1].level == WARNING

    error_("\n" + "*" * 80 + "\n")
    debug_("debug in WARNING level - should not appear")
    info_("info in WARNING level - should not appear")
    warning_("OK this is a Warning")
    error_("OK This is an Error")

    error_("\n" + "*" * 80 + "\n")

    set_loglevel(INFO)
    assert logger.handlers[0].level == INFO
    assert logger.handlers[1].level == INFO

    debug_("debug in INFO level - should not appear on stdout")
    info_("OK - info in INFO level")
    warning_("OK this is a Warning")
    error_("OK This is an Error")

    error_("\n" + "*" * 80 + "\n")

    set_loglevel("DEBUG")
    assert logger.handlers[0].level == INFO
    assert logger.handlers[1].level == DEBUG

    debug_("OK - debug in DEBUG level")
    info_("OK - info in DEBUG level")
    assert caplog.records[-1].levelname == "INFO"
    assert caplog.records[-1].message.endswith("OK - info in DEBUG level")
    warning_("OK this is a Warning")
    assert caplog.records[-1].levelname == "WARNING"
    assert caplog.records[-1].message.endswith("OK this is a Warning")
    error_("OK This is an Error")
    assert caplog.records[-1].levelname == "ERROR"
    assert caplog.records[-1].message.endswith("OK This is an Error")
Example #3
0
def test_logger(caplog):
    logger = logging.getLogger('SpectroChemPy')
    logger.propagate = True
    caplog.set_level(logging.DEBUG)

    # We can set the level using strings
    set_loglevel("DEBUG")
    assert logger.level == logging.DEBUG

    set_loglevel(WARNING)
    assert logger.level == logging.WARNING

    error_('\n' + '*' * 80 + '\n')
    debug_('debug in WARNING level - should not appear')
    info_('info in WARNING level - should not appear')
    warning_('OK this is a Warning')
    error_('OK This is an Error')

    error_('\n' + '*' * 80 + '\n')

    set_loglevel(INFO)
    assert logger.level == logging.INFO

    debug_('debug in INFO level - should not appear')
    info_('OK - info in INFO level')
    warning_('OK this is a Warning')
    error_('OK This is an Error')

    error_('\n' + '*' * 80 + '\n')

    set_loglevel('DEBUG')
    assert logger.level == logging.DEBUG

    debug_('OK - debug in DEBUG level')
    info_('OK - info in DEBUG level')
    assert caplog.records[-1].levelname == 'INFO'
    assert caplog.records[-1].message == 'OK - info in DEBUG level'
    warning_('OK this is a Warning')
    assert caplog.records[-1].levelname == 'WARNING'
    assert caplog.records[-1].message == 'OK this is a Warning'
    error_('OK This is an Error')
    assert caplog.records[-1].levelname == 'ERROR'
    assert caplog.records[-1].message == 'OK This is an Error'
Example #4
0
#
# This is a basic behavior of python : on way to avoid. stoppping the execution without displaying a message is :

# %%
try:
    from spectrochempy import toto  # noqa: F811, F401
except Exception:
    pass

# %% [markdown]
# ## API Configuration
#
# Many options of the API can be set up

# %%
scp.set_loglevel(scp.INFO)

# %% [markdown]
# In the above cell, we have set the **log** level to display ``info`` messages, such as this one:

# %%
scp.info_('this is an info message!')
scp.debug_('this is a debug message!')

# %% [markdown]
# Only the info message is displayed, as expected.
#
# If we change it to ``DEBUG``, we should get the two messages

# %%
scp.set_loglevel(scp.DEBUG)
Example #5
0
# ```python
# scp.set_loglevel('DEBUG')
# scp.set_loglevel(scp.DEBUG)
# scp.set_loglevel(10)
# ```
# The current loglevel can be obtained with the `scp.get_loglevel()` function.
#
# The following instruction prints the current loglevel
# %%
print(f"Default: {scp.get_loglevel()}")  # print the current loglevel

# %% [markdown]
# It yields `30` the numerical value corresponding to the `WARNING` level. Now, the next instruction
# lowers the loglevel to `ÌNFO`:
# %%
scp.set_loglevel(scp.INFO)  # set loglevel to 'INFO'

# %% [markdown]
# We see that the API then delivers the INFO message: `changed default log_level to INFO`.

# %% [markdown]
# And  finally, the next instructions reset the loglevel to `WARNING` level (default), and print it.
# As seen below, no message `changed default log_level to ...` is delivered

# %%
scp.set_loglevel("WARNING")  # reset to default
print(f"New loglevel: {scp.get_loglevel()}")

# %% [markdown]
# It is also possible to issue such messages in scripts. In the cell below, we set the loglevel to `INFO` and try to
# print two types of messages:
Example #6
0
def test_MCRALS():
    """Test MCRALS with synthetic data"""

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

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

    def get_C(C):
        return C

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

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

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

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

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

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

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

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

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

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

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

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

    # test plot
    mcr.plotmerit()
    show()

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

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

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

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

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

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

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

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

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

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

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

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

    # wrong nonnegSpec
    try:
        mcr = MCRALS(D, C, nonnegSpec=[2])
    except ValueError as e:
        assert "please check nonnegSpec" in e.args[0]
    try:
        mcr = MCRALS(D, C, nonnegSpec=[0, 1, 1])
    except ValueError as e:
        assert "please check nonnegSpec" in e.args[0]
Example #7
0
# -*- coding: utf-8 -*-

from subprocess import Popen, PIPE

# ======================================================================================================================
#  Copyright (©) 2015-2021 LCS - Laboratoire Catalyse et Spectrochimie, Caen, France.                                  =
#  CeCILL-B FREE SOFTWARE LICENSE AGREEMENT - See full LICENSE agreement in the root directory                         =
# ======================================================================================================================
import pytest

from spectrochempy import APIref, set_loglevel, get_loglevel, warning_, version

set_loglevel('WARNING')


def test_api():
    assert 'EFA' in APIref
    assert 'CRITICAL' in APIref
    assert 'np' in APIref
    assert 'NDDataset' in APIref
    assert 'abs' in APIref


def test_datadir():
    from spectrochempy.application import DataDir
    print(DataDir().listing())


def test_version():
    # test version
    assert len(version.split('.')) >= 3
Example #8
0
X.y.default

########################################################################################################################
# Let's now plot the spectral range of interest. The default coordinate is now used:
X_ = X[:, 2250.0:1950.0]
print(X_.y.default)
_ = X_.plot()
_ = X_.plot_map()

###############################################################################
# IRIS analysis without regularization
# ------------------------------------

########################################################################################################################
# Perform IRIS without regularization (the loglevel can be set to `INFO` to have information on the running process)
scp.set_loglevel(scp.INFO)
iris = scp.IRIS(X_, "langmuir", q=[-8, -1, 50])

########################################################################################################################
# Plots the results
iris.plotdistribution()
_ = iris.plotmerit()
###############################################################################
# With regularization and a manual search
# ---------------------------------------

########################################################################################################################
# Perform  IRIS with regularization, manual search

iris = scp.IRIS(X_, "langmuir", q=[-8, -1, 50], reg_par=[-10, 1, 12])
iris.plotlcurve(title="L curve, manual search")
# %%
St0 = A[3]
_ = St0.plot()

# %% [markdown]
# Note that, again, no information has been given as to the ordinate and abscissa data. We could add them as previously
# but this is not very important. The key point is that the 'wavelength' dimension is compatible with the data 'X',
# which is indeed the case (both have a length of 95). If it was not, an error would be generated in the following.
#
# #### ALS Optimization
# With this guess 'St0' and the dataset 'X' we can create a MCRALS object. At this point of the tutorial, we will use
# all the default parameters. We switch to `log_level = "INFO"` have a summary of the ALS iterations:

# %%
scp.set_loglevel("INFO")
mcr = scp.MCRALS(X, St0)

# %% [markdown]
# The optimization has converged within few iterations. The figures reported for each iteration are defined as follows:
#
# - 'Error/PCA' is the standard deviation of the residuals with respect to data reconstructed by a PCA with as many
# components as pure species (4 in this example),
#
# - 'Error/exp': is the standard deviation of the residuals with respect to the experimental data X,
#
# - '%change': is the percent change of 'Error/exp' between 2 iterations
#
# The default is to stop when this %change between two iteration is negative (so that the solution is improving),
# but with an absolute value lower than 0.1% (so that the improvement is considered negligible). This parameter -
# as well as several other parameters affecting the ALS optimization can be changed by the setting the 'tol' value.
Example #10
0
# -*- coding: utf-8 -*-
# flake8: noqa

from subprocess import Popen, PIPE


import pytest

from spectrochempy import APIref, set_loglevel, get_loglevel, warning_, version

set_loglevel("WARNING")


def test_api():
    assert "EFA" in APIref
    assert "CRITICAL" in APIref
    assert "NDDataset" in APIref
    assert "abs" in APIref


def test_datadir():
    # test print a listing of the testdata directory

    from spectrochempy.application import DataDir

    print(DataDir().listing())

    # or simply
    print(DataDir())

    assert str(DataDir()).startswith("testdata")