Figures and data

Models for grid representation and experiment design
(A) Positional grid code model with firing fields anchored to the environment.
(B) Grid firing encoding distance rather than position.
(C) Neurons were recorded in an open arena and then in a location memory task. Trials were configured with a reward for stopping in a visually cued zone (beaconed), or a reward for stopping in the same zone but with the cue absent (non-beaconed), or without the visual cue or the reward (probe).
(D) In a positional coding scheme, a grid cell fires with field spacing λ and resets its firing every trial by anchoring its fields to the same track location, with a realignment lag R observed in the spatial autocorrelogram. Fields locations remain constant on each trial and thus peaks in the spatial periodogram occur at integer spatial frequencies relative to the track repetition (see Figure 1, Figure Supplement 3)
(E) In a distance coding scheme, a grid cell fires with field spacing λ and continues to fire at regular intervals without anchoring to the track. Unless field spacing and the track length are integer divisible, the location of fields varies across trials, and thus the peak of the spatial periodogram is not constrained to an integer spatial frequency (see Figure 1, Figure Supplement 3).

Grid cells use position or distance coding schemes
(A-C) Examples of grid cells with activity during the location memory task that follows predictions of positional (A) or distance coding schemes (B), and example of a cell that does not follow either scheme (C). Examples are ordered in their respective groups by their spatial information content on the track. From top to bottom plots show: heap map of firing rate as a function of track position, spatial autocorrelation of the track firing rate, spatial periodogram of the track firing, open field firing rate map and open field spatial autocorrelogram. The red line indicates the false alarm threshold estimated from shuffled data and significant peaks are labelled with a triangle. X-axis scales are adjusted on the virtual reality spatial autocorrelation for several examples to better illustrate the long-range periodic signal.
(D) Peak power as a function of the spatial frequency at which the peak occurs for all recorded cells. The red dashed line indicates the false alarm threshold generated from shuffled data.
(E) Percentage of grid (G) and non-grid (NG) cells classified to position, distance and aperiodic groups.
(F) Comparison of mean firing rate (position vs distance, DF = 98, P = 0.63, T = 1257.0; position vs aperiodic, DF = 69, P = 0.03, T = 185.0; distance vs aperiodic, DF = 33, P = 0.06, T = 10.0, Mann-Whitney U tests, adjusted for multiple comparison using a Bonferroni correction), spatial information scores (position vs distance, DF = 98, P = 0.003, T = 1535.0; position vs aperiodic, DF = 69, P = 0.78, T = 61.0; distance vs aperiodic, DF = 33, P = 0.09, T = 83.0, Mann-Whitney U tests, adjusted for multiple comparison using a Bonferroni correction), peak power (DF = 98, P = 0.0007, T = 1549.0, Mann-Whitney U test), and peak width (DF = 98, P = 0.0001, KS = 0.46, Kolmogorov-Smirnov test) between position, distance and aperiodic encoding grid cells.

Coding schemes switch within behavioural sessions
(A) Under a ‘stable coding’ scenario grid cells represent either position or distance throughout the recording session (left), whereas with ‘unstable coding’ the grid activity switches between representation of position and distance (right).
(B-E) Example trial by trial firing rate heat maps (upper left), corresponding rolling periodogram heat maps (upper right), mean rate maps (lower left) and mean periodograms (lower right) for neurons exhibiting stable positional coding (B), stable distance coding (C) and unstable coding in which representations switch between position and distance (D-E).
(F) Distribution across all recorded grid cells of position encoding trials (left), distance encoding trials (centre) and aperiodic trials (right).
(G) Spatial information is higher for trials on which grid cells encode position compared to distance (left)(P = 0.006, T = 1018.0, DF = 78, Wilcoxon signed-rank test), whereas the average firing rate is similar between position and distance-encoding epochs (right)(P = 0.2, T = 1541.0, DF = 84, Wilcoxon signed-rank test).

Spatial behaviour during position and distance encoding by grid cells
(A-B) Running speed as a function of track position for trial outcomes classified as a hit, try, or run (A), and for beaconed and non-beaconed trials in which a recorded grid cell encodes position (upper) or distance (lower)(B).
(C-F) Example of variation in the behaviour-related activity of grid cells recorded on the location memory task. (C) and (D) show examples of stable position and distance coding respectively, whereas (E) and (F) show unstable position-distance coding. Speed profiles in (A) and (B) are taken from the session and grid cell shown in (E).

Positional grid coding is not required for cued spatial localisation but promotes path integration-dependent localisation
(A, C) Stopping probability relative to baseline as a function of position for grid cells with stable grid cell codes (A) and for all grid cells (C). Shaded regions in A and B represent standard error of the mean measured across sessions and cells respectively.
(B) Percentage of hits on beaconed, non-beaconed and probe trials when grid cells encode position (turquoise) or distance (purple) for sessions with stable grid code. (Beaconed, P = 0.33, Χ2 = 0.95, DF = 19; Non-beaconed, P = 0.024, Χ2 = 5.11, DF = 1; Probe, P = 0.075, Χ2 = 3.17, DF = 1, ANOVA, see Methods).
(D) Percentage of hits on beaconed, non-beaconed and probe trials when grid cells encode position or distance trial epochs. (Beaconed, P = 0.26, Χ2 = 1.25, DF = 1; Non-beaconed, P = 3.16e-5, Χ2 = 17.3, DF = 1; Probe, P = 0.32, Χ2 = 0.98, DF = 1, ANOVA, see Methods)
(E) Percentage of trials with hit, try and run outcomes in which grid cells encode position or distance. (Beaconed-position, P = 0.3, Χ2 = 2.38, DF = 1; Non-beaconed-position, P = 0.02, Χ2 = 7.87, DF = 1; Probe-position, P = 0.13, Χ2 = 4.05, DF = 1, Beaconed-distance, P = 0.28, Χ2 = 2.53, DF = 1; Non-beaconed-distance, P = 0.26, Χ2 = 2.70, DF = 1; Probe-distance, P = 0.08, Χ2 = 5.04, DF = 1, ANOVA, see Methods). Error bars denote standard error of the mean measured across the mean values for each animal. Faded lines show percentage values for individual mice that contained hit, try and run trials for a given trial type.
(F) Odds ratio between receipt of reward for trials on which grid cells encode position relative to trials on which they encode distance (Beaconed, P = 0.23, non-beaconed, P = 0.002, probe, P = 0.27).

Procedure for extracting a spatial periodogram from a set of spike timestamps
A single cell’s spikes are binned in space (1 cm bin size) and smoothed using a Guassian kernel (SD = 2 cm). Least squares spectral analysis (LSSA) was computed on a signal equivalent to 3 track lengths long using the Lomb-Scargle implementation in the Astropy python module (Δx = distance elapsed in trials, Δy = firing rate)(The Astropy Collaboration et al., 2022). The signal was advanced 10 cm and repeated for the whole session. All spatial periodograms were concatenated and averaged across trials to generate the average spatial periodogram.

Procedure for estimating a false alarm threshold for a given cell
A cell’s spikes were binned in space (1 cm bin size) and smoothed using a Guassian kernel (SD = 4 cm). Firing fields were detected and the original field bins reallocated to a new unsmoothed rate map. Bins not attributed to a field were allocated to the remaining gaps in the new map. To identify fields, the peaks and troughs of the highly smoothed firing rate map were first identified, with a minimum peak distance of 20 cm, smaller peaks were removed until the condition was satisfied (see Scipy.signal.find_peaks). Fields were defined as the detected trough to trough. The unsmoothed rate map was then smoothed with a Guassian kernel (SD = 2 cm). An average spatial periodogram was calculated and peak power detected. This is repeated 1000 times and a false alarm threshold assigned from the 99th percentile of the shuffled cell’s peak power distribution.

Identification of position-anchored periodic signals
Examples of least squares spectral analysis (LSSA) used to estimate the sinusoidal components that best describe the firing rate profile of a positional grid code (upper) and a distance grid code (lower). In each plot the schematised firing fields (solid colours) are shown as a function of track position. Each row shows examples of different sinusoidal components (f = frequency, p = period) at different phases. LSSA minimises the chi-squared error by searching over all phases (three example phases are shown in red, green and blue per row) and amplitudes (amplitude is ignored in this example for simplicity) for each spatial frequency. For positional grid representations, the chi-squared error is minimal at positive-integer spatial frequencies that correspond to sinusoidal waves with periods that are integer divisible by the track length. For distance encoding grid representations the chi-squared error is minimal at a spatial frequency that corresponds to the underlying periodic firing rate profile.

Expected spatial periodicity for different functional cell types
Plots show simulations of track firing for (i) a positional grid (PG) cell, (ii) a distance grid (DG) cell, (iii) a positional grid cell with field jitter, (iv) a distance grid cell with field jitter, (v) a place cell, (vi) a ramp-like cell, (vii) a cell with randomly positioned fields and (viii) a cell with Guassian noise-like activity. Each group of panels shows firing rate heat map by trial (upper left) and corresponding average firing rate (lower left), rolling spatial periodogram (upper centre) and corresponding average (lower centre), the true and predicted trial classification (upper right). The predicted trial classification is established using analyses described in Figure 1, Figure Supplements 1–3.

Validation of accuracy and bias for classification of positional and distance encoding by grid cells
To validate classification of grid encoding we simulated 500 anchored grid cells and 500 non-anchored grid cells with grid spacings between 40-400 cm (randomly assigned individually). Each grid cell was simulated as a periodic set of firing fields on a linear track of length 200 cm and with 100 trials. Cells were simulated as position encoding by anchoring field locations to track locations on each trial, or as distance encoding by continuing the periodic field locations without explicit track anchoring. Simulations were repeated using different spike rate probabilities and different field jitters (as defined by the standard deviation of the field locations from their preassigned locations) parameter values. Roughly, Pmax(spike) values of 0.01, 0.1 and 1 covered average firing rates on the order of magnitudes of 10-1, 100, 101 Hz respectively whereas jitter SD values of 0, 10, 20 and 30 cm covered a range of deviations from perfect periodic firing. To compute the maximal prediction accuracy from our classification based on a spatial frequency threshold, we calculated the accuracy and bias across the full range of spatial frequencies from a positive integer (0 - 0.5). Classifications were made on each set of simulations in the same way as for analysis of experimental data (see Figure 1, Figure Supplements 1–4). Prediction accuracy (left) and bias (right) are shown as a function of the spatial frequency from a positive-integer used for the spatial frequency threshold. Chance level accuracy is denoted with a red dashed line.

Classification accuracy and bias as a function of the number of periodograms used for rolling classification
To assess classification of grid firing on individual trials 100 grid cells were simulated with grid spacings between 40-400 cm (randomly assigned individually). Each grid cell was simulated as a periodic set of firing fields following on a linear track of length 200 cm and with 100 trials. Cells were simulated as position encoding by anchoring field locations to track locations on each trial, or as distance by maintaining the field periodicity across trials irrespective of track position. Simulations were repeated using different spike rate probabilities and different field jitters (as defined by the standard deviation of the field locations from their preassigned locations) parameter values. Roughly, Pmax(spike) values of 0.01, 0.1 and 1 covered average firing rates on the order of magnitudes of 10-1, 100, 101 Hz respectively whereas jitter SD values of 0, 10, 20 and 30 cm covered a range of deviations from perfect periodic firing. Two separate methods were used to alternate between position coding and distance coding within the simulated session. The first allowed for alternation in blocks of trials (by trial block) whereas the second allowed for alternation independent of the previous trial (by trial), see methods. For each trial the rolling classifier was used to assess whether firing could be identified as position or distance encoding.
(A) Examples of the activity of simulated grid cells that switch between positional and distance encoding modes of operation. Each example shows the firing rate heatmap by trials (upper left), the average firing rate map (lower left), the rolling spatial periodogram (upper right), the average spatial periodogram (lower right) and the classification labels (true and predicted; far right).
(B) To assess classification accuracy under different rolling windows, we calculated the prediction accuracy and bias. Chance level is denoted with a red dashed line. Classifications were made on each set of simulations by comparing the spatial frequency at which a peak occurred within the average spatial periodogram. Prediction accuracy and bias were calculated for a range of window sizes of the rolling periodogram to generate the average spatial periodogram in which to extract the peak spatial frequency. On the left, shows the prediction accuracy (top) and the prediction bias (bottom) when grid cells alternate between anchored and non-anchored firing fields in blocks of trials, whereas the right shows the accuracy and bias when the probability of track anchoring is independent of whether track anchoring occurred on the previous trial.

Classification accuracy and bias on the level of trial as a function of rolling window size
The false alarm threshold is defined by the 99th percentile of the distribution of the peak power of the average spatial periodogram for 1000 shuffled instances of a cell’s firing rate profile across the full experimental session. The increased number of periodograms over which to average decreases this false alarm threshold asymptotically. Each red line represents a single cell within a representative session. The blue line represents the maximum number of periodograms in this session.

Assessment of the length of periodic coding blocks
(A) We compared the length of coding blocks - sequences of trials in which grid cells operate in the same mode - in the observed data with the length in datasets shuffled at the level of trials. To generate shuffled data spike locations were reallocated to a new position in the firing profile by randomly shuffling the trials. The rolling classification was then recomputed and length of coding blocks measured.
(B) Cumulative histogram of block lengths for the observed grid cell population (red) and the same grid cell population with the trial order shuffled and blocks recalculated (grey). P value for the Kolmogorov-Smirnov test is given above.

Grid cells switch between coding schemes coherently
(A) Joint activity of 6 simultaneously recorded grid cells. From top to bottom, plots show the firing rate map across trials, rolling spatial periodogram with predicted classifications to the right, and the average spatial periodogram. Cluster IDs (CIDs) are labelled at the top left on each panel.
(B) (i) Encoding blocks of the rolling classification are shuffled by positioning whole encoding blocks at random positions and (ii) an agreement score calculated based on the proportion of the session where coding blocks overlap between two cells.
(C) Agreement matrix for the coding scheme across a session for the example cells in A for the true comparison and between the average of shuffled blocks. Cluster IDs are ordered by their grid spacing (lowest to highest).
(D) Agreements between the true grid pairs and the block shuffled pairs test whether coding blocks agree within a session between pairs of grid cells above chance level (n = 110 grid pairs, N = 18 sessions). Only cells without a stable coding scheme were compared (<85 % of a stable coding scheme; Wilcoxon sign-rank test).

Classification of trial outcomes
Trials were classified into hits, tries and runs, based on stopping locations and the speed profile on a given trial. First, trials were classified into hits and misses based on whether a stop was registered in the reward zone or not. Next, a distribution of the average speeds in the reward zone is drawn for hit and miss trials, the 95th percentile of the hit average speeds in the reward zone was used to split the miss trials into near hits (tries) and run-throughs (runs). Finally, slow trials were removed to better discriminate trial outcomes based on the mouse’s engagement.


Tetrode localisation.
Micro-CT images used for assessment of tetrode locations in eight mice. For each animal, sagittal slices are presented lateral to medial from left to right and the classification of the tetrodes target is shown at the top left. This classification is based on the terminal location of the tetrode and the distance travelled during the experiment (see Methods). M10 sustained significant damage during preparation for microCT imaging and was not imaged. In all images red triangles point to the tetrode tracks. Scale bar denotes 1 mm.