Skip to content

Commit af816e0

Browse files
committed
refined entropy_graph.py
1 parent edc3d86 commit af816e0

1 file changed

Lines changed: 40 additions & 26 deletions

File tree

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,63 @@
1-
import logging
2-
from typing import Any, Dict
3-
41
import networkx as nx
52

6-
logger = logging.getLogger(__name__)
3+
from CodeEntropy.entropy.nodes.configurational_entropy import ConfigurationalEntropyNode
4+
from CodeEntropy.entropy.nodes.entropy_aggregator import EntropyAggregatorNode
5+
from CodeEntropy.entropy.nodes.orientational_entropy import OrientationalEntropyNode
6+
from CodeEntropy.entropy.nodes.vibrational_entropy import VibrationalEntropyNode
77

88

99
class EntropyGraph:
1010
"""
11-
A Directed Acyclic Graph (DAG) for managing entropy calculation nodes.
11+
DAG representing the entropy computation pipeline:
1212
13-
Each node must implement:
14-
run(shared_data: dict, **kwargs) -> dict
13+
1. Vibrational entropy
14+
2. Rotational (orientational) entropy
15+
3. Conformational entropy
16+
4. Aggregate entropy across levels and groups
1517
"""
1618

1719
def __init__(self):
1820
self.graph = nx.DiGraph()
19-
self.nodes: Dict[str, Any] = {}
21+
self.nodes = {}
22+
23+
def build(self):
24+
self.add("vibrational_entropy", VibrationalEntropyNode())
25+
26+
self.add(
27+
"orientational_entropy",
28+
OrientationalEntropyNode(),
29+
depends_on=["vibrational_entropy"],
30+
)
31+
32+
self.add(
33+
"configurational_entropy",
34+
ConfigurationalEntropyNode(),
35+
depends_on=["orientational_entropy"],
36+
)
2037

21-
def add_node(self, name: str, node_obj: Any, depends_on=None):
22-
if not hasattr(node_obj, "run"):
23-
raise TypeError(f"Node '{name}' must implement run(shared_data, **kwargs)")
38+
self.add(
39+
"aggregate_entropy",
40+
EntropyAggregatorNode(),
41+
depends_on=["configurational_entropy"],
42+
)
2443

25-
self.nodes[name] = node_obj
44+
return self
45+
46+
def add(self, name, obj, depends_on=None):
47+
self.nodes[name] = obj
2648
self.graph.add_node(name)
2749
if depends_on:
2850
for dep in depends_on:
2951
self.graph.add_edge(dep, name)
3052

31-
def execute(self, shared_data: Dict[str, Any]) -> Dict[str, Dict[str, Any]]:
32-
results: Dict[str, Dict[str, Any]] = {}
53+
def execute(self, shared_data):
54+
results = {}
3355

34-
for node_name in nx.topological_sort(self.graph):
35-
preds = list(self.graph.predecessors(node_name))
56+
for node in nx.topological_sort(self.graph):
57+
preds = list(self.graph.predecessors(node))
3658
kwargs = {p: results[p] for p in preds}
3759

38-
node = self.nodes[node_name]
39-
output = node.run(shared_data, **kwargs)
40-
41-
if not isinstance(output, dict):
42-
raise TypeError(
43-
f"Node '{node_name}' returned {type(output)} (expected dict)"
44-
)
45-
46-
results[node_name] = output
47-
shared_data.update(output)
60+
output = self.nodes[node].run(shared_data, **kwargs)
61+
results[node] = output
4862

4963
return results

0 commit comments

Comments
 (0)