/* * ExperimentalCondition.java * * Created on May 11, 2007, 6:01 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ import java.util.Vector; import java.awt.Point; import org.shodor.math11.SingleStat; /** * * @author Matt DesVoigne */ public class ExperimentalCondition { private int conditionID; // The experimental condition ID to track multiple conditions. private Vector timesteps; // Data records for this condition ID. Each element in the vector represents data at a cross-sectional time step. private String[] propertyDescriptions; //Array of Strings of property descriptions. private int runningTimeStep; // Used to sort records into timestep vectors private int runningParticipantID; // When the running participant ID does not match the participant ID, reset the time. /** * Creates a new instance of ExperimentalCondition. * Upon construction, no data fields should be added to the experimental * condition data set. */ public ExperimentalCondition(int condID, String[] descriptions) { conditionID = condID; timesteps = new Vector(); propertyDescriptions = descriptions; runningTimeStep = 0; runningParticipantID = -1; } public void addTrial(int participantID, double[] dataFields) throws TrialException { if (dataFields.length != propertyDescriptions.length) { throw new TrialException("Incorrect number of properties for participant ID " + participantID + ", Experimental Condition ID " + conditionID + "Properties required: "+propertyDescriptions.length+ ", Properties Provided: " + dataFields.length); } // If a time step element in the timestep vector exists, add it to // the time step record. If not, create a new timestep record. TimeStep timestep = getTimeStepByParticipant(participantID); if (timestep == null) { timesteps.addElement(new TimeStep(runningTimeStep)); TimeStep lastTimeStep = (TimeStep) timesteps.lastElement(); lastTimeStep.addTrial(new Trial(participantID, dataFields)); } else { timestep.addTrial(new Trial(participantID, dataFields)); } } /** *Retrieves the timestep object to add the data record to if the timestep record has been created. *If the timestep record has not yet been created, then return null which should subsequently *create a new timestep and add the record to it. */ private TimeStep getTimeStepByParticipant(int partID) { runningTimeStep++; if (runningParticipantID != partID) { runningTimeStep = 0; runningParticipantID = partID; } // If the timestep vector contains an element whose index corresponds to runningTimeStep, // return that element. If not, return null which will create the new time step. if (runningTimeStep < 0 || runningTimeStep >= timesteps.size()) { return null; } return ((TimeStep)timesteps.elementAt(runningTimeStep)); } public TimeStep getTimeStepByTime(int timeInterval) { if (timeInterval < 0 || timeInterval > timesteps.size()) { return null; } return ((TimeStep)timesteps.elementAt(timeInterval)); } /** *Returns the experimental condition mean for the property referenced by index *the passed index at a given timestep. For example, getMean(2, 3) returns the mean of the fourth property *(index 3 since the first property is index 0) of all the participants at the third (index 0) timestep in the experimental *condition. * *@param timestepindex The index corresponding to the timestep to retrieve the mean of all participants of. *@param propertyindex The index corresponding to the property to retrieve the mean of all participants of. */ public double getMean(int timestepIndex, int propertyIndex) throws TrialException { if (timestepIndex < 0 || timestepIndex >= timesteps.size()) { throw new TrialException("timestep index out of range. Timestep input: " + timestepIndex + ", Number of timesteps: " + timesteps.size()); } if (propertyIndex < 0 || propertyIndex >= propertyDescriptions.length) { throw new TrialException("property index out of range. Property index input: " + propertyIndex + ", Number of properties: " + propertyDescriptions.length); } return ((TimeStep)timesteps.elementAt(timestepIndex)).getMean(propertyIndex); } /** *Returns the maximum mean for all timesteps in the experimental condition for setting limits on graph */ public double getMaxMean(int propertyIndex) { if (timesteps.size() == 0) return 0.0; double max = -1E20; for (int i=0; i