Filter Large Dataset Entry by Entry
MEMBER
Hi ! you can do

```python
filtered_dataset = dataset.filter(filter_function)
```

on a subset:

```python
filtered_subset = dataset.select(range(10_000)).filter(filter_function)
```

2024-09-20T13:28:23Z
https://github.com/huggingface/datasets/issues/7128#issuecomment-2363740653
https://api.github.com/repos/huggingface/datasets/issues/7128
IC_kwDODunzps6M49Ht
https://api.github.com/repos/huggingface/datasets/issues/comments/2363740653/reactions
2024-09-20T13:29:00Z
https://api.github.com/repos/huggingface/datasets/issues/comments/2363740653
https://avatars.githubusercontent.com/u/42851186?v=4
https://api.github.com/users/lhoestq/events{/privacy}
https://api.github.com/users/lhoestq/followers
https://api.github.com/users/lhoestq/following{/other_user}
https://api.github.com/users/lhoestq/gists{/gist_id}
https://github.com/lhoestq
lhoestq
MDQ6VXNlcjQyODUxMTg2
https://api.github.com/users/lhoestq/orgs
https://api.github.com/users/lhoestq/received_events
https://api.github.com/users/lhoestq/repos
https://api.github.com/users/lhoestq/starred{/owner}{/repo}
https://api.github.com/users/lhoestq/subscriptions
User
https://api.github.com/users/lhoestq
public
NONE
Jumping on this as it seems relevant - when I use the `filter` method, it often results in an OOM (or at least unacceptably high memory usage).

For example in the [this notebook](https://colab.research.google.com/drive/1N_rWko6jzGji3j_ayDR7ngT5lf4P8at_), we load an object detection dataset from HF and imagine I want to filter such that I only have images which contain a single annotation class. Each row has a JSON field that contains MS-COCO annotations for the image, so we could load that field and filter on it.

The test dataset is only about 440 images, probably less than 1GB, but running the following filter crashes the VM (over 12 GB RAM):

```python
import json
def filter_single_class(example, target_class_id):
  """Filters examples based on whether they contain annotations from a single class.

  Args:
    example: A dictionary representing a single example from the dataset.
    target_class_id: The target class ID to filter for.

  Returns:
    True if the example contains only annotations from the target class, False otherwise.
  """
  if not example['coco_annotations']:
    return False

  annotation_category_ids = set([annotation['category_id'] for annotation in json.loads(example['coco_annotations'])])

  return len(annotation_category_ids) == 1 and target_class_id in annotation_category_ids

target_class_id = 1 
filtered_dataset = dataset['test'].filter(lambda example: filter_single_class(example, target_class_id))
```

<img width="255" alt="image" src="https://github.com/user-attachments/assets/be475f15-5b6b-4df2-b5b5-a1f60ae2b05c">

Iterating over the dataset works fine:

```python
filtered_dataset = []
for example in dataset['test']:
  if filter_single_class(example, target_class_id):
    filtered_dataset.append(example)
```

<img width="129" alt="image" src="https://github.com/user-attachments/assets/34fa5612-0394-4c46-9f34-e94650f05d65">

It would be great if there was guidance in the documentation on how to use filters efficiently, or if this is some performance bug that could be addressed. At the very least I would expect a filter operation to use at most 2x the footprint of the database plus some overhead for the lambda (i.e. worst case would be a duplicate copy with all entries retained). Even if the operation is parallelised, each thread/worker should only take a subset of the dataset - so I'm not sure where this ballooning in memory usage comes from.

From some other comments there seems to be a workaround with `writer_batch_size` or caching to file, but in the [docs](https://huggingface.co/docs/datasets/v3.0.0/en/package_reference/main_classes#datasets.Dataset.filter) at least, `keep_in_memory` defaults to `False`.
2024-09-21T20:33:14Z
https://github.com/huggingface/datasets/issues/7128#issuecomment-2365312142
https://api.github.com/repos/huggingface/datasets/issues/7128
IC_kwDODunzps6M-8yO
https://api.github.com/repos/huggingface/datasets/issues/comments/2365312142/reactions
2024-09-21T20:48:06Z
https://api.github.com/repos/huggingface/datasets/issues/comments/2365312142
https://avatars.githubusercontent.com/u/3159591?v=4
https://api.github.com/users/jveitchmichaelis/events{/privacy}
https://api.github.com/users/jveitchmichaelis/followers
https://api.github.com/users/jveitchmichaelis/following{/other_user}
https://api.github.com/users/jveitchmichaelis/gists{/gist_id}
https://github.com/jveitchmichaelis
jveitchmichaelis
MDQ6VXNlcjMxNTk1OTE=
https://api.github.com/users/jveitchmichaelis/orgs
https://api.github.com/users/jveitchmichaelis/received_events
https://api.github.com/users/jveitchmichaelis/repos
https://api.github.com/users/jveitchmichaelis/starred{/owner}{/repo}
https://api.github.com/users/jveitchmichaelis/subscriptions
User
https://api.github.com/users/jveitchmichaelis
public
MEMBER
You can try passing input_columns=["coco_annotations"] to only load this column instead of all the columns. In that case your function should take coco_annotations as input instead of example
2024-09-23T09:37:19Z
https://github.com/huggingface/datasets/issues/7128#issuecomment-2367693920
https://api.github.com/repos/huggingface/datasets/issues/7128
IC_kwDODunzps6NICRg
https://api.github.com/repos/huggingface/datasets/issues/comments/2367693920/reactions
2024-09-23T09:37:19Z
https://api.github.com/repos/huggingface/datasets/issues/comments/2367693920
https://avatars.githubusercontent.com/u/42851186?v=4
https://api.github.com/users/lhoestq/events{/privacy}
https://api.github.com/users/lhoestq/followers
https://api.github.com/users/lhoestq/following{/other_user}
https://api.github.com/users/lhoestq/gists{/gist_id}
https://github.com/lhoestq
lhoestq
MDQ6VXNlcjQyODUxMTg2
https://api.github.com/users/lhoestq/orgs
https://api.github.com/users/lhoestq/received_events
https://api.github.com/users/lhoestq/repos
https://api.github.com/users/lhoestq/starred{/owner}{/repo}
https://api.github.com/users/lhoestq/subscriptions
User
https://api.github.com/users/lhoestq
public
NONE
If your filter_function is large and computationally intensive, consider using multi-processing or multi-threading with concurrent.futures to filter the dataset. This approach allows you to process multiple tables concurrently, reducing overall processing time, especially for CPU-bound tasks. Use ThreadPoolExecutor for I/O-bound operations and ProcessPoolExecutor for CPU-bound operations.

2024-10-07T23:37:42Z
https://github.com/huggingface/datasets/issues/7128#issuecomment-2398169335
https://api.github.com/repos/huggingface/datasets/issues/7128
IC_kwDODunzps6O8Sj3
https://api.github.com/repos/huggingface/datasets/issues/comments/2398169335/reactions
2024-10-07T23:37:42Z
https://api.github.com/repos/huggingface/datasets/issues/comments/2398169335
https://avatars.githubusercontent.com/u/143315164?v=4
https://api.github.com/users/Mythripaluri/events{/privacy}
https://api.github.com/users/Mythripaluri/followers
https://api.github.com/users/Mythripaluri/following{/other_user}
https://api.github.com/users/Mythripaluri/gists{/gist_id}
https://github.com/Mythripaluri
Mythripaluri
U_kgDOCIrQ3A
https://api.github.com/users/Mythripaluri/orgs
https://api.github.com/users/Mythripaluri/received_events
https://api.github.com/users/Mythripaluri/repos
https://api.github.com/users/Mythripaluri/starred{/owner}{/repo}
https://api.github.com/users/Mythripaluri/subscriptions
User
https://api.github.com/users/Mythripaluri
public
### Feature request

I am not sure if this is a new feature, but I wanted to post this problem here, and hear if others have ways of optimizing and speeding up this process.

Let's say I have a really large dataset that I cannot load into memory. At this point, I am only aware of `streaming=True` to load the dataset. Now, the dataset consists of many tables. Ideally, I would want to have some simple filtering criterion, such that I only see the "good" tables. Here is an example of what the code might look like:

```
dataset = load_dataset(
    "really-large-dataset",
    streaming=True
)
# And let's say we process the dataset bit by bit because we want intermediate results
dataset = islice(dataset, 10000)

# Define a function to filter the data
def filter_function(table):
    if some_condition:
        return True
    else:
        return False

# Use the filter function on your dataset
filtered_dataset = (ex for ex in dataset if filter_function(ex))
```

And then I work on the processed dataset, which would be magnitudes faster than working on the original. I would love to hear if the problem setup + solution makes sense to people, and if anyone has suggestions!

### Motivation

See description above

### Your contribution

Happy to make PR if this is a new feature