dispel.data.flags module#
A module containing models for flags.
Flags are basically a structured way to mark the data contained in a certain entity as deviating or invalidating assumptions while processing.
- class dispel.data.flags.Flag[source]#
Bases:
object
A class for entity flag.
- __init__(id_, reason, stop_processing=False)#
- Parameters:
id_ (dataclasses.InitVar[Union[str, dispel.data.flags.FlagId]]) –
reason (str) –
stop_processing (bool) –
- Return type:
None
- id_: dataclasses.InitVar[Union[str, dispel.data.flags.FlagId]]#
The flag identifier (string or id format)
- exception dispel.data.flags.FlagAlreadyExists[source]#
Bases:
Exception
Exception raised when the same flag is added twice to an entity.
- Parameters:
flag – The duplicated flag.
- class dispel.data.flags.FlagId[source]#
Bases:
DefinitionId
The identifier of an entity flag for a task.
- Parameters:
task_name – The name and abbreviation of the task. Note that if no abbreviation is provided the name is used directly in the id.
flag_name – The name of the flag and its abbreviation.
flag_type – The type of the flag. See
FlagType
.flag_severity – The severity of the flag. See
FlagSeverity
.
Notes
The abbreviations of values are passed using
AbbreviatedValue
. To generate the actual id the .abbr accessor is used. If one passes only strings, the class actually wraps those intoAbbreviatedValue
instances.Examples
>>> from dispel.data.values import AbbreviatedValue as AV >>> from dispel.data.flags import FlagId, FlagType >>> FlagId( ... task_name=AV('Cognitive Processing Speed', 'CPS'), ... flag_name=AV('tilt angle', 'ta'), ... flag_type=FlagType.BEHAVIORAL, ... flag_severity=FlagSeverity.DEVIATION, ... ) cps-behavioral-deviation-ta
- __init__(task_name, flag_name, flag_type, flag_severity)[source]#
- Parameters:
task_name (str | AbbreviatedValue) –
flag_name (str | AbbreviatedValue) –
flag_severity (str | FlagSeverity) –
- classmethod from_str(value)[source]#
Create a flag id from a string representation.
- Parameters:
value (str) – The string from which the flag id is to be constructed. It ought to respect the following format
<task_name>-<flag_type>-<flag_name>
where the flag type should one of the enumerations defined inFlagType
.- Returns:
The initialised flag identifier.
- Return type:
- Raises:
ValueError – If the flag string representation does not respect the required format.
- class dispel.data.flags.FlagMixIn[source]#
Bases:
ABC
Flag Mix-In class that groups multiple flags.
- add_flag(flag, ignore_duplicates=False)[source]#
Add a flag to the flag mix in.
- Parameters:
- Raises:
FlagAlreadyExists – If ignore_duplicates is False and the flag already exists.
- get_flags(flag_id=None)[source]#
Retrieve flags from the mix in.
- Parameters:
flag_id (str | FlagId | None) – The id corresponding to the flags that are to be retrieved. If
None
is provided, all flags will be returned.- Returns:
The flags corresponding to the given id.
- Return type:
List[Flag]
- Raises:
FlagNotFound – If the given flag id does not correspond to any flag in the mix-in.
- exception dispel.data.flags.FlagNotFound[source]#
Bases:
Exception
Exception raised when a flag is not found in the mix in.
- Parameters:
flag_id – The flag id.
entity – The flag Mix-In entity where the flag was not found.
message – An optional error message to be added at the end.
- class dispel.data.flags.FlagSeverity[source]#
Bases:
AVEnum
An enumeration for flag severity.
- DEVIATION = 1#
- INVALIDATION = 2#
- class dispel.data.flags.FlagType[source]#
Bases:
AVEnum
An enumeration for flag types.
- BEHAVIORAL = 2#
- TECHNICAL = 1#
- class dispel.data.flags.WrappedResult[source]#
Bases:
FlagMixIn
,Generic
[WrappedResultType
]A wrapped result to carry potential flags.
This class provides a convenient way to add flags to values from extract steps that are known to be invalid. This avoids having to write a separate flag step and is useful in cases where the information to flag a result is only accessible in the extract function.
- Parameters:
measure_value – The value of the measure returned by the extraction function.
- measure_value#
The value of the measure returned by the extraction function.
Examples
Assuming we wanted to flag measures directly inside a custom extraction function based on some metrics calculated, one can do
>>> from dispel.processing.extract import WrappedResult >>> from dispel.data.flags import Flag >>> from typing import Union >>> def custom_aggregation_func(data) -> Union[WrappedResult, float]: ... result = data.agg('mean') ... if len(data) < 3: ... inv = Flag( ... reason='Not enough data point', ... flag_severity=FlagSeverity.INVALIDATION ... ) ... result = WrappedResult(result, inv) ... return result
During processing, the class ExtractStep allows the transformation function to output
WrappedResult
objects. The extract step will automatically add any flags present in theWrappedResult
object to the measure value. TheWrappedResult
class supports basic operations with other scalars orWrappedResult
object:>>> from dispel.processing.extract import WrappedResult >>> res1 = WrappedResult(measure_value=1) >>> res2 = WrappedResult(measure_value=2) >>> melted_res = res1 + res2 >>> melted_res2 = res1 + 1
- dispel.data.flags.verify_flag_uniqueness(flags)[source]#
Check whether all provided flags are unique.
- Parameters:
flags (List[Flag]) – The flags whose uniqueness is to be verified.
- Raises:
FlagAlreadyExists – Per non-unique flag.