|
| 1 | + |
| 2 | + |
| 3 | +function [allScores, allFPs, allMisses, allMerges] = compareClustering2(cluGT, resGT, cluTest, resTest, datFilename) |
| 4 | +% function compareClustering(cluGT, resGT, cluTest, resTest[, datFilename]) |
| 5 | +% - clu and res variables are length nSpikes, for ground truth (GT) and for |
| 6 | +% the clustering to be evaluated (Test). |
| 7 | + |
| 8 | + |
| 9 | +if nargin<5 |
| 10 | + datFilename = []; |
| 11 | +end |
| 12 | + |
| 13 | +GTcluIDs = unique(cluGT); |
| 14 | +testCluIDs = unique(cluTest); |
| 15 | +jitter = 12; |
| 16 | + |
| 17 | +nSp = zeros(max(testCluIDs), 1); |
| 18 | +for j = 1:max(testCluIDs); |
| 19 | + nSp(j) = max(1, sum(cluTest==j)); |
| 20 | +end |
| 21 | +nSp0 = nSp; |
| 22 | + |
| 23 | +for cGT = 1:length(GTcluIDs) |
| 24 | +% fprintf(1,'ground truth cluster ID = %d (%d spikes)\n', GTcluIDs(cGT), sum(cluGT==GTcluIDs(cGT))); |
| 25 | + |
| 26 | + rGT = int32(resGT(cluGT==GTcluIDs(cGT))); |
| 27 | + |
| 28 | +% S = sparse(numel(rGT), max(testCluIDs)); |
| 29 | + S = spalloc(numel(rGT), max(testCluIDs), numel(rGT) * 10); |
| 30 | + % find the initial best match |
| 31 | + mergeIDs = []; |
| 32 | + scores = []; |
| 33 | + falsePos = []; |
| 34 | + missRate = []; |
| 35 | + |
| 36 | + igt = 1; |
| 37 | + |
| 38 | + nSp = nSp0; |
| 39 | + nrGT = numel(rGT); |
| 40 | + flag = false; |
| 41 | + for j = 1:numel(cluTest) |
| 42 | + while (resTest(j) > rGT(igt) + jitter) |
| 43 | + % the curent spikes is now too large compared to GT, advance the GT |
| 44 | + igt = igt + 1; |
| 45 | + if igt>nrGT |
| 46 | + flag = true; |
| 47 | + break; |
| 48 | + end |
| 49 | + end |
| 50 | + if flag |
| 51 | + break; |
| 52 | + end |
| 53 | + |
| 54 | + if resTest(j)>rGT(igt)-jitter |
| 55 | + % we found a match, add a tick to the right cluster |
| 56 | +% numMatch(cluTest(j)) = numMatch(cluTest(j)) + 1; |
| 57 | + S(igt, cluTest(j)) = 1; |
| 58 | + end |
| 59 | + end |
| 60 | + numMatch = sum(S,1)'; |
| 61 | + misses = (nrGT-numMatch)/nrGT; % missed these spikes, as a proportion of the total true spikes |
| 62 | + fps = (nSp-numMatch)./nSp; % number of comparison spikes not near a GT spike, as a proportion of the number of guesses |
| 63 | + % |
| 64 | + % for cTest = 1:length(testCluIDs) |
| 65 | +% rTest = int32(resTest(cluTest==testCluIDs(cTest))); |
| 66 | +% |
| 67 | +% [miss, fp] = compareSpikeTimes(rTest, rGT); |
| 68 | +% misses(cTest) = miss; |
| 69 | +% fps(cTest) = fp; |
| 70 | +% |
| 71 | +% end |
| 72 | +% |
| 73 | + sc = 1-(fps+misses); |
| 74 | + best = find(sc==max(sc),1); |
| 75 | + mergeIDs(end+1) = best; |
| 76 | + scores(end+1) = sc(best); |
| 77 | + falsePos(end+1) = fps(best); |
| 78 | + missRate(end+1) = misses(best); |
| 79 | + |
| 80 | +% fprintf(1, ' found initial best %d: score %.2f (%d spikes, %.2f FP, %.2f miss)\n', ... |
| 81 | +% mergeIDs(1), scores(1), sum(cluTest==mergeIDs(1)), fps(best), misses(best)); |
| 82 | + |
| 83 | + S0 = S(:, best); |
| 84 | + nSp = nSp + nSp0(best); |
| 85 | + while scores(end)>0 && (length(scores)==1 || ( scores(end)>(scores(end-1) + 1*0.01) && scores(end)<=0.99 )) |
| 86 | + % find the best match |
| 87 | + S = bsxfun(@max, S, S0); |
| 88 | + |
| 89 | + numMatch = sum(S,1)'; |
| 90 | + misses = (nrGT-numMatch)/nrGT; % missed these spikes, as a proportion of the total true spikes |
| 91 | + fps = (nSp-numMatch)./nSp; % number of comparison spikes not near a GT spike, as a proportion of the number of guesses |
| 92 | + |
| 93 | + sc = 1-(fps+misses); |
| 94 | + best = find(sc==max(sc),1); |
| 95 | + mergeIDs(end+1) = best; |
| 96 | + scores(end+1) = sc(best); |
| 97 | + falsePos(end+1) = fps(best); |
| 98 | + missRate(end+1) = misses(best); |
| 99 | + |
| 100 | +% fprintf(1, ' best merge with %d: score %.2f (%d/%d new/total spikes, %.2f FP, %.2f miss)\n', ... |
| 101 | +% mergeIDs(end), scores(end), nSp0(best), nSp(best), fps(best), misses(best)); |
| 102 | + |
| 103 | + S0 = S(:, best); |
| 104 | + nSp = nSp + nSp0(best); |
| 105 | + |
| 106 | + end |
| 107 | + |
| 108 | + if length(scores)==1 || scores(end)>(scores(end-1)+0.01) |
| 109 | + % the last merge did help, so include it |
| 110 | + allMerges{cGT} = mergeIDs(1:end); |
| 111 | + allScores{cGT} = scores(1:end); |
| 112 | + allFPs{cGT} = falsePos(1:end); |
| 113 | + allMisses{cGT} = missRate(1:end); |
| 114 | + else |
| 115 | + % the last merge actually didn't help (or didn't help enough), so |
| 116 | + % exclude it |
| 117 | + allMerges{cGT} = mergeIDs(1:end-1); |
| 118 | + allScores{cGT} = scores(1:end-1); |
| 119 | + allFPs{cGT} = falsePos(1:end-1); |
| 120 | + allMisses{cGT} = missRate(1:end-1); |
| 121 | + end |
| 122 | + |
| 123 | +end |
| 124 | + |
| 125 | +initScore = zeros(1, length(GTcluIDs)); |
| 126 | +finalScore = zeros(1, length(GTcluIDs)); |
| 127 | +numMerges = zeros(1, length(GTcluIDs)); |
| 128 | +fprintf(1, '\n\n--Results Summary--\n') |
| 129 | +for cGT = 1:length(GTcluIDs) |
| 130 | +% |
| 131 | +% fprintf(1,'ground truth cluster ID = %d (%d spikes)\n', GTcluIDs(cGT), sum(cluGT==GTcluIDs(cGT))); |
| 132 | +% fprintf(1,' initial score: %.2f\n', allScores{cGT}(1)); |
| 133 | +% fprintf(1,' best score: %.2f (after %d merges)\n', allScores{cGT}(end), length(allScores{cGT})-1); |
| 134 | +% |
| 135 | + initScore(cGT) = allScores{cGT}(1); |
| 136 | + finalScore(cGT) = allScores{cGT}(end); |
| 137 | + numMerges(cGT) = length(allScores{cGT})-1; |
| 138 | +end |
| 139 | + |
| 140 | +fprintf(1, 'median initial score: %.2f; median best score: %.2f\n', median(initScore), median(finalScore)); |
| 141 | +fprintf(1, 'total merges required: %d\n', sum(numMerges)); |
0 commit comments