2007-01-04 14:53:03 +01:00
|
|
|
import gtk, gobject
|
2006-04-24 23:52:18 +02:00
|
|
|
import sys
|
|
|
|
import os
|
2006-05-05 12:10:03 +02:00
|
|
|
import inspect
|
2006-10-17 16:42:27 +02:00
|
|
|
import logger
|
2006-04-24 23:52:18 +02:00
|
|
|
|
|
|
|
def _workflow_classes(modname):
|
|
|
|
"""Returns a list of all subclasses of Workflow in a given module"""
|
|
|
|
workflow_classes = []
|
|
|
|
|
|
|
|
__import__('workflows.%s' % modname)
|
|
|
|
module = sys.modules['workflows.%s' % modname]
|
|
|
|
|
|
|
|
d = module.__dict__
|
|
|
|
for wf in d.values():
|
|
|
|
try:
|
|
|
|
if issubclass(wf, Workflow):
|
|
|
|
workflow_classes.append(wf)
|
|
|
|
except TypeError, e:
|
|
|
|
pass
|
|
|
|
return workflow_classes
|
|
|
|
|
|
|
|
def workflow_list():
|
|
|
|
"""Returns a ListStore containing all new workflows"""
|
|
|
|
retval = []
|
|
|
|
|
|
|
|
# List all .py files that can contain workflow classes
|
|
|
|
wf_path = sys.modules['workflows'].__path__
|
|
|
|
wf_files = []
|
|
|
|
|
|
|
|
for dir in wf_path:
|
|
|
|
for fn in os.listdir(dir):
|
|
|
|
if fn.endswith('.py') and ('#' not in fn):
|
|
|
|
wf_files.append(fn[:-3])
|
|
|
|
|
|
|
|
# Try to load each file and look for Workflow derived classes
|
|
|
|
for fn in wf_files:
|
|
|
|
try:
|
|
|
|
for wf in _workflow_classes(fn):
|
|
|
|
retval.append(wf)
|
|
|
|
except Exception, e:
|
|
|
|
logger.log('warning', 'Cannot load workflow: %s' % fn)
|
|
|
|
logger.log('warning', e)
|
2006-08-28 14:06:05 +02:00
|
|
|
|
2006-04-24 23:52:18 +02:00
|
|
|
return retval
|
2006-04-16 20:25:54 +02:00
|
|
|
|
|
|
|
class Workflow:
|
|
|
|
"""Defines a workflow that contains a set of analysis stages.
|
|
|
|
|
|
|
|
A Workflow is a set of analysis stages for a certain type of analysis.
|
|
|
|
Each stage contains some possible operations to do accomplish that
|
|
|
|
task.
|
|
|
|
"""
|
|
|
|
|
2006-04-22 23:46:44 +02:00
|
|
|
name = "Workflow"
|
2006-04-25 14:19:25 +02:00
|
|
|
ident = None
|
2006-04-23 00:29:02 +02:00
|
|
|
description = "Workflow Description"
|
2006-04-22 23:46:44 +02:00
|
|
|
|
2006-04-16 20:25:54 +02:00
|
|
|
def __init__(self, app):
|
|
|
|
self.stages = []
|
|
|
|
self.stages_by_id = {}
|
|
|
|
self.app = app
|
|
|
|
|
|
|
|
def add_stage(self, stage):
|
|
|
|
self.stages.append(stage)
|
|
|
|
self.stages_by_id[stage.id] = stage
|
|
|
|
|
|
|
|
def print_tree(self):
|
|
|
|
print self.name
|
|
|
|
for stage in self.stages:
|
|
|
|
print ' %s' % stage.name
|
|
|
|
for fun in stage.functions:
|
|
|
|
print ' %s' % fun.name
|
|
|
|
|
2006-04-20 17:30:29 +02:00
|
|
|
def add_project(self,project):
|
2006-04-24 11:53:07 +02:00
|
|
|
if project == None:
|
|
|
|
logger.log('notice','Proejct is empty')
|
|
|
|
logger.log('notice','Project added in : %s' %self.name)
|
2006-04-20 17:30:29 +02:00
|
|
|
self.project = project
|
|
|
|
|
2006-04-22 23:46:44 +02:00
|
|
|
class EmptyWorkflow(Workflow):
|
|
|
|
name = 'Empty Workflow'
|
|
|
|
|
|
|
|
def __init__(self, app):
|
|
|
|
Workflow.__init__(self, None)
|
|
|
|
|
2006-04-16 20:25:54 +02:00
|
|
|
class Stage:
|
|
|
|
"""A stage is a part of the data analysis process.
|
|
|
|
|
|
|
|
Each stage contains a set of functions that can be used to
|
|
|
|
accomplish the task. A typical early stage is 'preprocessing', which
|
|
|
|
can be done in several ways, each represented by a function.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, id, name):
|
|
|
|
self.id = id
|
|
|
|
self.name = name
|
|
|
|
self.functions = []
|
|
|
|
self.functions_by_id = {}
|
|
|
|
|
|
|
|
def add_function(self, fun):
|
|
|
|
self.functions.append(fun)
|
|
|
|
self.functions_by_id[fun.id] = fun
|
|
|
|
|
2006-05-03 13:11:45 +02:00
|
|
|
|
2006-04-16 20:25:54 +02:00
|
|
|
class Function:
|
|
|
|
"""A Function object encapsulates a function on a data set.
|
|
|
|
|
|
|
|
Each Function instance encapsulates some function that can be applied
|
|
|
|
to one or more types of data.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, id, name):
|
|
|
|
self.id = id
|
|
|
|
self.name = name
|
|
|
|
|
2006-05-03 13:52:54 +02:00
|
|
|
# ,ust return a Validation object
|
|
|
|
def validate_input(input):
|
|
|
|
return Validation(True,"Validation Not Implemented")
|
2006-04-16 20:25:54 +02:00
|
|
|
|
2006-05-03 13:52:54 +02:00
|
|
|
def run(self):
|
2006-04-16 20:25:54 +02:00
|
|
|
pass
|
|
|
|
|
2006-05-03 13:52:54 +02:00
|
|
|
class Validation:
|
|
|
|
def __init__(self,result, reason):
|
|
|
|
self.succeeded = result
|
|
|
|
self.reason = reason
|
|
|
|
|
|
|
|
|
2006-04-16 20:25:54 +02:00
|
|
|
class WorkflowView (gtk.VBox):
|
|
|
|
|
|
|
|
def __init__(self, wf):
|
|
|
|
gtk.VBox.__init__(self)
|
|
|
|
self.workflow = wf
|
2006-04-22 23:46:44 +02:00
|
|
|
self.setup_workflow(wf)
|
2006-04-16 20:25:54 +02:00
|
|
|
|
2006-04-22 23:46:44 +02:00
|
|
|
def setup_workflow(self, wf):
|
2006-04-16 20:25:54 +02:00
|
|
|
# Add stage in the process
|
|
|
|
for stage in wf.stages:
|
|
|
|
exp = gtk.Expander(stage.name)
|
|
|
|
btn_box = gtk.VBox()
|
|
|
|
btn_box.show()
|
|
|
|
exp.add(btn_box)
|
|
|
|
|
|
|
|
# Add functions in each stage
|
|
|
|
for fun in stage.functions:
|
|
|
|
btn = gtk.Button(fun.name)
|
2006-05-04 17:32:59 +02:00
|
|
|
btn.connect('clicked',
|
|
|
|
lambda button, f=fun : self.run_function(f))
|
2006-04-16 20:25:54 +02:00
|
|
|
|
|
|
|
btn_box.add(btn)
|
|
|
|
btn.show()
|
|
|
|
|
|
|
|
exp.show()
|
|
|
|
self.pack_start(exp, expand=False, fill=False)
|
|
|
|
|
2006-04-22 23:46:44 +02:00
|
|
|
def remove_workflow(self):
|
|
|
|
for c in self.get_children():
|
|
|
|
c.hide()
|
|
|
|
self.remove(c)
|
|
|
|
|
|
|
|
def set_workflow(self, workflow):
|
|
|
|
self.workflow = workflow
|
|
|
|
self.remove_workflow()
|
|
|
|
self.setup_workflow(workflow)
|
|
|
|
|
2006-04-19 21:59:55 +02:00
|
|
|
def run_function(self, function):
|
2006-04-16 20:25:54 +02:00
|
|
|
logger.log('debug', 'Starting function: %s' % function.name)
|
2006-04-19 21:59:55 +02:00
|
|
|
project = self.workflow.app.project
|
2006-04-24 16:52:21 +02:00
|
|
|
parent_data = project.current_data
|
|
|
|
|
2006-05-03 13:52:54 +02:00
|
|
|
validation = function.validate_input()
|
|
|
|
|
|
|
|
if not validation.succeeded:
|
|
|
|
logger.log('warning','Invalid Inputdata: ' + str(reason))
|
|
|
|
return
|
2006-05-05 12:10:03 +02:00
|
|
|
|
|
|
|
args, varargs, varkw, defaults = inspect.getargspec(function.run)
|
|
|
|
|
2006-05-05 15:10:54 +02:00
|
|
|
# first argument is 'self' and second should be the selection
|
|
|
|
# and we don't care about those...
|
|
|
|
args.remove('self')
|
|
|
|
if "selection" in args:
|
|
|
|
pass_selection = True
|
|
|
|
args.remove('selection')
|
|
|
|
else:
|
|
|
|
pass_selection = False
|
2006-05-05 12:10:03 +02:00
|
|
|
|
|
|
|
if varargs and len(parent_data) < len(args):
|
|
|
|
logger.log('warning', "Function requires minimum %d datasets selected." % len(args))
|
2006-05-03 13:52:54 +02:00
|
|
|
return
|
2006-05-05 12:10:03 +02:00
|
|
|
elif not varargs and args and len(args) != len(parent_data):
|
|
|
|
# functions requiring datasets have to have the right number
|
|
|
|
logger.log('warning', "Function requires %d datasets, but only %d selected." % (len(args), len(parent_data)))
|
|
|
|
return
|
|
|
|
|
|
|
|
if not args:
|
2006-05-05 15:10:54 +02:00
|
|
|
# we allow functions requiring no data to be run even if a
|
|
|
|
# dataset is is selected
|
|
|
|
data = []
|
2006-05-03 13:52:54 +02:00
|
|
|
else:
|
2006-05-05 15:10:54 +02:00
|
|
|
data = parent_data
|
|
|
|
|
|
|
|
if pass_selection:
|
|
|
|
# if the function has a 'selection' argument, we pass in
|
|
|
|
# the selection
|
|
|
|
new_data = function.run(selection=project.get_selection(), *data)
|
|
|
|
else:
|
|
|
|
new_data = function.run(*data)
|
|
|
|
|
2006-04-19 21:59:55 +02:00
|
|
|
if new_data != None:
|
2006-04-24 17:20:27 +02:00
|
|
|
project.add_data(parent_data, new_data, function.name)
|
2006-05-03 13:52:54 +02:00
|
|
|
|
2006-04-16 20:25:54 +02:00
|
|
|
logger.log('debug', 'Function ended: %s' % function.name)
|
|
|
|
|
2007-01-04 14:53:03 +01:00
|
|
|
|
|
|
|
class Options(dict):
|
|
|
|
"""Options base class.
|
|
|
|
"""
|
|
|
|
def __init__(self, *args,**kw):
|
|
|
|
dict.__init__(self, *args, **kw)
|
|
|
|
self['out_plots'] = None
|
|
|
|
self['out_data'] = None
|
|
|
|
|
|
|
|
def _copy_from_list(self, key_list):
|
|
|
|
"""Returns suboptions (dictionary) from a list of keys.
|
|
|
|
"""
|
|
|
|
d = {}
|
|
|
|
for key in key_list:
|
|
|
|
d[key] = self.get(key, None)
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
|
|
|
class OptionsDialog(gtk.Dialog):
|
|
|
|
"""The basic input/output dialog box.
|
|
|
|
|
|
|
|
This defines the first page of the function options-gui.
|
|
|
|
Any function that invokes a option-gui will inherit from this class.
|
|
|
|
"""
|
|
|
|
def __init__(self, data, options, input_names=['X','Y']):
|
|
|
|
gtk.Dialog.__init__(self, 'Input-Output dialog',
|
|
|
|
None,
|
|
|
|
gtk.DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
(gtk.STOCK_OK, gtk.RESPONSE_OK,
|
|
|
|
gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
|
|
|
|
|
|
|
|
self._options = options
|
|
|
|
self._data = data
|
|
|
|
self._editable = True
|
|
|
|
self.set_size_request(300,400)
|
|
|
|
|
|
|
|
# create notebook
|
|
|
|
nb = gtk.Notebook()
|
|
|
|
|
|
|
|
# 1. page: input/output
|
|
|
|
#
|
|
|
|
#inputs
|
|
|
|
input_frame = gtk.Frame("Input")
|
|
|
|
hbox = gtk.HBox(True, 8)
|
|
|
|
align = gtk.Alignment(1, 1, 1, 1)
|
|
|
|
align.set_padding(8, 8, 8, 8)
|
|
|
|
align.add(hbox)
|
|
|
|
input_frame.add(align)
|
|
|
|
for i, name in enumerate(input_names):
|
|
|
|
frame = gtk.Frame(name)
|
|
|
|
frame.set_label_align(0.5, 0.5)
|
|
|
|
label = gtk.Label(data[i]._name + "\n" + str(data[i]._array.shape))
|
|
|
|
frame.add(label)
|
|
|
|
hbox.add(frame)
|
|
|
|
|
|
|
|
#outputs
|
|
|
|
output_frame = gtk.Frame("Output")
|
|
|
|
output_hbox = gtk.HBox(True,4)
|
|
|
|
output_align = gtk.Alignment(1, 1, 1, 1)
|
|
|
|
output_align.set_padding(8, 8, 8, 8) #left padding:8
|
|
|
|
output_align.add(output_hbox)
|
|
|
|
output_frame.add(output_align)
|
|
|
|
|
|
|
|
# plots
|
|
|
|
plot_list = gtk.ListStore(gobject.TYPE_STRING,
|
|
|
|
gobject.TYPE_BOOLEAN)
|
|
|
|
plot_treeview = gtk.TreeView(plot_list)
|
|
|
|
|
|
|
|
# Add plots
|
|
|
|
for plt, name, use in self._options['all_plots']:
|
|
|
|
plot_list.append((name, use))
|
|
|
|
|
|
|
|
# Renderer for active toggle.
|
|
|
|
active_renderer = gtk.CellRendererToggle()
|
|
|
|
active_renderer.set_property('mode', gtk.CELL_RENDERER_MODE_ACTIVATABLE)
|
|
|
|
active_renderer.connect('toggled', toggled, plot_list)
|
|
|
|
active_column = gtk.TreeViewColumn('Use', active_renderer, active=1)
|
|
|
|
|
|
|
|
# Renderer for plot title.
|
|
|
|
title_renderer = gtk.CellRendererText()
|
|
|
|
title_renderer.set_property('mode', gtk.CELL_RENDERER_MODE_EDITABLE)
|
|
|
|
title_column = gtk.TreeViewColumn('Plot', title_renderer, text=0)
|
|
|
|
|
|
|
|
# Add columns to tree view.
|
|
|
|
plot_treeview.append_column(active_column)
|
|
|
|
plot_treeview.append_column(title_column)
|
|
|
|
|
|
|
|
## datasets
|
|
|
|
dataset_list = gtk.ListStore(gobject.TYPE_STRING,
|
|
|
|
gobject.TYPE_BOOLEAN)
|
|
|
|
dataset_treeview = gtk.TreeView(dataset_list)
|
|
|
|
|
|
|
|
# Add datasets
|
|
|
|
for dat, name, use in self._options['all_data']:
|
|
|
|
dataset_list.append((name, use))
|
|
|
|
|
|
|
|
# Renderer for active toggle.
|
|
|
|
active_renderer = gtk.CellRendererToggle()
|
|
|
|
active_renderer.set_property('mode', gtk.CELL_RENDERER_MODE_ACTIVATABLE)
|
|
|
|
active_renderer.connect('toggled', toggled, dataset_list)
|
|
|
|
active_column = gtk.TreeViewColumn('Use', active_renderer, active=1)
|
|
|
|
|
|
|
|
# Renderer for plot title.
|
|
|
|
title_renderer = gtk.CellRendererText()
|
|
|
|
title_renderer.set_property('mode', gtk.CELL_RENDERER_MODE_EDITABLE)
|
|
|
|
title_column = gtk.TreeViewColumn('Dataset', title_renderer, text=0)
|
|
|
|
|
|
|
|
# Add columns to tree view.
|
|
|
|
dataset_treeview.append_column(active_column)
|
|
|
|
dataset_treeview.append_column(title_column)
|
|
|
|
|
|
|
|
# add treeviews to output frame
|
|
|
|
output_hbox.add(plot_treeview)
|
|
|
|
output_hbox.add(dataset_treeview)
|
|
|
|
|
|
|
|
# vbox for input/spacer/output
|
|
|
|
vbox1 = gtk.VBox()
|
|
|
|
vbox1.add(input_frame)
|
|
|
|
vbox1.add(gtk.HSeparator())
|
|
|
|
vbox1.add(output_frame)
|
|
|
|
|
|
|
|
# add vbox to notebook
|
|
|
|
nb.append_page(vbox1, gtk.Label("I/O"))
|
|
|
|
self.vbox.add(nb)
|
|
|
|
|
|
|
|
#keep ref to liststores
|
|
|
|
self.dataset_list = dataset_list
|
|
|
|
self.plot_list = plot_list
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.vbox.show_all()
|
|
|
|
return gtk.Dialog.run(self)
|
|
|
|
|
|
|
|
def set_options(self, options):
|
|
|
|
self._options = options
|
|
|
|
|
|
|
|
def update_options(self, options):
|
|
|
|
self._options.update(options)
|
|
|
|
|
|
|
|
def set_output(self):
|
|
|
|
# get marked output data
|
|
|
|
out_data = [item[0] for name, mark in self.dataset_list for item in self._options['all_data'] if mark==True and name==item[1]]
|
|
|
|
# get marked plots
|
|
|
|
out_plots = [item[0] for name, mark in self.plot_list for item in self._options['all_plots'] if mark==True and name==item[1]]
|
|
|
|
# update options
|
|
|
|
self._options['out_data'] = out_data
|
|
|
|
self._options['out_plots'] = out_plots
|
|
|
|
|
|
|
|
def set_editable(self, editable):
|
|
|
|
self._editable = True
|
|
|
|
|
|
|
|
def set_data(self, data):
|
|
|
|
self._data = data
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return self._data
|
|
|
|
|
|
|
|
def get_options(self):
|
|
|
|
return self._options
|
|
|
|
|
|
|
|
def toggled(renderer, path, store):
|
|
|
|
it = store.get_iter(path)
|
|
|
|
old_value = store.get_value(it, 1)
|
|
|
|
store.set_value(it, 1, not old_value)
|