Multiple lib changes
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
"""This module contains bilinear models(Functions)
|
||||
"""
|
||||
|
||||
import pygtk
|
||||
import gtk
|
||||
import gtk.glade
|
||||
from fluents.workflow import Function, OptionsDialog, Options
|
||||
from fluents.dataset import Dataset
|
||||
from fluents import plots, dataset, workflow, logger
|
||||
@@ -12,7 +14,7 @@ from cx_utils import mat_center
|
||||
from validation import *
|
||||
import blmplots
|
||||
import engines
|
||||
|
||||
import copy
|
||||
|
||||
class Model(Function):
|
||||
"""Base class of bilinear models.
|
||||
@@ -39,23 +41,42 @@ class PCA(Model):
|
||||
Model.__init__(self,id,name)
|
||||
self._options = PcaOptions()
|
||||
|
||||
def pre_validation(self, amax, n_sets, val_engine):
|
||||
"""Model calculations for maximum number of components.
|
||||
def validation(self, amax, cv_val_sets, pert_val_sets, cv_val_method, pert_val_method):
|
||||
"""Model validation and estimate of optimal numer of components.
|
||||
"""
|
||||
rmsep = val_engine(self.model['E0'], amax, n_sets)
|
||||
self.model['rmsep'] = rmsep
|
||||
self.model['aopt'] = rmsep.argmin()
|
||||
if self._options['calc_cv']:
|
||||
if cv_val_method == 'random':
|
||||
sep, aopt = pca_cv_val(self.model['E0'], amax, cv_val_sets)
|
||||
self.model['sep'] = sep
|
||||
|
||||
if self._options['calc_pert']:
|
||||
if pert_val_method == 'random_diag':
|
||||
sep, aopt = pca_alter_val(self.model['E0'], amax, pert_val_sets)
|
||||
self.model['sep'] = sep
|
||||
|
||||
if self._options['calc_cv']==False and self._options['calc_pert']==False:
|
||||
self.model['sep'] = None
|
||||
aopt = self._options['amax']
|
||||
|
||||
if self._options['auto_aopt']:
|
||||
logger.log("notice", "Auto aopt: " + str(aopt))
|
||||
self._options['aopt'] = aopt
|
||||
if aopt==1:
|
||||
logger.log('notice', 'Aopt at first component!')
|
||||
|
||||
def confidence(self, aopt, n_sets, alpha, p_center,
|
||||
crot, strict, cov_center ):
|
||||
"""Returns a confidence measure for model parameters.
|
||||
Based on aopt.
|
||||
"""
|
||||
aopt = self.model['aopt']
|
||||
if aopt<2:
|
||||
aopt = 2
|
||||
logger.log('notice','Hotellings T2 needs more than 1 comp.\n switching to 2!!')
|
||||
|
||||
jk_segments = pca_jkP(self.model['E0'], aopt, n_sets)
|
||||
Pcal = self.model['P'][:,:aopt]
|
||||
tsq = hotelling(jk_segments, Pcal, p_center,
|
||||
cov_center, alpha, crot, strict)
|
||||
cov_center, alpha, crot, strict)
|
||||
self.model['p_tsq'] = tsq
|
||||
|
||||
def make_model(self, amax, mode, scale):
|
||||
@@ -96,8 +117,8 @@ class PCA(Model):
|
||||
# vars
|
||||
ids_1 = [dim_name_1, DX.get_identifiers(dim_name_1, sorted=True)]
|
||||
# components (hidden)
|
||||
pc_ids = ['_comp_a', map(str,range(self.model['aopt'])) ]
|
||||
pc_ids_opt = ['_comp_o', map(str, range(self.model['aopt'])) ]
|
||||
pc_ids = ['_amax', map(str,range(self._options['amax'])) ]
|
||||
pc_ids_opt = ['_aopt', map(str, range(self._options['aopt'])) ]
|
||||
zero_dim = ['_doe', ['0']] # null dim, vector (hidden)
|
||||
match_ids = {'E':[ids_0, ids_1],
|
||||
'E0':[ids_0, ids_1],
|
||||
@@ -121,8 +142,7 @@ class PCA(Model):
|
||||
#try:
|
||||
out.append(plt(self))
|
||||
#except:
|
||||
# print plt
|
||||
#logger.log('debug', 'Plot: %s failed') %plt
|
||||
# logger.log('debug', 'Plot: %s failed') %str(plt)
|
||||
return out
|
||||
|
||||
def run_o(self, data):
|
||||
@@ -130,6 +150,8 @@ class PCA(Model):
|
||||
"""
|
||||
self.clear()
|
||||
options = self._options
|
||||
for item in options.items():
|
||||
print item
|
||||
self._dataset['X'] = data
|
||||
self._data['X'] = data.asarray().astype('<f8')
|
||||
if options['center']:
|
||||
@@ -137,8 +159,9 @@ class PCA(Model):
|
||||
self.model['E0'] = center(self._data['X'])
|
||||
else:
|
||||
self.model['E0'] = data.asarray()
|
||||
|
||||
self.pre_validation(**options.pre_validation_options())
|
||||
|
||||
self.validation(**options.validation_options())
|
||||
self.model['aopt'] = self._options['aopt']
|
||||
self.make_model(**options.make_model_options())
|
||||
if options['calc_conf']:
|
||||
self.confidence(**options.confidence_options())
|
||||
@@ -159,7 +182,6 @@ class PCA(Model):
|
||||
if response == gtk.RESPONSE_OK:
|
||||
# set output data and plots
|
||||
dialog.set_output()
|
||||
|
||||
#run with current data and options
|
||||
return self.run_o(data)
|
||||
|
||||
@@ -172,10 +194,10 @@ class PLS(Model):
|
||||
def pre_validation(self, amax, n_sets, val_engine):
|
||||
"""Returns rmsec,rmsep for model.
|
||||
"""
|
||||
rmsep = val_engine(self.model['E0'], self.model['F0'],
|
||||
rmsep, aopt = val_engine(self.model['E0'], self.model['F0'],
|
||||
amax, n_sets)
|
||||
self.model['rmsep'] = rmsep.mean(0)
|
||||
self.model['aopt'] = rmsep.mean(0).argmin()
|
||||
self.model['aopt'] = aopt
|
||||
|
||||
def confidence(self, aopt, n_sets, alpha, p_center,
|
||||
crot, strict, cov_center ):
|
||||
@@ -341,34 +363,39 @@ class PcaOptions(Options):
|
||||
opt['algo'] = 'pca'
|
||||
opt['engine'] = engines.pca
|
||||
opt['mode'] = 'normal' # how much info to calculate
|
||||
opt['lod'] = 'compact' # how much info to store
|
||||
opt['amax'] = 5
|
||||
opt['aopt'] = 5
|
||||
opt['amax'] = 10
|
||||
opt['aopt'] = 100
|
||||
opt['auto_aopt'] = False
|
||||
opt['center'] = True
|
||||
opt['center_mth'] = mat_center
|
||||
opt['scale'] = 'scores'
|
||||
opt['calc_conf'] = True
|
||||
opt['n_sets'] = 5
|
||||
|
||||
opt['calc_conf'] = False
|
||||
opt['n_sets'] = 5
|
||||
opt['strict'] = True
|
||||
opt['p_center'] = 'med'
|
||||
opt['alpha'] = .8
|
||||
opt['cov_center'] = 'med'
|
||||
opt['crot'] = True
|
||||
|
||||
opt['val_engine'] = pca_alter_val
|
||||
opt['val_n_sets'] = 10
|
||||
|
||||
opt['calc_cv'] = False
|
||||
opt['calc_pert'] = True
|
||||
opt['pert_val_method'] = 'random_diag'
|
||||
opt['cv_val_method'] = 'random'
|
||||
opt['cv_val_sets'] = 10
|
||||
opt['pert_val_sets'] = 10
|
||||
|
||||
opt['all_data'] = [('T', 'scores', True),
|
||||
('P', 'loadings', True),
|
||||
('E','residuals', False),
|
||||
('p_tsq', 't2', False),
|
||||
('rmsep', 'root mean square error of prediction', False)
|
||||
('rmsep', 'RMSEP', False)
|
||||
]
|
||||
|
||||
opt['all_plots'] = [(blmplots.PcaScorePlot, 'Scores', True),
|
||||
(blmplots.PcaLoadingPlot, 'Loadings', True),
|
||||
(blmplots.LineViewXc, 'Line view', True)
|
||||
(blmplots.LineViewXc, 'Line view', True),
|
||||
(blmplots.PredictionErrorPlot, 'Residual Error', True)
|
||||
]
|
||||
|
||||
opt['out_data'] = ['T','P', 'p_tsq']
|
||||
@@ -387,9 +414,10 @@ class PcaOptions(Options):
|
||||
'strict', 'crot', 'cov_center']
|
||||
return self._copy_from_list(opt_list)
|
||||
|
||||
def pre_validation_options(self):
|
||||
def validation_options(self):
|
||||
"""Options for pre_validation method."""
|
||||
opt_list = ['amax', 'n_sets', 'val_engine']
|
||||
opt_list = ['amax', 'cv_val_sets', 'pert_val_sets',
|
||||
'cv_val_method', 'pert_val_method']
|
||||
return self._copy_from_list(opt_list)
|
||||
|
||||
|
||||
@@ -411,7 +439,7 @@ class PlsOptions(Options):
|
||||
opt['center'] = True
|
||||
opt['center_mth'] = mat_center
|
||||
opt['scale'] = 'scores'
|
||||
opt['calc_conf'] = True
|
||||
opt['calc_conf'] = False
|
||||
opt['n_sets'] = 10
|
||||
|
||||
opt['strict'] = True
|
||||
@@ -419,14 +447,16 @@ class PlsOptions(Options):
|
||||
opt['alpha'] = .2
|
||||
opt['cov_center'] = 'med'
|
||||
opt['crot'] = True
|
||||
|
||||
|
||||
opt['calc_cv'] = True
|
||||
opt['calc_pert'] = False
|
||||
opt['val_engine'] = w_pls_cv_val
|
||||
|
||||
opt['all_data'] = [('T', 'scores', True),
|
||||
('P', 'loadings', True),
|
||||
('E','residuals', False),
|
||||
('p_tsq', 't2', False),
|
||||
('rmsep', 'root mean square error of prediction', False)
|
||||
('rmsep', 'RMSEP', False)
|
||||
]
|
||||
|
||||
opt['all_plots'] = [(blmplots.PlsScorePlot, 'Scores', True),
|
||||
@@ -434,7 +464,7 @@ class PlsOptions(Options):
|
||||
(blmplots.LineViewXc, 'Line view', True)]
|
||||
|
||||
opt['out_plots'] = [blmplots.PlsScorePlot,
|
||||
blmplots.PlsLoadingPlot,
|
||||
blmplots.PlsLoadingPlot,
|
||||
blmplots.LineViewXc]
|
||||
opt['out_data'] = None
|
||||
|
||||
@@ -453,7 +483,7 @@ class PlsOptions(Options):
|
||||
def confidence_options(self):
|
||||
"""Options for confidence method."""
|
||||
opt_list = ['n_sets', 'aopt', 'alpha', 'p_center',
|
||||
'strict', 'crot', 'cov_center']
|
||||
'strict', 'crot', 'cov_center']
|
||||
return self._copy_from_list(opt_list)
|
||||
|
||||
def pre_validation_options(self):
|
||||
@@ -468,9 +498,175 @@ class PcaOptionsDialog(OptionsDialog):
|
||||
def __init__(self, data, options, input_names=['X']):
|
||||
OptionsDialog.__init__(self, data, options, input_names)
|
||||
|
||||
glade_file = "/home/flatberg/Projects/project4/project4.glade"
|
||||
notebook_name = "vbox1"
|
||||
page_name = "Options"
|
||||
self.add_page_from_glade(glade_file, notebook_name, page_name)
|
||||
# connect signals to handlers
|
||||
dic = {"on_amax_value_changed" : self.on_amax_changed,
|
||||
"on_aopt_value_changed" : self.on_aopt_changed,
|
||||
"auto_aopt_toggled" : self.auto_aopt_toggled,
|
||||
"center_toggled" : self.center_toggled,
|
||||
"on_scale_changed" : self.on_scale_changed,
|
||||
"on_val_none" : self.val_toggled,
|
||||
"on_val_cv" : self.cv_toggled,
|
||||
"on_val_pert" : self.pert_toggled,
|
||||
"on_cv_method_changed" : self.on_cv_method_changed,
|
||||
"on_cv_sets_changed" : self.on_cv_sets_changed,
|
||||
"on_pert_sets_changed" : self.on_pert_sets_changed,
|
||||
"on_conf_toggled" : self.on_conf_toggled
|
||||
}
|
||||
|
||||
self.wTree.signal_autoconnect(dic)
|
||||
|
||||
# set/ensure valid default values/ranges
|
||||
amax_sb = self.wTree.get_widget("amax_spinbutton")
|
||||
max_comp = min(data[0].shape) # max num of components
|
||||
if self._options['amax']>max_comp:
|
||||
logger.log('debug', 'amax default too large ... adjusting')
|
||||
self._options['amax'] = max_comp
|
||||
amax_sb.get_adjustment().set_all(self._options['amax'], 1, max_comp, 1, 0, 0)
|
||||
# aopt spin button
|
||||
aopt_sb = self.wTree.get_widget("aopt_spinbutton")
|
||||
if self._options['aopt']>self._options['amax']:
|
||||
self._options['aopt'] = self._options['amax'] + 1 - 1
|
||||
aopt_sb.get_adjustment().set_all(self._options['aopt'], 1, self._options['amax'], 1, 0, 0)
|
||||
|
||||
# scale
|
||||
scale_cb = self.wTree.get_widget("scale_combobox")
|
||||
scale_cb.set_active(0)
|
||||
|
||||
# validation frames
|
||||
if self._options['calc_cv']==False:
|
||||
cv_frame = self.wTree.get_widget("cv_frame")
|
||||
cv_frame.set_sensitive(False)
|
||||
if self._options['calc_pert']==False:
|
||||
pert_frame = self.wTree.get_widget("pert_frame")
|
||||
pert_frame.set_sensitive(False)
|
||||
|
||||
cv = self.wTree.get_widget("cv_method").set_active(0)
|
||||
pm = self.wTree.get_widget("pert_method").set_active(0)
|
||||
|
||||
# confidence
|
||||
if self._options['calc_conf']==True:
|
||||
self.wTree.get_widget("subset_frame").set_sensitive(True)
|
||||
else:
|
||||
self.wTree.get_widget("subset_frame").set_sensitive(False)
|
||||
|
||||
|
||||
def on_amax_changed(self, sb):
|
||||
logger.log("debug", "amax changed: new value: %s" %sb.get_value_as_int())
|
||||
amax = sb.get_value_as_int()
|
||||
# update aopt if needed
|
||||
if amax<self._options['aopt']:
|
||||
self._options['aopt'] = amax
|
||||
aopt_sb = self.wTree.get_widget("aopt_spinbutton")
|
||||
aopt_sb.get_adjustment().set_all(self._options['aopt'], 1, amax, 1, 0, 0)
|
||||
self._options['amax'] = sb.get_value_as_int()
|
||||
|
||||
def on_aopt_changed(self, sb):
|
||||
aopt = sb.get_value_as_int()
|
||||
self._options['aopt'] = aopt
|
||||
|
||||
def auto_aopt_toggled(self, tb):
|
||||
aopt_sb = self.wTree.get_widget("aopt_spinbutton")
|
||||
if tb.get_active():
|
||||
self._options['auto_aopt'] = True
|
||||
aopt_sb.set_sensitive(False)
|
||||
else:
|
||||
self._options['auto_aopt'] = False
|
||||
aopt_sb.set_sensitive(True)
|
||||
|
||||
def center_toggled(self, tb):
|
||||
if tb.get_active():
|
||||
self._options['center'] = True
|
||||
else:
|
||||
logger.log("debug", "centering set to False")
|
||||
self._options['center'] = False
|
||||
|
||||
def on_scale_changed(self, cb):
|
||||
scale = cb.get_active_text()
|
||||
if scale=='Scores':
|
||||
self._options['scale'] = 'scores'
|
||||
elif scale=='Loadings':
|
||||
self._options['scale'] = 'loads'
|
||||
else:
|
||||
raise IOError
|
||||
|
||||
def val_toggled(self, tb):
|
||||
"""Callback for validation: None. """
|
||||
cv_frame = self.wTree.get_widget("cv_frame")
|
||||
pert_frame = self.wTree.get_widget("pert_frame")
|
||||
cv_tb = self.wTree.get_widget("cv_toggle")
|
||||
p_tb = self.wTree.get_widget("pert_toggle")
|
||||
if tb.get_active():
|
||||
self._options['calc_cv'] = False
|
||||
self._options['calc_pert'] = False
|
||||
cv_frame.set_sensitive(False)
|
||||
pert_frame.set_sensitive(False)
|
||||
cv_tb.set_sensitive(False)
|
||||
p_tb.set_sensitive(False)
|
||||
else:
|
||||
p_tb.set_sensitive(True)
|
||||
cv_tb.set_sensitive(True)
|
||||
if p_tb.get_active():
|
||||
pert_frame.set_sensitive(True)
|
||||
self._options['calc_pert'] = True
|
||||
if cv_tb.get_active():
|
||||
cv_frame.set_sensitive(True)
|
||||
self._options['calc_cv'] = True
|
||||
|
||||
def cv_toggled(self, tb):
|
||||
cv_frame = self.wTree.get_widget("cv_frame")
|
||||
if tb.get_active():
|
||||
cv_frame.set_sensitive(True)
|
||||
self._options['calc_cv'] = True
|
||||
else:
|
||||
cv_frame.set_sensitive(False)
|
||||
self._options['calc_cv'] = False
|
||||
|
||||
def pert_toggled(self, tb):
|
||||
pert_frame = self.wTree.get_widget("pert_frame")
|
||||
if tb.get_active():
|
||||
pert_frame.set_sensitive(True)
|
||||
self._options['calc_pert'] = True
|
||||
else:
|
||||
pert_frame.set_sensitive(False)
|
||||
self._options['calc_pert'] = False
|
||||
|
||||
|
||||
def on_cv_method_changed(self, cb):
|
||||
method = cb.get_active_text()
|
||||
if method == 'Random':
|
||||
self._options['cv_val_method'] = 'random'
|
||||
|
||||
def on_pert_method_changed(self, cb):
|
||||
method = cb.get_active_text()
|
||||
if method == 'Random diags':
|
||||
self._options['pert_val_method'] = 'random_diag'
|
||||
|
||||
def on_cv_sets_changed(self, sb):
|
||||
val = sb.get_value_as_int()
|
||||
self._options['cv_val_sets'] = val
|
||||
|
||||
def on_pert_sets_changed(self, sb):
|
||||
val = sb.get_value_as_int()
|
||||
self._options['pert_val_sets'] = val
|
||||
|
||||
def on_conf_toggled(self, tb):
|
||||
if tb.get_active():
|
||||
self._options['calc_conf'] = False
|
||||
self.wTree.get_widget("subset_frame").set_sensitive(False)
|
||||
else:
|
||||
self._options['calc_conf'] = True
|
||||
self.wTree.get_widget("subset_frame").set_sensitive(True)
|
||||
|
||||
|
||||
|
||||
|
||||
class PlsOptionsDialog(OptionsDialog):
|
||||
"""Options dialog for Partial Least Squares Regression.
|
||||
"""
|
||||
|
||||
def __init__(self, data, options, input_names=['X', 'Y']):
|
||||
OptionsDialog.__init__(self, data, options, input_names)
|
||||
|
Reference in New Issue
Block a user