def handle(self, *args, **options): # 1) Get all the sounds that have remixes cursor = connection.cursor() cursor.execute(""" SELECT src.from_sound_id AS from, src.to_sound_id AS to, snd.created AS created FROM sounds_sound_sources src, sounds_sound snd WHERE src.to_sound_id = snd.id ORDER BY snd.created ASC """) # 2) Create directed graph dg = nx.DiGraph() for row in cursor: dg.add_edge(row[0], row[1]) # 3) Add date to nodes for sorting (FIXME: how can we avoid this query???) """ for node in dg.nodes(): cursor.execute("SELECT snd.created, snd.original_filename, au.username " \ "FROM sounds_sound snd, auth_user au WHERE au.id=snd.user_id AND snd.id = %s", [node]) temp = cursor.fetchone() dg.add_node(node, {'date':temp[0], 'nodeName': temp[1], 'username': temp[2]}) """ dg = _create_nodes(dg) # print dg.nodes(data=True) # 4) Find weakly connected components (single direction) subgraphs = nx.weakly_connected_component_subgraphs(dg) node_list = [] # 5) delete all remixgroup objects to recalculate RemixGroup.objects.all().delete() # 6) Loop through all connected graphs in the dataset # and create the groups for sg in subgraphs: # _create_and_save_remixgroup(sg_idx, sg) _create_and_save_remixgroup(sg, RemixGroup()) """
def handle(self, *args, **options): logger.info("Starting to create remix grooups") # 1) Get all the sounds that have remixes cursor = connection.cursor() cursor.execute( """ SELECT src.from_sound_id AS from, src.to_sound_id AS to, snd.created AS created FROM sounds_sound_sources src, sounds_sound snd WHERE src.to_sound_id = snd.id ORDER BY snd.created ASC """ ) # 2) Create directed graph dg = nx.DiGraph() for row in cursor: dg.add_edge(row[0], row[1]) # 3) Create nodes with dates and metadata dg = _create_nodes(dg) # 4) Find weakly connected components (single direction) subgraphs = nx.weakly_connected_component_subgraphs(dg) # 5) delete all remixgroup objects to recalculate RemixGroup.objects.all().delete() # 6) Loop through all connected graphs in the dataset and create the groups n_groups_created = 0 for sg in subgraphs: _create_and_save_remixgroup(sg, RemixGroup()) n_groups_created += 1 logger.info("Finished createing remix grooups (%i groups created)" % n_groups_created)
def handle(self, *args, **options): logger.info("Starting to create remix grooups") # 1) Get all the sounds that have remixes cursor = connection.cursor() cursor.execute(""" SELECT src.from_sound_id AS from, src.to_sound_id AS to, snd.created AS created FROM sounds_sound_sources src, sounds_sound snd WHERE src.to_sound_id = snd.id ORDER BY snd.created ASC """) # 2) Create directed graph dg = nx.DiGraph() for row in cursor: dg.add_edge(row[0], row[1]) # 3) Create nodes with dates and metadata dg = _create_nodes(dg) # 4) Find weakly connected components (single direction) subgraphs = nx.weakly_connected_component_subgraphs(dg) # 5) delete all remixgroup objects to recalculate RemixGroup.objects.all().delete() # 6) Loop through all connected graphs in the dataset and create the groups n_groups_created = 0 for sg in subgraphs: _create_and_save_remixgroup(sg, RemixGroup()) n_groups_created += 1 logger.info("Finished createing remix grooups (%i groups created)" % n_groups_created)
p_linkage_network = network_configuration_parameters["node"]["p"] # sys.exit() # create the random network where links rapresent truck link of the overall network graph = nx.erdos_renyi_graph(num_node_network, p_linkage_network, directed=True) output_folder = "data" # assign to the random network edges the cost and capacity value of the truck for s, d, data in graph.edges(data=True): # transp = random.choice(type_of_transport) data["capacity"] = type_of_transport["truck"]["capacity"] data["cost"] = type_of_transport["truck"]["cost"]["mean"] + random.randint(-type_of_transport["truck"]["cost"]["variance"], type_of_transport["truck"]["cost"]["variance"]) largest_component = max(nx.weakly_connected_component_subgraphs(graph), key=len) print len(largest_component) print len(graph.edges()) # diameter = nx.diameter(largest) nodes = largest_component.nodes() print len(nodes) # print diameter diameter = 0 # create train arcs inside the random network of truck for n1 in nodes: tmp = list(nodes) tmp.remove(n1) for n2 in tmp: try: shortest_path_len = nx.shortest_path_length(largest_component, source=n1, target=n2) if shortest_path_len > diameter: