|
23 | 23 |
|
24 | 24 | package qupath.lib.analysis.stats; |
25 | 25 |
|
26 | | -import java.util.Collection; |
27 | | - |
28 | 26 | import qupath.lib.objects.PathObject; |
29 | 27 |
|
| 28 | +import java.util.Collection; |
| 29 | + |
30 | 30 |
|
31 | 31 | /** |
32 | 32 | * Class for storing histogram data & basic statistics. |
@@ -350,6 +350,39 @@ public Histogram(ArrayWrappers.ArrayWrapper values, int nBins, double minEdge, d |
350 | 350 | buildHistogram(values, nBins, minEdge, maxEdge); |
351 | 351 | } |
352 | 352 |
|
| 353 | + /** |
| 354 | + * Create histogram from an existing edges and counts arrays. |
| 355 | + * <p> |
| 356 | + * This is useful when a histogram already exists in another format, and it would be too expensive to rebuild |
| 357 | + * from the original values. |
| 358 | + * <p> |
| 359 | + * Note that the statistics (min, max, mean, std.dev. and variance) are estimated from the bin centers, and may not |
| 360 | + * match the values from the data originally used to build the histogram. |
| 361 | + * |
| 362 | + * @param edges an array of edges, which should be of length {@code nBins + 1}. |
| 363 | + * @param counts an array of counts, which should be of length {@code nBins}. |
| 364 | + */ |
| 365 | + public Histogram(double[] edges, long[] counts) { |
| 366 | + this.edgeMin = edges[0]; |
| 367 | + this.edgeMax = edges[edges.length-1]; |
| 368 | + this.edges = edges.clone(); |
| 369 | + this.counts = counts.clone(); |
| 370 | + boolean maybeInteger = true; |
| 371 | + long countsSum = 0L; |
| 372 | + var stats = new RunningStatistics(); |
| 373 | + for (int i = 0; i < edges.length-1; i++) { |
| 374 | + double center = (edges[i] + edges[i+1]) / 2.0; |
| 375 | + maybeInteger = maybeInteger && Math.round(center) == center; |
| 376 | + for (long k = 0; k < counts[i]; k++) { |
| 377 | + stats.addValue(center); |
| 378 | + countsSum++; |
| 379 | + } |
| 380 | + } |
| 381 | + this.isInteger = maybeInteger; |
| 382 | + this.stats = stats; |
| 383 | + this.countSum = countsSum; |
| 384 | + } |
| 385 | + |
353 | 386 |
|
354 | 387 | private void buildHistogram(final ArrayWrappers.ArrayWrapper values, int nBins, double minEdge, double maxEdge) { |
355 | 388 |
|
|
0 commit comments