void AMScanDatabaseImportController::copySamples() { if(state_ != Importing) return; emit progressDescription("Copying Samples..."); emit stepProgress(-1); int totalSteps = s2dSampleIds_.count(); int currentStep = 0; QMutableMapIterator<int, int> i(s2dSampleIds_); while (i.hasNext()) { if(state_ != Importing) return; i.next(); emit stepProgress(int(100.0*(++currentStep)/totalSteps)); // key is id; value is id in destination database, or -1 if not there yet. (if not there, need to insert) int sourceId = i.key(), destinationId = i.value(); if(destinationId<1) { AMSample s; s.loadFromDb(sourceDb_, sourceId); if(s.storeToDb(destinationDb_)) i.setValue(s.id()); else AMErrorMon::report(AMErrorReport(this, AMErrorReport::Alert, -2, "Could not import the sample '" % sourceSamples_.value(sourceId) % "' into the database.")); } qApp->sendPostedEvents(); qApp->processEvents(); } }
void AMScanDatabaseImportController::copyExperiments() { if(state_ != Importing) return; emit progressDescription("Copying Experiments..."); emit stepProgress(-1); int totalSteps = s2dExperimentIds_.count(); int currentStep = 0; QMutableMapIterator<int, int> i(s2dExperimentIds_); while (i.hasNext()) { if(state_ != Importing) return; i.next(); emit stepProgress(int(100.0*(++currentStep)/totalSteps)); // key is id; value is id in destination database, or -1 if not there yet. (if not there, need to insert) int sourceId = i.key(), destinationId = i.value(); if(destinationId<1) { AMExperiment e; e.loadFromDb(sourceDb_, sourceId); if(e.storeToDb(destinationDb_)) i.setValue(e.id()); else AMErrorMon::report(AMErrorReport(this, AMErrorReport::Alert, -2, "Could not import the experiment '" % sourceExperiments_.value(sourceId) % "' into the database.")); } // TODO: need to update the scans that should be in this experiment. This will be tricky... don't know new scan IDs yet. qApp->sendPostedEvents(); qApp->processEvents(); } }
void pdp::ExampleStep::work(LungDataset& input, LungDataset& output) { std::cout << "performing the example step!" << std::endl; const DWORD pause = 1000; Sleep(pause); emit stepProgress(0.33f); Sleep(pause); emit stepProgress(0.66f); Sleep(pause); emit stepProgress(1.00f); }
void pdp::convexHull::work(LungDataset& input, LungDataset& output) { std::cout << "performing the convexHull step!" << std::endl; typedef itk::PolyLineParametricPath< 2 >::Pointer PathPointer; for (int i = 0; i < input.slicesContours.size(); ++i) { std::vector<PathPointer> paths; if(input.slicesContours[i].isLeftLung()){ ContourFilter contourFilterLeft(input.slicesContours[i].getLeftLung()); PathPointer hull = contourFilterLeft.convexHull(); input.slicesContours[i].setLeftHull(hull); input.slicesThickenings[i].addLeftThickening(contourFilterLeft.generateThickenings(20, i)); //std::cout << "number of thickenings detected: " << thicks.size() << std::endl; } if(input.slicesContours[i].isRightLung()){ ContourFilter contourFilterRight(input.slicesContours[i].getRightLung()); PathPointer hull = contourFilterRight.convexHull(); input.slicesContours[i].setRightHull(hull); input.slicesThickenings[i].addRightThickening(contourFilterRight.generateThickenings(20, i)); //std::cout << "number of thickenings detected: " << thicks.size() << std::endl; } emit stepProgress(1.0f/input.slicesContours.size()*i); } }
void AMScanDatabaseImportController::analyzeFacilitiesForDuplicates() { if(state_ != Analyzing) { return; } sourceFacilities_.clear(); s2dFacilityIds_.clear(); emit progressDescription("Checking Facilities..."); emit stepProgress(-1); // go through facilities in source, and make sure they all exist in destination. Map ids. QSqlQuery q = AMDbObjectSupport::s()->select<AMFacility>(sourceDb_, "id, name, description"); q.exec(); int stepIndex = 0; int totalSteps = q.size(); while(q.next()) { emit stepProgress(int(100.0*(++stepIndex) / totalSteps)); // if size is not supported in db, then this will be negative... which will trigger a "undefined" progress bar. Perfect. int id = q.value(0).toInt(); QString name = q.value(1).toString(); QString description = q.value(2).toString(); sourceFacilities_[id] = name % " " % description; // Find a matching one in destinationDb_. QSqlQuery q2 = AMDbObjectSupport::s()->select<AMFacility>(destinationDb_, "id", "name = ? AND description = ?"); q2.bindValue(0, name); q2.bindValue(1, description); q2.exec(); if(q2.first()) { // found one. s2dFacilityIds_[id] = q2.value(0).toInt(); // insert a mapping from source facility IDs to destination facility IDs } else { // none found in destination. Will need to create it. s2dFacilityIds_[id] = -1; } qApp->sendPostedEvents(); qApp->processEvents(); } emit stepProgress(100); }
void AMScanDatabaseImportController::analyzeSamplesForDuplicates() { if(state_ != Analyzing) return; sourceSamples_.clear(); s2dSampleIds_.clear(); emit progressDescription("Reviewing Samples..."); emit stepProgress(-1); // go through samples in source, and see if any exist in destination QSqlQuery q = AMDbObjectSupport::s()->select<AMSample>(sourceDb_, "id, name, dateTime"); q.exec(); int stepIndex = 0; int totalSteps = q.size(); while(q.next()) { emit stepProgress(int(100.0*(++stepIndex) / totalSteps)); // if size is not supported in db, then this will be negative... which will trigger a "undefined" progress bar. Perfect. int id = q.value(0).toInt(); QString name = q.value(1).toString(); QDateTime dateTime = q.value(2).toDateTime(); sourceSamples_[id] = name % ", created " % AMDateTimeUtils::prettyDateTime(dateTime); // Find a matching one in destinationDb_. QSqlQuery q2 = AMDbObjectSupport::s()->select<AMSample>(destinationDb_, "id", "name = ? AND dateTime = ?"); q2.bindValue(0, name); q2.bindValue(1, dateTime); q2.exec(); if(q2.first()) { // found one. s2dSampleIds_[id] = q2.value(0).toInt(); // insert a mapping from source sample IDs to destination sample IDs } else { // none found in destination. Will need to create it. s2dSampleIds_[id] = -1; } qApp->sendPostedEvents(); qApp->processEvents(); } emit stepProgress(100); state_ = ReadyToImport; }
void AMScanDatabaseImportController::startDatabaseOperations() { if(state_ != ReadyToImport) return; destinationDb_->startTransaction(); state_ = Importing; copyFacilities(); copyRuns(); copyExperiments(); copySamples(); copyScans(); if(state_ == Importing) { // if we were cancelled, don't commit the transaction. state_ = Finished; destinationDb_->commitTransaction(); emit progressDescription("Import complete."); emit stepProgress(100); } }
void AMScanDatabaseImportController::copyScans() { if(state_ != Importing) return; emit progressDescription("Copying Scans..."); emit stepProgress(-1); QString scanTableName = AMDbObjectSupport::s()->tableNameForClass<AMScan>(); QList<int> scanIds = sourceDb_->objectsWhere(scanTableName, QString()); AMScan::setAutoLoadData(false); // disable loading data for performance. It will also fail to load because it's in the wrong spot. for(int i=0, cc=scanIds.count(); i<cc; i++) { if(state_ != Importing) return; emit stepProgress(int(100.0*(i+1)/cc)); AMDbObject* object = AMDbObjectSupport::s()->createAndLoadObjectAt(sourceDb_, scanTableName, scanIds.at(i)); if(!object) { AMErrorMon::report(AMErrorReport(this, AMErrorReport::Alert, -3, QString("Error copying the scan with ID '%1' out of the import database. Your database might be corrupted. Please report this problem to the Acquaman developers.").arg(scanIds.at(i)))); continue; } AMScan* scan = qobject_cast<AMScan*>(object); if(!scan) { AMErrorMon::report(AMErrorReport(this, AMErrorReport::Alert, -4, QString("Error copying the scan with ID '%1' out of the import database: it wasn't a scan object. Your database might be corrupted. Please report this problem to the Acquaman developers.").arg(scanIds.at(i)))); delete object; continue; } scan->retain(this); // update scan fields: scan->setRunId(s2dRunIds_.value(scan->runId(), -1)); scan->setSampleId(s2dSampleIds_.value(scan->sampleId(), -1)); // copy the raw data: filePath and additionalFilePaths QDir destinationDir(destinationPath_); // Assuming paths always in relative file format. QString filePath = scan->filePath(); // Does the file exist already in the destination? if(destinationDir.exists(filePath)) { // I know we're not supposed to be a GUI module... but is it okay to prompt here for what to do? if(userSaysShouldSkipDuplicateScan(scan)) { scan->release(this); continue; } filePath = makeUniqueFileName(destinationPath_, filePath); } copyFileAndMakeFoldersAsRequired(sourcePath_ % "/" % scan->filePath(), destinationPath_ % "/" % filePath); scan->setFilePath(filePath); // Copy additional file paths: QStringList extraPaths = scan->additionalFilePaths(); for(int j=0, cc=extraPaths.count(); j<cc; j++) { QString destinationFilePath = extraPaths.at(j); if(destinationDir.exists(destinationFilePath)) { destinationFilePath = makeUniqueFileName(destinationPath_, destinationFilePath); } copyFileAndMakeFoldersAsRequired(sourcePath_ % "/" % extraPaths.at(j), destinationPath_ % "/" % destinationFilePath); extraPaths[j] = destinationFilePath; } scan->setAdditionalFilePaths(extraPaths); // OK. raw data copied and file paths adjusted. Just store this scan in the new db: if(!scan->storeToDb(destinationDb_, false)) { // HOLD UP! Don't generate those thumbnails... We'll do a direct copy. AMErrorMon::report(AMErrorReport(this, AMErrorReport::Alert, -4, QString("Error copying the scan with ID '%1' into the new database. Your database might be corrupted. Please report this problem to the Acquaman developers.").arg(scanIds.at(i)))); scan->release(this); continue; } // Copy the thumbnails from the old DB to the new DB: // find the old thumbnails: QStringList thumbnailCols; thumbnailCols << "objectId" << "objectTableName" << "number" << "type" << "title" << "subtitle" << "thumbnail"; int thumbnailFirstId = -1; int thumbnailCount = 0; QSqlQuery q = sourceDb_->select(AMDbObjectSupport::thumbnailTableName(), thumbnailCols.join(","), "objectId = ? AND objectTableName = ?"); q.bindValue(0, scanIds.at(i)); q.bindValue(1, scanTableName); if(!q.exec()) { AMErrorMon::report(AMErrorReport(this, AMErrorReport::Alert, -4, QString("Error copying the the thumbnails for scan with ID '%1' out of the old database. Please report this problem to the Acquaman developers.").arg(scanIds.at(i)))); } else { if(q.first()) { QVariantList thumbnailVals; thumbnailVals << q.value(0) << q.value(1) << q.value(2) << q.value(3) << q.value(4) << q.value(5) << q.value(6); thumbnailVals[0] = scan->id(); // update the scan id from old DB to new DB's value thumbnailFirstId = destinationDb_->insertOrUpdate(0, AMDbObjectSupport::thumbnailTableName(), thumbnailCols, thumbnailVals); if(thumbnailFirstId > 0) thumbnailCount++; else AMErrorMon::report(AMErrorReport(this, AMErrorReport::Alert, -4, QString("Error copying the the thumbnails for scan with ID '%1' into the new database. Please report this problem to the Acquaman developers.").arg(scanIds.at(i)))); } while(q.next()) { QVariantList thumbnailVals; thumbnailVals << q.value(0) << q.value(1) << q.value(2) << q.value(3) << q.value(4) << q.value(5) << q.value(6); thumbnailVals[0] = scan->id(); // update the scan id from old DB to new DB's value if(destinationDb_->insertOrUpdate(0, AMDbObjectSupport::thumbnailTableName(), thumbnailCols, thumbnailVals) > 0) thumbnailCount++; } } // Finally, update the thumbnail references in the scan object: // now that we know where the thumbnails are, update this in the object table if(!destinationDb_->update(scan->id(), scanTableName, QStringList() << "thumbnailCount" << "thumbnailFirstId", QVariantList() << thumbnailCount << thumbnailFirstId)) { AMErrorMon::report(AMErrorReport(this, AMErrorReport::Alert, AMSCANDATABASEIMPORTCONTROLLER_ERROR_STORING_UPDATED_THUMBNAIL_COUNT_AND_FIRST_ID, QString("While importing, error trying to store the updated thumbnail count and firstThumbnailId for database object %1. Please report this problem to the Acquaman developers.").arg(scanIds.at(i)))); } // What experiments was this scan in? Need to map and add in new database. QList<int> experiments = AMExperiment::experimentsContainingScan(scanIds.at(i), sourceDb_); foreach(int oldExperimentId, experiments) { int newExperimentId = s2dExperimentIds_.value(oldExperimentId); if(newExperimentId > 0) AMExperiment::addScanToExperiment(scan->id(), newExperimentId, destinationDb_); } // All done! scan->release(); qApp->sendPostedEvents(); qApp->processEvents(); }
void pdp::EMClassification::work(LungDataset& input, LungDataset& output) { std::cout << "performing the expectation maximization step!" << std::endl; typedef itk::Image<float, 3> ImageType; typedef itk::Image<unsigned char, 3> MaskType; mitk::Image::Pointer mitkImage = input.getImage("Lungs"); mitkImage->DisconnectPipeline(); ImageType::Pointer img; mitk::CastToItkImage(mitkImage, img); mitk::Image::Pointer mitkMask = input.getImage("Otsu"); mitkMask->DisconnectPipeline(); MaskType::Pointer mask; mitk::CastToItkImage(mitkMask, mask); typedef itk::Vector< double, 1 > MeasurementVectorType; typedef itk::Statistics::ListSample< MeasurementVectorType > SampleType; SampleType::Pointer sample = prepareSample(img, mask); emit stepProgress(0.33f); //numberOfClasses = 4; typedef itk::Array< double > ParametersType; ParametersType params( 2 ); std::vector< ParametersType > initialParameters( numberOfClasses ); params[0] = -920.0; params[1] = 2500.0;//std = 50 initialParameters[0] = params; params[0] = -835.0; params[1] = 1600.0;//std = 40 initialParameters[1] = params; params[0] = -720.0; params[1] = 900.0;//std = 30 initialParameters[2] = params; params[0] = -570.0; params[1] = 900.0;//std = 30 initialParameters[3] = params; itk::Array< double > initialProportions(numberOfClasses); initialProportions[0] = 0.8; initialProportions[1] = 0.1; initialProportions[2] = 0.05; initialProportions[3] = 0.05; expectationMaximization(initialParameters, initialProportions, sample); emit stepProgress(0.66f); MaskType::Pointer nonLungTissueMask = classify(img, mask); // Cast the ITK -> MITK image and add it to the datatree. mitk::Image::Pointer mitkOut = mitk::Image::New(); mitk::CastToMitkImage(nonLungTissueMask, mitkOut); output.addImage(mitkOut, "nonLungTissueMask"); output.getDataStore()->Remove(output.getDataStore()->GetNamedNode("Otsu")); emit stepProgress(1.00f); }
void pdp::ThickeningDetector::onStepProgress(float totalpercent) { emit stepProgress(totalpercent); }