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 Gaussian moments to each of T likelihood distributions over spatial bins.
Ordinary Euclidean weighted moments are used by default. When
is_1D_angular=True, the function instead computes the circular mean and
variance of wrapped residuals around that mean. The circular variance is in
radians squared, making it suitable as observation noise for the wrapped
Kalman filter.
The linear mean and covariance are:
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 |
is_1D_angular
|
bool
|
Whether |
False
|
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
175 176 177 178 179 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 219 220 221 222 223 224 | |
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
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
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
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 | |
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
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 | |
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
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 | |
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
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 | |
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.
Also, spatial information and mutual information will be inflated
roughly by a factor of window. To calculate the corrected
information, divide F by window before calling
calculate_spatial_information or calculate_mutual_information.
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
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 | |
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
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 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 | |
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 |
Array
|
A boolean mask with the specified properties, placed on |
Notes
Each column's False blocks are laid down vectorised, without a per-element Python loop:
for a chunk of columns, +1/-1 markers are scattered at every block's start/end into a
delta array, and a cumulative sum along time recovers the per-bin "inside a block"
count (> 0 ⇒ held out). Overlapping blocks within a column are handled naturally
by the running count. Columns are processed in chunks sized by MAX_BATCH_ELEMENTS
so the transient integer delta array stays bounded, and each chunk is written into a
single pre-allocated boolean array; the result is moved to the default JAX device in
one (zero-copy, on CPU) device_put.
Source code in src/simpl/utils.py
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 | |
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
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 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 908 909 910 911 912 913 914 915 916 917 918 919 | |
Save a SIMPL results xr.Dataset to a netCDF file.
Before writing, the function performs several type conversions required by
the NetCDF 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
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 | |
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
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 | |
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
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 | |
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
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 | |
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
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 | |