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(
data: SiRealList,
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: ExplicitSerializerMixin
Represents the <dcc:quantity> element, serialized in the exact order required by the
XSD <quantityType> sequence.
The class can be imported directly from the dcc_quantities package:
from dcc_quantities import DccQuantityType
| Parameters: |
|
|---|
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.
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: |
|
|---|
set_label ¶
set_label(label: str)
Updates the field 'label' used for the quantity data.
| Parameters: |
|
|---|
set_name ¶
set_name(name: str | dict, language: str | None = None)
Updates the field 'name' used for the quantity data.
This function overwrites any existing name(s) at the specified language(s), while keeping all other existing names.
| Parameters: |
|
|---|
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 + otherandother + DccQuantityType - Subtraction
DccQuantityType - otherandother - DccQuantityType - Multiplication
DccQuantityType * otherandother * DccQuantityType - Division
DccQuantityType / otherandother / 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(data=SiRealList(data=[1, 2, 3], unit="\\metre", label="Q1"), name={"en": "First distance"})
>>> q2 = DccQuantityType(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.