Ejemplo n.º 1
0
 def test_init_type_map(self):
     dd = DeepmdData(self.data_name, type_map=['bar', 'foo', 'tar'])
     self.assertEqual(dd.idx_map[0], 0)
     self.assertEqual(dd.idx_map[1], 1)
     self.assertEqual(dd.atom_type[0], 0)
     self.assertEqual(dd.atom_type[1], 1)
     self.assertEqual(dd.type_map, ['bar', 'foo', 'tar'])
Ejemplo n.º 2
0
 def test_init (self) :
     dd = DeepmdData(self.data_name)
     self.assertEqual(dd.idx_map[0], 1)
     self.assertEqual(dd.idx_map[1], 0)
     self.assertEqual(dd.type_map, ['foo', 'bar'])
     self.assertEqual(dd.test_dir, 'test_data/set.tar')
     self.assertEqual(dd.train_dirs, ['test_data/set.bar', 'test_data/set.foo'])
Ejemplo n.º 3
0
 def test_avg(self):
     dd = DeepmdData(self.data_name)\
          .add('test_frame', 5, atomic=False, must=True)
     favg = dd.avg('test_frame')
     fcmp = np.average(np.concatenate(
         (self.test_frame, self.test_frame_bar), axis=0),
                       axis=0)
     np.testing.assert_almost_equal(favg, fcmp, places)
Ejemplo n.º 4
0
 def test_reduce(self):
     dd = DeepmdData(self.data_name)\
          .add('test_atomic', 7, atomic=True, must=True)
     dd.reduce('redu', 'test_atomic')
     data = dd._load_set(os.path.join(self.data_name, 'set.foo'))
     self.assertEqual(data['find_test_atomic'], 1)
     self._comp_np_mat2(data['test_atomic'], self.test_atomic)
     self.assertEqual(data['find_redu'], 1)
     self._comp_np_mat2(data['redu'], self.redu_atomic)
Ejemplo n.º 5
0
 def test_reduce_null(self) :
     dd = DeepmdData(self.data_name)\
          .add('test_atomic_1', 7, atomic=True, must=False)
     dd.reduce('redu', 'test_atomic_1')
     data = dd._load_set(os.path.join(self.data_name, 'set.foo'))
     self.assertEqual(data['find_test_atomic_1'], 0)
     self._comp_np_mat2(data['test_atomic_1'], np.zeros([self.nframes, self.natoms * 7]))
     self.assertEqual(data['find_redu'], 0)
     self._comp_np_mat2(data['redu'], np.zeros([self.nframes, 7]))
Ejemplo n.º 6
0
 def test_shuffle(self):
     dd = DeepmdData(self.data_name)\
          .add('test_atomic', 7, atomic=True, must=True)\
          .add('test_frame', 5, atomic=False, must=True)
     data = dd._load_set(os.path.join(self.data_name, 'set.foo'))
     data_bk = copy.deepcopy(data)
     data, idx = dd._shuffle_data(data)
     self._comp_np_mat2(data_bk['coord'][idx, :], data['coord'])
     self._comp_np_mat2(data_bk['test_atomic'][idx, :], data['test_atomic'])
     self._comp_np_mat2(data_bk['test_frame'][idx, :], data['test_frame'])
Ejemplo n.º 7
0
 def test_get_batch(self):
     dd = DeepmdData(self.data_name)
     data = dd.get_batch(5)
     self._comp_np_mat2(np.sort(data['coord'], axis=0),
                        np.sort(self.coord_bar, axis=0))
     data = dd.get_batch(5)
     self._comp_np_mat2(np.sort(data['coord'], axis=0),
                        np.sort(self.coord, axis=0))
     data = dd.get_batch(5)
     self._comp_np_mat2(np.sort(data['coord'], axis=0),
                        np.sort(self.coord_bar, axis=0))
     data = dd.get_batch(5)
     self._comp_np_mat2(np.sort(data['coord'], axis=0),
                        np.sort(self.coord, axis=0))
Ejemplo n.º 8
0
 def test_load_set(self):
     dd = DeepmdData(self.data_name)\
          .add('test_atomic', 7, atomic=True, must=True)\
          .add('test_frame', 5, atomic=False, must=True)\
          .add('test_null', 2, atomic=True, must=False)
     data = dd._load_set(os.path.join(self.data_name, 'set.foo'))
     nframes = data['coord'].shape[0]
     self.assertEqual(dd.get_numb_set(), 2)
     self.assertEqual(dd.get_type_map(), ['foo', 'bar'])
     self.assertEqual(dd.get_natoms(), 2)
     self.assertEqual(list(dd.get_natoms_vec(3)), [2, 2, 1, 1, 0])
     for ii in range(nframes):
         self.assertEqual(data['type'][ii][0], 0)
         self.assertEqual(data['type'][ii][1], 1)
     self.assertEqual(data['find_coord'], 1)
     self._comp_np_mat2(data['coord'], self.coord)
     self.assertEqual(data['find_test_atomic'], 1)
     self._comp_np_mat2(data['test_atomic'], self.test_atomic)
     self.assertEqual(data['find_test_frame'], 1)
     self._comp_np_mat2(data['test_frame'], self.test_frame)
     self.assertEqual(data['find_test_null'], 0)
     self._comp_np_mat2(data['test_null'], self.test_null)
Ejemplo n.º 9
0
 def test_load_set_2(self):
     dd = DeepmdData(self.data_name)\
          .add('value_2', 1, atomic=True, must=True, type_sel = [1])
     data = dd._load_set(os.path.join(self.data_name, 'set.foo'))
     self.assertEqual(data['value_2'].shape, (self.nframes, 4))
     np.testing.assert_almost_equal(data['value_2'], self.value_2)
Ejemplo n.º 10
0
 def test_get_nbatch(self):
     dd = DeepmdData(self.data_name)
     nb = dd.get_numb_batch(1, 0)
     self.assertEqual(nb, 5)
     nb = dd.get_numb_batch(2, 0)
     self.assertEqual(nb, 2)
Ejemplo n.º 11
0
 def test_check_test_size(self):
     dd = DeepmdData(self.data_name)
     ret = dd.check_test_size(10)
     self.assertEqual(ret, (os.path.join(self.data_name, 'set.tar'), 2))
     ret = dd.check_test_size(2)
     self.assertEqual(ret, None)
Ejemplo n.º 12
0
 def test_check_batch_size(self):
     dd = DeepmdData(self.data_name)
     ret = dd.check_batch_size(10)
     self.assertEqual(ret, (os.path.join(self.data_name, 'set.bar'), 5))
     ret = dd.check_batch_size(5)
     self.assertEqual(ret, None)
Ejemplo n.º 13
0
 def test_load_null_must(self):
     dd = DeepmdData(self.data_name)\
          .add('test_atomic_1', 7, atomic=True, must=True)
     with self.assertRaises(RuntimeError):
         data = dd._load_set(os.path.join(self.data_name, 'set.foo'))
Ejemplo n.º 14
0
 def test_get_batch(self) :
     dd = DeepmdData(self.data_name)
     data = dd.get_batch(5)
Ejemplo n.º 15
0
 def test_init (self) :
     dd = DeepmdData(self.data_name)
     self.assertEqual(dd.idx_map[0], 0)
     self.assertEqual(dd.type_map, ['X'])
     self.assertEqual(dd.test_dir, self.data_name + '#/set.000')
     self.assertEqual(dd.train_dirs, [self.data_name + '#/set.000'])
Ejemplo n.º 16
0
def test(
    *,
    model: str,
    system: str,
    set_prefix: str,
    numb_test: int,
    rand_seed: Optional[int],
    shuffle_test: bool,
    detail_file: str,
    atomic: bool,
    **kwargs,
):
    """Test model predictions.

    Parameters
    ----------
    model : str
        path where model is stored
    system : str
        system directory
    set_prefix : str
        string prefix of set
    numb_test : int
        munber of tests to do
    rand_seed : Optional[int]
        seed for random generator
    shuffle_test : bool
        whether to shuffle tests
    detail_file : Optional[str]
        file where test details will be output
    atomic : bool
        whether per atom quantities should be computed

    Raises
    ------
    RuntimeError
        if no valid system was found
    """
    all_sys = expand_sys_str(system)
    if len(all_sys) == 0:
        raise RuntimeError("Did not find valid system")
    err_coll = []
    siz_coll = []

    # init random seed
    if rand_seed is not None:
        dp_random.seed(rand_seed % (2 ** 32))

    # init model
    dp = DeepPotential(model)

    for cc, system in enumerate(all_sys):
        log.info("# ---------------output of dp test--------------- ")
        log.info(f"# testing system : {system}")

        # create data class
        tmap = dp.get_type_map() if dp.model_type == "ener" else None
        data = DeepmdData(system, set_prefix, shuffle_test=shuffle_test, type_map=tmap)

        if dp.model_type == "ener":
            err = test_ener(
                dp,
                data,
                system,
                numb_test,
                detail_file,
                atomic,
                append_detail=(cc != 0),
            )
        elif dp.model_type == "dipole":
            err = test_dipole(dp, data, numb_test, detail_file, atomic)
        elif dp.model_type == "polar":
            err = test_polar(dp, data, numb_test, detail_file, atomic=atomic)
        elif dp.model_type == "global_polar":   # should not appear in this new version
            log.warning("Global polar model is not currently supported. Please directly use the polar mode and change loss parameters.")
            err = test_polar(dp, data, numb_test, detail_file, atomic=False)    # YWolfeee: downward compatibility
        log.info("# ----------------------------------------------- ")
        err_coll.append(err)

    avg_err = weighted_average(err_coll)

    if len(all_sys) != len(err_coll):
        log.warning("Not all systems are tested! Check if the systems are valid")

    if len(all_sys) > 1:
        log.info("# ----------weighted average of errors----------- ")
        log.info(f"# number of systems : {len(all_sys)}")
        if dp.model_type == "ener":
            print_ener_sys_avg(avg_err)
        elif dp.model_type == "dipole":
            print_dipole_sys_avg(avg_err)
        elif dp.model_type == "polar":
            print_polar_sys_avg(avg_err)
        elif dp.model_type == "global_polar":
            print_polar_sys_avg(avg_err)
        elif dp.model_type == "wfc":
            print_wfc_sys_avg(avg_err)
        log.info("# ----------------------------------------------- ")