1 package com.github.celldynamics.quimp.utils;
2
3 import java.io.File;
4 import java.io.FileReader;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.io.LineNumberReader;
8 import java.io.PrintWriter;
9 import java.lang.reflect.Array;
10 import java.nio.BufferOverflowException;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.StringTokenizer;
18 import java.util.stream.IntStream;
19
20 import org.apache.commons.lang3.ArrayUtils;
21 import org.apache.commons.math3.linear.Array2DRowRealMatrix;
22 import org.apache.commons.math3.linear.RealMatrix;
23 import org.apache.commons.math3.stat.StatUtils;
24
25 import ij.process.FloatProcessor;
26 import ij.process.ImageProcessor;
27
28
29
30
31
32
33
34 public class QuimPArrayUtils {
35
36
37
38
39
40
41
42
43 public static double[] float2double(float[] input) {
44 final double[] result = new double[input.length];
45 IntStream.range(0, input.length).forEach(index -> result[index] = input[index]);
46 return result;
47 }
48
49
50
51
52
53
54
55 public static float[] double2float(double[] input) {
56 final float[] result = new float[input.length];
57 IntStream.range(0, input.length).forEach(index -> result[index] = (float) input[index]);
58 return result;
59 }
60
61
62
63
64
65
66
67 public static Double[] object2double(Number[] input) {
68 final Double[] result = new Double[input.length];
69 IntStream.range(0, input.length).forEach(index -> result[index] = input[index].doubleValue());
70 return result;
71 }
72
73
74
75
76
77
78
79 public static double[][] float2ddouble(float[][] input) {
80 if (input == null) {
81 return null;
82 }
83 int rows = input.length;
84 double[][] out = new double[rows][];
85 IntStream.range(0, input.length).forEach(index -> out[index] = float2double(input[index]));
86 return out;
87 }
88
89
90
91
92
93
94
95 public static float[][] double2dfloat(double[][] input) {
96 if (input == null) {
97 return null;
98 }
99 int rows = input.length;
100 float[][] out = new float[rows][];
101 IntStream.range(0, input.length).forEach(index -> out[index] = double2float(input[index]));
102 return out;
103 }
104
105
106
107
108
109
110
111 public static double[] getMeanR(double[][] map) {
112 double[] ret = new double[map.length];
113 IntStream.range(0, map.length)
114 .forEach(index -> ret[index] = Arrays.stream(map[index]).sum() / map[index].length);
115 return ret;
116 }
117
118
119
120
121
122
123
124 public static double[] getMeanC(double[][] map) {
125 double[] ret = new double[map[0].length];
126 for (int r = 0; r < map[0].length; r++) {
127 double mean = 0;
128 for (int f = 0; f < map.length; f++) {
129 mean += map[f][r];
130 }
131 ret[r] = mean / map.length;
132 }
133 return ret;
134 }
135
136
137
138
139
140
141
142 public static double[] getVarR(double[][] map) {
143 double[] ret = new double[map.length];
144 double[] means = getMeanR(map);
145 for (int f = 0; f < map.length; f++) {
146 double var = 0;
147 for (int r = 0; r < map[f].length; r++) {
148 var += Math.pow(means[f] - map[f][r], 2.0);
149 }
150 ret[f] = var / map[f].length;
151 }
152 return ret;
153 }
154
155
156
157
158
159
160
161 public static <T> List<T> removeDuplicates(List<T> in) {
162 Set<T> retNoDpl = new HashSet<>();
163 retNoDpl.addAll(in);
164 ArrayList<T> retP2i = new ArrayList<>();
165 retP2i.addAll(retNoDpl);
166 return retP2i;
167 }
168
169
170
171
172
173
174 public static void reverseIntArray(int[] input) {
175
176 if (input == null || input.length <= 1) {
177 return;
178 }
179
180 for (int i = 0; i < input.length / 2; i++) {
181 int temp = input[i];
182 input[i] = input[input.length - 1 - i];
183 input[input.length - 1 - i] = temp;
184 }
185 }
186
187
188
189
190
191
192
193 public static short[] int2short(int[] input) {
194 short[] ret = new short[input.length];
195 for (int i = 0; i < input.length; i++) {
196 if (input[i] > Short.MAX_VALUE) {
197 throw new BufferOverflowException();
198 }
199 ret[i] = (short) input[i];
200 }
201 return ret;
202 }
203
204
205
206
207
208
209
210
211 public static double[][] initDouble2dArray(int rows, int cols) {
212 double[][] ret;
213 ret = new double[rows][];
214 for (int r = 0; r < rows; r++) {
215 ret[r] = new double[cols];
216 }
217 return ret;
218 }
219
220
221
222
223
224
225
226
227 public static int[][] initInteger2dArray(int rows, int cols) {
228 int[][] ret;
229 ret = new int[rows][];
230 for (int r = 0; r < rows; r++) {
231 ret[r] = new int[cols];
232 }
233 return ret;
234 }
235
236
237
238
239
240
241
242
243
244
245
246 public static double[][] copy2darray(double[][] source, double[][] dest) {
247 double[][] ret;
248 int rows = source.length;
249 int cols = source[0].length;
250 if (dest == null) {
251 ret = initDouble2dArray(rows, cols);
252 } else {
253 ret = dest;
254 }
255 for (int r = 0; r < source.length; r++) {
256 System.arraycopy(source[r], 0, ret[r], 0, source[r].length);
257 }
258 return ret;
259 }
260
261
262
263
264
265
266
267
268
269
270
271
272 public static void arrayToFile(double[][] a, String delim, File outFile) throws IOException {
273 PrintWriter pw = new PrintWriter(new FileWriter(outFile), true);
274 for (int i = 0; i < a.length; i++) {
275 if (i != 0) {
276 pw.write("\n");
277 }
278 pw.write(a[i][0] + "");
279 for (int j = 1; j < a[0].length; j++) {
280 pw.write(delim + a[i][j]);
281 }
282 }
283 pw.close();
284 }
285
286
287
288
289
290
291
292
293
294
295
296 public static void realMatrix2D2File(RealMatrix matrix, String outFile) throws IOException {
297 if (matrix instanceof Array2DRowRealMatrix) {
298 double[][] ref = ((Array2DRowRealMatrix) matrix).getDataRef();
299 arrayToFile(ref, ",", new File(outFile));
300 } else {
301 throw new IllegalArgumentException("Input matrix should be instance of Array2DRowRealMatrix");
302 }
303 }
304
305
306
307
308
309
310
311
312
313
314
315
316 public static double[][] file2Array(String delim, File inFile) throws IOException {
317 LineNumberReader pw = new LineNumberReader(new FileReader(inFile));
318 int lines = getNumberOfLinesinFile(inFile);
319 double[][] ret = new double[lines][];
320 String line = pw.readLine();
321 while (line != null) {
322 StringTokenizer tk = new StringTokenizer(line, delim);
323 ret[pw.getLineNumber() - 1] = new double[tk.countTokens()];
324 int colno = 0;
325 while (tk.hasMoreTokens()) {
326 ret[pw.getLineNumber() - 1][colno++] = Double.valueOf(tk.nextToken());
327 }
328 line = pw.readLine();
329 }
330 pw.close();
331 return ret;
332 }
333
334
335
336
337
338
339
340
341 public static int getNumberOfLinesinFile(File file) throws IOException {
342 LineNumberReader lnr = new LineNumberReader(new FileReader(file));
343 lnr.skip(Long.MAX_VALUE);
344 int lines = lnr.getLineNumber() + 1;
345 lnr.close();
346 return lines;
347 }
348
349
350
351
352
353
354
355
356 public static int findNumPeaks(double[] data, int peakWidth) {
357
358 int[] peaks = new int[data.length];
359 int flag;
360
361
362 for (int i = peakWidth; i < data.length - peakWidth; i++) {
363 flag = 0;
364 peaks[i] = 0;
365 for (int j = -peakWidth; j <= peakWidth; j++) {
366 if (data[i + j] > data[i]) {
367 flag = 1;
368 break;
369 }
370 }
371 if (flag == 0) {
372 peaks[i] = 1;
373
374 }
375 }
376
377
378 int realPeaks = 0;
379 for (int i = 0; i < peaks.length; i++) {
380 if (peaks[i] == 1) {
381 realPeaks++;
382 if (peaks[i + 1] == 1) {
383 realPeaks--;
384 }
385 }
386 }
387
388 return realPeaks;
389 }
390
391
392
393
394
395
396
397 public static int arrayMax(int[] a) {
398 return Arrays.stream(a).max().getAsInt();
399 }
400
401
402
403
404
405
406
407 public static double arrayMax(double[] a) {
408 double max = a[0];
409 if (a.length == 1) {
410 return max;
411 }
412
413 for (int i = 1; i < a.length; i++) {
414 if (max < a[i]) {
415 max = a[i];
416 }
417 }
418 return max;
419 }
420
421
422
423
424
425
426
427 public static double array2dMax(double[][] a) {
428 double max = arrayMax(a[0]);
429 if (a.length == 1) {
430 return max;
431 }
432 for (int i = 1; i < a.length; i++) {
433 double rmax = arrayMax(a[i]);
434 if (max < rmax) {
435 max = rmax;
436 }
437 }
438 return max;
439 }
440
441
442
443
444
445
446
447 public static int minArrayIndex(double[] a) {
448
449 double min = a[0];
450 int imin = 0;
451 if (a.length == 1) {
452 return imin;
453 }
454
455 for (int i = 1; i < a.length; i++) {
456 if (min > a[i]) {
457 min = a[i];
458 imin = i;
459 }
460 }
461 return imin;
462 }
463
464
465
466
467
468
469
470 public static double[] minArrayIndexElement(double[] a) {
471
472 double[] ret = new double[2];
473 double min = a[0];
474 int imin = 0;
475 if (a.length == 1) {
476 ret[0] = min;
477 ret[1] = imin;
478 return ret;
479 }
480
481 for (int i = 1; i < a.length; i++) {
482 if (min > a[i]) {
483 min = a[i];
484 imin = i;
485 }
486 }
487 ret[0] = min;
488 ret[1] = imin;
489 return ret;
490 }
491
492
493
494
495
496
497
498 public static double arrayMin(double[] a) {
499 double min = a[0];
500 if (a.length == 1) {
501 return min;
502 }
503
504 for (int i = 1; i < a.length; i++) {
505 if (min > a[i]) {
506 min = a[i];
507 }
508 }
509 return min;
510 }
511
512
513
514
515
516
517
518 public static double array2dMin(double[][] a) {
519 double min = arrayMin(a[0]);
520 if (a.length == 1) {
521 return min;
522 }
523 for (int i = 1; i < a.length; i++) {
524 double rmin = arrayMin(a[i]);
525 if (min > rmin) {
526 min = rmin;
527 }
528 }
529 return min;
530 }
531
532
533
534
535
536
537
538 public static int sumArray(int[] a) {
539 return Arrays.stream(a).sum();
540 }
541
542
543
544
545
546
547 public static void print(double[] a) {
548
549 for (int i = 0; i < a.length; i++) {
550 System.out.print("" + a[i] + "\n");
551 }
552 System.out.println("");
553 }
554
555
556
557
558
559
560 public static void print(double[][] a) {
561
562 for (int i = 0; i < a.length; i++) {
563 System.out.print("" + a[i][0] + " " + a[i][1] + "\n");
564 }
565 }
566
567
568
569
570
571
572
573 public static int getArraySize(Object object) {
574 if (!object.getClass().isArray()) {
575 return 1;
576 }
577 int size = 0;
578 for (int i = 0; i < Array.getLength(object); i++) {
579 size += getArraySize(Array.get(object, i));
580 }
581 return size;
582 }
583
584
585
586
587
588
589
590
591
592 public static <T> void fill2Darray(double[][] array, double d) {
593 for (double[] row : array) {
594 Arrays.fill(row, d);
595 }
596 }
597
598
599
600
601
602
603
604
605
606 public static <T> void fill2Darray(int[][] array, int d) {
607 for (int[] row : array) {
608 Arrays.fill(row, d);
609 }
610 }
611
612
613
614
615
616
617
618 public static <T extends Comparable<T>> int minListIndex(List<T> a) {
619 return a.indexOf(Collections.min(a));
620 }
621
622
623
624
625
626
627
628 public static double getMax(RealMatrix input) {
629 double[][] data;
630 if (input instanceof Array2DRowRealMatrix) {
631 data = ((Array2DRowRealMatrix) input).getDataRef();
632 } else {
633 data = input.getData();
634 }
635 double[] maxs = new double[input.getRowDimension()];
636 for (int r = 0; r < input.getRowDimension(); r++) {
637 maxs[r] = StatUtils.max(data[r]);
638 }
639 return StatUtils.max(maxs);
640 }
641
642
643
644
645
646
647
648 public static double getMin(RealMatrix input) {
649 double[][] data;
650 if (input instanceof Array2DRowRealMatrix) {
651 data = ((Array2DRowRealMatrix) input).getDataRef();
652 } else {
653 data = input.getData();
654 }
655 double[] maxs = new double[input.getRowDimension()];
656 for (int r = 0; r < input.getRowDimension(); r++) {
657 maxs[r] = StatUtils.min(data[r]);
658 }
659 return StatUtils.min(maxs);
660 }
661
662
663
664
665
666
667
668 public static Number[] castToNumber(ImageProcessor input) {
669 Object pixels = input.getPixels();
670 if (pixels == null) {
671 return null;
672 } else if (pixels instanceof byte[]) {
673 return ArrayUtils.toObject((byte[]) pixels);
674 } else if (pixels instanceof short[]) {
675 return ArrayUtils.toObject((short[]) pixels);
676 } else if (pixels instanceof int[]) {
677 return ArrayUtils.toObject((int[]) pixels);
678 } else if (pixels instanceof float[]) {
679 return ArrayUtils.toObject((float[]) pixels);
680 } else {
681 throw new IllegalArgumentException("Unknown bit depth");
682 }
683 }
684
685
686
687
688
689
690
691 public static FloatProcessor realMatrix2ImageProcessor(RealMatrix rm) {
692 double[][] rawData = rm.transpose().getData();
693 return new FloatProcessor(double2dfloat(rawData));
694 }
695
696
697
698
699
700
701
702 public static RealMatrix imageProcessor2RealMatrix(ImageProcessor ip) {
703 if (ip == null) {
704 return null;
705 }
706 RealMatrix out;
707 float[][] image = ip.getFloatArray();
708
709 out = new Array2DRowRealMatrix(float2ddouble(image), false);
710 return out.transpose();
711 }
712 }