-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFilterDataArrayMaxCount.cs
500 lines (416 loc) · 20.3 KB
/
FilterDataArrayMaxCount.cs
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
using System;
namespace MASIC
{
/// <summary>
/// This class can be used to select the top N data points in a list, sorting descending
/// It does not require a full sort of the data, which allows for faster filtering of the data
/// </summary>
/// <remarks>
/// To use, first call AddDataPoint() for each source data point, specifying the value to sort on and a data point index
/// When done, call FilterData()
/// This method will determine which data points to retain
/// For the remaining points, their data values will be changed to SkipDataPointFlag (defaults to -1)
/// </remarks>
public class FilterDataArrayMaxCount
{
// Ignore Spelling: MASIC
private const int INITIAL_MEMORY_RESERVE = 50000;
private const float DEFAULT_SKIP_DATA_POINT_FLAG = -1;
// 4 steps in method FilterDataByMaxDataCountToLoad
private const int SUBTASK_STEP_COUNT = 4;
private double[] mDataValues = Array.Empty<double>();
private int[] mDataIndices = Array.Empty<int>();
/// <summary>
/// Progress changed event
/// </summary>
public event ProgressChangedEventHandler ProgressChanged;
/// <summary>
/// Delegate for the progress changed event
/// </summary>
/// <param name="progressVal"></param>
public delegate void ProgressChangedEventHandler(float progressVal);
/// <summary>
/// Number of data points tracked by this class
/// </summary>
public int DataCount { get; private set; }
/// <summary>
/// Maximum number of data points to retain
/// </summary>
public int MaximumDataCountToKeep { get; set; }
/// <summary>
/// Progress
/// </summary>
/// <remarks>Value between 0 and 100</remarks>
public float Progress { get; private set; }
/// <summary>
/// Flag to use to indicate data that should be skipped
/// </summary>
/// <remarks>
/// Defaults to -1
/// </remarks>
public float SkipDataPointFlag { get; set; }
/// <summary>
/// When true, the total intensity percentage filter is enabled
/// </summary>
[Obsolete("Not implemented")]
public bool TotalIntensityPercentageFilterEnabled { get; set; }
/// <summary>
/// Value to use when TotalIntensityPercentageFilterEnabled is true
/// </summary>
[Obsolete("Not implemented")]
public float TotalIntensityPercentageFilter { get; set; } = 90;
/// <summary>
/// Constructor
/// </summary>
public FilterDataArrayMaxCount()
: this(INITIAL_MEMORY_RESERVE)
{
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="initialCapacity"></param>
public FilterDataArrayMaxCount(int initialCapacity)
{
SkipDataPointFlag = DEFAULT_SKIP_DATA_POINT_FLAG;
Clear(initialCapacity);
}
/// <summary>
/// Add a data point
/// </summary>
/// <param name="abundance"></param>
/// <param name="dataPointIndex"></param>
public void AddDataPoint(double abundance, int dataPointIndex)
{
if (DataCount >= mDataValues.Length)
{
var oldMDataValues = mDataValues;
mDataValues = new double[(int)Math.Floor(mDataValues.Length * 1.5)];
Array.Copy(oldMDataValues, mDataValues, Math.Min((int)Math.Floor(mDataValues.Length * 1.5), oldMDataValues.Length));
var oldMDataIndices = mDataIndices;
mDataIndices = new int[mDataValues.Length];
Array.Copy(oldMDataIndices, mDataIndices, Math.Min(mDataValues.Length, oldMDataIndices.Length));
}
mDataValues[DataCount] = abundance;
mDataIndices[DataCount] = dataPointIndex;
DataCount++;
}
/// <summary>
/// Clear cached data
/// </summary>
/// <param name="initialCapacity"></param>
public void Clear(int initialCapacity)
{
MaximumDataCountToKeep = 400000;
#pragma warning disable 618
TotalIntensityPercentageFilterEnabled = false;
TotalIntensityPercentageFilter = 90;
#pragma warning restore 618
if (initialCapacity < 4)
{
initialCapacity = 4;
}
DataCount = 0;
mDataValues = new double[initialCapacity];
mDataIndices = new int[initialCapacity];
}
/// <summary>
/// Get the abundance value associated with the given data point
/// </summary>
/// <param name="dataPointIndex"></param>
public double GetAbundanceByIndex(int dataPointIndex)
{
if (dataPointIndex >= 0 && dataPointIndex < DataCount)
{
return mDataValues[dataPointIndex];
}
// Invalid data point index value
return -1;
}
/// <summary>
/// Filter the stored data to assure there is no more than MaximumDataCountToKeep data points
/// </summary>
public void FilterData()
{
if (DataCount <= 0)
{
// Nothing to do
}
else
{
// Shrink the arrays to DataCount
if (DataCount < mDataValues.Length)
{
var oldMDataValues = mDataValues;
mDataValues = new double[DataCount];
Array.Copy(oldMDataValues, mDataValues, Math.Min(DataCount, oldMDataValues.Length));
var oldMDataIndices = mDataIndices;
mDataIndices = new int[DataCount];
Array.Copy(oldMDataIndices, mDataIndices, Math.Min(DataCount, oldMDataIndices.Length));
}
FilterDataByMaxDataCountToKeep();
}
}
private void FilterDataByMaxDataCountToKeep()
{
const int HISTOGRAM_BIN_COUNT = 5000;
try
{
var binToSortAbundances = new double[10];
var binToSortDataIndices = new int[10];
UpdateProgress(0);
var useFullDataSort = false;
if (DataCount == 0)
{
// No data loaded
UpdateProgress((float)(4 / (double)SUBTASK_STEP_COUNT * 100.0D));
return;
}
if (DataCount <= MaximumDataCountToKeep)
{
// Loaded less than mMaximumDataCountToKeep data points
// Nothing to filter
UpdateProgress((float)(4 / (double)SUBTASK_STEP_COUNT * 100.0D));
return;
}
// In order to speed up the sorting, we're first going to make a histogram
// (aka frequency distribution) of the abundances in mDataValues
// First, determine the maximum abundance value in mDataValues
var maxAbundance = double.MinValue;
for (var index = 0; index < DataCount; index++)
{
if (mDataValues[index] > maxAbundance)
{
maxAbundance = mDataValues[index];
}
}
// Round maxAbundance up to the next highest integer
maxAbundance = (long)Math.Ceiling(maxAbundance);
// Now determine the histogram bin size
var binSize = maxAbundance / HISTOGRAM_BIN_COUNT;
if (binSize < 1)
binSize = 1;
// Initialize histogramData
var binCount = (int)(maxAbundance / binSize) + 1;
var histogramBinCounts = new int[binCount];
var histogramBinStartIntensity = new double[binCount];
for (var index = 0; index < binCount; index++)
{
histogramBinStartIntensity[index] = index * binSize;
}
// Parse mDataValues to populate histogramBinCounts
for (var index = 0; index < DataCount; index++)
{
int targetBin;
if (mDataValues[index] <= 0)
{
targetBin = 0;
}
else
{
targetBin = (int)(Math.Floor(mDataValues[index] / binSize));
}
if (targetBin < binCount - 1)
{
if (mDataValues[index] >= histogramBinStartIntensity[targetBin + 1])
{
targetBin++;
}
}
histogramBinCounts[targetBin]++;
if (mDataValues[index] < histogramBinStartIntensity[targetBin])
{
if (mDataValues[index] < histogramBinStartIntensity[targetBin] - binSize / 1000)
{
// This is unexpected
mDataValues[index] = mDataValues[index];
}
}
if (index % 10000 == 0)
{
UpdateProgress((float)((0 + (index + 1) / (double)DataCount) / SUBTASK_STEP_COUNT * 100.0));
}
}
// Now examine the frequencies in histogramBinCounts() to determine the minimum value to consider when sorting
var pointTotal = 0;
var binToSort = -1;
for (var index = binCount - 1; index >= 0; index += -1)
{
pointTotal += histogramBinCounts[index];
if (pointTotal >= MaximumDataCountToKeep)
{
binToSort = index;
break;
}
}
UpdateProgress((float)(1 / (double)SUBTASK_STEP_COUNT * 100.0D));
if (binToSort >= 0)
{
// Find the data with intensity >= histogramBinStartIntensity(binToSort)
// We actually only need to sort the data in bin binToSort
var binToSortAbundanceMinimum = histogramBinStartIntensity[binToSort];
var binToSortAbundanceMaximum = maxAbundance + 1;
if (binToSort < binCount - 1)
{
binToSortAbundanceMaximum = histogramBinStartIntensity[binToSort + 1];
}
if (Math.Abs(binToSortAbundanceMaximum - binToSortAbundanceMinimum) < float.Epsilon)
{
// Is this code ever reached?
// If yes, the code below won't populate binToSortAbundances() and binToSortDataIndices() with any data
useFullDataSort = true;
}
var binToSortDataCount = 0;
if (!useFullDataSort)
{
if (histogramBinCounts[binToSort] > 0)
{
binToSortAbundances = new double[histogramBinCounts[binToSort]];
binToSortDataIndices = new int[histogramBinCounts[binToSort]];
}
else
{
// Is this code ever reached?
useFullDataSort = true;
}
}
if (!useFullDataSort)
{
var dataCountImplicitlyIncluded = 0;
for (var index = 0; index < DataCount; index++)
{
if (mDataValues[index] < binToSortAbundanceMinimum)
{
// Skip this data point when re-reading the input data file
mDataValues[index] = SkipDataPointFlag;
}
else if (mDataValues[index] < binToSortAbundanceMaximum)
{
// Value is in the bin to sort; add to the BinToSort arrays
if (binToSortDataCount >= binToSortAbundances.Length)
{
// Need to reserve more space (this is unexpected)
var oldBinToSortAbundances = binToSortAbundances;
binToSortAbundances = new double[binToSortAbundances.Length * 2];
Array.Copy(oldBinToSortAbundances, binToSortAbundances, Math.Min(binToSortAbundances.Length * 2, oldBinToSortAbundances.Length));
var oldBinToSortDataIndices = binToSortDataIndices;
binToSortDataIndices = new int[binToSortAbundances.Length];
Array.Copy(oldBinToSortDataIndices, binToSortDataIndices, Math.Min(binToSortAbundances.Length, oldBinToSortDataIndices.Length));
}
binToSortAbundances[binToSortDataCount] = mDataValues[index];
binToSortDataIndices[binToSortDataCount] = mDataIndices[index];
binToSortDataCount++;
}
else
{
dataCountImplicitlyIncluded++;
}
if (index % 10000 == 0)
{
UpdateProgress((float)((1 + (index + 1) / (double)DataCount) / SUBTASK_STEP_COUNT * 100.0D));
}
}
if (binToSortDataCount > 0)
{
if (binToSortDataCount < binToSortAbundances.Length)
{
var oldBinToSortAbundances1 = binToSortAbundances;
binToSortAbundances = new double[binToSortDataCount];
Array.Copy(oldBinToSortAbundances1, binToSortAbundances, Math.Min(binToSortDataCount, oldBinToSortAbundances1.Length));
var oldBinToSortDataIndices1 = binToSortDataIndices;
binToSortDataIndices = new int[binToSortDataCount];
Array.Copy(oldBinToSortDataIndices1, binToSortDataIndices, Math.Min(binToSortDataCount, oldBinToSortDataIndices1.Length));
}
}
else
{
// This code shouldn't be reached
}
if (MaximumDataCountToKeep - dataCountImplicitlyIncluded == binToSortDataCount)
{
// No need to sort and examine the data for BinToSort since we'll ultimately include all of it
}
else
{
SortAndMarkPointsToSkip(binToSortAbundances, binToSortDataIndices, binToSortDataCount, MaximumDataCountToKeep - dataCountImplicitlyIncluded, SUBTASK_STEP_COUNT);
}
// Synchronize the data in binToSortAbundances and binToSortDataIndices with mDataValues and mDataIndices
// mDataValues and mDataIndices have not been sorted and therefore mDataIndices should currently be sorted ascending on "valid data point index"
// binToSortDataIndices should also currently be sorted ascending on "valid data point index" so the following Do Loop within a For Loop should sync things up
var originalDataArrayIndex = 0;
for (var index = 0; index < binToSortDataCount; index++)
{
while (binToSortDataIndices[index] > mDataIndices[originalDataArrayIndex])
{
originalDataArrayIndex++;
}
if (Math.Abs(binToSortAbundances[index] - SkipDataPointFlag) < float.Epsilon)
{
if (mDataIndices[originalDataArrayIndex] == binToSortDataIndices[index])
{
mDataValues[originalDataArrayIndex] = SkipDataPointFlag;
}
else
{
// This code shouldn't be reached
}
}
originalDataArrayIndex++;
if (binToSortDataCount < 1000 || binToSortDataCount % 100 == 0)
{
UpdateProgress((float)((3 + (index + 1) / (double)binToSortDataCount) / SUBTASK_STEP_COUNT * 100.0));
}
}
}
}
else
{
useFullDataSort = true;
}
if (useFullDataSort)
{
// This shouldn't normally be necessary
// We have to sort every data value; this can be quite slow
SortAndMarkPointsToSkip(mDataValues, mDataIndices, DataCount, MaximumDataCountToKeep, SUBTASK_STEP_COUNT);
}
UpdateProgress((float)(4 / (double)SUBTASK_STEP_COUNT * 100.0D));
}
catch (Exception ex)
{
throw new Exception("Error in FilterDataByMaxDataCountToKeep: " + ex.Message, ex);
}
}
/// <summary>
/// This method uses a full sort to filter the data
/// </summary>
/// <remarks>Sorting will be slow for large arrays, and you should therefore use FilterDataByMaxDataCountToKeep if possible</remarks>
/// <param name="abundances"></param>
/// <param name="dataIndices"></param>
/// <param name="dataCount"></param>
/// <param name="maximumDataCountInArraysToLoad"></param>
/// <param name="subtaskStepCount"></param>
private void SortAndMarkPointsToSkip(double[] abundances, int[] dataIndices, int dataCount, int maximumDataCountInArraysToLoad, int subtaskStepCount)
{
if (dataCount > 0)
{
// Sort abundances ascending, sorting dataIndices in parallel
Array.Sort(abundances, dataIndices, 0, dataCount);
UpdateProgress((float)(2.333 / subtaskStepCount * 100.0));
// Change the abundance values to SkipDataPointFlag for data up to index dataCount-maximumDataCountInArraysToLoad-1
for (var index = 0; index < dataCount - maximumDataCountInArraysToLoad; index++)
{
abundances[index] = SkipDataPointFlag;
}
UpdateProgress((float)(2.666 / subtaskStepCount * 100.0D));
// Re-sort, this time on dataIndices with abundances in parallel
Array.Sort(dataIndices, abundances, 0, dataCount);
}
UpdateProgress((float)(3 / (double)subtaskStepCount * 100.0D));
}
private void UpdateProgress(float progressValue)
{
Progress = progressValue;
ProgressChanged?.Invoke(Progress);
}
}
}