darabos commited on
Commit
1c50522
·
1 Parent(s): ef3b791

Fixes for airlines demo.

Browse files
lynxkite-graph-analytics/src/lynxkite_plugins/graph_analytics/lynxkite_ops.py CHANGED
@@ -201,8 +201,7 @@ def import_graphml(*, filename: str):
201
  else:
202
  raise ValueError("No GraphML file found in the ZIP archive.")
203
  else:
204
- with z.open(filename) as f:
205
- G = nx.read_graphml(f)
206
  return G
207
 
208
 
@@ -317,7 +316,9 @@ def _map_color(value):
317
  colors = cmap.colors[: len(categories)]
318
  return [
319
  "#{:02x}{:02x}{:02x}".format(int(r * 255), int(g * 255), int(b * 255))
320
- for r, g, b in [colors[categories.get_loc(v)] for v in value]
 
 
321
  ]
322
 
323
 
@@ -326,17 +327,27 @@ def visualize_graph(graph: Bundle, *, color_nodes_by: ops.NodeAttribute = None):
326
  nodes = graph.dfs["nodes"].copy()
327
  if color_nodes_by:
328
  nodes["color"] = _map_color(nodes[color_nodes_by])
329
- if (
330
- "x" in nodes.columns
331
- and nodes["x"].dtype == "float64"
332
- and "y" in nodes.columns
333
- and nodes["y"].dtype == "float64"
334
- ):
335
- pos = {
336
- node_id: (row["x"] * 1000, row["y"] * 1000)
337
- for node_id, row in nodes.iterrows()
338
- }
339
- curveness = 0 # Street maps are better with straight streets.
 
 
 
 
 
 
 
 
 
 
340
  else:
341
  pos = nx.spring_layout(
342
  graph.to_nx(), iterations=max(1, int(10000 / len(nodes)))
 
201
  else:
202
  raise ValueError("No GraphML file found in the ZIP archive.")
203
  else:
204
+ G = nx.read_graphml(filename)
 
205
  return G
206
 
207
 
 
316
  colors = cmap.colors[: len(categories)]
317
  return [
318
  "#{:02x}{:02x}{:02x}".format(int(r * 255), int(g * 255), int(b * 255))
319
+ for r, g, b in [
320
+ colors[min(len(colors) - 1, categories.get_loc(v))] for v in value
321
+ ]
322
  ]
323
 
324
 
 
327
  nodes = graph.dfs["nodes"].copy()
328
  if color_nodes_by:
329
  nodes["color"] = _map_color(nodes[color_nodes_by])
330
+ for cols in ["x y", "long lat"]:
331
+ x, y = cols.split()
332
+ if (
333
+ x in nodes.columns
334
+ and nodes[x].dtype == "float64"
335
+ and y in nodes.columns
336
+ and nodes[y].dtype == "float64"
337
+ ):
338
+ cx, cy = nodes[x].mean(), nodes[y].mean()
339
+ dx, dy = nodes[x].std(), nodes[y].std()
340
+ # Scale up to avoid float precision issues and because eCharts omits short edges.
341
+ scale_x = 100 / max(dx, dy)
342
+ scale_y = scale_x
343
+ if y == "lat":
344
+ scale_y *= -1
345
+ pos = {
346
+ node_id: ((row[x] - cx) * scale_x, (row[y] - cy) * scale_y)
347
+ for node_id, row in nodes.iterrows()
348
+ }
349
+ curveness = 0 # Street maps are better with straight streets.
350
+ break
351
  else:
352
  pos = nx.spring_layout(
353
  graph.to_nx(), iterations=max(1, int(10000 / len(nodes)))