Skip to content

Commit

Permalink
Re #6151 starting to make UI compatible with APIv2
Browse files Browse the repository at this point in the history
  • Loading branch information
mdoucet committed Dec 19, 2012
1 parent bfea0f2 commit f3e93d6
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,16 @@ def PyExec(self):
meas_err = None
if alg.existsProperty("MeasuredTransmission"):
meas_trans = alg.getProperty("MeasuredTransmission").value
property_manager.declareProperty("MeasuredBckTransmissionValue", meas_trans)
if property_manager.existsProperty("MeasuredBckTransmissionValue"):
property_manager.setProperty("MeasuredBckTransmissionValue", meas_trans)
else:
property_manager.declareProperty("MeasuredBckTransmissionValue", meas_trans)
if alg.existsProperty("MeasuredError"):
meas_err = alg.getProperty("MeasuredError").value
property_manager.declareProperty("MeasuredBckTransmissionError", meas_err)
if property_manager.existsProperty("MeasuredBckTransmissionValue"):
property_manager.setProperty("MeasuredBckTransmissionError", meas_err)
else:
property_manager.declareProperty("MeasuredBckTransmissionError", meas_err)

if alg.existsProperty("OutputMessage"):
output_msg += alg.getProperty("OutputMessage").value+'\n'
Expand Down Expand Up @@ -317,6 +323,17 @@ def process_data_file(self, workspace):
alg.execute()
if alg.existsProperty("OutputMessage"):
output_msg += alg.getProperty("OutputMessage").value+'\n'

# Store sensitivity beam center so that we can access it later
if beam_center_x is not None and beam_center_y is not None:
if property_manager.existsProperty("SensitivityBeamCenterXUsed"):
property_manager.setProperty("SensitivityBeamCenterXUsed", beam_center_x)
else:
property_manager.declareProperty("SensitivityBeamCenterXUsed", beam_center_y)
if property_manager.existsProperty("SensitivityBeamCenterYUsed"):
property_manager.setProperty("SensitivityBeamCenterYUsed", beam_center_y)
else:
property_manager.declareProperty("SensitivityBeamCenterYUsed", beam_center_y)

return output_msg

Expand Down
16 changes: 16 additions & 0 deletions Code/Mantid/scripts/Interface/reduction_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,18 @@ def _update_file_menu(self):
debugAction.setStatusTip(debug_menu_item_str)
self.connect(debugAction, QtCore.SIGNAL("triggered()"), self._debug_mode)

api_menu_item_str = "Use Mantid Python API v2"
if self.general_settings.api2:
api_menu_item_str = "Use old Mantid Python API"
apiAction = QtGui.QAction(api_menu_item_str, self)
apiAction.setShortcut("Ctrl+A")
apiAction.setStatusTip("Select Mantid Python API")
self.connect(apiAction, QtCore.SIGNAL("triggered()"), self._change_api)

self.tools_menu.clear()
self.tools_menu.addAction(instrAction)
self.tools_menu.addAction(debugAction)
self.tools_menu.addAction(apiAction)

recent_files = []
for fname in self._recent_files:
Expand All @@ -280,6 +289,13 @@ def _debug_mode(self, mode=None):
self._new()
self.setup_layout()

def _change_api(self):
"""
Toggle the Python API version to use
"""
self.general_settings.api2 = not self.general_settings.api2
self._update_file_menu()

def _interface_choice(self, advanced_ui=None):
if advanced_ui is None:
advanced_ui = self.general_settings.advanced
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self, name, settings):
self.LAST_REDUCTION_NAME = ".mantid_last_HFIR_reduction.xml"

# Scripter object to interface with Mantid
self.scripter = HFIRReductionScripter(name=name)
self.scripter = HFIRReductionScripter(name=name, settings = self._settings)

# Instrument description
self.attach(SANSInstrumentWidget(settings = self._settings, name=name, data_proxy=DataProxy))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class HFIRReductionScripter(BaseReductionScripter):
will each have their own UI representation.
"""

def __init__(self, name="BIOSANS"):
def __init__(self, name="BIOSANS", settings=None):
super(HFIRReductionScripter, self).__init__(name=name)

self._settings = settings

def to_script(self, file_name=None):
"""
Spits out the text of a reduction script with the current state.
Expand All @@ -24,23 +25,33 @@ def to_script(self, file_name=None):
script = "# HFIR reduction script\n"
script += "# Script automatically generated on %s\n\n" % time.ctime(time.time())

script += "from MantidFramework import *\n"
script += "mtd.initialise(False)\n"
script += "from reduction.instruments.sans.hfir_command_interface import *\n"
if self._settings.api2:
script += "import mantid\n"
script += "from mantid.simpleapi import *\n"
script += "from reduction_workflow.instruments.sans.hfir_command_interface import *\n"
else:
script += "from MantidFramework import *\n"
script += "mtd.initialise(False)\n"
script += "from reduction.instruments.sans.hfir_command_interface import *\n"

script += "\n"

for item in self._observers:
if item.state() is not None:
script += str(item.state())

xml_process = "None"
xml_process = ''
if file_name is None:
xml_process = os.path.join(self._output_directory, "HFIRSANS_process.xml")
xml_process = os.path.normpath(xml_process)
self.to_xml(xml_process)

script += "SaveIqAscii(process=%r)\n" % xml_process
script += "Reduce1D()\n"
if self._settings.api2:
script += "SaveIq(process=%r)\n" % xml_process
else:
script += "SaveIqAscii(process=%r)\n" % xml_process

script += "Reduce()\n"

if file_name is not None:
f = open(file_name, 'w')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
# Check whether we are running in MantidPlot
IS_IN_MANTIDPLOT = False
try:
from MantidFramework import *
mtd.initialise()
from reduction.command_interface import *
import mantidplot
IS_IN_MANTIDPLOT = True
except:
Expand All @@ -20,9 +17,14 @@ def update(self):
Update data members according to reduction results
"""
if IS_IN_MANTIDPLOT:
if self.PYTHON_API==1:
from reduction.command_interface import ReductionSingleton
else:
from reduction_workflow.command_interface import ReductionSingleton
self.log_text = ReductionSingleton().log_text
try:
for item in ReductionSingleton().output_workspaces:
mantidplot.plotSpectrum(item, 0, True)
if hasattr(ReductionSingleton(), "output_workspaces"):
for item in ReductionSingleton().output_workspaces:
mantidplot.plotSpectrum(item, 0, True)
except:
raise RuntimeError, "Could not plot resulting output\n %s" % sys.exc_value
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@
from reduction_gui.reduction.scripter import BaseScriptElement
from reduction_gui.reduction.sans.hfir_sample_script import SampleData

# Check whether Mantid is available
try:
from MantidFramework import *
mtd.initialise(False)
from reduction.instruments.sans.hfir_command_interface import *
HAS_MANTID = True
except:
HAS_MANTID = False

# Check whether we are running in MantidPlot
IS_IN_MANTIDPLOT = False
try:
Expand Down Expand Up @@ -125,11 +116,23 @@ def update(self):
"""
Update data member from reduction output
"""
if HAS_MANTID and ReductionSingleton()._background_subtracter is not None:
trans = ReductionSingleton()._background_subtracter.get_transmission()
if trans is not None:
self.bck_transmission = trans[0]
self.bck_transmission_spread = trans[1]
if IS_IN_MANTIDPLOT:
if self.PYTHON_API==1:
from reduction.command_interface import ReductionSingleton
if ReductionSingleton()._background_subtracter is not None:
trans = ReductionSingleton()._background_subtracter.get_transmission()
if trans is not None:
self.bck_transmission = trans[0]
self.bck_transmission_spread = trans[1]
else:
from mantid.api import PropertyManagerDataService
from reduction_workflow.command_interface import ReductionSingleton
property_manager_name = ReductionSingleton().get_reduction_table_name()
property_manager = PropertyManagerDataService.retrieve(property_manager_name)
if property_manager.existsProperty("MeasuredBckTransmissionValue"):
self.bck_transmission = property_manager.getProperty("MeasuredBckTransmissionValue").value
if property_manager.existsProperty("MeasuredBckTransmissionError"):
self.bck_transmission_spread = property_manager.getProperty("MeasuredBckTransmissionError").value

def to_xml(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@
import os
from reduction_gui.reduction.scripter import BaseScriptElement

# Check whether Mantid is available
try:
from MantidFramework import *
mtd.initialise(False)
from reduction.instruments.sans.hfir_command_interface import *
HAS_MANTID = True
except:
HAS_MANTID = False

# Check whether we are running in MantidPlot
IS_IN_MANTIDPLOT = False
try:
Expand Down Expand Up @@ -260,15 +251,40 @@ def update(self):
"""
Update data members according to reduction results
"""
if HAS_MANTID and ReductionSingleton()._beam_finder is not None:
pos = ReductionSingleton()._beam_finder.get_beam_center()
self.x_position = pos[0]
self.y_position = pos[1]

if self.use_sample_beam_center:
self.flood_x_position = pos[0]
self.flood_y_position = pos[1]
elif self.sensitivity_corr:
pos_flood = ReductionSingleton()._sensitivity_correcter.get_beam_center()
self.flood_x_position = pos_flood[0]
self.flood_y_position = pos_flood[1]
if IS_IN_MANTIDPLOT:
if self.PYTHON_API==1:
from reduction.command_interface import ReductionSingleton
if ReductionSingleton()._beam_finder is not None:
pos = ReductionSingleton()._beam_finder.get_beam_center()
self.x_position = pos[0]
self.y_position = pos[1]

if self.use_sample_beam_center:
self.flood_x_position = self.x_position
self.flood_y_position = self.y_position
elif self.sensitivity_corr:
pos_flood = ReductionSingleton()._sensitivity_correcter.get_beam_center()
self.flood_x_position = pos_flood[0]
self.flood_y_position = pos_flood[1]
else:
from mantid.api import PropertyManagerDataService
from reduction_workflow.command_interface import ReductionSingleton
property_manager_name = ReductionSingleton().get_reduction_table_name()
property_manager = PropertyManagerDataService.retrieve(property_manager_name)
if property_manager.existsProperty("LatestBeamCenterX"):
self.x_position = property_manager.getProperty("LatestBeamCenterX").value
if property_manager.existsProperty("LatestBeamCenterY"):
self.y_position = property_manager.getProperty("LatestBeamCenterY").value

if self.use_sample_beam_center:
self.flood_x_position = self.x_position
self.flood_y_position = self.y_position
elif self.sensitivity_corr:
if property_manager.existsProperty("SensitivityBeamCenterXUsed"):
self.flood_x_position = property_manager.getProperty("SensitivityBeamCenterXUsed").value
else:
self.flood_x_position = self.x_position
if property_manager.existsProperty("SensitivityBeamCenterYUsed"):
self.flood_y_position = property_manager.getProperty("SensitivityBeamCenterYUsed").value
else:
self.flood_y_position = self.y_position
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class BaseScriptElement(object):
"""

UPDATE_1_CHANGESET_CUTOFF = 10735
PYTHON_API = 1

def __str__(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class GeneralSettings(QtCore.QObject):
instrument_name = ''
facility_name = ''

# Mantid Python API version
api2 = False

data_updated = QtCore.pyqtSignal('PyQt_PyObject','PyQt_PyObject')
progress = QtCore.pyqtSignal(int)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ def get_state(self):
"""
Returns an object with the state of the interface
"""
return Output()
m = Output()
if self._settings.api2: m.PYTHON_API=2
return m
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def __init__(self, parent=None):
if state is not None:
self.set_state(state)
else:
self.set_state(Background())
m = Background()
if self._settings.api2: m.PYTHON_API=2
self.set_state(m)

self._last_direct_state = None
self._last_spreader_state = None
Expand Down Expand Up @@ -157,7 +159,7 @@ def get_state(self):
Returns an object with the state of the interface
"""
m = Background()

if self._settings.api2: m.PYTHON_API=2
m.background_corr = self._content.background_chk.isChecked()
m.background_file = str(self._content.background_edit.text())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def __init__(self, parent=None):
if state is not None:
self.set_state(state)
else:
self.set_state(Detector())
m = Detector()
if self._settings.api2: m.PYTHON_API=2
self.set_state(m)

def initialize_content(self):
"""
Expand Down Expand Up @@ -237,6 +239,7 @@ def get_state(self):
Returns an object with the state of the interface
"""
m = Detector()
if self._settings.api2: m.PYTHON_API=2

# Mask
m.x_position = util._check_and_get_float_line_edit(self._content.x_pos_edit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
IS_IN_MANTIDPLOT = False
try:
import mantidplot
from MantidFramework import *
mtd.initialise(False)
from mantidsimple import *
IS_IN_MANTIDPLOT = True
from reduction import extract_workspace_name
except:
pass

Expand Down Expand Up @@ -146,8 +142,9 @@ def initialize_content(self):
self._summary.dark_plot_button.hide()
self._summary.scale_data_plot_button.hide()

def _mask_plot_clicked(self):
self.mask_ws = "__mask_%s" % extract_workspace_name(str(self._summary.mask_edit.text()))
def _mask_plot_clicked(self):
ws_name = os.path.basename(str(self._summary.mask_edit.text()))
self.mask_ws = "__mask_%s" % ws_name
self.show_instrument(self._summary.mask_edit.text, workspace=self.mask_ws, tab=2, reload=self.mask_reload, mask=self._masked_detectors)
self._masked_detectors = []
self.mask_reload = False
Expand Down Expand Up @@ -408,10 +405,21 @@ def get_state(self):
m.mask_file = unicode(self._summary.mask_edit.text())
m.detector_ids = self._masked_detectors
if self._in_mantidplot:
if mtd.workspaceExists(self.mask_ws):
masked_detectors = ExtractMask(InputWorkspace=self.mask_ws, OutputWorkspace="__edited_mask")
ids_str = masked_detectors.getPropertyValue("DetectorList")
m.detector_ids = map(int, ids_str.split(','))

if self._settings.api2:
from MantidFramework import mtd
mtd.initialise(False)
import mantidsimple
if mtd.workspaceExists(self.mask_ws):
masked_detectors = mantidsimple.ExtractMask(InputWorkspace=self.mask_ws, OutputWorkspace="__edited_mask")
ids_str = masked_detectors.getPropertyValue("DetectorList")
m.detector_ids = map(int, ids_str.split(','))
else:
from mantid.api import AnalysisDataService
import mantid.simpleapi as api
if AnalysisDataService.doesExist(self.mask_ws):
masked_detectors = api.ExtractMask(InputWorkspace=self.mask_ws, OutputWorkspace="__edited_mask")
ids_str = masked_detectors.getPropertyValue("DetectorList")
m.detector_ids = map(int, ids_str.split(','))

self._settings.emit_key_value("DARK_CURRENT", QtCore.QString(str(self._summary.dark_file_edit.text())))
return m

0 comments on commit f3e93d6

Please sign in to comment.