Skip to content

Commit

Permalink
Simplify the argument panel. when creating new demos, developer only
Browse files Browse the repository at this point in the history
need to fillin a python dict with types and names of each argument.
  • Loading branch information
foulwall committed Jun 3, 2013
1 parent 367ba23 commit b16be7b
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 124 deletions.
28 changes: 23 additions & 5 deletions demo/clustering.py
Expand Up @@ -7,7 +7,25 @@
import json

def entrance(request):
properties = { 'title': 'Clustering Demo' }
arguments = [
{
'argument_type': 'select',
'argument_label': 'Distance',
'argument_name': 'distance',
'argument_items': ['EuclideanDistance',
'ManhattanMetric',
'JensenMetric']},
{
'argument_type': 'integer',
'argument_name': 'number_of_clusters',
'argument_label': 'Number of Clusters (<500)',
'argument_default': '2'},
{
'argument_type': 'button-group',
'argument_items': ['cluster', 'clear']
}]
properties = { 'title': 'Clustering Demo' ,
'arguments': arguments}
return render_to_response("clustering/index.html", properties, context_instance=RequestContext(request))

def cluster(request):
Expand All @@ -31,7 +49,7 @@ def _read_data(request):
raise TypeError
positive = json.loads(request.POST['positive'])['points']
negative = json.loads(request.POST['negative'])['points']
distance_name = request.POST['distance_name']
distance_name = request.POST['distance']

if len(positive) == 0 and len(negative) == 0:
raise TypeError
Expand All @@ -54,11 +72,11 @@ def _train_clustering(positive, negative, distance_name, k):
lab = sg.BinaryLabels(labels)
train = sg.RealFeatures(features)

if distance_name == "eucl":
if distance_name == "EuclideanDistance":
distance = sg.EuclideanDistance(train, train)
elif distance_name == "manh":
elif distance_name == "ManhattanMetric":
distance = sg.ManhattanMetric(train, train)
elif distance_name == "jens":
elif distance_name == "JensenMetric":
distance = sg.JensenMetric(train, train)
else:
raise TypeError
Expand Down
29 changes: 28 additions & 1 deletion demo/gp.py
Expand Up @@ -9,7 +9,34 @@
import json

def entrance(request):
properties = { 'title': 'Gaussian Process Regression Demo' }
arguments = [
{
'argument_type': 'decimal',
'argument_label': 'Sine Freq.',
'argument_name': 'frequency',
'argument_default': '2.0'},
{
'argument_type': 'decimal',
'argument_label': 'Amplitude',
'argument_name': 'amplitude',
'argument_default': '2.0'},
{
'argument_type': 'decimal',
'argument_label': 'Noise Level',
'argument_name': 'noise_level',
'argument_default': '0.1'},
{
'argument_type': 'decimal',
'argument_label': 'Kernel Width',
'argument_name': 'kernel_width',
'argument_default': '2.0'},
{
'argument_type': 'button-group',
'argument_items': ['GenerateToyData', 'TrainGP', 'Clear']
}
]
properties = { 'title': 'Gaussian Process Regression Demo' ,
'arguments': arguments }
return render_to_response("gp/index.html", properties, context_instance = RequestContext(request))

def create_toy_data(request):
Expand Down
48 changes: 44 additions & 4 deletions demo/kernel_matrix.py
Expand Up @@ -8,7 +8,47 @@
import json

def entrance(request):
properties = { 'title': 'Kernel Matrix Visualization' }
arguments = [
{
'argument_type': 'decimal',
'argument_name': 'frequency',
'argument_label': 'Sine Freq.',
'argument_default': '1.0'},
{
'argument_type': 'decimal',
'argument_name': 'amplitude',
'argument_label': 'Amplitude',
'argument_default': '1.0'},
{
'argument_type': 'decimal',
'argument_name': 'noise_level',
'argument_label': 'Noise Level',
'argument_default': '0.3'},
{
'argument_type': 'select',
'argument_label': 'Kernel Function',
'argument_name': 'kernel',
'argument_items': ['GaussianKernel', 'PolynomialKernel', 'LinearKernel'],
'argument_default': 'GaussianKernel'},
{
'argument_type': 'decimal',
'argument_label': 'Kernel Width',
'argument_name': 'kernel_width',
'argument_default': '0.3'},
{
'argument_type': 'integer',
'argument_name': 'degree',
'argument_default': '5'},
{
'argument_type': 'button-group',
'argument_items': ['CreateToyData', 'Draw', 'Clear']},
{
'argument_type': 'div',
'argument_label': 'Legend',
'argument_id': 'legend'}
]
properties = { 'title': 'Kernel Matrix Visualization',
'arguments': arguments }
return render_to_response("kernel_matrix/index.html",
properties,
context_instance = RequestContext(request))
Expand Down Expand Up @@ -73,11 +113,11 @@ def _process(x1_set, x2_set, kernel_width, kernel_name, degree):
feat_train = sg.RealFeatures(examples)

# construct covariance function
if kernel_name == "line":
if kernel_name == "LinearKernel":
kernel = sg.LinearKernel(feat_train, feat_train)
elif kernel_name == "poly":
elif kernel_name == "PolynomialKernel":
kernel = sg.PolyKernel(feat_train, feat_train, degree, True)
elif kernel_name == "gaus":
elif kernel_name == "GaussianKernel":
kernel = sg.GaussianKernel(feat_train, feat_train, kernel_width)
kernel_matrix=kernel.get_kernel_matrix()
return kernel_matrix.tolist()
42 changes: 38 additions & 4 deletions demo/svr.py
Expand Up @@ -7,7 +7,41 @@
import json

def entrance(request):
properties = { 'title': 'Supported Vector Regression Demo' }
arguments = [
{
'argument_type': 'select',
'argument_name': 'kernel',
'argument_items': ['GaussianKernel', 'PolynomialKernel', 'LinearKernel' ],
'argument_default': 'GaussianKernel'
},
{
'argument_type': 'decimal',
'argument_name': 'C',
'argument_default': '1.2'
},
{
'argument_type': 'decimal',
'argument_name': 'tube',
'argument_default': '0.04'
},
{
'argument_type': 'decimal',
'argument_name': 'sigma',
'argument_default': '0.3'
},
{
'argument_type': 'integer',
'argument_name': 'd',
'argument_default': '5'
},
{
'argument_type': 'button-group',
'argument_items': ['regress', 'clear']
}
]

properties = { 'title': 'Supported Vector Regression Demo',
'arguments': arguments }
return render_to_response("svr/index.html", properties, context_instance=RequestContext(request))

def point(request):
Expand Down Expand Up @@ -50,13 +84,13 @@ def _train_svr(cost, tubeeps, degree, width, kernel_name, labels, features):
lab = sg.RegressionLabels(labels)
train = sg.RealFeatures(examples)

if kernel_name == "line":
if kernel_name == "LinearKernel":
gk = sg.LinearKernel(train, train)
gk.set_normalizer(sg.IdentityKernelNormalizer())
elif kernel_name == "poly":
elif kernel_name == "PolynomialKernel":
gk = sg.PolyKernel(train, train, degree, True)
gk.set_normalizer(sg.IdentityKernelNormalizer())
elif kernel_name == "gaus":
elif kernel_name == "GaussianKernel":
gk = sg.GaussianKernel(train, train, width)
else:
raise TypeError
Expand Down
32 changes: 32 additions & 0 deletions templates/arguments_form.html
@@ -0,0 +1,32 @@
<form name="arguments">
{% for argument in arguments %}
{% if argument.argument_label %}
<label> {{ argument.argument_label }} </label>
{% elif argument.argument_name %}
<label> {{ argument.argument_name }} </label>
{% endif %}
{% if argument.argument_type == 'decimal' %}
<input type="text" value="{{ argument.argument_default }}" name="{{ argument.argument_name }}" onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')"></br>
{% elif argument.argument_type == 'integer' %}
<input type="text" value="{{ argument.argument_default }}" name="{{ argument.argument_name }}" onkeyup="value=value.replace(/[^\d]/g,'')">
{% elif argument.argument_type == 'select' %}
<select name="{{ argument.argument_name }}" >
{% for item in argument.argument_items %}
<option value="{{ item }}" {% if item == argument.argument_default %} selected {% endif %}>{{ item }} </option>
{% endfor %}
</select> <br/>
{% elif argument.argument_type == 'button-group' %}
<div class="btn-group">
{% for item in argument.argument_items %}
<button type="button" class="btn btn-small btn-primary" id="{{ item }}" onclick="{{ item }}_action();">
{{ item }}
</button>
{% endfor %}
</div>
{% elif argument.argument_type == 'div' %}
<div {% if argument.argument_id %} id="{{ argument.argument_id}}" {% endif %}
{% if argument.argument_class %} class="{{ argument.argument_class }}" {% endif %}
></div>
{% endif %}
{% endfor %}
</form>
28 changes: 6 additions & 22 deletions templates/clustering/index.html
Expand Up @@ -36,22 +36,6 @@
}
</style>
{% endblock %}
{% block arguments %}
<form name="arguments">
<label> Distance </label>
<select name="distance_name" class="span3">
<option value="eucl" selected>Euclidean Distance</option>
<option value="manh">Manhattan Metric</option>
<option value="jens">Jensen Metric</option>
</select></br>
<label> Number of Clusters ( <500 )</label>
<input type="text" value="2" name="number_of_clusters" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')"></br>
<div class="btn-group">
<button type="button" class="btn btn-small btn-primary" id="clust" onclick="cluster();">Cluster</button>
<button type="button" class="btn btn-small btn-primary" onclick="clear_canvas();">Clear</button>
</div>
</form>
{% endblock %}
{% block javascript %}
<script>
var positive_dots = {"points" : []};
Expand Down Expand Up @@ -184,7 +168,7 @@
.text("Y-axis");


function clear_canvas()
function clear_action()
{
svg.selectAll(".dot").remove();
svg.selectAll(".line")
Expand All @@ -196,19 +180,19 @@
negative_dots = {"points" : []};
}

function cluster()
function cluster_action()
{
if (positive_dots.points.length + negative_dots.points.length>0)
$.ajax(
{
url:"cluster",
type: "POST",
dataType: 'text',
beforeSend: function(){$("#clust").attr('disabled', true);},
beforeSend: function(){$("#cluster").attr('disabled', true);},
data: {positive: JSON.stringify(positive_dots),
negative: JSON.stringify(negative_dots),
number_of_clusters: document.arguments.number_of_clusters.value,
distance_name: document.arguments.distance_name.value,
distance: document.arguments.distance.value,
csrfmiddlewaretoken: '{{ csrf_token }}'},
success: function(data){
svg.selectAll(".line").remove();
Expand Down Expand Up @@ -243,13 +227,13 @@
.attr("x2", function(d) {return x(d.x+0.02);})
.attr("y2", function(d) {return y(d.y-0.02);})
.style("stroke", "gray");
$("#clust").attr('disabled', false);
$("#cluster").attr('disabled', false);

},

error: function(){
alert("Sorry! Got something wrong here, Check your input");
$("#clust").attr('disabled', false);
$("#cluster").attr('disabled', false);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion templates/default.html
Expand Up @@ -17,7 +17,7 @@
<div class="span9" style="display:inline;"></div>
<!-- arguments -->
<div class="span3" style="display:inline;">
{% block arguments %} {% endblock %}
{% include "arguments_form.html" %}
</div>
</div>
{% block javascript %} {% endblock %}
Expand Down

0 comments on commit b16be7b

Please sign in to comment.