Skip to content

Commit

Permalink
chore(DO): update parser (#6675)
Browse files Browse the repository at this point in the history
* chore(DO): update parser

* fix NaN checks

* fix type error

* format

* update snapshots

---------

Co-authored-by: Pierre Segonne <pierre.segonne@electricitymaps.com>
Co-authored-by: Viktor Andersson <30777521+VIKTORVAV99@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 23, 2024
1 parent dd2fc1a commit d555e59
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 198 deletions.
14 changes: 12 additions & 2 deletions electricitymap/contrib/lib/models/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _none_safe_round(value: float | None, precision: int = 6) -> float | None:
If the value is None, it is returned as is.
The default precision is 6 decimal places, which gives us a precision of 1 W.
"""
return None if value is None else round(value, precision)
return None if value is None or math.isnan(value) else round(value, precision)


class Mix(BaseModel, ABC):
Expand Down Expand Up @@ -107,7 +107,9 @@ def __init__(self, **data: Any):
for attr, value in data.items():
if value is not None and value < 0:
self._corrected_negative_values.add(attr)
self.__setattr__(attr, None)
value = None
# Ensure that the value is rounded to 6 decimal places and set to None if it is NaN.
self.__setattr__(attr, value)

def dict( # noqa: A003
self,
Expand Down Expand Up @@ -232,6 +234,14 @@ class StorageMix(Mix):
battery: float | None = None
hydro: float | None = None

def __init__(self, **data: Any):
"""
Overriding the constructor to check for NaN values and set them to None.
"""
super().__init__(**data)
for attr, value in data.items():
self.__setattr__(attr, value)

def __setattr__(self, name: str, value: float | None) -> None:
"""
Overriding the setattr method to raise an error if the mode is unknown.
Expand Down
16 changes: 16 additions & 0 deletions electricitymap/contrib/lib/tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,14 @@ def test_production_with_nan_using_numpy(self):
assert mix.wind == 10
assert mix.corrected_negative_modes == set()

def test_production_with_nan_init(self):
mix = ProductionMix(wind=math.nan)
assert mix.wind is None

def test_production_with_nan_using_numpy_init(self):
mix = ProductionMix(wind=np.nan)
assert mix.wind is None

def test_storage(self):
mix = StorageMix()
mix.add_value("hydro", 10)
Expand Down Expand Up @@ -804,6 +812,14 @@ def test_storage_with_nan_using_numpy(self):
mix.add_value("hydro", np.nan)
assert mix.hydro == -5

def test_storage_with_nan_init(self):
mix = StorageMix(hydro=math.nan)
assert mix.hydro is None

def test_storage_with_nan_using_numpy_init(self):
mix = StorageMix(hydro=np.nan)
assert mix.hydro is None


class TestMixUpdate:
def test_update_production(self):
Expand Down
Loading

0 comments on commit d555e59

Please sign in to comment.