def ensure_checkpoint_is_drawn(c): if c.name not in nodes: next_node_idx[0] += 1 nodes[c.name] = next_node_idx[0] if c.name.startswith(GENERATED_CHECKPOINT_PREFIX): # colors generated checkpoints based on their hash color = ColorHash(c.name[-GENERATED_HASH_LENGTH:]).hex G.add_node(next_node_idx[0], label=utils.cap_length(c.name), style="filled", fillcolor=color) else: G.add_node(next_node_idx[0], label=utils.cap_length(c.name))
def ensure_checkpoint_is_drawn(cp): if cp.name not in nodes: next_node_idx[0] += 1 nodes[cp.name] = next_node_idx[0] if cp.name.startswith(GENERATED_CHECKPOINT_PREFIX): # colors generated checkpoints based on their hash color = ColorHash(cp.name[-GENERATED_HASH_LENGTH:]).hex graph.add_node(next_node_idx[0], label=utils.cap_length(cp.name), style="filled", fillcolor=color) else: graph.add_node(next_node_idx[0], label=utils.cap_length(cp.name))
def visualize(self, output_file=None): import networkx as nx from rasa_core.training import visualization from colorhash import ColorHash graph = nx.MultiDiGraph() next_node_idx = [0] nodes = {"STORY_START": 0, "STORY_END": -1} def ensure_checkpoint_is_drawn(cp): if cp.name not in nodes: next_node_idx[0] += 1 nodes[cp.name] = next_node_idx[0] if cp.name.startswith(GENERATED_CHECKPOINT_PREFIX): # colors generated checkpoints based on their hash color = ColorHash(cp.name[-GENERATED_HASH_LENGTH:]).hex graph.add_node(next_node_idx[0], label=utils.cap_length(cp.name), style="filled", fillcolor=color) else: graph.add_node(next_node_idx[0], label=utils.cap_length(cp.name)) graph.add_node(nodes["STORY_START"], label="START", fillcolor="green", style="filled") graph.add_node(nodes["STORY_END"], label="END", fillcolor="red", style="filled") for step in self.story_steps: next_node_idx[0] += 1 step_idx = next_node_idx[0] graph.add_node(next_node_idx[0], label=utils.cap_length(step.block_name), style="filled", fillcolor="lightblue", shape="rect") for c in step.start_checkpoints: ensure_checkpoint_is_drawn(c) graph.add_edge(nodes[c.name], step_idx) for c in step.end_checkpoints: ensure_checkpoint_is_drawn(c) graph.add_edge(step_idx, nodes[c.name]) if not step.end_checkpoints: graph.add_edge(step_idx, nodes["STORY_END"]) if output_file: visualization.persist_graph(graph, output_file) return graph
def visualize(self, output_file=None): import networkx as nx from rasa_core.training import visualization from colorhash import ColorHash G = nx.MultiDiGraph() next_node_idx = [0] nodes = {"STORY_START": 0, "STORY_END": -1} def ensure_checkpoint_is_drawn(c): if c.name not in nodes: next_node_idx[0] += 1 nodes[c.name] = next_node_idx[0] if c.name.startswith(GENERATED_CHECKPOINT_PREFIX): # colors generated checkpoints based on their hash color = ColorHash(c.name[-GENERATED_HASH_LENGTH:]).hex G.add_node(next_node_idx[0], label=utils.cap_length(c.name), style="filled", fillcolor=color) else: G.add_node(next_node_idx[0], label=utils.cap_length(c.name)) G.add_node(nodes["STORY_START"], label="START", fillcolor="green", style="filled") G.add_node(nodes["STORY_END"], label="END", fillcolor="red", style="filled") for step in self.story_steps: next_node_idx[0] += 1 step_idx = next_node_idx[0] G.add_node(next_node_idx[0], label=utils.cap_length(step.block_name), style="filled", fillcolor="lightblue", shape="box") for c in step.start_checkpoints: ensure_checkpoint_is_drawn(c) G.add_edge(nodes[c.name], step_idx) for c in step.end_checkpoints: ensure_checkpoint_is_drawn(c) G.add_edge(step_idx, nodes[c.name]) if not step.end_checkpoints: G.add_edge(step_idx, nodes["STORY_END"]) if output_file: visualization.persist_graph(G, output_file) return G
def test_cap_length_with_short_string(): assert utils.cap_length("my", 3) == "my"
def test_cap_length_without_ellipsis(): assert utils.cap_length("mystring", 3, append_ellipsis=False) == "mys"
def test_cap_length(): assert utils.cap_length("mystring", 6) == "mys..."