void StepScan::plotCurve() { // Get the name of the dataset to produce the plot title std::string title = m_inputWSName.substr(2); // qtiplot may unhelpfully change '_' to '-' so I need to as well std::replace(title.begin(), title.end(), '_', '-'); // Figure out the axis titles const std::string xAxisTitle = m_uiForm.plotVariable->currentText().toStdString(); std::string yAxisTitle = "Counts"; const std::string normalization = m_uiForm.normalization->currentText().toStdString(); if ( normalization == "nothing") /* Do nothing */; else if ( normalization == "time" ) yAxisTitle += " / second"; else if ( normalization == "proton_charge" ) yAxisTitle += " / picocoulomb"; else yAxisTitle += " / " + normalization; // Has to be done via python std::string pyCode = "g = graph('" + title + "')\n" "if g is None:\n" " g = plotSpectrum('" + m_plotWSName + "',0,type=Layer.Scatter)\n" " l = g.activeLayer()\n" " l.legend().hide()\n" " l.removeTitle()\n" " setWindowName(g,'" + title + "')\n" " g.setWindowLabel('Step Scan')\n" "l = g.activeLayer()\n" "l.setAxisTitle(Layer.Bottom,'" + xAxisTitle + "')\n" "l.setAxisTitle(Layer.Left,'" + yAxisTitle + "')"; runPythonCode( QString::fromStdString(pyCode) ); }
/** Show the user the dialog for an algorithm * @param algorithm : [input] The algorithm */ void QDataProcessorWidget::showAlgorithmDialog(const std::string &algorithm) { std::stringstream pythonSrc; pythonSrc << "try:\n"; pythonSrc << " algm = " << algorithm << "Dialog()\n"; pythonSrc << "except:\n"; pythonSrc << " pass\n"; runPythonCode(QString::fromStdString(pythonSrc.str()), false); }
SANSEventSlicing::ChargeAndTime SANSEventSlicing::runSliceEvent(const QString & code) { QString result = runPythonCode(code).simplified(); checkPythonOutput(result); return values2ChargeAndTime(result); }
/** Show the user the dialog for an algorithm */ void QtReflMainView::showAlgorithmDialog(const std::string& algorithm) { std::stringstream pythonSrc; pythonSrc << "try:\n"; pythonSrc << " " << algorithm << "Dialog()\n"; pythonSrc << "except:\n"; pythonSrc << " pass\n"; runPythonCode(QString::fromStdString(pythonSrc.str())); }
/// Open a new graph window and plot a fitting parameter /// against a log value. The name of the parameter to plot /// and the log name must be selected in m_fitOptionsBrowser. void MultiDatasetFit::showParameterPlot() { auto table = m_fitOptionsBrowser->getProperty("OutputWorkspace"); auto logValue = m_fitOptionsBrowser->getProperty("LogValue"); auto parName = m_fitOptionsBrowser->getParameterToPlot(); if (table.isEmpty() || logValue.isEmpty() || parName.isEmpty()) return; auto pyInput = QString("table = importTableWorkspace('%1')\n" "plotTableColumns(table, ('%2','%3_Err'))\n").arg(table, parName, parName); runPythonCode(pyInput); }
void StepScan::launchInstrumentWindow() { // Gotta do this in python std::string pyCode = "instrument_view = getInstrumentView('" + m_inputWSName + "',2)\n" "instrument_view.show()"; runPythonCode( QString::fromStdString(pyCode) ); // Attach the observers so that if a mask workspace is generated over in the instrument view, // it is automatically selected by the combobox over here AnalysisDataService::Instance().notificationCenter.addObserver(m_addObserver); AnalysisDataService::Instance().notificationCenter.addObserver(m_replObserver); }
/** Plot a workspace */ void QtReflMainView::plotWorkspaces(const std::set<std::string>& workspaces) { if(workspaces.empty()) return; std::stringstream pythonSrc; pythonSrc << "base_graph = None\n"; for(auto ws = workspaces.begin(); ws != workspaces.end(); ++ws) pythonSrc << "base_graph = plotSpectrum(\"" << *ws << "\", 0, True, window = base_graph)\n"; pythonSrc << "base_graph.activeLayer().logLogAxes()\n"; runPythonCode(QString::fromStdString(pythonSrc.str())); }
/** * Check if a file corresponds to a histogram workspace * @param fileName: the file name * @returns true if it is a histogram workspace */ bool SANSAddFiles::isEventWorkspace(QString fileName) { auto isEvent = false; fileName.replace("\\", "/"); QString code_torun = "import ISISCommandInterface as i\n"; code_torun += "i.check_if_event_workspace(file_name='"; code_torun += fileName; code_torun += +"')\n"; auto status = runPythonCode(code_torun, false); if (status.contains(m_constants.getPythonTrueKeyword())) { isEvent = true; } return isEvent; }
/** Show the user the dialog for "LoadReflTBL" */ void QDataProcessorWidget::showImportDialog() { std::stringstream pythonSrc; pythonSrc << "try:\n"; pythonSrc << " algm = " << "LoadTBL" << "Dialog()\n"; pythonSrc << " print algm.getPropertyValue(\"OutputWorkspace\")\n"; pythonSrc << "except:\n"; pythonSrc << " pass\n"; // outputWorkspaceName will hold the name of the workspace // otherwise this should be an empty string. QString outputWorkspaceName = runPythonCode(QString::fromStdString(pythonSrc.str()), false); m_toOpen = outputWorkspaceName.trimmed().toStdString(); // notifying the presenter that a new table should be opened // The presenter will ask about any unsaved changes etc // before opening the new table m_presenter->notify(DataProcessorPresenter::OpenTableFlag); }
SANSEventSlicing::ChargeAndTime SANSEventSlicing::getFullChargeAndTime(const QString & name_ws) { QString code; QTextStream stream (&code); stream << "import SANSUtility as su\n" << "import sys\n" << "ws = mtd['"<<name_ws << "']\n" << "try:\n" << " charge, t_passed = su.getChargeAndTime(ws)\n" << " print '%.2f, %.2f' %(charge, t_passed)\n" << "except :\n" << " print 'EXCEPTION:',sys.exc_info()[1]\n"; QString result = runPythonCode(code).simplified(); checkPythonOutput(result); return values2ChargeAndTime(result); }
/* * Check the validity of the time shift input field for added event files */ bool SANSAddFiles::checkValidityTimeShiftsForAddedEventFiles() { bool state = true; if (m_SANSForm->comboBox_histogram_choice->currentIndex() == SAVEASEVENTDATA && m_SANSForm->overlayCheckBox->isChecked()) { QString code_torun = "import ISISCommandInterface as i\n"; code_torun += "i.check_time_shifts_for_added_event_files(number_of_files="; code_torun += QString::number(m_SANSForm->toAdd_List->count() - 1); code_torun += ", time_shifts='" + m_SANSForm->eventToHistBinning->text() + "')\n"; QString status = runPythonCode(code_torun, false); if (!status.isEmpty()) { g_log.warning() << status.toStdString(); } if (status.contains("Error")) { state = false; } } return state; }
/** Executes the add_runs() function inside the SANSadd2 script */ void SANSAddFiles::runPythonAddFiles() { // Check the validty of the input for the if (!checkValidityTimeShiftsForAddedEventFiles()) { return; } if (m_pythonRunning) { // it is only possible to run one python script at a // time return; } if (ConfigService::Instance().getString("defaultsave.directory").empty()) { QMessageBox::critical(this, "Setting Required", "Unable to add runs until a default save directory " "has been specified. Please set this using the " "Manage User Directories dialog."); return; } add2Runs2Add(); QString code_torun = "import SANSadd2\n"; code_torun += "print SANSadd2.add_runs(("; // there are multiple file list inputs that can be filled in loop through them for (int i = 0; i < m_SANSForm->toAdd_List->count(); ++i) { QString filename = m_SANSForm->toAdd_List->item(i)->data(Qt::WhatsThisRole).toString(); // allow but do nothing with empty entries if (!filename.isEmpty()) { // Make sure that the file separators are valid filename.replace("\\", "/"); code_torun += "'" + filename + "',"; } } if (code_torun.endsWith(',')) { // we've made a comma separated list, there // can be no comma at the end code_torun.truncate(code_torun.size() - 1); } // pass the current instrument code_torun += "),'" + m_SANSForm->inst_opt->currentText() + "', '"; QString ext = m_SANSForm->file_opt->itemData(m_SANSForm->file_opt->currentIndex()) .toString(); code_torun += ext + "'"; code_torun += ", rawTypes=("; std::vector<std::string>::const_iterator end = m_rawExts.end(); for (std::vector<std::string>::const_iterator j = m_rawExts.begin(); j != end; ++j) { code_torun += "'" + QString::fromStdString(*j) + "',"; } // remove the comma that would remain at the end of the list code_torun.truncate(code_torun.length() - 1); code_torun += ")"; QString lowMem = m_SANSForm->loadSeparateEntries->isChecked() ? "True" : "False"; code_torun += ", lowMem=" + lowMem; QString overlay = m_SANSForm->overlayCheckBox->isChecked() ? "True" : "False"; // In case of event data, check if the user either wants // 0. Custom historgram binning // 1. A binning which is set by the data set // 2. To save the actual event data switch (m_SANSForm->comboBox_histogram_choice->currentIndex()) { case CUSTOMBINNING: code_torun += ", binning='" + m_SANSForm->eventToHistBinning->text() + "'"; break; case FROMMONITORS: break; case SAVEASEVENTDATA: code_torun += ", saveAsEvent=True"; code_torun += ", isOverlay=" + overlay; code_torun += ", time_shifts=" + createPythonStringList(m_SANSForm->eventToHistBinning->text()); break; default: break; } code_torun += ")\n"; g_log.debug() << "Executing Python: \n" << code_torun.toStdString() << '\n'; m_SANSForm->sum_Btn->setEnabled(false); m_pythonRunning = true; // call the algorithms by executing the above script as Python QString status = runPythonCode(code_torun, false); // reset the controls and display any errors m_SANSForm->sum_Btn->setEnabled(true); m_pythonRunning = false; if (status.startsWith("The following file has been created:")) { QMessageBox::information(this, "Files summed", status); } else if (status.startsWith("Error copying log file:")) { QMessageBox::warning(this, "Error adding files", status); } else { if (status.isEmpty()) { status = "Could not sum files, there may be more\ninformation in the " "Results Log window"; } QMessageBox::critical(this, "Error adding files", status); } }
/** * Allow Python to be called locally. */ void IndirectDataAnalysis::initLocalPython() { QString pyInput = "from mantid.simpleapi import *"; QString pyOutput = runPythonCode(pyInput).trimmed(); loadSettings(); }