Skip to content

Commit

Permalink
Improve testing (#5)
Browse files Browse the repository at this point in the history
* Add coverage test

* Remove coverage

* Update ci

* Run isort

* Change black version

* Black version

---------

Co-authored-by: Juliette-Gerbaux <[email protected]>
  • Loading branch information
Juliette-Gerbaux and Juliette-Gerbaux authored Dec 11, 2024
1 parent b709fa2 commit 51a4533
Show file tree
Hide file tree
Showing 27 changed files with 173 additions and 151 deletions.
22 changes: 21 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,31 @@ jobs:

- name: Test
run: |
pytest
pytest --cov --cov-report xml
- name: Archive code coverage results
uses: actions/upload-artifact@v4
with:
name: python-code-coverage-report
path: coverage.xml

- name: Check typing
run: |
python -m mypy src
- name: Check imports sort order
uses: isort/isort-action@v1
with:
sort-paths: src, tests
configuration: --profile black --check-only --diff
requirements-files: "requirements.txt requirements-dev.txt"

- name: Check formatting
uses: psf/black@stable
with:
# Version of Black should match the versions set in `requirements-dev.txt`
version: "~=24.3.0"
options: --check --diff



4 changes: 3 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
black
black==24.3.0
isort
mypy
pytest
coverage
pytest-cov
10 changes: 6 additions & 4 deletions src/calculate_reward_and_bellman_values.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from read_antares_data import TimeScenarioParameter, Reservoir, TimeScenarioIndex
from scipy.interpolate import interp1d
import numpy as np
from type_definition import Array1D, Array2D, Optional, Dict, Callable, List, Iterable
from itertools import product

import numpy as np
from scipy.interpolate import interp1d

from read_antares_data import Reservoir, TimeScenarioIndex, TimeScenarioParameter
from type_definition import Array1D, Array2D, Callable, Dict, Iterable, List, Optional


class ReservoirManagement:

Expand Down
5 changes: 3 additions & 2 deletions src/display.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import numpy as np
from tqdm import tqdm
import plotly.graph_objects as go
from read_antares_data import TimeScenarioParameter
from tqdm import tqdm

from multi_stock_bellman_value_calculation import MultiStockManagement
from read_antares_data import TimeScenarioParameter
from type_definition import Array1D, Dict


Expand Down
6 changes: 4 additions & 2 deletions src/estimation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Any, Dict, Optional, Union

import numpy as np
from typing import Dict, Union, Any, Optional
from read_antares_data import TimeScenarioParameter

from hyperplane_interpolation import get_interpolation
from read_antares_data import TimeScenarioParameter
from type_definition import Array1D


Expand Down
17 changes: 8 additions & 9 deletions src/functions_iterative.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from time import time

import numpy as np
from scipy.interpolate import interp1d
from time import time
from optimization import (
AntaresProblem,
Basis,
)
from read_antares_data import TimeScenarioParameter, TimeScenarioIndex

from calculate_reward_and_bellman_values import (
RewardApproximation,
ReservoirManagement,
BellmanValueCalculation,
MultiStockManagement,
MultiStockBellmanValueCalculation,
MultiStockManagement,
ReservoirManagement,
RewardApproximation,
)
from optimization import AntaresProblem, Basis
from read_antares_data import TimeScenarioIndex, TimeScenarioParameter
from type_definition import Array1D, Array2D, Array3D, Array4D, Dict, List


Expand Down
1 change: 1 addition & 0 deletions src/hyperplane_decomposition.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
from numpy.linalg import norm

from type_definition import Array1D


Expand Down
1 change: 1 addition & 0 deletions src/hyperplane_interpolation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Ceci est le ficiher des coupes gourmandes, l'utilité n'a malheuresement pas été prouvée et la méthode n'a pas été retenue
import numpy as np
from numpy.linalg import norm

from type_definition import Array1D


Expand Down
6 changes: 3 additions & 3 deletions src/launch_calculation.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from calculate_reward_and_bellman_values import ReservoirManagement
from functions_iterative import itr_control
from read_antares_data import TimeScenarioParameter
from simple_bellman_value_calculation import (
calculate_bellman_value_directly,
calculate_bellman_value_with_precalculated_reward,
)
from functions_iterative import itr_control
from read_antares_data import TimeScenarioParameter
from calculate_reward_and_bellman_values import ReservoirManagement
from type_definition import Array1D, Array2D


Expand Down
39 changes: 19 additions & 20 deletions src/multi_stock_bellman_value_calculation.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
import os as os
import pickle as pkl
from itertools import product
from pathlib import Path
from typing import Annotated, Any, Dict, Literal, Optional

import juliacall
import numpy as np
import numpy.typing as npt
import ortools.linear_solver.pywraplp as pywraplp
from ortools.linear_solver.python import model_builder
from scipy.stats import random_correlation
from tqdm import tqdm

from calculate_reward_and_bellman_values import (
RewardApproximation,
BellmanValueCalculation,
MultiStockManagement,
MultiStockBellmanValueCalculation,
MultiStockManagement,
RewardApproximation,
)

import ortools.linear_solver.pywraplp as pywraplp
from ortools.linear_solver.python import model_builder
from display import ConvergenceProgressBar, draw_usage_values, draw_uvs_sddp
from estimation import Estimator, LinearCostEstimator, LinearInterpolator
from read_antares_data import TimeScenarioParameter, TimeScenarioIndex

from optimization import (
AntaresProblem,
WeeklyBellmanProblem,
Basis,
solve_problem_with_multivariate_bellman_values,
WeeklyBellmanProblem,
solve_for_optimal_trajectory,
solve_problem_with_multivariate_bellman_values,
)
from typing import Annotated, Literal, Dict, Optional, Any
import numpy.typing as npt
import numpy as np

from display import ConvergenceProgressBar, draw_usage_values, draw_uvs_sddp
from scipy.stats import random_correlation
import pickle as pkl
from tqdm import tqdm
from pathlib import Path
import juliacall
import os as os
from read_antares_data import TimeScenarioIndex, TimeScenarioParameter

jl = juliacall.Main

Expand Down
16 changes: 9 additions & 7 deletions src/optimization.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import pickle as pkl
import re
from time import time
from typing import Optional

import numpy as np
import ortools.linear_solver.pywraplp as pywraplp
from ortools.linear_solver.python import model_builder
from scipy.interpolate import interp1d

from calculate_reward_and_bellman_values import (
BellmanValueCalculation,
MultiStockBellmanValueCalculation,
MultiStockManagement,
)
from estimation import Estimator, LinearInterpolator
from read_antares_data import TimeScenarioParameter
import numpy as np
from time import time
from scipy.interpolate import interp1d
from ortools.linear_solver.python import model_builder
import ortools.linear_solver.pywraplp as pywraplp
from type_definition import Array2D, List, Dict, Array1D, npt
import pickle as pkl
from type_definition import Array1D, Array2D, Dict, List, npt


class Basis:
Expand Down
5 changes: 3 additions & 2 deletions src/read_antares_data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from dataclasses import dataclass, field
from typing import Optional
import subprocess
from configparser import ConfigParser
from dataclasses import dataclass, field
from typing import Optional

import numpy as np


Expand Down
16 changes: 8 additions & 8 deletions src/simple_bellman_value_calculation.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import numpy as np
from scipy.interpolate import interp1d

from calculate_reward_and_bellman_values import (
RewardApproximation,
ReservoirManagement,
BellmanValueCalculation,
MultiStockManagement,
MultiStockBellmanValueCalculation,
MultiStockManagement,
ReservoirManagement,
RewardApproximation,
)
from read_antares_data import TimeScenarioParameter, TimeScenarioIndex
from optimization import AntaresProblem
from optimization import Basis
import numpy as np
from scipy.interpolate import interp1d
from functions_iterative import compute_upper_bound
from optimization import AntaresProblem, Basis
from read_antares_data import TimeScenarioIndex, TimeScenarioParameter
from type_definition import Array1D, Array2D, Dict


Expand Down
5 changes: 3 additions & 2 deletions src/type_definition.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy.typing as npt
from typing import Annotated, Callable, Dict, Iterable, List, Literal, Optional

import numpy as np
from typing import Annotated, Literal, Dict, List, Optional, Callable, Iterable
import numpy.typing as npt

Array1D = Annotated[npt.NDArray[np.float32], Literal["N"]]
Array2D = Annotated[npt.NDArray[np.float32], Literal["N", "N"]]
Expand Down
17 changes: 9 additions & 8 deletions tests/test_basis.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import numpy as np
import ortools.linear_solver.pywraplp as pywraplp
import pytest

from calculate_reward_and_bellman_values import MultiStockManagement
from functions_iterative import (
TimeScenarioParameter,
BellmanValueCalculation,
ReservoirManagement,
RewardApproximation,
TimeScenarioIndex,
TimeScenarioParameter,
compute_upper_bound,
BellmanValueCalculation,
RewardApproximation,
)
from read_antares_data import Reservoir
from optimization import AntaresProblem, Basis
from calculate_reward_and_bellman_values import MultiStockManagement
import pytest
import ortools.linear_solver.pywraplp as pywraplp
import numpy as np
from read_antares_data import Reservoir


def test_basis_with_xpress() -> None:
Expand Down
14 changes: 5 additions & 9 deletions tests/test_bellman_value_exact.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
from functions_iterative import (
TimeScenarioParameter,
ReservoirManagement,
)
from simple_bellman_value_calculation import (
calculate_bellman_value_directly,
)
from read_antares_data import Reservoir
import pytest
import numpy as np
import pytest

from functions_iterative import ReservoirManagement, TimeScenarioParameter
from read_antares_data import Reservoir
from simple_bellman_value_calculation import calculate_bellman_value_directly


def test_bellman_value_exact() -> None:
Expand Down
24 changes: 13 additions & 11 deletions tests/test_bellman_value_exact_two_stocks.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
from itertools import product

import numpy as np
import pytest

from calculate_reward_and_bellman_values import (
BellmanValueCalculation,
MultiStockBellmanValueCalculation,
MultiStockManagement,
RewardApproximation,
)
from functions_iterative import (
TimeScenarioParameter,
ReservoirManagement,
TimeScenarioIndex,
TimeScenarioParameter,
)
from multi_stock_bellman_value_calculation import (
AntaresProblem,
Dict,
solve_problem_with_multivariate_bellman_values,
calculate_bellman_value_multi_stock,
)
from calculate_reward_and_bellman_values import (
MultiStockManagement,
BellmanValueCalculation,
MultiStockBellmanValueCalculation,
RewardApproximation,
solve_problem_with_multivariate_bellman_values,
)
from read_antares_data import Reservoir
import pytest
import numpy as np
from itertools import product


def test_iterate_over_stock_discretization() -> None:
Expand Down
9 changes: 5 additions & 4 deletions tests/test_bellman_value_precalculated_reward.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import numpy as np
import pytest

from functions_iterative import (
TimeScenarioParameter,
ReservoirManagement,
TimeScenarioIndex,
TimeScenarioParameter,
)
from read_antares_data import Reservoir
from simple_bellman_value_calculation import (
calculate_bellman_value_with_precalculated_reward,
)
from read_antares_data import Reservoir
import pytest
import numpy as np


def test_bellman_value_precalculated_reward() -> None:
Expand Down
14 changes: 5 additions & 9 deletions tests/test_bellman_value_precalculated_reward_two_stocks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
from functions_iterative import (
TimeScenarioParameter,
ReservoirManagement,
)
import numpy as np
import pytest

from calculate_reward_and_bellman_values import MultiStockManagement
from functions_iterative import ReservoirManagement, TimeScenarioParameter
from multi_stock_bellman_value_calculation import *
from calculate_reward_and_bellman_values import (
MultiStockManagement,
)
from read_antares_data import Reservoir
import pytest
import numpy as np


def test_bellman_value_precalculated_multi_stock() -> None:
Expand Down
Loading

0 comments on commit 51a4533

Please sign in to comment.