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

Handle type conversion errors in FilterByValueBlock #78

Merged
merged 2 commits into from
Jul 6, 2024
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
15 changes: 11 additions & 4 deletions src/instructlab/sdg/filterblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@ def __init__(
self.convert_dtype = convert_dtype
self.num_procs = batch_kwargs.get("num_procs", 1)

def _convert_dtype(self, sample):
try:
sample[self.column_name] = self.convert_dtype(sample[self.column_name])
except ValueError as e:
logger.error(
"Error converting dtype: %s, filling with None to be filtered later", e
)
sample[self.column_name] = None
return sample

def generate(self, samples) -> Dataset:
if self.convert_dtype:
samples = samples.map(
lambda x: {
**x,
self.column_name: self.convert_dtype(x[self.column_name]),
},
self._convert_dtype,
num_proc=self.num_procs,
)

Expand Down
31 changes: 31 additions & 0 deletions tests/test_filterblock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Standard
from unittest.mock import patch
import operator
import unittest

# Third Party
from datasets import Dataset, Features, Value

# First Party
from instructlab.sdg.filterblock import FilterByValueBlock


class TestFilterByValueBlock(unittest.TestCase):
def setUp(self):
self.block = FilterByValueBlock(
filter_column="age",
filter_value=30,
operation=operator.eq,
convert_dtype=int,
)
self.dataset = Dataset.from_dict(
{"age": ["25", "30", "35", "forty", "45"]},
features=Features({"age": Value("string")}),
)

@patch("instructlab.sdg.filterblock.logger")
def test_generate_mixed_types(self, mock_logger):
filtered_dataset = self.block.generate(self.dataset)
self.assertEqual(len(filtered_dataset), 1)
self.assertEqual(filtered_dataset["age"], [30])
mock_logger.error.assert_called()