Skip to content

Add image_resolver.get_for_export with collection filter support#1637

Merged
LeonardoRosaa merged 4 commits into
mainfrom
leonardo-lig-8988-figure-out-add-context-to-sampling-and-export-get-for-export
Jul 15, 2026
Merged

Add image_resolver.get_for_export with collection filter support#1637
LeonardoRosaa merged 4 commits into
mainfrom
leonardo-lig-8988-figure-out-add-context-to-sampling-and-export-get-for-export

Conversation

@LeonardoRosaa

@LeonardoRosaa LeonardoRosaa commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

What has changed and why?

Extracts a dedicated get_for_export function into the image_resolver module to serve image export pipelines. The function returns a lazy generator of ImageSample objects for a given collection, applying an optional ImageFilter (including embedding-region polygon filters resolved to concrete sample IDs before the query runs).

This is a follow-up to #1632, which introduced the export endpoint layer; this PR adds the resolver that backs it.

How has it been tested?

Unit tests

Did you update CHANGELOG.md?

  • Yes
  • Not needed (internal change)

Summary by CodeRabbit

  • New Features
    • Added collection image export with lazy, streamed results.
    • Added optional preload to eagerly include annotations and/or captions during export.
    • Expanded export filtering to support image property constraints and embedding-region criteria.
  • Bug Fixes
    • Improved embedding-region filtering to avoid mutating existing filter state.
  • Tests
    • Expanded automated coverage for unfiltered exports, image-property filters, embedding-region filters, and preload behavior for annotations/captions.

@LeonardoRosaa
LeonardoRosaa requested a review from a team as a code owner July 13, 2026 14:38
@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 3014e8df-ed0b-4c77-91f8-e3d551c972bc

📥 Commits

Reviewing files that changed from the base of the PR and between d488a54 and 70e368f.

📒 Files selected for processing (1)
  • lightly_studio/tests/resolvers/image_resolver/test_get_for_export.py

📝 Walkthrough

Walkthrough

Adds a public get_for_export image resolver with collection, image, and embedding-region filtering plus optional annotation and caption preloading. Embedding-region handling now uses copied filter models, with tests covering export selection and relationship loading.

Changes

Image export filtering

Layer / File(s) Summary
Export contract and public API
lightly_studio/src/lightly_studio/resolvers/image_resolver/get_for_export.py, lightly_studio/src/lightly_studio/resolvers/image_resolver/__init__.py
Defines ImageExportPreload options and publicly exports them with get_for_export.
Export query and filter handling
lightly_studio/src/lightly_studio/resolvers/image_resolver/get_for_export.py, lightly_studio/src/lightly_studio/resolvers/image_resolver/get_all_by_collection_id.py
Builds collection-scoped export queries, applies filters and optional relationship preloading, resolves embedding regions, and avoids mutating filter models.
Export resolver validation
lightly_studio/tests/resolvers/image_resolver/test_get_for_export.py
Tests collection scoping, image dimensions, embedding-region polygons, and annotation/caption relationship loading.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested reviewers: horatiualmasan

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant get_for_export
  participant EmbeddingRegionResolver
  participant Database
  Caller->>get_for_export: Request collection export with optional filter and preload
  get_for_export->>EmbeddingRegionResolver: Resolve embedding-region sample IDs
  EmbeddingRegionResolver-->>get_for_export: Return matching sample IDs
  get_for_export->>Database: Execute filtered image/sample query with eager-loading
  Database-->>get_for_export: Return matching rows
  get_for_export-->>Caller: Yield ImageSample values
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly names the new image_resolver.get_for_export resolver and its collection filter support.
Description check ✅ Passed The description matches the template and covers change summary, context, testing, and changelog status, though testing details are brief.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch leonardo-lig-8988-figure-out-add-context-to-sampling-and-export-get-for-export

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 00e4524bb4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread lightly_studio/tests/resolvers/image_resolver/test_get_for_export.py Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
lightly_studio/src/lightly_studio/resolvers/image_resolver/get_for_export.py (1)

39-52: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Duplicate embedding-region resolution logic across two resolvers. Both sites implement the identical "resolve embedding_regionregion_sample_ids, then model_copy the filter" sequence; extracting a shared helper avoids the two copies drifting out of sync as this logic evolves.

  • lightly_studio/src/lightly_studio/resolvers/image_resolver/get_for_export.py#L39-L52: extract this block into a shared helper (e.g. _resolve_embedding_region_filter(session, collection_id, collection_filter)) and call it here.
  • lightly_studio/src/lightly_studio/resolvers/image_resolver/get_all_by_collection_id.py#L103-L116: replace this block with a call to the same shared helper.
♻️ Proposed shared helper
def _resolve_embedding_region_filter(
    session: Session,
    collection_id: UUID,
    filters: ImageFilter | None,
) -> ImageFilter | None:
    """Resolve an embedding-region filter to concrete sample ids, if present."""
    if filters is None or filters.sample_filter is None or filters.sample_filter.embedding_region is None:
        return filters
    region_sample_ids = embedding_region_resolver.get_sample_ids_in_region(
        session=session,
        collection_id=collection_id,
        region=filters.sample_filter.embedding_region,
    )
    resolved_sample_filter = filters.sample_filter.model_copy(
        update={"region_sample_ids": region_sample_ids}
    )
    return filters.model_copy(update={"sample_filter": resolved_sample_filter})
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lightly_studio/src/lightly_studio/resolvers/image_resolver/get_for_export.py`
around lines 39 - 52, Extract the duplicated embedding-region resolution logic
into a shared helper, such as _resolve_embedding_region_filter, preserving the
existing None and no-region behavior while returning a copied filter with
resolved region_sample_ids when needed. Update get_for_export.py lines 39-52 and
get_all_by_collection_id.py lines 103-116 to call the helper, with no direct
logic remaining in either site.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In
`@lightly_studio/src/lightly_studio/resolvers/image_resolver/get_for_export.py`:
- Around line 39-52: Extract the duplicated embedding-region resolution logic
into a shared helper, such as _resolve_embedding_region_filter, preserving the
existing None and no-region behavior while returning a copied filter with
resolved region_sample_ids when needed. Update get_for_export.py lines 39-52 and
get_all_by_collection_id.py lines 103-116 to call the helper, with no direct
logic remaining in either site.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: aad83a9a-706f-428d-b6f4-063925a93096

📥 Commits

Reviewing files that changed from the base of the PR and between c3dc8b6 and 00e4524.

📒 Files selected for processing (4)
  • lightly_studio/src/lightly_studio/resolvers/image_resolver/__init__.py
  • lightly_studio/src/lightly_studio/resolvers/image_resolver/get_all_by_collection_id.py
  • lightly_studio/src/lightly_studio/resolvers/image_resolver/get_for_export.py
  • lightly_studio/tests/resolvers/image_resolver/test_get_for_export.py

@LeonardoRosaa

Copy link
Copy Markdown
Contributor Author

/review

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
lightly_studio/src/lightly_studio/resolvers/image_resolver/get_for_export.py (1)

94-94: 🚀 Performance & Scalability | 🟠 Major | ⚡ Quick win

Use yield_per to prevent buffering all rows in memory.

Although get_for_export returns a generator, session.exec(query) will execute the statement and buffer all matching ORM instances into memory before yielding the first row. For a collection export generator, this can cause Out-Of-Memory (OOM) errors on large datasets.

Use .execution_options(yield_per=...) to stream the results in memory-efficient chunks. (Note: yield_per is fully compatible with both contains_eager and selectinload in modern SQLAlchemy).

⚡ Proposed fix
-    return (ImageSample(row) for row in session.exec(query))
+    query = query.execution_options(yield_per=1000)
+    return (ImageSample(row) for row in session.exec(query))
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lightly_studio/src/lightly_studio/resolvers/image_resolver/get_for_export.py`
at line 94, Update the query execution in get_for_export to apply SQLAlchemy
execution_options(yield_per=...) before session.exec, so ORM rows are streamed
in chunks rather than buffered entirely; preserve the existing ImageSample
generator behavior and eager-loading options.
🧹 Nitpick comments (1)
lightly_studio/src/lightly_studio/resolvers/image_resolver/get_for_export.py (1)

85-87: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Unset embedding_region after resolving it to prevent stale filter processing.

When replacing the embedding_region filter with concrete region_sample_ids, explicitly unset the embedding_region field in the copied model. This cleanly discards the unresolvable polygon state and ensures that downstream query builders (like build_sample_ids_query) do not redundantly process it or raise unsupported-filter errors.

♻️ Proposed fix
-            resolved_sample_filter = sample_filter.model_copy(
-                update={"region_sample_ids": region_sample_ids}
-            )
+            resolved_sample_filter = sample_filter.model_copy(
+                update={
+                    "region_sample_ids": region_sample_ids,
+                    "embedding_region": None,
+                }
+            )
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lightly_studio/src/lightly_studio/resolvers/image_resolver/get_for_export.py`
around lines 85 - 87, Update the copied filter construction in the export
resolver so resolving embedding_region into region_sample_ids also explicitly
clears embedding_region. Ensure downstream query builders such as
build_sample_ids_query receive the concrete region IDs without retaining the
original polygon filter.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In
`@lightly_studio/src/lightly_studio/resolvers/image_resolver/get_for_export.py`:
- Line 94: Update the query execution in get_for_export to apply SQLAlchemy
execution_options(yield_per=...) before session.exec, so ORM rows are streamed
in chunks rather than buffered entirely; preserve the existing ImageSample
generator behavior and eager-loading options.

---

Nitpick comments:
In
`@lightly_studio/src/lightly_studio/resolvers/image_resolver/get_for_export.py`:
- Around line 85-87: Update the copied filter construction in the export
resolver so resolving embedding_region into region_sample_ids also explicitly
clears embedding_region. Ensure downstream query builders such as
build_sample_ids_query receive the concrete region IDs without retaining the
original polygon filter.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 8f9dcfc6-077a-4581-afd7-0b403416048f

📥 Commits

Reviewing files that changed from the base of the PR and between 30cc00a and d488a54.

📒 Files selected for processing (3)
  • lightly_studio/src/lightly_studio/resolvers/image_resolver/__init__.py
  • lightly_studio/src/lightly_studio/resolvers/image_resolver/get_for_export.py
  • lightly_studio/tests/resolvers/image_resolver/test_get_for_export.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • lightly_studio/src/lightly_studio/resolvers/image_resolver/init.py
  • lightly_studio/tests/resolvers/image_resolver/test_get_for_export.py

@horatiualmasan horatiualmasan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@LeonardoRosaa
LeonardoRosaa enabled auto-merge July 15, 2026 13:57
@LeonardoRosaa
LeonardoRosaa added this pull request to the merge queue Jul 15, 2026
Merged via the queue into main with commit faccddc Jul 15, 2026
23 checks passed
@LeonardoRosaa
LeonardoRosaa deleted the leonardo-lig-8988-figure-out-add-context-to-sampling-and-export-get-for-export branch July 15, 2026 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants