30def read_snapshots(file_name):
33 Return list of (time stamp, list of patch files) from a patch meta file
35 The visualiser resolves the included patch files relative to the meta
36 file, so we can call the routine from any directory.
39 visualiser =
Visualiser(file_name, start=0, end=sys.maxsize)
40 with contextlib.redirect_stdout(io.StringIO()):
41 visualiser.read_metadata()
43 (snapshot.timestamp, list(snapshot.filenames))
44 for snapshot
in visualiser.snapshots
48@functools.lru_cache(maxsize=256)
52 Return list of (offset, size, values) for all patches of one patch file
54 values is a numpy array of shape (dof, dof, unknowns), i.e. we index it
55 with values[y,x,unknown].
61 for attributes
in parser.unknown_attributes.values():
62 assert attributes.dimensions == 2,
"Only 2d data is supported"
63 assert attributes.is_data_associated_to_cell,
"Only cell data is supported"
64 for patch
in attributes.cell_data:
70 attributes.dof, attributes.dof, attributes.unknowns
99 file_name, snapshot=-1, unknown=0, clim=None, ax=None, cmap="viridis", label=None
103 Plot one unknown of one snapshot
106 Patch meta file as written by the Peano/ExaHyPE plotters.
109 Index of snapshot. Negative values count from the end.
112 Index of unknown to plot.
114 clim: (float,float) or None
115 Colour range. If None, we use the range of the plotted data.
117 Returns the matplotlib axes.
120 snapshots = read_snapshots(file_name)
121 time_stamp, patch_files = snapshots[snapshot]
124 _, ax = plt.subplots(figsize=(6, 5))
126 patches = [p
for f
in patch_files
for p
in read_patches(f)]
129 min(np.min(values[:, :, unknown])
for _, _, values
in patches),
130 max(np.max(values[:, :, unknown])
for _, _, values
in patches),
133 for offset, size, values
in patches:
135 values[:, :, unknown],
137 extent=(offset[0], offset[0] + size[0], offset[1], offset[1] + size[1]),
141 interpolation=
"nearest",
144 min(o[0]
for o, _, _
in patches), max(o[0] + s[0]
for o, s, _
in patches)
147 min(o[1]
for o, _, _
in patches), max(o[1] + s[1]
for o, s, _
in patches)
149 ax.set_aspect(
"equal")
150 ax.locator_params(nbins=5)
153 name = label
if label
is not None else "Q[{}]".format(unknown)
154 ax.set_title(
"{} at t={:.4f}".format(name, time_stamp))
155 ax.figure.colorbar(image, ax=ax)
162 Interactive viewer for a patch file within a Jupyter notebook
164 Adds a slider over all snapshots, a play button and - if unknown_names
165 is given - a dropdown to pick the unknown.
167 unknown_names: List of strings or None
168 Names of the unknowns in the order they are stored. If None, we take
169 the names from the patch file (@see read_unknown_names()), if it
172 clim: (float,float) or None
173 Fixed colour range. If None, the colour range per unknown is taken
174 from the first snapshot, so you see the solution evolve against a
180 snapshots = read_snapshots(file_name)
181 if unknown_names
is None:
183 if unknown_names
is None:
184 unknown_names = [
"Q[{}]".format(unknown)]
185 unknown_indices = {unknown_names[0]: unknown}
187 unknown_indices = {name: i
for i, name
in enumerate(unknown_names)}
189 @functools.lru_cache(maxsize=None)
190 def colour_range(index):
193 patches = [p
for f
in snapshots[0][1]
for p
in read_patches(f)]
194 low = min(np.min(values[:, :, index])
for _, _, values
in patches)
195 high = max(np.max(values[:, :, index])
for _, _, values
in patches)
197 low, high = low - 0.5, high + 0.5
200 def draw(snapshot, name):
201 index = unknown_indices[name]
202 plot_snapshot(file_name, snapshot, index, clim=colour_range(index), label=name)
205 slider = ipywidgets.IntSlider(
206 value=0, min=0, max=len(snapshots) - 1, description=
"snapshot"
208 play = ipywidgets.Play(
209 value=0, min=0, max=len(snapshots) - 1, interval=300, description=
"play"
211 ipywidgets.jslink((play,
"value"), (slider,
"value"))
212 selector = ipywidgets.Dropdown(
213 options=list(unknown_indices.keys()),
214 value=[n
for n, i
in unknown_indices.items()
if i == unknown][0],
215 description=
"unknown",
217 output = ipywidgets.interactive_output(draw, {
"snapshot": slider,
"name": selector})
218 return ipywidgets.VBox([ipywidgets.HBox([play, slider, selector]), output])
plot_snapshot(file_name, snapshot=-1, unknown=0, clim=None, ax=None, cmap="viridis", label=None)
Plot one unknown of one snapshot.