Decoding API Reference
Bayesian encoding and decoding
BayesianDecoder
Bases: object
Bayesian decoder for neural population activity.
This class provides a scikit-learn-like API for Bayesian decoding using tuning curves. Supports 1D and 2D decoding, and can be extended for more complex models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tuningcurve | TuningCurve1D or TuningCurve2D | Tuning curve to use for decoding. | None |
Source code in nelpy/decoding.py
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 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 |
|
fit(X, y=None)
Fit the decoder to data X. (Stores the tuning curve if provided.)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X | array-like or TuningCurve1D/2D | Training data or tuning curve. | required |
y | Ignored | | None |
Source code in nelpy/decoding.py
887 888 889 890 891 892 893 894 895 896 897 898 899 |
|
predict(X, **kwargs)
Predict external variable from data X (returns mode path).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X | array - like or BinnedEventArray | Data to decode. | required |
Returns:
Name | Type | Description |
---|---|---|
mode_pth | ndarray | Most likely position at each time bin. |
Source code in nelpy/decoding.py
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 |
|
predict_asa(X, **kwargs)
Predict analog signal array (mean path) from data X.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X | array - like or BinnedEventArray | Data to decode. | required |
Returns:
Name | Type | Description |
---|---|---|
asa | AnalogSignalArray or ndarray | Mean path as AnalogSignalArray if possible, else array. |
Source code in nelpy/decoding.py
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 |
|
predict_proba(X, **kwargs)
Predict posterior probabilities for data X.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X | array - like or BinnedEventArray | Data to decode. | required |
Returns:
Name | Type | Description |
---|---|---|
posterior | ndarray | Posterior probability matrix. |
Source code in nelpy/decoding.py
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 |
|
Cumhist
Bases: ndarray
Cumulative histogram with interpolation support.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cumhist | ndarray | Cumulative histogram values. | required |
bincenters | ndarray | Bin centers corresponding to the cumulative histogram. | required |
Source code in nelpy/decoding.py
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 |
|
ItemGetter_iloc
Bases: object
.iloc is primarily integer position based (from 0 to length-1 of the axis).
Allows integer-based selection of intervals and series in event arrays. Raises IndexError if a requested indexer is out-of-bounds, except slice indexers which allow out-of-bounds indexing (conforms with python/numpy slice semantics).
Allowed inputs are: - An integer e.g. 5 - A list or array of integers [4, 3, 0] - A slice object with ints 1:7
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj | object | The parent object to slice. | required |
Source code in nelpy/decoding.py
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 |
|
ItemGetter_loc
Bases: object
.loc is primarily label based (that is, series_id based).
Allows label-based selection of intervals and series in event arrays. Raises KeyError when the items are not found.
Allowed inputs are: - A single label, e.g. 5 or 'a' (interpreted as a label, not a position) - A list or array of labels ['a', 'b', 'c'] - A slice object with labels 'a':'f' (both start and stop are included)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj | object | The parent object to slice. | required |
Source code in nelpy/decoding.py
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 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 |
|
cumulative_dist_decoding_error(bst, *, tuningcurve, extern, decodefunc=decode1D, transfunc=None, n_bins=None)
Compute the cumulative distribution of decoding errors using a fixed tuning curve.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bst | BinnedSpikeTrainArray | Binned spike train array containing all epochs to decode. | required |
tuningcurve | TuningCurve1D | Tuning curve to use for decoding. | required |
extern | object | Query-able object of external correlates (e.g., position AnalogSignalArray). | required |
decodefunc | callable | Decoding function to use (default is decode1D). | decode1D |
transfunc | callable | Function to transform external variable (default is None). | None |
n_bins | int | Number of decoding error bins (default is 200). | None |
Returns:
Name | Type | Description |
---|---|---|
cumhist | Cumhist | Cumulative histogram of decoding errors. |
bincenters | ndarray | Bin centers for the cumulative histogram. |
Source code in nelpy/decoding.py
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 813 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 |
|
cumulative_dist_decoding_error_using_xval(bst, extern, *, decodefunc=decode1D, k=5, transfunc=None, n_extern=100, extmin=0, extmax=100, sigma=3, n_bins=None, randomize=False)
Compute the cumulative distribution of decoding errors using k-fold cross-validation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bst | BinnedSpikeTrainArray | Binned spike train array containing all epochs to decode. | required |
extern | object | Query-able object of external correlates (e.g., position AnalogSignalArray). | required |
decodefunc | callable | Decoding function to use (default is decode1D). | decode1D |
k | int | Number of folds for cross-validation (default is 5). | 5 |
transfunc | callable | Function to transform external variable (default is None). | None |
n_extern | int | Number of external bins (default is 100). | 100 |
extmin | float | Minimum value of external variable (default is 0). | 0 |
extmax | float | Maximum value of external variable (default is 100). | 100 |
sigma | float | Smoothing parameter for tuning curve (default is 3). | 3 |
n_bins | int | Number of decoding error bins (default is 200). | None |
randomize | bool | If True, randomize the order of epochs (default is False). | False |
Returns:
Name | Type | Description |
---|---|---|
cumhist | Cumhist | Cumulative histogram of decoding errors. |
bincenters | ndarray | Bin centers for the cumulative histogram. |
Source code in nelpy/decoding.py
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 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 |
|
decode1D(bst, ratemap, xmin=0, xmax=100, w=1, nospk_prior=None, _skip_empty_bins=True)
Decode binned spike trains using a 1D ratemap (Bayesian decoding).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bst | BinnedSpikeTrainArray | Binned spike train array to decode. | required |
ratemap | array_like or TuningCurve1D | Firing rate map with shape (n_units, n_ext), where n_ext is the number of external correlates (e.g., position bins). The rate map is in spks/second. | required |
xmin | float | Minimum value of external variable (default is 0). | 0 |
xmax | float | Maximum value of external variable (default is 100). | 100 |
w | int | Window size for decoding (default is 1). | 1 |
nospk_prior | array_like or float | Prior distribution over external correlates with shape (n_ext,). Used if no spikes are observed in a decoding window. If scalar, a uniform prior is assumed. Default is np.nan. | None |
_skip_empty_bins | bool | If True, skip bins with no spikes. If False, fill with prior. | True |
Returns:
Name | Type | Description |
---|---|---|
posterior | ndarray | Posterior distribution with shape (n_ext, n_posterior_bins). |
cum_posterior_lengths | ndarray | Cumulative posterior lengths for each epoch. |
mode_pth | ndarray | Most likely position at each time bin. |
mean_pth | ndarray | Expected position at each time bin. |
Examples:
>>> posterior, cum_posterior_lengths, mode_pth, mean_pth = decode1D(bst, ratemap)
Source code in nelpy/decoding.py
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 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 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 |
|
decode2D(bst, ratemap, xmin=0, xmax=100, ymin=0, ymax=100, w=1, nospk_prior=None, _skip_empty_bins=True)
Decode binned spike trains using a 2D ratemap (Bayesian decoding).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bst | BinnedSpikeTrainArray | Binned spike train array to decode. | required |
ratemap | array_like or TuningCurve2D | Firing rate map with shape (n_units, ext_nx, ext_ny), where ext_nx and ext_ny are the number of external correlates (e.g., position bins). The rate map is in spks/second. | required |
xmin | float | Minimum x value of external variable (default is 0). | 0 |
xmax | float | Maximum x value of external variable (default is 100). | 100 |
ymin | float | Minimum y value of external variable (default is 0). | 0 |
ymax | float | Maximum y value of external variable (default is 100). | 100 |
w | int | Window size for decoding (default is 1). | 1 |
nospk_prior | array_like or float | Prior distribution over external correlates with shape (ext_nx, ext_ny). Used if no spikes are observed in a decoding window. If scalar, a uniform prior is assumed. Default is np.nan. | None |
_skip_empty_bins | bool | If True, skip bins with no spikes. If False, fill with prior. | True |
Returns:
Name | Type | Description |
---|---|---|
posterior | ndarray | Posterior distribution with shape (ext_nx, ext_ny, n_posterior_bins). |
cum_posterior_lengths | ndarray | Cumulative posterior lengths for each epoch. |
mode_pth | ndarray | Most likely (x, y) position at each time bin. |
mean_pth | ndarray | Expected (x, y) position at each time bin. |
Examples:
>>> posterior, cum_posterior_lengths, mode_pth, mean_pth = decode2D(bst, ratemap)
Source code in nelpy/decoding.py
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 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 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 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 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 |
|
get_mean_pth_from_array(posterior, tuningcurve=None)
Compute the mean path (expected position) from a posterior probability matrix.
If a tuning curve is provided, the mean is mapped back to external coordinates/units. Otherwise, the mean is in bin space.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
posterior | ndarray | Posterior probability matrix (position x time). | required |
tuningcurve | TuningCurve1D | Tuning curve for mapping bins to external coordinates. | None |
Returns:
Name | Type | Description |
---|---|---|
mean_pth | ndarray | Expected position at each time bin. |
Source code in nelpy/decoding.py
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 |
|
get_mode_pth_from_array(posterior, tuningcurve=None)
Compute the mode path (most likely position) from a posterior probability matrix.
If a tuning curve is provided, the mode is mapped back to external coordinates/units. Otherwise, the mode is in bin space.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
posterior | ndarray | Posterior probability matrix (position x time). | required |
tuningcurve | TuningCurve1D | Tuning curve for mapping bins to external coordinates. | None |
Returns:
Name | Type | Description |
---|---|---|
mode_pth | ndarray | Most likely position at each time bin. |
Source code in nelpy/decoding.py
180 181 182 183 184 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 |
|
k_fold_cross_validation(X, k=None, randomize=False)
Generate K (training, validation) pairs from the items in X for cross-validation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X | list or int | List of items, list of indices, or integer number of indices. | required |
k | int or str | Number of folds for k-fold cross-validation. 'loo' or 'LOO' for leave-one-out. Default is 5. | None |
randomize | bool | If True, shuffle X before partitioning. Default is False. | False |
Yields:
Name | Type | Description |
---|---|---|
training | list | Training set indices. |
validation | list | Validation set indices. |
Examples:
>>> X = [i for i in range(97)]
>>> for training, validation in k_fold_cross_validation(X, k=5):
... print(training, validation)
Source code in nelpy/decoding.py
586 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 626 627 628 629 630 631 632 |
|
rmse(predictions, targets)
Calculate the root mean squared error (RMSE) between predictions and targets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions | array_like | Array of predicted values. | required |
targets | array_like | Array of target values. | required |
Returns:
Name | Type | Description |
---|---|---|
rmse | float | Root mean squared error of the predictions with respect to the targets. |
Source code in nelpy/decoding.py
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 |
|