-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
Model
accepts a wider range of data
Values which resemble Numpy `ndarray` and pandas `Series` should be accepted. They will be converted into (JSON-serializable) lists. Closes #51
- Loading branch information
1 parent
b7929d3
commit 661d064
Showing
3 changed files
with
83 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,35 @@ | ||
import json | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
|
||
import stan.model | ||
|
||
|
||
def test_ensure_json(): | ||
data = {"K": 3} | ||
assert stan.model._ensure_json_serializable(data) == data | ||
data = {"K": 3, "x": [3, 4, 5]} | ||
assert stan.model._ensure_json_serializable(data) == data | ||
|
||
class DummyClass: | ||
pass | ||
|
||
data = {"K": DummyClass(), "x": [3, 4, 5]} | ||
with pytest.raises(TypeError, match=r"Value associated with variable `K`"): | ||
stan.model._ensure_json_serializable(data) | ||
|
||
|
||
def test_ensure_json_numpy(): | ||
data = {"K": 3, "x": np.array([3, 4, 5])} | ||
expected = {"K": 3, "x": [3, 4, 5]} | ||
assert stan.model._ensure_json_serializable(data) == expected | ||
assert json.dumps(stan.model._ensure_json_serializable(data)) | ||
|
||
|
||
def test_ensure_json_pandas(): | ||
data = {"K": 3, "x": pd.Series([3, 4, 5])} | ||
expected = {"K": 3, "x": [3, 4, 5]} | ||
assert stan.model._ensure_json_serializable(data) == expected | ||
assert json.dumps(stan.model._ensure_json_serializable(data)) |
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