Skip to content

[WIP] Refactor forecast #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open

[WIP] Refactor forecast #39

wants to merge 30 commits into from

Conversation

isaacaka
Copy link
Collaborator

@isaacaka isaacaka commented Mar 4, 2025

The aim is to refactor the forecast.py code to make it easier to swap between different dimensionality reduction and forecasting techniques.

Refactored jupyter notebook, main_forecast and main to reflect changes to the refactored libraries. The jumper notebook is specific to using normal PCA (not Kernal PCA)
Separated code into forecasting technique classes and dimensionality reduction classes
Forecast.py calls the methods defined in the above two classes
The DR and forecasting techniques created are imported and stored in a dictionary
The DR and forecasting techniques the user wants to use can be specified by setting the name within "techniques_config.yaml"

Ocean_terms.yaml defines the names of the terms stored in the NEMO grid files, e.g. different configurations of nemo use different names for temperature, ssh and salinity. You can specify which names are used in the yaml file. Restart.py will then be able to read the correct keys.

Tested that the refactored code runs end-to-end

Test written against the non-refactored code need to be written.

@ma595 ma595 marked this pull request as draft April 9, 2025 19:39
@ma595 ma595 changed the title Refactor forecast [DRAFT] Refactor forecast Apr 9, 2025
@ma595 ma595 changed the title [DRAFT] Refactor forecast [WIP] Refactor forecast Apr 9, 2025
@ma595 ma595 mentioned this pull request Apr 17, 2025
…gnature for get_component

Creates a setter method to pass neccessary variables from simulation class to the decompostion class
Corrects method signature for get_component in forecast.py and dimensionality_reduction.py
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@isaacaka isaacaka requested review from ma595 and surbhigoel77 April 23, 2025 15:11
@isaacaka isaacaka marked this pull request as ready for review April 24, 2025 09:06
@Copilot Copilot AI review requested due to automatic review settings April 24, 2025 09:06
Copy link

@Copilot 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 the forecasting code by separating dimensionality reduction and forecasting techniques into dedicated classes, updating configuration files, and modifying related scripts for consistency. Key changes include updating function signatures (e.g. adding a filename parameter to prepare), refactoring PCA/decomposition routines to leverage strategy classes, and adjusting forecast prediction reconstruction and error evaluation.

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
techniques_config.yaml Added configuration entries to specify DR and forecast techniques.
ocean_terms.yaml Introduced a YAML file to map ocean term names for flexible usage.
main_forecast.py Updated function signatures, logging, and file handling logic.
lib/restart.py Added YAML loading for ocean terms in restart functions.
lib/forecast_technique.py Refactored forecast techniques including direct and recursive approaches.
lib/forecast.py Modified simulation loading, decomposition, reconstruction and forecasting logic.
lib/dimensionality_reduction.py Updated PCA and KernelPCA implementations with new methods for reconstruction.
Comments suppressed due to low confidence (3)

main_forecast.py:557

  • The revised logic for generating x_pred may not cover the full intended time range; please verify that the prediction array handles the simulation length and forecast steps as expected.
x_pred = np.arange(1 + pas, 1 + steps * pas, pas).reshape(-1, 1)

lib/forecast.py:156

  • The comparison 'if self.len < self.end' may lead to errors if self.end is None; ensure that self.end is always set to a valid integer value or introduce a conditional branch to handle the None case.
array = [self.loadFile(file) for file in self.files if self.len < self.end]

main_forecast.py:85

  • [nitpick] Minor typo: 'forcasted' should be corrected to 'forecasted'.
print(f"{term} time series forcasted")

Comment on lines +58 to +66
if (
hasattr(self.regressor, "predict")
and "return_std" in self.regressor.predict.__code__.co_varnames
):
y_hat, y_hat_std = self.regressor.predict(x_pred, return_std=True)
return y_hat, y_hat_std
else:
y_hat = self.regressor.predict(x_pred)
return y_hat, None
Copy link
Preview

Copilot AI Apr 24, 2025

Choose a reason for hiding this comment

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

Using code to check for the 'return_std' parameter may fail for built-in or compiled functions; consider using a more robust approach (e.g. try/except) to detect if standard deviation can be returned.

Suggested change
if (
hasattr(self.regressor, "predict")
and "return_std" in self.regressor.predict.__code__.co_varnames
):
y_hat, y_hat_std = self.regressor.predict(x_pred, return_std=True)
return y_hat, y_hat_std
else:
y_hat = self.regressor.predict(x_pred)
return y_hat, None
if hasattr(self.regressor, "predict"):
try:
# Attempt to call predict with return_std=True
y_hat, y_hat_std = self.regressor.predict(x_pred, return_std=True)
return y_hat, y_hat_std
except TypeError:
# If TypeError is raised, fall back to predict without return_std
y_hat = self.regressor.predict(x_pred)
return y_hat, None
else:
raise AttributeError("The regressor does not have a 'predict' method.")

Copilot uses AI. Check for mistakes.

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.

1 participant