def test_spc_file(filename, spectrum_shape, wavelength_shape): spec = specread(filename) if isinstance(spec, list): # in '-xy.spc', there is two different wavelength size: we are checking # each of them for wi in range(1): assert spec[wi].spectrum.shape == spectrum_shape[wi] assert spec[wi].wavelength.shape == wavelength_shape[wi] else: assert spec.spectrum.shape == spectrum_shape assert spec.wavelength.shape == wavelength_shape
def test_spectrum_to_csv(filename): spec = specread(filename) tmp_dir = mkdtemp() filename = os.path.join(tmp_dir, 'spectra.csv') try: spec.to_csv(filename) df = pd.read_csv(filename, index_col=0) df.columns = df.columns.astype(float) assert_allclose(df.values, spec.to_dataframe().values) assert_allclose(df.columns.values, spec.to_dataframe().columns.values) assert_array_equal(df.index.values, spec.to_dataframe().index.values) finally: rmtree(tmp_dir)
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: raise AssertionError('specread should return either a Spectrum ' 'instance or a list of Spectrum. ' 'Got {!r}.'.format(spec))
def test_specread_consitent_wavelength(side_effect, tol_wavelength, spectra_type, spectra_shape, mocker): # emulate that we read several file mocker.patch('specio.core.functions._validate_filenames', return_value=['filename' for _ in range(10)]) mocker.patch('specio.core.functions._get_reader_get_data', side_effect=side_effect) # emulate the spectrum reading spectra = specread('', tol_wavelength=tol_wavelength) assert isinstance(spectra, spectra_type) if isinstance(spectra, Spectrum): assert spectra.amplitudes.shape == spectra_shape assert spectra.wavelength.shape == (spectra_shape[1], ) assert spectra.meta == tuple({} for _ in range(spectra_shape[0])) elif isinstance(spectra, list): assert len(spectra) == spectra_shape
""" ==================================================== 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()
def test_spc_xy(): filename = join(DATA_PATH, 'data', 'gxy.spc') spec = specread(filename) assert spec.spectrum.shape == (151, ) assert spec.wavelength.shape == (151, )
import sys import matplotlib.pyplot as plt from specio import specread print(__doc__) # Get the path to the data relatively to this example DATA_PATH = os.path.dirname(sys.argv[0]) spc_filenames = os.path.join(DATA_PATH, 'data', '*.spc') print('The SPC files will be search in: {}'.format(spc_filenames)) # Read the data spectra = specread(spc_filenames) # Plot the first spectra plt.figure() plt.plot(spectra.wavelength, spectra.amplitudes.T) # We get the axis information by using the meta data of the first file read. plt.xlabel(spectra.meta[0]['xlabel']) plt.ylabel(spectra.meta[0]['ylabel']) plt.title('Spectra extracted from the SPC files') # Export all spectrum into csv spectra.to_csv('spectra.csv') # We can open the exported file
""" ============================================== Read FSM Perkin Elmer Spotlight IR binary file ============================================== This example shows how to read FSM file and plot the results. """ from __future__ import print_function import matplotlib.pyplot as plt from specio import specread from specio.datasets import load_fsm_path # Find the path to the FSM toy data fsm_filename = load_fsm_path() print(fsm_filename) # Read the data spectra = specread(fsm_filename) # Plot the first spectra plt.plot(spectra.wavelength, spectra.spectrum[0]) plt.show()
This example shows how to read CSV file and plot the results. Note that this format is the format used when using :func:`specio.core.Spectrum.to_csv` """ # Authors: Guillaume Lemaitre <*****@*****.**> # License: BSD3 from __future__ import print_function import matplotlib.pyplot as plt from specio import specread from specio.datasets import load_csv_path print(__doc__) # Find the path to the CSV toy data csv_filename = load_csv_path() print(csv_filename) # Read the data spectra = specread(csv_filename) # Plot the first spectra plt.plot(spectra.wavelength, spectra.amplitudes.T) plt.xlabel('Amplitude') plt.ylabel('Wavelength') plt.show()
This example illustrates how to export a spectrum into a dataframe which can be useful for some later processing. """ # Authors: Guillaume Lemaitre <*****@*****.**> # License: BSD3 import matplotlib.pyplot as plt from specio.datasets import load_csv_path from specio import specread # read the spectrum spectra = specread(load_csv_path()) # convert to a dataframe df_spectra = spectra.to_dataframe() # print the head of dataframe print(df_spectra.head()) # plot the five first spectra df_spectra.head().T.plot() # or on different plots df_spectra.head().T.plot(subplots=True) plt.show()