Skip to content
Snippets Groups Projects
MLBasicFunctions.py 1.43 KiB
Newer Older
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