API reference¶
Models¶
The :mod:~pyter.models module provides
flexible model classes to represent
distinct experimental setups.
The :class:AbstractModel base class
serves as a template for :class:Model
subclasses that represent different
possible experimental setups with different
inferred quantities of interest.
The core of any :class:Model subclass is
the :meth:~pyter.models.AbstractModel.model
method, which takes in a data dictionary
and makes calls to :func:numpyro.sample()
<numpyro.primitives.sample> to
define the stochastic generative process.
AbstractModel ¶
Abstract base class for Pyter models
get_reparam ¶
get_reparam()
Source code in pyter/models.py
350 351 352 | |
model ¶
model(data: dict = None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
(Default value = None) |
None
|
Source code in pyter/models.py
336 337 338 339 340 341 342 343 344 345 346 347 348 | |
validate_data ¶
validate_data(data: AbstractData, run_data: dict)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
AbstractData
|
|
required |
run_data
|
dict
|
|
required |
Source code in pyter/models.py
354 355 356 357 358 359 360 361 362 363 364 365 366 | |
HalfLifeModel ¶
Bases: AbstractModel
Model to infer virus halflives from experimental timeseries data.
A timeseries here is any set titration
results taken at different timepoints
that represent repeat samples from the
same viral stock. But we can also handle
cases in which non-destructive sampling
is impossible (for example, depositing
stock onto a surface and retrieving it at
:math:t = 0 h, :math:t=1 h, etc.).
To do this, we use a hierarchical approach:
we infer a shared halflife for the samples
jointly with a and modal value for the
initial titer deposited. Each individual
sample's unknown :math:t = 0 value
may vary about this value. This allows
the model to use the immediately retrieved
t = 0 titers to make inferences about the
what the unmeasured :math:t = 0 h titers
were for the samples taken at :math:t = 1 h,
:math:t = 2 h, etc. samples.
model ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
:class:`dict`
|
Dictionary of data with which to fit the model.
Defaults to :py:data: |
None
|
Returns:
| Type | Description |
|---|---|
log_titer, wells : :class:`tuple`
|
|
( :class:`jax.Array`, :class:`jax.Array` )
|
Tuple of arrays containing sampled log titer values and sampled well statuses / plaque counts. |
Source code in pyter/models.py
627 628 629 630 631 632 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 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 | |
sample_log_halflife ¶
Sample log half-life values, either from a fixed-parameter prior or hierarchically, as specified for the user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
:class:`dict`
|
Dictionary of data with which to fit the model.
Defaults to :py:data: |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
log_halflife |
:class:`jax.Array`
|
An array of sampled halflives. |
Source code in pyter/models.py
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 | |
sample_log_titer ¶
Sample realized log titer values for the modeled titers, either deterministically predicted from the other parameters, or with an inferred degree of noise, as specified by the user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicted_titer
|
:class:`jax.Array`
|
An array of predicted titer values. |
required |
data
|
:class:`dict`
|
Dictionary of data with which to fit the model.
Defaults to :py:data: |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
log_titer |
:class:`jax.Array`
|
|
Source code in pyter/models.py
587 588 589 590 591 592 593 594 595 596 597 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 | |
sample_log_titer_intercept ¶
Sample log intercept (i.e. t = 0) values for the modeled titers, either a fixed-parameter prior or hierarchically, as specified for the user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
:class:`dict`
|
Dictionary of data with which to fit the model.
Defaults to :data: |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
log_titer_intercept |
:class:`jax.Array`
|
An array of sampled intercepts. |
Source code in pyter/models.py
549 550 551 552 553 554 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 | |
validate_data ¶
validate_data(data: AbstractData, run_data: dict)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
AbstractData
|
|
required |
run_data
|
dict
|
|
required |
Source code in pyter/models.py
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 | |
MultiphaseHalfLifeModel ¶
Bases: HalfLifeModel
model ¶
model(data: dict = None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
(Default value = None) |
None
|
Source code in pyter/models.py
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 | |
sample_log_halflife ¶
sample_log_halflife(data: dict = None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
(Default value = None) |
None
|
Source code in pyter/models.py
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 | |
TiterModel ¶
Bases: AbstractModel
Model to infer individual titers independently
model ¶
model(data: dict = None)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
|
None
|
Source code in pyter/models.py
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 | |
validate_data ¶
validate_data(data: AbstractData, run_data: dict)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
:class:`~pyter.data.TiterData` :
|
Pyter data object to validate. |
required |
run_data
|
:class:`dict` :
|
Frozen dictionary of data with which
to fit the model, generated from
a :class: |
required |
Returns:
| Type | Description |
|---|---|
py:data:`True`
|
|
Source code in pyter/models.py
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | |
loc_scale_factory ¶
loc_scale_factory(distribution: str, loc: ArrayLike = None, scale: ArrayLike = None) -> Distribution
Factory function for distributions with a loc/scale parameterization
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
:class:`str`
|
the name of the desired distribution |
required |
loc
|
:data:`~numpy.typing.ArrayLike`
|
the location parameter(s) of the desired distribution |
None
|
scale
|
:data:`~numpy.typing.ArrayLike`
|
the scale parameter(s) of the desired distribution |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dist |
:class:`~numpyro.distributions.distribution.Distribution`
|
The parameterized distribution. |
Source code in pyter/models.py
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 325 | |
sample_loc_scale_hier ¶
sample_loc_scale_hier(param_name: str, param_dim: int, n_locs: int, n_scales: int, param_distribution: Distribution, loc_ids: ArrayLike, scale_ids: ArrayLike, loc_prior: Distribution, scale_prior: Distribution) -> Array
Sample a vector of hierarchical inferred parameters alongside their inferred parent parameters.
Convenience wrapper to sample
a vectorized parameter in which individual
values are "loc/scale" hierarchical. That is,
parameter values have a distribution
that is determined by two parameters---
a location parameter
(loc, e.g. the mean/median/mode of a
:class:~numpyro.distributions.continuous.Normal
distribution) scale parameter
(scale, e.g. the standard
deviation of a
:class:~numpyro.distributions.continuous.Normal
distribution)---but the values of the
location and/or the scale parameter
are unknown and inferred alongside the
child parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param_name
|
:class:`str` :
|
The name of the parameter. |
required |
param_dim
|
:class:`int` :
|
The length of the parameter vector to sample. |
required |
n_locs
|
:class:`int` :
|
The number of groups of |
required |
n_scales
|
:class:`int` :
|
The number of groups of |
required |
param_distribution
|
:class:`~numpyro.distributions.
|
|
required |
distribution
|
A loc / scale parameterizable probability distribution. |
required | |
loc_ids
|
:data:`~numpy.typing.ArrayLike`
|
Array of ids associating each parameter in the
desired vector to one of the |
required |
scale_ids
|
:data:`~numpy.typing.ArrayLike`
|
Array of ids associating each parameter in the
desired vector to one of the |
required |
loc_prior
|
:class:`~numpyro.distributions.distribution.Distribution`
|
Prior distribution for the inferred unknown
|
required |
scale_prior
|
:class:`~numpyro.distributions.distribution.Distribution`
|
Prior distribution for the inferred unknown
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
param |
:class:`jax.Array`
|
A sampled vector of parameters. |
Source code in pyter/models.py
185 186 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 216 217 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 287 288 | |
sample_non_hier ¶
Sample a vector of inferred parameters whose prior is fixed
Convenience wrapper for :func:numpyro.sample()
<numpyro.primitives.sample> to sample
a vectorized parameter that is non-hierarchical.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param_name
|
:class:`str` :
|
The name of the parameter |
required |
param_dim
|
:class:`int` :
|
The length of the parameter vector |
required |
param_prior
|
:class:`~numpyro.distributions.distribution.Distribution`
|
A prior distribution for the parameter |
required |
Returns:
| Name | Type | Description |
|---|---|---|
param |
:class:`jax.Array`:
|
The sampled parameter vector. |
Source code in pyter/models.py
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 | |
well_distribution_factory ¶
well_distribution_factory(assay: str, log_titer: ArrayLike, log_dilution: ArrayLike, log_base: ArrayLike, well_volume: ArrayLike, false_hit_rate: ArrayLike, validate_args: bool = True) -> TiterPlate
Get an appropriate distribution for titer wells.
Each entry of the various array inputs represents exactly one titration well.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assay
|
:class:`str` = {'pfu', 'tcid'}
|
Which titration assay to use. Options are
|
required |
log_titer
|
:data:`~numpy.typing.ArrayLike`
|
Underlying log titer(s) per unit volume in the undilute sample(s). |
required |
log_dilution
|
:data:`~numpy.typing.ArrayLike`
|
Log dilution(s) relative to the original sample(s) for each well's inoculum. |
required |
log_base
|
ArrayLike
|
Base of the logarithim for logarithmic quantities including titer and dilution (e.g. e, 2, 10, etc). |
required |
well_volume
|
:data:`~numpy.typing.ArrayLike`
|
Volume of the inoculum delivered to
each well, in the same units as the
per unit volume for the |
required |
false_hit_rate
|
:data:`~numpy.typing.ArrayLike`
|
Rate (mean number per well) of false hits (i.e. rate of apparent infection with a sample containing no infectious material). |
required |
validate_args
|
:class:`bool`
|
Passed to the
:class: |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
dist |
:class:`~pyter.distributions.TiterPlate`
|
A :class: |
Source code in pyter/models.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
Data¶
AbstractData ¶
Abstract base class for holding data associated to Pyter inferential models.
freeze ¶
freeze()
Validate, fix, and format data for use in inference.
Data is returned as a :class:dict
that can be passed to a
corresponding :class:Model
<AbstractModel> instance.
The actual logic of validation
and data preparation is handled
by sub-class specific :meth:validate
and :meth:_freeze methods; the
common :meth:freeze method ensures
common data dictionary output
formatting across all
:class:Data <AbstractData>
subclasses.
Returns:
| Name | Type | Description |
|---|---|---|
data_dict |
:class:`dict`
|
A dictionary of data to pass to a model. |
Source code in pyter/data.py
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 | |
validate ¶
validate()
Source code in pyter/data.py
187 188 189 190 191 | |
HalfLifeData ¶
Bases: AbstractData
Data struct for inferring half-life of infectious virus
index_prior_parameters ¶
index_prior_parameters()
Assign prior parameters to appropriate indices
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Returns
|
|
required |
Source code in pyter/data.py
369 370 371 372 373 374 375 376 377 378 379 380 381 | |
update_internal_ids ¶
update_internal_ids()
Assign internal ids for parameters
Source code in pyter/data.py
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 | |
NullData ¶
Bases: AbstractData
:class:Data <AbstractData> class for models that do not
take any user-provided data, and
for testing.
TiterData ¶
Bases: AbstractData
:class:Data <AbstractData> class
for inference of individual titers.
get_associated_internal_ids ¶
get_associated_internal_ids(key_param: str, value_param: str, internal_id_dict: dict, representative_row_dict: dict) -> ndarray
For example, get internal intercept id for each internal titer id
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_param
|
str
|
|
required |
value_param
|
str
|
|
required |
internal_id_dict
|
dict
|
|
required |
representative_row_dict
|
dict
|
|
required |
Source code in pyter/data.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
to_internal_ids ¶
Internally index a long tidy data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
external_ids
|
:data:`~numpy.typing.ArrayLike`
|
Array of external ids, which may be strings, numeric
values, or another type coercible to a
:class: |
required |
Returns:
| Name | Type | Description |
|---|---|---|
result |
:class:`tuple`
|
A tuple containing: internal_ids : :class: unique_internal_ids : :class: unique_external_ids : :class: representative_rows : :class: |
Source code in pyter/data.py
12 13 14 15 16 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
validate_internal_ids ¶
validate_internal_ids(internal_ids: ArrayLike, unique_internal_ids: ArrayLike, unique_external_ids: ArrayLike, representative_rows: ArrayLike, n_values: int) -> None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
internal_ids
|
ArrayLike
|
|
required |
unique_internal_ids
|
ArrayLike
|
|
required |
unique_external_ids
|
ArrayLike
|
param representative_rows: |
required |
n_values
|
int
|
|
required |
unique_internal_ids
|
ArrayLike
|
|
required |
representative_rows
|
ArrayLike
|
|
required |
Source code in pyter/data.py
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
Distributions¶
Custom probability distributions for quantitative virology.
Distributions are subclasses of
:class:numpyro.distributions.Distribution
<numpyro.distributions.distribution.Distribution>,
which we will refer to simply as class
:class:~numpyro.distributions.distribution.Distribution.
EndpointTiterPlate ¶
EndpointTiterPlate(**kwargs)
Bases: TiterPlate
Distribution class to represent a set of titers quantified by endpoint titration.
Source code in pyter/distributions.py
200 201 202 203 204 205 206 207 208 209 210 211 212 | |
log_prob ¶
log_prob(value)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
|
required |
Source code in pyter/distributions.py
231 232 233 234 235 236 237 238 239 240 241 242 243 | |
sample ¶
sample(key, sample_shape=())
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
|
required | |
sample_shape
|
(Default value = ()) |
()
|
Source code in pyter/distributions.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
PlaquePlate ¶
PlaquePlate(**kwargs)
Bases: TiterPlate
Distribution class to represent a set of titers quantified by a plaque assay.
Source code in pyter/distributions.py
150 151 152 153 154 155 156 157 | |
log_prob ¶
log_prob(value)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
|
required |
Source code in pyter/distributions.py
176 177 178 179 180 181 182 183 184 185 186 187 188 | |
sample ¶
sample(key, sample_shape=())
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
|
required | |
sample_shape
|
(Default value = ()) |
()
|
Source code in pyter/distributions.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
PoissonSingleHit ¶
PoissonSingleHit(rate=0, validate_args=None)
Bases: Distribution
Poisson Single-Hit Distribution
This is a distribution that yields a 1 if a Poisson random variable is non-zero and a zero otherwise. It occurs in virology because if we expose a set of cells to some quantity of infectious virus particles ("virions"), the number that succesfully enter a cell and replicate can be modeled as a Poisson distributed random variable with a mean related to the initial quantity of virions. The probability of seeing any evidence of cell invasion is then equal to the probability that this Poisson random variable is non-zero (i.e. at least one virion successfully invaded a cell).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
:py:class:`float`
|
The rate of the Poisson random variable. |
0
|
Source code in pyter/distributions.py
53 54 55 56 57 58 59 60 61 | |
log_prob ¶
log_prob(value)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
|
required |
Source code in pyter/distributions.py
78 79 80 81 82 83 84 85 86 87 88 89 90 | |
sample ¶
sample(key, sample_shape=())
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
|
required | |
sample_shape
|
(Default value = ()) |
()
|
Source code in pyter/distributions.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
TiterPlate ¶
TiterPlate(log_titer=None, log_dilution=None, log_base=10, well_volume=1, false_hit_rate=0, validate_args=None)
Bases: Distribution
Base distribution to represent a set of titers
Subclasses represent different assays:
:class:PlaquePlate for plaque assays,
and :class:EndpointTiterPlate for
endpoint titration assays.
Source code in pyter/distributions.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
Inference¶
Inference ¶
infer ¶
infer(model: AbstractModel = None, data: AbstractData = None, random_seed: int = None, num_warmup: int = 1000, num_samples: int = 1000, validate_data: bool = True, **kwargs)
Conduct inference.
Draw posterior samples from the given model with the given data
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
AbstractModel
|
(Default value = None) |
None
|
data
|
AbstractData
|
(Default value = None) |
None
|
random_seed
|
int
|
(Default value = None) |
None
|
num_warmup
|
int
|
(Default value = 1000) |
1000
|
num_samples
|
int
|
(Default value = 1000) |
1000
|
validate_data
|
bool
|
(Default value = True) |
True
|
**kwargs
|
|
{}
|
Source code in pyter/infer.py
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
new_kernel ¶
new_kernel(model)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
|
required |
Source code in pyter/infer.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
new_runner ¶
new_runner(kernel, num_warmup, num_samples, **kwargs)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kernel
|
|
required | |
num_warmup
|
|
required | |
num_samples
|
|
required | |
**kwargs
|
|
{}
|
Source code in pyter/infer.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |