pyhealth.processors.utils#
Helpers shared by processors: learning normalization statistics and placing values on a uniform time grid.
Utilities shared by processors.
- class pyhealth.processors.utils.datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])#
Bases:
dateThe year, month and day arguments are required. tzinfo may be None, or an instance of a tzinfo subclass. The remaining arguments may be ints.
- astimezone()#
tz -> convert to local time in new timezone tz
- combine()#
date, time -> datetime with same date and time fields
- ctime()#
Return ctime() style string.
- date()#
Return date object with same year, month and day.
- day#
- dst()#
Return self.tzinfo.dst(self).
- fold#
- fromisocalendar()#
int, int, int -> Construct a date from the ISO year, week number and weekday.
This is the inverse of the date.isocalendar() function
- fromisoformat()#
string -> datetime from a string in most ISO 8601 formats
- fromordinal()#
int -> date corresponding to a proleptic Gregorian ordinal.
- fromtimestamp()#
timestamp[, tz] -> tz’s local time from POSIX timestamp.
- hour#
- isocalendar()#
Return a named tuple containing ISO year, week number, and weekday.
- isoformat()#
[sep] -> string in ISO 8601 format, YYYY-MM-DDT[HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM]. sep is used to separate the year from the time, and defaults to ‘T’. The optional argument timespec specifies the number of additional terms of the time to include. Valid options are ‘auto’, ‘hours’, ‘minutes’, ‘seconds’, ‘milliseconds’ and ‘microseconds’.
- isoweekday()#
Return the day of the week represented by the date. Monday == 1 … Sunday == 7
- max = datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)#
- microsecond#
- min = datetime.datetime(1, 1, 1, 0, 0)#
- minute#
- month#
- now()#
Returns new datetime object representing current time local to tz.
- tz
Timezone object.
If no tz is specified, uses local timezone.
- replace()#
Return datetime with new specified fields.
- resolution = datetime.timedelta(microseconds=1)#
- second#
- strftime()#
format -> strftime() style string.
- strptime()#
string, format -> new datetime parsed from a string (like time.strptime()).
- time()#
Return time object with same time but with tzinfo=None.
- timestamp()#
Return POSIX timestamp as float.
- timetuple()#
Return time tuple, compatible with time.localtime().
- timetz()#
Return time object with same time and tzinfo.
- today()#
Current date or datetime: same as self.__class__.fromtimestamp(time.time()).
- toordinal()#
Return proleptic Gregorian ordinal. January 1 of year 1 is day 1.
- tzinfo#
- tzname()#
Return self.tzinfo.tzname(self).
- utcfromtimestamp()#
Construct a naive UTC datetime from a POSIX timestamp.
- utcnow()#
Return a new datetime representing UTC day and time.
- utcoffset()#
Return self.tzinfo.utcoffset(self).
- utctimetuple()#
Return UTC time tuple, compatible with time.localtime().
- weekday()#
Return the day of the week represented by the date. Monday == 0 … Sunday == 6
- year#
- class pyhealth.processors.utils.timedelta#
Bases:
objectDifference between two datetime values.
timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
All arguments are optional and default to 0. Arguments may be integers or floats, and may be positive or negative.
- days#
Number of days.
- max = datetime.timedelta(days=999999999, seconds=86399, microseconds=999999)#
- microseconds#
Number of microseconds (>= 0 and less than 1 second).
- min = datetime.timedelta(days=-999999999)#
- resolution = datetime.timedelta(microseconds=1)#
- seconds#
Number of seconds (>= 0 and less than 1 day).
- total_seconds()#
Total seconds in the duration.
- pyhealth.processors.utils.fit_normalization(arrays, method)[source]#
Learns per-feature
(center, scale)from the observed values.Missing values (
NaN) are ignored."zscore"uses the mean and the population standard deviation,"minmax"the minimum and the range. A feature that is never observed or constant gets a scale of 1.- Parameters:
- Return type:
- Returns:
The center and scale of each feature, as lists of floats.
Examples
>>> import numpy as np >>> from pyhealth.processors.utils import fit_normalization >>> fit_normalization([np.array([[1.0], [3.0]]), np.array([[np.nan]])], "zscore") ([2.0], [1.0])
- pyhealth.processors.utils.grid_steps(timestamps, sampling_rate)[source]#
Step of each timestamp on a uniform grid starting at the first one.
A timestamp
tgoes to stepfloor((t - t0) / sampling_rate).- Parameters:
- Return type:
ndarray- Returns:
Integer array with the step of each timestamp.
Examples
>>> from datetime import datetime, timedelta >>> from pyhealth.processors.utils import grid_steps >>> t0 = datetime(2020, 1, 1) >>> times = [t0, t0 + timedelta(minutes=90), t0 + timedelta(hours=3)] >>> grid_steps(times, timedelta(hours=1)).tolist() [0, 1, 3]
- pyhealth.processors.utils.sample_on_grid(timestamps, values, sampling_rate)[source]#
Places values on a uniform time grid starting at the first timestamp.
Each value goes to the step of its timestamp (see
grid_steps()); when several values fall into the same step, the last one is kept.- Parameters:
- Return type:
ndarray- Returns:
Array of shape
(S, F), NaN where the grid has no value.
Examples
>>> from datetime import datetime, timedelta >>> from pyhealth.processors.utils import sample_on_grid >>> t0 = datetime(2020, 1, 1) >>> sample_on_grid([t0, t0 + timedelta(hours=2)], [1.0, 3.0], timedelta(hours=1)) array([[ 1.], [nan], [ 3.]])