Skip to content

Add base_datetime property #381

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 6 commits into from
Jun 1, 2022
Merged

Add base_datetime property #381

merged 6 commits into from
Jun 1, 2022

Conversation

bemoody
Copy link
Collaborator

@bemoody bemoody commented May 31, 2022

wfdb.rdrecord currently sets an undocumented attribute base_datetime, but only if the record has a base date and a base time.

>>> r = wfdb.rdrecord('sample-data/n16')
>>> r.base_time
datetime.time(22, 34, 47)
>>> r.base_date
datetime.date(2006, 1, 1)
>>> r.base_datetime
datetime.datetime(2006, 1, 1, 22, 34, 47)

>>> r2 = wfdb.rdrecord('sample-data/3000003_0003')
>>> r2.base_time
datetime.time(19, 46, 25, 757000)
>>> r2.base_date
>>> r2.base_datetime
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Record' object has no attribute 'base_datetime'

It is frequently convenient to have this attribute available, so:

  1. Always set base_datetime; set it to None if either base_date or base_time is None.

  2. Define it as an aliased property so that setting base_date and base_time implicitly sets base_datetime and vice versa.

  3. For consistency, also accept base_datetime as an argument to wfdb.Record, wfdb.MultiRecord, and wfdb.wrsamp.

Also, fix the documentation that incorrectly claimed base_date and base_time were strings. They weren't.

Benjamin Moody added 6 commits May 31, 2022 11:45
The base_time and base_date attributes of the BaseRecord class, and
the parameters to the BaseRecord/Record/MultiRecord constructors, must
be a datetime.time object (or None) and a datetime.date object (or
None.)  The documentation of all three classes erroneously claimed
these attributes were strings.
BaseRecord.__init__ takes a large number of arguments and the order is
not especially meaningful or memorable; use keyword arguments instead
of positional arguments for clarity.
Previously, if a record contained both a base date and base time, then
rdrecord would set the undocumented 'base_datetime' attribute
accordingly.

This attribute is useful to have in general, so define it for all
BaseRecords, Records, and MultiRecords.  It is defined as a property
that aliases the base_date and base_time attributes.  That is to say,
if the package or application sets record.base_date and
record.base_time, then record.base_datetime is calculated
automatically; if the package or application sets
record.base_datetime, then record.base_date and record.base_time are
calculated automatically.

Internally, we continue to store the time and date attributes
separately, to accommodate records where the base time is defined but
the base date is not.

Note that when storing a value in record.base_datetime, it must be a
naive datetime object.  Records don't have a time zone, and therefore
trying to set the base time to an aware datetime object is incorrect.
For convenience and consistency, allow passing base_datetime to the
BaseRecord, Record, or MultiRecord constructor.

Since this attribute is an alias for base_date and base_time, it is an
error for the caller to specify this argument while simultaneously
specifying base_date or base_time.
_adjust_datetime is called by _arrange_fields, which is called by
rdrecord, in order to calculate the base date and time, given that a
subset of the record has been selected (using sampfrom).

Previously, this function would calculate base_datetime by combining
base_date and base_time; this is now unnecessary because base_datetime
is set implicitly when base_date and base_time are set.  Likewise, it
is unnecessary to set base_time and base_date afterwards, because
these are set implicitly when base_datetime is set.
Allow passing base_datetime to wfdb.wrsamp, as an alternative to
specifying both base_date and base_time.  Note that it is an
error to specify base_datetime while also specifying base_date or
base_time, and we raise a TypeError in that case.
@bemoody
Copy link
Collaborator Author

bemoody commented May 31, 2022

Oh, incidentally it looks like this is required in order for pull #380 to work correctly - I didn't even notice that. :)

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 3c88d19 into master Jun 1, 2022
@tompollard tompollard deleted the base-datetime branch June 1, 2022 15:15
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