|
1 | 1 | import logging |
2 | | -from typing import Any, Dict |
3 | 2 |
|
4 | | -from CodeEntropy.levels.hierarchy_graph import HierarchyGraph |
| 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 |
5 | 9 |
|
6 | 10 | logger = logging.getLogger(__name__) |
7 | 11 |
|
8 | 12 |
|
9 | 13 | class LevelManager: |
10 | 14 | """ |
11 | | - Coordinates the DAG-based computation of molecular levels and beads. |
12 | | - All physics/maths are delegated to lower-level classes and DAG nodes. |
| 15 | + High–level orchestrator for all 'level' computations. |
| 16 | +
|
| 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 |
13 | 22 | """ |
14 | 23 |
|
15 | | - def __init__(self, universe, run_manager, args): |
| 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): |
16 | 36 | """ |
| 37 | + Wrapper around LevelHierarchy.select_levels |
| 38 | +
|
17 | 39 | Parameters |
18 | 40 | ---------- |
19 | | - universe : MDAnalysis.Universe |
20 | | - The MD system being analysed. |
21 | | - run_manager : RunManager |
22 | | - Provides selection helpers and unit conversions. |
23 | | - args : Namespace |
24 | | - Parsed CLI arguments. |
| 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"], ...] |
25 | 49 | """ |
26 | | - self.universe = universe |
27 | | - self.run_manager = run_manager |
28 | | - self.args = args |
| 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 |
29 | 54 |
|
30 | | - def run_hierarchy(self) -> Dict[str, Any]: |
| 55 | + def get_beads(self, data_container, level): |
31 | 56 | """ |
32 | | - Execute the structural hierarchy DAG (levels → beads). |
| 57 | + Simple pass-through to LevelHierarchy.get_beads |
| 58 | +
|
| 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) |
| 63 | + |
| 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 | + ): |
| 75 | + """ |
| 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. |
| 81 | +
|
| 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 |
| 92 | +
|
| 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 | + ) |
| 112 | + |
| 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. |
| 128 | +
|
| 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. |
33 | 142 |
|
34 | 143 | Returns |
35 | 144 | ------- |
36 | | - dict |
37 | | - Contains: |
38 | | - - number_molecules |
39 | | - - levels |
40 | | - - beads_by_mol_level |
| 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, ...] |
41 | 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 | + ) |
42 | 165 |
|
43 | | - shared_data = { |
44 | | - "universe": self.universe, |
45 | | - "args": self.args, |
46 | | - "run_manager": self.run_manager, |
47 | | - } |
| 166 | + def filter_zero_rows_columns(self, matrix): |
| 167 | + """ |
| 168 | + Wrapper around MatrixOperations.filter_zero_rows_columns. |
48 | 169 |
|
49 | | - graph = HierarchyGraph().build(self.run_manager) |
50 | | - results = graph.execute(shared_data) |
51 | 170 |
|
52 | | - logger.debug("[LevelManager] Hierarchy DAG results:") |
53 | | - logger.debug(results) |
| 171 | + Physics and numerical behaviour are unchanged – MatrixOperations |
| 172 | + contains the original implementation. |
| 173 | + """ |
| 174 | + return self._mat_ops.filter_zero_rows_columns(matrix) |
54 | 175 |
|
55 | | - return results |
| 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) |
56 | 182 |
|
57 | | - def run(self): |
| 183 | + def find_neighbours(self, data_container, bead, cutoff): |
58 | 184 | """ |
59 | | - Placeholder: eventually will run all DAGs (hierarchy, matrices, entropy). |
60 | | - For now, only run the hierarchy graph. |
| 185 | + Convenience wrapper around Neighbours. |
61 | 186 | """ |
62 | | - return self.run_hierarchy() |
| 187 | + return self._neighbours.find_neighbours(data_container, bead, cutoff) |
0 commit comments