Skip to content

Commit

Permalink
Merge pull request #200 from h2020charisma/lightnovo_tsv
Browse files Browse the repository at this point in the history
Support for TSV file format
  • Loading branch information
georgievgeorgi authored Jan 16, 2025
2 parents 6bdf50b + 57d0cf6 commit b7550bb
Show file tree
Hide file tree
Showing 6 changed files with 2,334 additions and 6 deletions.
21 changes: 21 additions & 0 deletions src/ramanchada2/io/experimental/lightnovo_tsv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import Dict, List, Tuple

import pandas


def lightnovo_tsv(lines: List[str]) -> Tuple[pandas.DataFrame, Dict]:
for i, ll in enumerate(lines):
if not ll.startswith(('DeviceSN', 'LaserWavelength_nm', 'Exposure_ms',
'GainMultiplier', 'LaserCurrent_mA', 'LaserPower_mW',
'RepetitionCount', 'Tags')):
start_spe = i
break
else:
raise ValueError('The input is not lightnovo tsv format')
meta = dict([i.split('\t', 1) for i in lines[:start_spe]])
spe_lines = [ll.strip().split('\t') for ll in lines[start_spe:]]

data = pandas.DataFrame.from_records(data=spe_lines, columns=['Position', 'Amplitude']
).apply(pandas.to_numeric, errors='coerce'
).dropna(axis=0)
return data, meta
7 changes: 7 additions & 0 deletions src/ramanchada2/io/experimental/read_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from .bw_format import bw_format
from .neegala_format import neegala_format
from .lightnovo_tsv import lightnovo_tsv
from .rruf_format import rruf_format

""" There are 4 types of TXT data files that can be distinguished by their first line:
Expand Down Expand Up @@ -35,6 +36,12 @@ def read_txt(data_in: TextIO) -> Tuple[NDArray, NDArray, Dict]:
intensities = data['Processed_Data'].to_numpy()
meta['@axes'] = ['Raman_Shift']
meta['@signal'] = 'Processed_Data'
elif lines[0].startswith('DeviceSN\t'):
data, meta = lightnovo_tsv(lines)
positions = data['Position'].to_numpy()
intensities = data['Amplitude'].to_numpy()
meta['@axes'] = ['']
meta['@signal'] = ''
else: # assume two column spectrum
meta = dict()
if lines[0].startswith('#'):
Expand Down
7 changes: 4 additions & 3 deletions src/ramanchada2/spectrum/creators/from_local_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def from_local_file(
in_file_name: str,
filetype: Union[None, Literal['spc', 'sp', 'spa', '0', '1', '2',
'wdf', 'ngs', 'jdx', 'dx',
'txt', 'txtr', 'csv', 'prn', 'rruf', 'spe', 'cha']] = None,
'txt', 'txtr', 'tsv', 'csv', 'prn',
'rruf', 'spe', 'cha']] = None,
backend: Union[None, Literal['native', 'rc1_parser']] = None,
custom_meta: Dict = {}):
"""
Expand All @@ -32,7 +33,7 @@ def from_local_file(
Path to a local file containing a spectrum.
filetype:
Specify the filetype. Filetype can be any of: `spc`, `sp`, `spa`, `0`, `1`, `2`, `wdf`, `ngs`, `jdx`, `dx`,
`txt`, `txtr`, `csv`, `prn`, `rruf`, `spe` (Princeton Instruments) or `None`.
`txt`, `txtr`, `tsv`, `csv`, `prn`, `rruf`, `spe` (Princeton Instruments) or `None`.
`None` used to determine by extension of the file.
backend:
`native`, `rc1_parser` or `None`. `None` means both.
Expand All @@ -50,7 +51,7 @@ def load_native():
ft = filetype
if ft in {'cha'}:
return from_chada(filename=in_file_name)
elif ft in {'txt', 'txtr', 'prn', 'rruf'}:
elif ft in {'txt', 'txtr', 'prn', 'rruf', 'tsv'}:
with open(in_file_name) as fp:
x, y, meta = read_txt(fp)
elif ft in {'csv'}:
Expand Down
5 changes: 3 additions & 2 deletions src/ramanchada2/spectrum/creators/from_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
def from_stream(in_stream: Union[io.TextIOBase, io.BytesIO, io.BufferedReader],
filetype: Union[None, Literal['spc', 'sp', 'spa', '0', '1', '2',
'wdf', 'ngs', 'jdx', 'dx',
'txt', 'txtr', 'csv', 'prn', 'rruf', 'spe']],
'txt', 'txtr', 'tsv', 'csv', 'prn',
'rruf', 'spe']],
filename: Optional[str] = None,
backend: Union[None, Literal['native', 'rc1_parser']] = None,
):
def load_native():
if filetype in {'txt', 'txtr', 'prn', 'rruf'}:
if filetype in {'txt', 'txtr', 'tsv', 'prn', 'rruf'}:
if isinstance(in_stream, io.TextIOBase):
fp = in_stream
else:
Expand Down
Loading

0 comments on commit b7550bb

Please sign in to comment.