Skip to content

Commit

Permalink
refactoring post processing input, unit test for primary statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
danv61 committed Feb 19, 2025
1 parent 39cba82 commit b652941
Show file tree
Hide file tree
Showing 8 changed files with 442 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import cbit.vcell.simdata.*;
import cbit.vcell.solver.*;
import cbit.vcell.solver.ode.ODESimData;
import cbit.vcell.solver.ode.ODESolverResultSet;
import cbit.vcell.solver.server.*;
import cbit.vcell.util.ColumnDescription;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -236,18 +237,18 @@ public void postProcessLangevinResults(Simulation sim) {
AsynchClientTask postProcessLangevinResultsTask = new AsynchClientTask("PostProcessLangevinResultsTask", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {

Map<Integer, ODEDataManager> odeDataManagerMap = new LinkedHashMap<>();
Map<Integer, ODESolverResultSet> odeSolverResultSetMap = new LinkedHashMap<>();
try {
for (int trialIndex = 0; trialIndex < numTrials; trialIndex++) {
VCSimulationDataIdentifier vcSimulationDataIdentifier = new VCSimulationDataIdentifier(vcSimulationIdentifier, trialIndex);
ODEDataManager dm = (ODEDataManager) getDocumentWindowManager().getRequestManager().getDataManager(null, vcSimulationDataIdentifier, false);
odeDataManagerMap.put(trialIndex, dm);
odeSolverResultSetMap.put(trialIndex, dm.getODESolverResultSet());
}
} catch(DataAccessException dae) {
System.out.println("ClientSimManager.postProcessLangevinResults() DataAccessException: " + dae.getMessage());
hashTable.put(LangevinPostProcessor.FAILURE_KEY, true);
}
if(odeDataManagerMap.isEmpty()) {
if(odeSolverResultSetMap.isEmpty()) {
System.out.println("ClientSimManager: no data");
hashTable.put(LangevinPostProcessor.FAILURE_KEY, true);
}
Expand All @@ -256,7 +257,7 @@ public void run(Hashtable<String, Object> hashTable) throws Exception {
SimulationOwner simOwner = (SimulationOwner)hashTable.get(LangevinPostProcessor.SIMULATION_OWNER_KEY);
LangevinPostProcessorInput lppInput = new LangevinPostProcessorInput(sim, simOwner);
lppInput.setFailed((boolean)hashTable.get(LangevinPostProcessor.FAILURE_KEY));
lppInput.setOdeDataManagerMap(odeDataManagerMap);
lppInput.setOdeSolverResultSetMap(odeSolverResultSetMap);

LangevinPostProcessor lpp = new LangevinPostProcessor();
LangevinPostProcessorOutput lppOutput = lpp.postProcessLangevinResults(lppInput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class LangevinPostProcessor {
boolean failure;
Simulation sim;
SimulationOwner simOwner;
Map<Integer, ODEDataManager> odeDataManagerMap;
Map<Integer, ODESolverResultSet> odeSolverResultSetMap;

// the results
RowColumnResultSet averagesResultSet;
Expand All @@ -37,39 +37,35 @@ public LangevinPostProcessorOutput postProcessLangevinResults(LangevinPostProces

sim = lppInput.getSimulation();
simOwner = lppInput.getSimulationOwner();
LangevinPostProcessorOutput pllOut = new LangevinPostProcessorOutput(sim, simOwner);

odeSolverResultSetMap = lppInput.getOdeSolverResultSetMap(); // key = trial index, value = simulation results (ODESolverResultSet object) for that trial
failure = lppInput.isFailed();
SolverTaskDescription std = sim.getSolverTaskDescription();
int numTrials = std.getNumTrials();
isMultiTrial = numTrials > 1 ? true : false;
isMultiTrial = odeSolverResultSetMap.size() > 1 ? true : false;

LangevinPostProcessorOutput pllOut = new LangevinPostProcessorOutput(sim, simOwner);
if(failure) {
pllOut.setFailed(failure);
pllOut.setMultiTrial(isMultiTrial);
return pllOut;
}

odeDataManagerMap = lppInput.getOdeDataManagerMap(); // key = trial index, value = simulation results (ODESimData object) for that trial

// probably useless at this point
SimulationInfo simInfo = sim.getSimulationInfo();
VCSimulationIdentifier vcSimulationIdentifier = simInfo.getAuthoritativeVCSimulationIdentifier();
MathOverrides mathOverrides = sim.getMathOverrides();
SimulationVersion simVersion = simInfo.getSimulationVersion();
if(sim != null) {
SimulationInfo simInfo = sim.getSimulationInfo();
VCSimulationIdentifier vcSimulationIdentifier = simInfo.getAuthoritativeVCSimulationIdentifier();
MathOverrides mathOverrides = sim.getMathOverrides();
SimulationVersion simVersion = simInfo.getSimulationVersion();
}

try {
ODEDataManager tempODEDataManager = odeDataManagerMap.get(0);
ODESimData tempODESimData = (ODESimData) tempODEDataManager.getODESolverResultSet();
String format = tempODESimData.getFormatID();
String mathName = tempODESimData.getMathName(); // should be different instances?
ODESolverResultSet tempODESolverResultSet = odeSolverResultSetMap.get(0);

// sanity check: shouldn't be, that only works for non-spatial stochastic where things are done differently
System.out.println("isGibsonMultiTrial: " + tempODEDataManager.getODESolverResultSet().isMultiTrialData());
System.out.println("isGibsonMultiTrial: " + tempODESolverResultSet.isMultiTrialData());

averagesResultSet = RowColumnResultSet.deepCopy(tempODEDataManager.getODESolverResultSet(), RowColumnResultSet.DuplicateMode.ZeroInitialize);
stdResultSet = RowColumnResultSet.deepCopy(tempODEDataManager.getODESolverResultSet(), RowColumnResultSet.DuplicateMode.ZeroInitialize);
minResultSet = RowColumnResultSet.deepCopy(tempODEDataManager.getODESolverResultSet(), RowColumnResultSet.DuplicateMode.CopyValues);
maxResultSet = RowColumnResultSet.deepCopy(tempODEDataManager.getODESolverResultSet(), RowColumnResultSet.DuplicateMode.CopyValues);
averagesResultSet = RowColumnResultSet.deepCopy(tempODESolverResultSet, RowColumnResultSet.DuplicateMode.ZeroInitialize);
stdResultSet = RowColumnResultSet.deepCopy(tempODESolverResultSet, RowColumnResultSet.DuplicateMode.ZeroInitialize);
minResultSet = RowColumnResultSet.deepCopy(tempODESolverResultSet, RowColumnResultSet.DuplicateMode.CopyValues);
maxResultSet = RowColumnResultSet.deepCopy(tempODESolverResultSet, RowColumnResultSet.DuplicateMode.CopyValues);

calculateLangevinPrimaryStatistics(); // averages, standard deviation, min, max
calculateLangevinAdvancedStatistics();
Expand All @@ -90,10 +86,9 @@ public LangevinPostProcessorOutput postProcessLangevinResults(LangevinPostProces

private void calculateLangevinPrimaryStatistics() throws DataAccessException {

int numTrials = odeDataManagerMap.size();
int numTrials = odeSolverResultSetMap.size();
for(int trialIndex = 0; trialIndex < numTrials; trialIndex++) {
ODEDataManager sourceOdm = odeDataManagerMap.get(trialIndex);
ODESolverResultSet sourceOsrs = sourceOdm.getODESolverResultSet();
ODESolverResultSet sourceOsrs = odeSolverResultSetMap.get(trialIndex);
int rowCount = sourceOsrs.getRowCount();
for (int row = 0; row < rowCount; row++) {
double[] sourceRowData = sourceOsrs.getRow(row);
Expand All @@ -119,8 +114,7 @@ private void calculateLangevinPrimaryStatistics() throws DataAccessException {
}

for(int trialIndex = 0; trialIndex < numTrials; trialIndex++) {
ODEDataManager sourceOdm = odeDataManagerMap.get(trialIndex);
ODESolverResultSet sourceOsrs = sourceOdm.getODESolverResultSet();
ODESolverResultSet sourceOsrs = odeSolverResultSetMap.get(trialIndex);
int rowCount = sourceOsrs.getRowCount();
for (int row = 0; row < rowCount; row++) {
double[] sourceRowData = sourceOsrs.getRow(row);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cbit.vcell.simdata;

import cbit.vcell.solver.*;
import cbit.vcell.solver.ode.ODESolverResultSet;

import java.util.*;

Expand All @@ -10,7 +11,7 @@ public class LangevinPostProcessorInput {
private final Simulation sim;
private final SimulationOwner simOwner;

Map<Integer, ODEDataManager> odeDataManagerMap = new LinkedHashMap<>(); // key = trialIndex
Map<Integer, ODESolverResultSet> odeSolverResultSetMap = new LinkedHashMap<>(); // key = trialIndex

private boolean failed = false;

Expand All @@ -26,15 +27,15 @@ public Simulation getSimulation() {
public SimulationOwner getSimulationOwner() {
return simOwner;
}
public Map<Integer, ODEDataManager> getOdeDataManagerMap() {
return odeDataManagerMap;
public Map<Integer, ODESolverResultSet> getOdeSolverResultSetMap() {
return odeSolverResultSetMap;
}
public boolean isFailed() {
return failed;
}

public void setOdeDataManagerMap(Map<Integer, ODEDataManager> odeDataManagerMap) {
this.odeDataManagerMap = odeDataManagerMap;
public void setOdeSolverResultSetMap(Map<Integer, ODESolverResultSet> odeSolverResultSetMap) {
this.odeSolverResultSetMap = odeSolverResultSetMap;
}
public void setFailed(boolean failed) {
this.failed = failed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void test_springsalad_bad_reactions() throws IOException, XmlParseExcepti
simContext.gatherIssues(issueContext, issueList, true); // bIgnoreMathDescription == true
int numErrors = checkIssuesBySeverity(issueList, Issue.Severity.ERROR);
int numWarnings = checkIssuesBySeverity(issueList, Issue.Severity.WARNING);
assertTrue((numErrors == 2 && numWarnings == 14) ? true : false, "expecting 1 errors and 14 warning issues");
assertTrue((numErrors == 2 && numWarnings == 14) ? true : false, "expecting 2 errors and 14 warning issues");
}

/* -------------------------------------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package cbit.vcell.simdata;

import cbit.vcell.math.ODESolverResultSetColumnDescription;
import cbit.vcell.solver.ode.ODESolverResultSet;
import com.google.common.io.Files;
import com.google.common.io.Resources;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.LinkedHashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Tag("Fast")
public class LangevinPostProcessorTest {

// the .IDA files are in vcell-core/src/test/java /cbit/vcell/simdata
static File ida_0_File;
static File ida_1_File;
static File ida_2_File;

@BeforeAll
public static void setUp() throws IOException {
ida_0_File = File.createTempFile("SimID_284673710_0_", ".ida");
Resources.asByteSource(Resources.getResource("cbit/vcell/simdata/SimID_284673710_0_.ida"))
.copyTo(Files.asByteSink(ida_0_File));
ida_1_File = File.createTempFile("SimID_284673710_1_", ".ida");
Resources.asByteSource(Resources.getResource("cbit/vcell/simdata/SimID_284673710_1_.ida"))
.copyTo(Files.asByteSink(ida_1_File));
ida_2_File = File.createTempFile("SimID_284673710_2_", ".ida");
Resources.asByteSource(Resources.getResource("cbit/vcell/simdata/SimID_284673710_2_.ida"))
.copyTo(Files.asByteSink(ida_2_File));
}

@AfterAll
public static void tearDown() {

ida_0_File.delete();
ida_1_File.delete();
ida_2_File.delete();
// if (inputStream != null) {
// inputStream.close();
}

@Test
public void testRead() throws IOException {

// read the input data (3 .IDA files)
ODESolverResultSet osrs_0 = getOdeSolverResultSet(ida_0_File);
ODESolverResultSet osrs_1 = getOdeSolverResultSet(ida_1_File);
ODESolverResultSet osrs_2 = getOdeSolverResultSet(ida_2_File);

Map<Integer, ODESolverResultSet> odeSolverResultSetMap = new LinkedHashMap<>();
odeSolverResultSetMap.put(0, osrs_0);
odeSolverResultSetMap.put(1, osrs_1);
odeSolverResultSetMap.put(2, osrs_2);

LangevinPostProcessorInput lppInput = new LangevinPostProcessorInput(null, null);
lppInput.setFailed(false);
lppInput.setOdeSolverResultSetMap(odeSolverResultSetMap);

// compute primary statistics
LangevinPostProcessor lpp = new LangevinPostProcessor();
LangevinPostProcessorOutput lppOutput = lpp.postProcessLangevinResults(lppInput);

assertFalse(lppOutput.isFailed(), "expected to not fail");
assertTrue(lppOutput.isMultiTrial(), "expected to be multi-trial");

// get some timepoint for some variable
String name = osrs_0.getColumnDescriptions()[7].getName();
double anAverage = lppOutput.getAveragesResultSet().getRow(10)[7]; // TOTAL_MT0__Site1__state0
double aStd = lppOutput.getStdResultSet().getRow(10)[7];
double aMin = lppOutput.getMinResultSet().getRow(10)[7];
double aMax = lppOutput.getMaxResultSet().getRow(10)[7];

// compare to what's expected
assertTrue("TOTAL_MT0__Site1__state0".contentEquals(name), "expecting column name 'TOTAL_MT0__Site1__state0', found: '" + name + "'");
assertTrue(anAverage == 21.0 ? true : false, "expecting 21.0, found " + anAverage);
assertTrue(aStd == 0.816496580927726 ? true : false, "expecting 0.816496580927726, found " + aStd);
assertTrue(aMin == 20.0 ? true : false, "expecting 20.0, found " + aMin);
assertTrue(aMax == 22.0 ? true : false, "expecting 22.0, found " + aMax);
}

//
// ------- IDA file parsing / initializing ODESolverResultSet -----------------------------------------------------
//
private static ODESolverResultSet getOdeSolverResultSet(File idaFile) throws IOException {
ODESolverResultSet odeSolverResultSet = new ODESolverResultSet();
FileInputStream inputStream = null;
inputStream = new FileInputStream(idaFile);
if(readIDA(odeSolverResultSet, inputStream) == null) {
return null;
}
return (odeSolverResultSet);
}
private static ODESolverResultSet readIDA(ODESolverResultSet odeSolverResultSet, FileInputStream inputStream) throws IOException {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
// read header
String line = bufferedReader.readLine();
if (line == null) {
return null;
}
while (line.indexOf(':') > 0) {
String name = line.substring(0, line.indexOf(':'));
odeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription(name));
line = line.substring(line.indexOf(':') + 1);
}
// read data
while ((line = bufferedReader.readLine()) != null) {
line = line + " ";
double[] values = new double[odeSolverResultSet.getDataColumnCount()];
boolean bCompleteRow = true;
for (int i = 0; i < odeSolverResultSet.getDataColumnCount(); i++) {
if (line.indexOf(' ')==-1) { // here and below we assume separator is ' ', in other cases might also be '\t'
bCompleteRow = false;
break;
}else{
String value = line.substring(0, line.indexOf(' ')).trim();
values[i] = Double.valueOf(value).doubleValue();
line = line.substring(line.indexOf(' ') + 1);
}
}
if (bCompleteRow){
odeSolverResultSet.addRow(values);
}else{
break;
}
}
return odeSolverResultSet;
}
}
Loading

0 comments on commit b652941

Please sign in to comment.