def test_add_task_contrast(self): self.assertTrue( self.client.login(username=self.user.username, password=self.password)) task = Task() condition = Condition() tsk = task.create('test_add_task_contrast', {'prop': 'prop'}) cond1 = condition.create('test_add_task_contrast_cond1', {'prop': 'prop'}) cond2 = condition.create('test_add_task_contrast_cond2', {'prop': 'prop'}) cond_names = [ 'test_add_task_contrast_cond1', 'test_add_task_contrast_cond2' ] task.link(tsk.properties['id'], cond1.properties['id'], 'HASCONDITION', endnode_type='condition') task.link(tsk.properties['id'], cond2.properties['id'], 'HASCONDITION', endnode_type='condition') response = self.client.get( reverse('add_task_contrast', kwargs={'uid': tsk.properties['id']})) self.assertEqual(response.status_code, 200) self.assertIn(response.context['conditions'][0]['condition_name'], cond_names) self.assertIn(response.context['conditions'][1]['condition_name'], cond_names) self.assertEqual(len(response.context['conditions']), 2) tsk.delete_related() tsk.delete() cond1.delete() cond2.delete()
def add_contrast(request, task_id): '''add_contrast is the function called when the user submits a set of conditions and an operator to specify a new contrast. :param task_id: the id of the task, to return to the correct page after submission ''' if request.method == "POST": relation_type = "HASCONTRAST" #condition --HASCONTRAST-> contrast # Get fields from post post = dict(request.POST) #pickle.dump(post,open('result.pkl','wb')) contrast_name = post.get('contrast_name', '') skip = ["contrast_name", "csrfmiddlewaretoken"] # Get dictionary with new conditions with nonzero weights conditions = dict() condition_ids = [x for x in post.keys() if x not in skip] for condition_id in condition_ids: weight = int(post.get(condition_id, 0)[0]) if weight != 0: conditions[condition_id] = weight if contrast_name != "" and len(conditions) > 0: node = Contrast.create(name=contrast_name) # Make a link between contrast and conditions, specify side as property of relation for condition_id, weight in conditions.items(): properties = {"weight": weight} Condition.link(condition_id, node["id"], relation_type, endnode_type="contrast", properties=properties) return view_task(request, task_id)
def add_cypher_relations(cypher, lookup): '''add_cypher_relations will look up relations for connected nodes returned from an object. This is akin to adding one layer of the tree to the graph (and the function could be modified to add more) :param cypher: the cypher object, a dictionary with nodes and links as keys, each a list of them. ''' node_types = list(lookup) for node_type in node_types: for term in lookup[node_type]: if node_type == "condition": new_cypher, lookup = Condition.cypher(term, lookup=lookup, return_lookup=True) elif node_type == "task": new_cypher, lookup = Task.cypher(term, lookup=lookup, return_lookup=True) elif node_type == "contrast": new_cypher, lookup = Contrast.cypher(term, lookup=lookup, return_lookup=True) elif node_type == "concept": new_cypher, lookup = Concept.cypher(term, lookup=lookup, return_lookup=True) cypher = merge_cypher(cypher, new_cypher) return cypher
def setUp(self): self.task = Task() self.node_name = "test_name" self.node_properties = {'test_key': 'test_value'} self.graph = Graph("http://graphdb:7474/db/data/") self.task1 = self.task.create( name=self.node_name, properties=self.node_properties) self.task2 = self.task.create( name=self.node_name, properties=self.node_properties) condition = Condition() self.cond = condition.create( name=self.node_name, properties=self.node_properties) contrast = Contrast() self.cont = contrast.create( name=self.node_name, properties=self.node_properties) concept = Concept() self.con = concept.create( name=self.node_name, properties=self.node_properties) self.task.link( self.task1.properties['id'], self.cond.properties['id'], "HASCONDITION", endnode_type='condition') self.task.link( self.task1.properties['id'], self.con.properties['id'], "ASSERTS", endnode_type='concept') condition.link( self.cond.properties['id'], self.cont.properties['id'], "HASCONTRAST", endnode_type='contrast') concept.link( self.con.properties['id'], self.cont.properties['id'], "MEASUREDBY", endnode_type='contrast')
def add_condition(request, task_id): '''add_condition will add a condition associated with a task :param task_id: the uid of the task, to return to the correct page after creation ''' if request.method == "POST": relation_type = "HASCONDITION" #task --HASCONDITION-> condition condition_name = request.POST.get('condition_name', '') if condition_name != "": condition = Condition.create(name=condition_name) Task.link(task_id, condition["id"], relation_type, endnode_type="condition") return view_task(request, task_id)
def add_cypher_relations(cypher,lookup): '''add_cypher_relations will look up relations for connected nodes returned from an object. This is akin to adding one layer of the tree to the graph (and the function could be modified to add more) :param cypher: the cypher object, a dictionary with nodes and links as keys, each a list of them. ''' node_types = list(lookup) for node_type in node_types: for term in lookup[node_type]: if node_type == "condition": new_cypher,lookup = Condition.cypher(term,lookup=lookup,return_lookup=True) elif node_type == "task": new_cypher,lookup = Task.cypher(term,lookup=lookup,return_lookup=True) elif node_type == "contrast": new_cypher,lookup = Contrast.cypher(term,lookup=lookup,return_lookup=True) elif node_type == "concept": new_cypher,lookup = Concept.cypher(term,lookup=lookup,return_lookup=True) cypher = merge_cypher(cypher,new_cypher) return cypher
def test_add_condition(self): self.assertTrue( self.client.login(username=self.user.username, password=self.password)) task = Task() condition = Condition() tsk = task.create("test_add_condition_task", { "prop": "prop", "definition": "definition" }) uid = tsk.properties['id'] response = self.client.post( reverse('add_condition', kwargs={'task_id': uid}), {'condition_name': 'test_add_condition'}) self.assertEqual(response.status_code, 200) relation = task.get_conditions(uid) self.assertEqual(len(relation), 1) self.assertEqual(relation[0]['condition_name'], 'test_add_condition') con = condition.graph.find_one('condition', 'id', relation[0]['condition_id']) tsk.delete_related() tsk.delete() con.delete()
from django.http import HttpResponse, JsonResponse from django.shortcuts import render from django.template import loader, Context from cognitive.apps.atlas.query import Concept, Condition, Contrast, Task from cognitive.apps.atlas.utils import merge_cypher Task = Task() Concept = Concept() Condition = Condition() Contrast = Contrast() # Return full graph visualizations def graph_view(request, label, uid): query = "MATCH (n:{}) where n.id = '{}' OPTIONAL MATCH (n)-[]-(r) return n, r" query = query.format(label, uid) return render(request, "graph/graph.html", {'query': query}) def task_graph(request, uid): nodes = Task.get_graph(uid) context = {"graph": nodes} return render(request, "graph/task.html", context) def concept_graph(request, uid): nodes = Concept.get_graph(uid) context = {"graph": nodes} return render(request, "graph/task.html", context)