From a02369746c5fbefef0640aff7a0a6aa349aaae2d Mon Sep 17 00:00:00 2001 From: lanery Date: Mon, 24 Feb 2025 14:43:21 -0800 Subject: [PATCH 1/2] Update spectra.py - add a class method for a "generic" reader that assumes the convention that many vendors abide by in which the first column of a csv file is the wavenumber data and the second column is the intensity data --- ramanalysis/spectra.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ramanalysis/spectra.py b/ramanalysis/spectra.py index eb0ecdb..2bbd007 100644 --- a/ramanalysis/spectra.py +++ b/ramanalysis/spectra.py @@ -5,6 +5,7 @@ from typing import cast import numpy as np +import pandas as pd from scipy.signal import find_peaks, medfilt from .calibrate import ( @@ -138,6 +139,18 @@ def from_wasatch_csvfile(cls, csv_filepath: Path | str) -> RamanSpectrum: wavenumbers_cm1, intensities, _metadata = read_wasatch_csv(csv_filepath) return RamanSpectrum(wavenumbers_cm1, intensities) + @classmethod + def from_generic_csvfile( + cls, + csv_filepath: Path | str, + wavenumber_cm1_index: int = 0, + intensity_index: int = 1, + **kwargs, + ) -> RamanSpectrum: + """""" + data = pd.read_csv(csv_filepath, **kwargs).values # type: ignore + return RamanSpectrum(wavenumbers_cm1=data[:, 0], intensities=data[:, 1]) + @property def snr(self) -> float: raise NotImplementedError("TODO: SNR calculation hasn't been implemented.") From fbd3b7f4e47487ba9a9622968cf99750978c1bf9 Mon Sep 17 00:00:00 2001 From: lanery Date: Mon, 24 Feb 2025 16:35:28 -0800 Subject: [PATCH 2/2] Update spectra.py - add docstring - actually use the wavenumber and intensity index arguments to choose the appropriate columns --- ramanalysis/spectra.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/ramanalysis/spectra.py b/ramanalysis/spectra.py index 2bbd007..ee85ff4 100644 --- a/ramanalysis/spectra.py +++ b/ramanalysis/spectra.py @@ -147,9 +147,21 @@ def from_generic_csvfile( intensity_index: int = 1, **kwargs, ) -> RamanSpectrum: - """""" + """Load a Raman spectrum from a CSV file for which there is no pre-defined reader. + + Most data files from Raman spectrometer manufacturers follow a convention where the first + column contains the wavenumbers and the second column contains the intensities. Where + they vary is in e.g. the delimiter character, the number of rows of metadata, etc. This + function assumes this generic format and creates a spectrum object from the data by passing + keyword arguments to :func:`pd.read_csv` to support a range of possible formats. + + See also: + - https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html + """ data = pd.read_csv(csv_filepath, **kwargs).values # type: ignore - return RamanSpectrum(wavenumbers_cm1=data[:, 0], intensities=data[:, 1]) + wavenumbers_cm1 = data[:, wavenumber_cm1_index] + intensities = data[:, intensity_index] + return RamanSpectrum(wavenumbers_cm1, intensities) @property def snr(self) -> float: