|
| 1 | +package com.conveyal.r5.analyst; |
| 2 | + |
| 3 | +import com.amazonaws.services.s3.AmazonS3; |
| 4 | +import com.amazonaws.services.s3.AmazonS3Client; |
| 5 | +import com.amazonaws.services.s3.model.S3Object; |
| 6 | +import com.google.common.io.LittleEndianDataInputStream; |
| 7 | + |
| 8 | +import java.io.FileInputStream; |
| 9 | +import java.io.FileOutputStream; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.util.zip.GZIPInputStream; |
| 13 | + |
| 14 | +/** |
| 15 | + * A DualGridStatisticComputer computes statistics based on two grids. |
| 16 | + */ |
| 17 | +public abstract class DualGridStatisticComputer { |
| 18 | + private static final AmazonS3 s3 = new AmazonS3Client(); |
| 19 | + /** Version of the access grid format we read */ |
| 20 | + private static final int ACCESS_GRID_VERSION = 0; |
| 21 | + |
| 22 | + /** |
| 23 | + * Calculate the probability at each origin that a random individual sample from regional analysis B is larger than one from regional |
| 24 | + * analysis A. We do this empirically and exhaustively by for each origin looping over every possible combination of |
| 25 | + * samples and taking a difference, then evaluating the number that yielded results greater than zero. |
| 26 | + * |
| 27 | + * The regional analysis access grids must be of identical size and zoom level, and a Grid object (the same as is used |
| 28 | + * for destination grids) will be returned, with probabilities scaled from 0 to 100,000. |
| 29 | + */ |
| 30 | + public Grid computeImprovementProbability (String resultBucket, String regionalAnalysisAKey, String regionalAnalysisBKey) throws IOException { |
| 31 | + S3Object aGrid = s3.getObject(resultBucket, regionalAnalysisAKey); |
| 32 | + S3Object bGrid = s3.getObject(resultBucket, regionalAnalysisBKey); |
| 33 | + return computeImprovementProbability(aGrid.getObjectContent(), bGrid.getObjectContent()); |
| 34 | + } |
| 35 | + |
| 36 | + public Grid computeImprovementProbability(InputStream a, InputStream b) throws IOException { |
| 37 | + LittleEndianDataInputStream aIn = new LittleEndianDataInputStream(new GZIPInputStream(a)); |
| 38 | + LittleEndianDataInputStream bIn = new LittleEndianDataInputStream(new GZIPInputStream(b)); |
| 39 | + |
| 40 | + validateHeaderAndVersion(aIn); |
| 41 | + validateHeaderAndVersion(bIn); |
| 42 | + |
| 43 | + int aZoom = aIn.readInt(); |
| 44 | + int aWest = aIn.readInt(); |
| 45 | + int aNorth = aIn.readInt(); |
| 46 | + int aWidth = aIn.readInt(); |
| 47 | + int aHeight = aIn.readInt(); |
| 48 | + |
| 49 | + int bZoom = bIn.readInt(); |
| 50 | + int bWest = bIn.readInt(); |
| 51 | + int bNorth = bIn.readInt(); |
| 52 | + int bWidth = bIn.readInt(); |
| 53 | + int bHeight = bIn.readInt(); |
| 54 | + |
| 55 | + if (aZoom != bZoom || |
| 56 | + aWest != bWest || |
| 57 | + aNorth != bNorth || |
| 58 | + aWidth != bWidth || |
| 59 | + aHeight != bHeight) { |
| 60 | + throw new IllegalArgumentException("Grid sizes for comparison must be identical!"); |
| 61 | + } |
| 62 | + |
| 63 | + // number of iterations need not be equal, the computed probability is still valid even if they are not |
| 64 | + // as the probability of choosing any particular sample is still uniform within each scenario. |
| 65 | + int aIterations = aIn.readInt(); |
| 66 | + int bIterations = bIn.readInt(); |
| 67 | + |
| 68 | + Grid out = new Grid(aZoom, aWidth, aHeight, aNorth, aWest); |
| 69 | + |
| 70 | + // pixels are in row-major order, iterate over y on outside |
| 71 | + for (int y = 0; y < aHeight; y++) { |
| 72 | + for (int x = 0; x < aWidth; x++) { |
| 73 | + int[] aValues = new int[aIterations]; |
| 74 | + int[] bValues = new int[bIterations]; |
| 75 | + |
| 76 | + for (int iteration = 0, val = 0; iteration < aIterations; iteration++) { |
| 77 | + aValues[iteration] = (val += aIn.readInt()); |
| 78 | + } |
| 79 | + |
| 80 | + for (int iteration = 0, val = 0; iteration < bIterations; iteration++) { |
| 81 | + bValues[iteration] = (val += bIn.readInt()); |
| 82 | + } |
| 83 | + |
| 84 | + out.grid[x][y] = computeValuesForOrigin(x, y, aValues, bValues); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return out; |
| 89 | + } |
| 90 | + |
| 91 | + private static void validateHeaderAndVersion(LittleEndianDataInputStream input) throws IOException { |
| 92 | + char[] header = new char[8]; |
| 93 | + for (int i = 0; i < 8; i++) { |
| 94 | + header[i] = (char) input.readByte(); |
| 95 | + } |
| 96 | + |
| 97 | + if (!"ACCESSGR".equals(new String(header))) { |
| 98 | + throw new IllegalArgumentException("Input not in access grid format!"); |
| 99 | + } |
| 100 | + |
| 101 | + int version = input.readInt(); |
| 102 | + |
| 103 | + if (version != ACCESS_GRID_VERSION) { |
| 104 | + throw new IllegalArgumentException(String.format("Version mismatch of access grids, expected %s, found %s", ACCESS_GRID_VERSION, version)); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** Given the origin coordinates and the values from the two grids, compute a value for the output grid */ |
| 109 | + protected abstract double computeValuesForOrigin (int x, int y, int[] aValues, int[] bValues); |
| 110 | + |
| 111 | + public static void main (String... args) throws IOException { |
| 112 | + DualGridStatisticComputer comp; |
| 113 | + if ("--two-tailed".equals(args[0])) { |
| 114 | + comp = new BootstrapPercentileHypothesisTestGridStatisticComputer(); |
| 115 | + } else { |
| 116 | + throw new RuntimeException("Unknown grid statistic computer " + args[0]); |
| 117 | + } |
| 118 | + |
| 119 | + FileInputStream a = new FileInputStream(args[1]); |
| 120 | + FileInputStream b = new FileInputStream(args[2]); |
| 121 | + Grid grid = comp.computeImprovementProbability(a, b); |
| 122 | + |
| 123 | + if (args[3].endsWith(".grid")) |
| 124 | + grid.write(new FileOutputStream(args[3])); |
| 125 | + else if (args[3].endsWith(".png")) |
| 126 | + grid.writePng(new FileOutputStream(args[3])); |
| 127 | + else if (args[3].endsWith(".tif")) |
| 128 | + grid.writeGeotiff(new FileOutputStream(args[3])); |
| 129 | + } |
| 130 | +} |
0 commit comments