imdclient 0.1.3__py3-none-any.whl → 0.2.0b0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. imdclient/IMDClient.py +43 -12
  2. imdclient/IMDProtocol.py +1 -0
  3. imdclient/__init__.py +0 -5
  4. imdclient/data/gromacs/md/gromacs_v3_nst1.mdp +3 -3
  5. imdclient/data/namd/md/namd3 +0 -0
  6. imdclient/data/namd/md/namd_v3_nst_1.namd +1 -1
  7. imdclient/tests/base.py +108 -83
  8. imdclient/tests/conftest.py +0 -39
  9. imdclient/tests/datafiles.py +16 -1
  10. imdclient/tests/docker_testing/docker.md +1 -1
  11. imdclient/tests/hpc_testing/gromacs/README.md +112 -0
  12. imdclient/tests/hpc_testing/gromacs/gmx_gpu_test.mdp +58 -0
  13. imdclient/tests/hpc_testing/gromacs/gmx_gpu_test.top +11764 -0
  14. imdclient/tests/hpc_testing/gromacs/struct.gro +21151 -0
  15. imdclient/tests/hpc_testing/gromacs/validate_gmx.sh +90 -0
  16. imdclient/tests/hpc_testing/lammps/README.md +62 -0
  17. imdclient/tests/hpc_testing/lammps/lammps_v3_nst_1.in +71 -0
  18. imdclient/tests/hpc_testing/lammps/topology_after_min.data +8022 -0
  19. imdclient/tests/hpc_testing/lammps/validate_lmp.sh +66 -0
  20. imdclient/tests/hpc_testing/namd/README.md +147 -0
  21. imdclient/tests/hpc_testing/namd/alanin.params +402 -0
  22. imdclient/tests/hpc_testing/namd/alanin.pdb +77 -0
  23. imdclient/tests/hpc_testing/namd/alanin.psf +206 -0
  24. imdclient/tests/hpc_testing/namd/namd_v3_nst_1.namd +59 -0
  25. imdclient/tests/hpc_testing/namd/validate_namd.sh +71 -0
  26. imdclient/tests/minimalreader.py +86 -0
  27. imdclient/tests/server.py +6 -14
  28. imdclient/tests/test_gromacs.py +15 -3
  29. imdclient/tests/test_imdclient.py +26 -7
  30. imdclient/tests/test_lammps.py +22 -19
  31. imdclient/tests/test_manual.py +224 -66
  32. imdclient/tests/test_namd.py +39 -16
  33. imdclient/tests/test_utils.py +31 -0
  34. imdclient/utils.py +50 -17
  35. {imdclient-0.1.3.dist-info → imdclient-0.2.0b0.dist-info}/METADATA +60 -39
  36. imdclient-0.2.0b0.dist-info/RECORD +53 -0
  37. {imdclient-0.1.3.dist-info → imdclient-0.2.0b0.dist-info}/WHEEL +1 -1
  38. {imdclient-0.1.3.dist-info → imdclient-0.2.0b0.dist-info/licenses}/AUTHORS.md +4 -1
  39. {imdclient-0.1.3.dist-info → imdclient-0.2.0b0.dist-info/licenses}/LICENSE +3 -1
  40. imdclient/IMD.py +0 -130
  41. imdclient/backends.py +0 -352
  42. imdclient/results.py +0 -332
  43. imdclient/streamanalysis.py +0 -1056
  44. imdclient/streambase.py +0 -199
  45. imdclient/tests/test_imdreader.py +0 -658
  46. imdclient/tests/test_stream_analysis.py +0 -61
  47. imdclient-0.1.3.dist-info/RECORD +0 -42
  48. {imdclient-0.1.3.dist-info → imdclient-0.2.0b0.dist-info}/top_level.txt +0 -0
@@ -1,658 +0,0 @@
1
- """Test for MDAnalysis trajectory reader expectations
2
- """
3
-
4
- from MDAnalysisTests.datafiles import (
5
- COORDINATES_TOPOLOGY,
6
- COORDINATES_TRR,
7
- COORDINATES_H5MD,
8
- )
9
- import MDAnalysis as mda
10
- from .utils import (
11
- get_free_port,
12
- create_default_imdsinfo_v3,
13
- )
14
-
15
- # NOTE: replaceme with imdclient.tests.server
16
- from .server import InThreadIMDServer
17
- from MDAnalysisTests.coordinates.base import (
18
- MultiframeReaderTest,
19
- BaseReference,
20
- assert_timestep_almost_equal,
21
- )
22
- from numpy.testing import (
23
- assert_almost_equal,
24
- assert_array_almost_equal,
25
- assert_equal,
26
- assert_allclose,
27
- )
28
- import numpy as np
29
- import logging
30
- import pytest
31
- from MDAnalysis.transformations import translate
32
- import pickle
33
-
34
- from imdclient.IMD import IMDReader
35
-
36
- logger = logging.getLogger("imdclient.IMDClient")
37
- file_handler = logging.FileHandler("test.log")
38
- formatter = logging.Formatter(
39
- "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
40
- )
41
- file_handler.setFormatter(formatter)
42
- logger.addHandler(file_handler)
43
- logger.setLevel(logging.DEBUG)
44
-
45
-
46
- class IMDReference(BaseReference):
47
- def __init__(self):
48
- super(IMDReference, self).__init__()
49
- self.port = get_free_port()
50
- # Serve TRR traj data via the server
51
- traj = mda.coordinates.TRR.TRRReader(COORDINATES_TRR)
52
- self.server = InThreadIMDServer(traj)
53
- self.server.set_imdsessioninfo(create_default_imdsinfo_v3())
54
-
55
- self.n_atoms = traj.n_atoms
56
- self.prec = 3
57
-
58
- self.trajectory = f"imd://localhost:{self.port}"
59
- self.topology = COORDINATES_TOPOLOGY
60
- self.changing_dimensions = True
61
- self.reader = IMDReader
62
-
63
- self.first_frame.velocities = self.first_frame.positions / 10
64
- self.first_frame.forces = self.first_frame.positions / 100
65
-
66
- self.second_frame.velocities = self.second_frame.positions / 10
67
- self.second_frame.forces = self.second_frame.positions / 100
68
-
69
- self.last_frame.velocities = self.last_frame.positions / 10
70
- self.last_frame.forces = self.last_frame.positions / 100
71
-
72
- self.jump_to_frame.velocities = self.jump_to_frame.positions / 10
73
- self.jump_to_frame.forces = self.jump_to_frame.positions / 100
74
-
75
- def iter_ts(self, i):
76
- ts = self.first_frame.copy()
77
- ts.positions = 2**i * self.first_frame.positions
78
- ts.velocities = ts.positions / 10
79
- ts.forces = ts.positions / 100
80
- ts.time = i
81
- ts.frame = i
82
- return ts
83
-
84
-
85
- class TestIMDReaderBaseAPI(MultiframeReaderTest):
86
-
87
- @pytest.fixture()
88
- def ref(self):
89
- """Not a static method like in base class- need new server for each test"""
90
- return IMDReference()
91
-
92
- @pytest.fixture()
93
- def reader(self, ref):
94
- # This will start the test IMD Server, waiting for a connection
95
- # to then send handshake & first frame
96
- ref.server.handshake_sequence("localhost", ref.port)
97
- # This will connect to the test IMD Server and read the first frame
98
- reader = ref.reader(ref.trajectory, n_atoms=ref.n_atoms)
99
- # Send the rest of the frames- small enough to all fit in socket itself
100
- ref.server.send_frames(1, 5)
101
-
102
- reader.add_auxiliary(
103
- "lowf",
104
- ref.aux_lowf,
105
- dt=ref.aux_lowf_dt,
106
- initial_time=0,
107
- time_selector=None,
108
- )
109
- reader.add_auxiliary(
110
- "highf",
111
- ref.aux_highf,
112
- dt=ref.aux_highf_dt,
113
- initial_time=0,
114
- time_selector=None,
115
- )
116
- return reader
117
-
118
- @pytest.fixture()
119
- def transformed(self, ref):
120
- # This will start the test IMD Server, waiting for a connection
121
- # to then send handshake & first frame
122
- ref.server.handshake_sequence("localhost", ref.port)
123
- # This will connect to the test IMD Server and read the first frame
124
- transformed = ref.reader(ref.trajectory, n_atoms=ref.n_atoms)
125
- # Send the rest of the frames- small enough to all fit in socket itself
126
- ref.server.send_frames(1, 5)
127
- transformed.add_transformations(
128
- translate([1, 1, 1]), translate([0, 0, 0.33])
129
- )
130
- return transformed
131
-
132
- @pytest.mark.skip(
133
- reason="Stream-based reader cannot determine n_frames until EOF"
134
- )
135
- def test_n_frames(self, reader, ref):
136
- assert_equal(
137
- self.universe.trajectory.n_frames,
138
- 1,
139
- "wrong number of frames in pdb",
140
- )
141
-
142
- def test_first_frame(self, ref, reader):
143
- # don't rewind here as in inherited base test
144
- assert_timestep_almost_equal(
145
- reader.ts, ref.first_frame, decimal=ref.prec
146
- )
147
-
148
- @pytest.mark.skip(reason="IMD is not a writeable format")
149
- def test_get_writer_1(self, ref, reader, tmpdir):
150
- with tmpdir.as_cwd():
151
- outfile = "test-writer." + ref.ext
152
- with reader.Writer(outfile) as W:
153
- assert_equal(isinstance(W, ref.writer), True)
154
- assert_equal(W.n_atoms, reader.n_atoms)
155
-
156
- @pytest.mark.skip(reason="IMD is not a writeable format")
157
- def test_get_writer_2(self, ref, reader, tmpdir):
158
- with tmpdir.as_cwd():
159
- outfile = "test-writer." + ref.ext
160
- with reader.Writer(outfile, n_atoms=100) as W:
161
- assert_equal(isinstance(W, ref.writer), True)
162
- assert_equal(W.n_atoms, 100)
163
-
164
- @pytest.mark.skip(
165
- reason="Stream-based reader cannot determine total_time until EOF"
166
- )
167
- def test_total_time(self, reader, ref):
168
- assert_almost_equal(
169
- reader.totaltime,
170
- ref.totaltime,
171
- decimal=ref.prec,
172
- )
173
-
174
- @pytest.mark.skip(reason="Stream-based reader can only be read iteratively")
175
- def test_changing_dimensions(self, ref, reader):
176
- if ref.changing_dimensions:
177
- reader.rewind()
178
- if ref.dimensions is None:
179
- assert reader.ts.dimensions is None
180
- else:
181
- assert_array_almost_equal(
182
- reader.ts.dimensions,
183
- ref.dimensions,
184
- decimal=ref.prec,
185
- )
186
- reader[1]
187
- if ref.dimensions_second_frame is None:
188
- assert reader.ts.dimensions is None
189
- else:
190
- assert_array_almost_equal(
191
- reader.ts.dimensions,
192
- ref.dimensions_second_frame,
193
- decimal=ref.prec,
194
- )
195
-
196
- def test_iter(self, ref, reader):
197
- for i, ts in enumerate(reader):
198
- assert_timestep_almost_equal(ts, ref.iter_ts(i), decimal=ref.prec)
199
-
200
- def test_first_dimensions(self, ref, reader):
201
- # don't rewind here as in inherited base test
202
- if ref.dimensions is None:
203
- assert reader.ts.dimensions is None
204
- else:
205
- assert_array_almost_equal(
206
- reader.ts.dimensions,
207
- ref.dimensions,
208
- decimal=ref.prec,
209
- )
210
-
211
- def test_volume(self, ref, reader):
212
- # don't rewind here as in inherited base test
213
- vol = reader.ts.volume
214
- # Here we can only be sure about the numbers upto the decimal point due
215
- # to floating point impressions.
216
- assert_almost_equal(vol, ref.volume, 0)
217
-
218
- @pytest.mark.skip(reason="Cannot create new reader from same stream")
219
- def test_reload_auxiliaries_from_description(self, ref, reader):
220
- # get auxiliary desscriptions form existing reader
221
- descriptions = reader.get_aux_descriptions()
222
- # load a new reader, without auxiliaries
223
- reader = ref.reader(ref.trajectory)
224
- # load auxiliaries into new reader, using description...
225
- for aux in descriptions:
226
- reader.add_auxiliary(**aux)
227
- # should have the same number of auxiliaries
228
- assert_equal(
229
- reader.aux_list,
230
- reader.aux_list,
231
- "Number of auxiliaries does not match",
232
- )
233
- # each auxiliary should be the same
234
- for auxname in reader.aux_list:
235
- assert_equal(
236
- reader._auxs[auxname],
237
- reader._auxs[auxname],
238
- "AuxReaders do not match",
239
- )
240
-
241
- @pytest.mark.skip(reason="Stream can only be read in for loop")
242
- def test_stop_iter(self, reader):
243
- # reset to 0
244
- reader.rewind()
245
- for ts in reader[:-1]:
246
- pass
247
- assert_equal(reader.frame, 0)
248
-
249
- @pytest.mark.skip(reason="Cannot rewind stream")
250
- def test_iter_rewinds(self, reader, accessor):
251
- for ts_indices in accessor(reader):
252
- pass
253
- assert_equal(ts_indices.frame, 0)
254
-
255
- @pytest.mark.skip(
256
- reason="Timeseries currently requires n_frames to be known"
257
- )
258
- @pytest.mark.parametrize(
259
- "order", ("fac", "fca", "afc", "acf", "caf", "cfa")
260
- )
261
- def test_timeseries_shape(self, reader, order):
262
- timeseries = reader.timeseries(order=order)
263
- a_index = order.index("a")
264
- # f_index = order.index("f")
265
- c_index = order.index("c")
266
- assert timeseries.shape[a_index] == reader.n_atoms
267
- # assert timeseries.shape[f_index] == len(reader)
268
- assert timeseries.shape[c_index] == 3
269
-
270
- @pytest.mark.skip(
271
- reason="Timeseries currently requires n_frames to be known"
272
- )
273
- @pytest.mark.parametrize("asel", ("index 1", "index 2", "index 1 to 3"))
274
- def test_timeseries_asel_shape(self, reader, asel):
275
- atoms = mda.Universe(reader.filename).select_atoms(asel)
276
- timeseries = reader.timeseries(atoms, order="fac")
277
- assert timeseries.shape[0] == len(reader)
278
- assert timeseries.shape[1] == len(atoms)
279
- assert timeseries.shape[2] == 3
280
-
281
- @pytest.mark.skip("Cannot slice stream")
282
- @pytest.mark.parametrize("slice", ([0, 2, 1], [0, 10, 2], [0, 10, 3]))
283
- def test_timeseries_values(self, reader, slice):
284
- ts_positions = []
285
- if isinstance(reader, mda.coordinates.memory.MemoryReader):
286
- pytest.xfail(
287
- "MemoryReader uses deprecated stop inclusive"
288
- " indexing, see Issue #3893"
289
- )
290
- if slice[1] > len(reader):
291
- pytest.skip("too few frames in reader")
292
- for i in range(slice[0], slice[1], slice[2]):
293
- ts = reader[i]
294
- ts_positions.append(ts.positions.copy())
295
- positions = np.asarray(ts_positions)
296
- timeseries = reader.timeseries(
297
- start=slice[0],
298
- stop=slice[1],
299
- step=slice[2],
300
- order="fac",
301
- )
302
- assert_allclose(timeseries, positions)
303
-
304
- @pytest.mark.skip(reason="Cannot rewind stream")
305
- def test_transformations_2iter(self, ref, transformed):
306
- # Are the transformations applied and
307
- # are the coordinates "overtransformed"?
308
- v1 = np.float32((1, 1, 1))
309
- v2 = np.float32((0, 0, 0.33))
310
- idealcoords = []
311
- for i, ts in enumerate(transformed):
312
- idealcoords.append(ref.iter_ts(i).positions + v1 + v2)
313
- assert_array_almost_equal(
314
- ts.positions,
315
- idealcoords[i],
316
- decimal=ref.prec,
317
- )
318
-
319
- for i, ts in enumerate(transformed):
320
- assert_almost_equal(
321
- ts.positions,
322
- idealcoords[i],
323
- decimal=ref.prec,
324
- )
325
-
326
- @pytest.mark.skip(reason="Cannot slice stream")
327
- def test_transformations_slice(self, ref, transformed):
328
- # Are the transformations applied when iterating over a slice of the trajectory?
329
- v1 = np.float32((1, 1, 1))
330
- v2 = np.float32((0, 0, 0.33))
331
- for i, ts in enumerate(transformed[2:3:1]):
332
- idealcoords = ref.iter_ts(ts.frame).positions + v1 + v2
333
- assert_array_almost_equal(
334
- ts.positions, idealcoords, decimal=ref.prec
335
- )
336
-
337
- @pytest.mark.skip(reason="Cannot slice stream")
338
- def test_transformations_switch_frame(self, ref, transformed):
339
- # This test checks if the transformations are applied and if the coordinates
340
- # "overtransformed" on different situations
341
- # Are the transformations applied when we switch to a different frame?
342
- v1 = np.float32((1, 1, 1))
343
- v2 = np.float32((0, 0, 0.33))
344
- first_ideal = ref.iter_ts(0).positions + v1 + v2
345
- if len(transformed) > 1:
346
- assert_array_almost_equal(
347
- transformed[0].positions,
348
- first_ideal,
349
- decimal=ref.prec,
350
- )
351
- second_ideal = ref.iter_ts(1).positions + v1 + v2
352
- assert_array_almost_equal(
353
- transformed[1].positions,
354
- second_ideal,
355
- decimal=ref.prec,
356
- )
357
-
358
- # What if we comeback to the previous frame?
359
- assert_array_almost_equal(
360
- transformed[0].positions,
361
- first_ideal,
362
- decimal=ref.prec,
363
- )
364
-
365
- # How about we switch the frame to itself?
366
- assert_array_almost_equal(
367
- transformed[0].positions,
368
- first_ideal,
369
- decimal=ref.prec,
370
- )
371
- else:
372
- assert_array_almost_equal(
373
- transformed[0].positions,
374
- first_ideal,
375
- decimal=ref.prec,
376
- )
377
-
378
- @pytest.mark.skip(reason="Cannot rewind stream")
379
- def test_transformation_rewind(self, ref, transformed):
380
- # this test checks if the transformations are applied after rewinding the
381
- # trajectory
382
- v1 = np.float32((1, 1, 1))
383
- v2 = np.float32((0, 0, 0.33))
384
- ideal_coords = ref.iter_ts(0).positions + v1 + v2
385
- transformed.rewind()
386
- assert_array_almost_equal(
387
- transformed[0].positions,
388
- ideal_coords,
389
- decimal=ref.prec,
390
- )
391
-
392
- @pytest.mark.skip(reason="Cannot make a copy of a stream")
393
- def test_copy(self, ref, transformed):
394
- # this test checks if transformations are carried over a copy and if the
395
- # coordinates of the copy are equal to the original's
396
- v1 = np.float32((1, 1, 1))
397
- v2 = np.float32((0, 0, 0.33))
398
- new = transformed.copy()
399
- assert_equal(
400
- transformed.transformations,
401
- new.transformations,
402
- "transformations are not equal",
403
- )
404
- for i, ts in enumerate(new):
405
- ideal_coords = ref.iter_ts(i).positions + v1 + v2
406
- assert_array_almost_equal(
407
- ts.positions, ideal_coords, decimal=ref.prec
408
- )
409
-
410
- @pytest.mark.skip(reason="Cannot pickle socket")
411
- def test_pickle_reader(self, reader):
412
- """It probably wouldn't be a good idea to pickle a
413
- reader that is connected to a server"""
414
- reader_p = pickle.loads(pickle.dumps(reader))
415
- assert_equal(len(reader), len(reader_p))
416
- assert_equal(
417
- reader.ts,
418
- reader_p.ts,
419
- "Timestep is changed after pickling",
420
- )
421
-
422
- @pytest.mark.skip(reason="Cannot pickle socket")
423
- def test_pickle_next_ts_reader(self, reader):
424
- reader_p = pickle.loads(pickle.dumps(reader))
425
- assert_equal(
426
- next(reader),
427
- next(reader_p),
428
- "Next timestep is changed after pickling",
429
- )
430
-
431
- @pytest.mark.skip(reason="Cannot pickle socket")
432
- def test_pickle_last_ts_reader(self, reader):
433
- # move current ts to last frame.
434
- reader[-1]
435
- reader_p = pickle.loads(pickle.dumps(reader))
436
- assert_equal(
437
- len(reader),
438
- len(reader_p),
439
- "Last timestep is changed after pickling",
440
- )
441
- assert_equal(
442
- reader.ts,
443
- reader_p.ts,
444
- "Last timestep is changed after pickling",
445
- )
446
-
447
- @pytest.mark.skip(reason="Cannot copy stream")
448
- def test_transformations_copy(self, ref, transformed):
449
- # this test checks if transformations are carried over a copy and if the
450
- # coordinates of the copy are equal to the original's
451
- v1 = np.float32((1, 1, 1))
452
- v2 = np.float32((0, 0, 0.33))
453
- new = transformed.copy()
454
- assert_equal(
455
- transformed.transformations,
456
- new.transformations,
457
- "transformations are not equal",
458
- )
459
- for i, ts in enumerate(new):
460
- ideal_coords = ref.iter_ts(i).positions + v1 + v2
461
- assert_array_almost_equal(
462
- ts.positions, ideal_coords, decimal=ref.prec
463
- )
464
-
465
- @pytest.mark.skip(
466
- reason="Timeseries currently requires n_frames to be known"
467
- )
468
- def test_timeseries_empty_asel(self, reader):
469
- with pytest.warns(
470
- UserWarning,
471
- match="Empty string to select atoms, empty group returned.",
472
- ):
473
- atoms = mda.Universe(reader.filename).select_atoms(None)
474
- with pytest.raises(ValueError, match="Timeseries requires at least"):
475
- reader.timeseries(asel=atoms)
476
-
477
- @pytest.mark.skip(
478
- reason="Timeseries currently requires n_frames to be known"
479
- )
480
- def test_timeseries_empty_atomgroup(self, reader):
481
- with pytest.warns(
482
- UserWarning,
483
- match="Empty string to select atoms, empty group returned.",
484
- ):
485
- atoms = mda.Universe(reader.filename).select_atoms(None)
486
- with pytest.raises(ValueError, match="Timeseries requires at least"):
487
- reader.timeseries(atomgroup=atoms)
488
-
489
- @pytest.mark.skip(
490
- reason="Timeseries currently requires n_frames to be known"
491
- )
492
- def test_timeseries_asel_warns_deprecation(self, reader):
493
- atoms = mda.Universe(reader.filename).select_atoms("index 1")
494
- with pytest.warns(DeprecationWarning, match="asel argument to"):
495
- timeseries = reader.timeseries(asel=atoms, order="fac")
496
-
497
- @pytest.mark.skip(
498
- reason="Timeseries currently requires n_frames to be known"
499
- )
500
- def test_timeseries_atomgroup(self, reader):
501
- atoms = mda.Universe(reader.filename).select_atoms("index 1")
502
- timeseries = reader.timeseries(atomgroup=atoms, order="fac")
503
-
504
- @pytest.mark.skip(
505
- reason="Timeseries currently requires n_frames to be known"
506
- )
507
- def test_timeseries_atomgroup_asel_mutex(self, reader):
508
- atoms = mda.Universe(reader.filename).select_atoms("index 1")
509
- with pytest.raises(ValueError, match="Cannot provide both"):
510
- timeseries = reader.timeseries(
511
- atomgroup=atoms, asel=atoms, order="fac"
512
- )
513
-
514
- @pytest.mark.skip("Cannot slice stream")
515
- def test_last_frame(self, ref, reader):
516
- ts = reader[-1]
517
- assert_timestep_almost_equal(ts, ref.last_frame, decimal=ref.prec)
518
-
519
- @pytest.mark.skip("Cannot slice stream")
520
- def test_go_over_last_frame(self, ref, reader):
521
- with pytest.raises(IndexError):
522
- reader[ref.n_frames + 1]
523
-
524
- @pytest.mark.skip("Cannot slice stream")
525
- def test_frame_jump(self, ref, reader):
526
- ts = reader[ref.jump_to_frame.frame]
527
- assert_timestep_almost_equal(ts, ref.jump_to_frame, decimal=ref.prec)
528
-
529
- @pytest.mark.skip("Cannot slice stream")
530
- def test_frame_jump_issue1942(self, ref, reader):
531
- """Test for issue 1942 (especially XDR on macOS)"""
532
- reader.rewind()
533
- try:
534
- for ii in range(ref.n_frames + 2):
535
- reader[0]
536
- except StopIteration:
537
- pytest.fail("Frame-seeking wrongly iterated (#1942)")
538
-
539
- def test_next_gives_second_frame(self, ref, reader):
540
- # don't recreate reader here as in inherited base test
541
- ts = reader.next()
542
- assert_timestep_almost_equal(ts, ref.second_frame, decimal=ref.prec)
543
-
544
- @pytest.mark.skip(
545
- reason="Stream isn't rewound after iteration- base reference is the same but it is the last frame"
546
- )
547
- def test_frame_collect_all_same(self, reader):
548
- # check that the timestep resets so that the base reference is the same
549
- # for all timesteps in a collection with the exception of memoryreader
550
- # and DCDReader
551
- if isinstance(reader, mda.coordinates.memory.MemoryReader):
552
- pytest.xfail("memoryreader allows independent coordinates")
553
- if isinstance(reader, mda.coordinates.DCD.DCDReader):
554
- pytest.xfail(
555
- "DCDReader allows independent coordinates."
556
- "This behaviour is deprecated and will be changed"
557
- "in 3.0"
558
- )
559
- collected_ts = []
560
- for i, ts in enumerate(reader):
561
- collected_ts.append(ts.positions)
562
- for array in collected_ts:
563
- assert_allclose(array, collected_ts[0])
564
-
565
-
566
- class TestStreamIteration:
567
-
568
- @pytest.fixture
569
- def port(self):
570
- return get_free_port()
571
-
572
- @pytest.fixture
573
- def universe(self):
574
- return mda.Universe(COORDINATES_TOPOLOGY, COORDINATES_H5MD)
575
-
576
- @pytest.fixture
577
- def imdsinfo(self):
578
- return create_default_imdsinfo_v3()
579
-
580
- @pytest.fixture
581
- def reader(self, universe, imdsinfo, port):
582
- server = InThreadIMDServer(universe.trajectory)
583
- server.set_imdsessioninfo(imdsinfo)
584
- server.handshake_sequence("localhost", port, first_frame=True)
585
- reader = IMDReader(
586
- f"imd://localhost:{port}",
587
- n_atoms=universe.trajectory.n_atoms,
588
- )
589
- server.send_frames(1, 5)
590
-
591
- yield reader
592
- server.cleanup()
593
-
594
- def test_iterate_step(self, reader, universe):
595
- i = 0
596
- for ts in reader[::2]:
597
- assert ts.frame == i
598
- i += 2
599
-
600
- def test_iterate_twice_sliced_raises_error(self, reader):
601
- for ts in reader[::2]:
602
- pass
603
- with pytest.raises(RuntimeError):
604
- for ts in reader[::2]:
605
- pass
606
-
607
- def test_iterate_twice_all_raises_error(self, reader):
608
- for ts in reader:
609
- pass
610
- with pytest.raises(RuntimeError):
611
- for ts in reader:
612
- pass
613
-
614
- def test_iterate_twice_fi_all_raises_error(self, reader):
615
- for ts in reader[:]:
616
- pass
617
- with pytest.raises(RuntimeError):
618
- for ts in reader[:]:
619
- pass
620
-
621
- def test_index_stream_raises_error(self, reader):
622
- with pytest.raises(TypeError):
623
- reader[0]
624
-
625
- def test_iterate_backwards_raises_error(self, reader):
626
- with pytest.raises(ValueError):
627
- for ts in reader[::-1]:
628
- pass
629
-
630
- def test_iterate_start_stop_raises_error(self, reader):
631
- with pytest.raises(ValueError):
632
- for ts in reader[1:3]:
633
- pass
634
-
635
- def test_subslice_fi_all_after_iteration_raises_error(self, reader):
636
- sliced_reader = reader[:]
637
- for ts in sliced_reader:
638
- pass
639
- sub_sliced_reader = sliced_reader[::1]
640
- with pytest.raises(RuntimeError):
641
- for ts in sub_sliced_reader:
642
- pass
643
-
644
-
645
- def test_n_atoms_mismatch():
646
- universe = mda.Universe(COORDINATES_TOPOLOGY, COORDINATES_H5MD)
647
- port = get_free_port()
648
- server = InThreadIMDServer(universe.trajectory)
649
- server.set_imdsessioninfo(create_default_imdsinfo_v3())
650
- server.handshake_sequence("localhost", port, first_frame=True)
651
- with pytest.raises(
652
- EOFError,
653
- match="IMDProducer: Expected n_atoms value 6, got 5. Ensure you are using the correct topology file.",
654
- ):
655
- IMDReader(
656
- f"imd://localhost:{port}",
657
- n_atoms=universe.trajectory.n_atoms + 1,
658
- )