Datasets:
Fix filtering: use GeneratorBasedBuilder instead of ArrowBasedBuilder
Browse files- permutation-groups.py +39 -34
permutation-groups.py
CHANGED
@@ -76,7 +76,7 @@ class PermutationGroupsConfig(datasets.BuilderConfig):
|
|
76 |
self.min_len = min_len
|
77 |
self.max_len = max_len
|
78 |
|
79 |
-
class PermutationGroups(datasets.
|
80 |
"""Permutation groups dataset with dynamic filtering."""
|
81 |
|
82 |
VERSION = datasets.Version("4.0.0")
|
@@ -226,14 +226,12 @@ class PermutationGroups(datasets.ArrowBasedBuilder):
|
|
226 |
name=datasets.Split.TRAIN,
|
227 |
gen_kwargs={
|
228 |
"files": downloaded_files["train"],
|
229 |
-
"config": self.config,
|
230 |
},
|
231 |
),
|
232 |
datasets.SplitGenerator(
|
233 |
name=datasets.Split.TEST,
|
234 |
gen_kwargs={
|
235 |
"files": downloaded_files["test"],
|
236 |
-
"config": self.config,
|
237 |
},
|
238 |
),
|
239 |
]
|
@@ -244,52 +242,59 @@ class PermutationGroups(datasets.ArrowBasedBuilder):
|
|
244 |
name=datasets.Split.TRAIN,
|
245 |
gen_kwargs={
|
246 |
"files": [],
|
247 |
-
"config": self.config,
|
248 |
},
|
249 |
),
|
250 |
datasets.SplitGenerator(
|
251 |
name=datasets.Split.TEST,
|
252 |
gen_kwargs={
|
253 |
"files": [],
|
254 |
-
"config": self.config,
|
255 |
},
|
256 |
),
|
257 |
]
|
258 |
|
259 |
-
def
|
260 |
-
"""Yield
|
261 |
-
|
262 |
-
|
263 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
|
265 |
-
#
|
266 |
-
|
267 |
-
#
|
268 |
-
|
269 |
-
|
|
|
270 |
|
271 |
# Filter by degree
|
272 |
-
if config.min_degree and
|
273 |
-
|
274 |
-
if config.max_degree and
|
275 |
-
|
276 |
|
277 |
# Filter by order
|
278 |
-
if config.min_order and
|
279 |
-
|
280 |
-
if config.max_order and
|
281 |
-
|
282 |
|
283 |
# Filter by sequence length
|
284 |
-
seq_len =
|
285 |
-
if seq_len < config.min_len or seq_len > config.max_len:
|
286 |
-
|
287 |
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
|
|
|
|
|
76 |
self.min_len = min_len
|
77 |
self.max_len = max_len
|
78 |
|
79 |
+
class PermutationGroups(datasets.GeneratorBasedBuilder):
|
80 |
"""Permutation groups dataset with dynamic filtering."""
|
81 |
|
82 |
VERSION = datasets.Version("4.0.0")
|
|
|
226 |
name=datasets.Split.TRAIN,
|
227 |
gen_kwargs={
|
228 |
"files": downloaded_files["train"],
|
|
|
229 |
},
|
230 |
),
|
231 |
datasets.SplitGenerator(
|
232 |
name=datasets.Split.TEST,
|
233 |
gen_kwargs={
|
234 |
"files": downloaded_files["test"],
|
|
|
235 |
},
|
236 |
),
|
237 |
]
|
|
|
242 |
name=datasets.Split.TRAIN,
|
243 |
gen_kwargs={
|
244 |
"files": [],
|
|
|
245 |
},
|
246 |
),
|
247 |
datasets.SplitGenerator(
|
248 |
name=datasets.Split.TEST,
|
249 |
gen_kwargs={
|
250 |
"files": [],
|
|
|
251 |
},
|
252 |
),
|
253 |
]
|
254 |
|
255 |
+
def _generate_examples(self, files):
|
256 |
+
"""Yield examples with filtering."""
|
257 |
+
idx = 0
|
258 |
+
|
259 |
+
for file_path in files:
|
260 |
+
# Load the arrow file
|
261 |
+
with pa.memory_map(file_path, 'r') as source:
|
262 |
+
table = pa.ipc.open_file(source).read_all()
|
263 |
+
|
264 |
+
# Convert to pandas for easier row-wise iteration
|
265 |
+
df = table.to_pandas()
|
266 |
|
267 |
+
# Iterate through rows and apply filters
|
268 |
+
for _, row in df.iterrows():
|
269 |
+
# Apply filters
|
270 |
+
# Filter by group type (if specified)
|
271 |
+
if self.config.group_type and row.get("group_type") != self.config.group_type:
|
272 |
+
continue
|
273 |
|
274 |
# Filter by degree
|
275 |
+
if self.config.min_degree and row.get("group_degree", 0) < self.config.min_degree:
|
276 |
+
continue
|
277 |
+
if self.config.max_degree and row.get("group_degree", float('inf')) > self.config.max_degree:
|
278 |
+
continue
|
279 |
|
280 |
# Filter by order
|
281 |
+
if self.config.min_order and row.get("group_order", 0) < self.config.min_order:
|
282 |
+
continue
|
283 |
+
if self.config.max_order and row.get("group_order", float('inf')) > self.config.max_order:
|
284 |
+
continue
|
285 |
|
286 |
# Filter by sequence length
|
287 |
+
seq_len = row.get("sequence_length", len(row["input_sequence"].split()))
|
288 |
+
if seq_len < self.config.min_len or seq_len > self.config.max_len:
|
289 |
+
continue
|
290 |
|
291 |
+
# Yield the example
|
292 |
+
yield idx, {
|
293 |
+
"input_sequence": row["input_sequence"],
|
294 |
+
"target": row["target"],
|
295 |
+
"group_type": row.get("group_type", ""),
|
296 |
+
"group_degree": int(row.get("group_degree", 0)),
|
297 |
+
"group_order": int(row.get("group_order", 0)),
|
298 |
+
"sequence_length": int(row.get("sequence_length", seq_len)),
|
299 |
+
}
|
300 |
+
idx += 1
|