diff --git a/package/AUTHORS b/package/AUTHORS index bd979f62be..16dbaea475 100644 --- a/package/AUTHORS +++ b/package/AUTHORS @@ -289,6 +289,7 @@ Chronological list of authors - Sumit Saraswat - Carlos Vega - Erik Fransson + - Jonathan Berg External code ------------- diff --git a/package/CHANGELOG b/package/CHANGELOG index 7297dcefe6..7ac52541c0 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -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 diff --git a/package/MDAnalysis/analysis/pca.py b/package/MDAnalysis/analysis/pca.py index fddbf7d009..e208494e0d 100644 --- a/package/MDAnalysis/analysis/pca.py +++ b/package/MDAnalysis/analysis/pca.py @@ -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] - 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 diff --git a/testsuite/MDAnalysisTests/analysis/test_pca.py b/testsuite/MDAnalysisTests/analysis/test_pca.py index 663aa5e68f..99c8c2f9f3 100644 --- a/testsuite/MDAnalysisTests/analysis/test_pca.py +++ b/testsuite/MDAnalysisTests/analysis/test_pca.py @@ -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)