graphxplore.DataMapping.Conditionals package
Conditional objects check a source line of data (as dictionary) for a specific constraint and return a boolean. E.g. the
MetricOperator checks if the value for a specific variable is of numeric
type and compares it with a given input value. Conditionals can be combined with negations as well as and/or
compositions. All classes of this subpackage have the same interface, inherit from the
LogicOperator class and can be parsed from/to strings. Code might look
like
>>> from graphxplore.MetaDataHandling import DataType
>>> from graphxplore.DataMapping.Conditionals import MetricOperator, MetricOperatorType, StringOperator, StringOperatorType, AndOperator
>>> metric_operator = MetricOperator(table='table', variable='var', value=42, data_type=DataType.Integer,
>>> compare=MetricOperatorType.SmallerOrEqual)
>>> str(metric_operator)
'(VARIABLE var OF TYPE Integer IN TABLE table <= 42)'
>>> source_line = SourceDataLine({'table' : {'var' : 25}})
>>> metric_operator.valid(source_line)
True
>>> source_line = SourceDataLine({'table' : {'var' : 1.5}})
>>> metric_operator.valid(source_line)
# 1.5 is not an integer
False
>>> string_operator = StringOperator(table='other_table', variable='other_var', value='nana',
>>> compare=StringOperatorType.Contains)
>>> and_operator = AndOperator(sub_operators=[metric_operator, string_operator])
>>> source_line = SourceDataLine({'table' : {'var' : 25}, 'other_table' : {'other_var' : 'apple'}})
>>> and_operator.valid(source_line)
# 'nana' not in 'apple'
False
>>> source_line = SourceDataLine({'table' : {'var' : 25}, 'other_table' : {'other_var' : 'banana'}})
>>> and_operator.valid(source_line)
True
Module contents
- class graphxplore.DataMapping.Conditionals.AggregatorOperator(table: str, variable: str, value: str | int | float, data_type: DataType, aggregator: AggregatorType, compare: StringOperatorType | MetricOperatorType)[source]
Bases:
AtomicOperatorThis logic operator checks aggregated data of a specific table, variable and data type for a primary key value. It can be used to check time series for certain events. E.g., if at least one blood pressure measurements was above a certain threshold or all doctor’s notes mentioned a certain precondition.
- Parameters:
table (str) – The table of origin of the variable to check
variable (str) – The name of variable to check
value (str | int | float) – The value to check the aggregated data against
data_type (DataType) – Only values of this type will be aggregated
aggregator (AggregatorType) – The type of aggregation
compare (StringOperatorType | MetricOperatorType) – The comparison between the aggregated data and
value. Must matchaggregator. E.g. minimum, maximum or average calculations must be compared withMetricOperatorTypeobjects.AggregatorType.Listmust be used withStringOperatorType.ContainsandAggregatorType.ConcatenatewithStringOperatorTypeobjects
- static from_string(input_str: str) AggregatorOperator | None[source]
Parses an input string and generates the operator if the string is valid
- Parameters:
input_str (str) – The string to parse
- Returns:
Returns the parsed operator or
Noneifinput_stris invalid for this type of operator- Return type:
AggregatorOperator | None
- valid(source_data: SourceDataLine) bool[source]
Checks if the conditions of the logic operator are given by a line of source data.
- Parameters:
source_data (SourceDataLine) – The line of source data
- Returns:
Returns
Trueif the conditional is met,Falseotherwise- Return type:
bool
- class graphxplore.DataMapping.Conditionals.AlwaysTrueOperator[source]
Bases:
LogicOperatorThis logic operator always evaluates to
True. Consequently, itsMappingCaseis always triggered, when checked. As a result, it can be used as a default.- get_required_data() Dict[str, List[Tuple[str, Tuple[AggregatorType, DataType] | None]]][source]
Returns the source tables and variables needed by the conditional for its validity check
- Returns:
Returns the required data as a dictionary
- Return type:
Dict[str, List[Tuple[str, Tuple[AggregatorType, DataType] | None]]]
- valid(source_data: SourceDataLine) bool[source]
Checks if the conditions of the logic operator are given by a line of source data.
- Parameters:
source_data (SourceDataLine) – The line of source data
- Returns:
Returns
Trueif the conditional is met,Falseotherwise- Return type:
bool
- class graphxplore.DataMapping.Conditionals.AndOperator(sub_operators: Iterable[LogicOperator])[source]
Bases:
LogicOperatorThis logic operator checks if all of its sub-operators are valid.
- Parameters:
sub_operators (Iterable[LogicOperator]) – The sub-operators
- get_required_data() Dict[str, List[Tuple[str, Tuple[AggregatorType, DataType] | None]]][source]
Returns the source tables and variables needed by the conditional for its validity check
- Returns:
Returns the required data as a dictionary
- Return type:
Dict[str, List[Tuple[str, Tuple[AggregatorType, DataType] | None]]]
- valid(source_data: SourceDataLine) bool[source]
Checks if the conditions of the logic operator are given by a line of source data.
- Parameters:
source_data (SourceDataLine) – The line of source data
- Returns:
Returns
Trueif the conditional is met,Falseotherwise- Return type:
bool
- class graphxplore.DataMapping.Conditionals.AtomicOperator(table: str, variable: str, data_type: DataType)[source]
Bases:
LogicOperatorThis abstract class and all its children check the value of a single source variable in a line of source data.
- Parameters:
table (str) – The table of the source variable
variable (str) – The name of the source variable
data_type (DataType) – The data type the value of the source variable should have
- common_atomic_str(inner: str) str[source]
Wraps an inner string with the prefix and brackets common to all atomic logic operators.
- Parameters:
inner (str) – The inner part of the string which depends on the type of logic operator
- Returns:
Returns the generated str
- Return type:
str
- common_prefix() str[source]
Generates the common prefix of all atomic logic operators containing the variable, its data type and its origin table.
- Returns:
Returns the prefix string
- Return type:
str
- static extract_common_atomic(input_str: str) Tuple[str, str, DataType] | None[source]
Extracts source table, variable and data type from a string if it is valid.
- Parameters:
input_str (str) – The input string
- Returns:
Returns the source table, variable and data type
- Return type:
Tuple[str, str, DataType] | None
- static from_string(input_str: str) AtomicOperator | None[source]
Parses an input string and generates the operator if the string is valid
- Parameters:
input_str (str) – The string to parse
- Returns:
Returns the parsed operator or
Noneifinput_stris invalid for this type of operator- Return type:
AtomicOperator | None
- get_required_data() Dict[str, List[Tuple[str, Tuple[AggregatorType, DataType] | None]]][source]
Returns the source tables and variables needed by the conditional for its validity check
- Returns:
Returns the required data as a dictionary
- Return type:
Dict[str, List[Tuple[str, Tuple[AggregatorType, DataType] | None]]]
- valid(source_data: SourceDataLine) bool[source]
Checks if the conditions of the logic operator are given by a line of source data.
- Parameters:
source_data (SourceDataLine) – The line of source data
- Returns:
Returns
Trueif the conditional is met,Falseotherwise- Return type:
bool
- class graphxplore.DataMapping.Conditionals.InListOperator(table: str, variable: str, data_type: DataType, white_list: Iterable)[source]
Bases:
AtomicOperatorThis logic operator checks if the value for a given source variable has the correct data type and is contained in a list of values. Can be combined with
NegatedOperatorto form a “black list check”.- Parameters:
table (str) – The source table of the variable
variable (str) – The source variable
data_type (DataType) – The desired data type of the variable’s value
white_list (Iterable) – The list of acceptable values, gets converted so string values
- static from_string(input_str: str) InListOperator | None[source]
Parses an input string and generates the operator if the string is valid
- Parameters:
input_str (str) – The string to parse
- Returns:
Returns the parsed operator or
Noneifinput_stris invalid for this type of operator- Return type:
InListOperator | None
- valid(source_data: SourceDataLine) bool[source]
Checks if the conditions of the logic operator are given by a line of source data.
- Parameters:
source_data (SourceDataLine) – The line of source data
- Returns:
Returns
Trueif the conditional is met,Falseotherwise- Return type:
bool
- class graphxplore.DataMapping.Conditionals.LogicOperator[source]
Bases:
objectThis is the abstract parent class of all conditionals for a
MappingCase. Each logic operator checks the validity of a given line of source data based on the described conditional- get_required_data() Dict[str, List[Tuple[str, Tuple[AggregatorType, DataType] | None]]][source]
Returns the source tables and variables needed by the conditional for its validity check
- Returns:
Returns the required data as a dictionary
- Return type:
Dict[str, List[Tuple[str, Tuple[AggregatorType, DataType] | None]]]
- valid(source_data: SourceDataLine) bool[source]
Checks if the conditions of the logic operator are given by a line of source data.
- Parameters:
source_data (SourceDataLine) – The line of source data
- Returns:
Returns
Trueif the conditional is met,Falseotherwise- Return type:
bool
- class graphxplore.DataMapping.Conditionals.LogicOperatorParser[source]
Bases:
objectThis class parses conditional strings and extracts the represented
LogicOperator- static from_string(input_str: str) LogicOperator[source]
Parses a string and returns the generated
LogicOperatoror raises an exception of the string is invalid.- Parameters:
input_str (str) – The input string
- Returns:
Returns the generated operator
- Return type:
- static resolve_composition(substring: str, input_str: str) AndOperator | OrOperator[source]
Decompose and/or composition into sub operators
- Parameters:
substring (str) – The current substring
input_str (str) – The full input string
- Returns:
Returns the and/or logic operator
- Return type:
- class graphxplore.DataMapping.Conditionals.MetricOperator(table: str, variable: str, value: int | float, data_type: DataType, compare: MetricOperatorType)[source]
Bases:
AtomicOperatorThis logic operator performs metric comparisons between the value of a single source variable and
value.- Parameters:
table (str) – The source table of the variable
variable (str) – The source variable
value (int | float) – The value for comparison
compare (MetricOperatorType) – The type of metric comparison
data_type (DataType)
- static check_value(val_to_check: int | float, to_check_against: int | float, compare: MetricOperatorType) bool[source]
Checks the validity of
val_to_check- Parameters:
val_to_check (int | float) – The value to check the validity for
to_check_against (int | float) – The base value to check against
compare (MetricOperatorType) – The type of comparison
- Returns:
Returns
Trueifval_to_checkis valid, elseFalse- Return type:
bool
- static from_string(input_str: str) MetricOperator | None[source]
Parses an input string and generates the operator if the string is valid
- Parameters:
input_str (str) – The string to parse
- Returns:
Returns the parsed operator or
Noneifinput_stris invalid for this type of operator- Return type:
MetricOperator | None
- valid(source_data: SourceDataLine) bool[source]
Checks if the conditions of the logic operator are given by a line of source data.
- Parameters:
source_data (SourceDataLine) – The line of source data
- Returns:
Returns
Trueif the conditional is met,Falseotherwise- Return type:
bool
- class graphxplore.DataMapping.Conditionals.MetricOperatorType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
str,EnumThe type of logic operator on metric or categorical variables of numeric type
- Equals = '=='
- Larger = '>'
- LargerOrEqual = '>='
- Smaller = '<'
- SmallerOrEqual = '<='
- class graphxplore.DataMapping.Conditionals.NegatedOperator(pos_operator: LogicOperator)[source]
Bases:
LogicOperatorThis logic operator negates an input operator
pos_operator, i.e. it checks ifpos_operator.valid()evaluates toFalse- Parameters:
pos_operator (LogicOperator) – The operator to negate
- get_required_data() Dict[str, List[Tuple[str, Tuple[AggregatorType, DataType] | None]]][source]
Returns the source tables and variables needed by the conditional for its validity check
- Returns:
Returns the required data as a dictionary
- Return type:
Dict[str, List[Tuple[str, Tuple[AggregatorType, DataType] | None]]]
- valid(source_data: SourceDataLine) bool[source]
Checks if the conditions of the logic operator are given by a line of source data.
- Parameters:
source_data (SourceDataLine) – The line of source data
- Returns:
Returns
Trueif the conditional is met,Falseotherwise- Return type:
bool
- class graphxplore.DataMapping.Conditionals.OrOperator(sub_operators: Iterable[LogicOperator])[source]
Bases:
LogicOperatorThis logic operator checks if any of its sub-operators is valid.
- Parameters:
sub_operators (Iterable[LogicOperator]) – The sub-operators
- get_required_data() Dict[str, List[Tuple[str, Tuple[AggregatorType, DataType] | None]]][source]
Returns the source tables and variables needed by the conditional for its validity check
- Returns:
Returns the required data as a dictionary
- Return type:
Dict[str, List[Tuple[str, Tuple[AggregatorType, DataType] | None]]]
- valid(source_data: SourceDataLine) bool[source]
Checks if the conditions of the logic operator are given by a line of source data.
- Parameters:
source_data (SourceDataLine) – The line of source data
- Returns:
Returns
Trueif the conditional is met,Falseotherwise- Return type:
bool
- class graphxplore.DataMapping.Conditionals.StringOperator(table: str, variable: str, value: str, compare: StringOperatorType)[source]
Bases:
AtomicOperatorThis logic operator performs string comparisons between the value of a single source variable and
value.- Parameters:
table (str) – The source table of the variable
variable (str) – The source variable
value (str) – The value for comparison
compare (StringOperatorType) – The type of string comparison
- static check_value(val_to_check: str, to_check_against: str, compare: StringOperatorType) bool[source]
Checks the validity of
val_to_check- Parameters:
val_to_check (str) – The value to check the validity for
to_check_against (str) – The base value to check against
compare (StringOperatorType) – The type of comparison
- Returns:
Returns
Trueifval_to_checkis valid, elseFalse- Return type:
bool
- static from_string(input_str: str) StringOperator | None[source]
Parses an input string and generates the operator if the string is valid
- Parameters:
input_str (str) – The string to parse
- Returns:
Returns the parsed operator or
Noneifinput_stris invalid for this type of operator- Return type:
StringOperator | None
- valid(source_data: SourceDataLine) bool[source]
Checks if the conditions of the logic operator are given by a line of source data.
- Parameters:
source_data (SourceDataLine) – The line of source data
- Returns:
Returns
Trueif the conditional is met,Falseotherwise- Return type:
bool