Refactor: Externalize prediction loading logic for database compatibility#59
Merged
Merged
Conversation
- Created detailed implementation plan - Documented current problems and proposed solution - Outlined testing strategy and backward compatibility approach Related to #58
- Implement load_predictions_from_filesystem() for CSV loading - Implement load_predictions_from_dataframe() for database compatibility - Implement apply_area_conversion() for unit conversions - Implement handle_duplicate_predictions() for deduplication - Implement standardize_prediction_columns() for format consistency - Add comprehensive docstrings and logging This enables database compatibility and separation of concerns. Related to #58
- 24 unit tests covering all prediction_loader functions - Tests for filesystem loading, DataFrame loading, conversions - Tests for duplicate handling and column standardization - Fix model name extraction for directory paths - Fix Q_obs preservation in duplicate handling All tests passing. Related to #58
- Add base_predictors and base_model_names parameters to __init__ - Store external predictions as instance attributes - Update __load_lr_predictors__ to use external predictions when provided - Add deprecation warning for internal file-based loading - Maintain backward compatibility with existing code Related to #58
- Add base_predictors and base_model_names parameters to __init__ - Store external predictions as instance attributes - Update __load_base_predictors__ to use external predictions when provided - Handle Q_ prefix normalization (external uses Q_, internal doesn't) - Add deprecation warning for internal file-based loading - Maintain backward compatibility with existing code Related to #58
- Document expected input formats for SciRegressor and BaseMetaLearner - Provide complete usage examples for filesystem and database loading - Explain key differences between model types - Include data validation guidelines and common issues - Add migration guide from old to new pattern - Cover advanced usage scenarios (APIs, custom conversions, filtering) Related to #58
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Refactors prediction loading logic out of model classes into a centralized
prediction_loadermodule, enabling database compatibility and improving separation of concerns.Resolves #58
Changes
New Module:
lt_forecasting/scr/prediction_loader.pyCreated centralized prediction loading utilities:
load_predictions_from_filesystem()- Load from CSV filesload_predictions_from_dataframe()- Load from pre-loaded DataFrames (database-ready)apply_area_conversion()- Apply area-based unit conversionshandle_duplicate_predictions()- Handle duplicate (date, code) pairsstandardize_prediction_columns()- Ensure consistent column namingUpdated Model Classes
SciRegressor:
base_predictorsandbase_model_namesoptional parameters to__init____load_lr_predictors__()to use external predictions when providedBaseMetaLearner:
base_predictorsandbase_model_namesoptional parameters to__init____load_base_predictors__()to use external predictions when providedTests
prediction_loadermoduleUsage
Old Way (still works with deprecation warning)
```python
model = SciRegressor(data, static_data, general_config, model_config,
feature_config, path_config)
```
New Way (recommended)
```python
from lt_forecasting.scr.prediction_loader import (
load_predictions_from_filesystem,
apply_area_conversion
)
Load predictions externally
base_preds, model_names = load_predictions_from_filesystem(
path_config["path_to_lr_predictors"],
join_type="inner"
)
Apply conversions if needed
base_preds = apply_area_conversion(base_preds, static_data, model_names)
Pass to model
model = SciRegressor(
data, static_data, general_config, model_config,
feature_config, path_config,
base_predictors=base_preds,
base_model_names=model_names
)
```
Database-Ready Pattern
```python
from lt_forecasting.scr.prediction_loader import load_predictions_from_dataframe
Load from database
df = load_from_database(query="SELECT * FROM predictions WHERE ...")
Convert to standard format
base_preds, model_names = load_predictions_from_dataframe(
df, model_names=['model1', 'model2']
)
Use with model
model = BaseMetaLearner(
data, static_data, general_config, model_config,
feature_config, path_config,
base_predictors=base_preds,
base_model_names=model_names
)
```
Benefits
Backward Compatibility
✅ All existing code continues to work
✅ No breaking changes
✅ Deprecation warnings guide users to new pattern
✅ All 191 tests passing
Test Plan
Migration Path
🤖 Generated with Claude Code