Skip to content

Convenience methods for time conversion #384

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

Merged
merged 2 commits into from
Jun 3, 2022
Merged

Convenience methods for time conversion #384

merged 2 commits into from
Jun 3, 2022

Conversation

bemoody
Copy link
Collaborator

@bemoody bemoody commented Jun 2, 2022

Add some convenience methods for converting between different representations of time:

  • frame number (represented as an integer or float)
  • elapsed time (represented as a timedelta)
  • absolute time (represented as a datetime)

For example, if I want to see the signals for MIMIC-III patient 44083, between 2112-05-04 20:00:00 and 2112-05-04 20:05:00, I might do this:

>>> rec_name = 'p044083-2112-05-04-19-50'
>>> pn_dir = 'mimic3wdb-matched/1.0/p04/p044083'
>>> header = wfdb.rdheader(rec_name, pn_dir=pn_dir)
>>> dt1 = datetime.datetime.fromisoformat('2112-05-04 20:00:00')
>>> dt2 = datetime.datetime.fromisoformat('2112-05-04 20:05:00')
>>> start = round(header.get_frame_number(dt1))
>>> end = round(header.get_frame_number(dt2))
>>> chunk = wfdb.rdrecord(rec_name, pn_dir=pn_dir, sampfrom=start, sampto=end)
>>> wfdb.plot_wfdb(chunk)

Benjamin Moody added 2 commits June 2, 2022 15:34
These methods can be used to convert between three different
representations of time:

- frame number (i.e., an index into the p_signal or d_signal array)

- elapsed time in microseconds, represented by a datetime.timedelta
  object

- absolute date and time, represented by a datetime.datetime object

Each of these methods will accept any of these three representations
as an argument, and convert into the specified representation.

For example, using record 100 (which has a frame frequency of 360 Hz):

    >>> r = wfdb.rdrecord('sample-data/100')
    >>> r.get_elapsed_time(180)
    datetime.timedelta(microseconds=500000)
    >>> r.get_frame_number(datetime.timedelta(seconds=0.5))
    180.0
Test that the get_frame_number, get_elapsed_time, and
get_absolute_time methods work correctly, for both Record and
MultiRecord objects.
Copy link
Member

@tompollard tompollard left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@tompollard tompollard merged commit 84cbefb into main Jun 3, 2022
@tompollard tompollard deleted the time-conversion branch June 3, 2022 17:35
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