dcc_quantities.dcc_quantity_type

Module containing all the logic regarding the DccQuantityType class.

The class DccQuantityType is a generic container for quantity data, designed to deserialize data directly from the XML tables.

DccQuantityType

DccQuantityType(
    si_data: SiDataListType,
    identifier: str | None = None,
    ref_id: list[str] | None = None,
    ref_type: list[str] | None = None,
    name: DccLangName | None = None,
    description: DccLangDescription | None = None,
    relative_uncertainty: str | None = None,
    used_methods: dict | None = None,
    used_software: dict | None = None,
    measuring_equipments: dict | None = None,
    measurement_meta_data: dict | None = None,
    index: str | None = None,
)

Bases: BaseQuantitySerializer

Element to represent Quantities defined by the dcc:quantity tag.

The class can be imported directly from the dcc_quantities package:

from dcc_quantities import DccQuantityType

Parameters:
  • si_data (SiDataListType) –

    Numerical quantity data. It contains all the values, uncertainties and D-SI unit corresponding to the DCC quantity. Currently only SiRealList is supported as data.

  • identifier (str | None, default: None ) –

    Unique string identifier for the quantity. If not defined, the identifier is initialized to a unique UUID value.

  • name (DccLangName | None, default: None ) –

    Mapping from the supported languages to the names (in those languages) that were given to the table.

  • description (DccLangDescription | None, default: None ) –

    Mapping, analogous to 'name', where each value corresponds to the same description in the specified language by the key.

  • index (str | None, default: None ) –

    Numerical index corresponding to the order of the Index Quantities. This parameter must be None for any Value Quantity. The value of index cannot be repeated among different Index Quantities.

values property

values: np.ndarray

All values without the uncertainties of the quantity data as a numpy array.

uncertainties property

uncertainties: np.ndarray

All uncertainties of the quantity data as a numpy array.

dsi_unit property

dsi_unit: DsiUnit

Unit that is defined for the quantity data.

is_top_level property

is_top_level: bool

Whether the Quantity is set at the top level of the table.

Examples:

Considering a table with two quantities, one within the dcc:measurementMetaData field:

<dcc:list tableDimension=1>
    <dcc:quantity>
        <dcc:name>"Index0 quantity"</dcc:name>
        ...
    </dcc:quantity>
    <dcc:quantity>
        <dcc:name>"Value0 quantity"</dcc:name>
        ...
    </dcc:quantity>
    <dcc:measurementMetaData>
        <dcc:quantity>
            <dcc:name>"Value1 quantity"</dcc:name>
            ...
        </dcc:quantity>
    </dcc:measurementMetaData>
</dcc:list>
The quantities named 'Index0 quantity' and 'Value0 quantity' at the example are placed at the top level of the table. The quantity named 'Value1 quantity' is not.

unique_id property

unique_id: str

Unique ID (or UUID) referencing the element.

from_dcc_data classmethod

from_dcc_data(dcc_data: dict) -> DccQuantityType

Creates a DccQuantityType instance from a dictionary representation.

This method exists for the purpose to a quick quantity initialization in the cases the information is extracted from a DCC file. Thus, it is not expected for anyone to manually create the dictionary that composes dcc_data.

Parameters:
  • dcc_data (dict) –

    Dictionary representation of a <dcc:quantity> block from the original DCC file.

from_single_quantity_value classmethod

from_single_quantity_value(
    name: str | DccLangName,
    value: int | float,
    uncertainty: int | float | None,
    unit: str | DsiUnit,
    label: str | None = None,
) -> DccQuantityType

Creating a DccQuantity instance with a single value.

Parameters:
  • name (str or DccLangName) –

    Name of the Quantity to be created, specified as: - A single string in English - A dictionary {lang: name} with as many entries as desired

  • value (number) –

    The value related with the Quantity.

  • uncertainty (number) –

    The uncertainty correlated with the value. It can also take the value None for undefined uncertainties.

  • unit (str or DsiUnit) –

    The unit of the quantity, specified as the DsiUnit rules.

  • label (str, default: None ) –

    Used label to identify the data. This label can be either a word, a character or a symbol.

extract_all_quantities classmethod

extract_all_quantities(dcc_data: dict | list) -> list[DccQuantityType]

Finds all quantities (recursively) provided some DCC data.

set_label

set_label(label: str)

Updates the field 'label' used for the quantity data.

Parameters:
  • label (str) –

    New label to override the previous one. This is usually represented with a letter (like 'f' for 'Frequency') or a symbol (Unicode character) like 'λ'.

where

where(condition: Callable) -> np.ndarray[bool]

Wrapper over SiDataListType.where().

set_unique_id

set_unique_id(new_id: str)

Updates the unique identifier of the table.

Warning

The identifier must be unique among any other element (table, quantity, etc.). dccQuantities does not perform an in-depth search of whether the new ID is truly unique among all elements. This is the responsibility of the user.

set_name

set_name(name: str | dict, language: str | None = None)

Updates the field 'name' used for the data.

This function overwrites any existing name(s) at the specified language(s), while keeping all other existing names.

Parameters:
  • name (str | dict) –

    String specifying the new name to add or update from the DccLangName instance containing all names. It can also be a dictionary following the structure {language: name}, where:

    • `language` follows the same rules as the `language` parameter at this method.
    • The dictionary can contain as many entries as required.
  • language (str, default: None ) –

    Two letters in lowercase indicating the language of the name. E.G.: 'en' for 'English' or 'de' for 'Deutsch'. This parameter is ignored when name is a dictionary, but must be provided when name is a string.

export_as_xml_structure

export_as_xml_structure() -> str

Exports the current table as an XML string compliant with the DCC-schema.

Built-in Operators

Length: len(q)

Returns the number of elements (values) contained within the quantity data.

>>> q = DccQuantityType(data=SiRealList([1, 2, 3], ...), ...)
>>> len(q)
3

Math operators

Each DccQuantityType instance can perform math operations (through built-in operators like +, *, /, etc.) providing as a result a new DccQuantityType instance. The provided result presents propagated uncertainties, calculated with the help of metas_unclib_python_wrapper, as well as the newly calculated D-SI unit.

Supported Math

  • Sum DccQuantityType + other and other + DccQuantityType
  • Subtraction DccQuantityType - other and other - DccQuantityType
  • Multiplication DccQuantityType * other and other * DccQuantityType
  • Division DccQuantityType / other and other / DccQuantityType
  • Power DccQuantityType ** other
  • Value comparison DccQuantityType == other
  • Negation -DccQuantityType

Note

Any new DccQuantityType instance is always generated with the fields 'name' and 'label' empty. Make sure to initialize them with the methods DccQuantityType.set_name and DccQuantityType.set_label.

Addition: +

Subtraction: -

Support for addition quant + other and subtraction quant - other operations. Each operation returns a new DccQuantityType instance with the updated quantity data.

>>> from dcc_quantities import DccQuantityType, SiRealList

>>> q1 = DccQuantityType(si_data=SiRealList(data=[1, 2, 3], unit="\\metre", label="Q1"), name={"en": "First distance"})
>>> q2 = DccQuantityType(si_data=SiRealList(data=[4, 5, 6], unit="\\metre", label="Q2"), name={"en": "Second distance"})

>>> q3 = q1 + q2
>>> q3.set_name("Sum of distances", "en")
>>> q3.set_label("Q3")

>>> print(q3.values)
[5, 7, 9]
>>> print(q3.dsi_unit)
'\metre'

Tip

Only quantities with the same base D-SI unit can be added. E.G.: \centi\metre + \metre is possible, while \centi\metre + \newton is not.

Negation: -q

Support for unary negation operator -. This operator allows to obtain -quant, where all the quantity data is negated.

>>> from dcc_quantities import DccQuantityType, SiRealList

>>> q1 = DccQuantityType(SiRealList(data=[1, -2, 3], unit="\\metre", label="Q1"), name={"en": "Distance"})

>>> q2 = -q1
>>> q2.set_name("Negated distance", "en")
>>> q2.set_label("Q2")

>>> print(q2.values)
[-1, 2, -3]
>>> print(q2.dsi_unit)
'\metre'

Multiplication: *

Support for multiplications quant * other and other * quant. A quantity can be multiplied by:

  • Another quantity, where the resulting quantity is defined by the unit multiplication of both quantities.
  • A scalar, where the result quantity has different values at the quantity data but the same original unit.
  • Any array like with the same size as the quantity data, applying item per item multiplication at the quantity data.
>>> from dcc_quantities import DccQuantityType, SiRealList

>>> q1 = DccQuantityType(SiRealList(data=[1, 2, 3], unit="\\metre", label="Q1"), name={"en": "First distance"})
>>> q2 = DccQuantityType(SiRealList(data=[4, 5, 6], unit="\\metre", label="Q2"), name={"en": "Second distance"})

>>> # Case: Quantities multiplication
... q3 = q1 * q2
>>> q3.set_label("Q3")
>>> q3.set_name("Multiplied distances", "en")

>>> print(q3.values)
[4, 10, 18]
>>> print(q3.dsi_unit)  
'\metre\tothe{2}'

>>> # Case: Quantity by scalar
... q4 = q1 * 2
>>> q4.set_label("Q4")
>>> q4.set_name("Distance times scalar", "en")

>>> print(q4.values)
[1, 4, 6]
>>> print(q4.dsi_unit)
'\metre'

>>> # Case: Quantity by array
... import numpy as np
>>> q5 = q1 * np.array([4, 5, 6])
>>> q5.set_name("Quantity multiplied by array of same length", "en")
>>> q5.set_label("Q5")

>>> print(q5.values)
[4, 10, 18]
>>> print(q5.dsi_unit)
'\metre'

Division: /

Support for dividing a quantity over a target as quant / T and the inverse, a target over a quantity T / quant. The rules that the division follow are the same three as the multiplication, returning then a result quantity with updated quantity data and D-SI unit.

>>> from dcc_quantities import DccQuantityType, SiRealList

>>> q1 = DccQuantityType(SiRealList(data=[2, 4, 6], unit="\\metre", label="d"), name={"en": "Distance"})
>>> q2 = DccQuantityType(SiRealList(data=[2], unit="\\second", label="t"), name={"en", "Time"})

>>> q3 = q1 / q2
>>> q3.set_name("Velocity", "en")
>>> q3.set_label("V")

>>> print(q3.values)
[1, 2, 3]
>>> print(q3.dsi_unit)
'\metre\per\second'

Power: **

Support the operation of a quantity powered to a target quant ** exponent. The power operator is supported only for:

  • The target being a scalar.
  • The target being another quantity with adimensional D-SI units.

Warning

The instance SiComplexList to support complex values is not yet implemented. Any result with complex values will raise a NotImplementedError. These results most likely are produced by a negative value powered to a fraction.