Skip to content
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

Increase resolution of train_test_split #611

Merged
merged 8 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 6 additions & 2 deletions src/datachain/toolkit/split.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ def train_test_split(dc: DataChain, weights: list[float]) -> list[DataChain]:

weights_normalized = [weight / sum(weights) for weight in weights]

resolution = 2**31 - 1 # Maximum positive value for a 32-bit signed integer.
0x2b3bfa0 marked this conversation as resolved.
Show resolved Hide resolved
0x2b3bfa0 marked this conversation as resolved.
Show resolved Hide resolved

return [
dc.filter(
C("sys__rand") % 1000 >= round(sum(weights_normalized[:index]) * 1000),
C("sys__rand") % 1000 < round(sum(weights_normalized[: index + 1]) * 1000),
C("sys__rand") % resolution
>= round(sum(weights_normalized[:index]) * resolution),
C("sys__rand") % resolution
< round(sum(weights_normalized[: index + 1]) * resolution),
)
for index, _ in enumerate(weights_normalized)
]
40 changes: 20 additions & 20 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,16 +707,16 @@ def studio_datasets(requests_mock):
def not_random_ds(test_session):
return DataChain.from_records(
[
{"sys__id": 1, "sys__rand": 50, "fib": 0},
{"sys__id": 2, "sys__rand": 150, "fib": 1},
{"sys__id": 3, "sys__rand": 250, "fib": 1},
{"sys__id": 4, "sys__rand": 350, "fib": 2},
{"sys__id": 5, "sys__rand": 450, "fib": 3},
{"sys__id": 6, "sys__rand": 550, "fib": 5},
{"sys__id": 7, "sys__rand": 650, "fib": 8},
{"sys__id": 8, "sys__rand": 750, "fib": 13},
{"sys__id": 9, "sys__rand": 850, "fib": 21},
{"sys__id": 10, "sys__rand": 950, "fib": 34},
{"sys__id": 1, "sys__rand": 200000000, "fib": 0},
{"sys__id": 2, "sys__rand": 400000000, "fib": 1},
{"sys__id": 3, "sys__rand": 600000000, "fib": 1},
{"sys__id": 4, "sys__rand": 800000000, "fib": 2},
{"sys__id": 5, "sys__rand": 1000000000, "fib": 3},
{"sys__id": 6, "sys__rand": 1200000000, "fib": 5},
{"sys__id": 7, "sys__rand": 1400000000, "fib": 8},
{"sys__id": 8, "sys__rand": 1600000000, "fib": 13},
{"sys__id": 9, "sys__rand": 1800000000, "fib": 21},
{"sys__id": 10, "sys__rand": 2000000000, "fib": 34},
],
session=test_session,
schema={"sys": Sys, "fib": int},
Expand All @@ -727,16 +727,16 @@ def not_random_ds(test_session):
def pseudo_random_ds(test_session):
return DataChain.from_records(
[
{"sys__id": 1, "sys__rand": 1344339883, "fib": 0},
{"sys__id": 2, "sys__rand": 3901153096, "fib": 1},
{"sys__id": 3, "sys__rand": 4255991360, "fib": 1},
{"sys__id": 4, "sys__rand": 2526403609, "fib": 2},
{"sys__id": 5, "sys__rand": 1871733386, "fib": 3},
{"sys__id": 6, "sys__rand": 9380910850, "fib": 5},
{"sys__id": 7, "sys__rand": 2770679740, "fib": 8},
{"sys__id": 8, "sys__rand": 2538886575, "fib": 13},
{"sys__id": 9, "sys__rand": 3969542617, "fib": 21},
{"sys__id": 10, "sys__rand": 7541790992, "fib": 34},
{"sys__id": 1, "sys__rand": 5879675686166179356, "fib": 0},
{"sys__id": 2, "sys__rand": 1072763634206539916, "fib": 1},
{"sys__id": 3, "sys__rand": 7470878726525325522, "fib": 1},
{"sys__id": 4, "sys__rand": 5291421404395990161, "fib": 2},
{"sys__id": 5, "sys__rand": 3788858646844612545, "fib": 3},
{"sys__id": 6, "sys__rand": 5386753411829147253, "fib": 5},
{"sys__id": 7, "sys__rand": 6728313339644935000, "fib": 8},
{"sys__id": 8, "sys__rand": 9104853878801776163, "fib": 13},
{"sys__id": 9, "sys__rand": 8521762708321943183, "fib": 21},
{"sys__id": 10, "sys__rand": 6715195136789658794, "fib": 34},
],
session=test_session,
schema={"sys": Sys, "fib": int},
Expand Down
2 changes: 1 addition & 1 deletion tests/func/test_pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _adapt_row(row):
def schema():
return {
"id": {"type": "UInt64"},
"sys__rand": {"type": "Int64"},
"sys__rand": {"type": "UInt64"},
0x2b3bfa0 marked this conversation as resolved.
Show resolved Hide resolved
"file__path": {"type": "String"},
"file__etag": {"type": "String"},
"file__version": {"type": "String"},
Expand Down
Loading