Skip to content

Commit

Permalink
Re #4048 Added data set tab
Browse files Browse the repository at this point in the history
  • Loading branch information
mdoucet committed Nov 4, 2011
1 parent 70737e1 commit 0c80261
Show file tree
Hide file tree
Showing 12 changed files with 655 additions and 133 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/scripts/Interface/compile_sans_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
os.system("pyuic4 -o ui/ui_trans_direct_beam.py ui/trans_direct_beam.ui")
os.system("pyuic4 -o ui/ui_trans_spreader.py ui/trans_spreader.ui")
os.system("pyuic4 -o ui/ui_instrument_dialog.py ui/instrument_dialog.ui")
os.system("pyuic4 -o ui/ui_data_catalog.py ui/data_catalog.ui")

# Example
#os.system("pyuic4 -o ui/ui_example.py ui/example.ui")
Expand Down
55 changes: 28 additions & 27 deletions Code/Mantid/scripts/Interface/reduction_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,44 @@
import traceback
from PyQt4 import QtGui, QtCore, uic

REDUCTION_WARNING = False
WARNING_MESSAGE = ""
# Check whether Mantid is available
try:
import reduction
if os.path.splitext(os.path.basename(reduction.__file__))[0] == "reduction":
REDUCTION_WARNING = True
home_dir = os.path.expanduser('~')
if os.path.abspath(reduction.__file__).startswith(home_dir):
WARNING_MESSAGE = "The following file is in your home area, please delete it and restart Mantid:\n\n"
else:
WARNING_MESSAGE = "If the following file is in your home area, please delete it and restart Mantid:\n\n"
WARNING_MESSAGE += os.path.abspath(reduction.__file__)
from MantidFramework import *
mtd.initialise(False)
HAS_MANTID = True
except:
REDUCTION_WARNING = True
WARNING_MESSAGE = "Please contact the Mantid team with the following message:\n\n\n"
WARNING_MESSAGE += unicode(traceback.format_exc())

from reduction_gui.instruments.instrument_factory import instrument_factory, INSTRUMENT_DICT
from reduction_gui.settings.application_settings import GeneralSettings
import ui.ui_reduction_main
import ui.ui_instrument_dialog
HAS_MANTID = False

IS_IN_MANTIDPLOT = False
try:
import qti
IS_IN_MANTIDPLOT = True
except:
pass


# Check whether Mantid is available
try:
from MantidFramework import *
mtd.initialise(False)
HAS_MANTID = True
except:
HAS_MANTID = False
REDUCTION_WARNING = False
WARNING_MESSAGE = ""

if HAS_MANTID:
try:
import reduction
if os.path.splitext(os.path.basename(reduction.__file__))[0] == "reduction":
REDUCTION_WARNING = True
home_dir = os.path.expanduser('~')
if os.path.abspath(reduction.__file__).startswith(home_dir):
WARNING_MESSAGE = "The following file is in your home area, please delete it and restart Mantid:\n\n"
else:
WARNING_MESSAGE = "If the following file is in your home area, please delete it and restart Mantid:\n\n"
WARNING_MESSAGE += os.path.abspath(reduction.__file__)
except:
REDUCTION_WARNING = True
WARNING_MESSAGE = "Please contact the Mantid team with the following message:\n\n\n"
WARNING_MESSAGE += unicode(traceback.format_exc())

from reduction_gui.instruments.instrument_factory import instrument_factory, INSTRUMENT_DICT
from reduction_gui.settings.application_settings import GeneralSettings
import ui.ui_reduction_main
import ui.ui_instrument_dialog

class ReductionGUI(QtGui.QMainWindow, ui.ui_reduction_main.Ui_SANSReduction):
def __init__(self, instrument=None, instrument_list=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from reduction_gui.widgets.sans.eqsans_data import DataSetsWidget
from reduction_gui.widgets.sans.eqsans_output import EQSANSOutputWidget
from reduction_gui.reduction.eqsans_reduction import EQSANSReductionScripter
from reduction_gui.widgets.sans.sans_catalog import SANSCatalogWidget

from reduction_gui.reduction.sans.eqsans_catalog import DataCatalog

from reduction_gui.reduction.sans.eqsans_data_proxy import DataProxy

Expand All @@ -36,3 +39,6 @@ def __init__(self, name, settings):

# Reduction output
self.attach(EQSANSOutputWidget(settings = self._settings))

# Catalog
self.attach(SANSCatalogWidget(settings = self._settings, catalog_cls=DataCatalog))
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from reduction_gui.widgets.sans.hfir_background import BackgroundWidget
from reduction_gui.widgets.output import OutputWidget
from reduction_gui.reduction.hfir_reduction import HFIRReductionScripter
from reduction_gui.widgets.sans.sans_catalog import SANSCatalogWidget

from reduction_gui.reduction.sans.hfir_catalog import DataCatalog

from reduction_gui.reduction.sans.hfir_data_proxy import DataProxy

Expand Down Expand Up @@ -39,3 +42,6 @@ def __init__(self, name, settings):

# Reduction output
self.attach(OutputWidget(settings = self._settings))

# Catalog
self.attach(SANSCatalogWidget(settings = self._settings, catalog_cls=DataCatalog))
210 changes: 210 additions & 0 deletions Code/Mantid/scripts/Interface/reduction_gui/reduction/sans/data_cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
"""
Simple local data catalog for Mantid
Gets main information from data files in a directory and stores
that information in a database in the user home area.
"""
import os
import sqlite3
import re
import time
import sys

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

class DataSet(object):
def __init__(self, run_number, title, run_start, duration, ssd):
self.run_number = run_number
self.title = title
self.run_start = run_start
self.duration = duration
self.ssd = ssd

@classmethod
def header(cls):
"""
Return the column headers
"""
return "%-6s %-60s %-16s %-7s %-10s" % ("Run", "Title", "Start", "Time[s]", "SDD[mm]")

@classmethod
def load_meta_data(cls, file_path, outputWorkspace):
"""
Load method that needs to be implemented for each catalog/instrument
"""
return False

@classmethod
def handle(cls, file_path):
"""
Return a DB handle for the given file, such as a run number
"""
return file_path

@classmethod
def read_properties(cls, ws, run, cursor):
return None

def __str__(self):
"""
Pretty print the current data set attributes
"""
return "%-6s %-60s %-16s %-7g %-10.0f" % (self.run_number, self.title, self.run_start, self.duration, self.ssd)

def as_list(self):
"""
Return a list of data set attributes
"""
return (self.run_number, self.title, self.run_start, self.duration, self.ssd)

def as_string_list(self):
"""
Return a list of data set attributes as strings
"""
return (str(self.run_number), self.title, self.run_start, "%-g"%self.duration, "%-10.0f"%self.ssd)

@classmethod
def find(cls, file_path, cursor, process_files=True):
"""
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 dataset where run=?', t)
rows = cursor.fetchall()

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

class DataCatalog(object):
"""
Data catalog
"""
extension = "nxs"
data_set_cls = DataSet

def __init__(self, replace_db=False):
## List of data sets
self.catalog = []

# Connect/create to DB
db_path = os.path.join(os.path.expanduser("~"), ".mantid_data_sets")
self.db_exists = False
self.db = None

try:
self._create_db(db_path, replace_db)
except:
if HAS_MANTID:
mtd.sendLogMessage("DataCatalog: Could not access local data catalog\n%s" % sys.exc_value)
else:
raise

def _create_db(self, db_path, replace_db):
"""
Create the database if we need to
"""
self.db_exists = False
if os.path.isfile(db_path):
if replace_db:
os.remove(db_path)
else:
self.db_exists = True

self.db = sqlite3.connect(db_path)
cursor = self.db.cursor()

if not self.db_exists:
cursor.execute("""create table dataset (run text, title text, start text, duration real, ssd real)""")
self.db.commit()
cursor.close()

def __str__(self):
"""
Pretty print the whole list of data
"""
output = "%s\n" % self.data_set_cls.header()
for r in self.catalog:
output += "%s\n" % str(r)
return output

def size(self):
"""
Return size of the catalog
"""
return len(self.catalog)

def get_list(self, data_dir=None):
"""
Get list of catalog entries
"""
self.list_data_sets(data_dir, process_files=False)
output = []
for r in self.catalog:
output.append(r.as_list())
return output

def get_string_list(self, data_dir=None):
"""
Get list of catalog entries
"""
self.list_data_sets(data_dir, process_files=False)
output = []
for r in self.catalog:
output.append(r.as_string_list())
return output

def list_data_sets(self, data_dir=None, call_back=None, process_files=True):
"""
Process a data directory
"""
self.catalog = []

if self.db is None:
if HAS_MANTID:
mtd.sendLogMessage("DataCatalog: Could not access local data catalog")
return

c = self.db.cursor()

if not os.path.isdir(data_dir):
return

try:
for f in os.listdir(data_dir):
if f.endswith(self.extension):
path = os.path.join(data_dir, f)
d = self.data_set_cls.find(path, c, process_files=process_files)
if d is not None:
if call_back is not None:
call_back(d.as_string_list())
self.catalog.append(d)

self.db.commit()
c.close()
except:
if HAS_MANTID:
mtd.sendLogMessage("DataCatalog: Error working with the local data catalog\n%s" % sys.exc_value)
else:
raise

self.db_exists = True

0 comments on commit 0c80261

Please sign in to comment.