def test_graphviz_usage_exception_dot_both(mocker, tmpdir, setup_graphviz_graph): exists_patch = mocker.patch("aizynthfinder.utils.image.os.path.exists") exists_patch.return_value = False molecules, reactions, edges, frame_colors = setup_graphviz_graph with pytest.raises(FileNotFoundError, match=".*'dot'.*"): image.make_graphviz_image(molecules, reactions, edges, frame_colors)
def test_graphviz_usage_exception_dot(mocker, tmpdir, setup_graphviz_graph): exists_patch = mocker.patch("aizynthfinder.utils.image.os.path.exists") exists_patch.side_effect = [False, True] molecules, reactions, edges, frame_colors = setup_graphviz_graph img = image.make_graphviz_image(molecules, reactions, edges, frame_colors) assert img.height > 0 assert img.width > 0
def test_graphviz_usage(mocker, tmpdir, setup_graphviz_graph): mkstemp_patch = mocker.patch("aizynthfinder.utils.image.tempfile.mkstemp") files = [ (None, str(tmpdir / "graph1.dot")), (None, str(tmpdir / "img2.png")), ] mkstemp_patch.side_effect = files molecules, reactions, edges, frame_colors = setup_graphviz_graph img = image.make_graphviz_image(molecules, reactions, edges, frame_colors) assert img.height > 0 assert img.width > 0 for _, filename in files: assert os.path.exists(filename)
def to_image( self, in_stock_colors: FrameColors = None, show_all: bool = True, ) -> PilImage: """ Return a pictorial representation of the route :raises ValueError: if image could not be produced :param in_stock_colors: the colors around molecules, defaults to {True: "green", False: "orange"} :param show_all: if True, also show nodes that are marked as hidden :return: the image of the route """ def show(node_): return not self.graph.nodes[node_].get("hide", False) in_stock_colors = in_stock_colors or {True: "green", False: "orange"} molecules = [] frame_colors = [] mols = sorted( self.molecules(), key=lambda mol: ( self.depth(mol), mol.weight, ), ) for node in mols: if not show_all and not show(node): continue molecules.append(node) frame_colors.append(in_stock_colors[self.in_stock(node)]) reactions = [ node for node in self.reactions() if show_all or show(node) ] edges = [(node1, node2) for node1, node2 in self.graph.edges() if show_all or (show(node1) and show(node2))] try: return make_graphviz_image(molecules, reactions, edges, frame_colors) except FileNotFoundError as err: raise ValueError(str(err))
def to_image(self, in_stock_colors={ True: "green", False: "orange" }, show_all=True): """ Return a pictoral representation of the route :raises ValueError: if image could not be produced :param in_stock_colors: the colors around molecules, defaults to {True: "green", False: "orange"} :type in_stock_colors: dict, optional :param show_all: if True, also show nodes that are marked as hidden :type show_all: bool, optional :return: the image of the route :rtype: PIL.Image """ def show(node): return not self.graph.nodes[node].get("hide", False) molecules = [] frame_colors = [] for node in self.molecules(): if not show_all and not show(node): continue molecules.append(node) frame_colors.append(in_stock_colors[self.in_stock(node)]) reactions = [ node for node in self.reactions() if show_all or show(node) ] edges = [(node1, node2) for node1, node2 in self.graph.edges() if show_all or (show(node1) and show(node2))] try: return make_graphviz_image(molecules, reactions, edges, frame_colors) except FileNotFoundError as err: raise ValueError(str(err))