Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FrameIteratorIndices doesn't rewind whilst FrameIteratorSliced does #3416

Closed
IAlibay opened this issue Sep 6, 2021 · 10 comments · Fixed by #3606
Closed

FrameIteratorIndices doesn't rewind whilst FrameIteratorSliced does #3416

IAlibay opened this issue Sep 6, 2021 · 10 comments · Fixed by #3606

Comments

@IAlibay
Copy link
Member

IAlibay commented Sep 6, 2021

Related to #3415

Expected behavior

See #3415 (comment)

Iterating through FrameIteratorSliced should leave u.trajectory at the same point as if I iterated through FrameIteratorIndices.

Actual behavior

The former rewinds whilst the latter doesn't.

I might be missing a pythonic reason (which I'd be happy to accept), just bringing this up because of discussions on #3415

Code to reproduce the behavior

import MDAnalysis as mda
from MDAnalysisTests.datafiles import TPR, XTC

u = mda.Universe(TPR, XTC)

iterator = u.trajectory[[0,1,2]]

for ts in iterator:
    pass

print(u.trajectory.ts)

Ouput: < Timestep 2 with unit cell dimensions [79.98359 79.98359 79.98358 60. 60. 90. ] >

Whilst:

import MDAnalysis as mda
from MDAnalysisTests.datafiles import TPR, XTC

u = mda.Universe(TPR, XTC)

for ts in u.trajectory[:3]
    pass

print(u.trajectory.ts)

Ouput: < Timestep 0 with unit cell dimensions [80.017006 80.017006 80.017006 60. 60. 90. ] >

Current version of MDAnalysis

  • Which version are you using? (run python -c "import MDAnalysis as mda; print(mda.__version__)") develop
  • Which version of Python (python -V)? 3.9
@IAlibay
Copy link
Member Author

IAlibay commented Sep 7, 2021

Looking into it more, looks like a possibly intention decision by @jbarnoud ?

@orbeckst
Copy link
Member

orbeckst commented Sep 8, 2021

I looked through PR #1934 and although there are some interesting comments like #1934 (comment) (why FrameIterators are like range() and not really iterators) I couldn't see a real reason why FrameIteratorSliced rewinds in __iter__

def __iter__(self):
for i in range(self.start, self.stop, self.step):
yield self.trajectory[i]
self.trajectory.rewind()
while FrameIteratorIndices does not
def __iter__(self):
for frame in self.frames:
yield self.trajectory._read_frame_with_aux(frame)

Given that FrameIteratorAll just defers to the reader's behavior (which rewinds) it looks to me that FrameIteratorIndices should do the same.

I can't think of a use case where this should be handled differently, especially as it breaks user expectations (and is not documented either).

@jbarnoud
Copy link
Contributor

jbarnoud commented Sep 8, 2021 via email

@IAlibay
Copy link
Member Author

IAlibay commented Sep 15, 2021

Looks like the consensus here is to just add the rewind. Do we need to warn or can we treat this as a bug and fix in 2.1.0?

@orbeckst
Copy link
Member

orbeckst commented Sep 15, 2021 via email

@orbeckst
Copy link
Member

As elsewhere #3423 (comment) I'd like to label this issue a defect because the behavior is not documented and wasn't intended as such. A fix would fix the API to be consistent with the rest of the readers.

@orbeckst orbeckst added this to the 2.1.0 milestone Sep 23, 2021
@orbeckst
Copy link
Member

If we get this (and #3423 ) done then we can also finish up PR #3415 .

@IAlibay
Copy link
Member Author

IAlibay commented Oct 14, 2021

If we get this (and #3423 ) done then we can also finish up PR #3415 .

Yeah I'll try to have a butcher this weekend maybe - got to get CI back to green first.

@orbeckst orbeckst modified the milestones: 2.1.0, 2.2.0 Mar 8, 2022
@megosato
Copy link
Contributor

megosato commented Apr 5, 2022

Hello!
I was hoping to work on this issue, but wanted to confirm a few things.

My current understanding is:
FrameIteratorIndices should rewind to Time Step 0 similar to FrameIteratorSliced and FrameIteratorAll

Questions:
If rather than making the iterator iterator = u.trajectory[[0,1,2]] we do iterator = u.trajectory[[2,3,4]], should it still rewind to timestep 0? And likewise for FrameIteratorSliced?

I'm also wondering logistically if/how bugs are handled differently from defects from a programming and documentation perspective? (per #3416 (comment))

Thank you for your help!

@orbeckst
Copy link
Member

orbeckst commented Apr 6, 2022

My current understanding is: FrameIteratorIndices should rewind to Time Step 0 similar to FrameIteratorSliced and FrameIteratorAll

Yes.

If rather than making the iterator iterator = u.trajectory[[0,1,2]] we do iterator = u.trajectory[[2,3,4]], should it still rewind to timestep 0? And likewise for FrameIteratorSliced?

Good question. I'd say yes, for consistency, because that's what FrameIteratorSliced is already doing. (The alternative would be to rewind to the first frame index but that seems less consistent with what we have at the moment.)

I'm also wondering logistically if/how bugs are handled differently from defects from a programming and documentation perspective? (per #3416 (comment))

I use bugs and defects synonymously. What difference do you perceive?

In this specific case the question was if the change will be an API change or a bug fix (= defect fix). Because we use semantic versioning this has wide-ranging implication. Under SemVer, we can only make backwards-incompatible API changes when we increase the major release number (e.g., the next one would be 3.0.0). Bug-fixes can go in a patch release, e.g., 2.1.1 or in the next minor release 2.2.0. The consensus seemed to be that this is not an intentional API change but we are fixing undocumented behavior. Thus, a PR can actually be merged right away and does not have to wait for 3.0.0.

megosato added a commit to megosato/mdanalysis that referenced this issue Apr 6, 2022
- Previously iterating through FrameIteratorIndices did not rewind back to 0
  and stayed on the last iterated frame
- FrameIteratorIndices now rewinds back to 0 and is consistent with
  FrameIteratorSliced and FrameIteratorAll
megosato added a commit to megosato/mdanalysis that referenced this issue Apr 6, 2022
- Previously iterating through FrameIteratorIndices did not rewind back to 0
  and stayed on the last iterated frame
- FrameIteratorIndices now rewinds back to 0 and is consistent with
  FrameIteratorSliced and FrameIteratorAll
hmacdope pushed a commit that referenced this issue Apr 11, 2022
* Fix FrameIteratorIndices doesn't rewind (Issue #3416)

- Previously iterating through FrameIteratorIndices did not rewind back to 0 and stayed on the last iterated frame.
- FrameIteratorIndices now rewinds back to 0 and is consistent with FrameIteratorSliced and FrameIteratorAll.

Co-authored-by: IAlibay <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants