Utilities
Gaussian Helpers
Calculates the multivariate Gaussian PDF at x.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Array
|
The position at which to evaluate the pdf |
required |
mu
|
Array
|
The mean of the distribution |
required |
sigma
|
Array
|
The covariance of the distribution |
required |
Returns:
| Name | Type | Description |
|---|---|---|
pdf |
float
|
The probability density at x |
Source code in src/simpl/utils.py
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 | |
Calculates the log of the multivariate Gaussian PDF at x.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Array
|
The position at which to evaluate the pdf |
required |
mu
|
Array
|
The mean of the distribution |
required |
sigma
|
Array
|
The covariance of the distribution |
required |
Returns:
| Name | Type | Description |
|---|---|---|
log_pdf |
float
|
The log probability density at x |
Source code in src/simpl/utils.py
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 | |
Calculates the normalizing constant of a multivariate normal distribution with covariance sigma.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sigma
|
Array
|
The covariance matrix of the distribution |
required |
Returns:
| Name | Type | Description |
|---|---|---|
norm_const |
(ndarray, shape(1))
|
The normalizing constant |
Source code in src/simpl/utils.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
Gaussian Fitting
Fits a multivariate-Gaussian to each of T likelihood distributions over spatial bins.
For each timestep, computes the weighted mean, mode, and covariance of the
spatial bin coordinates x under the likelihood weights:
The covariance uses the identity Cov = E[xx^T] - mu mu^T. This lets us
precompute x x^T once as a small (N_bins, D, D) array and contract it
with the (T, N_bins) likelihoods via a single einsum, rather than
materialising a (T, N_bins, D) intermediate as the naive formula would.
The function is JIT-compiled so the XLA computation is traced once and reused on subsequent calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
(ndarray, shape(N_bins, D))
|
The position bins (shared across all time steps). |
required |
likelihoods
|
(ndarray, shape(T, N_bins))
|
Likelihood values (not log) at each bin for each time step. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
means |
(ndarray, shape(T, D))
|
The weighted mean position at each time step. |
modes |
(ndarray, shape(T, D))
|
The bin coordinate with the highest likelihood at each time step. |
covariances |
(ndarray, shape(T, D, D))
|
The weighted covariance at each time step. |
Source code in src/simpl/utils.py
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 185 186 187 188 | |
Samples from a multivariate normal distribution with mean mu and covariance sigma.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
PRNGKey
|
The random key |
required |
mu
|
(ndarray, shape(D))
|
The mean of the distribution |
required |
sigma
|
(ndarray, shape(D, D))
|
The covariance of the distribution |
required |
Returns:
| Name | Type | Description |
|---|---|---|
sample |
(ndarray, shape(D))
|
The sample |
Source code in src/simpl/utils.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
Statistical and Analysis Helpers
Calculates the coefficient of determination (\(R^2\)) between X and Y.
This reflects the proportion of the variance in Y that is predictable from X.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
(ndarray, shape(N, D))
|
The predicted latent positions |
required |
Y
|
(ndarray, shape(N, D))
|
The true latent positions |
required |
Returns:
| Name | Type | Description |
|---|---|---|
R2 |
(Array, scalar)
|
The coefficient of determination. 1.0 indicates a perfect prediction; 0.0 indicates the model explains no more variance than the mean of Y; negative values indicate worse-than-mean predictions. |
Source code in src/simpl/utils.py
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 | |
Uses canonical correlation between X and Y (the "target") to establish the best linear mapping from X to Y.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
(ndarray, shape(N, D))
|
The inputs |
required |
Y
|
(ndarray, shape(N, D))
|
The targets |
required |
Returns:
coef : jnp.ndarray, shape (D, D) The coefficients of the linear mapping from X to Y such that Y ~= Y_pred = X @ coef.T + intercept intercept : jnp.ndarray, shape (D,) The intercept of the linear mapping from X to Y such that Y ~= Y_pred = X @ coef.T + intercept
Source code in src/simpl/utils.py
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 | |
Align 1D circular trajectories by a pure rotation (no scaling).
Searches rotation angles in [-pi, pi) and returns the angle that minimises
mean squared wrapped angular error. Unlike cca, this only performs a rotation
(no shift or scaling), which is the correct transform for angular data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
(ndarray, shape(N, 1) or (N,))
|
Source trajectory in radians. |
required |
Y
|
(ndarray, shape(N, 1) or (N,))
|
Target trajectory in radians. |
required |
n_angles
|
int
|
Number of candidate angles in [-pi, pi), by default 360. |
360
|
Returns:
| Name | Type | Description |
|---|---|---|
best_angle |
(ndarray, shape())
|
Rotation angle (radians) that minimises circular error. |
best_error |
(ndarray, shape())
|
Minimum mean squared wrapped angular error. |
Source code in src/simpl/utils.py
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 | |
Calculates the correlation between X1 and X2[lag:].
If X is D-dimensional, calculates the average correlation across dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X1
|
(ndarray, shape(T, D))
|
The first time series - remains fixed |
required |
X2
|
(ndarray, shape(T, D))
|
The second time series |
required |
lag
|
int
|
The lag to calculate the correlation at |
required |
Returns:
| Type | Description |
|---|---|
float
|
The average correlation across dimensions |
Source code in src/simpl/utils.py
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 | |
Data Preparation
Causal rolling sum of spikes over a backward-looking window.
Each time bin accumulates spikes from the current and previous
window - 1 bins. This is equivalent to smoothing the spikes with
a causal rectangular kernel.
Warning
This changes the interpretation of the estimated receptive fields.
Since each bin now contains on average window times more spikes,
the fitted firing rates (and therefore F) will be approximately
window times higher than the true single-bin rates. The receptive
field shapes are unaffected, but their amplitudes should not be
interpreted as physical firing rates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Y
|
(ndarray, shape(T, N_neurons))
|
Spike counts. |
required |
window
|
int
|
Number of bins to sum over (looking backwards). For example,
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
Y_accumulated |
(ndarray, shape(T, N_neurons))
|
Spike counts after causal rolling sum. |
Source code in src/simpl/utils.py
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 | |
Coarsen data by averaging over groups of dt_multiplier time bins.
Spikes are summed (not averaged) so that spike counts remain integers. Positions and time are averaged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Y
|
(ndarray, shape(T, N_neurons))
|
Spike counts. |
required |
Xb
|
(ndarray, shape(T, D))
|
Behavioral positions. |
required |
time
|
(ndarray, shape(T))
|
Time stamps. |
required |
dt_multiplier
|
int
|
Factor by which to coarsen the data. |
required |
Xt
|
(ndarray or None, shape(T, D))
|
Ground truth positions. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Y_coarse |
ndarray
|
Coarsened spike counts (summed). |
Xb_coarse |
ndarray
|
Coarsened behavioral positions (averaged). |
time_coarse |
ndarray
|
Coarsened time stamps (averaged). |
Xt_coarse |
np.ndarray (only if Xt was provided)
|
Coarsened ground truth positions (averaged). |
Source code in src/simpl/utils.py
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 | |
TODO : Rewrite this in JAX
Creates a boolean mask of size size. This mask is all True except along each column randomly
there are contiguous blocks of False of length block_size. Overall ~sparsity
of the mask is False. For example, if sparsity is 0.3, block size is 3 and size is
(4, 15), a valid mask would be:
[[T, T, T, T, T, T, T, T, F, F, F, T, F, F, F, T, T, T, T, T], [T, T, F, F, F, T, T, T, T, T, T, T, T, T, T, T, F, F, F, T], [T, T, T, T, T, T, T, T, T, F, F, F, T, T, F, F, F, T, T, T], [F, F, F, T, T, T, T, T, T, T, T, T, F, F, F, T, T, T, T, T]]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
tuple of int
|
The dimensions of the mask to create. |
required |
sparsity
|
float
|
The fraction of the mask that should be False. |
0.1
|
block_size
|
int
|
The size of the contiguous False blocks. |
10
|
Returns:
| Name | Type | Description |
|---|---|---|
mask |
ndarray
|
A boolean mask with the specified properties. |
Source code in src/simpl/utils.py
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 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 | |
Data I/O
Load a demo data file, downloading from GitHub releases if not cached.
Resolution order (skipped when force_download is True):
- User-specified directory — if directory is given, look for
<directory>/<name>first. - Local source tree —
examples/data/relative to the package root (available in editable / development installs). - User cache —
~/.simpl/data/. - Download — fetched from the latest GitHub release and saved to the user cache for next time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Filename to load (e.g. |
'gridcells_synthetic.npz'
|
directory
|
str or None
|
Optional directory to search for name before the default locations. |
None
|
force_download
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
NpzFile
|
The loaded |
Raises:
| Type | Description |
|---|---|
ValueError
|
If name is not one of the available demo data files. |
Source code in src/simpl/utils.py
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 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 | |
Save a SIMPL results xr.Dataset to a netCDF file.
Before writing, the function performs several type conversions required by
the netCDF4 format: boolean arrays (e.g. spike_mask) are cast to
int32, boolean attrs are cast to int, and trial_slices
(a list of Python slice objects) is serialised to a flat int64
array. Use load_results to reload and automatically reverse
these conversions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
Dataset
|
The results dataset (typically |
required |
path
|
str
|
Destination file path (e.g. |
required |
Source code in src/simpl/utils.py
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 | |
Load results from a saved file. Some variables need to be converted back to their original form.
See below issues for detail. https://github.com/TomGeorge1234/SIMPL/issues/5 https://github.com/TomGeorge1234/SIMPL/issues/8
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the saved file. |
required |
Returns:
| Type | Description |
|---|---|
Dataset
|
Results. |
Source code in src/simpl/utils.py
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 | |
Place-Field Analysis
Get argmax spatial position for each neuron's receptive field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
F
|
(ndarray, shape(N_neurons, N_bins))
|
Receptive fields. |
required |
coords
|
(ndarray, shape(N_bins, D))
|
Spatial coordinates for each bin (e.g. |
required |
Returns:
| Type | Description |
|---|---|
(ndarray, shape(N_neurons, D))
|
Peak spatial position for each neuron. |
Source code in src/simpl/utils.py
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 | |
Analyse tuning curves and return information about place fields.
Terminology: "field" is the whole tuning curve. "place field" (pf) is the portion of the whole tuning curve identified as a particular place field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
F
|
(ndarray, shape(N_neurons, N_bins))
|
The estimated place fields. |
required |
N_neurons
|
int
|
Number of neurons. |
required |
N_PFmax
|
int
|
Maximum number of place fields per neuron (for fixed-shape arrays). |
required |
D
|
int
|
Dimensionality of the latent space. |
required |
xF_shape
|
tuple
|
Shape of the discretised environment grid (e.g. |
required |
xF
|
(ndarray, shape(N_bins, D))
|
Flattened discretised environment coordinates. |
required |
dt
|
float
|
Time-step size (seconds). |
required |
bin_size
|
float
|
Spatial bin size of the environment. |
required |
n_bins
|
int
|
Total number of spatial bins. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Place-field results dictionary with keys such as
|
Source code in src/simpl/utils.py
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 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 | |
Calculate Skaggs spatial information per neuron (bits/s).
where \(r(x)\) is the firing rate at position \(x\), \(\bar{r}\) is the mean firing rate, and \(P(x)\) is the occupancy probability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r
|
Array(N_neurons, N_bins)
|
Firing rate maps in Hz (spikes per second, not per bin). |
required |
PX
|
Array(N_bins)
|
Occupancy probability over spatial bins (sums to 1). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
spatial_info |
Array(N_neurons)
|
Spatial information per neuron in bits/s. |
Source code in src/simpl/utils.py
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 | |