BeeGass commited on
Commit
8abeac9
·
verified ·
1 Parent(s): 91777eb

Fix filtering: use GeneratorBasedBuilder instead of ArrowBasedBuilder

Browse files
Files changed (1) hide show
  1. 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.ArrowBasedBuilder):
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 _generate_tables(self, files, config):
260
- """Yield arrow tables with filtering."""
261
- for file_idx, file in enumerate(files):
262
- # Load the dataset
263
- dataset = datasets.Dataset.from_file(file)
 
 
 
 
 
 
264
 
265
- # Apply filters
266
- def filter_fn(example):
267
- # Filter by group type (if not already filtered by file selection)
268
- if config.group_type and example.get("group_type") != config.group_type:
269
- return False
 
270
 
271
  # Filter by degree
272
- if config.min_degree and example.get("group_degree", 0) < config.min_degree:
273
- return False
274
- if config.max_degree and example.get("group_degree", float('inf')) > config.max_degree:
275
- return False
276
 
277
  # Filter by order
278
- if config.min_order and example.get("group_order", 0) < config.min_order:
279
- return False
280
- if config.max_order and example.get("group_order", float('inf')) > config.max_order:
281
- return False
282
 
283
  # Filter by sequence length
284
- seq_len = example.get("sequence_length", len(example["input_sequence"].split()))
285
- if seq_len < config.min_len or seq_len > config.max_len:
286
- return False
287
 
288
- return True
289
-
290
- # Apply filtering
291
- dataset = dataset.filter(filter_fn)
292
-
293
- # Get the underlying Arrow table
294
- table = dataset.data.table
295
- yield file_idx, table
 
 
 
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