LogoLogo
API ReferenceGitHubSlackService StatusLogin
v3.8.16
v3.8.16
  • Deep Lake Docs
  • Vector Store Quickstart
  • Deep Learning Quickstart
  • Storage & Credentials
    • Storage Options
    • User Authentication
    • Storing Deep Lake Data in Your Own Cloud
      • Microsoft Azure
        • Provisioning Federated Credentials
        • Enabling CORS
      • Amazon Web Services
        • Provisioning Role-Based Access
        • Enabling CORS
  • List of ML Datasets
  • 🏢High-Performance Features
    • Introduction
    • Performant Dataloader
    • Tensor Query Language (TQL)
      • TQL Syntax
      • Sampling Datasets
    • Deep Memory
      • How it Works
    • Index for ANN Search
      • Caching and Optimization
    • Managed Tensor Database
      • REST API
      • Migrating Datasets to the Tensor Database
  • 📚EXAMPLE CODE
    • Getting Started
      • Vector Store
        • Step 1: Hello World
        • Step 2: Creating Deep Lake Vector Stores
        • Step 3: Performing Search in Vector Stores
        • Step 4: Customizing Vector Stores
      • Deep Learning
        • Step 1: Hello World
        • Step 2: Creating Deep Lake Datasets
        • Step 3: Understanding Compression
        • Step 4: Accessing and Updating Data
        • Step 5: Visualizing Datasets
        • Step 6: Using Activeloop Storage
        • Step 7: Connecting Deep Lake Datasets to ML Frameworks
        • Step 8: Parallel Computing
        • Step 9: Dataset Version Control
        • Step 10: Dataset Filtering
    • Tutorials (w Colab)
      • Vector Store Tutorials
        • Vector Search Options
          • Deep Lake Vector Store API
          • REST API
          • LangChain API
        • Image Similarity Search
        • Deep Lake Vector Store in LangChain
        • Deep Lake Vector Store in LlamaIndex
        • Improving Search Accuracy using Deep Memory
      • Deep Learning Tutorials
        • Creating Datasets
          • Creating Complex Datasets
          • Creating Object Detection Datasets
          • Creating Time-Series Datasets
          • Creating Datasets with Sequences
          • Creating Video Datasets
        • Training Models
          • Splitting Datasets for Training
          • Training an Image Classification Model in PyTorch
          • Training Models Using MMDetection
          • Training Models Using PyTorch Lightning
          • Training on AWS SageMaker
          • Training an Object Detection and Segmentation Model in PyTorch
        • Updating Datasets
        • Data Processing Using Parallel Computing
      • Concurrent Writes
        • Concurrency Using Zookeeper Locks
    • Playbooks
      • Querying, Training and Editing Datasets with Data Lineage
      • Evaluating Model Performance
      • Training Reproducibility Using Deep Lake and Weights & Biases
      • Working with Videos
    • Low-Level API Summary
  • 🔬Technical Details
    • Best Practices
      • Creating Datasets at Scale
      • Training Models at Scale
      • Storage Synchronization and "with" Context
      • Restoring Corrupted Datasets
      • Concurrent Writes
    • Data Layout
    • Version Control and Querying
    • Dataset Visualization
    • Tensor Relationships
    • Visualizer Integration
    • Shuffling in dataloaders
    • How to Contribute
Powered by GitBook
On this page
  • How to Filter and Query Data in Deep Lake
  • Filtering using our Tensor Query Language (TQL)
  • Filtering with user-defined-functions (UDF)
  • Dataset Views

Was this helpful?

  1. EXAMPLE CODE
  2. Getting Started
  3. Deep Learning

Step 10: Dataset Filtering

Filtering datasets using user-defined-functions or SQL-style queries.

PreviousStep 9: Dataset Version ControlNextTutorials (w Colab)

Was this helpful?

How to Filter and Query Data in Deep Lake

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 using our highly-performance SQL-style query language.

Filtering using our Tensor Query Language (TQL)

Deep Lake offers a 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 Views API described below.

Full details about the query language are described in a .

Filtering with user-defined-functions (UDF)

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 True if 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 @deeplake.compute.

import deeplake
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

@deeplake.compute
def filter_labels(sample_in, labels_list):
    
    return sample_in.labels.data()['text'][0] in labels_list
ds_view = ds.filter(filter_labels(labels_list), scheduler = 'threaded', num_workers = 0)
print(len(ds_view))

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 Views

A Dataset View is 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 ds[0:2:100].

The data in the returned ds_view can be accessed just like a regular dataset.

Image.fromarray(ds_view.images[10].numpy())

A Dataset View can 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 Views are 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 HEAD.

Each Dataset View has a unique id, and views can be examined or loaded using:

views = ds.get_views()

print(views)
ds_view = views[0].load()

# OR

# ds_view = ds.load_view(id)
print(len(ds_view))

Congrats! You just learned to filter and query data with Deep Lake! 🎈

The filtering function is executed using the ds.filter() command below, and it returns a Dataset View that only contains the indices that met the filtering condition (more details below). Just like in the , the sample_in parameter does not need to be passed into the filter function when evaluating it, and multi-processing can be specified using the scheduler and num_workers parameters.

In the filtering example above, we copied mnist-train locally 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 . Users have write access to their own datasets, regardless of whether the datasets are local or in the cloud.

📚
Activeloop Platform
highly-performant SQL-style query language
standalone tutorial
Parallel Computing API
available here