-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start developing random functions tests
- Loading branch information
1 parent
a6064b7
commit 4f82387
Showing
5 changed files
with
100,173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
|
||
import pandas as pd | ||
|
||
# imports needed by the translated functions | ||
import numpy as np | ||
from scipy import stats | ||
|
||
|
||
from pysd.translators.vensim.vensim_element import Component | ||
from pysd.builders.python.python_expressions_builder import\ | ||
CallBuilder, NumericBuilder | ||
from pysd.builders.python.imports import ImportsManager | ||
|
||
|
||
outfile = Path(__file__).parent / 'test.tab' | ||
|
||
cols = [ | ||
'RANDOM UNIFORM(0, 1, 0)', | ||
'RANDOM UNIFORM(-3, 10, 0)', | ||
'RANDOM UNIFORM(0.5, 7.8, 0)', | ||
'RANDOM NORMAL(-1000, 1000, 0, 1, 0)', | ||
'RANDOM NORMAL(0, 1000, 0, 1, 0)', | ||
'RANDOM NORMAL(-100, 100, 3, 1, 0)', | ||
'RANDOM NORMAL(-100, 100, -3, 0.5, 0)', | ||
'RANDOM NORMAL(-100, 10, 5, 10, 0)' | ||
] | ||
|
||
|
||
@dataclass | ||
class FakeComponent: | ||
element: str | ||
section: object | ||
subscripts_dict: dict | ||
|
||
|
||
@dataclass | ||
class FakeSection: | ||
namespace: object | ||
macrospace: dict | ||
imports: object | ||
|
||
|
||
@dataclass | ||
class FakeNamespace: | ||
cleanspace: dict | ||
|
||
|
||
imports = ImportsManager() | ||
fake_component = FakeComponent( | ||
'', | ||
FakeSection(FakeNamespace({}), {}, imports), | ||
{} | ||
) | ||
|
||
size = int(1e5) | ||
|
||
out = {} | ||
for col in cols: | ||
component = Component('', ([], []), col) | ||
component.parse() | ||
builder = CallBuilder(component.ast, fake_component) | ||
args = { | ||
i: NumericBuilder(arg, fake_component).build({}) | ||
for i, arg in builder.arguments.items() | ||
} | ||
expr = builder.build(args).expression | ||
expr = expr.replace('()', str(size)) | ||
out[col] = eval(expr) | ||
|
||
df = pd.DataFrame(out) | ||
df.to_csv(outfile, sep='\t') |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import pytest | ||
|
||
import pandas as pd | ||
|
||
# imports needed by the translated functions | ||
import numpy as np | ||
from scipy import stats | ||
|
||
|
||
from pysd.translators.vensim.vensim_element import Component | ||
from pysd.builders.python.python_expressions_builder import\ | ||
CallBuilder, NumericBuilder | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input_file", | ||
[ | ||
( # rectangular | ||
'test.tab' | ||
), | ||
], | ||
ids=["test_file"] | ||
) | ||
class TestRandomVensim(): | ||
|
||
@pytest.fixture(scope="function") | ||
def data_raw(self, input_file, _test_random): | ||
file = _test_random.joinpath(input_file) | ||
data = pd.read_table(file, sep='\t', index_col=0) | ||
return data | ||
|
||
@pytest.fixture(scope="function") | ||
def data_python(self, data_raw, fake_component): | ||
size = len(data_raw.index) | ||
out = {} | ||
for col in data_raw.columns: | ||
component = Component('', ([], []), col) | ||
component.parse() | ||
builder = CallBuilder(component.ast, fake_component) | ||
args = { | ||
i: NumericBuilder(arg, fake_component).build({}) | ||
for i, arg in builder.arguments.items() | ||
} | ||
expr = builder.build(args).expression | ||
expr = expr.replace('()', str(size)) | ||
out[col] = eval(expr) | ||
|
||
return pd.DataFrame(out, index=data_raw.index) | ||
|
||
def test_statistics(self, data_raw, data_python): | ||
|
||
raw_desc = stats.describe(data_raw, axis=0, nan_policy='omit') | ||
py_desc = stats.describe(data_python, axis=0, nan_policy='omit') | ||
for stat in ('mean', 'variance', 'skewness', 'kurtosis'): | ||
assert np.allclose( | ||
getattr(raw_desc, stat), | ||
getattr(py_desc, stat), | ||
atol=5e-2, rtol=5e-2), 'Failed when comparing %s' % stat |
Oops, something went wrong.