Exemple #1
0
def test_spc_format():
    filename = load_spc_path()

    R = Request(filename)
    F = formats['SPC']
    assert F.can_read(R)
    reader = F.get_reader(R)
    assert reader.get_length() == 1
    assert reader.get_meta_data()['dat_fmt'] == 'x-y'
    spec = reader.get_data()
    assert spec.spectrum.shape == (1911, )
    assert spec.wavelength.shape == (1911, )
    spec = reader.get_data(0)
    assert spec.spectrum.shape == (1911, )
    assert spec.wavelength.shape == (1911, )
"""
====================================================
Read SPC Galactic Industries Corporation binary file
====================================================

This example shows how to read SPC file and plot the results.

"""

from __future__ import print_function

import matplotlib.pyplot as plt

from specio import specread
from specio.datasets import load_spc_path

# Find the path to the SPC toy data
spc_filename = load_spc_path()
print(spc_filename)

# Read the data
spectra = specread(spc_filename)

# Plot the first spectra
plt.plot(spectra.wavelength, spectra.spectrum[0])
plt.xlabel(spectra.meta['xlabel'])
plt.ylabel(spectra.meta['ylabel'])
plt.show()
Exemple #3
0
def test_load_spc_path():
    path_data = load_spc_path()
    assert 'datasets/data/spectra.spc' in path_data
Exemple #4
0
"""Test that all spectra files are retrieved."""

# Copyright (c) 2017
# Authors: Guillaume Lemaitre <*****@*****.**>
# License: BSD 3 clause

from os.path import join

import pytest

from specio.datasets import load_fsm_path
from specio.datasets import load_mzml_path
from specio.datasets import load_sp_path
from specio.datasets import load_spc_path
from specio.datasets import load_csv_path


@pytest.mark.parametrize("path_data,extension", [(load_fsm_path(), 'fsm'),
                                                 (load_mzml_path(), 'mzml'),
                                                 (load_sp_path(), 'sp'),
                                                 (load_spc_path(), 'spc'),
                                                 (load_csv_path(), 'csv')])
def test_load_spectra_path(path_data, extension):
    assert join('datasets', 'data', 'spectra.' + extension) in path_data
Exemple #5
0
@pytest.mark.parametrize("spectrum,wavelength,metadata", [(np.ones(
    (1, 10)), np.ones((10, )), {
        'kind': 'random'
    }), (np.ones((10, )), np.ones((10, )), {
        'kind': 'random'
    })])
def test_spectrum(spectrum, wavelength, metadata):
    spec = Spectrum(spectrum, wavelength, metadata)
    assert_allclose(spec.amplitudes, spectrum)
    assert_allclose(spec.wavelength, wavelength)
    assert spec.meta == {'kind': 'random'}


@pytest.mark.parametrize("filename",
                         [(os.path.join(module_path, 'data', '*.spc')),
                          (load_spc_path())])
def test_spectrum_to_dataframe(filename):
    spec = specread(filename)
    df_spec = spec.to_dataframe()
    if isinstance(spec.meta, tuple):
        expected_index = np.array([meta['filename'] for meta in spec.meta])
    else:
        expected_index = np.array(spec.meta['filename'])
    assert_array_equal(df_spec.index.values, expected_index)
    assert_allclose(df_spec.columns.values, spec.wavelength)
    assert_allclose(df_spec.values, np.atleast_2d(spec.amplitudes))


@pytest.mark.parametrize("filename",
                         [(os.path.join(module_path, 'data', '*.spc')),
                          (load_spc_path())])
Exemple #6
0
        for m in spec.meta:
            if filename_cmp:
                assert m['filename'] == basename(filename)
            else:
                assert m['filename']
    else:
        if filename_cmp:
            assert spec.meta['filename'] == basename(filename)
        else:
            assert spec.meta['filename']


@pytest.mark.parametrize(
    "filename,filename_cmp,spectrum_shape,wavelength_shape",
    [(load_fsm_path(), True, (7998, 1641), (1641,)),
     (load_spc_path(), True, (1911,), (1911,)),
     (load_sp_path(), True, (3301,), (3301,)),
     pytest.param(load_mzml_path(), True, (282,), (282,),
                  marks=skip_windows_py27()),
     (load_csv_path(), False, (84, 1732,), (1732,))])
def test_specread(filename, filename_cmp, spectrum_shape, wavelength_shape):
    spec = specread(filename)
    if isinstance(spec, Spectrum):
        _check_spectra(spec, filename, filename_cmp, spectrum_shape,
                       wavelength_shape)
    elif isinstance(spec, list):
        assert all([isinstance(sp, Spectrum) for sp in spec])
        sp = spec[0]
        _check_spectra(sp, filename, filename_cmp, spectrum_shape,
                       wavelength_shape)
    else: