Skip to content
Snippets Groups Projects
MLBasicFunctions.py 2.46 KiB
Newer Older
##
# This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course.
# © Copyright Utrecht University (Department of Information and Computing Sciences)
##
import networkx as nx
##
#  buildGraph builds a NetworkX graph based on the incoming query data
#   data: Any, the incoming query data 
#   Return: Graph, a NetworkX graph
##
def buildGraph(incomingQueryData):
    graph = nx.Graph()
    # Add nodes from JSON
    for index,attributes in enumerate(incomingQueryData['value']['nodes']):
        # Get the node ID from the data like an overly nested dictionary    
        graph.add_nodes_from([(incomingQueryData['value']['nodes'][index]['id'],attributes)])    
    for index,attributes in enumerate(incomingQueryData['value']['edges']):
        fr = incomingQueryData['value']['edges'][index]['from']
        to = incomingQueryData['value']['edges'][index]['to']
##
# addNodeMetaData adds machine learning data to a node under a field called mldata for each node
#   queryData: dict, the incoming query data
#   metaData: any, the incoming machine learning results
#   Return: dict, the query result with the added machine learning data
##
def addNodeMetaData(incomingQueryData: dict, metaData):
    for index,attributes in enumerate(incomingQueryData['value']['nodes']):            
        currId = incomingQueryData['value']['nodes'][index]['id']
            incomingQueryData['value']['nodes'][index]['mldata'] = metaData[currId]
    return incomingQueryData
##
# addNewEdgeMetaData adds machine learning data to a completely new edge under a top-level field as a list of edge objects
#   queryData: dict, the incoming query data
#   metaData: any, the incoming machine learning results
#   Return: dict, the query result with the added machine learning data
##
def addNewEdgeMetaData(queryData: dict, metaData):
    queryData['value']["mlEdges"] = metaData
    return queryData

##
# optionalCheck checks wether an optional parameter is present in the request
#   params: dict, the parameters that were in the request propagated from the front-end
#   Return: dict, the parameters dict modified to be set to None when an optional parameter is left empty
##
def optionalCheck(params: dict):
    for attribute in params:
        if len(params[attribute]) == 0:
            params[attribute] = None
    return params