Getting Started
Build temporal observations, compute a layout sequence, and export an animation. The tutorial then explores interpolation and an illustrative infection display; displayed attributes are supplied data, not an epidemic model fitted by NDTV.jl.
Layouts encode graph structure, not physical positions or an inferred movement process. Interpolation is a visual transition between frames. HTML and SVG exports are self-contained outputs; video and GIF export require the external tools documented in the export guide.
Installation
Use Julia 1.12 or newer and the shared workspace installation guide. These development packages are not yet registered; the guide prepares the required sibling checkouts and a Julia environment for the examples.
Run the blocks below in order in that environment. They build on variables from earlier steps; stochastic examples use seeded random number generators where shown.
Basic Workflow
The typical NDTV.jl workflow consists of four steps:
- Create a dynamic network – Define vertices and edges with activity spells
- Compute layouts – Position vertices at each time point using layout algorithms
- Visualize – Generate timelines, filmstrips, or animation frames
- Export – Save results as HTML, video, or GIF
Step 1: Create a Dynamic Network
Dynamic networks are created using DynamicNetworks.jl. Vertices and edges have activity spells that define when they are present:
using NetworkCore # for nv, ne on extracted snapshots
using DynamicNetworks
using NDTV, Random
# Create a dynamic network with 5 vertices observed from t=0 to t=100
dnet = DynamicNetwork(5; observation_start=0.0, observation_end=100.0)
# Activate all vertices for the full observation period
for i in 1:5
activate!(dnet, 0.0, 100.0; vertex=i)
end
# Add edge activity spells
# Edge (1,2) is active from t=0 to t=40
activate!(dnet, 0.0, 40.0; edge=(1, 2))
# Edge (2,3) is active from t=20 to t=70
activate!(dnet, 20.0, 70.0; edge=(2, 3))
# Edge (3,4) is active from t=40 to t=90
activate!(dnet, 40.0, 90.0; edge=(3, 4))
# Edge (4,5) is active from t=60 to t=100
activate!(dnet, 60.0, 100.0; edge=(4, 5))
# Edge (1,3) appears temporarily from t=10 to t=30
activate!(dnet, 10.0, 30.0; edge=(1, 3))Understanding Activity Spells
Each vertex and edge can have multiple activity spells – intervals during which they are active. A vertex or edge can appear, disappear, and reappear. Activation merges overlapping or adjacent spells (as R's activate.* do), so a gap is made with deactivate!:
# Vertex 1 leaves between t=50 and t=70: its spell [0,100) becomes [0,50) and [70,100)
deactivate!(dnet, 50.0, 70.0; vertex=1)
# Edge (1,2) gets a second spell; [0,30) is absorbed by the existing [0,40)
activate!(dnet, 0.0, 30.0; edge=(1, 2))
activate!(dnet, 60.0, 90.0; edge=(1, 2))
get_spells(dnet; edge=(1, 2)) # [Spell[0.0, 40.0), Spell[60.0, 90.0)]Vertices and edges that never receive a spell are active throughout, as in R's networkDynamic (active.default = TRUE).
Network Snapshots
At any time point, you can extract the active subnetwork:
# Get the network at time t=25
snapshot = network_extract(dnet, 25.0)
println("Vertices at t=25: ", nv(snapshot))
println("Edges at t=25: ", ne(snapshot))Step 2: Compute Animation Layout
NDTV.jl computes vertex positions for each time point using force-directed or geometric layout algorithms:
# Compute animation with 50 frames using Fruchterman-Reingold layout
layout = render_animation(dnet;
algorithm=FRLayout(),
n_frames=50,
interpolation=:linear,
rng=Xoshiro(1)
)
println("Number of frames: ", length(layout))
println("Time range: ", layout.times[1], " to ", layout.times[end])
println("Layout bounds: ", layout.bounds)The result is a DynamicLayout containing vertex positions at each frame:
# Access positions at frame 1
positions_frame1 = layout[1]
# Returns Dict{vertex_id => (x, y)}
# Access the time for each frame
layout.times # Vector of time pointsAnchored Layouts
With FRLayout() and anchor=true, each frame starts from the previous frame's positions before force-directed updates. Reusing these coordinates can reduce visual jumps; it does not fix positions or guarantee a smooth result:
# Compute layout sequence with anchoring (default)
times = collect(0.0:10.0:100.0)
layout = layout_sequence(dnet, times;
algorithm=FRLayout(),
anchor=true, # Use previous positions as starting point
rng=Xoshiro(2)
)
# Without anchoring -- each frame is computed independently
layout_unanchored = layout_sequence(dnet, times;
algorithm=FRLayout(),
anchor=false,
rng=Xoshiro(2)
)Anchoring affects the force-directed initialization. CircleLayout() and MDSLayout() recompute their deterministic layouts, and layout movement should not be interpreted as physical motion or changing social distance.
Choosing a Layout Algorithm
NDTV.jl provides four layout algorithms:
# Fruchterman-Reingold: Force-directed placement
fr = FRLayout(iterations=100, cooling=0.95, k=1.0)
# Classical MDS of geodesic distances (deterministic, no parameters).
# NOT Kamada-Kawai: no iterative energy minimization is performed.
# On a dynamic network FR and MDS lay out only the vertices active in each
# frame.
mds = MDSLayout()
# Circle: Uniform circular placement
circle = CircleLayout(radius=1.0, start_angle=0.0)
# Random: Random uniform placement
random = RandomLayout(xmin=0.0, xmax=1.0, ymin=0.0, ymax=1.0)| Algorithm | What determines positions | Across frames |
|---|---|---|
FRLayout | Iterative attractive and repulsive forces | Optional initialization from previous positions; inspect movement |
MDSLayout | Classical MDS of geodesic distances | Recomputed; changing distances can rotate or move the drawing |
CircleLayout | Vertex order on a circle | Fixed for the same retained actor set and parameters |
RandomLayout | Uniform random coordinates | Seed the RNG; anchoring keeps existing positions |
Step 3: Visualize the Network
Timeline Plot
The simplest visualization is an ASCII timeline showing when vertices and edges are active:
timeline_plot(dnet)This produces (the window is the observation period; bars run to its edge for open-ended spells):
Timeline: 0.0 to 100.0
============================================================
Vertices:
V1: |─────────────────────────────── ───────────────────|
V2: |────────────────────────────────────────────────────────────|
V3: |────────────────────────────────────────────────────────────|
V4: |────────────────────────────────────────────────────────────|
V5: |────────────────────────────────────────────────────────────|
Edges:
1→2: |═════════════════════════ ═══════════════════ |
1→3: | ═════════════ |
2→3: | ══════════════════════════════ |
3→4: | ══════════════════════════════ |
4→5: | ═════════════════════════|You can control the display width:
timeline_plot(dnet; width=80)Proximity Timeline
View activity from a single vertex's perspective:
# Show all edges involving vertex 2
ego_timeline(dnet, 2)Output:
Ego timeline for vertex 2
============================================================
← V1: |═════════════════════════ ═══════════════════ |
→ V3: | ══════════════════════════════ |Filmstrip
Generate data for multiple snapshots side-by-side:
# Network snapshots at 4 instants (t = 100 would be empty: spells are
# half-open, so everything active "until 100" has ended at 100)
times = [0.0, 25.0, 50.0, 75.0]
frames = filmstrip(dnet, times; algorithm=FRLayout(), rng=Xoshiro(1))
for frame in frames
println("t=$(frame.time): $(frame.n_vertices) vertices, $(frame.n_edges) edges")
endt=0.0: 5 vertices, 1 edges
t=25.0: 5 vertices, 3 edges
t=50.0: 4 vertices, 2 edges
t=75.0: 5 vertices, 3 edgesslice_layout cuts an interval into equal, contiguous slices; by default each frame shows everything active during its slice (R ndtv's aggregated slices), so short spells between two instants are not lost:
frames = slice_layout(dnet, 0.0, 100.0; n_slices=5, algorithm=FRLayout(), rng=Xoshiro(1))
[(f.time, f.n_edges) for f in frames] # slices [0,20), [20,40), ...: 2, 3, 2, 4, 3 edgesTransmission Timeline
For visualizing diffusion or contagion events:
# Define transmission events: (source, target, time)
transmissions = [
(1, 2, 5.0), # Vertex 1 transmits to 2 at t=5
(2, 3, 15.0), # Vertex 2 transmits to 3 at t=15
(2, 4, 20.0), # Vertex 2 transmits to 4 at t=20
(3, 5, 30.0), # Vertex 3 transmits to 5 at t=30
]
transmission_timeline(dnet, transmissions)Output:
Transmission Timeline
============================================================
1→2: | * | t=5.0
2→3: | * | t=15.0
2→4: | * | t=20.0
3→5: | * | t=30.0Step 4: Export the Animation
HTML Export
The simplest export format creates a self-contained HTML file:
layout = render_animation(dnet; n_frames=100, rng=Xoshiro(3))
export_html(layout, "my_animation.html";
config=HTMLConfig(width=800, height=600, controls=true)
)Video Export
Video export requires FFmpeg to be installed on your system:
export_movie(layout, "my_animation.mp4";
config=VideoConfig(fps=30, width=800, height=600, codec="h264")
)GIF Export
GIF export requires ImageMagick:
export_gif(layout, "my_animation.gif";
config=GIFConfig(fps=10, width=400, height=400, loop=0)
)Interpolated Layout
For smoother animation between computed frames, use interpolated layouts:
# Compute base layout
base = layout_sequence(dnet, collect(0.0:20.0:100.0);
algorithm=FRLayout()
)
# Create interpolated layout with linear interpolation
interp_linear = InterpolatedLayout(base; interpolation=:linear)
# Or with ease (smooth acceleration/deceleration)
interp_ease = InterpolatedLayout(base; interpolation=:ease)
# Query any vertex position at any time
pos = get_position(interp_ease, 1, 35.0)
println("Vertex 1 at t=35: x=$(pos[1]), y=$(pos[2])")Interpolation modes:
| Mode | Formula | Effect |
|---|---|---|
:linear | $p(t) = p_1 + \alpha(p_2 - p_1)$ | Constant velocity |
:ease | Cubic ease-in-out | Smooth acceleration and deceleration |
Complete Example: Epidemic Visualization
Here is a complete example combining multiple NDTV.jl features to visualize disease transmission through a contact network:
using DynamicNetworks
using NDTV, Random
# Create a contact network with 10 individuals
dnet = DynamicNetwork(10; observation_start=0.0, observation_end=50.0)
# Activate all individuals
for i in 1:10
activate!(dnet, 0.0, 50.0; vertex=i)
end
# Define contact patterns (edges)
contacts = [
(1, 2, 0.0, 50.0), # Household contacts
(1, 3, 0.0, 50.0),
(2, 4, 5.0, 40.0), # Workplace contacts
(3, 5, 5.0, 40.0),
(4, 6, 10.0, 35.0),
(5, 7, 10.0, 35.0),
(6, 8, 15.0, 30.0),
(7, 9, 15.0, 45.0),
(8, 10, 20.0, 50.0),
]
for (i, j, t_start, t_end) in contacts
activate!(dnet, t_start, t_end; edge=(i, j))
end
# Show the full timeline
println("=== Contact Timeline ===")
timeline_plot(dnet)
# Define transmission events
transmissions = [
(1, 2, 3.0), # Index case infects household member
(1, 3, 5.0),
(2, 4, 12.0), # Workplace transmission
(3, 5, 14.0),
(4, 6, 22.0), # Second wave
(5, 7, 25.0),
]
# Show transmission timeline
println("\n=== Transmission Events ===")
transmission_timeline(dnet, transmissions)
# Show proximity timeline for the index case
println("\n=== Index Case (Vertex 1) Contacts ===")
ego_timeline(dnet, 1)
# Compute animation layout
layout = render_animation(dnet; n_frames=100, algorithm=FRLayout(), rng=Xoshiro(4))
# Generate filmstrip at key time points
frames = filmstrip(dnet, [0.0, 10.0, 20.0, 30.0, 40.0, 50.0])
println("\n=== Network Snapshots ===")
for f in frames
println("t=$(f.time): $(f.n_vertices) vertices, $(f.n_edges) edges")
end
# Export animation
export_html(layout, "epidemic_animation.html")Best Practices
- Start with timeline plots: Use
timeline_plotto understand your data before computing layouts - Compare anchored layouts: For force-directed layouts, compare frames with
anchor=trueand inspect large position changes - Choose frames from the data: Include times that capture short activity spells; visual smoothness alone does not establish temporal coverage
- Seed for reproducibility: Use
Random.seed!()before layout computation for consistent results - Match FPS to frames: Total duration = n_frames / fps – aim for 5-15 second animations
- Export to HTML first: HTML export requires no dependencies and allows interactive exploration
Next Steps
- Learn about Animation Layout algorithms and configuration
- Explore Layout Algorithms in detail
- Read about Export options and formats