Debian Codegraph @

src/dr/evomodel/coalescent/GPSkytrackAnalysis.java

raw symbols SCIP
1 /*
2 * GPSkytrackAnalysis.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evomodel.coalescent;
27
28 import dr.evolution.io.Importer;
29 import dr.inference.model.Parameter;
30
31 //import dr.evolution.io.NexusImporter;
32 //import dr.evolution.io.TreeImporter;
33 //import dr.evolution.tree.Tree;
34 //import dr.inference.operators.AdaptationMode;
35 //import dr.inference.trace.AbstractTraceList;
36 import dr.inference.trace.LogFileTraces;
37 import dr.inference.trace.TraceException;
38 import dr.evomodel.coalescent.operators.GaussianProcessSkytrackBlockUpdateOperator;
39 //import dr.inference.trace.TraceFactory;
40 import dr.stats.DiscreteStatistics;
41 //import dr.util.FileHelpers;
42 //import dr.util.HeapSort;
43 import dr.util.TabularData;
44 //import no.uib.cipr.matrix.SymmTridiagEVD;
45 //import no.uib.cipr.matrix.*;
46
47
48 import java.io.*;
49 //import java.io.PrintWriter;
50 import java.util.Arrays;
51 import java.util.StringTokenizer;
52
53 /**
54 * @author Joseph Heled
55 */
56 public class GPSkytrackAnalysis extends TabularData {
57 // TabularData
58
59 private final double[] xPoints;
60 private final double[] means;
61 private final double[] medians;
62 private final double[] hpdLower;
63 private final double[] hpdHigh;
64 private final double [][] gValues;
65 private final double [][] tValues;
66 private final double [][] newGvalues;
67 private final double [][] popValues;
68
69
70 // final File gvalues = FileHelpers.getFile("gvalues.txt");
71 // final File locations = FileHelpers.getFile("locations.txt");
72
73 // private final double[] HPDLevels;
74 private Parameter numGridPoints;
75 // each bin covers xPoints[-1]/coalBins.length
76 // private int[] coalBins;
77
78 // private final boolean quantiles;
79
80 // GaussianProcessSkytrackLikelihood gpLikelihood = (GaussianProcessSkytrackLikelihood) xo.getChild(GaussianProcessSkytrackLikelihood.class);
81 // return new GaussianProcessSkytrackBlockUpdateOperator(gpLikelihood, weight, mode, scaleFactor,
82 // maxIterations, stopValue);
83
84 // TODO: Error in loadTraces() because a String {..} is being converted to "real/double"
85 // To make my life more miserable I will not use logFileTraces class and do it by hand
86
87
88 public GPSkytrackAnalysis(File log, double burnIn, Parameter numGridPoints) throws IOException, Importer.ImportException, TraceException {
89 GaussianProcessSkytrackBlockUpdateOperator GPOperator=new GaussianProcessSkytrackBlockUpdateOperator();
90 this.numGridPoints=numGridPoints;
91 LogFileTraces ltraces = new LogFileTraces(log.getCanonicalPath(), log);
92
93 // ltraces.changeTraceType(1, TraceFactory.TraceType.STRING);
94
95 ltraces.loadTraces();
96 // System.exit(-1);
97
98 ltraces.setBurnIn(0);
99 final int runLengthIncludingBurnin = ltraces.getStateCount();
100
101 int intBurnIn = (int) Math.floor(burnIn < 1 ? runLengthIncludingBurnin * burnIn : burnIn);
102 final int nStates = runLengthIncludingBurnin - intBurnIn;
103 ltraces.setBurnIn(intBurnIn * ltraces.getStepSize());
104 assert ltraces.getStateCount() == nStates;
105
106
107 xPoints = new double[(int) numGridPoints.getParameterValue(0)+1];
108 means = new double[(int) numGridPoints.getParameterValue(0)+1];
109 medians = new double[(int) numGridPoints.getParameterValue(0)+1];
110 hpdHigh = new double[(int) numGridPoints.getParameterValue(0)+1];
111 hpdLower = new double[(int) numGridPoints.getParameterValue(0)+1];
112
113 int numbPointsColumn = -1;
114 int gvaluesColumn=-1;
115 int xvaluesColumn=-1;
116 int lambdaColumn = -1;
117 int precColumn = -1;
118 int tmrcaColumn=-1;
119
120
121
122 for (int n = 0; n < ltraces.getTraceCount(); ++n) {
123 final String traceName = ltraces.getTraceName(n);
124 System.err.println(traceName);
125 if (traceName.equals("skyride.points")) {
126 numbPointsColumn = n;
127 } else if (traceName.equals("skyride.lambda_bound")) {
128 lambdaColumn = n;
129 } else if (traceName.equals("skyride.precision")) {
130 precColumn = n;
131 } else if (traceName.equals("skyride.tmrca")) {
132 tmrcaColumn = n;
133 } else if (traceName.equals("changePoints")){
134 xvaluesColumn=n;
135 } else if (traceName.equals("Gvalues")){
136 gvaluesColumn=n;
137 }
138
139
140 }
141 // System.err.println("columns"+tmrcaColumn+" tmrca"+xvaluesColumn+" and"+gvaluesColumn);
142 if (numbPointsColumn < 0 || lambdaColumn < 0 || precColumn<0 || tmrcaColumn<0 || xvaluesColumn<0 || gvaluesColumn<0) {
143 throw new TraceException("incorrect trace column names: unable to find correct columns for summary");
144 }
145
146 // TODO: Check if it is ok to define the grid from 0 to max(TMRCA) always
147 double binSize = 0;
148 // double hSum = -0;
149
150 // System.err.println("states"+nStates);
151 int [] numPoints = new int[nStates];
152 double[] lambda = new double[nStates];
153 double[] kappa= new double[nStates];
154 double tmrca=0;
155 // double binSize=0;
156 double tempTmrca=0.0;
157 int maxpts=0;
158 for (int ns = 0; ns < nStates; ++ns) {
159 lambda[ns]= (Double) ltraces.getTrace(lambdaColumn).getValue(ns);
160 numPoints[ns]=(int)Math.round((Double) ltraces.getTrace(numbPointsColumn).getValue(ns));
161 kappa[ns]=(Double) ltraces.getTrace(precColumn).getValue(ns);
162 tempTmrca=(Double) ltraces.getTrace(tmrcaColumn).getValue(ns);
163 // System.err.println(tempTmrca);
164 System.exit(-1);
165 if (tempTmrca>tmrca){tmrca=tempTmrca;}
166 if (numPoints[ns]>maxpts) {maxpts=numPoints[ns];}
167 }
168
169 binSize = tmrca / numGridPoints.getParameterValue(0);
170 xPoints[0]=0.0;
171 for (int np=1;np<xPoints.length;np++){
172 xPoints[np]=xPoints[np-1]+binSize;
173 }
174
175 gValues=new double[nStates][];
176 tValues=new double[nStates][];
177 newGvalues=new double[nStates][];
178 popValues=new double[(int) numGridPoints.getParameterValue(0)+1][];
179 readChain(gValues,"gvalues.txt");
180 readChain(tValues,"locations.txt");
181 for (int i=0;i<=numGridPoints.getParameterValue(0);i++){
182 popValues[i]=new double[nStates-1] ;
183 }
184 //
185 for (int j=0;j<nStates-1;j++){
186 // newGvalues[j]=new double[numPoints[j]];
187
188 newGvalues[j]=GPOperator.getGPvaluesS(tValues[j], gValues[j], xPoints, kappa[j]);
189 // popValues[j]=new double[nStates];
190 for (int i=0;i<=numGridPoints.getParameterValue(0);i++){
191 popValues[i][j]=(1+Math.exp(-newGvalues[j][i]))/lambda[j];
192 }
193 }
194 //
195 ////
196 // hpdLower = new double[HPDLevels.length][];
197 // hpdHigh = new double[HPDLevels.length][];
198 //
199
200 for (int nx = 0; nx < xPoints.length; ++nx) {
201 means[nx] = DiscreteStatistics.mean(popValues[nx]);
202 medians[nx]=DiscreteStatistics.median(popValues[nx]);
203 hpdLower[nx]=DiscreteStatistics.quantile(0.025,popValues[nx]);
204 hpdHigh[nx]=DiscreteStatistics.quantile(0.975,popValues[nx]);
205
206 }
207
208
209
210 //
211 // for (int i = 0; i < HPDLevels.length; ++i) {
212 // if (quantiles) {
213 // hpdLower[i][nx] = DiscreteStatistics.quantile((1 - HPDLevels[i]) / 2, popValues, indices);
214 // hpdHigh[i][nx] = DiscreteStatistics.quantile((1 + HPDLevels[i]) / 2, popValues, indices);
215 // } else {
216 // final double[] hpd = DiscreteStatistics.HPDInterval(HPDLevels[i], popValues, indices);
217 // hpdLower[i][nx] = hpd[0];
218 // hpdHigh[i][nx] = hpd[1];
219 // }
220 // }
221 // medians[nx] = DiscreteStatistics.median(popValues, indices);
222 // }
223 //
224 // if( allDemoWriter != null ) {
225 // for(double xPoint : xPoints) {
226 // allDemoWriter.print(xPoint);
227 // allDemoWriter.append(' ');
228 // }
229 //
230 // for (int ns = 0; ns < nDataPoints; ++ns) {
231 // allDemoWriter.println();
232 // for(double xPoint : xPoints) {
233 // allDemoWriter.print(allDemog[ns].getDemographic(xPoint));
234 // allDemoWriter.append(' ');
235 // }
236 // }
237 // allDemoWriter.close();
238 // }
239 }
240
241 public void readChain(double [][] current,String fileName){
242 try {
243 BufferedReader br = new BufferedReader(new FileReader(fileName));
244
245 String line=null;
246 int i=0;
247 // System.err.println("will read line1");
248 while ((line=br.readLine())!=null){
249 String[] parts=line.split(" ");
250 // System.err.println(i+"with cols:"+parts.length);
251 current[i]=new double[parts.length];
252 for (int j=0;j<parts.length;j++){
253 current[i][j]=Double.parseDouble(parts[j]);
254 }
255 i++;
256 } br.close();
257 }
258 catch(java.io.IOException ioe){
259 System.err.println("IOException:"+ ioe.getMessage());
260 }
261 }
262
263
264
265 private final String[] columnNames = {"time", "mean", "median","lower","upper"};
266
267 public int nColumns() {
268 return 5;
269 }
270
271 public String columnName(int nColumn) {
272 // final int fixed = columnNames.length;
273 // if (nColumn < fixed) {
274 return columnNames[nColumn];
275 }
276 // nColumn -= fixed;
277 // if (nColumn < 2 * HPDLevels.length) {
278 // final double p = HPDLevels[nColumn / 2];
279 // final String s = (nColumn % 2 == 0) ? "lower" : "upper";
280 // return (quantiles ? "cpd " : "hpd ") + s + " " + Math.round(p * 100);
281 // }
282 // assert (nColumn - 2 * HPDLevels.length) == 0;
283 // return "bins";
284 // }
285
286 public int nRows() {
287 return (int) numGridPoints.getParameterValue(0)+1;
288 }
289
290
291 public Object data(int nRow, int nColumn) {
292 switch (nColumn) {
293 case 0: {
294 if (nRow < xPoints.length) {
295 return xPoints[nRow];
296 }
297 break;
298 }
299 case 1: {
300 if (nRow < means.length) {
301 return means[nRow];
302 }
303 break;
304 }
305 case 2: {
306 if (nRow < medians.length) {
307 return medians[nRow];
308 }
309 break;
310 }
311 case 3: {
312 if (nRow < hpdLower.length) {
313 return hpdLower[nRow];
314 }
315 break;
316 }
317 case 4: {
318 if (nRow < hpdHigh.length) {
319 return hpdHigh[nRow];
320 }
321 break;
322 }
323 // default: {
324 // final int j = nColumn - columnNames.length;
325 // if (j < 2 * HPDLevels.length) {
326 // if (nRow < xPoints.length) {
327 // final int k = j / 2;
328 // if (0 <= k && k < HPDLevels.length) {
329 // if (j % 2 == 0) {
330 // return hpdLower[k][nRow];
331 // } else {
332 // return hpdHigh[k][nRow];
333 // }
334 // }
335 // }
336 // } else {
337 // if (nRow < coalBins.length) {
338 // return coalBins[nRow];
339 // }
340 // }
341 // break;
342 // }
343 }
344 return "";
345 }
346
347
348 }