Skip to content

Commit

Permalink
Raise error if ClassLabel names is not python list (#5359)
Browse files Browse the repository at this point in the history
* Raise error if ClassLabel names is not python list

* Change to accepting Sequence for names

* Replace `ValueError` with `TypeError`

Co-authored-by: Mario 艩a拧ko <mariosasko777@gmail.com>
  • Loading branch information
freddyheppell and mariosasko committed Dec 22, 2022
1 parent db025eb commit 9a7272c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/datasets/features/features.py
Expand Up @@ -19,6 +19,7 @@
import re
import sys
from collections.abc import Iterable, Mapping
from collections.abc import Sequence as SequenceABC
from dataclasses import InitVar, dataclass, field, fields
from functools import reduce, wraps
from operator import mul
Expand Down Expand Up @@ -944,6 +945,8 @@ def __post_init__(self, num_classes, names_file):
self.names = [str(i) for i in range(self.num_classes)]
else:
raise ValueError("Please provide either num_classes, names or names_file.")
elif not isinstance(self.names, SequenceABC):
raise TypeError(f"Please provide names as a list, is {type(self.names)}")
# Set self.num_classes
if self.num_classes is None:
self.num_classes = len(self.names)
Expand Down
2 changes: 2 additions & 0 deletions tests/features/test_features.py
Expand Up @@ -287,6 +287,8 @@ def test_classlabel_init(tmp_path_factory):
classlabel = ClassLabel(names=names, names_file=names_file)
with pytest.raises(ValueError):
classlabel = ClassLabel()
with pytest.raises(TypeError):
classlabel = ClassLabel(names=np.array(names))


def test_classlabel_str2int():
Expand Down

1 comment on commit 9a7272c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Show benchmarks

PyArrow==6.0.0

Show updated benchmarks!

Benchmark: benchmark_array_xd.json

metric read_batch_formatted_as_numpy after write_array2d read_batch_formatted_as_numpy after write_flattened_sequence read_batch_formatted_as_numpy after write_nested_sequence read_batch_unformated after write_array2d read_batch_unformated after write_flattened_sequence read_batch_unformated after write_nested_sequence read_col_formatted_as_numpy after write_array2d read_col_formatted_as_numpy after write_flattened_sequence read_col_formatted_as_numpy after write_nested_sequence read_col_unformated after write_array2d read_col_unformated after write_flattened_sequence read_col_unformated after write_nested_sequence read_formatted_as_numpy after write_array2d read_formatted_as_numpy after write_flattened_sequence read_formatted_as_numpy after write_nested_sequence read_unformated after write_array2d read_unformated after write_flattened_sequence read_unformated after write_nested_sequence write_array2d write_flattened_sequence write_nested_sequence
new / old (diff) 0.012251 / 0.011353 (0.000898) 0.006091 / 0.011008 (-0.004917) 0.129859 / 0.038508 (0.091351) 0.036829 / 0.023109 (0.013720) 0.389070 / 0.275898 (0.113172) 0.453526 / 0.323480 (0.130046) 0.009020 / 0.007986 (0.001035) 0.004575 / 0.004328 (0.000247) 0.103234 / 0.004250 (0.098983) 0.048322 / 0.037052 (0.011270) 0.402995 / 0.258489 (0.144506) 0.432899 / 0.293841 (0.139058) 0.057139 / 0.128546 (-0.071407) 0.020338 / 0.075646 (-0.055308) 0.428577 / 0.419271 (0.009306) 0.061495 / 0.043533 (0.017962) 0.377381 / 0.255139 (0.122242) 0.449701 / 0.283200 (0.166502) 0.121571 / 0.141683 (-0.020112) 1.862508 / 1.452155 (0.410354) 1.936987 / 1.492716 (0.444271)

Benchmark: benchmark_getitem_100B.json

metric get_batch_of_1024_random_rows get_batch_of_1024_rows get_first_row get_last_row
new / old (diff) 0.203237 / 0.018006 (0.185231) 0.531319 / 0.000490 (0.530830) 0.005473 / 0.000200 (0.005273) 0.000107 / 0.000054 (0.000052)

Benchmark: benchmark_indices_mapping.json

metric select shard shuffle sort train_test_split
new / old (diff) 0.030489 / 0.037411 (-0.006922) 0.127645 / 0.014526 (0.113119) 0.129709 / 0.176557 (-0.046848) 0.180264 / 0.737135 (-0.556871) 0.144201 / 0.296338 (-0.152137)

Benchmark: benchmark_iterating.json

metric read 5000 read 50000 read_batch 50000 10 read_batch 50000 100 read_batch 50000 1000 read_formatted numpy 5000 read_formatted pandas 5000 read_formatted tensorflow 5000 read_formatted torch 5000 read_formatted_batch numpy 5000 10 read_formatted_batch numpy 5000 1000 shuffled read 5000 shuffled read 50000 shuffled read_batch 50000 10 shuffled read_batch 50000 100 shuffled read_batch 50000 1000 shuffled read_formatted numpy 5000 shuffled read_formatted_batch numpy 5000 10 shuffled read_formatted_batch numpy 5000 1000
new / old (diff) 0.609700 / 0.215209 (0.394490) 6.050985 / 2.077655 (3.973331) 2.482487 / 1.504120 (0.978367) 2.099480 / 1.541195 (0.558285) 2.116431 / 1.468490 (0.647941) 1.220263 / 4.584777 (-3.364514) 5.492601 / 3.745712 (1.746888) 3.151682 / 5.269862 (-2.118180) 2.303395 / 4.565676 (-2.262281) 0.151992 / 0.424275 (-0.272283) 0.015119 / 0.007607 (0.007512) 0.783869 / 0.226044 (0.557824) 7.857813 / 2.268929 (5.588885) 3.171587 / 55.444624 (-52.273037) 2.481024 / 6.876477 (-4.395452) 2.603822 / 2.142072 (0.461750) 1.484962 / 4.805227 (-3.320265) 0.259814 / 6.500664 (-6.240850) 0.075144 / 0.075469 (-0.000325)

Benchmark: benchmark_map_filter.json

metric filter map fast-tokenizer batched map identity map identity batched map no-op batched map no-op batched numpy map no-op batched pandas map no-op batched pytorch map no-op batched tensorflow
new / old (diff) 1.680635 / 1.841788 (-0.161153) 17.605008 / 8.074308 (9.530700) 22.569270 / 10.191392 (12.377878) 0.236945 / 0.680424 (-0.443478) 0.048046 / 0.534201 (-0.486154) 0.557765 / 0.579283 (-0.021518) 0.633711 / 0.434364 (0.199347) 0.634956 / 0.540337 (0.094619) 0.752852 / 1.386936 (-0.634084)
PyArrow==latest
Show updated benchmarks!

Benchmark: benchmark_array_xd.json

metric read_batch_formatted_as_numpy after write_array2d read_batch_formatted_as_numpy after write_flattened_sequence read_batch_formatted_as_numpy after write_nested_sequence read_batch_unformated after write_array2d read_batch_unformated after write_flattened_sequence read_batch_unformated after write_nested_sequence read_col_formatted_as_numpy after write_array2d read_col_formatted_as_numpy after write_flattened_sequence read_col_formatted_as_numpy after write_nested_sequence read_col_unformated after write_array2d read_col_unformated after write_flattened_sequence read_col_unformated after write_nested_sequence read_formatted_as_numpy after write_array2d read_formatted_as_numpy after write_flattened_sequence read_formatted_as_numpy after write_nested_sequence read_unformated after write_array2d read_unformated after write_flattened_sequence read_unformated after write_nested_sequence write_array2d write_flattened_sequence write_nested_sequence
new / old (diff) 0.010116 / 0.011353 (-0.001237) 0.009385 / 0.011008 (-0.001623) 0.128823 / 0.038508 (0.090315) 0.036015 / 0.023109 (0.012906) 0.463060 / 0.275898 (0.187162) 0.502877 / 0.323480 (0.179397) 0.006924 / 0.007986 (-0.001061) 0.004834 / 0.004328 (0.000506) 0.100416 / 0.004250 (0.096165) 0.055005 / 0.037052 (0.017953) 0.475232 / 0.258489 (0.216743) 0.526853 / 0.293841 (0.233012) 0.054634 / 0.128546 (-0.073912) 0.021335 / 0.075646 (-0.054311) 0.427556 / 0.419271 (0.008285) 0.064707 / 0.043533 (0.021175) 0.470892 / 0.255139 (0.215753) 0.501285 / 0.283200 (0.218086) 0.112355 / 0.141683 (-0.029328) 1.834388 / 1.452155 (0.382233) 1.984745 / 1.492716 (0.492029)

Benchmark: benchmark_getitem_100B.json

metric get_batch_of_1024_random_rows get_batch_of_1024_rows get_first_row get_last_row
new / old (diff) 0.299642 / 0.018006 (0.281636) 0.536983 / 0.000490 (0.536493) 0.049960 / 0.000200 (0.049760) 0.000449 / 0.000054 (0.000394)

Benchmark: benchmark_indices_mapping.json

metric select shard shuffle sort train_test_split
new / old (diff) 0.030028 / 0.037411 (-0.007383) 0.138001 / 0.014526 (0.123475) 0.135516 / 0.176557 (-0.041040) 0.183424 / 0.737135 (-0.553712) 0.140621 / 0.296338 (-0.155718)

Benchmark: benchmark_iterating.json

metric read 5000 read 50000 read_batch 50000 10 read_batch 50000 100 read_batch 50000 1000 read_formatted numpy 5000 read_formatted pandas 5000 read_formatted tensorflow 5000 read_formatted torch 5000 read_formatted_batch numpy 5000 10 read_formatted_batch numpy 5000 1000 shuffled read 5000 shuffled read 50000 shuffled read_batch 50000 10 shuffled read_batch 50000 100 shuffled read_batch 50000 1000 shuffled read_formatted numpy 5000 shuffled read_formatted_batch numpy 5000 10 shuffled read_formatted_batch numpy 5000 1000
new / old (diff) 0.682725 / 0.215209 (0.467516) 6.737031 / 2.077655 (4.659376) 2.725291 / 1.504120 (1.221171) 2.329505 / 1.541195 (0.788311) 2.388634 / 1.468490 (0.920144) 1.325764 / 4.584777 (-3.259013) 5.876019 / 3.745712 (2.130307) 4.833960 / 5.269862 (-0.435901) 2.805582 / 4.565676 (-1.760094) 0.149938 / 0.424275 (-0.274337) 0.015983 / 0.007607 (0.008376) 0.858643 / 0.226044 (0.632599) 8.562383 / 2.268929 (6.293454) 3.531761 / 55.444624 (-51.912864) 2.748393 / 6.876477 (-4.128083) 2.858888 / 2.142072 (0.716816) 1.546435 / 4.805227 (-3.258792) 0.270419 / 6.500664 (-6.230245) 0.080139 / 0.075469 (0.004670)

Benchmark: benchmark_map_filter.json

metric filter map fast-tokenizer batched map identity map identity batched map no-op batched map no-op batched numpy map no-op batched pandas map no-op batched pytorch map no-op batched tensorflow
new / old (diff) 1.645904 / 1.841788 (-0.195884) 18.103779 / 8.074308 (10.029470) 19.896848 / 10.191392 (9.705456) 0.232255 / 0.680424 (-0.448169) 0.027504 / 0.534201 (-0.506697) 0.541644 / 0.579283 (-0.037640) 0.652051 / 0.434364 (0.217687) 0.620042 / 0.540337 (0.079705) 0.727936 / 1.386936 (-0.659001)

Please sign in to comment.