Time
Helper functions for handling timeseries in Pyrenew
Days of the week in pyrenew are 0-indexed and follow ISO standards, so 0 is Monday at 6 is Sunday.
aggregate_with_dates
aggregate_with_dates(
daily_data: ArrayLike,
start_date: Union[datetime, datetime64],
target_freq: str = "mmwr_weekly",
) -> Tuple[ndarray, datetime]
Aggregate daily data with automatic date handling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
daily_data
|
ArrayLike
|
Daily time series |
required |
start_date
|
Union[datetime, datetime64]
|
Date of first data point |
required |
target_freq
|
str
|
Target frequency ("mmwr_weekly" or "weekly") |
'mmwr_weekly'
|
Returns:
Type | Description |
---|---|
Tuple[ndarray, datetime]
|
Tuple containing (aggregated_data, first_aggregated_date) |
Raises:
Type | Description |
---|---|
ValueError
|
For unsupported frequencies |
Notes
Python's datetime.weekday uses 0=Monday..6=Sunday which matches PyRenew's day-of-week indexing.
Source code in pyrenew/time.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
|
align_observation_times
align_observation_times(
observation_dates: ArrayLike,
model_start_date: Union[datetime, datetime64],
aggregation_freq: str = "daily",
) -> ndarray
Convert observation dates to model time indices with temporal aggregation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
observation_dates
|
ArrayLike
|
Dates when observations occurred |
required |
model_start_date
|
Union[datetime, datetime64]
|
Date corresponding to model time t=0 |
required |
aggregation_freq
|
str
|
Temporal aggregation ("daily", "weekly", "mmwr_weekly") |
'daily'
|
Returns:
Type | Description |
---|---|
ndarray
|
Model time indices for observations |
Raises:
Type | Description |
---|---|
NotImplementedError
|
For unsupported frequencies |
Source code in pyrenew/time.py
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 |
|
convert_date
convert_date(date: Union[datetime, date, datetime64]) -> date
Normalize a date-like object to a python datetime.date
.
The function accepts any of the common representations used in this
codebase and returns a datetime.date
(i.e. without time component).
Supported input types:
- numpy.datetime64
: converted to date (day precision)
- datetime.datetime
: converted via .date()
- datetime.date
: returned unchanged
Parameters:
Name | Type | Description | Default |
---|---|---|---|
date
|
Union[datetime, date, datetime64]
|
A date-like object to normalize. |
required |
Returns:
Type | Description |
---|---|
date
|
The corresponding date (with no time information). |
Notes
- ``numpy.datetime64`` objects are first normalized to day precision
(``datetime64[D]``) and then converted by computing the integer
number of days since the UNIX epoch and constructing a ``datetime.date``.
This is robust across NumPy versions where direct conversion to Python
datetimes can behave differently.
- Fails fast for unsupported input types by raising a ``TypeError``
Source code in pyrenew/time.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
create_date_time_spine
create_date_time_spine(
start_date: Union[datetime, datetime64],
end_date: Union[datetime, datetime64],
freq: str = "1d",
) -> DataFrame
Create a DataFrame mapping calendar dates to model time indices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_date
|
Union[datetime, datetime64]
|
First date (becomes t=0) |
required |
end_date
|
Union[datetime, datetime64]
|
Last date |
required |
freq
|
str
|
Frequency string for polars date_range |
'1d'
|
Returns:
Type | Description |
---|---|
DataFrame
|
DataFrame with 'date' and 't' columns |
Source code in pyrenew/time.py
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
|
daily_to_mmwr_epiweekly
Aggregate daily values to weekly values
using pyrenew.time.daily_to_weekly
with
MMWR epidemiological weeks (begin on Sundays,
end on Saturdays).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
daily_values
|
ArrayLike
|
Daily timeseries values. |
required |
input_data_first_dow
|
int
|
First day of the week in the input timeseries |
6
|
Returns:
Type | Description |
---|---|
ArrayLike
|
Data converted to epiweekly values. |
Source code in pyrenew/time.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
daily_to_weekly
daily_to_weekly(
daily_values: ArrayLike,
input_data_first_dow: int = 0,
week_start_dow: int = 0,
) -> ArrayLike
Aggregate daily values (e.g. incident hospital admissions) to weekly total values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
daily_values
|
ArrayLike
|
Daily timeseries values (e.g. incident infections or incident ed visits). |
required |
input_data_first_dow
|
int
|
First day of the week in the input timeseries |
0
|
week_start_dow
|
int
|
Day of the week on which weeks are considered to start in the output timeseries of weekly values (e.g. ISO weeks start on Mondays and end on Sundays; MMWR epiweeks start on Sundays and end on Saturdays). An integer between 0 and 6, inclusive (0 for Monday, 1 for Tuesday, ..., 6 for Sunday). Default 0 (i.e. ISO weeks, starting on Mondays). |
0
|
Returns:
Type | Description |
---|---|
ArrayLike
|
Data converted to weekly values starting with the first full week available. |
Raises:
Type | Description |
---|---|
ValueError
|
If the specified days of the week fail validation. |
Notes
This is not a simple inverse of pyrenew.time.weekly_to_daily
.
This function aggregates (by summing) daily values to
create a timeseries of weekly total values.
pyrenew.time.weekly_to_daily
broadcasts a single shared value
for a given week as the (repeated) daily value for each day
of that week.
Source code in pyrenew/time.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
date_to_model_t
date_to_model_t(
date: Union[datetime, datetime64], start_date: Union[datetime, datetime64]
) -> int
Convert calendar date to model time index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
date
|
Union[datetime, datetime64]
|
Target date |
required |
start_date
|
Union[datetime, datetime64]
|
Date corresponding to model time t=0 |
required |
Returns:
Type | Description |
---|---|
int
|
Model time index (days since start_date) |
Source code in pyrenew/time.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
|
get_date_range_length
Calculate number of time steps in a date range.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
date_array
|
ArrayLike
|
Array of observation dates |
required |
timestep_days
|
int
|
Days between consecutive points |
1
|
Returns:
Type | Description |
---|---|
int
|
Number of time steps in the date range |
Source code in pyrenew/time.py
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
|
get_end_date
get_end_date(
start_date: Union[datetime, datetime64],
n_points: int,
timestep_days: int = 1,
) -> Union[datetime64, None]
Calculate end date from start date and number of data points.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_date
|
Union[datetime, datetime64]
|
First date in the series |
required |
n_points
|
int
|
Number of data points |
required |
timestep_days
|
int
|
Days between consecutive points |
1
|
Returns:
Type | Description |
---|---|
Union[datetime64, None]
|
Date of the last data point |
Raises:
Type | Description |
---|---|
ValueError
|
If n_points is non-positive |
Source code in pyrenew/time.py
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 |
|
get_first_week_on_or_after_t0
get_first_week_on_or_after_t0(
model_t_first_weekly_value: int, week_interval_days: int = 7
) -> int
Find the first weekly index where the week ends on or after model t=0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_t_first_weekly_value
|
int
|
Model time of the first weekly value (often negative during initialization period). Represents week-ending date. |
required |
week_interval_days
|
int
|
Days between consecutive weekly values. Default 7. |
7
|
Returns:
Type | Description |
---|---|
int
|
Index of first week ending on or after model t=0. |
Notes
Weekly values are indexed 0, 1, 2, ... and occur at model times: - Week 0: model_t_first_weekly_value - Week k: model_t_first_weekly_value + k * week_interval_days
We find min k such that: model_t_first_weekly_value + k * week_interval_days >= 0 Equivalently: k >= ceil(-model_t_first_weekly_value / week_interval_days) Using ceiling division identity: ceil(-x / d) = (-x - 1) // d + 1
Source code in pyrenew/time.py
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 |
|
get_n_data_days
get_n_data_days(
n_points: int = None, date_array: ArrayLike = None, timestep_days: int = 1
) -> int
Determine data length from either point count or date array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_points
|
int
|
Explicit number of data points |
None
|
date_array
|
ArrayLike
|
Array of observation dates |
None
|
timestep_days
|
int
|
Days between consecutive points |
1
|
Returns:
Type | Description |
---|---|
int
|
Number of data points. Returns 0 if both n_points and date_array are None. |
Raises:
Type | Description |
---|---|
ValueError
|
If both n_points and date_array are provided. |
Source code in pyrenew/time.py
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 |
|
get_observation_indices
get_observation_indices(
observed_dates: ArrayLike,
data_start_date: Union[datetime, datetime64],
freq: str = "mmwr_weekly",
) -> ndarray
Get indices for observed data in aggregated time series.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
observed_dates
|
ArrayLike
|
Dates of observations |
required |
data_start_date
|
Union[datetime, datetime64]
|
Start date of the data series |
required |
freq
|
str
|
Frequency of aggregated data ("mmwr_weekly" or "weekly") |
'mmwr_weekly'
|
Returns:
Type | Description |
---|---|
ndarray
|
Indices for observed data points in aggregated series |
Raises:
Type | Description |
---|---|
NotImplementedError
|
For unsupported frequencies |
Source code in pyrenew/time.py
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
|
mmwr_epiweekly_to_daily
Convert an MMWR epiweekly timeseries to a daily
timeseries using pyrenew.time.weekly_to_daily
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
weekly_values
|
ArrayLike
|
Timeseries of weekly values, where (discrete) time is the first dimension of the array (following Pyrenew convention). |
required |
output_data_first_dow
|
int
|
Day of the week on which to start the output timeseries.
An integer between 0 and 6, inclusive (0 for Monday,
1 for Tuesday, ..., 6 for Sunday). Defaults to the MMWR
epiweek start day (6, Sunday).
If |
6
|
Returns:
Type | Description |
---|---|
ArrayLike
|
The daily timeseries. |
Source code in pyrenew/time.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
|
model_t_to_date
model_t_to_date(
model_t: int, start_date: Union[datetime, datetime64]
) -> datetime
Convert model time index to calendar date.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_t
|
int
|
Model time index |
required |
start_date
|
Union[datetime, datetime64]
|
Date corresponding to model time t=0 |
required |
Returns:
Type | Description |
---|---|
datetime
|
Calendar date |
Source code in pyrenew/time.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
|
validate_dow
Confirm that an integer is a valid Pyrenew day of the week index, with informative error messages on failure.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
day_of_week
|
int
|
Integer to validate. |
required |
variable_name
|
str
|
Name of the variable being validated, to increase the informativeness of the error message. |
required |
Returns:
Type | Description |
---|---|
None
|
If validation passes. |
Raises:
Type | Description |
---|---|
ValueError
|
If validation fails. |
Source code in pyrenew/time.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
validate_mmwr_dates
validate_mmwr_dates(dates: ArrayLike) -> None
Validate that dates are Saturdays (MMWR week endings).
:param dates: Array of dates to validate :raises ValueError: If any date is not a Saturday
Source code in pyrenew/time.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
weekly_to_daily
weekly_to_daily(
weekly_values: ArrayLike,
week_start_dow: int = 0,
output_data_first_dow: int = None,
) -> ArrayLike
Broadcast a weekly timeseries to a daily
timeseries. The value for the week will be used
as the value each day in that week, via
jax.numpy.repeat
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
weekly_values
|
ArrayLike
|
Timeseries of weekly values, where (discrete) time is the first dimension of the array (following Pyrenew convention). |
required |
week_start_dow
|
int
|
Day of the week on which weeks are considered to
start in the input |
0
|
output_data_first_dow
|
int
|
Day of the week on which to start the output timeseries.
An integer between 0 and 6, inclusive (0 for Monday,
1 for Tuesday, ..., 6 for Sunday). Defaults to the week
start date as specified by |
None
|
Returns:
Type | Description |
---|---|
ArrayLike
|
The daily timeseries. |
Raises:
Type | Description |
---|---|
ValueError
|
If the specified days of the week fail validation. |
Notes
This is not a simple inverse of pyrenew.time.daily_to_weekly
.
pyrenew.time.daily_to_weekly
aggregates (by summing) daily values to
create a timeseries of weekly total values.
This function broadcasts a single shared value
for a given week as the (repeated) daily value for each day
of that week.
Source code in pyrenew/time.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|