Skip to content

Commit 652a52f

Browse files
authored
Networkx memory issue (#368)
* Fix networkx memory issue * Some cleanup
1 parent ab1c7aa commit 652a52f

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

PrismaFastApi/src/services/decision_tree/decision_tree_creator_v3.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ class DecisionTreeGraph_v3:
3939
def __init__(self, root: uuid.UUID, **kwargs: Dict[str, Any]) -> None:
4040
self.nx: nx.DiGraph = nx.DiGraph(**kwargs) # type: ignore
4141
self.root: uuid.UUID = root
42-
self.nx.add_node(self.root) # type: ignore
4342
self.edge_names: Dict[Tuple[uuid.UUID, uuid.UUID], str] = {}
4443
self.utility_lookup: Dict[tuple[str, ...], List[float]] = {}
4544
self.discrete_probability_lookup = defaultdict(set)
4645
self.node_treenode_lookup: NodeTreeNodeLookup
4746
self.final_expected_value: float = 0
4847
self.treenode_oldid_to_newid_map: Dict[uuid.UUID, uuid.UUID] = {}
49-
self.treenodeid_to_parentid_map: Dict[uuid.UUID, uuid.UUID] = {}
48+
self.treenodeid_to_parentid_map : Dict[uuid.UUID, Optional[uuid.UUID]] = {}
49+
self.treenodeid_to_parentid_map[self.root] = None
5050

5151
def add_edge(self, edge: EdgeUUIDDto) -> None:
52-
self.nx.add_edge(edge.tail, edge.head, name=edge.name) # type: ignore
5352
self.treenodeid_to_parentid_map[edge.head] = edge.tail
53+
self.edge_names[(edge.tail, edge.head)] = edge.name
5454

5555
def transfer_node_treenode_lookup(self, lookup: NodeTreeNodeLookup) -> None:
5656
self.node_treenode_lookup = lookup
@@ -101,8 +101,7 @@ def populate_discrete_probabilities_lookup(self) -> None:
101101

102102
def get_dto_map(self):
103103
dto_map: Dict[uuid.UUID, TreeNodeDto2] = {}
104-
for treenode_id in self.nx.nodes: # type: ignore
105-
treenode_id = cast(uuid.UUID, treenode_id)
104+
for treenode_id in self.treenodeid_to_parentid_map.keys():
106105
if node := self.node_treenode_lookup.get_dto_for_treenode_id(treenode_id):
107106
type = Type.END.value if isinstance(node, EndPointNodeDto) else node.type
108107
parent_state_id: Optional[str] = self.edge_names.get((self.get_parent(treenode_id), treenode_id))
@@ -123,12 +122,11 @@ def get_dto_map(self):
123122

124123
dto_map[treenode_id] = dto
125124

126-
for parent_id in self.nx.nodes: # type: ignore
127-
parent_id = cast(uuid.UUID, parent_id)
125+
for parent_id in self.treenodeid_to_parentid_map.keys():
128126
parent_dto = dto_map.get(parent_id)
129127
if parent_dto is not None:
130128
# Get child node ids (outgoing edges from parent)
131-
child_ids: List[uuid.UUID] = list(self.nx.successors(parent_id)) # type: ignore
129+
child_ids: List[uuid.UUID] = list(self.get_successors(parent_id)) # type: ignore
132130
# Set children as list of DTOs
133131
parent_dto.children = [
134132
dto_map[child_id] for child_id in child_ids if child_id in dto_map
@@ -137,6 +135,9 @@ def get_dto_map(self):
137135

138136
return dto_map
139137

138+
def get_successors(self, parent_id: uuid.UUID):
139+
return [k for k, v in self.treenodeid_to_parentid_map.items() if v == parent_id]
140+
140141
def topological_sort(self, dto_map: Dict[uuid.UUID, TreeNodeDto2]) -> List[uuid.UUID]:
141142
visited: Set[uuid.UUID] = set()
142143
order: List[uuid.UUID] = []
@@ -165,7 +166,6 @@ def to_issue_dtos(self, backwards_calc: bool = True) -> Optional[TreeNodeDto2]:
165166
self.populate_utility_lookup() # create lookup for discrete utilities
166167
if backwards_calc:
167168
self.populate_discrete_probabilities_lookup() # create lookup for discrete probabilities
168-
self.edge_names = nx.get_edge_attributes(self.nx, "name") # type: ignore
169169
dto_map = self.get_dto_map()
170170
self.calculate_endpoint_nodes(self.root, dto_map)
171171
if backwards_calc:

0 commit comments

Comments
 (0)