<commit_before>#!/usr/bin/env python
import collections
import myreglexer
import sys

index = 0
ahead1 = 1

stack = []

def parser(stream):
  global stack, index, ahead1
  tokens = list(myreglexer.tokenize(stream))
  index = 0
  ahead1 = 1
  T(tokens)
  #print stack

def T(tokens): # T -> [S]
  global index, stack

  if tokens[index][0] == "LBRACE":

    stack.append(tokens[index][1])
    nextToken()

    S(tokens)

    stack.append(tokens[index][1])
    nextToken()
    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

    stack.append(tokens[index][1])
    nextToken()

    return not None
  else:
    error(tokens[index], '[')

def S(tokens): # S -> expr S_ | []S_ | [S]S_
  global index, ahead1, stack

  if (tokens[ahead1][0] == "BINOP" or
      tokens[ahead1][0] == "UNOP" or
      tokens[ahead1][0] == "MINUS" or
      tokens[ahead1][0] == "NAME" or
      tokens[ahead1][0] == "BOOL" or
      tokens[ahead1][0] == "ASSIGN" or
      tokens[ahead1][0] == "STATEMENT" or
      tokens[ahead1][0] == "REAL" or
      tokens[ahead1][0] == "INTEGER" or
      tokens[ahead1][0] == "STRING"):
    # S -> expr S_
    expr(tokens)
    if not tokens[ahead1][0] == "RBRACE":
      stack.append(tokens[index][1])
      nextToken()
      S_(tokens)

  elif tokens[index][0] == "LBRACE" and tokens[ahead1][0] == "RBRACE":
    # S -> []S_
    stack.append(tokens[index][1])
    nextToken()
    if not tokens[ahead1][0] == "RBRACE":
      stack.append(tokens[index][1])
      nextToken()
      S_(tokens)

  elif tokens[index][0] == "LBRACE":
    stack.append(tokens[index][1])
    nextToken()
    S(tokens)
    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

    if not tokens[ahead1][0] == "RBRACE":
      stack.append(tokens[index][1])
      nextToken()
      S_(tokens)

  else:
    return None

  return not None

def S_(tokens): # S_ -> SS_ | empty
  global index, ahead1, stack

  resultS = S(tokens)

  if resultS:
    if tokens[ahead1][0] == "RBRACE":
      return not None

    stack.append(tokens[index][1])
    nextToken()

    resultS_ = S_(tokens)
    if resultS_:
      pass

    return not None

  else:
    return None

def expr(tokens): # expr -> oper | stmts
  global index, ahead1

  if tokens[ahead1][0] == "STATEMENT":
    stmts(tokens)

  elif (tokens[ahead1][0] == "BINOP" or
        tokens[ahead1][0] == "UNOP" or
        tokens[ahead1][0] == "MINUS" or
        tokens[index][0] == "NAME" or
        tokens[index][0] == "BOOL" or
        tokens[ahead1][0] == "ASSIGN" or
        tokens[index][0] == "REAL" or
        tokens[index][0] == "INTEGER" or
        tokens[index][0] == "STRING"):
    oper(tokens)

  else:
    error(tokens[ahead1], "statement or oper")

  return not None

def oper(tokens): # oper -> [:= name oper] | [binops oper oper] | [unops oper] | constants | name
  global index, ahead1, stack

  if (tokens[index][0] == "REAL" or
      tokens[index][0] == "INTEGER" or
      tokens[index][0] == "STRING" or
      tokens[index][0] == "BOOL" or
      tokens[index][0] == "NAME"):
    return not None

  elif tokens[index][0] == "LBRACE":
    stack.append(tokens[index][1])
    nextToken()

    # special case for '-' sign, because it can be binop OR unop
    if tokens[index][0] == "MINUS":
      stack.append(tokens[index][1])
      nextToken()
      oper(tokens)

      if not tokens[ahead1][0] == "RBRACE":
        stack.append(tokens[index][1])
        nextToken()
        oper(tokens)

      stack.append(tokens[index][1])
      nextToken()
      if not tokens[index][0] == "RBRACE":
        error(tokens[index], ']')

      return not None

    if tokens[index][0] == "BINOP":
      # Production: [binops oper oper]
      stack.append(tokens[index][1])
      nextToken()
      oper(tokens)

      stack.append(tokens[index][1])
      nextToken()
      oper(tokens)

    elif tokens[index][0] == "UNOP":
      # Production: [unops oper]
      stack.append(tokens[index][1])
      nextToken()
      oper(tokens)

    elif tokens[index][0] == "ASSIGN":
      # Production: [:= name oper]
      stack.append(tokens[index][1])
      nextToken()

      if not tokens[index][0] == "NAME":
        error(tokens[index], 'NAME')

      stack.append(tokens[index][1])
      nextToken()
      oper(tokens)

    stack.append(tokens[index][1])
    nextToken()
    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

  else:
    error(tokens[index], '[')

  return not None

def stmts(tokens): # stmts -> ifstmts | whilestmts | letstmts | printsmts
  global index, ahead1

  if tokens[ahead1][1] == "if":
    ifstmts(tokens)

  elif tokens[ahead1][1] == "while":
    whilestmts(tokens)

  elif tokens[ahead1][1] == "let":
    letstmts(tokens)

  elif tokens[ahead1][1] == "stdout":
    printstmts(tokens)

  else:
    error(tokens[index], 'STATEMENT')

  return not None

def ifstmts(tokens): # ifstmts -> [if expr expr expr] | [if expr expr]
  global index, ahead1, stack

  if tokens[index][0] == "LBRACE":
    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][1] == "if":
      error(tokens[index], 'if')
    stack.append(tokens[index][1])
    nextToken()

    expr(tokens)

    stack.append(tokens[index][1])
    nextToken()
    expr(tokens)

    if tokens[ahead1][0] == "LBRACE":
      stack.append(tokens[index][1])
      nextToken()
      expr(tokens)

    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

    return not None

  else:
    error(tokens[index], '[')

  return None

def whilestmts(tokens): # whilestmts -> [while expr exprlist]
  global index, stack

  if tokens[index][0] == "LBRACE":
    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][1] == "while":
      error(tokens[index], 'while')

    stack.append(tokens[index][1])
    nextToken()
    expr(tokens)

    stack.append(tokens[index][1])
    nextToken()
    exprlist(tokens)

    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

    return not None
  else:
    error(tokens[index], '[')

  return None

def letstmts(tokens): # letstmts -> [let [varlist]]
  global index, stack

  if tokens[index][0] == "LBRACE":
    stack.append(tokens[index][1])
    nextToken()
    if not tokens[index][1] == "let":
      error(tokens[index], 'let')

    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][0] == "LBRACE":
      error(tokens[index], '[')

    stack.append(tokens[index][1])
    nextToken()
    varlist(tokens)

    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

    return not None
  else:
    error(tokens[index], '[')

  return None

def varlist(tokens): # varlist -> [name type] | [name type] varlist
  global index, ahead1, stack

  if tokens[index][0] == "LBRACE":
    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][0] == "NAME":
      error(tokens[index], 'NAME')

    stack.append(tokens[index][1])
    nextToken()
    if not tokens[index][0] == "TYPES":
      error(tokens[index], 'TYPES')

    stack.append(tokens[index][1])
    nextToken()
    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

    # Use the lookahead to determine if there's another varlist
    if tokens[ahead1][0] == "LBRACE":
      stack.append(tokens[index][1])
      nextToken()
      varlist(tokens)

    return not None
  else:
    error(tokens[index], '[')

  return None

def printstmts(tokens): # printstmts -> [stdout oper]
  global index, stack

  if tokens[index][0] == "LBRACE":
    stack.append(tokens[index][1])
    nextToken()
    if not tokens[index][1] == "stdout":
      error(tokens[index], 'stdout')

    stack.append(tokens[index][1])
    nextToken()
    oper(tokens)

    stack.append(tokens[index][1])
    nextToken()
    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

    return not None

  else:
    error(tokens[index], '[')

  return None

def exprlist(tokens): # exprlist -> expr | expr exprlist
  global index, ahead1, stack

  expr(tokens)

  if not tokens[ahead1][0] == "RBRACE":
    stack.append(tokens[index][1])
    nextToken()
    exprlist(tokens)

  return not None

def nextToken():
  global index
  global ahead1

  index += 1
  ahead1 += 1

def error(token, expected):
  print 'Invalid input: "%s"! Line: %s, colunm: %s' % (token[1], token[2], token[3])
  print 'Expecting: %s' % expected
  sys.exit(1)

def main(argument):
  with open(argument, 'r') as f:
    contents = f.read()
  parser(contents)

if __name__ == "__main__":
  main(sys.argv[1])
<commit_msg>Fixed a bug with how S goes to expr<commit_after>#!/usr/bin/env python
import collections
import myreglexer
import sys
from tree import *

index = 0
ahead1 = 1

stack = []

def parser(stream):
  global stack, index, ahead1
  tokens = list(myreglexer.tokenize(stream))
  index = 0
  ahead1 = 1
  root = Node()
  root.addChild(T(tokens))

  root.traverse()

def T(tokens): # T -> [S]
  global index, stack

  temp = Node()

  if tokens[index][0] == "LBRACE":
    temp.addChild(Node(tokens[index][1]))

    nextToken()
    temp.addChild(S(tokens))

    nextToken()
    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

    temp.addChild(Node(tokens[index][1]))

    return temp
  else:
    error(tokens[index], '[')

def S(tokens): # S -> expr S_ | []S_ | [S]S_
  global index, ahead1, stack

  temp = Node()

  if (tokens[ahead1][0] == "BINOP" or tokens[ahead1][0] == "UNOP" or
      tokens[ahead1][0] == "MINUS" or tokens[ahead1][0] == "ASSIGN" or
      tokens[ahead1][0] == "STATEMENT" or tokens[index][0] == "NAME" or
      tokens[index][0] == "BOOL" or tokens[index][0] == "REAL" or
      tokens[index][0] == "INTEGER" or tokens[index][0] == "STRING"):
    # S -> expr S_
    temp.addChild(expr(tokens))

    if not tokens[ahead1][0] == "RBRACE":
      nextToken()
      temp.addChild(S_(tokens))

  elif tokens[index][0] == "LBRACE" and tokens[ahead1][0] == "RBRACE":
    # S -> []S_
    temp.addChild(Node(tokens[index][1])) # [
    nextToken()
    temp.addChild(Node(tokens[index][1])) # ]
    if not tokens[ahead1][0] == "RBRACE":
      nextToken()
      temp.addChild(S_(tokens)) # S_

  elif tokens[index][0] == "LBRACE":
    # S -> [S]S_
    temp.addChild(Node(tokens[index][1])) # [
    nextToken()
    temp.addChild(S(tokens)) # S
    nextToken()

    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

    temp.addChild(Node(tokens[index][1])) # ]

    if not tokens[ahead1][0] == "RBRACE":
      nextToken()
      temp.addChild(S_(tokens)) # S_

  else:
    return None

  return temp

def S_(tokens): # S_ -> SS_ | empty
  global index, ahead1, stack

  temp = Node()

  resultS = S(tokens)

  if resultS:
    temp.addChild(resultS) # S
    # Handle empty case
    if tokens[ahead1][0] == "RBRACE":
      return temp

    nextToken()

    resultS_ = S_(tokens)
    if resultS_:
      temp.addChild(resultS_) # S_

    return temp

  else:
    return None

  return temp

def expr(tokens): # expr -> oper | stmts
  global index, ahead1

  temp = Node()

  if tokens[ahead1][0] == "STATEMENT":
    temp.addChild(stmts(tokens))
    return temp

  elif (tokens[ahead1][0] == "BINOP" or tokens[ahead1][0] == "UNOP" or
        tokens[ahead1][0] == "MINUS" or tokens[ahead1][0] == "ASSIGN" or
        tokens[index][0] == "NAME" or tokens[index][0] == "BOOL" or
        tokens[index][0] == "REAL" or tokens[index][0] == "INTEGER" or
        tokens[index][0] == "STRING"):
    temp.addChild(oper(tokens))
    return temp

  else:
    error(tokens[index], "statement or oper")

  return temp

def oper(tokens): # oper -> [:= name oper] | [binops oper oper] | [unops oper] | constants | name
  global index, ahead1, stack

  temp = Node()

  if (tokens[index][0] == "REAL" or
      tokens[index][0] == "INTEGER" or
      tokens[index][0] == "STRING" or
      tokens[index][0] == "BOOL" or
      tokens[index][0] == "NAME"):
    temp.addChild(Node(tokens[index][1]))
    return temp

  elif tokens[index][0] == "LBRACE":
    temp.addChild(Node(tokens[index][1]))
    nextToken()

    # special case for '-' sign, because it can be binop OR unop
    if tokens[index][0] == "MINUS":
      temp.addChild(Node(tokens[index][1]))
      nextToken()
      temp.addChild(oper(tokens))

      if not tokens[ahead1][0] == "RBRACE":
        #stack.append(tokens[index][1])
        nextToken()
        temp.addChild(oper(tokens))

      #stack.append(tokens[index][1])
      nextToken()
      if not tokens[index][0] == "RBRACE":
        error(tokens[index], ']')

      temp.addChild(Node(tokens[index][1]))
      return temp

    if tokens[index][0] == "BINOP":
      # Production: [binops oper oper]
      stack.append(tokens[index][1])
      nextToken()
      oper(tokens)

      stack.append(tokens[index][1])
      nextToken()
      oper(tokens)

    elif tokens[index][0] == "UNOP":
      # Production: [unops oper]
      stack.append(tokens[index][1])
      nextToken()
      oper(tokens)

    elif tokens[index][0] == "ASSIGN":
      # Production: [:= name oper]
      stack.append(tokens[index][1])
      nextToken()

      if not tokens[index][0] == "NAME":
        error(tokens[index], 'NAME')

      stack.append(tokens[index][1])
      nextToken()
      oper(tokens)

    stack.append(tokens[index][1])
    nextToken()
    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

  else:
    error(tokens[index], '[')

  return not None

def stmts(tokens): # stmts -> ifstmts | whilestmts | letstmts | printsmts
  global index, ahead1

  if tokens[ahead1][1] == "if":
    ifstmts(tokens)

  elif tokens[ahead1][1] == "while":
    whilestmts(tokens)

  elif tokens[ahead1][1] == "let":
    letstmts(tokens)

  elif tokens[ahead1][1] == "stdout":
    printstmts(tokens)

  else:
    error(tokens[index], 'STATEMENT')

  return not None

def ifstmts(tokens): # ifstmts -> [if expr expr expr] | [if expr expr]
  global index, ahead1, stack

  if tokens[index][0] == "LBRACE":
    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][1] == "if":
      error(tokens[index], 'if')
    stack.append(tokens[index][1])
    nextToken()

    expr(tokens)

    stack.append(tokens[index][1])
    nextToken()
    expr(tokens)

    if tokens[ahead1][0] == "LBRACE":
      stack.append(tokens[index][1])
      nextToken()
      expr(tokens)

    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

    return not None

  else:
    error(tokens[index], '[')

  return None

def whilestmts(tokens): # whilestmts -> [while expr exprlist]
  global index, stack

  if tokens[index][0] == "LBRACE":
    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][1] == "while":
      error(tokens[index], 'while')

    stack.append(tokens[index][1])
    nextToken()
    expr(tokens)

    stack.append(tokens[index][1])
    nextToken()
    exprlist(tokens)

    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

    return not None
  else:
    error(tokens[index], '[')

  return None

def letstmts(tokens): # letstmts -> [let [varlist]]
  global index, stack

  if tokens[index][0] == "LBRACE":
    stack.append(tokens[index][1])
    nextToken()
    if not tokens[index][1] == "let":
      error(tokens[index], 'let')

    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][0] == "LBRACE":
      error(tokens[index], '[')

    stack.append(tokens[index][1])
    nextToken()
    varlist(tokens)

    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

    return not None
  else:
    error(tokens[index], '[')

  return None

def varlist(tokens): # varlist -> [name type] | [name type] varlist
  global index, ahead1, stack

  if tokens[index][0] == "LBRACE":
    stack.append(tokens[index][1])
    nextToken()

    if not tokens[index][0] == "NAME":
      error(tokens[index], 'NAME')

    stack.append(tokens[index][1])
    nextToken()
    if not tokens[index][0] == "TYPES":
      error(tokens[index], 'TYPES')

    stack.append(tokens[index][1])
    nextToken()
    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

    # Use the lookahead to determine if there's another varlist
    if tokens[ahead1][0] == "LBRACE":
      stack.append(tokens[index][1])
      nextToken()
      varlist(tokens)

    return not None
  else:
    error(tokens[index], '[')

  return None

def printstmts(tokens): # printstmts -> [stdout oper]
  global index, stack

  if tokens[index][0] == "LBRACE":
    stack.append(tokens[index][1])
    nextToken()
    if not tokens[index][1] == "stdout":
      error(tokens[index], 'stdout')

    stack.append(tokens[index][1])
    nextToken()
    oper(tokens)

    stack.append(tokens[index][1])
    nextToken()
    if not tokens[index][0] == "RBRACE":
      error(tokens[index], ']')

    return not None

  else:
    error(tokens[index], '[')

  return None

def exprlist(tokens): # exprlist -> expr | expr exprlist
  global index, ahead1, stack

  expr(tokens)

  if not tokens[ahead1][0] == "RBRACE":
    stack.append(tokens[index][1])
    nextToken()
    exprlist(tokens)

  return not None

def nextToken():
  global index
  global ahead1

  index += 1
  ahead1 += 1

def error(token, expected):
  print 'Invalid input: "%s"! Line: %s, colunm: %s' % (token[1], token[2], token[3])
  print 'Expecting: %s' % expected
  sys.exit(1)

def main(argument):
  with open(argument, 'r') as f:
    contents = f.read()
  parser(contents)

if __name__ == "__main__":
  main(sys.argv[1])
<|endoftext|>


<commit_before>from __future__ import print_function
import numpy as np
from numpy.testing import (TestCase,
                           assert_almost_equal,
                           assert_array_equal,
                           assert_array_almost_equal)
from scipy.spatial import spherical_voronoi
import scipy

class TestDeterminantFallback(TestCase):

    def test_zero(self):
        m = np.zeros((4, 4))
        assert_almost_equal(spherical_voronoi.determinant_fallback(m), 0.0)

    def test_unit(self):
        m = np.eye(4)
        assert_almost_equal(spherical_voronoi.determinant_fallback(m), 1.0)

    def test_example(self):
        m = np.array([[2, 5, -10, -7], [-7, -3, -1, 9],
                      [8, -6, -4, 2], [-9, -4, -3, 4]])
        assert_almost_equal(spherical_voronoi.determinant_fallback(m), 6528.0)

class TestCircumcenters(TestCase):

    def test_circumcenters(self):
        tetrahedrons = np.array([
            [[1, 2, 3],
             [-1.1, -2.1, -3.1],
             [-1.2, 2.2, 3.2],
             [-1.3, -2.3, 3.3]],
            [[10, 20, 30],
             [-10.1, -20.1, -30.1],
             [-10.2, 20.2, 30.2],
             [-10.3, -20.3, 30.3]]
        ])

        result = spherical_voronoi.calc_circumcenters(tetrahedrons)

        expected = [
            [-0.5680861153262529, -0.133279590288315, 0.1843323216995444],
            [-0.5965330784014926, -0.1480377040397778, 0.1981967854886021]
        ]

        assert_array_almost_equal(result, expected)

class TestProjectToSphere(TestCase):

    def test_unit_sphere(self):
        points = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
        center = np.array([0, 0, 0])
        radius = 1
        projected = spherical_voronoi.project_to_sphere(points, center, radius)
        assert_array_almost_equal(points, projected)

    def test_scaled_points(self):
        points = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
        center = np.array([0, 0, 0])
        radius = 1
        scaled = points * 2
        projected = spherical_voronoi.project_to_sphere(scaled, center, radius)
        assert_array_almost_equal(points, projected)

    def test_translated_sphere(self):
        points = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
        center = np.array([1, 2, 3])
        translated = points + center
        radius = 1
        projected = spherical_voronoi.project_to_sphere(translated, center,
                                                        radius)
        assert_array_almost_equal(translated, projected)

class TestSphericalVoronoi(TestCase):

    points_unsymmetric = np.array([
        [-0.78928481, -0.16341094, 0.59188373],
        [-0.66839141, 0.73309634, 0.12578818],
        [0.32535778, -0.92476944, -0.19734181],
        [-0.90177102, -0.03785291, -0.43055335],
        [0.71781344, 0.68428936, 0.12842096],
        [-0.96064876, 0.23492353, -0.14820556],
        [0.73181537, -0.22025898, -0.6449281],
        [0.79979205, 0.54555747, 0.25039913]]
    )

    def test_constructor(self):
        generators = TestSphericalVoronoi.points_unsymmetric
        center = np.array([1, 2, 3])
        radius = 2
        s1 = spherical_voronoi.SphericalVoronoi(generators)
        s2 = spherical_voronoi.SphericalVoronoi(generators, radius)
        s3 = spherical_voronoi.SphericalVoronoi(generators, None, center)
        s4 = spherical_voronoi.SphericalVoronoi(generators, radius, center)
        assert_array_equal(s1.center, np.array([0, 0, 0]))
        self.assertEqual(s1.radius, 1)
        assert_array_equal(s2.center, np.array([0, 0, 0]))
        self.assertEqual(s2.radius, 2)
        assert_array_equal(s3.center, center)
        self.assertEqual(s3.radius, 1)
        assert_array_equal(s4.center, center)
        self.assertEqual(s4.radius, radius)

    def test_vertices_regions_translation_invariance(self):
        points = TestSphericalVoronoi.points_unsymmetric
        sv_origin = spherical_voronoi.SphericalVoronoi(points)
        center = np.array([1, 1, 1])
        points += center
        sv_translated = spherical_voronoi.SphericalVoronoi(points, None, center)
        assert_array_equal(sv_origin.regions, sv_translated.regions)
        assert_array_almost_equal(sv_origin.vertices + center,
                                  sv_translated.vertices)

    def test_vertices_regions_scaling_invariance(self):
        points = TestSphericalVoronoi.points_unsymmetric
        sv_unit = spherical_voronoi.SphericalVoronoi(points)
        points *= 2
        sv_scaled = spherical_voronoi.SphericalVoronoi(points, 2)
        assert_array_equal(sv_unit.regions, sv_scaled.regions)
        assert_array_almost_equal(sv_unit.vertices * 2,
                                  sv_scaled.vertices)

    def test_sort_vertices_of_regions(self):
        generators = TestSphericalVoronoi.points_unsymmetric
        sv = spherical_voronoi.SphericalVoronoi(generators)
        unsorted_regions = sv.regions
        sv.sort_vertices_of_regions()
        assert_array_equal(sorted(sv.regions), sorted(unsorted_regions))

    def test_voronoi_circles(self):
        generators = TestSphericalVoronoi.points_unsymmetric
        sv = spherical_voronoi.SphericalVoronoi(generators)
        for vertex in sv.vertices:
            distances = scipy.spatial.distance.cdist(sv.points,
                                                     np.array([vertex]))
            closest = np.array(sorted(distances)[0:3])
            assert_almost_equal(closest[0], closest[1])
            assert_almost_equal(closest[0], closest[2])
<commit_msg>supplied test_voronoi_circles with vertex as error msg<commit_after>from __future__ import print_function
import numpy as np
from numpy.testing import (TestCase,
                           assert_almost_equal,
                           assert_array_equal,
                           assert_array_almost_equal)
from scipy.spatial import spherical_voronoi
import scipy

class TestDeterminantFallback(TestCase):

    def test_zero(self):
        m = np.zeros((4, 4))
        assert_almost_equal(spherical_voronoi.determinant_fallback(m), 0.0)

    def test_unit(self):
        m = np.eye(4)
        assert_almost_equal(spherical_voronoi.determinant_fallback(m), 1.0)

    def test_example(self):
        m = np.array([[2, 5, -10, -7], [-7, -3, -1, 9],
                      [8, -6, -4, 2], [-9, -4, -3, 4]])
        assert_almost_equal(spherical_voronoi.determinant_fallback(m), 6528.0)

class TestCircumcenters(TestCase):

    def test_circumcenters(self):
        tetrahedrons = np.array([
            [[1, 2, 3],
             [-1.1, -2.1, -3.1],
             [-1.2, 2.2, 3.2],
             [-1.3, -2.3, 3.3]],
            [[10, 20, 30],
             [-10.1, -20.1, -30.1],
             [-10.2, 20.2, 30.2],
             [-10.3, -20.3, 30.3]]
        ])

        result = spherical_voronoi.calc_circumcenters(tetrahedrons)

        expected = [
            [-0.5680861153262529, -0.133279590288315, 0.1843323216995444],
            [-0.5965330784014926, -0.1480377040397778, 0.1981967854886021]
        ]

        assert_array_almost_equal(result, expected)

class TestProjectToSphere(TestCase):

    def test_unit_sphere(self):
        points = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
        center = np.array([0, 0, 0])
        radius = 1
        projected = spherical_voronoi.project_to_sphere(points, center, radius)
        assert_array_almost_equal(points, projected)

    def test_scaled_points(self):
        points = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
        center = np.array([0, 0, 0])
        radius = 1
        scaled = points * 2
        projected = spherical_voronoi.project_to_sphere(scaled, center, radius)
        assert_array_almost_equal(points, projected)

    def test_translated_sphere(self):
        points = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
        center = np.array([1, 2, 3])
        translated = points + center
        radius = 1
        projected = spherical_voronoi.project_to_sphere(translated, center,
                                                        radius)
        assert_array_almost_equal(translated, projected)

class TestSphericalVoronoi(TestCase):

    points_unsymmetric = np.array([
        [-0.78928481, -0.16341094, 0.59188373],
        [-0.66839141, 0.73309634, 0.12578818],
        [0.32535778, -0.92476944, -0.19734181],
        [-0.90177102, -0.03785291, -0.43055335],
        [0.71781344, 0.68428936, 0.12842096],
        [-0.96064876, 0.23492353, -0.14820556],
        [0.73181537, -0.22025898, -0.6449281],
        [0.79979205, 0.54555747, 0.25039913]]
    )

    def test_constructor(self):
        generators = TestSphericalVoronoi.points_unsymmetric
        center = np.array([1, 2, 3])
        radius = 2
        s1 = spherical_voronoi.SphericalVoronoi(generators)
        s2 = spherical_voronoi.SphericalVoronoi(generators, radius)
        s3 = spherical_voronoi.SphericalVoronoi(generators, None, center)
        s4 = spherical_voronoi.SphericalVoronoi(generators, radius, center)
        assert_array_equal(s1.center, np.array([0, 0, 0]))
        self.assertEqual(s1.radius, 1)
        assert_array_equal(s2.center, np.array([0, 0, 0]))
        self.assertEqual(s2.radius, 2)
        assert_array_equal(s3.center, center)
        self.assertEqual(s3.radius, 1)
        assert_array_equal(s4.center, center)
        self.assertEqual(s4.radius, radius)

    def test_vertices_regions_translation_invariance(self):
        points = TestSphericalVoronoi.points_unsymmetric
        sv_origin = spherical_voronoi.SphericalVoronoi(points)
        center = np.array([1, 1, 1])
        points += center
        sv_translated = spherical_voronoi.SphericalVoronoi(points, None, center)
        assert_array_equal(sv_origin.regions, sv_translated.regions)
        assert_array_almost_equal(sv_origin.vertices + center,
                                  sv_translated.vertices)

    def test_vertices_regions_scaling_invariance(self):
        points = TestSphericalVoronoi.points_unsymmetric
        sv_unit = spherical_voronoi.SphericalVoronoi(points)
        points *= 2
        sv_scaled = spherical_voronoi.SphericalVoronoi(points, 2)
        assert_array_equal(sv_unit.regions, sv_scaled.regions)
        assert_array_almost_equal(sv_unit.vertices * 2,
                                  sv_scaled.vertices)

    def test_sort_vertices_of_regions(self):
        generators = TestSphericalVoronoi.points_unsymmetric
        sv = spherical_voronoi.SphericalVoronoi(generators)
        unsorted_regions = sv.regions
        sv.sort_vertices_of_regions()
        assert_array_equal(sorted(sv.regions), sorted(unsorted_regions))

    def test_voronoi_circles(self):
        generators = TestSphericalVoronoi.points_unsymmetric
        sv = spherical_voronoi.SphericalVoronoi(generators)
        for vertex in sv.vertices:
            distances = scipy.spatial.distance.cdist(sv.points,
                                                     np.array([vertex]))
            closest = np.array(sorted(distances)[0:3])
            assert_almost_equal(closest[0], closest[1], 7, str(vertex))
            assert_almost_equal(closest[0], closest[2], 7, str(vertex))
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models

class ResPartner(models.Model):
    _inherit = 'res.partner'

    event_count = fields.Integer("Events", compute='_compute_event_count', help="Number of events the partner has participated.")

    def _compute_event_count(self):
        for partner in self:
            partner.event_count = self.env['event.event'].sudo().search_count([('registration_ids.partner_id', 'child_of', partner.ids)])

    @api.multi
    def action_event_view(self):
        action = self.env.ref('event.action_event_view').read()[0]
        action['context'] = {}
        action['domain'] = [('registration_ids.partner_id', 'child_of', self.ids)]
        return action
<commit_msg>[FIX] event: do not use sudo to compute event_count<commit_after># -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models

class ResPartner(models.Model):
    _inherit = 'res.partner'

    event_count = fields.Integer("Events", compute='_compute_event_count', help="Number of events the partner has participated.")

    def _compute_event_count(self):
        for partner in self:
            partner.event_count = self.env['event.event'].search_count([('registration_ids.partner_id', 'child_of', partner.ids)])

    @api.multi
    def action_event_view(self):
        action = self.env.ref('event.action_event_view').read()[0]
        action['context'] = {}
        action['domain'] = [('registration_ids.partner_id', 'child_of', self.ids)]
        return action
<|endoftext|>


<commit_before>import collections
import itertools
import os
import sys

from artificialproject.field_generators import (
    BuildTargetSetGenerator,
    NullableGenerator,
    PathSetGenerator,
    SingletonGenerator,
    SourcePathSetGenerator,
    SourcesWithFlagsGenerator,
    StringGenerator,
    VisibilityGenerator,
)

from artificialproject.random import weighted_choice

Context = collections.namedtuple('Context', [
    'input_target_data',
    'gen_targets_by_type',
    'gen_targets_with_output_by_type',
    'output_repository',
    'file_path_generator',
])

class TargetDataGenerator:
    def __init__(self, type, target_name_generator, context):
        self._type = type
        self._context = context
        self._target_name_generator = target_name_generator
        self._generators = {}

    def _get_field_generator(self, target_type, field_name):

        def nullable(generator_class):
            return lambda context: NullableGenerator(generator_class(context))

        def singleton(set_generator_class):
            return lambda context: SingletonGenerator(
                    set_generator_class(context))

        def string(context):
            return StringGenerator()

        def visibility(context):
            return VisibilityGenerator()

        build_target_set = BuildTargetSetGenerator
        build_target = singleton(build_target_set)
        paths = PathSetGenerator
        path = singleton(paths)
        source_path_set = SourcePathSetGenerator
        source_path = singleton(source_path_set)
        sources_with_flags = SourcesWithFlagsGenerator

        FIELDS = {
            '*.deps': build_target_set,
            '*.out': nullable(string),
            '*.srcs': source_path_set,
            '*.visibility': visibility,
            'android_binary.keystore': build_target,
            'android_binary.manifest': source_path,
            'android_build_config.package': string,
            'android_manifest.skeleton': source_path,
            'android_react_native_library.bundle_name': string,
            'android_react_native_library.entry_path': source_path,
            'android_react_native_library.package': string,
            'cxx_library.srcs': sources_with_flags,
            'export_file.src': source_path,
            'gen_aidl.aidl': source_path,
            'gen_aidl.import_path': string,
            'keystore.properties': source_path,
            'keystore.store': source_path,
            'prebuilt_jar.binary_jar': source_path,
            'prebuilt_native_library.native_libs': path,
            'sh_binary.main': source_path,
            'worker_tool.exe': build_target,
        }

        sentinel = object()

        generator_class = FIELDS.get(target_type + '.' + field_name, sentinel)
        if generator_class is not sentinel:
            return generator_class(self._context)

        generator_class = FIELDS.get('*.' + field_name, sentinel)
        if generator_class is not sentinel:
            return generator_class(self._context)

        return None

    def add_sample(self, sample):
        target_type = sample['buck.type']
        base_path = sample['buck.base_path']
        if target_type == 'export_file' and sample['src'] is None:
            sample['src'] = sample['name']
        for field, value in sample.items():
            if '.' in field or field == 'name':
                continue
            if (target_type == 'android_build_config' and
                    field == 'java_package'):
                field = 'package'
            if (target_type in [
                    'android_resource',
                    'android_react_native_library',
                    ] and field == 'r_dot_java_package'):
                field = 'package'
            if field in self._generators:
                generator = self._generators[field]
            else:
                generator = self._get_field_generator(target_type, field)
                self._generators[field] = generator
                if generator is None:
                    print('Warning: no generator for {}.{}'.format(
                        target_type, field), file=sys.stderr)
            if generator is None:
                continue
            generator.add_sample(base_path, value)

    def generate(self):
        base_path = self._context.file_path_generator.generate_package_path()
        result = {
            'name': self._target_name_generator(),
            'buck.type': self._type,
            'buck.base_path': base_path,
        }
        all_deps = set()
        for field, generator in self._generators.items():
            if generator is None:
                continue
            generated_field = generator.generate(base_path)
            result[field] = generated_field.value
            all_deps.update(generated_field.deps)
        if self._type == 'ndk_library':
            makefile_path = os.path.join(base_path, 'Android.mk')
            self._context.file_path_generator.register_path(makefile_path)
            makefile_path = os.path.join(
                    self._context.output_repository,
                    makefile_path)
            os.makedirs(os.path.dirname(makefile_path), exist_ok=True)
            with open(makefile_path, 'a'):
                pass
        result['.all_deps'] = all_deps
        return result

class TargetGenerator:
    def __init__(self, context):
        self._types = collections.Counter()
        self._data_generators = {}
        target_id = itertools.count()

        def target_name_generator():
            return 't' + str(next(target_id))

        self._create_new_data_generator = lambda type: TargetDataGenerator(
                type,
                target_name_generator,
                context)

    def add_sample(self, sample):
        type = sample['buck.type']
        if type not in self._data_generators:
            self._data_generators[type] = self._create_new_data_generator(type)
        self._data_generators[type].add_sample(sample)
        self._types.update([type])

    def generate(self, force_type=None):
        if force_type is not None:
            type = force_type
        else:
            type = weighted_choice(self._types)
        return self._data_generators[type].generate()
<commit_msg>Artificial project: generate implicit files for prebuilt libraries<commit_after>import collections
import itertools
import os
import sys

from artificialproject.field_generators import (
    BuildTargetSetGenerator,
    NullableGenerator,
    PathSetGenerator,
    SingletonGenerator,
    SourcePathSetGenerator,
    SourcesWithFlagsGenerator,
    StringGenerator,
    VisibilityGenerator,
)

from artificialproject.random import weighted_choice

Context = collections.namedtuple('Context', [
    'input_target_data',
    'gen_targets_by_type',
    'gen_targets_with_output_by_type',
    'output_repository',
    'file_path_generator',
])

class TargetDataGenerator:
    def __init__(self, type, target_name_generator, context):
        self._type = type
        self._context = context
        self._target_name_generator = target_name_generator
        self._generators = {}

    def _get_field_generator(self, target_type, field_name):

        def nullable(generator_class):
            return lambda context: NullableGenerator(generator_class(context))

        def singleton(set_generator_class):
            return lambda context: SingletonGenerator(
                    set_generator_class(context))

        def string(context):
            return StringGenerator()

        def visibility(context):
            return VisibilityGenerator()

        build_target_set = BuildTargetSetGenerator
        build_target = singleton(build_target_set)
        paths = PathSetGenerator
        path = singleton(paths)
        source_path_set = SourcePathSetGenerator
        source_path = singleton(source_path_set)
        sources_with_flags = SourcesWithFlagsGenerator

        FIELDS = {
            '*.deps': build_target_set,
            '*.out': nullable(string),
            '*.srcs': source_path_set,
            '*.visibility': visibility,
            'android_binary.keystore': build_target,
            'android_binary.manifest': source_path,
            'android_build_config.package': string,
            'android_manifest.skeleton': source_path,
            'android_react_native_library.bundle_name': string,
            'android_react_native_library.entry_path': source_path,
            'android_react_native_library.package': string,
            'cxx_library.srcs': sources_with_flags,
            'export_file.src': source_path,
            'gen_aidl.aidl': source_path,
            'gen_aidl.import_path': string,
            'keystore.properties': source_path,
            'keystore.store': source_path,
            'prebuilt_jar.binary_jar': source_path,
            'prebuilt_native_library.native_libs': path,
            'sh_binary.main': source_path,
            'worker_tool.exe': build_target,
        }

        sentinel = object()

        generator_class = FIELDS.get(target_type + '.' + field_name, sentinel)
        if generator_class is not sentinel:
            return generator_class(self._context)

        generator_class = FIELDS.get('*.' + field_name, sentinel)
        if generator_class is not sentinel:
            return generator_class(self._context)

        return None

    def add_sample(self, sample):
        target_type = sample['buck.type']
        base_path = sample['buck.base_path']
        if target_type == 'export_file' and sample['src'] is None:
            sample['src'] = sample['name']
        for field, value in sample.items():
            if '.' in field or field == 'name':
                continue
            if (target_type == 'android_build_config' and
                    field == 'java_package'):
                field = 'package'
            if (target_type in [
                    'android_resource',
                    'android_react_native_library',
                    ] and field == 'r_dot_java_package'):
                field = 'package'
            if field in self._generators:
                generator = self._generators[field]
            else:
                generator = self._get_field_generator(target_type, field)
                self._generators[field] = generator
                if generator is None:
                    print('Warning: no generator for {}.{}'.format(
                        target_type, field), file=sys.stderr)
            if generator is None:
                continue
            generator.add_sample(base_path, value)

    def generate(self):
        base_path = self._context.file_path_generator.generate_package_path()
        result = {
            'name': self._target_name_generator(),
            'buck.type': self._type,
            'buck.base_path': base_path,
        }
        all_deps = set()
        for field, generator in self._generators.items():
            if generator is None:
                continue
            generated_field = generator.generate(base_path)
            result[field] = generated_field.value
            all_deps.update(generated_field.deps)
        if self._type == 'ndk_library':
            makefile_path = os.path.join(base_path, 'Android.mk')
            self._register_and_create_file_at_path(makefile_path)
        elif self._type == 'prebuilt_cxx_library':
            library_path = os.path.join(base_path, 'lib', 'lib{}.a'.format(
                result['name']))
            self._register_and_create_file_at_path(library_path)
        result['.all_deps'] = all_deps
        return result

    def _register_and_create_file_at_path(self, path):
        self._context.file_path_generator.register_path(path)
        path = os.path.join(self._context.output_repository, path)
        os.makedirs(os.path.dirname(path), exist_ok=True)
        with open(path, 'a'):
            pass

class TargetGenerator:
    def __init__(self, context):
        self._types = collections.Counter()
        self._data_generators = {}
        target_id = itertools.count()

        def target_name_generator():
            return 't' + str(next(target_id))

        self._create_new_data_generator = lambda type: TargetDataGenerator(
                type,
                target_name_generator,
                context)

    def add_sample(self, sample):
        type = sample['buck.type']
        if type not in self._data_generators:
            self._data_generators[type] = self._create_new_data_generator(type)
        self._data_generators[type].add_sample(sample)
        self._types.update([type])

    def generate(self, force_type=None):
        if force_type is not None:
            type = force_type
        else:
            type = weighted_choice(self._types)
        return self._data_generators[type].generate()
<|endoftext|>


<commit_before>from files import *
from mods import MOD

import re

class GeneLoader:
    def get_data(self, gene_data, batch_size, test_set):

        gene_dataset = {}
        list_to_yield = []

        dateProduced = gene_data['metaData']['dateProduced']
        dataProvider = gene_data['metaData']['dataProvider']
        release = None

        if 'release' in gene_data['metaData']:
            release = gene_data['metaData']['release']

        for geneRecord in gene_data['data']:
            cross_references = []
            external_ids = []
            genomic_locations = []
            start = None
            end = None
            strand = None
            name = None

            primary_id = geneRecord['primaryId']

            if geneRecord['taxonId'] == "10116" and not primary_id.startswith("RGD"):
                primary_id = dataProvider + ":" + geneRecord['primaryId']

            if test_set == 'true':
                is_it_test_entry = self.check_for_test_entry(primary_id, geneRecord['taxonId'])
                if is_it_test_entry == 'false':
                    continue

            if 'crossReferences' in geneRecord:
                for crossRef in geneRecord['crossReferences']:
                    ref_text = crossRef['dataProvider'] + " " + crossRef['id']
                    external_ids.append(ref_text)
                    cross_references.append({"dataProvider": crossRef['dataProvider'], "id": crossRef['id']})
            if 'genomeLocations' in geneRecord:
                for genomeLocation in geneRecord['genomeLocations']:
                    chromosome = genomeLocation['chromosome']
                    assembly = genomeLocation['assembly']
                    if 'startPosition' in genomeLocation:
                        start = genomeLocation['startPosition']
                    if 'endPosition' in genomeLocation:
                        end = genomeLocation['endPosition']
                    if 'strand' in geneRecord['genomeLocations']:
                        strand = genomeLocation['strand']
                    genomic_locations.append(
                        {"chromosome": chromosome, "start": start, "end": end, "strand": strand, "assembly": assembly})

            gene_dataset = {
                "symbol": geneRecord['symbol'],
                "name": geneRecord.get('name'),
                "description": geneRecord.get('description'),
                "synonyms": geneRecord.get('synonyms'),
                "soTermId": geneRecord['soTermId'],
                "soTermName": None,
                "secondaryIds": geneRecord.get('secondaryIds'),
                "geneSynopsis": geneRecord.get('geneSynopsis'),
                "geneSynopsisUrl": geneRecord.get('geneSynopsisUrl'),
                "taxonId": geneRecord['taxonId'],
                "species": self.get_species(geneRecord['taxonId']),
                "external_ids": external_ids,
                "gene_biological_process": [],
                "gene_molecular_function": [],
                "gene_cellular_component": [],
                "genomeLocations": genomic_locations,
                "homologs": [],
                "geneLiteratureUrl": geneRecord.get('geneLiteratureUrl'),
                "name_key": geneRecord['symbol'],
                "primaryId": primary_id,
                "crossReferences": cross_references,
                "href": None,
                "category": "gene",
                "dateProduced": dateProduced,
                "dataProvider": dataProvider,
                "release": release
            }

            # Establishes the number of genes to yield (return) at a time.
            list_to_yield.append(gene_dataset)
            if len(list_to_yield) == batch_size:
                yield list_to_yield
                list_to_yield[:] = [] # Empty the list.

        if len(list_to_yield) > 0:
            yield list_to_yield

    def get_species(self, taxon_id):
        if taxon_id in ("7955"):
            return "Danio rerio"
        elif taxon_id in ("6239"):
            return "Caenorhabditis elegans"
        elif taxon_id in ("10090"):
            return "Mus musculus"
        elif taxon_id in ("10116"):
            return "Rattus norvegicus"
        elif taxon_id in ("559292"):
            return "Saccharomyces cerevisiae"
        elif taxon_id in ("7227"):
            return "Drosophila melanogaster"
        elif taxon_id in ("9606"):
            return "Homo sapiens"
        else:
            return None

    def check_for_test_entry(self, primary_id, taxon):
        test_dict = {
            '9606': ['HGNC:17889', 'HGNC:25818', 'HGNC:3686'],
            '10116': ['RGD:70891', 'RGD:1306349', 'RGD:620796'],
            '10090': ['MGI:109337', 'MGI:108202', 'MGI:2676586'],
            '7955': ['ZDB-GENE-990415-72', 'ZDB-GENE-030131-3445', 'ZDB-GENE-980526-388'],
            '7227': ['FBgn0083974', 'FBgn0037960', 'FBgn0027296'],
            '6239': ['WBGene00044305', 'WBGene00169423', 'WBGene00000987'],
            '559292': ['S000003256', 'S000003513', 'S000000119']
        }

        if primary_id in test_dict[taxon]:
            return 'true'
        else:
            return 'false'<commit_msg>AGR-325 Typo in FBgn number.<commit_after>from files import *
from mods import MOD

import re

class GeneLoader:
    def get_data(self, gene_data, batch_size, test_set):

        gene_dataset = {}
        list_to_yield = []

        dateProduced = gene_data['metaData']['dateProduced']
        dataProvider = gene_data['metaData']['dataProvider']
        release = None

        if 'release' in gene_data['metaData']:
            release = gene_data['metaData']['release']

        for geneRecord in gene_data['data']:
            cross_references = []
            external_ids = []
            genomic_locations = []
            start = None
            end = None
            strand = None
            name = None

            primary_id = geneRecord['primaryId']

            if geneRecord['taxonId'] == "10116" and not primary_id.startswith("RGD"):
                primary_id = dataProvider + ":" + geneRecord['primaryId']

            if test_set == 'true':
                is_it_test_entry = self.check_for_test_entry(primary_id, geneRecord['taxonId'])
                if is_it_test_entry == 'false':
                    continue

            if 'crossReferences' in geneRecord:
                for crossRef in geneRecord['crossReferences']:
                    ref_text = crossRef['dataProvider'] + " " + crossRef['id']
                    external_ids.append(ref_text)
                    cross_references.append({"dataProvider": crossRef['dataProvider'], "id": crossRef['id']})
            if 'genomeLocations' in geneRecord:
                for genomeLocation in geneRecord['genomeLocations']:
                    chromosome = genomeLocation['chromosome']
                    assembly = genomeLocation['assembly']
                    if 'startPosition' in genomeLocation:
                        start = genomeLocation['startPosition']
                    if 'endPosition' in genomeLocation:
                        end = genomeLocation['endPosition']
                    if 'strand' in geneRecord['genomeLocations']:
                        strand = genomeLocation['strand']
                    genomic_locations.append(
                        {"chromosome": chromosome, "start": start, "end": end, "strand": strand, "assembly": assembly})

            gene_dataset = {
                "symbol": geneRecord['symbol'],
                "name": geneRecord.get('name'),
                "description": geneRecord.get('description'),
                "synonyms": geneRecord.get('synonyms'),
                "soTermId": geneRecord['soTermId'],
                "soTermName": None,
                "secondaryIds": geneRecord.get('secondaryIds'),
                "geneSynopsis": geneRecord.get('geneSynopsis'),
                "geneSynopsisUrl": geneRecord.get('geneSynopsisUrl'),
                "taxonId": geneRecord['taxonId'],
                "species": self.get_species(geneRecord['taxonId']),
                "external_ids": external_ids,
                "gene_biological_process": [],
                "gene_molecular_function": [],
                "gene_cellular_component": [],
                "genomeLocations": genomic_locations,
                "homologs": [],
                "geneLiteratureUrl": geneRecord.get('geneLiteratureUrl'),
                "name_key": geneRecord['symbol'],
                "primaryId": primary_id,
                "crossReferences": cross_references,
                "href": None,
                "category": "gene",
                "dateProduced": dateProduced,
                "dataProvider": dataProvider,
                "release": release
            }

            # Establishes the number of genes to yield (return) at a time.
            list_to_yield.append(gene_dataset)
            if len(list_to_yield) == batch_size:
                yield list_to_yield
                list_to_yield[:] = [] # Empty the list.

        if len(list_to_yield) > 0:
            yield list_to_yield

    def get_species(self, taxon_id):
        if taxon_id in ("7955"):
            return "Danio rerio"
        elif taxon_id in ("6239"):
            return "Caenorhabditis elegans"
        elif taxon_id in ("10090"):
            return "Mus musculus"
        elif taxon_id in ("10116"):
            return "Rattus norvegicus"
        elif taxon_id in ("559292"):
            return "Saccharomyces cerevisiae"
        elif taxon_id in ("7227"):
            return "Drosophila melanogaster"
        elif taxon_id in ("9606"):
            return "Homo sapiens"
        else:
            return None

    def check_for_test_entry(self, primary_id, taxon):
        test_dict = {
            '9606': ['HGNC:17889', 'HGNC:25818', 'HGNC:3686'],
            '10116': ['RGD:70891', 'RGD:1306349', 'RGD:620796'],
            '10090': ['MGI:109337', 'MGI:108202', 'MGI:2676586'],
            '7955': ['ZDB-GENE-990415-72', 'ZDB-GENE-030131-3445', 'ZDB-GENE-980526-388'],
            '7227': ['FBgn0083973', 'FBgn0037960', 'FBgn0027296'],
            '6239': ['WBGene00044305', 'WBGene00169423', 'WBGene00000987'],
            '559292': ['S000003256', 'S000003513', 'S000000119']
        }

        if primary_id in test_dict[taxon]:
            return 'true'
        else:
            return 'false'<|endoftext|>


<commit_before>from google.appengine.ext import db
from google.appengine.api import search
import logging
import uuid
from math import sqrt
import configuration.site
from google.appengine.api import memcache

class Profile(db.Model):
  account = db.UserProperty()
  display_name = db.StringProperty()
  products = db.ListProperty(long, default=[-1])
  is_publisher = db.BooleanProperty(default=False)

  @staticmethod
  def for_user(user):
    return Profile.all().filter("account = ", user).get()

  def get_products(self):
    result = []
    for product in self.products:
      if product != -1:
        result.append(Product.get_by_id(product))
    return result

  def get_published_products(self):
    return Product.all().filter("creator = ", self).fetch(100)

  def has_product(self, product):
    return product.key().id() in self.products

_MONSTER_INDEX = "monsters"

class Monster(db.Model):
  """Model for a Dungeon World monster"""

  # Dungeon World Monster Properties
  name = db.StringProperty()
  hp = db.StringProperty()
  armor = db.StringProperty()
  damage = db.StringProperty()
  damage_tags = db.StringListProperty()
  tags = db.StringListProperty()
  special_qualities = db.StringListProperty()
  instinct = db.StringProperty()
  description = db.TextProperty()
  moves = db.StringListProperty()

  # Monster Builder Properties
  creator = db.ReferenceProperty(reference_class=Profile)
  creation_time = db.DateTimeProperty(auto_now_add=True)
  creation_rules = db.StringProperty()
  edited = db.BooleanProperty(default=False)
  product = db.IntegerProperty(default=-1)
  license = db.StringProperty()
  is_core = db.BooleanProperty(default=False)
  ups = db.IntegerProperty(default=0)
  downs = db.IntegerProperty(default=0)
  score = db.FloatProperty(default=0.0)

  def __str__(self):
    x = " ".join(self.tags)
    return ("%s %s %s %s %s %s %s %s %s %s" % (self.name, " ".join(self.tags),
      self.damage, self.hp, self.armor, " ".join(self.damage_tags),
      self.instinct, self.description, " ".join(self.special_qualities),
      " ".join(self.moves))).encode('utf-8')

  def create_document(self):
    return search.Document(
            doc_id=str(self.key().id()),
            fields=[search.TextField(name='stats', value=str(self))])

  def make_searchable(self):
    try:
        search.Index(name=_MONSTER_INDEX).put(self.create_document())
    except search.Error:
        logging.exception('Put failed')

  def make_unsearchable(self):
    try:
        search.Index(name=_MONSTER_INDEX).delete(self.key().id())
    except search.Error:
        logging.exception('Delete failed')

  def compute_score(self):
    # It's possible these properties might be None. Make sure they're
    # ints instead.
    if not self.ups:
      self.ups = 0
    if not self.downs:
      self.downs = 0

    if self.ups - self.downs == 0:
      self.score = 0
    else:
      n = self.ups + self.downs
      z = 1.0 #1.0 = 85%, 1.6 = 95%
      phat = float(self.ups) / n
      self.score = ((phat + z*z/(2*n) - z * sqrt((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n))

  def put_unsearchable(self):
    db.Model.put(self)

  def put(self):
    db.Model.put(self)
    self.make_searchable()

  def delete(self):
    for favorite in Vote.all().filter("monster = ",self).run():
      favorite.delete()
    try:
        search.Index(name=_MONSTER_INDEX).delete(str(self.key().id()))
    except AssertionError as e:
      if e.message == 'No api proxy found for service "search"':
        pass
      else:
        raise e

    db.Model.delete(self)

  def get_product(self):
    mem_key = "product:%s" % self.product
    data = memcache.get(mem_key)
    if not data:
      data = Product.get_by_id(self.product)
      memcache.add(mem_key, data, 300)
    return data

  def get_tags(self):
    return ", ".join(self.tags)

  def get_damage_tags(self):
    return ", ".join(self.damage_tags)

  def get_special_qualities(self):
    return ", ".join(self.special_qualities)

  def get_license(self):
    if self.is_core:
      return configuration.site.licenses['Creative Commons Attribution']
    if not hasattr(self, 'license'):
      return "All rights reserved."
    if not self.license:
      return "All rights reserved."
    else:
      return self.license

  def url(self):
    return "/monster/"+str(self.key().id())

  def vote(self, profile):
    return Vote.all().filter("voter = ", profile).filter("monster = ",self).get()

  @staticmethod
  def get_recent(limit, creator=None, user=None):
    query = db.Query(Monster)
    if creator:
      query.filter("creator = ",creator)

    if user:
      query.filter("product IN", user.products)
    else:
      query.filter("product = ",-1)

    query.order("-creation_time")
    return query.fetch(limit)

  @staticmethod
  def get_recent_public(skip=0):
    mem_key = "recent:%s" % skip

    data = memcache.get(mem_key)
    if not data:
      query = db.Query(Monster)
      query.filter("product = ",-1)
      query.order("-creation_time")
      data = query.fetch(10, offset=skip)
      memcache.add(mem_key, data, 300)
    return data

  @staticmethod
  def get_top_rated(limit, creator=None, user=None):
    query = db.Query(Monster)
    if creator:
      query.filter("creator = ",creator)

    if user:
      query.filter("product IN", user.products)
    else:
      query.filter("product = ",-1)

    query.order("-score")
    return query.fetch(limit)

  @staticmethod
  def search(query, user=None):
    raw_results = search.Index(name=_MONSTER_INDEX).search(query)
    return [Monster.get_by_id_safe(int(result.doc_id), user) for result in raw_results]

  @staticmethod
  def get_by_id_safe(id, user=None):
    mem_key = "monster:%s" % id
    result = memcache.get(mem_key)
    if not result:
      result = Monster.get_by_id(id)
    if not result:
      return None
    if user and (result.product in user.products):
      return result
    elif result.product == -1:
      return result

    return None

class Vote(db.Model):
  voter = db.ReferenceProperty(reference_class=Profile)
  monster = db.ReferenceProperty(reference_class=Monster)
  creation_time = db.DateTimeProperty(auto_now_add=True)
  is_up = db.BooleanProperty(default=True)

  def is_down(self):
    return not self.is_up

class Product(db.Model):
  name = db.StringProperty()
  creator = db.ReferenceProperty(reference_class=Profile)
  access_code = db.StringProperty()
  description = db.TextProperty()
  link = db.LinkProperty()

  def generate_access_code(self):
    self.access_code = uuid.uuid4().hex

  def get_contents(self):
    return Monster.all().filter("product = ", self.key().id()).fetch(200)

  @staticmethod
  def get_by_access_code(code):
    return Product.all().filter("access_code = ", code).get()<commit_msg>Make a lot more things hit the memcache<commit_after>from google.appengine.ext import db
from google.appengine.api import search
import logging
import uuid
from math import sqrt
import configuration.site
from google.appengine.api import memcache

class Profile(db.Model):
  account = db.UserProperty()
  display_name = db.StringProperty()
  products = db.ListProperty(long, default=[-1])
  is_publisher = db.BooleanProperty(default=False)

  @staticmethod
  def for_user(user):
    key = Profile.all().filter("account = ", user).get(keys_only=True)
    if not key:
      return None

    mem_key = "profile:%s" % key.id()
    data = memcache.get(mem_key)
    if not data:
      logging.info('Not found')
      data = Profile.all().filter("account = ", user).get()
      memcache.add(mem_key, data, 300)
    return data

  def get_mem_key(self):
    return "profile:%s" % self.key().id()

  def put(self):
    db.Model.put(self)
    memcache.delete(self.get_mem_key())

  def get_products(self):
    result = []
    for product in self.products:
      if product != -1:
        result.append(Product.get_by_id(product))
    return result

  def get_published_products(self):
    return Product.all().filter("creator = ", self).fetch(100)

  def has_product(self, product):
    return product.key().id() in self.products

_MONSTER_INDEX = "monsters"

class Monster(db.Model):
  """Model for a Dungeon World monster"""

  # Dungeon World Monster Properties
  name = db.StringProperty()
  hp = db.StringProperty()
  armor = db.StringProperty()
  damage = db.StringProperty()
  damage_tags = db.StringListProperty()
  tags = db.StringListProperty()
  special_qualities = db.StringListProperty()
  instinct = db.StringProperty()
  description = db.TextProperty()
  moves = db.StringListProperty()

  # Monster Builder Properties
  creator = db.ReferenceProperty(reference_class=Profile)
  creation_time = db.DateTimeProperty(auto_now_add=True)
  creation_rules = db.StringProperty()
  edited = db.BooleanProperty(default=False)
  product = db.IntegerProperty(default=-1)
  license = db.StringProperty()
  is_core = db.BooleanProperty(default=False)
  ups = db.IntegerProperty(default=0)
  downs = db.IntegerProperty(default=0)
  score = db.FloatProperty(default=0.0)

  def __str__(self):
    x = " ".join(self.tags)
    return ("%s %s %s %s %s %s %s %s %s %s" % (self.name, " ".join(self.tags),
      self.damage, self.hp, self.armor, " ".join(self.damage_tags),
      self.instinct, self.description, " ".join(self.special_qualities),
      " ".join(self.moves))).encode('utf-8')

  def create_document(self):
    return search.Document(
            doc_id=str(self.key().id()),
            fields=[search.TextField(name='stats', value=str(self))])

  def make_searchable(self):
    try:
        search.Index(name=_MONSTER_INDEX).put(self.create_document())
    except search.Error:
        logging.exception('Put failed')

  def make_unsearchable(self):
    try:
        search.Index(name=_MONSTER_INDEX).delete(self.key().id())
    except search.Error:
        logging.exception('Delete failed')

  def compute_score(self):
    # It's possible these properties might be None. Make sure they're
    # ints instead.
    if not self.ups:
      self.ups = 0
    if not self.downs:
      self.downs = 0

    if self.ups - self.downs == 0:
      self.score = 0
    else:
      n = self.ups + self.downs
      z = 1.0 #1.0 = 85%, 1.6 = 95%
      phat = float(self.ups) / n
      self.score = ((phat + z*z/(2*n) - z * sqrt((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n))

  def put_unsearchable(self):
    db.Model.put(self)

  def put(self):
    db.Model.put(self)
    memcache.delete(self.get_mem_key())
    self.make_searchable()

  def delete(self):
    for favorite in Vote.all().filter("monster = ",self).run():
      favorite.delete()
    try:
        search.Index(name=_MONSTER_INDEX).delete(str(self.key().id()))
    except AssertionError as e:
      if e.message == 'No api proxy found for service "search"':
        pass
      else:
        raise e

    db.Model.delete(self)

  def get_product(self):
    mem_key = "product:%s" % self.product
    data = memcache.get(mem_key)
    if not data:
      data = Product.get_by_id(self.product)
      memcache.add(mem_key, data, 300)
    return data

  def get_tags(self):
    return ", ".join(self.tags)

  def get_damage_tags(self):
    return ", ".join(self.damage_tags)

  def get_special_qualities(self):
    return ", ".join(self.special_qualities)

  def get_license(self):
    if self.is_core:
      return configuration.site.licenses['Creative Commons Attribution']
    if not hasattr(self, 'license'):
      return "All rights reserved."
    if not self.license:
      return "All rights reserved."
    else:
      return self.license

  def url(self):
    return "/monster/"+str(self.key().id())

  def vote(self, profile):
    return Vote.all().filter("voter = ", profile).filter("monster = ",self).get()

  def get_mem_key(self):
     return "monster:%s" % self.key().id()

  @staticmethod
  def get_recent(limit, creator=None, user=None):
    query = db.Query(Monster)
    if creator:
      query.filter("creator = ",creator)
    query.order("-creation_time")

    result = []
    for monster_key in query.run(keys_only=True):
      monster = Monster.get_by_id_safe(monster_key.id(), user)
      if monster:
        result.append(monster)
        if len(result) >= limit:
          return result

  @staticmethod
  def get_recent_public(skip=0):
    mem_key = "recent:%s" % skip

    data = memcache.get(mem_key)
    if not data:
      query = db.Query(Monster)
      query.filter("product = ",-1)
      query.order("-creation_time")
      data = query.fetch(10, offset=skip)
      memcache.add(mem_key, data, 300)
    return data

  @staticmethod
  def get_top_rated(limit, creator=None, user=None):
    query = db.Query(Monster)
    if creator:
      query.filter("creator = ",creator)

    query.order("-score")
    result = []
    for monster_key in query.run(keys_only=True):
      monster = Monster.get_by_id_safe(monster_key.id(), user)
      if monster:
        result.append(monster)
        if len(result) >= limit:
          return result

  @staticmethod
  def search(query, user=None):
    raw_results = search.Index(name=_MONSTER_INDEX).search(query)
    return [Monster.get_by_id_safe(int(result.doc_id), user) for result in raw_results]

  @staticmethod
  def get_by_id_safe(id, user=None):
    mem_key = "monster:%s" % id
    result = memcache.get(mem_key)
    if not result:
      result = Monster.get_by_id(id)
    if not result:
      return None
    if user and (result.product in user.products):
      return result
    elif result.product == -1:
      return result

    return None

class Vote(db.Model):
  voter = db.ReferenceProperty(reference_class=Profile)
  monster = db.ReferenceProperty(reference_class=Monster)
  creation_time = db.DateTimeProperty(auto_now_add=True)
  is_up = db.BooleanProperty(default=True)

  def is_down(self):
    return not self.is_up

class Product(db.Model):
  name = db.StringProperty()
  creator = db.ReferenceProperty(reference_class=Profile)
  access_code = db.StringProperty()
  description = db.TextProperty()
  link = db.LinkProperty()

  def generate_access_code(self):
    self.access_code = uuid.uuid4().hex

  def get_contents(self):
    return Monster.all().filter("product = ", self.key().id()).fetch(200)

  @staticmethod
  def get_by_access_code(code):
    return Product.all().filter("access_code = ", code).get()<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

from .molecule import Molecule
from .geometry import Geometry
from .calculation import Calculation
from .experiment import Experiment
from girder import events
from girder.models.model_base import ValidationException
from girder.utility.model_importer import ModelImporter
from .constants import PluginSettings
from girder.utility import setting_utilities

from .models.calculation import Calculation as CalculationModel
from .models.cubecache import Cubecache as CubecacheModel
from .models.experimental import Experimental as ExperimentalModel
from .models.molecule import Molecule as MoleculeModel

from girder.plugin import GirderPlugin

@setting_utilities.validator({
    PluginSettings.VIRTUOSO_BASE_URL,
    PluginSettings.VIRTUOSO_RDF_UPLOAD_PATH,
    PluginSettings.VIRTUOSO_USER,
    PluginSettings.SEMANTIC_URI_BASE,
    PluginSettings.VIRTUOSO_PASSWORD,
    PluginSettings.JENA_BASE_URL,
    PluginSettings.JENA_USER,
    PluginSettings.JENA_PASSWORD,
    PluginSettings.JENA_DATASET,
    PluginSettings.OPENBABEL_BASE_URL
})
def validateSettings(event):
    pass

class MoleculesPlugin(GirderPlugin):
    DISPLAY_NAME = 'Molecular Data'

    def load(self, info):
        # Register models for ModelImporter
        ModelImporter.registerModel('calculation', CalculationModel,
                                    'molecules')
        ModelImporter.registerModel('cubecache', CubecacheModel, 'molecules')
        ModelImporter.registerModel('experimental', ExperimentalModel,
                                    'molecules')
        ModelImporter.registerModel('molecule', MoleculeModel, 'molecules')

        info['apiRoot'].molecules = Molecule()
        info['apiRoot'].geometry = Geometry()
        info['apiRoot'].calculations = Calculation()
        info['apiRoot'].experiments = Experiment()
        events.bind('model.setting.validate', 'molecules',
                    validateSettings)
<commit_msg>Register geometry model for model importer<commit_after># -*- coding: utf-8 -*-

from .molecule import Molecule
from .geometry import Geometry
from .calculation import Calculation
from .experiment import Experiment
from girder import events
from girder.models.model_base import ValidationException
from girder.utility.model_importer import ModelImporter
from .constants import PluginSettings
from girder.utility import setting_utilities

from .models.calculation import Calculation as CalculationModel
from .models.cubecache import Cubecache as CubecacheModel
from .models.experimental import Experimental as ExperimentalModel
from .models.geometry import Geometry as GeometryModel
from .models.molecule import Molecule as MoleculeModel

from girder.plugin import GirderPlugin

@setting_utilities.validator({
    PluginSettings.VIRTUOSO_BASE_URL,
    PluginSettings.VIRTUOSO_RDF_UPLOAD_PATH,
    PluginSettings.VIRTUOSO_USER,
    PluginSettings.SEMANTIC_URI_BASE,
    PluginSettings.VIRTUOSO_PASSWORD,
    PluginSettings.JENA_BASE_URL,
    PluginSettings.JENA_USER,
    PluginSettings.JENA_PASSWORD,
    PluginSettings.JENA_DATASET,
    PluginSettings.OPENBABEL_BASE_URL
})
def validateSettings(event):
    pass

class MoleculesPlugin(GirderPlugin):
    DISPLAY_NAME = 'Molecular Data'

    def load(self, info):
        # Register models for ModelImporter
        ModelImporter.registerModel('calculation', CalculationModel,
                                    'molecules')
        ModelImporter.registerModel('cubecache', CubecacheModel, 'molecules')
        ModelImporter.registerModel('experimental', ExperimentalModel,
                                    'molecules')
        ModelImporter.registerModel('geometry', GeometryModel, 'molecules')
        ModelImporter.registerModel('molecule', MoleculeModel, 'molecules')

        info['apiRoot'].molecules = Molecule()
        info['apiRoot'].geometry = Geometry()
        info['apiRoot'].calculations = Calculation()
        info['apiRoot'].experiments = Experiment()
        events.bind('model.setting.validate', 'molecules',
                    validateSettings)
<|endoftext|>


<commit_before>'''
Created on Mar 7, 2016
This Module contains the first cut of the recommender service
@author: auerbach
'''
from SuperGLU.Util.ErrorHandling import logInfo
from SuperGLU.Core.FIPA.SpeechActs import INFORM_ACT, REQUEST_ACT
from SuperGLU.Core.MessagingGateway import BaseService
from SuperGLU.Services.QueryService.DBBridge import DBBridge
from SuperGLU.Services.StudentModel import StudentModel
from SuperGLU.Services.StudentModel.PersistentData import DBTask
from SuperGLU.Services.StudentModel.StudentModelFactories import BasicStudentModelFactory
from SuperGLU.Core.MessagingDB import RECOMMENDED_TASKS_VERB, MASTERY_VERB
from builtins import int

RECOMMENDER_SERVICE_NAME = "Recommender"

class Recommender(DBBridge):

    def calculateMasteryOfTask(self, task, studentModel):

        if studentModel is not None:
            total = 0.0
            for kc in task._kcs:
                taskMastery = 0.0
                if kc in studentModel.kcMastery.keys():
                    taskMastery = studentModel.kcMastery[kc]
                total += 1 - taskMastery

            if len(task._kcs) > 0:#really wish I didn't have to do this, but math is math
                result = total / len(task._kcs)
            else:
                result = 0.0#what should we do if a task has no knowledge components associated with it?

            return result
        else:
            #if no student model exists then set all task mastery to  zero
            return 0.0

    #remove erroneous entries from task list.
    #this isn't strictly necessary, but it guards against a corrupted database.
    def validateTasks(self, taskList):
        validTasks = []

        for task in taskList:
            if len(task._ids) > 0:
                validTasks.append(task)

        return validTasks

    def getRecommendedTasks(self, studentId, studentModel, numberOfTasksRequested):

        taskMastery = list()

        dbtaskList = DBTask.find_all()
        taskList = [x.toSerializable() for x in dbtaskList]

        taskList = self.validateTasks(taskList)

        for task in taskList:
            taskMastery.append((self.calculateMasteryOfTask(task, studentModel), task))

        sortedTaskMastery = sorted(taskMastery, key=lambda taskMastery : taskMastery[0], reverse=True)

        #logInfo("sortedTaskMastery={0}".format(sortedTaskMastery), 6)

        result = sortedTaskMastery[0:numberOfTasksRequested]

        #logInfo(msg, lod)

        return result

class RecommenderMessaging (BaseService):

    recommender = Recommender(RECOMMENDER_SERVICE_NAME)

    def studentModelCallBack(self, msg, oldMsg):
        logInfo("Entering Recommender.studentModelCallback", 5)
        numberOfRecommendations = int(oldMsg.getResult())
        recommendedTasks = self.recommender.getRecommendedTasks(msg.getObject(), msg.getResult(), numberOfRecommendations)
        outMsg = self._createRequestReply(oldMsg)#need to make sure this how we send the reply
        outMsg.setSpeechAct(INFORM_ACT)
        outMsg.setVerb(RECOMMENDED_TASKS_VERB)
        outMsg.setResult(recommendedTasks)
        self.sendMessage(outMsg)

    def receiveMessage(self, msg):
        super(RecommenderMessaging, self).receiveMessage(msg)
        #depending on the content of the message react differently
        logInfo('Entering Reccomender.receiveMessage', 5)

        if msg.getSpeechAct() == REQUEST_ACT:
            if msg.getVerb() == RECOMMENDED_TASKS_VERB:
                outMsg = self._createRequestReply(msg)
                outMsg.setVerb(MASTERY_VERB)
                outMsg.setObject(msg.getActor())
                outMsg.setResult(msg.getObject())
                self._makeRequest(outMsg, self.studentModelCallBack)

    <commit_msg>added a simple check novelty function<commit_after>'''
Created on Mar 7, 2016
This Module contains the first cut of the recommender service
@author: auerbach
'''
from SuperGLU.Util.ErrorHandling import logInfo
from SuperGLU.Core.FIPA.SpeechActs import INFORM_ACT, REQUEST_ACT
from SuperGLU.Core.MessagingGateway import BaseService
from SuperGLU.Services.QueryService.DBBridge import DBBridge
from SuperGLU.Services.StudentModel import StudentModel
from SuperGLU.Services.StudentModel.PersistentData import DBTask
from SuperGLU.Services.StudentModel.StudentModelFactories import BasicStudentModelFactory
from SuperGLU.Core.MessagingDB import RECOMMENDED_TASKS_VERB, MASTERY_VERB
from builtins import int

RECOMMENDER_SERVICE_NAME = "Recommender"

class Recommender(DBBridge):

    def calculateMasteryOfTask(self, task, studentModel):

        if studentModel is not None:
            total = 0.0
            for kc in task._kcs:
                taskMastery = 0.0
                if kc in studentModel.kcMastery.keys():
                    taskMastery = studentModel.kcMastery[kc]
                total += 1 - taskMastery

            if len(task._kcs) > 0:#really wish I didn't have to do this, but math is math
                result = total / len(task._kcs)
            else:
                result = 0.0#what should we do if a task has no knowledge components associated with it?

            return result
        else:
            #if no student model exists then set all task mastery to  zero
            return 0.0

    def checkNovelty(self, studentId, taskList):
        student = self.retrieveStudentFromCacheOrDB(studentId, None, False)

        tasksToRemove = []

        if len(student.sessionIds) > 0:
            sessions = student.getSessions(False)
            for task in taskList:
                for session in sessions:
                    if session.task is not None and session.task.name == task.name:
                        tasksToRemove.append(task)

        for taskToRemove in tasksToRemove:
            taskList.remove(taskToRemove)

        return taskList

    #remove erroneous entries from task list.
    #this isn't strictly necessary, but it guards against a corrupted database.
    def validateTasks(self, taskList):
        validTasks = []

        for task in taskList:
            if len(task._ids) > 0:
                validTasks.append(task)

        return validTasks

    def getRecommendedTasks(self, studentId, studentModel, numberOfTasksRequested):

        taskMastery = list()

        dbtaskList = DBTask.find_all()
        taskList = [x.toSerializable() for x in dbtaskList]

        taskList = self.validateTasks(taskList)
        taskList = self.checkNovelty(studentId, taskList)

        for task in taskList:
            taskMastery.append((self.calculateMasteryOfTask(task, studentModel), task))

        sortedTaskMastery = sorted(taskMastery, key=lambda taskMastery : taskMastery[0], reverse=True)

        #logInfo("sortedTaskMastery={0}".format(sortedTaskMastery), 6)

        result = sortedTaskMastery[0:numberOfTasksRequested]

        #logInfo(msg, lod)

        return result

class RecommenderMessaging (BaseService):

    recommender = Recommender(RECOMMENDER_SERVICE_NAME)

    def studentModelCallBack(self, msg, oldMsg):
        logInfo("Entering Recommender.studentModelCallback", 5)
        numberOfRecommendations = int(oldMsg.getResult())
        recommendedTasks = self.recommender.getRecommendedTasks(msg.getObject(), msg.getResult(), numberOfRecommendations)
        outMsg = self._createRequestReply(oldMsg)#need to make sure this how we send the reply
        outMsg.setSpeechAct(INFORM_ACT)
        outMsg.setVerb(RECOMMENDED_TASKS_VERB)
        outMsg.setResult(recommendedTasks)
        self.sendMessage(outMsg)

    def receiveMessage(self, msg):
        super(RecommenderMessaging, self).receiveMessage(msg)
        #depending on the content of the message react differently
        logInfo('Entering Reccomender.receiveMessage', 5)

        if msg.getSpeechAct() == REQUEST_ACT:
            if msg.getVerb() == RECOMMENDED_TASKS_VERB:
                outMsg = self._createRequestReply(msg)
                outMsg.setVerb(MASTERY_VERB)
                outMsg.setObject(msg.getActor())
                outMsg.setResult(msg.getObject())
                self._makeRequest(outMsg, self.studentModelCallBack)

    <|endoftext|>


<commit_before># Copyright (c) 2016 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.

from UM.Signal import Signal, signalemitter
from UM.PluginObject import PluginObject

from . import ContainerInterface

##  A stack of setting containers to handle setting value retrieval.
@signalemitter
class ContainerStack(ContainerInterface.ContainerInterface, PluginObject):
    Version = 1

    ##  Constructor
    #
    #   \param stack_id \type{string} A unique, machine readable/writable ID.
    def __init__(self, stack_id, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self._id = stack_id
        self._name = stack_id
        self._metadata = {}
        self._containers = []
        self._next_stack = None

    ##  \copydoc ContainerInterface::getId
    #
    #   Reimplemented from ContainerInterface
    def getId(self):
        return self._id

    ##  \copydoc ContainerInterface::getName
    #
    #   Reimplemented from ContainerInterface
    def getName(self):
        return self._name

    ##  Emitted whenver the name of this stack changes.
    nameChanged = Signal()

    ##  Set the name of this stack.
    #
    #   \param name \type{string} The new name of the stack.
    def setName(self, name):
        if name != self._name:
            self._name = name
            self.nameChanged.emit()

    ##  \copydoc ContainerInterface::getMetaData
    #
    #   Reimplemented from ContainerInterface
    def getMetaData(self):
        return self._metadata

    ##  \copydoc ContainerInterface::getMetaDataEntry
    #
    #   Reimplemented from ContainerInterface
    def getMetaDataEntry(self, entry, default = None):
        return self._metadata.get(entry, default)

    ##  \copydoc ContainerInterface::getValue
    #
    #   Reimplemented from ContainerInterface.
    #
    #   getValue will start at the top of the stack and try to get the value
    #   specified. If that container returns no value, the next container on the
    #   stack will be tried and so on until the bottom of the stack is reached.
    #   If a next stack is defined for this stack it will then try to get the
    #   value from that stack. If no next stack is defined, None will be returned.
    def getValue(self, key):
        return None

    ##  \copydoc ContainerInterface::serialize
    #
    #   Reimplemented from ContainerInterface
    def serialize(self):
        return ""

    ##  \copydoc ContainerInterface::deserialize
    #
    #   Reimplemented from ContainerInterface
    def deserialize(self, serialized):
        pass

    ##  Get a list of all containers in this stack.
    #
    #   \return \type{list} A list of all containers in this stack.
    def getContainers(self):
        return self._containers

    ##  Get a container by index.
    #
    #   \param index \type{int} The index of the container to get.
    #
    #   \return The container at the specified index.
    #
    #   \exception IndexError Raised when the specified index is out of bounds.
    def getContainer(self, index):
        return None

    ##  Find a container matching certain criteria.
    #
    #   \param filter \type{dict} A dictionary containing key and value pairs that need to match the container.
    #
    #   \return The first container that matches the filter criteria or None if not found.
    def findContainer(self, filter):
        return None

    ##  Add a container to the top of the stack.
    #
    #   \param container The container to add to the stack.
    def addContainer(self, container):
        pass

    ##  Replace a container in the stack.
    #
    #   \param index \type{int} The index of the container to replace.
    #   \param container The container to replace the existing entry with.
    #
    #   \exception IndexError Raised when the specified index is out of bounds.
    def replaceContainer(self, index, container):
        pass

    ##  Remove a container from the stack.
    #
    #   \param index \type{int} The index of the container to remove.
    #
    #   \exception IndexError Raised when the specified index is out of bounds.
    def removeContainer(self, index = 0):
        pass

    ##  Get the next stack
    #
    #   The next stack is the stack that is searched for a setting value if the
    #   bottom of the stack is reached when searching for a value.
    #
    #   \return \type{ContainerStack} The next stack or None if not set.
    def getNextStack(self):
        return self._next_stack

    ##  Set the next stack
    #
    #   \param stack \type{ContainerStack} The next stack to set. Can be None.
    #
    #   \sa getNextStack
    def setNextStack(self, stack):
        self._next_stack = stack
<commit_msg>Fix spelling in documentation<commit_after># Copyright (c) 2016 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.

from UM.Signal import Signal, signalemitter
from UM.PluginObject import PluginObject

from . import ContainerInterface

##  A stack of setting containers to handle setting value retrieval.
@signalemitter
class ContainerStack(ContainerInterface.ContainerInterface, PluginObject):
    Version = 1

    ##  Constructor
    #
    #   \param stack_id \type{string} A unique, machine readable/writable ID.
    def __init__(self, stack_id, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self._id = stack_id
        self._name = stack_id
        self._metadata = {}
        self._containers = []
        self._next_stack = None

    ##  \copydoc ContainerInterface::getId
    #
    #   Reimplemented from ContainerInterface
    def getId(self):
        return self._id

    ##  \copydoc ContainerInterface::getName
    #
    #   Reimplemented from ContainerInterface
    def getName(self):
        return self._name

    ##  Emitted whenever the name of this stack changes.
    nameChanged = Signal()

    ##  Set the name of this stack.
    #
    #   \param name \type{string} The new name of the stack.
    def setName(self, name):
        if name != self._name:
            self._name = name
            self.nameChanged.emit()

    ##  \copydoc ContainerInterface::getMetaData
    #
    #   Reimplemented from ContainerInterface
    def getMetaData(self):
        return self._metadata

    ##  \copydoc ContainerInterface::getMetaDataEntry
    #
    #   Reimplemented from ContainerInterface
    def getMetaDataEntry(self, entry, default = None):
        return self._metadata.get(entry, default)

    ##  \copydoc ContainerInterface::getValue
    #
    #   Reimplemented from ContainerInterface.
    #
    #   getValue will start at the top of the stack and try to get the value
    #   specified. If that container returns no value, the next container on the
    #   stack will be tried and so on until the bottom of the stack is reached.
    #   If a next stack is defined for this stack it will then try to get the
    #   value from that stack. If no next stack is defined, None will be returned.
    def getValue(self, key):
        return None

    ##  \copydoc ContainerInterface::serialize
    #
    #   Reimplemented from ContainerInterface
    def serialize(self):
        return ""

    ##  \copydoc ContainerInterface::deserialize
    #
    #   Reimplemented from ContainerInterface
    def deserialize(self, serialized):
        pass

    ##  Get a list of all containers in this stack.
    #
    #   \return \type{list} A list of all containers in this stack.
    def getContainers(self):
        return self._containers

    ##  Get a container by index.
    #
    #   \param index \type{int} The index of the container to get.
    #
    #   \return The container at the specified index.
    #
    #   \exception IndexError Raised when the specified index is out of bounds.
    def getContainer(self, index):
        return None

    ##  Find a container matching certain criteria.
    #
    #   \param filter \type{dict} A dictionary containing key and value pairs that need to match the container.
    #
    #   \return The first container that matches the filter criteria or None if not found.
    def findContainer(self, filter):
        return None

    ##  Add a container to the top of the stack.
    #
    #   \param container The container to add to the stack.
    def addContainer(self, container):
        pass

    ##  Replace a container in the stack.
    #
    #   \param index \type{int} The index of the container to replace.
    #   \param container The container to replace the existing entry with.
    #
    #   \exception IndexError Raised when the specified index is out of bounds.
    def replaceContainer(self, index, container):
        pass

    ##  Remove a container from the stack.
    #
    #   \param index \type{int} The index of the container to remove.
    #
    #   \exception IndexError Raised when the specified index is out of bounds.
    def removeContainer(self, index = 0):
        pass

    ##  Get the next stack
    #
    #   The next stack is the stack that is searched for a setting value if the
    #   bottom of the stack is reached when searching for a value.
    #
    #   \return \type{ContainerStack} The next stack or None if not set.
    def getNextStack(self):
        return self._next_stack

    ##  Set the next stack
    #
    #   \param stack \type{ContainerStack} The next stack to set. Can be None.
    #
    #   \sa getNextStack
    def setNextStack(self, stack):
        self._next_stack = stack
<|endoftext|>