File size: 9,682 Bytes
ffdff5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Adaptive Sampling System for Large Datasets
// Inspired by Weights & Biases approach to handle massive time series

/**
 * Adaptive Sampler - Intelligently reduces data points while preserving visual fidelity
 */
export class AdaptiveSampler {
  constructor(options = {}) {
    this.options = {
      maxPoints: 400,           // Threshold to trigger sampling
      targetPoints: 200,        // Target number of points after sampling
      preserveFeatures: true,   // Preserve important peaks/valleys
      adaptiveStrategy: 'smart', // 'uniform', 'smart', 'lod'
      smoothingWindow: 3,       // Window for feature detection
      ...options
    };
  }

  /**
   * Determine if sampling is necessary
   */
  needsSampling(dataLength) {
    return dataLength > this.options.maxPoints;
  }

  /**
   * Main entry point for sampling
   */
  sampleSeries(data, strategy = null) {
    if (!Array.isArray(data) || data.length === 0) {
      return { data: [], sampledIndices: [], compressionRatio: 1 };
    }

    const actualStrategy = strategy || this.options.adaptiveStrategy;

    if (!this.needsSampling(data.length)) {
      return {
        data: data.slice(),
        sampledIndices: data.map((_, i) => i),
        compressionRatio: 1,
        strategy: 'none'
      };
    }

    console.log(`🎯 Sampling ${data.length} points with strategy: ${actualStrategy}`);

    switch (actualStrategy) {
      case 'uniform':
        return this.uniformSampling(data);
      case 'smart':
        return this.smartSampling(data);
      case 'lod':
        return this.lodSampling(data);
      default:
        return this.smartSampling(data);
    }
  }

  /**
   * Sampling uniforme - simple mais pas optimal
   */
  uniformSampling(data) {
    const step = Math.ceil(data.length / this.options.targetPoints);
    const sampledData = [];
    const sampledIndices = [];

    // Toujours inclure le premier et dernier point
    sampledData.push(data[0]);
    sampledIndices.push(0);

    for (let i = step; i < data.length - 1; i += step) {
      sampledData.push(data[i]);
      sampledIndices.push(i);
    }

    // Toujours inclure le dernier point
    if (data.length > 1) {
      sampledData.push(data[data.length - 1]);
      sampledIndices.push(data.length - 1);
    }

    return {
      data: sampledData,
      sampledIndices,
      compressionRatio: sampledData.length / data.length,
      strategy: 'uniform'
    };
  }

  /**
   * Smart sampling - preserves important features
   * Inspired by Douglas-Peucker algorithm adapted for time series
   */
  smartSampling(data) {
    const targetPoints = this.options.targetPoints;
    const features = this.detectFeatures(data);

    // Step 1: Critical points (start, end, important features)
    const criticalPoints = new Set([0, data.length - 1]);

    // Add detected features
    features.peaks.forEach(idx => criticalPoints.add(idx));
    features.valleys.forEach(idx => criticalPoints.add(idx));
    features.inflectionPoints.forEach(idx => criticalPoints.add(idx));

    // Step 2: Logarithmic distribution to preserve density
    const remaining = targetPoints - criticalPoints.size;
    if (remaining > 0) {
      const logSamples = this.generateLogSpacing(data.length, remaining);
      logSamples.forEach(idx => criticalPoints.add(idx));
    }

    // Step 3: Adaptive density in zones of change
    if (criticalPoints.size < targetPoints) {
      const variationSamples = this.sampleByVariation(data, targetPoints - criticalPoints.size);
      variationSamples.forEach(idx => criticalPoints.add(idx));
    }

    const sampledIndices = Array.from(criticalPoints).sort((a, b) => a - b);
    const sampledData = sampledIndices.map(idx => data[idx]);

    return {
      data: sampledData,
      sampledIndices,
      compressionRatio: sampledData.length / data.length,
      strategy: 'smart',
      features
    };
  }

  /**
   * Level-of-Detail sampling - adaptive based on zoom/context
   */
  lodSampling(data, viewportStart = 0, viewportEnd = 1, zoomLevel = 1) {
    const viewStart = Math.floor(viewportStart * data.length);
    const viewEnd = Math.ceil(viewportEnd * data.length);
    const viewData = data.slice(viewStart, viewEnd);

    // More detail in the visible area
    const visibleTargetPoints = Math.floor(this.options.targetPoints * 0.7);
    const contextTargetPoints = this.options.targetPoints - visibleTargetPoints;

    // Dense sampling in the visible area
    const visibleSample = this.smartSampling(viewData);

    // Sparse sampling in the context
    const beforeContext = data.slice(0, viewStart);
    const afterContext = data.slice(viewEnd);

    const beforeSample = beforeContext.length > 0 ?
      this.uniformSampling(beforeContext) : { data: [], sampledIndices: [] };
    const afterSample = afterContext.length > 0 ?
      this.uniformSampling(afterContext) : { data: [], sampledIndices: [] };

    // Combine results
    const combinedData = [
      ...beforeSample.data,
      ...visibleSample.data,
      ...afterSample.data
    ];

    const combinedIndices = [
      ...beforeSample.sampledIndices,
      ...visibleSample.sampledIndices.map(idx => idx + viewStart),
      ...afterSample.sampledIndices.map(idx => idx + viewEnd)
    ];

    return {
      data: combinedData,
      sampledIndices: combinedIndices,
      compressionRatio: combinedData.length / data.length,
      strategy: 'lod'
    };
  }

  /**
   * Detect important features in the series
   */
  detectFeatures(data) {
    const peaks = [];
    const valleys = [];
    const inflectionPoints = [];
    const window = this.options.smoothingWindow;

    for (let i = window; i < data.length - window; i++) {
      const current = data[i].value;
      const prev = data[i - 1].value;
      const next = data[i + 1].value;

      // Detect local peaks
      if (current > prev && current > next) {
        // Check if it's a significant peak
        const localMax = Math.max(
          ...data.slice(i - window, i + window + 1).map(d => d.value)
        );
        if (current === localMax) {
          peaks.push(i);
        }
      }

      // Detect local valleys
      if (current < prev && current < next) {
        const localMin = Math.min(
          ...data.slice(i - window, i + window + 1).map(d => d.value)
        );
        if (current === localMin) {
          valleys.push(i);
        }
      }

      // Detect inflection points (curvature change)
      if (i >= 2 && i < data.length - 2) {
        const trend1 = data[i].value - data[i - 2].value;
        const trend2 = data[i + 2].value - data[i].value;

        if (Math.sign(trend1) !== Math.sign(trend2) && Math.abs(trend1) > 0.01 && Math.abs(trend2) > 0.01) {
          inflectionPoints.push(i);
        }
      }
    }

    return { peaks, valleys, inflectionPoints };
  }

  /**
   * Generate indices with logarithmic spacing
   */
  generateLogSpacing(totalLength, count) {
    const indices = [];
    for (let i = 1; i <= count; i++) {
      const progress = i / (count + 1);
      // Logarithmic function for more density at the beginning
      const logProgress = Math.log(1 + progress * (Math.E - 1)) / Math.log(Math.E);
      const index = Math.floor(logProgress * (totalLength - 1));
      indices.push(Math.max(1, Math.min(totalLength - 2, index)));
    }
    return [...new Set(indices)]; // Remove duplicates
  }

  /**
   * Sampling based on local variation
   */
  sampleByVariation(data, targetPoints) {
    const variations = [];

    // Calculate local variation for each point
    for (let i = 1; i < data.length - 1; i++) {
      const prev = data[i - 1].value;
      const curr = data[i].value;
      const next = data[i + 1].value;

      // Variation = difference from the average of neighbors
      const avgNeighbors = (prev + next) / 2;
      const variation = Math.abs(curr - avgNeighbors);

      variations.push({ index: i, variation });
    }

    // Sort by decreasing variation and take the most important ones
    variations.sort((a, b) => b.variation - a.variation);

    return variations.slice(0, targetPoints).map(v => v.index);
  }

  /**
   * Apply sampling on a complete data object (multi-run)
   */
  sampleMetricData(metricData, strategy = null) {
    const sampledData = {};
    const samplingInfo = {};

    Object.keys(metricData).forEach(runName => {
      const runData = metricData[runName] || [];
      const result = this.sampleSeries(runData, strategy);

      sampledData[runName] = result.data;
      samplingInfo[runName] = {
        originalLength: runData.length,
        sampledLength: result.data.length,
        compressionRatio: result.compressionRatio,
        strategy: result.strategy,
        sampledIndices: result.sampledIndices
      };
    });

    return { sampledData, samplingInfo };
  }

  /**
   * Rebuild full data for a specific range (for zoom)
   */
  getFullDataForRange(originalData, samplingInfo, startStep, endStep) {
    // This method would allow recovering more details
    // when the user zooms on a specific area
    const startIdx = originalData.findIndex(d => d.step >= startStep);
    const endIdx = originalData.findIndex(d => d.step > endStep);

    return originalData.slice(startIdx, endIdx === -1 ? undefined : endIdx);
  }
}

/**
 * Global instance configured for TrackIO
 */
export const trackioSampler = new AdaptiveSampler({
  maxPoints: 400,
  targetPoints: 200,
  preserveFeatures: true,
  adaptiveStrategy: 'smart'
});

/**
 * Utility function for direct usage
 */
export function sampleLargeDataset(metricData, options = {}) {
  const sampler = new AdaptiveSampler(options);
  return sampler.sampleMetricData(metricData);
}