Search in sources :

Example 1 with MultiTable

use of org.simulator.math.odes.MultiTable in project synthea by synthetichealth.

the class PhysiologySimulator method run.

/**
 * Solves the model at each time step for the specified duration using the provided inputs
 * as initial parameters. Provides the results as a map of value lists where each key is
 * a model parameter. In addition to the model parameters is a "Time" field which provides
 * a list of all simulated time points.
 *
 * <p>Note that this method will throw a DerivativeException if the model encounters an error
 * while attempting to solve the system.
 * @param inputs Map of model parameter inputs. For any parameters which are not provided
 *               the default value from the model will be used. If null, all default
 *               parameter values will be used.
 * @return map of parameter names to value lists
 * @throws DerivativeException Exception if the solver encounters errors while computing the
 *        solution to differential equations
 */
public MultiTable run(Map<String, Double> inputs) throws DerivativeException {
    try {
        // Reinitialize the interpreter to prevent old values from affecting the new simulation
        interpreter.init(true);
    } catch (ModelOverdeterminedException | SBMLException ex) {
        // at least once
        throw new RuntimeException(ex);
    }
    // Create a copy of the default parameters to use
    double[] params = Arrays.copyOf(modelDefaults, modelDefaults.length);
    // Overwrite model defaults with the provided input parameters, if present
    if (inputs != null) {
        for (int i = 0; i < modelFields.length; i++) {
            String field = modelFields[i];
            if (inputs.containsKey(field)) {
                params[i] = inputs.get(field);
            }
        }
    }
    // Solve the ODE for the specified duration and return the results
    MultiTable results = solver.solve(interpreter, params, 0, simDuration);
    return results;
}
Also used : SBMLException(org.sbml.jsbml.SBMLException) ModelOverdeterminedException(org.sbml.jsbml.validator.ModelOverdeterminedException) MultiTable(org.simulator.math.odes.MultiTable)

Example 2 with MultiTable

use of org.simulator.math.odes.MultiTable in project synthea by synthetichealth.

the class SimRunner method execute.

/**
 * Executes the simulation if any input values are beyond the variance threshold.
 * @param time simulation time
 */
public void execute(long time) {
    // Copy our input parameters for future threshold checks
    prevInputs = new HashMap<String, Double>(modelInputs);
    MultiTable results = runSim(time, modelInputs);
    firstExecution = true;
    // Set all of the results
    for (IoMapper mapper : config.getOutputs()) {
        switch(mapper.getType()) {
            default:
            case ATTRIBUTE:
                person.attributes.put(mapper.getTo(), mapper.getOutputResult(results, config.getLeadTime()));
                break;
            case VITAL_SIGN:
                VitalSign vs = mapper.getVitalSignTarget();
                Object result = mapper.getOutputResult(results, config.getLeadTime());
                if (result instanceof List) {
                    throw new IllegalArgumentException("Mapping lists to VitalSigns is currently unsupported. " + "Cannot map list to VitalSign \"" + mapper.getTo() + "\".");
                }
                vitalSignResults.put(vs, (double) result);
                break;
        }
    }
}
Also used : VitalSign(org.mitre.synthea.world.concepts.VitalSign) List(java.util.List) MultiTable(org.simulator.math.odes.MultiTable)

Example 3 with MultiTable

use of org.simulator.math.odes.MultiTable in project synthea by synthetichealth.

the class PhysiologySimulatorTest method testCvsSimulation.

@Test
public void testCvsSimulation() {
    try {
        PhysiologySimulator physio = new PhysiologySimulator("circulation/Smith2004_CVS_human.xml", "runge_kutta", 0.01, 4);
        // Ensure we can get parameters. Check a couple
        List<String> params = physio.getParameters();
        assertTrue("V_lv", params.contains("V_lv"));
        assertTrue("P_rv", params.contains("P_rv"));
        assertTrue("period", params.contains("period"));
        assertTrue("P_ao", params.contains("P_ao"));
        // First run with all default parameters
        MultiTable results = physio.run(new HashMap<String, Double>());
        // Row 200 should be about 2 minutes into the simulation, which is where
        // we want to start capturing results
        List<Double> pao = new ArrayList<Double>();
        Column paoCol = results.getColumn("P_ao");
        for (int i = 200; i < paoCol.getRowCount(); i++) {
            pao.add(paoCol.getValue(i));
        }
        Double sys = Collections.max(pao);
        Double dia = Collections.min(pao);
        System.out.println("sys: " + sys);
        System.out.println("dia: " + dia);
        assertTrue("sys > 100", sys > 100);
        assertTrue("sys < 120", sys < 120);
        assertTrue("dia > 60", dia > 60);
        assertTrue("dia < 80", dia < 80);
        Map<String, Double> inputs = new HashMap<String, Double>();
        inputs.put("R_sys", 2.0);
        // Run with some inputs
        results = physio.run(inputs);
        pao = Lists.newArrayList(results.getColumn("P_ao"));
        sys = Collections.max(pao);
        dia = Collections.min(pao);
        assertEquals(results.getColumn("R_sys").getValue(0), 2.0, 0.0001);
        // Check that levels have appropriately changed
        assertTrue("sys > 120", sys > 120);
        assertTrue("sys < 150", sys < 150);
        assertTrue("dia > 80", dia > 80);
        assertTrue("dia < 100", dia < 100);
    } catch (DerivativeException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Column(org.simulator.math.odes.MultiTable.Block.Column) MultiTable(org.simulator.math.odes.MultiTable) DerivativeException(org.apache.commons.math.ode.DerivativeException) Test(org.junit.Test)

Example 4 with MultiTable

use of org.simulator.math.odes.MultiTable in project synthea by synthetichealth.

the class ChartRendererTest method testRenderFileFromMultiTable.

@Test
public void testRenderFileFromMultiTable() throws Exception {
    tmpFolder.create();
    Path tmpFilePath = Paths.get(tmpFolder.getRoot().getAbsolutePath(), "tmp.png");
    MultiTableChartConfig chartCfg = new MultiTableChartConfig();
    chartCfg.setAxisLabelX("Time");
    chartCfg.setAxisLabelY("Test1");
    chartCfg.setAxisParamX("time");
    chartCfg.setFilename(tmpFilePath.toString());
    chartCfg.setTitle("Test Image");
    chartCfg.setType("line");
    MultiTableSeriesConfig seriesCfg = new MultiTableSeriesConfig();
    seriesCfg.setParam("test1");
    ArrayList<MultiTableSeriesConfig> seriesList = new ArrayList<MultiTableSeriesConfig>();
    seriesList.add(seriesCfg);
    chartCfg.setSeries(seriesList);
    MultiTable table = getMockTable();
    ChartRenderer.drawChartAsFile(table, chartCfg);
    // Verify that the image file was created
    File imgFile = tmpFilePath.toFile();
    assertTrue("image file created", imgFile.exists());
    assertTrue("image file not empty", imgFile.length() > 0);
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) MultiTableChartConfig(org.mitre.synthea.helpers.ChartRenderer.MultiTableChartConfig) MultiTableSeriesConfig(org.mitre.synthea.helpers.ChartRenderer.MultiTableSeriesConfig) MultiTable(org.simulator.math.odes.MultiTable) File(java.io.File) Test(org.junit.Test)

Example 5 with MultiTable

use of org.simulator.math.odes.MultiTable in project synthea by synthetichealth.

the class ChartRendererTest method testRenderBase64FromMultiTable.

@Test
public void testRenderBase64FromMultiTable() throws Exception {
    MultiTableChartConfig chartCfg = new MultiTableChartConfig();
    chartCfg.setAxisParamX("test2");
    chartCfg.setAxisHiddenX(true);
    chartCfg.setAxisHiddenY(true);
    chartCfg.setType("scatter");
    MultiTableSeriesConfig seriesCfg = new MultiTableSeriesConfig();
    seriesCfg.setParam("test1");
    seriesCfg.setLabel("Test 1");
    MultiTableSeriesConfig seriesCfg2 = new MultiTableSeriesConfig();
    seriesCfg2.setParam("test2");
    seriesCfg2.setLabel("Test 2");
    ArrayList<MultiTableSeriesConfig> seriesList = new ArrayList<MultiTableSeriesConfig>();
    seriesList.add(seriesCfg);
    seriesList.add(seriesCfg2);
    chartCfg.setSeries(seriesList);
    MultiTable table = getMockTable();
    String b64 = ChartRenderer.drawChartAsBase64(table, chartCfg).getEncodedBytes();
    assertTrue(Base64.isBase64(b64));
}
Also used : ArrayList(java.util.ArrayList) MultiTableChartConfig(org.mitre.synthea.helpers.ChartRenderer.MultiTableChartConfig) MultiTableSeriesConfig(org.mitre.synthea.helpers.ChartRenderer.MultiTableSeriesConfig) MultiTable(org.simulator.math.odes.MultiTable) Test(org.junit.Test)

Aggregations

MultiTable (org.simulator.math.odes.MultiTable)7 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 MultiTableChartConfig (org.mitre.synthea.helpers.ChartRenderer.MultiTableChartConfig)3 File (java.io.File)2 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 DerivativeException (org.apache.commons.math.ode.DerivativeException)2 MultiTableSeriesConfig (org.mitre.synthea.helpers.ChartRenderer.MultiTableSeriesConfig)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 List (java.util.List)1 TimeSeriesData (org.mitre.synthea.helpers.TimeSeriesData)1 VitalSign (org.mitre.synthea.world.concepts.VitalSign)1 SBMLException (org.sbml.jsbml.SBMLException)1 ModelOverdeterminedException (org.sbml.jsbml.validator.ModelOverdeterminedException)1 Column (org.simulator.math.odes.MultiTable.Block.Column)1 TypeDescription (org.yaml.snakeyaml.TypeDescription)1 Yaml (org.yaml.snakeyaml.Yaml)1