def test_negative_weights(self): """Negative edges whose absolute value is greater than the defined threshold should not be removed""" sm = StructureModel() strong_edges = [(1, 2, -3.0), (3, 1, 0.7), (1, 5, -2.0)] weak_edges = [(1, 4, 0.4), (2, 3, -0.6), (3, 5, -0.5)] sm.add_weighted_edges_from(strong_edges) sm.add_weighted_edges_from(weak_edges) sm.remove_edges_below_threshold(0.7) assert set(sm.edges(data="weight")) == set(strong_edges)
def test_remove_edges_below_threshold(self): """Edges whose weight is less than a defined threshold should be removed""" sm = StructureModel() strong_edges = [(1, 2, 1.0), (1, 3, 0.8), (1, 5, 2.0)] weak_edges = [(1, 4, 0.4), (2, 3, 0.6), (3, 5, 0.5)] sm.add_weighted_edges_from(strong_edges) sm.add_weighted_edges_from(weak_edges) sm.remove_edges_below_threshold(0.7) assert set(sm.edges(data="weight")) == set(strong_edges)
def test_equal_weights(self): """Edges whose absolute value is equal to the defined threshold should not be removed""" sm = StructureModel() strong_edges = [(1, 2, 1.0), (1, 5, 2.0)] equal_edges = [(1, 3, 0.6), (2, 3, 0.6)] weak_edges = [(1, 4, 0.4), (3, 5, 0.5)] sm.add_weighted_edges_from(strong_edges) sm.add_weighted_edges_from(equal_edges) sm.add_weighted_edges_from(weak_edges) sm.remove_edges_below_threshold(0.6) assert set(sm.edges(data="weight")) == set.union( set(strong_edges), set(equal_edges))
def plot_pretty_structure( g: StructureModel, edges_to_highlight: Tuple[str, str], default_weight: float = 0.2, weighted: bool = False, ): """ Utility function to plot our networks in a pretty format Args: g: Structure model (directed acyclic graph) edges_to_highlight: List of edges to highlight in the plots default_weight: Default edge weight weighted: Whether the graph is weighted Returns: a styled pygraphgiz graph that can be rendered as an image """ graph_attributes = { "splines": "spline", # I use splies so that we have no overlap "ordering": "out", "ratio": "fill", # This is necessary to control the size of the image "size": "16,9!", # Set the size of the final image. (this is a typical presentation size) "fontcolor": "#FFFFFFD9", "fontname": "Helvetica", "fontsize": 24, "labeljust": "c", "labelloc": "c", "pad": "1,1", "nodesep": 0.8, "ranksep": ".5 equally", } # Making all nodes hexagonal with black coloring node_attributes = { node: { "shape": "hexagon", "width": 2.2, "height": 2, "fillcolor": "#000000", "penwidth": "10", "color": "#4a90e2d9", "fontsize": 24, "labelloc": "c", "labeljust": "c", } for node in g.nodes } # Customising edges if weighted: edge_weights = [(u, v, w if w else default_weight) for u, v, w in g.edges(data="weight")] else: edge_weights = [(u, v, default_weight) for u, v in g.edges()] edge_attributes = { (u, v): { "penwidth": w * 20 + 2, # Setting edge thickness "weight": int(w), # Higher "weight"s mean shorter edges "arrowsize": 2 - 2.0 * w, # Avoid too large arrows "arrowtail": "dot", "color": "#DF5F00" if ((u, v) in set(edges_to_highlight)) else "#888888", } for u, v, w in edge_weights } return plot_structure( g, prog="dot", graph_attributes=graph_attributes, node_attributes=node_attributes, edge_attributes=edge_attributes, )