Newer
Older
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
import networkx as nx
# These are some basic functions that each machine learning implementation is bound to use
def buildGraph(data):
graph = nx.Graph()
# Add nodes from JSON
for index,attributes in enumerate(data['value']['nodes']):
# Get the node ID from the data like an overly nested dictionary
graph.add_nodes_from([(data['value']['nodes'][index]['id'],attributes)])
# Add edges from JSON
for index,attributes in enumerate(data['value']['edges']):
fr = data['value']['edges'][index]['from']
to = data['value']['edges'][index]['to']
graph.add_edge(fr,to)
return graph
# Adds machine learning data to a node
def addNodeMetaData(queryData: dict, metaData):
for index,attributes in enumerate(queryData['value']['nodes']):
currId = queryData['value']['nodes'][index]['id']
if currId in metaData:
queryData['value']['nodes'][index]['mldata'] = metaData[currId]
return queryData
# Adds machine learning new edge
def addNewEdgeMetaData(queryData: dict, metaData):
queryData['value']["mlEdges"] = metaData
return queryData
# If a variable is optional, it will be empty an empty string
# To ensure compatibility with NetworkX, those empty strings are turned into None
def optionalCheck(params: dict):
for attribute in params:
if len(params[attribute]) == 0:
params[attribute] = None
return params