Spaces:
Running
Running
File size: 14,617 Bytes
2af956d 45e4c73 fc15efa f366c41 f141fec 5e29feb ac05fb9 f141fec 0b71528 d842e73 b6d30cb ca01fa3 b6d30cb f366c41 bf8f4f1 ca01fa3 97b3d79 2af956d 9497543 45e4c73 fc15efa a112474 fc15efa a112474 fc15efa a112474 fc15efa 75c875f ca01fa3 f366c41 45e4c73 ca01fa3 f366c41 97b3d79 a112474 f366c41 a112474 f366c41 5e29feb 97b3d79 89a8508 5e29feb 89a8508 5e29feb 97b3d79 c66f4fa ac05fb9 45e4c73 b6d30cb d842e73 f366c41 2af956d f366c41 a112474 f366c41 dcedc35 f366c41 0b71528 2af956d 0b71528 c51c9b4 0b71528 c51c9b4 0b71528 f366c41 75c875f 45e4c73 4c8d260 45e4c73 d4a220c b6d30cb de9a525 ffbad5c de9a525 a112474 de9a525 45e4c73 ca01fa3 8efcf30 0af1e7d 2af956d 0af1e7d 2af956d 45e4c73 4b66e0a c66f4fa a112474 c66f4fa 45e4c73 a112474 0af1e7d 45e4c73 0af1e7d 45e4c73 0af1e7d 45e4c73 c66f4fa 45e4c73 0af1e7d 45e4c73 0af1e7d 045796a a112474 45e4c73 0af1e7d a112474 0af1e7d 45e4c73 a18645a 8efcf30 2af956d bf8f4f1 c51c9b4 bf8f4f1 a112474 bf8f4f1 6934d0a f141fec 6934d0a 47e2956 f141fec 6934d0a 8f44c91 f141fec 47e2956 8f44c91 47e2956 f141fec 47e2956 f141fec 6934d0a f141fec 47e2956 6934d0a 8f44c91 f141fec 47e2956 f141fec 9fda8df 8f44c91 9fda8df 8f44c91 f141fec 6934d0a f141fec |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
"""Graph analytics operations."""
import enum
import os
import pathlib
import fsspec
from lynxkite.core import ops
from collections import deque
from tqdm import tqdm
from . import core, pytorch_model_ops
from lynxkite.core import workspace
import grandcypher
import joblib
import matplotlib
import networkx as nx
import pandas as pd
import polars as pl
import json
mem = joblib.Memory(".joblib-cache")
op = ops.op_registration(core.ENV)
class FileFormat(enum.StrEnum):
csv = "csv"
parquet = "parquet"
json = "json"
excel = "excel"
@op(
"Import file",
params={
"file_format": ops.ParameterGroup(
name="file_format",
selector=ops.Parameter(name="file_format", type=FileFormat, default=FileFormat.csv),
groups={
"csv": [
ops.Parameter.basic("columns", type=str, default="<from file>"),
ops.Parameter.basic("separator", type=str, default="<auto>"),
],
"parquet": [],
"json": [],
"excel": [ops.Parameter.basic("sheet_name", type=str, default="Sheet1")],
},
default=FileFormat.csv,
),
},
)
def import_file(
*, file_path: str, table_name: str, file_format: FileFormat, **kwargs
) -> core.Bundle:
"""Read the contents of the a file into a `Bundle`.
Args:
file_path: Path to the file to import.
table_name: Name to use for identifying the table in the bundle.
file_format: Format of the file. Has to be one of the values in the `FileFormat` enum.
Returns:
Bundle: Bundle with a single table with the contents of the file.
"""
if file_format == "csv":
names = kwargs.pop("columns", "<from file>")
names = pd.api.extensions.no_default if names == "<from file>" else names.split(",")
sep = kwargs.pop("separator", "<auto>")
sep = pd.api.extensions.no_default if sep == "<auto>" else sep
df = pd.read_csv(file_path, names=names, sep=sep, **kwargs)
elif file_format == "json":
df = pd.read_json(file_path, **kwargs)
elif file_format == "parquet":
df = pd.read_parquet(file_path, **kwargs)
elif file_format == "excel":
df = pd.read_excel(file_path, **kwargs)
else:
df = ValueError(f"Unsupported file format: {file_format}")
return core.Bundle(dfs={table_name: df})
@op("Import Parquet")
def import_parquet(*, filename: str):
"""Imports a Parquet file."""
return pd.read_parquet(filename)
@op("Import CSV")
@mem.cache
def import_csv(*, filename: str, columns: str = "<from file>", separator: str = "<auto>"):
"""Imports a CSV file."""
return pd.read_csv(
filename,
names=pd.api.extensions.no_default if columns == "<from file>" else columns.split(","),
sep=pd.api.extensions.no_default if separator == "<auto>" else separator,
)
@op("Import GraphML")
@mem.cache
def import_graphml(*, filename: str):
"""Imports a GraphML file."""
files = fsspec.open_files(filename, compression="infer")
for f in files:
if ".graphml" in f.path:
with f as f:
return nx.read_graphml(f)
raise ValueError(f"No .graphml file found at {filename}")
@op("Graph from OSM")
@mem.cache
def import_osm(*, location: str):
import osmnx as ox
return ox.graph.graph_from_place(location, network_type="drive")
@op("Discard loop edges")
def discard_loop_edges(graph: nx.Graph):
graph = graph.copy()
graph.remove_edges_from(nx.selfloop_edges(graph))
return graph
@op("Discard parallel edges")
def discard_parallel_edges(graph: nx.Graph):
return nx.DiGraph(graph)
@op("SQL")
def sql(bundle: core.Bundle, *, query: ops.LongStr, save_as: str = "result"):
"""Run a SQL query on the DataFrames in the bundle. Save the results as a new DataFrame."""
bundle = bundle.copy()
if os.environ.get("NX_CUGRAPH_AUTOCONFIG", "").strip().lower() == "true":
with pl.Config() as cfg:
cfg.set_verbose(True)
res = pl.SQLContext(bundle.dfs).execute(query).collect(engine="gpu").to_pandas()
# TODO: Currently `collect()` moves the data from cuDF to Polars. Then we convert it to Pandas,
# which (hopefully) puts it back into cuDF. Hopefully we will be able to keep it in cuDF.
else:
res = pl.SQLContext(bundle.dfs).execute(query).collect().to_pandas()
bundle.dfs[save_as] = res
return bundle
@op("Cypher")
def cypher(bundle: core.Bundle, *, query: ops.LongStr, save_as: str = "result"):
"""Run a Cypher query on the graph in the bundle. Save the results as a new DataFrame."""
bundle = bundle.copy()
graph = bundle.to_nx()
res = grandcypher.GrandCypher(graph).run(query)
bundle.dfs[save_as] = pd.DataFrame(res)
return bundle
@op("Organize")
def organize(bundle: list[core.Bundle], *, code: ops.LongStr) -> core.Bundle:
"""Lets you rename/copy/delete DataFrames, and modify relations.
TODO: Merge this with "Create graph".
"""
bundle = bundle.copy()
exec(code, globals(), {"bundle": bundle})
return bundle
@op("Sample graph")
def sample_graph(graph: nx.Graph, *, nodes: int = 100):
"""Takes a (preferably connected) subgraph."""
sample = set()
to_expand = deque([next(graph.nodes.keys().__iter__())])
while to_expand and len(sample) < nodes:
node = to_expand.pop()
for n in graph.neighbors(node):
if n not in sample:
sample.add(n)
to_expand.append(n)
if len(sample) == nodes:
break
return nx.Graph(graph.subgraph(sample))
def _map_color(value):
if pd.api.types.is_numeric_dtype(value):
cmap = matplotlib.cm.get_cmap("viridis")
value = (value - value.min()) / (value.max() - value.min())
rgba = cmap(value.values)
return [
"#{:02x}{:02x}{:02x}".format(int(r * 255), int(g * 255), int(b * 255))
for r, g, b in rgba[:, :3]
]
else:
cmap = matplotlib.cm.get_cmap("Paired")
categories = pd.Index(value.unique())
colors = cmap.colors[: len(categories)]
return [
"#{:02x}{:02x}{:02x}".format(int(r * 255), int(g * 255), int(b * 255))
for r, g, b in [colors[min(len(colors) - 1, categories.get_loc(v))] for v in value]
]
@op("Visualize graph", view="visualization")
def visualize_graph(
graph: core.Bundle,
*,
color_nodes_by: ops.NodeAttribute = None,
label_by: ops.NodeAttribute = None,
color_edges_by: ops.EdgeAttribute = None,
):
nodes = core.df_for_frontend(graph.dfs["nodes"], 10_000)
if color_nodes_by:
nodes["color"] = _map_color(nodes[color_nodes_by])
for cols in ["x y", "long lat"]:
x, y = cols.split()
if (
x in nodes.columns
and nodes[x].dtype == "float64"
and y in nodes.columns
and nodes[y].dtype == "float64"
):
cx, cy = nodes[x].mean(), nodes[y].mean()
dx, dy = nodes[x].std(), nodes[y].std()
# Scale up to avoid float precision issues and because eCharts omits short edges.
scale_x = 100 / max(dx, dy)
scale_y = scale_x
if y == "lat":
scale_y *= -1
pos = {
node_id: ((row[x] - cx) * scale_x, (row[y] - cy) * scale_y)
for node_id, row in nodes.iterrows()
}
curveness = 0 # Street maps are better with straight streets.
break
else:
pos = nx.spring_layout(graph.to_nx(), iterations=max(1, int(10000 / len(nodes))))
curveness = 0.3
nodes = nodes.to_records()
edges = core.df_for_frontend(graph.dfs["edges"].drop_duplicates(["source", "target"]), 10_000)
if color_edges_by:
edges["color"] = _map_color(edges[color_edges_by])
edges = edges.to_records()
v = {
"animationDuration": 500,
"animationEasingUpdate": "quinticInOut",
"tooltip": {"show": True},
"series": [
{
"type": "graph",
# Mouse zoom/panning is disabled for now. It interacts badly with ReactFlow.
# "roam": True,
"lineStyle": {
"color": "gray",
"curveness": curveness,
},
"emphasis": {
"focus": "adjacency",
"lineStyle": {
"width": 10,
},
},
"label": {"position": "top", "formatter": "{b}"},
"data": [
{
"id": str(n.id),
"x": float(pos[n.id][0]),
"y": float(pos[n.id][1]),
# Adjust node size to cover the same area no matter how many nodes there are.
"symbolSize": 50 / len(nodes) ** 0.5,
"itemStyle": {"color": n.color} if color_nodes_by else {},
"label": {"show": label_by is not None},
"name": str(getattr(n, label_by, "")) if label_by else None,
"value": str(getattr(n, color_nodes_by, "")) if color_nodes_by else None,
}
for n in nodes
],
"links": [
{
"source": str(r.source),
"target": str(r.target),
"lineStyle": {"color": r.color} if color_edges_by else {},
"value": str(getattr(r, color_edges_by, "")) if color_edges_by else None,
}
for r in edges
],
},
],
}
return v
@op("View tables", view="table_view")
def view_tables(bundle: core.Bundle, *, limit: int = 100):
return bundle.to_dict(limit=limit)
@op(
"Create graph",
view="graph_creation_view",
outputs=["output"],
)
def create_graph(bundle: core.Bundle, *, relations: str = None) -> core.Bundle:
"""Replace relations of the given bundle
relations is a stringified JSON, instead of a dict, because complex Yjs types (arrays, maps)
are not currently supported in the UI.
Args:
bundle: Bundle to modify
relations (str, optional): Set of relations to set for the bundle. The parameter
should be a JSON object where the keys are relation names and the values are
a dictionary representation of a `RelationDefinition`.
Defaults to None.
Returns:
Bundle: The input bundle with the new relations set.
"""
bundle = bundle.copy()
if not (relations is None or relations.strip() == ""):
bundle.relations = [core.RelationDefinition(**r) for r in json.loads(relations).values()]
return ops.Result(output=bundle, display=bundle.to_dict(limit=100))
def load_ws(model_workspace: str):
cwd = pathlib.Path()
path = cwd / model_workspace
assert path.is_relative_to(cwd)
assert path.exists(), f"Workspace {path} does not exist"
ws = workspace.load(path)
return ws
@op("Biomedical foundation graph (PLACEHOLDER)")
def biomedical_foundation_graph(*, filter_nodes: str):
"""Loads the gigantic Lynx-maintained knowledge graph. Includes drugs, diseases, genes, proteins, etc."""
return None
@op("Define model")
def define_model(
bundle: core.Bundle,
*,
model_workspace: str,
save_as: str = "model",
):
"""Trains the selected model on the selected dataset. Most training parameters are set in the model definition."""
assert model_workspace, "Model workspace is unset."
ws = load_ws(model_workspace)
# Build the model without inputs, to get its interface.
m = pytorch_model_ops.build_model(ws, {})
m.source_workspace = model_workspace
bundle = bundle.copy()
bundle.other[save_as] = m
return bundle
# These contain the same mapping, but they get different UIs.
# For inputs, you select existing columns. For outputs, you can create new columns.
class ModelInferenceInputMapping(pytorch_model_ops.ModelMapping):
pass
class ModelTrainingInputMapping(pytorch_model_ops.ModelMapping):
pass
class ModelOutputMapping(pytorch_model_ops.ModelMapping):
pass
@op("Train model")
def train_model(
bundle: core.Bundle,
*,
model_name: str = "model",
input_mapping: ModelTrainingInputMapping,
epochs: int = 1,
):
"""Trains the selected model on the selected dataset. Most training parameters are set in the model definition."""
m = bundle.other[model_name].copy()
inputs = pytorch_model_ops.to_tensors(bundle, input_mapping)
if not m.trained and m.source_workspace:
# Rebuild the model for the correct inputs.
ws = load_ws(m.source_workspace)
m = pytorch_model_ops.build_model(ws, inputs)
t = tqdm(range(epochs), desc="Training model")
for _ in t:
loss = m.train(inputs)
t.set_postfix({"loss": loss})
m.trained = True
bundle = bundle.copy()
bundle.other[model_name] = m
return bundle
@op("Model inference")
def model_inference(
bundle: core.Bundle,
*,
model_name: str = "model",
input_mapping: ModelInferenceInputMapping,
output_mapping: ModelOutputMapping,
):
"""Executes a trained model."""
if input_mapping is None or output_mapping is None:
return ops.Result(bundle, error="Mapping is unset.")
m = bundle.other[model_name]
assert m.trained, "The model is not trained."
inputs = pytorch_model_ops.to_tensors(bundle, input_mapping)
outputs = m.inference(inputs)
bundle = bundle.copy()
copied = set()
for k, v in output_mapping.map.items():
if not v.df or not v.column:
continue
if v.df not in copied:
bundle.dfs[v.df] = bundle.dfs[v.df].copy()
copied.add(v.df)
bundle.dfs[v.df][v.column] = outputs[k].detach().numpy().tolist()
return bundle
@op("Train/test split")
def train_test_split(bundle: core.Bundle, *, table_name: str, test_ratio: float = 0.1):
"""Splits a dataframe in the bundle into separate "_train" and "_test" dataframes."""
df = bundle.dfs[table_name]
test = df.sample(frac=test_ratio)
train = df.drop(test.index)
bundle = bundle.copy()
bundle.dfs[f"{table_name}_train"] = train
bundle.dfs[f"{table_name}_test"] = test
return bundle
|