Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jinzhe Zeng <[email protected]>
  • Loading branch information
njzjz committed Feb 28, 2024
1 parent 9a7ff32 commit ced2a3a
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 11 deletions.
4 changes: 2 additions & 2 deletions deepmd/pt/model/task/ener.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
to_numpy_array,
)
from deepmd.utils.out_stat import (

Check warning on line 34 in deepmd/pt/model/task/ener.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/task/ener.py#L34

Added line #L34 was not covered by tests
compute_bias_from_redu,
compute_stats_from_redu,
)
from deepmd.utils.path import (
DPPath,
Expand Down Expand Up @@ -160,7 +160,7 @@ def compute_output_stats(self, merged, stat_file_path: Optional[DPPath] = None):
)
else:
assigned_atom_ener = None
bias_atom_e, _ = compute_bias_from_redu(
bias_atom_e, _ = compute_stats_from_redu(

Check warning on line 163 in deepmd/pt/model/task/ener.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/task/ener.py#L162-L163

Added lines #L162 - L163 were not covered by tests
merged_energy,
merged_natoms,
assigned_bias=assigned_atom_ener,
Expand Down
4 changes: 2 additions & 2 deletions deepmd/tf/fit/dos.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
one_layer_rand_seed_shift,
)
from deepmd.utils.out_stat import (
compute_bias_from_redu,
compute_stats_from_redu,
)

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -228,7 +228,7 @@ def _compute_output_stats(self, all_stat, rcond=1e-3, mixed_type=False):
sys_tynatom = np.reshape(sys_tynatom, [nsys, -1])
sys_tynatom = sys_tynatom[:, 2:]

dos_shift, _ = compute_bias_from_redu(
dos_shift, _ = compute_stats_from_redu(

Check warning on line 231 in deepmd/tf/fit/dos.py

View check run for this annotation

Codecov / codecov/patch

deepmd/tf/fit/dos.py#L231

Added line #L231 was not covered by tests
sys_dos,
sys_tynatom,
rcond=rcond,
Expand Down
4 changes: 2 additions & 2 deletions deepmd/tf/fit/ener.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
Spin,
)
from deepmd.utils.out_stat import (
compute_bias_from_redu,
compute_stats_from_redu,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -299,7 +299,7 @@ def _compute_output_stats(self, all_stat, rcond=1e-3, mixed_type=False):
)
else:
assigned_atom_ener = None
energy_shift, _ = compute_bias_from_redu(
energy_shift, _ = compute_stats_from_redu(

Check warning on line 302 in deepmd/tf/fit/ener.py

View check run for this annotation

Codecov / codecov/patch

deepmd/tf/fit/ener.py#L301-L302

Added lines #L301 - L302 were not covered by tests
sys_ener.reshape(-1, 1),
sys_tynatom,
assigned_bias=assigned_atom_ener,
Expand Down
4 changes: 2 additions & 2 deletions deepmd/utils/data_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
DeepmdData,
)
from deepmd.utils.out_stat import (
compute_bias_from_redu,
compute_stats_from_redu,
)

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -251,7 +251,7 @@ def compute_energy_shift(self, rcond=None, key="energy"):
sys_tynatom = np.array(self.natoms_vec, dtype=GLOBAL_NP_FLOAT_PRECISION)
sys_tynatom = np.reshape(sys_tynatom, [self.nsystems, -1])
sys_tynatom = sys_tynatom[:, 2:]
energy_shift, _ = compute_bias_from_redu(
energy_shift, _ = compute_stats_from_redu(

Check warning on line 254 in deepmd/utils/data_system.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/data_system.py#L254

Added line #L254 was not covered by tests
sys_ener.reshape(-1, 1),
sys_tynatom,
rcond=rcond,
Expand Down
2 changes: 1 addition & 1 deletion deepmd/utils/out_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import numpy as np


def compute_bias_from_redu(
def compute_stats_from_redu(
output_redu: np.ndarray,
natoms: np.ndarray,
assigned_bias: Optional[np.ndarray] = None,
Expand Down
81 changes: 81 additions & 0 deletions source/tests/common/test_out_stat.py
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,
)
4 changes: 2 additions & 2 deletions source/tests/tf/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)
from deepmd.tf.utils import random as dp_random
from deepmd.utils.out_stat import (
compute_bias_from_redu,
compute_stats_from_redu,
)

if GLOBAL_NP_FLOAT_PRECISION == np.float32:
Expand Down Expand Up @@ -1044,7 +1044,7 @@ def compute_energy_shift(self):
sys_tynatom = np.array(self.natoms_vec, dtype=GLOBAL_NP_FLOAT_PRECISION)
sys_tynatom = np.reshape(sys_tynatom, [self.nsystems, -1])
sys_tynatom = sys_tynatom[:, 2:]
energy_shift, _ = compute_bias_from_redu(
energy_shift, _ = compute_stats_from_redu(
sys_ener.reshape(-1, 1),
sys_tynatom,
rcond=None,
Expand Down

0 comments on commit ced2a3a

Please sign in to comment.