Search in sources :

Example 6 with VCImage

use of cbit.image.VCImage in project vcell by virtualcell.

the class SimulationContext method refreshCharacteristicSize.

/**
 * This method was created in VisualAge.
 * @return java.lang.Double
 */
private void refreshCharacteristicSize() throws PropertyVetoException {
    Geometry geo = getGeometryContext().getGeometry();
    int dimension = geo.getDimension();
    if (dimension == 0) {
        setCharacteristicSize(null);
        return;
    }
    Extent extent = geo.getExtent();
    if (characteristicSize == null) {
        // 
        // if characteristicSize is not specified, estimate a 'good' value
        // 
        VCImage image = geo.getGeometrySpec().getImage();
        if (image != null) {
            double pixelSizeX = extent.getX() / image.getNumX();
            double pixelSizeY = extent.getY() / image.getNumY();
            double pixelSizeZ = extent.getZ() / image.getNumZ();
            switch(dimension) {
                case 1:
                    {
                        setCharacteristicSize(new Double(pixelSizeX));
                        break;
                    }
                case 2:
                    {
                        setCharacteristicSize(new Double(Math.min(pixelSizeX, pixelSizeY)));
                        break;
                    }
                case 3:
                    {
                        setCharacteristicSize(new Double(Math.min(pixelSizeX, Math.min(pixelSizeY, pixelSizeZ))));
                        break;
                    }
                default:
                    {
                        throw new RuntimeException("unexpected geometry dimension = '" + dimension + "'");
                    }
            }
        } else {
            // 
            // image doesn't exist, apply heuristics
            // 
            // 
            // 
            // choose spatial steps to give reasonable size mesh ( < MaxNumberElements)
            // 
            // start with very small elements (~0.01 * smallest dimension)
            // 
            // if number of elements exceed 100,000 then take larger steps
            // 
            long MaxNumberElements = 1000000;
            long MaxPerDimension = 500;
            long totalElements = MaxNumberElements + 1;
            double elementLength = Math.min(extent.getX(), Math.min(extent.getY(), extent.getZ())) / (MaxPerDimension);
            while (true) {
                // 
                // calculate total elements based on characteristic length
                // 
                int numY = -1;
                int numZ = -1;
                int numX = (int) Math.round(extent.getX() / elementLength);
                if (dimension > 1) {
                    numY = (int) Math.round(extent.getY() / elementLength);
                } else {
                    numY = 1;
                }
                if (dimension > 2) {
                    numZ = (int) Math.round(extent.getZ() / elementLength);
                } else {
                    numZ = 1;
                }
                totalElements = numX * numY * numZ;
                if ((numX <= MaxPerDimension) && (numY <= MaxPerDimension) && (numZ <= MaxPerDimension) && (totalElements <= MaxNumberElements)) {
                    break;
                } else {
                    elementLength *= 1.125;
                }
            }
            setCharacteristicSize(new Double(elementLength));
        }
    }
}
Also used : Geometry(cbit.vcell.geometry.Geometry) Extent(org.vcell.util.Extent) VCImage(cbit.image.VCImage)

Example 7 with VCImage

use of cbit.image.VCImage in project vcell by virtualcell.

the class FRAPStudy method createNewRefBioModel.

public static BioModel createNewRefBioModel(FRAPStudy sourceFrapStudy, String baseDiffusionRate, TimeStep tStep, KeyValue simKey, User owner, FieldDataIdentifierSpec psfFDIS, int startingIndexForRecovery) throws Exception {
    if (owner == null) {
        throw new Exception("Owner is not defined");
    }
    ROI cellROI_2D = sourceFrapStudy.getFrapData().getRoi(FRAPData.VFRAP_ROI_ENUM.ROI_CELL.name());
    Extent extent = sourceFrapStudy.getFrapData().getImageDataset().getExtent();
    TimeBounds timeBounds = FRAPOptData.getEstimatedRefTimeBound(sourceFrapStudy);
    double timeStepVal = FRAPOptData.REFERENCE_DIFF_DELTAT;
    int numX = cellROI_2D.getRoiImages()[0].getNumX();
    int numY = cellROI_2D.getRoiImages()[0].getNumY();
    int numZ = cellROI_2D.getRoiImages().length;
    short[] shortPixels = cellROI_2D.getRoiImages()[0].getPixels();
    byte[] bytePixels = new byte[numX * numY * numZ];
    final byte EXTRACELLULAR_PIXVAL = 0;
    final byte CYTOSOL_PIXVAL = 1;
    for (int i = 0; i < bytePixels.length; i++) {
        if (shortPixels[i] != 0) {
            bytePixels[i] = CYTOSOL_PIXVAL;
        }
    }
    VCImage maskImage;
    try {
        maskImage = new VCImageUncompressed(null, bytePixels, extent, numX, numY, numZ);
    } catch (ImageException e) {
        e.printStackTrace();
        throw new RuntimeException("failed to create mask image for geometry");
    }
    Geometry geometry = new Geometry("geometry", maskImage);
    if (geometry.getGeometrySpec().getNumSubVolumes() != 2) {
        throw new Exception("Cell ROI has no ExtraCellular.");
    }
    int subVolume0PixVal = ((ImageSubVolume) geometry.getGeometrySpec().getSubVolume(0)).getPixelValue();
    geometry.getGeometrySpec().getSubVolume(0).setName((subVolume0PixVal == EXTRACELLULAR_PIXVAL ? EXTRACELLULAR_NAME : CYTOSOL_NAME));
    int subVolume1PixVal = ((ImageSubVolume) geometry.getGeometrySpec().getSubVolume(1)).getPixelValue();
    geometry.getGeometrySpec().getSubVolume(1).setName((subVolume1PixVal == CYTOSOL_PIXVAL ? CYTOSOL_NAME : EXTRACELLULAR_NAME));
    geometry.getGeometrySurfaceDescription().updateAll();
    BioModel bioModel = new BioModel(null);
    bioModel.setName("unnamed");
    Model model = new Model("model");
    bioModel.setModel(model);
    Feature extracellular = model.addFeature(EXTRACELLULAR_NAME);
    Feature cytosol = model.addFeature(CYTOSOL_NAME);
    Membrane plasmaMembrane = model.addMembrane(PLASMAMEMBRANE_NAME);
    String roiDataName = FRAPStudy.ROI_EXTDATA_NAME;
    final int ONE_DIFFUSION_SPECIES_COUNT = 1;
    final int MOBILE_SPECIES_INDEX = 0;
    Expression[] diffusionConstants = new Expression[ONE_DIFFUSION_SPECIES_COUNT];
    Species[] species = new Species[ONE_DIFFUSION_SPECIES_COUNT];
    SpeciesContext[] speciesContexts = new SpeciesContext[ONE_DIFFUSION_SPECIES_COUNT];
    Expression[] initialConditions = new Expression[ONE_DIFFUSION_SPECIES_COUNT];
    // Mobile Species
    diffusionConstants[MOBILE_SPECIES_INDEX] = new Expression(baseDiffusionRate);
    species[MOBILE_SPECIES_INDEX] = new Species(SPECIES_NAME_PREFIX_MOBILE, "Mobile bleachable species");
    speciesContexts[MOBILE_SPECIES_INDEX] = new SpeciesContext(null, species[MOBILE_SPECIES_INDEX].getCommonName(), species[MOBILE_SPECIES_INDEX], cytosol);
    FieldFunctionArguments postBleach_first = new FieldFunctionArguments(roiDataName, "postbleach_first", new Expression(0), VariableType.VOLUME);
    FieldFunctionArguments prebleach_avg = new FieldFunctionArguments(roiDataName, "prebleach_avg", new Expression(0), VariableType.VOLUME);
    Expression expPostBleach_first = new Expression(postBleach_first.infix());
    Expression expPreBleach_avg = new Expression(prebleach_avg.infix());
    initialConditions[MOBILE_SPECIES_INDEX] = Expression.div(expPostBleach_first, expPreBleach_avg);
    SimulationContext simContext = new SimulationContext(bioModel.getModel(), geometry);
    bioModel.addSimulationContext(simContext);
    FeatureMapping cytosolFeatureMapping = (FeatureMapping) simContext.getGeometryContext().getStructureMapping(cytosol);
    FeatureMapping extracellularFeatureMapping = (FeatureMapping) simContext.getGeometryContext().getStructureMapping(extracellular);
    MembraneMapping plasmaMembraneMapping = (MembraneMapping) simContext.getGeometryContext().getStructureMapping(plasmaMembrane);
    SubVolume cytSubVolume = geometry.getGeometrySpec().getSubVolume(CYTOSOL_NAME);
    SubVolume exSubVolume = geometry.getGeometrySpec().getSubVolume(EXTRACELLULAR_NAME);
    SurfaceClass pmSurfaceClass = geometry.getGeometrySurfaceDescription().getSurfaceClass(exSubVolume, cytSubVolume);
    cytosolFeatureMapping.setGeometryClass(cytSubVolume);
    extracellularFeatureMapping.setGeometryClass(exSubVolume);
    plasmaMembraneMapping.setGeometryClass(pmSurfaceClass);
    cytosolFeatureMapping.getUnitSizeParameter().setExpression(new Expression(1.0));
    extracellularFeatureMapping.getUnitSizeParameter().setExpression(new Expression(1.0));
    plasmaMembraneMapping.getUnitSizeParameter().setExpression(new Expression(1.0));
    for (int i = 0; i < initialConditions.length; i++) {
        model.addSpecies(species[i]);
        model.addSpeciesContext(speciesContexts[i]);
    }
    for (int i = 0; i < speciesContexts.length; i++) {
        SpeciesContextSpec scs = simContext.getReactionContext().getSpeciesContextSpec(speciesContexts[i]);
        scs.getInitialConditionParameter().setExpression(initialConditions[i]);
        scs.getDiffusionParameter().setExpression(diffusionConstants[i]);
    }
    MathMapping mathMapping = simContext.createNewMathMapping();
    MathDescription mathDesc = mathMapping.getMathDescription();
    // Add PSF function
    mathDesc.addVariable(new Function(Simulation.PSF_FUNCTION_NAME, new Expression(psfFDIS.getFieldFuncArgs().infix()), null));
    simContext.setMathDescription(mathDesc);
    SimulationVersion simVersion = new SimulationVersion(simKey, "sim1", owner, new GroupAccessNone(), new KeyValue("0"), new BigDecimal(0), new Date(), VersionFlag.Current, "", null);
    Simulation newSimulation = new Simulation(simVersion, simContext.getMathDescription());
    newSimulation.getSolverTaskDescription().setSolverDescription(SolverDescription.FiniteVolumeStandalone);
    simContext.addSimulation(newSimulation);
    newSimulation.getSolverTaskDescription().setTimeBounds(timeBounds);
    newSimulation.getSolverTaskDescription().setOutputTimeSpec(new UniformOutputTimeSpec(timeStepVal));
    newSimulation.getMeshSpecification().setSamplingSize(cellROI_2D.getISize());
    newSimulation.getSolverTaskDescription().setTimeStep(new TimeStep(timeStepVal, timeStepVal, timeStepVal));
    return bioModel;
}
Also used : MembraneMapping(cbit.vcell.mapping.MembraneMapping) ImageException(cbit.image.ImageException) KeyValue(org.vcell.util.document.KeyValue) Extent(org.vcell.util.Extent) SurfaceClass(cbit.vcell.geometry.SurfaceClass) MathDescription(cbit.vcell.math.MathDescription) VCImage(cbit.image.VCImage) SpeciesContext(cbit.vcell.model.SpeciesContext) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) Feature(cbit.vcell.model.Feature) TimeBounds(cbit.vcell.solver.TimeBounds) Function(cbit.vcell.math.Function) GroupAccessNone(org.vcell.util.document.GroupAccessNone) TimeStep(cbit.vcell.solver.TimeStep) SimulationVersion(org.vcell.util.document.SimulationVersion) FeatureMapping(cbit.vcell.mapping.FeatureMapping) SubVolume(cbit.vcell.geometry.SubVolume) ImageSubVolume(cbit.vcell.geometry.ImageSubVolume) Membrane(cbit.vcell.model.Membrane) Species(cbit.vcell.model.Species) ImageSubVolume(cbit.vcell.geometry.ImageSubVolume) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) FieldFunctionArguments(cbit.vcell.field.FieldFunctionArguments) VCImageUncompressed(cbit.image.VCImageUncompressed) SimulationContext(cbit.vcell.mapping.SimulationContext) ROI(cbit.vcell.VirtualMicroscopy.ROI) ImageException(cbit.image.ImageException) UserCancelException(org.vcell.util.UserCancelException) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Geometry(cbit.vcell.geometry.Geometry) Simulation(cbit.vcell.solver.Simulation) Expression(cbit.vcell.parser.Expression) BioModel(cbit.vcell.biomodel.BioModel) Model(cbit.vcell.model.Model) BioModel(cbit.vcell.biomodel.BioModel) MathMapping(cbit.vcell.mapping.MathMapping)

Example 8 with VCImage

use of cbit.image.VCImage in project vcell by virtualcell.

the class ClientDocumentManager method saveAsNew.

/**
 * Insert the method's description here.
 * Creation date: (2/5/01 4:58:40 PM)
 */
public VCImage saveAsNew(VCImage vcImage, java.lang.String newName) throws DataAccessException {
    try {
        String vcImageXML = null;
        try {
            vcImageXML = XmlHelper.imageToXML(vcImage);
        } catch (XmlParseException e) {
            e.printStackTrace(System.out);
            throw new DataAccessException(e.getMessage());
        }
        String savedVCImageXML = sessionManager.getUserMetaDbServer().saveVCImageAs(new BigString(vcImageXML), newName).toString();
        VCImage savedVCImage = null;
        try {
            savedVCImage = XmlHelper.XMLToImage(savedVCImageXML);
        } catch (XmlParseException e) {
            e.printStackTrace(System.out);
            throw new DataAccessException(e.getMessage());
        }
        savedVCImage.refreshDependencies();
        KeyValue savedKey = savedVCImage.getVersion().getVersionKey();
        if (xmlHash.get(savedKey) == null) {
            xmlHash.put(savedKey, savedVCImageXML);
        }
        try {
            ISize size = new ISize(savedVCImage.getNumX(), savedVCImage.getNumY(), savedVCImage.getNumZ());
            GIFImage browseData = BrowseImage.makeBrowseGIFImage(savedVCImage);
            VCImageInfo savedVCImageInfo = new VCImageInfo(savedVCImage.getVersion(), size, savedVCImage.getExtent(), browseData, VCellSoftwareVersion.fromSystemProperty());
            imgInfoHash.put(savedKey, savedVCImageInfo);
            fireDatabaseInsert(new DatabaseEvent(this, DatabaseEvent.INSERT, null, savedVCImageInfo));
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
        return savedVCImage;
    } catch (RemoteProxyException e) {
        e.printStackTrace(System.out);
        throw new DataAccessException(VCellErrorMessages.FAIL_SAVE_MESSAGE + "\n\n" + e.getMessage());
    }
}
Also used : GIFImage(cbit.image.GIFImage) KeyValue(org.vcell.util.document.KeyValue) ISize(org.vcell.util.ISize) VCImage(cbit.image.VCImage) BigString(org.vcell.util.BigString) XmlParseException(cbit.vcell.xml.XmlParseException) BigString(org.vcell.util.BigString) DataAccessException(org.vcell.util.DataAccessException) VCImageInfo(cbit.image.VCImageInfo) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) XmlParseException(cbit.vcell.xml.XmlParseException) RemoteProxyException(cbit.vcell.message.server.bootstrap.client.RemoteProxyVCellConnectionFactory.RemoteProxyException) DataAccessException(org.vcell.util.DataAccessException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) RemoteProxyException(cbit.vcell.message.server.bootstrap.client.RemoteProxyVCellConnectionFactory.RemoteProxyException)

Example 9 with VCImage

use of cbit.image.VCImage in project vcell by virtualcell.

the class ClientDocumentManager method updateGeometryRelatedHashes.

private void updateGeometryRelatedHashes(Geometry savedGeometry) {
    if (savedGeometry == null || (savedGeometry.getVersion() != null && geoInfoHash.get(savedGeometry.getVersion().getVersionKey()) != null)) {
        return;
    }
    KeyValue imageRef = (savedGeometry.getGeometrySpec().getImage() != null) ? (savedGeometry.getGeometrySpec().getImage().getKey()) : (null);
    if (imageRef != null && imgInfoHash.get(imageRef) == null) {
        VCImage savedVCImage = savedGeometry.getGeometrySpec().getImage();
        ISize size = new ISize(savedVCImage.getNumX(), savedVCImage.getNumY(), savedVCImage.getNumZ());
        VCImageInfo savedVCImageInfo = new VCImageInfo(savedVCImage.getVersion(), size, savedVCImage.getExtent(), null, VCellSoftwareVersion.fromSystemProperty());
        imgInfoHash.put(savedVCImage.getVersion().getVersionKey(), savedVCImageInfo);
    }
    GeometryInfo savedGeometryInfo = new GeometryInfo(savedGeometry.getVersion(), savedGeometry.getDimension(), savedGeometry.getExtent(), savedGeometry.getOrigin(), imageRef, VCellSoftwareVersion.fromSystemProperty());
    geoInfoHash.put(savedGeometry.getVersion().getVersionKey(), savedGeometryInfo);
    fireDatabaseInsert(new DatabaseEvent(this, DatabaseEvent.INSERT, null, geoInfoHash.get(savedGeometry.getVersion().getVersionKey())));
}
Also used : KeyValue(org.vcell.util.document.KeyValue) ISize(org.vcell.util.ISize) GeometryInfo(cbit.vcell.geometry.GeometryInfo) VCImage(cbit.image.VCImage) VCImageInfo(cbit.image.VCImageInfo)

Example 10 with VCImage

use of cbit.image.VCImage in project vcell by virtualcell.

the class GeometrySpec method setImage.

/**
 * This method was created in VisualAge.
 * @param image cbit.image.FileImage
 */
public void setImage(VCImage image) throws PropertyVetoException {
    VCImage oldImage = this.vcImage;
    fireVetoableChange("image", oldImage, image);
    if (this.vcImage != image) {
        uncompressedPixels = null;
        if (image != null) {
            setExtent(image.getExtent());
            if (image.getNumY() == 1 && image.getNumZ() == 1) {
                dimension = 1;
            } else if (image.getNumZ() == 1) {
                dimension = 2;
            } else {
                dimension = 3;
            }
        }
        this.vcImage = image;
        SubVolume[] newImageSubVolumes = new SubVolume[vcImage.getNumPixelClasses()];
        int svCount = 0;
        cbit.image.VCPixelClass[] vcPixelClasses = vcImage.getPixelClasses();
        for (int i = 0; i < vcPixelClasses.length; i++) {
            ImageSubVolume isv = new ImageSubVolume(null, vcPixelClasses[i], svCount);
            // 
            if (getSubVolumes() != null && getSubVolumes().length > i && getSubVolumes().length == vcPixelClasses.length) {
                isv.setName(getSubVolumes()[i].getName());
                isv.setHandle(getSubVolumes()[i].getHandle());
                if (isv.compareEqual(getSubVolumes()[i])) {
                    isv = (ImageSubVolume) getSubVolumes()[i];
                    isv.setPixelClass(vcPixelClasses[i]);
                }
            } else {
                isv.setHandle(svCount);
            }
            newImageSubVolumes[svCount++] = isv;
        }
        // 
        // merge existing analytic subvolumes with the new image subvolumes
        // 
        SubVolume[] allSubVolumes = new SubVolume[newImageSubVolumes.length + getNumAnalyticOrCSGSubVolumes()];
        Enumeration<SubVolume> analyticOrCSGSubVolumeEnum = getAnalyticOrCSGSubVolumes();
        svCount = 0;
        TreeSet<Integer> analyticSubVolHandlesTreeSet = new TreeSet<Integer>();
        while (analyticOrCSGSubVolumeEnum.hasMoreElements()) {
            allSubVolumes[svCount] = analyticOrCSGSubVolumeEnum.nextElement();
            analyticSubVolHandlesTreeSet.add(allSubVolumes[svCount].getHandle());
            svCount++;
        }
        for (int i = 0; i < newImageSubVolumes.length; i++) {
            allSubVolumes[svCount] = newImageSubVolumes[i];
            if (analyticSubVolHandlesTreeSet.contains(allSubVolumes[svCount].getHandle())) {
                throw new RuntimeException("Duplicate Subvolume handles found while setting new Image");
            }
            svCount++;
        }
        setSubVolumes(allSubVolumes);
    }
    firePropertyChange("image", oldImage, image);
}
Also used : VCPixelClass(cbit.image.VCPixelClass) VCImage(cbit.image.VCImage) TreeSet(java.util.TreeSet)

Aggregations

VCImage (cbit.image.VCImage)54 Geometry (cbit.vcell.geometry.Geometry)22 Extent (org.vcell.util.Extent)20 ISize (org.vcell.util.ISize)19 VCImageUncompressed (cbit.image.VCImageUncompressed)18 ImageException (cbit.image.ImageException)16 PropertyVetoException (java.beans.PropertyVetoException)15 DataAccessException (org.vcell.util.DataAccessException)15 KeyValue (org.vcell.util.document.KeyValue)15 SubVolume (cbit.vcell.geometry.SubVolume)14 Origin (org.vcell.util.Origin)13 RegionImage (cbit.vcell.geometry.RegionImage)11 SurfaceClass (cbit.vcell.geometry.SurfaceClass)11 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)11 UserCancelException (org.vcell.util.UserCancelException)11 ImageSubVolume (cbit.vcell.geometry.ImageSubVolume)10 Expression (cbit.vcell.parser.Expression)10 VCPixelClass (cbit.image.VCPixelClass)9 BioModel (cbit.vcell.biomodel.BioModel)8 FieldDataFileOperationSpec (cbit.vcell.field.io.FieldDataFileOperationSpec)8