Step 10: Dataset Filtering
Filtering datasets using user-defined-functions or SQL-style queries.
Filtering and querying is an important aspect of data engineering because analyzing and utilizing data in smaller units is much more productive than executing workflows on all data all the time.
Queries can be performed in Deep Lake enables with user-defined functions, or they can be executed in Activeloop Platform using our highly-performance SQL-style query language.
Activeloop Platform offers a highly-performant SQL-style query language that is built in C++ and is optimized for Deep Lake datasets. Queries and their results are executed and saved in the UI, and they can be accessed in Deep Lake using using the the
Dataset ViewsAPI described below.
The first step for querying using UDFs is to define a function that returns a boolean depending on whether an dataset sample meets the user-defined condition. In this example, we define a function that returns
Trueif the labels in a tensor are in the desired
labels_list. If there are inputs to the filtering function other than
sample_in, it must be decorated with
from PIL import Image
# Let's create a local copy of the dataset (Explanation is in the next section)
ds = deeplake.deepcopy('hub://activeloop/mnist-train', './mnist-train-local')
labels_list = ['0', '8'] # Desired labels for filtering
def filter_labels(sample_in, labels_list):
return sample_in.labels.data()['text'] in labels_list
The filtering function is executed using the
ds.filter()command below, and it returns a
Dataset Viewthat only contains the indices that met the filtering condition (more details below). Just like in the Parallel Computing API, the
sample_inparameter does not need to be passed into the filter function when evaluating it, and multi-processing can be specified using the
ds_view = ds.filter(filter_labels(labels_list), scheduler = 'threaded', num_workers = 0)
In most cases, multi-processing is not necessary for queries that involve simple data such as labels or bounding boxes. However, multi-processing significantly accelerates queries that must load rich data types such as images and videos.
Dataset Viewis any subset of a Deep Lake dataset that does not contains all of the samples. It can be an output of a query, filtering function, or regular indexing like
In the filtering example above, we copied
mnist-trainlocally in order to gain write access to the dataset. With write access, the views are saved as part of the dataset. Without write access, views are stored elsewhere or in custom paths, and full details are available here. Users have write access to their own datasets, regardless of whether the datasets are local or in the cloud.
The data in the returned
ds_viewcan be accessed just like a regular dataset.
Dataset Viewcan be saved permanently using the method below, which stores its indices without copying the data:
ds_view.save_view(message = 'Samples with 0 and 8')
In order to maintain data lineage,
Dataset Viewsare immutable and are connected to specific commits. Therefore, views can only be saved if the dataset has a commit and there are no uncommitted changes in the
Dataset Viewhas a unique
id, and views can be examined or loaded using:
views = ds.get_views()
ds_view = views.load()
# ds_view = ds.load_view(id)
Congrats! You just learned to filter and query data with Deep Lake! 🎈