def test_heat_data_is_updated_throughout_graph(self, graph_class): graph = nx.complete_graph(7, create_using=graph_class) temperature_graph = hx.temperature_graph(graph, source_nodes=[0]) for node in graph.nodes: assert temperature_graph.nodes[node]['heat'] == 1 for edge in graph.edges: assert temperature_graph.edges[edge]['heat'] == 1
def test_heat_from_multiple_sources_accumulates_additively(self): square_graph = nx.Graph([(0, 1), (0, 2), (1, 3), (2, 3)]) temperature_graph = hx.temperature_graph(square_graph, [1, 2]) for node in square_graph.nodes: assert temperature_graph.nodes[node]['heat'] == 2 for edge in square_graph.edges: assert temperature_graph.edges[edge]['heat'] == 2
def test_supports_common_graph_and_node_label_types( self, graph_class, node_labels): graph = graph_class(list(zip(node_labels[:-1], node_labels[1:]))) temperature_graph = hx.temperature_graph(graph, source_nodes=[node_labels[0]]) for node in graph.nodes: assert temperature_graph.nodes[node]['heat'] == 1 for edge in graph.edges: assert temperature_graph.edges[edge]['heat'] == 1
def test_heat_data_is_only_updated_in_connected_components_of_source_nodes( self, graph_class): graph: nx.Graph = graph_class([(0, 1), (1, 2), (2, 0), (3, 4), (4, 5), (5, 3)]) graph.add_nodes_from([6, 7, 8]) temperature_graph = hx.temperature_graph(graph, [0]) for node in range(3): assert temperature_graph.nodes[node]['heat'] == 1 for node in range(3, 9): assert temperature_graph.nodes[node]['heat'] == 0
def test_all_output_nodes_and_edges_store_heat_data(self): heat_key = 'heat goes here!' cyclic_graph = nx.cycle_graph(3) cyclic_temperature_graph = hx.temperature_graph(cyclic_graph, source_nodes=[], heat_key=heat_key) for node in cyclic_graph.nodes(): assert heat_key in cyclic_temperature_graph.nodes[node] for source, target in cyclic_temperature_graph.edges(): assert heat_key in cyclic_temperature_graph.edges[source, target]
def test_heat_distribution_respects_edge_directedness(self): square_graph = nx.DiGraph([(0, 1), (0, 2), (1, 3), (2, 3)]) temperature_graph = hx.temperature_graph(square_graph, [1, 2]) assert temperature_graph.nodes[0]['heat'] == 0 assert temperature_graph.edges[0, 1]['heat'] == 0 assert temperature_graph.edges[0, 2]['heat'] == 0 assert temperature_graph.nodes[1]['heat'] == 1 assert temperature_graph.nodes[2]['heat'] == 1 assert temperature_graph.edges[1, 3]['heat'] == 1 assert temperature_graph.edges[2, 3]['heat'] == 1 assert temperature_graph.nodes[3]['heat'] == 2
def test_accepts_iterable_of_depth_specific_heat_increments( self, graph_class): square_graph = graph_class([(0, 1), (0, 2), (1, 3), (2, 3)]) heat_source = 0 temperature_graph = hx.temperature_graph(square_graph, source_nodes=[heat_source], heat_increments=[1, 0.5]) heat_source_and_neighbors = {heat_source}.union( set(temperature_graph.neighbors(heat_source))) incident_edges = set(graph_edges(temperature_graph, heat_source)) for neighbor in heat_source_and_neighbors: assert temperature_graph.nodes[neighbor]['heat'] == 1 for edge in incident_edges: assert temperature_graph.edges[edge]['heat'] == 1 for node in set(temperature_graph.nodes) - heat_source_and_neighbors: assert temperature_graph.nodes[node]['heat'] == 0.5 for edge in set(graph_edges(temperature_graph)) - incident_edges: assert temperature_graph.edges[edge]['heat'] == 0.5
def test_can_limit_distance_heat_spreads_from_heat_sources( self, graph_class): square_graph = graph_class([(0, 1), (0, 2), (1, 3), (2, 3)]) heat_source = 0 temperature_graph = hx.temperature_graph(square_graph, source_nodes=[heat_source], heat_increments=[1, 0.5], depth_limit=1) heat_source_and_neighbors = {heat_source}.union( set(temperature_graph.neighbors(heat_source))) incident_edges = set(graph_edges(temperature_graph, heat_source)) for neighbor in heat_source_and_neighbors: assert temperature_graph.nodes[neighbor]['heat'] == 1 for edge in incident_edges: assert temperature_graph.edges[edge]['heat'] == 1 for node in set(temperature_graph.nodes) - heat_source_and_neighbors: assert temperature_graph.nodes[node]['heat'] == 0 for edge in set(graph_edges(temperature_graph)) - incident_edges: assert temperature_graph.edges[edge]['heat'] == 0
def test_given_a_graph_and_source_nodes_returns_a_new_graph(self): G = nx.Graph() G_temperatures = hx.temperature_graph(G, source_nodes=[]) assert isinstance(G_temperatures, nx.Graph) assert G_temperatures != G
def test_the_default_heat_key_is_heat(self): cyclic_graph = nx.cycle_graph(3) cyclic_temperature_graph = hx.temperature_graph(cyclic_graph, source_nodes=[]) assert 'heat' in cyclic_temperature_graph.nodes[0]
def test_output_has_same_graph_structure_as_input(self): G = nx.Graph() G_temperatures = hx.temperature_graph(G, source_nodes=[]) assert set(G_temperatures.nodes()) == set(G.nodes()) assert set(G_temperatures.edges()) == set(G.edges())
def test_output_type_matches_its_input(self, input_class): G = input_class() G_temperatures = hx.temperature_graph(G, source_nodes=[]) assert isinstance(G_temperatures, input_class)