Utilies for filtering a Table using column slice definitions
-
cosmic.filter.
is_filter_tuple
(tup)¶ Return whether a tuple matches the format for a column filter
-
cosmic.filter.
parse_column_filter
(definition)¶ Parse a str of the form ‘column>50’
- Parameters
definition : str
a column filter definition of the form
<name><operator><threshold>
or<threshold><operator><name><operator><threshold>
, e.g.frequency >= 10
, or50 < snr < 100
- Returns
filters : list of tuple
a list of filter 3-tuple`s, where each `tuple contains the following elements:
column
(str) - the name of the column on which to operateoperator
(callable) - the operator to call when evaluating the filteroperand
(anything) - the argument to the operator function
- Raises
ValueError
if the filter definition cannot be parsed
KeyError
if any parsed operator string cannnot be mapped to a function from the operator module
Notes
Strings that contain non-alphanumeric characters (e.g. hyphen -) should be quoted inside the filter definition, to prevent such characters being interpreted as operators, e.g.
channel = X1:TEST
should always be passed aschannel = "X1:TEST"
.Examples
>>> parse_column_filter("frequency>10") [('frequency', <function operator.gt>, 10.)] >>> parse_column_filter("50 < snr < 100") [('snr', <function operator.gt>, 50.), ('snr', <function operator.lt>, 100.)] >>> parse_column_filter("channel = "H1:TEST") [('channel', <function operator.eq>, 'H1:TEST')]
-
cosmic.filter.
parse_column_filters
(*definitions)¶ Parse multiple compound column filter definitions
Examples
>>> parse_column_filters('snr > 10', 'frequency < 1000') [('snr', <function operator.gt>, 10.), ('frequency', <function operator.lt>, 1000.)] >>> parse_column_filters('snr > 10 && frequency < 1000') [('snr', <function operator.gt>, 10.), ('frequency', <function operator.lt>, 1000.)]
-
cosmic.filter.
parse_operator
(mathstr)¶ Parse a str as a function from the operator module
- Parameters
mathstr : str
a str representing a mathematical operator
- Returns
op : func
a callable operator module function
- Raises
KeyError
if input str cannot be mapped to an operator function
Examples
>>> parse_operator('>') <built-in function gt>