Skip to content

Commit

Permalink
Re #6151 More transition to API2
Browse files Browse the repository at this point in the history
  • Loading branch information
mdoucet committed Dec 20, 2012
1 parent 2bc5085 commit 4851f74
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,13 @@ class DataSet(object):
TABLE_NAME = "dataset"
data_type_cls = DataType

def __init__(self, run_number, title, run_start, duration,
sdd, id=None, python_api=1):
def __init__(self, run_number, title, run_start, duration, sdd, id=None):
self.run_number = run_number
self.title = title
self.run_start = run_start
self.duration = duration
self.sdd = sdd
self.id = id
self.python_api = python_api

@classmethod
def header(cls):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from data_cat import DataType
import re
import time
import datetime

# Check whether Mantid is available
try:
from MantidFramework import *
mtd.initialise(False)
import mantidsimple
from mantid.api import AnalysisDataService
import mantid.simpleapi as api
HAS_MANTID = True
except:
HAS_MANTID = False
Expand All @@ -29,7 +29,7 @@ def __init__(self, run_number, title, run_start, duration, sdd):
@classmethod
def load_meta_data(cls, file_path, outputWorkspace):
try:
mantidsimple.LoadEventNexus(Filename=file_path, OutputWorkspace=outputWorkspace, MetaDataOnly=True)
api.LoadEventNexus(Filename=file_path, OutputWorkspace=outputWorkspace, MetaDataOnly=True)
return True
except:
return False
Expand All @@ -56,12 +56,14 @@ def handle(cls, file_path):
def read_properties(cls, ws, run, cursor):
def read_prop(prop):
try:
return str(mtd[ws].getRun().getProperty(prop).value)
ws_object = AnalysisDataService.retrieve(ws)
return str(ws_object.getRun().getProperty(prop).value)
except:
return ""
def read_series(prop):
try:
return float(mtd[ws].getRun().getProperty(prop).getStatistics().mean)
ws_object = AnalysisDataService.retrieve(ws)
return float(ws_object.getRun().getProperty(prop).getStatistics().mean)
except:
return -1

Expand All @@ -71,8 +73,15 @@ def read_series(prop):

title = read_prop("run_title")
t_str = read_prop("start_time")
t = time.strptime(t_str, '%Y-%m-%dT%H:%M:%S')
run_start = time.strftime('%y-%m-%d %H:%M', t)
# Get rid of the training microseconds
toks = t_str.split('.')
if len(toks)>=2:
t_str=toks[0]
t = datetime.datetime.strptime(t_str, '%Y-%m-%dT%H:%M:%S')
# TZ offset
offset = datetime.datetime.now()-datetime.datetime.utcnow()
t = t+offset
run_start = t.strftime('%y-%m-%d %H:%M')

duration = read_prop("duration")
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sys
# Check whether Mantid is available
try:
from mantid.api import AnalysisDataService
from mantid.kernel import Logger
import mantid.simpleapi as api
HAS_MANTID = True
except:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@
import os
import time

# Check whether Mantid is available
try:
from mantid.api import AnalysisDataService
import mantid.simpleapi as api
HAS_MANTID = True
except:
HAS_MANTID = False

class HFIRDataType(DataType):
TABLE_NAME="hfir_datatype"

class HFIRDataSet(DataSet):
TABLE_NAME="hfir_dataset"
data_type_cls = HFIRDataType
python_api = 1

def __init__(self, run_number, title, run_start, duration, sdd, python_api=1):
def __init__(self, run_number, title, run_start, duration, sdd):
super(HFIRDataSet, self).__init__(run_number, title, run_start,
duration, sdd, python_api=1)
duration, sdd)

@classmethod
def load_meta_data(cls, file_path, outputWorkspace, python_api=1):
def load_meta_data(cls, file_path, outputWorkspace):
try:
if python_api==1:
import mantidsimple as api
else:
import mantid.simpleapi as api
api.LoadSpice2D(Filename=file_path, OutputWorkspace=outputWorkspace)
return True
except:
Expand All @@ -42,57 +45,17 @@ def handle(cls, file_path):
return handle

@classmethod
def find_with_api(cls, file_path, cursor, process_files=True, python_api=1):
"""
Find an entry in the database, or create on as needed
"""
run = cls.handle(file_path)
if run is None:
return None

t = (run,)
cursor.execute('select * from %s where run=?'% cls.TABLE_NAME, t)
rows = cursor.fetchall()

if len(rows) == 0:
if process_files:
log_ws = "__log"
if cls.load_meta_data(file_path,
outputWorkspace=log_ws,
python_api=python_api):
return cls.read_properties(log_ws, run, cursor,
python_api=python_api)
else:
return None
else:
return None
else:
row = rows[0]
return DataSet(row[1], row[2], row[3], row[4], row[5], id=row[0])


@classmethod
def read_properties(cls, ws, run, cursor, python_api=1):
def read_properties(cls, ws, run, cursor):
def read_prop(prop):
try:
if python_api==1:
from MantidFramework import mtd
return str(mtd[ws].getRun().getProperty(prop).value)
else:
from mantid.api import AnalysisDataService
ws_object = AnalysisDataService.retrieve(ws)
return str(ws_object.getRun().getProperty(prop).value)
ws_object = AnalysisDataService.retrieve(ws)
return str(ws_object.getRun().getProperty(prop).value)
except:
return ""
def read_series(prop):
try:
if python_api==1:
from MantidFramework import mtd
return float(mtd[ws].getRun().getProperty(prop).getStatistics().mean)
else:
from mantid.api import AnalysisDataService
ws_object = AnalysisDataService.retrieve(ws)
return str(ws_object.getRun().getProperty(prop).getStatistics().mean)
ws_object = AnalysisDataService.retrieve(ws)
return str(ws_object.getRun().getProperty(prop).getStatistics().mean)
except:
return -1

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import sys

# Check whether Mantid is available
try:
from mantid.api import AnalysisDataService
from mantid.kernel import Logger
import mantid.simpleapi as api
HAS_MANTID = True
except:
HAS_MANTID = False

class DataProxy(object):
"""
Class used to load a data file temporarily to extract header information
Expand All @@ -11,66 +19,29 @@ class DataProxy(object):
data_ws = ''
sample_thickness = None
beam_diameter = None
python_api = 1

## Error log
errors = []

def __init__(self, data_file, workspace_name=None, python_api=1):
def __init__(self, data_file, workspace_name=None):
self.errors = []
self.python_api = python_api
if python_api==1:
return self._load_python_api_1(data_file, workspace_name)
else:
return self._load_python_api_2(data_file, workspace_name)

def _load_python_api_1(self, data_file, workspace_name):
from MantidFramework import mtd
import mantidsimple
from reduction.instruments.sans.hfir_load import LoadRun
from reduction.instruments.sans.sans_reducer import SANSReducer
import reduction.instruments.sans.hfir_instrument as hfir_instrument
try:
if workspace_name is None:
self.data_ws = "__raw_data_file"
else:
self.data_ws = str(workspace_name)
reducer = SANSReducer()
reducer.set_instrument(hfir_instrument.HFIRSANS())
loader = LoadRun(str(data_file))
loader.execute(reducer, self.data_ws)
x = mtd[self.data_ws].dataX(0)
self.wavelength = (x[0]+x[1])/2.0
self.wavelength_spread = x[1]-x[0]
self.sample_detector_distance = mtd[self.data_ws].getRun().getProperty("sample_detector_distance").value
self.sample_thickness = mtd[self.data_ws].getRun().getProperty("sample-thickness").value
self.beam_diameter = mtd[self.data_ws].getRun().getProperty("beam-diameter").value

mtd.sendLogMessage("Loaded data file: %s" % data_file)
except:
mtd.sendLogMessage("DataProxy: Error loading data file:\n%s" % sys.exc_value)
self.errors.append("Error loading data file:\n%s" % sys.exc_value)

def _load_python_api_2(self, data_file, workspace_name):
from mantid.api import AnalysisDataService
from mantid.kernel import Logger
import mantid.simpleapi as api
try:
if workspace_name is None:
self.data_ws = "__raw_data_file"
else:
self.data_ws = str(workspace_name)
api.HFIRLoad(Filename=str(data_file), OutputWorkspace=self.data_ws)
ws = AnalysisDataService.retrieve(self.data_ws)
x = ws.dataX(0)
self.wavelength = (x[0]+x[1])/2.0
self.wavelength_spread = x[1]-x[0]
self.sample_detector_distance = ws.getRun().getProperty("sample_detector_distance").value
self.sample_thickness = ws.getRun().getProperty("sample-thickness").value
self.beam_diameter = ws.getRun().getProperty("beam-diameter").value

Logger.get("hfir_data_proxy").information("Loaded data file: %s" % data_file)
except:
Logger.get("hfir_data_proxy").error("Error loading data file:\n%s" % sys.exc_value)
self.errors.append("Error loading data file:\n%s" % sys.exc_value)

if HAS_MANTID:
try:
if workspace_name is None:
self.data_ws = "__raw_data_file"
else:
self.data_ws = str(workspace_name)
api.HFIRLoad(Filename=str(data_file), OutputWorkspace=self.data_ws)
ws = AnalysisDataService.retrieve(self.data_ws)
x = ws.dataX(0)
self.wavelength = (x[0]+x[1])/2.0
self.wavelength_spread = x[1]-x[0]
self.sample_detector_distance = ws.getRun().getProperty("sample_detector_distance").value
self.sample_thickness = ws.getRun().getProperty("sample-thickness").value
self.beam_diameter = ws.getRun().getProperty("beam-diameter").value

Logger.get("hfir_data_proxy").information("Loaded data file: %s" % data_file)
except:
Logger.get("hfir_data_proxy").error("Error loading data file:\n%s" % sys.exc_value)
self.errors.append("Error loading data file:\n%s" % sys.exc_value)

Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
"""
# Check whether Mantid is available
try:
from MantidFramework import *
mtd.initialise(False)
from mantid.kernel import ConfigService, Logger, version_str
HAS_MANTID = True
except:
HAS_MANTID = False

try:
import mantidplot
HAS_MANTIDPLOT = True
except ImportError:
except:
HAS_MANTIDPLOT = False

import xml.dom.minidom
Expand Down Expand Up @@ -272,13 +271,13 @@ def __init__(self, name="", facility=""):
self._observers = []
self._output_directory = os.path.expanduser('~')
if HAS_MANTID:
config = ConfigService.Instance()
try:
head, tail = os.path.split(ConfigService().getUserFilename())
head, tail = os.path.split(config.getUserFilename())
if os.path.isdir(head):
self._output_directory = head
except:
# Doesn't really matter
pass
Logger.get("scripter").debug("Could not get user filename")

def clear(self):
"""
Expand Down Expand Up @@ -349,7 +348,7 @@ def to_xml(self, file_name=None):
xml_str += " <platform>%s</platform>\n" % platform.system()
xml_str += " <architecture>%s</architecture>\n" % str(platform.architecture())
if HAS_MANTID:
xml_str += " <mantid_version>%s</mantid_version>\n" % mantid_build_version()
xml_str += " <mantid_version>%s</mantid_version>\n" % version_str()

for item in self._observers:
if item.state() is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@
import ui.sans.ui_eqsans_instrument
import ui.sans.ui_eqsans_info

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

class SANSInstrumentWidget(BaseWidget):
"""
Widget that present instrument details to the user
Expand Down Expand Up @@ -205,8 +194,9 @@ def _resolution_clicked(self, is_checked):
self._summary.sample_apert_edit.setEnabled(is_checked)
self._summary.sample_apert_label.setEnabled(is_checked)

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 @@ -472,10 +462,11 @@ 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(','))
from mantid.api import AnalysisDataService
import mantid.simpleapi as api
if AnalysisDataService.doesExist(self.mask_ws):
ws, masked_detectors = api.ExtractMask(InputWorkspace=self.mask_ws, OutputWorkspace="__edited_mask")
m.detector_ids = [int(i) for i in masked_detectors]

# Resolution parameters
m.compute_resolution = self._summary.resolution_chk.isChecked()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ def get_data_info(self):
fname = str(self._content.background_edit.text())
if len(str(fname).strip())>0:
api = 2 if self._settings.api2 else 1
dataproxy = self._data_proxy(fname, "__background_raw",
python_api=api)
dataproxy = self._data_proxy(fname, "__background_raw")
if len(dataproxy.errors)>0:
#QtGui.QMessageBox.warning(self, "Error", dataproxy.errors[0])
return
Expand Down

0 comments on commit 4851f74

Please sign in to comment.