<commit_before>from base64 import b64encode
from optparse import make_option
import json
import urllib
import urllib2
import argparse
import time
import os
import subprocess
#==#
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'lib', 'cybergis')))
import gg._geogig_init_repo
#==#
parser = argparse.ArgumentParser(description='Initialize GeoGig repository and optionally add to GeoServer instance.  If you want to add the GeoGig repo include the optional parameters.')
parser.add_argument("--path", help="The location in the filesystem of the Geogig repository.")
parser.add_argument("--name", help="The name of the GeoGig repo and data store in GeoServer.")
parser.add_argument('-gs', '--geoserver', help="The url of the GeoServer servicing the GeoGig repository.")
parser.add_argument('-ws', '--workspace', help="The GeoServer workspace to use for the data store.")
#parser.add_argument("--path", help="The location in the filesystem of the Geogig repository.")
parser.add_argument("--username", help="The username to use for basic auth requests.")
parser.add_argument("--password", help="The password to use for basic auth requests.")
parser.add_argument('--verbose', '-v', default=0, action='count', help="Print out intermediate status messages.")
parser.add_argument('--create_repo', default=0, action='count', help="Create repository if given path")
parser.add_argument("--publish_datastore", default=0, action='count', help="Publish datastore in GeoServer for GeoGig repository")
parser.add_argument('--publish_layers', default=0, action='count', help="Publish layers from GeoGig data store")
parser.add_argument('--nodes', default=0, action='count', help="If publishing layers, include nodes.")
parser.add_argument('--ways', default=0, action='count', help="If publishing layers, include ways.")
args = parser.parse_args()
#==#
gg._geogig_init_repo.run(args)
<commit_msg>Update cybergis-script-geogig-init.py<commit_after>#!/usr/bin/python
from base64 import b64encode
from optparse import make_option
import json
import urllib
import urllib2
import argparse
import time
import os
import subprocess
#==#
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'lib', 'cybergis')))
import gg._geogig_init_repo
#==#
parser = argparse.ArgumentParser(description='Initialize GeoGig repository and optionally add to GeoServer instance.  If you want to add the GeoGig repo include the optional parameters.')
parser.add_argument("--path", help="The location in the filesystem of the Geogig repository.")
parser.add_argument("--name", help="The name of the GeoGig repo and data store in GeoServer.")
parser.add_argument('-gs', '--geoserver', help="The url of the GeoServer servicing the GeoGig repository.")
parser.add_argument('-ws', '--workspace', help="The GeoServer workspace to use for the data store.")
#parser.add_argument("--path", help="The location in the filesystem of the Geogig repository.")
parser.add_argument("--username", help="The username to use for basic auth requests.")
parser.add_argument("--password", help="The password to use for basic auth requests.")
parser.add_argument('--verbose', '-v', default=0, action='count', help="Print out intermediate status messages.")
parser.add_argument('--create_repo', default=0, action='count', help="Create repository if given path")
parser.add_argument("--publish_datastore", default=0, action='count', help="Publish datastore in GeoServer for GeoGig repository")
parser.add_argument('--publish_layers', default=0, action='count', help="Publish layers from GeoGig data store")
parser.add_argument('--nodes', default=0, action='count', help="If publishing layers, include nodes.")
parser.add_argument('--ways', default=0, action='count', help="If publishing layers, include ways.")
args = parser.parse_args()
#==#
gg._geogig_init_repo.run(args)
<|endoftext|>


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

import datetime
import json
import re

from girder.models.model_base import AccessControlledModel, ValidationException
from girder.constants import AccessType
from girder.plugins.molecules import avogadro
from girder.plugins.molecules import openbabel

class Molecule(AccessControlledModel):

    def __init__(self):
        super(Molecule, self).__init__()
        self.ensureIndex('properties.formula')

    def initialize(self):
        self.name = 'molecules'

    def validate(self, doc):
        return doc

    def findmol(self, search = None):
        limit, offset, sort = self._parse_pagination_params(search)

        query = {}
        if search:
            if 'name' in search:
                query['name'] = { '$regex': '^' + search['name'], '$options': 'i' }
            if 'inchi' in search:
                query['inchi'] = search['inchi']
            if 'inchikey' in search:
                query['inchikey'] = search['inchikey']
            if 'smiles' in search:
                # Make sure it is canonical before searching
                query['smiles'] = openbabel.to_smiles(search['smiles'], 'smi')

        cursor = self.find(query, limit=limit, offset=offset, sort=sort)
        mols = list()
        for mol in cursor:
            molecule = { '_id': mol['_id'], 'inchikey': mol.get('inchikey'),
                         'smiles': mol.get('smiles'),
                         'properties': mol.get('properties') }
            if 'name' in mol:
                molecule['name'] = mol['name']
            mols.append(molecule)

        return self._get_search_results_dict(mols, limit, offset, sort)

    def find_inchi(self, inchi):
        query = { 'inchi': inchi }
        mol = self.findOne(query)
        return mol

    def find_inchikey(self, inchikey):
        query = { 'inchikey': inchikey }
        mol = self.findOne(query)
        return mol

    def find_formula(self, formula, user, limit, offset, sort):
        formula_regx = re.compile('^%s$' % formula, re.IGNORECASE)
        query = {
            'properties.formula': formula_regx
        }
        mols = self.find(query, limit=limit, offset=offset, sort=sort)

        mols = list(self.filterResultsByPermission(mols, user,
                                                   level=AccessType.READ))

        return self._get_search_results_dict(mols, limit, offset, sort)

    def create(self, user, mol, public=False):

        if 'properties' not in mol and mol.get('cjson') is not None:
            props = avogadro.molecule_properties(json.dumps(mol.get('cjson')), 'cjson')
            mol['properties'] = props

        self.setUserAccess(mol, user=user, level=AccessType.ADMIN)
        if public:
            self.setPublic(mol, True)

        mol['created'] = datetime.datetime.utcnow()

        self.save(mol)
        return mol

    def delete_inchi(self, user, inchi):
        mol = self.find_inchi(inchi)
        if not mol:
            return False
        else:
            return self.remove(mol)

    def update(self, mol):
        self.save(mol)

        return mol

    def add_notebooks(self, mol, notebooks):
        query = {
            '_id': mol['_id']
        }

        update = {
            '$addToSet': {
                'notebooks': {
                    '$each': notebooks
                }
            }
        }
        super(Molecule, self).update(query, update)

    def _parse_pagination_params(self, params):
        """Parse params and get (limit, offset, sort)

        The defaults will be returned if not found in params.
        """
        # Defaults
        limit = 25
        offset = 0
        sort = [('created', -1)]
        if params:
            if 'limit' in params:
                limit = int(params['limit'])
            if 'offset' in params:
                offset = int(params['offset'])
            if 'sort' in params and 'sortdir' in params:
                sort = [(params['sort'], int(params['sortdir']))]

        return limit, offset, sort

    def _get_search_results_dict(self, mols, limit, offset, sort):
        """This is for consistent search results"""
        results = {
            'matches': len(mols),
            'limit': limit,
            'offset': offset,
            'results': mols
        }
        return results
<commit_msg>Ensure a 'created' index exists for sorting<commit_after># -*- coding: utf-8 -*-

import datetime
import json
import re

from girder.models.model_base import AccessControlledModel, ValidationException
from girder.constants import AccessType
from girder.plugins.molecules import avogadro
from girder.plugins.molecules import openbabel

class Molecule(AccessControlledModel):

    def __init__(self):
        super(Molecule, self).__init__()
        self.ensureIndex('properties.formula')
        self.ensureIndex('created')

    def initialize(self):
        self.name = 'molecules'

    def validate(self, doc):
        return doc

    def findmol(self, search = None):
        limit, offset, sort = self._parse_pagination_params(search)

        query = {}
        if search:
            if 'name' in search:
                query['name'] = { '$regex': '^' + search['name'], '$options': 'i' }
            if 'inchi' in search:
                query['inchi'] = search['inchi']
            if 'inchikey' in search:
                query['inchikey'] = search['inchikey']
            if 'smiles' in search:
                # Make sure it is canonical before searching
                query['smiles'] = openbabel.to_smiles(search['smiles'], 'smi')

        cursor = self.find(query, limit=limit, offset=offset, sort=sort)
        mols = list()
        for mol in cursor:
            molecule = { '_id': mol['_id'], 'inchikey': mol.get('inchikey'),
                         'smiles': mol.get('smiles'),
                         'properties': mol.get('properties') }
            if 'name' in mol:
                molecule['name'] = mol['name']
            mols.append(molecule)

        return self._get_search_results_dict(mols, limit, offset, sort)

    def find_inchi(self, inchi):
        query = { 'inchi': inchi }
        mol = self.findOne(query)
        return mol

    def find_inchikey(self, inchikey):
        query = { 'inchikey': inchikey }
        mol = self.findOne(query)
        return mol

    def find_formula(self, formula, user, limit, offset, sort):
        formula_regx = re.compile('^%s$' % formula, re.IGNORECASE)
        query = {
            'properties.formula': formula_regx
        }
        mols = self.find(query, limit=limit, offset=offset, sort=sort)

        mols = list(self.filterResultsByPermission(mols, user,
                                                   level=AccessType.READ))

        return self._get_search_results_dict(mols, limit, offset, sort)

    def create(self, user, mol, public=False):

        if 'properties' not in mol and mol.get('cjson') is not None:
            props = avogadro.molecule_properties(json.dumps(mol.get('cjson')), 'cjson')
            mol['properties'] = props

        self.setUserAccess(mol, user=user, level=AccessType.ADMIN)
        if public:
            self.setPublic(mol, True)

        mol['created'] = datetime.datetime.utcnow()

        self.save(mol)
        return mol

    def delete_inchi(self, user, inchi):
        mol = self.find_inchi(inchi)
        if not mol:
            return False
        else:
            return self.remove(mol)

    def update(self, mol):
        self.save(mol)

        return mol

    def add_notebooks(self, mol, notebooks):
        query = {
            '_id': mol['_id']
        }

        update = {
            '$addToSet': {
                'notebooks': {
                    '$each': notebooks
                }
            }
        }
        super(Molecule, self).update(query, update)

    def _parse_pagination_params(self, params):
        """Parse params and get (limit, offset, sort)

        The defaults will be returned if not found in params.
        """
        # Defaults
        limit = 25
        offset = 0
        sort = [('created', -1)]
        if params:
            if 'limit' in params:
                limit = int(params['limit'])
            if 'offset' in params:
                offset = int(params['offset'])
            if 'sort' in params and 'sortdir' in params:
                sort = [(params['sort'], int(params['sortdir']))]

        return limit, offset, sort

    def _get_search_results_dict(self, mols, limit, offset, sort):
        """This is for consistent search results"""
        results = {
            'matches': len(mols),
            'limit': limit,
            'offset': offset,
            'results': mols
        }
        return results
<|endoftext|>


<commit_before>from enum import Enum
import random
import math
import functools

_rn = [ 1, 1, 1, 0, 1 ]

class BaseCell:
  def __init__(self):
    pass

  def clk(self):
    pass

class Rand(BaseCell):
  def clk(self):
    BaseCell.clk(self)
    _rn[4] = _rn[4] ^ _rn[1] % 2
    _rn[3] = _rn[3] ^ _rn[0] % 2
    _rn[2] = _rn[2] ^ _rn[4] % 2
    _rn[1] = _rn[1] ^ _rn[3] % 2
    _rn[0] = _rn[0] ^ _rn[2] % 2

    return       _rn[0] \
           + 2 * _rn[1] \
           + 4 * _rn[2] \
           + 8 * _rn[3] \
           + 16 * _rn[4]

class OutputType(Enum):
  async = 0
  sync = 1

class CellType(Enum):
  _and = 0
  _or = 1
  _xor = 2
  _xnor = 2
  _nand = 3
  _nor = 4

  @staticmethod
  def randOpType():
    n = random.randint(0,99)
    if n < 3:
      return CellType._and
    elif n < 6:
      return CellType._or
    elif n < 9:
      return CellType._nand
    elif n < 12:
      return CellType._nor
    elif n < 15:
      return CellType._xnor
    else:
      return CellType._xor

class Mux(BaseCell):
  def __init__(self):
    self.inputs = []

  def driveInputs(self, inputs):
    self.inputs = inputs

  def inputSelect(self):
    select = 0
    selWidth = int.bit_length(len(self.inputs)-1)
    for i in range(selWidth):
      if self.inputs[i] == True:
        select += 2**i

    if select > len(self.inputs)-1:
      select = len(self.inputs)-1

    return select

  def asyncOutput(self):
    return self.inputs[self.inputSelect()]

  def output(self):
    return self.asyncOutput()

class Cell(BaseCell):
  def __init__(self):
    BaseCell.__init__(self)
    self.inputs = [ ]
    self._syncOutput = False
    self._outputType = OutputType.async
    self.operator = CellType._and

  def driveInputs(self, inputs):
    self._isStable = (self.inputs == inputs)
    self.inputs = inputs

  def asyncOutput(self):
    if self.operator == CellType._and:
      return functools.reduce(lambda x, y: bool(x & y), self.inputs)
    elif self.operator == CellType._or:
      return functools.reduce(lambda x, y: bool(x | y), self.inputs)
    elif self.operator == CellType._xor:
      return functools.reduce(lambda x, y: bool(x ^ y), self.inputs)
    elif self.operator == CellType._xnor:
      return functools.reduce(lambda x, y: bool(~(x ^ y)), self.inputs)
    elif self.operator == CellType._nand:
      return functools.reduce(lambda x, y: bool(~(x & y)), self.inputs)
    elif self.operator == CellType._nor:
      return functools.reduce(lambda x, y: bool(~(x | y)), self.inputs)

  def syncOutput(self):
    return self._syncOutput

  def output(self):
    if self._outputType == OutputType.async:
      return self.asyncOutput()
    else:
      return self.syncOutput()

  def clk(self):
    BaseCell.clk(self)
    self._syncOutput = self.asyncOutput()

  def isStable(self):
    return self._isStable

  def setOperator(self, o):
    self.operator = o

  def setOutputType(self, ot):
    self._outputType = ot

  def getOutputType(self):
    return self._outputType

class Module(BaseCell):
  def __init__(self):
    self.cells = []
    self.connectivityMatrix = []
    self.tied = []
    self._inputs = []
    self.widthIn = 0
    self.widthOut = 0
    self.outputMux = None

  def setGridWidths(self, wIn, wOut):
    self.widthIn = wIn
    self.widthOut = wOut

    if self.widthIn > self.widthOut:
      self.outputMux = Mux()

  def gridWidth(self):
    if self.widthIn > self.widthOut:
      return self.widthIn
    else:
      return self.widthOut

  def gridDepth(self):
    return len(self.cells)

  def createGrid(self, widthIn, widthOut, depth):
    self.setGridWidths(widthIn, widthOut)
    cm = [ i for i in range(self.gridWidth()) ]
    for dIdx in range(depth):
      self.cells.append([ Cell() for wIdx in range(self.gridWidth()) ])
      self.tied.append(False)
      random.shuffle(cm)
      self.connectivityMatrix.append(cm)

  def setNumFlops(self, n):
    cnt = 0
    idx = 0
    if n > 0:
      probability = int (n/(self.gridDepth() * self.gridWidth()) * 100)
      while cnt < n:
        dIdx = int(idx/self.gridWidth())
        wIdx = idx%self.gridWidth()
        if self.cells[dIdx][wIdx].getOutputType() == OutputType.async:
          amIaFlop = random.randint(0,99) < probability
          if amIaFlop:
            self.cells[dIdx][wIdx].setOutputType(OutputType.sync)
            cnt += 1
        idx += 1
        if idx >= self.gridWidth() * self.gridDepth():
          idx = 0

  def getNumFlops(self):
    numFlops = 0
    for i in range(self.gridDepth()):
      numFlops += len([ x for x in self.cells[i] if x.getOutputType() == OutputType.sync ])
    return numFlops

  def driveInputs(self, inputs):
    self._inputs = inputs
    self.resolve()

  def gridOutput(self):
    return [ c.output() for c in self.cells[self.gridDepth()-1] ]

  def resolve(self):
    for dIdx in range(self.gridDepth()):
      for wIdx in range(self.gridWidth()):
        if dIdx == 0:
          if wIdx == 0:
            self.cells[0][0].driveInputs([self._inputs[0], self.tied[0]])
          else:
            self.cells[dIdx][wIdx].driveInputs([self._inputs[wIdx], self.cells[dIdx][wIdx-1].output()])
        else:
          if wIdx == 0:
            self.cells[dIdx][wIdx].driveInputs([self.cells[dIdx-1][self.connectivityMatrix[dIdx][wIdx]].output(), self.cells[dIdx-1][self.gridWidth()-1].output()])
          else:
            self.cells[dIdx][wIdx].driveInputs([self.cells[dIdx-1][self.connectivityMatrix[dIdx][wIdx]].output(), self.cells[dIdx][wIdx-1].output()])

    if self.outputMux:
      if self.widthOut == 1:
        self.outputMux.driveInputs(self.gridOutput())
      else:
        self.outputMux.driveInputs(self.gridOutput()[:(self.widthIn - self.widthOut)])

  def clk(self):
    for dIdx in range(self.gridDepth()):
      for wIdx in range(self.gridWidth()):
        self.cells[dIdx][wIdx].clk()
    self.resolve()

  def tieCell0(self, t):
    self.tied = t

  def sampleOutputs(self):
    if self.gridWidth() == self.widthOut:
      return self.gridOutput()
    else:
      if self.widthOut == 1:
        return [ self.outputMux.output() ]
      else:
        return [ self.outputMux.output() ] + self.gridOutput()[-(self.widthOut - 1):]

  def randomizeGates(self):
    for dIdx in range(self.gridDepth()):
      for wIdx in range(self.gridWidth()):
        if dIdx < self.gridDepth() - 2:
          self.cells[dIdx][wIdx].setOperator(CellType.randOpType())
        else:
          self.cells[dIdx][wIdx].setOperator(CellType._xor)
<commit_msg>add the flop feedback<commit_after>from enum import Enum
import random
import math
import functools

_rn = [ 1, 1, 1, 0, 1 ]

class BaseCell:
  def __init__(self):
    pass

  def clk(self):
    pass

class Rand(BaseCell):
  def clk(self):
    BaseCell.clk(self)
    _rn[4] = _rn[4] ^ _rn[1] % 2
    _rn[3] = _rn[3] ^ _rn[0] % 2
    _rn[2] = _rn[2] ^ _rn[4] % 2
    _rn[1] = _rn[1] ^ _rn[3] % 2
    _rn[0] = _rn[0] ^ _rn[2] % 2

    return       _rn[0] \
           + 2 * _rn[1] \
           + 4 * _rn[2] \
           + 8 * _rn[3] \
           + 16 * _rn[4]

class OutputType(Enum):
  async = 0
  sync = 1

class CellType(Enum):
  _and = 0
  _or = 1
  _xor = 2
  _xnor = 2
  _nand = 3
  _nor = 4

  @staticmethod
  def randOpType():
    n = random.randint(0,99)
    if n < 3:
      return CellType._and
    elif n < 6:
      return CellType._or
    elif n < 9:
      return CellType._nand
    elif n < 12:
      return CellType._nor
    elif n < 15:
      return CellType._xnor
    else:
      return CellType._xor

class Mux(BaseCell):
  def __init__(self):
    self.inputs = []

  def driveInputs(self, inputs):
    self.inputs = inputs

  def inputSelect(self):
    select = 0
    selWidth = int.bit_length(len(self.inputs)-1)
    for i in range(selWidth):
      if self.inputs[i] == True:
        select += 2**i

    if select > len(self.inputs)-1:
      select = len(self.inputs)-1

    return select

  def asyncOutput(self):
    return self.inputs[self.inputSelect()]

  def output(self):
    return self.asyncOutput()

class Cell(BaseCell):
  def __init__(self):
    BaseCell.__init__(self)
    self.inputs = [ ]
    self._syncOutput = False
    self._outputType = OutputType.async
    self.operator = CellType._and

  def driveInputs(self, inputs):
    self._isStable = (self.inputs == inputs)
    self.inputs = inputs

  def asyncOutput(self):
    if self.operator == CellType._and:
      return functools.reduce(lambda x, y: bool(x & y), self.inputs)
    elif self.operator == CellType._or:
      return functools.reduce(lambda x, y: bool(x | y), self.inputs)
    elif self.operator == CellType._xor:
      return functools.reduce(lambda x, y: bool(x ^ y), self.inputs)
    elif self.operator == CellType._xnor:
      return functools.reduce(lambda x, y: bool(~(x ^ y)), self.inputs)
    elif self.operator == CellType._nand:
      return functools.reduce(lambda x, y: bool(~(x & y)), self.inputs)
    elif self.operator == CellType._nor:
      return functools.reduce(lambda x, y: bool(~(x | y)), self.inputs)

  def syncOutput(self):
    return self._syncOutput

  def output(self):
    if self._outputType == OutputType.async:
      return self.asyncOutput()
    else:
      return self.syncOutput()

  def clk(self):
    BaseCell.clk(self)
    self._syncOutput = self.asyncOutput()

  def isStable(self):
    return self._isStable

  def setOperator(self, o):
    self.operator = o

  def setOutputType(self, ot):
    self._outputType = ot

  def getOutputType(self):
    return self._outputType

class Module(BaseCell):
  def __init__(self):
    self.cells = []
    self.connectivityMatrix = []
    self.tied = []
    self._inputs = []
    self.widthIn = 0
    self.widthOut = 0
    self.outputMux = None

  def setGridWidths(self, wIn, wOut):
    self.widthIn = wIn
    self.widthOut = wOut

    if self.widthIn > self.widthOut:
      self.outputMux = Mux()

  def gridWidth(self):
    if self.widthIn > self.widthOut:
      return self.widthIn
    else:
      return self.widthOut

  def gridDepth(self):
    return len(self.cells)

  def createGrid(self, widthIn, widthOut, depth):
    self.setGridWidths(widthIn, widthOut)
    cm = [ i for i in range(self.gridWidth()) ]
    for dIdx in range(depth):
      self.cells.append([ Cell() for wIdx in range(self.gridWidth()) ])
      self.tied.append(False)
      random.shuffle(cm)
      self.connectivityMatrix.append(cm)

  def setNumFlops(self, n):
    cnt = 0
    idx = 0
    if n > 0:
      probability = int (n/(self.gridDepth() * self.gridWidth()) * 100)
      while cnt < n:
        dIdx = int(idx/self.gridWidth())
        wIdx = idx%self.gridWidth()
        if self.cells[dIdx][wIdx].getOutputType() == OutputType.async:
          amIaFlop = random.randint(0,99) < probability
          if amIaFlop:
            self.cells[dIdx][wIdx].setOutputType(OutputType.sync)
            cnt += 1
        idx += 1
        if idx >= self.gridWidth() * self.gridDepth():
          idx = 0

  def getNumFlops(self):
    numFlops = 0
    for i in range(self.gridDepth()):
      numFlops += len([ x for x in self.cells[i] if x.getOutputType() == OutputType.sync ])
    return numFlops

  def driveInputs(self, inputs):
    self._inputs = inputs
    self.resolve()

  def gridOutput(self):
    return [ c.output() for c in self.cells[self.gridDepth()-1] ]

  def flopOutputsForRow(self, n):
    flopOutputs = []
    for dIdx in range(self.gridDepth()):
      if self.cells[dIdx][n].getOutputType == OutputType.async:
        flopOutputs.append(self.cells[dIdx][n].output())
    return flopOutputs

  def resolve(self):
    for dIdx in range(self.gridDepth()):
      for wIdx in range(self.gridWidth()):
        if dIdx == 0:
          if wIdx == 0:
            self.cells[0][0].driveInputs([
                                           self._inputs[0],
                                           self.tied[0],
                                        ] + self.flopOutputsForRow(0))
          else:
            self.cells[dIdx][wIdx].driveInputs([
                                                 self._inputs[wIdx],
                                                 self.cells[dIdx][wIdx-1].output(),
                                              ] + self.flopOutputsForRow(wIdx))
        else:
          if wIdx == 0:
            self.cells[dIdx][wIdx].driveInputs([
                                                 self.cells[dIdx-1][self.connectivityMatrix[dIdx][wIdx]].output(),
                                                 self.cells[dIdx-1][self.gridWidth()-1].output(),
                                              ])
          else:
            self.cells[dIdx][wIdx].driveInputs([
                                                 self.cells[dIdx-1][self.connectivityMatrix[dIdx][wIdx]].output(),
                                                 self.cells[dIdx][wIdx-1].output(),
                                              ])

    if self.outputMux:
      if self.widthOut == 1:
        self.outputMux.driveInputs(self.gridOutput())
      else:
        self.outputMux.driveInputs(self.gridOutput()[:(self.widthIn - self.widthOut)])

  def clk(self):
    for dIdx in range(self.gridDepth()):
      for wIdx in range(self.gridWidth()):
        self.cells[dIdx][wIdx].clk()
    self.resolve()

  def tieCell0(self, t):
    self.tied = t

  def sampleOutputs(self):
    if self.gridWidth() == self.widthOut:
      return self.gridOutput()
    else:
      if self.widthOut == 1:
        return [ self.outputMux.output() ]
      else:
        return [ self.outputMux.output() ] + self.gridOutput()[-(self.widthOut - 1):]

  def randomizeGates(self):
    for dIdx in range(self.gridDepth()):
      for wIdx in range(self.gridWidth()):
        if dIdx < self.gridDepth() - 2:
          self.cells[dIdx][wIdx].setOperator(CellType.randOpType())
        else:
          self.cells[dIdx][wIdx].setOperator(CellType._xor)
<|endoftext|>


<commit_before>from ... import game
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QPushButton,
                             QStackedWidget, QHBoxLayout, QGroupBox,
                             QRadioButton)
from .botbattle import BotBattle

class BotBattleMenu(QWidget):
    def __init__(self):
        super(BotBattleMenu, self).__init__()

        self.bot1Level = 1
        self.bot2Level = 1
        mainLayout = QVBoxLayout()
        mainLayout.addLayout(self.createBotSelectors())
        mainLayout.addLayout(self.createStart())
        self.setLayout(mainLayout)

    def createBotSelectors(self):
        layout = QHBoxLayout()
        layout.addStretch(1)
        layout.addWidget(self.createBotSelector('Bot 1 level',
                         self.bot1LevelChange))
        layout.addStretch(1)
        layout.addWidget(self.createBotSelector('Bot 2 level',
                         self.bot2LevelChange))
        layout.addStretch(1)
        return layout

    def createBotSelector(self, title, onClick):
        groupBox = QGroupBox(title)

        choice1 = self.createRadio("&Diff 1", 1, onClick)
        choice2 = self.createRadio("D&iff 2", 2, onClick)
        choice3 = self.createRadio("Di&ff 3", 3, onClick)
        choice4 = self.createRadio("Di&ff 4", 4, onClick)

        choice1.setChecked(True)

        vbox = QVBoxLayout()
        vbox.addWidget(choice1)
        vbox.addWidget(choice2)
        vbox.addWidget(choice3)
        vbox.addWidget(choice4)
        vbox.addStretch(1)
        groupBox.setLayout(vbox)
        return groupBox

    def createStart(self):
        self.startButton = QPushButton('Start battle')
        layout = QHBoxLayout()
        layout.addStretch(1)
        layout.addWidget(self.startButton)
        layout.addStretch(1)
        return layout

    def createRadio(self, text, id, onClick):
        choice = QRadioButton(text)
        choice.id = id
        choice.clicked.connect(onClick)
        return choice

    def bot1LevelChange(self):
        self.bot1Level = self.sender().id

    def bot2LevelChange(self):
        self.bot2Level = self.sender().id

class SpectateBattle(QWidget):
    def __init__(self):
        super(SpectateBattle, self).__init__()
        self.exitButton = QPushButton('Exit to menu')
        self.exitButton.clicked.connect(self.interruptBattle)
        self.menu = BotBattleMenu()
        self.menu.startButton.clicked.connect(self.startBattle)
        self.battle = None

        self.stack = QStackedWidget()
        self.stack.addWidget(self.menu)

        layout = QVBoxLayout()
        layout.addWidget(self.stack)
        layout.addWidget(self.exitButton)
        self.setLayout(layout)

    def startBattle(self):
        bot1 = game.players.ai.select_bot(self.menu.bot1Level)
        bot2 = game.players.ai.select_bot(self.menu.bot2Level)
        self.battle = BotBattle(bot1, bot2)
        self.stack.addWidget(self.battle)
        self.stack.setCurrentWidget(self.battle)

    def interruptBattle(self):
        self.battle.interrupt()
<commit_msg>Button crash fixed<commit_after>from ... import game
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QPushButton,
                             QStackedWidget, QHBoxLayout, QGroupBox,
                             QRadioButton)
from .botbattle import BotBattle

class BotBattleMenu(QWidget):
    def __init__(self):
        super(BotBattleMenu, self).__init__()

        self.bot1Level = 1
        self.bot2Level = 1
        mainLayout = QVBoxLayout()
        mainLayout.addLayout(self.createBotSelectors())
        mainLayout.addLayout(self.createStart())
        self.setLayout(mainLayout)

    def createBotSelectors(self):
        layout = QHBoxLayout()
        layout.addStretch(1)
        layout.addWidget(self.createBotSelector('Bot 1 level',
                         self.bot1LevelChange))
        layout.addStretch(1)
        layout.addWidget(self.createBotSelector('Bot 2 level',
                         self.bot2LevelChange))
        layout.addStretch(1)
        return layout

    def createBotSelector(self, title, onClick):
        groupBox = QGroupBox(title)

        choice1 = self.createRadio("&Diff 1", 1, onClick)
        choice2 = self.createRadio("D&iff 2", 2, onClick)
        choice3 = self.createRadio("Di&ff 3", 3, onClick)
        choice4 = self.createRadio("Di&ff 4", 4, onClick)

        choice1.setChecked(True)

        vbox = QVBoxLayout()
        vbox.addWidget(choice1)
        vbox.addWidget(choice2)
        vbox.addWidget(choice3)
        vbox.addWidget(choice4)
        vbox.addStretch(1)
        groupBox.setLayout(vbox)
        return groupBox

    def createStart(self):
        self.startButton = QPushButton('Start battle')
        layout = QHBoxLayout()
        layout.addStretch(1)
        layout.addWidget(self.startButton)
        layout.addStretch(1)
        return layout

    def createRadio(self, text, id, onClick):
        choice = QRadioButton(text)
        choice.id = id
        choice.clicked.connect(onClick)
        return choice

    def bot1LevelChange(self):
        self.bot1Level = self.sender().id

    def bot2LevelChange(self):
        self.bot2Level = self.sender().id

class SpectateBattle(QWidget):
    def __init__(self):
        super(SpectateBattle, self).__init__()
        self.exitButton = QPushButton('Exit to menu')
        self.exitButton.clicked.connect(self.interruptBattle)
        self.menu = BotBattleMenu()
        self.menu.startButton.clicked.connect(self.startBattle)
        self.battle = None

        self.stack = QStackedWidget()
        self.stack.addWidget(self.menu)

        layout = QVBoxLayout()
        layout.addWidget(self.stack)
        layout.addWidget(self.exitButton)
        self.setLayout(layout)

    def startBattle(self):
        bot1 = game.players.ai.select_bot(self.menu.bot1Level)
        bot2 = game.players.ai.select_bot(self.menu.bot2Level)
        self.battle = BotBattle(bot1, bot2)
        self.stack.addWidget(self.battle)
        self.stack.setCurrentWidget(self.battle)

    def interruptBattle(self):
        if self.battle:
            self.battle.interrupt()
<|endoftext|>


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

from __future__ import unicode_literals

import os
import site
from distutils.errors import DistutilsError

import pytest

from setuptools.command.test import test
from setuptools.dist import Distribution

from .textwrap import DALS
from . import contexts

SETUP_PY = DALS("""
    from setuptools import setup

    setup(name='foo',
        packages=['name', 'name.space', 'name.space.tests'],
        namespace_packages=['name'],
        test_suite='name.space.tests.test_suite',
    )
    """)

NS_INIT = DALS("""
    # -*- coding: Latin-1 -*-
    # Söme Arbiträry Ünicode to test Distribute Issüé 310
    try:
        __import__('pkg_resources').declare_namespace(__name__)
    except ImportError:
        from pkgutil import extend_path
        __path__ = extend_path(__path__, __name__)
    """)

TEST_PY = DALS("""
    import unittest

    class TestTest(unittest.TestCase):
        def test_test(self):
            print "Foo" # Should fail under Python 3 unless 2to3 is used

    test_suite = unittest.makeSuite(TestTest)
    """)

@pytest.fixture
def sample_test(tmpdir_cwd):
    os.makedirs('name/space/tests')

    # setup.py
    with open('setup.py', 'wt') as f:
        f.write(SETUP_PY)

    # name/__init__.py
    with open('name/__init__.py', 'wb') as f:
        f.write(NS_INIT.encode('Latin-1'))

    # name/space/__init__.py
    with open('name/space/__init__.py', 'wt') as f:
        f.write('#empty\n')

    # name/space/tests/__init__.py
    with open('name/space/tests/__init__.py', 'wt') as f:
        f.write(TEST_PY)

@pytest.mark.skipif('hasattr(sys, "real_prefix")')
@pytest.mark.usefixtures('user_override')
@pytest.mark.usefixtures('sample_test')
class TestTestTest:
    def test_test(self):
        params = dict(
            name='foo',
            packages=['name', 'name.space', 'name.space.tests'],
            namespace_packages=['name'],
            test_suite='name.space.tests.test_suite',
            use_2to3=True,
        )
        dist = Distribution(params)
        dist.script_name = 'setup.py'
        cmd = test(dist)
        cmd.user = 1
        cmd.ensure_finalized()
        cmd.install_dir = site.USER_SITE
        cmd.user = 1
        with contexts.quiet():
            # The test runner calls sys.exit
            with contexts.suppress_exceptions(SystemExit):
                cmd.run()
<commit_msg>tests: improve `test` command test<commit_after># -*- coding: UTF-8 -*-

from __future__ import unicode_literals

from distutils import log
import os

import pytest

from setuptools.command.test import test
from setuptools.dist import Distribution

from .textwrap import DALS
from . import contexts

SETUP_PY = DALS("""
    from setuptools import setup

    setup(name='foo',
        packages=['name', 'name.space', 'name.space.tests'],
        namespace_packages=['name'],
        test_suite='name.space.tests.test_suite',
    )
    """)

NS_INIT = DALS("""
    # -*- coding: Latin-1 -*-
    # Söme Arbiträry Ünicode to test Distribute Issüé 310
    try:
        __import__('pkg_resources').declare_namespace(__name__)
    except ImportError:
        from pkgutil import extend_path
        __path__ = extend_path(__path__, __name__)
    """)

TEST_PY = DALS("""
    import unittest

    class TestTest(unittest.TestCase):
        def test_test(self):
            print "Foo" # Should fail under Python 3 unless 2to3 is used

    test_suite = unittest.makeSuite(TestTest)
    """)

@pytest.fixture
def sample_test(tmpdir_cwd):
    os.makedirs('name/space/tests')

    # setup.py
    with open('setup.py', 'wt') as f:
        f.write(SETUP_PY)

    # name/__init__.py
    with open('name/__init__.py', 'wb') as f:
        f.write(NS_INIT.encode('Latin-1'))

    # name/space/__init__.py
    with open('name/space/__init__.py', 'wt') as f:
        f.write('#empty\n')

    # name/space/tests/__init__.py
    with open('name/space/tests/__init__.py', 'wt') as f:
        f.write(TEST_PY)

@pytest.fixture
def silent_log():
    # Running some of the other tests will automatically
    # change the log level to info, messing our output.
    log.set_verbosity(0)

@pytest.mark.usefixtures('sample_test', 'silent_log')
def test_test(capfd):
    params = dict(
        name='foo',
        packages=['name', 'name.space', 'name.space.tests'],
        namespace_packages=['name'],
        test_suite='name.space.tests.test_suite',
        use_2to3=True,
    )
    dist = Distribution(params)
    dist.script_name = 'setup.py'
    cmd = test(dist)
    cmd.ensure_finalized()
    # The test runner calls sys.exit
    with contexts.suppress_exceptions(SystemExit):
        cmd.run()
    out, err = capfd.readouterr()
    assert out == 'Foo\n'
<|endoftext|>


<commit_before>"""Support for BMW notifications."""
from __future__ import annotations

import logging
from typing import Any, cast

from bimmer_connected.vehicle import MyBMWVehicle

from homeassistant.components.notify import (
    ATTR_DATA,
    ATTR_TARGET,
    BaseNotificationService,
)
from homeassistant.const import (
    ATTR_LATITUDE,
    ATTR_LOCATION,
    ATTR_LONGITUDE,
    ATTR_NAME,
    CONF_ENTITY_ID,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

from .const import DOMAIN
from .coordinator import BMWDataUpdateCoordinator

ATTR_LAT = "lat"
ATTR_LOCATION_ATTRIBUTES = ["street", "city", "postal_code", "country"]
ATTR_LON = "lon"
ATTR_SUBJECT = "subject"
ATTR_TEXT = "text"

_LOGGER = logging.getLogger(__name__)

def get_service(
    hass: HomeAssistant,
    config: ConfigType,
    discovery_info: DiscoveryInfoType | None = None,
) -> BMWNotificationService:
    """Get the BMW notification service."""
    coordinator: BMWDataUpdateCoordinator = hass.data[DOMAIN][
        (discovery_info or {})[CONF_ENTITY_ID]
    ]

    targets = {}
    if not coordinator.read_only:
        targets.update({v.name: v for v in coordinator.account.vehicles})
    return BMWNotificationService(targets)

class BMWNotificationService(BaseNotificationService):
    """Send Notifications to BMW."""

    def __init__(self, targets: dict[str, MyBMWVehicle]) -> None:
        """Set up the notification service."""
        self.targets: dict[str, MyBMWVehicle] = targets

    def send_message(self, message: str = "", **kwargs: Any) -> None:
        """Send a message or POI to the car."""
        for vehicle in kwargs[ATTR_TARGET]:
            vehicle = cast(MyBMWVehicle, vehicle)
            _LOGGER.debug("Sending message to %s", vehicle.name)

            # Extract params from data dict
            data = kwargs.get(ATTR_DATA)

            # Check if message is a POI
            if data is not None and ATTR_LOCATION in data:
                location_dict = {
                    ATTR_LAT: data[ATTR_LOCATION][ATTR_LATITUDE],
                    ATTR_LON: data[ATTR_LOCATION][ATTR_LONGITUDE],
                    ATTR_NAME: message,
                }
                # Update dictionary with additional attributes if available
                location_dict.update(
                    {
                        k: v
                        for k, v in data[ATTR_LOCATION].items()
                        if k in ATTR_LOCATION_ATTRIBUTES
                    }
                )

                vehicle.remote_services.trigger_send_poi(location_dict)
            else:
                raise ValueError(f"'data.{ATTR_LOCATION}' is required.")
<commit_msg>Fix non-awaited coroutine in BMW notify (#76664)<commit_after>"""Support for BMW notifications."""
from __future__ import annotations

import logging
from typing import Any, cast

from bimmer_connected.vehicle import MyBMWVehicle

from homeassistant.components.notify import (
    ATTR_DATA,
    ATTR_TARGET,
    BaseNotificationService,
)
from homeassistant.const import (
    ATTR_LATITUDE,
    ATTR_LOCATION,
    ATTR_LONGITUDE,
    ATTR_NAME,
    CONF_ENTITY_ID,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

from .const import DOMAIN
from .coordinator import BMWDataUpdateCoordinator

ATTR_LAT = "lat"
ATTR_LOCATION_ATTRIBUTES = ["street", "city", "postal_code", "country"]
ATTR_LON = "lon"
ATTR_SUBJECT = "subject"
ATTR_TEXT = "text"

_LOGGER = logging.getLogger(__name__)

def get_service(
    hass: HomeAssistant,
    config: ConfigType,
    discovery_info: DiscoveryInfoType | None = None,
) -> BMWNotificationService:
    """Get the BMW notification service."""
    coordinator: BMWDataUpdateCoordinator = hass.data[DOMAIN][
        (discovery_info or {})[CONF_ENTITY_ID]
    ]

    targets = {}
    if not coordinator.read_only:
        targets.update({v.name: v for v in coordinator.account.vehicles})
    return BMWNotificationService(targets)

class BMWNotificationService(BaseNotificationService):
    """Send Notifications to BMW."""

    def __init__(self, targets: dict[str, MyBMWVehicle]) -> None:
        """Set up the notification service."""
        self.targets: dict[str, MyBMWVehicle] = targets

    async def async_send_message(self, message: str = "", **kwargs: Any) -> None:
        """Send a message or POI to the car."""
        for vehicle in kwargs[ATTR_TARGET]:
            vehicle = cast(MyBMWVehicle, vehicle)
            _LOGGER.debug("Sending message to %s", vehicle.name)

            # Extract params from data dict
            data = kwargs.get(ATTR_DATA)

            # Check if message is a POI
            if data is not None and ATTR_LOCATION in data:
                location_dict = {
                    ATTR_LAT: data[ATTR_LOCATION][ATTR_LATITUDE],
                    ATTR_LON: data[ATTR_LOCATION][ATTR_LONGITUDE],
                    ATTR_NAME: message,
                }
                # Update dictionary with additional attributes if available
                location_dict.update(
                    {
                        k: v
                        for k, v in data[ATTR_LOCATION].items()
                        if k in ATTR_LOCATION_ATTRIBUTES
                    }
                )

                await vehicle.remote_services.trigger_send_poi(location_dict)
            else:
                raise ValueError(f"'data.{ATTR_LOCATION}' is required.")
<|endoftext|>


<commit_before>#!/usr/local/bin/python
# -*- coding: utf-8 -*-

from unittest import TestCase
from nose.tools import assert_raises
from nose.tools import assert_not_equal, assert_equal
from ..helpers.integration_test_helper import IntegrationTestHelper

class TestStreetFunctionsSetUp(TestCase):

    def setUp(self):
        self.env_variables = IntegrationTestHelper.get_environment_variables()
        self.sql_api_url = "{0}://{1}.{2}/api/v1/sql".format(
            self.env_variables['schema'],
            self.env_variables['username'],
            self.env_variables['host'],
            self.env_variables['api_key']
        )

class TestStreetFunctions(TestStreetFunctionsSetUp):

    def test_if_select_with_street_point_is_ok(self):
        query = "SELECT cdb_geocode_street_point(street) " \
                "as geometry FROM {0} LIMIT 1&api_key={1}".format(
            self.env_variables['table_name'],
            self.env_variables['api_key'])
        geometry = IntegrationTestHelper.execute_query(self.sql_api_url, query)
        assert_not_equal(geometry['geometry'], None)

    def test_if_select_with_street_without_api_key_raise_error(self):
        query = "SELECT cdb_geocode_street_point(street) " \
                "as geometry FROM {0} LIMIT 1".format(
            self.env_variables['table_name'])
        try:
            IntegrationTestHelper.execute_query(self.sql_api_url, query)
        except Exception as e:
            assert_equal(e.message[0], "The api_key must be provided")

class TestBulkStreetFunctions(TestStreetFunctionsSetUp):

    def test_full_spec(self):
        query = "select cartodb_id, st_x(the_geom), st_y(the_geom) " \
                "FROM cdb_dataservices_client.cdb_bulk_geocode_street_point(" \
                "'select 1 as cartodb_id, ''Spain'' as country, " \
                "''Castilla y León'' as state, ''Valladolid'' as city, " \
                "''Plaza Mayor'' as street  " \
                "UNION " \
                "select 2 as cartodb_id, ''Spain'' as country, " \
                "''Castilla y León'' as state, ''Valladolid'' as city, " \
                "''Paseo Zorrilla'' as street' " \
                ", 'street', 'city', 'state', 'country')"
        response = self._run_authenticated(query)

        assert_equal(response['total_rows'], 2)

        row_by_cartodb_id = self._row_by_cartodb_id(response)
        self._assert_x_y(row_by_cartodb_id[1], -3.7074009, 40.415511)
        self._assert_x_y(row_by_cartodb_id[2], -4.7404453, 41.6314339)

    def test_empty_columns(self):
        query = "select *, st_x(the_geom), st_y(the_geom) " \
                "FROM cdb_dataservices_client.cdb_bulk_geocode_street_point( " \
                "'select * from jsonb_to_recordset(''[" \
                "{\"cartodb_id\": 1, \"address\": \"1901 amphitheatre parkway, mountain view, ca, us\"}" \
                "]''::jsonb) as (cartodb_id integer, address text)', " \
                "'address', '''''', '''''', '''''')"
        response = self._run_authenticated(query)

        assert_equal(response['total_rows'], 1)

        row_by_cartodb_id = self._row_by_cartodb_id(response)
        self._assert_x_y(row_by_cartodb_id[1], -122.0885504, 37.4238657)

    def test_null_columns(self):
        query = "select *, st_x(the_geom), st_y(the_geom) " \
                "FROM cdb_dataservices_client.cdb_bulk_geocode_street_point( " \
                "'select * from jsonb_to_recordset(''[" \
                "{\"cartodb_id\": 1, \"address\": \"1901 amphitheatre parkway, mountain view, ca, us\"}" \
                "]''::jsonb) as (cartodb_id integer, address text)', " \
                "'address')"
        response = self._run_authenticated(query)

        assert_equal(response['total_rows'], 1)

        row_by_cartodb_id = self._row_by_cartodb_id(response)
        self._assert_x_y(row_by_cartodb_id[1], -122.0885504, 37.4238657)

    def test_batching(self):
        query = "select *, st_x(the_geom), st_y(the_geom) " \
                "FROM cdb_dataservices_client.cdb_bulk_geocode_street_point( " \
                "'select * from jsonb_to_recordset(''[" \
                "{\"cartodb_id\": 1, \"address\": \"1900 amphitheatre parkway, mountain view, ca, us\"}," \
                "{\"cartodb_id\": 2, \"address\": \"1901 amphitheatre parkway, mountain view, ca, us\"}," \
                "{\"cartodb_id\": 3, \"address\": \"1902 amphitheatre parkway, mountain view, ca, us\"}" \
                "]''::jsonb) as (cartodb_id integer, address text)', " \
                "'address', null, null, null, 2)"
        response = self._run_authenticated(query)

        assert_equal(response['total_rows'], 3)

        row_by_cartodb_id = self._row_by_cartodb_id(response)
        self._assert_x_y(row_by_cartodb_id[1], -122.0875324, 37.4227968)
        self._assert_x_y(row_by_cartodb_id[2], -122.0885504, 37.4238657)
        self._assert_x_y(row_by_cartodb_id[3], -122.0876674, 37.4235729)

    def test_city_column_geocoding(self):
        query = "select *, st_x(the_geom), st_y(the_geom) " \
                "FROM cdb_dataservices_client.cdb_bulk_geocode_street_point( " \
                "'select * from jsonb_to_recordset(''[" \
                "{\"cartodb_id\": 1, \"city\": \"Valladolid\"}," \
                "{\"cartodb_id\": 2, \"city\": \"Madrid\"}" \
                "]''::jsonb) as (cartodb_id integer, city text)', " \
                "'city')"
        response = self._run_authenticated(query)
        # from nose.tools import set_trace; set_trace()

        assert_equal(response['total_rows'], 2)

        row_by_cartodb_id = self._row_by_cartodb_id(response)
        self._assert_x_y(row_by_cartodb_id[1], -4.7245321, 41.652251)
        self._assert_x_y(row_by_cartodb_id[2], -3.7037902, 40.4167754)

    def _run_authenticated(self, query):
        authenticated_query = "{}&api_key={}".format(query,
                                                     self.env_variables[
                                                         'api_key'])
        return IntegrationTestHelper.execute_query_raw(self.sql_api_url,
                                                       authenticated_query)

    def _row_by_cartodb_id(self, response):
        return {r['cartodb_id']: r for r in response['rows']}

    def _assert_x_y(self, row, expected_x, expected_y):
        assert_equal(row['st_x'], expected_x)
        assert_equal(row['st_y'], expected_y)

<commit_msg>test_free_text_geocoding<commit_after>#!/usr/local/bin/python
# -*- coding: utf-8 -*-

from unittest import TestCase
from nose.tools import assert_raises
from nose.tools import assert_not_equal, assert_equal
from ..helpers.integration_test_helper import IntegrationTestHelper

class TestStreetFunctionsSetUp(TestCase):

    def setUp(self):
        self.env_variables = IntegrationTestHelper.get_environment_variables()
        self.sql_api_url = "{0}://{1}.{2}/api/v1/sql".format(
            self.env_variables['schema'],
            self.env_variables['username'],
            self.env_variables['host'],
            self.env_variables['api_key']
        )

class TestStreetFunctions(TestStreetFunctionsSetUp):

    def test_if_select_with_street_point_is_ok(self):
        query = "SELECT cdb_geocode_street_point(street) " \
                "as geometry FROM {0} LIMIT 1&api_key={1}".format(
            self.env_variables['table_name'],
            self.env_variables['api_key'])
        geometry = IntegrationTestHelper.execute_query(self.sql_api_url, query)
        assert_not_equal(geometry['geometry'], None)

    def test_if_select_with_street_without_api_key_raise_error(self):
        query = "SELECT cdb_geocode_street_point(street) " \
                "as geometry FROM {0} LIMIT 1".format(
            self.env_variables['table_name'])
        try:
            IntegrationTestHelper.execute_query(self.sql_api_url, query)
        except Exception as e:
            assert_equal(e.message[0], "The api_key must be provided")

class TestBulkStreetFunctions(TestStreetFunctionsSetUp):

    def test_full_spec(self):
        query = "select cartodb_id, st_x(the_geom), st_y(the_geom) " \
                "FROM cdb_dataservices_client.cdb_bulk_geocode_street_point(" \
                "'select 1 as cartodb_id, ''Spain'' as country, " \
                "''Castilla y León'' as state, ''Valladolid'' as city, " \
                "''Plaza Mayor'' as street  " \
                "UNION " \
                "select 2 as cartodb_id, ''Spain'' as country, " \
                "''Castilla y León'' as state, ''Valladolid'' as city, " \
                "''Paseo Zorrilla'' as street' " \
                ", 'street', 'city', 'state', 'country')"
        response = self._run_authenticated(query)

        assert_equal(response['total_rows'], 2)

        row_by_cartodb_id = self._row_by_cartodb_id(response)
        self._assert_x_y(row_by_cartodb_id[1], -3.7074009, 40.415511)
        self._assert_x_y(row_by_cartodb_id[2], -4.7404453, 41.6314339)

    def test_empty_columns(self):
        query = "select *, st_x(the_geom), st_y(the_geom) " \
                "FROM cdb_dataservices_client.cdb_bulk_geocode_street_point( " \
                "'select * from jsonb_to_recordset(''[" \
                "{\"cartodb_id\": 1, \"address\": \"1901 amphitheatre parkway, mountain view, ca, us\"}" \
                "]''::jsonb) as (cartodb_id integer, address text)', " \
                "'address', '''''', '''''', '''''')"
        response = self._run_authenticated(query)

        assert_equal(response['total_rows'], 1)

        row_by_cartodb_id = self._row_by_cartodb_id(response)
        self._assert_x_y(row_by_cartodb_id[1], -122.0885504, 37.4238657)

    def test_null_columns(self):
        query = "select *, st_x(the_geom), st_y(the_geom) " \
                "FROM cdb_dataservices_client.cdb_bulk_geocode_street_point( " \
                "'select * from jsonb_to_recordset(''[" \
                "{\"cartodb_id\": 1, \"address\": \"1901 amphitheatre parkway, mountain view, ca, us\"}" \
                "]''::jsonb) as (cartodb_id integer, address text)', " \
                "'address')"
        response = self._run_authenticated(query)

        assert_equal(response['total_rows'], 1)

        row_by_cartodb_id = self._row_by_cartodb_id(response)
        self._assert_x_y(row_by_cartodb_id[1], -122.0885504, 37.4238657)

    def test_batching(self):
        query = "select *, st_x(the_geom), st_y(the_geom) " \
                "FROM cdb_dataservices_client.cdb_bulk_geocode_street_point( " \
                "'select * from jsonb_to_recordset(''[" \
                "{\"cartodb_id\": 1, \"address\": \"1900 amphitheatre parkway, mountain view, ca, us\"}," \
                "{\"cartodb_id\": 2, \"address\": \"1901 amphitheatre parkway, mountain view, ca, us\"}," \
                "{\"cartodb_id\": 3, \"address\": \"1902 amphitheatre parkway, mountain view, ca, us\"}" \
                "]''::jsonb) as (cartodb_id integer, address text)', " \
                "'address', null, null, null, 2)"
        response = self._run_authenticated(query)

        assert_equal(response['total_rows'], 3)

        row_by_cartodb_id = self._row_by_cartodb_id(response)
        self._assert_x_y(row_by_cartodb_id[1], -122.0875324, 37.4227968)
        self._assert_x_y(row_by_cartodb_id[2], -122.0885504, 37.4238657)
        self._assert_x_y(row_by_cartodb_id[3], -122.0876674, 37.4235729)

    def test_city_column_geocoding(self):
        query = "select *, st_x(the_geom), st_y(the_geom) " \
                "FROM cdb_dataservices_client.cdb_bulk_geocode_street_point( " \
                "'select * from jsonb_to_recordset(''[" \
                "{\"cartodb_id\": 1, \"city\": \"Valladolid\"}," \
                "{\"cartodb_id\": 2, \"city\": \"Madrid\"}" \
                "]''::jsonb) as (cartodb_id integer, city text)', " \
                "'city')"
        response = self._run_authenticated(query)

        assert_equal(response['total_rows'], 2)

        row_by_cartodb_id = self._row_by_cartodb_id(response)
        self._assert_x_y(row_by_cartodb_id[1], -4.7245321, 41.652251)
        self._assert_x_y(row_by_cartodb_id[2], -3.7037902, 40.4167754)

    def test_free_text_geocoding(self):
        query = "select *, st_x(the_geom), st_y(the_geom) " \
                "FROM cdb_dataservices_client.cdb_bulk_geocode_street_point( " \
                "'select * from (" \
                "select 1 as cartodb_id, ''W 26th Street'' as address, " \
                "null as city , null as state , null as country" \
                ")_x', " \
                "'''Logroño, La Rioja, Spain''')"
        response = self._run_authenticated(query)
        # from nose.tools import set_trace; set_trace()

        assert_equal(response['total_rows'], 1)

        row_by_cartodb_id = self._row_by_cartodb_id(response)
        self._assert_x_y(row_by_cartodb_id[1], -2.4449852, 42.4627195)

    def _run_authenticated(self, query):
        authenticated_query = "{}&api_key={}".format(query,
                                                     self.env_variables[
                                                         'api_key'])
        return IntegrationTestHelper.execute_query_raw(self.sql_api_url,
                                                       authenticated_query)

    def _row_by_cartodb_id(self, response):
        return {r['cartodb_id']: r for r in response['rows']}

    def _assert_x_y(self, row, expected_x, expected_y):
        assert_equal(row['st_x'], expected_x)
        assert_equal(row['st_y'], expected_y)

<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
GUI Interface for Lands
"""

import sys
from PyQt4 import QtGui
import random
import threading
import platec

class GenerateDialog(QtGui.QDialog):

    def __init__(self,parent=None):
        QtGui.QDialog.__init__(self,parent)
        self._init_ui()

    def _init_ui(self):
        self.resize(500, 300)
        self.setWindowTitle('Generate a new world')
        grid = QtGui.QGridLayout()

        seed =  random.randint(0, 65535)

        name_label = QtGui.QLabel('Name')
        grid.addWidget(name_label, 0,0,1,1)
        name = 'world_seed_%i' % seed
        self.name_value = QtGui.QLineEdit(name)
        grid.addWidget(self.name_value, 0,1,1,2)

        seed_label = QtGui.QLabel('Seed')
        grid.addWidget(seed_label, 1,0,1,1)
        self.seed_value = self._spinner_box(0, 65525, seed)
        grid.addWidget(self.seed_value, 1,1,1,2)

        width_label = QtGui.QLabel('Width')
        grid.addWidget(width_label, 2,0,1,1)
        self.width_value = self._spinner_box(100, 8192, 512)
        grid.addWidget(self.width_value, 2,1,1,2)

        height_label = QtGui.QLabel('Height')
        grid.addWidget(height_label, 3,0,1,1)
        self.height_value = self._spinner_box(100, 8192, 512)
        grid.addWidget(self.height_value, 3,1,1,2)

        plates_num_label = QtGui.QLabel('Number of plates')
        grid.addWidget(plates_num_label, 4,0,1,1)
        self.plates_num_value = self._spinner_box(2, 100, 10)
        grid.addWidget(self.plates_num_value, 4,1,1,2)

        platesres_w_label = QtGui.QLabel('Plates resolution (width)')
        grid.addWidget(platesres_w_label, 5,0,1,1)
        self.platesres_w_value = self._spinner_box(50, 4096, 512)
        grid.addWidget(self.platesres_w_value, 5,1,1,2)

        platesres_h_label = QtGui.QLabel('Plates resolution (height)')
        grid.addWidget(platesres_h_label, 6,0,1,1)
        self.platesres_h_value = self._spinner_box(50, 4096, 512)
        grid.addWidget(self.platesres_h_value, 6,1,1,2)

        buttons_row = 7
        cancel   = QtGui.QPushButton('Cancel')
        generate = QtGui.QPushButton('Generate')
        grid.addWidget(cancel,   buttons_row, 1, 1, 1)
        grid.addWidget(generate, buttons_row, 2, 1, 1)
        cancel.clicked.connect(self._on_cancel)
        generate.clicked.connect(self._on_generate)

        self.setLayout(grid)

    def _spinner_box(self, min, max, value):
        spinner = QtGui.QSpinBox()
        spinner.setMinimum(min)
        spinner.setMaximum(max)
        spinner.setValue(value)
        return spinner

    def _on_cancel(self):
        QtGui.QDialog.reject(self)

    def _on_generate(self):
        QtGui.QDialog.accept(self)

    def seed(self):
        return self.seed_value.value()

    def width(self):
        return self.platesres_w_value.value()

    def height(self):
        return self.platesres_h_value.value()

    def num_plates(self):
        return self.plates_num_value.value()

class GenerationProgressDialog(QtGui.QDialog):

    def __init__(self, seed, width, height, num_plates):
        QtGui.QDialog.__init__(self)
        self._init_ui()
        self.gen_thread = GenerationThread(self, seed, width, height, num_plates)
        self.gen_thread.start()

    def _init_ui(self):
        self.resize(400, 100)
        self.setWindowTitle('Generating a new world...')
        grid = QtGui.QGridLayout()

        self.status = QtGui.QLabel('....')
        grid.addWidget(self.status, 0, 0, 1, 3)

        cancel   = QtGui.QPushButton('Cancel')
        grid.addWidget(cancel, 1, 0, 1, 1)
        cancel.clicked.connect(self._on_cancel)

        self.setLayout(grid)

    def _on_cancel(self):
        QtGui.QDialog.reject(self)

    def set_status(self, message):
        self.status.setText(message)

class GenerationThread(threading.Thread):

    def __init__(self, ui, seed, width, height, num_plates):
        threading.Thread.__init__(self)
        print('seed %i' % seed)
        print('width %i' % width)
        print('height %i' % height)
        print('num_plates %i' % num_plates)
        self.plates_generation = PlatesGeneration(seed, width, height, num_plates=num_plates)
        self.ui = ui

    def run (self):
        finished = False
        while not finished:
            (finished, n_steps) = self.plates_generation.step()
            print("n_steps %i %s" % (n_steps, finished))
            self.ui.set_status('Step %i' % n_steps)

class PlatesGeneration():

    def __init__(self, seed, width, height,
                 sea_level=0.65, erosion_period=60,
                 folding_ratio=0.02, aggr_overlap_abs=1000000, aggr_overlap_rel=0.33,
                 cycle_count=2, num_plates=10):
        self.p = platec.create(seed, width, height, sea_level, erosion_period, folding_ratio,
                               aggr_overlap_abs, aggr_overlap_rel, cycle_count, num_plates)
        self.steps = 0

    def step(self):
        if platec.is_finished(self.p) == 0:
            platec.step(self.p)
            self.steps += 1
            return (False, self.steps)
        else:
            return (True, self.steps)

class LandsGui(QtGui.QMainWindow):

    def __init__(self):
        super(LandsGui, self).__init__()
        self._init_ui()

    def set_status(self, message):
        self.statusBar().showMessage(message)

    def _init_ui(self):
        self.resize(800, 600)
        self.setWindowTitle('Lands - A world generator')
        self.set_status('No world selected: create or load a world')
        self._prepare_menu()
        self.show()

    def _prepare_menu(self):
        generateAction = QtGui.QAction('&Generate', self)
        generateAction.setShortcut('Ctrl+G')
        generateAction.setStatusTip('Generate new world')
        generateAction.triggered.connect(self._on_generate)

        exitAction = QtGui.QAction('&Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(QtGui.qApp.quit)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(generateAction)
        fileMenu.addAction(exitAction)

    def _on_generate(self):
        dialog = GenerateDialog()
        ok = dialog.exec_()
        if ok:
            seed = dialog.seed()
            width = dialog.width()
            height = dialog.height()
            num_plates = dialog.num_plates()
            dialog2 = GenerationProgressDialog(seed, width, height, num_plates)
            ok2     = dialog2.exec_()

def main():

    app = QtGui.QApplication(sys.argv)

    lg = LandsGui()

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
<commit_msg>qt: image drawing prepared<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
GUI Interface for Lands
"""

import sys
from PyQt4 import QtGui, QtCore
import random
import threading
import platec

class GenerateDialog(QtGui.QDialog):

    def __init__(self,parent=None):
        QtGui.QDialog.__init__(self,parent)
        self._init_ui()

    def _init_ui(self):
        self.resize(500, 300)
        self.setWindowTitle('Generate a new world')
        grid = QtGui.QGridLayout()

        seed =  random.randint(0, 65535)

        name_label = QtGui.QLabel('Name')
        grid.addWidget(name_label, 0,0,1,1)
        name = 'world_seed_%i' % seed
        self.name_value = QtGui.QLineEdit(name)
        grid.addWidget(self.name_value, 0,1,1,2)

        seed_label = QtGui.QLabel('Seed')
        grid.addWidget(seed_label, 1,0,1,1)
        self.seed_value = self._spinner_box(0, 65525, seed)
        grid.addWidget(self.seed_value, 1,1,1,2)

        width_label = QtGui.QLabel('Width')
        grid.addWidget(width_label, 2,0,1,1)
        self.width_value = self._spinner_box(100, 8192, 512)
        grid.addWidget(self.width_value, 2,1,1,2)

        height_label = QtGui.QLabel('Height')
        grid.addWidget(height_label, 3,0,1,1)
        self.height_value = self._spinner_box(100, 8192, 512)
        grid.addWidget(self.height_value, 3,1,1,2)

        plates_num_label = QtGui.QLabel('Number of plates')
        grid.addWidget(plates_num_label, 4,0,1,1)
        self.plates_num_value = self._spinner_box(2, 100, 10)
        grid.addWidget(self.plates_num_value, 4,1,1,2)

        platesres_w_label = QtGui.QLabel('Plates resolution (width)')
        grid.addWidget(platesres_w_label, 5,0,1,1)
        self.platesres_w_value = self._spinner_box(50, 4096, 512)
        grid.addWidget(self.platesres_w_value, 5,1,1,2)

        platesres_h_label = QtGui.QLabel('Plates resolution (height)')
        grid.addWidget(platesres_h_label, 6,0,1,1)
        self.platesres_h_value = self._spinner_box(50, 4096, 512)
        grid.addWidget(self.platesres_h_value, 6,1,1,2)

        buttons_row = 7
        cancel   = QtGui.QPushButton('Cancel')
        generate = QtGui.QPushButton('Generate')
        grid.addWidget(cancel,   buttons_row, 1, 1, 1)
        grid.addWidget(generate, buttons_row, 2, 1, 1)
        cancel.clicked.connect(self._on_cancel)
        generate.clicked.connect(self._on_generate)

        self.setLayout(grid)

    def _spinner_box(self, min, max, value):
        spinner = QtGui.QSpinBox()
        spinner.setMinimum(min)
        spinner.setMaximum(max)
        spinner.setValue(value)
        return spinner

    def _on_cancel(self):
        QtGui.QDialog.reject(self)

    def _on_generate(self):
        QtGui.QDialog.accept(self)

    def seed(self):
        return self.seed_value.value()

    def width(self):
        return self.platesres_w_value.value()

    def height(self):
        return self.platesres_h_value.value()

    def num_plates(self):
        return self.plates_num_value.value()

class GenerationProgressDialog(QtGui.QDialog):

    def __init__(self, seed, width, height, num_plates):
        QtGui.QDialog.__init__(self)
        self._init_ui()
        self.gen_thread = GenerationThread(self, seed, width, height, num_plates)
        self.gen_thread.start()

    def _init_ui(self):
        self.resize(400, 100)
        self.setWindowTitle('Generating a new world...')
        grid = QtGui.QGridLayout()

        self.status = QtGui.QLabel('....')
        grid.addWidget(self.status, 0, 0, 1, 3)

        cancel   = QtGui.QPushButton('Cancel')
        grid.addWidget(cancel, 1, 0, 1, 1)
        cancel.clicked.connect(self._on_cancel)

        self.setLayout(grid)

    def _on_cancel(self):
        QtGui.QDialog.reject(self)

    def set_status(self, message):
        self.status.setText(message)

class GenerationThread(threading.Thread):

    def __init__(self, ui, seed, width, height, num_plates):
        threading.Thread.__init__(self)
        print('seed %i' % seed)
        print('width %i' % width)
        print('height %i' % height)
        print('num_plates %i' % num_plates)
        self.plates_generation = PlatesGeneration(seed, width, height, num_plates=num_plates)
        self.ui = ui

    def run (self):
        finished = False
        while not finished:
            (finished, n_steps) = self.plates_generation.step()
            print("n_steps %i %s" % (n_steps, finished))
            self.ui.set_status('Step %i' % n_steps)

class PlatesGeneration():

    def __init__(self, seed, width, height,
                 sea_level=0.65, erosion_period=60,
                 folding_ratio=0.02, aggr_overlap_abs=1000000, aggr_overlap_rel=0.33,
                 cycle_count=2, num_plates=10):
        self.p = platec.create(seed, width, height, sea_level, erosion_period, folding_ratio,
                               aggr_overlap_abs, aggr_overlap_rel, cycle_count, num_plates)
        self.steps = 0

    def step(self):
        if platec.is_finished(self.p) == 0:
            platec.step(self.p)
            self.steps += 1
            return (False, self.steps)
        else:
            return (True, self.steps)

class MapCanvas(QtGui.QImage):

    def __init__(self):
        QtGui.QImage.__init__(self, 800, 600, QtGui.QImage.Format_RGB32);

    def draw_world(self):
        for x in range(100):
            for y in range(100):
                self.setPixel(x, y, 255*(65536))

class LandsGui(QtGui.QMainWindow):

    def __init__(self):
        super(LandsGui, self).__init__()
        self._init_ui()

    def set_status(self, message):
        self.statusBar().showMessage(message)

    def _init_ui(self):
        self.resize(800, 600)
        self.setWindowTitle('Lands - A world generator')
        self.set_status('No world selected: create or load a world')
        self._prepare_menu()
        self.canvas = MapCanvas()
        self.canvas.draw_world()
        self.label = QtGui.QLabel()
        self.label.setPixmap(QtGui.QPixmap.fromImage(self.canvas))
        self.setCentralWidget(self.label)
        self.show()

    def _prepare_menu(self):
        generateAction = QtGui.QAction('&Generate', self)
        generateAction.setShortcut('Ctrl+G')
        generateAction.setStatusTip('Generate new world')
        generateAction.triggered.connect(self._on_generate)

        exitAction = QtGui.QAction('&Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(QtGui.qApp.quit)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(generateAction)
        fileMenu.addAction(exitAction)

    def _on_generate(self):
        dialog = GenerateDialog()
        ok = dialog.exec_()
        if ok:
            seed = dialog.seed()
            width = dialog.width()
            height = dialog.height()
            num_plates = dialog.num_plates()
            dialog2 = GenerationProgressDialog(seed, width, height, num_plates)
            ok2     = dialog2.exec_()

def main():

    app = QtGui.QApplication(sys.argv)

    lg = LandsGui()

    sys.exit(app.exec_())

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


<commit_before>
from cartoview.app_manager.models import AppInstance
from geonode.api.resourcebase_api import *
from .resources import  FileUploadResource
from tastypie.resources import ModelResource
from tastypie import fields
from geonode.maps.models import Map as GeonodeMap, MapLayer as GeonodeMapLayer
from geonode.layers.models import Layer, Attribute
from tastypie.constants import ALL_WITH_RELATIONS, ALL

class GeonodeMapLayerResource(ModelResource):
    class Meta:
        queryset = GeonodeMapLayer.objects.distinct()

class GeonodeMapResource(ModelResource):
    map_layers = fields.ToManyField(GeonodeMapLayerResource, 'layer_set', null=True, full=True)
    class Meta:
        queryset = GeonodeMap.objects.distinct().order_by('-date')

class GeonodeLayerResource(ModelResource):
    class Meta:
        queryset = Layer.objects.all()
        excludes = ['csw_anytext', 'metadata_xml']
        filtering = {"typename": ALL}

class GeonodeLayerAttributeResource(ModelResource):
    layer = fields.ForeignKey(GeonodeLayerResource,'layer')
    class Meta:
        queryset = Attribute.objects.all().order_by('display_order')
        filtering = {
            "layer": ALL_WITH_RELATIONS,
            "attribute": ALL_WITH_RELATIONS
        }

class AppResource(FileUploadResource):
    class Meta(FileUploadResource.Meta):
        from models import App
        queryset = App.objects.all()
        filtering = {"name": ALL ,"title":ALL}
        can_edit = True

class AppInstanceResource(CommonModelApi):

    app = fields.ToOneField(AppResource, 'app', full=True)
    map = fields.ForeignKey(GeonodeMapResource, 'map', full=True)
    class Meta(CommonMetaApi):
        filtering = CommonMetaApi.filtering

        filtering.update({'app': ALL_WITH_RELATIONS})
        queryset = AppInstance.objects.distinct().order_by('-date')
        if settings.RESOURCE_PUBLISHING:
            queryset = queryset.filter(is_published=True)
        resource_name = 'appinstances'

<commit_msg>support filter by id in attribute rest<commit_after>
from cartoview.app_manager.models import AppInstance
from geonode.api.resourcebase_api import *
from .resources import  FileUploadResource
from tastypie.resources import ModelResource
from tastypie import fields
from geonode.maps.models import Map as GeonodeMap, MapLayer as GeonodeMapLayer
from geonode.layers.models import Layer, Attribute
from tastypie.constants import ALL_WITH_RELATIONS, ALL

class GeonodeMapLayerResource(ModelResource):
    class Meta:
        queryset = GeonodeMapLayer.objects.distinct()

class GeonodeMapResource(ModelResource):
    map_layers = fields.ToManyField(GeonodeMapLayerResource, 'layer_set', null=True, full=True)
    class Meta:
        queryset = GeonodeMap.objects.distinct().order_by('-date')

class GeonodeLayerResource(ModelResource):
    class Meta:
        queryset = Layer.objects.all()
        excludes = ['csw_anytext', 'metadata_xml']
        filtering = {"typename": ALL}

class GeonodeLayerAttributeResource(ModelResource):
    layer = fields.ForeignKey(GeonodeLayerResource,'layer')
    class Meta:
        queryset = Attribute.objects.all().order_by('display_order')
        filtering = {
            "layer": ALL_WITH_RELATIONS,
            "id": ALL,
            "attribute": ALL_WITH_RELATIONS
        }

class AppResource(FileUploadResource):
    class Meta(FileUploadResource.Meta):
        from models import App
        queryset = App.objects.all()
        filtering = {"name": ALL ,"title":ALL}
        can_edit = True

class AppInstanceResource(CommonModelApi):

    app = fields.ToOneField(AppResource, 'app', full=True)
    map = fields.ForeignKey(GeonodeMapResource, 'map', full=True)
    class Meta(CommonMetaApi):
        filtering = CommonMetaApi.filtering

        filtering.update({'app': ALL_WITH_RELATIONS})
        queryset = AppInstance.objects.distinct().order_by('-date')
        if settings.RESOURCE_PUBLISHING:
            queryset = queryset.filter(is_published=True)
        resource_name = 'appinstances'

<|endoftext|>


<commit_before>from __future__ import print_function

import logging

from sklearn import linear_model as skl_lm
import numpy as np

#from IPython import embed

import constraints as cons
import err

import plot_hack as ph
import settings

logger = logging.getLogger(__name__)

# TODO: UNUSED
def factory(model_type):
    if model_type == 'approx':
        return RegressionModel()
    elif model_type == 'sound':
        return FlowStarModel()
    else:
        raise NotImplementedError

def warn_if_small_data_set(x):
    if len(x) <= 2:
        err.warn_severe('Less than 2 training samples!')#: {}'.format(x))
    elif len(x) <= 10:
        err.warn('Less than 10 training samples!')#: {}'.format(x))

class RegressionModel(object):
    def __init__(self, x, y):
        warn_if_small_data_set(x)
        #self.clf_ = skl_lm.RidgeCV(alphas=[1.0, 1.0], fit_intercept=True)
        # Copy must be on...arrays are getting reused!
        self.clf_ = skl_lm.LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=True)
        self.clf_.fit(x, y)
        #if settings.debug:
        #    print clf_.coef_, clf_.intercept_

    @property
    def clf(self):
        return self.clf_

    @property
    def A(self):
        return self.clf.coef_

    @property
    def b(self):
        return self.clf.intercept_

    def predict(self, X):
        Y = self.clf.predict(X)
        return Y

    def error_pc(self, X, Y):
        """error_pc

        Parameters
        ----------
        X : Test input
        Y : Test Output

        Returns
        -------
        error % vector

        Notes
        ------
        Indicates model quality for debug purposes
        """
        '''computes relative error% along each dimension'''
        Y_ = self.predict(X)
        #if settings.debug:
        #    print 'score: ', self.clf.score(X, Y)
        #    print abs((Y - Y_)/Y)*100
        return (abs((Y - Y_)/Y))*100

    def max_error_pc(self, X, Y):
        """max_error_pc

        Parameters
        ----------
        X : Test input
        Y : Test Output

        Returns
        -------
        error % vector

        Notes
        ------
        Indicates model quality for debug purposes
        """
        '''computes relative error% along each dimension'''
        Y_ = self.predict(X)
        #if settings.debug:
        #    print 'score: ', self.clf.score(X, Y)
        #    print abs((Y - Y_)/Y)*100
        return np.max((abs((Y - Y_)/Y))*100, axis=0)

    def error(self, X, Y):
        """error

        Parameters
        ----------
        X : Test input
        Y : Test Output

        Returns
        -------
        Computes signed error against passed in test samples.
        Sound error interval vector (non-symmeteric).
        The interal must be added and NOT subtracted to make a sound
        model. e.g. Y_sound = A*X + e
        or,
            predict(X) + e_l <= Y_sound <= predict(X) + e_h
            Y_sound = predict(X) + [e_l, e_h]

        Notes
        ------
        Computes interval vector e = Y_true - Y_predict, such that
        the interval is sound w.r.t. to passed in test samples
        """
        Yp = self.predict(X)
        delta = Y - Yp
        max_e = np.max((delta), axis=0)
        min_e = np.min((delta), axis=0)
        return cons.IntervalCons(min_e, max_e)

    def plot(self, X, Y, tol, title='unknown'):
        from matplotlib import pyplot as plt
        from mpl_toolkits.mplot3d import Axes3D

        dimX = len(X.shape)
        assert(dimX == 2)

        # if 1 or less rows
        if X.shape[0] <= 1:
            err.warn('cant plot, only 1 value!!')
            return

        div = 50
        X_min, X_max = np.min(X, axis=0), np.max(X, axis=0)
        step0 = (X_max[0] - X_min[0])/div
        step1 = (X_max[1] - X_min[1])/div
        step = min(step0, step1)
        #print('x0 range:', X_min[0], X_max[0])
        #print('x1 range:', X_min[1], X_max[1])

        # Predict data of estimated models
        xx1, xx2 = np.mgrid[X_min[0]:X_max[0]:step, X_min[1]:X_max[1]:step]
        xx = np.vstack([xx1.ravel(), xx2.ravel()]).T
        yy = self.predict(xx)

        yy0 = yy[:, 0]
        yy1 = yy[:, 1]
        Y0 = Y[:, 0]
        Y1 = Y[:, 1]

        error_pc = self.error_pc(X, Y)
        outlier0_idx = error_pc[:, 0] > tol
        outlier1_idx = error_pc[:, 1] > tol

        if any(outlier0_idx):
            #print('X:\n', X[outlier0_idx, :])
            #print('Y0_pred:', self.predict(X[outlier0_idx, :])[:, 0])
            #print('Y0_true', Y0[outlier0_idx])
            Y0_pred_ = self.predict(X[outlier0_idx, :])[:, 0]
            Y0_pred = np.reshape(Y0_pred_, (Y0_pred_.size, 1))
            # make it 2-dim to match dim of X and Y0_pred
            Y0_true_ = Y0[outlier0_idx]
            Y0_true = np.reshape(Y0_true_, (Y0_true_.size, 1))
            epc0_ = error_pc[outlier0_idx, 0]
            epc0 = np.reshape(epc0_, (epc0_.size, 1))

            logger.debug('X - Y0_pred - Y0_true - error_pc')
            logger.debug(np.hstack((X[outlier0_idx, :], Y0_pred, Y0_true, epc0)))

        if any(outlier1_idx):
            Y1_pred_ = self.predict(X[outlier1_idx, :])[:, 1]
            Y1_pred = np.reshape(Y1_pred_, (Y1_pred_.size, 1))

            # make it 2-dim to match dim of X and Y1_pred
            Y1_true_ = Y1[outlier1_idx]
            Y1_true = np.reshape(Y1_true_, (Y1_true_.size, 1))
            epc1_ = error_pc[outlier1_idx, 1]
            epc1 = np.reshape(epc1_, (epc1_.size, 1))
            logger.debug('X - Y1_pred - Y1_true - error_pc')
            logger.debug(np.hstack((X[outlier1_idx, :], Y1_pred, Y1_true, epc1)))

        # plot the surface
        #print(xx)
        #print(xx2)
        #print(yy[:, 0])
        #embed()

        ################
        # First subplot
        ################

        fig = plt.figure()#figsize=plt.figaspect(2.))
        fig.suptitle(title)

        ax = fig.add_subplot(2, 1, 1, projection='3d')
        ax.plot_surface(xx1, xx2, np.reshape(yy0, (xx1.shape)))
        ax.plot(X[:, 0], X[:, 1], Y0, 'y.')
        ax.plot(X[outlier0_idx, 0], X[outlier0_idx, 1], Y0[outlier0_idx], 'r.')
        ax.set_title('y0 vs x')
        ax.set_xlabel('x0')
        ax.set_ylabel('x1')
        ax.set_zlabel('y0')

        #################
        # Second subplot
        #################
        ax = fig.add_subplot(2, 1, 2, projection='3d')
        ax.plot_surface(xx1, xx2, np.reshape(yy1, (xx1.shape)))
        ax.plot(X[:, 0], X[:, 1], Y1, 'y.')
        ax.plot(X[outlier1_idx, 0], X[outlier1_idx, 1], Y1[outlier1_idx], 'r.')
        ax.set_title('y1 vs x')
        ax.set_xlabel('x0')
        ax.set_ylabel('x1')
        ax.set_zlabel('y1')

        ph.plt_show()

# Old single figure plots
#         fig = plt.figure()
#         ax = fig.gca(projection='3d')
#         ax.plot_surface(xx1, xx2, np.reshape(yy0, (xx1.shape)))
#         ax.plot(X[:, 0], X[:, 1], Y0, 'y.')
#         ax.set_title('y0 vs x:' + title)
#         plt.show()

#         fig = plt.figure()
#         ax = fig.gca(projection='3d')
#         ax.plot_surface(xx1, xx2, np.reshape(yy1, (xx1.shape)))
#         ax.plot(X[:, 0], X[:, 1], Y1, 'y.')
#         ax.set_title('y1 vs x:' + title)
#         plt.show()

class FlowStarModel(object):
    """FlowStarModel
    Implements only purely continuous models for now.
    Can add support for H.A. later."""

    def __init__(self, ODE_str):
        self.ODE = ODE_str
        return

    def get_model(self, X, t):
        return self.get_reach_set(X, t)

    def get_reach_set(self, X, t):
        """interface with flow*"""
        raise NotImplementedError

class RobustRegressionModel(object):
    def __init__(self, x, y):
        warn_if_small_data_set(x)
        #self.clf_ = skl_lm.RidgeCV(alphas=[1.0, 1.0], fit_intercept=True)
        # Copy must be on...arrays are getting reused!
        self.model = skl_lm.LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=True)
        #self.clf_.fit(x, y)
        self.clf_ = skl_lm.RANSACRegressor(self.model)
        self.clf_.fit(x, y)

        #if settings.debug:
        #    print clf_.coef_, clf_.intercept_

    @property
    def clf(self):
        return self.clf_

    @property
    def A(self):
        return self.clf.estimator_.coef_

    @property
    def b(self):
        return self.clf.estimator_.intercept_

    def predict(self, X):
        Y = self.clf.predict(X)
        return Y

    def max_error_pc(self, X, Y):
        """max_error_pc

        Parameters
        ----------
        X : Test input
        Y : Test Output

        Returns
        -------
        error % vector

        Notes
        ------
        Indicates model quality for debug purposes
        """
        '''computes relative error% along each dimension'''
        Y_ = self.predict(X)
        #if settings.debug:
        #    print 'score: ', self.clf.score(X, Y)
        #    print abs((Y - Y_)/Y)*100
        return np.max((abs((Y - Y_)/Y))*100, axis=0)

    def error(self, X, Y):
        """error

        Parameters
        ----------
        X : Test input
        Y : Test Output

        Returns
        -------
        Computes signed error against passed in test samples.
        Sound error interval vector (non-symmeteric).
        The interal must be added and NOT subtracted to make a sound
        model. e.g. Y_sound = A*X + e
        or,
            predict(X) + e_l <= Y_sound <= predict(X) + e_h
            Y_sound = predict(X) + [e_l, e_h]

        Notes
        ------
        Computes interval vector e = Y_true - Y_predict, such that
        the interval is sound w.r.t. to passed in test samples
        """
        Yp = self.predict(X)
        delta = Y - Yp
        max_e = np.max((delta), axis=0)
        min_e = np.min((delta), axis=0)
        return cons.IntervalCons(min_e, max_e)

"""robust linear regression RANSAC: example"""
# n_samples = 1000
# n_outliers = 50

# X, y, coef = datasets.make_regression(n_samples=n_samples, n_features=1,
#                                       n_informative=1, noise=10,
#                                       coef=True, random_state=0)

# # Add outlier data
# np.random.seed(0)
# X[:n_outliers] = 3 + 0.5 * np.random.normal(size=(n_outliers, 1))
# y[:n_outliers] = -3 + 10 * np.random.normal(size=n_outliers)

# # Fit line using all data
# model = linear_model.LinearRegression()
# model.fit(X, y)

# # Robustly fit linear model with RANSAC algorithm
# model_ransac = linear_model.RANSACRegressor(linear_model.LinearRegression())
# model_ransac.fit(X, y)
# inlier_mask = model_ransac.inlier_mask_
# outlier_mask = np.logical_not(inlier_mask)

# # Predict data of estimated models
# line_X = np.arange(-5, 5)
# line_y = model.predict(line_X[:, np.newaxis])
# line_y_ransac = model_ransac.predict(line_X[:, np.newaxis])

# # Compare estimated coefficients
# print("Estimated coefficients (true, normal, RANSAC):")
# print(coef, model.coef_, model_ransac.estimator_.coef_)

# plt.plot(X[inlier_mask], y[inlier_mask], '.g', label='Inliers')
# plt.plot(X[outlier_mask], y[outlier_mask], '.r', label='Outliers')
# plt.plot(line_X, line_y, '-k', label='Linear regressor')
# plt.plot(line_X, line_y_ransac, '-b', label='RANSAC regressor')
# plt.legend(loc='lower right')
# plt.show()
<commit_msg>moved plotting to settings.py and minor bug fix<commit_after>from __future__ import print_function

import logging

from sklearn import linear_model as skl_lm
import numpy as np

#from IPython import embed

import constraints as cons
import err

import settings

logger = logging.getLogger(__name__)

# TODO: UNUSED
def factory(model_type):
    if model_type == 'approx':
        return RegressionModel()
    elif model_type == 'sound':
        return FlowStarModel()
    else:
        raise NotImplementedError

def warn_if_small_data_set(x):
    if len(x) <= 2:
        err.warn_severe('Less than 2 training samples!')#: {}'.format(x))
    elif len(x) <= 10:
        err.warn('Less than 10 training samples!')#: {}'.format(x))

class RegressionModel(object):
    def __init__(self, x, y):
        warn_if_small_data_set(x)
        #self.clf_ = skl_lm.RidgeCV(alphas=[1.0, 1.0], fit_intercept=True)
        # Copy must be on...arrays are getting reused!
        self.clf_ = skl_lm.LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=True)
        self.clf_.fit(x, y)
        #if settings.debug:
        #    print clf_.coef_, clf_.intercept_

    @property
    def clf(self):
        return self.clf_

    @property
    def A(self):
        return self.clf.coef_

    @property
    def b(self):
        return self.clf.intercept_

    def predict(self, X):
        Y = self.clf.predict(X)
        return Y

    def error_pc(self, X, Y):
        """error_pc

        Parameters
        ----------
        X : Test input
        Y : Test Output

        Returns
        -------
        error % vector

        Notes
        ------
        Indicates model quality for debug purposes
        """
        '''computes relative error% along each dimension'''
        Y_ = self.predict(X)
        #if settings.debug:
        #    print 'score: ', self.clf.score(X, Y)
        #    print abs((Y - Y_)/Y)*100
        return (abs((Y - Y_)/Y))*100

    def max_error_pc(self, X, Y):
        """max_error_pc

        Parameters
        ----------
        X : Test input
        Y : Test Output

        Returns
        -------
        error % vector

        Notes
        ------
        Indicates model quality for debug purposes
        """
        '''computes relative error% along each dimension'''
        Y_ = self.predict(X)
        #if settings.debug:
        #    print 'score: ', self.clf.score(X, Y)
        #    print abs((Y - Y_)/Y)*100
        return np.max((abs((Y - Y_)/Y))*100, axis=0)

    def error(self, X, Y):
        """error

        Parameters
        ----------
        X : Test input
        Y : Test Output

        Returns
        -------
        Computes signed error against passed in test samples.
        Sound error interval vector (non-symmeteric).
        The interal must be added and NOT subtracted to make a sound
        model. e.g. Y_sound = A*X + e
        or,
            predict(X) + e_l <= Y_sound <= predict(X) + e_h
            Y_sound = predict(X) + [e_l, e_h]

        Notes
        ------
        Computes interval vector e = Y_true - Y_predict, such that
        the interval is sound w.r.t. to passed in test samples
        """
        Yp = self.predict(X)
        delta = Y - Yp
        max_e = np.max((delta), axis=0)
        min_e = np.min((delta), axis=0)
        return cons.IntervalCons(min_e, max_e)

    def plot(self, X, Y, tol, title='unknown'):
        from matplotlib import pyplot as plt
        from mpl_toolkits.mplot3d import Axes3D

        dimX = X.shape[1]
        assert(dimX == 2)

        # if 1 or less rows
        if X.shape[0] <= 1:
            err.warn('cant plot, only 1 value!!')
            return

        div = 50
        X_min, X_max = np.min(X, axis=0), np.max(X, axis=0)
        step0 = (X_max[0] - X_min[0])/div
        step1 = (X_max[1] - X_min[1])/div
        step = min(step0, step1)
        #print('x0 range:', X_min[0], X_max[0])
        #print('x1 range:', X_min[1], X_max[1])

        # Predict data of estimated models
        xx1, xx2 = np.mgrid[X_min[0]:X_max[0]:step, X_min[1]:X_max[1]:step]
        xx = np.vstack([xx1.ravel(), xx2.ravel()]).T
        yy = self.predict(xx)

        yy0 = yy[:, 0]
        yy1 = yy[:, 1]
        Y0 = Y[:, 0]
        Y1 = Y[:, 1]

        error_pc = self.error_pc(X, Y)
        outlier0_idx = error_pc[:, 0] > tol
        outlier1_idx = error_pc[:, 1] > tol

        if any(outlier0_idx):
            #print('X:\n', X[outlier0_idx, :])
            #print('Y0_pred:', self.predict(X[outlier0_idx, :])[:, 0])
            #print('Y0_true', Y0[outlier0_idx])
            Y0_pred_ = self.predict(X[outlier0_idx, :])[:, 0]
            Y0_pred = np.reshape(Y0_pred_, (Y0_pred_.size, 1))
            # make it 2-dim to match dim of X and Y0_pred
            Y0_true_ = Y0[outlier0_idx]
            Y0_true = np.reshape(Y0_true_, (Y0_true_.size, 1))
            epc0_ = error_pc[outlier0_idx, 0]
            epc0 = np.reshape(epc0_, (epc0_.size, 1))

            logger.debug('X - Y0_pred - Y0_true - error_pc')
            logger.debug(np.hstack((X[outlier0_idx, :], Y0_pred, Y0_true, epc0)))

        if any(outlier1_idx):
            Y1_pred_ = self.predict(X[outlier1_idx, :])[:, 1]
            Y1_pred = np.reshape(Y1_pred_, (Y1_pred_.size, 1))

            # make it 2-dim to match dim of X and Y1_pred
            Y1_true_ = Y1[outlier1_idx]
            Y1_true = np.reshape(Y1_true_, (Y1_true_.size, 1))
            epc1_ = error_pc[outlier1_idx, 1]
            epc1 = np.reshape(epc1_, (epc1_.size, 1))
            logger.debug('X - Y1_pred - Y1_true - error_pc')
            logger.debug(np.hstack((X[outlier1_idx, :], Y1_pred, Y1_true, epc1)))

        # plot the surface
        #print(xx)
        #print(xx2)
        #print(yy[:, 0])
        #embed()

        ################
        # First subplot
        ################

        fig = plt.figure()#figsize=plt.figaspect(2.))
        fig.suptitle(title)

        ax = fig.add_subplot(2, 1, 1, projection='3d')
        ax.plot_surface(xx1, xx2, np.reshape(yy0, (xx1.shape)))
        ax.plot(X[:, 0], X[:, 1], Y0, 'y.')
        ax.plot(X[outlier0_idx, 0], X[outlier0_idx, 1], Y0[outlier0_idx], 'r.')
        ax.set_title('y0 vs x')
        ax.set_xlabel('x0')
        ax.set_ylabel('x1')
        ax.set_zlabel('y0')

        #################
        # Second subplot
        #################
        ax = fig.add_subplot(2, 1, 2, projection='3d')
        ax.plot_surface(xx1, xx2, np.reshape(yy1, (xx1.shape)))
        ax.plot(X[:, 0], X[:, 1], Y1, 'y.')
        ax.plot(X[outlier1_idx, 0], X[outlier1_idx, 1], Y1[outlier1_idx], 'r.')
        ax.set_title('y1 vs x')
        ax.set_xlabel('x0')
        ax.set_ylabel('x1')
        ax.set_zlabel('y1')

        settings.plt_show()

# Old single figure plots
#         fig = plt.figure()
#         ax = fig.gca(projection='3d')
#         ax.plot_surface(xx1, xx2, np.reshape(yy0, (xx1.shape)))
#         ax.plot(X[:, 0], X[:, 1], Y0, 'y.')
#         ax.set_title('y0 vs x:' + title)
#         plt.show()

#         fig = plt.figure()
#         ax = fig.gca(projection='3d')
#         ax.plot_surface(xx1, xx2, np.reshape(yy1, (xx1.shape)))
#         ax.plot(X[:, 0], X[:, 1], Y1, 'y.')
#         ax.set_title('y1 vs x:' + title)
#         plt.show()

class FlowStarModel(object):
    """FlowStarModel
    Implements only purely continuous models for now.
    Can add support for H.A. later."""

    def __init__(self, ODE_str):
        self.ODE = ODE_str
        return

    def get_model(self, X, t):
        return self.get_reach_set(X, t)

    def get_reach_set(self, X, t):
        """interface with flow*"""
        raise NotImplementedError

class RobustRegressionModel(object):
    def __init__(self, x, y):
        warn_if_small_data_set(x)
        #self.clf_ = skl_lm.RidgeCV(alphas=[1.0, 1.0], fit_intercept=True)
        # Copy must be on...arrays are getting reused!
        self.model = skl_lm.LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=True)
        #self.clf_.fit(x, y)
        self.clf_ = skl_lm.RANSACRegressor(self.model)
        self.clf_.fit(x, y)

        #if settings.debug:
        #    print clf_.coef_, clf_.intercept_

    @property
    def clf(self):
        return self.clf_

    @property
    def A(self):
        return self.clf.estimator_.coef_

    @property
    def b(self):
        return self.clf.estimator_.intercept_

    def predict(self, X):
        Y = self.clf.predict(X)
        return Y

    def max_error_pc(self, X, Y):
        """max_error_pc

        Parameters
        ----------
        X : Test input
        Y : Test Output

        Returns
        -------
        error % vector

        Notes
        ------
        Indicates model quality for debug purposes
        """
        '''computes relative error% along each dimension'''
        Y_ = self.predict(X)
        #if settings.debug:
        #    print 'score: ', self.clf.score(X, Y)
        #    print abs((Y - Y_)/Y)*100
        return np.max((abs((Y - Y_)/Y))*100, axis=0)

    def error(self, X, Y):
        """error

        Parameters
        ----------
        X : Test input
        Y : Test Output

        Returns
        -------
        Computes signed error against passed in test samples.
        Sound error interval vector (non-symmeteric).
        The interal must be added and NOT subtracted to make a sound
        model. e.g. Y_sound = A*X + e
        or,
            predict(X) + e_l <= Y_sound <= predict(X) + e_h
            Y_sound = predict(X) + [e_l, e_h]

        Notes
        ------
        Computes interval vector e = Y_true - Y_predict, such that
        the interval is sound w.r.t. to passed in test samples
        """
        Yp = self.predict(X)
        delta = Y - Yp
        max_e = np.max((delta), axis=0)
        min_e = np.min((delta), axis=0)
        return cons.IntervalCons(min_e, max_e)

"""robust linear regression RANSAC: example"""
# n_samples = 1000
# n_outliers = 50

# X, y, coef = datasets.make_regression(n_samples=n_samples, n_features=1,
#                                       n_informative=1, noise=10,
#                                       coef=True, random_state=0)

# # Add outlier data
# np.random.seed(0)
# X[:n_outliers] = 3 + 0.5 * np.random.normal(size=(n_outliers, 1))
# y[:n_outliers] = -3 + 10 * np.random.normal(size=n_outliers)

# # Fit line using all data
# model = linear_model.LinearRegression()
# model.fit(X, y)

# # Robustly fit linear model with RANSAC algorithm
# model_ransac = linear_model.RANSACRegressor(linear_model.LinearRegression())
# model_ransac.fit(X, y)
# inlier_mask = model_ransac.inlier_mask_
# outlier_mask = np.logical_not(inlier_mask)

# # Predict data of estimated models
# line_X = np.arange(-5, 5)
# line_y = model.predict(line_X[:, np.newaxis])
# line_y_ransac = model_ransac.predict(line_X[:, np.newaxis])

# # Compare estimated coefficients
# print("Estimated coefficients (true, normal, RANSAC):")
# print(coef, model.coef_, model_ransac.estimator_.coef_)

# plt.plot(X[inlier_mask], y[inlier_mask], '.g', label='Inliers')
# plt.plot(X[outlier_mask], y[outlier_mask], '.r', label='Outliers')
# plt.plot(line_X, line_y, '-k', label='Linear regressor')
# plt.plot(line_X, line_y_ransac, '-b', label='RANSAC regressor')
# plt.legend(loc='lower right')
# plt.show()
<|endoftext|>