Skip to content

fl_server_core.models.metric

Classes:

Name Description
Metric

Metric model class.

Classes

Metric

Bases: Model


              flowchart TD
              fl_server_core.models.metric.Metric[Metric]

              

              click fl_server_core.models.metric.Metric href "" "fl_server_core.models.metric.Metric"
            

Metric model class.

Methods:

Name Description
is_binary

Check if the value of the metric is binary.

is_float

Check if the value of the metric is a float.

to_torch

Convert the binary value of the metric to a torch module or tensor.

Attributes:

Name Type Description
identifier CharField

Identifier of the metric.

key CharField

Key of the metric.

model ForeignKey

Model associated with the metric.

reporter ForeignKey

User who reported the metric.

step IntegerField

Step of the metric.

value float | bytes

Value of the metric.

value_binary BinaryField

Binary value of the metric.

value_float FloatField

Float value of the metric.

Source code in fl_server_core/models/metric.py
class Metric(models.Model):
    """
    Metric model class.
    """

    model: ForeignKey = ForeignKey(Model, on_delete=CASCADE)
    """Model associated with the metric."""
    identifier: CharField = CharField(max_length=64, null=True, blank=True)
    """Identifier of the metric."""
    key: CharField = CharField(max_length=32)
    """Key of the metric."""
    value_float: FloatField = FloatField(null=True, blank=True)
    """Float value of the metric."""
    value_binary: BinaryField = BinaryField(null=True, blank=True)
    """Binary value of the metric."""
    step: IntegerField = IntegerField(null=True, blank=True)
    """Step of the metric."""
    reporter: ForeignKey = ForeignKey(User, null=True, blank=True, on_delete=CASCADE)
    """User who reported the metric."""

    @property
    def value(self) -> float | bytes:
        """
        Value of the metric.

        Returns:
            float | bytes: The value of the metric.
        """
        if self.is_float():
            return self.value_float
        return self.value_binary

    @value.setter
    def value(self, value: float | int | bytes | Module | Tensor):
        """
        Setter for the value of the metric.

        Args:
            value (float | int | bytes | Module | Tensor): The value to set.
        """
        if isinstance(value, float):
            self.value_float = value
        elif isinstance(value, int):
            self.value_float = float(value)
        elif isinstance(value, (Module, Tensor)):
            self.value_binary = from_torch_module_or_tensor(value)
        else:
            self.value_binary = value

    @value.deleter
    def value(self):
        """
        Deleter for the value of the metric.
        """
        self.value_float = None
        self.value_binary = None

    def is_float(self) -> bool:
        """
        Check if the value of the metric is a float.

        Returns:
            bool: `True` if the value of the metric is a float, otherwise `False`.
        """
        return self.value_float is not None

    def is_binary(self) -> bool:
        """
        Check if the value of the metric is binary.

        Returns:
            bool: `True` if the value of the metric is binary, otherwise `False`.
        """
        return self.value_binary is not None

    def to_torch(self) -> Module | Tensor:
        """
        Convert the binary value of the metric to a torch module or tensor.

        Returns:
            Module | Tensor: The converted torch module or tensor.
        """
        return to_torch_module_or_tensor(self.value_binary)

Attributes

identifier class-attribute instance-attribute
identifier: CharField = CharField(max_length=64, null=True, blank=True)

Identifier of the metric.

key class-attribute instance-attribute
key: CharField = CharField(max_length=32)

Key of the metric.

model class-attribute instance-attribute
model: ForeignKey = ForeignKey(Model, on_delete=CASCADE)

Model associated with the metric.

reporter class-attribute instance-attribute
reporter: ForeignKey = ForeignKey(User, null=True, blank=True, on_delete=CASCADE)

User who reported the metric.

step class-attribute instance-attribute
step: IntegerField = IntegerField(null=True, blank=True)

Step of the metric.

value deletable property writable
value: float | bytes

Value of the metric.

Returns:

Type Description
float | bytes

float | bytes: The value of the metric.

value_binary class-attribute instance-attribute
value_binary: BinaryField = BinaryField(null=True, blank=True)

Binary value of the metric.

value_float class-attribute instance-attribute
value_float: FloatField = FloatField(null=True, blank=True)

Float value of the metric.

Functions

is_binary
is_binary() -> bool

Check if the value of the metric is binary.

Returns:

Name Type Description
bool bool

True if the value of the metric is binary, otherwise False.

Source code in fl_server_core/models/metric.py
def is_binary(self) -> bool:
    """
    Check if the value of the metric is binary.

    Returns:
        bool: `True` if the value of the metric is binary, otherwise `False`.
    """
    return self.value_binary is not None
is_float
is_float() -> bool

Check if the value of the metric is a float.

Returns:

Name Type Description
bool bool

True if the value of the metric is a float, otherwise False.

Source code in fl_server_core/models/metric.py
def is_float(self) -> bool:
    """
    Check if the value of the metric is a float.

    Returns:
        bool: `True` if the value of the metric is a float, otherwise `False`.
    """
    return self.value_float is not None
to_torch
to_torch() -> Module | Tensor

Convert the binary value of the metric to a torch module or tensor.

Returns:

Type Description
Module | Tensor

Module | Tensor: The converted torch module or tensor.

Source code in fl_server_core/models/metric.py
def to_torch(self) -> Module | Tensor:
    """
    Convert the binary value of the metric to a torch module or tensor.

    Returns:
        Module | Tensor: The converted torch module or tensor.
    """
    return to_torch_module_or_tensor(self.value_binary)

Functions