|
1 | | -import logging |
2 | | -from typing import Any, Dict |
3 | | - |
4 | 1 | import networkx as nx |
5 | 2 |
|
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 |
7 | 7 |
|
8 | 8 |
|
9 | 9 | class EntropyGraph: |
10 | 10 | """ |
11 | | - A Directed Acyclic Graph (DAG) for managing entropy calculation nodes. |
| 11 | + DAG representing the entropy computation pipeline: |
12 | 12 |
|
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 |
15 | 17 | """ |
16 | 18 |
|
17 | 19 | def __init__(self): |
18 | 20 | 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 | + ) |
20 | 37 |
|
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 | + ) |
24 | 43 |
|
25 | | - self.nodes[name] = node_obj |
| 44 | + return self |
| 45 | + |
| 46 | + def add(self, name, obj, depends_on=None): |
| 47 | + self.nodes[name] = obj |
26 | 48 | self.graph.add_node(name) |
27 | 49 | if depends_on: |
28 | 50 | for dep in depends_on: |
29 | 51 | self.graph.add_edge(dep, name) |
30 | 52 |
|
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 = {} |
33 | 55 |
|
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)) |
36 | 58 | kwargs = {p: results[p] for p in preds} |
37 | 59 |
|
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 |
48 | 62 |
|
49 | 63 | return results |
0 commit comments