3
3
About xarray-simlab
4
4
===================
5
5
6
- xarray-simlab provides a framework for easily building custom computational
7
- models from a set of modular components (i.e., Python classes) , called
6
+ xarray-simlab provides a framework to easily build custom
7
+ computational models from a collection of modular components, called
8
8
processes.
9
9
10
- The framework handles issues that scientists who are developing models
11
- should not care too much about, like the model interface and the
12
- overall workflow management. Both are automatically determined from
13
- the succint, declarative-like interfaces of the model processes .
10
+ It also provides an extension to ` xarray < https://xarray.pydata.org >`_
11
+ (i.e., labeled arrays and datasets), that connects it to a wide range
12
+ of libraries of the SciPy / PyData stack for processing, analysis,
13
+ visualization, etc .
14
14
15
- Notably via its xarray extension, xarray-simlab has already deep
16
- integration with the SciPy / PyData stack. Next versions will
17
- hopefully handle other technical issues like command line integration,
18
- interactive visualization and/or running many simulations in parallel,
19
- e.g., in the context of sensitivity analyses or inversion procedures.
15
+ In a nutshell
16
+ -------------
17
+
18
+ The Conway's Game of Life example shown below is adapted from this
19
+ `blog post <https://jakevdp.github.io/blog/2013/08/07/conways-game-of-life/ >`_
20
+ by Jake VanderPlas.
21
+
22
+ 1. Create new model components by writing compact Python classes,
23
+ i.e., very much like dataclasses _ (note: more features and Python <
24
+ 3.7 support are also available through the `attrs
25
+ <https://www.attrs.org> `_ library):
26
+
27
+ .. ipython ::
28
+
29
+ In [1]: import numpy as np
30
+ ...: import xsimlab as xs
31
+ ...:
32
+ ...:
33
+ ...: @xs.process
34
+ ...: class GameOfLife:
35
+ ...: world = xs.variable(dims=('x', 'y'), intent='inout')
36
+ ...:
37
+ ...: def run_step(self):
38
+ ...: nbrs_count = sum(
39
+ ...: np.roll(np.roll(self.world, i, 0), j, 1)
40
+ ...: for i in (-1, 0, 1) for j in (-1, 0, 1)
41
+ ...: if (i != 0 or j != 0)
42
+ ...: )
43
+ ...: self._world_next = (nbrs_count == 3) | (self.world & (nbrs_count == 2))
44
+ ...:
45
+ ...: def finalize_step(self):
46
+ ...: self.world[:] = self._world_next
47
+ ...:
48
+ ...:
49
+ ...: @xs.process
50
+ ...: class Glider:
51
+ ...: pos = xs.variable(dims='point_xy', description='glider position')
52
+ ...: world = xs.foreign(GameOfLife, 'world', intent='out')
53
+ ...:
54
+ ...: def initialize(self):
55
+ ...: x, y = self.pos
56
+ ...:
57
+ ...: kernel = [[1, 0, 0],
58
+ ...: [0, 1, 1],
59
+ ...: [1, 1, 0]]
60
+ ...:
61
+ ...: self.world = np.zeros((10, 10), dtype=bool)
62
+ ...: self.world[x:x+3, y:y+3] = kernel
63
+
64
+ 2. Create a new model just by providing a dictionary of model components:
65
+
66
+ .. ipython ::
67
+
68
+ In [2]: model = xs.Model({'gol': GameOfLife,
69
+ ...: 'init': Glider})
70
+
71
+ 3. Create an input :py:class: `xarray.Dataset `, run the model and get an
72
+ output Dataset:
73
+
74
+ .. ipython ::
75
+
76
+ In [3]: input_dataset = xs.create_setup(
77
+ ...: model=model,
78
+ ...: clocks={'step': np.arange(9)},
79
+ ...: input_vars={'init__pos': ('point_xy', [4, 5])},
80
+ ...: output_vars={'step': 'gol__world'}
81
+ ...: )
82
+ ...:
83
+ ...: output_dataset = input_dataset.xsimlab.run(model=model)
84
+ ...:
85
+ ...: output_dataset
86
+
87
+ 4. Perform model setup, pre-processing, run, post-processing and
88
+ visualization in a functional style, using method chaining:
89
+
90
+ .. ipython ::
91
+
92
+ @savefig gol.png width=4in
93
+ In [5]: import matplotlib.pyplot as plt
94
+ ...:
95
+ ...: with model:
96
+ ...: (input_dataset
97
+ ...: .xsimlab.update_vars(
98
+ ...: input_vars={'init__pos': ('point_xy', [2, 2])}
99
+ ...: )
100
+ ...: .xsimlab.run()
101
+ ...: .gol__world.plot.imshow(
102
+ ...: col='step', col_wrap=3, figsize=(5, 5),
103
+ ...: xticks=[], yticks=[],
104
+ ...: add_colorbar=False, cmap=plt.cm.binary)
105
+ ...: )
106
+
107
+ .. _dataclasses : https://docs.python.org/3/library/dataclasses.html
20
108
21
109
Motivation
22
110
----------
23
111
24
- xarray-simlab is being developped with the idea of reducing the gap between the
25
- environments used for building and running computational models and the ones
26
- used for processing and analyzing simulation results. If the latter environments
27
- become more powerful and interactive, progress has still to be done for the
28
- former ones.
29
-
30
- xarray-simlab also encourages building new models from re-usable sets of
31
- components in order to avoid reinventing the wheel. In many cases we want to
32
- customize existing models (e.g., adding a new feature or slightly modifying
33
- the behavior) instead of building new models from scratch. This modular
34
- framework allows to do that with minimal effort. By implementing models using
35
- a large number of small components that can be easily plugged in/out, we
36
- eliminate the need of hard-coding changes that we want to apply to a model,
37
- which often leads to over-complicated code and interface.
38
-
39
- The design of this tool is thus mainly focused on both fast model development
40
- and easy, interactive model exploration. Ultimately, this would optimize the
41
- iterative back-and-forth process between ideas that we have on how to model a
42
- particular phenomenon and insights that we get from the exploration of model
43
- behavior.
112
+ xarray-simlab is a tool for * fast model development * and * easy,
113
+ interactive model exploration *. It aims at empowering scientists to do
114
+ better research in less time, collaborate efficiently and make new
115
+ discoveries.
116
+
117
+ ** Fast model development **: xarray-simlab allows building new models
118
+ from re-usable sets of components, with minimal effort. Models are
119
+ created dynamically and instantly just by plugging in/out components,
120
+ always keeping the model structure and interface tidy even in
121
+ situations where the model development workflow is highly experimental
122
+ or organic.
123
+
124
+ ** Interactive model exploration **: xarray-simlab is being developed
125
+ with the idea of reducing the gap between the environments used for
126
+ building and running computational models and the ones used for
127
+ processing, analyzing and visualizing simulation results. Users may
128
+ fully leverage powerful environments like jupyter _ at all stages of
129
+ their modeling workflow.
130
+
131
+ .. _ jupyter : https://jupyter.org/
44
132
45
133
Sources of inspiration
46
134
----------------------
@@ -49,8 +137,8 @@ xarray-simlab leverages the great number of packages that are part of the
49
137
Python scientific ecosystem. More specifically, the packages below have been
50
138
great sources of inspiration for this project.
51
139
52
- - xarray _: xarray-simlab actually provides an xarray extension for setting and
53
- running models.
140
+ - xarray _: xarray-simlab actually provides an xarray extension for
141
+ setting up and running models.
54
142
- attrs _: a package that allows writing Python classes without
55
143
boilerplate. xarray-simlab uses and extends attrs for writing
56
144
processes as succinct Python classes.
@@ -73,8 +161,6 @@ great sources of inspiration for this project.
73
161
processes. In this project we actually borrow some code from dask
74
162
for resolving process dependencies and for model visualization.
75
163
76
- .. _attrs : https://github.com/python-attrs/attrs
77
- .. _xarray : https://github.com/pydata/xarray
78
164
.. _dask : https://github.com/dask/dask
79
165
.. _luigi : https://github.com/spotify/luigi
80
166
.. _django : https://github.com/django/django
0 commit comments