-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathES_Extensions.cs
385 lines (310 loc) · 12.8 KB
/
ES_Extensions.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
using System.Collections.Generic;
using System.Linq;
using System;
using System.Security.Cryptography;
using System.Text;
using Accord.Collections;
using Accord.Statistics.Distributions.Multivariate;
using Accord;
using System.ComponentModel;
using Accord.Math;
using Microsoft.Office.Interop.Excel;
namespace GINtool
{
using stat_dict = System.Collections.Generic.Dictionary<string, double>;
using rank_dict = System.Collections.Generic.Dictionary<string, int>;
using dict_rank = System.Collections.Generic.Dictionary<int, string>;
using lib_dict = System.Collections.Generic.Dictionary<string, string[]>;
using LoessFunc = System.Func<int, double>;
public static class ES_Extensions
{
// from https://stackoverflow.com/questions/4823467/using-linq-to-find-the-cumulative-sum-of-an-array-of-numbers-in-c-sharp
public static IEnumerable<double> CumulativeSum(this IEnumerable<double> sequence)
{
double sum = 0;
foreach (var item in sequence)
{
sum += item;
yield return sum;
}
}
// https://stackoverflow.com/questions/2733541/what-is-the-best-way-to-implement-this-composite-gethashcode
public static int GetHashCodeValue(this IEnumerable<string> ar)
{
ar = ar.OrderBy(x => x);
int hash = 3;
unchecked
{
foreach (string s in ar)
{
hash = hash * 5 + s.GetHashCode();
}
// Maybe nullity checks, if these are objects not primitives!
}
return hash;
}
public static int GetHashCodeValue(this IEnumerable<double> ar)
{
ar = ar.OrderBy(x => x);
int hash = 3;
unchecked
{
foreach (double d in ar)
{
hash = hash * 5 + d.GetHashCode();
}
// Maybe nullity checks, if these are objects not primitives!
}
return hash;
}
public static IEnumerable<double> CumulativeMin(this IEnumerable<double> sequence)
{
double m = sequence.First();
foreach (var item in sequence)
{
if (item < m)
m = item;
yield return m;
}
}
public static IEnumerable<int> CumulativeSum(this IEnumerable<int> sequence)
{
int sum = 0;
foreach (var item in sequence)
{
sum += item;
yield return sum;
}
}
public static rank_dict RankMap(this stat_dict lib)
{
return (rank_dict)ParallelEnumerable.Range(0, lib.Count()).Select(i => i).ToDictionary(i => lib.ElementAt(i).Key, i => i);
}
public static dict_rank MapRank(this stat_dict lib)
{
return (dict_rank)ParallelEnumerable.Range(0, lib.Count()).Select(i => i).ToDictionary(i => i, i => lib.ElementAt(i).Key);
}
public static IEnumerable<rank_map> RankMap(this stat_map[] sequence)
{
// sequence.ToList().Sort((x, y) => x.Stat.CompareTo(y.Stat));
sequence = sequence.OrderBy(p => p.Stat).Reverse().ToArray();
int order = 0;
foreach (var item in sequence)
{
yield return new rank_map(order++, item.Label);
}
}
public static string[] subset(this stat_map[] s, int[] y)
{
return ParallelEnumerable.Range(0, y.Length).Select(i => s[y[i]].Label).ToArray();
}
public static double[] Plus(this double[] x, double[] y)
{
return ParallelEnumerable.Range(0, y.Length).Select(i => x[i] + y[i]).ToArray();
}
public static string[] subset(this stat_dict s, int[] y)
{
return ParallelEnumerable.Range(0, y.Length).Select(i => s.ElementAt(y[i]).Key).ToArray();
}
public static string[] subset2(this stat_dict s, string[] y)
{
return s.Keys.Intersect(y).ToArray();
//return ParallelEnumerable.Range(0, matches.Count()).Select(i => s[matches[i]]).ToArray();
}
public static IEnumerable<double> Range(double min, double max, int nrpoints)
{
double step = (max - min) / nrpoints;
double result = min;
for (int i = 0; result < max; i++)
{
result = min + (step * i);
yield return result;
}
}
// https://www.statsmodels.org/stable/_modules/statsmodels/stats/multitest.html
public static double Log1p(double x)
{
return Math.Log(x + 1);
}
public static double Exp1m(double x)
{
return Math.Exp(x) - 1;
}
public static double[] sidak_correction(double[] pvals)
{
/* reject = pvals <= alphacSidak
pvals_corrected = -np.expm1(ntests * np.log1p(-pvals))
*/
int ntests = pvals.Length;
return pvals.Select(x => -Exp1m(ntests * Log1p(-x))).ToArray();
}
public static double[] ecdf(int n)
{
// assume sorted array
return Enumerable.Range(1, n).Select(x => (double)x / (double)n).ToArray();
// ys = np.cumsum(1 for _ in x)/ float(len(x))
}
public static double[] fdr_correction(double[] pvals, double alpha = 0.05)
{
// https://stackoverflow.com/questions/10443461/c-sharp-array-findallindexof-which-findall-indexof
List<int> _missings = Enumerable.Range(0, pvals.Length).Where(i => double.IsNaN(pvals[i])).ToList();
//pvals = pvals.Where(x => !double.IsNaN(x)).ToArray();
var sortedElements = pvals.Select((x, i) => new KeyValuePair<double, int>(x, i)).OrderBy(x => x.Key).ToArray();
List<int> sortedIndex = sortedElements.Select(x => x.Value).ToList();
double[] pvals_sorted = Enumerable.Range(0, sortedIndex.Count).Select(i => pvals[sortedIndex[i]]).ToArray();
// double[] pvals_sorted = pvals.OrderBy(x => x).ToArray();
double[] ecdffactor = ecdf(pvals_sorted.Length);
double[] pvals_corrected_raw = Enumerable.Range(0, pvals.Length).Select(i => pvals_sorted[i] / ecdffactor[i]).ToArray();
double[] pvals_corrected = pvals_corrected_raw.Reverse().CumulativeMin().Reverse().ToArray();
for (int i = 0; i < pvals_corrected.Length; i++)
{
if (pvals_corrected[i] > 1)
pvals_corrected[i] = 1;
}
pvals_corrected_raw = Enumerable.Repeat(0.0, pvals_corrected.Length).ToArray();
for (int i = 0; i < pvals_corrected.Length; i++)
{
pvals_corrected_raw[sortedIndex[i]] = pvals_corrected[i];
}
foreach (int i in _missings)
pvals_corrected_raw[i] = double.NaN;
return pvals_corrected_raw;
}
public static double percentile(int[] sortedData, double p)
{
return (percentile(sortedData.Select(x => Convert.ToDouble(x)).ToArray(), p));
}
public static double percentile(double[] sortedData, double p)
{
// algo derived from Aczel pg 15 bottom
if (p >= 100.0d) return sortedData[sortedData.Length - 1];
double position = (double)(sortedData.Length + 1) * p / 100.0;
double leftNumber = 0.0d, rightNumber = 0.0d;
double n = p / 100.0d * (sortedData.Length - 1) + 1.0d;
if (position >= 1)
{
leftNumber = sortedData[(int)System.Math.Floor(n) - 1];
rightNumber = sortedData[(int)System.Math.Floor(n)];
}
else
{
leftNumber = sortedData[0]; // first data
rightNumber = sortedData[1]; // first data
}
if (leftNumber == rightNumber)
return leftNumber;
else
{
double part = n - System.Math.Floor(n);
return leftNumber + part * (rightNumber - leftNumber);
}
}
public static IEnumerable<int> toint(this double[] array)
{
return ParallelEnumerable.Range(0, array.Length).Select(pp => Convert.ToInt32(array[pp]));
}
public static IEnumerable<double> percentiles(int[] array, double[] p)
{
return ParallelEnumerable.Range(0, p.Length).Select(pp => percentile(array, p[pp]));
}
public static IEnumerable<double> percentiles(double[] array, double[] p)
{
return ParallelEnumerable.Range(0, p.Length).Select(pp => percentile(array, p[pp]));
}
public static string[] keys(this libitem[] s)
{
return ParallelEnumerable.Range(0, s.Length).Select(i => s[i].Label).ToArray();
}
public static int[] lengths(this libitem[] s)
{
return ParallelEnumerable.Range(0, s.Length).Select(i => s[i].Items.Length).ToArray();
}
public static int[] lengths(this lib_dict s)
{
return s.AsParallel().Select(x => x.Value.Length).ToArray();
}
public static double[] AbsStats(this stat_map[] stats)
{
return stats.Select(st => Math.Abs(st.Stat)).ToArray();
}
public static double[] AbsStats(this stat_dict stats)
{
return stats.Select(k => Math.Abs(k.Value)).ToArray();
}
public static double Sum(this double[] arr, int[] index)
{
double sum = 0;
foreach (int i in index)
sum += arr[i];
return sum;
}
public static byte[] Hashvalue(Object x)
{
var tmpSource = ASCIIEncoding.ASCII.GetBytes(x.ToString());
return new MD5CryptoServiceProvider().ComputeHash(tmpSource);
}
#region Linq operators
// from https://stackoverflow.com/questions/59012299/multiplying-arrays-element-wise-has-unexpected-performance-in-c-sharp
public static int[] Ranks(rank_map[] signatures)
{
return signatures.Select(signature => signature.Rank).ToArray();
}
public static double[] Pmult(double[] x, double[] y)
{
return ParallelEnumerable.Range(0, x.Length).Select(i => x[i] * y[i]).ToArray();
}
public static double[] Pmult(double[] x, int[] y)
{
return ParallelEnumerable.Range(0, x.Length).Select(i => x[i] * y[i]).ToArray();
}
public static int[] Pmult(rank_map[] x, int[] y)
{
return ParallelEnumerable.Range(0, y.Length).Select(i => x[i] * y[i]).ToArray();
}
public static int[] Pmult(int[] x, int[] y)
{
return ParallelEnumerable.Range(0, y.Length).Select(i => x[i] * y[i]).ToArray();
}
public static double[] Pmult(int[] y, double x)
{
return ParallelEnumerable.Range(0, y.Length).Select(i => x * (double)y[i]).ToArray();
}
public static double[] Pmult(double[] y, double x)
{
return ParallelEnumerable.Range(0, y.Length).Select(i => x * y[i]).ToArray();
}
public static double[] Pmult(double[] y, int x)
{
return ParallelEnumerable.Range(0, y.Length).Select(i => x * y[i]).ToArray();
}
public static double[] Pmin(double[] y, int[] x)
{
return ParallelEnumerable.Range(0, y.Length).Select(i => y[i] - (double)x[i]).ToArray();
}
public static double[] Pmin(double[] y, double[] x)
{
return ParallelEnumerable.Range(0, y.Length).Select(i => y[i] - x[i]).ToArray();
}
public static double[] Pabs(this double[] y)
{
return ParallelEnumerable.Range(0, y.Length).Select(i => Math.Abs(y[i])).ToArray();
}
//public static double[] Pabs(double[] y)
//{
// return ParallelEnumerable.Range(0, y.Length).Select(i => Math.Abs(y[i])).ToArray();
//}
public static string[] subset(this string[] s, int[] y)
{
return ParallelEnumerable.Range(0, y.Length).Select(i => s[y[i]]).ToArray();
}
//public static List<string> strip_gene_set(List<string> signature_genes, List<string> gene_set)
//{
// return gene_set.Intersect(signature_genes).ToList();
//}
public static string Join(this stat_map[] signatures, int[] subset)
{
return string.Join(",", ParallelEnumerable.Range(0, subset.Length).Select(i => signatures[subset[i]].Label).ToArray());
}
#endregion
};
}