Search in sources :

Example 1 with VariableWeights

use of cbit.vcell.opt.VariableWeights in project vcell by virtualcell.

the class OptXmlWriter method getDataXML.

public static Element getDataXML(ReferenceData refData) {
    Element refDataElement = null;
    if (refData instanceof SimpleReferenceData) {
        refDataElement = new Element(OptXmlTags.SimpleReferenceData_Tag);
    } else if (refData instanceof SpatialReferenceData) {
        refDataElement = new Element(OptXmlTags.SpatialReferenceData_Tag);
    }
    if (refDataElement != null) {
        // write variable declarations
        // independent variable is t and dimension is 1, these are fixed.
        Element timeVarElement = new Element(OptXmlTags.Variable_Tag);
        timeVarElement.setAttribute(OptXmlTags.VariableType_Attr, OptXmlTags.VariableType_Attr_Independent);
        timeVarElement.setAttribute(OptXmlTags.VariableName_Attr, ReservedVariable.TIME.getName());
        timeVarElement.setAttribute(OptXmlTags.VariableDimension_Attr, "1");
        refDataElement.addContent(timeVarElement);
        // check if t is at the first column
        int timeIndex = refData.findColumn(ReservedVariable.TIME.getName());
        if (timeIndex != 0) {
            throw new RuntimeException("t must be the first column");
        }
        // add all other dependent variables, recall that the dependent variables start from 2nd column onward in reference data
        for (int i = 1; i < refData.getNumDataColumns(); i++) {
            Element variableElement = new Element(OptXmlTags.Variable_Tag);
            variableElement.setAttribute(OptXmlTags.VariableType_Attr, OptXmlTags.VariableType_Attr_Dependent);
            variableElement.setAttribute(OptXmlTags.VariableName_Attr, refData.getColumnNames()[i]);
            variableElement.setAttribute(OptXmlTags.VariableDimension_Attr, refData.getDataSize() + "");
            refDataElement.addContent(variableElement);
        }
        // write data
        Element dataRowElement = new Element(OptXmlTags.Datarow_Tag);
        for (int i = 0; i < refData.getNumDataRows(); i++) {
            double[] data = refData.getDataByRow(i);
            Element rowElement = new Element(OptXmlTags.Row_Tag);
            StringBuffer rowText = new StringBuffer();
            for (int j = 0; j < data.length; j++) {
                rowText.append(data[j] + " ");
            }
            rowElement.addContent(rowText.toString());
            dataRowElement.addContent(rowElement);
        }
        refDataElement.addContent(dataRowElement);
        // write weights
        Element weightRowElement = new Element(OptXmlTags.WeightDatarow_Tag);
        // add weight type
        if (// print weights in multiple rows, each row has one-to-more elements
        refData.getWeights() instanceof ElementWeights) {
            weightRowElement.setAttribute(OptXmlTags.WeightType_Attr, OptXmlTags.WeightType_Attr_Element);
            double[][] weightData = ((ElementWeights) refData.getWeights()).getWeightData();
            // elementWeights: first dimensin is number of rows, second dimension is number of vars
            for (int i = 0; i < weightData.length; i++) {
                double[] rowData = weightData[i];
                Element rowElement = new Element(OptXmlTags.Row_Tag);
                StringBuffer rowText = new StringBuffer();
                for (int j = 0; j < rowData.length; j++) {
                    rowText.append(rowData[j] + " ");
                }
                rowElement.addContent(rowText.toString());
                weightRowElement.addContent(rowElement);
            }
        } else if (// print weights in one row, the row has one-to-more elements
        refData.getWeights() instanceof VariableWeights) {
            weightRowElement.setAttribute(OptXmlTags.WeightType_Attr, OptXmlTags.WeightType_Attr_Variable);
            double[] weightData = ((VariableWeights) refData.getWeights()).getWeightData();
            Element rowElement = new Element(OptXmlTags.Row_Tag);
            StringBuffer rowText = new StringBuffer();
            for (int j = 0; j < weightData.length; j++) {
                rowText.append(weightData[j] + " ");
            }
            rowElement.addContent(rowText.toString());
            weightRowElement.addContent(rowElement);
        } else // time weights. Print weights in multiple rows, each row has one element
        {
            weightRowElement.setAttribute(OptXmlTags.WeightType_Attr, OptXmlTags.WeightType_Attr_Time);
            double[] weightData = ((TimeWeights) refData.getWeights()).getWeightData();
            for (int j = 0; j < weightData.length; j++) {
                Element rowElement = new Element(OptXmlTags.Row_Tag);
                StringBuffer rowText = new StringBuffer();
                rowText.append(weightData[j] + " ");
                rowElement.addContent(rowText.toString());
                weightRowElement.addContent(rowElement);
            }
        }
        refDataElement.addContent(weightRowElement);
    }
    return refDataElement;
}
Also used : VariableWeights(cbit.vcell.opt.VariableWeights) ElementWeights(cbit.vcell.opt.ElementWeights) Element(org.jdom.Element) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData) SpatialReferenceData(cbit.vcell.opt.SpatialReferenceData) Constraint(cbit.vcell.opt.Constraint)

Example 2 with VariableWeights

use of cbit.vcell.opt.VariableWeights in project vcell by virtualcell.

the class CurveFitting method solve.

// legacy method, which taks one explicit function to fit one reference data column (colIndex = 1, index 0 is time column)
// if weights is null, set the only one dependent variable weight is 1
public static OptimizationResultSet solve(Expression modelExp, Parameter[] parameters, double[] time, double[] data, Weights weights) throws ExpressionException, OptimizationException, IOException {
    // one fit function and data pair
    ExplicitFitObjectiveFunction.ExpressionDataPair[] expDataPairs = new ExplicitFitObjectiveFunction.ExpressionDataPair[1];
    expDataPairs[0] = new ExplicitFitObjectiveFunction.ExpressionDataPair(modelExp, 1);
    // one column of reference data in two dimensional array
    double[][] refData = new double[1][];
    refData[0] = data;
    // column names
    String[] colNames = new String[] { ReservedVariable.TIME.getName(), "intensity" };
    // weights
    Weights dataWeights = weights;
    if (dataWeights == null) {
        dataWeights = new VariableWeights(new double[] { 1.0 });
    }
    return CurveFitting.solve(expDataPairs, parameters, time, refData, colNames, dataWeights);
}
Also used : VariableWeights(cbit.vcell.opt.VariableWeights) Weights(cbit.vcell.opt.Weights) VariableWeights(cbit.vcell.opt.VariableWeights) ExplicitFitObjectiveFunction(cbit.vcell.opt.ExplicitFitObjectiveFunction)

Aggregations

VariableWeights (cbit.vcell.opt.VariableWeights)2 Constraint (cbit.vcell.opt.Constraint)1 ElementWeights (cbit.vcell.opt.ElementWeights)1 ExplicitFitObjectiveFunction (cbit.vcell.opt.ExplicitFitObjectiveFunction)1 SimpleReferenceData (cbit.vcell.opt.SimpleReferenceData)1 SpatialReferenceData (cbit.vcell.opt.SpatialReferenceData)1 Weights (cbit.vcell.opt.Weights)1 Element (org.jdom.Element)1