<commit_before>#!/usr/bin/env python3
import requests
import simplejson
from collections import namedtuple
from collections import defaultdict as base_dd
from IPython import embed
import numpy as np

BLANK = '   '
LEAF = '──'
BRANCH = '│  '
MID_STEM = '├' + LEAF
BOT_STEM = '└' + LEAF

CYCLE = 'CYCLE DETECTED DERPS'

def tcsort(item):  # FIXME SUCH WOW SO INEFFICIENT O_O
    """ get len of transitive closure assume type items is tree... """
    #if type(item[1]) == type(base_dd):
    return len(item[1]) + sum(tcsort(v) for v in item[1].items())
    #else:
        #jreturn 1

def in_tree(node, tree):  # XXX TODO
    if not tree:
        return False
    elif node in tree:
        return True
    else:
        for subtree in tree.values():
            if in_tree(node, subtree):
                return True
        return False

def get_node(start, tree):
    """ for each parent find a single branch to root """
    def get_first_branch(node):
        if node not in pnames:  # one way to hit a root
            return []
        if pnames[node]:  # mmmm names
            fp = pnames[node][0]
            if cycle_check(node, fp, pnames):
                fp = pnames[node][1]  # if there are double cycles I WILL KILL FOR THE PLEASURE IF IT
            print(fp)
            return [fp] + get_first_branch(fp)
        else:
            return []

    branch = get_first_branch(start)

    for n in branch[::-1]:
        tree = tree[n]

    assert start in tree, "our start wasnt in the tree! OH NO!"
    branch = [start] + branch
    print('branch', branch)
    return tree, branch

def cycle_check(puta_end, start, graph):  # XXX use the flat_tree trick!
    visited = []
    def inner(next_):
        if next_ not in graph:  # since these aren't actually graphs and some nodes dont have parents
            return False
        elif not graph[next_]:
            return False
        elif puta_end in graph[next_]:
            return True
        elif next_ in visited:
            print('CYCLE: ', visited + [next_])
            return False  # A DIFFERENT CYCLE WAS DETECTED will handle later
        else:
            visited.append(next_)
            test = any([inner(c) for c in graph[next_]])
            visited.pop()
            return test

    return inner(start)

def dematerialize(parent_name, parent_node):  # FIXME we need to demat more than just leaves!
    """ Remove nodes higher in the tree that occur further down the
        SAME branch. If they occur down OTHER branchs leave them alone.

        NOTE: modifies in place!
    """
    lleaves = {}
    children = parent_node[parent_name]

    if not children:  # children could be empty ? i think this only happens @ root?
        #print('at bottom', parent_name)
        lleaves[parent_name] = None
        return lleaves

    children_ord = reversed(sorted([(k, v) for k, v in children.items()], key=tcsort))  # make sure we hit deepest first

    for child_name, _ in children_ord:  # get list so we can go ahead and pop
        new_lleaves = dematerialize(child_name, children)

        if child_name in new_lleaves:  # if it is a leaf!
            if child_name in lleaves:  # if it has previously been identified as a leaf!
                print('MATERIALIZATION DETECTED! LOWER PARENT:',
                      lleaves[child_name],'ZAPPING!:', child_name,
                      'OF PARENT:', parent_name)
                children.pop(child_name)
                #print('cn', child_name, 'pn', parent_name, 'BOTTOM')
            #else:  # if it has NOT previously been identified as a leaf, add the parent!
                #new_lleaves[child_name] = parent_name  # pass it back up to nodes above
                #print('cn', child_name, 'pn', parent_name)
        #else:  # it is a node but we want to dematerizlize them too!
        lleaves[child_name] = parent_name

        lleaves.update(new_lleaves)

    return lleaves

class defaultdict(base_dd):
    __str__ = dict.__str__
    __repr__ = dict.__repr__

class TreeNode(defaultdict):  # FIXME need to factory this to allow separate trees!

    pad = '  '
    prefix = []
    existing = {}  # FIXME CAREFUL WITH THIS
    current_parent = None

    def print_tree(self, level = 0):
        output = ''

        if level == 0:
            self.__class__.existing = {}  # clean up any old mess
            if len(self) == 1:
                item = [k for k in self.keys()][0]
                output += str(item)
                self.__class__.current_parent = item
                output += ''.join([v.print_tree(1) for v in self.values()])
                #self.__class__.prefix.pop()  # FIXME causes errors???
                self.__class__.existing = {}  # clean up new mess
                self.__class__.current_parent = None
                return output
            elif len(self) > 1:  # FIXME need a way to pop the last prefix!
                level = 1
                output += '\n.'

        if not self:
            return output
        elif len(self) > 1:  # not sure why we need this... :/
            output += '\n'

        self.__class__.prefix.append(MID_STEM)

        items = []

        def switch(symboltype, key, value):
            #if key in self.existing and type(value) == type(self):
            if False:  # we deal with this by dematerializing the tree
                #print('WEEEE MULTIPARENT!', items[-1], self.current_parent)
                if len(value):
                    parent = self.existing[key]
                    __t = tree()
                    #__t['MULTIPARENT, SEE: %s' % parent]
                    __t['* %s *' % tcsort((None, value))]

                    cend = self.__class__.prefix[-1]
                    if cend == MID_STEM:
                        self.__class__.prefix[-1] = symboltype
                    self.__class__.current_parent = key
                    value = __t.print_tree(level)
                    items.append((str(key), value))
                    self.__class__.prefix[-1] = cend
                else:
                    items.append((str(key), ' *'))  # wait... how the hell...
                return

            if key in pnames:  # XXX FIXME XXX
                if len(pnames[key]) > 1:  # XXX FIXME XXX parents not avail in m cases!
                    #print('MORE THAN ONE PARENT')
                    self.existing[key] = self.current_parent
                    key += ' *'  # mark that it will appear elsewhere

            if type(value) == type(self):
                cend = self.__class__.prefix[-1]
                if cend == MID_STEM:
                    self.__class__.prefix[-1] = symboltype

                self.__class__.current_parent = key
                v = value.print_tree(level + 1)  # recurse here XXX
                self.__class__.prefix[-1] = cend
            else:
                v = str(value)

            items.append((str(key), v))

        # FIXME ideally want to sort by length of the transitive closure :/
        #items_list = [a for a in reversed(sorted([i for i in self.items()], key=tcsort))]
        items_list = sorted([i for i in self.items()], key=tcsort)  # XXX best

        #items_list = [a for a in reversed(sorted([i for i in self.items()], key=lambda a: len(a[1])))]
        #items_list = sorted([i for i in self.items()], key=lambda a: len(a[1]))

        #items_list = sorted([i for i in self.items()])
        for key, value in items_list[:-1]:
            switch(BRANCH, key, value)

        switch(BLANK, *items_list[-1])  # need to put blanks after BOT_STEM

        output += '\n'.join(['{}{}{}'.format(''.join(self.prefix), k, v) for k, v in items[:-1]])
        self.__class__.prefix[-1] = BOT_STEM
        output += '\n' + '{}{}{}'.format(''.join(self.prefix), *items[-1])

        if len(self.__class__.prefix) > 1:
            self.__class__.prefix.pop()

        return output

    def __str__(self):
        output = self.print_tree()
        # FIXME gotta do cleanup here for now :/
        self.__class__.prefix = []
        self.__class__.existing = {}  # clean up new mess
        self.__class__.current_parent = None
        return output

    def __repr__(self, level = 0):
        pad = level * self.pad

        output = '{'
        if self.values():
            output +=  '\n'

        items = []
        for key, value in self.items():
            if type(value) == type(self):
                v = value.__repr__(level = level + 1)
            else:
                v = repr(value)

            items.append((repr(key), v))

        output += ',\n'.join(['{}{}: {}'.format(pad, k, v) for k, v in items])

        output += '}'

        return output

def tree():
    return TreeNode(tree)

def creatTree(root, relationshipType, direction, depth, url_base='matrix.neuinfo.org:9000'):
    query_string = 'http://{url_base}/scigraph/graph/neighbors/{root}?relationshipType={relationshipType}&direction={direction}&depth={depth}'

    relationshipType = relationshipType.replace('#','%23')
    query = query_string.format(root=root, relationshipType=relationshipType,
                                direction=direction, depth=depth, url_base=url_base)

    j = requests.get(query).json()
    print(len(j['nodes']))

    nodes = {n['id']:n['lbl'] for n in j['nodes']}
    nodes[CYCLE] = CYCLE  # make sure we can look up the cycle
    edgerep = ['{} {} {}'.format(nodes[e['sub']], e['pred'], nodes[e['obj']]) for e in j['edges']]

    objects = defaultdict(list)  # note: not all nodes are objects!
    for edge in j['edges']:
        objects[edge['obj']].append(edge['sub'])

    parents = defaultdict(list)
    for edge in j['edges']:
        parents[edge['sub']].append(edge['obj'])

    if direction == 'OUTGOING':  # flip for the tree
        objects, parents = parents, objects

    names = {nodes[k]:[nodes[s] for s in v] for k,v in objects.items()}
    pnames = {nodes[k]:[nodes[s] for s in v] for k,v in parents.items()}

    def build_tree(obj, existing = {}, flat_tree = set()):
        subjects = objects[obj]
        t = tree()
        t[obj]

        for sub in subjects:
            if sub in existing:  # the first time down gets all children
                t[obj][sub] = existing[sub]
            elif sub in flat_tree:  # prevent cycles  KEK that is faster than doing in_tree :D
                print(CYCLE, sub, 'parent is', obj)
                t[obj][CYCLE][sub]
            else:
                flat_tree.add(sub)
                subtree, _ = build_tree(sub, existing, flat_tree)
                t[obj].update(subtree)

            if len(parents[sub]) > 1:
                existing[sub] = t[obj][sub]

        return t, existing
        # for each list of subjects
        # look up the subjects of that subject as if it were and object
        # and and look up those subjects subjects until there are no subjects
        # but we are not guranteed to have started at the right place
        # and so we may need to reorder ?

    def rename(tree):
        dict_ = TreeNode()
        for k in tree:
            dict_[nodes[k]] = rename(tree[k])
        return dict_

    hierarchy, dupes = build_tree(root)
    named_hierarchy = rename(hierarchy)

    return named_hierarchy, (hierarchy, dupes, nodes, edgerep, objects, parents, names, pnames)

def levels(tree, p, l = 0):
    if p == 0:
        return [k for k in tree.keys()]
    elif p == l:
        return tree.keys()
    else:
        lvls = []
        for ls in [levels(t, p, l + 1) for t in tree.values()]:
            lvls.extend(ls)

        return lvls

def count(tree): return sum([count(tree[k]) if tree[k] else 1 for k in tree])

def main():
    Query = namedtuple('Query', ['root','relationshipType','direction','depth'])

    wut = Query("GO:0044464", 'subClassOf', 'INCOMING', 9)
    nifga = Query('NIFGA:birnlex_796', 'http://www.obofoundry.org/ro/ro.owl#has_proper_part', 'OUTGOING', 9)
    uberon = Query('UBERON:0000955', 'http://purl.obolibrary.org/obo/BFO_0000050', 'INCOMING', 9)
    uberon_cc = Query('UBERON:0002749', 'http://purl.obolibrary.org/obo/BFO_0000050', 'INCOMING', 9)

    queries = (wut, nifga, uberon, uberon_cc)

    url = 'localhost:9000'
    fma_r = Query('FMA:Brain', 'http://sig.biostr.washington.edu/fma3.0#regional_part_of', 'INCOMING', 9)
    fma_c = Query('FMA:Brain', 'http://sig.biostr.washington.edu/fma3.0#constitutional_part_of', 'INCOMING', 9)

    ngao, extra = creatTree(*nifga)

    level_sizes = [len(levels(ngao, i)) for i in range(11)]

    parent_counts = np.unique([len(v) for v in extra[-3].values()])

    print(ngao)
    dematerialize('Brain',ngao)
    print(ngao)
    print(extra[0])

    embed()

if __name__ == '__main__':
    main()
<commit_msg>updates to hierarchies so that each tree has its own TreeNode class<commit_after>#!/usr/bin/env python3
import requests
import simplejson
from collections import namedtuple
from collections import defaultdict as base_dd
from IPython import embed
import numpy as np

BLANK = '   '
LEAF = '──'
BRANCH = '│  '
MID_STEM = '├' + LEAF
BOT_STEM = '└' + LEAF

CYCLE = 'CYCLE DETECTED DERPS'

def tcsort(item):  # FIXME SUCH WOW SO INEFFICIENT O_O
    """ get len of transitive closure assume type items is tree... """
    #if type(item[1]) == type(base_dd):
    return len(item[1]) + sum(tcsort(v) for v in item[1].items())
    #else:
        #jreturn 1

def in_tree(node, tree):  # XXX TODO
    if not tree:
        return False
    elif node in tree:
        return True
    else:
        for subtree in tree.values():
            if in_tree(node, subtree):
                return True
        return False

def get_node(start, tree):
    """ for each parent find a single branch to root """
    def get_first_branch(node):
        if node not in pnames:  # one way to hit a root
            return []
        if pnames[node]:  # mmmm names
            fp = pnames[node][0]
            if cycle_check(node, fp, pnames):
                fp = pnames[node][1]  # if there are double cycles I WILL KILL FOR THE PLEASURE IF IT
            print(fp)
            return [fp] + get_first_branch(fp)
        else:
            return []

    branch = get_first_branch(start)

    for n in branch[::-1]:
        tree = tree[n]

    assert start in tree, "our start wasnt in the tree! OH NO!"
    branch = [start] + branch
    print('branch', branch)
    return tree, branch

def cycle_check(puta_end, start, graph):  # XXX use the flat_tree trick!
    visited = []
    def inner(next_):
        if next_ not in graph:  # since these aren't actually graphs and some nodes dont have parents
            return False
        elif not graph[next_]:
            return False
        elif puta_end in graph[next_]:
            return True
        elif next_ in visited:
            print('CYCLE: ', visited + [next_])
            return False  # A DIFFERENT CYCLE WAS DETECTED will handle later
        else:
            visited.append(next_)
            test = any([inner(c) for c in graph[next_]])
            visited.pop()
            return test

    return inner(start)

def dematerialize(parent_name, parent_node):  # FIXME we need to demat more than just leaves!
    """ Remove nodes higher in the tree that occur further down the
        SAME branch. If they occur down OTHER branchs leave them alone.

        NOTE: modifies in place!
    """
    lleaves = {}
    children = parent_node[parent_name]

    if not children:  # children could be empty ? i think this only happens @ root?
        #print('at bottom', parent_name)
        lleaves[parent_name] = None
        return lleaves

    children_ord = reversed(sorted([(k, v) for k, v in children.items()], key=tcsort))  # make sure we hit deepest first

    for child_name, _ in children_ord:  # get list so we can go ahead and pop
        new_lleaves = dematerialize(child_name, children)

        if child_name in new_lleaves:  # if it is a leaf!
            if child_name in lleaves:  # if it has previously been identified as a leaf!
                print('MATERIALIZATION DETECTED! LOWER PARENT:',
                      lleaves[child_name],'ZAPPING!:', child_name,
                      'OF PARENT:', parent_name)
                children.pop(child_name)
                #print('cn', child_name, 'pn', parent_name, 'BOTTOM')
            #else:  # if it has NOT previously been identified as a leaf, add the parent!
                #new_lleaves[child_name] = parent_name  # pass it back up to nodes above
                #print('cn', child_name, 'pn', parent_name)
        #else:  # it is a node but we want to dematerizlize them too!
        lleaves[child_name] = parent_name

        lleaves.update(new_lleaves)

    return lleaves

class defaultdict(base_dd):
    __str__ = dict.__str__
    __repr__ = dict.__repr__

class TreeNode(defaultdict):  # FIXME need to factory this to allow separate trees!

    pad = '  '
    #prefix = []
    #existing = {}  # FIXME CAREFUL WITH THIS
    #current_parent = None

    def print_tree(self, level = 0):
        output = ''

        if level == 0:
            self.__class__.existing = {}  # clean up any old mess
            if len(self) == 1:
                item = [k for k in self.keys()][0]
                output += str(item)
                self.__class__.current_parent = item
                output += ''.join([v.print_tree(1) for v in self.values()])
                #self.__class__.prefix.pop()  # FIXME causes errors???
                self.__class__.existing = {}  # clean up new mess
                self.__class__.current_parent = None
                return output
            elif len(self) > 1:  # FIXME need a way to pop the last prefix!
                level = 1
                output += '\n.'

        if not self:
            return output
        elif len(self) > 1:  # not sure why we need this... :/
            output += '\n'

        self.__class__.prefix.append(MID_STEM)

        items = []

        def switch(symboltype, key, value):
            #if key in self.existing and type(value) == type(self):
            if False:  # we deal with this by dematerializing the tree
                #print('WEEEE MULTIPARENT!', items[-1], self.current_parent)
                if len(value):
                    parent = self.existing[key]
                    __t = tree()
                    #__t['MULTIPARENT, SEE: %s' % parent]
                    __t['* %s *' % tcsort((None, value))]

                    cend = self.__class__.prefix[-1]
                    if cend == MID_STEM:
                        self.__class__.prefix[-1] = symboltype
                    self.__class__.current_parent = key
                    value = __t.print_tree(level)
                    items.append((str(key), value))
                    self.__class__.prefix[-1] = cend
                else:
                    items.append((str(key), ' *'))  # wait... how the hell...
                return

            if key in self.parent_dict:  # XXX FIXME XXX
                if len(self.parent_dict[key]) > 1:  # XXX FIXME XXX parents not avail in m cases!
                    #print('MORE THAN ONE PARENT')
                    self.existing[key] = self.current_parent
                    key += ' *'  # mark that it will appear elsewhere

            if type(value) == type(self):
                cend = self.__class__.prefix[-1]
                if cend == MID_STEM:
                    self.__class__.prefix[-1] = symboltype

                self.__class__.current_parent = key
                v = value.print_tree(level + 1)  # recurse here XXX
                self.__class__.prefix[-1] = cend
            else:
                v = str(value)

            items.append((str(key), v))

        # FIXME ideally want to sort by length of the transitive closure :/
        #items_list = [a for a in reversed(sorted([i for i in self.items()], key=tcsort))]
        items_list = sorted([i for i in self.items()], key=tcsort)  # XXX best

        #items_list = [a for a in reversed(sorted([i for i in self.items()], key=lambda a: len(a[1])))]
        #items_list = sorted([i for i in self.items()], key=lambda a: len(a[1]))

        #items_list = sorted([i for i in self.items()])
        for key, value in items_list[:-1]:
            switch(BRANCH, key, value)

        switch(BLANK, *items_list[-1])  # need to put blanks after BOT_STEM

        output += '\n'.join(['{}{}{}'.format(''.join(self.prefix), k, v) for k, v in items[:-1]])
        self.__class__.prefix[-1] = BOT_STEM
        output += '\n' + '{}{}{}'.format(''.join(self.prefix), *items[-1])

        if len(self.__class__.prefix) > 1:
            self.__class__.prefix.pop()

        return output

    def __str__(self):
        output = self.print_tree()
        # FIXME gotta do cleanup here for now :/
        self.__class__.prefix = []
        self.__class__.existing = {}  # clean up new mess
        self.__class__.current_parent = None
        return output

    def __repr__(self, level = 0):
        pad = level * self.pad

        output = '{'
        if self.values():
            output +=  '\n'

        items = []
        for key, value in self.items():
            if type(value) == type(self):
                v = value.__repr__(level = level + 1)
            else:
                v = repr(value)

            items.append((repr(key), v))

        output += ',\n'.join(['{}{}: {}'.format(pad, k, v) for k, v in items])

        output += '}'

        return output

def tree():
    return TreeNode(tree)

def newTree(name, **kwargs):
    base_dict = {'prefix':[], 'existing':{}, 'current_parent':None}
    base_dict.update(kwargs)
    newTreeNode = type('TreeNode_' + str(hash(name)).replace('-','_'), (TreeNode,), base_dict)
    def Tree(): return newTreeNode(Tree)

    return Tree, newTreeNode

def creatTree(root, relationshipType, direction, depth, url_base='matrix.neuinfo.org:9000'):
    query_string = 'http://{url_base}/scigraph/graph/neighbors/{root}?relationshipType={relationshipType}&direction={direction}&depth={depth}'

    relationshipType = relationshipType.replace('#','%23')
    query = query_string.format(root=root, relationshipType=relationshipType,
                                direction=direction, depth=depth, url_base=url_base)

    j = requests.get(query).json()
    print(len(j['nodes']))

    nodes = {n['id']:n['lbl'] for n in j['nodes']}
    nodes[CYCLE] = CYCLE  # make sure we can look up the cycle
    edgerep = ['{} {} {}'.format(nodes[e['sub']], e['pred'], nodes[e['obj']]) for e in j['edges']]

    objects = defaultdict(list)  # note: not all nodes are objects!
    for edge in j['edges']:
        objects[edge['obj']].append(edge['sub'])

    parents = defaultdict(list)
    for edge in j['edges']:
        parents[edge['sub']].append(edge['obj'])

    if direction == 'OUTGOING':  # flip for the tree
        objects, parents = parents, objects

    names = {nodes[k]:[nodes[s] for s in v] for k,v in objects.items()}
    pnames = {nodes[k]:[nodes[s] for s in v] for k,v in parents.items()}

    Tree, _ = newTree(query, parent_dict=parents)

    def build_tree(obj, existing = {}, flat_tree = set()):
        subjects = objects[obj]
        t = Tree()
        t[obj]

        for sub in subjects:
            if sub in existing:  # the first time down gets all children
                t[obj][sub] = existing[sub]
            elif sub in flat_tree:  # prevent cycles  KEK that is faster than doing in_tree :D
                print(CYCLE, sub, 'parent is', obj)
                t[obj][CYCLE][sub]
            else:
                flat_tree.add(sub)
                subtree, _ = build_tree(sub, existing, flat_tree)
                t[obj].update(subtree)

            if len(parents[sub]) > 1:
                existing[sub] = t[obj][sub]

        return t, existing
        # for each list of subjects
        # look up the subjects of that subject as if it were and object
        # and and look up those subjects subjects until there are no subjects
        # but we are not guranteed to have started at the right place
        # and so we may need to reorder ?

    _, nTreeNode = newTree('names' + query, parent_dict=pnames)  # FIXME pnames is wrong...
    def rename(tree):
        dict_ = nTreeNode()
        for k in tree:
            dict_[nodes[k]] = rename(tree[k])
        return dict_

    hierarchy, dupes = build_tree(root)
    named_hierarchy = rename(hierarchy)

    return named_hierarchy, (hierarchy, dupes, nodes, edgerep, objects, parents, names, pnames)

def levels(tree, p, l = 0):
    if p == 0:
        return [k for k in tree.keys()]
    elif p == l:
        return tree.keys()
    else:
        lvls = []
        for ls in [levels(t, p, l + 1) for t in tree.values()]:
            lvls.extend(ls)

        return lvls

def count(tree): return sum([count(tree[k]) if tree[k] else 1 for k in tree])

def main():
    Query = namedtuple('Query', ['root','relationshipType','direction','depth'])

    cell = Query("GO:0044464", 'subClassOf', 'INCOMING', 9)
    nifga = Query('NIFGA:birnlex_796', 'http://www.obofoundry.org/ro/ro.owl#has_proper_part', 'OUTGOING', 9)
    uberon = Query('UBERON:0000955', 'http://purl.obolibrary.org/obo/BFO_0000050', 'INCOMING', 9)
    uberon_cc = Query('UBERON:0002749', 'http://purl.obolibrary.org/obo/BFO_0000050', 'INCOMING', 9)

    queries = cell, #, nifga, uberon, uberon_cc

    url = 'localhost:9000'
    fma_r = Query('FMA:Brain', 'http://sig.biostr.washington.edu/fma3.0#regional_part_of', 'INCOMING', 9)
    fma_c = Query('FMA:Brain', 'http://sig.biostr.washington.edu/fma3.0#constitutional_part_of', 'INCOMING', 9)

    for query in queries:
        tree, extra = creatTree(*query)
        dematerialize(list(tree.keys())[0], tree)
        print(tree)
        #print(extra[0])

        level_sizes = [len(levels(tree, i)) for i in range(11)]
        print('level sizes', level_sizes)
        parent_counts = np.unique([len(v) for v in extra[-3].values()])
        print('unique parent counts', parent_counts)
        print('num terms', len(extra[2]))

    embed()

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from django.db import models
from django.db.models import permalink, Q
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.core.files import File
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.core.urlresolvers import reverse

from newtagging.models import TagBase
from newtagging import managers

from librarian import html, dcparser

TAG_CATEGORIES = (
    ('author', _('author')),
    ('epoch', _('epoch')),
    ('kind', _('kind')),
    ('genre', _('genre')),
    ('theme', _('theme')),
    ('set', _('set')),
)

class TagSubcategoryManager(models.Manager):
    def __init__(self, subcategory):
        super(TagSubcategoryManager, self).__init__()
        self.subcategory = subcategory

    def get_query_set(self):
        return super(TagSubcategoryManager, self).get_query_set().filter(category=self.subcategory)

class Tag(TagBase):
    name = models.CharField(_('name'), max_length=50, db_index=True)
    slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
    sort_key = models.SlugField(_('sort key'), max_length=120, db_index=True)
    category = models.CharField(_('category'), max_length=50, blank=False, null=False,
        db_index=True, choices=TAG_CATEGORIES)
    description = models.TextField(_('description'), blank=True)
    main_page = models.BooleanField(_('main page'), default=False, db_index=True, help_text=_('Show tag on main page'))

    user = models.ForeignKey(User, blank=True, null=True)
    book_count = models.IntegerField(_('book count'), default=0, blank=False, null=False)

    def has_description(self):
        return len(self.description) > 0
    has_description.short_description = _('description')
    has_description.boolean = True

    @permalink
    def get_absolute_url(self):
        return ('catalogue.views.tagged_object_list', [self.slug])

    class Meta:
        ordering = ('sort_key',)
        verbose_name = _('tag')
        verbose_name_plural = _('tags')

    def __unicode__(self):
        return self.name

    @staticmethod
    def get_tag_list(tags):
        if isinstance(tags, basestring):
            tag_slugs = tags.split('/')
            return [Tag.objects.get(slug=slug) for slug in tag_slugs]
        else:
            return TagBase.get_tag_list(tags)

def book_upload_path(ext):
    def get_dynamic_path(book, filename):
        return 'lektura/%s.%s' % (book.slug, ext)
    return get_dynamic_path

class Book(models.Model):
    title = models.CharField(_('title'), max_length=120)
    slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
    description = models.TextField(_('description'), blank=True)
    created_at = models.DateTimeField(_('creation date'), auto_now=True)
    _short_html = models.TextField(_('short HTML'), editable=False)
    parent_number = models.IntegerField(_('parent number'), default=0)

    # Formats
    xml_file = models.FileField(_('XML file'), upload_to=book_upload_path('xml'), blank=True)
    html_file = models.FileField(_('HTML file'), upload_to=book_upload_path('html'), blank=True)
    pdf_file = models.FileField(_('PDF file'), upload_to=book_upload_path('pdf'), blank=True)
    odt_file = models.FileField(_('ODT file'), upload_to=book_upload_path('odt'), blank=True)
    txt_file = models.FileField(_('TXT file'), upload_to=book_upload_path('txt'), blank=True)

    parent = models.ForeignKey('self', blank=True, null=True, related_name='children')

    objects = models.Manager()
    tagged = managers.ModelTaggedItemManager(Tag)
    tags = managers.TagDescriptor(Tag)

    @property
    def name(self):
        return self.title

    def short_html(self):
        if len(self._short_html):
            return mark_safe(self._short_html)
        else:
            tags = self.tags.filter(~Q(category__in=('set', 'theme')))
            tags = [u'%s' % (tag.get_absolute_url(), tag.name) for tag in tags]

            formats = []
            if self.html_file:
                formats.append(u'Czytaj online' % reverse('book_text', kwargs={'slug': self.slug}))
            if self.pdf_file:
                formats.append(u'Plik PDF' % self.pdf_file.url)
            if self.odt_file:
                formats.append(u'Plik ODT' % self.odt_file.url)
            if self.txt_file:
                formats.append(u'Plik TXT' % self.txt_file.url)

            self._short_html = unicode(render_to_string('catalogue/book_short.html',
                {'book': self, 'tags': tags, 'formats': formats}))
            self.save()
            return mark_safe(self._short_html)

    def has_description(self):
        return len(self.description) > 0
    has_description.short_description = _('description')
    has_description.boolean = True

    def has_pdf_file(self):
        return bool(self.pdf_file)
    has_pdf_file.short_description = 'PDF'
    has_pdf_file.boolean = True

    def has_odt_file(self):
        return bool(self.odt_file)
    has_odt_file.short_description = 'ODT'
    has_odt_file.boolean = True

    def has_html_file(self):
        return bool(self.html_file)
    has_html_file.short_description = 'HTML'
    has_html_file.boolean = True

    class AlreadyExists(Exception):
        pass

    @staticmethod
    def from_xml_file(xml_file, overwrite=False):
        from tempfile import NamedTemporaryFile
        from slughifi import slughifi
        from markupstring import MarkupString

        # Read book metadata
        book_info = dcparser.parse(xml_file)
        book_base, book_slug = book_info.url.rsplit('/', 1)
        book, created = Book.objects.get_or_create(slug=book_slug)

        if created:
            book_shelves = []
        else:
            if not overwrite:
                raise Book.AlreadyExists('Book %s already exists' % book_slug)
            # Save shelves for this book
            book_shelves = book.tags.filter(category='set')

        book.title = book_info.title
        book._short_html = ''
        book.save()

        book_tags = []
        for category in ('kind', 'genre', 'author', 'epoch'):
            tag_name = getattr(book_info, category)
            tag_sort_key = tag_name
            if category == 'author':
                tag_sort_key = tag_name.last_name
                tag_name = ' '.join(tag_name.first_names) + ' ' + tag_name.last_name
            tag, created = Tag.objects.get_or_create(slug=slughifi(tag_name))
            if created:
                tag.name = tag_name
                tag.sort_key = slughifi(tag_sort_key)
                tag.category = category
                tag.save()
            book_tags.append(tag)
        book.tags = book_tags

        if hasattr(book_info, 'parts'):
            for n, part_url in enumerate(book_info.parts):
                base, slug = part_url.rsplit('/', 1)
                child_book = Book.objects.get(slug=slug)
                child_book.parent = book
                child_book.parent_number = n
                child_book.save()

        # Save XML and HTML files
        book.xml_file.save('%s.xml' % book.slug, File(file(xml_file)), save=False)

        html_file = NamedTemporaryFile()
        if html.transform(book.xml_file.path, html_file):
            book.html_file.save('%s.html' % book.slug, File(html_file), save=False)

            # Extract fragments
            closed_fragments, open_fragments = html.extract_fragments(book.html_file.path)
            book_themes = []
            for fragment in closed_fragments.values():
                text = fragment.to_string()
                short_text = ''
                if (len(MarkupString(text)) > 240):
                    short_text = unicode(MarkupString(text)[:160])
                new_fragment, created = Fragment.objects.get_or_create(anchor=fragment.id, book=book,
                    defaults={'text': text, 'short_text': short_text})

                try:
                    theme_names = [s.strip() for s in fragment.themes.split(',')]
                except AttributeError:
                    continue
                themes = []
                for theme_name in theme_names:
                    tag, created = Tag.objects.get_or_create(slug=slughifi(theme_name))
                    if created:
                        tag.name = theme_name
                        tag.sort_key = slughifi(theme_name)
                        tag.category = 'theme'
                        tag.save()
                    themes.append(tag)
                new_fragment.save()
                new_fragment.tags = list(book.tags) + themes
                book_themes += themes

            book_themes = set(book_themes)
            book.tags = list(book.tags) + list(book_themes) + list(book_shelves)

        book.save()
        return book

    @permalink
    def get_absolute_url(self):
        return ('catalogue.views.book_detail', [self.slug])

    class Meta:
        ordering = ('title',)
        verbose_name = _('book')
        verbose_name_plural = _('books')

    def __unicode__(self):
        return self.title

class Fragment(models.Model):
    text = models.TextField()
    short_text = models.TextField(editable=False)
    _short_html = models.TextField(editable=False)
    anchor = models.CharField(max_length=120)
    book = models.ForeignKey(Book, related_name='fragments')

    objects = models.Manager()
    tagged = managers.ModelTaggedItemManager(Tag)
    tags = managers.TagDescriptor(Tag)

    def short_html(self):
        if len(self._short_html):
            return mark_safe(self._short_html)
        else:
            book_authors = [u'%s' % (tag.get_absolute_url(), tag.name)
                for tag in self.book.tags if tag.category == 'author']

            self._short_html = unicode(render_to_string('catalogue/fragment_short.html',
                {'fragment': self, 'book': self.book, 'book_authors': book_authors}))
            self.save()
            return mark_safe(self._short_html)

    def get_absolute_url(self):
        return '%s#m%s' % (reverse('book_text', kwargs={'slug': self.book.slug}), self.anchor)

    class Meta:
        ordering = ('book', 'anchor',)
        verbose_name = _('fragment')
        verbose_name_plural = _('fragments')

<commit_msg>Fixed a bug in saving shelves for each bug (beware of lazy evaluation of querysets!).<commit_after># -*- coding: utf-8 -*-
from django.db import models
from django.db.models import permalink, Q
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.core.files import File
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.core.urlresolvers import reverse

from newtagging.models import TagBase
from newtagging import managers

from librarian import html, dcparser

TAG_CATEGORIES = (
    ('author', _('author')),
    ('epoch', _('epoch')),
    ('kind', _('kind')),
    ('genre', _('genre')),
    ('theme', _('theme')),
    ('set', _('set')),
)

class TagSubcategoryManager(models.Manager):
    def __init__(self, subcategory):
        super(TagSubcategoryManager, self).__init__()
        self.subcategory = subcategory

    def get_query_set(self):
        return super(TagSubcategoryManager, self).get_query_set().filter(category=self.subcategory)

class Tag(TagBase):
    name = models.CharField(_('name'), max_length=50, db_index=True)
    slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
    sort_key = models.SlugField(_('sort key'), max_length=120, db_index=True)
    category = models.CharField(_('category'), max_length=50, blank=False, null=False,
        db_index=True, choices=TAG_CATEGORIES)
    description = models.TextField(_('description'), blank=True)
    main_page = models.BooleanField(_('main page'), default=False, db_index=True, help_text=_('Show tag on main page'))

    user = models.ForeignKey(User, blank=True, null=True)
    book_count = models.IntegerField(_('book count'), default=0, blank=False, null=False)

    def has_description(self):
        return len(self.description) > 0
    has_description.short_description = _('description')
    has_description.boolean = True

    @permalink
    def get_absolute_url(self):
        return ('catalogue.views.tagged_object_list', [self.slug])

    class Meta:
        ordering = ('sort_key',)
        verbose_name = _('tag')
        verbose_name_plural = _('tags')

    def __unicode__(self):
        return self.name

    @staticmethod
    def get_tag_list(tags):
        if isinstance(tags, basestring):
            tag_slugs = tags.split('/')
            return [Tag.objects.get(slug=slug) for slug in tag_slugs]
        else:
            return TagBase.get_tag_list(tags)

def book_upload_path(ext):
    def get_dynamic_path(book, filename):
        return 'lektura/%s.%s' % (book.slug, ext)
    return get_dynamic_path

class Book(models.Model):
    title = models.CharField(_('title'), max_length=120)
    slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
    description = models.TextField(_('description'), blank=True)
    created_at = models.DateTimeField(_('creation date'), auto_now=True)
    _short_html = models.TextField(_('short HTML'), editable=False)
    parent_number = models.IntegerField(_('parent number'), default=0)

    # Formats
    xml_file = models.FileField(_('XML file'), upload_to=book_upload_path('xml'), blank=True)
    html_file = models.FileField(_('HTML file'), upload_to=book_upload_path('html'), blank=True)
    pdf_file = models.FileField(_('PDF file'), upload_to=book_upload_path('pdf'), blank=True)
    odt_file = models.FileField(_('ODT file'), upload_to=book_upload_path('odt'), blank=True)
    txt_file = models.FileField(_('TXT file'), upload_to=book_upload_path('txt'), blank=True)

    parent = models.ForeignKey('self', blank=True, null=True, related_name='children')

    objects = models.Manager()
    tagged = managers.ModelTaggedItemManager(Tag)
    tags = managers.TagDescriptor(Tag)

    @property
    def name(self):
        return self.title

    def short_html(self):
        if len(self._short_html):
            return mark_safe(self._short_html)
        else:
            tags = self.tags.filter(~Q(category__in=('set', 'theme')))
            tags = [u'%s' % (tag.get_absolute_url(), tag.name) for tag in tags]

            formats = []
            if self.html_file:
                formats.append(u'Czytaj online' % reverse('book_text', kwargs={'slug': self.slug}))
            if self.pdf_file:
                formats.append(u'Plik PDF' % self.pdf_file.url)
            if self.odt_file:
                formats.append(u'Plik ODT' % self.odt_file.url)
            if self.txt_file:
                formats.append(u'Plik TXT' % self.txt_file.url)

            self._short_html = unicode(render_to_string('catalogue/book_short.html',
                {'book': self, 'tags': tags, 'formats': formats}))
            self.save()
            return mark_safe(self._short_html)

    def has_description(self):
        return len(self.description) > 0
    has_description.short_description = _('description')
    has_description.boolean = True

    def has_pdf_file(self):
        return bool(self.pdf_file)
    has_pdf_file.short_description = 'PDF'
    has_pdf_file.boolean = True

    def has_odt_file(self):
        return bool(self.odt_file)
    has_odt_file.short_description = 'ODT'
    has_odt_file.boolean = True

    def has_html_file(self):
        return bool(self.html_file)
    has_html_file.short_description = 'HTML'
    has_html_file.boolean = True

    class AlreadyExists(Exception):
        pass

    @staticmethod
    def from_xml_file(xml_file, overwrite=False):
        from tempfile import NamedTemporaryFile
        from slughifi import slughifi
        from markupstring import MarkupString

        # Read book metadata
        book_info = dcparser.parse(xml_file)
        book_base, book_slug = book_info.url.rsplit('/', 1)
        book, created = Book.objects.get_or_create(slug=book_slug)

        if created:
            book_shelves = []
        else:
            if not overwrite:
                raise Book.AlreadyExists('Book %s already exists' % book_slug)
            # Save shelves for this book
            book_shelves = list(book.tags.filter(category='set'))

        book.title = book_info.title
        book._short_html = ''
        book.save()

        book_tags = []
        for category in ('kind', 'genre', 'author', 'epoch'):
            tag_name = getattr(book_info, category)
            tag_sort_key = tag_name
            if category == 'author':
                tag_sort_key = tag_name.last_name
                tag_name = ' '.join(tag_name.first_names) + ' ' + tag_name.last_name
            tag, created = Tag.objects.get_or_create(slug=slughifi(tag_name))
            if created:
                tag.name = tag_name
                tag.sort_key = slughifi(tag_sort_key)
                tag.category = category
                tag.save()
            book_tags.append(tag)
        book.tags = book_tags

        if hasattr(book_info, 'parts'):
            for n, part_url in enumerate(book_info.parts):
                base, slug = part_url.rsplit('/', 1)
                child_book = Book.objects.get(slug=slug)
                child_book.parent = book
                child_book.parent_number = n
                child_book.save()

        # Save XML and HTML files
        book.xml_file.save('%s.xml' % book.slug, File(file(xml_file)), save=False)

        html_file = NamedTemporaryFile()
        if html.transform(book.xml_file.path, html_file):
            book.html_file.save('%s.html' % book.slug, File(html_file), save=False)

            # Extract fragments
            closed_fragments, open_fragments = html.extract_fragments(book.html_file.path)
            book_themes = []
            for fragment in closed_fragments.values():
                text = fragment.to_string()
                short_text = ''
                if (len(MarkupString(text)) > 240):
                    short_text = unicode(MarkupString(text)[:160])
                new_fragment, created = Fragment.objects.get_or_create(anchor=fragment.id, book=book,
                    defaults={'text': text, 'short_text': short_text})

                try:
                    theme_names = [s.strip() for s in fragment.themes.split(',')]
                except AttributeError:
                    continue
                themes = []
                for theme_name in theme_names:
                    tag, created = Tag.objects.get_or_create(slug=slughifi(theme_name))
                    if created:
                        tag.name = theme_name
                        tag.sort_key = slughifi(theme_name)
                        tag.category = 'theme'
                        tag.save()
                    themes.append(tag)
                new_fragment.save()
                new_fragment.tags = list(book.tags) + themes
                book_themes += themes

            book_themes = set(book_themes)
            book.tags = list(book.tags) + list(book_themes) + book_shelves

        book.save()
        return book

    @permalink
    def get_absolute_url(self):
        return ('catalogue.views.book_detail', [self.slug])

    class Meta:
        ordering = ('title',)
        verbose_name = _('book')
        verbose_name_plural = _('books')

    def __unicode__(self):
        return self.title

class Fragment(models.Model):
    text = models.TextField()
    short_text = models.TextField(editable=False)
    _short_html = models.TextField(editable=False)
    anchor = models.CharField(max_length=120)
    book = models.ForeignKey(Book, related_name='fragments')

    objects = models.Manager()
    tagged = managers.ModelTaggedItemManager(Tag)
    tags = managers.TagDescriptor(Tag)

    def short_html(self):
        if len(self._short_html):
            return mark_safe(self._short_html)
        else:
            book_authors = [u'%s' % (tag.get_absolute_url(), tag.name)
                for tag in self.book.tags if tag.category == 'author']

            self._short_html = unicode(render_to_string('catalogue/fragment_short.html',
                {'fragment': self, 'book': self.book, 'book_authors': book_authors}))
            self.save()
            return mark_safe(self._short_html)

    def get_absolute_url(self):
        return '%s#m%s' % (reverse('book_text', kwargs={'slug': self.book.slug}), self.anchor)

    class Meta:
        ordering = ('book', 'anchor',)
        verbose_name = _('fragment')
        verbose_name_plural = _('fragments')

<|endoftext|>


<commit_before>#!/usr/bin/env python

# Copyright (c) 2013, Rethink Robotics
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the Rethink Robotics nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

"""
Tool to tuck/untuck Baxter's arms to/from the shipping pose
"""
import argparse
import math
import threading

import roslib
roslib.load_manifest('tools')
import rospy

from std_msgs.msg import (
    Empty,
    Bool,
)
from baxter_core_msgs.msg import (
    CollisionAvoidanceState,
)

import dataflow
import baxter_interface

class Tuck(object):
    def __init__(self, tuck_cmd):
        self._done = False
        self._limbs = ('left', 'right')
        self._arms = {
            'left': baxter_interface.Limb('left'),
            'right': baxter_interface.Limb('right'),
            }
        self._tuck = tuck_cmd
        self._tuck_rate = rospy.Rate(20.0) # Hz
        self._tuck_threshold = 0.2 # Radians
        self._peak_angle = -1.6 # Radians
        self._arm_state = {
                           'tuck': {'left': 'none', 'right': 'none'},
                           'collide': {'left': False, 'right': False},
                           'flipped': {'left': False, 'right': False}
                          }
        self._joint_moves = {
            'tuck': {
                     'left':  [-1.0, -2.07,  3.0, 2.55,  0.0, 0.01,  0.0],
                     'right': [ 1.0, -2.07, -3.0, 2.55, -0.0, 0.01,  0.0]
                     },
            'untuck': {
                       'left':  [-0.08, -1.0, -1.19, 1.94,  0.67, 1.03, -0.50],
                       'right': [ 0.08, -1.0,  1.19, 1.94, -0.67, 1.03,  0.50]
                       }
            }
        self._collide_lsub = rospy.Subscriber(
                             'robot/limb/left/collision_avoidance_state',
                             CollisionAvoidanceState,
                             self._update_collision, 'left')
        self._collide_rsub = rospy.Subscriber(
                             'robot/limb/right/collision_avoidance_state',
                             CollisionAvoidanceState,
                             self._update_collision, 'right')
        self._disable_pub = {
            'left': rospy.Publisher(
                 'robot/limb/left/suppress_collision_avoidance',
                 Empty),
            'right': rospy.Publisher(
                 'robot/limb/right/suppress_collision_avoidance',
                 Empty)
        }
        self._rs = baxter_interface.RobotEnable()
        self._enable_pub = rospy.Publisher('robot/set_super_enable', Bool)

    def _update_collision(self, data, limb):
        self._arm_state['collide'][limb] = len(data.collision_object) > 0
        self._check_arm_state()

    def _check_arm_state(self):
        """Check for goals and behind collision field.

        If s1 joint is over the peak, collision will need to be disabled
        to get the arm around the head-arm collision force-field.
        """
        diff_check = lambda a,b: abs(a-b) <= self._tuck_threshold
        for limb in self._limbs:
            angles = [self._arms[limb].joint_angle(joint)
                      for joint in self._arms[limb].joint_names()]

            # Check if in a goal position
            untuck_goal = map(diff_check, angles,
                              self._joint_moves['untuck'][limb])
            tuck_goal = map(diff_check, angles[0:2],
                            self._joint_moves['tuck'][limb][0:2])
            if all(untuck_goal):
                self._arm_state['tuck'][limb] = 'untuck'
            elif all(tuck_goal):
                self._arm_state['tuck'][limb] = 'tuck'
            else:
                self._arm_state['tuck'][limb] = 'none'

            # Check if shoulder is flipped over peak
            self._arm_state['flipped'][limb] = (
                self._arms[limb].joint_angle(limb+'_s1') <= self._peak_angle)

    def _prepare_to_tuck(self, disabled=False):
        # If arms are in "tucked" state, disable collision avoidance
        # before enabling robot, to avoid arm jerking from "force-field".
        head = baxter_interface.Head()
        disabled = not self._rs.state().enabled
        at_goal = lambda: (abs(head.pan()) <=
                        baxter_interface.settings.HEAD_PAN_ANGLE_TOLERANCE)

        print("Moving head to neutral position")
        while not at_goal() and not rospy.is_shutdown():
            if disabled:
                [pub.publish(Empty()) for pub in self._disable_pub.values()]
            if not self._rs.state().enabled:
                self._enable_pub.publish(True)
            head.set_pan(0.0, 50.0, timeout=0)
            self._tuck_rate.sleep()

        if not disabled:
            while self._rs.state().enabled == True and not rospy.is_shutdown():
                [pub.publish(Empty()) for pub in self._disable_pub.values()]
                self._enable_pub.publish(False)
                self._tuck_rate.sleep()

    def _move_to(self, tuck, disabled):
        if type(tuck) == str:
            tuck = dict(zip(self._limbs,(tuck,tuck)))
        if type(disabled) == bool:
            disabled = dict(zip(self._limbs,(disabled,disabled)))

        if any(disabled.values()):
            [pub.publish(Empty()) for pub in self._disable_pub.values()]
        while (any(self._arm_state['tuck'][limb] != goal
                   for limb,goal in tuck.viewitems())
               and not rospy.is_shutdown()):
            if self._rs.state().enabled == False:
                self._enable_pub.publish(True)
            for limb in self._limbs:
                if disabled[limb]:
                    self._disable_pub[limb].publish(Empty())
                if limb in tuck:
                    self._arms[limb].set_joint_positions(dict(zip(
                                      self._arms[limb].joint_names(),
                                      self._joint_moves[tuck[limb]][limb])))
            self._check_arm_state()
            self._tuck_rate.sleep()

        if any(self._arm_state['collide'].values()):
            self._rs.disable()
        return

    def supervised_tuck(self):
        # Update our starting state to check if arms are tucked
        self._prepare_to_tuck()
        self._check_arm_state()
        # Tuck Arms
        if self._tuck == True:
            # If arms are already tucked, report this to user and exit.
            if all(self._arm_state['tuck'][limb] == 'tuck'
                   for limb in self._limbs):
                rospy.loginfo("Tucking: Arms already in 'Tucked' position.")
                self._done = True
                return
            else:
                rospy.loginfo("Tucking: One or more arms not Tucked.")
                any_flipped = not any(self._arm_state['flipped'].values())
                if any_flipped:
                    rospy.loginfo(
                        "Moving to neutral start position with collision %s.",
                        "on" if any_flipped else "off")
                # Move to neutral pose before tucking arms to avoid damage
                self._check_arm_state()
                actions = dict()
                disabled = {'left': True, 'right': True}
                for limb in self._limbs:
                    if not self._arm_state['flipped'][limb]:
                        actions[limb] = 'untuck'
                        disabled[limb] = False
                self._move_to(actions, disabled)

                # Disable collision and Tuck Arms
                rospy.loginfo("Tucking: Tucking with collision avoidance off.")
                actions = {'left': 'tuck', 'right': 'tuck'}
                disabled = {'left': True, 'right': True}
                self._move_to(actions, disabled)
                self._done = True
                return

        # Untuck Arms
        else:
            # If arms are tucked disable collision and untuck arms
            if any(self._arm_state['flipped'].values()):
                rospy.loginfo("Untucking: One or more arms Tucked;"
                              " Disabling Collision Avoidance and untucking.")
                self._check_arm_state()
                suppress = self._arm_state['flipped']
                actions = {'left': 'untuck', 'right': 'untuck'}
                self._move_to(actions, suppress)
                self._done = True
                return
            # If arms already untucked, move to neutral location
            else:
                rospy.loginfo("Untucking: Arms already Untucked;"
                              " Moving to neutral position.")
                self._check_arm_state()
                suppress = self._arm_state['flipped']
                actions = {'left': 'untuck', 'right': 'untuck'}
                self._move_to(actions, suppress)
                self._done = True
                return

    def clean_shutdown(self):
        """Handles ROS shutdown (Ctrl-C) safely."""
        if not self._done:
            rospy.logwarn('Aborting: Shutting down safely...')
        if any(self._arm_state['collide'].values()):
            while self._rs.state().enabled != False:
                [pub.publish(Empty()) for pub in self._disable_pub.values()]
                self._enable_pub.publish(False)
                self._tuck_rate.sleep()

def main(tuck):
    print("Initializing node... ")
    rospy.init_node("tuck_arms")
    print("%sucking arms" % ("T" if tuck else "Unt",))
    tucker = Tuck(tuck)
    rospy.on_shutdown(tucker.clean_shutdown)
    tucker.supervised_tuck()
    print("Finished tuck")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    tuck_group = parser.add_mutually_exclusive_group(required=True)
    tuck_group.add_argument("-t", "--tuck", dest="tuck",
        action='store_true', default=False, help="tuck arms")
    tuck_group.add_argument("-u", "--untuck", dest="untuck",
        action='store_true', default=False, help="untuck arms")
    args = parser.parse_args(rospy.myargv()[1:])

    main(args.tuck)
<commit_msg>Tuck arms fix-review: remove type check; convert print<commit_after>#!/usr/bin/env python

# Copyright (c) 2013, Rethink Robotics
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the Rethink Robotics nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

"""
Tool to tuck/untuck Baxter's arms to/from the shipping pose
"""
import argparse
import math
import threading

import roslib
roslib.load_manifest('tools')
import rospy

from std_msgs.msg import (
    Empty,
    Bool,
)
from baxter_core_msgs.msg import (
    CollisionAvoidanceState,
)

import dataflow
import baxter_interface

class Tuck(object):
    def __init__(self, tuck_cmd):
        self._done = False
        self._limbs = ('left', 'right')
        self._arms = {
            'left': baxter_interface.Limb('left'),
            'right': baxter_interface.Limb('right'),
            }
        self._tuck = tuck_cmd
        self._tuck_rate = rospy.Rate(20.0) # Hz
        self._tuck_threshold = 0.2 # Radians
        self._peak_angle = -1.6 # Radians
        self._arm_state = {
                           'tuck': {'left': 'none', 'right': 'none'},
                           'collide': {'left': False, 'right': False},
                           'flipped': {'left': False, 'right': False}
                          }
        self._joint_moves = {
            'tuck': {
                     'left':  [-1.0, -2.07,  3.0, 2.55,  0.0, 0.01,  0.0],
                     'right': [ 1.0, -2.07, -3.0, 2.55, -0.0, 0.01,  0.0]
                     },
            'untuck': {
                       'left':  [-0.08, -1.0, -1.19, 1.94,  0.67, 1.03, -0.50],
                       'right': [ 0.08, -1.0,  1.19, 1.94, -0.67, 1.03,  0.50]
                       }
            }
        self._collide_lsub = rospy.Subscriber(
                             'robot/limb/left/collision_avoidance_state',
                             CollisionAvoidanceState,
                             self._update_collision, 'left')
        self._collide_rsub = rospy.Subscriber(
                             'robot/limb/right/collision_avoidance_state',
                             CollisionAvoidanceState,
                             self._update_collision, 'right')
        self._disable_pub = {
            'left': rospy.Publisher(
                 'robot/limb/left/suppress_collision_avoidance',
                 Empty),
            'right': rospy.Publisher(
                 'robot/limb/right/suppress_collision_avoidance',
                 Empty)
        }
        self._rs = baxter_interface.RobotEnable()
        self._enable_pub = rospy.Publisher('robot/set_super_enable', Bool)

    def _update_collision(self, data, limb):
        self._arm_state['collide'][limb] = len(data.collision_object) > 0
        self._check_arm_state()

    def _check_arm_state(self):
        """Check for goals and behind collision field.

        If s1 joint is over the peak, collision will need to be disabled
        to get the arm around the head-arm collision force-field.
        """
        diff_check = lambda a,b: abs(a-b) <= self._tuck_threshold
        for limb in self._limbs:
            angles = [self._arms[limb].joint_angle(joint)
                      for joint in self._arms[limb].joint_names()]

            # Check if in a goal position
            untuck_goal = map(diff_check, angles,
                              self._joint_moves['untuck'][limb])
            tuck_goal = map(diff_check, angles[0:2],
                            self._joint_moves['tuck'][limb][0:2])
            if all(untuck_goal):
                self._arm_state['tuck'][limb] = 'untuck'
            elif all(tuck_goal):
                self._arm_state['tuck'][limb] = 'tuck'
            else:
                self._arm_state['tuck'][limb] = 'none'

            # Check if shoulder is flipped over peak
            self._arm_state['flipped'][limb] = (
                self._arms[limb].joint_angle(limb+'_s1') <= self._peak_angle)

    def _prepare_to_tuck(self, disabled=False):
        # If arms are in "tucked" state, disable collision avoidance
        # before enabling robot, to avoid arm jerking from "force-field".
        head = baxter_interface.Head()
        disabled = not self._rs.state().enabled
        at_goal = lambda: (abs(head.pan()) <=
                        baxter_interface.settings.HEAD_PAN_ANGLE_TOLERANCE)

        rospy.loginfo("Moving head to neutral position")
        while not at_goal() and not rospy.is_shutdown():
            if disabled:
                [pub.publish(Empty()) for pub in self._disable_pub.values()]
            if not self._rs.state().enabled:
                self._enable_pub.publish(True)
            head.set_pan(0.0, 50.0, timeout=0)
            self._tuck_rate.sleep()

        if not disabled:
            while self._rs.state().enabled == True and not rospy.is_shutdown():
                [pub.publish(Empty()) for pub in self._disable_pub.values()]
                self._enable_pub.publish(False)
                self._tuck_rate.sleep()

    def _move_to(self, tuck, disabled):
        if any(disabled.values()):
            [pub.publish(Empty()) for pub in self._disable_pub.values()]
        while (any(self._arm_state['tuck'][limb] != goal
                   for limb,goal in tuck.viewitems())
               and not rospy.is_shutdown()):
            if self._rs.state().enabled == False:
                self._enable_pub.publish(True)
            for limb in self._limbs:
                if disabled[limb]:
                    self._disable_pub[limb].publish(Empty())
                if limb in tuck:
                    self._arms[limb].set_joint_positions(dict(zip(
                                      self._arms[limb].joint_names(),
                                      self._joint_moves[tuck[limb]][limb])))
            self._check_arm_state()
            self._tuck_rate.sleep()

        if any(self._arm_state['collide'].values()):
            self._rs.disable()
        return

    def supervised_tuck(self):
        # Update our starting state to check if arms are tucked
        self._prepare_to_tuck()
        self._check_arm_state()
        # Tuck Arms
        if self._tuck == True:
            # If arms are already tucked, report this to user and exit.
            if all(self._arm_state['tuck'][limb] == 'tuck'
                   for limb in self._limbs):
                rospy.loginfo("Tucking: Arms already in 'Tucked' position.")
                self._done = True
                return
            else:
                rospy.loginfo("Tucking: One or more arms not Tucked.")
                any_flipped = not any(self._arm_state['flipped'].values())
                if any_flipped:
                    rospy.loginfo(
                        "Moving to neutral start position with collision %s.",
                        "on" if any_flipped else "off")
                # Move to neutral pose before tucking arms to avoid damage
                self._check_arm_state()
                actions = dict()
                disabled = {'left': True, 'right': True}
                for limb in self._limbs:
                    if not self._arm_state['flipped'][limb]:
                        actions[limb] = 'untuck'
                        disabled[limb] = False
                self._move_to(actions, disabled)

                # Disable collision and Tuck Arms
                rospy.loginfo("Tucking: Tucking with collision avoidance off.")
                actions = {'left': 'tuck', 'right': 'tuck'}
                disabled = {'left': True, 'right': True}
                self._move_to(actions, disabled)
                self._done = True
                return

        # Untuck Arms
        else:
            # If arms are tucked disable collision and untuck arms
            if any(self._arm_state['flipped'].values()):
                rospy.loginfo("Untucking: One or more arms Tucked;"
                              " Disabling Collision Avoidance and untucking.")
                self._check_arm_state()
                suppress = self._arm_state['flipped']
                actions = {'left': 'untuck', 'right': 'untuck'}
                self._move_to(actions, suppress)
                self._done = True
                return
            # If arms already untucked, move to neutral location
            else:
                rospy.loginfo("Untucking: Arms already Untucked;"
                              " Moving to neutral position.")
                self._check_arm_state()
                suppress = self._arm_state['flipped']
                actions = {'left': 'untuck', 'right': 'untuck'}
                self._move_to(actions, suppress)
                self._done = True
                return

    def clean_shutdown(self):
        """Handles ROS shutdown (Ctrl-C) safely."""
        if not self._done:
            rospy.logwarn('Aborting: Shutting down safely...')
        if any(self._arm_state['collide'].values()):
            while self._rs.state().enabled != False:
                [pub.publish(Empty()) for pub in self._disable_pub.values()]
                self._enable_pub.publish(False)
                self._tuck_rate.sleep()

def main(tuck):
    rospy.loginfo("Initializing node... ")
    rospy.init_node("tuck_arms")
    rospy.loginfo("%sucking arms" % ("T" if tuck else "Unt",))
    tucker = Tuck(tuck)
    rospy.on_shutdown(tucker.clean_shutdown)
    tucker.supervised_tuck()
    rospy.loginfo("Finished tuck")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    tuck_group = parser.add_mutually_exclusive_group(required=True)
    tuck_group.add_argument("-t", "--tuck", dest="tuck",
        action='store_true', default=False, help="tuck arms")
    tuck_group.add_argument("-u", "--untuck", dest="untuck",
        action='store_true', default=False, help="untuck arms")
    args = parser.parse_args(rospy.myargv()[1:])

    main(args.tuck)
<|endoftext|>


<commit_before># Stored variables:
#
# Last known block
# 10: version
# 11: hashPrevBlock
# 12: hashMerkleRoot
# 13: time
# 14: bits
# 15: nonce
# 16: blockHash / lastKnownBlock
# 17: score
#

shared:
    TWO_POW_24 = 2 ^ 24

init:
    self.storage[16] = 0x00000000000000000cfdd50d917943949fa708829ab70108c98cdb9f7d62339d

code:
    def storeBlockHeader(version, hashPrevBlock, hashMerkleRoot, time, bits, nonce):
        exp = bits / TWO_POW_24
        mant = bits & 0xffffff
        target = mant * slt(1, (8*(exp - 3)))

    # shift left
    def slt(n, x):
        return(n * 2^x)
<commit_msg>update to poc7 syntax<commit_after># Stored variables:
#
# Last known block
# 10: version
# 11: hashPrevBlock
# 12: hashMerkleRoot
# 13: time
# 14: bits
# 15: nonce
# 16: blockHash / lastKnownBlock
# 17: score
#

def shared():
    TWO_POW_24 = 2 ^ 24

def init():
    self.storage[16] = 0x00000000000000000cfdd50d917943949fa708829ab70108c98cdb9f7d62339d

def code():
    return(-1)

def storeBlockHeader(version, hashPrevBlock, hashMerkleRoot, time, bits, nonce):
    exp = bits / TWO_POW_24
    mant = bits & 0xffffff
    target = mant * slt(1, (8*(exp - 3)))

# shift left
def slt(n, x):
    return(n * 2^x)
<|endoftext|>


<commit_before>__version__ = '30.5.4'
<commit_msg>Bump version to 30.5.5<commit_after>__version__ = '30.5.5'
<|endoftext|>


<commit_before>"""
spaceshooter.py
Author: Avery Wallis
Credit: Original Spacewar Code, Daniel

Assignment:
Write and submit a program that implements the spacewar game:
https://github.com/HHS-IntroProgramming/Spacewar
"""
from ggame import App, ImageAsset, RectangleAsset, SoundAsset, Frame
from ggame import LineStyle, Sprite, Sound, Color
import math

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

class SpaceShip(Sprite):
    """
    Animated space ship
    """
    asset = ImageAsset("images/four_spaceship_by_albertov_with_thrust.png",
        Frame(227,0,292-227,125), 4, 'vertical')

    def __init__(self, position):
        super().__init__(SpaceShip.asset, position)
        self.visible=True
        self.vx = 0
        self.vy = 0
        self.vr = 0
        self.rotation = 0
        self.thrust = 0
        self.thrustframe = 1
        SpaceGame.listenKeyEvent("keydown", "w", self.thrustOn)
        SpaceGame.listenKeyEvent("keyup", "w", self.thrustOff)
        SpaceGame.listenKeyEvent("keydown", "s", self.thrustDecel)
        SpaceGame.listenKeyEvent("keyup", "s", self.thrustOff)
        SpaceGame.listenKeyEvent("keydown", "a" , self.CCOn)
        SpaceGame.listenKeyEvent("keyup", "a", self.CCOff)
        SpaceGame.listenKeyEvent("keydown", "d" , self.CCthing)
        SpaceGame.listenKeyEvent("keyup", "d", self.CCOff)
        self.fxcenter = self.fycenter = 0.5

    def step(self):
        self.x += self.vx
        self.y += self.vy
        self.rotation += self.vr
        if self.thrust == 1:
            self.setImage(self.thrustframe)
            self.thrustframe += 1
            if self.rotation in range(0,((math.pi)/6)+1,.0000000001):
                self.vx = 0
                self.vy = -1
            if self.thrustframe == 4:
                self.thrustframe = 1
            else:
                self.setImage(0)
        if self.thrust == -1:
            self.setImage(self.thrustframe)
            self.thrustframe += 1
            self.vx=self.vx+.01
            if self.thrustframe == 4:
                self.thrustframe = 1
            else:
                self.setImage(0)
        if self.vr==.1:
            self.rotation =self.rotation+.001
        if self.vr==-.1:
            self.rotation =self.rotation-.001
        if self.vr==0:
            self.rotation=self.rotation

        col= self.collidingWithSprites(Sunthing)
        if col:
            self.explode

    def thrustOn(self, event):
        self.thrust = 1
    def thrustOff(self, event):
        self.thrust = 0
    def thrustDecel(self, event):
        self.thrust=-1

    def CCOn(self, event):
        self.vr=.1
    def CCOff(self, event):
        self.vr=0
    def CCthing(self, event):
        self.vr=-.1

    def explode(self):
        self.visible =False
        self.ExplosionSmall

class ExplosionSmall(Sprite):

    asset = ImageAsset("images/explosion1.png", Frame(0,0,128,128), 10)
    boomasset = SoundAsset("sounds/explosion1.mp3")

    def __init__(self, position):
        super().__init__(ExplosionSmall.asset, position)
        self.image = 0
        self.center = (0.5, 0.5)
        self.boom = Sound(ExplosionSmall.boomasset)
        self.boom.play()

    def step(self):
        self.setImage(self.image//2)  # slow it down
        self.image += 1
        if self.image == 20:
            self.destroy()

class Sunthing(Sprite):
    asset=ImageAsset("images/sun.png")
    width=80
    length=76
    def __init__(self, position):
        super().__init__(Sunthing.asset, position)
        self.mass = 30*1000
        self.fxcenter = 0.5
        self.fycenter = 0.5
        self.circularCollisionModel()

class SpaceGame(App):
    """
    Tutorial4 space game example.
    """
    def __init__(self, width, height):
        super().__init__(width, height)
        black = Color(0, 1)
        noline = LineStyle(0, black)
        bg_asset = ImageAsset("images/starfield.jpg")
        star0 = Sprite(bg_asset, (0,0))
        star1= Sprite(bg_asset, (512,0))
        star2= Sprite(bg_asset, (0,512))
        star3= Sprite(bg_asset, (512, 512))
        Sunthing((500,360))
        SpaceShip((100,100))

    def step(self):
            for ship in self.getSpritesbyClass(SpaceShip):
                ship.step()

myapp = SpaceGame(SCREEN_WIDTH*1.5, SCREEN_HEIGHT*1.5)
myapp.run()
<commit_msg>Updated from Brython Server: 3/21/2016 2:24:07 PM<commit_after>"""
spaceshooter.py
Author: Avery Wallis
Credit: Original Spacewar Code, Daniel

Assignment:
Write and submit a program that implements the spacewar game:
https://github.com/HHS-IntroProgramming/Spacewar
"""
from ggame import App, ImageAsset, RectangleAsset, SoundAsset, Frame
from ggame import LineStyle, Sprite, Sound, Color
import math

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

class SpaceShip(Sprite):
    """
    Animated space ship
    """
    asset = ImageAsset("images/four_spaceship_by_albertov_with_thrust.png",
        Frame(227,0,292-227,125), 4, 'vertical')

    def __init__(self, position):
        super().__init__(SpaceShip.asset, position)
        self.visible=True
        self.vx = 0
        self.vy = 0
        self.vr = 0
        self.rotation = 0
        self.thrust = 0
        self.thrustframe = 1
        SpaceGame.listenKeyEvent("keydown", "w", self.thrustOn)
        SpaceGame.listenKeyEvent("keyup", "w", self.thrustOff)
        SpaceGame.listenKeyEvent("keydown", "s", self.thrustDecel)
        SpaceGame.listenKeyEvent("keyup", "s", self.thrustOff)
        SpaceGame.listenKeyEvent("keydown", "a" , self.CCOn)
        SpaceGame.listenKeyEvent("keyup", "a", self.CCOff)
        SpaceGame.listenKeyEvent("keydown", "d" , self.CCthing)
        SpaceGame.listenKeyEvent("keyup", "d", self.CCOff)
        self.fxcenter = self.fycenter = 0.5

    def step(self):
        self.x += self.vx
        self.y += self.vy
        self.rotation += self.vr
        if self.thrust == 1:
            self.setImage(self.thrustframe)
            self.thrustframe += 1
            if self.rotation in range(0,((math.pi)/6)+1):
                self.vx = 0
                self.vy = -1
            if self.thrustframe == 4:
                self.thrustframe = 1
            else:
                self.setImage(0)
        if self.thrust == -1:
            self.setImage(self.thrustframe)
            self.thrustframe += 1
            self.vx=self.vx+.01
            if self.thrustframe == 4:
                self.thrustframe = 1
            else:
                self.setImage(0)
        if self.vr==.1:
            self.rotation =self.rotation+.001
        if self.vr==-.1:
            self.rotation =self.rotation-.001
        if self.vr==0:
            self.rotation=self.rotation

        col= self.collidingWithSprites(Sunthing)
        if col:
            self.explode

    def thrustOn(self, event):
        self.thrust = 1
    def thrustOff(self, event):
        self.thrust = 0
    def thrustDecel(self, event):
        self.thrust=-1

    def CCOn(self, event):
        self.vr=.1
    def CCOff(self, event):
        self.vr=0
    def CCthing(self, event):
        self.vr=-.1

    def explode(self):
        self.visible =False
        self.ExplosionSmall

class ExplosionSmall(Sprite):

    asset = ImageAsset("images/explosion1.png", Frame(0,0,128,128), 10)
    boomasset = SoundAsset("sounds/explosion1.mp3")

    def __init__(self, position):
        super().__init__(ExplosionSmall.asset, position)
        self.image = 0
        self.center = (0.5, 0.5)
        self.boom = Sound(ExplosionSmall.boomasset)
        self.boom.play()

    def step(self):
        self.setImage(self.image//2)  # slow it down
        self.image += 1
        if self.image == 20:
            self.destroy()

class Sunthing(Sprite):
    asset=ImageAsset("images/sun.png")
    width=80
    length=76
    def __init__(self, position):
        super().__init__(Sunthing.asset, position)
        self.mass = 30*1000
        self.fxcenter = 0.5
        self.fycenter = 0.5
        self.circularCollisionModel()

class SpaceGame(App):
    """
    Tutorial4 space game example.
    """
    def __init__(self, width, height):
        super().__init__(width, height)
        black = Color(0, 1)
        noline = LineStyle(0, black)
        bg_asset = ImageAsset("images/starfield.jpg")
        star0 = Sprite(bg_asset, (0,0))
        star1= Sprite(bg_asset, (512,0))
        star2= Sprite(bg_asset, (0,512))
        star3= Sprite(bg_asset, (512, 512))
        Sunthing((500,360))
        SpaceShip((100,100))

    def step(self):
            for ship in self.getSpritesbyClass(SpaceShip):
                ship.step()

myapp = SpaceGame(SCREEN_WIDTH*1.5, SCREEN_HEIGHT*1.5)
myapp.run()
<|endoftext|>


<commit_before>from django.contrib import admin

# Register your models here.
<commit_msg>Challenges: Setup ModelAdmin class for each model in Challenge app (#149)<commit_after>from django.contrib import admin

from base.admin import TimeStampedAdmin

from .models import Challenge, Phase

@admin.register(Challenge)
class ChallengeAdmin(TimeStampedAdmin):
    list_display = ("title", "start_date", "end_date", "creator", "published", "enable_forum", "anonymous_leaderboard")
    list_filter = ("creator", "published", "enable_forum", "anonymous_leaderboard")
    search_fields = ("title", "creator")

@admin.register(Phase)
class PhaseAdmin(TimeStampedAdmin):
    list_display = ("name", "leaderboard_public", "challenge")
    list_filter = ("leaderboard_public", "challenge")
    search_fields = ("name",)
<|endoftext|>


<commit_before>import heapq

import numpy as np
import scipy.ndimage as nd
import scipy.spatial.distance as distance

def neighbors(coords, shape, connectivity=1):
    """Return the neighbors of a set of coordinates.

    Parameters
    ----------
    coords : array-like
        The coordinates for which we want neighbor coordinates.
    shape : array-like
        The shape of the array including coords. Used to check for borders.
    connectivity : int (optional), min=1, max=len(shape)
        The connectivity of the neighborhood.

    Returns
    -------
    neighbors : np.ndarray (num_neighbors x len(shape))
        The coordinates of neighboring array elements.
    """
    coords = np.atleast_2d(coords)
    shape = np.asarray(shape)
    ndim = len(shape)
    n_elem = 3 ** ndim
    footprint = nd.generate_binary_structure(ndim, connectivity)
    footprint.ravel()[n_elem / 2] = 0
    neighbors = coords + (np.asarray(footprint.nonzero()).T - np.ones(ndim))
    not_border = True - ((neighbors < 0).any(axis=1) +
                         (neighbors >= shape).any(axis=1))
    return neighbors[not_border].astype(int)

def geodesic_expansion(labels, image, mode='viscosity', connectivity=1):
    """Expand the location of labels into a geodesic space defined by image.

    Parameters
    ----------
    labels : numpy array (integer type)
        The initial location of the labels (typically sparse)
    image : numpy array, dimensions = labels.ndim or labels.ndim + 1
        The space along which labels must expand. It can either be
        single-channel (same dimension as `labels`) or multi-channel (one more
        dimension than `labels`; the last dimension is assumed to be the
        channels).
    mode : string (optional)
        Whether to treat the image values as a `viscosity` (default) or a
        `feature`. In the first case, the cost of expanding from pixel `x` to
        pixel `y` is `image[y]`. In the second, it is `d(image[x], image[y])`.
    connectivity : int (optional)
        The connectivity defining neighboring pixels. It is an int between 1
        and `labels.ndim`, inclusive. (default: 1)

    Returns
    -------
    labels : numpy array (integer type)
        The resulting label field
    """
    label_locations = labels.nonzero()
    initial_labels = labels[label_locations]
    distance_heap = [(0, coord, label) for coord, label in
         zip(np.transpose(label_locations), initial_labels)]
    distance_array = np.inf * np.ones(labels.shape, float)
    distance_array[label_locations] = 0
    if mode == 'viscosity':
        def dist(img, src, dst): return img[dst]
    else:
        def dist(img, src, dst): return distance.euclidean(img[src], img[dst])
    labels_out = np.zeros_like(labels)
    while len(distance_heap) > 0:
        nearest = heapq.heappop(distance_heap)
        d, loc, lab = nearest
        if labels_out[loc] == 0:
            labels_out[loc] = lab
            distance_array[loc] = d
            for n in neighbors(loc, labels_out.shape, connectivity):
                if labels_out[n] == 0:
                    next_d = d + dist(image, loc, n)
                    heapq.heappush(distance_heap, (next_d, n, lab))
    return labels_out
<commit_msg>Remove unneeded distance_array structure<commit_after>import heapq

import numpy as np
import scipy.ndimage as nd
import scipy.spatial.distance as distance

def neighbors(coords, shape, connectivity=1):
    """Return the neighbors of a set of coordinates.

    Parameters
    ----------
    coords : array-like
        The coordinates for which we want neighbor coordinates.
    shape : array-like
        The shape of the array including coords. Used to check for borders.
    connectivity : int (optional), min=1, max=len(shape)
        The connectivity of the neighborhood.

    Returns
    -------
    neighbors : np.ndarray (num_neighbors x len(shape))
        The coordinates of neighboring array elements.
    """
    coords = np.atleast_2d(coords)
    shape = np.asarray(shape)
    ndim = len(shape)
    n_elem = 3 ** ndim
    footprint = nd.generate_binary_structure(ndim, connectivity)
    footprint.ravel()[n_elem / 2] = 0
    neighbors = coords + (np.asarray(footprint.nonzero()).T - np.ones(ndim))
    not_border = True - ((neighbors < 0).any(axis=1) +
                         (neighbors >= shape).any(axis=1))
    return neighbors[not_border].astype(int)

def geodesic_expansion(labels, image, mode='viscosity', connectivity=1):
    """Expand the location of labels into a geodesic space defined by image.

    Parameters
    ----------
    labels : numpy array (integer type)
        The initial location of the labels (typically sparse)
    image : numpy array, dimensions = labels.ndim or labels.ndim + 1
        The space along which labels must expand. It can either be
        single-channel (same dimension as `labels`) or multi-channel (one more
        dimension than `labels`; the last dimension is assumed to be the
        channels).
    mode : string (optional)
        Whether to treat the image values as a `viscosity` (default) or a
        `feature`. In the first case, the cost of expanding from pixel `x` to
        pixel `y` is `image[y]`. In the second, it is `d(image[x], image[y])`.
    connectivity : int (optional)
        The connectivity defining neighboring pixels. It is an int between 1
        and `labels.ndim`, inclusive. (default: 1)

    Returns
    -------
    labels : numpy array (integer type)
        The resulting label field
    """
    label_locations = labels.nonzero()
    initial_labels = labels[label_locations]
    distance_heap = [(0, coord, label) for coord, label in
         zip(np.transpose(label_locations), initial_labels)]
    if mode == 'viscosity':
        def dist(img, src, dst): return img[dst]
    else:
        def dist(img, src, dst): return distance.euclidean(img[src], img[dst])
    labels_out = np.zeros_like(labels)
    while len(distance_heap) > 0:
        nearest = heapq.heappop(distance_heap)
        d, loc, lab = nearest
        if labels_out[loc] == 0:
            labels_out[loc] = lab
            for n in neighbors(loc, labels_out.shape, connectivity):
                if labels_out[n] == 0:
                    next_d = d + dist(image, loc, n)
                    heapq.heappush(distance_heap, (next_d, n, lab))
    return labels_out
<|endoftext|>