forked from ruvnet/RuView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_pose.js
More file actions
118 lines (105 loc) · 3.62 KB
/
Copy pathbenchmark_pose.js
File metadata and controls
118 lines (105 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const M = 20000;
const N = 20000;
const labeledTimeline = Array.from({length: M}, (_, i) => ({
nodeId: i % 10,
timestamp: i * 0.1,
labels: { poseProxy5: [1, 2, 3], confidence: 0.9 }
}));
// Shuffle labeledTimeline
for (let i = labeledTimeline.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[labeledTimeline[i], labeledTimeline[j]] = [labeledTimeline[j], labeledTimeline[i]];
}
const encodedFeatures = Array.from({length: N}, (_, i) => ({
nodeId: i % 10,
timestamp: i * 0.1 + 0.05,
embedding: [0, 0, 0]
}));
console.time('Baseline');
const poseTrainData1 = [];
for (const ef of encodedFeatures) {
const tlFrame = labeledTimeline.find(f =>
f.nodeId === ef.nodeId && Math.abs(f.timestamp - ef.timestamp) < 0.1
);
if (tlFrame && tlFrame.labels && tlFrame.labels.poseProxy5) {
poseTrainData1.push({
embedding: ef.embedding,
target: tlFrame.labels.poseProxy5,
confidence: tlFrame.labels.confidence,
});
}
}
console.timeEnd('Baseline');
console.time('Optimized (Node + Sort + Binary Search)');
const nodeFramesSorted = new Map();
for (let i = 0; i < labeledTimeline.length; i++) {
const f = labeledTimeline[i];
if (!nodeFramesSorted.has(f.nodeId)) nodeFramesSorted.set(f.nodeId, []);
// Keep track of original index to preserve `find()` semantics of returning the *first* matching element in the original array
nodeFramesSorted.get(f.nodeId).push({ f, index: i });
}
for (const frames of nodeFramesSorted.values()) {
frames.sort((a, b) => a.f.timestamp - b.f.timestamp);
}
const poseTrainData2 = [];
for (const ef of encodedFeatures) {
const frames = nodeFramesSorted.get(ef.nodeId);
let bestMatch = null;
if (frames) {
let left = 0;
let right = frames.length - 1;
// Binary search for the closest timestamp
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (frames[mid].f.timestamp < ef.timestamp) {
left = mid + 1;
} else {
right = mid - 1;
}
}
// left is now the insertion point. The closest could be left or left - 1.
// However, since we want *any* within 0.1, we could just scan around the insertion point.
// But remember we need the *first* match from the original array (lowest original index)
// to strictly preserve `.find()` semantics.
// Find all frames within 0.1
let minIndexMatch = null;
let scanIdx = left;
while (scanIdx < frames.length && frames[scanIdx].f.timestamp - ef.timestamp < 0.1) {
if (!minIndexMatch || frames[scanIdx].index < minIndexMatch.index) {
minIndexMatch = frames[scanIdx];
}
scanIdx++;
}
scanIdx = left - 1;
while (scanIdx >= 0 && ef.timestamp - frames[scanIdx].f.timestamp < 0.1) {
if (!minIndexMatch || frames[scanIdx].index < minIndexMatch.index) {
minIndexMatch = frames[scanIdx];
}
scanIdx--;
}
if (minIndexMatch) {
bestMatch = minIndexMatch.f;
}
}
if (bestMatch && bestMatch.labels && bestMatch.labels.poseProxy5) {
poseTrainData2.push({
embedding: ef.embedding,
target: bestMatch.labels.poseProxy5,
confidence: bestMatch.labels.confidence,
});
}
}
console.timeEnd('Optimized (Node + Sort + Binary Search)');
// Check equivalence
let same = true;
if (poseTrainData1.length !== poseTrainData2.length) {
same = false;
console.log(`Length mismatch: ${poseTrainData1.length} vs ${poseTrainData2.length}`);
} else {
for (let i = 0; i < poseTrainData1.length; i++) {
if (poseTrainData1[i].target !== poseTrainData2[i].target) {
same = false;
}
}
}
console.log('Results are same:', same);