Exemplo n.º 1
0
def test_issue_375():

    # minimal example
    n_pc = 3

    color1, color2 = "b", "r"

    ratio = NDDataset([1, 2, 3])
    cum = ratio.cumsum()

    ax1 = ratio.plot_bar(color=color1, title="Scree plot")
    assert len(ax1.lines) == 0, "no lines"
    assert len(ax1.patches) == 3, "bar present"
    ax2 = cum.plot_scatter(color=color2, pen=True, markersize=7.0, twinx=ax1)
    assert len(ax2.lines) == 1, "1 lines"
    assert len(ax2.patches) == 0, "no bar present on the second plot"
    # TODO: Don't know yet how to get the marker present.
    ax1.set_title("Scree plot")
    show()
Exemplo n.º 2
0
# To load the API, you must import it using one of the following syntax.
#
# In the first syntax we load the library into a namespace called `scp` (you can choose whatever you want - except
# something already in use):

# %%
import spectrochempy as scp  # SYNTAX 1
nd = scp.NDDataset()


# %% [markdown]
# or in the second syntax, with a wild `*` import.

# %%
from spectrochempy import *
nd = NDDataset()

# %% [markdown]
# With the second syntax, as often in python, the access to objects/functions can be greatly simplified. For example,
# we can use "NDDataset" without a prefix instead of `scp.NDDataset` which is the first syntax) but there is always a
# risk of overwriting some variables or functions already present in the namespace.
# Therefore, the first syntax is generally highly recommended.
#
# Alternatively, you can also load only the onbjects and function required by your application:
#

# %%
from spectrochempy import NDDataset

nd = NDDataset()
Exemplo n.º 3
0
# *  `mask`: Data can be partially masked at will
# *  `units`: Data can have units, allowing units-aware operations
# *  `coordset`: Data can have a set of coordinates, one or several by dimensions
#
# Additional metadata can also be added to the instances of this class through the `meta` properties.

# %% [markdown]
# ## 1D-Dataset (unidimensional dataset)

# %% [markdown]
# In the following example, a minimal 1D dataset is created from a simple list, to which we can add some metadata:

# %%
d1D = NDDataset(
    [10.0, 20.0, 30.0],
    name="Dataset N1",
    author="Blake and Mortimer",
    description="A dataset from scratch",
)
d1D

# %% [markdown]
# <div class='alert alert-info'>
#     <b>Note</b>
#
#  In the above code, run in a notebook, the output of d1D is in html for a nice display.
#
#  To get the same effect, from a console script, one can use `print_` (with an underscore) and not the usual python
#  function `print`. As you can see below, the `print` function only gives a short summary of the information,
#  while the `print_` method gives more detailed output
#
# </div>
Exemplo n.º 4
0
def fake_dataset(*args, size=3, **kwargs):
    if not args:
        ds = NDDataset([range(size)])
    else:
        ds = NDDataset([[range(4)]], )
    return ds
Exemplo n.º 5
0
def test_importer(monkeypatch, fs):

    fs.create_file("/var/data/xx1.txt")
    assert os.path.exists("/var/data/xx1.txt")

    # mock filesystem
    fs.create_dir(DATADIR)

    # try to read unexistent scp file
    f = DATADIR / "fakedir/fakescp.scp"
    with pytest.raises(FileNotFoundError):
        read(f)

    # make fake file
    fs.create_file(f)
    monkeypatch.setattr(NDDataset, "load", fake_dataset)

    nd = read(f)
    assert nd == fake_dataset(f)

    nd = read(f.stem, directory=DATADIR / "fakedir/", protocol="scp")
    assert nd == fake_dataset(f)

    nd = read(f.stem, directory=DATADIR / "fakedir/")
    assert nd == fake_dataset(f)

    # Generic read without parameters and dialog cancel
    monkeypatch.setattr(spectrochempy.core, "open_dialog", dialog_cancel)
    monkeypatch.setenv(
        "KEEP_DIALOGS",
        "True")  # we ask to display dialogs as we will mock them.

    nd = read()
    assert nd is None

    # read as class method
    nd1 = NDDataset.read()
    assert nd1 is None

    # NDDataset instance as first arguments
    nd = NDDataset()
    nd2 = nd.read()
    assert nd2 is None

    nd = read(default_filter="matlab")
    assert nd is None

    # Check if Filetype is not known
    f = DATADIR / "fakedir/not_exist_fake.fk"
    with pytest.raises(TypeError):
        read_fake(f)

    # Make fake type acceptable
    FILETYPES.append(("fake", "FAKE files (*.fk)"))
    ALIAS.append(("fk", "fake"))
    monkeypatch.setattr("spectrochempy.core.readers.importer.FILETYPES",
                        FILETYPES)
    monkeypatch.setattr("spectrochempy.core.readers.importer.ALIAS", ALIAS)

    # Check not existing filename
    f = DATADIR / "fakedir/not_exist_fake.fk"
    with pytest.raises(FileNotFoundError):
        read_fake(f)

    # Generic read with a wrong protocol
    with pytest.raises(spectrochempy.utils.exceptions.ProtocolError):
        read(f, protocol="wrongfake")

    # Generic read with a wrong file extension
    with pytest.raises(TypeError):
        g = DATADIR / "fakedir/otherfake.farfelu"
        read(g)

    # Mock file
    f = DATADIR / "fakedir/fake.fk"
    fs.create_file(f)

    # specific read_(protocol) function
    nd = read_fk(f)
    assert nd == fake_dataset()

    # should also be a Class function
    nd = NDDataset.read_fk(f)
    assert nd == fake_dataset()

    # and a NDDataset instance function
    nd = NDDataset().read_fk(f)
    assert nd == fake_dataset()

    # single file without protocol inferred from filename
    nd = read(f)
    assert nd == fake_dataset()

    # single file read with protocol specified
    nd = read(f, protocol="fake")
    assert nd == fake_dataset()

    # attribute a new name
    nd = read(f, name="toto")
    assert nd.name == "toto"

    # mock some fake file and assume they exists
    f1 = DATADIR / "fakedir/fake1.fk"
    f2 = DATADIR / "fakedir/fake2.fk"
    f3 = DATADIR / "fakedir/fake3.fk"
    f4 = DATADIR / "fakedir/fake4.fk"
    f5 = DATADIR / "fakedir/otherdir/otherfake.fk"
    f6 = DATADIR / "fakedir/emptyfake.fk"  # return None when reader
    fs.create_file(f1)
    fs.create_file(f2)
    fs.create_file(f3)
    fs.create_file(f4)
    fs.create_file(f5)
    fs.create_file(f6)
    # l = list(pathclean("/Users/christian/test_data/fakedir").iterdir())

    # multiple compatible 1D files automatically merged
    nd = read(f1, f2, f3)
    assert nd.shape == (3, 3)

    nd = read([f1, f2, f3], name="fake_merged")
    assert nd.shape == (3, 3)
    assert nd.name == "fake_merged"

    # multiple compatible 1D files not merged if the merge keyword is set to False
    nd = read([f1, f2, f3], names=["a", "c", "b"], merge=False)
    assert isinstance(nd, list)
    assert len(nd) == 3 and nd[0] == fake_dataset()
    assert nd[1].name == "c"

    # do not merge inhomogeneous dataset
    nd = read([f1, f2, f5])
    assert isinstance(nd, list)

    # too short list of names.  Not applied
    nd = read([f1, f2, f3], names=["a", "c"], merge=False)
    assert nd[0].name.startswith("NDDataset")

    monkeypatch.setattr(spectrochempy.core, "open_dialog", dialog_open)
    nd = (
        read()
    )  # should open a dialog (but to selects individual filename (here only simulated)
    assert nd.shape == (2, 3)

    # read in a directory
    monkeypatch.setattr(pathlib.Path, "glob", directory_glob)

    # directory selection
    nd = read(protocol="fake", directory=DATADIR / "fakedir")
    assert nd.shape == (4, 3)

    nd = read(protocol="fake", directory=DATADIR / "fakedir", merge=False)
    assert len(nd) == 4
    assert isinstance(nd, list)

    nd = read(listdir=True, directory=DATADIR / "fakedir")
    assert len(nd) == 4
    assert not isinstance(nd, list)

    # if a directory is passed as a keyword, the behavior is different:
    # a dialog for file selection occurs except if listdir is set to True
    nd = read(directory=DATADIR / "fakedir", listdir=False)
    assert nd.shape == (2, 3)  # -> file selection dialog

    nd = read(directory=DATADIR / "fakedir", listdir=True)
    assert nd.shape == (4, 3)  # -> directory selection dialog

    # read_dir()

    nd = read_dir(DATADIR / "fakedir")
    assert nd.shape == (4, 3)

    nd1 = read_dir()
    assert nd1 == nd

    nd = read_dir(
        directory=DATADIR / "fakedir"
    )  # open a dialog to eventually select directory inside the specified
    # one
    assert nd.shape == (4, 3)

    fs.create_file(DATADIR / "fakedir/subdir/fakesub1.fk")
    nd = read_dir(directory=DATADIR / "fakedir", recursive=True)
    assert nd.shape == (5, 3)

    # no merging
    nd = read_dir(directory=DATADIR / "fakedir", recursive=True, merge=False)
    assert len(nd) == 5
    assert isinstance(nd, list)

    # Simulate reading a content
    nd = read({"somename.fk": "a fake content"})
    assert nd == fake_dataset(content=True)
    nd = read_fake({"somename.fk": "a fake content"})
    assert nd == fake_dataset(content=True)

    # read multiple contents and merge them
    nd = read({
        "somename.fk": "a fake content",
        "anothername.fk": "another fake content",
        "stillanothername.fk": "still another fake content",
    })
    assert nd.shape == (3, 3)

    # do not merge
    nd = read(
        {
            "somename.fk": "a fake content",
            "anothername.fk": "another fake content"
        },
        merge=False,
    )
    assert isinstance(nd, list)
    assert len(nd) == 2
Exemplo n.º 6
0
# handling spectroscopic information, one of the major objectives of the SpectroChemPy package:
#
# *  `mask`: Data can be partially masked at will
# *  `units`: Data can have units, allowing units-aware operations
# *  `coordset`: Data can have a set of coordinates, one or sevral by dimensions
#
# Additional metadata can also be added to the instances of this class through the `meta` properties.

# %% [markdown]
# ## 1D-Dataset (unidimensional dataset)

# %% [markdown]
# In the following example, a minimal 1D dataset is created from a simple list, to which we can add some metadata:

# %%
d1D = NDDataset([10., 20., 30.], name="Dataset N1", author='Blake and Mortimer', description='A dataset from scratch')
d1D

# %% [markdown]
# <div class='alert alert-info'>
#     <b>Note</b>
#
#  In the above code, run in a notebook, the output of d1D is in html for a nice display.
#
#  To get the same effect, from a console script, one can use `print_` (with an underscore) and not the usual python
#  function `print`. As you can see below, the `print` function only gives a short summary of the information,
#  while the `print_` method gives more detailed output
#
# </div>

# %%