MRG: Expand ~ in _check_fname()#9613
Conversation
Currently just a proof of concept for FIFF reading.
|
Sounds like a good idea to me! Then once we standardize _check_fname use in all functions that read and write (which we can do progressively / not in this PR) then |
|
Can we use |
Oh, my intention actually was to get this to work in this PR :) But maybe that's too ambitious. Should I limit things to just FIFF for now (as currently implemented) in this PR? |
|
I think it's okay to go for as much as you're up for doing in this PR
No idea what the implications are of switching from our |
That's resolving symlinks, not expanding If the intention of the question is, "can we start moving our path handling over to pathlib", I'd be +9999, it's so much nicer and cleaner than dealing with strings and |
Maybe you were thinking of |
Yes! |
I'm using that one in the test, it's something 😅🙈 |
|
Ok I think I'm now covering a large number (all? probably forgot some!) data types we deal with in MNE. I'm too lazy to add tests for all of them now :( Somebody willing to give me a hand? |
|
Also sometimes it's not absolutely clear at which function nesting level to expand the fname. I'm trying to do it early, but I'm not always super consistent here. |
| check_fname(fname, 'inverse operator', ('-inv.fif', '-inv.fif.gz', | ||
| '_inv.fif', '_inv.fif.gz')) |
There was a problem hiding this comment.
Might be better / simpler just to make check_fname call _check_fname internally and return the str-ified absolute path, then our code ends up more DRY. It seems like every time we check_fname we probably want to call _check_fname anyway...
Or we could go the other way and make _check_fname accept a ext argument, and have it call check_fname internally. I think I'd rather go that way.
There was a problem hiding this comment.
Yes I was thinking the same, however I don't want to touch this in this PR to avoid unwelcome surprises and to keep the diff smaller.
There was a problem hiding this comment.
Can you open an issue about this? It would make a hopefully straightforward follow-up PR
|
Ok this is ready to go from my end! |
|
@agramfort I also tested with your Forward solution notebook, passing Path objects around like you suggested. All working nicely! |
agramfort
left a comment
There was a problem hiding this comment.
@larsoner merge if happy
thx @hoechenberger 👏
larsoner
left a comment
There was a problem hiding this comment.
Other than some minor stuff, LGTM!
mne/epochs.py
Outdated
| if isinstance(fname, str): | ||
| check_fname(fname, 'epochs', ('-epo.fif', '-epo.fif.gz', | ||
| '_epo.fif', '_epo.fif.gz')) | ||
| if _check_path_like(fname): |
There was a problem hiding this comment.
Usually _check_* functions raise errors, this would be better as _path_like(...) I think
There was a problem hiding this comment.
Not the case for this function. Checked git blame and … well, it was me who added it this way 2 years ago. Pushing a rename.
There was a problem hiding this comment.
Renamed to _path_like. It's still residing check.py, which should probably be reconsidered at some time in the future.
| check_fname(fname, 'inverse operator', ('-inv.fif', '-inv.fif.gz', | ||
| '_inv.fif', '_inv.fif.gz')) |
There was a problem hiding this comment.
Can you open an issue about this? It would make a hopefully straightforward follow-up PR
| """ | ||
| _validate_type(fname, 'path-like', 'fname') | ||
| fname = str(fname) | ||
| fname = _check_fname(fname=fname, overwrite=True) |
There was a problem hiding this comment.
Any place you have had to add an overwrite=True like this can you add a # TODO comment saying we should add an overwrite kwarg to the function?
mne/utils/check.py
Outdated
| need_dir=False): | ||
| """Check for file existence, and return string of its absolute path.""" | ||
| _validate_type(fname, 'path-like', name) | ||
| fname = op.expanduser(fname) |
There was a problem hiding this comment.
now the modification of fname happens it two places -- here and below during the return. Can you move the fname = str(op.abspath(fname)) after this line, then just return fname at the end? That way it's immediately clear what all operations we are actually doing to fname before returning it.
There was a problem hiding this comment.
I've reworked the logic as you suggested, and I'm operating on a Path now before casting to a string, as I felt that the method chaining available via Path made the sequence of operations easier to read.
|
@larsoner This should be good to go. |
|
thx @hoechenberger ! |
* upstream/main: MAINT: Update broken link, fix rendering (mne-tools#9829) Ensure plot_ica_sources() always plots traces of rejected ICs on top (mne-tools#9823) Improve plot_ica_sources() docstring (mne-tools#9825) MRG: Fix docstring for plot_ica_components() (mne-tools#9826) unpin jsonschema and filter its warning instead (mne-tools#9822) Add warning for SNIRF files with != 2 wavelengths (mne-tools#9817) add show_scalebars param to epochs.plot() (mne-tools#9815) MRG: Allow _plot_mri_contours() to return arrays (mne-tools#9818) MRG: Expand ~ in _check_fname() (mne-tools#9613) Improve ICA.plot_overlay() docstrings (mne-tools#9820) WIP, MAINT: Fix CircleCI (again) (mne-tools#9814) MRG, ENH: Add options to fit_dipole (mne-tools#9810) Rework Reports (new history) (mne-tools#9754) MRG, CI: Use VTK pre (mne-tools#9803)
Followup of mne-tools#9613
* Expand ~ in Report file operations Followup of #9613 * Update changelog * Fix report opening
Currently just a proof of concept for FIFF reading. Will expand to other places across the entire code base. Feel super free to help me and push to this branch.