epymorph 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- epymorph/__init__.py +7 -0
- epymorph/__main__.py +16 -0
- epymorph/adrio/__init__.py +0 -0
- epymorph/adrio/acs5.py +1378 -0
- epymorph/adrio/adrio.py +953 -0
- epymorph/adrio/cdc.py +1826 -0
- epymorph/adrio/commuting_flows.py +283 -0
- epymorph/adrio/csv.py +499 -0
- epymorph/adrio/humidity.py +206 -0
- epymorph/adrio/lodes.py +509 -0
- epymorph/adrio/numpy.py +160 -0
- epymorph/adrio/prism.py +345 -0
- epymorph/adrio/processing.py +1221 -0
- epymorph/adrio/soda.py +602 -0
- epymorph/adrio/us_tiger.py +289 -0
- epymorph/adrio/validation.py +382 -0
- epymorph/attribute.py +614 -0
- epymorph/cache.py +680 -0
- epymorph/cli/__init__.py +0 -0
- epymorph/cli/cache.py +78 -0
- epymorph/cli/main.py +29 -0
- epymorph/compartment_model.py +1873 -0
- epymorph/data/__init__.py +1 -0
- epymorph/data/ipm/__init__.py +22 -0
- epymorph/data/ipm/no.py +14 -0
- epymorph/data/ipm/pei.py +40 -0
- epymorph/data/ipm/seirs.py +50 -0
- epymorph/data/ipm/sirh.py +55 -0
- epymorph/data/ipm/sirs.py +48 -0
- epymorph/data/ipm/sparsemod.py +96 -0
- epymorph/data/mm/__init__.py +22 -0
- epymorph/data/mm/centroids.py +76 -0
- epymorph/data/mm/flat.py +61 -0
- epymorph/data/mm/icecube.py +50 -0
- epymorph/data/mm/no.py +29 -0
- epymorph/data/mm/pei.py +105 -0
- epymorph/data/mm/sparsemod.py +73 -0
- epymorph/data/pei.py +424 -0
- epymorph/data_shape.py +593 -0
- epymorph/data_type.py +185 -0
- epymorph/data_usage.py +258 -0
- epymorph/database.py +1301 -0
- epymorph/error.py +195 -0
- epymorph/event.py +196 -0
- epymorph/geography/__init__.py +0 -0
- epymorph/geography/custom.py +93 -0
- epymorph/geography/scope.py +457 -0
- epymorph/geography/us_census.py +1056 -0
- epymorph/geography/us_geography.py +502 -0
- epymorph/geography/us_tiger.py +1083 -0
- epymorph/initializer.py +786 -0
- epymorph/kit.py +107 -0
- epymorph/log/__init__.py +0 -0
- epymorph/log/file.py +128 -0
- epymorph/log/messaging.py +139 -0
- epymorph/log/movement.py +228 -0
- epymorph/movement_model.py +347 -0
- epymorph/parameter_fitting/__init__.py +0 -0
- epymorph/parameter_fitting/distribution.py +68 -0
- epymorph/parameter_fitting/dynamics.py +19 -0
- epymorph/parameter_fitting/filter/__init__.py +0 -0
- epymorph/parameter_fitting/filter/base_filter.py +50 -0
- epymorph/parameter_fitting/filter/particle.py +32 -0
- epymorph/parameter_fitting/filter/particle_filter.py +296 -0
- epymorph/parameter_fitting/likelihood.py +54 -0
- epymorph/parameter_fitting/output.py +73 -0
- epymorph/parameter_fitting/particlefilter_simulation.py +175 -0
- epymorph/parameter_fitting/perturbation.py +19 -0
- epymorph/parameter_fitting/utils/__init__.py +0 -0
- epymorph/parameter_fitting/utils/data_loader.py +97 -0
- epymorph/parameter_fitting/utils/epymorph_simulation.py +119 -0
- epymorph/parameter_fitting/utils/observations.py +63 -0
- epymorph/parameter_fitting/utils/parameter_estimation.py +49 -0
- epymorph/parameter_fitting/utils/params_perturb.py +43 -0
- epymorph/parameter_fitting/utils/particle_initializer.py +95 -0
- epymorph/parameter_fitting/utils/resampler.py +258 -0
- epymorph/parameter_fitting/utils/utils.py +101 -0
- epymorph/params.py +438 -0
- epymorph/py.typed +0 -0
- epymorph/rume.py +871 -0
- epymorph/simulation.py +991 -0
- epymorph/simulator/__init__.py +0 -0
- epymorph/simulator/basic/__init__.py +0 -0
- epymorph/simulator/basic/basic_simulator.py +171 -0
- epymorph/simulator/basic/ipm_exec.py +437 -0
- epymorph/simulator/basic/mm_exec.py +244 -0
- epymorph/simulator/basic/output.py +213 -0
- epymorph/simulator/world.py +149 -0
- epymorph/simulator/world_list.py +274 -0
- epymorph/strata.py +24 -0
- epymorph/sympy_shim.py +57 -0
- epymorph/time.py +1197 -0
- epymorph/tools/__init__.py +0 -0
- epymorph/tools/data.py +297 -0
- epymorph/tools/ipm_diagram.py +208 -0
- epymorph/tools/out_map.py +551 -0
- epymorph/tools/out_plot.py +423 -0
- epymorph/tools/out_table.py +582 -0
- epymorph/util.py +1036 -0
- epymorph-1.0.0.dist-info/METADATA +57 -0
- epymorph-1.0.0.dist-info/RECORD +104 -0
- epymorph-1.0.0.dist-info/WHEEL +5 -0
- epymorph-1.0.0.dist-info/entry_points.txt +2 -0
- epymorph-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,582 @@
|
|
|
1
|
+
"""Tools for rendering tables from epymorph simulation output data."""
|
|
2
|
+
|
|
3
|
+
import dataclasses
|
|
4
|
+
from typing import Callable, Literal, Sequence, overload
|
|
5
|
+
from warnings import warn
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
import pandas as pd
|
|
9
|
+
from pandas.core.groupby.generic import DataFrameGroupBy
|
|
10
|
+
from sparklines import sparklines
|
|
11
|
+
from sympy import Symbol
|
|
12
|
+
|
|
13
|
+
from epymorph.compartment_model import (
|
|
14
|
+
CompartmentDef,
|
|
15
|
+
EdgeDef,
|
|
16
|
+
QuantityAggregation,
|
|
17
|
+
QuantitySelection,
|
|
18
|
+
)
|
|
19
|
+
from epymorph.geography.scope import GeoAggregation, GeoSelection
|
|
20
|
+
from epymorph.time import NBins, TimeAggregation, TimeSelection
|
|
21
|
+
from epymorph.tools.data import Output, munge
|
|
22
|
+
from epymorph.util import filter_unique
|
|
23
|
+
|
|
24
|
+
OrderingOption = Literal["location", "quantity"]
|
|
25
|
+
"""Options for table row ordering."""
|
|
26
|
+
|
|
27
|
+
FormatOption = Literal["dataframe", "string", "print"]
|
|
28
|
+
"""Options for table render output format."""
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _to_sparklines(values):
|
|
32
|
+
"""Produce a sparkline result, as a Pandas aggregation function."""
|
|
33
|
+
# NOTE: I tried using the same max value for each quantity;
|
|
34
|
+
# in a way this improves comparison between locations, but means that
|
|
35
|
+
# large locations "drown out" smaller locations, hiding the dynamics.
|
|
36
|
+
# I made the judgement call that the dynamics were more important here.
|
|
37
|
+
# If you want to understand relative scale you should use a line graph.
|
|
38
|
+
# NOTE: I'm checking for max == 0 here because if min and max are both zero,
|
|
39
|
+
# sparkline draws a mid-height box instead of a min-height box;
|
|
40
|
+
# with this adjustment, all-zero data looks like zeros.
|
|
41
|
+
maximum = values.max()
|
|
42
|
+
if maximum == 0:
|
|
43
|
+
maximum = 1.0
|
|
44
|
+
(spark,) = sparklines(values, minimum=0, maximum=maximum, num_lines=1)
|
|
45
|
+
return spark
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _process_output(
|
|
49
|
+
out: Output,
|
|
50
|
+
geo: GeoSelection | GeoAggregation,
|
|
51
|
+
time: TimeSelection | TimeAggregation,
|
|
52
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
53
|
+
ordering: OrderingOption,
|
|
54
|
+
column_names: Sequence[str],
|
|
55
|
+
process_groups: Callable[[DataFrameGroupBy], pd.DataFrame],
|
|
56
|
+
) -> pd.DataFrame:
|
|
57
|
+
# The different tables share a lot of data processing logic.
|
|
58
|
+
# The only significant difference is how to turn
|
|
59
|
+
# the grouped (geo,quantity) time series into values.
|
|
60
|
+
# This difference is factored out by taking a lambda for processing groups.
|
|
61
|
+
|
|
62
|
+
# Prepare a consistent sort order.
|
|
63
|
+
# By default: we sort by location (alphanumeric by node_id),
|
|
64
|
+
# then by quantity in IPM declaration order (compartments then events).
|
|
65
|
+
# Passing ordering "quantity" just flips the ordering of two columns.
|
|
66
|
+
|
|
67
|
+
# Compute the grouped time series.
|
|
68
|
+
data_df = munge(out, geo, time, quantity)
|
|
69
|
+
|
|
70
|
+
# Before melting, disambiguate any quantities with the same name.
|
|
71
|
+
q_mapping = quantity.disambiguate_groups()
|
|
72
|
+
|
|
73
|
+
groups_df = (
|
|
74
|
+
data_df.set_axis(["time", "geo", *q_mapping.keys()], axis=1)
|
|
75
|
+
.melt(id_vars=["time", "geo"], var_name="quantity")
|
|
76
|
+
.drop(columns="time")
|
|
77
|
+
.groupby(["geo", "quantity"], sort=False)
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
# Process the groups into values.
|
|
81
|
+
result_df = process_groups(groups_df)
|
|
82
|
+
|
|
83
|
+
# Set column names.
|
|
84
|
+
result_df = result_df.set_axis(["geo", "quantity", *column_names], axis=1)
|
|
85
|
+
|
|
86
|
+
# If this is a multistrata model add a column for "other strata";
|
|
87
|
+
# for any quantities that are meta edge events, the value of this column
|
|
88
|
+
# are which other strata are involved (by examining the rate expression).
|
|
89
|
+
# For compartments and events that are purely intrastrata, this should be blank.
|
|
90
|
+
if quantity.ipm.is_multistrata:
|
|
91
|
+
strata = quantity.ipm.strata
|
|
92
|
+
|
|
93
|
+
def which_strata(s: Symbol) -> str | None:
|
|
94
|
+
for x in strata:
|
|
95
|
+
if str(s).endswith(f"_{x}"):
|
|
96
|
+
return x
|
|
97
|
+
return None
|
|
98
|
+
|
|
99
|
+
def calc_other_strata(e: EdgeDef) -> str:
|
|
100
|
+
# If we can't tell which other strata are involved,
|
|
101
|
+
# just return empty string.
|
|
102
|
+
own_strata = which_strata(e.compartment_from)
|
|
103
|
+
if own_strata is None:
|
|
104
|
+
return ""
|
|
105
|
+
other_strata = filter_unique(
|
|
106
|
+
strata
|
|
107
|
+
for strata in (
|
|
108
|
+
which_strata(x)
|
|
109
|
+
for x in e.rate.free_symbols
|
|
110
|
+
if isinstance(x, Symbol)
|
|
111
|
+
)
|
|
112
|
+
if strata is not None and strata != own_strata
|
|
113
|
+
)
|
|
114
|
+
return ",".join(other_strata)
|
|
115
|
+
|
|
116
|
+
map_other_strata = {
|
|
117
|
+
name: calc_other_strata(q) if isinstance(q, EdgeDef) else ""
|
|
118
|
+
for name, q in zip(q_mapping.keys(), quantity.selected)
|
|
119
|
+
}
|
|
120
|
+
other_strata = result_df["quantity"].apply(lambda x: map_other_strata[x])
|
|
121
|
+
result_df.insert(2, column="other strata", value=other_strata)
|
|
122
|
+
|
|
123
|
+
# Sort rows.
|
|
124
|
+
def sort_key(series):
|
|
125
|
+
if series.name == "quantity":
|
|
126
|
+
quantity_order = {s: i for i, s in enumerate(q_mapping.keys())}
|
|
127
|
+
return series.apply(lambda x: quantity_order[x])
|
|
128
|
+
else:
|
|
129
|
+
return series
|
|
130
|
+
|
|
131
|
+
sort_cols = ["quantity", "geo"] if ordering == "quantity" else ["geo", "quantity"]
|
|
132
|
+
result_df = result_df.sort_values(sort_cols, key=sort_key, ignore_index=True)
|
|
133
|
+
|
|
134
|
+
# Restore quantity names (now in rows).
|
|
135
|
+
qs = result_df["quantity"].apply(lambda x: q_mapping[x])
|
|
136
|
+
result_df = result_df.assign(quantity=qs)
|
|
137
|
+
|
|
138
|
+
# If we have a way to do this, replace node IDs with friendly labels.
|
|
139
|
+
result_scope = geo.to_scope()
|
|
140
|
+
if (labels := result_scope.labels_option) is not None:
|
|
141
|
+
geo_map = dict(zip(result_scope.node_ids, labels))
|
|
142
|
+
result_df["geo"] = result_df["geo"].apply(lambda x: geo_map[x])
|
|
143
|
+
|
|
144
|
+
return result_df
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class TableRenderer:
|
|
148
|
+
"""
|
|
149
|
+
Provides a number of methods for rendering an output in tabular form.
|
|
150
|
+
|
|
151
|
+
Most commonly, you will use TableRenderer starting from a simulation output object
|
|
152
|
+
that supports it:
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
out = BasicSimulation(rume).run()
|
|
156
|
+
out.table.quantiles(...)
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Parameters
|
|
160
|
+
----------
|
|
161
|
+
output :
|
|
162
|
+
The output the renderer will use.
|
|
163
|
+
"""
|
|
164
|
+
|
|
165
|
+
output: Output
|
|
166
|
+
"""The output the renderer will use."""
|
|
167
|
+
|
|
168
|
+
def __init__(self, output: Output):
|
|
169
|
+
self.output = output
|
|
170
|
+
|
|
171
|
+
def _format_output(self, result_df: pd.DataFrame, result_format: FormatOption):
|
|
172
|
+
"""Produce output according to the given format setting."""
|
|
173
|
+
match result_format:
|
|
174
|
+
case "dataframe":
|
|
175
|
+
return result_df
|
|
176
|
+
case "string":
|
|
177
|
+
return result_df.to_string()
|
|
178
|
+
case "print":
|
|
179
|
+
print(result_df.to_string(index=False)) # noqa: T201
|
|
180
|
+
return None
|
|
181
|
+
case x:
|
|
182
|
+
msg = f"Invalid result_format: {x}"
|
|
183
|
+
raise ValueError(msg)
|
|
184
|
+
|
|
185
|
+
@overload
|
|
186
|
+
def quantiles(
|
|
187
|
+
self,
|
|
188
|
+
quantiles: Sequence[float],
|
|
189
|
+
geo: GeoSelection | GeoAggregation,
|
|
190
|
+
time: TimeSelection | TimeAggregation,
|
|
191
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
192
|
+
*,
|
|
193
|
+
ordering: OrderingOption = "location",
|
|
194
|
+
result_format: Literal["dataframe"] = "dataframe",
|
|
195
|
+
column_names: Sequence[str] | None = None,
|
|
196
|
+
) -> pd.DataFrame: ...
|
|
197
|
+
|
|
198
|
+
@overload
|
|
199
|
+
def quantiles(
|
|
200
|
+
self,
|
|
201
|
+
quantiles: Sequence[float],
|
|
202
|
+
geo: GeoSelection | GeoAggregation,
|
|
203
|
+
time: TimeSelection | TimeAggregation,
|
|
204
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
205
|
+
*,
|
|
206
|
+
ordering: OrderingOption = "location",
|
|
207
|
+
result_format: Literal["string"],
|
|
208
|
+
column_names: Sequence[str] | None = None,
|
|
209
|
+
) -> str: ...
|
|
210
|
+
|
|
211
|
+
@overload
|
|
212
|
+
def quantiles(
|
|
213
|
+
self,
|
|
214
|
+
quantiles: Sequence[float],
|
|
215
|
+
geo: GeoSelection | GeoAggregation,
|
|
216
|
+
time: TimeSelection | TimeAggregation,
|
|
217
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
218
|
+
*,
|
|
219
|
+
ordering: OrderingOption = "location",
|
|
220
|
+
result_format: Literal["print"],
|
|
221
|
+
column_names: Sequence[str] | None = None,
|
|
222
|
+
) -> None: ...
|
|
223
|
+
|
|
224
|
+
def quantiles(
|
|
225
|
+
self,
|
|
226
|
+
quantiles: Sequence[float],
|
|
227
|
+
geo: GeoSelection | GeoAggregation,
|
|
228
|
+
time: TimeSelection | TimeAggregation,
|
|
229
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
230
|
+
*,
|
|
231
|
+
ordering: OrderingOption = "location",
|
|
232
|
+
result_format: FormatOption = "dataframe",
|
|
233
|
+
column_names: Sequence[str] | None = None,
|
|
234
|
+
) -> pd.DataFrame | str | None:
|
|
235
|
+
"""
|
|
236
|
+
Render a table showing time-series quantiles for the given selections.
|
|
237
|
+
|
|
238
|
+
Parameters
|
|
239
|
+
----------
|
|
240
|
+
quantiles :
|
|
241
|
+
The list of quantiles to calculate and display, in the range [0,1].
|
|
242
|
+
geo :
|
|
243
|
+
The geographic selection to make on the output data.
|
|
244
|
+
time :
|
|
245
|
+
The time selection to make on the output data.
|
|
246
|
+
quantity :
|
|
247
|
+
The quantity selection to make on the output data.
|
|
248
|
+
ordering :
|
|
249
|
+
Controls the ordering of rows in the result;
|
|
250
|
+
both location and quantity are used to sort the resulting rows,
|
|
251
|
+
this just decides which gets priority.
|
|
252
|
+
result_format :
|
|
253
|
+
Controls the type of the result of this method;
|
|
254
|
+
"dataframe" returns a Pandas dataframe,
|
|
255
|
+
"string" returns the stringified table, and
|
|
256
|
+
"print" just prints the stringified table directly and returns `None`.
|
|
257
|
+
column_names :
|
|
258
|
+
Overrides the default names of the quantiles columns;
|
|
259
|
+
by default, this is just the quantile value itself.
|
|
260
|
+
|
|
261
|
+
Returns
|
|
262
|
+
-------
|
|
263
|
+
:
|
|
264
|
+
Output according to the value of the `result_format` parameter.
|
|
265
|
+
"""
|
|
266
|
+
_quantiles = np.array(quantiles)
|
|
267
|
+
if len(quantiles) == 0:
|
|
268
|
+
err = "Please provide a list of quantiles to calculate."
|
|
269
|
+
raise ValueError(err)
|
|
270
|
+
if np.any(_quantiles < 0) or np.any(_quantiles > 1):
|
|
271
|
+
err = "Invalid quantiles: must be in the range [0.0, 1.0]"
|
|
272
|
+
raise ValueError(err)
|
|
273
|
+
if column_names is None:
|
|
274
|
+
column_names = [str(x) for x in quantiles]
|
|
275
|
+
|
|
276
|
+
result_df = _process_output(
|
|
277
|
+
self.output,
|
|
278
|
+
geo,
|
|
279
|
+
time,
|
|
280
|
+
quantity,
|
|
281
|
+
ordering,
|
|
282
|
+
column_names,
|
|
283
|
+
lambda df: (
|
|
284
|
+
# `pivot` is more appropriate than `pivot_table` because we don't want
|
|
285
|
+
# aggregation to happen accidentally
|
|
286
|
+
df.quantile(_quantiles) # noqa: PD010
|
|
287
|
+
.reset_index()
|
|
288
|
+
.pivot(index=["geo", "quantity"], columns=["level_2"])
|
|
289
|
+
.reset_index()
|
|
290
|
+
),
|
|
291
|
+
)
|
|
292
|
+
return self._format_output(result_df, result_format)
|
|
293
|
+
|
|
294
|
+
@overload
|
|
295
|
+
def range(
|
|
296
|
+
self,
|
|
297
|
+
geo: GeoSelection | GeoAggregation,
|
|
298
|
+
time: TimeSelection | TimeAggregation,
|
|
299
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
300
|
+
*,
|
|
301
|
+
ordering: OrderingOption = "location",
|
|
302
|
+
result_format: Literal["dataframe"] = "dataframe",
|
|
303
|
+
) -> pd.DataFrame: ...
|
|
304
|
+
|
|
305
|
+
@overload
|
|
306
|
+
def range(
|
|
307
|
+
self,
|
|
308
|
+
geo: GeoSelection | GeoAggregation,
|
|
309
|
+
time: TimeSelection | TimeAggregation,
|
|
310
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
311
|
+
*,
|
|
312
|
+
ordering: OrderingOption = "location",
|
|
313
|
+
result_format: Literal["string"],
|
|
314
|
+
) -> str: ...
|
|
315
|
+
|
|
316
|
+
@overload
|
|
317
|
+
def range(
|
|
318
|
+
self,
|
|
319
|
+
geo: GeoSelection | GeoAggregation,
|
|
320
|
+
time: TimeSelection | TimeAggregation,
|
|
321
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
322
|
+
*,
|
|
323
|
+
ordering: OrderingOption = "location",
|
|
324
|
+
result_format: Literal["print"],
|
|
325
|
+
) -> None: ...
|
|
326
|
+
|
|
327
|
+
def range(
|
|
328
|
+
self,
|
|
329
|
+
geo: GeoSelection | GeoAggregation,
|
|
330
|
+
time: TimeSelection | TimeAggregation,
|
|
331
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
332
|
+
*,
|
|
333
|
+
ordering: OrderingOption = "location",
|
|
334
|
+
result_format: FormatOption = "dataframe",
|
|
335
|
+
) -> pd.DataFrame | str | None:
|
|
336
|
+
"""
|
|
337
|
+
Render a table showing minimum and maximum values over time for the given
|
|
338
|
+
selections. This is equivalent to calling `quantiles()` with 0 and 1.
|
|
339
|
+
|
|
340
|
+
Parameters
|
|
341
|
+
----------
|
|
342
|
+
geo :
|
|
343
|
+
The geographic selection to make on the output data.
|
|
344
|
+
time :
|
|
345
|
+
The time selection to make on the output data.
|
|
346
|
+
quantity :
|
|
347
|
+
The quantity selection to make on the output data.
|
|
348
|
+
ordering :
|
|
349
|
+
Controls the ordering of rows in the result;
|
|
350
|
+
both location and quantity are used to sort the resulting rows,
|
|
351
|
+
this just decides which gets priority.
|
|
352
|
+
result_format :
|
|
353
|
+
Controls the type of the result of this method;
|
|
354
|
+
"dataframe" returns a Pandas dataframe,
|
|
355
|
+
"string" returns the stringified table, and
|
|
356
|
+
"print" just prints the stringified table directly and returns `None`.
|
|
357
|
+
|
|
358
|
+
Returns
|
|
359
|
+
-------
|
|
360
|
+
:
|
|
361
|
+
Output according to the value of the `result_format` parameter.
|
|
362
|
+
"""
|
|
363
|
+
return self.quantiles(
|
|
364
|
+
quantiles=(0.0, 1.0),
|
|
365
|
+
geo=geo,
|
|
366
|
+
time=time,
|
|
367
|
+
quantity=quantity,
|
|
368
|
+
ordering=ordering,
|
|
369
|
+
result_format=result_format,
|
|
370
|
+
column_names=("min", "max"),
|
|
371
|
+
)
|
|
372
|
+
|
|
373
|
+
@overload
|
|
374
|
+
def sum(
|
|
375
|
+
self,
|
|
376
|
+
geo: GeoSelection | GeoAggregation,
|
|
377
|
+
time: TimeSelection | TimeAggregation,
|
|
378
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
379
|
+
*,
|
|
380
|
+
ordering: OrderingOption = "location",
|
|
381
|
+
result_format: Literal["dataframe"] = "dataframe",
|
|
382
|
+
) -> pd.DataFrame: ...
|
|
383
|
+
|
|
384
|
+
@overload
|
|
385
|
+
def sum(
|
|
386
|
+
self,
|
|
387
|
+
geo: GeoSelection | GeoAggregation,
|
|
388
|
+
time: TimeSelection | TimeAggregation,
|
|
389
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
390
|
+
*,
|
|
391
|
+
ordering: OrderingOption = "location",
|
|
392
|
+
result_format: Literal["string"],
|
|
393
|
+
) -> str: ...
|
|
394
|
+
|
|
395
|
+
@overload
|
|
396
|
+
def sum(
|
|
397
|
+
self,
|
|
398
|
+
geo: GeoSelection | GeoAggregation,
|
|
399
|
+
time: TimeSelection | TimeAggregation,
|
|
400
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
401
|
+
*,
|
|
402
|
+
ordering: OrderingOption = "location",
|
|
403
|
+
result_format: Literal["print"],
|
|
404
|
+
) -> None: ...
|
|
405
|
+
|
|
406
|
+
def sum(
|
|
407
|
+
self,
|
|
408
|
+
geo: GeoSelection | GeoAggregation,
|
|
409
|
+
time: TimeSelection | TimeAggregation,
|
|
410
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
411
|
+
*,
|
|
412
|
+
ordering: OrderingOption = "location",
|
|
413
|
+
result_format: FormatOption = "dataframe",
|
|
414
|
+
) -> pd.DataFrame | str | None:
|
|
415
|
+
"""
|
|
416
|
+
Render a table showing summed values over time for the given selections.
|
|
417
|
+
|
|
418
|
+
Because it is not valid to sum compartment values over time -- this would be
|
|
419
|
+
double-counting individuals in a way that has no physical meaning --
|
|
420
|
+
compartment quantities are automatically omitted even if they are part of the
|
|
421
|
+
selection, so only events are reflected in the result.
|
|
422
|
+
|
|
423
|
+
Parameters
|
|
424
|
+
----------
|
|
425
|
+
geo :
|
|
426
|
+
The geographic selection to make on the output data.
|
|
427
|
+
time :
|
|
428
|
+
The time selection to make on the output data.
|
|
429
|
+
quantity :
|
|
430
|
+
The quantity selection to make on the output data.
|
|
431
|
+
ordering :
|
|
432
|
+
Controls the ordering of rows in the result;
|
|
433
|
+
both location and quantity are used to sort the resulting rows,
|
|
434
|
+
this just decides which gets priority.
|
|
435
|
+
result_format :
|
|
436
|
+
Controls the type of the result of this method;
|
|
437
|
+
"dataframe" returns a Pandas dataframe,
|
|
438
|
+
"string" returns the stringified table, and
|
|
439
|
+
"print" just prints the stringified table directly and returns `None`.
|
|
440
|
+
|
|
441
|
+
Returns
|
|
442
|
+
-------
|
|
443
|
+
:
|
|
444
|
+
Output according to the value of the `result_format` parameter.
|
|
445
|
+
"""
|
|
446
|
+
if any(isinstance(q, CompartmentDef) for q in quantity.selected):
|
|
447
|
+
warn(
|
|
448
|
+
"Although your selection includes IPM compartments, "
|
|
449
|
+
"`sum()` has removed these from the output.\nThis is because "
|
|
450
|
+
"adding a time-series of compartment values is likely to "
|
|
451
|
+
"double-count individuals and produce a misleading result.\n"
|
|
452
|
+
"Adding a time-series of event occurrences, on the other hand, is fine."
|
|
453
|
+
)
|
|
454
|
+
|
|
455
|
+
# Since it doesn't make real sense to sum compartment values,
|
|
456
|
+
# exclude compartments from selection.
|
|
457
|
+
C, E = quantity.ipm.num_compartments, quantity.ipm.num_events
|
|
458
|
+
m = np.zeros(shape=C + E, dtype=np.bool_)
|
|
459
|
+
m[C:] = True
|
|
460
|
+
quantity = dataclasses.replace(quantity, selection=quantity.selection & m)
|
|
461
|
+
|
|
462
|
+
result_df = _process_output(
|
|
463
|
+
self.output,
|
|
464
|
+
geo,
|
|
465
|
+
time,
|
|
466
|
+
quantity,
|
|
467
|
+
ordering,
|
|
468
|
+
["sum"],
|
|
469
|
+
lambda df: df.sum().reset_index(),
|
|
470
|
+
)
|
|
471
|
+
return self._format_output(result_df, result_format)
|
|
472
|
+
|
|
473
|
+
@overload
|
|
474
|
+
def chart(
|
|
475
|
+
self,
|
|
476
|
+
geo: GeoSelection | GeoAggregation,
|
|
477
|
+
time: TimeSelection | TimeAggregation,
|
|
478
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
479
|
+
*,
|
|
480
|
+
chart_length: int = 20,
|
|
481
|
+
ordering: OrderingOption = "location",
|
|
482
|
+
result_format: Literal["dataframe"] = "dataframe",
|
|
483
|
+
) -> pd.DataFrame: ...
|
|
484
|
+
|
|
485
|
+
@overload
|
|
486
|
+
def chart(
|
|
487
|
+
self,
|
|
488
|
+
geo: GeoSelection | GeoAggregation,
|
|
489
|
+
time: TimeSelection | TimeAggregation,
|
|
490
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
491
|
+
*,
|
|
492
|
+
chart_length: int = 20,
|
|
493
|
+
ordering: OrderingOption = "location",
|
|
494
|
+
result_format: Literal["string"],
|
|
495
|
+
) -> str: ...
|
|
496
|
+
|
|
497
|
+
@overload
|
|
498
|
+
def chart(
|
|
499
|
+
self,
|
|
500
|
+
geo: GeoSelection | GeoAggregation,
|
|
501
|
+
time: TimeSelection | TimeAggregation,
|
|
502
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
503
|
+
*,
|
|
504
|
+
chart_length: int = 20,
|
|
505
|
+
ordering: OrderingOption = "location",
|
|
506
|
+
result_format: Literal["print"],
|
|
507
|
+
) -> None: ...
|
|
508
|
+
|
|
509
|
+
def chart(
|
|
510
|
+
self,
|
|
511
|
+
geo: GeoSelection | GeoAggregation,
|
|
512
|
+
time: TimeSelection | TimeAggregation,
|
|
513
|
+
quantity: QuantitySelection | QuantityAggregation,
|
|
514
|
+
*,
|
|
515
|
+
chart_length: int = 20,
|
|
516
|
+
ordering: OrderingOption = "location",
|
|
517
|
+
result_format: FormatOption = "dataframe",
|
|
518
|
+
) -> pd.DataFrame | str | None:
|
|
519
|
+
"""
|
|
520
|
+
Render a table showing a rough time series bar chart for the given selections
|
|
521
|
+
using ASCII characters.
|
|
522
|
+
|
|
523
|
+
It is of course limited by the fact that this is a
|
|
524
|
+
relatively coarse display method. The y-axis of each chart is on its own scale,
|
|
525
|
+
and thus is not comparable to others. However the x-axis is on a shared scale,
|
|
526
|
+
so this can give you an idea of the time-series behavior of your simulation
|
|
527
|
+
and relative timing between the selected quantities and locations.
|
|
528
|
+
|
|
529
|
+
Parameters
|
|
530
|
+
----------
|
|
531
|
+
geo :
|
|
532
|
+
The geographic selection to make on the output data.
|
|
533
|
+
time :
|
|
534
|
+
The time selection to make on the output data.
|
|
535
|
+
quantity :
|
|
536
|
+
The quantity selection to make on the output data.
|
|
537
|
+
chart_length :
|
|
538
|
+
Approximately how many characters should we use to render the charts?
|
|
539
|
+
This is simply a ballpark, similar to automatically selecting the number
|
|
540
|
+
of bins in a histogram, so you may get more or less than you ask for.
|
|
541
|
+
Multiple days may be compressed into one bin, but one day will never be
|
|
542
|
+
split between bins. The last bin may contain less days of data than the
|
|
543
|
+
rest of the bins.
|
|
544
|
+
ordering :
|
|
545
|
+
Controls the ordering of rows in the result;
|
|
546
|
+
both location and quantity are used to sort the resulting rows,
|
|
547
|
+
this just decides which gets priority.
|
|
548
|
+
result_format :
|
|
549
|
+
Controls the type of the result of this method;
|
|
550
|
+
"dataframe" returns a Pandas dataframe,
|
|
551
|
+
"string" returns the stringified table, and
|
|
552
|
+
"print" just prints the stringified table directly and returns `None`.
|
|
553
|
+
|
|
554
|
+
Returns
|
|
555
|
+
-------
|
|
556
|
+
:
|
|
557
|
+
Output according to the value of the `result_format` parameter.
|
|
558
|
+
"""
|
|
559
|
+
if isinstance(time, TimeSelection):
|
|
560
|
+
# Unless the user supplies their own TimeAggregation,
|
|
561
|
+
# use NBins to condense the series.
|
|
562
|
+
time = time.group(NBins(chart_length)).agg()
|
|
563
|
+
|
|
564
|
+
result_df = _process_output(
|
|
565
|
+
self.output,
|
|
566
|
+
geo,
|
|
567
|
+
time,
|
|
568
|
+
quantity,
|
|
569
|
+
ordering,
|
|
570
|
+
["chart"],
|
|
571
|
+
lambda df: df.agg(_to_sparklines).reset_index(),
|
|
572
|
+
)
|
|
573
|
+
return self._format_output(result_df, result_format)
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
class TableRendererMixin(Output):
|
|
577
|
+
"""Mixin class that adds a convenient method for rendering tables from an output."""
|
|
578
|
+
|
|
579
|
+
@property
|
|
580
|
+
def table(self) -> TableRenderer:
|
|
581
|
+
"""Render a table from this output."""
|
|
582
|
+
return TableRenderer(self)
|