Skip to content

Conversation

@hoechenberger
Copy link
Member

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.

Currently just a proof of concept for FIFF reading.
@larsoner
Copy link
Member

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 ~ should work everywhere, which is nice!

@cbrnr
Copy link
Contributor

cbrnr commented Jul 26, 2021

Can we use pathlib.resolve() instead?

@hoechenberger
Copy link
Member Author

Then once we standardize _check_fname use in all functions that read and write (which we can do progressively / not in this PR)

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?

@larsoner
Copy link
Member

I think it's okay to go for as much as you're up for doing in this PR

Can we use pathlib.resolve() instead?

No idea what the implications are of switching from our os.path-based functions to pathlib.resolve

@hoechenberger
Copy link
Member Author

Can we use pathlib.resolve() instead?

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 os.path in many situations.

@hoechenberger
Copy link
Member Author

Can we use pathlib.resolve() instead?

That's resolving symlinks, not expanding ~.

Maybe you were thinking of Path.expanduser()?

@cbrnr
Copy link
Contributor

cbrnr commented Jul 26, 2021

Maybe you were thinking of Path.expanduser()?

Yes!

@hoechenberger
Copy link
Member Author

Maybe you were thinking of Path.expanduser()?

Yes!

I'm using that one in the test, it's something 😅🙈

@hoechenberger
Copy link
Member Author

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?

@hoechenberger
Copy link
Member Author

hoechenberger commented Jul 28, 2021

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.

Comment on lines 332 to 333
check_fname(fname, 'inverse operator', ('-inv.fif', '-inv.fif.gz',
'_inv.fif', '_inv.fif.gz'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you open an issue about this? It would make a hopefully straightforward follow-up PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue filed under #9819

@larsoner larsoner added this to the 0.24 milestone Sep 1, 2021
@hoechenberger
Copy link
Member Author

Ok this is ready to go from my end!

@hoechenberger
Copy link
Member Author

@agramfort I also tested with your Forward solution notebook, passing Path objects around like you suggested. All working nicely!

Copy link
Member

@agramfort agramfort left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@larsoner merge if happy

thx @hoechenberger 👏

Copy link
Member

@larsoner larsoner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually _check_* functions raise errors, this would be better as _path_like(...) I think

Copy link
Member Author

@hoechenberger hoechenberger Oct 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to _path_like. It's still residing check.py, which should probably be reconsidered at some time in the future.

Comment on lines 332 to 333
check_fname(fname, 'inverse operator', ('-inv.fif', '-inv.fif.gz',
'_inv.fif', '_inv.fif.gz'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

need_dir=False):
"""Check for file existence, and return string of its absolute path."""
_validate_type(fname, 'path-like', name)
fname = op.expanduser(fname)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@hoechenberger
Copy link
Member Author

@larsoner This should be good to go.

@agramfort agramfort merged commit 96e830b into mne-tools:main Oct 8, 2021
@agramfort
Copy link
Member

thx @hoechenberger !

@hoechenberger hoechenberger deleted the expanduser branch October 8, 2021 13:57
larsoner added a commit to rob-luke/mne-python that referenced this pull request Oct 11, 2021
* 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)
hoechenberger added a commit to hoechenberger/mne-python that referenced this pull request Oct 14, 2021
larsoner pushed a commit that referenced this pull request Oct 14, 2021
* Expand ~ in Report file operations

Followup of #9613

* Update changelog

* Fix report opening
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants