tesliper.extraction.spectra_parser
Parser for spectra files.
Classes
Parser for files containing spectral data. |
- class tesliper.extraction.spectra_parser.SpectraParser[source]
Parser for files containing spectral data. It can parse .txt (in “x y” format) and .csv files, returning an numpy.ndarray with loaded spectrum. Parsing process may be customized by specifying what delimiter of values should be expected and in which column x- and y-values are, if there are more than 2 columns of data. If file contains any header, it is ignored.
- parse(filename: Union[str, pathlib.Path], delimiter: Optional[str] = None, xcolumn: int = 0, ycolumn: int = 1) numpy.ndarray[source]
Loads spectral data from file to numpy.array. Currently supports only .txt, .xy, and .csv files.
- Parameters
filename (str) – path to file containing spectral data
delimiter (str, optional) – character used to delimit columns in file, defaults to whitespace
xcolumn (int, optional) – column, that should be used as points on x axis, defaults to 0 (first column)
ycolumn (int, optional) – column, that should be used as values on y axis, defaults to 1 (second column)
- Returns
two-dimensional numpy array ([[x-values], [y-values]]) of data type
float- Return type
numpy.array
- initial(filename: str)[source]
An initial parser state.
A default implementation checks if any of defined triggers matches a line and sets an associated state as parser’s workhorse, if it does. This is an abstract method and should be overridden in subclass. Its default implementation can be used, however, by calling
super().initial(line)in subclass’s method.Notes
initial()method is always registered as parser’s state.- Parameters
line (str) – currently parsed line
- Returns
empty dictionary
- Return type
dict
- parse_txt(file: pathlib.Path)[source]
Loads spectral data from .txt or .xy file to numpy.array.
- Parameters
file (str) – path to file containing spectral data
delimiter (str, optional) – character used to delimit columns in file, defaults to whitespace
xcolumn (int, optional) – column, that should be used as points on x axis, defaults to 0 (first column)
ycolumn (int, optional) – column, that should be used as values on y axis, defaults to 1 (second column)
- Returns
numpy.array – two-dimensional numpy array ([[x-values], [y-values]]) of data type ‘float’
Rises
—–
ValueError – if file passed was read to end, but no spectral data was found; this includes columns’ numbers out of range and usage of inappropriate delimiter
- parse_csv(file: pathlib.Path)[source]
Loads spectral data from csv file to numpy.array.
- Parameters
file (str) – path to file containing spectral data
delimiter (str, optional) – character used to delimit columns in file, defaults to ‘,’
xcolumn (int, optional) – column, that should be used as points on x axis, defaults to 0 (first column)
ycolumn (int, optional) – column, that should be used as values on y axis, defaults to 1 (second column)
- Returns
two-dimensional numpy array ([[x-values], [y-values]]) of data type ‘float’
- Return type
numpy.array
- parse_spc(file)[source]
Loads spectral data from spc file to numpy.array.
Notes
This method is not implemented yet, it will raise an error when called.
- Parameters
file (str) – path to file containing spectral data
- Returns
two-dimensional numpy array ([[x-values], [y-values]]) of data type ‘float’
- Return type
numpy.array
- Raises
NotImplementedError – Whenever called, as this functionality is not implemented yet.
- add_state(state: Callable, name: str = '', trigger: str = '')
Register callable as parser’s state.
This method registers a callable under name key in
statesdictionary. If trigger parameter is given, it is registered under the same key intriggersdictionary.- Parameters
state (Callable) – callable, that is to be registered as parser’s state
name (str, optional) – name under which the callable should be registered; defaults to callable.__name__
trigger (str, optional) – string with regular expression, that will be compiled with re module
- Returns
callable object registered as state
- Return type
Callable
- remove_state(name: str)
Removes the state from parser’s registered states.
- Parameters
name (str) – name of state, that should be unregistered
- Raises
InvalidStateError – if no callable was registered under the name ‘name’
- static state(state=None, trigger=None)
Convenience decorator for registering a method as parser’s state. It can be with or without ‘trigger’ parameter, like this:
>>> @ParserBase.state ... def method(self, arg): pass
or
>>> @ParserBase.state(trigger='triggering regex') ... def method(self, arg): pass
This function marks a method state as parser’s state by defining
is_stateattribute on said method and setting its values toTrue. If trigger is given, it is stored in method’s attribute trigger. During instantiation ofParserBase’s subclass, methods marked as states are registered undermethod.__name__key in itsstates(and possiblytriggers) attribute. It is meaningless if used outside ofParserBase’s subclass definition.- Parameters
state (Callable) – callable, that is to be registered as parser’s state
trigger (str, optional) – string with regular expression, that will be compiled with re module
- Returns
callable object registered as state if ‘state’ was given or decorator if only ‘trigger’ was given
- Return type
Callable
- Raises
TypeError – if no arguments given
InvalidStateError – if state argument is not callable
- property workhorse: Callable
Callable marked as a current state used by parser object.
Setter can take a callable or a string as a parameter. If name as string is passed to setter, it will be translated to a method registered as state. If no method was registered under this name,
InvalidStateErrorwill be raised. No other checks are performed when argument is callable.