Skip to content

Refactor: Extract duplicated code from test and infer verbs into shared utilities#646

Merged
drewoldag merged 9 commits intomainfrom
copilot/refactor-code-structure
Feb 5, 2026
Merged

Refactor: Extract duplicated code from test and infer verbs into shared utilities#646
drewoldag merged 9 commits intomainfrom
copilot/refactor-code-structure

Conversation

Copy link
Contributor

Copilot AI commented Jan 28, 2026

Change Description

Two verb modules (test.py and infer.py) contained significant code duplication for model weight loading and batch result saving.

Solution Description

Extracted shared logic into reusable utilities:

  • load_model_weights()models/model_utils.py
    Consolidates ~96 lines of duplicated code from test.py and infer.py. Handles explicit path or auto-detection from recent training run. Parameterized by verb name.

  • create_save_batch_callback()pytorch_ignite.py
    Consolidates ~84 lines of duplicated code from test.py and infer.py. Factory function returns closure that saves model outputs batch-by-batch during inference/testing. Includes validation and error handling to ensure batches contain required object_id field, with clear error messages to guide dataset implementation.

Scope: Only test.py and infer.py were refactored. The train.py verb remains unchanged and retains its own _log_params method. Similarly, test.py has its own _log_params method (not shared with train).

Impact: Reduced code duplication in test and infer verbs while maintaining clear separation of concerns between verbs. Removed unused variables (batch_len, write_index) from the callback implementation for cleaner code.

Breaking changes: None. All verb interfaces unchanged. The callback now requires datasets to provide object_id in batch dictionaries, which was already the expected behavior in the original implementation.

Code Quality

  • I have read the Contribution Guide and agree to the Code of Conduct
  • My code follows the code style of this project
  • My code builds (or compiles) cleanly without any errors or warnings
  • My code contains relevant comments and necessary documentation
Original prompt

@copilot, will you take a moment to address this refactor?


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Refactor code structure for better maintainability Refactor: Extract duplicated code from verb modules into shared utilities Jan 28, 2026
Copilot AI requested a review from drewoldag January 28, 2026 20:15
@codecov
Copy link

codecov bot commented Jan 28, 2026

Codecov Report

❌ Patch coverage is 82.45614% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 62.96%. Comparing base (808dbfa) to head (d94afb1).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/hyrax/models/model_utils.py 76.00% 6 Missing ⚠️
src/hyrax/pytorch_ignite.py 73.33% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #646      +/-   ##
==========================================
+ Coverage   62.95%   62.96%   +0.01%     
==========================================
  Files          58       59       +1     
  Lines        5782     5730      -52     
==========================================
- Hits         3640     3608      -32     
+ Misses       2142     2122      -20     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link

github-actions bot commented Feb 4, 2026

Before [808dbfa] After [3b5ebea] Ratio Benchmark (Parameter)
3.74G 3.88G 1.04 vector_db_benchmarks.VectorDBInsertBenchmarks.peakmem_load_vector_db(16384, 'qdrant')
1.91±0.01s 1.96±0.03s 1.03 benchmarks.time_rebuild_manifest_help
16.8962717254681 17.46608516124805 1.03 data_cache_benchmarks.DataCacheBenchmarks.track_cache_hsc1k_hyrax_size_undercount
113±1μs 116±2μs 1.03 data_request_benchmarks.DatasetRequestBenchmarks.time_request_all_data
9.17±0.1ms 9.34±0.1ms 1.02 vector_db_benchmarks.VectorDBSearchBenchmarks.time_search_by_vector_many_shards(128, 'chromadb')
1.92±0.02s 1.94±0.02s 1.01 benchmarks.time_lookup_help
36.7±0.4ms 37.0±0.1ms 1.01 benchmarks.time_nb_obj_construct
1.91±0.01s 1.94±0.03s 1.01 benchmarks.time_prepare_help
1.91±0.01s 1.93±0.01s 1.01 benchmarks.time_train_help
1.27G 1.29G 1.01 vector_db_benchmarks.VectorDBInsertBenchmarks.peakmem_load_vector_db(2048, 'qdrant')

Click here to view all benchmarks.

…that `batch["object_id"]` will be used for the ids.
Copy link
Collaborator

@drewoldag drewoldag left a comment

Choose a reason for hiding this comment

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

@copilot, please address the two comments where you are explicitly called out.

- Move load_model_weights from config_utils.py to models/model_utils.py
- Remove log_mlflow_params from config_utils.py
- Restore _log_params method in test.py (no longer using shared function)
- Revert all changes to train.py (back to original state)
- Update imports in test.py and infer.py to use models.model_utils
- Update tests to import from new location

Co-authored-by: drewoldag <47493171+drewoldag@users.noreply.github.com>
Copilot AI changed the title Refactor: Extract duplicated code from verb modules into shared utilities Refactor: Extract duplicated code from test and infer verbs into shared utilities Feb 4, 2026
Copilot AI requested a review from drewoldag February 4, 2026 23:00
@drewoldag drewoldag marked this pull request as ready for review February 4, 2026 23:58
Copilot AI review requested due to automatic review settings February 4, 2026 23:58
@drewoldag drewoldag requested a review from mtauraso February 5, 2026 00:02
Copy link
Contributor

Copilot AI left a comment

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 refactors duplicated code from the test.py and infer.py verbs by extracting shared functionality into reusable utilities. The refactoring consolidates model weight loading and batch result saving logic, reducing code duplication and improving maintainability.

Changes:

  • Extracted load_model_weights() function to models/model_utils.py for shared model weight loading logic
  • Extracted create_save_batch_callback() factory function to pytorch_ignite.py for creating batch result saving callbacks
  • Updated test.py and infer.py to use the new shared utilities, removing ~180 lines of duplicated code
  • Updated test imports and function calls to reference the new utility functions

Reviewed changes

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

Show a summary per file
File Description
src/hyrax/models/model_utils.py New file containing load_model_weights() utility function that consolidates model weight loading logic from test and infer verbs
src/hyrax/pytorch_ignite.py Added create_save_batch_callback() factory function that creates closures for saving batch results during inference/testing
src/hyrax/verbs/test.py Removed load_model_weights() static method and _save_batch nested function, replaced with calls to new shared utilities
src/hyrax/verbs/infer.py Removed load_model_weights() static method and _save_batch nested function, replaced with calls to new shared utilities
tests/hyrax/test_infer.py Updated test imports to reference new load_model_weights() location in models/model_utils.py

Comment on lines 775 to 776
data_loader_indexes : array-like
Indices mapping data loader order to dataset order
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

The data_loader_indexes parameter is documented and accepted but never used in the function body. The old implementations in test.py and infer.py used this parameter as a fallback when batch didn't contain object_id. Consider either removing this parameter if it's truly unnecessary, or documenting why it's passed but unused.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

@mtauraso mtauraso left a comment

Choose a reason for hiding this comment

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

This looks good to me

Copy link
Collaborator

@drewoldag drewoldag left a comment

Choose a reason for hiding this comment

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

@copilot please address PR comments.

…allback

- Remove unused batch_len variable
- Remove unused write_index variable and nonlocal reference
- Add error handling to log and raise RuntimeError if batch doesn't contain object_id
- Update docstring to remove mention of "indexes" parameter

Co-authored-by: drewoldag <47493171+drewoldag@users.noreply.github.com>
Copy link
Collaborator

@drewoldag drewoldag left a comment

Choose a reason for hiding this comment

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

Looking good now

@drewoldag drewoldag enabled auto-merge (squash) February 5, 2026 00:31
@drewoldag drewoldag merged commit a800e9c into main Feb 5, 2026
8 of 9 checks passed
@drewoldag drewoldag deleted the copilot/refactor-code-structure branch February 5, 2026 00:32
@drewoldag drewoldag linked an issue Feb 5, 2026 that may be closed by this pull request
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.

Pull _save_batch out of infer and test into single location

3 participants