Skip to content

Commit

Permalink
chore: revert nan to num in expressions (#202)
Browse files Browse the repository at this point in the history
* chore: revert nan to num in expressions
  • Loading branch information
frodehk authored Sep 25, 2023
1 parent cebde33 commit 2f95c29
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 103 deletions.
12 changes: 8 additions & 4 deletions src/libecalc/expression/expression_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ def eval_tokens(tokens: List[Token], array_length: int) -> NDArray[np.float64]:
token_values = [token.value for token in tokens]
check_tokens(token_values)

evaluated_values = eval_parentheses(tokens=token_values)
evaluated_values = np.nan_to_num(
x=eval_parentheses(
tokens=token_values,
) # type: ignore[arg-type]
)

if isinstance(evaluated_values, (Number, int, float)):
evaluated_values = np.full(fill_value=evaluated_values, shape=array_length)
Expand Down Expand Up @@ -206,7 +210,7 @@ def eval_mults(tokens):
for factor in values:
value = value * factor
np.seterr(**current_numpy_error)
return value
return np.nan_to_num(value)


def eval_powers(tokens):
Expand All @@ -224,7 +228,7 @@ def eval_powers(tokens):
else:
value = eval_value(tokens)

return value
return np.nan_to_num(value)


def eval_value(tokens):
Expand Down Expand Up @@ -262,7 +266,7 @@ def eval_value(tokens):
raise ValueError("Should not enter here - no time series in expression evaluator")
else:
var = tokens[0]
var = var
var = np.nan_to_num(var)
return var


Expand Down
96 changes: 48 additions & 48 deletions src/tests/ecalc_cli/snapshots/test_app/test_json_true/test_v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -836,18 +836,18 @@
],
"unit": "bara",
"values": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
]
},
"requested_outlet_pressure": {
Expand All @@ -867,18 +867,18 @@
],
"unit": "bara",
"values": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
]
},
"stage_results": [
Expand Down Expand Up @@ -2316,18 +2316,18 @@
],
"unit": "bara",
"values": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
]
},
"requested_outlet_pressure": {
Expand All @@ -2347,18 +2347,18 @@
],
"unit": "bara",
"values": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
]
},
"stage_results": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -836,18 +836,18 @@
],
"unit": "bara",
"values": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
]
},
"requested_outlet_pressure": {
Expand All @@ -867,18 +867,18 @@
],
"unit": "bara",
"values": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
]
},
"stage_results": [
Expand Down Expand Up @@ -2316,18 +2316,18 @@
],
"unit": "bara",
"values": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
]
},
"requested_outlet_pressure": {
Expand All @@ -2347,18 +2347,18 @@
],
"unit": "bara",
"values": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
]
},
"stage_results": [
Expand Down
5 changes: 2 additions & 3 deletions src/tests/libecalc/expression/test_expression.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime

import numpy as np
import pytest
from libecalc.expression import Expression
from pydantic import parse_obj_as
Expand Down Expand Up @@ -121,8 +120,8 @@ def test_invalid_key(self, caplog):

# Check that nan is handled correctly when division by zero occur, but
# values are later disregarded by conditions
assert np.isnan(
Expression.setup_from_expression("0{/}0").evaluate(variables=variables, fill_length=len(time_vector))[0]
assert (
Expression.setup_from_expression("0{/}0").evaluate(variables=variables, fill_length=len(time_vector)) == 0
)

with pytest.raises(ValueError):
Expand Down

0 comments on commit 2f95c29

Please sign in to comment.