Skip to content

Response params and available columns from operations api #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cognitive/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.db import models
from django.core.validators import validate_email
from django.core.exceptions import ValidationError
from jsonfield import JSONField
import string
import random

Expand Down Expand Up @@ -54,6 +55,8 @@ class Data_operation_type(models.Model):
function_arg = models.CharField(max_length=50, choices=FUNCTION_ARG)
function_arg_id = models.CharField(max_length=50, blank=True, null=True)
function_subtype = models.CharField(max_length=50, choices=FUNCTION_SUBTYPE)

# This should be JSON format
function_subtype_arg = models.CharField(max_length=50, blank=True, null=True)

def __str__(self):
Expand Down Expand Up @@ -167,6 +170,7 @@ class Component(models.Model):
execution_end_time = models.DateField(blank=True, null=True)
data_location = models.CharField(max_length=50, blank=True, null=True)
preferred_data_location = models.CharField(max_length=50, blank=True, null=True)
outputs = JSONField()
# component_id = models.IntegerField(blank=True, null=True)


Expand Down
25 changes: 24 additions & 1 deletion cognitive/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
# License for the specific language governing permissions and limitations
# under the License.

import json

from rest_framework import serializers
from .models import User, Experiment, Component, Workflow
from .models import User, Experiment, Component, Workflow, Data_operation_type


class UserSerializer(serializers.ModelSerializer):
Expand All @@ -35,11 +37,32 @@ class Meta:
# 'execution_start_time', 'execution_end_time', 'component_start_id')


class ComponentOutputValue(serializers.ModelSerializer):
class Meta:
fields = ('function_subtype', 'function_subtype_arg')
model = Data_operation_type


class ComponentSerializer(serializers.ModelSerializer):
type = serializers.SerializerMethodField('component_type')
params = serializers.SerializerMethodField('component_params')

class Meta:
model = Component

def component_type(self, obj):
return obj.operation_type.function_subtype

def component_params(self, obj):
data = obj.operation_type.function_subtype_arg
if data is None:
return ''
elif data.startswith(('[', '{')):
return json.loads(data)
else:
# TODO(|Less Priority| All Data_operation_type.function_subtype_arg values should be JSON)
return data


class WorkflowSerializer(serializers.ModelSerializer):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
.on("dragend", finishDrawingConnection));

g.append('text')
.attr('x', '{{node.x - 15}}')
.attr('y', '{{node.y - 5}}')
.attr('ng-attr-x', '{{node.x - 15}}')
.attr('ng-attr-y', '{{node.y - 5}}')
.attr('font-family', 'FontAwesome')
.attr('class', 'node close-icon')
.attr('node', '{{node.id}}')
Expand Down
17 changes: 15 additions & 2 deletions cognitive/app/view/view_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,19 @@
from rest_framework.renderers import JSONRenderer
from pandas import read_csv, datetime
import json
import re
import urllib2

MAX_COMPONENTS_PER_EXP = 100

def get_csv_fields(csv_data):
for line in re.split('\n|\r', csv_data):
if line == '':
continue
parsed_line = line.split(',')
return [{'id': i,'name': parsed_line[i]} for i in range(len(parsed_line))]
return []

#####
# Operation function_type function_arg function_subtype function_arg_id function_subtype_arg
# mathformula Update {comp_type} {op_type} {comp_id} {op_constant}
Expand All @@ -35,7 +44,6 @@
# input Create Table Input - {filename}
# machine learning Create Model {model_type} {Train-test} {ML arguments}


class OperationViewSet(viewsets.ViewSet):

def set_operation(self, operation, data):
Expand Down Expand Up @@ -178,9 +186,14 @@ def create(self, request, operation):
print "Experiment ", exp_id, " Operation ", operation
op = self.set_operation(operation, data)

outputs = []
if operation == 'input' and data["input_file_type"] == "csv":
outputs = get_csv_fields(data["data_values"])

component = Component(
experiment=exp, created_time=datetime.now(),
modified_time=datetime.now(), operation_type=op)
modified_time=datetime.now(), operation_type=op, outputs=outputs)

component.save()
serializer = ComponentSerializer(component)
return send_response("GET", serializer)
Expand Down
54 changes: 27 additions & 27 deletions cognitive/app/view/view_result_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ def run(self):
input_data, model_type, train_data_percentage,
input_features, target_feature)
output_data = classifier.learn()
except ValueError as e:
except ValueError:
status = "failure"
err_msg = " Invalid input for the model training"
except KeyError as e:
except KeyError:
status = "failure"
err_msg = target_feature + " column is not available for Model Training"

Expand All @@ -132,72 +132,72 @@ def run(self):
column_id = float(op.function_arg_id)
column_name = feature_names[column_id]
if column_name not in input_data:
#print "Column name ", column_name, " not present. Skipping"
#continue # throw error in module status
# print "Column name ", column_name, " not present. Skipping"
# continue # throw error in module status
status = "failure"
err_msg = column_name + " column is not available for current operation"
elif input_data[column_name].dtype == 'object':
#print "Column name ", column_name, " is not integer/float. Skipping"
#continue # throw error in module status
# print "Column name ", column_name, " is not integer/float. Skipping"
# continue # throw error in module status
status = "failure"
err_msg = " Invalid input in column "+ column_name+ " for the current operation"
err_msg = " Invalid input in column " + column_name + " for the current operation"
else:
input_data[column_name] += constant_value
if op.function_subtype == 'Sub':
constant_value = float(op.function_subtype_arg)
column_id = float(op.function_arg_id)
column_name = feature_names[column_id]
if column_name not in input_data:
#print "Column name ", column_name, " not present. Skipping"
#continue # throw error in module status
# print "Column name ", column_name, " not present. Skipping"
# continue # throw error in module status
status = "failure"
err_msg = column_name + " column is not available for current operation"
elif input_data[column_name].dtype == 'object':
#print "Column name ", column_name, " is not integer/float. Skipping"
#continue # throw error in module status
# print "Column name ", column_name, " is not integer/float. Skipping"
# continue # throw error in module status
status = "failure"
err_msg = " Invalid input in column "+ column_name+ " for the current operation"
err_msg = " Invalid input in column " + column_name + " for the current operation"
else:
input_data[column_name] -= constant_value
if op.function_subtype == 'Mult':
constant_value = float(op.function_subtype_arg)
column_id = float(op.function_arg_id)
column_name = feature_names[column_id]
if column_name not in input_data:
#print "Column name ", column_name, " not present. Skipping"
#continue # throw error in module status
# print "Column name ", column_name, " not present. Skipping"
# continue # throw error in module status
status = "failure"
err_msg = column_name + " column is not available for current operation"
elif input_data[column_name].dtype == 'object':
#print "Column name ", column_name, " is not integer/float. Skipping"
#continue # throw error in module status
# print "Column name ", column_name, " is not integer/float. Skipping"
# continue # throw error in module status
status = "failure"
err_msg = " Invalid input in column "+ column_name+ " for the current operation"
err_msg = " Invalid input in column " + column_name + " for the current operation"
else:
input_data[column_name] *= constant_value
if op.function_subtype == 'Div':
constant_value = float(op.function_subtype_arg)
column_id = float(op.function_arg_id)
column_name = feature_names[column_id]
if column_name not in input_data:
#print "Column name ", column_name, " not present. Skipping"
#continue # throw error in module status
# print "Column name ", column_name, " not present. Skipping"
# continue # throw error in module status
status = "failure"
err_msg = column_name + " column is not available for current operation"
elif input_data[column_name].dtype == 'object':
#print "Column name ", column_name, " is not integer/float. Skipping"
#continue # throw error in module status
# print "Column name ", column_name, " is not integer/float. Skipping"
# continue # throw error in module status
status = "failure"
err_msg = " Invalid input in column "+ column_name+ " for the current operation"
err_msg = " Invalid input in column " + column_name + " for the current operation"
else:
input_data[column_name] /= constant_value
if op.function_subtype == 'Normalize':
column_id = float(op.function_arg_id)
column_name = feature_names[column_id]
sum_array = input_data.sum(axis=0)
if column_name not in sum_array:
#print "Column name ", column_name, " not present. Skipping"
#continue # throw error in module status
# print "Column name ", column_name, " not present. Skipping"
# continue # throw error in module status
status = "failure"
err_msg = column_name + " column is not available for current operation"
else:
Expand All @@ -222,8 +222,8 @@ def run(self):
for elem in column_id_list:
column_name = feature_names[elem]
if column_name not in input_data:
#print "Column name ", column_name, " not present. Skipping"
#continue # throw error in module status
# print "Column name ", column_name, " not present. Skipping"
# continue # throw error in module status
status = "failure"
err_msg = column_name + " column is not available for current operation"
else:
Expand Down Expand Up @@ -330,7 +330,7 @@ def run(self):
if self.cache_results is True:
CACHE[self.experiment] = input_data

#print self.result
# print self.result
print self.result["status"]
print self.result["message"]
break
Expand Down
7 changes: 4 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
Django==1.7.1
Django>=1.7.1
django-rest-swagger>=0.2.9
Markdown==2.5.1
Pygments==2.0.1
django-bower==5.0.1
django-bower>=5.0.1
django-mptt==0.6.1
django-treebeard==2.0
djangorestframework==2.4.4
hamlpy==0.82.2
hamlpy>=0.82.2
jasmine==2.3.0
jasmine-core==2.3.4
jsonfield>=1.0.3
numpy==1.9.1
pandas==0.15.1
python-dateutil==2.2
Expand Down