52 lines
1.4 KiB
Python
Raw Normal View History

2024-10-30 20:22:09 -03:00
import random
from abc import ABC, abstractmethod
from dataclasses import dataclass
2024-10-30 20:22:09 -03:00
from pathlib import Path
2024-11-01 18:07:08 -03:00
from typing import Callable, Iterator, Tuple
2024-10-30 20:22:09 -03:00
from typing_extensions import Generic
from benchmarks.core.network import TInitialMetadata
2024-10-30 20:22:09 -03:00
# A Sampler samples without replacement from [0, ..., n].
2024-11-01 18:07:08 -03:00
type Sampler = Callable[[int], Iterator[int]]
2024-10-30 20:22:09 -03:00
@dataclass
class DataHandle(Generic[TInitialMetadata], ABC):
"""A :class:`DataHandle` knows how to clean up data and metadata that has been generated
by a :class:`DataGenerator`."""
meta: TInitialMetadata
data: Path
def cleanup(self):
if self.data.exists():
self.data.unlink()
class DataGenerator(Generic[TInitialMetadata], ABC):
"""A :class:`DataGenerator` knows how to generate data for an :class:`Experiment`."""
@abstractmethod
def generate(self) -> DataHandle[TInitialMetadata]:
"""Generates fresh data and metadata and returns a :class:`DataHandle`."""
pass
2024-10-30 20:22:09 -03:00
def sample(n: int) -> Iterator[int]:
2024-11-01 18:07:08 -03:00
"""Samples without replacement using a basic Fisher-Yates shuffle."""
2024-10-30 20:22:09 -03:00
p = list(range(0, n))
for i in range(n - 1):
j = i + random.randint(0, n - i)
tmp = p[j]
p[j], p[j + 1] = p[j + 1], tmp
yield p[i]
2024-11-01 18:07:08 -03:00
def kilobytes(n: int) -> int:
return n * 1024
def megabytes(n: int) -> int:
return kilobytes(n) * 1024