package stat;
import java.io.*;
import java.awt.*;
import parameters.ParameterSet;
import parameters.ParameterSetArray;
import main.Globals;
import main.GlobalIO;
/**
Stat is the base class for statistical output classes from GeneNet.
Override this class to implement a particular statistic. The only
function you really need to override is the run method. paramSets
will be set to the currently loaded Cam file. Each stat should check
to make sure this is not null before starting to do anything.
Calling paramSets.goToBeginning() will get the first ParameterSet
in that cam (which meets all the criteria currently set for the cam).
After that, use calls to paramSets.getNextSet() to get subsequent sets.
When either of these return null, you're done.
Note - To add a new stat into the program, all you need to do is
add its name, minus the Stat at the end, into the Statistics menu
set up in the CamGraph object.
Note - because of how CamGraph figures out statistic names, all
statistics objects must end in Stat, as in DumpStat or AveragesStat.
*/
public class Stat implements Runnable {
ParameterSetArray paramSets = null;
ParameterSet prototype;
int numSets = 0;
PrintWriter outputFile = null;
public Stat() {}
public void init(ParameterSetArray psa) {
paramSets = psa;
}
public void run()
{
calculateStat();
}
public void calculateStat() {} // Override for each stat
/** This function makes a matrix filled with all the parameters from this parameter
set. Each parameter set takes up one row, and there are as many rows as parameter
sets in the ParameterSetArray. Note that only parameters which appear in the prototype
set (or in the first set of parameters if there is no prototype) will be put into the
matrix. Missing parameters are given a value of 0.
This function also, incidentally, sets prototype equal to the ParameterSetArray's
prototype parameter set, or if that is null, to the first parameter set.
It also sets numSets equal to the number of parameter sets that we are working with.
*/
double [] makeParameterMatrix() {
prototype = paramSets.getPrototype();
if(prototype == null) prototype = paramSets.goToBeginning();
if(prototype == null) return null; // Nothing in cam file
int num_params = prototype.getNumParams();
if(num_params == 0) return null; // Nothing in cam file
// Figure out how many parameter sets we're working with
ParameterSet set = paramSets.goToBeginning();
numSets = 0;
while(set != null) {
set = paramSets.getNextSet();
numSets++;
}
double [] mat = new double[numSets * num_params];
int i;
for(i = 0; i < numSets * num_params; i++) mat[i] = 0;
int set_num = 0;
set = paramSets.goToBeginning();
while(set != null) {
for(i = 0; i < set.getNumParams(); i++) {
try {
int pos = prototype.getPosition(set.getName(i));
mat[set_num * num_params + pos] = set.getValue(i);
} catch(Exception e) {} // Missing from prototype, so don't do anything
}
set = paramSets.getNextSet();
set_num++;
}
return mat;
}
/** Returns the standard deviation. Sum is the sum of the values, sum_sqr is the
sum of the squares of the values, and num is the number of values.
*/
static double getStdDev(double sum, double sum_sqr, double num) {
return Math.sqrt((sum_sqr - (sum * sum / num)) / (num - 1.0));
}
void printParameterLabels(ParameterSet set, PrintWriter ps) {
for(int i = 0; i < set.getNumParams(); i++)
ps.print(set.getName(i) + "\t");
}
public void setOutputFile(String filename) throws IOException
{
if(Globals.isScriptRunning()) return;
outputFile = new PrintWriter(new FileOutputStream(filename));
}
void makeOutputFile() throws IOException
{
if(Globals.isScriptRunning()) return;
FileDialog fd = new FileDialog(new Frame(), "Save into file:", FileDialog.SAVE);
fd.show();
if(fd.getFile() == null) return;
outputFile = new PrintWriter(new FileOutputStream(fd.getDirectory() + fd.getFile()));
}
void closeOutputFile()
{
if(outputFile != null)
{
outputFile.close();
outputFile = null;
}
}
void printToFile(String str)
{
if(Globals.isScriptRunning()) GlobalIO.printScript(str);
else outputFile.print(str);
}
void printlnToFile(String str)
{
if(Globals.isScriptRunning()) GlobalIO.printlnScript(str);
else outputFile.println(str);
}
}