Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tqdm bar for: pca.transform() #4531

Merged
merged 11 commits into from
Mar 28, 2024
1 change: 1 addition & 0 deletions package/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ Chronological list of authors
2024
- Aditya Keshari
- Philipp Stärk
- Sampurna Mukherjee

External code
-------------
Expand Down
4 changes: 3 additions & 1 deletion package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The rules for this file:
-------------------------------------------------------------------------------
??/??/?? IAlibay, HeetVekariya, marinegor, lilyminium, RMeli,
ljwoods2, aditya292002, pstaerk, PicoCentauri, BFedder,
tyler.je.reddy
tyler.je.reddy, SampurnaM

* 2.8.0

Expand All @@ -34,6 +34,8 @@ Fixes
* Fix deploy action to use the correct version of the pypi upload action.

Enhancements
* Added a tqdm progress bar for `MDAnalysis.analysis.pca.PCA.transform()`
(PR #4531)
* Improved performance of PDBWriter (Issue #2785, PR #4472)
* Added parsing of arbitrary columns of the LAMMPS dump parser. (Issue #3504)
* Documented the r0 attribute in the `Contacts` class and added the
Expand Down
22 changes: 17 additions & 5 deletions package/MDAnalysis/analysis/pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@

import numpy as np
import scipy.integrate
from tqdm.auto import tqdm

from MDAnalysis import Universe
from MDAnalysis.analysis.align import _fit_to
Expand Down Expand Up @@ -355,12 +356,12 @@ def n_components(self, n):
n = len(self._variance)
self.results.variance = self._variance[:n]
self.results.cumulated_variance = (np.cumsum(self._variance) /
np.sum(self._variance))[:n]
np.sum(self._variance))[:n]
orbeckst marked this conversation as resolved.
Show resolved Hide resolved
self.results.p_components = self._p_components[:, :n]
self._n_components = n

def transform(self, atomgroup, n_components=None, start=None, stop=None,
step=None):
step=None, verbose=False):
"""Apply the dimensionality reduction on a trajectory

Parameters
Expand All @@ -382,6 +383,11 @@ def transform(self, atomgroup, n_components=None, start=None, stop=None,
Include every `step` frames in the PCA transform. If set to
``None`` (the default) then every frame is analyzed (i.e., same as
``step=1``).
verbose : bool, optional
``verbose = True`` option displays a progress bar for the
iterations of transform. ``verbose = False`` disables the
progress bar, just returns the pca_space array when the
calculations are finished.

Returns
-------
Expand All @@ -391,14 +397,17 @@ def transform(self, atomgroup, n_components=None, start=None, stop=None,
.. versionchanged:: 0.19.0
Transform now requires that :meth:`run` has been called before,
otherwise a :exc:`ValueError` is raised.
.. versionchanged:: 2.8.0
Transform now has shows a tqdm progressbar, which can be toggled
on with ``verbose = True``, or off with ``verbose = False``
"""
if not self._calculated:
raise ValueError('Call run() on the PCA before using transform')

if isinstance(atomgroup, Universe):
atomgroup = atomgroup.atoms

if(self._n_atoms != atomgroup.n_atoms):
if self._n_atoms != atomgroup.n_atoms:
raise ValueError('PCA has been fit for'
'{} atoms. Your atomgroup'
'has {} atoms'.format(self._n_atoms,
Expand All @@ -415,7 +424,10 @@ def transform(self, atomgroup, n_components=None, start=None, stop=None,

dot = np.zeros((n_frames, dim))

for i, ts in enumerate(traj[start:stop:step]):
for i, ts in tqdm(enumerate(traj[start:stop:step]), disable=not verbose,
total=len(traj[start:stop:step])
):

xyz = atomgroup.positions.ravel() - self._xmean
dot[i] = np.dot(xyz, self._p_components[:, :dim])

Expand Down Expand Up @@ -561,7 +573,7 @@ def project_single_frame(self, components=None, group=None, anchor=None):
for res in group.residues:
# n_common is the number of pca atoms in a residue
n_common = pca_res_counts[np.where(
pca_res_indices == res.resindex)][0]
pca_res_indices == res.resindex)][0]
non_pca_atoms = np.append(non_pca_atoms,
res.atoms.n_atoms - n_common)
# index_extrapolate records the anchor number for each non-PCA atom
Expand Down
5 changes: 4 additions & 1 deletion package/MDAnalysis/lib/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ def create(logger_name="MDAnalysis", logfile="MDAnalysis.log"):

# handler that writes to logfile
logfile_handler = logging.FileHandler(logfile)
logfile_formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
logfile_formatter = logging.Formatter(
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
logfile_handler.setFormatter(logfile_formatter)
logger.addHandler(logfile_handler)

Expand Down Expand Up @@ -172,6 +173,7 @@ class NullHandler(logging.Handler):
see the advice on logging and libraries in
http://docs.python.org/library/logging.html?#configuring-logging-for-a-library
"""

def emit(self, record):
pass

Expand Down Expand Up @@ -327,6 +329,7 @@ class ProgressBar(tqdm):
.. _`tqdm documentation`: https://tqdm.github.io/

"""

def __init__(self, *args, **kwargs):
""""""
# ^^^^ keep the empty doc string to avoid Sphinx doc errors with the
Expand Down
Loading