Skip to content

initial draft of new TD-fNIRS KF2 example#586

Merged
larsoner merged 42 commits intomne-tools:mainfrom
JohnGriffiths:ENH_TD-FNIRS_KF2
Oct 16, 2025
Merged

initial draft of new TD-fNIRS KF2 example#586
larsoner merged 42 commits intomne-tools:mainfrom
JohnGriffiths:ENH_TD-FNIRS_KF2

Conversation

@JohnGriffiths
Copy link
Copy Markdown
Contributor

@JohnGriffiths JohnGriffiths commented Nov 13, 2024

Following from this PR discussion, this example demonstrates I/O of TD-FNIRS data, and also serves as an introduction to KF2 data.

Code and data based on example code from @julien-dubois-k

Remaining to-do:

  • Modify installation target to the above-linked PR
  • Add data download functionality
  • Modify some of the hdf access bits with references to getting the info from the mne snirf loader

@JohnGriffiths JohnGriffiths marked this pull request as ready for review November 14, 2024 06:05
@JohnGriffiths
Copy link
Copy Markdown
Contributor Author

PR adding KF2 tutorial example, as discussed.

I have verified that this builds an correct-looking example webpage with Make html

@larsoner - do need some help here with getting the CI to pass

Some things to note re: the contents of this example:

  1. It currently uses gdown to pull the example file. I suggest we keep this as-in, and consider updating to something with mne-misc-data after this one is merged

  2. Likewise, I suggest we edit the mne dependency back to the mne-tools/mne-python master in a later PR, once the PR in the above-linked thread has been resolved.

  3. Some of the contents regarding pulling stuff from the hdf5 archive could be modified in future versions to show pulling this info from the hdf archive directly or from the mne reader. However I don't believe this functionality is in the MNE .snirf reader yet, so we would need to complete a new PR with that code first.

Comment thread tools/circleci_dependencies.sh Outdated
@larsoner
Copy link
Copy Markdown
Member

FYI I am a bit busy for the next two weeks then should be able to come back to this!

@larsoner
Copy link
Copy Markdown
Member

Don't worry about the pip-pre failure it should be fixed by nilearn/nilearn#4724 soon. Can just work on getting the example to run and look good!

@larsoner
Copy link
Copy Markdown
Member

generating gallery for auto_examples/general... [ 27%] plot_11b_kf2_fingertapping.py
make: *** [Makefile:60: html] Killed

Probably due to memory usage

Screenshot 2024-11-18 at 11 39 57 AM

Is there some easy / reasonable way to reduce the memory usage? It's good if examples don't use a ton of memory (e.g., try to keep to < 2 GB).

In a worst case we could use a larger docker image but it would be nice to avoid it if possible

@JohnGriffiths
Copy link
Copy Markdown
Contributor Author

Is there some easy / reasonable way to reduce the memory usage? It's good if examples don't use a ton of memory (e.g., try to keep to < 2 GB)

If it's an option to keep as-is with larger docker image can we go with that for now?

It would be helpful to focus on the code needed to get this up and running first, and circle back to optimizations next.

I think RAM usage limits are also a more general question that would inform a lot of what we decide to add examples for on this topic (e.g. connectivity, source analysis, etc.).

@larsoner
Copy link
Copy Markdown
Member

It would be helpful to focus on the code needed to get this up and running first, and circle back to optimizations next.

Okay I pushed a commit that should do it.

FWIW bumping the resource class actually costs the mne-tools org compute credits because it's not part of the free tier so we should try to avoid keeping it that way as well long-term if possible unless there is a really compelling use case that we can't get memory usage down for. Even in MNE-Python so far we have managed to avoid this, only in MNE-BIDS-Pipeline have we needed to use it. So I'm cautiously optimistic we might be able to keep memory usage low here, too.

Comment thread examples/general/plot_11b_kf2_fingertapping.py
@JohnGriffiths
Copy link
Copy Markdown
Contributor Author

It would be helpful to focus on the code needed to get this up and running first, and circle back to optimizations next.

Okay I pushed a commit that should do it.

FWIW bumping the resource class actually costs the mne-tools org compute credits because it's not part of the free tier so we should try to avoid keeping it that way as well long-term if possible unless there is a really compelling use case that we can't get memory usage down for. Even in MNE-Python so far we have managed to avoid this, only in MNE-BIDS-Pipeline have we needed to use it. So I'm cautiously optimistic we might be able to keep memory usage low here, too.

Good, thanks.

Agree to avoiding this being a long-term solution.

@JohnGriffiths
Copy link
Copy Markdown
Contributor Author

@larsoner @julien-dubois-k I think this is ready for full review now.

I did not in the end get round to adding the moments analysis. What I have done here is add the new dataset, plus the data fetcher in mne and dataset repo on OSF, and quite a few updates to the example text and code.

I suggest that we tackle the job of extending this demo to include a part that goes all the way from the moments on a new PR thread. The data for that is now there in the downloader files, so should be a fairly easy job.

LMK your thoughts.

j

Comment thread requirements.txt Outdated
Comment thread tools/circleci_dependencies.sh Outdated
Comment thread .circleci/config.yml Outdated
Comment on lines +9 to +10
# TODO: Revert this once our examples are fixed!
resource_class: large # memory
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The example takes almost 2 minutes to run on my machine and 3GB of memory

image

Is there any way to speed it up and reduce memory usage?

@larsoner
Copy link
Copy Markdown
Member

larsoner commented Aug 11, 2025

Okay I moved the dataset downloader here and got CIs green (EDIT: appears headed that way at least!). @JohnGriffiths can you look into reducing memory consumption and runtime if possible?

@JohnGriffiths
Copy link
Copy Markdown
Contributor Author

Yes I can look into memory stuff.

LMK

  • What is the tool you use to generate this plot?
  • What are the run time and memory usage levels to aim for?
  • Are you waiting on these tweaks to merge or can we do that as a follow-up PR?

@drammock
Copy link
Copy Markdown
Member

What is the tool you use to generate this plot?

memory_profiler is the tool. Run on command line like:

mprof run python my_script.py
mprof plot

What are the run time and memory usage levels to aim for?

off the cuff: less than a minute and less than 2GB (1GB would be better) is what we shoot for in the MNE-Python repo. @larsoner may have a clearer picture of what is OK for MNE-NIRS.

Are you waiting on these tweaks to merge or can we do that as a follow-up PR?

I'd say we're waiting; if we merge now, then every PR will use the large resource class (costing us more money) until the usage is decreased and the resource class reverted to default.

@larsoner
Copy link
Copy Markdown
Member

That looks right to me @drammock

@JohnGriffiths if you'd prefer I can take another look to see if I can cut down the resource usage without changing the plots/output too much

@JohnGriffiths
Copy link
Copy Markdown
Contributor Author

Hi guys.

I have tried a number of things to reduce memory, none make much of a dent, other than removing bits.

@larsoner if you do have any bright ideas on how to save memory consumption in mne egs?

Is it cumulative? So, things like parallellizing over cores will not help?

thx

* upstream/main:
  Build(deps): Bump github/codeql-action from 3 to 4 (mne-tools#639)
  FIX: Rel
  Fix osf.io downloads, pre-commit autoupdate and fix  (mne-tools#640)
  [pre-commit.ci] pre-commit autoupdate (mne-tools#638)
  [pre-commit.ci] pre-commit autoupdate (mne-tools#637)
  [pre-commit.ci] pre-commit autoupdate (mne-tools#636)
  [pre-commit.ci] pre-commit autoupdate (mne-tools#635)
  [pre-commit.ci] pre-commit autoupdate (mne-tools#634)
  Build(deps): Bump actions/setup-python from 5 to 6 (mne-tools#633)
  [pre-commit.ci] pre-commit autoupdate (mne-tools#632)
  [pre-commit.ci] pre-commit autoupdate (mne-tools#629)
  [pre-commit.ci] pre-commit autoupdate (mne-tools#628)
@larsoner
Copy link
Copy Markdown
Member

Okay here's the last one you produced @JohnGriffiths (185.96 sec 2442.0 MB):

https://output.circle-artifacts.com/output/job/e580bcd6-81fa-4c82-a934-ada97354b1f9/artifacts/0/html/auto_examples/general/plot_11b_kf2_fingertapping.html

And here's after my commit (72.85 sec 702.8 MB):

https://output.circle-artifacts.com/output/job/99b58bb6-bb0b-48c8-b0f1-ce29292e440f/artifacts/0/html/auto_examples/general/plot_11b_kf2_fingertapping.html

The big change is switching to drift_model="polynomial", which made things slightly less clean and lowered some contrast values and I adjusted vlim to compensate, which seems like an okay / expected tradeoff (and I added a comment about it). Look okay to you?

@JohnGriffiths
Copy link
Copy Markdown
Contributor Author

Looks fine to me. Nice trick!

@JohnGriffiths
Copy link
Copy Markdown
Contributor Author

Code modifications and comments and images all check out.
Thanks for taking the time. LMK if you need me to do any more review of this.

Related side note: I'm giving an mne-nirs tutorial at westfnirs conference on Monday.
I am going to build it partly around this demo.
Would be fantastic if it can be in main branch and one of the docs pages (devel?) by then.
Are there any mne-nirs tutorial slide decks etc. you could point me to / send to me that I can borrow from?
thanks!

@larsoner larsoner merged commit 1603003 into mne-tools:main Oct 16, 2025
14 checks passed
@larsoner
Copy link
Copy Markdown
Member

Okay in it goes, then! Would be good to work on the moments next when you get a chance 😄

Are there any mne-nirs tutorial slide decks etc. you could point me to / send to me that I can borrow from?

No not that I know of... back when I gave MNE-Python tutorials I'd generally start from the ipynb Notebook files from the tutorials themselves and build out from there depending on what the group(s) in particular wanted to learn

@larsoner
Copy link
Copy Markdown
Member

... and version numbers/releases are pretty easy nowadays, here you go:

https://pypi.org/project/mne-nirs/0.7.3/

Should land in conda soon as well!

@ghost
Copy link
Copy Markdown

ghost commented Oct 16, 2025

Thank you both for pushing this through!

@JohnGriffiths
Copy link
Copy Markdown
Contributor Author

Okay in it goes, then! Would be good to work on the moments next when you get a chance 😄

Ok yes moments next

And also DOT analysis for this data

Are there any mne-nirs tutorial slide decks etc. you could point me to / send to me that I can borrow from?

No not that I know of... back when I gave MNE-Python tutorials I'd generally start from the ipynb Notebook files from the tutorials themselves and build out from there depending on what the group(s) in particular wanted to learn

Yup ok this is basically my plan

zEdS15B3GCwq pushed a commit to zEdS15B3GCwq/mne-nirs that referenced this pull request Jan 31, 2026
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
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.

3 participants