Beispiel #1
0
def plot_results(scenario_id, attribute_name, username, password):
    """
        Get the details for a network
    """
    #Don't proceed if matplotlib is not installed
    if plt == None:
        return

    #Connect to hydra
    conn = hf.connect()
    
    #make a lookup of the ID of all attributes to their name so we can look it up
    all_attributes = conn.get_attributes()
    attribute_id_name_map = {}
    for a in all_attributes:
        attribute_id_name_map[a.id] = a.name
    
    datasets_to_plot = []

    try:
        #Get all the datasets matching the specified attribute
        scenario = conn.get_scenario(scenario_id=scenario_id)
        for rs in scenario.resourcescenarios:
            #Check the attribute map created above to see if it's the right attribute.
            if attribute_id_name_map[rs.resourceattr.attr_id].lower() == attribute_name.lower():
                datasets_to_plot.append(rs.dataset.value)
                
    except Exception as e:
        print("An error occurred retrieving scenario {0}".format( scenario_id ))
        return

    plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
    plt.show()
Beispiel #2
0
def add_user(network_id, recipient_username, hidden_attribute):
    """
        Get the details for a network
    """

    #Connect to hydra
    conn = hf.connect()
    
    #make a lookup of the ID of all attributes to their name so we can look it up
    all_attributes = conn.get_attributes()
    attribute_id_name_map = {}
    for a in all_attributes:
        attribute_id_name_map[a.id] = a.name

    try:
        network = conn.get_network(network_id=network_id, include_data='Y')

        print("Hiding sensitive datasets...")
        hidden_count = 0 # count how many datasets get hidden
        for rs in network.scenarios[0].resourcescenarios:
            if attribute_id_name_map[rs.attr_id].lower() == hidden_attribute.lower():
                # Hide the dataset. 
                conn.hide_dataset(dataset_id=rs.dataset.id) 
                hidden_count = hidden_count + 1
        print("{0} datasets hidden".format(hidden_count))
    except Exception as e:
        print("An error occurred retrieving network {0}".format( network_id ))
        return


    
    #Check if the user exists.
    conn.share_network(network_id=network_id, usernames=[recipient_username], read_only='N', share='N')
    print("Network shared successfully")
Beispiel #3
0
def create_project(name):
    """
        Add a new project.
    """

    #Connect to hydra
    conn = hf.connect()
    
    #Check if the project exists. A user can have access to multiple projects
    #with the same name, so this returns a list.
    projects_with_name = conn.get_project_by_name(project_name=name)

    if len(projects_with_name) == 0:
        project = {'name':name}
        #If the project doesn't exist, create it.
        new_project = conn.add_project(project)
    else:
        #return the first project with this name. THis could be improved
        #to somehow check for the correct project, but lets assume the list is 
        #always 1 in length.
        new_project = projects_with_name[0]

    print("Project {0} created with ID {1}".format(name, new_project.id))

    return new_project
Beispiel #4
0
def plot_results(scenario_id, attribute_name, username, password):
    """
        Get the details for a network
    """
    #Don't proceed if matplotlib is not installed
    if plt == None:
        return

    #Connect to hydra
    conn = hf.connect()

    #make a lookup of the ID of all attributes to their name so we can look it up
    all_attributes = conn.get_attributes()
    attribute_id_name_map = {}
    for a in all_attributes:
        attribute_id_name_map[a.id] = a.name

    datasets_to_plot = []

    try:
        #Get all the datasets matching the specified attribute
        scenario = conn.get_scenario(scenario_id=scenario_id)
        for rs in scenario.resourcescenarios:
            #Check the attribute map created above to see if it's the right attribute.
            if attribute_id_name_map[
                    rs.resourceattr.attr_id].lower() == attribute_name.lower():
                datasets_to_plot.append(rs.dataset.value)

    except Exception as e:
        print("An error occurred retrieving scenario {0}".format(scenario_id))
        return

    plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
    plt.show()
Beispiel #5
0
def plot_results(scenario_id, attribute_name, username, password):
    """
        Get the details for a network
    """
    #Don't proceed if matplotlib is not installed
    if plt == None:
        return

    #Connect to hydra
    conn = hf.connect(username, password)

    #make a lookup of the ID of all attributes to their name so we can look it up
    all_attributes = conn.get_attributes()
    attribute_id_name_map = {}
    for a in all_attributes:
        attribute_id_name_map[a.id] = a.name

    datasets_to_plot = {}

    try:
        #Get all the datasets matching the specified attribute
        scenario = conn.get_scenario(scenario_id=scenario_id)

        network = conn.get_network(network_id=scenario.network_id)

        node_ra_map = {}

        for node in network.nodes:
            for ra in node.attributes:
                node_ra_map[ra.id] = node.name

        for rs in scenario.resourcescenarios:
            #Check the attribute map created above to see if it's the right attribute.
            if attribute_id_name_map[
                    rs.resourceattr.attr_id].lower() == attribute_name.lower():
                if rs.dataset.value is None:
                    raise Exception(
                        "Unable to view value for dataset {0}".format(
                            rs.dataset.name))
                datasets_to_plot[node_ra_map.get(
                    rs.resourceattr.id, rs.resourceattr.id)] = rs.dataset.value

    except Exception as e:
        print("An error occurred retrieving scenario {0}. Reason: {1}".format(
            scenario_id, e))
        return

    df = {}
    for name, d in datasets_to_plot.items():
        df[name] = pd.read_json(d).iloc[:, 0]

    df = pd.concat(df, axis=1)
    df.plot()

    plt.show()
Beispiel #6
0
def add_user(username, password):
    """
        Get the details for a network
    """

    #Connect to hydra
    conn = hf.connect()

    #Check if the user exists.
    try:
        user = conn.get_user_by_name(uname=username)
        print("User {0} already exists with ID {1}".format(username, user.id))
    except:
        user = hb.JSONObject({'username': username, 'password': password})
        newuser = conn.add_user(user=user)
        print("User {0} added with ID {1}".format(username, newuser.id))
Beispiel #7
0
def add_user(username, password):
    """
        Get the details for a network
    """

    #Connect to hydra
    conn = hf.connect()
    
    #Check if the user exists.
    try:
        user = conn.get_user_by_name(uname=username)
        print("User {0} already exists with ID {1}".format(username, user.id))
    except:
        user = hb.JSONObject({'username':username, 'password':password})
        newuser = conn.add_user(user=user)
        print("User {0} added with ID {1}".format(username, newuser.id))
Beispiel #8
0
def get_network_details(network_id):
    """
        Get the details for a network
    """

    #Connect to hydra
    conn = hf.connect()

    #Check if the project exists. A user can have access to multiple projects
    #with the same name, so this returns a list.
    try:
        network = conn.get_network(network_id=network_id)
    except:
        print("An error occurred retrieving network %s", network_id)

    print("Name: {0}".format(network.name))
    print("ID  : {0}".format(network.id))
    print("Scenario ID: {0}".format(network.scenarios[0].id))
def add_user(network_id, recipient_username, hidden_attribute):
    """
        Get the details for a network
    """

    #Connect to hydra
    conn = hf.connect(username=None, password=None)

    #make a lookup of the ID of all attributes to their name so we can look it up
    all_attributes = conn.get_attributes()
    attribute_id_name_map = {}
    for a in all_attributes:
        attribute_id_name_map[a.id] = a.name

    try:
        network = conn.get_network(network_id=network_id, include_data='Y')

        print("Hiding sensitive datasets...")
        hidden_count = 0  # count how many datasets get hidden
        for rs in network.scenarios[0].resourcescenarios:
            if attribute_id_name_map[
                    rs.attr_id].lower() == hidden_attribute.lower():
                # Hide the dataset.
                conn.hide_dataset(rs.dataset.id, [],
                                  read='N',
                                  write='N',
                                  share='N')
                hidden_count = hidden_count + 1
        print("{0} datasets hidden".format(hidden_count))
    except Exception as e:
        logging.exception(e)
        print("An error occurred retrieving network {0}".format(network_id))
        return

    #Check if the user exists.
    conn.share_network(network_id=network_id,
                       usernames=[recipient_username],
                       read_only='N',
                       share='N')
    print("Network shared successfully")