package uk.ac.leeds.ccg.cluster; import java.awt.*; import java.awt.event.*; import java.util.*; //import com.sun.java.util.collections.*; //change for 1.2! import uk.ac.leeds.ccg.geotools.*; /** A class that implements Besag and Newell Cluster Hunt * The main class BandN creates the main frame and proivides 3 buttons * modified to use the new results object - 31/01/01 */ public class Spattimescan extends SimpleMethod implements Method, Runnable { // Members SpattimescanParameters parameters; SpattimescanParametersViewer parametersViewer; Database database; Results results; Viewer viewer; // Geotools viewer GeoRectangle geoRectangle = null; // Geotools geoRectangle Shader shader; // Geotools shader // Methods public static void main(String args[]){ Spattimescan spattimescan = new Spattimescan(); spattimescan.run(); } public Spattimescan(){ parameters = new SpattimescanParameters(); parametersViewer = new SpattimescanParametersViewer(parameters); } public String toString() {return "Spattimescan";} public Parameters getParameters() {return parameters;} public ParametersViewer getParametersViewer() {return parametersViewer;} public void setShader(Shader s) {shader=s;} public void setViewer(Viewer v) {viewer=v;} public void setData(Database d) {database=d;} public SignificanceTest getSignificanceTest(){return significanceTest;} public void setSearch(GeoRectangle r) {geoRectangle=r;} public Results getResults(){return results;} public void run(){ results = new Results(); double radMin = parameters.getRadMin(); double radMax = parameters.getRadMax(); double radInc = parameters.getRadInc(); double timeMin = parameters.getTimeMin(); double timeMax = parameters.getTimeMax(); double timeInc = parameters.getTimeInc(); int numRuns = parameters.getNumRuns(); double thresh = parameters.getSignificanceThreshold(); MyPoint p, p2; double n_g = (double) 0.0; double mu_g = (double) 0.0; double lambda = (double) 0.0; double maxLam = (double) 0.0; double maxLam2 = (double) 0.0; int nge = 0; double stat = (double) 0.0; System.out.println("Spattimescan "+significanceTest); if (significanceTest == null) { System.out.println("Spattimescan started with a null sig test using Poisson"); significanceTest = new PoissonTest(); significanceTest.setParameters(parameters); } System.out.println(""+this+" "+significanceTest); if(geoRectangle==null) geoRectangle=database.getBounds(); Vector tcases = database.getCases(); Vector cases= new Vector(); for(int i=0;i timeMax ) continue; obsP += ((MyPoint)points.elementAt(i2)).getPop(); //mu_z obsC += ((MyPoint)points.elementAt(i2)).getCases(); //n_z } // System.out.println("loop "+loop+" L2 "+l2+" obsP "+obsP+" obsC "+obsC); if (obsC == 0) { // System.out.println("No cases in this interval"); continue; } double num1 = obsC / obsP; double num2 = (n_g - obsC) / (mu_g - obsP); double denom = n_g / mu_g; double fac1 = num1 /num2; double fac2 = num2 / denom; // extra bits for binomial rather than Poisson // denominator equals 1 as tot popn rescaled to equal tot no of cases! if (num1 < num2) continue; // double lambda = Math.pow(num1,obsC) *Math.pow(num2, (n_g-obsC)); lambda = Math.pow(fac1,obsC) *Math.pow(fac2, n_g); // lambda /= Math.pow(denom, n_g); // System.out.println("fac1 "+fac1+" fac2 "+ fac2 +" "+ Math.pow(fac1,obsC) +" "+ Math.pow(fac2, n_g) ); if (lambda > maxLam){ maxLam = lambda; p2 = p; System.out.println("num1 "+num1+" num2 "+num2+" lambda "+lambda); } // what's all this lot?? // this method just finds one cluster (at present) so I have commented out // the stuff that marks it(them) on a map - might have to check what sort // of value is returned as 'stat' if/when I allow more than one cluster! // try{ // if(significanceTest.isSignificant(points)) // { // stat = significanceTest.getStatistic(); // // results.addResult(cc,stat); // results.addResult(gc,stat); // } // }catch(SignificanceException e){ // System.out.println(""+e); // } } } } if (lambda != 0) System.out.println(p2); else System.out.println("No cases! - check time/circle radius parameters?"); // Here starts the Monte Carlo significance part Vector orig = database.getPoints(); // System.out.println(orig.size()); int no_points = orig.size(); // numRuns = 2; //just for the time being! for (int k=0; k timeMax ) continue; obsP += ((MyPoint)newpoints.elementAt(i2)).getPop(); //mu_z obsC += ((MyPoint)newpoints.elementAt(i2)).getCases(); //n_z } if (obsC == 0) { // System.out.println("No cases in this interval"); continue; } // System.out.println("loop "+loop+" l2 "+l2+" obsP "+obsP+" obsC "+obsC); double num1 = obsC / obsP; double num2 = (n_g - obsC) / (mu_g - obsP); double denom = n_g / mu_g; double fac1 = num1 / num2; double fac2 = num2 / denom; if (num1 < num2) continue; double lamb2 = Math.pow(fac1,obsC) *Math.pow(fac2,n_g); if (lamb2 > maxLam2){ maxLam2 = lamb2; // System.out.println("num1 "+num1+" num2 "+num2+" lamb2 "+lamb2); } } } } if (maxLam2 >= maxLam) nge++; if ( (double) (nge+1)/(numRuns+1) > thresh){ System.out.println("Will not be significant, nge = " + nge +","+ k +" out of NRuns"); break; } } if (lambda != 0) System.out.println("Prob. of such a result is less than, or equal to " + (double) (nge+1)/(numRuns+1)); notifyProgressEventListener(0.0); } }