m_strat.api.dag.graph_manager

  1import json
  2import os
  3from collections import defaultdict, deque
  4from dataclasses import asdict
  5
  6from api.project_management.wrappers import Serializable
  7
  8from .node import ExecutionNode
  9
 10
 11class GraphManager(Serializable):
 12    """Manages a single DAG which is used to build a ML pipeline from Blueprint Nodes"""
 13
 14    def __init__(
 15        self,
 16        name: str,
 17        load_path: str = None,
 18        load: bool = False,
 19        dir: str = "exec_graphs",
 20    ):
 21        """Instansiates a Graphmanager object used to manage a DAG
 22        Args:
 23            name (str): The designated label of this DAG
 24            load_path (str, optional): Path to json config . Defaults to None.
 25            load (bool, optional): If true attemps to load the graph by name or load path. Defaults to False.
 26            dir (str, optional): Path to where this graph is stored. Defaults to "exec_graphs".
 27
 28        Raises:
 29            ValueError: if no name or load_path is supplied and load is true as no data can be loaded.
 30        """
 31        self.nodes = {"start": ExecutionNode("start", "", "")}
 32        self.edges = defaultdict(list)
 33        self.name = name
 34        self.dir = dir
 35        if load and (name or load_path):
 36            self.load(name=name, load_path=load_path)
 37        elif load:
 38            raise ValueError("GraphManager cannot load without a valid graph name")
 39
 40    @staticmethod
 41    def get_file_path(name: str, dir: str):
 42        """From the name and directory, returns what the graph data file should be"""
 43        return os.path.join(dir, f"gm_{name}.json")
 44
 45    @staticmethod
 46    def get_name_from_filename(path: str):
 47        """if given a standardised graph data file, extracts the name of the graph"""
 48        base_name: str = os.path.splitext(os.path.basename(path))[0]
 49        return base_name[3:]
 50
 51    def create_add_new_node(self, node_id: str, blueprint_name: str, param_id: str):
 52        """From data supplied, creates an ExecutionNode and adds it to the graph
 53        Args:
 54            node_id (str): human readable name of node
 55            blueprint_name (str): name of the blueprint in blueprint Register
 56            param_id (str): name of node params from paramater manager
 57        """
 58        new_node = ExecutionNode(
 59            id=node_id, blueprint_name=blueprint_name, param_id=param_id
 60        )
 61        self.add_node(new_node)
 62
 63    def add_node(self, new_node: ExecutionNode):
 64        """Adds a node to the graph"""
 65        assert self.nodes.get(new_node.id, None) is None, "Node Already exists"
 66        self.nodes[new_node.id] = new_node
 67
 68    def change_node_param_template(self, node_id: str, new_param_id: str):
 69        """Changes a node parameter template (i.e. the "params" parsed into the ExecutionNode at runtime)
 70        Args:
 71            node_id (str): instance name of the node within the DAG
 72            new_param_id (str): the new parameter id for the node to use
 73        """
 74        assert isinstance(new_param_id, str) and new_param_id, (
 75            "New Parameter id Invalid"
 76        )
 77        node = self.nodes.get(node_id, None)
 78        assert node is not None, "Node not found"
 79        node.param_id = new_param_id
 80
 81    def remove_node(self, node_id: str):
 82        """
 83            Removes a node from the DAG (and its edges)
 84        Args:
 85            node_id(str): the target node to be removed (DAG name not blueprint name!)
 86        """
 87        assert node_id != "start", (
 88            "Cannot Remove or Alter Start Node, alter it's edges, or parse different start point to build_execution instead"
 89        )
 90        self.edges.pop(node_id, [])
 91        self.nodes.pop(node_id)
 92        for e_v_lst in self.edges.values():
 93            if node_id in e_v_lst:
 94                e_v_lst.remove(node_id)
 95
 96    def add_edge(self, node_from: str, node_to: str):
 97        """Connects 2 nodes together, representing execution order
 98        Args:
 99            node_from (str): Node from which the connection is to be made
100            node_to (str): Node the connection ends at.
101        """
102        assert self.nodes.get(node_from, None) is not None, (
103            f"Add Edge From Node {node_from} not present in manager"
104        )
105        assert self.nodes.get(node_to, None) is not None, (
106            f"Add Edge To Node {node_to} not present in manager"
107        )
108        neighbours = self.edges[node_from]
109        if node_to not in neighbours:
110            neighbours.append(node_to)
111
112    def remove_edge(self, node_from: str, node_to: str, allow_inverse: bool = False):
113        """Removes 2 connecting edges of a node, allow inverse checks with the nodes in the opposite direction"""
114        self.edges[node_from].remove(node_to)
115        if allow_inverse:
116            inverse_edge = self.edges.get(node_to, None)
117            if not inverse_edge:
118                return
119            if node_from in inverse_edge:
120                inverse_edge.remove(node_from)
121
122    def get_edges(self):
123        """Gets all edges as a set of  tuples  (of -> from, to) and returns them
124        Returns:
125            set: tuple of (node_from, node_to)
126        """
127        edge_tuples = set()
128        for node_from, neighbours in self.edges.items():
129            for node_to in neighbours:
130                edge_tuples.add((node_from, node_to))
131        return edge_tuples
132
133    def get_nodes(self):
134        """Returns a shallow copy of all nodes"""
135        return dict(self.nodes)
136
137    def get_node(self, node_id: str):
138        """Gets a node via node_id from this dag, returns None if not present"""
139        return self.nodes.get(node_id, None)
140
141    def build_execution(self, start_node_id="start"):
142        """Generates a linear execution of nodes from star_node_id until there are ends
143        Args:
144            start_node_id (str, optional): String of first node_id. Defaults to "start".
145        Returns:
146            list[ExecutionNode]: returns the order of nodes by execution from start->finish
147        NB: Currently does not detect loops, parallelisation is flattened currently.
148        """
149        in_degree = {node_id: 0 for node_id in self.nodes}
150
151        for _, neighbors in self.edges.items():
152            for n in neighbors:
153                in_degree[n] += 1
154
155        queue = deque([start_node_id])
156        exec_order = []
157
158        while queue:
159            current = queue.popleft()
160
161            exec_order.append(self.nodes.get(current))
162
163            for neighbor in self.edges.get(current, []):
164                in_degree[neighbor] -= 1
165                if in_degree[neighbor] == 0:
166                    queue.append(neighbor)
167
168        return exec_order
169
170    def load(self, name: str = None, load_path: str = None):
171        """using the load path primarily or the name if not supplied. load selection order: load_path->name->self.name
172        Args:
173            name (str, optional): Name of graph manager. Defaults to None.
174            load_path (str, optional): Path to graph configuration file. Priortised. Defaults to None.
175        """
176        assert load_path is not None or name is not None or self.name is not None, (
177            "At least one identifier required to find the graph manager"
178        )
179        if load_path is None:
180            load_path = self.get_file_path(name, self.dir)
181
182        with open(load_path, "r", encoding="utf-8") as load_file:
183            load_data = json.load(load_file)
184            self.edges = defaultdict(list, load_data["edges"])
185            self.name = load_data["name"] if name is None else name
186            node_data = {}
187            for node_id, exec_dict in load_data["nodes"].items():
188                node_data[node_id] = ExecutionNode(
189                    id=node_id,
190                    blueprint_name=exec_dict["blueprint_name"],
191                    param_id=exec_dict["param_id"],
192                    pos=exec_dict.get("pos", None),
193                )
194            self.nodes = node_data
195
196    def save(self):
197        """using the name of the current graph, generates a name and saves the configuration as json"""
198        save_path = self.get_file_path(self.name, self.dir)
199        save_data = {"edges": self.edges, "name": self.name}
200        save_data["nodes"] = {
201            node_id: asdict(exec_node) for node_id, exec_node in self.nodes.items()
202        }
203        with open(save_path, "w", encoding="utf-8") as save_file:
204            json.dump(save_data, save_file)
205
206    def duplicate(self, new_name=""):
207        """Duplicates the current graph manager into another"""
208        if not new_name:
209            new_name = f"{self.name}_copy"
210        dupe = GraphManager(name=new_name, load=False, dir=self.dir)
211        for n in self.nodes.values():
212            if n.id == "start":
213                if n.pos:
214                    dupe.nodes["start"].pos = list(n.pos)
215                continue
216            n_copy = ExecutionNode(
217                id=n.id,
218                blueprint_name=n.blueprint_name,
219                param_id=n.param_id,
220                pos=list(n.pos),
221            )
222            dupe.add_node(n_copy)
223        for node_name, neighbours in self.edges.items():
224            for neigh in neighbours:
225                dupe.add_edge(node_name, neigh)
226        return dupe
227
228    @classmethod
229    def file_type(cls):
230        return "json"
class GraphManager(api.project_management.wrappers.Serializable):
 12class GraphManager(Serializable):
 13    """Manages a single DAG which is used to build a ML pipeline from Blueprint Nodes"""
 14
 15    def __init__(
 16        self,
 17        name: str,
 18        load_path: str = None,
 19        load: bool = False,
 20        dir: str = "exec_graphs",
 21    ):
 22        """Instansiates a Graphmanager object used to manage a DAG
 23        Args:
 24            name (str): The designated label of this DAG
 25            load_path (str, optional): Path to json config . Defaults to None.
 26            load (bool, optional): If true attemps to load the graph by name or load path. Defaults to False.
 27            dir (str, optional): Path to where this graph is stored. Defaults to "exec_graphs".
 28
 29        Raises:
 30            ValueError: if no name or load_path is supplied and load is true as no data can be loaded.
 31        """
 32        self.nodes = {"start": ExecutionNode("start", "", "")}
 33        self.edges = defaultdict(list)
 34        self.name = name
 35        self.dir = dir
 36        if load and (name or load_path):
 37            self.load(name=name, load_path=load_path)
 38        elif load:
 39            raise ValueError("GraphManager cannot load without a valid graph name")
 40
 41    @staticmethod
 42    def get_file_path(name: str, dir: str):
 43        """From the name and directory, returns what the graph data file should be"""
 44        return os.path.join(dir, f"gm_{name}.json")
 45
 46    @staticmethod
 47    def get_name_from_filename(path: str):
 48        """if given a standardised graph data file, extracts the name of the graph"""
 49        base_name: str = os.path.splitext(os.path.basename(path))[0]
 50        return base_name[3:]
 51
 52    def create_add_new_node(self, node_id: str, blueprint_name: str, param_id: str):
 53        """From data supplied, creates an ExecutionNode and adds it to the graph
 54        Args:
 55            node_id (str): human readable name of node
 56            blueprint_name (str): name of the blueprint in blueprint Register
 57            param_id (str): name of node params from paramater manager
 58        """
 59        new_node = ExecutionNode(
 60            id=node_id, blueprint_name=blueprint_name, param_id=param_id
 61        )
 62        self.add_node(new_node)
 63
 64    def add_node(self, new_node: ExecutionNode):
 65        """Adds a node to the graph"""
 66        assert self.nodes.get(new_node.id, None) is None, "Node Already exists"
 67        self.nodes[new_node.id] = new_node
 68
 69    def change_node_param_template(self, node_id: str, new_param_id: str):
 70        """Changes a node parameter template (i.e. the "params" parsed into the ExecutionNode at runtime)
 71        Args:
 72            node_id (str): instance name of the node within the DAG
 73            new_param_id (str): the new parameter id for the node to use
 74        """
 75        assert isinstance(new_param_id, str) and new_param_id, (
 76            "New Parameter id Invalid"
 77        )
 78        node = self.nodes.get(node_id, None)
 79        assert node is not None, "Node not found"
 80        node.param_id = new_param_id
 81
 82    def remove_node(self, node_id: str):
 83        """
 84            Removes a node from the DAG (and its edges)
 85        Args:
 86            node_id(str): the target node to be removed (DAG name not blueprint name!)
 87        """
 88        assert node_id != "start", (
 89            "Cannot Remove or Alter Start Node, alter it's edges, or parse different start point to build_execution instead"
 90        )
 91        self.edges.pop(node_id, [])
 92        self.nodes.pop(node_id)
 93        for e_v_lst in self.edges.values():
 94            if node_id in e_v_lst:
 95                e_v_lst.remove(node_id)
 96
 97    def add_edge(self, node_from: str, node_to: str):
 98        """Connects 2 nodes together, representing execution order
 99        Args:
100            node_from (str): Node from which the connection is to be made
101            node_to (str): Node the connection ends at.
102        """
103        assert self.nodes.get(node_from, None) is not None, (
104            f"Add Edge From Node {node_from} not present in manager"
105        )
106        assert self.nodes.get(node_to, None) is not None, (
107            f"Add Edge To Node {node_to} not present in manager"
108        )
109        neighbours = self.edges[node_from]
110        if node_to not in neighbours:
111            neighbours.append(node_to)
112
113    def remove_edge(self, node_from: str, node_to: str, allow_inverse: bool = False):
114        """Removes 2 connecting edges of a node, allow inverse checks with the nodes in the opposite direction"""
115        self.edges[node_from].remove(node_to)
116        if allow_inverse:
117            inverse_edge = self.edges.get(node_to, None)
118            if not inverse_edge:
119                return
120            if node_from in inverse_edge:
121                inverse_edge.remove(node_from)
122
123    def get_edges(self):
124        """Gets all edges as a set of  tuples  (of -> from, to) and returns them
125        Returns:
126            set: tuple of (node_from, node_to)
127        """
128        edge_tuples = set()
129        for node_from, neighbours in self.edges.items():
130            for node_to in neighbours:
131                edge_tuples.add((node_from, node_to))
132        return edge_tuples
133
134    def get_nodes(self):
135        """Returns a shallow copy of all nodes"""
136        return dict(self.nodes)
137
138    def get_node(self, node_id: str):
139        """Gets a node via node_id from this dag, returns None if not present"""
140        return self.nodes.get(node_id, None)
141
142    def build_execution(self, start_node_id="start"):
143        """Generates a linear execution of nodes from star_node_id until there are ends
144        Args:
145            start_node_id (str, optional): String of first node_id. Defaults to "start".
146        Returns:
147            list[ExecutionNode]: returns the order of nodes by execution from start->finish
148        NB: Currently does not detect loops, parallelisation is flattened currently.
149        """
150        in_degree = {node_id: 0 for node_id in self.nodes}
151
152        for _, neighbors in self.edges.items():
153            for n in neighbors:
154                in_degree[n] += 1
155
156        queue = deque([start_node_id])
157        exec_order = []
158
159        while queue:
160            current = queue.popleft()
161
162            exec_order.append(self.nodes.get(current))
163
164            for neighbor in self.edges.get(current, []):
165                in_degree[neighbor] -= 1
166                if in_degree[neighbor] == 0:
167                    queue.append(neighbor)
168
169        return exec_order
170
171    def load(self, name: str = None, load_path: str = None):
172        """using the load path primarily or the name if not supplied. load selection order: load_path->name->self.name
173        Args:
174            name (str, optional): Name of graph manager. Defaults to None.
175            load_path (str, optional): Path to graph configuration file. Priortised. Defaults to None.
176        """
177        assert load_path is not None or name is not None or self.name is not None, (
178            "At least one identifier required to find the graph manager"
179        )
180        if load_path is None:
181            load_path = self.get_file_path(name, self.dir)
182
183        with open(load_path, "r", encoding="utf-8") as load_file:
184            load_data = json.load(load_file)
185            self.edges = defaultdict(list, load_data["edges"])
186            self.name = load_data["name"] if name is None else name
187            node_data = {}
188            for node_id, exec_dict in load_data["nodes"].items():
189                node_data[node_id] = ExecutionNode(
190                    id=node_id,
191                    blueprint_name=exec_dict["blueprint_name"],
192                    param_id=exec_dict["param_id"],
193                    pos=exec_dict.get("pos", None),
194                )
195            self.nodes = node_data
196
197    def save(self):
198        """using the name of the current graph, generates a name and saves the configuration as json"""
199        save_path = self.get_file_path(self.name, self.dir)
200        save_data = {"edges": self.edges, "name": self.name}
201        save_data["nodes"] = {
202            node_id: asdict(exec_node) for node_id, exec_node in self.nodes.items()
203        }
204        with open(save_path, "w", encoding="utf-8") as save_file:
205            json.dump(save_data, save_file)
206
207    def duplicate(self, new_name=""):
208        """Duplicates the current graph manager into another"""
209        if not new_name:
210            new_name = f"{self.name}_copy"
211        dupe = GraphManager(name=new_name, load=False, dir=self.dir)
212        for n in self.nodes.values():
213            if n.id == "start":
214                if n.pos:
215                    dupe.nodes["start"].pos = list(n.pos)
216                continue
217            n_copy = ExecutionNode(
218                id=n.id,
219                blueprint_name=n.blueprint_name,
220                param_id=n.param_id,
221                pos=list(n.pos),
222            )
223            dupe.add_node(n_copy)
224        for node_name, neighbours in self.edges.items():
225            for neigh in neighbours:
226                dupe.add_edge(node_name, neigh)
227        return dupe
228
229    @classmethod
230    def file_type(cls):
231        return "json"

Manages a single DAG which is used to build a ML pipeline from Blueprint Nodes

GraphManager( name: str, load_path: str = None, load: bool = False, dir: str = 'exec_graphs')
15    def __init__(
16        self,
17        name: str,
18        load_path: str = None,
19        load: bool = False,
20        dir: str = "exec_graphs",
21    ):
22        """Instansiates a Graphmanager object used to manage a DAG
23        Args:
24            name (str): The designated label of this DAG
25            load_path (str, optional): Path to json config . Defaults to None.
26            load (bool, optional): If true attemps to load the graph by name or load path. Defaults to False.
27            dir (str, optional): Path to where this graph is stored. Defaults to "exec_graphs".
28
29        Raises:
30            ValueError: if no name or load_path is supplied and load is true as no data can be loaded.
31        """
32        self.nodes = {"start": ExecutionNode("start", "", "")}
33        self.edges = defaultdict(list)
34        self.name = name
35        self.dir = dir
36        if load and (name or load_path):
37            self.load(name=name, load_path=load_path)
38        elif load:
39            raise ValueError("GraphManager cannot load without a valid graph name")

Instansiates a Graphmanager object used to manage a DAG Args: name (str): The designated label of this DAG load_path (str, optional): Path to json config . Defaults to None. load (bool, optional): If true attemps to load the graph by name or load path. Defaults to False. dir (str, optional): Path to where this graph is stored. Defaults to "exec_graphs".

Raises: ValueError: if no name or load_path is supplied and load is true as no data can be loaded.

nodes
edges
name
dir
@staticmethod
def get_file_path(name: str, dir: str):
41    @staticmethod
42    def get_file_path(name: str, dir: str):
43        """From the name and directory, returns what the graph data file should be"""
44        return os.path.join(dir, f"gm_{name}.json")

From the name and directory, returns what the graph data file should be

@staticmethod
def get_name_from_filename(path: str):
46    @staticmethod
47    def get_name_from_filename(path: str):
48        """if given a standardised graph data file, extracts the name of the graph"""
49        base_name: str = os.path.splitext(os.path.basename(path))[0]
50        return base_name[3:]

if given a standardised graph data file, extracts the name of the graph

def create_add_new_node(self, node_id: str, blueprint_name: str, param_id: str):
52    def create_add_new_node(self, node_id: str, blueprint_name: str, param_id: str):
53        """From data supplied, creates an ExecutionNode and adds it to the graph
54        Args:
55            node_id (str): human readable name of node
56            blueprint_name (str): name of the blueprint in blueprint Register
57            param_id (str): name of node params from paramater manager
58        """
59        new_node = ExecutionNode(
60            id=node_id, blueprint_name=blueprint_name, param_id=param_id
61        )
62        self.add_node(new_node)

From data supplied, creates an ExecutionNode and adds it to the graph Args: node_id (str): human readable name of node blueprint_name (str): name of the blueprint in blueprint Register param_id (str): name of node params from paramater manager

def add_node(self, new_node: m_strat.api.dag.node.ExecutionNode):
64    def add_node(self, new_node: ExecutionNode):
65        """Adds a node to the graph"""
66        assert self.nodes.get(new_node.id, None) is None, "Node Already exists"
67        self.nodes[new_node.id] = new_node

Adds a node to the graph

def change_node_param_template(self, node_id: str, new_param_id: str):
69    def change_node_param_template(self, node_id: str, new_param_id: str):
70        """Changes a node parameter template (i.e. the "params" parsed into the ExecutionNode at runtime)
71        Args:
72            node_id (str): instance name of the node within the DAG
73            new_param_id (str): the new parameter id for the node to use
74        """
75        assert isinstance(new_param_id, str) and new_param_id, (
76            "New Parameter id Invalid"
77        )
78        node = self.nodes.get(node_id, None)
79        assert node is not None, "Node not found"
80        node.param_id = new_param_id

Changes a node parameter template (i.e. the "params" parsed into the ExecutionNode at runtime) Args: node_id (str): instance name of the node within the DAG new_param_id (str): the new parameter id for the node to use

def remove_node(self, node_id: str):
82    def remove_node(self, node_id: str):
83        """
84            Removes a node from the DAG (and its edges)
85        Args:
86            node_id(str): the target node to be removed (DAG name not blueprint name!)
87        """
88        assert node_id != "start", (
89            "Cannot Remove or Alter Start Node, alter it's edges, or parse different start point to build_execution instead"
90        )
91        self.edges.pop(node_id, [])
92        self.nodes.pop(node_id)
93        for e_v_lst in self.edges.values():
94            if node_id in e_v_lst:
95                e_v_lst.remove(node_id)

Removes a node from the DAG (and its edges) Args: node_id(str): the target node to be removed (DAG name not blueprint name!)

def add_edge(self, node_from: str, node_to: str):
 97    def add_edge(self, node_from: str, node_to: str):
 98        """Connects 2 nodes together, representing execution order
 99        Args:
100            node_from (str): Node from which the connection is to be made
101            node_to (str): Node the connection ends at.
102        """
103        assert self.nodes.get(node_from, None) is not None, (
104            f"Add Edge From Node {node_from} not present in manager"
105        )
106        assert self.nodes.get(node_to, None) is not None, (
107            f"Add Edge To Node {node_to} not present in manager"
108        )
109        neighbours = self.edges[node_from]
110        if node_to not in neighbours:
111            neighbours.append(node_to)

Connects 2 nodes together, representing execution order Args: node_from (str): Node from which the connection is to be made node_to (str): Node the connection ends at.

def remove_edge(self, node_from: str, node_to: str, allow_inverse: bool = False):
113    def remove_edge(self, node_from: str, node_to: str, allow_inverse: bool = False):
114        """Removes 2 connecting edges of a node, allow inverse checks with the nodes in the opposite direction"""
115        self.edges[node_from].remove(node_to)
116        if allow_inverse:
117            inverse_edge = self.edges.get(node_to, None)
118            if not inverse_edge:
119                return
120            if node_from in inverse_edge:
121                inverse_edge.remove(node_from)

Removes 2 connecting edges of a node, allow inverse checks with the nodes in the opposite direction

def get_edges(self):
123    def get_edges(self):
124        """Gets all edges as a set of  tuples  (of -> from, to) and returns them
125        Returns:
126            set: tuple of (node_from, node_to)
127        """
128        edge_tuples = set()
129        for node_from, neighbours in self.edges.items():
130            for node_to in neighbours:
131                edge_tuples.add((node_from, node_to))
132        return edge_tuples

Gets all edges as a set of tuples (of -> from, to) and returns them Returns: set: tuple of (node_from, node_to)

def get_nodes(self):
134    def get_nodes(self):
135        """Returns a shallow copy of all nodes"""
136        return dict(self.nodes)

Returns a shallow copy of all nodes

def get_node(self, node_id: str):
138    def get_node(self, node_id: str):
139        """Gets a node via node_id from this dag, returns None if not present"""
140        return self.nodes.get(node_id, None)

Gets a node via node_id from this dag, returns None if not present

def build_execution(self, start_node_id='start'):
142    def build_execution(self, start_node_id="start"):
143        """Generates a linear execution of nodes from star_node_id until there are ends
144        Args:
145            start_node_id (str, optional): String of first node_id. Defaults to "start".
146        Returns:
147            list[ExecutionNode]: returns the order of nodes by execution from start->finish
148        NB: Currently does not detect loops, parallelisation is flattened currently.
149        """
150        in_degree = {node_id: 0 for node_id in self.nodes}
151
152        for _, neighbors in self.edges.items():
153            for n in neighbors:
154                in_degree[n] += 1
155
156        queue = deque([start_node_id])
157        exec_order = []
158
159        while queue:
160            current = queue.popleft()
161
162            exec_order.append(self.nodes.get(current))
163
164            for neighbor in self.edges.get(current, []):
165                in_degree[neighbor] -= 1
166                if in_degree[neighbor] == 0:
167                    queue.append(neighbor)
168
169        return exec_order

Generates a linear execution of nodes from star_node_id until there are ends Args: start_node_id (str, optional): String of first node_id. Defaults to "start". Returns: list[ExecutionNode]: returns the order of nodes by execution from start->finish NB: Currently does not detect loops, parallelisation is flattened currently.

def load(self, name: str = None, load_path: str = None):
171    def load(self, name: str = None, load_path: str = None):
172        """using the load path primarily or the name if not supplied. load selection order: load_path->name->self.name
173        Args:
174            name (str, optional): Name of graph manager. Defaults to None.
175            load_path (str, optional): Path to graph configuration file. Priortised. Defaults to None.
176        """
177        assert load_path is not None or name is not None or self.name is not None, (
178            "At least one identifier required to find the graph manager"
179        )
180        if load_path is None:
181            load_path = self.get_file_path(name, self.dir)
182
183        with open(load_path, "r", encoding="utf-8") as load_file:
184            load_data = json.load(load_file)
185            self.edges = defaultdict(list, load_data["edges"])
186            self.name = load_data["name"] if name is None else name
187            node_data = {}
188            for node_id, exec_dict in load_data["nodes"].items():
189                node_data[node_id] = ExecutionNode(
190                    id=node_id,
191                    blueprint_name=exec_dict["blueprint_name"],
192                    param_id=exec_dict["param_id"],
193                    pos=exec_dict.get("pos", None),
194                )
195            self.nodes = node_data

using the load path primarily or the name if not supplied. load selection order: load_path->name->self.name Args: name (str, optional): Name of graph manager. Defaults to None. load_path (str, optional): Path to graph configuration file. Priortised. Defaults to None.

def save(self):
197    def save(self):
198        """using the name of the current graph, generates a name and saves the configuration as json"""
199        save_path = self.get_file_path(self.name, self.dir)
200        save_data = {"edges": self.edges, "name": self.name}
201        save_data["nodes"] = {
202            node_id: asdict(exec_node) for node_id, exec_node in self.nodes.items()
203        }
204        with open(save_path, "w", encoding="utf-8") as save_file:
205            json.dump(save_data, save_file)

using the name of the current graph, generates a name and saves the configuration as json

def duplicate(self, new_name=''):
207    def duplicate(self, new_name=""):
208        """Duplicates the current graph manager into another"""
209        if not new_name:
210            new_name = f"{self.name}_copy"
211        dupe = GraphManager(name=new_name, load=False, dir=self.dir)
212        for n in self.nodes.values():
213            if n.id == "start":
214                if n.pos:
215                    dupe.nodes["start"].pos = list(n.pos)
216                continue
217            n_copy = ExecutionNode(
218                id=n.id,
219                blueprint_name=n.blueprint_name,
220                param_id=n.param_id,
221                pos=list(n.pos),
222            )
223            dupe.add_node(n_copy)
224        for node_name, neighbours in self.edges.items():
225            for neigh in neighbours:
226                dupe.add_edge(node_name, neigh)
227        return dupe

Duplicates the current graph manager into another

@classmethod
def file_type(cls):
229    @classmethod
230    def file_type(cls):
231        return "json"

The file type that will be generated on save