# Automated Measurement of Grid Cell Firing Characteristics
## 2 Design of Algorithms
```
Input:autocorrelogram:\(A\);x-andy-axiscoordinatesofcentroids:\(C_{x},C_{y}\), respectively; angleincrement:\(I\);maximumdetectiondistance:\(D\); non-fieldfilteringthreshold:\(T\); countofcolumnsandrowsin\(A:x_{max},\ y_{max}\), respectively Initialize:centroid correlation values:\(V\)asan empty vector; boundary found boolean flag:\(B=false\);field boundariesx-and y-coordinates:\(X,Y,\)respectively,asempty matrices Output:\(X\);\(Y\)
1:for\((i=1:\)number of centroids\()\)do
2:\(append\)\(V\)with \(A\big{(}round\big{(}C_{y}(i)\big{)},\ round(C_{x}(i)\big{)}\big{)}\)
3:endfor
4:for\((i=1:\)number of centroids\()\)do
5:for\((a=0;a<360;a=a+1)\)do
6:\(x_{prior}\gets C_{x}(i)\)
7:\(y_{prior}\gets C_{y}(i)\)
8:\(B\gets false\)
9:for\((h=0:D)\)do
10:\(x_{scan},y_{scan}\gets find\_ver\_hor(a,h)\)
11:\(x_{scan}\gets round(x_{scan}+C_{x}(i))\)
12:\(y_{scan}\gets round\big{(}y_{scan}+C_{y}(i)\big{)}\)
13:if\(x_{scan}>x_{max}\)do\(x_{scan}\gets x_{max}\)endif
14:if\(y_{scan}>y_{max}\)do\(y_{scan}\gets y_{max}\)endif
15:if\(x_{scan}<1\)do\(x_{scan}\gets 1\)endif
16:if\(y_{scan}<1\)do\(y_{scan}\gets 1\)endif
17:if\(A(y_{scan},x_{scan})<V(i)*T\)&\(B==false\)do
18:\(append\)\(X(i)\)with\(x_{prior}\)
19:\(append\)\(Y(i)\)with\(y_{prior}\)
20:\(B\gets true\)
21:endif
22:\(x_{prior}\gets x_{scan}\)
23:\(y_{prior}\gets y_{scan}\)
24:endfor
25:endfor
```  
**Algorithm 2** Radar scan  
Figure 3: Local Border Detection. (**A**) Radar scanning approach to field boundary detection. (**B**) Field boundaries found for the seven central fields. (**C**) Filled-in field regions within the respective boundaries. The radar scan algorithm first stores the correlation or firing rate values of each field centroid position. A 360-degree scan with a preset polar increment (e.g., 2 degrees) starts 1 pixel from each centroid point and is repeated at increasing 1-pixel distances up to a prespecified maximum scanning distance. A function named find_ver_hor converts each angle and distance tested into x- and \(y\)-axis movements (rounded to the nearest pixel), starting from the centroid and reaching to the scanned distance. If those movements extend beyond the borders of the environment, then they are updated to only reach the border to avoid testing a correlation or firing value beyond the environment area. The algorithm checks if the scanned point has a value below the non-field value threshold multiplied by the value at the centroid. This localizes the comparison to each centroid instead of relying on a global average of all values. Once a value below the threshold is found, the prior value in the scan is saved as a boundary point. This process continues until a boundary for each field is constructed. Automatic border point outlier removal is an option enabled by default and sets points to be, at most, a user-set percentage (default 25%) beyond the median distance between a field's centroid and its border points. This helps fields retain more accurate shapes by avoiding outlier points skewing that shape recognition. All positions within the field's border are then added to that field's area (Figure 3C). GridMet then re-calculates the centroid location of each field (using simple coordinate means or weighted 'centers-of-mass') based on the so-derived areas. This local border detection step helps correct issues with uneven firing rates among fields, but users can also choose to exclude it and keep the fields and corresponding centroids computed from DBSCAN in subsequent measurements. GridMet derives all the reported measurements from this process. The area of each field consists of the number of contained pixels converted into physical units (cm). The Euclidean distance between centroids quantifies the field spacing. In order to calculate a ratio of field spacing to size, GridMet converts field sizes from areas into a linear metric by a diameter-like formula of \(\text{Si}_{\text{n}}=2(\text{Si}_{\text{a}}/\pi)^{0.5}\), where \(\text{Si}_{\text{a}}\) is the mean area of that neuron's grid fields.