Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 21, 2026. It is now read-only.

[Feat] Add select_samples & improve print readability for BatchMeta#133

Merged
0oshowero0 merged 2 commits into
devfrom
han/metadata
Dec 13, 2025
Merged

[Feat] Add select_samples & improve print readability for BatchMeta#133
0oshowero0 merged 2 commits into
devfrom
han/metadata

Conversation

@0oshowero0

@0oshowero0 0oshowero0 commented Dec 13, 2025

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • New Features

    • Added ability to select specific samples from a batch while preserving all metadata and field information.
    • Improved batch metadata readability with enhanced string representation.
  • Tests

    • Comprehensive test coverage for batch sample selection across various scenarios, including edge cases and data type handling.

✏️ Tip: You can customize this high-level summary in your review settings.

Signed-off-by: 0oshowero0 <[email protected]>
Copilot AI review requested due to automatic review settings December 13, 2025 13:04
@coderabbitai

coderabbitai Bot commented Dec 13, 2025

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

The pull request adds a select_samples() method to BatchMeta for constructing new batches with specified samples, updates __getitem__, concat, and union methods to properly copy extra_info, and introduces a __str__ method for string representation. Comprehensive tests verify correct behavior across multiple scenarios.

Changes

Cohort / File(s) Change Summary
Core BatchMeta enhancements
transfer_queue/metadata.py
Added select_samples(sample_indices: list[int]) method to construct new BatchMeta with specified samples. Modified __getitem__ to return BatchMeta with copied extra_info when indexing by int. Updated concat and union methods to use extra_info.copy(). Added __str__ method for string representation.
Test coverage
tests/test_metadata.py
Added comprehensive tests for select_samples covering index selection, field metadata preservation, extra_info handling across types (tensors, strings, numbers, lists), single sample/empty/reverse-order selections, batch_index reassignment, and edge cases. Verifies original batch remains unchanged.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Pay particular attention to index reassignment logic in select_samples() and how global_indexes are updated
  • Verify extra_info copying behavior is consistent across all modified methods (__getitem__, concat, union)
  • Review edge case handling in tests (empty selections, reverse order, single-sample batches)
  • Ensure metadata field preservation is correctly validated across complex extra_info types

Poem

🐰 A hop through samples, picking just right,
Metadata preserved, indices take flight,
Extra info copied with bunny care,
New batches bloom from everywhere—
Select with joy! The code is fair! ✨


Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

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

@0oshowero0

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Dec 13, 2025

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copilot AI 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.

Pull request overview

This PR adds a new select_samples method to the BatchMeta class for selecting specific samples by index, improves print readability by implementing a __str__ method, and fixes inconsistent handling of the extra_info dictionary to prevent unintended sharing of mutable state between BatchMeta instances.

  • Adds select_samples(sample_indices) method to create new BatchMeta with only specified samples
  • Implements __str__ method to provide readable string representation of BatchMeta instances
  • Fixes __getitem__ and union methods to properly copy extra_info dictionary

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
transfer_queue/metadata.py Added select_samples method for sample selection, implemented __str__ for better debugging, and fixed extra_info handling in __getitem__ and union to prevent mutable state sharing
tests/test_metadata.py Comprehensive test suite for select_samples including empty list, single sample, all samples, reverse order, and extra_info preservation scenarios

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_metadata.py Outdated
Comment thread transfer_queue/metadata.py
Comment thread transfer_queue/metadata.py Outdated
Comment thread tests/test_metadata.py
Comment on lines +538 to +686
def test_batch_meta_select_samples(self):
"""Example: Select specific samples from a batch."""
fields = {
"field1": FieldMeta(name="field1", dtype=torch.float32, shape=(2,)),
"field2": FieldMeta(name="field2", dtype=torch.int64, shape=(3,)),
}
samples = [
SampleMeta(partition_id="partition_0", global_index=0, fields=fields),
SampleMeta(partition_id="partition_0", global_index=1, fields=fields),
SampleMeta(partition_id="partition_0", global_index=2, fields=fields),
SampleMeta(partition_id="partition_0", global_index=3, fields=fields),
]
batch = BatchMeta(samples=samples, extra_info={"test_key": "test_value"})

# Select samples at indices [0, 2]
selected_batch = batch.select_samples([0, 2])

# Check number of samples
assert len(selected_batch) == 2
# Check global indexes
assert selected_batch.global_indexes == [0, 2]
# Check fields are preserved
for sample in selected_batch.samples:
assert "field1" in sample.fields
assert "field2" in sample.fields
# Original batch is unchanged
assert len(batch) == 4
# Extra info is preserved
assert selected_batch.extra_info["test_key"] == "test_value"

def test_batch_meta_select_samples_all_indices(self):
"""Example: Select all samples using complete index list."""
fields = {
"test_field": FieldMeta(
name="test_field", dtype=torch.float32, shape=(2,), production_status=ProductionStatus.READY_FOR_CONSUME
)
}
samples = [
SampleMeta(partition_id="partition_0", global_index=0, fields=fields),
SampleMeta(partition_id="partition_0", global_index=1, fields=fields),
SampleMeta(partition_id="partition_0", global_index=2, fields=fields),
]
batch = BatchMeta(samples=samples, extra_info={"test_key": "test_value"})

# Select all samples
selected_batch = batch.select_samples([0, 1, 2])

# All samples are selected
assert len(selected_batch) == 3
assert selected_batch.global_indexes == [0, 1, 2]
# Extra info is preserved
assert selected_batch.extra_info["test_key"] == "test_value"

def test_batch_meta_select_samples_single_sample(self):
"""Example: Select a single sample from batch."""
fields = {
"test_field": FieldMeta(
name="test_field", dtype=torch.float32, shape=(2,), production_status=ProductionStatus.READY_FOR_CONSUME
)
}
samples = [
SampleMeta(partition_id="partition_0", global_index=0, fields=fields),
SampleMeta(partition_id="partition_0", global_index=1, fields=fields),
SampleMeta(partition_id="partition_0", global_index=2, fields=fields),
]
batch = BatchMeta(samples=samples)

# Select only the middle sample
selected_batch = batch.select_samples([1])

assert len(selected_batch) == 1
assert selected_batch.global_indexes == [1]
assert selected_batch.samples[0].batch_index == 0 # New batch index

def test_batch_meta_select_samples_empty_list(self):
"""Example: Select with empty list returns empty batch."""
fields = {
"test_field": FieldMeta(
name="test_field", dtype=torch.float32, shape=(2,), production_status=ProductionStatus.READY_FOR_CONSUME
)
}
samples = [
SampleMeta(partition_id="partition_0", global_index=0, fields=fields),
SampleMeta(partition_id="partition_0", global_index=1, fields=fields),
]
batch = BatchMeta(samples=samples, extra_info={"test_key": "test_value"})

# Select with empty list
selected_batch = batch.select_samples([])

assert len(selected_batch) == 0
assert selected_batch.global_indexes == []
# Extra info is still preserved
assert selected_batch.extra_info["test_key"] == "test_value"

def test_batch_meta_select_samples_reverse_order(self):
"""Example: Select samples in reverse order."""
fields = {
"test_field": FieldMeta(
name="test_field", dtype=torch.float32, shape=(2,), production_status=ProductionStatus.READY_FOR_CONSUME
)
}
samples = [
SampleMeta(partition_id="partition_0", global_index=0, fields=fields),
SampleMeta(partition_id="partition_0", global_index=1, fields=fields),
SampleMeta(partition_id="partition_0", global_index=2, fields=fields),
]
batch = BatchMeta(samples=samples)

# Select samples in reverse order
selected_batch = batch.select_samples([2, 1, 0])

assert len(selected_batch) == 3
assert selected_batch.global_indexes == [2, 1, 0]
# Batch indexes are re-assigned
assert selected_batch.samples[0].global_index == 2
assert selected_batch.samples[1].global_index == 1
assert selected_batch.samples[2].global_index == 0

def test_batch_meta_select_samples_with_extra_info(self):
"""Example: Select samples preserves all extra info types."""
fields = {
"test_field": FieldMeta(
name="test_field", dtype=torch.float32, shape=(2,), production_status=ProductionStatus.READY_FOR_CONSUME
)
}
samples = [
SampleMeta(partition_id="partition_0", global_index=0, fields=fields),
SampleMeta(partition_id="partition_0", global_index=1, fields=fields),
]
batch = BatchMeta(samples=samples)

print(batch)

# Add various extra info types
batch.extra_info["tensor"] = torch.randn(3, 4)
batch.extra_info["string"] = "test_string"
batch.extra_info["number"] = 42
batch.extra_info["list"] = [1, 2, 3]

# Select one sample
selected_batch = batch.select_samples([0])

# All extra info is preserved
assert "tensor" in selected_batch.extra_info
assert selected_batch.extra_info["string"] == "test_string"
assert selected_batch.extra_info["number"] == 42
assert selected_batch.extra_info["list"] == [1, 2, 3]

Copilot AI Dec 13, 2025

Copy link

Choose a reason for hiding this comment

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

Consider adding test coverage for invalid index scenarios, such as out-of-bounds indices (e.g., select_samples([5]) on a 3-element batch) or negative indices. This would help ensure the method handles these edge cases gracefully and provides clear error messages to users.

Copilot uses AI. Check for mistakes.
Comment thread transfer_queue/metadata.py
Comment thread transfer_queue/metadata.py
Signed-off-by: 0oshowero0 <[email protected]>
@0oshowero0 0oshowero0 merged commit 32d14bd into dev Dec 13, 2025
3 checks passed
@0oshowero0 0oshowero0 mentioned this pull request Dec 15, 2025
22 tasks
@0oshowero0 0oshowero0 deleted the han/metadata branch December 17, 2025 09:33
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants