## 2 Materials and Methods
### Clustering and Trajectory Generation  
In order to work in the most reliable way possible, knowing that our system, experiment and conditions have limitations and being particularly concerned about a potential loss of information due to the image segmentation and processing methods, we decided to use a clustering method to identify the fish group and calculate the group's centroid. The centroids' positions were estimated by k-means because this algorithm is robust, with a good relationship between speed and stability and it works well with large amounts of data. Thus, once the centers of the objects were calculated, and knowing their coordinates in the two axes within each frame, k-means was applied to find the center of the entire group. In our particular case, the dataset were the objects' centers in each frame, from the first frame to the last one. K-means clustering operates on actual observations (rather than on a larger set of dissimilarity measures), and creates a single level of clusters. K-means then treats each observation in the data set as an object having a location in space. It finds a partition in which objects within each cluster are as close to each other, and as far from objects in other clusters, as possible. Depending on the kind of data to cluster, it is possible to use up to five different methods to calculate the distances. The best results for the current problem were obtained using the Euclidean distance [47, 48]. Each cluster in the partition was defined by its member objects and by its centroid, or center: the point where the sum of distances from all objects in the cluster is minimized. K-means computes the cluster's centroids differently for each distance measured in order to minimize the sum with respect to the distance specified. This is done using an iterative algorithm that minimizes the sum of the distances from each object to its cluster's centroid, over all clusters. The algorithm moves objects between clusters until the sum cannot be decreased further. The result is a set of clusters that are as compact and separated as possible. It is possible to control the details of the minimization using several optional input parameters to k-means, including the initial values of the cluster centroids, and the maximum number of iterations. This algorithm, often applied for image segmentation, has successfully been used to detect animals [48]. The centers of the clusters are calculated in a two space dimension (2D), using both coordinates (X and Y) and the frame number, which goes from 1 to n in the image sequence, and then a trajectory table is built (Table 1). A new figure (Figure 4) was then created by plotting the values obtained from Table 1: in the vertical axes the values corresponding to the pixel number of the \(x\)-coordinate of the centroid (in red) and to the pixel number of the \(y\)-coordinate of the centroid (in blue) and in the horizontal axis the frame number to which each \(x\) and \(y\) values correspond. Since the images have a 640 pixel \(\times\) 480 pixel format, the scale of the vertical axis in Figure 4 goes from 0 to 640 pixel number (the lowest and highest theoretical possible value for the \(x\)-pixel number in Table 1, when the centroid is placed either in the border right or border left of the frame). The lowest and highest theoretical possible value for the \(y\)-pixel number in Table are 0 and 480 respectively. The horizontal axis, representing the frame number varies in the three cases due to the processing of the video sequences. In all three cases there was a sharp change in the trajectory of the centroid in response to the stochastic event, which occurs around frame number 720. The panels to the right in Figure 4 show a magnification of the region of the plot corresponding to the response to the event.