Search in sources :

Example 96 with Expression

use of cbit.vcell.parser.Expression in project vcell by virtualcell.

the class FRAPStudy method getROIDataGenerator.

public ROIDataGenerator getROIDataGenerator(LocalWorkspace localWorkspace) {
    // create ROI image
    short[] roiFieldData = null;
    ROI[] rois = getFrapData().getRois();
    if (rois.length > 0) {
        Extent extent = rois[0].getRoiImages()[0].getExtent();
        ISize isize = rois[0].getISize();
        int numROIX = rois[0].getISize().getX();
        int numROIY = rois[0].getISize().getY();
        roiFieldData = new short[numROIX * numROIY];
        short regionCounter = 1;
        for (int roiIdx = FRAPData.VFRAP_ROI_ENUM.ROI_BLEACHED_RING1.ordinal(); roiIdx < rois.length; roiIdx++) {
            short[] roiImg = rois[roiIdx].getPixelsXYZ();
            for (int pixelIdx = 0; pixelIdx < (numROIX * numROIY); pixelIdx++) {
                if (roiImg[pixelIdx] > 0) {
                    roiFieldData[pixelIdx] = regionCounter;
                }
            }
            regionCounter++;
        }
        // create field data
        int NumTimePoints = 1;
        // 8 rois integrated into 1 image
        int NumChannels = 1;
        short[][][] pixData = new short[NumTimePoints][NumChannels][];
        pixData[0][0] = roiFieldData;
        // get extental data id
        ExternalDataIdentifier newROIExtDataID = FRAPStudy.createNewExternalDataInfo(localWorkspace, FRAPStudy.ROI_SUMDATA_NAME).getExternalDataIdentifier();
        CartesianMesh cartesianMesh;
        try {
            cartesianMesh = getCartesianMesh();
            Origin origin = new Origin(0, 0, 0);
            FieldDataFileOperationSpec fdos = new FieldDataFileOperationSpec();
            fdos.opType = FieldDataFileOperationSpec.FDOS_ADD;
            fdos.cartesianMesh = cartesianMesh;
            fdos.shortSpecData = pixData;
            fdos.specEDI = newROIExtDataID;
            fdos.varNames = new String[] { "roiSumDataVar" };
            fdos.owner = LocalWorkspace.getDefaultOwner();
            fdos.times = new double[] { 0.0 };
            fdos.variableTypes = new VariableType[] { VariableType.VOLUME };
            fdos.origin = origin;
            fdos.extent = extent;
            fdos.isize = isize;
            localWorkspace.getDataSetControllerImpl().fieldDataFileOperation(fdos);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return new ROIDataGenerator(ROI_EXTDATA_NAME, /*name*/
        new int[] { 0 }, /* volumePoints*/
        new int[0], /* membranePoints*/
        regionCounter, /*numRegions*/
        0, /*zSlice*/
        newROIExtDataID.getKey(), /* fieldDataKey, sample image*/
        new FieldFunctionArguments(FRAPStudy.ROI_SUMDATA_NAME, "roiSumDataVar", new Expression(0), VariableType.VOLUME), /*FieldFunctionArguments, sample image*/
        false);
    }
    return null;
}
Also used : Origin(org.vcell.util.Origin) Extent(org.vcell.util.Extent) FieldFunctionArguments(cbit.vcell.field.FieldFunctionArguments) ISize(org.vcell.util.ISize) FieldDataFileOperationSpec(cbit.vcell.field.io.FieldDataFileOperationSpec) ROI(cbit.vcell.VirtualMicroscopy.ROI) ImageException(cbit.image.ImageException) UserCancelException(org.vcell.util.UserCancelException) CartesianMesh(cbit.vcell.solvers.CartesianMesh) Expression(cbit.vcell.parser.Expression) ExternalDataIdentifier(org.vcell.util.document.ExternalDataIdentifier)

Example 97 with Expression

use of cbit.vcell.parser.Expression in project vcell by virtualcell.

the class ROIDataGenerator method getSampleImageFieldData.

public FieldDataIdentifierSpec getSampleImageFieldData(User user) {
    // key
    String key = fieldDataKey.toString();
    String fieldInput = fieldFuncArguments.infix();
    StringTokenizer st = null;
    int index = fieldInput.indexOf(FieldFunctionDefinition.FUNCTION_name);
    if (index >= 0) {
        st = new StringTokenizer(fieldInput.substring(index), "\n");
        if (st.hasMoreTokens()) {
            String fieldFunction = st.nextToken();
            try {
                Expression exp = new Expression(fieldFunction);
                FieldFunctionArguments[] ffa = FieldUtilities.getFieldFunctionArguments(exp);
                return new FieldDataIdentifierSpec(ffa[0], new ExternalDataIdentifier(KeyValue.fromString(key), user, ffa[0].getFieldName()));
            } catch (ExpressionException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("Failed to load data processing script.");
            }
        }
    }
    return null;
}
Also used : StringTokenizer(java.util.StringTokenizer) Expression(cbit.vcell.parser.Expression) FieldFunctionArguments(cbit.vcell.field.FieldFunctionArguments) FieldDataIdentifierSpec(cbit.vcell.field.FieldDataIdentifierSpec) ExternalDataIdentifier(org.vcell.util.document.ExternalDataIdentifier) ExpressionException(cbit.vcell.parser.ExpressionException) DataAccessException(org.vcell.util.DataAccessException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException)

Example 98 with Expression

use of cbit.vcell.parser.Expression in project vcell by virtualcell.

the class ReactionDominantTest method solve.

private OptimizationResultSet solve() throws ExpressionException, IOException {
    Expression Fbleached_bleachFastExp = new Expression(Fbleached_bleachFast);
    Expression OmegaExp = new Expression(strOmega);
    Expression Omega_cExp = new Expression(strOmega_c);
    Expression WExp = new Expression(strW);
    Fbleached_bleachFastExp.substituteInPlace(OmegaExp, new Expression(Omega));
    Fbleached_bleachFastExp.substituteInPlace(Omega_cExp, new Expression(Omega_c));
    Fbleached_bleachFastExp.substituteInPlace(WExp, new Expression(W));
    Parameter[] parameters = new Parameter[] { para_alpha, para_Kon_star, para_Koff };
    // Expression Fbleached_bleachFast = Fbleached_bleachFastExp.flatten();
    // choose optimization solver, currently we have Powell and CFSQP
    PowellOptimizationSolver optService = new PowellOptimizationSolver();
    OptimizationSpec optSpec = new OptimizationSpec();
    // create simple reference data
    double[][] realData = new double[2][t.length];
    for (int i = 0; i < t.length; i++) {
        realData[0][i] = t[i];
        realData[1][i] = data_bleached[i];
    }
    String[] colNames = new String[] { "t", "intensity" };
    double[] weights = new double[] { 1.0, 1.0 };
    SimpleReferenceData refData = new SimpleReferenceData(colNames, weights, realData);
    // send to optimization service
    ExplicitFitObjectiveFunction.ExpressionDataPair oneExpDataPair = new ExplicitFitObjectiveFunction.ExpressionDataPair(Fbleached_bleachFastExp.flatten(), 1);
    ExplicitFitObjectiveFunction.ExpressionDataPair[] expDataPairs = new ExplicitFitObjectiveFunction.ExpressionDataPair[] { oneExpDataPair };
    optSpec.setObjectiveFunction(new ExplicitFitObjectiveFunction(expDataPairs, refData));
    optSpec.setComputeProfileDistributions(true);
    // get the initial guess to send it to the f() function. ....
    for (int i = 0; i < parameters.length; i++) {
        optSpec.addParameter(parameters[i]);
    }
    // Parameters in OptimizationSolverSpec are solver type and objective function change tolerance.
    OptimizationSolverSpec optSolverSpec = new OptimizationSolverSpec(OptimizationSolverSpec.SOLVERTYPE_POWELL, 0.000001);
    OptSolverCallbacks optSolverCallbacks = new DefaultOptSolverCallbacks();
    OptimizationResultSet optResultSet = null;
    optResultSet = optService.solve(optSpec, optSolverSpec, optSolverCallbacks);
    String[] paramNames = optResultSet.getOptSolverResultSet().getParameterNames();
    ArrayList<ProfileDistribution> profileDistributionList = optResultSet.getOptSolverResultSet().getProfileDistributionList();
    for (int i = 0; i < profileDistributionList.size(); i++) {
        outputProfileLikelihood(profileDistributionList.get(i), i, paramNames[i], new File(fileDir));
    }
    return optResultSet;
}
Also used : PowellOptimizationSolver(cbit.vcell.opt.solvers.PowellOptimizationSolver) ProfileDistribution(cbit.vcell.opt.OptSolverResultSet.ProfileDistribution) DefaultOptSolverCallbacks(org.vcell.optimization.DefaultOptSolverCallbacks) OptSolverCallbacks(org.vcell.optimization.OptSolverCallbacks) OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) ExplicitFitObjectiveFunction(cbit.vcell.opt.ExplicitFitObjectiveFunction) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData) Expression(cbit.vcell.parser.Expression) DefaultOptSolverCallbacks(org.vcell.optimization.DefaultOptSolverCallbacks) Parameter(cbit.vcell.opt.Parameter) OptimizationSolverSpec(cbit.vcell.opt.OptimizationSolverSpec) OptimizationSpec(cbit.vcell.opt.OptimizationSpec) File(java.io.File)

Example 99 with Expression

use of cbit.vcell.parser.Expression in project vcell by virtualcell.

the class AbstractMathMapping method getFluxCorrectionParameter.

/**
 * This method was created in VisualAge.
 * @return Expression
 */
public KFluxParameter getFluxCorrectionParameter(StructureMapping sourceStructureMapping, StructureMapping targetStructureMapping) throws MappingException, ExpressionException {
    for (int i = 0; i < fieldMathMappingParameters.length; i++) {
        if (fieldMathMappingParameters[i] instanceof KFluxParameter) {
            KFluxParameter kfluxParameter = (KFluxParameter) fieldMathMappingParameters[i];
            if (kfluxParameter.getSourceStructureMapping() == sourceStructureMapping && kfluxParameter.getTargetStructureMapping() == targetStructureMapping) {
                return kfluxParameter;
            }
        }
    }
    // 
    // not found, add new parameter
    // 
    String sourceName = sourceStructureMapping.getStructure().getNameScope().getName();
    String targetName = targetStructureMapping.getStructure().getNameScope().getName();
    Parameter sourceSizeParameter = null;
    Parameter targetSizeParameter = null;
    if (sourceStructureMapping.getGeometryClass() instanceof CompartmentSubVolume) {
        sourceSizeParameter = sourceStructureMapping.getSizeParameter();
    } else {
        sourceSizeParameter = sourceStructureMapping.getUnitSizeParameter();
    }
    if (targetStructureMapping.getGeometryClass() instanceof CompartmentSubVolume) {
        targetSizeParameter = targetStructureMapping.getSizeParameter();
        if (targetSizeParameter == null || targetSizeParameter.getExpression() == null) {
            throw new MappingException("structure mapping sizes not set for application " + simContext.getName());
        }
    } else {
        targetSizeParameter = targetStructureMapping.getUnitSizeParameter();
    }
    Expression fluxCorrectionExp = Expression.div(new Expression(sourceSizeParameter, simContext.getNameScope()), new Expression(targetSizeParameter, simContext.getNameScope()));
    VCUnitDefinition sourceSizeUnit = sourceSizeParameter.getUnitDefinition();
    VCUnitDefinition targetSizeUnit = targetSizeParameter.getUnitDefinition();
    VCUnitDefinition unit = sourceSizeUnit.divideBy(targetSizeUnit);
    fluxCorrectionExp.bindExpression(this);
    String parameterName = PARAMETER_K_FLUX_PREFIX + sourceName + "_" + targetName;
    KFluxParameter kFluxParameter = new KFluxParameter(parameterName, fluxCorrectionExp, unit, sourceStructureMapping, targetStructureMapping);
    MathMappingParameter[] newMathMappingParameters = (MathMappingParameter[]) BeanUtils.addElement(this.fieldMathMappingParameters, kFluxParameter);
    try {
        setMathMapppingParameters(newMathMappingParameters);
    } catch (java.beans.PropertyVetoException e) {
        e.printStackTrace(System.out);
        throw new RuntimeException(e.getMessage());
    }
    return kFluxParameter;
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) CompartmentSubVolume(cbit.vcell.geometry.CompartmentSubVolume) Expression(cbit.vcell.parser.Expression) ProxyParameter(cbit.vcell.model.ProxyParameter) Parameter(cbit.vcell.model.Parameter) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) ModelParameter(cbit.vcell.model.Model.ModelParameter)

Example 100 with Expression

use of cbit.vcell.parser.Expression in project vcell by virtualcell.

the class AbstractMathMapping method substituteGlobalParameters.

private Expression substituteGlobalParameters(Expression exp) throws ExpressionException {
    Expression exp2 = new Expression(exp);
    // 
    // do until no more globals to substitute
    // 
    int count = 0;
    ModelParameter[] modelParams = simContext.getModel().getModelParameters();
    while (true) {
        if (count++ > 30) {
            throw new ExpressionBindingException("infinite loop in eliminating function nesting");
        }
        // 
        // get all symbols (identifiers), make list of globals used
        // 
        String[] symbols = exp2.getSymbols();
        Vector<ModelParameter> globalsVector = new Vector<ModelParameter>();
        if (symbols != null) {
            for (int i = 0; i < symbols.length; i++) {
                for (int j = 0; j < modelParams.length; j++) {
                    if (symbols[i].equals(modelParams[j].getName())) {
                        globalsVector.addElement(modelParams[j]);
                    }
                }
            }
        }
        // 
        if (globalsVector.size() == 0) {
            break;
        }
        // 
        for (int i = 0; i < globalsVector.size(); i++) {
            ModelParameter mp = globalsVector.elementAt(i);
            Expression mpExp = new Expression(mp.getName());
            exp2.substituteInPlace(mpExp, mp.getExpression());
        }
    }
    // exp2.bindExpression(simContext.getModel());
    return exp2;
}
Also used : ModelParameter(cbit.vcell.model.Model.ModelParameter) Expression(cbit.vcell.parser.Expression) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) Vector(java.util.Vector)

Aggregations

Expression (cbit.vcell.parser.Expression)549 ExpressionException (cbit.vcell.parser.ExpressionException)163 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)76 PropertyVetoException (java.beans.PropertyVetoException)73 Variable (cbit.vcell.math.Variable)69 ArrayList (java.util.ArrayList)58 Vector (java.util.Vector)56 MathException (cbit.vcell.math.MathException)55 VolVariable (cbit.vcell.math.VolVariable)53 SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)51 SpeciesContext (cbit.vcell.model.SpeciesContext)50 Element (org.jdom.Element)47 KineticsParameter (cbit.vcell.model.Kinetics.KineticsParameter)45 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)45 Model (cbit.vcell.model.Model)43 Function (cbit.vcell.math.Function)42 Constant (cbit.vcell.math.Constant)41 ModelParameter (cbit.vcell.model.Model.ModelParameter)41 ModelUnitSystem (cbit.vcell.model.ModelUnitSystem)41 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)38