Skip to content

pixano.data.dataset.dataset_stat

DatasetStat(**data)

Bases: BaseModel

DatasetStat

Attributes:

Name Type Description
name str

Stats name

type str

Stats type ('numerical' or 'categorical')

histogram str

Stats histogram data

Raises 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.

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.',
            category=None,
        )

from_json(json_fp) staticmethod

Read list of DatasetStats from JSON file

Parameters:

Name Type Description Default
json_fp Path | S3Path

JSON file path

required

Returns:

Type Description
list[DatasetStats]

List of DatasetStat

Source code in pixano/data/dataset/dataset_stat.py
@staticmethod
def from_json(json_fp: Path | S3Path) -> list["DatasetStat"]:
    """Read list of DatasetStats from JSON file

    Args:
        json_fp (Path | S3Path): JSON file path

    Returns:
        list[DatasetStats]: List of DatasetStat
    """

    if isinstance(json_fp, S3Path):
        with json_fp.open(encoding="utf-8") as json_file:
            stats_json = json.load(json_file)
    else:
        with open(json_fp, encoding="utf-8") as json_file:
            stats_json = json.load(json_file)

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

save(save_dir)

Save DatasetInfo to json file replace existing histogram with same name in json_fp

Parameters:

Name Type Description Default
save_dir Path | S3Path

Save directory

required
Source code in pixano/data/dataset/dataset_stat.py
def save(self, save_dir: Path | S3Path):
    """Save DatasetInfo to json file
       replace existing histogram with same name in json_fp

    Args:
        save_dir (Path | S3Path): Save directory
    """

    try:
        if isinstance(save_dir, S3Path):
            with (save_dir / "stats.json").open(encoding="utf-8") as json_file:
                json_stats = json.load(json_file)
        else:
            with open(save_dir / "stats.json", "r", encoding="utf-8") as json_file:
                json_stats = json.load(json_file)
    except FileNotFoundError:
        json_stats = []
    # keep all stats except the one with same name, we replace it if exist
    json_stats = [stat for stat in json_stats if stat["name"] != self.name]
    json_stats.append(
        {"name": self.name, "type": self.type, "histogram": self.histogram}
    )

    if isinstance(save_dir, S3Path):
        with (save_dir / "stats.json").open("w", encoding="utf-8") as f:
            json.dump(json_stats, f, indent="\t")
    else:
        with open(save_dir / "stats.json", "w", encoding="utf-8") as f:
            json.dump(json_stats, f, indent="\t")