Skip to content

Add optional metadata output (DatasetSpecs fields) to DataLoader - #27

Merged
fercer merged 7 commits into
mainfrom
copilot/add-filename-data-scale-outputs
Jan 5, 2026
Merged

Add optional metadata output (DatasetSpecs fields) to DataLoader#27
fercer merged 7 commits into
mainfrom
copilot/add-filename-data-scale-outputs

Conversation

Copilot AI commented Dec 31, 2025

Copy link
Copy Markdown
Contributor

Extends ZarrDataset to optionally return complete DatasetSpecs metadata alongside samples for tracking sample provenance during training/inference workflows.

Changes

  • Added return_metadata parameter to ZarrDataset: When enabled, appends metadata dict as last element in output tuple containing all standard DatasetSpecs fields for each modality: filenames (string representation of source), source_axes, axes, data_group (resolution level), and roi (formatted as human-readable string)

  • Created zarrdataset_collate_fn: Custom collate function required for PyTorch DataLoader to properly batch non-numeric metadata separately from tensors. Detects metadata by checking for expected DatasetSpecs keys.

  • Added format_roi() helper function: Converts ROI slices to human-readable string format (start_coords):(lengths), consistent with the ROI parsing convention (e.g., (10,20,0):(100,100,-1) or : for full selection)

  • Maintains backward compatibility: Metadata placed last in output tuple (after worker_id, positions, arrays). Existing code unaffected when flag disabled.

Usage

import zarrdataset as zds
from torch.utils.data import DataLoader

ds = zds.ZarrDataset(
    dataset_specs=specs,
    return_metadata=True
)

loader = DataLoader(
    ds,
    batch_size=16,
    collate_fn=zds.zarrdataset_collate_fn,
    worker_init_fn=zds.zarrdataset_worker_init_fn
)

for *tensors, metadata_list in loader:
    for metadata in metadata_list:
        # metadata is dict keyed by modality (e.g., 'images', 'labels')
        for modality, mod_meta in metadata.items():
            # mod_meta contains: filenames, source_axes, axes, data_group, roi
            print(f"{modality}: {mod_meta['filenames']}, roi: {mod_meta['roi']}")

Output Order

  • When combined with other flags: worker_idpositionsarraysmetadata
  • Metadata always last when enabled
Original prompt

This section details on the original issue you should resolve

<issue_title>Add Optional filename and data_scale Outputs to DataLoader</issue_title>
<issue_description># Add Optional filename and dataset_scale Outputs to DataLoader

Summary

Propose extending the current DataLoader behavior to optionally return filename and data_scale alongside each data sample during training or inference.

Motivation

In some workflows (e.g., inference, debugging, or evaluation), it's valuable to track which file a given sample originated from, as well as the dataset resolution or zoom level (data_scale). Currently, this metadata is not returned from the DataLoader, making it harder to:

  • Associate predictions with specific files or scales
  • Log or visualize results contextually
  • Perform file- or scale-specific postprocessing or filtering

Proposed Change

Add a flag (e.g. return_metadata: bool = False) to the dataset/dataloader spec that, when enabled, changes the output format from: x to: x, metadata, where metadata is a dictionary or tuple containing:

{
  "filename": str,
  "dataset_scale": Optional[int]  # or float if appropriate
}

Alternatively, the return could be a named tuple for consistency.

Usage Example

for x, meta in dataloader:
    print(meta["filename"], meta["dataset_scale"])

Impact

This change would be fully backward-compatible. By default, the DataLoader would continue returning only x. When return_metadata=True, the additional values are returned.

Open Questions

  • Should metadata be a dictionary, namedtuple, or part of a structured dataclass?
  • Should the dataset_scale be inferred, passed in, or tied to the sample's spec?
  • Are there training scenarios where metadata should be excluded for performance?</issue_description>

Comments on the Issue (you are @copilot in this section)

@fercer Sorry for the late follow up.

Yes, I think making all relevant metadata generated with the sampling process is valuable and would require some sort of structure.
Currently that is being yielded as additional outputs with return_positions and return_worker_id arguments.
The reason is that those values can be converted into pytroch tensors and handled by the default collate function.

For returning non-numeric values, like the filenames or other metadata, a custom collate function would be necesarry.
That function could merge all the metadata into a single dictionary as you suggested.
The collate function would be shipped within ZarrDataset, just as the worker_init_fn that should be passed to the DataLoader.

I'll start looking into how can the metadata be collated and delivered, any other ideas are welcome!</comment_new>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 31, 2025 16:08
- Added return_metadata parameter to ZarrDataset.__init__
- Metadata includes filename and data_scale from collection
- Metadata is appended as last element in output tuple
- Created zarrdataset_collate_fn to handle non-numeric metadata
- Exported collate function in __init__.py
- Added comprehensive tests for metadata functionality

Co-authored-by: fercer <17553283+fercer@users.noreply.github.com>
- Clarified data_scale documentation
- Improved metadata detection logic in collate function
- Cleaned up zarr attribute checking
- Fixed line continuation in test file

Co-authored-by: fercer <17553283+fercer@users.noreply.github.com>
Copilot AI changed the title [WIP] Add optional filename and data scale outputs to DataLoader Add optional metadata output (filename, data_scale) to DataLoader Dec 31, 2025
Copilot AI requested a review from fercer December 31, 2025 16:15
Comment thread zarrdataset/_zarrdataset.py Outdated
return_worker_id: bool
Return the worker id that extracted the sample.
return_metadata: bool
Return metadata dictionary containing `filename` and `data_scale` for

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This really should return the standard metadata from the DatasetSpecs class

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated the docstring to clarify that return_metadata returns all standard DatasetSpecs fields (filenames, source_axes, axes, data_group, roi) for each modality. (3883974)

Comment thread zarrdataset/_zarrdataset.py Outdated
source_axes=mod_metadata[im_id]["source_axes"],
axes=mod_metadata[im_id]["axes"],
data_group=mod_metadata[im_id]["data_group"],
roi=mod_metadata[im_id]["roi"]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should roi be also converted into a string. Maybe with the format used to define ROIs within the same context.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added format_roi() helper function that converts ROI slices to string format (start_coords):(lengths), consistent with the format used in the metadata parsing context. For example: (0,10,5):(100,50,3) or : for full slices. (3883974)

- Updated docstring to reflect that return_metadata returns all standard DatasetSpecs fields
- Added format_roi() helper function to convert ROI slices to string format
- ROI is now returned as string in format "(start):(length)" for better readability

Co-authored-by: fercer <17553283+fercer@users.noreply.github.com>
Copilot AI changed the title Add optional metadata output (filename, data_scale) to DataLoader Add optional metadata output (DatasetSpecs fields) to DataLoader Jan 2, 2026
Copilot AI requested a review from fercer January 2, 2026 16:54
@fercer
fercer marked this pull request as ready for review January 2, 2026 19:01
@fercer
fercer merged commit bc6a843 into main Jan 5, 2026
2 checks passed
@fercer
fercer deleted the copilot/add-filename-data-scale-outputs branch January 5, 2026 14:00
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.

Add Optional filename and data_scale Outputs to DataLoader

2 participants