Skip to content

improve: support pydantic 2 #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions datastream/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
from datastream.dataset import Dataset
from datastream.datastream import Datastream

from pkg_resources import get_distribution, DistributionNotFound

try:
__version__ = get_distribution("pytorch-datastream").version
except DistributionNotFound:
pass
33 changes: 7 additions & 26 deletions datastream/datastream.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
from __future__ import annotations
from pydantic import BaseModel, PositiveInt
from typing import (
Tuple,
Dict,
List,
Callable,
Optional,
TypeVar,
Generic,
Union,
)

from typing import Callable, Dict, Generic, List, Optional, Tuple, TypeVar, Union

import numpy as np
import torch
from pathlib import Path
from pydantic import BaseModel, PositiveInt

from datastream import Dataset
from datastream.samplers import (
StandardSampler,
MergeSampler,
ZipSampler,
MultiSampler,
RepeatSampler,
StandardSampler,
ZipSampler,
)


T = TypeVar("T")
R = TypeVar("R")

Expand All @@ -46,7 +37,7 @@ class Datastream(BaseModel, Generic[T]):
16
"""

dataset: Dataset[T]
dataset: Dataset
sampler: Optional[torch.utils.data.Sampler]

class Config:
Expand Down Expand Up @@ -286,29 +277,25 @@ def cache(


def test_infinite():

datastream = Datastream(Dataset.from_subscriptable(list("abc")))
it = iter(datastream.data_loader(batch_size=8, n_batches_per_epoch=10))
for _ in range(10):
batch = next(it)


def test_iter():

datastream = Datastream(Dataset.from_subscriptable(list("abc")))
assert len(list(datastream)) == 3


def test_empty():

import pytest

with pytest.raises(ValueError):
Datastream(Dataset.from_subscriptable(list()))


def test_datastream_merge():

datastream = Datastream.merge(
[
Datastream(Dataset.from_subscriptable(list("abc"))),
Expand All @@ -328,7 +315,6 @@ def test_datastream_merge():


def test_datastream_zip():

datasets = [
Dataset.from_subscriptable([1, 2]),
Dataset.from_subscriptable([3, 4, 5]),
Expand Down Expand Up @@ -384,7 +370,6 @@ def ZippedMergedDatastream():


def test_datastream_simple_weights():

dataset = Dataset.from_subscriptable([1, 2, 3, 4])
datastream = (
Datastream(dataset)
Expand Down Expand Up @@ -412,7 +397,6 @@ def test_datastream_simple_weights():


def test_merge_datastream_weights():

datasets = [
Dataset.from_subscriptable([1, 2]),
Dataset.from_subscriptable([3, 4, 5]),
Expand Down Expand Up @@ -441,7 +425,6 @@ def test_merge_datastream_weights():


def test_multi_sample():

data = [1, 2, 4]
n_multi_sample = 2

Expand Down Expand Up @@ -475,7 +458,6 @@ def test_multi_sample():


def test_take():

import pytest

datastream = Datastream(Dataset.from_subscriptable(list("abc"))).take(2)
Expand All @@ -494,7 +476,6 @@ def test_take():


def test_sequential_sampler():

from datastream.samplers import SequentialSampler

dataset = Dataset.from_subscriptable(list("abc"))
Expand Down
13 changes: 9 additions & 4 deletions datastream/samplers/merge_sampler.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from __future__ import annotations
from pydantic import BaseModel
from typing import Tuple, Callable, Iterable

from functools import partial
from itertools import chain, islice
from typing import Callable, Iterable, Tuple

import torch
from datastream.tools import repeat_map_chain
from pydantic import BaseModel

from datastream import Dataset
from datastream.tools import repeat_map_chain


class MergeSampler(BaseModel, torch.utils.data.Sampler):
Expand Down Expand Up @@ -39,7 +42,9 @@ def __iter__(self):

@staticmethod
def merged_samplers_length(samplers, ns):
return min([len(sampler) / n for sampler, n in zip(samplers, ns)]) * sum(ns)
return int(
min([len(sampler) / n for sampler, n in zip(samplers, ns)]) * sum(ns)
)

@staticmethod
def merge_samplers(samplers, datasets, ns):
Expand Down
Loading