Commit 3ffb31d5 authored by Anze Vavpetic's avatar Anze Vavpetic
Browse files

moved DS logic to a separate package

parent d58feade
def test(input_dict):
print 'great success'
return input_dict
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
from decision_support.interaction import *
def test_interaction(request,input_dict,output_dict,widget): def test_interaction(request,input_dict,output_dict,widget):
return render(request, 'interactions/test_interaction.html',{'widget':widget}) return render(request, 'interactions/test_interaction.html',{'widget':widget})
...@@ -79,10 +80,6 @@ def build_subgroups(request, input_dict, output_dict, widget): ...@@ -79,10 +80,6 @@ def build_subgroups(request, input_dict, output_dict, widget):
return render(request, 'interactions/build_subgroups.html', {'widget':widget, 'data':data, 'target':target}) return render(request, 'interactions/build_subgroups.html', {'widget':widget, 'data':data, 'target':target})
def kepner_tregoe(request, input_dict, output_dict, widget):
attributes = [att.name for att in input_dict['data'].domain.features]
return render(request, 'interactions/kepner_tregoe.html', {'widget':widget, 'attributes':attributes})
def alter_table(request, input_dict, output_dict, widget): def alter_table(request, input_dict, output_dict, widget):
from visualization_views import orng_table_to_dict from visualization_views import orng_table_to_dict
data = input_dict['data'] data = input_dict['data']
......
...@@ -2,6 +2,7 @@ from workflows.security import safeOpen ...@@ -2,6 +2,7 @@ from workflows.security import safeOpen
import nlp import nlp
import cPickle import cPickle
import json import json
from decision_support.library import *
def test_interaction(input_dict): def test_interaction(input_dict):
return input_dict return input_dict
...@@ -839,70 +840,6 @@ def load_corpus(input_dict): ...@@ -839,70 +840,6 @@ def load_corpus(input_dict):
response = ws.client.parseFile(fileName=fname, inFile=data) response = ws.client.parseFile(fileName=fname, inFile=data)
return {'corpus' : response['parsedFile']} return {'corpus' : response['parsedFile']}
def kepner_tregoe(input_dict):
output_dict = input_dict
output_dict['model'] = None
return output_dict
class KepnerTregoe:
'''
Kepner-Tregoe model.
'''
def __init__(self, data, weights, smaller_is_better=None):
self.data = data
self.weights = weights
self.smaller_is_better = smaller_is_better if smaller_is_better else set()
self.name = 'Kepner-Tregoe'
def __call__(self, weights=None):
import Orange
from Orange.feature import Type
if weights == None:
weights = self.weights
# New augmented table
norm_data = Orange.data.Table(self.data)
newid = min(norm_data.domain.get_metas().keys()) - 1
score_attr = Orange.feature.Continuous('score')
norm_data.domain.add_meta(newid, score_attr)
norm_data.add_meta_attribute(score_attr)
# Normalize the attributes column-wise
for att in norm_data.domain:
if att.var_type == Type.Continuous:
col = [ex[att] for ex in norm_data]
col_norm = float(sum(col))
for ex in norm_data:
if att.var_type == Type.Continuous:
ex[att] = ex[att] / col_norm
# Use the inverse of an attr. value if smaller values should be treated as 'better'.
inverse = lambda x, att: 1-x if att in self.smaller_is_better else x
for ex in norm_data:
score = sum([inverse(ex[att], att.name) * weights.get(att.name, 1) for att in norm_data.domain.features if att.var_type == Type.Continuous])
ex['score'] = score
return norm_data
def kepner_tregoe_finished(postdata, input_dict, output_dict):
# Fetch the data and the weights from the form.
data = input_dict['data']
attributes = [att.name for att in data.domain.features]
weights = {}
widget_id = postdata['widget_id'][0]
smaller_is_better = set()
for att in attributes:
weights[att]=int(postdata['weight'+str(widget_id)+str(att)][0])
if postdata.has_key('smallerIsBetter'+str(widget_id)+str(att)):
smaller_is_better.add(att)
# Instantiate a KepnerTregoe model.
kt = KepnerTregoe(data, weights, smaller_is_better=smaller_is_better)
output_dict = {}
output_dict['data'] = kt()
output_dict['model'] = kt
return output_dict
def sensitivity_analysis(input_dict):
return input_dict
def ds_charts(input_dict):
return input_dict
def string_to_file(input_dict): def string_to_file(input_dict):
return {} return {}
...@@ -931,3 +868,4 @@ def alter_table_finished(postdata, input_dict, output_dict): ...@@ -931,3 +868,4 @@ def alter_table_finished(postdata, input_dict, output_dict):
except: # Catch orange exception and give a proper error message. except: # Catch orange exception and give a proper error message.
raise Exception("Illegal value '%s' for discrete attribute '%s', legal values are: %s." % (new_value, att, new_table.domain[att].values)) raise Exception("Illegal value '%s' for discrete attribute '%s', legal values are: %s." % (new_value, att, new_table.domain[att].values))
return {'altered_data' : new_table} return {'altered_data' : new_table}
from django.shortcuts import render from django.shortcuts import render
from django.http import Http404, HttpResponse from django.http import Http404, HttpResponse
import nlp import nlp
import json from decision_support.visualization import *
def odt_to_tab(request,input_dict,output_dict,widget): def odt_to_tab(request,input_dict,output_dict,widget):
import Orange import Orange
...@@ -218,60 +218,3 @@ def term_candidate_viewer(request, input_dict, output_dict, widget): ...@@ -218,60 +218,3 @@ def term_candidate_viewer(request, input_dict, output_dict, widget):
terms = sorted(terms, key = lambda x: x['score'], reverse=True) terms = sorted(terms, key = lambda x: x['score'], reverse=True)
return render(request, 'visualizations/terms.html', {'widget' : widget, 'terms' : terms}) return render(request, 'visualizations/terms.html', {'widget' : widget, 'terms' : terms})
def sensitivity_analysis_viewer(request, input_dict, output_dict, widget):
'''
Computes the sensitivity analysis graph.
@author: Anze Vavpeltic, 2012
'''
model = input_dict['model']
attributes = [att.name for att in input_dict['model'].data.domain.features]
data_points = {}
domain = range(0, 101, 10)
# Compute for each attribute
for target_att in attributes:
y, ex_data = [], {}
# For collecting scores for each example across different weights
for ex in model.data:
ex_data[ex['label'].value] = []
# Compute the scores for each weight
for w in domain:
model.weights[target_att] = w
ds = model()
for ex in ds:
ex_data[ex['label'].value].append([w, ex['score'].value])
for ex in model.data:
y.append({'name' : ex['label'].value, 'data' : ex_data[ex['label'].value]})
data_points[target_att] = y
return render(request, 'visualizations/sensitivity_analysis.html',
{'widget' : widget,
'attributes': attributes,
'data_points' : json.dumps(data_points),
'output_dict': {}
})
def ds_charts_viewer(request, input_dict, output_dict, widget):
'''
Decision support visualization.
@author: Anze Vavpeltic, 2012
'''
model = input_dict['model']
norm_data = model()
weight_shares = [ [att, weight] for att, weight in model.weights.items() ]
attributes = sorted(model.weights.keys())
alternatives = [ex['label'].value for ex in norm_data]
weights_bar = [{ 'data' : [model.weights[att] for att in attributes] }]
values_column = [{ 'data' : [ex['score'].value for ex in norm_data] }]
alt_data = [{ 'name' : ex['label'].value, 'data' : [ex[att].value for att in attributes] } for ex in norm_data ]
return render(request, 'visualizations/ds_charts.html',
{'widget' : widget,
'model_name' : model.name,
'attributes' : json.dumps(attributes),
'alternatives' : json.dumps(alternatives),
'weight_shares' : json.dumps(weight_shares),
'weights_bar' : json.dumps(weights_bar),
'values_column' : json.dumps(values_column),
'alt_data' : json.dumps(alt_data)
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment