#Seeing your results
eLucid8 writes its results in forms the tools you already use can draw: a Zarr store for xarray, matplotlib, Plotly and napari; a Vega-Lite chart for Jupyter, VS Code, Observable and AI assistants; and values in Prometheus's format for live Grafana dashboards. This page gives a recipe for each; each one runs as written, and prints what is shown under it.
| you use | ask elucid8 for | how it draws |
|---|---|---|
| xarray, then matplotlib, Plotly or napari | a Zarr store (--format zarr), or elucid8.to_xarray from Python | values over named dimensions with their coordinates; eod and errors as NaN, told apart by each cell's kind |
| Jupyter, VS Code, Observable, an AI assistant | a Vega-Lite chart (--format vega-lite) | a picture, a line or bars, with a slider over the dimension the value moves along |
| Grafana | values in Prometheus's format, from elucid8 serve | each cell a time series, scraped as data arrives |
These forms are newer than release 0.18.0: elucid8 0.18.0 does not write them.
Every form keeps what makes eLucid8's answers eLucid8's: each cell's kind (a value, eod, or an error), its timestamp, and where a cell is an error, the error itself.
The programs on this page are in the language's notation v0.2.
./elucid8 --versionsays which plans yourelucid8runs.
#The example: heat on a plate
Heat spreading from a hot spot, a picture over x and y that changes along time. Save this program as heat.el8 in a folder heat:
-- Heat spreading on a 16 × 16 plate from a hot spot at its centre, one time step after another.
initial = restrict if #x == 8 and #y == 8 then 100 else 0 to (x : 16, y : 16) end
heat = restrict
initial fby.time mean heat over (x : #x - 1 .. #x + 1, y : #y - 1 .. #y + 1) end
to (time : inf, x : 16, y : 16) end
Compile it to plan.json with the site's compile endpoint (the command is in Using eLucid8 with AI coding agents), or, from 0.19.0, with elucid8 compile heat.el8 -o plan.json, and put the elucid8 command beside it. It reads no data, so it needs no configuration.
#A Zarr store, read by xarray
A Zarr store is a folder of arrays with their dimensions' names: xarray opens it as a dataset, with each dimension's coordinates. Ask for the plate at times 0 to 40:
# In the heat folder:
./elucid8 run plan.json --query 'heat[time = 0..40, x = 0..15, y = 0..15]' --out heat.zarr
ls heat.zarr
heat_kind
heat_ts
heat_value
time
x
y
The store holds, for a value heat:
heat_value: the values, over the dimensions in the order the query names them; NaN where a cell is not a value (booleans: false; text: the empty text);heat_kind: each cell's kind, 0 a value, 1 eod, 2 an error;heat_ts: each cell's timestamp, in milliseconds (over one dimension only, where it depends on that one alone; xarray broadcasts it);heat_error, when there are errors: each error, as JSON, where the cell is one;- one array per dimension, its coordinates: whole numbers, or names.
# xarray_zarr.py: run it in the heat folder with python3 xarray_zarr.py
import xarray as xr
ds = xr.open_zarr("heat.zarr")
heat = ds["heat_value"]
print(heat.dims, heat.shape)
print("the centre at time 40:", round(float(heat.sel(time=40, x=8, y=8)), 4))
print("cells that are values:", int((ds["heat_kind"] == 0).sum()), "of", heat.size)
('time', 'x', 'y') (41, 16, 16)
the centre at time 40: 0.6238
cells that are values: 10496 of 10496
From Python, the elucid8 package gives the same dataset without a file: elucid8.to_xarray turns what elucid8.run (or a service's query) returns into it (eLucid8 from Python):
# from_python.py: run it in the heat folder with python3 from_python.py
import elucid8
table = elucid8.run("plan.json", query="heat[time = 0..40, x = 0..15, y = 0..15]", binary="./elucid8")
ds = elucid8.to_xarray(table)
print(list(ds.data_vars))
print("the centre at time 40:", round(float(ds["heat_value"].sel(time=40, x=8, y=8)), 4))
['heat_value', 'heat_kind', 'heat_ts']
the centre at time 40: 0.6238
#matplotlib: a picture, and an animation
# matplotlib_heat.py: run it in the heat folder with python3 matplotlib_heat.py
import matplotlib
matplotlib.use("Agg") # files only, no window
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation, PillowWriter
import xarray as xr
heat = xr.open_zarr("heat.zarr")["heat_value"]
# One step, as a picture: x across, y up.
heat.sel(time=10).plot(x="x", y="y")
plt.savefig("heat-10.png")
plt.close()
# Every step, as an animation.
fig, ax = plt.subplots()
image = ax.imshow(heat.sel(time=0).T, origin="lower", vmin=0, vmax=float(heat.max()))
fig.colorbar(image)
def show(t):
image.set_data(heat.sel(time=t).T)
ax.set_title(f"heat at time {t}")
return [image]
frames = list(heat["time"].values)
FuncAnimation(fig, show, frames=frames).save("heat.gif", writer=PillowWriter(fps=10))
print("saved heat-10.png, and heat.gif with", len(frames), "frames")
saved heat-10.png, and heat.gif with 41 frames
#Plotly: a slider over time
# plotly_slider.py: run it in the heat folder with python3 plotly_slider.py
import plotly.express as px
import xarray as xr
heat = xr.open_zarr("heat.zarr")["heat_value"]
# A picture per step, with a slider over time and a play button: x across, y up.
fig = px.imshow(heat.transpose("time", "y", "x"), animation_frame="time", origin="lower", zmin=0, zmax=float(heat.max()))
fig.write_html("heat.html", include_plotlyjs="cdn")
print("saved heat.html:", len(fig.frames), "frames, a slider of", len(fig.layout.sliders[0].steps), "steps")
saved heat.html: 41 frames, a slider of 41 steps
Open heat.html in a browser, or show fig in a notebook. hvPlot does the same from the dataset: ds.heat_value.hvplot.image(x="x", y="y", groupby="time").
#napari: large grids
napari draws arrays of millions of cells and gives a slider for each dimension it does not draw:
# napari_view.py: run it in the heat folder with python3 napari_view.py
import napari
import xarray as xr
heat = xr.open_zarr("heat.zarr")["heat_value"].transpose("time", "y", "x")
viewer = napari.Viewer()
viewer.add_image(heat.values, name="heat", colormap="magma")
viewer.dims.axis_labels = heat.dims # napari's slider steps through time
print("napari shows heat:", dict(zip(heat.dims, heat.shape)))
napari.run()
napari shows heat: {'time': 41, 'y': 16, 'x': 16}
#A Vega-Lite chart
A Vega-Lite chart is one JSON file holding the data and how to draw it. Jupyter, VS Code (with a Vega viewer extension), Observable and AI assistants such as Claude draw it as it is.
# In the heat folder:
./elucid8 run plan.json --query 'heat[time = 0..40, x = 0..15, y = 0..15]' --out heat.vl.json
head -c 64 heat.vl.json; echo
{"$schema":"https://vega.github.io/schema/vega-lite/v5.json","co
elucid8 chooses the chart from the query's dimensions, as the playground and the eLucid8 Player choose their views:
- A dimension the query names one coordinate of is fixed, and named in the title.
- The dimension the value iterates along (here
time) gets a slider; the other dimensions are drawn. - Two or more dimensions drawn: a picture (a heatmap) of the last two, the second last across and the last up; any others get sliders too. One dimension: a line with its points (a number dimension) or bars (names); booleans and text as a strip of coloured cells. None: the value, large.
- Numbers are coloured from white to the playground's blue; eod cells are grey and errors red; in a line, eod and errors are marked by a rule. Pointing at a cell shows its coordinates, value (six significant digits), kind, timestamp and, for an error, what went wrong.
A chart holds at most 100,000 cells (a larger box is refused: draw it from a Zarr store). In a notebook, altair reads it, checks it against Vega-Lite's schema, and draws it:
# vega_chart.py: run it in the heat folder with python3 vega_chart.py
import json
import altair as alt
with open("heat.vl.json") as f:
spec = json.load(f)
chart = alt.Chart.from_dict(spec) # checks it against Vega-Lite's schema; in Jupyter, `chart` draws it
chart.save("heat-chart.png") # draws it with Vega
print("saved heat-chart.png:", spec["title"], "with a slider over", spec["params"][0]["bind"]["name"].strip())
saved heat-chart.png: heat with a slider over time
An output the configuration names ("to": "chart.vl.json", or "to": "cells.zarr") holds several values: a store holds each value's arrays, and a chart draws them side by side, sharing the sliders.
#Grafana: live values through Prometheus
elucid8 serve answers the values its configuration names at GET /values, in Prometheus's text format: Prometheus scrapes them as data arrives, and Grafana draws them. The example: three sensors' readings, each smoothed as they come. Save this program as level.el8 in a folder sensors:
-- Three sensors' readings, each smoothed: half the reading, half the level before.
reading = input("reading", t : inf, s : 3)
level = reading fby.t (0.5 * level + 0.5 * next.t reading)
Compile it to plan.json there, beside the elucid8 command, with its readings so far in reading.csv:
t,s,value
0,0,20
0,1,21
0,2,19
1,0,22
1,1,error: probe fault
1,2,20
and a deploy.json naming the values to answer: a metric's name (letters, digits and _), then the query, here the newest level of each sensor:
{
"inputs": { "reading": { "from": "reading.csv" } },
"http": { "values": { "sensor_level": "level[t = last, s = 0..2]" } }
}
Start the service with a token, as Running eLucid8 as a service says, wait until /health says it is ready (up to 10 s), and ask for the values:
# In the sensors folder:
openssl rand -hex 16 > token
./elucid8 serve plan.json --config deploy.json --http 127.0.0.1:8080 --token-file token > serve.log 2>&1 &
for i in $(seq 100); do curl -sf http://127.0.0.1:8080/health > /dev/null && break; sleep 0.1; done
curl -s -H "Authorization: Bearer $(cat token)" http://127.0.0.1:8080/values
# HELP sensor_level level[t = last, s = 0..2] (elucid8 serve)
# TYPE sensor_level gauge
sensor_level{t="1",s="0"} 21
sensor_level{t="1",s="2"} 19.5
# HELP sensor_level_timestamp_seconds Each cell's timestamp, in seconds.
# TYPE sensor_level_timestamp_seconds gauge
sensor_level_timestamp_seconds{t="1",s="0"} 0
sensor_level_timestamp_seconds{t="1",s="2"} 0
# HELP sensor_level_cells The cells of sensor_level by kind; stopped is 1 when a program error stopped the query.
# TYPE sensor_level_cells gauge
sensor_level_cells{kind="value"} 2
sensor_level_cells{kind="eod"} 0
sensor_level_cells{kind="error"} 1
sensor_level_cells{kind="other"} 0
sensor_level_cells{kind="stopped"} 0
For each metric m:
m: a sample per cell that is a number (a boolean is 1 or 0), labelled by the query's dimensions;m_timestamp_seconds: each such cell's timestamp, in seconds;m_cells: the cells by kind: values, eod, errors (sensor 1's probe fault, above),other(text and lists, which are not samples), andstopped, 1 when a program error stopped the query.
Each scrape answers with the newest data: last moves as rows arrive. A query is limited to 10,000 cells (each is a time series), and a scrape takes its turn with the service's other requests. Avoid names ending in _total, _sum or _count, which Prometheus keeps for counters, summaries and histograms.
Prometheus scrapes it with the token (prometheus.yml):
scrape_configs:
- job_name: elucid8-values
metrics_path: /values
scrape_interval: 5s
authorization:
credentials_file: /etc/prometheus/elucid8-token
static_configs:
- targets: ["127.0.0.1:8080"]
In Grafana, add Prometheus as a data source and draw sensor_level (one line per sensor), or alert on sensor_level_cells{kind="error"} > 0. Stop the service when you are done:
# In the sensors folder:
curl -s -X POST -H "Authorization: Bearer $(cat token)" -H "Content-Type: application/json" http://127.0.0.1:8080/shutdown; echo
{"id":null,"status":"done","now":false}