def test_floyd_warshall_1(): paths = floyd_warshall( create_graph( ["1", "2", "3", "4", "6", "7"], [ ("1", "4", 1), ("2", "1", 1), ("2", "3", -1), ("2", "6", 2), ("2", "7", 5), ("3", "6", 1), ("6", "7", 0), ("7", "4", 2), ], ) ) expected = { "1": {"2": np.inf, "3": np.inf, "4": 1, "6": np.inf, "7": np.inf}, "2": {"1": 1, "3": -1, "4": 2, "6": 0, "7": 0}, "3": {"1": np.inf, "2": np.inf, "4": 3, "6": 1, "7": 1}, "4": {"1": np.inf, "2": np.inf, "3": np.inf, "6": np.inf, "7": np.inf}, "6": {"1": np.inf, "2": np.inf, "3": np.inf, "4": 2, "7": 0}, "7": {"1": np.inf, "2": np.inf, "3": np.inf, "4": 2, "6": np.inf}, } for i in paths: assert i in paths, f"{i} not in paths" for j in expected[i]: assert j in paths[i], f"{j} not in paths[{i}]" assert expected[i][j] == paths[i][j]
def new_graph(bot, update): """ Creates new graph """ graph = update.message.text.split()[1:] user_id = update.message.from_user.id update.message.reply_text(graphs.create_graph(user_id, graph))
def test_dijkstra_1(): paths = dijkstra( create_graph( ["A", "B", "C", "D", "E", "F", "G", "H"], [ ("A", "B", 4), ("A", "C", 8), ("A", "D", 1), ("B", "C", 3), ("C", "D", 9), ("C", "F", 5), ("C", "H", 4), ("D", "E", 2), ("E", "F", 3), ("F", "G", 2), ("G", "H", 3), ], directed=False, ), "A", ) expected = { "B": ("A", "B"), "C": ("A", "B", "C"), "D": ("A", "D"), "E": ("A", "D", "E"), "F": ("A", "D", "E", "F"), "G": ("A", "D", "E", "F", "G"), "H": ("A", "B", "C", "H"), } for v in expected: assert set(paths[v]) == set(expected[v])
def test_sssp_dag_1(): sssps = sssp_dag( create_graph( ["s", "a", "b", "c", "d", "e", "f", "t"], [ ("s", "a", 9), ("s", "b", 4), ("a", "c", 1), ("a", "d", -3), ("b", "c", 2), ("b", "d", 3), ("b", "e", 2), ("c", "e", 2), ("c", "f", -4), ("d", "e", 1), ("d", "f", 2), ("e", "t", 5), ("f", "t", 3), ], track_in=True, ), "s", ) expected = { "a": ("s", "a"), "b": ("s", "b"), "c": ("s", "b", "c"), "d": ("s", "a", "d"), "e": ("s", "b", "e"), "f": ("s", "b", "c", "f"), "t": ("s", "b", "c", "f", "t"), } for k in expected: assert set(sssps[k]) == set(expected[k])
def test_bellman_ford_2(): compare_bellman_ford( bellman_ford( create_graph( ["s", "a", "b", "c", "x", "w", "z", "t"], [ ("s", "a", -1), ("s", "w", 2), ("s", "x", 1), ("a", "b", 1), ("b", "c", 0), ("c", "t", 2), ("w", "z", 3), ("z", "t", -6), ("x", "t", 1), ("w", "a", -3), ("b", "w", -1), ], track_in=True, ), "s", ), None, ) print("All Bellman Ford Tests Passed!")
def test_bellman_ford_1(): compare_bellman_ford( bellman_ford( create_graph( ["s", "a", "b", "c", "x", "w", "z", "t"], [ ("s", "a", -1), ("s", "w", 2), ("s", "x", 1), ("a", "b", 1), ("b", "c", 0), ("c", "t", 2), ("w", "z", 3), ("z", "t", -6), ("x", "t", 1), ], track_in=True, ), "s", ), { "a": ["s", "a"], "b": ["s", "a", "b"], "c": ["s", "a", "b", "c"], "x": ["s", "x"], "w": ["s", "w"], "z": ["s", "w", "z"], "t": ["s", "w", "z", "t"], }, )
def test_kruskal_1(): kruskal_test( create_graph( ["A", "B", "C", "D", "E", "F", "G", "H"], [ ("A", "B", 4), ("A", "C", 6), ("A", "D", 1), ("B", "C", 8), ("C", "D", 5), ("C", "F", 2.5), ("C", "H", 7), ("D", "E", 2), ("E", "F", 3), ("F", "G", 7.5), ("G", "H", 9), ], directed=False, ), [ ("A", "B"), ("A", "D"), ("D", "E"), ("E", "F"), ("C", "F"), ("C", "H"), ("F", "G"), ], )
def start(bot, update): print('bot started') global global_graph bot.send_message(chat_id=update.message.chat_id, text="Hello! I'm GraphBot") # creates initial graph bot.send_message(chat_id=update.message.chat_id, text="Generating initial graph...") global_graph = graphs.create_graph(300, 100000) bot.send_message(chat_id=update.message.chat_id, text="Graph generated!") bot.send_message(chat_id=update.message.chat_id, text="You may send me your current location...")
def test_topsort_1(): assert_topsort( create_graph( ["A", "B", "C", "D", "E", "F", "G", "H"], [ ("A", "B"), ("A", "C"), ("H", "A"), ("H", "D"), ("D", "F"), ("E", "H"), ("E", "G"), ("G", "D"), ], ))
def derive_alien_language(sorted_words: List[str]) -> List[str]: """ Given a list of words, derive the order of the alphabet that would classify the list of words as "lexicographically" sorted. """ vertices = set() for word in sorted_words: vertices |= set(list(word)) edges = [] for w1, w2 in zip(sorted_words, sorted_words[1:]): for l1, l2 in zip(w1, w2): if l1 != l2: edges.append((l1, l2)) break G = create_graph(list(vertices), list(edges)) return topsort(G)
def graph(bot, update, args): print('received graph') global global_graph try: if len(args) == 2: distance = float(args[0]) population = float(args[1]) bot.send_message(chat_id=update.message.chat_id, text="Generating graph...") global_graph = graphs.create_graph(distance, population) bot.send_message(chat_id=update.message.chat_id, text="Graph generated!") else: bot.send_message( chat_id=update.message.chat_id, text= 'Remember: the command format is /graph <distance> <population>' ) except Exception as e: print(e) bot.send_message( chat_id=update.message.chat_id, text='An error was encountered while creating the graph')
def test_kosaraju_1(): G = create_graph( ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"], [ ("L", "I"), ("I", "J"), ("J", "K"), ("K", "L"), ("J", "A"), ("A", "D"), ("D", "H"), ("H", "A"), ("D", "E"), ("H", "B"), ("E", "F"), ("F", "G"), ("G", "E"), ("B", "C"), ("C", "B"), ], ) expected = [["A", "D", "H"], ["B", "C"], ["E", "F", "G"], ["I", "J", "K", "L"]] result = kosaraju(G) for scc in result: scc.sort() for scc in expected: scc.sort() result.sort(key=lambda x: x[0]) expected.sort(key=lambda x: x[0]) for r, s in zip(result, expected): assert set(r) == set(s)
def program_run(): """ Executes the program in a text-driven manner from the console allowing user selection to drive data analysis. """ intro_menu() survey_csv = 'survey_results_public_modified.csv' proceed = input() continue_running = True while continue_running: selection_menu() print('Accumulating data for graphs and interpreting it may take up ' 'to 10 seconds. Please be patient!') selection = input('Selection: ') if selection == '1': graph.create_graph('exercise') print('Based on these findings, we found that the top three ' ' genders that exercise at least once a week are: ' + analysis.top_exercisers_by_gender( analysis.exercise_by_gender(survey_csv)) + 'based on the csv data.\n') elif selection == '2': graph.create_graph('salary') print('Based on these findings, we found that the top three ' 'genders with the highest salaries are: ' + analysis.top_earners_by_gender( analysis.median_salary_by_gender(survey_csv)) + 'based on the csv data.\n') elif selection == '3': graph.create_graph('job satisfaction') print('Based on these findings, we found that the top three ' 'genders that are at least slightly satisfied with their ' 'jobs are: ' + analysis.happiest_genders_by_job( analysis.job_satisfaction_by_gender(survey_csv)) + 'based on the csv data.\n') else: proceed = input('Sorry, that is not a valid selection. ' 'Press any key to return to the selection screen.') back_to_start = input('Would you like to see another graph? Please ' 'answer \'yes\' or \'no\': ') if back_to_start == 'no': print('Thank you for using this program.') break
graphs.bfs(node_1, node_2) print("Checking for path from node_2 to node_1") graphs.bfs(node_2, node_1) num_vertices = 7 src_node = 0 target_node = 4 # Used for visualizing the graph labels = [] for index in range(num_vertices): labels.append(str(index)) # Create a random adjacency matrix for graph connectivity. adj_matrix = np.random.randint(0, 2, (num_vertices, num_vertices)) # Just keeping things a bit more interesting by preventing direct edges. adj_matrix[src_node][target_node] = 0 print(adj_matrix) # Create graph using ``adj_matrix`` and return a list # of nodes in the graph for easy access to each node. nodes = graphs.create_graph(adj_matrix, num_vertices) # Find route between the nodes find_route(nodes[src_node], nodes[target_node]) # Visualize graph for confirmation. g = igraph.Graph.Adjacency((adj_matrix > 0).tolist()) g.vs['label'] = labels igraph.plot(g, labels=True)
data.limit_samples(n=limit, verbose=verbose) verbose_print("Performing center crops", verbose=verbose, end="") data.do_center_crops() verbose_print_done(verbose) # ============================================================================== # GRAPH AND SESSION # ============================================================================== model = model_a checkpoint_dir = "results/A_02/checkpoints/" checkpoint_file = os.path.join(checkpoint_dir, "checkpoint_max.chk") # CREATE TENSORFLOW GRAPH graph = create_graph(logit_func=model, settings=settings) # PREPARE SESSION print_headers("TENSORFLOW SESSION", border="=", width=PRINT_WIDTH) with tf.Session(graph=graph) as sess: # GET IMPORTANT OPERATIONS AND TENSORS FROM GRAPH g = GraphOps(graph, "X", "Y", "BBOX", "is_training", "digit_logits", "bbox_logits") # INITIALIZE VARIABLES saver = tf.train.Saver(name="saver") tf_initialize_vars_from_file(f=checkpoint_file, s=sess, saver=saver, verbose=verbose)
# LIMIT TRAIN DATA - eg during development and debugging limit = opts.data_size data.train.limit_samples(n=limit, verbose=verbose) # PORTION OF THE TRAINING DATA USED FOR EVALUATION data.set_train_eval_data(n=1024, random=False, random_transforms=True, batchsize=128, verbose=verbose) # CREATE TENSORFLOW GRAPH print("USING MODEL: ", opts.model) models = {"a": model_a, "b": model_b, "c": model_c, "d": model_d} graph = create_graph(logit_func=models[opts.model], settings=opts) # RUN TENSORFLOW TRAINING SESSION run_session(graph=graph, data=data, paths=paths, alpha=opts.alpha, epochs=opts.epochs) # SAVE SETTINGS TO TEXT AND PICKLE FILES save_dict_as_text_file(d=vars(opts), f=paths.settings_text_file) obj2pickle(opts, paths.settings_pickle_file, verbose=verbose) # CREATE A COMPARISONS FILE comparisons_file(opts, paths)
async def graphcr(m): max_graph = 1.0 min_graph = 0.0 reverse = True graph = None def downloadd(lambdafunc): try: return lambdafunc except: return -1 try: tags = dbase.get_tags() if '--char' in m.get_full_command()[1].split(): tagst = [] for tag in tags: if tag[1] in dbr.character_tags: tagst.append(tag) tags = tagst if '--reverse' in m.get_full_command()[1].split(): reverse = False tags.sort(key=lambda x: x[3], reverse=reverse) values = [] backgrounds = dbr.posts(tags=tags[random.randint(0, 9)][1] + ' -animated', limit=100, require_file_url=1) stime = time.time() while True: try: if time.time() - stime > 20: bgpath = None break background = backgrounds[random.randint( 0, len(backgrounds) - 1)] bg = await asyncio.get_event_loop().run_in_executor( None, downloadd(lambda: background.file_url.download( '.tmp{}'.format(background.md5)))) if bg == -1: raise 'A' bgpath = '.tmp{}'.format(background.md5) break except: bgpath = None if tags[0][3] < 0.9: max_graph = round(tags[0][3] * 1.1, 2) for tag in tags[:10]: print(tag) stime = time.time() images = dbr.posts(tags=tag[1] + ' -animated', limit=100, require_file_url=1) while True: try: if time.time() - stime > 20: imgpath = None break image = images[random.randint(0, len(images) - 1)] img = asyncio.get_event_loop().run_in_executor( None, downloadd(lambda: image.file_url.download( '.tmp{}'.format(image.md5)))) if img == -1: raise 'A' imgpath = '.tmp{}'.format(image.md5) break except: imgpath = None values.append((tag[3], tag[1], imgpath)) graph = graphs.create_graph(min_graph, max_graph, values, background=bgpath) await m.answer_photo(types.InputFile(graph)) for x in values: os.remove(x[2]) os.remove('.tmp{}'.format(background.md5)) except Exception as E: await m.answer(E) os.system('rm .tmp*') if graph: with open('graph.err', 'wb') as fwr: graph.seek(0) fwr.write(graph.read())
timestamp = datetime.datetime.utcnow().isoformat() if DO_TASKS['save_descriptor_paths']: descriptor_filename = 'runs/descriptor_paths_' + timestamp + '.txt' with open(descriptor_filename, 'wb') as f1: f1.write(pprint.pformat(descriptor_paths, indent=1, width=80, depth=None)) f1.close() if DO_TASKS['save_matches']: matches_filename = 'runs/matches_data_' + timestamp + '.txt' with open(matches_filename, 'wb') as f2: f2.write(pprint.pformat(sorted_weights, indent=1, width=80, depth=None)) f2.close() if DO_TASKS['create_graphs']: create_graph(sorted_weights) graph_matches(sorted_weights) if DO_TASKS['save_to_db']: save_weights_to_db(sorted_weights, session) timekeeper.time_now('Final', True) else: # Worker processes execute code below s_print("I am a worker with rank {} on {}.".format(rank, name)) while True: comm.send(None, dest=0, tag=tags['READY']) task = comm.recv(source=0, tag=MPI.ANY_TAG, status=status) tag = status.Get_tag() if tag == tags['WAIT']:
file_functions.write(round(int(temperature[-1]), 2), "../data/Current/temperature.csv") file_functions.write(round(int(air_qual[-1]), 2), "../data/Current/air_qual.csv") file_functions.write(round(int(pressure[-1]), 2), "../data/Current/pressure.csv") file_functions.write(round(int(humidity[-1]), 2), "../data/Current/humidity.csv") file_functions.write(round(int(wind_speed[-1]), 2), "../data/Current/wind_speed.csv") file_functions.write(round(int(rain[-1]), 2), "../data/Current/rain.csv") file_functions.write(round(int(wind_direction[-1]), 2), "../data/Current/wind_direction.csv") file_functions.write(temperature, "../data/" + date() + "/csv/temperature.csv") file_functions.write(air_qual, "../data/" + date() + "/csv/air_qual.csv") file_functions.write(pressure, "../data/" + date() + "/csv/pressure.csv") file_functions.write(humidity, "../data/" + date() + "/csv/humidity.csv") file_functions.write(wind_speed, "../data/" + date() + "/csv/wind_speed.csv") file_functions.write(rain, "../data/" + date() + "/csv/rain.csv") file_functions.write(wind_direction, "../data/" + date() + "/csv/wind_direction.csv") file_functions.write(times, "../data/" + date() + "/csv/time.csv") graphs.create_graph(times, temperature, "Temperature on " + date(), "Time", "Temperature", "../data/" + date() + "/graphs/temperature.png") graphs.create_graph(times, air_qual, "Air Quality on " + date(), "Time", "Air Quality", "../data/" + date() + "/graphs/air_qual.png") graphs.create_graph(times, pressure, "Pressure on " + date(), "Time", "Pressure", "../data/" + date() + "/graphs/pressure.png") graphs.create_graph(times, humidity, "Humidity on " + date(), "Time", "Humidity", "../data/" + date() + "/graphs/humidity.png") graphs.create_graph(times, rain, "Rain on " + date(), "Time", "Rain", "../data/" + date() + "/graphs/rain.png") graphs.create_graph(times, wind_speed, "Wind Speed on " + date(), "Time", "Wind Speed", "../data/" + date() + "/graphs/wind_speed.png") graphs.create_graph(times, wind_direction, "Wind Direction on " + date(), "Time", "Wind Direction", "../data/" + date() + "/graphs/wind_direction.png") os.system("sudo cp -r ../data/" + date() + " /var/www/html/data/") os.system("sudo cp -r ../data/Current" + " /var/www/html/data/") print(time.strftime("%H:%M:%S : Done")) time.sleep(1800)
def test_DFS_1(): G = create_graph(["A", "B", "C"], [("A", "B"), ("B", "C")]) assert DFS(G, "A", "C") assert not DFS(G, "C", "A")