# Reconstructing Nerve Structures from Unorganized Points
## 4 Materials and Methods  
### Initializing the Algorithm and the Clusters  
The algorithm works recurrently and processes each cross-sectional level of each nerve fascicle individually. It starts by loading all the points from one level. As the points are initially unsorted, the goal is to organize and group them into clusters, i.e., connect them into lines. The initial point of the first cluster can be sampled from the input set randomly. Each following cluster from the same level is initialized by the first remaining, i.e., unsorted point from the input set. The part of the algorithm that sorts points into corresponding clusters is based on the valley-seeking clustering method. This is a nonparametric technique that estimates the gradient of a density function [31; 32; 33]. Since a higher density of this function means more samples, our method's objective is to find the regions with the highest density function and, thus, detect individual clusters. Figure 3: (**A**) Collection of unorganized points to be linked into a curve. (**B**) The anticipated curve’s form. (**C**) The outcome after linking the initial, unsorted points. (**D**) A magnified view of (**C**). The graph theoretic valley-seeking technique involves the following procedure: an area \(G(X)\) around point \(X\) is selected (Figure 4):  
\[G(X)=\{Y:d(Y,X)\leq r\}, \tag{1}\]  
where \(r\) is the radius of the area \(G(X)\). A measure of the distance \(d(Y,X)\) is specified by:  
\[d^{2}(Y,X)=(Y-X)^{T}A^{-1}(Y-X), \tag{2}\]  
where \(A\) is the metric to measure the distance. If the data about the distribution statistics are insufficient, the Euclidean metric \(A=I\) can be used, in which case the area \(G(X)\) will take the form of a circle. This assumption is applicable to the problem we tackled (Figure 4). All the points that can be found inside the area \(G(X)\) are taken into consideration as potential candidates for the point \(X\)'s successor. The local mean \(M(X)\) can be calculated as:  
\[M(X)=E\{(Y-X)\mid G(X)\}=\int_{G(X)}(Y-X)\frac{f(Y)}{u_{0}}\mathrm{d}Y, \tag{3}\]  
where \(f(Y)\) is the a priori density function in the point \(Y\) and \(u_{0}\) is the probability that \(Y\) belongs to the area \(G(X)\)[32]. Under the assumption that the area \(G(X)\) is small enough, the probability \(u_{0}\) can be expressed as:  
\[u_{0}=\int_{G(X)}f(Y)\mathrm{d}Y\cong f(X)v, \tag{4}\]  
where \(v\) would be the volume of the area \(G(X)\). By implementing Equation (4) in Equation (3) and expanding \(f(Y)\) from Equation (3) by a Taylor series, we can write the following:  
\[M(X)\cong\int_{G(X)}(Y-X)\frac{f(X)+(Y-X)^{T}\bigtriangledown f(X)}{f(X)v} \mathrm{d}Y=\frac{r^{2}}{n+2}A\frac{\bigtriangledown f(X)}{f(X)}, \tag{5}\]  
where \(n=dim\{X\}\) and \(\bigtriangledown f(X)\) is the gradient of the density function in the area \(G(X)\). From Equation (5), we can derive the normalized gradient:  
\[\frac{\bigtriangledown f(X)}{f(X)}\cong\frac{n+2}{r^{2}}A^{-1}M(X), \tag{6}\]  
where \(M(X)\) can be estimated based on the sample of all the points contained in the area \(G(X)\), as their mean value. Figure 4: Graphical representation of valley-seeking clustering implemented in the algorithm. The point \(X\) (black) and the area \(G(X)\) around it (shaded region); all the points located in this area (pink) are candidates for the next point Y in the cluster. For finding the next point of the cluster, i.e., the successor of the point \(X\), we can define a measure \(S_{xy}\):  
\[S_{xy}=\left\|\frac{\bigtriangledown f(X)}{f(X)}\right\|\cos\theta_{xy}, \tag{7}\]  
where \(S_{xy}\) is defined for each point \(Y\) from the area \(G(X)\). \(\left\|\frac{\bigtriangledown f(X)}{f(X)}\right\|\) is defined by Equation (6), while \(\theta_{xy}\) is the angle between vectors \(\left\|\bigtriangledown f(X)\right\|\) and \((Y-X)\). The point that maximizes the measure \(S_{xy}\) is chosen as \(X\)'s successor.