-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jinzhe Zeng <[email protected]>
- Loading branch information
Showing
7 changed files
with
92 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
import unittest | ||
|
||
import numpy as np | ||
|
||
from deepmd.utils.out_stat import ( | ||
compute_stats_from_atomic, | ||
compute_stats_from_redu, | ||
) | ||
|
||
|
||
class TestOutStat(unittest.TestCase): | ||
def setUp(self) -> None: | ||
rng = np.random.default_rng(20240227) | ||
ndim = 5 | ||
nframes = 1000 | ||
ntypes = 3 | ||
nloc = 1000 | ||
self.atype = rng.integers(0, ntypes, size=(nframes, nloc)) | ||
# compute the number of atoms for each type in each frame | ||
self.natoms = np.zeros((nframes, ntypes), dtype=np.int64) | ||
for i in range(ntypes): | ||
self.natoms[:, i] = (self.atype == i).sum(axis=1) | ||
self.mean = rng.random((ntypes, ndim)) * 1e4 | ||
self.std = rng.random((ntypes, ndim)) * 1e-3 | ||
|
||
# generate random output | ||
self.output = rng.normal( | ||
loc=self.mean[self.atype, :], | ||
scale=self.std[self.atype, :], | ||
size=(nframes, nloc, ndim), | ||
) | ||
self.output_redu = self.output.sum(axis=1) | ||
|
||
return super().setUp() | ||
|
||
def test_compute_stats_from_redu(self): | ||
bias, std = compute_stats_from_redu(self.output_redu, self.natoms) | ||
np.testing.assert_allclose(bias, self.mean, rtol=1e-7) | ||
np.testing.assert_allclose( | ||
std, | ||
np.sqrt(self.natoms.mean(axis=0) @ np.square(self.std)), | ||
rtol=1e-1, | ||
) | ||
# ensure the sum is close | ||
np.testing.assert_allclose( | ||
self.output_redu, | ||
self.natoms @ bias, | ||
rtol=1e-7, | ||
) | ||
|
||
def test_compute_stats_from_redu_with_assigned_bias(self): | ||
assigned_bias = np.full_like(self.mean, np.nan) | ||
assigned_bias[0] = self.mean[0] | ||
bias, std = compute_stats_from_redu( | ||
self.output_redu, | ||
self.natoms, | ||
assigned_bias=assigned_bias, | ||
) | ||
np.testing.assert_allclose(bias, self.mean, rtol=1e-7) | ||
np.testing.assert_allclose(bias[0], self.mean[0], rtol=1e-14) | ||
np.testing.assert_allclose( | ||
std, | ||
np.sqrt(self.natoms.mean(axis=0) @ np.square(self.std)), | ||
rtol=1e-1, | ||
) | ||
# ensure the sum is close | ||
np.testing.assert_allclose( | ||
self.output_redu, | ||
self.natoms @ bias, | ||
rtol=1e-7, | ||
) | ||
|
||
def test_compute_stats_from_atomic(self): | ||
bias, std = compute_stats_from_atomic(self.output, self.atype) | ||
np.testing.assert_allclose(bias, self.mean) | ||
np.testing.assert_allclose( | ||
std, | ||
self.std, | ||
rtol=1e-2, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters