1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
##############################################################################
# MDTraj: A Python Library for Loading, Saving, and Manipulating
# Molecular Dynamics Trajectories.
# Copyright 2012-2013 Stanford University and the Authors
#
# Authors: Robert McGibbon
# Contributors:
#
# MDTraj is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 2.1
# of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with MDTraj. If not, see <http://www.gnu.org/licenses/>.
##############################################################################
import sys
import numpy as np
import pytest
from mdtraj import io
from mdtraj.formats import XTCTrajectoryFile
from mdtraj.testing import eq
@pytest.fixture()
def fn_xtc(get_fn):
return get_fn("frame0.xtc")
@pytest.fixture()
def pdb(get_fn):
return get_fn("native.pdb")
strides = (1, 2, 3, 4, 5, 7, 10, 11)
def test_read_chunk1(get_fn, fn_xtc):
with XTCTrajectoryFile(fn_xtc, "r", chunk_size_multiplier=0.5) as f:
xyz, time, step, box = f.read()
iofile = io.loadh(get_fn("frame0.xtc.h5"), deferred=False)
assert eq(xyz, iofile["xyz"])
assert eq(step, iofile["step"])
assert eq(box, iofile["box"])
assert eq(time, iofile["time"])
def test_read_stride(get_fn, fn_xtc):
# read xtc with stride
iofile = io.loadh(get_fn("frame0.xtc.h5"), deferred=False)
for s in strides:
with XTCTrajectoryFile(fn_xtc) as f:
xyz, time, step, box = f.read(stride=s)
assert eq(xyz, iofile["xyz"][::s])
assert eq(step, iofile["step"][::s])
assert eq(box, iofile["box"][::s])
assert eq(time, iofile["time"][::s])
def test_read_stride_n_frames(get_fn, fn_xtc):
# read xtc with stride with n_frames
iofile = io.loadh(get_fn("frame0.xtc.h5"), deferred=False)
for s in strides:
with XTCTrajectoryFile(fn_xtc) as f:
xyz, time, step, box = f.read(n_frames=1000, stride=s)
assert eq(xyz, iofile["xyz"][::s])
assert eq(step, iofile["step"][::s])
assert eq(box, iofile["box"][::s])
assert eq(time, iofile["time"][::s])
def test_read_stride_offsets(get_fn, fn_xtc):
# read xtc with stride and offsets
iofile = io.loadh(get_fn("frame0.xtc.h5"), deferred=False)
for s in strides:
with XTCTrajectoryFile(fn_xtc) as f:
f.offsets # pre-compute byte offsets between frames
xyz, time, step, box = f.read(stride=s)
assert eq(xyz, iofile["xyz"][::s])
assert eq(step, iofile["step"][::s])
assert eq(box, iofile["box"][::s])
assert eq(time, iofile["time"][::s])
def test_read_stride_n_frames_offsets(get_fn, fn_xtc):
# read xtc with stride with n_frames and offsets
iofile = io.loadh(get_fn("frame0.xtc.h5"), deferred=False)
for s in strides:
with XTCTrajectoryFile(fn_xtc) as f:
f.offsets # pre-compute byte offsets between frames
xyz, time, step, box = f.read(n_frames=1000, stride=s)
assert eq(xyz, iofile["xyz"][::s])
assert eq(step, iofile["step"][::s])
assert eq(box, iofile["box"][::s])
assert eq(time, iofile["time"][::s])
def test_read_stride_switching_offsets(get_fn, fn_xtc):
iofile = io.loadh(get_fn("frame0.xtc.h5"), deferred=False)
with XTCTrajectoryFile(fn_xtc) as f:
f.offsets # pre-compute byte offsets between frames
# read the first 10 frames with stride of 2
s = 2
n_frames = 10
xyz, time, step, box = f.read(n_frames=n_frames, stride=s)
assert eq(xyz, iofile["xyz"][: n_frames * s : s])
assert eq(step, iofile["step"][: n_frames * s : s])
assert eq(box, iofile["box"][: n_frames * s : s])
assert eq(time, iofile["time"][: n_frames * s : s])
# now read the rest with stride 3, should start from frame index 8.
# eg. np.arange(0, n_frames*s + 1, 2)[-1] == 20
offset = f.tell()
assert offset == 20
s = 3
xyz, time, step, box = f.read(n_frames=None, stride=s)
assert eq(xyz, iofile["xyz"][offset::s])
assert eq(step, iofile["step"][offset::s])
assert eq(box, iofile["box"][offset::s])
assert eq(time, iofile["time"][offset::s])
def test_read_atomindices_1(get_fn, fn_xtc):
iofile = io.loadh(get_fn("frame0.xtc.h5"), deferred=False)
with XTCTrajectoryFile(fn_xtc) as f:
xyz, time, step, box = f.read(atom_indices=[0, 1, 2])
assert eq(xyz, iofile["xyz"][:, [0, 1, 2]])
assert eq(step, iofile["step"])
assert eq(box, iofile["box"])
assert eq(time, iofile["time"])
def test_read_atomindices_w_stride(get_fn, fn_xtc):
# test case for bug: https://github.com/mdtraj/mdtraj/issues/1394
iofile = io.loadh(get_fn("frame0.xtc.h5"), deferred=False)
for stride in strides:
with XTCTrajectoryFile(fn_xtc) as f:
xyz, time, step, box = f.read(atom_indices=[0, 1, 2], stride=stride)
assert eq(xyz, iofile["xyz"][:, [0, 1, 2]][::stride])
assert eq(step, iofile["step"][::stride])
assert eq(box, iofile["box"][::stride])
assert eq(time, iofile["time"][::stride])
def test_read_atomindices_2(get_fn, fn_xtc):
iofile = io.loadh(get_fn("frame0.xtc.h5"), deferred=False)
with XTCTrajectoryFile(fn_xtc) as f:
xyz, time, step, box = f.read(atom_indices=slice(None, None, 2))
assert eq(xyz, iofile["xyz"][:, ::2])
assert eq(step, iofile["step"])
assert eq(box, iofile["box"])
assert eq(time, iofile["time"])
def test_read_chunk2(get_fn, fn_xtc):
with XTCTrajectoryFile(fn_xtc, "r", chunk_size_multiplier=1) as f:
xyz, time, step, box = f.read()
iofile = io.loadh(get_fn("frame0.xtc.h5"), deferred=False)
assert eq(xyz, iofile["xyz"])
assert eq(step, iofile["step"])
assert eq(box, iofile["box"])
assert eq(time, iofile["time"])
def test_read_chunk3(get_fn, fn_xtc):
with XTCTrajectoryFile(fn_xtc, chunk_size_multiplier=2) as f:
xyz, time, step, box = f.read(n_frames=100)
iofile = io.loadh(get_fn("frame0.xtc.h5"), deferred=False)
assert eq(xyz, iofile["xyz"][:100])
assert eq(step, iofile["step"][:100])
assert eq(box, iofile["box"][:100])
assert eq(time, iofile["time"][:100])
def test_write_0(tmpdir, fn_xtc):
with XTCTrajectoryFile(fn_xtc) as f:
xyz = f.read()[0]
tmpfn = f"{tmpdir}/traj.xtc"
f = XTCTrajectoryFile(tmpfn, "w")
f.write(xyz)
f.close()
with XTCTrajectoryFile(tmpfn) as f:
xyz2, time2, step2, box2 = f.read()
eq(xyz, xyz2)
def test_write_1(tmpdir):
xyz = np.asarray(np.around(np.random.randn(100, 10, 3), 3), dtype=np.float32)
time = np.asarray(np.random.randn(100), dtype=np.float32)
step = np.arange(100)
box = np.asarray(np.random.randn(100, 3, 3), dtype=np.float32)
tmpfn = f"{tmpdir}/traj.xtc"
with XTCTrajectoryFile(tmpfn, "w") as f:
f.write(xyz, time=time, step=step, box=box)
with XTCTrajectoryFile(tmpfn) as f:
xyz2, time2, step2, box2 = f.read()
eq(xyz, xyz2)
eq(time, time2)
eq(step, step2)
eq(box, box2)
def test_write_2(tmpdir):
xyz = np.asarray(np.around(np.random.randn(100, 10, 3), 3), dtype=np.float32)
time = np.asarray(np.random.randn(100), dtype=np.float32)
step = np.arange(100)
box = np.asarray(np.random.randn(100, 3, 3), dtype=np.float32)
tmpfn = f"{tmpdir}/traj.xtc"
with XTCTrajectoryFile(tmpfn, "w") as f:
for i in range(len(xyz)):
f.write(xyz[i], time=time[i], step=step[i], box=box[i])
with XTCTrajectoryFile(tmpfn) as f:
xyz2, time2, step2, box2 = f.read()
eq(xyz, xyz2)
eq(time, time2)
eq(step, step2)
eq(box, box2)
def test_read_error_0(tmpdir):
tmpfn = f"{tmpdir}/traj.xtc"
with pytest.raises(IOError):
with XTCTrajectoryFile(tmpfn, "r") as f:
f.read()
def test_write_error_0(tmpdir):
xyz = np.asarray(np.random.randn(100, 3, 3), dtype=np.float32)
tmpfn = f"{tmpdir}/traj.xtc"
with XTCTrajectoryFile(tmpfn, "w") as f:
with pytest.raises(ValueError):
f.read(xyz)
def test_read_error_1():
with pytest.raises(IOError):
XTCTrajectoryFile("/tmp/sdfsdfsdf")
def test_read_error_2(get_fn):
with pytest.raises(IOError):
XTCTrajectoryFile(get_fn("frame0.dcd")).read()
def test_xtc_write_wierd_0(tmpdir):
x0 = np.asarray(np.random.randn(100, 3, 3), dtype=np.float32)
x1 = np.asarray(np.random.randn(100, 9, 3), dtype=np.float32)
tmpfn = f"{tmpdir}/traj.xtc"
with XTCTrajectoryFile(tmpfn, "w") as f:
f.write(x0)
with pytest.raises(ValueError):
f.write(x1)
xr = XTCTrajectoryFile(tmpfn).read()[0]
print(xr.shape)
def test_tell(get_fn):
with XTCTrajectoryFile(get_fn("frame0.xtc")) as f:
eq(f.tell(), 0)
f.read(101)
eq(f.tell(), 101)
f.read(3)
eq(f.tell(), 104)
def test_seek(get_fn):
reference = XTCTrajectoryFile(get_fn("frame0.xtc")).read()[0]
with XTCTrajectoryFile(get_fn("frame0.xtc")) as f:
eq(f.tell(), 0)
eq(f.read(1)[0][0], reference[0])
eq(f.tell(), 1)
xyz = f.read(1)[0][0]
eq(xyz, reference[1])
eq(f.tell(), 2)
f.seek(0)
eq(f.tell(), 0)
xyz = f.read(1)[0][0]
eq(f.tell(), 1)
eq(xyz, reference[0])
f.seek(5) # offset array is going to be built
assert len(f.offsets) == len(reference)
eq(f.read(1)[0][0], reference[5])
eq(f.tell(), 6)
f.seek(-5, 1)
eq(f.tell(), 1)
eq(f.read(1)[0][0], reference[1])
def test_seek_natoms9(tmpdir, get_fn):
# create a xtc file with 9 atoms and seek it.
with XTCTrajectoryFile(get_fn("frame0.xtc"), "r") as fh:
xyz = fh.read()[0][:, :9, :]
tmpfn = f"{tmpdir}/traj.xtc"
with XTCTrajectoryFile(tmpfn, "w", force_overwrite=True) as f:
f.write(xyz)
with XTCTrajectoryFile(tmpfn, "r") as f:
eq(f.read(1)[0].shape, (1, 9, 3))
eq(f.tell(), 1)
f.seek(99)
eq(f.read(1)[0].squeeze(), xyz[99])
# seek relative
f.seek(-1, 1)
eq(f.read(1)[0].squeeze(), xyz[99])
f.seek(0, 0)
eq(f.read(1)[0].squeeze(), xyz[0])
def test_seek_out_of_bounds(get_fn):
with XTCTrajectoryFile(get_fn("frame0.xtc"), "r") as fh:
with pytest.raises(IOError):
fh.seek(10000000)
def test_ragged_1(tmpdir):
# try first writing no box vectors,, and then adding some
xyz = np.random.randn(100, 5, 3)
time = np.random.randn(100)
box = np.random.randn(100, 3, 3)
tmpfn = f"{tmpdir}/traj.xtc"
with XTCTrajectoryFile(tmpfn, "w", force_overwrite=True) as f:
f.write(xyz)
with pytest.raises(ValueError):
f.write(xyz, time, box)
def test_ragged_2(tmpdir):
# try first writing no box vectors, and then adding some
xyz = np.random.randn(100, 5, 3)
time = np.random.randn(100)
box = np.random.randn(100, 3, 3)
tmpfn = f"{tmpdir}/traj.xtc"
with XTCTrajectoryFile(tmpfn, "w", force_overwrite=True) as f:
f.write(xyz, time=time, box=box)
with pytest.raises(ValueError):
f.write(xyz)
def test_short_traj(tmpdir):
tmpfn = f"{tmpdir}/traj.xtc"
with XTCTrajectoryFile(tmpfn, "w") as f:
f.write(np.random.uniform(size=(5, 100000, 3)))
with XTCTrajectoryFile(tmpfn, "r") as f:
assert len(f) == 5, len(f)
not_on_win = pytest.mark.skipif(
sys.platform.startswith("win"),
reason="Can not open file being written again due to file locking.",
)
@not_on_win
def test_flush(tmpdir):
tmpfn = f"{tmpdir}/traj.xtc"
data = np.random.random((5, 100, 3))
with XTCTrajectoryFile(tmpfn, "w") as f:
f.write(data)
f.flush()
# note that f is still open, so we can now try to read the contents flushed to disk.
with XTCTrajectoryFile(tmpfn, "r") as f2:
out = f2.read()
np.testing.assert_allclose(out[0], data, atol=1e-3)
|