Esempio n. 1
0
def add_next_dominators(list_of_next_dominators: list,
                        args: argparse.ArgumentParser):
    """
    Description: Add next dominators to connected dominating set

    Args:
        list_of_next_dominators (list): A list with next dominators

    Returns:
        -
    """
    import graph as g

    # Sort list of next dominators by how important are
    sorted_list_of_next_dominators = sorted(list_of_next_dominators.items(),
                                            key=operator.itemgetter(1))

    # Add next dominators until DS be connected
    for node in sorted_list_of_next_dominators:
        connected_dominating_set[node[0]] = node[1]
        g.add_node(node[0], False)
        add_dominator_to_all_nodes(node[0])
        remove_nodes_from_dominatees([node[0]])
        write_message(args, "[+] Add to DS node with name {}".format(node[0]),
                      "INFO")
        connectivity_list = check_connectivity(
            dict_of_objects[list(connected_dominating_set.keys())[0]], [],
            connected_dominating_set)
        if len(
                set(connectivity_list) & set(list(dict_of_objects.keys()))
        ) == len(connected_dominating_set) and all_dominees_have_dominators():
            break
Esempio n. 2
0
def draw_graph(counter_total, counter_lives, counter_deaths, n=20):
    """Draw graph of survival depending on sequence.

    Parameters:
    counter_total          - Counter object with total number of sequences
    counter_lives          - Counter object with number of sequences without death date
    counter_deaths         - Counter object with number of sequences with death date
    n                      - specifies how many of the sequences with highest death rate should be printed to output. If set to 0 then all sequences will be printed
    """

    print 'Creating graph'
    node_labels = {}
    edge_labels = {}
    graph = nx.DiGraph()
    graph.add_node(0)
    node_labels[0] = 'End'

    i = 1
    for k, v in counter_total.most_common(n) if n else counter_total.most_common():
        lastI = 0
        for medicine in reversed(k.split(';')):
            graph.add_edge(i, lastI)
            edge_labels[(i, lastI)] = medicine
            lastI = i
            i += 1
        node_labels[lastI] = '{0}%'.format(100 * counter_deaths[k] / v)

    print 'Graph created'
    layout = nx.graphviz_layout(graph)
    print 'Graph layout created'
    nx.draw(graph, pos = layout, hold = True, node_color = 'w', node_size = 1000)
    nx.draw_networkx_labels(graph, layout, labels = node_labels)
    nx.draw_networkx_edge_labels(graph, layout, edge_labels = edge_labels)
    plt.show()
Esempio n. 3
0
def amr_2_graph(sentence, graph):
	if  sentence[0] != '(' or sentence[-1] != ')':
		raise Exception("Error: Initial and closing character should be parenthesis.")
	sentence = sentence[1:-1]
	
	if re.search('([^:)(]*)([:)(].*)', sentence):
		root, sentence = re.search('([^:)(]*)([:)(].*)', sentence).group(1, 2)
	else:
		root = sentence
		sentence = ""
	graph.add_node(root)
	while sentence.find(':') >= 0:
		terminal = False
		argument, sentence = re.search('(:[^ ]+) ?(.*)', sentence).group(1, 2)
		child_node = None
		if sentence[0] == '(':
			subgraph, sentence = find_closing_parenthesis(sentence)
			child_node = amr_2_graph(subgraph, graph)
		else:
			
			child_node, sentence = re.search('([^ ]+) ?(:.*)?', sentence).group(1, 2)
			if not sentence: sentence = ""
			terminal = True
		graph.add_edge(root, child_node, argument, terminal=terminal)
	return root
Esempio n. 4
0
	def toDOT(self, label=''):
		"""
		Convert a Structure object to a graph image via DOT. This is useful
		for visualizing the functional groups that make up the databases. The
		output is a string containing a graph in DOT format, which can be
		passed to graphviz to produce an image; neato is recommended.

		Atoms are visualized as vertices in the outputted graph. Vertices are
		labeled with the atom type(s) of each corresponding	atom. Labeled atoms
		('\*', '\*1', etc.) are color-coded, with a unique color for each label.
		Bonds are indicated with edges; multiple bonds are represented by
		multiple edges between the same pair of vertices. The edge line style is
		used to denote further semantic information: dashed lines indicate
		optional higher-order bonds, while dotted lines indicate benzene bonds.
		"""

		import pydot

		graph = pydot.Dot(size='5,4', rankdir='LR',
				graph_type='graph', simplify=True, fontsize='8',
				overlap='true', dpi='85',center="True")

		# List of atoms (vertices)
		for i, atom in enumerate(self.atoms()):
			# Generate vertex label from atom type labels
			label = ','.join([atomType.label for atomType in atom._atomType])
			# Labeled atoms are color coded
			color = 'black'
			if atom.label != '':
				colors = {'*': 'red', '*1': 'red', '*2': 'green', '*3': 'blue', '*4': 'yellow', '*5': 'purple', '*6': 'orange', '*7': 'magenta', '*8': 'cyan'}
				color = colors[atom.label]
			# Create and add vertex to graph
			node = pydot.Node(str(i+1), label=label, color=color, fontcolor=color)
			graph.add_node(node)
			
		# List of bonds (edges)
		for i, bond in enumerate(self.bonds()):
			index1 = self.atoms().index(bond.atoms[0])
			index2 = self.atoms().index(bond.atoms[1])
			
			single = False; double = False; triple = False; benzene = False
			for type in bond._bondType:
				if type.order == 1: single = True
				if type.order == 2: double = True
				if type.order == 3: triple = True
				if type.order == 1.5: benzene = True

			label = []
			if single: label.append('S')
			if double: label.append('D')
			if triple: label.append('T')
			if benzene: label.append('B')
			label = ','.join(label)

			# Create and add edge to graph
			edge = pydot.Edge(str(index1+1), str(index2+1), label=label, len='2')
			graph.add_edge(edge)

		return graph
def test_depth_first():
    graph = Graph()
    A = graph.add_node('A')
    B = graph.add_node('B')
    C = graph.add_node('C')
    D = graph.add_node('D')
    E = graph.add_node('E')
    F = graph.add_node('F')
    G = graph.add_node('G')
    H = graph.add_node('H')
    graph.add_edge(A,B)
    graph.add_edge(A,D)
    graph.add_edge(D,E)
    graph.add_edge(D,H)
    graph.add_edge(D,F)
    graph.add_edge(B,C)
    graph.add_edge(C,G)
    output = graph.depth_first(A)
    assert output[0] == A
    assert output[1] == B
    assert output[2] == C
    assert output[3] == G
    assert output[4] == D
    assert output[5] == E
    assert output[6] == H
    assert output[7] == F
Esempio n. 6
0
def add_API_nodes(name):
    """ Queries the API for a list of related nodes from the current_node and
        their relationships to be added to the database.
        The process is multithreaded."""

    add_node(name)

    start_time = time.time()
    all_links = get_top_links(name)

    if all_links is None:
        return None

    # avoids stupid quotations breaking queries
    all_links = [
        link for link in all_links if not contains_quotes(link["title"])
    ]

    threads = []
    for link in all_links:
        #print link
        q = Queue()
        print link
        try:
            link["name"] = link.pop("title")
            link.pop("ns")
        except:
            pass

        if not node_exists(link):
            print 'node does not exist '
            add = lambda x: add_node(x)
            q.put(add)

        # otherwise always add relationship
        edge = lambda x, y, z: add_edge(x, y, z)
        q.put(edge)

        t = threading.Thread(target=process, args=(q, name, link))
        threads.append(t)
        t.start()

    [thread.join() for thread in threads]
    print "--- adding nodes took %s seconds ---" % (time.time() - start_time)
    return True
Esempio n. 7
0
def load_nodes(graph):
	with open(AUTHORS_FILE,'r') as f:		
		line = f.readline()
		while line != "":
			
			line = line[len("#index "):]		# remove index prefix from line
			a_id = line.strip()					# 1st line (stripped) is author's index
			line = f.readline()					# read next line
			line = line[len("#n "):]			# remove name prefix from line
			if line.strip() == "":				# make sure name isn't empty
				line = "<empty_" + a_id + ">"
			name = line.strip()					# 2nd line (stripped) is author's name

			auth = author.Author(int(a_id), name)	# create author and
			graph.add_node(auth)				# add it to the graph

			#if graph.number_of_nodes() % 100000 == 0:
			#	print graph.number_of_nodes()
			
			while line.strip() != "":			# ignore additional author details and read next line
				line = f.readline()
			line = f.readline()
Esempio n. 8
0
def load_nodes(graph):
    with open(AUTHORS_FILE, 'r') as f:
        line = f.readline()
        while line != "":

            line = line[len("#index "):]  # remove index prefix from line
            a_id = line.strip()  # 1st line (stripped) is author's index
            line = f.readline()  # read next line
            line = line[len("#n "):]  # remove name prefix from line
            if line.strip() == "":  # make sure name isn't empty
                line = "<empty_" + a_id + ">"
            name = line.strip()  # 2nd line (stripped) is author's name

            auth = author.Author(int(a_id), name)  # create author and
            graph.add_node(auth)  # add it to the graph

            #if graph.number_of_nodes() % 100000 == 0:
            #	print graph.number_of_nodes()

            while line.strip(
            ) != "":  # ignore additional author details and read next line
                line = f.readline()
            line = f.readline()
def test_breadth_first():
    graph = Graph()
    a = graph.add_node('a')
    b = graph.add_node('b')
    c = graph.add_node('c')
    d = graph.add_node('d')
    e = graph.add_node('e')
    f = graph.add_node('f')
    g = graph.add_node('g')
    h = graph.add_node('h')
    i = graph.add_node('i')
    k = graph.add_node('k')
    graph.add_edge(a, b)
    graph.add_edge(a, c)
    graph.add_edge(a,e)
    graph.add_edge(b, d)
    graph.add_edge(c, f)
    graph.add_edge(c, b)
    graph.add_edge(e,g)
    graph.add_edge(f,h)
    graph.add_edge(g,h)
    graph.add_edge(f,i)
    graph.add_edge(h,k)
    graph.add_edge(i,k)
    assert len(graph.breadth_first(a)) == 10
    breadth = graph.breadth_first(a)
    assert breadth[0].value == 'a'
    assert breadth[1].value == 'b'
    assert breadth[2].value == 'c'
    assert breadth[3].value == 'e'
    assert breadth[4].value == 'd'
    assert breadth[5].value == 'f'
    assert breadth[6].value == 'g'
    assert breadth[7].value == 'h'
    assert breadth[8].value == 'i'
    assert breadth[9].value == 'k'
def test_graph():
    # check for successful input
    graph = Graph()
    a = graph.add_node('a')
    assert a.value == 'a'
    b = graph.add_node('b')
    c = graph.add_node('c')
    d = graph.add_node('d')
    e = graph.add_node('e')
    f = graph.add_node('f')
    # check the get_nodes function
    assert graph.get_nodes()[0].value == 'a'
    assert graph.get_nodes()[1].value == 'b'
    assert graph.get_nodes()[5].value == 'f'
    # check if the add_edges and get_neighbors are working properly
    graph.add_edge(a, c)
    graph.add_edge(a, d)
    graph.add_edge(b, c)
    graph.add_edge(b, f)
    graph.add_edge(c, a)
    graph.add_edge(c, b)
    graph.add_edge(c, e)
    graph.add_edge(d, a)
    graph.add_edge(d, e)
    graph.add_edge(e, c)
    graph.add_edge(e, d)
    graph.add_edge(e, f)
    graph.add_edge(f, b)
    graph.add_edge(f, e)
    assert graph.get_neighbors(a)[0].node.value == 'c'
    assert graph.get_neighbors(a)[1].node.value == 'd'
    # testing the size method
    assert graph.size()==6
    # check for empty graph
    graph2 = Graph()
    assert graph2.get_nodes() == None
Esempio n. 11
0
def add_dominatee_to_CDS(args: argparse.ArgumentParser):
    """
    Description: Add dominatee to CDS 

    Args:
        args (obj): An object with all user input arguments

    Returns:
        - or ERROR_CODE
    """
    import graph as g

    sequense_size = 1
    next_node = 0
    nodes_list = []
    _add_dominatee = False
    while not _add_dominatee:

        try:
            nodes_list = [get_list_of_dominatees()[next_node][0]]
        except Exception as err:
            write_message(args, err, "ERROR")

            next_node = 0
            sequense_size += 1
        if sequense_size > 1:
            for i in range(int(args.k)):
                if next_node < len(get_list_of_dominatees()):
                    nodes_list = find_sequense_nodes(nodes_list,
                                                     get_list_of_dominatees())

        if next_node >= len(get_list_of_dominatees()) or sequense_size > int(
                args.k):
            next_node = 0
            sequense_size += 1

            if sequense_size > int(args.k):
                write_message(
                    args, "Input file with name {} cannot create {}-{} CDS",
                    "INFO")
                return

        dom_list = []
        for node in nodes_list:
            if len(dict_of_objects[node].get_dominators()) >= int(
                    args.k) - sequense_size + 1:
                dom_list.append(node)
                if len(dom_list) >= int(args.k):
                    break

        for node in dom_list:
            _add_dominatee = True
            connected_dominating_set[node] = 1
            add_dominator_to_all_nodes(node)
            remove_nodes_from_dominatees([node])
            g.add_node(node)

        next_node += 1

        if sequense_size > int(args.k):
            write_message(
                args, "This input network cannot create {}-{}-MCDS".format(
                    args.k, args.m), "INFO")
            return -1
Esempio n. 12
0
def find_MCDS(args: argparse.ArgumentParser):
    """
    Description: Minimize existing connected dominating set (CDS)

    Args:
        -
    Returns:
        -
    """
    import graph

    write_message(args,
                  "[!] CDS created. Need to minimize CDS and create MCDS",
                  "INFO")
    write_message(args, "[!] Start to minimize CDS", "INFO")

    counter = 0
    pbar = tqdm(total=len(connected_dominating_set), initial=counter)
    while counter < len(connected_dominating_set) and len(
            connected_dominating_set) > 1:
        # Remove dominator from DS to find out if it needs
        key = list(connected_dominating_set.items())[counter][0]
        value = connected_dominating_set.pop(key)
        write_message(args, "[!] Try to remove node with name {}".format(key),
                      "INFO")

        # Check if every dominatee has dominator
        all_nodes = []
        if int(args.algorithm) == 1 or int(args.algorithm) == 2:
            # for key1 in tqdm(connected_dominating_set):
            for key1 in connected_dominating_set:
                all_nodes = all_nodes + dict_of_objects[key1].get_N_of_u()
                if key1 not in all_nodes:
                    all_nodes.append(key1)
            if int(args.algorithm) == 2:
                # for node in tqdm(dict_of_objects):
                for node in dict_of_objects:
                    node_obj = dict_of_objects[node]
                    if key in node_obj.get_dominators():
                        node_obj.add_temp_dominator(key)
                        node_obj.get_dominators().remove(key)

            # Check if DS is connected without dominator
            connectivity_list = check_connectivity(
                dict_of_objects[list(connected_dominating_set.keys())[0]], [],
                connected_dominating_set)
            all_nodes = dict_of_objects[list(connected_dominating_set.keys())
                                        [0]].remove_duplicate(all_nodes)
            if not len(set(connectivity_list) & set(connected_dominating_set)
                       ) == len(connected_dominating_set) or not len(
                           set(all_nodes)
                           & set(dict_of_objects)) == len(dict_of_objects):
                connected_dominating_set[key] = value
                write_message(
                    args,
                    "[!] Node with name {} cannot be removed because CDS will disconnected"
                    .format(key), "INFO")
                counter += 1
                pbar.update(1)
            else:
                write_message(
                    args, "[-] Node with name {} removed from CDS".format(key),
                    "INFO")
                message = "New CDS with out node {} is [%s]" % ", ".join(
                    [a for a in connected_dominating_set])
                message = message.format(key)
                write_message(args, message, "DEBUG")

                if int(args.algorithm) == 2:
                    # for node in tqdm(dict_of_objects):
                    for node in dict_of_objects:
                        node_obj = dict_of_objects[node]
                        if key in node_obj.get_temp_dominators():
                            node_obj.get_dominators().append(key)
                            node_obj.delete_temp_dominator(key)
                        node_obj.clear_temp_dominators()
        else:
            import graph as g

            g.remove_node(key)
            write_message(
                args, "Try to remove dominator from neighbor for all nodes...",
                "INFO")
            # for node in tqdm(dict_of_objects):
            for node in dict_of_objects:
                node_obj = dict_of_objects[node]
                try:
                    node_obj.get_dominators().remove(key)
                    node_obj.add_temp_dominator(key)
                except Exception:
                    pass
            write_message(
                args,
                "Check if network is connected without node {}".format(key),
                "INFO")

            if all_dominators_have_k_dominators(int(
                    args.k)) and all_dominees_have_m_dominators(int(
                        args.m)) and g._is_k_connected(args):
                # for node in tqdm(dict_of_objects):
                for node in dict_of_objects:
                    node_obj = dict_of_objects[node]
                    node_obj.clear_temp_dominators()

                write_message(
                    args, "[-] Node with name {} removed from CDS".format(key),
                    "INFO")
            else:
                connected_dominating_set[key] = value
                # for node in tqdm(dict_of_objects):
                for node in dict_of_objects:
                    node_obj = dict_of_objects[node]
                    if key in node_obj.get_temp_dominators():
                        node_obj.get_dominators().append(key)
                        node_obj.delete_temp_dominator(key)
                    node_obj.clear_temp_dominators()
                g.add_node(key)
                add_dominator_to_all_nodes(key)
                write_message(
                    args,
                    "[!] Node with name {} cannot be removed because CDS will disconnected"
                    .format(key), "INFO")
                counter += 1
                pbar.update(1)
    pbar.close()
Esempio n. 13
0
import graph
from collections import namedtuple
import re, sys

infile = sys.argv[1]
infile = open(infile, "r")

graph = graph.Graph()

for i, line in enumerate(infile):
    if i == 1:
        s = line.split(', ')
    if i > 2:
        nodes = graph.get_nodes()
        words = line.split()
        if words[0] not in graph.get_nodes():
            graph.add_node(words[0])
        elif words[1] not in graph.get_nodes():
            graph.add_node(words[1])
        graph.add_edge(int(words[0]), int(words[1]), int(words[2]))
print(graph.get_nodes())
graph.dijkstra(1, 10)
Esempio n. 14
0
map = osmapi.OsmApi().Map(-3.9524, 38.9531 , -3.8877, 39.0086)

for map_dict in map:
    # Selecionamos las vías
    list_nodes = []
    if map_dict['type'] == 'way':
        if 'highway' in map_dict['data']['tag'] and map_dict['data']['tag']['highway'] in ['trunk', 'residential', 'pedestrian']:
            way_dict = osmapi.OsmApi().WayGet(map_dict['data']['id'])
            list_nodes.append(way_dict['nd'])

    for i, node in enumerate(list_nodes):
        if not graph.node_exist(node):
            if i == 0:
                node_aux = osmapi.OsmApi().NodeGet(node)
                node_dic = {'lat' : node_aux['lat'], 'lon' : node_aux['lon'], 'id' : node_aux['id'], 'edges' : []}
                graph.add_node(node_dic)

            else:
                node_aux = osmapi.OsmApi().NodeGet(node)
                node_dic = {'lat' : node_aux['lat'], 'lon' : node_aux['lon'], 'id' : node_aux['id'], 'edges': []}
                graph.add_node(node_dic)
                graph.add_edge(list_nodes[i], list_nodes[i-1], 0)
                graph.add_edge(list_nodes[i-1], list_nodes[i], 0)
        else:
            if i != 0:
                graph.add_edge(list_nodes[i], list_nodes[i-1], 0)
                graph.add_edge(list_nodes[i-1], list_nodes[i], 0)

    # Impresión de las aristas de los nodos.
    for i, node in enumerate(list_nodes):
        print("[nodo]",node,"->", graph.nodes[node]['edges'])
Esempio n. 15
0
        if 'highway' in map_dict['data']['tag'] and map_dict['data']['tag'][
                'highway'] in ['trunk', 'residential', 'pedestrian']:
            way_dict = osmapi.OsmApi().WayGet(map_dict['data']['id'])
            list_nodes.append(way_dict['nd'])

    for i, node in enumerate(list_nodes):
        if not graph.node_exist(node):
            if i == 0:
                node_aux = osmapi.OsmApi().NodeGet(node)
                node_dic = {
                    'lat': node_aux['lat'],
                    'lon': node_aux['lon'],
                    'id': node_aux['id'],
                    'edges': []
                }
                graph.add_node(node_dic)

            else:
                node_aux = osmapi.OsmApi().NodeGet(node)
                node_dic = {
                    'lat': node_aux['lat'],
                    'lon': node_aux['lon'],
                    'id': node_aux['id'],
                    'edges': []
                }
                graph.add_node(node_dic)
                graph.add_edge(list_nodes[i], list_nodes[i - 1], 0)
                graph.add_edge(list_nodes[i - 1], list_nodes[i], 0)
        else:
            if i != 0:
                graph.add_edge(list_nodes[i], list_nodes[i - 1], 0)