Open kain88-de opened 8 years ago
I think the frame after an iteration is actually pretty badly defined currently...
In [4]: from MDAnalysisTests.datafiles import PSF, DCD
In [5]: u = mda.Universe(PSF, DCD)
In [6]: print u.trajectory.ts.frame
0
In [7]: u.trajectory[10]
Out[7]: < Timestep 10 with unit cell dimensions [ 0. 0. 0. 90. 90. 90.] >
In [8]: print u.trajectory.ts.frame
10
In [9]: for ts in u.trajectory[:]:
...: pass
...:
In [10]: print u.trajectory.ts.frame
0
I think we're blindly rewind
ing after an iteration rather than reverting to the previous frame.
I think for this issue, we'd have to make the iterator into a context manager to allow it to clean up even after a KeyboardInterrupt? Not sure if that's necessary though
Does the context manager work when calling an iterator?
I think the problem is that __iter__
returns the object itself. But that is hard to fix the way AtomGroups and TimeSteps depend on each other.
I think the current context manager is off coordinates.base.IOBase
and is more concerned with file handles, so you could do:
with mda.coordinates.XTCReader('this.xtc') as f:
for ts in f:
second gist. I have the problem running a special analysis class. I don't do any interrupts here. Its strange that this doesn't work as expected. It looks like we don't to a good reset to frame 0 after the iteration has finished for an arbitrary iteration stop.
Hmm, it might be because rewind is only called on StopIteration
which we only hit at the end of a trajectory, not when we voluntarily stop.
I'll try a quick fix.
@jbarnoud has this been fixed with your recent updates to the iteration?
@kain88-de I just tried and it does not solves it. I maintained the same behavior.
I'm not entirely sure we're responsible for keyboard interrupts? If we wanted to be though, we'd have to put in an except KeyboardInterrupt
inside the classes @jbarnoud made, and handle it there
Expected behaviour
When I break a loop like this with
KeyboardInterupt
I expect the trajectory to point to the same frame as at the beginning.This is very important when I have an analysis that extracts from information from
mobile
in the initialization. Otherwise code like this would return different results based on the following.Actual behaviour
The last comparison returns false
Code to reproduce the behaviour
Gist
Currently version of MDAnalysis:
(run
python -c "import MDAnalysis as mda; print(mda.__version__)"
) 0.16.0dev