Skip to content

opensampl.metrics

Functions and objects for managing openSAMPL Metric Types

METRICS

Class for storing metric types

Source code in opensampl/metrics.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class METRICS:
    """Class for storing metric types"""

    # --- SUPPORTED METRICS ----
    PHASE_OFFSET = MetricType(
        name="Phase Offset",
        description="Difference in seconds between the probe's time reading and the reference time reading",
        unit="s",
        value_type=float,
    )
    EB_NO = MetricType(
        name="Eb/No",
        description=(
            "Energy per bit to noise power spectral density ratio measured at the clock probe. "
            "Indicates the quality of the received signal relative to noise."
        ),
        unit="dB",
        value_type=float,
    )
    UNKNOWN = MetricType(
        name="UNKNOWN",
        description="Unknown or unspecified metric type, with value_type of jsonb due to flexibility",
        unit="unknown",
        value_type=object,
    )

MetricType

Bases: BaseModel

Object for defining different metric types

Source code in opensampl/metrics.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class MetricType(BaseModel):
    """Object for defining different metric types"""

    name: str
    description: str
    unit: str
    value_type: type

    def convert_to_type(self, value: Any) -> Any:
        """Convert a given value to the expected type for the Metric"""
        return self.value_type(value)

    @field_serializer("value_type")
    def serialize_type(self, value: type):
        """Return the name of value_type for serializing"""
        return value.__name__

    @field_validator("value_type", mode="before")
    @classmethod
    def validate_type(cls, value: Union[str, type]) -> Any:
        """Ensure the value_type field is converted to a type if provided as a string"""
        if isinstance(value, str):
            value = value.strip()
            if value in type_map:
                return type_map[value]
        return value

convert_to_type(value)

Convert a given value to the expected type for the Metric

Source code in opensampl/metrics.py
18
19
20
def convert_to_type(self, value: Any) -> Any:
    """Convert a given value to the expected type for the Metric"""
    return self.value_type(value)

serialize_type(value)

Return the name of value_type for serializing

Source code in opensampl/metrics.py
22
23
24
25
@field_serializer("value_type")
def serialize_type(self, value: type):
    """Return the name of value_type for serializing"""
    return value.__name__

validate_type(value) classmethod

Ensure the value_type field is converted to a type if provided as a string

Source code in opensampl/metrics.py
27
28
29
30
31
32
33
34
35
@field_validator("value_type", mode="before")
@classmethod
def validate_type(cls, value: Union[str, type]) -> Any:
    """Ensure the value_type field is converted to a type if provided as a string"""
    if isinstance(value, str):
        value = value.strip()
        if value in type_map:
            return type_map[value]
    return value