Skip to content

Commit

Permalink
Merge pull request #1257 from lisphilar/issue1239-fu
Browse files Browse the repository at this point in the history
Follow-up Issue #1239
  • Loading branch information
lisphilar authored Oct 9, 2022
2 parents b7449de + b57efc8 commit 4cdd8e9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
8 changes: 0 additions & 8 deletions covsirphy/science/ode_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,6 @@ def __init__(self, data, location_name, complement=True):
# Aliases of variable names
self._variable_alias = Alias.for_variables()

def __eq__(self, other):
return isinstance(other, self.__class__) and \
self._location_name == other._location_name and \
self.describe().equals(other.describe())

def __ne__(self, other):
return not self.__eq__(other)

def to_json(self, filename):
"""Write a JSON file which can usable for recreating ODEScenario instance with .from_json()
Expand Down
52 changes: 50 additions & 2 deletions example/05_scenario_analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"An instance of `ODEScenario()` will be created here. We have two options as follows.\n",
"\n",
"- With dataset of recommended servers\n",
"- With `pandas.DataFrame`"
"- With `pandas.DataFrame`\n",
"- With serialized ODEScenario object (JSON file, from version 2.28.0)"
]
},
{
Expand Down Expand Up @@ -278,6 +279,53 @@
"If we need to perform time-series segmentation, tau estimation and ODE parameter estimation, we can use `.build_with_model(name=<str>, model=<ODEModel>, data_range=<tuple of (str, str) or None>, tau=<int or None>` instead of `.build_with_dynamics()`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.3 With serialized `ODEScenario` object\n",
"From version 2.28.0, we can save scenario settings as JSON file using `ODEScenario.to_json(filename)`. The JSON file will be used to re-create `ODEScenario` object, skipping ODE parameter estimation step and so on."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To JSON file:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create {\"filename\": <absolute path>} easily\n",
"filer = cs.Filer(directory=\"input\", prefix=\"jpn\")\n",
"file_dict = filer.json(title=\"scenarios\")\n",
"# To JSON file\n",
"snr_build = cs.ODEScenario.auto_build(geo=\"Japan\", model=cs.SIRFModel)\n",
"json_file = snr_build.to_json(**file_dict)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From JSON file:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"snr_recreated = cs.ODEScenario.from_json(json_file)\n",
"# Describe (explained later)\n",
"snr_recreated.describe()"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -868,7 +916,7 @@
},
"language_info": {
"name": "python",
"version": "3.9.13"
"version": "3.10.7"
},
"vscode": {
"interpreter": {
Expand Down
6 changes: 0 additions & 6 deletions tests/test_science/test_ode_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ def snr(jsonpath):
return ODEScenario.from_json(filename=jsonpath)
instance = ODEScenario.auto_build(geo="Japan", model=SIRFModel)
instance.to_json(filename=jsonpath)
snr_build = ODEScenario.from_json(filename=jsonpath)
assert instance._location_name == snr_build._location_name
assert instance.describe().equals(snr_build.describe())
assert instance == snr_build
snr_build._location_name = "unknown"
assert snr != snr_build
return instance


Expand Down

0 comments on commit 4cdd8e9

Please sign in to comment.