use of org.vcell.util.document.KeyValue in project vcell by virtualcell.
the class RunSimulation2DOp method runRefSimulation.
public ImageTimeSeries<FloatImage> runRefSimulation(LocalWorkspace localWorkspace, Simulation simulation, String varName, ClientTaskStatusSupport progressListener) throws Exception {
User owner = LocalWorkspace.getDefaultOwner();
KeyValue simKey = LocalWorkspace.createNewKeyValue();
runFVSolverStandalone(new File(localWorkspace.getDefaultSimDataDirectory()), simulation, progressListener);
Extent extent = simulation.getMathDescription().getGeometry().getExtent();
Origin origin = simulation.getMathDescription().getGeometry().getOrigin();
VCDataIdentifier vcDataIdentifier = new VCSimulationDataIdentifier(new VCSimulationIdentifier(simKey, owner), 0);
CartesianMesh mesh = localWorkspace.getDataSetControllerImpl().getMesh(vcDataIdentifier);
ISize isize = new ISize(mesh.getSizeX(), mesh.getSizeY(), mesh.getSizeZ());
double[] dataTimes = localWorkspace.getDataSetControllerImpl().getDataSetTimes(vcDataIdentifier);
FloatImage[] solutionImages = new FloatImage[dataTimes.length];
for (int i = 0; i < dataTimes.length; i++) {
SimDataBlock simDataBlock = localWorkspace.getDataSetControllerImpl().getSimDataBlock(null, vcDataIdentifier, varName, dataTimes[i]);
double[] doubleData = simDataBlock.getData();
float[] floatPixels = new float[doubleData.length];
for (int j = 0; j < doubleData.length; j++) {
floatPixels[j] = (float) doubleData[j];
}
solutionImages[i] = new FloatImage(floatPixels, origin, extent, isize.getX(), isize.getY(), isize.getZ());
}
ImageTimeSeries<FloatImage> solution = new ImageTimeSeries<FloatImage>(FloatImage.class, solutionImages, dataTimes, 1);
return solution;
}
use of org.vcell.util.document.KeyValue in project vcell by virtualcell.
the class RunRefSimulationOp method runRefSimulation.
private static ImageTimeSeries<FloatImage> runRefSimulation(LocalWorkspace localWorkspace, ROI cellROI, double timeStepVal, TimeBounds timeBounds, FloatImage initRefConc, double baseDiffusionRate, ClientTaskStatusSupport progressListener) throws Exception {
String INITCONC_EXTDATA_NAME = "initConc";
String INITCONC_EXTDATA_VARNAME = "conc";
String VAR_NAME = "species";
User owner = LocalWorkspace.getDefaultOwner();
KeyValue simKey = LocalWorkspace.createNewKeyValue();
ExternalDataInfo initialConcentrationExtData = createNewExternalDataInfo(localWorkspace, INITCONC_EXTDATA_NAME);
Extent extent = initRefConc.getExtent();
Origin origin = initRefConc.getOrigin();
ISize isize = new ISize(initRefConc.getNumX(), initRefConc.getNumY(), initRefConc.getNumZ());
saveExternalData(initRefConc, INITCONC_EXTDATA_VARNAME, initialConcentrationExtData.getExternalDataIdentifier(), localWorkspace);
FieldFunctionArguments initConditionFFA = new FieldFunctionArguments(INITCONC_EXTDATA_NAME, INITCONC_EXTDATA_VARNAME, new Expression(0.0), VariableType.VOLUME);
BioModel bioModel = createRefSimBioModel(simKey, owner, origin, extent, cellROI, timeStepVal, timeBounds, VAR_NAME, new Expression(initConditionFFA.infix()), baseDiffusionRate);
if (progressListener != null) {
progressListener.setMessage("Running Reference Simulation...");
}
// run simulation
runFVSolverStandalone(new File(localWorkspace.getDefaultSimDataDirectory()), bioModel.getSimulation(0), initialConcentrationExtData.getExternalDataIdentifier(), progressListener, true);
VCDataIdentifier vcDataIdentifier = new VCSimulationDataIdentifier(new VCSimulationIdentifier(simKey, owner), 0);
double[] dataTimes = localWorkspace.getDataSetControllerImpl().getDataSetTimes(vcDataIdentifier);
FloatImage[] solutionImages = new FloatImage[dataTimes.length];
for (int i = 0; i < dataTimes.length; i++) {
SimDataBlock simDataBlock = localWorkspace.getDataSetControllerImpl().getSimDataBlock(null, vcDataIdentifier, VAR_NAME, dataTimes[i]);
double[] doubleData = simDataBlock.getData();
float[] floatPixels = new float[doubleData.length];
for (int j = 0; j < doubleData.length; j++) {
floatPixels[j] = (float) doubleData[j];
}
solutionImages[i] = new FloatImage(floatPixels, origin, extent, isize.getX(), isize.getY(), isize.getZ());
}
ImageTimeSeries<FloatImage> solution = new ImageTimeSeries<FloatImage>(FloatImage.class, solutionImages, dataTimes, 1);
return solution;
}
use of org.vcell.util.document.KeyValue in project vcell by virtualcell.
the class RunRefSimulationOp method createNewExternalDataInfo.
private static ExternalDataInfo createNewExternalDataInfo(LocalWorkspace localWorkspace, String extDataIDName) {
File targetDir = new File(localWorkspace.getDefaultSimDataDirectory());
KeyValue key = LocalWorkspace.createNewKeyValue();
User owner = LocalWorkspace.getDefaultOwner();
ExternalDataIdentifier newImageDataExtDataID = new ExternalDataIdentifier(key, owner, extDataIDName);
String filename = new File(targetDir, newImageDataExtDataID.getID() + SimDataConstants.LOGFILE_EXTENSION).getAbsolutePath();
ExternalDataInfo newImageDataExtDataInfo = new ExternalDataInfo(newImageDataExtDataID, filename);
return newImageDataExtDataInfo;
}
use of org.vcell.util.document.KeyValue in project vcell by virtualcell.
the class DisplayTimeSeriesOp method displayImageTimeSeries.
public void displayImageTimeSeries(final ImageTimeSeries<? extends Image> imageTimeSeries, final String title, final WindowListener windowListener) throws ImageException, IOException {
try {
System.out.println("starting to prepare data for time series viewing");
final PDEDataViewer pdeDataViewer = new PDEDataViewer();
DataSetControllerProvider dataSetControllerProvider;
try {
dataSetControllerProvider = getDataSetControllerProvider(imageTimeSeries, pdeDataViewer);
} catch (ImageException | IOException e1) {
e1.printStackTrace();
throw new RuntimeException(e1.getMessage(), e1);
}
VCDataManager vcDataManager = new VCDataManager(dataSetControllerProvider);
OutputContext outputContext = new OutputContext(new AnnotatedFunction[0]);
final VCDataIdentifier vcDataIdentifier = new VCDataIdentifier() {
public User getOwner() {
return new User("nouser", null);
}
public KeyValue getDataKey() {
return null;
}
public String getID() {
return "mydata";
}
};
PDEDataManager pdeDataManager = new PDEDataManager(outputContext, vcDataManager, vcDataIdentifier);
final ClientPDEDataContext myPdeDataContext = new ClientPDEDataContext(pdeDataManager);
final RequestManager requestManager = new RequestManagerAdapter() {
};
final DataViewerManager dataViewerManager = new DataViewerManager() {
public void dataJobMessage(DataJobEvent event) {
}
public void exportMessage(ExportEvent event) {
}
public void addDataListener(DataListener newListener) {
}
public UserPreferences getUserPreferences() {
// getRequestManager().getUserPreferences();
return null;
}
public void removeDataListener(DataListener newListener) {
}
public void startExport(Component requester, OutputContext outputContext, ExportSpecs exportSpecs) {
// getLocalRequestManager().startExport(outputContext, FieldDataWindowManager.this, exportSpecs);
}
public void simStatusChanged(SimStatusEvent simStatusEvent) {
}
public User getUser() {
return new User("dummy", new KeyValue("123"));
// return getRequestManager().getDocumentManager().getUser();
}
public RequestManager getRequestManager() {
return requestManager;
}
};
System.out.println("ready to display time series");
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
JFrame jframe = new TopLevelFrame();
jframe.setTitle(title);
jframe.getContentPane().add(pdeDataViewer);
jframe.setSize(1000, 600);
jframe.setVisible(true);
if (windowListener != null) {
jframe.addWindowListener(windowListener);
}
try {
pdeDataViewer.setDataViewerManager(dataViewerManager);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
pdeDataViewer.setPdeDataContext(myPdeDataContext);
}
});
} catch (InvocationTargetException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.vcell.util.document.KeyValue in project vcell by virtualcell.
the class MigrateDbManager method migrate.
public static void migrate(Table table, ConnectionFactory conFactory_from, ConnectionFactory conFactory_to) throws SQLException {
System.out.println("starting migration of table " + table.getTableName());
assert (conFactory_from.getDatabaseSyntax() == DatabaseSyntax.ORACLE);
assert (conFactory_to.getDatabaseSyntax() == DatabaseSyntax.POSTGRES);
Connection con_from = null;
Connection con_to = null;
try {
con_from = conFactory_from.getConnection(new Object());
con_to = conFactory_to.getConnection(new Object());
con_from.setReadOnly(true);
con_to.setReadOnly(false);
//
// get Last key in target table (using the "to" connection) - NULL if no records.
//
String sql = "SELECT ID FROM " + table.getTableName() + " ORDER BY ID DESC LIMIT 1";
System.out.println(sql);
//
// should disable self-referential reference in vc_struct table (field "parentRef")
//
KeyValue lastkey = null;
try (Statement stmt_to_init = con_to.createStatement();
ResultSet rset_to_init = stmt_to_init.executeQuery(sql)) {
if (rset_to_init.next()) {
BigDecimal keyBigDecimal = rset_to_init.getBigDecimal(1);
if (!rset_to_init.wasNull()) {
lastkey = new KeyValue(keyBigDecimal);
}
}
}
//
// gather fields to copy
//
ArrayList<Field> fieldsToCopy = new ArrayList<Field>();
for (Field f : table.getFields()) {
// if (f.getSqlDataType() == SQLDataType.clob_text || f.getSqlDataType() == SQLDataType.blob_bytea){
// continue; // insert in another pass
// }
fieldsToCopy.add(f);
}
//
// form the query using the "from" connection
//
StringBuffer selectBuffer = new StringBuffer();
selectBuffer.append("SELECT ");
for (int i = 0; i < fieldsToCopy.size(); i++) {
selectBuffer.append(fieldsToCopy.get(i).getUnqualifiedColName());
if (i < fieldsToCopy.size() - 1) {
selectBuffer.append(",");
}
}
selectBuffer.append(" FROM " + table.getTableName());
if (lastkey != null) {
selectBuffer.append(" WHERE id > " + lastkey + " ");
}
selectBuffer.append(" ORDER BY id");
System.out.println(selectBuffer.toString());
//
// form the prepared INSERT statement for the "to" connection
//
StringBuffer insertBuffer = new StringBuffer();
insertBuffer.append("INSERT INTO " + table.getTableName() + " values (");
for (int i = 0; i < fieldsToCopy.size(); i++) {
insertBuffer.append("?");
if (i < fieldsToCopy.size() - 1) {
insertBuffer.append(",");
}
}
insertBuffer.append(")");
System.out.println(insertBuffer.toString());
try (Statement stmt_from = con_from.createStatement();
ResultSet rset_from = stmt_from.executeQuery(selectBuffer.toString());
PreparedStatement stmt_to = con_to.prepareStatement(insertBuffer.toString())) {
long rowCount = 0;
long batchCount = 0;
while (rset_from.next()) {
for (int i = 0; i < fieldsToCopy.size(); i++) {
Field f = fieldsToCopy.get(i);
switch(f.getSqlDataType().basicDataType) {
case BLOB:
{
boolean found = false;
Object lob_object = rset_from.getObject(i + 1);
if (!rset_from.wasNull()) {
if (lob_object instanceof java.sql.Blob) {
java.sql.Blob blob_object = (java.sql.Blob) lob_object;
byte[] bytes = blob_object.getBytes((long) 1, (int) blob_object.length());
stmt_to.setBytes(i + 1, bytes);
found = true;
}
}
if (!found) {
stmt_to.setNull(i + 1, java.sql.Types.LONGVARBINARY);
}
break;
}
case CLOB:
{
boolean found = false;
Object lob_object = rset_from.getObject(i + 1);
if (!rset_from.wasNull()) {
if (lob_object instanceof java.sql.Clob) {
java.sql.Clob clob_object = (java.sql.Clob) lob_object;
byte[] ins = new byte[(int) clob_object.length()];
try {
clob_object.getAsciiStream().read(ins);
String str = new String(ins);
stmt_to.setString(i + 1, str);
found = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (!found) {
stmt_to.setNull(i + 1, java.sql.Types.LONGVARCHAR);
}
break;
}
case CHAR:
{
String str = rset_from.getString(i + 1);
if (!rset_from.wasNull()) {
stmt_to.setString(i + 1, str);
} else {
stmt_to.setNull(i + 1, java.sql.Types.CHAR);
}
break;
}
case VARCHAR:
{
String str = rset_from.getString(i + 1);
if (!rset_from.wasNull()) {
stmt_to.setString(i + 1, str);
} else {
stmt_to.setNull(i + 1, java.sql.Types.VARCHAR);
}
break;
}
case DATE:
{
Date date = rset_from.getDate(i + 1);
if (!rset_from.wasNull()) {
stmt_to.setDate(i + 1, date);
} else {
stmt_to.setNull(i + 1, java.sql.Types.DATE);
}
break;
}
case BIGINT:
{
BigDecimal bigDecimal = rset_from.getBigDecimal(i + 1);
if (!rset_from.wasNull()) {
stmt_to.setBigDecimal(i + 1, bigDecimal);
} else {
stmt_to.setNull(i + 1, java.sql.Types.BIGINT);
}
break;
}
case NUMERIC:
{
BigDecimal bigDecimal = rset_from.getBigDecimal(i + 1);
if (!rset_from.wasNull()) {
stmt_to.setBigDecimal(i + 1, bigDecimal);
} else {
stmt_to.setNull(i + 1, java.sql.Types.NUMERIC);
}
break;
}
default:
{
throw new RuntimeException("support for JDBC Type " + f.getSqlDataType().basicDataType + " not yet supported");
}
}
}
stmt_to.addBatch();
rowCount++;
batchCount++;
if (batchCount >= 300) {
System.out.println("writing " + batchCount + " of " + rowCount + " records into table " + table.getTableName());
batchCount = 0;
stmt_to.executeBatch();
con_to.commit();
}
}
if (batchCount > 0) {
stmt_to.executeBatch();
con_to.commit();
}
}
// end try
System.out.println("starting migration of table " + table.getTableName());
} finally {
if (con_from != null)
con_from.close();
if (con_to != null)
con_to.close();
}
}
Aggregations