Utilies for filtering a Table using column slice definitions

cosmic.filter._flatten(container)

Flatten arbitrary nested list of filters into a 1-D list

cosmic.filter._float_or_str(value)

Internal method to attempt float(value) handling a ValueError

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
definitionstr

a column filter definition of the form <name><operator><threshold> or <threshold><operator><name><operator><threshold>, e.g. frequency >= 10, or 50 < snr < 100

Returns
filterslist 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 operate

  • operator (callable) - the operator to call when evaluating the filter

  • operand (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 as channel = "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
mathstrstr

a str representing a mathematical operator

Returns
opfunc

a callable operator module function

Raises
KeyError

if input str cannot be mapped to an operator function

Examples

>>> parse_operator('>')
<built-in function gt>