Skip to content

Add a notebook#35

Merged
SarahAlidoost merged 12 commits into
mainfrom
add_nb
Oct 17, 2025
Merged

Add a notebook#35
SarahAlidoost merged 12 commits into
mainfrom
add_nb

Conversation

@SarahAlidoost
Copy link
Copy Markdown
Collaborator

@SarahAlidoost SarahAlidoost commented Sep 30, 2025

closes #17

The notebook is docs/notebooks/optimization.ipynb

🔴 To reviewers, please install the package from this branch to be able to run the notebook. Because the latest version is not on pypi yet.

@SarahAlidoost SarahAlidoost marked this pull request as ready for review September 30, 2025 14:23
Comment thread docs/notebooks/optimization.ipynb
Copy link
Copy Markdown
Collaborator

@fnattino fnattino left a comment

Choose a reason for hiding this comment

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

Nice work @SarahAlidoost, looks very clear to me! I have left only a few minor comments below.

Comment thread docs/notebooks/optimization.ipynb
Comment thread src/diffwofost/physical_models/utils.py
Copy link
Copy Markdown
Collaborator

@michielkallenberg michielkallenberg left a comment

Choose a reason for hiding this comment

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

I had a look at the code.
One suggestion; Perhaps it may be appreciated if it could be run on e.g. google-colab.

For example:
add a banner:
Open In Colab,

[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/WUR-AI/diffWOFOST/blob/add_nb/docs/notebooks/optimization.ipynb)

and then this cell to install packages....

from mpmath import hyper
import os
import sys
import subprocess # Added for subprocess

# Clone the repository
!rm -rf diffWOFOST/
!git clone -b add_nb https://github.com/WUR-AI/diffWOFOST.git

# Change directory
%cd diffWOFOST

# Get the absolute path of the project root *after* changing directory
PROJECT_ROOT = os.getcwd()

# Install poetry if needed.
!pip install -qqq poetry

# Configure poetry to create virtual environments in the project directory
!poetry config virtualenvs.in-project true

# Install project dependencies
# This step may take some time (e.g., 5 minutes)
!poetry install --quiet --all-extras

# 1. Add the project root to sys.path
if PROJECT_ROOT not in sys.path:
    sys.path.insert(0, PROJECT_ROOT)
    print(f"✅ Added project root {PROJECT_ROOT} to sys.path")

# 1b. Add the 'src' directory (needed because the package lives inside src/)
SRC_PATH = os.path.join(PROJECT_ROOT, "src")
if os.path.exists(SRC_PATH) and SRC_PATH not in sys.path:
    sys.path.insert(0, SRC_PATH)
    print(f"✅ Added src path {SRC_PATH} to sys.path")
else:
    print(f"⚠️ Warning: src path {SRC_PATH} does not exist or already in sys.path")

# 2. Get the path to the site-packages directory of the poetry virtual environment
try:
    venv_path_output = subprocess.check_output(
        ['poetry', 'env', 'info', '--path']
    ).decode('utf-8').strip()
    python_version_major_minor = f"python{sys.version_info.major}.{sys.version_info.minor}"
    SITE_PACKAGES_PATH = os.path.join(
        venv_path_output, 'lib', python_version_major_minor, 'site-packages'
    )
except Exception as e:
    print(f"❌ Could not determine poetry venv site-packages path using 'poetry env info': {e}")
    print("Attempting a common fallback path structure...")
    SITE_PACKAGES_PATH = os.path.abspath(
        os.path.join('.venv', 'lib', f'python{sys.version_info.major}.{sys.version_info.minor}', 'site-packages')
    )

# 3. Add the site-packages directory to sys.path
if os.path.exists(SITE_PACKAGES_PATH) and SITE_PACKAGES_PATH not in sys.path:
    sys.path.insert(0, SITE_PACKAGES_PATH)
    print(f"✅ Added {SITE_PACKAGES_PATH} to sys.path")
else:
    print(f"⚠️ Warning: Could not find site-packages at {SITE_PACKAGES_PATH} or it's already in sys.path.")

then continue with the existing code.

@SarahAlidoost
Copy link
Copy Markdown
Collaborator Author

I had a look at the code. One suggestion; Perhaps it may be appreciated if it could be run on e.g. google-colab.

For example: add a banner: Open In Colab,

[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/WUR-AI/diffWOFOST/blob/add_nb/docs/notebooks/optimization.ipynb)

and then this cell to install packages....

from mpmath import hyper
import os
import sys
import subprocess # Added for subprocess

# Clone the repository
!rm -rf diffWOFOST/
!git clone -b add_nb https://github.com/WUR-AI/diffWOFOST.git

# Change directory
%cd diffWOFOST

# Get the absolute path of the project root *after* changing directory
PROJECT_ROOT = os.getcwd()

# Install poetry if needed.
!pip install -qqq poetry

# Configure poetry to create virtual environments in the project directory
!poetry config virtualenvs.in-project true

# Install project dependencies
# This step may take some time (e.g., 5 minutes)
!poetry install --quiet --all-extras

# 1. Add the project root to sys.path
if PROJECT_ROOT not in sys.path:
    sys.path.insert(0, PROJECT_ROOT)
    print(f"✅ Added project root {PROJECT_ROOT} to sys.path")

# 1b. Add the 'src' directory (needed because the package lives inside src/)
SRC_PATH = os.path.join(PROJECT_ROOT, "src")
if os.path.exists(SRC_PATH) and SRC_PATH not in sys.path:
    sys.path.insert(0, SRC_PATH)
    print(f"✅ Added src path {SRC_PATH} to sys.path")
else:
    print(f"⚠️ Warning: src path {SRC_PATH} does not exist or already in sys.path")

# 2. Get the path to the site-packages directory of the poetry virtual environment
try:
    venv_path_output = subprocess.check_output(
        ['poetry', 'env', 'info', '--path']
    ).decode('utf-8').strip()
    python_version_major_minor = f"python{sys.version_info.major}.{sys.version_info.minor}"
    SITE_PACKAGES_PATH = os.path.join(
        venv_path_output, 'lib', python_version_major_minor, 'site-packages'
    )
except Exception as e:
    print(f"❌ Could not determine poetry venv site-packages path using 'poetry env info': {e}")
    print("Attempting a common fallback path structure...")
    SITE_PACKAGES_PATH = os.path.abspath(
        os.path.join('.venv', 'lib', f'python{sys.version_info.major}.{sys.version_info.minor}', 'site-packages')
    )

# 3. Add the site-packages directory to sys.path
if os.path.exists(SITE_PACKAGES_PATH) and SITE_PACKAGES_PATH not in sys.path:
    sys.path.insert(0, SITE_PACKAGES_PATH)
    print(f"✅ Added {SITE_PACKAGES_PATH} to sys.path")
else:
    print(f"⚠️ Warning: Could not find site-packages at {SITE_PACKAGES_PATH} or it's already in sys.path.")

then continue with the existing code.

Thanks for the suggestion. I will add the colab badge to the documentation when working on #33. I added a cell to install the package using pip, so there is no need to clone the repository.

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
77.6% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@SarahAlidoost
Copy link
Copy Markdown
Collaborator Author

SarahAlidoost commented Oct 15, 2025

Nice work @SarahAlidoost, looks very clear to me! I have left only a few minor comments below.

can you please approve this? Then I merge it. The failing sonar is due to "coverage" because I moved the helper functions (used in tests too) to the utils module. Since the utils will be changed, I will skip adding tests.

@SarahAlidoost SarahAlidoost merged commit a4cdf02 into main Oct 17, 2025
9 of 11 checks passed
@SarahAlidoost SarahAlidoost deleted the add_nb branch October 17, 2025 08:19
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.

[Tasks]: Add a notebook showing how differentiable versions of two module work

4 participants