def test_read_srs():
    a = scp.read('irdata/omnic series/rapid_scan.srs')
    assert str(a) == 'NDDataset: [float64] V (shape: (y:643, x:4160))'

    b = scp.read('irdata/omnic series/rapid_scan_reprocessed.srs')
    assert str(b) == 'NDDataset: [float64] a.u. (shape: (y:643, x:3734))'

    c = scp.read('irdata/omnic series/GC Demo.srs')
    assert c == []

    d = scp.read('irdata/omnic series/TGA demo.srs')
    assert d == []
Exemple #2
0
def test_issue417():
    X = scp.read_omnic("irdata/nh4y-activation.spg")
    x = X - X[-1]

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

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

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

    x_r = X_r - X_r[-1]

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

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

    assert_array_equal(x.data, x_r2.data)
    assert_array_equal(x.data, x_r.data)
    assert_dataset_equal(x, x_r)
Exemple #3
0
def test_read_labspec():

    ramandir = Path('ramandata')
    scp.info_(ramandir)

    A = scp.read_labspec('Activation.txt', directory=ramandir)
    A.plot()

    A = scp.read_labspec('532nm-191216-Si 200mu.txt', directory=ramandir)
    A.plot()

    A = scp.read_labspec('serie190214-1.txt', directory=ramandir)
    A.plot(colorbar=True)
    A.plot_map(colorbar=True)

    A = scp.read_labspec('SMC1-Initial RT.txt', directory=ramandir)
    A.plot()

    B = scp.read(protocol='labspec', directory=ramandir)

    # this pack all spectra of the subdir directory
    B = scp.read_dir(directory=ramandir / 'subdir')
    B.plot()

    scp.show()
def test_bug_462():

    A = scp.random((10, 100))
    A.x = scp.Coord(np.arange(0.0, 100.0, 1), title="coord1")
    A.write("A.scp", confirm=False)
    B = scp.read("A.scp")
    assert B.x == A.x, "incorrect encoding/decoding"

    C = scp.random((10, 100))
    C.x = [
        scp.Coord(np.arange(0.0, 100.0, 1), title="coord1"),
        scp.Coord(np.arange(0.0, 1000.0, 10), title="coord2"),
    ]
    C.write("C.scp", confirm=False)
    D = scp.read("C.scp")
    assert len(D.x) == 2, "incorrect encoding/decoding"
Exemple #5
0
def test_issue_227():
    # IR spectrum, we want to make a baseline correction on the absorbance vs. time axis:
    ir = scp.read("irdata/nh4y-activation.spg")

    # baseline correction along x
    blc = scp.BaselineCorrection(ir)
    s1 = blc([5999.0, 3500.0], [1800.0, 1500.0],
             method="multivariate",
             interpolation="pchip")

    # baseline correction the transposed data along x (now on axis 0) -> should produce the same results
    # baseline correction along axis -1 previuosly
    blc = scp.BaselineCorrection(ir.T)
    s2 = blc(
        [5999.0, 3500.0],
        [1800.0, 1500.0],
        dim="x",
        method="multivariate",
        interpolation="pchip",
    )

    # compare
    assert_dataset_equal(s1, s2.T)

    ir.y = ir.y - ir[0].y
    irs = ir[:, 2000.0:2020.0]
    blc = scp.BaselineCorrection(irs)
    blc.compute(*[[0.0, 2.0e3], [3.0e4, 3.3e4]],
                dim="y",
                interpolation="polynomial",
                order=1,
                method="sequential")
    blc.corrected.plot()

    # MS profiles, we want to make a baseline correction on the ion current vs. time axis:
    ms = scp.read("msdata/ion_currents.asc", timestamp=False)
    blc = scp.BaselineCorrection(ms[10.0:20.0, :])
    blc.compute(*[[10.0, 11.0], [19.0, 20.0]],
                dim="y",
                interpolation="polynomial",
                order=1,
                method="sequential")
    blc.corrected.T.plot()

    show()
def test_read_spa():
    nd = scp.read_spa('irdata/subdir/20-50/7_CZ0-100 Pd_21.SPA')
    assert str(nd) == 'NDDataset: [float64] a.u. (shape: (y:1, x:5549))'

    nd = scp.read_spa('irdata/subdir', merge=True)
    assert str(nd) == 'NDDataset: [float64] a.u. (shape: (y:4, x:5549))'

    nd = scp.read_spa('irdata/subdir', merge=True, recursive=True)
    assert str(nd) == 'NDDataset: [float64] a.u. (shape: (y:8, x:5549))'

    lst = scp.read('irdata', merge=True,
                   recursive=True)  # not selective on extension
    assert isinstance(lst, list)
    assert len(lst) >= 88
def test_generic_read():
    # filename + extension specified
    ds = scp.read('wodger.spg')
    assert ds.name == 'wodger'

    # save with no filename (should save wodger.scp)
    path = ds.save()

    assert isinstance(path, Path)
    assert path.stem == ds.name
    assert path.parent == ds.directory

    # read should be équivalent to load (but read is a more general function,
    dataset = NDDataset.load('wodger.scp')
    assert dataset.name == 'wodger'
    def addDataset(self, dataset=None):
        scp.debug_('Add a dataset')
        # Read the  dataset
        try:
            if not dataset:
                dataset = scp.read(Qt_parent=self.parent, default_filter='omnic')
            if dataset is None:  # still not determined.
                return
        except Exception as e:
            scp.error_(e)

        # Create a subproject with this dataset
        subproj = scp.Project()
        self.project.add_project(subproj, dataset.name)
        subproj.add_dataset(dataset, f'{dataset.name}/original')

        # Signal
        self.dirty = True
        self.sigProjectChanged.emit('dataset added')
def test_read_dir():
    datadir = Path(prefs.datadir)

    A = scp.read()  # should open a dialog (but to selects individual filename

    # if we want the whole dir  - listdir must be used
    # this is equivalent to read_dir with a dialog to select directories only
    A = scp.read(listdir=True, directory=datadir / 'irdata' / 'subdir')
    assert len(A) == 4
    A1 = scp.read_dir(directory=datadir / 'irdata' / 'subdir')
    assert A == A1

    # listdir is not necessary if a directory location is given as a single argument
    B = scp.read(datadir / 'irdata' / 'subdir', listdir=True)
    B1 = scp.read(datadir / 'irdata' / 'subdir')
    assert B == B1

    # 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
    scp.read(directory=datadir / 'irdata' / 'subdir',
             listdir=True)  # -> file selection dialog
    scp.read(directory=datadir / 'irdata' / 'subdir',
             listdir=True)  # -> directory selection dialog
Exemple #10
0
def test_round_docstring_example():
    ds = scp.read("wodger.spg")
    ds_transformed1 = np.round(ds, 3)
    ds_transformed2 = np.around(ds, 3)
    ds_transformed3 = scp.around(ds, 3)
    ds_transformed4 = scp.round(ds, 3)
    ds_transformed5 = ds.round(3)
    ds_transformed6 = NDDataset.round(ds, 3)

    assert_dataset_equal(ds_transformed1, ds_transformed2)
    assert_dataset_equal(ds_transformed1, ds_transformed3)
    assert_dataset_equal(ds_transformed1, ds_transformed4)
    assert_dataset_equal(ds_transformed1, ds_transformed5)
    assert_dataset_equal(ds_transformed1, ds_transformed6)

    ds[:, 3000.0:3500.0] = scp.MASKED
    dsm_transformed1 = np.ma.round(ds)
    dsm_transformed2 = np.around(ds)
    dsm_transformed3 = scp.around(ds)
    dsm_transformed4 = ds.round()

    assert_dataset_equal(dsm_transformed1, dsm_transformed2)
    assert_dataset_equal(dsm_transformed1, dsm_transformed3)
    assert_dataset_equal(dsm_transformed1, dsm_transformed4)
Exemple #11
0
# %% slideshow={"slide_type": "fragment"} tags=["hide-output"]
import spectrochempy as scp

# %% [markdown] slideshow={"slide_type": "subslide"}
# ## NDDataset, the main object

# %% [markdown] slideshow={"slide_type": "subslide"}
# NDDataset is a python object, actually a container, which can represent most of your multidimensional spectroscopic
# data.

# %% [markdown] slideshow={"slide_type": "subslide"}
# For instance, in the following we read data from a series of FTIR experiments,
# provided  by the OMNIC software, and create a **NDDataset** from these data

# %%
ds = scp.read('irdata/nh4y-activation.spg')
print(ds)

# %% [markdown] slideshow={"slide_type": "subslide"}
# ### Display dataset information

# %%
ds

# %% [markdown] slideshow={"slide_type": "subslide"}
# ### Plotting a dataset

# %% slideshow={"slide_type": "fragment"}
_ = ds.plot()

# %% [markdown] slideshow={"slide_type": "subslide"}
Exemple #12
0
dfit = lst.inverse_transform()

dfit.plot_pen(clear=False,
              color="g",
              lw=2,
              label=" Fitted line",
              legend="best")

# %% [markdown]
# ## NDDataset modelling using the Fit method

# %% [markdown]
# First we will load an IR dataset

# %%
nd = scp.read('irdata/nh4y-activation.spg"')

# %% [markdown]
# As we want to start with a single 1D spectra, we select the last one (index -1)

# %%
nd = nd[-1].squeeze()
# nd[-1] returns a nddataset with shape (1,5549)
# this is why we squeeze it to get a pure 1D dataset with shape (5549,)

# %% [markdown]
# Now we slice it to keep only the OH vibration region:

# %%
ndOH = nd[3700.0:3300.0]
ndOH.plot()
def test_read():
    f = Path('irdata/OPUS/test.0000')

    A1 = NDDataset.read_opus(f)
    assert A1.shape == (1, 2567)

    # single file read with protocol specified
    A2 = NDDataset.read(f, protocol='opus')
    assert A2 == A1

    A3 = scp.read('irdata/nh4y-activation.spg', protocol='omnic')
    assert str(A3) == 'NDDataset: [float64] a.u. (shape: (y:55, x:5549))'

    # single file without protocol
    # inferred from filename
    A4 = NDDataset.read(f)
    assert A4 == A1

    A5 = scp.read('irdata/nh4y-activation.spg')
    assert str(A5) == 'NDDataset: [float64] a.u. (shape: (y:55, x:5549))'

    # native format
    f = A5.save_as('nh4y.scp')
    A6 = scp.read('irdata/nh4y.scp')
    assert str(A6) == 'NDDataset: [float64] a.u. (shape: (y:55, x:5549))'

    A7 = scp.read('nh4y', directory='irdata', protocol='scp')
    assert str(A7) == 'NDDataset: [float64] a.u. (shape: (y:55, x:5549))'

    A8 = scp.read('nh4y', directory='irdata')
    assert str(A8) == 'NDDataset: [float64] a.u. (shape: (y:55, x:5549))'

    f.unlink()

    # multiple compatible 1D files automatically merged
    B = NDDataset.read('test.0000',
                       'test.0001',
                       'test.0002',
                       directory=os.path.join('irdata', 'OPUS'))
    assert str(B) == 'NDDataset: [float64] a.u. (shape: (y:3, x:2567))'
    assert len(B) == 3

    # multiple compatible 1D files not merged if the merge keyword is set to False
    C = scp.read('test.0000',
                 'test.0001',
                 'test.0002',
                 directory=os.path.join('irdata', 'OPUS'),
                 merge=False)
    assert isinstance(C, list)

    # multiple 1D files to merge
    D = NDDataset.read(['test.0000', 'test.0001', 'test.0002'],
                       directory=os.path.join('irdata', 'OPUS'))
    assert D.shape == (3, 2567)

    # multiple 1D files not merged : they are passed as a list but merge is set to false
    E = scp.read(['test.0000', 'test.0001', 'test.0002'],
                 directory=os.path.join('irdata', 'OPUS'),
                 merge=False)
    assert isinstance(E, list)
    assert len(E) == 3

    # read contents
    datadir = Path(prefs.datadir)
    p = datadir / 'irdata' / 'OPUS' / 'test.0000'
    content = p.read_bytes()
    F = NDDataset.read({p.name: content})
    assert F.name == p.name
    assert F.shape == (1, 2567)

    # read multiple 1D contents and merge them
    lst = [datadir / 'irdata' / 'OPUS' / f'test.000{i}' for i in range(3)]
    G = NDDataset.read({p.name: p.read_bytes() for p in lst})
    assert G.shape == (3, 2567)
    assert len(G) == 3

    # read multiple  1D contents awithout merging
    lst = [datadir / 'irdata' / 'OPUS' / f'test.000{i}' for i in range(3)]
    H = NDDataset.read({p.name: p.read_bytes() for p in lst}, merge=False)
    isinstance(H, list)
    assert len(H) == 3

    filename = datadir / 'wodger.spg'
    content = filename.read_bytes()

    # change the filename to be sure that the file will be read from the passed content
    filename = 'try.spg'

    # The most direct way to pass the byte content information
    nd = NDDataset.read(filename, content=content)
    assert str(nd) == 'NDDataset: [float64] a.u. (shape: (y:2, x:5549))'

    # It can also be passed using a dictionary structure {filename:content, ....}
    nd = NDDataset.read({filename: content})
    assert str(nd) == 'NDDataset: [float64] a.u. (shape: (y:2, x:5549))'

    # Case where the filename is not provided
    nd = NDDataset.read(content)
    assert str(nd) == 'NDDataset: [float64] a.u. (shape: (y:2, x:5549))'

    # Try with an .spa file
    filename = datadir / 'irdata/subdir/7_CZ0-100 Pd_101.SPA'
    content = filename.read_bytes()
    filename = 'try.spa'

    filename2 = datadir / 'irdata/subdir/7_CZ0-100 Pd_102.SPA'
    content2 = filename2.read_bytes()
    filename = 'try2.spa'

    nd = NDDataset.read({filename: content})
    assert str(nd) == 'NDDataset: [float64] a.u. (shape: (y:1, x:5549))'

    # Try with only a .spa content
    nd = NDDataset.read(content)
    assert str(nd) == 'NDDataset: [float64] a.u. (shape: (y:1, x:5549))'

    # Try with several .spa content (should be stacked into a single nddataset)
    nd = NDDataset.read({filename: content, filename2: content2})
    assert str(nd) == 'NDDataset: [float64] a.u. (shape: (y:2, x:5549))'

    nd = NDDataset.read(content, content2)
    assert str(nd) == 'NDDataset: [float64] a.u. (shape: (y:2, x:5549))'
# Fist, as usual, we need to load the API.

# %%
import spectrochempy as scp

# %% [markdown]
# ## Loading an experimental dataset

# %% [markdown]
# A typical IR dataset (CO adsorption on supported CoMo catalyst in the 2300-1900 cm-1 region) will be used throughout.

# %% [markdown]
# We load the data using the generic API method  `read` (the type of data is inferred from the extension)

# %%
ds = scp.read('irdata/CO@Mo_Al2O3.SPG')

# %%
ds.y -= ds.y.data[0]  # start time a 0 for the  first spectrum
ds.y.title = 'time'
ds.y = ds.y.to('minutes')

# %% [markdown]
# Let's set some preferences for plotting

# %%
prefs = ds.preferences
prefs.method_1D = 'scatter+pen'
prefs.method_2D = 'stack'
prefs.colorbar = True
prefs.colormap = 'Dark2'
Exemple #15
0
# %%

import spectrochempy as scp

# %% [markdown]
# ## Dialog boxes
#
# Retrieving Files and Directories, in day-to-day work is often made through Dialog Boxes. While we do not recommend
# this procedure for advanced usage (see below), it is quite easy to do that with SCPy. To do so, we can use the
# `read` function which open a dialog, allowing the selection of data file form various origin. By default,
# the native SCPy type of data is proposed (file suffix: `.scp`). The desired type of files to display can be chosen
# in a dropdown field.

# %%

X = scp.read()

# %% [markdown]
# The dialog box such as shown in this image:
#
# <center><img id='drawings' width='600px'  src='./images/read.png'></img></center>
#
# The dialog Box allows selecting the file which data will be loaded in the variable `X`. Try for instance to run the
# cell below, and select an omnic spg datafile (select the .spg extension), which you can find in the `irdata`
# directory.
#
# <div class='alert alert-warning'>
# <b>Tip</b>
#
# the dialog box does not necessarily pop up in the foreground: check your task bar !
# </div>
Exemple #16
0
# ======================================================================================================================
"""
2D-IRIS analysis example
=========================

In this example, we perform the 2D IRIS analysis of CO adsorption on a sulfide catalyst.

"""

import spectrochempy as scp

########################################################################################################################
# Uploading dataset
# -----------------

X = scp.read("irdata/CO@Mo_Al2O3.SPG")

########################################################################################################################
# ``X`` has two coordinates:
# * `wavenumbers` named "x"
# * and `timestamps` (*i.e.,* the time of recording) named "y".
print(X.coordset)

########################################################################################################################
# Setting new coordinates
# -----------------------
#
# The ``y`` coordinates of the dataset is the acquisition timestamp. However, each spectrum has been recorded
# with a given pressure of CO in the infrared cell.
#
# Hence, it would be interesting to add pressure coordinates to the ``y`` dimension:
def test_protocolerror():

    # wrong protocol
    with pytest.raises(ProtocolError):
        _ = scp.read("wodger", protocol="xxx")
Exemple #18
0
# Before using the package, we must load the **API (Application Programming Interface)**

# %%
import spectrochempy as scp

# %% [markdown]
# ## NDDataset, the main object

# %% [markdown]
# NDDataset is a python object, actually a container, which can represent most of your multidimensional spectroscopic
# data.
#
# For instance, in the following we read data from a series of FTIR experiments, provided  by the OMNIC software:

# %%
ds = scp.read("irdata/nh4y-activation.spg")

# %% [markdown]
# ### Display dataset information

# %% [markdown]
# Short information:

# %%
print(ds)

# %% [markdown]
# Detailed information on the main metadata:

# %%
ds
###############################################################################
# or may be simpler using `ur`:

ur = scp.ur
10.0 * ur.meter / ur.gram / ur.volt

###############################################################################
# `ur` stands for **unit registry**, which handle many type of units (and conversion between them)

###############################################################################
# ## Units for dataset
#
# When loading experimental dataset using the `read` method, units are generally affected to coordinates and data

ds = scp.read('wodger.spg')[0]
prefs = ds.preferences
prefs.figure.figsize = (7, 3)
_ = ds.plot()

###############################################################################
# * `wavenumbers` (`x`) coordinates are here expressed in $cm^{-1}$
# * and `data` are in absorbance ($a.u.$) units.

###############################################################################
# ## Convert between units
#
# Here are some examples

x = 36 * ur('km/hr')
x.to('cm/s')