def test_polar (args) : if args.rand_seed is not None : np.random.seed(args.rand_seed % (2**32)) dp = DeepPolar(args.model) data = DeepmdData(args.system, args.set_prefix, shuffle_test = args.shuffle_test) data.add('polarizability', 9, atomic=True, must=True, high_prec=False, type_sel = dp.get_sel_type()) test_data = data.get_test () numb_test = args.numb_test natoms = len(test_data["type"][0]) nframes = test_data["box"].shape[0] numb_test = min(nframes, numb_test) coord = test_data["coord"][:numb_test].reshape([numb_test, -1]) box = test_data["box"][:numb_test] atype = test_data["type"][0] polar = dp.eval(coord, box, atype) polar = polar.reshape([numb_test,-1]) l2f = (l2err (polar - test_data["polarizability"] [:numb_test])) print ("# number of test data : %d " % numb_test) print ("Polarizability L2err : %e eV/A" % l2f) detail_file = args.detail_file if detail_file is not None : pe = np.concatenate((np.reshape(test_data["polarizability"][:numb_test], [-1,9]), np.reshape(polar, [-1,9])), axis = 1) np.savetxt(detail_file+".out", pe, header = 'data_pxx data_pxy data_pxz data_pyx data_pyy data_pyz data_pzx data_pzy data_pzz pred_pxx pred_pxy pred_pxz pred_pyx pred_pyy pred_pyz pred_pzx pred_pzy pred_pzz')
def test_dipole (args) : if args.rand_seed is not None : np.random.seed(args.rand_seed % (2**32)) dp = DeepDipole(args.model) data = DeepmdData(args.system, args.set_prefix, shuffle_test = args.shuffle_test) data.add('dipole', 3, atomic=True, must=True, high_prec=False, type_sel = dp.get_sel_type()) test_data = data.get_test () numb_test = args.numb_test natoms = len(test_data["type"][0]) nframes = test_data["box"].shape[0] numb_test = min(nframes, numb_test) coord = test_data["coord"][:numb_test].reshape([numb_test, -1]) box = test_data["box"][:numb_test] atype = test_data["type"][0] dipole = dp.eval(coord, box, atype) dipole = dipole.reshape([numb_test,-1]) l2f = (l2err (dipole - test_data["dipole"] [:numb_test])) print ("# number of test data : %d " % numb_test) print ("Dipole L2err : %e eV/A" % l2f) detail_file = args.detail_file if detail_file is not None : pe = np.concatenate((np.reshape(test_data["dipole"][:numb_test], [-1,3]), np.reshape(dipole, [-1,3])), axis = 1) np.savetxt(detail_file+".out", pe, header = 'data_x data_y data_z pred_x pred_y pred_z')
def test_wfc (args) : if args.rand_seed is not None : np.random.seed(args.rand_seed % (2**32)) dp = DeepWFC(args.model) data = DeepmdData(args.system, args.set_prefix, shuffle_test = args.shuffle_test) data.add('wfc', 12, atomic=True, must=True, high_prec=False, type_sel = dp.get_sel_type()) test_data = data.get_test () numb_test = args.numb_test natoms = len(test_data["type"][0]) nframes = test_data["box"].shape[0] numb_test = min(nframes, numb_test) coord = test_data["coord"][:numb_test].reshape([numb_test, -1]) box = test_data["box"][:numb_test] atype = test_data["type"][0] wfc = dp.eval(coord, box, atype) wfc = wfc.reshape([numb_test,-1]) l2f = (l2err (wfc - test_data["wfc"] [:numb_test])) print ("# number of test data : %d " % numb_test) print ("WFC L2err : %e eV/A" % l2f) detail_file = args.detail_file if detail_file is not None : pe = np.concatenate((np.reshape(test_data["wfc"][:numb_test], [-1,12]), np.reshape(wfc, [-1,12])), axis = 1) np.savetxt(detail_file+".out", pe, header = 'ref_wfc(12 dofs) predicted_wfc(12 dofs)')
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) for ii in range(favg.size): self.assertAlmostEqual((favg[ii]), (fcmp[ii]), places=places)
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)
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)) for ii in range(self.nframes): for jj in range(4): self.assertAlmostEqual(data['value_2'][ii][jj], self.value_2[ii][jj])
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]))
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'])
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'])
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'])
def test_polar(dp, args, global_polar=False): if args.rand_seed is not None: np.random.seed(args.rand_seed % (2**32)) data = DeepmdData(args.system, args.set_prefix, shuffle_test=args.shuffle_test) if not global_polar: data.add('polarizability', 9, atomic=True, must=True, high_prec=False, type_sel=dp.get_sel_type()) else: data.add('polarizability', 9, atomic=False, must=True, high_prec=False, type_sel=dp.get_sel_type()) test_data = data.get_test() numb_test = args.numb_test natoms = len(test_data["type"][0]) nframes = test_data["box"].shape[0] numb_test = min(nframes, numb_test) coord = test_data["coord"][:numb_test].reshape([numb_test, -1]) box = test_data["box"][:numb_test] atype = test_data["type"][0] polar = dp.eval(coord, box, atype) sel_type = dp.get_sel_type() sel_natoms = 0 for ii in sel_type: sel_natoms += sum(atype == ii) polar = polar.reshape([numb_test, -1]) l2f = (l2err(polar - test_data["polarizability"][:numb_test])) l2fs = l2f / np.sqrt(sel_natoms) l2fa = l2f / sel_natoms print("# number of test data : %d " % numb_test) print("Polarizability L2err : %e eV/A" % l2f) if global_polar: print("Polarizability L2err/sqrtN : %e eV/A" % l2fs) print("Polarizability L2err/N : %e eV/A" % l2fa) detail_file = args.detail_file if detail_file is not None: pe = np.concatenate( (np.reshape(test_data["polarizability"][:numb_test], [-1, 9]), np.reshape(polar, [-1, 9])), axis=1) np.savetxt( detail_file + ".out", pe, header= 'data_pxx data_pxy data_pxz data_pyx data_pyy data_pyz data_pzx data_pzy data_pzz pred_pxx pred_pxy pred_pxz pred_pyx pred_pyy pred_pyz pred_pzx pred_pzy pred_pzz' ) return [l2f], [polar.size]
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))
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)
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'))
def __init__(self, systems, batch_size, test_size, rcut, set_prefix='set', shuffle_test=True, type_map=None, modifier=None): # init data self.rcut = rcut self.system_dirs = systems self.nsystems = len(self.system_dirs) self.data_systems = [] for ii in self.system_dirs: self.data_systems.append( DeepmdData(ii, set_prefix=set_prefix, shuffle_test=shuffle_test, type_map=type_map, modifier=modifier)) # batch size self.batch_size = batch_size if isinstance(self.batch_size, int): self.batch_size = self.batch_size * np.ones(self.nsystems, dtype=int) elif isinstance(self.batch_size, str): words = self.batch_size.split(':') if 'auto' == words[0]: rule = 32 if len(words) == 2: rule = int(words[1]) else: raise RuntimeError('unknown batch_size rule ' + words[0]) self.batch_size = self._make_auto_bs(rule) elif isinstance(self.batch_size, list): pass else: raise RuntimeError('invalid batch_size') assert (isinstance(self.batch_size, (list, np.ndarray))) assert (len(self.batch_size) == self.nsystems) # natoms, nbatches ntypes = [] for ii in self.data_systems: ntypes.append(ii.get_ntypes()) self.sys_ntypes = max(ntypes) self.natoms = [] self.natoms_vec = [] self.nbatches = [] type_map_list = [] for ii in range(self.nsystems): self.natoms.append(self.data_systems[ii].get_natoms()) self.natoms_vec.append(self.data_systems[ii].get_natoms_vec( self.sys_ntypes).astype(int)) self.nbatches.append(self.data_systems[ii].get_sys_numb_batch( self.batch_size[ii])) type_map_list.append(self.data_systems[ii].get_type_map()) self.type_map = self._check_type_map_consistency(type_map_list) # prob of batch, init pick idx self.prob_nbatches = [float(i) for i in self.nbatches] / np.sum(self.nbatches) self.pick_idx = 0 # check batch and test size for ii in range(self.nsystems): chk_ret = self.data_systems[ii].check_batch_size( self.batch_size[ii]) if chk_ret is not None: warnings.warn("system %s required batch size is larger than the size of the dataset %s (%d > %d)" % \ (self.system_dirs[ii], chk_ret[0], self.batch_size[ii], chk_ret[1])) chk_ret = self.data_systems[ii].check_test_size(test_size) if chk_ret is not None: warnings.warn("system %s required test size is larger than the size of the dataset %s (%d > %d)" % \ (self.system_dirs[ii], chk_ret[0], test_size, chk_ret[1]))
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)
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)
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)
sum_err[ii] += ee * ee * ss sum_siz[ii] += ss for ii in range(nitems): sum_err[ii] = np.sqrt(sum_err[ii] / sum_siz[ii]) return sum_err def test_ener (dp, args) : if args.rand_seed is not None : np.random.seed(args.rand_seed % (2**32)) <<<<<<< HEAD dp = DeepPot(args.model) ======= >>>>>>> 93aa90768089e265465f93fb3d9f99b494d56b09 data = DeepmdData(args.system, args.set_prefix, shuffle_test = args.shuffle_test, type_map = dp.get_type_map()) data.add('energy', 1, atomic=False, must=False, high_prec=True) data.add('force', 3, atomic=True, must=False, high_prec=False) data.add('virial', 9, atomic=False, must=False, high_prec=False) if dp.get_dim_fparam() > 0: data.add('fparam', dp.get_dim_fparam(), atomic=False, must=True, high_prec=False) if dp.get_dim_aparam() > 0: data.add('aparam', dp.get_dim_aparam(), atomic=True, must=True, high_prec=False) test_data = data.get_test () natoms = len(test_data["type"][0]) nframes = test_data["box"].shape[0] numb_test = args.numb_test numb_test = min(nframes, numb_test) <<<<<<< HEAD =======
def __init__(self, systems, batch_size, test_size, rcut, set_prefix='set', shuffle_test=True, type_map=None, modifier=None): # init data self.rcut = rcut self.system_dirs = systems self.nsystems = len(self.system_dirs) self.data_systems = [] for ii in self.system_dirs: self.data_systems.append( DeepmdData(ii, set_prefix=set_prefix, shuffle_test=shuffle_test, type_map=type_map, modifier=modifier)) # batch size self.batch_size = batch_size if isinstance(self.batch_size, int): self.batch_size = self.batch_size * np.ones(self.nsystems, dtype=int) elif isinstance(self.batch_size, str): words = self.batch_size.split(':') if 'auto' == words[0]: rule = 32 if len(words) == 2: rule = int(words[1]) else: raise RuntimeError('unknown batch_size rule ' + words[0]) self.batch_size = self._make_auto_bs(rule) elif isinstance(self.batch_size, list): pass else: raise RuntimeError('invalid batch_size') assert (isinstance(self.batch_size, (list, np.ndarray))) assert (len(self.batch_size) == self.nsystems) # natoms, nbatches ntypes = [] for ii in self.data_systems: ntypes.append(ii.get_ntypes()) self.sys_ntypes = max(ntypes) self.natoms = [] self.natoms_vec = [] self.nbatches = [] type_map_list = [] for ii in range(self.nsystems): self.natoms.append(self.data_systems[ii].get_natoms()) self.natoms_vec.append(self.data_systems[ii].get_natoms_vec( self.sys_ntypes).astype(int)) self.nbatches.append(self.data_systems[ii].get_sys_numb_batch( self.batch_size[ii])) type_map_list.append(self.data_systems[ii].get_type_map()) self.type_map = self._check_type_map_consistency(type_map_list) # ! altered by Marián Rynik # test size # now test size can be set as a percentage of systems data or test size # can be set for each system individualy in the same manner as batch # size. This enables one to use systems with diverse number of # structures and different number of atoms. self.test_size = test_size if isinstance(self.test_size, int): self.test_size = self.test_size * np.ones(self.nsystems, dtype=int) elif isinstance(self.test_size, str): words = self.test_size.split('%') try: percent = int(words[0]) except ValueError: raise RuntimeError('unknown test_size rule ' + words[0]) self.test_size = self._make_auto_ts(percent) elif isinstance(self.test_size, list): pass else: raise RuntimeError('invalid test_size') assert (isinstance(self.test_size, (list, np.ndarray))) assert (len(self.test_size) == self.nsystems) # prob of batch, init pick idx self.prob_nbatches = [float(i) for i in self.nbatches] / np.sum(self.nbatches) self.pick_idx = 0 # check batch and test size for ii in range(self.nsystems): chk_ret = self.data_systems[ii].check_batch_size( self.batch_size[ii]) if chk_ret is not None: warnings.warn("system %s required batch size is larger than the size of the dataset %s (%d > %d)" % \ (self.system_dirs[ii], chk_ret[0], self.batch_size[ii], chk_ret[1])) chk_ret = self.data_systems[ii].check_test_size(self.test_size[ii]) if chk_ret is not None: warnings.warn("system %s required test size is larger than the size of the dataset %s (%d > %d)" % \ (self.system_dirs[ii], chk_ret[0], self.test_size[ii], chk_ret[1]))
def test_ener(args): if args.rand_seed is not None: np.random.seed(args.rand_seed % (2**32)) dp = DeepPot(args.model) data = DeepmdData(args.system, args.set_prefix, shuffle_test=args.shuffle_test, type_map=dp.get_type_map()) data.add('energy', 1, atomic=False, must=False, high_prec=True) data.add('force', 3, atomic=True, must=False, high_prec=False) data.add('virial', 9, atomic=False, must=False, high_prec=False) if dp.get_dim_fparam() > 0: data.add('fparam', dp.get_dim_fparam(), atomic=False, must=True, high_prec=False) if dp.get_dim_aparam() > 0: data.add('aparam', dp.get_dim_aparam(), atomic=True, must=True, high_prec=False) test_data = data.get_test() natoms = len(test_data["type"][0]) nframes = test_data["box"].shape[0] numb_test = args.numb_test numb_test = min(nframes, numb_test) coord = test_data["coord"][:numb_test].reshape([numb_test, -1]) box = test_data["box"][:numb_test] atype = test_data["type"][0] if dp.get_dim_fparam() > 0: fparam = test_data["fparam"][:numb_test] else: fparam = None if dp.get_dim_aparam() > 0: aparam = test_data["aparam"][:numb_test] else: aparam = None energy, force, virial, ae, av = dp.eval(coord, box, atype, fparam=fparam, aparam=aparam, atomic=True) energy = energy.reshape([numb_test, 1]) force = force.reshape([numb_test, -1]) virial = virial.reshape([numb_test, 9]) ae = ae.reshape([numb_test, -1]) av = av.reshape([numb_test, -1]) l2e = (l2err(energy - test_data["energy"][:numb_test].reshape([-1, 1]))) l2f = (l2err(force - test_data["force"][:numb_test])) l2v = (l2err(virial - test_data["virial"][:numb_test])) l2ea = l2e / natoms l2va = l2v / natoms # print ("# energies: %s" % energy) print("# number of test data : %d " % numb_test) print("Energy L2err : %e eV" % l2e) print("Energy L2err/Natoms : %e eV" % l2ea) print("Force L2err : %e eV/A" % l2f) print("Virial L2err : %e eV" % l2v) print("Virial L2err/Natoms : %e eV" % l2va) detail_file = args.detail_file if detail_file is not None: pe = np.concatenate((np.reshape(test_data["energy"][:numb_test], [-1, 1]), np.reshape(energy, [-1, 1])), axis=1) np.savetxt(detail_file + ".e.out", pe, header='data_e pred_e') pf = np.concatenate((np.reshape(test_data["force"][:numb_test], [-1, 3]), np.reshape(force, [-1, 3])), axis=1) np.savetxt(detail_file + ".f.out", pf, header='data_fx data_fy data_fz pred_fx pred_fy pred_fz') pv = np.concatenate((np.reshape(test_data["virial"][:numb_test], [-1, 9]), np.reshape(virial, [-1, 9])), axis=1) np.savetxt( detail_file + ".v.out", pv, header= 'data_vxx data_vxy data_vxz data_vyx data_vyy data_vyz data_vzx data_vzy data_vzz pred_vxx pred_vxy pred_vxz pred_vyx pred_vyy pred_vyz pred_vzx pred_vzy pred_vzz' )
def test_ener(dp, system, set_prefix='set', numb_test=100000, shuffle_test=False, rand_seed=None, detail_file='detail'): if rand_seed is not None: np.random.seed(rand_seed % (2**32)) data = DeepmdData(system, set_prefix, shuffle_test=shuffle_test, type_map=dp.get_type_map()) data.add('energy', 1, atomic=False, must=False, high_prec=True) data.add('force', 3, atomic=True, must=False, high_prec=False) data.add('virial', 9, atomic=False, must=False, high_prec=False) if dp.get_dim_fparam() > 0: data.add('fparam', dp.get_dim_fparam(), atomic=False, must=True, high_prec=False) if dp.get_dim_aparam() > 0: data.add('aparam', dp.get_dim_aparam(), atomic=True, must=True, high_prec=False) test_data = data.get_test() natoms = len(test_data["type"][0]) nframes = test_data["box"].shape[0] numb_test = min(nframes, numb_test) coord = test_data["coord"][:numb_test].reshape([numb_test, -1]) box = test_data["box"][:numb_test] atype = test_data["type"][0] if dp.get_dim_fparam() > 0: fparam = test_data["fparam"][:numb_test] else: fparam = None if dp.get_dim_aparam() > 0: aparam = test_data["aparam"][:numb_test] else: aparam = None energy, force, virial, ae, av = dp.eval(coord, box, atype, fparam=fparam, aparam=aparam, atomic=True) energy = energy.reshape([numb_test, 1]) force = force.reshape([numb_test, -1]) virial = virial.reshape([numb_test, 9]) ae = ae.reshape([numb_test, -1]) av = av.reshape([numb_test, -1]) l2e = (l2err(energy - test_data["energy"][:numb_test].reshape([-1, 1]))) l2f = (l2err(force - test_data["force"][:numb_test])) l2v = (l2err(virial - test_data["virial"][:numb_test])) l2ea = l2e / natoms l2va = l2v / natoms if detail_file is not None: pe = np.concatenate( (np.reshape(test_data["energy"][:numb_test] / natoms, [-1, 1]), np.reshape(energy / natoms, [-1, 1])), axis=1) np.savetxt(detail_file + ".e.out", pe, header='data_e pred_e') pf = np.concatenate((np.reshape(test_data["force"][:numb_test], [-1, 3]), np.reshape(force, [-1, 3])), axis=1) np.savetxt(detail_file + ".f.out", pf, header='data_fx data_fy data_fz pred_fx pred_fy pred_fz') pv = np.concatenate((np.reshape(test_data["virial"][:numb_test], [-1, 9]), np.reshape(virial, [-1, 9])), axis=1) np.savetxt( detail_file + ".v.out", pv, header= 'data_vxx data_vxy data_vxz data_vyx data_vyy data_vyz data_vzx data_vzy data_vzz pred_vxx pred_vxy pred_vxz pred_vyx pred_vyy pred_vyz pred_vzx pred_vzy pred_vzz' ) return numb_test, l2e, l2ea, l2f, l2v, l2va