def test_nematic_order_args(): assert_raises(ValueError, lambda: order.compute_nematic_order(TRAJ2, indices='dog')) assert_raises(ValueError, lambda: order.compute_nematic_order(TRAJ2, indices='O')) assert_raises(ValueError, lambda: order.compute_nematic_order(TRAJ2, indices=1)) # Input indices with wrong "shapes". assert_raises(ValueError, lambda: order.compute_nematic_order(TRAJ2, indices=[[1, [2]], [2]])) assert_raises(ValueError, lambda: order.compute_nematic_order(TRAJ2, indices=[1, 2, 3]))
def test_write_units_mismatch(): velocoties = units.Quantity(np.random.randn(4, 10,3), units.angstroms/units.picosecond) with HDF5TrajectoryFile(temp, 'w') as f: # if you try to write coordinates that are unitted and not # in the correct units, we find that assert_raises(TypeError, lambda: f.write(coordinates=velocoties))
def test_write_inconsistent(): coordinates = np.random.randn(4, 10,3) with HDF5TrajectoryFile(temp, 'w') as f: f.write(coordinates) # since the first frames we saved didn't contain velocities, we # can't save more velocities assert_raises(ValueError, lambda: f.write(coordinates, velocities=coordinates))
def test_write_inconsistent_2(): coordinates = np.random.randn(4, 10,3) with HDF5TrajectoryFile(temp, 'w') as f: f.write(coordinates, velocities=coordinates) # we're saving a deficient set of data, since before we wrote # more information. assert_raises(ValueError, lambda: f.write(coordinates))
def test_select_atom_indices(): top = md.load(get_fn("native.pdb")).topology yield lambda: eq(top.select_atom_indices("alpha"), np.array([8])) yield lambda: eq(top.select_atom_indices("minimal"), np.array([4, 5, 6, 8, 10, 14, 15, 16, 18])) assert_raises(ValueError, lambda: top.select_atom_indices("sdfsdfsdf"))
def test_write_inconsistent_2(): coordinates = np.random.randn(4, 10, 3) with HDF5TrajectoryFile(temp, 'w') as f: f.write(coordinates, velocities=coordinates) # we're saving a deficient set of data, since before we wrote # more information. assert_raises(ValueError, lambda: f.write(coordinates))
def test_select_atom_indices(): top = md.load(get_fn('native.pdb')).topology yield lambda: eq(top.select_atom_indices('alpha'), np.array([8])) yield lambda: eq(top.select_atom_indices('minimal'), np.array([4, 5, 6, 8, 10, 14, 15, 16, 18])) assert_raises(ValueError, lambda: top.select_atom_indices('sdfsdfsdf'))
def test_write_inconsistent(): coordinates = np.random.randn(4, 10, 3) with HDF5TrajectoryFile(temp, 'w') as f: f.write(coordinates) # since the first frames we saved didn't contain velocities, we # can't save more velocities assert_raises(ValueError, lambda: f.write(coordinates, velocities=coordinates))
def test_write_units_mismatch(): velocoties = units.Quantity(np.random.randn(4, 10, 3), units.angstroms / units.picosecond) with HDF5TrajectoryFile(temp, 'w') as f: # if you try to write coordinates that are unitted and not # in the correct units, we find that assert_raises(TypeError, lambda: f.write(coordinates=velocoties))
def test_atoms_by_name(): top = md.load(get_fn('bpti.pdb')).topology atoms = list(top.atoms) for atom1, atom2 in zip(top.atoms_by_name('CA'), top.chain(0).atoms_by_name('CA')): assert atom1 == atom2 assert atom1 in atoms assert atom1.name == 'CA' assert len(list(top.atoms_by_name('CA'))) == sum(1 for _ in atoms if _.name == 'CA') assert top.residue(15).atom('CA') == [a for a in top.residue(15).atoms if a.name == 'CA'][0] assert_raises(KeyError, lambda: top.residue(15).atom('sdfsdsdf'))
def test_1(): # https://github.com/rmcgibbo/mdtraj/issues/438 try: traj = md.load(get_fn('frame0.h5')) np.save('temp.npy', traj.xyz) traj.xyz = np.load('temp.npy', mmap_mode='r') # since traj isn't precentered, this requires centering # the coordinates which is done inplace. but that's not possible # with mmap_mode = 'r' assert_raises(ValueError, md.rmsd, traj, traj, 0) # this should work traj.xyz = np.load('temp.npy', mmap_mode='c') md.rmsd(traj, traj, 0) finally: del traj os.unlink('temp.npy')
def test_rdf_args(): assert_raises( ValueError, lambda: md.geometry.rdf.compute_rdf(TRAJ, pair_names=('O', 'O', 'O'))) assert_raises(ValueError, lambda: md.geometry.rdf.compute_rdf(TRAJ, pair_names=('O'))) assert_raises( ValueError, lambda: md.geometry.rdf.compute_rdf(TRAJ, pair_names=('C', 'C')))
def test_immutable(): def f(): element.hydrogen.mass = 1 def g(): element.radium.symbol = 'sdfsdfsdf' def h(): element.iron.name = 'sdfsdf' assert_raises(AttributeError, f) assert_raises(AttributeError, g) assert_raises(AttributeError, h) assert element.hydrogen.mass == 1.007947 assert element.radium.symbol == 'Ra' assert element.iron.name == 'iron'
def test_2(): # https://github.com/mdtraj/mdtraj/issues/438 try: dir = tempfile.mkdtemp() fn = os.path.join(dir, 'temp.npy') traj = md.load(get_fn('frame0.h5')) # precenter the coordinates traj.center_coordinates() traces = traj._rmsd_traces np.save(fn, traj.xyz) traj.xyz = np.load(fn, mmap_mode='r') traj._rmsd_traces = traces with assert_raises(ValueError): md.rmsd(traj, traj, 0, precentered=True) finally: del traj os.unlink(fn) os.rmdir(dir)
def test_1(): # https://github.com/mdtraj/mdtraj/issues/438 try: dir = tempfile.mkdtemp() fn = os.path.join(dir, 'temp.npy') traj = md.load(get_fn('frame0.h5')) np.save(fn, traj.xyz) traj.xyz = np.load(fn, mmap_mode='r') # since traj isn't precentered, this requires centering # the coordinates which is done inplace. but that's not possible # with mmap_mode = 'r' with assert_raises(ValueError): md.rmsd(traj, traj, 0) # this should work traj.xyz = np.load(fn, mmap_mode='c') md.rmsd(traj, traj, 0) finally: del traj os.unlink(fn) os.rmdir(dir)
def test_raises(): assert_raises(ValueError, lambda: parse_selection('or')) assert_raises(ValueError, lambda: parse_selection('a <'))
def test_mismatch(): # loading a 22 atoms xtc with a topology that has 2,000 atoms # some kind of error should happen! assert_raises(ValueError, lambda: md.load(get_fn('frame0.xtc'), top=get_fn('4ZUO.pdb')))
def test_raises2(): assert_raises(ValueError, lambda: parse_selection('dog 5')) assert_raises(ValueError, lambda: parse_selection('dog == 5')) assert_raises(ValueError, lambda: parse_selection('dog frog')) assert_raises(ValueError, lambda: parse_selection('not dog')) assert_raises(ValueError, lambda: parse_selection('protein or dog')) assert_raises(ValueError, lambda: parse_selection('dog 1 to 5')) assert_raises(ValueError, lambda: parse_selection('dog'))
def test_2(): fn = 'temp-2%s' % ext open(fn, 'w').close() assert_raises(IOError, lambda: t_ref.save(fn, force_overwrite=False))