Skip to content
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

Benchmarking - handle all NaN signal for WFDB write_waveform format #60

Merged
merged 2 commits into from
May 10, 2024

Conversation

briangow
Copy link
Collaborator

@briangow briangow commented May 10, 2024

If a requested section of a signal was composed of all NaNs, the WFDB write_waveform function would fail with this error:

Traceback (most recent call last):
  File "/chorus_waveform/./waveform_benchmark.py", line 6, in <module>
    waveform_benchmark.__main__.main()
  File "/chorus_waveform/waveform_benchmark/__main__.py", line 110, in main
    format_list, waveform_list, test_list, result_list = run_benchmarks(input_record=record,
                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/chorus_waveform/waveform_benchmark/benchmark.py", line 114, in run_benchmarks
    fmt().write_waveforms(path, waveforms)
  File "/chorus_waveform/waveform_benchmark/formats/wfdb.py", line 48, in write_waveforms
    sig_baseline = round(-sig_gain * (sample_min + sample_max) / 2)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: cannot convert float NaN to integer

This was occurring with this waveform: mimic3wdb/1.0/36/3654093/3654093. This PR first checks to see if the requested signal is composed of all NaNs. If it is, sig_baseline is set to 0. If it isn't, the baseline is set as was being done previously.

@briangow briangow marked this pull request as ready for review May 10, 2024 16:05
@briangow briangow requested a review from bemoody May 10, 2024 16:05
@bemoody
Copy link
Collaborator

bemoody commented May 10, 2024

Thanks! Yeah, that should work, but this ought to be more efficient:

            # Determine the minimum and maximum non-NaN sample values.
            # (nanmin and nanmax will give a warning if all values are NaN.)
            sample_min = numpy.fmin.reduce(sig_samples)
            sample_max = numpy.fmax.reduce(sig_samples)
            if numpy.isnan(sample_min):
                sig_baseline = 0
            else:
                sig_baseline = round(-sig_gain * (sample_min + sample_max) / 2)

Alternatively, I think numpy.isnan(sig_samples).all() would be better than all(numpy.isnan(sig_samples)).

@briangow
Copy link
Collaborator Author

Thanks @bemoody , I've updated with your suggestion. I'll merge this now.

@briangow briangow merged commit b8d36f6 into main May 10, 2024
1 of 2 checks passed
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.

2 participants