Skip to content

pixano.datasets.dataset_stat

DatasetStatistic(**data)

Bases: BaseModel

A statistic of a dataset.

Attributes:

Name Type Description
name str

The name of the statistic.

type str

The type ('numerical' or 'categorical') of the statistic.

histogram list[dict[str, float | int | str]]

The histogram of the statistic.

range list[int | float] | None

The range of the statistic.

Source code in pydantic/main.py
def __init__(self, /, **data: Any) -> None:
    """Create a new model by parsing and validating input data from keyword arguments.

    Raises [`ValidationError`][pydantic_core.ValidationError] if the input data cannot be
    validated to form a valid model.

    `self` is explicitly positional-only to allow `self` as a field name.
    """
    # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
    __tracebackhide__ = True
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
    if self is not validated_self:
        warnings.warn(
            'A custom validator is returning a value other than `self`.\n'
            "Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.\n"
            'See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.',
            stacklevel=2,
        )

from_json(json_fp) staticmethod

Read list of DatasetStatistic from a JSON file.

Parameters:

Name Type Description Default
json_fp Path

JSON file path.

required

Returns:

Type Description
list[DatasetStatistic]

A list of DatasetStat.

Source code in pixano/datasets/dataset_stat.py
@staticmethod
def from_json(json_fp: Path) -> list["DatasetStatistic"]:
    """Read list of `DatasetStatistic` from a JSON file.

    Args:
        json_fp: JSON file path.

    Returns:
        A list of `DatasetStat`.
    """
    stats_json = json.loads(json_fp.read_text(encoding="utf-8"))

    return [DatasetStatistic.model_validate(stat) for stat in stats_json]

to_json(json_fp)

Save DatasetStatistic to a json file.

Replace the existing histogram in json_fp.

Parameters:

Name Type Description Default
json_fp Path

Save directory.

required
Source code in pixano/datasets/dataset_stat.py
def to_json(self, json_fp: Path) -> None:
    """Save `DatasetStatistic` to a json file.

    Replace the existing histogram in `json_fp`.

    Args:
        json_fp: Save directory.
    """
    try:
        stats_json = json.loads(json_fp.read_text(encoding="utf-8"))
    except FileNotFoundError:
        stats_json = []
    # keep all stats except the one with same name, we replace it if exist
    stats_json = [stat for stat in stats_json if stat["name"] != self.name]
    stats_json.append({"name": self.name, "type": self.type, "histogram": self.histogram, "range": self.range})

    json_fp.write_text(json.dumps(stats_json, indent=4), encoding="utf-8")