<commit_before>from django.http import HttpResponse
from django.shortcuts import get_object_or_404

from catmaid.models import *
from catmaid.objects import *
from catmaid.control.authentication import *
from catmaid.control.common import *
from catmaid.control.neuron import _delete_if_empty
from catmaid.control.neuron_annotations import create_annotation_query
from catmaid.control.neuron_annotations import _annotate_entities
from catmaid.control.neuron_annotations import _update_neuron_annotations
from catmaid.control.review import get_treenodes_to_reviews, get_review_status
from catmaid.control.treenode import _create_interpolated_treenode
from collections import defaultdict

import decimal
import json

from operator import itemgetter
import networkx as nx
from tree_util import reroot, edge_count_to_root


def get_skeleton_permissions(request, project_id, skeleton_id):
    """ Tests editing permissions of a user on a skeleton and returns the
    result as JSON object."""
    try:
        nn = _get_neuronname_from_skeletonid( project_id, skeleton_id )
        can_edit = can_edit_class_instance_or_fail(request.user,
                nn['neuronid'])
    except:
        can_edit = False

    permissions = {
      'can_edit': can_edit,
    }

    return HttpResponse(json.dumps(permissions))

@requires_user_role([UserRole.Annotate, UserRole.Browse])
def last_openleaf(request, project_id=None, skeleton_id=None):
    """ Return the ID of the nearest node (or itself), and its location string;
    or two nulls if none found. """
    tnid = int(request.POST['tnid'])
    cursor = connection.cursor()

    cursor.execute("SELECT id FROM relation WHERE project_id=%s AND relation_name='labeled_as'" % int(project_id))
    labeled_as = cursor.fetchone()[0]

    # Select all nodes and their tags
    cursor.execute('''
    SELECT t.id, t.parent_id, t.location_x, t.location_y, t.location_z, ci.name
    FROM treenode t LEFT OUTER JOIN (treenode_class_instance tci INNER JOIN class_instance ci ON tci.class_instance_id = ci.id AND tci.relation_id = %s) ON t.id = tci.treenode_id
    WHERE t.skeleton_id = %s
    ''' % (labeled_as, int(skeleton_id)))

    # Some entries repeated, when a node has more than one tag
    # Create a graph with edges from parent to child, and accumulate parents
    tree = nx.DiGraph()
    for row in cursor.fetchall():
        nodeID = row[0]
        if row[1]:
            # It is ok to add edges that already exist: DiGraph doesn't keep duplicates
            tree.add_edge(row[1], nodeID)
        else:
            tree.add_node(nodeID)
        tree.node[nodeID]['loc'] = (row[2], row[3], row[4])
        if row[3]:
            props = tree.node[nodeID]
            tags = props.get('tags')
            if tags:
                tags.append(row[3])
            else:
                props['tags'] = [row[5]]

    if tnid not in tree:
        raise Exception("Could not find %s in skeleton %s" % (tnid, int(skeleton_id)))
<commit_msg>Fix off index in retrieval of open leaf nodes<commit_after>from django.http import HttpResponse
from django.shortcuts import get_object_or_404

from catmaid.models import *
from catmaid.objects import *
from catmaid.control.authentication import *
from catmaid.control.common import *
from catmaid.control.neuron import _delete_if_empty
from catmaid.control.neuron_annotations import create_annotation_query
from catmaid.control.neuron_annotations import _annotate_entities
from catmaid.control.neuron_annotations import _update_neuron_annotations
from catmaid.control.review import get_treenodes_to_reviews, get_review_status
from catmaid.control.treenode import _create_interpolated_treenode
from collections import defaultdict

import decimal
import json

from operator import itemgetter
import networkx as nx
from tree_util import reroot, edge_count_to_root


def get_skeleton_permissions(request, project_id, skeleton_id):
    """ Tests editing permissions of a user on a skeleton and returns the
    result as JSON object."""
    try:
        nn = _get_neuronname_from_skeletonid( project_id, skeleton_id )
        can_edit = can_edit_class_instance_or_fail(request.user,
                nn['neuronid'])
    except:
        can_edit = False

    permissions = {
      'can_edit': can_edit,
    }

    return HttpResponse(json.dumps(permissions))

@requires_user_role([UserRole.Annotate, UserRole.Browse])
def last_openleaf(request, project_id=None, skeleton_id=None):
    """ Return the ID of the nearest node (or itself), and its location string;
    or two nulls if none found. """
    tnid = int(request.POST['tnid'])
    cursor = connection.cursor()

    cursor.execute("SELECT id FROM relation WHERE project_id=%s AND relation_name='labeled_as'" % int(project_id))
    labeled_as = cursor.fetchone()[0]

    # Select all nodes and their tags
    cursor.execute('''
    SELECT t.id, t.parent_id, t.location_x, t.location_y, t.location_z, ci.name
    FROM treenode t LEFT OUTER JOIN (treenode_class_instance tci INNER JOIN class_instance ci ON tci.class_instance_id = ci.id AND tci.relation_id = %s) ON t.id = tci.treenode_id
    WHERE t.skeleton_id = %s
    ''' % (labeled_as, int(skeleton_id)))

    # Some entries repeated, when a node has more than one tag
    # Create a graph with edges from parent to child, and accumulate parents
    tree = nx.DiGraph()
    for row in cursor.fetchall():
        nodeID = row[0]
        if row[1]:
            # It is ok to add edges that already exist: DiGraph doesn't keep duplicates
            tree.add_edge(row[1], nodeID)
        else:
            tree.add_node(nodeID)
        tree.node[nodeID]['loc'] = (row[2], row[3], row[4])
        if row[5]:
            props = tree.node[nodeID]
            tags = props.get('tags')
            if tags:
                tags.append(row[5])
            else:
                props['tags'] = [row[5]]

    if tnid not in tree:
        raise Exception("Could not find %s in skeleton %s" % (tnid, int(skeleton_id)))

{'fraction_non_alphanumeric': 0.08620113598396258, 'fraction_numerical': 0.004343468092215168, 'mean_word_length': 3.4579300074460164, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '926681', 'n_tokens_mistral': 1880, 'n_tokens_neox': 1800, 'n_words': 534}