unified_sampling
adjust_time_columns(df, intervention_start_month=None)
Add continuous_month column and identify intervention month if specified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame with 'year' and 'month' columns |
required |
intervention_start_month
|
int
|
Continuous month when intervention starts (optional) |
None
|
Returns:
| Type | Description |
|---|---|
|
pd.DataFrame: DataFrame with continuous_month added |
Source code in fpg_observational_model/unified_sampling.py
apply_emod_filters(infection_df, fever_filter=False, monogenomic_filter=False, day_filter=False, other_filters=None)
Apply initial filters to the data before any sampling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
infection_df
|
DataFrame
|
Input DataFrame |
required |
fever_filter
|
bool
|
If True, keep only fever cases; if False, keep only non-fever cases |
False
|
monogenomic_filter
|
bool
|
If True, keep only monogenomic; if False, keep only polygenomic |
False
|
coi_filter
|
str
|
'true_coi' or 'effective_coi' - which COI column to use |
required |
other_filters
|
dict
|
Additional filters to apply {column: value} |
None
|
Returns:
| Type | Description |
|---|---|
|
pd.DataFrame: Filtered DataFrame |
Source code in fpg_observational_model/unified_sampling.py
assign_peak_group(row)
Assign peak/off-peak season groups.
Source code in fpg_observational_model/unified_sampling.py
assign_season_group(row)
Assign wet/dry season groups based on month.
Source code in fpg_observational_model/unified_sampling.py
calculate_infection_metrics(df)
Calculate metrics for each infection before any sampling decisions.
For each infection, calculates:
1. true_coi: number of items in genome_ids
2. effective_coi: number of unique items in genome_ids
3. cotx: cotransmission status (NA if COI==1, True if single bite event, False if multiple)
4. Optionally notes intervention month for season marking
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input FPG data |
required |
intervention_start_month
|
int
|
Optional continuous month when intervention starts |
required |
Returns:
| Type | Description |
|---|---|
|
pd.DataFrame: Data with added infection metrics |
Source code in fpg_observational_model/unified_sampling.py
convert_month(df)
Convert simulation year and month to a continuous variable.
Source code in fpg_observational_model/unified_sampling.py
create_robust_random_seed(base_seed, replicate, year=None, population=None, extra=None)
Create a robust random seed that avoids correlations.
Source code in fpg_observational_model/unified_sampling.py
filter_emod_infections(infection_df, duplicate_window='continuous_month', duplicate_seed=123, is_test=False)
Filter EMOD infections to avoid duplicates.
Source code in fpg_observational_model/unified_sampling.py
handle_insufficient_samples(available, requested, group_info='')
Handle cases where requested samples exceed available samples.
Source code in fpg_observational_model/unified_sampling.py
n_samples_by_pop(infection_df, n_samples_year, population_proportions=False)
Returns the number of samples per population based on total samples and optional population fractions.
Source code in fpg_observational_model/unified_sampling.py
parse_list(s)
process_config_filters(config)
Convert config filter settings to function parameters.
Source code in fpg_observational_model/unified_sampling.py
run_sampling_functions(infection_df, sampling_config, **kwargs)
Enhanced main function to run any subset sampling method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
infection_df
|
DataFrame
|
Input DataFrame |
required |
sampling_config
|
dict
|
Configuration dictionary with method, n_samples_year, replicates, method_params |
required |
**kwargs
|
Additional arguments (e.g., base_seed) |
{}
|
Returns:
| Type | Description |
|---|---|
|
pd.DataFrame: DataFrame with sampling columns added |
Source code in fpg_observational_model/unified_sampling.py
run_sampling_model(input_df, config, intervention_start_month=None, verbose=True)
Main function to run the observational model sampling pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_df
|
DataFrame
|
Input FPG DataFrame (required) |
required |
config
|
dict
|
Configuration dictionary (required) |
required |
intervention_start_month
|
int
|
Continuous month when intervention starts |
None
|
verbose
|
bool
|
Whether to print progress messages |
True
|
Returns:
| Type | Description |
|---|---|
|
pd.DataFrame: Final dataframe with all sampling columns added |
Raises:
| Type | Description |
|---|---|
ValueError
|
If input_df or config is None |
Source code in fpg_observational_model/unified_sampling.py
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 670 671 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 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 | |
subset_by_age(infection_df, n_samples_year, replicates, scheme='ageBins', age_bins=None, age_bin_weights=None, base_seed=418)
Subset samples based on age bins and add columns to the original dataframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
infection_df
|
DataFrame
|
Input DataFrame |
required |
n_samples_year
|
int
|
Number of samples per year (or per age bin if weights provided) |
required |
replicates
|
int
|
Number of replicates |
required |
scheme
|
str
|
Base name for columns (e.g., 'ageBins') |
'ageBins'
|
age_bins
|
list
|
Age bin boundaries in days [0, bin1, bin2, ..., max] |
None
|
age_bin_weights
|
list
|
Optional weights for age bins (must sum to 1) |
None
|
base_seed
|
int
|
Base random seed |
418
|
Returns:
| Type | Description |
|---|---|
|
pd.DataFrame: Original dataframe with new age bin sampling columns added |
Source code in fpg_observational_model/unified_sampling.py
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 511 512 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 553 554 555 556 | |
subset_by_seasons(infection_df, n_samples_year, replicates, scheme='seasonal', season='full', base_seed=418)
Subset samples based on seasonal groupings.
Source code in fpg_observational_model/unified_sampling.py
subset_randomly(infection_df, n_samples_year, replicates, scheme='random', monogenomic_proportion=False, equal_monthly=False, population_proportions=False, base_seed=418)
Randomly subset samples and add columns to the original dataframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
infection_df
|
DataFrame
|
Input DataFrame |
required |
n_samples_year
|
int
|
Number of samples per year per population |
required |
replicates
|
int
|
Number of replicates |
required |
scheme
|
str
|
Base name for columns (e.g., 'random') |
'random'
|
equal_monthly
|
bool
|
Whether to sample equally across months |
False
|
population_proportions
|
list
|
Optional population fractions (must sum to 1) |
False
|
base_seed
|
int
|
Base random seed |
418
|
Returns:
| Type | Description |
|---|---|
|
pd.DataFrame: Original dataframe with new sampling columns added |
Source code in fpg_observational_model/unified_sampling.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 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 | |
validate_subset_inputs(infection_df, n_samples_year, replicates, method_name)
Validate common inputs for subset functions.