function [f1] = calcF1(GT, labels, frames, lim) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Calculates the F1 score of transient detection. Considers some situations %like if there is nothing in the ground truth, nothing detected by the %algorithm, or if everything detected by the algorithm is not a match. % Inputs: % - GT: Vector of the ground truth transient times % - labels: Vector of the identified transients times to test % - frames: Vector of frames over which to calculate the f1 score (only % useful if there is a specific portion of the data you want to % iterrogate) % - lim: The minimum difference between the GT and labels for which a % pair will be considered a hit % Output: % - f1: The f1 score %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% GT = GT(ismember(GT, frames)); labels = labels(ismember(labels, frames)); %If the ground truth is empty then the f1 is meaningless and a NaN is returned if ~isempty(GT) %If there are no labels it's an automatic 0 if ~isnan(labels) %No labels in the specified frames then it's a 0 recall = length(find(min(abs(GT-labels')) <= lim))/length(GT); Prec = length(find(min(abs(GT-labels')) <= lim))/length(labels); if recall == 0 && Prec == 0 f1 = 0; else f1 = (2*Prec*recall)/(Prec+recall); end else f1 = 0; end else f1 = NaN; end