Skip to content

Commit

Permalink
Prevent TextDataset objects from containing None (#73)
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Schröder <chschroeder@users.noreply.github.com>
  • Loading branch information
chschroeder committed Jan 21, 2025
1 parent 8adaeaa commit 1d38af8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ On the other hand, this also allowed us to deal with further issues that contain
- Added environment variables `SMALL_TEXT_PROGRESS_BARS` and `SMALL_TEXT_OFFLINE` to control the default behavior for progress bars and model downloading.
- PoolBasedActiveLearner:
- `initialize_data()` has been replaced by `initialize()` which can now also be used to provide an initial model in cold start scenarios. ([#10](https://github.com/webis-de/small-text/pull/10))
- Datasets
- Validation has been added to prevent TextDataset objects from containing None items (instead of str) either during initialization or when setting the x property. ([#73](https://github.com/webis-de/small-text/pull/73))
- Classification:
- All PyTorch-classifiers (KimCNN, TransformerBasedClassification, SetFitClassification) now support `torch.compile()` which can be enabled on demand. (Requires PyTorch >= 2.0.0).
- All PyTorch-classifiers (KimCNN, TransformerBasedClassification, SetFitClassification) now support Automatic Mixed Precision.
Expand Down
8 changes: 8 additions & 0 deletions small_text/data/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def check_dataset_and_labels(x, y):
f'x = ({len_x},), y.shape= ({y.shape[0]},) ### {type(x)} / {type(y)}')


def check_text_data(x):
for i, item in enumerate(x):
if item is None:
raise ValueError(f'instance #{i} is None which is not allowed.')


def check_target_labels(target_labels):
if target_labels is None:
warnings.warn('Passing target_labels=None is discouraged as it can lead to '
Expand Down Expand Up @@ -573,6 +579,7 @@ def __init__(self, x, y, target_labels=None):
target_labels : numpy.ndarray[int] or None, default=None
List of possible labels. Will be inferred from `y` if `None` is passed."""
check_dataset_and_labels(x, y)
check_text_data(x)
check_target_labels(target_labels)

if isinstance(x, np.ndarray):
Expand Down Expand Up @@ -603,6 +610,7 @@ def x(self):

@x.setter
def x(self, x_new):
check_text_data(x_new)
check_dataset_and_labels(x_new, self._y)
self._x = x_new

Expand Down
18 changes: 18 additions & 0 deletions tests/unit/small_text/data/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,19 @@ def test_init_with_dimension_mismatch(self):
with self.assertRaisesRegex(ValueError, 'Feature and label dimensions do not match'):
TextDataset(x, y, target_labels=ds.target_labels)

def test_init_when_samples_are_none(self):
x = [None] * self.NUM_SAMPLES
y = random_labels(self.NUM_SAMPLES, self.NUM_LABELS,
multi_label=self.labels_type == 'sparse')

if self.target_labels not in ['explicit', 'inferred']:
raise ValueError('Invalid test parameter value for target_labels:' + self.target_labels)

target_labels = None if self.target_labels == 'inferred' else np.arange(5)

with self.assertRaisesRegex(ValueError, 'instance #0 is None which is not allowed.'):
TextDataset(x, y, target_labels=target_labels)

def test_init_when_some_samples_are_unlabeled(self):
x = random_text_data(self.NUM_SAMPLES)
y = random_labels(self.NUM_SAMPLES, self.NUM_LABELS,
Expand Down Expand Up @@ -480,6 +493,11 @@ def test_set_features(self):

assert_array_equal(ds.x, ds_new.x)

def test_set_features_when_samples_are_none(self):
ds, x, _ = self._dataset(num_samples=self.NUM_SAMPLES, return_data=True)
with self.assertRaisesRegex(ValueError, 'instance #0 is None which is not allowed.'):
ds.x = [None] * len(x)

def test_set_features_with_dimension_mismatch(self):
ds, x, _ = self._dataset(num_samples=self.NUM_SAMPLES, return_data=True)
x = x[1:]
Expand Down

0 comments on commit 1d38af8

Please # to comment.