From f992ac9359624dd9561c65fbcf8f975a9d1d880b Mon Sep 17 00:00:00 2001 From: Sylvain Leclerc Date: Sat, 27 Jul 2024 12:56:10 +0200 Subject: [PATCH] fix include sort order and formatting Signed-off-by: Sylvain Leclerc --- src/cluster_parsing.py | 1 + src/duration_generator.py | 10 +++++++--- src/mersenne_twister.py | 5 +++-- src/random_generator.py | 8 ++++---- tests/test_unit.py | 9 +++++---- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/cluster_parsing.py b/src/cluster_parsing.py index 61a29ec..a621318 100644 --- a/src/cluster_parsing.py +++ b/src/cluster_parsing.py @@ -12,6 +12,7 @@ from pathlib import Path from typing import Iterable, Optional, TextIO, Tuple + import pandas as pd from pydantic import BaseModel, Field from yaml import safe_load diff --git a/src/duration_generator.py b/src/duration_generator.py index 116d68d..68ad3bb 100644 --- a/src/duration_generator.py +++ b/src/duration_generator.py @@ -11,9 +11,10 @@ # This file is part of the Antares project. from abc import ABC, abstractmethod from enum import Enum +from math import log, sqrt from typing import List + import numpy as np -from math import log, sqrt from random_generator import RNG @@ -39,7 +40,9 @@ class GeneratorWrapper(DurationGenerator): Used to keep backward compat with cpp implementation. """ - def __init__(self, delegate: DurationGenerator, volatility: float, expecs: List[int]) -> None: + def __init__( + self, delegate: DurationGenerator, volatility: float, expecs: List[int] + ) -> None: self.volatility = volatility self.expectations = expecs self.delegate = delegate @@ -97,11 +100,12 @@ def generate_duration(self, day: int) -> int: def make_duration_generator( - rng: RNG, law: ProbilityLaw, volatility: float, expecs: List[int] + rng: RNG, law: ProbilityLaw, volatility: float, expecs: List[int] ) -> DurationGenerator: """ return a DurationGenerator for the given law """ + base_rng: DurationGenerator if law == ProbilityLaw.UNIFORM: base_rng = UniformDurationGenerator(rng, volatility, expecs) elif law == ProbilityLaw.GEOMETRIC: diff --git a/src/mersenne_twister.py b/src/mersenne_twister.py index 6a3ba4d..879ecfb 100644 --- a/src/mersenne_twister.py +++ b/src/mersenne_twister.py @@ -31,6 +31,7 @@ from dataclasses import field from typing import List, Tuple + @dataclasses.dataclass class MersenneTwister: periodN: int = 624 @@ -40,12 +41,12 @@ class MersenneTwister: LOWER_MASK: int = 0x7FFFFFFF MAG: Tuple[int, int] = (0, MATRIX_A) - + mt: List[int] = field(default_factory=lambda: [0] * 624) mti: int = 0 - def seed(self, seed:int) -> None: + def seed(self, seed: int) -> None: self.mt[0] = seed & 0xFFFFFFFF for i in range(1, self.periodN): self.mt[i] = 1812433253 * (self.mt[i - 1] ^ (self.mt[i - 1] >> 30)) + i diff --git a/src/random_generator.py b/src/random_generator.py index e991307..dc78072 100644 --- a/src/random_generator.py +++ b/src/random_generator.py @@ -22,7 +22,7 @@ class RNG(ABC): """ @abstractmethod - def next(self) -> int: + def next(self) -> float: ... @@ -31,7 +31,7 @@ class PythonRNG(ABC): Native python RNG. """ - def next(self) -> int: + def next(self) -> float: return random.random() @@ -40,9 +40,9 @@ class MersenneTwisterRNG(RNG): Our own RNG based on Mersenne-Twister algorithm. """ - def __init__(self, seed: Optional[int] = 5489): + def __init__(self, seed: int = 5489): self._rng = MersenneTwister() self._rng.seed(seed) - def next(self) -> int: + def next(self) -> float: return self._rng.next() diff --git a/tests/test_unit.py b/tests/test_unit.py index fa0cae9..854fe28 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -9,11 +9,11 @@ # SPDX-License-Identifier: MPL-2.0 # # This file is part of the Antares project. +import cProfile import random from pstats import SortKey import pytest -import cProfile from mersenne_twister import MersenneTwister from random_generator import MersenneTwisterRNG @@ -40,8 +40,8 @@ def cluster() -> ThermalCluster: npo_max=[1 for i in range(NB_OF_DAY)], ) -def test_rng(): +def test_rng(): random = MersenneTwister() random.reset() for _ in range(100): @@ -54,6 +54,7 @@ def test_random(): for i in range(5): print(rng.next()) + def test_performances(): with cProfile.Profile() as pr: days = 365 @@ -79,7 +80,6 @@ def test_performances(): def test_compare_with_simulator(): - days = 365 cluster = ThermalCluster( unit_count=10, @@ -99,10 +99,11 @@ def test_compare_with_simulator(): generator = ThermalDataGenerator(rng=MersenneTwisterRNG(), days_per_year=days) results = generator.generate_time_series(cluster, 1) - for i in range(365*24): + for i in range(365 * 24): print(str(i) + " : " + str(results.available_power[0][i])) print(results.available_power[0]) + def test_ts_value(cluster): ts_nb = 4