Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ Chronological list of authors
- Sumit Saraswat
- Carlos Vega
- Erik Fransson
- Jonathan Berg

External code
-------------
Expand Down
5 changes: 4 additions & 1 deletion package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ The rules for this file:

-------------------------------------------------------------------------------
??/??/?? IAlibay, sumitsaraswat362, BradyAJohnston, carlosvega, erikfransson,
orbeckst
orbeckst, jberg5

* 2.12.0

Fixes
* PCA now uses `eigh`, which fixes complex-valued `variance` and `p_components`
on numpy 2.5 and later (issue #5351). It also lowers memory use and
speeds up `run()` and `transform()`. Eigenvector signs may differ.
* reduced resident memory (RSS) of the NCDF (Amber) file reader when used with
`mmap=True` (default for files on disk): on Linux, it no longer grows the RSS
towards the size of the whole trajectory file. Not changed on macOS (where
Expand Down
8 changes: 4 additions & 4 deletions package/MDAnalysis/analysis/pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,10 @@ def _single_frame(self):

def _conclude(self):
self.cov /= self.n_frames - 1
e_vals, e_vects = np.linalg.eig(self.cov)
sort_idx = np.argsort(e_vals)[::-1]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ok, I just checked that eigh() since at least 1.26 returns evals in sorted order (unlike eig)

self._variance = e_vals[sort_idx]
self._p_components = e_vects[:, sort_idx]
e_vals, e_vects = np.linalg.eigh(self.cov)
self._variance = e_vals[::-1]
# f-order helps performance of downstream matmuls
self._p_components = np.asfortranarray(e_vects[:, ::-1])
self._calculated = True
self.n_components = self._n_components

Expand Down
6 changes: 6 additions & 0 deletions testsuite/MDAnalysisTests/analysis/test_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ def test_pcs(pca):
)


def test_pca_results_are_real_valued(pca):
assert np.isrealobj(pca.results.variance)
assert np.isrealobj(pca.results.cumulated_variance)
assert np.isrealobj(pca.results.p_components)


def test_pcs_n_components(u):
pca = PCA(u, select=SELECTION).run()
assert_equal(pca.n_components, pca._n_atoms * 3)
Expand Down
Loading