-
Notifications
You must be signed in to change notification settings - Fork 4
docs: add examples tying the various data loaders and explain their hierarchy #718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a7c005a
docs: add examples tying the various data loaders and explaining thei…
ad-claw000 0a2b1e5
fix: pre-commit issues
ad-claw000 c67eb67
fix: remove trailing whitespace
ad-claw000 3887f37
Fix example data loader hierarchy script based on review comments
ad-claw000 33642f6
Fix pre-commit issues
ad-claw000 524985f
Fix data_loader_hierarchy.py: add constraints to CSVs and remove unus…
ad-claw000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| """ | ||
| Data Loader Hierarchy and Examples | ||
|
|
||
| ApertureDB python SDK uses `ParallelLoader` as the main mechanism to efficiently batch and load data in parallel. | ||
| The `ParallelLoader` relies on the `Subscriptable` interface. | ||
| Classes that inherit from `Subscriptable` can be passed to `ParallelLoader.ingest()`. | ||
|
|
||
| The main data loaders provided by ApertureDB handle parsing CSVs and generating the appropriate ApertureDB queries. | ||
| The hierarchy is as follows: | ||
|
|
||
| Subscriptable | ||
| └── CSVParser | ||
| ├── EntityDataCSV (Loads entities and their properties) | ||
| ├── ImageDataCSV (Loads images as entities and uploads image blobs) | ||
| ├── VideoDataCSV (Loads videos) | ||
| ├── BlobDataCSV (Loads generic blobs) | ||
| ├── DescriptorDataCSV (Loads descriptors, e.g., embeddings) | ||
| ├── ConnectionDataCSV (Loads connections between entities) | ||
| ├── BBoxDataCSV (Loads bounding boxes) | ||
| └── PolygonDataCSV (Loads polygons) | ||
|
|
||
| Other non-CSV data loaders (like PyTorchData, KaggleData) also inherit from `Subscriptable`. | ||
|
|
||
| The following script demonstrates how to instantiate different loaders and tie them together using `ParallelLoader`. | ||
| """ | ||
|
|
||
| import os | ||
| import base64 | ||
| import tempfile | ||
| import pandas as pd | ||
| from aperturedb.Connector import Connector | ||
| from aperturedb.ParallelLoader import ParallelLoader | ||
| from aperturedb.EntityDataCSV import EntityDataCSV | ||
| from aperturedb.ImageDataCSV import ImageDataCSV | ||
| from aperturedb.ConnectionDataCSV import ConnectionDataCSV | ||
|
|
||
|
|
||
| def create_sample_csvs(base_dir): | ||
| # 1. Create a sample CSV for Entities (Persons) | ||
| df_persons = pd.DataFrame({ | ||
| "EntityClass": ["Person", "Person"], | ||
| "name": ["Alice", "Bob"], | ||
| "age": [25, 30], | ||
| "constraint_name": ["Alice", "Bob"] | ||
| }) | ||
| persons_csv = os.path.join(base_dir, "persons.csv") | ||
| df_persons.to_csv(persons_csv, index=False) | ||
|
|
||
|
ad-claw000 marked this conversation as resolved.
|
||
| # 2. Create a sample CSV for Images | ||
| # Note: the paths should point to real images in a real scenario | ||
| dummy_image_path = os.path.join(base_dir, "dummy_image.png") | ||
| # 1x1 transparent PNG base64 | ||
| tiny_png_b64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" | ||
| with open(dummy_image_path, "wb") as f: | ||
| f.write(base64.b64decode(tiny_png_b64)) | ||
|
|
||
|
ad-claw000 marked this conversation as resolved.
|
||
| df_images = pd.DataFrame({ | ||
| "filename": [dummy_image_path, dummy_image_path], | ||
| "image_id": ["img1", "img2"], | ||
| "source": ["camera1", "camera2"], | ||
| "constraint_image_id": ["img1", "img2"] | ||
| }) | ||
| images_csv = os.path.join(base_dir, "images.csv") | ||
| df_images.to_csv(images_csv, index=False) | ||
|
|
||
| # 3. Create a sample CSV for Connections (Person -> Image) | ||
| df_connections = pd.DataFrame({ | ||
| "ConnectionClass": ["HasImage", "HasImage"], | ||
| "Person@name": ["Alice", "Bob"], | ||
| "_Image@image_id": ["img1", "img2"], | ||
| "connection_property": ["owns", "likes"] | ||
| }) | ||
| connections_csv = os.path.join(base_dir, "connections.csv") | ||
| df_connections.to_csv(connections_csv, index=False) | ||
|
|
||
| return persons_csv, images_csv, connections_csv | ||
|
|
||
|
|
||
| def main(): | ||
| # Connect to ApertureDB (Make sure your ApertureDB instance is running) | ||
| db = Connector() | ||
|
|
||
| # Initialize the ParallelLoader | ||
| # The ParallelLoader handles the actual ingestion of queries produced by the Data Loader objects | ||
| loader = ParallelLoader(db) | ||
|
|
||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| persons_csv, images_csv, connections_csv = create_sample_csvs(temp_dir) | ||
|
|
||
| # 1. Load Entities | ||
| print("Loading Entities...") | ||
| # Properties are derived directly from the CSV header names. | ||
| person_loader = EntityDataCSV(persons_csv) | ||
| loader.ingest(person_loader) | ||
|
|
||
| # 2. Load Images | ||
| print("Loading Images...") | ||
| image_loader = ImageDataCSV(images_csv) | ||
| loader.ingest(image_loader) | ||
|
|
||
| # 3. Load Connections | ||
| print("Loading Connections...") | ||
| connection_loader = ConnectionDataCSV(connections_csv) | ||
| loader.ingest(connection_loader) | ||
| print("Done loading data!") | ||
|
|
||
|
|
||
|
ad-claw000 marked this conversation as resolved.
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.