def test_compress(): old = get_rand_traj() # fake integer arrary which should not be casted assert old.forces is not None old.forces = np.ones_like(old.forces).astype(int) float_dtype_old = old.coords.dtype float_dtype_new = np.float32 assert float_dtype_new != float_dtype_old arr_t = type(np.array([1.0])) forget = ['stress', 'velocity'] new = crys.compress(old, copy=True, dtype=float_dtype_new, forget=forget) for name in forget: assert getattr(new, name) is None assert getattr(old, name) is not None for name in old.attr_lst: if name in forget: continue attr_old = getattr(old, name) if type(attr_old) == arr_t: attr_new = getattr(new, name) if (attr_old.dtype == float_dtype_old) and \ (attr_old.dtype.kind=='f'): print name assert type(attr_new) == arr_t assert attr_new.dtype == float_dtype_new # for all non-integer attrs, there must be a small numerical # difference assert abs(attr_old - attr_new).sum() > 0.0 else: assert attr_old.dtype == attr_new.dtype # sanity check assert new.forces.dtype.kind in ('u','i')
def test_mean(): tr = get_rand_traj() st_mean = crys.mean(tr) attrs_only_traj = ['time', 'timestep', 'nstep'] for attr_name in tr.attr_lst: attr = getattr(st_mean, attr_name) if attr_name in attrs_only_traj: assert attr is None, "%s is not None" %attr_name elif attr_name in tr.attrs_nstep: assert np.allclose(attr, getattr(tr, attr_name).mean(axis=tr.timeaxis))
def test_concatenate(): st = get_rand_struct() nstep = 5 # cat Structure tr_cat = crys.concatenate([st]*nstep) keys = tr_cat.attr_lst assert tr_cat.nstep == nstep for x in tr_cat: assert_dict_with_all_types_equal(x.__dict__, st.__dict__, keys=keys) none_attrs = ['ekin', 'timestep', 'velocity', 'temperature', 'time'] for attr_name in tr_cat.attrs_nstep: if attr_name in none_attrs: assert getattr(tr_cat, attr_name) is None else: print "test_concatenate: shape[0] == nstep:", attr_name assert getattr(tr_cat, attr_name).shape[0] == nstep print "test_concatenate: shape[0] == nstep:", attr_name, "...ok" # cat Trajectory tr = get_rand_traj() tr_cat = crys.concatenate([tr]*3) assert tr_cat.nstep == 3*tr.nstep none_attrs = ['timestep', 'time'] keys = remove_from_lst(tr_cat.attr_lst, none_attrs) for x in [tr_cat[0:tr.nstep], tr_cat[tr.nstep:2*tr.nstep], tr_cat[2*tr.nstep:3*tr.nstep]]: assert_dict_with_all_types_equal(x.__dict__, tr.__dict__, keys=keys) for attr_name in tr_cat.attrs_nstep: if attr_name in none_attrs: assert getattr(tr_cat, attr_name) is None else: assert getattr(tr_cat, attr_name).shape[0] == 3*tr.nstep # cat mixed, Structure is minimal API st = get_rand_struct() tr = crys.concatenate([st]*5) tr_cat = crys.concatenate([st]*5 + [tr]) assert tr_cat.nstep == 10 none_attrs = ['ekin', 'timestep', 'velocity', 'temperature', 'time'] keys = remove_from_lst(tr_cat.attr_lst, none_attrs) for x in tr_cat: assert_dict_with_all_types_equal(x.__dict__, st.__dict__, keys=keys) for attr_name in tr_cat.attrs_nstep: if attr_name in none_attrs: assert getattr(tr_cat, attr_name) is None else: assert getattr(tr_cat, attr_name).shape[0] == 10
def test(): # Explicit code duplication even though we could use [...,0,:] which should # work for (natoms,3) and (nstep,natoms,3) arrays [we use it in # center_on_atom()], but check if it really does. st = rc.get_rand_struct() stc = center_on_atom(st, idx=0, copy=True) assert (st.coords_frac != stc.coords_frac).all() assert (st.coords != stc.coords).all() assert (st.coords_frac[0,:] != np.array([0.5]*3)).all() assert (stc.coords_frac[0,:] == np.array([0.5]*3)).all() assert (stc.coords_frac[1:,:] != np.array([0.5]*3)).all() tr = rc.get_rand_traj() trc = center_on_atom(tr, idx=0, copy=True) assert (tr.coords_frac != trc.coords_frac).all() assert (tr.coords != trc.coords).all() assert (tr.coords_frac[:,0,:] != np.array([0.5]*3)).all() assert (trc.coords_frac[:,0,:] == np.array([0.5]*3)).all() assert (trc.coords_frac[:,1:,:] != np.array([0.5]*3)).all()
def test_api(): tr = get_rand_traj() st = get_rand_struct() for name in st.attr_lst: assert getattr(tr, name) is not None for name in tr.attrs_only_traj: assert getattr(st, name) is None aa = tr[0] # Structure bb = tr[0:1] # Trajectory keys = set.difference(set(aa.attr_lst), set(aa.attrs_only_traj)) assert aa.is_struct assert bb.is_traj # remove timeaxis before comparing arrays for name in bb.attrs_nstep: attr = getattr(bb, name) if attr.ndim == 1: setattr(bb, name, attr[0]) else: setattr(bb, name, attr[0,...]) assert_dict_with_all_types_equal(aa.__dict__, bb.__dict__, keys=keys)
def test_smooth(): tr = get_rand_traj() assert len(tr.attrs_nstep) > 0 trs = crys.smooth(tr, hanning(11)) assert len(trs.attrs_nstep) > 0 assert_attrs_not_none(trs, attr_lst=tr.attr_lst) for name in tr.attrs_nstep: a1 = getattr(tr, name) a2 = getattr(trs, name) assert a1.shape == a2.shape assert np.abs(a1 - a2).sum() > 0.0 assert trs.timestep == tr.timestep assert trs.nstep == tr.nstep # reproduce data with kernel [0,1,0] trs = crys.smooth(tr, hanning(3)) for name in tr.attrs_nstep: a1 = getattr(tr, name) a2 = getattr(trs, name) assert np.allclose(a1, a2) trs1 = crys.smooth(tr, hanning(3), method=1) trs2 = crys.smooth(tr, hanning(3), method=2) assert len(trs1.attrs_nstep) > 0 assert len(trs2.attrs_nstep) > 0 for name in tr.attrs_nstep: a1 = getattr(tr, name) a2 = getattr(trs1, name) a3 = getattr(trs2, name) assert np.allclose(a1, a2) assert np.allclose(a1, a3) trs1 = crys.smooth(tr, hanning(11), method=1) trs2 = crys.smooth(tr, hanning(11), method=2) assert len(trs1.attrs_nstep) > 0 assert len(trs2.attrs_nstep) > 0 for name in trs1.attrs_nstep: a1 = getattr(trs1, name) a2 = getattr(trs2, name) assert np.allclose(a1, a2)