Spaces:
Running
Running
File size: 1,279 Bytes
ca01fa3 9e91869 ca01fa3 a18645a ca01fa3 a18645a 76e9e8e 9e91869 76e9e8e 9e91869 a18645a |
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 |
'''Some operations. To be split into separate files when we have more.'''
from . import ops
import pandas as pd
import networkx as nx
@ops.op("Import Parquet")
def import_parquet(*, filename: str):
'''Imports a parquet file.'''
return pd.read_parquet(filename)
@ops.op("Create scale-free graph")
def create_scale_free_graph(*, nodes: int = 10):
'''Creates a scale-free graph with the given number of nodes.'''
return nx.scale_free_graph(nodes)
@ops.op("Compute PageRank")
def compute_pagerank(graph: nx.Graph, *, damping: 0.85, iterations: 3):
return nx.pagerank(graph)
@ops.op("Visualize graph")
def visualize_graph(graph: ops.Bundle) -> 'graph_view':
nodes = graph.dfs['nodes']['id'].tolist()
edges = graph.dfs['edges'].drop_duplicates(['source', 'target'])
edges = edges.to_dict(orient='records')
return {
'attributes': {},
'options': {},
'nodes': [{'key': id} for id in nodes],
'edges': [{'key': str(r['source']) + ' -> ' + str(r['target']), **r} for r in edges],
}
@ops.op("View table")
def view_table(dfs: ops.Bundle) -> 'table_view':
v = {
'dataframes': { name: {
'columns': [str(c) for c in df.columns],
'data': df.values.tolist(),
} for name, df in dfs.dfs.items() },
'edges': dfs.edges,
}
return v
|