Skip to content

ENH: Add support for Artinis SNIRF data#11926

Merged
drammock merged 25 commits intomne-tools:mainfrom
rob-luke:artinis
Sep 19, 2023
Merged

ENH: Add support for Artinis SNIRF data#11926
drammock merged 25 commits intomne-tools:mainfrom
rob-luke:artinis

Conversation

@rob-luke
Copy link
Copy Markdown
Member

@rob-luke rob-luke commented Aug 27, 2023

Reference issue

This is an issue as PR.

Artinis provided me with half a dozen files exported from their fNIRS device (the files are too large for me to share).

The sampling times of their devices aren't spaced to the level of precision that we currently expect. Which causes the current implementation of the SNIRF reader to throw an error.
They sent me the following screenshot where they describe the issue...

image

What does this implement/fix?

I implemented a maximum allowed percentage jitter for the file to be still considered uniformly sampled.
I (fairly arbitrarily) specified that we now allow for a 1% variation in the sampling times relative to the average sampling time difference of the file. I specified this parameter as MAXIMUM_ALLOWED_SAMPLING_JITTER_PERCENTAGE

Additional information

This solution doesn't seem ideal. I am open to alternative suggestions.

I also tried reducing the precision when calculating fs_diff = np.around(np.diff(time_data), decimals=4), but this broke support (and unit tests) for other devices.

The files exhibiting this behaviour are too large to add to our testing data. I have requested a test file from the manufacturer, but it might not be possible to capture this phenomenon in a small file as it seems to occur only many minutes into a recording. And if we read, crop, export and use the modified file, it will not reflect the manufacturer's formatting.

The submitted code allows me to read the files the manufacturer shared with me.

@rob-luke
Copy link
Copy Markdown
Member Author

@larsoner @agramfort @drammock could you please review.
There seems to be some unrelated issue in pip-pre, but I am pretty sure I didn't introduce that.

Copy link
Copy Markdown
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.

Sounds like a reasonable workaround to me. It would be awesome to have a test for this. If there are HDF5 files, and you could maybe copy the file to a tmp_path and edit it in-place (?) to change the last sample in time_data to be off by more than 1%. But if this isn't the case then I think we can merge as-is...

@larsoner larsoner added this to the 1.6 milestone Aug 28, 2023
@rob-luke
Copy link
Copy Markdown
Member Author

Good idea. I can write that test

rob-luke and others added 2 commits September 12, 2023 11:43
Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
@drammock
Copy link
Copy Markdown
Member

@rob-luke I've pushed some changes here that you may disagree with, so feel free to revert some of them. Mostly I:

  • renamed some variables
  • changed the way jitter is computed (now is "absolute jitter" i.e. max(abs(deviation from ideal times)), used to be max(second-order diff of times) which is "cycle-to-cycle jitter". My instinct was that absolute jitter is better here (i.e. it could catch drifts that cycle-to-cycle might miss) but if I'm wrong feel free to revert.
  • made the jitter test more stringent (testing values closer to the 1% threshold)

@rob-luke
Copy link
Copy Markdown
Member Author

Thanks @drammock that is amazing! I appreciate you taking the time to make the edits directly!

Copy link
Copy Markdown
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.

@drammock feel free to mark for merge-when-green if you're happy

@drammock
Copy link
Copy Markdown
Member

I'm happy but CI "old" is not. I'll debug tomorrow morning

@drammock drammock enabled auto-merge (squash) September 19, 2023 15:59
@drammock
Copy link
Copy Markdown
Member

ok I'm stumped. I created a local env that mirrors the compat_old env, replicated the failure, then got it passing locally, but it's still failing on the CIs.

(there's also now an unrelated pip-pre failure dealing with dependency resolution)

@larsoner
Copy link
Copy Markdown
Member

Okay I'll look

del f["nirs/data1/time"]
f.flush()
f.create_dataset("nirs/data1/time", data=acceptable_time_jitter)
read_raw_snirf(new_file)
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.

Suggested change
read_raw_snirf(new_file)
with use_log_level("info"), catch_logging() as log:
read_raw_snirf(new_file)
assert "Found jitter of 0.990000%" in log.getvalue()

(with associated from mne.utils import catch_logging, use_log_level at the top)

This won't fix the CI failure but it makes sure our jitter detection is actually working

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.

Feel free to push I'm still messing with conda envs

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.

Okay envs fixed I'll implement it

@drammock drammock merged commit e8baea6 into mne-tools:main Sep 19, 2023
larsoner added a commit to larsoner/mne-python that referenced this pull request Sep 20, 2023
* upstream/main:
  Interactive version of plot_evoked_fieldmap (mne-tools#11942)
  ENH: Add support for Artinis SNIRF data (mne-tools#11926)
snwnde pushed a commit to snwnde/mne-python that referenced this pull request Mar 20, 2024
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>
Co-authored-by: Daniel McCloy <dan@mccloy.info>
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