It is difficult to rank the importance of data points when you need to consider both how closely related they are to each other and where they are located spatially.
It ranks data points by looking at their connections and their geometric proximity at the same time.
It provides a way to prioritize information based on both context and location.
It was run in the sandbox and it failed. run output shows an error/traceback — the artifact does NOT run clean.
$ python3 saliency_graph_tile.py Saliency Scores: A: 8.4852 B: 8.4852 C: 0.0000 Most salient node: A
No screenshot — there is nothing working to show. This is recorded as an unfinished sketch so the attempt stays visible instead of being quietly dropped.
All of it — 108 lines, one file, standard library only.
import math
from collections import defaultdict, deque
class Graph:
def __init__(self):
self.nodes = {}
self.edges = defaultdict(list)
self.spatial_index = defaultdict(list)
self.tile_size = 1.0
self.tile_density = {}
def add_node(self, node_id, coords, semantic_weight=1.0):
self.nodes[node_id] = {'coords': coords, 'semantic_weight': semantic_weight}
def add_edge(self, src, dst):
self.edges[src].append(dst)
self.edges[dst].append(src)
def partition_spatially(self, tile_size=1.0):
self.tile_size = tile_size
self.spatial_index = defaultdict(list)
for node_id, data in self.nodes.items():
x, y = data['coords']
tile_x = math.floor(x / tile_size)
tile_y = math.floor(y / tile_size)
self.spatial_index[(tile_x, tile_y)].append(node_id)
# Calculate tile density
self.tile_density = {}
for tile, nodes in self.spatial_index.items():
self.tile_density[tile] = len(nodes)
def get_tile_for_node(self, node_id):
x, y = self.nodes[node_id]['coords']
tile_x = math.floor(x / self.tile_size)
tile_y = math.floor(y / self.tile_size)
return (tile_x, tile_y)
def calculate_reachability(self):
reachability = {}
for node in self.nodes:
distances = {n: -1 for n in self.nodes}
distances[node] = 0
queue = deque([node])
while queue:
current = queue.popleft()
for neighbor in self.edges[current]:
if distances[neighbor] == -1:
distances[neighbor] = distances[current] + 1
queue.append(neighbor)
total = sum(d for d in distances.values() if d != -1)
reachable_nodes = sum(1 for d in distances.values() if d != -1)
reachability[node] = total / reachable_nodes if reachable_nodes else 0
# Apply density-aware adjustment
for node in reachability:
tile = self.get_tile_for_node(node)
density = self.tile_density.get(tile, 0)
max_density = max(self.tile_density.values()) if self.tile_density else 0
if max_density > 0:
density_factor = 1.0 + (density / max_density)
else:
density_factor = 1.0
reachability[node] *= density_factor # Penalize dense tiles
return reachability
def calculate_saliency(self, tile_size=1.0):
self.partition_spatially(tile_size)
reachability = self.calculate_reachability()
spatial_weights = defaultdict(float)
for tile, nodes in self.spatial_index.items():
for node in nodes:
spatial_weights[node] = sum(1 / (math.dist(self.nodes[node]['coords'], self.nodes[other]['coords']) + 1e-6)
for other in nodes if other != node)
saliency = {}
for node in self.nodes:
g_score = 1 / (reachability[node] + 1e-6)
s_score = spatial_weights.get(node, 0)
semantic = self.nodes[node]['semantic_weight']
saliency[node] = g_score * s_score * semantic
return saliency
# Example usage
if __name__ == "__main__":
g = Graph()
# Add sample nodes with coordinates and semantic weights
g.add_node('A', coords=(0.0, 0.0), semantic_weight=1.2)
g.add_node('B', coords=(0.1, 0.1), semantic_weight=0.8)
g.add_node('C', coords=(1.0, 1.0), semantic_weight=1.5)
g.add_edge('A', 'B')
g.add_edge('B', 'C')
# Calculate and show results
saliency_scores = g.calculate_saliency(tile_size=0.5)
print("Saliency Scores:")
for node, score in sorted(saliency_scores.items(), key=lambda x: x[1], reverse=True):
print(f"{node}: {score:.4f}")
# Output most salient node
most_salient = max(saliency_scores, key=saliency_scores.get)
print(f"\nMost salient node: {most_salient}")