-
Notifications
You must be signed in to change notification settings - Fork 652
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
Pathlib object handling for Universe, SingleFrameReaderBase and Toplogy parsers (Issue #3937) #4535
base: develop
Are you sure you want to change the base?
Pathlib object handling for Universe, SingleFrameReaderBase and Toplogy parsers (Issue #3937) #4535
Conversation
…se + removal of duplicate import
adding the implementation of tests for str and pathlib handling
Hello @talagayev! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:
Comment last updated at 2024-08-25 01:10:27 UTC |
Linter Bot Results:Hi @talagayev! Thanks for making this PR. We linted your code and found the following: Some issues were found with the formatting of your code.
Please have a look at the Please note: The |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #4535 +/- ##
===========================================
- Coverage 93.66% 93.23% -0.44%
===========================================
Files 168 12 -156
Lines 21248 1079 -20169
Branches 3917 0 -3917
===========================================
- Hits 19902 1006 -18896
+ Misses 888 73 -815
+ Partials 458 0 -458 ☔ View full report in Codecov by Sentry. |
@hmacdope would you be able to look at this PR or assign to someone else, please? |
Apologies for the delay @orbeckst @talagayev, was away over easter. Reviewing now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@talagayev you have the right idea of what to test, and your test implementations are good!
However to achieve better coverage and to make your tests more powerful you can instead add your tests to BaseReaderTest
and _SingleFrameReader
that cover the base API functionality for all relevant trajectory types. This is in estsuite/MDAnalysisTests/coordinates/base.py
. :)
Ah, I think i see :) i would than adjust it so that the tests are in Does it than make also sense to move the |
@hmacdope
in the case of |
Adjusted the tests to be in base.py
@hmacdope |
@talagayev sorry for the delay here. I will review ASAP. |
@hmacdope all good, no worries :) |
@talagayev I have pushed some changes to your branch that fix the As we are now co-authors on this I can no longer fairly review, so I will defer to one of the other @MDAnalysis/coredevs. Perhaps @tylerjereddy as the author of previous Pathlib support issues and PRs #3937 (if you have some spare cycles). |
Ok looks like I messed some stuff up, Ill fix. |
@hmacdope Hm strange error, I try to find the error going through the pytests and running the developer version of MDAnalysis with this version and can't track down the error currently. |
if isinstance(filename, NamedStream): | ||
self.filename = filename | ||
else: | ||
self.filename = str(filename) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here errors if we use self.filename = str(filename)
in the test_mmtf.py
The lead to errors similar to this one:
FileNotFoundError: [Errno 2] No such file or directory: '<mmtf.api.mmtf_reader.MMTFDecoder object at 0x135416780>'
I managed to fix the error by adding:
else:
self.filename = str(filename)
if "MMTF" in self.filename:
self.filename = filename
basically putting a specific condition for MMTF cases, not the cleanest approach, but maybe this helps @hmacdope fixing the error in a better way :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to look more into it now, the "MMTF"
is a specific case, the other errors appear mainly because of the parmed parser which has both problems with the base.py
in the coordinates
and topology
. It is possible to use in the base.py
in the coordinates
:
elif hasattr(filename, 'topology'):
self.filename = filename
This helps to fix the errors, although it is quite similar as to just directly using self.filename = filename
without any if/elif/else
conditions. For the base.py
in topology
I will try to figure out if there is any option to fix it, since again having there only self.filename = filename
also fixes the errors with the parmed
parser
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@talagayev I think the FileNotFoundError
for mmtf above is because instead of a filename, sometimes a MMTFDecoder object is encountered. I think what you've got is fixing it, but it might be clearer to have a block like:
if isinstance(filename, NamedStream):
self.filename = filename
elif 'mmtf' in str(filename.__class__):
# mmtf case, where we've avoided a mmtf import in detection
self.filename = filename
Where you've enumerated all the special cases explicitly in each elif
branch, rather than hiding some special cases inside other branches
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see, yes the option that I got worked, but was definitely not the best option, but yes was connected with mmtf and yes elif 'mmtf' in str(filename.__class__):
looks much better :)
if isinstance(reader, MemoryReader): | ||
skip_reason = "MemoryReader" | ||
pytest.skip(f"Skipping test for Pathlib input with reason: {skip_reason}") | ||
path = Path(reader.filename) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here adding an additional Line:
path = path.as_posix())
Fixes the following Failed Pytest in 4 cases:
AttributeError: 'PosixPath' object has no attribute 'encode'
@@ -39,6 +40,7 @@ Fixes | |||
* Fix groups.py doctests using sphinx directives (Issue #3925, PR #4374) | |||
|
|||
Enhancements | |||
* Handling of Pathlib.Path in SingleFrameReaderBase, Topology and Universe (Issue #3937) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit vague and doesn't actually tell me what is fixed or what is now possible. e.g. Is it that pathlib.Path
s are now correctly handled?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, originally it was only targeting pathlib object handling of SingleFrameReaderBase in specific cases, but with the adjustment of @hmacdope it would cover Pathlib.Path
s for all cases if the errros that appear currently due to the changes in the base.py
in topology and also the base.py
in coordinates, which could be fixed with
elif hasattr(filename, 'topology'):
self.filename = filename
but this would just exclude the cases of topology
so I am not sure if this would fix it and cover then all the Pathlib.Path
cases
Partially Fixes #3937. The issue mentioned the addition of support and testing for
pathlib
objects forSingleFrameReaderBase
.Currently the
SingleFrameReaderBase
is able to handle bothpathlib
andstr
as input forSingleFrameReaderBase
to display this, this PR is focusing on tests that display the handling ofpathlib
andstr
as input forSingleFrameReaderBase
.Changes made in this Pull Request:
pathlib
object andstr
input forSingleFrameReaderBase
intest_gro.py
andtest_lammps.py
Currently the tests are as mentioned for
GRO
andLAMMPS
cases.SingleFrameReaderBase
also recognizesINPCRD
,CRD
,NAMDBIN
andDMS
if given as a single input, so tests for these cases could also be added if required.PR Checklist
Developers certificate of origin
📚 Documentation preview 📚: https://mdanalysis--4535.org.readthedocs.build/en/4535/