|
| 1 | +Batching |
| 2 | +======== |
| 3 | + |
| 4 | +`IMDReader`_ provides support for reading MD simulation data via the `IMDv3 Protocol`_ |
| 5 | +in MDAnalysis since `Release 2.10.0`_. |
| 6 | + |
| 7 | +Since IMD streams data in real-time from a running simulation, it has fundamental constraints |
| 8 | +that differ from traditional trajectory readers and this leads to some `Important Limitations`_ |
| 9 | +in `IMDReader`_. |
| 10 | + |
| 11 | +Buffered Access |
| 12 | +--------------- |
| 13 | + |
| 14 | +To support buffered, time-dependent analyses in ``mdadash``, a |
| 15 | +:class:`~mdadash.backend.kernel.core.BufferedTrajectory` is introduced. |
| 16 | + |
| 17 | +The original trajectory is wrapped by the :class:`~mdadash.backend.kernel.core.BufferedTrajectory` |
| 18 | +to provide buffered access to the last ``n`` timesteps, where ``n`` is the configured batch size. |
| 19 | + |
| 20 | +.. code-block:: python |
| 21 | +
|
| 22 | + u.trajectory = BufferedTrajectory(u.trajectory, config["batch_size"]) |
| 23 | +
|
| 24 | +``trajectory[index]`` can be used to access individual frames. Index values can range from 0 to the |
| 25 | +configured batch size ``n``. The batch size ``n`` is available via the ``trajectory.buffer_size`` |
| 26 | +attribute. |
| 27 | + |
| 28 | +When a Widget class supports :ref:`batching <run-frequency>` and implements the |
| 29 | +:meth:`~mdadash.backend.widgets.base.WidgetBase.run_batch` method, the trajectory can be iterated |
| 30 | +this way to access the last ``n`` timesteps. |
| 31 | + |
| 32 | +Here is a typical compute batch block used in the code for :doc:`built_in_widgets`: |
| 33 | + |
| 34 | +.. code-block:: python |
| 35 | +
|
| 36 | + def _compute_batch(self): |
| 37 | + """Compute for current batch""" |
| 38 | + values = [] |
| 39 | + for i in range(self.u.trajectory.buffer_size): |
| 40 | + _ = self.u.trajectory[i] # set the trajectory to frame i |
| 41 | + values.append(self._compute_current_frame()) |
| 42 | + return values |
| 43 | +
|
| 44 | +
|
| 45 | +AnalysisBase support |
| 46 | +-------------------- |
| 47 | + |
| 48 | +MDAnalysis provides an `AnalysisBase`_, which is the base class for defining multi-frame analysis. |
| 49 | + |
| 50 | +A lot of built-in MDAnalysis `Analysis modules`_ derive from `AnalysisBase`_. |
| 51 | + |
| 52 | +The :class:`~mdadash.backend.kernel.core.BufferedTrajectory` enables using these analysis |
| 53 | +modules in the Widget classes, which are not possible with `IMDReader`_. |
| 54 | + |
| 55 | +.. note:: |
| 56 | + |
| 57 | + The total number of frames as seen by the `AnalysisBase`_-based classes will be the |
| 58 | + configured Buffer / batch size during a full ``analysis.run()`` invocation. |
| 59 | + |
| 60 | +Here is an example of using an `AnalysisBase`_-based class within the Widget code by the |
| 61 | +:mod:`Native Contacts <mdadash.backend.analyses.native_contacts>` built-in Widget. |
| 62 | + |
| 63 | +.. code-block:: python |
| 64 | +
|
| 65 | + from MDAnalysis.analysis import contacts |
| 66 | + ..... |
| 67 | +
|
| 68 | + def _create_contacts(self): |
| 69 | + """Update atom groups when selection phrases change""" |
| 70 | + self.contacts = contacts.Contacts( |
| 71 | + self.u, |
| 72 | + ..... |
| 73 | +
|
| 74 | + def _compute_batch(self): |
| 75 | + """Compute values for current batch""" |
| 76 | + self.contacts.run() |
| 77 | + values = [] |
| 78 | + for i, (_, q) in enumerate(self.contacts.results.timeseries): |
| 79 | + ..... |
| 80 | +
|
| 81 | +
|
| 82 | +`AnalysisBase`_-based classes can also be used per-frame by passing the current frame as |
| 83 | +shown in this example: |
| 84 | +
|
| 85 | +.. code-block:: python |
| 86 | +
|
| 87 | + def _compute_current_frame(self): |
| 88 | + """Compute values for current frame""" |
| 89 | + self.contacts.run(frames=[self.u.trajectory.frame]) |
| 90 | + ..... |
| 91 | +
|
| 92 | +
|
| 93 | +---- |
| 94 | +
|
| 95 | +The list of all the Widgets that support batching can be found on the |
| 96 | +:doc:`built_in_widgets` page. |
| 97 | +
|
| 98 | +
|
| 99 | +.. _IMDReader: https://docs.mdanalysis.org/stable/ |
| 100 | + documentation_pages/coordinates/IMD.html |
| 101 | +
|
| 102 | +.. _Important Limitations: https://docs.mdanalysis.org/stable/ |
| 103 | + documentation_pages/coordinates/IMD.html#important-limitations |
| 104 | +
|
| 105 | +.. _IMDv3 Protocol: https://imdclient.readthedocs.io/en/latest/protocol_v3.html |
| 106 | +
|
| 107 | +.. _Release 2.10.0: https://www.mdanalysis.org/2025/10/26/release-2.10.0/ |
| 108 | +
|
| 109 | +.. _AnalysisBase: https://docs.mdanalysis.org/stable/ |
| 110 | + documentation_pages/analysis/base.html#MDAnalysis.analysis.base.AnalysisBase |
| 111 | +
|
| 112 | +.. _Analysis modules: https://docs.mdanalysis.org/stable/ |
| 113 | + documentation_pages/analysis_modules.html |
| 114 | +
|
0 commit comments