|
1 | 1 | import logging |
| 2 | +from typing import Any, Dict |
2 | 3 |
|
3 | | -from CodeEntropy.levels.coordinate_system import CoordinateSystem |
4 | | -from CodeEntropy.levels.dihedral_tools import DihedralAnalysis |
5 | | -from CodeEntropy.levels.force_torque_manager import ForceTorqueManager |
6 | | -from CodeEntropy.levels.level_hierarchy import LevelHierarchy |
7 | | -from CodeEntropy.levels.matrix_operations import MatrixOperations |
8 | | -from CodeEntropy.levels.neighbours import Neighbours |
| 4 | +from CodeEntropy.levels.hierarchy_graph import LevelDAG |
9 | 5 |
|
10 | 6 | logger = logging.getLogger(__name__) |
11 | 7 |
|
12 | 8 |
|
13 | 9 | class LevelManager: |
14 | 10 | """ |
15 | | - High–level orchestrator for all 'level' computations. |
| 11 | + High-level coordinator that runs the Level DAG and returns results required |
| 12 | + later for entropy nodes. |
16 | 13 |
|
17 | | - It does not implement physics itself. Instead it: |
18 | | - - delegates to LevelHierarchy to decide which levels exist |
19 | | - - delegates to ForceTorqueManager to build covariance matrices |
20 | | - - delegates to DihedralAnalysis to build conformational states |
21 | | - - delegates to MatrixOperations for matrix cleanup / utilities |
| 14 | + Output from this class is forwarded into EntropyGraph. |
22 | 15 | """ |
23 | 16 |
|
24 | | - def __init__(self): |
25 | | - """ |
26 | | - Construct modular helpers and keep shared references. |
27 | | - """ |
28 | | - self._hierarchy = LevelHierarchy() |
29 | | - self._coords = CoordinateSystem() |
30 | | - self._dihedrals = DihedralAnalysis() |
31 | | - self._mat_ops = MatrixOperations() |
32 | | - self._force_torque = ForceTorqueManager() |
33 | | - self._neighbours = Neighbours() |
34 | | - |
35 | | - def select_levels(self, data_container): |
36 | | - """ |
37 | | - Wrapper around LevelHierarchy.select_levels |
38 | | -
|
39 | | - Parameters |
40 | | - ---------- |
41 | | - data_container : MDAnalysis.Universe |
42 | | - Reduced universe / selection. |
43 | | -
|
44 | | - Returns |
45 | | - ------- |
46 | | - number_molecules : int |
47 | | - levels : list[list[str]] |
48 | | - e.g. [["united_atom", "residue", "polymer"], ["united_atom"], ...] |
49 | | - """ |
50 | | - number_molecules, levels = self._hierarchy.select_levels(data_container) |
51 | | - logger.debug(f"[LevelManager] number_molecules={number_molecules}") |
52 | | - logger.debug(f"[LevelManager] levels={levels}") |
53 | | - return number_molecules, levels |
| 17 | + def __init__(self, run_manager=None): |
| 18 | + self.run_manager = run_manager |
| 19 | + self.level_results = None |
54 | 20 |
|
55 | | - def get_beads(self, data_container, level): |
| 21 | + def run(self, universe) -> Dict[str, Any]: |
56 | 22 | """ |
57 | | - Simple pass-through to LevelHierarchy.get_beads |
| 23 | + Execute the level-processing DAG and return all structural results. |
58 | 24 |
|
59 | | - Kept here in case you later want the DAG to ask LevelManager for bead |
60 | | - sets without touching LevelHierarchy directly. |
61 | | - """ |
62 | | - return self._hierarchy.get_beads(data_container, level) |
| 25 | + Input: |
| 26 | + universe (MDAnalysis.Universe) |
63 | 27 |
|
64 | | - def build_covariance_matrices( |
65 | | - self, |
66 | | - entropy_manager, |
67 | | - reduced_atom, |
68 | | - levels, |
69 | | - groups, |
70 | | - start, |
71 | | - end, |
72 | | - step, |
73 | | - number_frames, |
74 | | - ): |
| 28 | + Output dictionary feeds forward into the entropy pipeline. |
75 | 29 | """ |
76 | | - Thin wrapper around ForceTorqueManager.build_covariance_matrices. |
77 | | -
|
78 | | - All the heavy lifting (loops over frames, groups, levels, beads, |
79 | | - physics of forces/torques, incremental averaging) stays inside |
80 | | - ForceTorqueManager. |
| 30 | + dag = LevelDAG().build() |
81 | 31 |
|
82 | | - Parameters |
83 | | - ---------- |
84 | | - entropy_manager : EntropyManager |
85 | | - Needed because ForceTorqueManager currently calls |
86 | | - entropy_manager._get_molecule_container(...) |
87 | | - reduced_atom : MDAnalysis.Universe |
88 | | - levels : list[list[str]] |
89 | | - groups : dict[int, list[int]] |
90 | | - start, end, step : int |
91 | | - number_frames : int |
| 32 | + shared_data = {"universe": universe, "run_manager": self.run_manager} |
92 | 33 |
|
93 | | - Returns |
94 | | - ------- |
95 | | - force_matrices : dict |
96 | | - torque_matrices : dict |
97 | | - frame_counts : dict |
98 | | - """ |
99 | | - logger.debug( |
100 | | - "[LevelManager] Delegating to ForceTorqueManager.build_covariance_matrices" |
101 | | - ) |
102 | | - return self._force_torque.build_covariance_matrices( |
103 | | - entropy_manager=entropy_manager, |
104 | | - reduced_atom=reduced_atom, |
105 | | - levels=levels, |
106 | | - groups=groups, |
107 | | - start=start, |
108 | | - end=end, |
109 | | - step=step, |
110 | | - number_frames=number_frames, |
111 | | - ) |
| 34 | + results = dag.execute(shared_data) |
112 | 35 |
|
113 | | - def build_conformational_states( |
114 | | - self, |
115 | | - entropy_manager, |
116 | | - reduced_atom, |
117 | | - levels, |
118 | | - groups, |
119 | | - start, |
120 | | - end, |
121 | | - step, |
122 | | - number_frames, |
123 | | - bin_width, |
124 | | - conformational_entropy_obj, |
125 | | - ): |
126 | | - """ |
127 | | - Wrapper around DihedralAnalysis.build_conformational_states. |
| 36 | + self.level_results = { |
| 37 | + "levels": results["detect_levels"]["levels"], |
| 38 | + "molecule_count": results["detect_molecules"]["molecule_count"], |
| 39 | + "beads": results["build_beads"]["beads_by_mol_level"], |
| 40 | + "axes": results["compute_axes"]["axes"], |
| 41 | + "forces": results["compute_weighted_forces"]["forces"], |
| 42 | + "torques": results["compute_weighted_torques"]["torques"], |
| 43 | + "cov_matrices": results["build_covariance"]["covariance"], |
| 44 | + "dihedrals": results["compute_dihedrials"]["dihedrals"], |
| 45 | + "conformations": results["build_conformations"]["states"], |
| 46 | + "neighbours": results["compute_neighbours"]["neighbours"], |
| 47 | + } |
128 | 48 |
|
129 | | - Parameters |
130 | | - ---------- |
131 | | - entropy_manager : EntropyManager |
132 | | - reduced_atom : MDAnalysis.Universe |
133 | | - levels : list[list[str]] |
134 | | - groups : dict[int, list[int]] |
135 | | - start, end, step : int |
136 | | - number_frames : int |
137 | | - bin_width : int |
138 | | - Histogram bin width (degrees) for dihedral distributions. |
139 | | - conformational_entropy_obj : ConformationalEntropy |
140 | | - The CE object, passed through because your dihedral code |
141 | | - may call its methods. |
| 49 | + return self.level_results |
142 | 50 |
|
143 | | - Returns |
144 | | - ------- |
145 | | - states_ua : dict |
146 | | - e.g. {(group_id, residue_id): states_array} |
147 | | - states_res : list |
148 | | - e.g. [states_for_group0, states_for_group1, ...] |
149 | | - """ |
150 | | - logger.debug( |
151 | | - "[LevelManager] Delegating to DihedralAnalysis.build_conformational_states" |
152 | | - ) |
153 | | - return self._dihedrals.build_conformational_states( |
154 | | - entropy_manager=entropy_manager, |
155 | | - reduced_atom=reduced_atom, |
156 | | - levels=levels, |
157 | | - groups=groups, |
158 | | - start=start, |
159 | | - end=end, |
160 | | - step=step, |
161 | | - number_frames=number_frames, |
162 | | - bin_width=bin_width, |
163 | | - conformational_entropy_obj=conformational_entropy_obj, |
164 | | - ) |
165 | | - |
166 | | - def filter_zero_rows_columns(self, matrix): |
167 | | - """ |
168 | | - Wrapper around MatrixOperations.filter_zero_rows_columns. |
169 | | -
|
170 | | -
|
171 | | - Physics and numerical behaviour are unchanged – MatrixOperations |
172 | | - contains the original implementation. |
173 | | - """ |
174 | | - return self._mat_ops.filter_zero_rows_columns(matrix) |
175 | | - |
176 | | - def get_axes(self, bead): |
177 | | - """ |
178 | | - Convenience forwarder to CoordinateSystem.get_axes (if/when needed). |
179 | | - Not used by EntropyManager right now but handy for future DAG nodes. |
180 | | - """ |
181 | | - return self._coords.get_axes(bead) |
182 | | - |
183 | | - def find_neighbours(self, data_container, bead, cutoff): |
184 | | - """ |
185 | | - Convenience wrapper around Neighbours. |
186 | | - """ |
187 | | - return self._neighbours.find_neighbours(data_container, bead, cutoff) |
| 51 | + def get(self, key): |
| 52 | + return self.level_results.get(key) |
0 commit comments