dispel.data.values module#
A module to handle values and their definitions.
- class dispel.data.values.AVEnum[source]#
Bases:
Enum
A base class for abbreviated value enumerations.
When extracting measures from tasks they are often done for specific modalities. This base class allows to do this in a convenient fashion to address modalities both from a processing and representation form. The enumeration is ordered.
Examples
Assuming you have a task that has two modalities, e.g. the Cognitive Processing Speed test has two forms: symbol-to-digit and digit-to-digit. The respective modalities class would look like:
enum >>> from dispel.data.values import AVEnum >>> class CPSTypeModality(AVEnum): ... SYMBOL_TO_DIGIT = ('symbol-to-digit', 'std') ... DIGIT_TO_DIGIT = ('digit-to-digit', 'dtd') ... >>> CPSTypeModality.SYMBOL_TO_DIGIT <CPSTypeModality.SYMBOL_TO_DIGIT: symbol-to-digit (std) [1]>
The constants can be used directly in
Series
as well as can be converted to an integer representation:enum >>> int(CPSTypeModality.SYMBOL_TO_DIGIT) 1
In order to conventiently pass the constants to modalities of
MeasureValueDefinition
andMeasureValueDefinitionPrototype
a property is exposed that contains theAbbreviatedValue
:enum >>> CPSTypeModality.SYMBOL_TO_DIGIT.av symbol-to-digit (std)
as well as for convenience the abbreviated value too:
enum >>> CPSTypeModality.SYMBOL_TO_DIGIT.abbr 'std'
Since the enumeration is odered, one can also perform comparisons between them:
enum >>> CPSTypeModality.DIGIT_TO_DIGIT < CPSTypeModality.SYMBOL_TO_DIGIT False
The member can also be retrieved from the abbreviation:
enum >>> CPSTypeModality.from_abbr('std') <CPSTypeModality.SYMBOL_TO_DIGIT: symbol-to-digit (std) [1]>
As well as from the variable name (case-insensitive):
enum >>> CPSTypeModality.from_variable('symbol_to_digit') <CPSTypeModality.SYMBOL_TO_DIGIT: symbol-to-digit (std) [1]>
- property abbr#
Get the abbreviated value.
- classmethod from_abbr(value)[source]#
Get the corresponding member from the abbreviated value.
- Parameters:
value (str) –
- classmethod from_variable(value)[source]#
Get the corresponding member from the variable name.
- Parameters:
value (str) –
- property variable#
Get the modality variable name.
- class dispel.data.values.AbbreviatedValue[source]#
Bases:
object
An abbreviated value.
Examples
This class allows to consistently handle abbreviated terms. Assuming you have a name of an assessment, e.g. Cognitive Processing Speed test and the respective abbreviation would be CPS, then you can create an abbreviated value like this:
>>> from dispel.data.values import AbbreviatedValue as AV >>> value = AV('Cognitive Processing Speed test', 'CPS') >>> value Cognitive Processing Speed test (CPS)
While this seems like a lot of overhead, it comes in handy when describing value definitions or higher-level abstractions, such as measure definitions.
- Parameters:
value – The full description of the value
abbr – The abbreviated form of the value
- value#
The full description of the value
- property abbr#
Get the abbreviated form of the value.
- classmethod wrap(value)[source]#
Wrap a value into an abbreviated value.
This is a small helper class to conveniently wrap values into an abbreviated value, if they are not already one.
- Parameters:
value – The value to be wrapped
- Returns:
The passed
value
if it is an instance ofAbbreviatedValue
. If a string is passed, then the string is passed asvalue
argument to the constructor.- Return type:
- Raises:
ValueError – If the passed value is neither a string nor an instance of
AbbreviatedValue
.
- class dispel.data.values.DefinitionId[source]#
Bases:
object
The definition of a measure id.
This class provides the basic functionality around ids used to reference columns and definitions. Other structured ids inherit from this class.
- Parameters:
id – The identifier of the definition.
- class dispel.data.values.Value[source]#
Bases:
object
A value with definition and actual value.
- Parameters:
definition – The definition of the value.
value – The actual value. If definition.precision is set, then the value will be rounded to the number of significant digits. The pre-rounded value is stored in raw_value.
- __init__(definition, value)[source]#
- Parameters:
definition (ValueDefinition) –
value (Any) –
- property id: DefinitionId#
Get the identifier from the definition of the value.
- class dispel.data.values.ValueDefinition[source]#
Bases:
object
The definition of a value.
- Parameters:
id – The identifier of the value definition
name – The human-readable name of the values
unit – The unit of the value
description – A more elaborate description of the values and how they were produced
data_type – The numpy data type of the value in question
validator – A function that ensures values comply with the definition. The module
validators
contains validators for common scenarios that can be used here.precision – The number of significance for the values expected under definition. If set, the value will be rounded to the set number of digits.
- __init__(id_, name, unit=None, description=None, data_type=None, validator=None, precision=None)[source]#
- derive(**kwargs)[source]#
Derive a value definition with updated properties.
- Parameters:
kwargs – Keyword arguments to be set/updated in the derived definition.
- Returns:
A new definition with updated parameters.
- Return type:
- Raises:
ValueError – If one of the provided arguments is not a parameter of the constructor.
- class dispel.data.values.ValueDefinitionPrototype[source]#
Bases:
object
The prototype of a
ValueDefinition
.Measure processing often leads to various related measures. To ease the creation of such, the
ValueDefinitionPrototype
allows to specify prototypic measures that can be used to derive actual definitions.- Parameters:
cls – The class to be used when creating concrete instances with
create_definition()
. By default, the class used is :class:ValueDefinition`.kwargs – All named parameters passed to the constructor will be passed to the measure definition class constructor. The parameter
cls
is reserved to pass a different measure definition class. Placeholders to be filled upon creation are specified with curly brackets, i.e.,a {placeholder} value
is populated when callingprototype.create_definition(placeholder='special')
.
Examples
Assuming we want to create a measure for different time windows one can create the following prototype:
>>> from dispel.data.values import ValueDefinitionPrototype >>> prototype = ValueDefinitionPrototype( ... id_='measure-{lower}-{upper}', ... name='measure from {lower} to {upper}', ... unit='s' ... ) >>> prototype.create_definition(lower=5, upper=6) <ValueDefinition: measure-5-6 (measure from 5 to 6, s)> >>> prototype.create_definition(lower=1, upper=5) <ValueDefinition: measure-1-5 (measure from 1 to 5, s)>
- create_definition(**values)[source]#
Create a definition from this prototype.
- Parameters:
values (Any) – The arguments and placeholders to be populated. All named arguments will be used to both provide additional named arguments to the measure definition class specified with
cls
during construction upon creation (the class is inspected for named parameters) and placeholders provided during construction of the prototype.- Returns:
The value definition created from the value definition prototype.
- Return type:
Examples
An example is given above to populate placeholders. This is also possible with arguments required by the definition class:
>>> from dispel.data.values import ValueDefinitionPrototype >>> prototype = ValueDefinitionPrototype(unit='s') >>> prototype.create_definition(id_='foo', name='bar') <ValueDefinition: foo (bar, s)> >>> prototype.create_definition(id_='baz', name='bam') <ValueDefinition: baz (bam, s)>
- Raises:
ValueError – If a placeholder is missing from kwargs.
- Parameters:
values (Any) –
- Return type:
- create_definitions(items)[source]#
Create multiple definitions.
This method provides a convenient way to specify multiple definitions at the same time for an iterable of dictionaries that are passed to
create_definition()
.- Parameters:
items (Iterable[Dict[str, Any]]) – An iterable of dictionaries passed to
create_definition()
.- Returns:
A list of the created value definitions.
- Return type:
List[ValueDefinition]
- class dispel.data.values.ValueSet[source]#
Bases:
object
A collection of multiple values.
- Parameters:
values – The values of the value set. This can be a list of
Value
s or a list of any value.definitions – An optional list of definitions describing the passed values through the parameter
values
.
Notes
The constructor passes both
values
anddefinitions
toValueSet.set_values()
. For details on how to specify values of theValueSet
please have a look there.- __init__(values=None, definitions=None)[source]#
- Parameters:
definitions (List[ValueDefinition] | None) –
- definitions()[source]#
Get all definitions of the set.
- Returns:
An iterable of all value definitions from all values of the set.
- Return type:
Iterable[ValueDefinition]
- get(id_)[source]#
Get a value for an id.
- Parameters:
id – The id or definition for which to obtain the value.
id_ (DefinitionId | str | ValueDefinition) –
- Returns:
The respective
Value
matching the providedid_
.- Return type:
- Raises:
KeyError – If the provided
id_
is not present in the set.
- get_definition(id_)[source]#
Get the definition of a value by its id.
- Parameters:
id – The id for which to obtain the definition
id_ (DefinitionId | str | ValueDefinition) –
- Returns:
The definition belonging to the passed id.
- Return type:
- get_raw_value(id_)[source]#
Get the raw value for an id.
This is a convenience method to not have to call
value_set.get(id).value
.- Parameters:
id – The id or definition for which to retrieve the raw value.
id_ (DefinitionId | str | ValueDefinition) –
- Returns:
The raw value of the
Value
matching the passed id or definition.- Return type:
Any
- has_value(id_)[source]#
Test if the set has a specific value.
- Parameters:
id – The id or definition for which to lookup if a value is present
id_ (DefinitionId | str | ValueDefinition) –
- Returns:
True
if the value set contains a value for the providedid_
. Otherwise,False
.- Return type:
- Raises:
TypeError – If the id is neither a
str
,DefinitionId
nor aValueDefinition
.
- ids()[source]#
Get all ids of the set.
- Returns:
An iterable of all definition ids from all values of the set.
- Return type:
Iterable[DefinitionId]
- set(value, definition=None, overwrite=False)[source]#
Set a value in the value set.
- Parameters:
value (Any) – The value to be set. If the value is not an instance of
Value
one needs to also provide adefinition
.definition (ValueDefinition | None) – An optional definition of the passed value should the value not be an instance of
Value
.overwrite (bool) – By default values in the
ValueSet
are not overwritten. If you want to update an already set value you will need to setoverwrite = True
.
- Raises:
ValueError – If
value
is not aValue
and no definition is passed.ValueError – If
value
’s id is already present in the Value Set, and theoverwrite
argument is set toFalse
.
- set_values(values, definitions=None, overwrite=True)[source]#
Set multiple values in the value set.
- Parameters:
values (List[Any]) – The values to be set. If the values are not an instance of
Value
the optional parameterdefinitions
needs to be provided with a list ofValueDefinition
describing each value invalues
.definitions (List[ValueDefinition] | None) – An optional list of definitions for values passed via
values
. Bothvalues
anddefinitions
need to be of equal length.overwrite (bool) – The overwrite-behavior. See
ValueSet.set()
for details.
- Raises:
ValueError – If
values
anddefinitions
are not of equal length.