| 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 |
package dr.geo.contouring; |
| 27 |
|
| 28 |
import org.rosuda.JRI.REXP; |
| 29 |
import org.rosuda.JRI.RVector; |
| 30 |
import org.rosuda.JRI.Rengine; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class ContourWithR implements ContourMaker { |
| 36 |
|
| 37 |
|
| 38 |
public ContourWithR(final double[] xValues, final double[] yValues) { |
| 39 |
this(xValues, yValues, 50); |
| 40 |
} |
| 41 |
|
| 42 |
public ContourWithR(final double[] xValues, final double[] yValues, int N) { |
| 43 |
this.xValues = xValues; |
| 44 |
this.yValues = yValues; |
| 45 |
this.N = N; |
| 46 |
} |
| 47 |
|
| 48 |
private final double[] xValues; |
| 49 |
private final double[] yValues; |
| 50 |
|
| 51 |
public ContourPath[] getContourPaths(double hpdValue) { |
| 52 |
makeContour(xValues, yValues, hpdValue, N); |
| 53 |
if (contourList == null) |
| 54 |
return null; |
| 55 |
|
| 56 |
ContourPath[] contourPaths = new ContourPath[getNumberContours()]; |
| 57 |
|
| 58 |
for(int i=0; i<getNumberContours(); i++) { |
| 59 |
double[][] cont = getContour(i); |
| 60 |
contourPaths[i] = new ContourPath(null,1,cont[0],cont[1]); |
| 61 |
} |
| 62 |
return contourPaths; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
public void makeContour(double[] xValues, double[] yValues, double hpd) { |
| 67 |
makeContour(xValues, yValues, hpd, N); |
| 68 |
} |
| 69 |
|
| 70 |
public void makeContour(double[] xValues, double[] yValues, double hpd, int N) { |
| 71 |
|
| 72 |
|
| 73 |
REXP x = rEngine.eval("makeContour(" + |
| 74 |
makeRString(xValues) + "," + |
| 75 |
makeRString(yValues) + "," + |
| 76 |
hpd + "," + |
| 77 |
N + ")"); |
| 78 |
contourList = x.asVector(); |
| 79 |
} |
| 80 |
|
| 81 |
public int getNumberContours() { |
| 82 |
if (contourList != null) |
| 83 |
return contourList.size(); |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public double[][] getContour(int whichContour) { |
| 91 |
|
| 92 |
if (contourList != null) { |
| 93 |
double[][] result = new double[2][]; |
| 94 |
RVector oneContour = contourList.at(whichContour).asVector(); |
| 95 |
result[0] = oneContour.at(1).asDoubleArray(); |
| 96 |
result[1] = oneContour.at(2).asDoubleArray(); |
| 97 |
return result; |
| 98 |
} |
| 99 |
return null; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
private static Rengine rEngine = null; |
| 104 |
private int N; |
| 105 |
|
| 106 |
|
| 107 |
private RVector contourList = null; |
| 108 |
|
| 109 |
private static final String[] rArgs = {"--no-save", "--max-vsize=1G"}; |
| 110 |
|
| 111 |
private static final String[] rBootCommands = { |
| 112 |
"library(MASS)", |
| 113 |
"makeContour = function(var1, var2, prob=0.95, n=50, h=c(1,1)) {" + |
| 114 |
"post1 = kde2d(var1, var2, n = n, h=h); " + |
| 115 |
"dx = diff(post1$x[1:2]); " + |
| 116 |
"dy = diff(post1$y[1:2]); " + |
| 117 |
"sz = sort(post1$z); " + |
| 118 |
"c1 = cumsum(sz) * dx * dy; " + |
| 119 |
"levels = sapply(prob, function(x) { approx(c1, sz, xout = 1 - x)$y }); " + |
| 120 |
"line = contourLines(post1$x, post1$y, post1$z, level = levels); " + |
| 121 |
"return(line) }" |
| 122 |
}; |
| 123 |
|
| 124 |
private String makeRString(double[] values) { |
| 125 |
StringBuffer sb = new StringBuffer("c("); |
| 126 |
sb.append(values[0]); |
| 127 |
for (int i = 1; i < values.length; i++) { |
| 128 |
sb.append(","); |
| 129 |
sb.append(values[i]); |
| 130 |
} |
| 131 |
sb.append(")"); |
| 132 |
return sb.toString(); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
static public boolean processWithR = false; |
| 137 |
|
| 138 |
static { |
| 139 |
try { |
| 140 |
System.loadLibrary("jri"); |
| 141 |
processWithR = true; |
| 142 |
System.err.println("JRI loaded. Will process using R contouring."); |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
rEngine = new Rengine(rArgs, false, null); |
| 149 |
|
| 150 |
if (!rEngine.waitForR()) { |
| 151 |
throw new RuntimeException("Cannot load R"); |
| 152 |
} |
| 153 |
|
| 154 |
for (String command : rBootCommands) { |
| 155 |
rEngine.eval(command); |
| 156 |
} |
| 157 |
} catch (UnsatisfiedLinkError e) { |
| 158 |
System.err.println("JRI not available. Will process using Java contouring."); |
| 159 |
} |
| 160 |
} |
| 161 |
} |