Skip to content

Focal search

Full example code: examples/algorithm/focal_search.py

Overview

  1. Load data
  2. Create graph
  3. Find focal sources and sinks
  4. Visualization

Loading data

We load a simple file containing coordinates with a local activation time using the DgmReader.

import numpy as np
from opendgm.parser import DgmReader

out = DgmReader.load_lat_file("focals_lat.txt")

# Convert parsed data
coords = out[["x", "y", "z"]].values
scalars = out["LAT_0"].values
scalars[scalars == -1] = np.nan # Get rid of bad measurements

Create graph

Next we create a graph on which we will apply our algorithms to find focal sources and sinks.

properties = {
    'max_edge_length': 12,
    'cv_min': 1,
    'cv_max': 200,
}
filters = "cvfilter"

graph = Graph(coords, scalars)
graph.set_properties(properties)
graph.apply_filter(filters)

Focal search

We define a radius to see how far at most from each node we want to search. We also define a threshold for both focal sources and sinks.

# Set parameters to determine sources and sinks
radius = 16
source_threshold = 0.9
sink_threshold = -0.55

# Area_flows = in, out and contained
area_flows = AreaFlows(graph).set_radius(radius).run()
heatmap = NodeSources(*area_flows).run()

# Filter out sources and sinks using thresholds
coords_source = coords[heatmap > source_threshold]
coords_sink = coords[heatmap < sink_threshold]

Visualization

Using our NodeBuilder class we can easily visualize our result. In the first subplot, we show the heatmap. In the second, the detected focal sources and sinks after applying thresholds to our heatmap.

heatmap_nodes = NodeBuilder(coords).set_size(4.5).set_scalars(heatmap)

source_nodes = NodeBuilder(coords_source).set_size(8).set_color('#65FF1E')
sink_nodes = NodeBuilder(coords_sink).set_size(8).set_color('#CA59FF')

graph_vis = GraphBuilder(graph).set_edge_opacity(.5).set_node_size(2)

title1, title2 = (
    TextBuilder(title).set_position(0.35, 0.88).set_font_size(24)
    for title in ["Heatmap", "Sources and sinks"]
)

subplot = Subplots(1280, 640, nrows=1, ncols=2)

subplot.renderers[0].add_object(heatmap_nodes)
subplot.renderers[0].add_object(title1)

subplot.renderers[1].add_object(graph_vis)
subplot.renderers[1].add_object(source_nodes)
subplot.renderers[1].add_object(sink_nodes)
subplot.renderers[1].add_object(title2)

subplot.start()

Focal sources and sinks