def mkcici(ci1, rel, ci2, project, user, save=True): cici = ClassInstanceClassInstance(user=user, project=project, class_instance_a=ci1, relation=rel, class_instance_b=ci2) if save: cici.save() return cici
def _create_relation(user, project_id, relation_id, instance_a_id, instance_b_id): relation = ClassInstanceClassInstance() relation.user = user relation.project_id = project_id relation.relation_id = relation_id relation.class_instance_a_id = instance_a_id relation.class_instance_b_id = instance_b_id relation.save() return relation
def create_node(): """ Creates a new node. """ # TODO: Test if class and parent class instance exist # if params['classid'] not in class_map: # raise CatmaidException('Failed to select class.') classification_instance_operation.res_on_err = 'Failed to insert instance of class.' node = ClassInstance( user=request.user, name=params['objname']) node.project_id = workspace_pid node.class_column_id = params['classid'] node.save() class_name = node.class_column.class_name insert_into_log(project_id, request.user.id, "create_%s" % class_name, None, "Created %s with ID %s" % (class_name, params['id'])) # We need to connect the node to its parent, or to root if no valid parent is given. node_parent_id = params['parentid'] # TODO: Test if tis parent exists #if 0 == params['parentid']: # # Find root element # classification_instance_operation.res_on_err = 'Failed to select classification root.' # node_parent_id = ClassInstance.objects.filter( # project=workspace_pid, # class_column=class_map['classification_root'])[0].id #Relation.objects.filter(id=params['relationid']) #if params['relationname'] not in relation_map: # raise CatmaidException('Failed to select relation %s' % params['relationname']) classification_instance_operation.res_on_err = 'Failed to insert CICI-link.' cici = ClassInstanceClassInstance() cici.user = request.user cici.project_id = workspace_pid cici.relation_id = params['relationid'] cici.class_instance_a_id = node.id cici.class_instance_b_id = node_parent_id cici.save() return HttpResponse(json.dumps({'class_instance_id': node.id}))
def create_node(): # Can only create a node if the parent node is owned by the user # or the user is a superuser # Given that the parentid is 0 to signal root (but root has a non-zero id), # this implies that regular non-superusers cannot create nodes under root, # but only in their staging area. can_edit_class_instance_or_fail(request.user, params['parentid']) if params['classname'] not in class_map: raise Exception('Failed to select class.') instance_operation.res_on_err = 'Failed to insert instance of class.' node = ClassInstance( user=request.user, name=params['objname']) node.project_id = project_id node.class_column_id = class_map[params['classname']] node.save() insert_into_log(project_id, request.user.id, "create_%s" % params['classname'], None, "Created %s with ID %s" % (params['classname'], params['id'])) # We need to connect the node to its parent, or to root if no valid parent is given. node_parent_id = params['parentid'] if 0 == params['parentid']: # Find root element instance_operation.res_on_err = 'Failed to select root.' node_parent_id = ClassInstance.objects.filter( project=project_id, class_column=class_map['root'])[0].id if params['relationname'] not in relation_map: instance_operation.res_on_err = '' raise Exception('Failed to select relation %s' % params['relationname']) instance_operation.res_on_err = 'Failed to insert relation.' cici = ClassInstanceClassInstance() cici.user = request.user cici.project_id = project_id cici.relation_id = relation_map[params['relationname']] cici.class_instance_a_id = node.id cici.class_instance_b_id = node_parent_id cici.save() return HttpResponse(json.dumps({'class_instance_id': node.id}))
def lines_add(request, project_id=None): p = Project.objects.get(pk=project_id) # FIXME: for the moment, just hardcode the user ID: user = User.objects.get(pk=3) neuron = get_object_or_404(ClassInstance, pk=request.POST['neuron_id'], project=p) # There's a race condition here, if two people try to add a line # with the same name at the same time. The normal way to deal # with this would be to make the `name` column unique in the # table, but since the class_instance table isn't just for driver # lines, we can't do that. (FIXME) try: line = ClassInstance.objects.get(name=request.POST['line_name']) except ClassInstance.DoesNotExist: line = ClassInstance() line.name = request.POST['line_name'] line.project = p line.user = user line.class_column = Class.objects.get(class_name='driver_line', project=p) line.save() r = Relation.objects.get(relation_name='expresses_in', project=p) cici = ClassInstanceClassInstance() cici.class_instance_a = line cici.class_instance_b = neuron cici.relation = r cici.user = user cici.project = p cici.save() return HttpResponseRedirect( reverse('vncbrowser.views.view', kwargs={ 'neuron_id': neuron.id, 'project_id': p.id }))
def lines_add(request, project_id=None): p = Project.objects.get(pk=project_id) # FIXME: for the moment, just hardcode the user ID: user = User.objects.get(pk=3) neuron = get_object_or_404(ClassInstance, pk=request.POST['neuron_id'], project=p) # There's a race condition here, if two people try to add a line # with the same name at the same time. The normal way to deal # with this would be to make the `name` column unique in the # table, but since the class_instance table isn't just for driver # lines, we can't do that. (FIXME) try: line = ClassInstance.objects.get(name=request.POST['line_name']) except ClassInstance.DoesNotExist: line = ClassInstance() line.name=request.POST['line_name'] line.project = p line.user = user line.class_column = Class.objects.get(class_name='driver_line', project=p) line.save() r = Relation.objects.get(relation_name='expresses_in', project=p) cici = ClassInstanceClassInstance() cici.class_instance_a = line cici.class_instance_b = neuron cici.relation = r cici.user = user cici.project = p cici.save() return HttpResponseRedirect(reverse('vncbrowser.views.view', kwargs={'neuron_id':neuron.id, 'project_id':p.id}))
def split_skeleton(request, project_id=None): """ The split is only possible if the neuron is not locked or if it is locked by the current user or if the current user belongs to the group of the user who locked it. Of course, the split is also possible if the current user is a super-user. Also, all reviews of the treenodes in the new neuron are updated to refer to the new skeleton. """ treenode_id = int(request.POST['treenode_id']) treenode = Treenode.objects.get(pk=treenode_id) skeleton_id = treenode.skeleton_id upstream_annotation_map = json.loads(request.POST.get('upstream_annotation_map')) downstream_annotation_map = json.loads(request.POST.get('downstream_annotation_map')) cursor = connection.cursor() # Check if the treenode is root! if not treenode.parent: return HttpResponse(json.dumps({'error': 'Can\'t split at the root node: it doesn\'t have a parent.'})) # Check if annotations are valid if not check_annotations_on_split(project_id, skeleton_id, frozenset(upstream_annotation_map.keys()), frozenset(downstream_annotation_map.keys())): raise Exception("Annotation distribution is not valid for splitting. " \ "One part has to keep the whole set of annotations!") skeleton = ClassInstance.objects.select_related('user').get(pk=skeleton_id) project_id=int(project_id) # retrieve neuron of this skeleton neuron = ClassInstance.objects.get( cici_via_b__relation__relation_name='model_of', cici_via_b__class_instance_a_id=skeleton_id) # Make sure the user has permissions to edit can_edit_class_instance_or_fail(request.user, neuron.id, 'neuron') # retrieve the id, parent_id of all nodes in the skeleton # with minimal ceremony cursor.execute(''' SELECT id, parent_id FROM treenode WHERE skeleton_id=%s ''' % skeleton_id) # no need to sanitize # build the networkx graph from it graph = nx.DiGraph() for row in cursor.fetchall(): graph.add_node( row[0] ) if row[1]: # edge from parent_id to id graph.add_edge( row[1], row[0] ) # find downstream nodes starting from target treenode_id # and generate the list of IDs to change, starting at treenode_id (inclusive) change_list = nx.bfs_tree(graph, treenode_id).nodes() if not change_list: # When splitting an end node, the bfs_tree doesn't return any nodes, # which is surprising, because when the splitted tree has 2 or more nodes # the node at which the split is made is included in the list. change_list.append(treenode_id) # create a new skeleton new_skeleton = ClassInstance() new_skeleton.name = 'Skeleton' new_skeleton.project_id = project_id new_skeleton.user = skeleton.user # The same user that owned the skeleton to split new_skeleton.class_column = Class.objects.get(class_name='skeleton', project_id=project_id) new_skeleton.save() new_skeleton.name = 'Skeleton {0}'.format( new_skeleton.id ) # This could be done with a trigger in the database new_skeleton.save() # Create new neuron new_neuron = ClassInstance() new_neuron.name = 'Neuron' new_neuron.project_id = project_id new_neuron.user = skeleton.user new_neuron.class_column = Class.objects.get(class_name='neuron', project_id=project_id) new_neuron.save() new_neuron.name = 'Neuron %s' % str(new_neuron.id) new_neuron.save() # Assign the skeleton to new neuron cici = ClassInstanceClassInstance() cici.class_instance_a = new_skeleton cici.class_instance_b = new_neuron cici.relation = Relation.objects.get(relation_name='model_of', project_id=project_id) cici.user = skeleton.user # The same user that owned the skeleton to split cici.project_id = project_id cici.save() # update skeleton_id of list in treenode table # This creates a lazy QuerySet that, upon calling update, returns a new QuerySet # that is then executed. It does NOT create an update SQL query for every treenode. tns = Treenode.objects.filter(id__in=change_list).update(skeleton=new_skeleton) # update the skeleton_id value of the treenode_connector table tc = TreenodeConnector.objects.filter( relation__relation_name__endswith = 'synaptic_to', treenode__in=change_list, ).update(skeleton=new_skeleton) # setting new root treenode's parent to null Treenode.objects.filter(id=treenode_id).update(parent=None, editor=request.user) # Update annotations of existing neuron to have only over set _update_neuron_annotations(project_id, request.user, neuron.id, upstream_annotation_map) # Update all reviews of the treenodes that are moved to a new neuron to # refer to the new skeleton. Review.objects.filter(treenode_id__in=change_list).update(skeleton=new_skeleton) # Update annotations of under skeleton _annotate_entities(project_id, [new_neuron.id], downstream_annotation_map) # Log the location of the node at which the split was done location = (treenode.location_x, treenode.location_y, treenode.location_z) insert_into_log(project_id, request.user.id, "split_skeleton", location, "Split skeleton with ID {0} (neuron: {1})".format( skeleton_id, neuron.name ) ) return HttpResponse(json.dumps({}), content_type='text/json')