def annotations_for_skeletons(request, project_id=None): skids = tuple(int(skid) for key, skid in request.POST.iteritems() if key.startswith('skids[')) cursor = connection.cursor() cursor.execute("SELECT id FROM relation WHERE project_id=%s AND relation_name='annotated_with'" % int(project_id)) annotated_with_id = cursor.fetchone()[0] # Select pairs of skeleton_id vs annotation name cursor.execute(''' SELECT skeleton_neuron.class_instance_a, annotation.name FROM class_instance_class_instance skeleton_neuron, class_instance_class_instance neuron_annotation, class_instance annotation WHERE skeleton_neuron.class_instance_a IN (%s) AND skeleton_neuron.class_instance_b = neuron_annotation.class_instance_a AND neuron_annotation.relation_id = %s AND neuron_annotation.class_instance_b = annotation.id ''' % (",".join(map(str, skids)), annotated_with_id)) # Group by skeleton ID m = defaultdict(list) for skid, name in cursor.fetchall(): m[skid].append(name) return HttpResponse(json.dumps(m, separators=(',', ':')))
def annotations_for_entities(request, project_id=None): ids = tuple(int(eid) for key, eid in request.POST.iteritems() if key.startswith("ids[")) cursor = connection.cursor() cursor.execute("SELECT id FROM relation WHERE project_id=%s AND relation_name='annotated_with'" % int(project_id)) annotated_with_id = cursor.fetchone()[0] # Select pairs of skeleton_id vs annotation name cursor.execute( """ SELECT entity_annotation.class_instance_a, annotation.id, annotation.name, entity_annotation.user_id FROM class_instance_class_instance entity_annotation, class_instance annotation WHERE entity_annotation.class_instance_a IN (%s) AND entity_annotation.relation_id = %s AND entity_annotation.class_instance_b = annotation.id """ % (",".join(map(str, ids)), annotated_with_id) ) # Group by entity ID m = defaultdict(list) a = dict() for eid, aid, name, uid in cursor.fetchall(): m[eid].append({"id": aid, "uid": uid}) a[aid] = name return HttpResponse(json.dumps({"entities": m, "annotations": a}, separators=(",", ":")))
def annotations_for_skeletons(request, project_id=None): skids = tuple( int(skid) for key, skid in request.POST.iteritems() if key.startswith('skids[')) cursor = connection.cursor() cursor.execute( "SELECT id FROM relation WHERE project_id=%s AND relation_name='annotated_with'" % int(project_id)) annotated_with_id = cursor.fetchone()[0] # Select pairs of skeleton_id vs annotation name cursor.execute(''' SELECT skeleton_neuron.class_instance_a, annotation.name FROM class_instance_class_instance skeleton_neuron, class_instance_class_instance neuron_annotation, class_instance annotation WHERE skeleton_neuron.class_instance_a IN (%s) AND skeleton_neuron.class_instance_b = neuron_annotation.class_instance_a AND neuron_annotation.relation_id = %s AND neuron_annotation.class_instance_b = annotation.id ''' % (",".join(map(str, skids)), annotated_with_id)) # Group by skeleton ID m = defaultdict(list) for skid, name in cursor.fetchall(): m[skid].append(name) return HttpResponse(json.dumps(m, separators=(',', ':')))
def annotations_for_entities(request, project_id=None): """Query annotations linked to a list of objects. These objects can for instance be neurons, annotations or stack groups. From a database perspective, these objects are class instances. Returned is an object with the fields "entities" and "annotations". The former is an object mapping an entity ID to a list of annotations. Each annotation is represented by an object containing its "id" and "uid", the user who annotated it. The latter maps annotation IDs to annotation names. For instance:: { "entities": { "42": [{id: 1, uid: 12}, {id: 3, uid: 14}] }, "annotations": { 12: "example1", 14: "example2" } } --- parameters: - name: object_ids description: A list of object IDs for which annotations should be returned. paramType: form type: array allowMultiple: true items: type: integer description: A skeleton ID """ # Get 'annotated_with' relation ID object_ids = tuple( get_request_list(request.POST, 'object_ids', [], map_fn=int)) cursor = connection.cursor() cursor.execute(""" SELECT id FROM relation WHERE project_id=%s AND relation_name='annotated_with'""" % int(project_id)) annotated_with_id = cursor.fetchone()[0] # Select pairs of skeleton_id vs annotation name cursor.execute(''' SELECT entity_annotation.class_instance_a, annotation.id, annotation.name, entity_annotation.user_id FROM class_instance_class_instance entity_annotation, class_instance annotation WHERE entity_annotation.class_instance_a IN (%s) AND entity_annotation.relation_id = %s AND entity_annotation.class_instance_b = annotation.id ''' % (",".join(map(str, object_ids)), annotated_with_id)) # Group by entity ID m = defaultdict(list) a = dict() for eid, aid, name, uid in cursor.fetchall(): m[eid].append({'id': aid, 'uid': uid}) a[aid] = name return JsonResponse({ 'entities': m, 'annotations': a }, json_dumps_params={'separators': (',', ':')})
def annotations_for_entities(request, project_id=None): """Query annotations linked to a list of objects. These objects can for instance be neurons, annotations or stack groups. From a database perspective, these objects are class instances. Returned is an object with the fields "entities" and "annotations". The former is an object mapping an entity ID to a list of annotations. Each annotation is represented by an object containing its "id" and "uid", the user who annotated it. The latter maps annotation IDs to annotation names. For instance:: { "entities": { "42": [{id: 1, uid: 12}, {id: 3, uid: 14}] }, "annotations": { 12: "example1", 14: "example2" } } --- parameters: - name: object_ids description: A list of object IDs for which annotations should be returned. paramType: form type: array allowMultiple: true items: type: integer description: A skeleton ID """ # Get 'annotated_with' relation ID object_ids = tuple(int(eid) for key, eid in request.POST.iteritems() \ if key.startswith('object_ids[')) cursor = connection.cursor() cursor.execute(""" SELECT id FROM relation WHERE project_id=%s AND relation_name='annotated_with'""" % int(project_id)) annotated_with_id = cursor.fetchone()[0] # Select pairs of skeleton_id vs annotation name cursor.execute(''' SELECT entity_annotation.class_instance_a, annotation.id, annotation.name, entity_annotation.user_id FROM class_instance_class_instance entity_annotation, class_instance annotation WHERE entity_annotation.class_instance_a IN (%s) AND entity_annotation.relation_id = %s AND entity_annotation.class_instance_b = annotation.id ''' % (",".join(map(str, object_ids)), annotated_with_id)) # Group by entity ID m = defaultdict(list) a = dict() for eid, aid, name, uid in cursor.fetchall(): m[eid].append({'id': aid, 'uid': uid}) a[aid] = name return HttpResponse(json.dumps({ 'entities': m, 'annotations': a }, separators=(',', ':')))
def annotations_for_skeletons(request, project_id=None): """Get annotations and who used them for a set of skeletons. This method focuses only on annotations linked to skeletons and is likely to be faster than the general query. Returns an object with two fields: "annotations", which is itself an object with annotation IDs as fields, giving access to the corresponding annotation names. And the field "skeletons" is also an object, mapping skeleton IDs to lists of annotation-annotator ID pairs. Also, as JSON separator a colon is used instead of a comma. --- parameters: - name: skeleton_ids description: A list of skeleton IDs which are annotated by the resulting annotations. paramType: form type: array items: type: integer description: A skeleton ID """ skids = tuple( get_request_list(request.POST, 'skeleton_ids', [], map_fn=int)) cursor = connection.cursor() cursor.execute( "SELECT id FROM relation WHERE project_id=%s AND relation_name='annotated_with'" % int(project_id)) annotated_with_id = cursor.fetchone()[0] # Select pairs of skeleton_id vs annotation name cursor.execute(''' SELECT skeleton_neuron.class_instance_a, annotation.id, annotation.name, neuron_annotation.user_id FROM class_instance_class_instance skeleton_neuron, class_instance_class_instance neuron_annotation, class_instance annotation WHERE skeleton_neuron.class_instance_a IN (%s) AND skeleton_neuron.class_instance_b = neuron_annotation.class_instance_a AND neuron_annotation.relation_id = %s AND neuron_annotation.class_instance_b = annotation.id ''' % (",".join(map(str, skids)), annotated_with_id)) # Group by skeleton ID m = defaultdict(list) a = dict() for skid, aid, name, uid in cursor.fetchall(): m[skid].append({'id': aid, 'uid': uid}) a[aid] = name return JsonResponse({ 'skeletons': m, 'annotations': a }, json_dumps_params={'separators': (',', ':')})
def annotations_for_skeletons(request, project_id=None): """Get annotations and who used them for a set of skeletons. This method focuses only on annotations linked to skeletons and is likely to be faster than the general query. Returns an object with two fields: "annotations", which is itself an object with annotation IDs as fields, giving access to the corresponding annotation names. And the field "skeletons" is also an object, mapping skeleton IDs to lists of annotation-annotator ID pairs. Also, as JSON separator a colon is used instead of a comma. --- parameters: - name: skeleton_ids description: A list of skeleton IDs which are annotated by the resulting annotations. paramType: form type: array items: type: integer description: A skeleton ID """ skids = tuple(int(skid) for key, skid in request.POST.iteritems() \ if key.startswith('skeleton_ids[')) cursor = connection.cursor() cursor.execute("SELECT id FROM relation WHERE project_id=%s AND relation_name='annotated_with'" % int(project_id)) annotated_with_id = cursor.fetchone()[0] # Select pairs of skeleton_id vs annotation name cursor.execute(''' SELECT skeleton_neuron.class_instance_a, annotation.id, annotation.name, neuron_annotation.user_id FROM class_instance_class_instance skeleton_neuron, class_instance_class_instance neuron_annotation, class_instance annotation WHERE skeleton_neuron.class_instance_a IN (%s) AND skeleton_neuron.class_instance_b = neuron_annotation.class_instance_a AND neuron_annotation.relation_id = %s AND neuron_annotation.class_instance_b = annotation.id ''' % (",".join(map(str, skids)), annotated_with_id)) # Group by skeleton ID m = defaultdict(list) a = dict() for skid, aid, name, uid in cursor.fetchall(): m[skid].append({'id': aid, 'uid': uid}) a[aid] = name return HttpResponse(json.dumps({ 'skeletons': m, 'annotations': a }, separators=(',', ':')))