<commit_before>

__author__ = 'scribblemaniac'

from renderchan.module import RenderChanModule
from renderchan.utils import which
from distutils.spawn import find_executable
import subprocess
import os
import re
import random

class RenderChanGimpModule(RenderChanModule):
    def __init__(self):
        RenderChanModule.__init__(self)
        if os.name == 'nt':
            self.conf['binary']=os.path.join(os.path.dirname(__file__),"..\\..\\..\\gimp\\gimp.exe")
        else:
            self.conf['binary']="gimp"
        self.conf["packetSize"]=0

    def getInputFormats(self):
        return ["xcf", "fli", "flc", "dds", "dcm", "dicom", "eps", "fit", "fits", "g3", "xjt", "cel", "wmf", "ico", "pnm", "ppm", "pgm", "pbm", "psp", "psd", "pdf", "ps", "tiff", "bmp", "xbm", "xwd", "pcx", "pcc"]

    def getOutputFormats(self):
        return ["png", "jpg", "jpeg", "pdf", "psd", "tif", "tiff", "bmp", "ico", "txt", "html", "gif", "mng"]

    def checkRequirements(self):
        binary_path = which(self.conf['binary'])
        if binary_path == None:
            self.active=False
            print "Module warning (%s): Cannot find '%s' executable." % (self.getName(), self.conf["binary"])
            print "    Please install GIMP package."
        else:
            self.active=True
            # Workaround because the gimp binary cannot be executed via symlink
            self.conf['binary'] = binary_path
        return self.active

    def render(self, filename, outputPath, startFrame, endFrame, format, updateCompletion, extraParams={}):

        comp=0.0
        updateCompletion(comp)

        # Determines drawable (depending on layer support)
        if format in ("gif", "ico", "mng", "psd"):
            drawable="gimp-image-get-active-drawable image"
        else:
            drawable="gimp-image-merge-visible-layers image CLIP-TO-IMAGE"

        # Preprocessing procedures
        if format == "gif":
            preprocedure="(gimp-image-convert-indexed image 0 0 255 0 0 \"\")"
        else:
            preprocedure=""

        # Determine procedure name
        if format in ("png", "tiff"):
            saveProcedure="file-%s-save2" % format
        elif format in ("txt", "html"):
            saveProcedure="file-aa-save"
        elif format == "jpg":
            saveProcedure="file-jpeg-save"
        elif format == "tif":
            saveProcedure="file-tiff-save2"
        elif format in ("jpeg", "pdf", "psd", "gif", "mng"):
            saveProcedure="file-%s-save" % format
        else:
            saveProcedure="gimp-file-save"

        # Determine paramters
        if format == "png":
            # PNG get special treatment because default parameters can be fetched from GIMP
            saveParameters=" ".join(subprocess.check_output([self.conf['binary'], "-i", "-b", "(let ((str \"\") (defaults (file-png-get-defaults))) (gimp-message (do ((i 0 (+ i 1))) ((= i 9) str) (set! str (string-append str (number->string (nth i defaults)))))))", "-b", "(gimp-quit 0)"], stderr=subprocess.STDOUT)[19:28])
            #saveParameters="0 9 0 0 0 1 0 0 0"
        elif format in ("jpg", "jpeg"):
            saveParameters=".9 0 0 0 \" \" 0 1 0 1"
        elif format == "pdf":
            saveParameters="0 1 1"
        elif format == "psd":
            saveParameters="1 1"
        elif format in ("tif", "tiff"):
            saveParameters="1 0"
        elif format == "txt":
            saveParameters="\"Text file\""
        elif format == "html":
            saveParameters="\"Pure html\""
        elif format == "gif":
            saveParameters="0 1 100 2"
        elif format == "mng":
            saveParameters="0 9 .9 0 1 100 2 1 0 0 1 0"
        else:
            saveParameters=""

        # See docs for readable script-fu code
        commandline=[self.conf['binary'], "-i", "-b", "(let*  ((filename \"%s\") (outpath \"%s\") (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename))) (drawable (car (%s)))) %s (gimp-image-scale image %s %s) (%s RUN-NONINTERACTIVE image drawable outpath outpath %s) (gimp-image-delete image))" % (filename, outputPath, drawable, preprocedure, extraParams['width'], extraParams['height'], saveProcedure, saveParameters), "-b", "(gimp-quit 0)"]

        subprocess.check_call(commandline)

        updateCompletion(1.0)
<commit_msg>Gimp: Use predefined parameters for PNG export<commit_after>

__author__ = 'scribblemaniac'

from renderchan.module import RenderChanModule
from renderchan.utils import which
from distutils.spawn import find_executable
import subprocess
import os
import re
import random

class RenderChanGimpModule(RenderChanModule):
    def __init__(self):
        RenderChanModule.__init__(self)
        if os.name == 'nt':
            self.conf['binary']=os.path.join(os.path.dirname(__file__),"..\\..\\..\\gimp\\gimp.exe")
        else:
            self.conf['binary']="gimp"
        self.conf["packetSize"]=0

    def getInputFormats(self):
        return ["xcf", "fli", "flc", "dds", "dcm", "dicom", "eps", "fit", "fits", "g3", "xjt", "cel", "wmf", "ico", "pnm", "ppm", "pgm", "pbm", "psp", "psd", "pdf", "ps", "tiff", "bmp", "xbm", "xwd", "pcx", "pcc"]

    def getOutputFormats(self):
        return ["png", "jpg", "jpeg", "pdf", "psd", "tif", "tiff", "bmp", "ico", "txt", "html", "gif", "mng"]

    def checkRequirements(self):
        binary_path = which(self.conf['binary'])
        if binary_path == None:
            self.active=False
            print "Module warning (%s): Cannot find '%s' executable." % (self.getName(), self.conf["binary"])
            print "    Please install GIMP package."
        else:
            self.active=True
            # Workaround because the gimp binary cannot be executed via symlink
            self.conf['binary'] = binary_path
        return self.active

    def render(self, filename, outputPath, startFrame, endFrame, format, updateCompletion, extraParams={}):

        comp=0.0
        updateCompletion(comp)

        # Determines drawable (depending on layer support)
        if format in ("gif", "ico", "mng", "psd"):
            drawable="gimp-image-get-active-drawable image"
        else:
            drawable="gimp-image-merge-visible-layers image CLIP-TO-IMAGE"

        # Preprocessing procedures
        if format == "gif":
            preprocedure="(gimp-image-convert-indexed image 0 0 255 0 0 \"\")"
        else:
            preprocedure=""

        # Determine procedure name
        if format in ("png", "tiff"):
            saveProcedure="file-%s-save2" % format
        elif format in ("txt", "html"):
            saveProcedure="file-aa-save"
        elif format == "jpg":
            saveProcedure="file-jpeg-save"
        elif format == "tif":
            saveProcedure="file-tiff-save2"
        elif format in ("jpeg", "pdf", "psd", "gif", "mng"):
            saveProcedure="file-%s-save" % format
        else:
            saveProcedure="gimp-file-save"

        # Determine paramters
        if format == "png":
            # PNG get special treatment because default parameters can be fetched from GIMP
            #saveParameters=" ".join(subprocess.check_output([self.conf['binary'], "-i", "-b", "(let ((str \"\") (defaults (file-png-get-defaults))) (gimp-message (do ((i 0 (+ i 1))) ((= i 9) str) (set! str (string-append str (number->string (nth i defaults)))))))", "-b", "(gimp-quit 0)"], stderr=subprocess.STDOUT)[19:28])
            #print saveParameters
            saveParameters="0 9 0 0 0 1 0 0 0"
        elif format in ("jpg", "jpeg"):
            saveParameters=".9 0 0 0 \" \" 0 1 0 1"
        elif format == "pdf":
            saveParameters="0 1 1"
        elif format == "psd":
            saveParameters="1 1"
        elif format in ("tif", "tiff"):
            saveParameters="1 0"
        elif format == "txt":
            saveParameters="\"Text file\""
        elif format == "html":
            saveParameters="\"Pure html\""
        elif format == "gif":
            saveParameters="0 1 100 2"
        elif format == "mng":
            saveParameters="0 9 .9 0 1 100 2 1 0 0 1 0"
        else:
            saveParameters=""

        # See docs for readable script-fu code
        commandline=[self.conf['binary'], "-i", "-b", "(let*  ((filename \"%s\") (outpath \"%s\") (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename))) (drawable (car (%s)))) %s (gimp-image-scale image %s %s) (%s RUN-NONINTERACTIVE image drawable outpath outpath %s) (gimp-image-delete image))" % (filename, outputPath, drawable, preprocedure, extraParams['width'], extraParams['height'], saveProcedure, saveParameters), "-b", "(gimp-quit 0)"]

        subprocess.check_call(commandline)

        updateCompletion(1.0)
<|endoftext|>


<commit_before><commit_msg>Pretty printing source and sink types&counts<commit_after><|endoftext|>


<commit_before>from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save, pre_delete

class ImagerProfile(models.Model):

    associated_user = models.OneToOneField(User)

    picture = models.FileField(blank=True)
    picture_privacy = models.BooleanField(default=True)

    phone_number = models.CharField(max_length=20, blank=True)
    phone_privacy = models.BooleanField(default=True)

    birthday = models.DateField(null=True, blank=True)
    birthday_privacy = models.BooleanField(default=True)

    name_privacy = models.BooleanField(default=True)
    email_privacy = models.BooleanField(default=True)

    def user(self):
        return self.associated_user.username

    def is_active(self):
        return self.associated_user.is_active

    @classmethod
    def active(self):
        qs = self.get_queryset()
        return qs.filter(associated_user__is_active=True)

    def create_profile(sender, **kwargs):
        if kwargs["created"]:
            ip = ImagerProfile(associated_user=kwargs["instance"])
            ip.save()

    def delete_user(sender, instance, *args, **kwargs):
        if instance.associated_user:
            instance.associated_user.delete()

    post_save.connect(create_profile, sender=User)

    pre_delete.connect(delete_user, sender=ImagerProfile)
<commit_msg>comment out unworking delete_user.<commit_after>from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save, pre_delete

class ImagerProfile(models.Model):

    associated_user = models.OneToOneField(User)

    picture = models.FileField(blank=True)
    picture_privacy = models.BooleanField(default=True)

    phone_number = models.CharField(max_length=20, blank=True)
    phone_privacy = models.BooleanField(default=True)

    birthday = models.DateField(null=True, blank=True)
    birthday_privacy = models.BooleanField(default=True)

    name_privacy = models.BooleanField(default=True)
    email_privacy = models.BooleanField(default=True)

    def user(self):
        return self.associated_user.username

    def is_active(self):
        return self.associated_user.is_active

    @classmethod
    def active(self):
        qs = self.get_queryset()
        return qs.filter(associated_user__is_active=True)

    def create_profile(sender, **kwargs):
        if kwargs["created"]:
            ip = ImagerProfile(associated_user=kwargs["instance"])
            ip.save()

    # def delete_user(sender, instance, *args, **kwargs):
    #     if instance.associated_user:
    #         instance.associated_user.delete()

    post_save.connect(create_profile, sender=User)

    # pre_delete.connect(delete_user, sender=ImagerProfile)
<|endoftext|>


<commit_before>from abc import ABCMeta, abstractmethod
import math
import sys
import uuid
import timeit
import numpy
# import theano
# import theano.tensor as T

class NeuronConnection(object):
    def __init__(self, sender, receiver):
        self.id = uuid.uuid4().hex
        self.sender = sender
        self.receiver = receiver
        self.weight = 1.0
        self.signalSent = 0.0
        self.signalReceived = 0.0

    def sendSignal(self, value):
        self.signalReceived = value
        self.signalSent = value * self.weight
        self.receiver.receiveSignal(self.signalSent, self.id)

    def disconnect(self):
        self.sender.removeOutbound(self)
        self.receiver.removeInbound(self)

class Neuron(object):
    # __metaclass__ = ABCMeta

    def __init__(self, activationFn, bias = 0.0):
        self.bias = bias
        self.activationFn = activationFn
        self.outConnections = set()
        self.inConnections = set()
        self.accumulatedSignals = 0.0
        self.output = 0.0

    def fire(self):
        for c in self.outConnections:
            c.sendSignal(self.output)

    # This creates a one way connection from this neuron to the passed in one.
    def connectTo(self, node):
        connection = NeuronConnection(self, node)
        self.addOutbound(connection)
        node.addInbound(connection)
        return connection

    def addOutbound(self, connection):
        self.outConnections.add(connection)
        self.onOutboundAdded(connection)

    def addInbound(self, connection):
        self.inConnections.add(connection)
        self.onInboundAdded(connection)

    def removeOutbound(self, connection):
        self.outConnections.remove(connection)
        self.onOutboundRemoved(connection)

    def removeInbound(self, connection):
        self.inConnections.remove(connection)
        self.onInboundRemoved(connection)

    def receiveSignal(self, value, connectionId = None):
        self.accumulatedSignals += value
        self.onSignalReceived(value, connectionId)

    # Overridable event methods
    def onInboundAdded(self, connection): pass
    def onInboundRemoved(self, connection): pass
    def onOutboundAdded(self, connection): pass
    def onOutboundRemoved(self, connection): pass
    def onSignalReceived(self, value, connectionId): pass

# This type is like most models. It requires all of the signals to have been received
# before sending any of its connections.
class ReceiveAllNeuron(Neuron):
    def __init__(self, activationFn, bias = 0.0):
        # Call inherited class.
        Neuron.__init__(self, activationFn, bias)
        self.weighedAverage = 0.0
        self.signalReceivedTracker = {}

    def reset(self):
        # gets the neruon ready to fire again
        self.weighedAverage = 0.0
        self.output = 0.0
        for key in self.signalReceivedTracker:
            self.signalReceivedTracker[key] = False

    def onInboundAdded(self, connection):
        self.signalReceivedTracker[connection.id] = False

    def onInboundRemoved(self, connection):
        self.signalReceivedTracker.pop(connection.id, None)

    def onSignalReceived(self, value, connectionId):
        # If all signals have been received, the output is set
        # and the node fires.
        if (len(self.signalReceivedTracker) and
            self.signalReceivedTracker[connectionId]): return

        if (connectionId):
            self.signalReceivedTracker[connectionId] = True

        self.weighedAverage += value

        if (not self.allSignalsReceived()): return

        self.output = self.activationFn(self.bias + self.weighedAverage)
        self.fire()

    def allSignalsReceived(self):
        return all(self.signalReceivedTracker.values())

class NetConfig(object):
    def __init__(self):
        self.inputCount = 0
        self.hiddenCount = 0
        self.outputCount = 0
        self.inputActivationFn = None # TODO: choose a default
        self.hiddenActivationFn = None # TODO: choose a default
        self.outputActivationFn = None # TODO: choose a default

class SimpleFeedForwardNN(object):
    def __init__(self, normalizers, nHidden = 5):
        self.input = ReceiveAllNeuron(normalizers['in'])
        # TODO: Implement the activation functions using your own methods, then
        # make one that uses the theano methods.
        self.hidden = [ReceiveAllNeuron(sigmoid) for n in range(nHidden)]
        self.output = ReceiveAllNeuron(normalizers['out'])

        # Connect the neurons
        for h in self.hidden:
            self.input.connectTo(h)
            h.connectTo(self.output)

def sigmoid(x):
    return 1.0 / (1.0 + math.exp(x))

def normalizers(range, offset):
    return {
        'in': lambda x: (x + offset) / range,
        'out': lambda x: (x * range) - offset
        }

if (__name__ == '__main__'):
    net = SimpleFeedForwardNN(normalizers(2, 1))
    testFn = lambda x: math.sin(x)

# TASK
# 1) Implement a simple feed forward neural network
# 2) Implement a backpropagation training algorithm for the simple network
# 3) Train the network to represent a simple forth degree quadratic function

"""
    http://www.iro.umontreal.ca/~bengioy/dlbook/mlp.html#pf2
    f?(x) = b + V sigmoid(c + W x),
"""<commit_msg>Closed #1<commit_after>from abc import ABCMeta, abstractmethod
import math
import sys
import uuid
import timeit
import numpy
# import theano
# import theano.tensor as T

class NeuronConnection(object):
    def __init__(self, sender, receiver):
        self.id = uuid.uuid4().hex
        self.sender = sender
        self.receiver = receiver
        self.weight = 1.0
        self.signalSent = 0.0
        self.signalReceived = 0.0

    def sendSignal(self, value):
        self.signalReceived = value
        self.signalSent = value * self.weight
        self.receiver.receiveSignal(self.signalSent, self.id)

    def disconnect(self):
        self.sender.removeOutbound(self)
        self.receiver.removeInbound(self)

class Neuron(object):
    # __metaclass__ = ABCMeta

    def __init__(self, activationFn, bias = 0.0):
        self.bias = bias
        self.activationFn = activationFn
        self.outConnections = set()
        self.inConnections = set()
        self.accumulatedSignals = 0.0
        self.output = 0.0

    def fire(self):
        for c in self.outConnections:
            c.sendSignal(self.output)

    # This creates a one way connection from this neuron to the passed in one.
    def connectTo(self, node):
        connection = NeuronConnection(self, node)
        self.addOutbound(connection)
        node.addInbound(connection)
        return connection

    def addOutbound(self, connection):
        self.outConnections.add(connection)
        self.onOutboundAdded(connection)

    def addInbound(self, connection):
        self.inConnections.add(connection)
        self.onInboundAdded(connection)

    def removeOutbound(self, connection):
        self.outConnections.remove(connection)
        self.onOutboundRemoved(connection)

    def removeInbound(self, connection):
        self.inConnections.remove(connection)
        self.onInboundRemoved(connection)

    def receiveSignal(self, value, connectionId = None):
        self.accumulatedSignals += value
        self.onSignalReceived(value, connectionId)

    # Overridable event methods
    def onInboundAdded(self, connection): pass
    def onInboundRemoved(self, connection): pass
    def onOutboundAdded(self, connection): pass
    def onOutboundRemoved(self, connection): pass
    def onSignalReceived(self, value, connectionId): pass

# This type is like most models. It requires all of the signals to have been received
# before sending any of its connections.
class ReceiveAllNeuron(Neuron):
    def __init__(self, activationFn, bias = 0.0):
        # Call inherited class.
        Neuron.__init__(self, activationFn, bias)
        self.weighedAverage = 0.0
        self.signalReceivedTracker = {}

    def reset(self):
        # gets the neruon ready to fire again
        self.weighedAverage = 0.0
        self.output = 0.0
        for key in self.signalReceivedTracker:
            self.signalReceivedTracker[key] = False

    def onInboundAdded(self, connection):
        self.signalReceivedTracker[connection.id] = False

    def onInboundRemoved(self, connection):
        self.signalReceivedTracker.pop(connection.id, None)

    def onSignalReceived(self, value, connectionId):
        # If all signals have been received, the output is set
        # and the node fires.
        if (len(self.signalReceivedTracker) and
            self.signalReceivedTracker[connectionId]): return

        if (connectionId):
            self.signalReceivedTracker[connectionId] = True

        self.weighedAverage += value

        if (not self.allSignalsReceived()): return

        self.output = self.activationFn(self.bias + self.weighedAverage)
        self.fire()

    def allSignalsReceived(self):
        return all(self.signalReceivedTracker.values())

class NetConfig(object):
    def __init__(self):
        self.inputCount = 0
        self.hiddenCount = 0
        self.outputCount = 0
        self.inputActivationFn = None # TODO: choose a default
        self.hiddenActivationFn = None # TODO: choose a default
        self.outputActivationFn = None # TODO: choose a default

class SimpleFeedForwardNN(object):
    def __init__(self, normalizers, nHidden = 5):
        self.input = ReceiveAllNeuron(normalizers['in'])
        # TODO: Implement the activation functions using your own methods, then
        # make one that uses the theano methods.
        self.hidden = [ReceiveAllNeuron(sigmoid) for n in range(nHidden)]
        self.output = ReceiveAllNeuron(normalizers['out'])

        # Connect the neurons
        for h in self.hidden:
            self.input.connectTo(h)
            h.connectTo(self.output)

def sigmoid(x):
    return 1.0 / (1.0 + math.exp(x))

def normalizers(range, offset):
    return {
        'in': lambda x: (x + offset) / range,
        'out': lambda x: (x * range) - offset
        }

if (__name__ == '__main__'):
    net = SimpleFeedForwardNN(normalizers(2, 1))
    testFn = lambda x: math.sin(x)

# TASK
# 1) [Done] Implement a simple feed forward neural network
# 2) Implement a backpropagation training algorithm for the simple network
# 3) Train simple network to represent a simple forth degree quadratic function

"""
    http://www.iro.umontreal.ca/~bengioy/dlbook/mlp.html#pf2
    f?(x) = b + V sigmoid(c + W x),
"""<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#   OpenERP, Open Source Management Solution
#   Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as
#   published by the Free Software Foundation, either version 3 of the
#   License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import logging
from openerp.osv import fields, osv;
from datetime import datetime, timedelta, date
from dateutil import parser
from dateutil import rrule
from dateutil.relativedelta import relativedelta
import time
import re
import pytz
import math
import copy
from openerp.tools.translate import _
from openerp import SUPERUSER_ID, tools
import urllib
import urllib2

_logger = logging.getLogger(__name__)

class doctor(osv.osv):

	_name = 'doctor.doctor'

	_columns = {
		'test': fields.char('test')
	}

	def fecha_UTC(self,fecha_usuario,context=None):

		if not context:
			context = {}

		#Obtener TimeZone Usuario
		tz = context.get('tz','America/Bogota')

		if not tz:
			raise osv.except_osv(_('Zona Horaria - Clinica Digital'),
								 _('Por favor configure su zona horaria para acceder a esta funcionalidad del sistema o solicite soporte.'))
		#Formato
		localFormat = "%Y-%m-%d %H:%M:%S"

		fecha_sin_tz = datetime.strptime(fecha_usuario, localFormat)
		tz_usuario = pytz.timezone(tz)
		fecha_tz = tz_usuario.localize(fecha_sin_tz)
		fecha_con_utc = fecha_tz.astimezone(pytz.utc)
		fecha_utc = fecha_con_utc.strftime(localFormat)

		return fecha_utc

	def _date_to_dateuser(self, cr, uid, date_begin):
		date_begin_user = datetime.strptime(date_begin, "%Y-%m-%d %H:%M:%S")

		user = self.pool.get('res.users').browse(cr, uid, uid)
		tz = pytz.timezone(user.tz) if user.tz else pytz.utc
		date_begin_user = pytz.utc.localize(date_begin_user).astimezone(tz)

		date_begin_user = datetime.strftime(date_begin_user, "%Y-%m-%d %H:%M:%S")
		return date_begin_user

	def modulo_instalado(self, cr, uid, nombre_modulo,context=None):

		modulo_instalado = self.pool.get('ir.module.module').search(cr,uid,[('name', '=', nombre_modulo), ('state', '=', 'installed')],context=context)
		if modulo_instalado:
			return True
		return False

	def company_nombre(self, cr, uid, context=None):

		if not context:
			context = {}

		nombre_compania = self.pool.get("res.users").browse(cr, uid, uid, context=context).company_id.name

		return nombre_compania

	def finalidad_consulta_db(self, cr, uid, context=None):

		finalidad_consulta = '07'

		if cr.dbname == 'DraConstanzaCastilla':
			finalidad_consulta = '04'
		if cr.dbname == 'Tomatis':
			finalidad_consulta = '10'
		if cr.dbname == 'DrCaceres':
			finalidad_consulta = '08'

		return finalidad_consulta

	def causa_externa(self, cr, uid, context=None):
		causa_externa='13'

		if cr.dbname == 'Tomatis':
			causa_externa='15'

		return causa_externa

	def fecha_utc(self, cr, uid, time_begin):
		time_begin_user = datetime.strptime(time_begin, "%Y-%m-%d %H:%M:%S")

		user = self.pool.get('res.users').browse(cr, uid, uid)
		tz = pytz.timezone(user.tz) if user.tz else pytz.utc
		time_begin_user = pytz.utc.localize(time_begin_user).astimezone(tz)

		time_begin_user = datetime.strftime(time_begin_user, "%Y-%m-%d %H:%M:%S")
		return time_begin_user

	def _model_default_get(self, cr, uid, model=False, fields=False, context=None):

		"""
		Retornar id de registro segun modelo
		@param model: Modelo donde se encuentra el registro
		@param fields: Condicionadores para encontrar registro ej. [ ('name', '=', 'ARMENIA') ]
		@return: id registro
		"""

		if not context:
			context = {}

		#_logger.info('Object  = %s' , model)
		#_logger.info('field  = %s' , fields)

		objectPool = self.pool.get(model)
		#Proceder a buscar el dato si, el modelo Existe
		if objectPool:
			objectSearch = objectPool.search(cr, uid,fields)
			objectBrowse = self.pool.get(model).browse(cr,uid,objectSearch)
			if objectBrowse:
				idObject = 0
				for obj in objectBrowse:
					idObject = obj.id
				return idObject
			else:
				return None

		else :
			_logger.warning('No se encontro el modelo (%s) , no se retornara el ID' , model)
			return None

	def tipo_documento(self, tipo):

		nombre_tipo = None

		if tipo == '13':
			nombre_tipo = 'CC'
		elif tipo == '11':
			nombre_tipo = 'RC'
		elif tipo == '12':
			nombre_tipo = 'TI'
		elif tipo == '21':
			nombre_tipo = 'CE'
		elif tipo == '41':
			nombre_tipo = 'Pasaporte'
		elif tipo == 'NU':
			nombre_tipo = 'NU'
		elif tipo == 'AS':
			nombre_tipo = 'AS'
		elif tipo == 'MS':
			nombre_tipo = 'MS'

		return nombre_tipo

	def causa_externa_rips(self, numero_causa):

		nombre_causa = None

		if numero_causa == '01':
			nombre_causa = 'Accidente de trabajo'
		elif numero_causa == '02':
			nombre_causa = u'Accidente de tránsito'
		elif numero_causa == '03':
			nombre_causa = u'Accidente rábico'
		elif numero_causa == '04':
			nombre_causa = u'Accidente ofídico'
		elif numero_causa == '05':
			nombre_causa = 'Otro tipo de accidente'
		elif numero_causa == '06':
			nombre_causa = u'Evento Catastrófico'
		elif numero_causa == '07':
			nombre_causa = u'Lesión por agresión'
		elif numero_causa == '08':
			nombre_causa = u'Lesión auto infligida'
		elif numero_causa == '09':
			nombre_causa = u'Sospecha de maltrato físico'
		elif numero_causa == '10':
			nombre_causa = 'Sospecha de abuso sexual'
		elif numero_causa == '11':
			nombre_causa = 'Sospecha de abuso sexual'
		elif numero_causa == '12':
			nombre_causa = 'Sospecha de maltrato emocional'
		elif numero_causa == '13':
			nombre_causa = 'Enfermedad general'
		elif numero_causa == '14':
			nombre_causa = 'Enfermedad profesional'
		elif numero_causa == '15':
			nombre_causa = 'Otra'
		return nombre_causa

	def finalidad_consulta_rips(self, numero_finalidad):

		nombre_finalidad = None

		if numero_finalidad == '01':
			nombre_finalidad = u'Atención del parto -puerperio'
		elif numero_finalidad == '02':
			nombre_finalidad = u'Atención del recién nacido'
		elif numero_finalidad == '03':
			nombre_finalidad = u'Atención en planificación familiar'
		elif numero_finalidad == '04':
			nombre_finalidad = u'Detección de alteraciones del crecimiento y desarrollo del menor de diez años'
		elif numero_finalidad == '05':
			nombre_finalidad = u'Detección de alteración del desarrollo joven'
		elif numero_finalidad == '06':
			nombre_finalidad = u'Detección de alteraciones del embarazo'
		elif numero_finalidad == '07':
			nombre_finalidad = u'Detección de alteración del adulto'
		elif numero_finalidad == '08':
			nombre_finalidad = u'Detección de alteración de agudeza visual'
		elif numero_finalidad == '09':
			nombre_finalidad = u'Detección de enfermedad profesional'
		elif numero_finalidad == '10':
			nombre_finalidad = u'No aplica'
		return nombre_finalidad

	def obtener_ultimas_atenciones_paciente(self, cr, uid, modelo_buscar, tiempo, paciente, fecha_atencion, context=None):
		'''
		if modelo_buscar:

			fecha_cita = datetime.strptime(fecha_atencion, "%Y-%m-%d %H:%M:%S")

			fecha_atencion_horas_menos = fecha_cita - timedelta(hours=tiempo)

			ids_atenciones = self.pool.get(modelo_buscar).search(cr, uid, [('patient_id', '=', paciente),
																		('date_attention', '>=', str(fecha_atencion_horas_menos)),
																		('date_attention', '<=', str(fecha_cita)),
																		('origin', '<>', None)], context=context)

			if ids_atenciones:

				raise osv.except_osv(_('ATENCION !!!'),_('El paciente ya fue atendido por otro profesional en la salud'))
				#_logger.info('asasasasas')
		'''
		pass
	def tipo_historia(self, modelo):

		if modelo == 'doctor_psychology':
			return 'doctor.psicologia'

		if modelo == 'doctor_control':
			return 'doctor.hc.control'

		if modelo == 'doctor_dental_care':
			return 'doctor.hc.odontologia'

		if modelo == 'doctor_biological_risk':
			return 'doctor.atencion.ries.bio'

		if modelo == 'doctor' or modelo == '10n_co_doctor':
			return 'doctor.attentions'<commit_msg>agregando cambio, para que salga el mensaje si se atiende al paciente dos veces en menos de dos horas<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#   OpenERP, Open Source Management Solution
#   Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as
#   published by the Free Software Foundation, either version 3 of the
#   License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import logging
from openerp.osv import fields, osv;
from datetime import datetime, timedelta, date
from dateutil import parser
from dateutil import rrule
from dateutil.relativedelta import relativedelta
import time
import re
import pytz
import math
import copy
from openerp.tools.translate import _
from openerp import SUPERUSER_ID, tools
import urllib
import urllib2

_logger = logging.getLogger(__name__)

class doctor(osv.osv):

	_name = 'doctor.doctor'

	_columns = {
		'test': fields.char('test')
	}

	def fecha_UTC(self,fecha_usuario,context=None):

		if not context:
			context = {}

		#Obtener TimeZone Usuario
		tz = context.get('tz','America/Bogota')

		if not tz:
			raise osv.except_osv(_('Zona Horaria - Clinica Digital'),
								 _('Por favor configure su zona horaria para acceder a esta funcionalidad del sistema o solicite soporte.'))
		#Formato
		localFormat = "%Y-%m-%d %H:%M:%S"

		fecha_sin_tz = datetime.strptime(fecha_usuario, localFormat)
		tz_usuario = pytz.timezone(tz)
		fecha_tz = tz_usuario.localize(fecha_sin_tz)
		fecha_con_utc = fecha_tz.astimezone(pytz.utc)
		fecha_utc = fecha_con_utc.strftime(localFormat)

		return fecha_utc

	def _date_to_dateuser(self, cr, uid, date_begin):
		date_begin_user = datetime.strptime(date_begin, "%Y-%m-%d %H:%M:%S")

		user = self.pool.get('res.users').browse(cr, uid, uid)
		tz = pytz.timezone(user.tz) if user.tz else pytz.utc
		date_begin_user = pytz.utc.localize(date_begin_user).astimezone(tz)

		date_begin_user = datetime.strftime(date_begin_user, "%Y-%m-%d %H:%M:%S")
		return date_begin_user

	def modulo_instalado(self, cr, uid, nombre_modulo,context=None):

		modulo_instalado = self.pool.get('ir.module.module').search(cr,uid,[('name', '=', nombre_modulo), ('state', '=', 'installed')],context=context)
		if modulo_instalado:
			return True
		return False

	def company_nombre(self, cr, uid, context=None):

		if not context:
			context = {}

		nombre_compania = self.pool.get("res.users").browse(cr, uid, uid, context=context).company_id.name

		return nombre_compania

	def finalidad_consulta_db(self, cr, uid, context=None):

		finalidad_consulta = '07'

		if cr.dbname == 'DraConstanzaCastilla':
			finalidad_consulta = '04'
		if cr.dbname == 'Tomatis':
			finalidad_consulta = '10'
		if cr.dbname == 'DrCaceres':
			finalidad_consulta = '08'

		return finalidad_consulta

	def causa_externa(self, cr, uid, context=None):
		causa_externa='13'

		if cr.dbname == 'Tomatis':
			causa_externa='15'

		return causa_externa

	def fecha_utc(self, cr, uid, time_begin):
		time_begin_user = datetime.strptime(time_begin, "%Y-%m-%d %H:%M:%S")

		user = self.pool.get('res.users').browse(cr, uid, uid)
		tz = pytz.timezone(user.tz) if user.tz else pytz.utc
		time_begin_user = pytz.utc.localize(time_begin_user).astimezone(tz)

		time_begin_user = datetime.strftime(time_begin_user, "%Y-%m-%d %H:%M:%S")
		return time_begin_user

	def _model_default_get(self, cr, uid, model=False, fields=False, context=None):

		"""
		Retornar id de registro segun modelo
		@param model: Modelo donde se encuentra el registro
		@param fields: Condicionadores para encontrar registro ej. [ ('name', '=', 'ARMENIA') ]
		@return: id registro
		"""

		if not context:
			context = {}

		#_logger.info('Object  = %s' , model)
		#_logger.info('field  = %s' , fields)

		objectPool = self.pool.get(model)
		#Proceder a buscar el dato si, el modelo Existe
		if objectPool:
			objectSearch = objectPool.search(cr, uid,fields)
			objectBrowse = self.pool.get(model).browse(cr,uid,objectSearch)
			if objectBrowse:
				idObject = 0
				for obj in objectBrowse:
					idObject = obj.id
				return idObject
			else:
				return None

		else :
			_logger.warning('No se encontro el modelo (%s) , no se retornara el ID' , model)
			return None

	def tipo_documento(self, tipo):

		nombre_tipo = None

		if tipo == '13':
			nombre_tipo = 'CC'
		elif tipo == '11':
			nombre_tipo = 'RC'
		elif tipo == '12':
			nombre_tipo = 'TI'
		elif tipo == '21':
			nombre_tipo = 'CE'
		elif tipo == '41':
			nombre_tipo = 'Pasaporte'
		elif tipo == 'NU':
			nombre_tipo = 'NU'
		elif tipo == 'AS':
			nombre_tipo = 'AS'
		elif tipo == 'MS':
			nombre_tipo = 'MS'

		return nombre_tipo

	def causa_externa_rips(self, numero_causa):

		nombre_causa = None

		if numero_causa == '01':
			nombre_causa = 'Accidente de trabajo'
		elif numero_causa == '02':
			nombre_causa = u'Accidente de tránsito'
		elif numero_causa == '03':
			nombre_causa = u'Accidente rábico'
		elif numero_causa == '04':
			nombre_causa = u'Accidente ofídico'
		elif numero_causa == '05':
			nombre_causa = 'Otro tipo de accidente'
		elif numero_causa == '06':
			nombre_causa = u'Evento Catastrófico'
		elif numero_causa == '07':
			nombre_causa = u'Lesión por agresión'
		elif numero_causa == '08':
			nombre_causa = u'Lesión auto infligida'
		elif numero_causa == '09':
			nombre_causa = u'Sospecha de maltrato físico'
		elif numero_causa == '10':
			nombre_causa = 'Sospecha de abuso sexual'
		elif numero_causa == '11':
			nombre_causa = 'Sospecha de abuso sexual'
		elif numero_causa == '12':
			nombre_causa = 'Sospecha de maltrato emocional'
		elif numero_causa == '13':
			nombre_causa = 'Enfermedad general'
		elif numero_causa == '14':
			nombre_causa = 'Enfermedad profesional'
		elif numero_causa == '15':
			nombre_causa = 'Otra'
		return nombre_causa

	def finalidad_consulta_rips(self, numero_finalidad):

		nombre_finalidad = None

		if numero_finalidad == '01':
			nombre_finalidad = u'Atención del parto -puerperio'
		elif numero_finalidad == '02':
			nombre_finalidad = u'Atención del recién nacido'
		elif numero_finalidad == '03':
			nombre_finalidad = u'Atención en planificación familiar'
		elif numero_finalidad == '04':
			nombre_finalidad = u'Detección de alteraciones del crecimiento y desarrollo del menor de diez años'
		elif numero_finalidad == '05':
			nombre_finalidad = u'Detección de alteración del desarrollo joven'
		elif numero_finalidad == '06':
			nombre_finalidad = u'Detección de alteraciones del embarazo'
		elif numero_finalidad == '07':
			nombre_finalidad = u'Detección de alteración del adulto'
		elif numero_finalidad == '08':
			nombre_finalidad = u'Detección de alteración de agudeza visual'
		elif numero_finalidad == '09':
			nombre_finalidad = u'Detección de enfermedad profesional'
		elif numero_finalidad == '10':
			nombre_finalidad = u'No aplica'
		return nombre_finalidad

	def obtener_ultimas_atenciones_paciente(self, cr, uid, modelo_buscar, tiempo, paciente, fecha_atencion, context=None):

		if modelo_buscar:

			fecha_cita = datetime.strptime(fecha_atencion, "%Y-%m-%d %H:%M:%S")

			fecha_atencion_horas_menos = fecha_cita - timedelta(hours=tiempo)

			ids_atenciones = self.pool.get(modelo_buscar).search(cr, uid, [('patient_id', '=', paciente),
																		('date_attention', '>=', str(fecha_atencion_horas_menos)),
																		('date_attention', '<=', str(fecha_cita)),
																		('origin', '<>', None)], context=context)

			if ids_atenciones:

				raise osv.except_osv(_('ATENCION !!!'),_('El paciente ya fue atendido por otro profesional en la salud'))
				#_logger.info('asasasasas')

	def tipo_historia(self, modelo):

		if modelo == 'doctor_psychology':
			return 'doctor.psicologia'

		if modelo == 'doctor_control':
			return 'doctor.hc.control'

		if modelo == 'doctor_dental_care':
			return 'doctor.hc.odontologia'

		if modelo == 'doctor_biological_risk':
			return 'doctor.atencion.ries.bio'

		if modelo == 'doctor' or modelo == '10n_co_doctor':
			return 'doctor.attentions'<|endoftext|>


<commit_before>######################################################################
# Copyright (C) 2014 Jaakko Luttinen
#
# This file is licensed under Version 3.0 of the GNU General Public
# License. See LICENSE for a text of the license.
######################################################################

######################################################################
# This file is part of BayesPy.
#
# BayesPy is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# BayesPy is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BayesPy.  If not, see <http://www.gnu.org/licenses/>.
######################################################################

"""
Demonstrate the linear state-space model with switching dynamics.

The model differs from the classical linear state-space model in that it has a
set of state dynamics matrices of which one is used at each time instance.  A
hidden Markov model is used to select the dynamics matrix.

Some functions in this module are re-usable:
  * ``model`` can be used to construct the LSSM with switching dynamics.
  * ``infer`` can be used to apply the model to given data.
"""

import numpy as np
import matplotlib.pyplot as plt

from bayespy.nodes import (GaussianARD,
                           SwitchingGaussianMarkovChain,
                           CategoricalMarkovChain,
                           Dirichlet,
                           Mixture,
                           Gamma,
                           SumMultiply)

from bayespy.inference.vmp.vmp import VB
from bayespy.inference.vmp import transformations

import bayespy.plot.plotting as bpplt

def model(M=20, N=100, D=10, K=3):
    """
    Construct the linear state-space model with switching dynamics.
    """

    #
    # Switching dynamics (HMM)
    #

    # Prior for initial state probabilities
    rho = Dirichlet(1e-3*np.ones(K),
                    name='rho')

    # Prior for state transition probabilities
    V = Dirichlet(1e-3*np.ones(K),
                  plates=(K,),
                  name='V')
    V.initialize_from_value(10*np.identity(K) + 1*np.ones((K,K)))

    # Hidden states (with unknown initial state probabilities and state
    # transition probabilities)
    Z = CategoricalMarkovChain(rho, V,
                               states=N-1,
                               name='Z',
                               plotter=bpplt.CategoricalMarkovChainPlotter(),
                               initialize=False)
    Z.u[0] = np.random.dirichlet(np.ones(K))
    Z.u[1] = np.reshape(np.random.dirichlet(0.5*np.ones(K*K), size=(N-2)),
                        (N-2, K, K))

    #
    # Linear state-space models
    #

    # Dynamics matrix with ARD
    # (K,D) x ()
    alpha = Gamma(1e-5,
                  1e-5,
                  plates=(K,1,D),
                  name='alpha')
    # (K,1,1,D) x (D)
    A = GaussianARD(0,
                    alpha,
                    shape=(D,),
                    plates=(K,D),
                    name='A',
                    plotter=bpplt.GaussianHintonPlotter())
    A.initialize_from_value(np.identity(D)*np.ones((K,D,D))
                            + 0.1*np.random.randn(K,D,D))

    # Latent states with dynamics
    # (K,1) x (N,D)
    X = SwitchingGaussianMarkovChain(np.zeros(D),         # mean of x0
                                     1e-3*np.identity(D), # prec of x0
                                     A,                   # dynamics
                                     Z,                   # dynamics selection
                                     np.ones(D),          # innovation
                                     n=N,                 # time instances
                                     name='X',
                                     plotter=bpplt.GaussianMarkovChainPlotter())
    X.initialize_from_value(10*np.random.randn(N,D))

    # Mixing matrix from latent space to observation space using ARD
    # (K,1,1,D) x ()
    gamma = Gamma(1e-5,
                  1e-5,
                  plates=(D,),
                  name='gamma')
    # (K,M,1) x (D)
    C = GaussianARD(0,
                    gamma,
                    shape=(D,),
                    plates=(M,1),
                    name='C',
                    plotter=bpplt.GaussianHintonPlotter(rows=-3,cols=-1))
    C.initialize_from_value(np.random.randn(M,1,D))

    # Underlying noiseless function
    # (K,M,N) x ()
    F = SumMultiply('i,i',
                    C,
                    X,
                    name='F')

    #
    # Mixing the models
    #

    # Observation noise
    tau = Gamma(1e-5,
                1e-5,
                name='tau')
    tau.initialize_from_value(1e2)

    # Emission/observation distribution
    Y = GaussianARD(F, tau,
                    name='Y')

    Q = VB(Y, F,
           Z, rho, V,
           C, gamma, X, A, alpha,
           tau)

    return Q

def infer(y, D, K, rotate=True, debug=False, maxiter=100, mask=True,
          monitor=False, update_hyper=0, autosave=None):
    """
    Apply LSSM with switching dynamics to the given data.
    """

    (M, N) = np.shape(y)

    # Construct model
    Q = model(M=M, K=K, N=N, D=D)

    if autosave is not None:
        Q.set_autosave(autosave, iterations=10)

    Q['Y'].observe(y, mask=mask)

    # Set up rotation speed-up
    if rotate:
        # Initial rotate the D-dimensional state space (X, A, C)
        # Do not update hyperparameters
        rotA_init = transformations.RotateGaussianARD(Q['A'])
        rotX_init = transformations.RotateSwitchingMarkovChain(Q['X'],
                                                               Q['A'],
                                                               Q['Z'],
                                                               rotA_init)
        rotC_init = transformations.RotateGaussianARD(Q['C'])
        R_init = transformations.RotationOptimizer(rotX_init, rotC_init, D)
        # Rotate the D-dimensional state space (X, A, C)
        rotA = transformations.RotateGaussianARD(Q['A'],
                                                 Q['alpha'])
        rotX = transformations.RotateSwitchingMarkovChain(Q['X'],
                                                          Q['A'],
                                                          Q['Z'],
                                                          rotA)
        rotC = transformations.RotateGaussianARD(Q['C'],
                                                 Q['gamma'])
        R = transformations.RotationOptimizer(rotX, rotC, D)
        if debug:
            rotate_kwargs = {'maxiter': 10,
                             'check_bound': True,
                             'check_gradient': True}
        else:
            rotate_kwargs = {'maxiter': 10}

    # Run inference
    if monitor:
        Q.plot()
    for n in range(maxiter):
        if n < update_hyper:
            Q.update('X', 'C', 'A', 'tau', plot=monitor)
            if rotate:
                R_init.rotate(**rotate_kwargs)
        else:
            Q.update(plot=monitor)
            if rotate:
                R.rotate(**rotate_kwargs)

    return Q

def simulate_data(N):
    """
    Generate time-series data with switching dynamics.
    """

    # Two states: 1) oscillation, 2) random walk
    w1 = 0.02 * 2*np.pi
    A = [ [[np.cos(w1), -np.sin(w1)],
           [np.sin(w1),  np.cos(w1)]],
          [[        1.0,         0.0],
           [        0.0,         0.0]] ]
    C = [[1.0, 0.0]]

    # State switching probabilities
    q = 0.993        # probability to stay in the same state
    r = (1-q)/(2-1)  # probability to switch
    P = q*np.identity(2) + r*(np.ones((2,2))-np.identity(2))

    X = np.zeros((N, 2))
    Z = np.zeros(N)
    Y = np.zeros(N)
    F = np.zeros(N)
    z = np.random.randint(2)
    x = np.random.randn(2)
    Z[0] = z
    X[0,:] = x
    for n in range(1,N):
        x = np.dot(A[z], x) + np.random.randn(2)
        f = np.dot(C, x)
        y = f + 5*np.random.randn()
        z = np.random.choice(2, p=P[z])

        Z[n] = z
        X[n,:] = x
        Y[n] = y
        F[n] = f

    Y = Y[None,:]

    return (Y, F)

def demo(N=1000, maxiter=100, D=3, K=2, seed=42, plot=True, debug=False,
        rotate=True, monitor=True):
    """
    Run the demo for linear state-space model with switching dynamics.
    """

    # Use deterministic random numbers
    if seed is not None:
        np.random.seed(seed)

    # Generate data
    (Y, F) = simulate_data(N)

    # Plot observations
    if plot:
        plt.figure()
        bpplt.timeseries(F, 'b-')
        bpplt.timeseries(Y, 'rx')

    # Apply the linear state-space model with switching dynamics
    Q = infer(Y, D, K,
              debug=debug,
              maxiter=maxiter,
              monitor=monitor,
              rotate=rotate,
              update_hyper=5)

    # Show results
    if plot:
        Q.plot()
        plt.show()

    return

if __name__ == '__main__':
    import sys, getopt, os
    try:
        opts, args = getopt.getopt(sys.argv[1:],
                                   "",
                                   ["n=",
                                    "d=",
                                    "k=",
                                    "seed=",
                                    "debug",
                                    "no-rotation",
                                    "no-monitor",
                                    "no-plot",
                                    "maxiter="])
    except getopt.GetoptError:
        print('python lssm_sd.py <options>')
        print('--n=<INT>        Number of data vectors')
        print('--d=<INT>        Latent space dimensionality')
        print('--k=<INT>        Number of mixed models')
        print('--maxiter=<INT>  Maximum number of VB iterations')
        print('--seed=<INT>     Seed (integer) for the random number generator')
        print('--no-rotation    Do not peform rotation speed ups')
        print('--no-plot        Do not plot results')
        print('--no-monitor     Do not plot distributions during VB learning')
        print('--debug          Check that the rotations are implemented correctly')
        sys.exit(2)

    kwargs = {}
    for opt, arg in opts:
        if opt == "--maxiter":
            kwargs["maxiter"] = int(arg)
        elif opt == "--d":
            kwargs["D"] = int(arg)
        elif opt == "--k":
            kwargs["K"] = int(arg)
        elif opt == "--seed":
            kwargs["seed"] = int(arg)
        elif opt == "--no-rotation":
            kwargs["rotate"] = False
        elif opt == "--no-monitor":
            kwargs["monitor"] = False
        elif opt == "--no-plot":
            kwargs["plot"] = False
        elif opt == "--debug":
            kwargs["debug"] = True
        elif opt in ("--n",):
            kwargs["N"] = int(arg)
        else:
            raise ValueError("Unhandled option given")

    demo(**kwargs)
<commit_msg>DEMO: Add option to disable monitoring of the loading matrix<commit_after>######################################################################
# Copyright (C) 2014 Jaakko Luttinen
#
# This file is licensed under Version 3.0 of the GNU General Public
# License. See LICENSE for a text of the license.
######################################################################

######################################################################
# This file is part of BayesPy.
#
# BayesPy is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# BayesPy is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BayesPy.  If not, see <http://www.gnu.org/licenses/>.
######################################################################

"""
Demonstrate the linear state-space model with switching dynamics.

The model differs from the classical linear state-space model in that it has a
set of state dynamics matrices of which one is used at each time instance.  A
hidden Markov model is used to select the dynamics matrix.

Some functions in this module are re-usable:
  * ``model`` can be used to construct the LSSM with switching dynamics.
  * ``infer`` can be used to apply the model to given data.
"""

import numpy as np
import matplotlib.pyplot as plt

from bayespy.nodes import (GaussianARD,
                           SwitchingGaussianMarkovChain,
                           CategoricalMarkovChain,
                           Dirichlet,
                           Mixture,
                           Gamma,
                           SumMultiply)

from bayespy.inference.vmp.vmp import VB
from bayespy.inference.vmp import transformations

import bayespy.plot.plotting as bpplt

def model(M=20, N=100, D=10, K=3):
    """
    Construct the linear state-space model with switching dynamics.
    """

    #
    # Switching dynamics (HMM)
    #

    # Prior for initial state probabilities
    rho = Dirichlet(1e-3*np.ones(K),
                    name='rho')

    # Prior for state transition probabilities
    V = Dirichlet(1e-3*np.ones(K),
                  plates=(K,),
                  name='V')
    V.initialize_from_value(10*np.identity(K) + 1*np.ones((K,K)))

    # Hidden states (with unknown initial state probabilities and state
    # transition probabilities)
    Z = CategoricalMarkovChain(rho, V,
                               states=N-1,
                               name='Z',
                               plotter=bpplt.CategoricalMarkovChainPlotter(),
                               initialize=False)
    Z.u[0] = np.random.dirichlet(np.ones(K))
    Z.u[1] = np.reshape(np.random.dirichlet(0.5*np.ones(K*K), size=(N-2)),
                        (N-2, K, K))

    #
    # Linear state-space models
    #

    # Dynamics matrix with ARD
    # (K,D) x ()
    alpha = Gamma(1e-5,
                  1e-5,
                  plates=(K,1,D),
                  name='alpha')
    # (K,1,1,D) x (D)
    A = GaussianARD(0,
                    alpha,
                    shape=(D,),
                    plates=(K,D),
                    name='A',
                    plotter=bpplt.GaussianHintonPlotter())
    A.initialize_from_value(np.identity(D)*np.ones((K,D,D))
                            + 0.1*np.random.randn(K,D,D))

    # Latent states with dynamics
    # (K,1) x (N,D)
    X = SwitchingGaussianMarkovChain(np.zeros(D),         # mean of x0
                                     1e-3*np.identity(D), # prec of x0
                                     A,                   # dynamics
                                     Z,                   # dynamics selection
                                     np.ones(D),          # innovation
                                     n=N,                 # time instances
                                     name='X',
                                     plotter=bpplt.GaussianMarkovChainPlotter())
    X.initialize_from_value(10*np.random.randn(N,D))

    # Mixing matrix from latent space to observation space using ARD
    # (K,1,1,D) x ()
    gamma = Gamma(1e-5,
                  1e-5,
                  plates=(D,),
                  name='gamma')
    # (K,M,1) x (D)
    C = GaussianARD(0,
                    gamma,
                    shape=(D,),
                    plates=(M,1),
                    name='C',
                    plotter=bpplt.GaussianHintonPlotter(rows=-3,cols=-1))
    C.initialize_from_value(np.random.randn(M,1,D))

    # Underlying noiseless function
    # (K,M,N) x ()
    F = SumMultiply('i,i',
                    C,
                    X,
                    name='F')

    #
    # Mixing the models
    #

    # Observation noise
    tau = Gamma(1e-5,
                1e-5,
                name='tau')
    tau.initialize_from_value(1e2)

    # Emission/observation distribution
    Y = GaussianARD(F, tau,
                    name='Y')

    Q = VB(Y, F,
           Z, rho, V,
           C, gamma, X, A, alpha,
           tau)

    return Q

def infer(y, D, K, rotate=True, debug=False, maxiter=100, mask=True,
          plot_C=True, monitor=False, update_hyper=0, autosave=None):

    """
    Apply LSSM with switching dynamics to the given data.
    """

    (M, N) = np.shape(y)

    # Construct model
    Q = model(M=M, K=K, N=N, D=D)
    if not plot_C:
        Q['C'].set_plotter(None)

    if autosave is not None:
        Q.set_autosave(autosave, iterations=10)

    Q['Y'].observe(y, mask=mask)

    # Set up rotation speed-up
    if rotate:
        # Initial rotate the D-dimensional state space (X, A, C)
        # Do not update hyperparameters
        rotA_init = transformations.RotateGaussianARD(Q['A'])
        rotX_init = transformations.RotateSwitchingMarkovChain(Q['X'],
                                                               Q['A'],
                                                               Q['Z'],
                                                               rotA_init)
        rotC_init = transformations.RotateGaussianARD(Q['C'])
        R_init = transformations.RotationOptimizer(rotX_init, rotC_init, D)
        # Rotate the D-dimensional state space (X, A, C)
        rotA = transformations.RotateGaussianARD(Q['A'],
                                                 Q['alpha'])
        rotX = transformations.RotateSwitchingMarkovChain(Q['X'],
                                                          Q['A'],
                                                          Q['Z'],
                                                          rotA)
        rotC = transformations.RotateGaussianARD(Q['C'],
                                                 Q['gamma'])
        R = transformations.RotationOptimizer(rotX, rotC, D)
        if debug:
            rotate_kwargs = {'maxiter': 10,
                             'check_bound': True,
                             'check_gradient': True}
        else:
            rotate_kwargs = {'maxiter': 10}

    # Run inference
    if monitor:
        Q.plot()
    for n in range(maxiter):
        if n < update_hyper:
            Q.update('X', 'C', 'A', 'tau', plot=monitor)
            if rotate:
                R_init.rotate(**rotate_kwargs)
        else:
            Q.update(plot=monitor)
            if rotate:
                R.rotate(**rotate_kwargs)

    return Q

def simulate_data(N):
    """
    Generate time-series data with switching dynamics.
    """

    # Two states: 1) oscillation, 2) random walk
    w1 = 0.02 * 2*np.pi
    A = [ [[np.cos(w1), -np.sin(w1)],
           [np.sin(w1),  np.cos(w1)]],
          [[        1.0,         0.0],
           [        0.0,         0.0]] ]
    C = [[1.0, 0.0]]

    # State switching probabilities
    q = 0.993        # probability to stay in the same state
    r = (1-q)/(2-1)  # probability to switch
    P = q*np.identity(2) + r*(np.ones((2,2))-np.identity(2))

    X = np.zeros((N, 2))
    Z = np.zeros(N)
    Y = np.zeros(N)
    F = np.zeros(N)
    z = np.random.randint(2)
    x = np.random.randn(2)
    Z[0] = z
    X[0,:] = x
    for n in range(1,N):
        x = np.dot(A[z], x) + np.random.randn(2)
        f = np.dot(C, x)
        y = f + 5*np.random.randn()
        z = np.random.choice(2, p=P[z])

        Z[n] = z
        X[n,:] = x
        Y[n] = y
        F[n] = f

    Y = Y[None,:]

    return (Y, F)

def demo(N=1000, maxiter=100, D=3, K=2, seed=42, plot=True, debug=False,
        rotate=True, monitor=True):
    """
    Run the demo for linear state-space model with switching dynamics.
    """

    # Use deterministic random numbers
    if seed is not None:
        np.random.seed(seed)

    # Generate data
    (Y, F) = simulate_data(N)

    # Plot observations
    if plot:
        plt.figure()
        bpplt.timeseries(F, 'b-')
        bpplt.timeseries(Y, 'rx')

    # Apply the linear state-space model with switching dynamics
    Q = infer(Y, D, K,
              debug=debug,
              maxiter=maxiter,
              monitor=monitor,
              rotate=rotate,
              update_hyper=5)

    # Show results
    if plot:
        Q.plot()
        plt.show()

    return

if __name__ == '__main__':
    import sys, getopt, os
    try:
        opts, args = getopt.getopt(sys.argv[1:],
                                   "",
                                   ["n=",
                                    "d=",
                                    "k=",
                                    "seed=",
                                    "debug",
                                    "no-rotation",
                                    "no-monitor",
                                    "no-plot",
                                    "maxiter="])
    except getopt.GetoptError:
        print('python lssm_sd.py <options>')
        print('--n=<INT>        Number of data vectors')
        print('--d=<INT>        Latent space dimensionality')
        print('--k=<INT>        Number of mixed models')
        print('--maxiter=<INT>  Maximum number of VB iterations')
        print('--seed=<INT>     Seed (integer) for the random number generator')
        print('--no-rotation    Do not peform rotation speed ups')
        print('--no-plot        Do not plot results')
        print('--no-monitor     Do not plot distributions during VB learning')
        print('--debug          Check that the rotations are implemented correctly')
        sys.exit(2)

    kwargs = {}
    for opt, arg in opts:
        if opt == "--maxiter":
            kwargs["maxiter"] = int(arg)
        elif opt == "--d":
            kwargs["D"] = int(arg)
        elif opt == "--k":
            kwargs["K"] = int(arg)
        elif opt == "--seed":
            kwargs["seed"] = int(arg)
        elif opt == "--no-rotation":
            kwargs["rotate"] = False
        elif opt == "--no-monitor":
            kwargs["monitor"] = False
        elif opt == "--no-plot":
            kwargs["plot"] = False
        elif opt == "--debug":
            kwargs["debug"] = True
        elif opt in ("--n",):
            kwargs["N"] = int(arg)
        else:
            raise ValueError("Unhandled option given")

    demo(**kwargs)
<|endoftext|>


<commit_before>"""

This contains the unit tests for the main bcml module.

"""

from __future__ import print_function
import unittest
import subprocess
import os

_dir = os.path.dirname(__file__)
_executable = os.path.abspath(os.path.join(_dir, os.pardir, 'bcml.py'))
_data_min = os.path.abspath(os.path.join(_dir, 'data', 'RON_min.txt'))
_data_mini = os.path.abspath(os.path.join(_dir, 'data', 'RON_mini.txt'))

class BCMLTests(unittest.TestCase):

    def setUp(self):
        """Create an instance of main"""
        print("Initializing tests")
        pass

    def tearDown(self):
        """Delete data structure"""
        print("Clearing out test suite")
        pass

    def test_parse_argument(self):
        print("Testing input parameters")
        print("Testing empty run")
        args = ['python', _executable]
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 2)
        self.assertEqual(stdoutdata, '')
        print("Testing read of incompatible modules")
        args = ['python', _executable, '-i data/null.txt', '--train', '--test']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 2)
        self.assertEqual(stdoutdata, '')
        args = ['python', _executable, '-i data/null.txt', '-d data/null.txt', '--pred', 'RON']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 2)
        self.assertEqual(stdoutdata, '')
        print("Testing random seeds")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--user', '--random', '12345']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        self.assertEqual(stderrdata, '')
        args = ['python', _executable, '-i', _data_min,  '--train', '--random', '-12345', '--pred', 'RON']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 1)
        print("Testing proxy")
        args = ['python', _executable, '-i', _data_min, '--train', '--proxy', 'https://wwwproxy.example.com', '--pred', 'RON', '--user', '--random', '12345']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        self.assertEqual(stderrdata, '')
        args = ['python', _executable, '-i', _data_min, '--train', '--proxy', '500', '--pred', 'RON', '--user', '--random', '12345']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 1)
        print("Testing user feature")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--user', '--random', '12345']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        self.assertEqual(stderrdata, '')
        print("Testing fingerprint feature")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--fingerprint', '--random', '12345']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        print("Testing experimental feature")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--verbose', '--experimental', '--random', '12345', '--split_value', '85.0']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        print("Testing chemofeatures feature")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--chemofeatures', '--random', '12345']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        print("Testing distance feature")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--fingerprint', '--distance', '--random', '12345']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        print("Testing split feature")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--fingerprint', '--random', '12345', '--split_value', '85.0']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        print("Testing imputation")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--fingerprint', '--random', '12345', '--impute']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        print("Testing feature selection")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--fingerprint', '--random', '12345', '--selection']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        print("Testing Cross Validation")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--experimental', '--split_value', '94.4', '--random', '12345', '--impute', '--cv']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)

if __name__ == '__main__':
    unittest.main()
<commit_msg>Fixing minor issues with testing<commit_after>"""

This contains the unit tests for the main bcml module.

"""

from __future__ import print_function
import unittest
import subprocess
import os

_dir = os.path.dirname(__file__)
_executable = os.path.abspath(os.path.join(_dir, os.pardir, 'bcml.py'))
_data_min = os.path.abspath(os.path.join(_dir, 'data', 'RON_min.txt'))
_data_mini = os.path.abspath(os.path.join(_dir, 'data', 'RON_mini.txt'))

class BCMLTests(unittest.TestCase):

    def setUp(self):
        """Create an instance of main"""
        print("Initializing tests")
        pass

    def tearDown(self):
        """Delete data structure"""
        print("Clearing out test suite")
        pass

    def test_parse_argument(self):
        print("Testing input parameters")
        print("Testing empty run")
        args = ['python', _executable]
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 2)
        print("Testing read of incompatible modules")
        args = ['python', _executable, '-i data/null.txt', '--train', '--test']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 2)
        args = ['python', _executable, '-i data/null.txt', '-d data/null.txt', '--pred', 'RON']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 2)
        self.assertEqual(stdoutdata, '')
        print("Testing random seeds")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--user', '--random', '12345']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        self.assertEqual(stderrdata, '')
        args = ['python', _executable, '-i', _data_min,  '--train', '--random', '-12345', '--pred', 'RON']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 1)
        print("Testing proxy")
        args = ['python', _executable, '-i', _data_min, '--train', '--proxy', 'https://wwwproxy.example.com', '--pred', 'RON', '--user', '--random', '12345']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        self.assertEqual(stderrdata, '')
        args = ['python', _executable, '-i', _data_min, '--train', '--proxy', '500', '--pred', 'RON', '--user', '--random', '12345']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 1)
        print("Testing user feature")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--user', '--random', '12345']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        self.assertEqual(stderrdata, '')
        print("Testing fingerprint feature")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--fingerprint', '--random', '12345']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        print("Testing experimental feature")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--verbose', '--experimental', '--random', '12345', '--split_value', '85.0']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        print("Testing chemofeatures feature")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--chemofeatures', '--random', '12345']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        print("Testing distance feature")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--fingerprint', '--distance', '--random', '12345']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        print("Testing split feature")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--fingerprint', '--random', '12345', '--split_value', '85.0']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        print("Testing imputation")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--fingerprint', '--random', '12345', '--impute']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        print("Testing feature selection")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--fingerprint', '--random', '12345', '--selection']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)
        print("Testing Cross Validation")
        args = ['python', _executable, '-i', _data_min, '--train', '--pred', 'RON', '--experimental', '--split_value', '94.4', '--random', '12345', '--impute', '--cv']
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdoutdata, stderrdata = process.communicate()
        self.assertEqual(process.returncode, 0)

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


<commit_before># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import re

from django.views.decorators.cache import cache_page

from commonware.response.decorators import xframe_allow
from bs4 import BeautifulSoup

from lib import l10n_utils

from bedrock.legal_docs.views import LegalDocView, load_legal_doc

HN_PATTERN = re.compile(r'^h(\d)$')
HREF_PATTERN = re.compile(r'^https?\:\/\/www\.mozilla\.org')

def process_legal_doc(content):
    """
    Load a static Markdown file and return the document as a BeautifulSoup
    object for easier manipulation.

    :param content: HTML Content of the legal doc.
    """
    soup = BeautifulSoup(content)

    # Convert the site's full URLs to absolute paths
    for link in soup.find_all(href=HREF_PATTERN):
        link['href'] = HREF_PATTERN.sub('', link['href'])

    # Return the HTML fragment as a BeautifulSoup object
    return soup

class PrivacyDocView(LegalDocView):
    def get_legal_doc(self):
        doc = super(PrivacyDocView, self).get_legal_doc()
        doc['content'] = process_legal_doc(doc['content'])
        return doc

class FirefoxPrivacyDocView(PrivacyDocView):
    template_name = 'privacy/notices/base-notice-paragraphs.html'

    def get_legal_doc(self):
        doc = super(FirefoxPrivacyDocView, self).get_legal_doc()
        if len(doc['content'].select('.privacy-header-firefox')) > 0:
            self.template_name = 'privacy/notices/firefox-quantum.html'
        return doc

firefox_notices = FirefoxPrivacyDocView.as_view(
    legal_doc_name='firefox_privacy_notice')

firefox_os_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-os.html',
    legal_doc_name='firefox_os_privacy_notice')

firefox_fire_tv_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-fire-tv.html',
    legal_doc_name='Firefox_FireTV_Privacy_Notice')

firefox_cloud_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-cloud.html',
    legal_doc_name='firefox_cloud_services_PrivacyNotice')

firefox_hello_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-hello.html',
    legal_doc_name='WebRTC_PrivacyNotice')

firefox_focus_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-focus.html',
    legal_doc_name='focus_privacy_notice')

firefox_lite_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-lite.html',
    legal_doc_name='firefox_lite_privacy_notice')

firefox_reality_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-reality.html',
    legal_doc_name='firefox_reality_privacy_notice')

firefox_screenshotgo_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-screenshotgo.html',
    legal_doc_name='firefox_screenshotgo_privacy_notice')

thunderbird_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/thunderbird.html',
    legal_doc_name='thunderbird_privacy_policy')

websites_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/websites.html',
    legal_doc_name='websites_privacy_notice')

facebook_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/facebook.html',
    legal_doc_name='facebook_privacy_info')
facebook_notices = xframe_allow(facebook_notices)

firefox_monitor_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-monitor.html',
    legal_doc_name='firefox_monitor_terms_privacy')

@cache_page(60 * 60)  # cache for 1 hour
def privacy(request):
    doc = load_legal_doc('mozilla_privacy_policy', l10n_utils.get_locale(request))

    template_vars = {
        'doc': process_legal_doc(doc['content']),
        'active_locales': doc['active_locales'],
    }

    return l10n_utils.render(request, 'privacy/index.html', template_vars)
<commit_msg>"No parser was explicitly specified" warning Fixed (#7812)<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import re

from django.views.decorators.cache import cache_page

from commonware.response.decorators import xframe_allow
from bs4 import BeautifulSoup

from lib import l10n_utils

from bedrock.legal_docs.views import LegalDocView, load_legal_doc

HN_PATTERN = re.compile(r'^h(\d)$')
HREF_PATTERN = re.compile(r'^https?\:\/\/www\.mozilla\.org')

def process_legal_doc(content):
    """
    Load a static Markdown file and return the document as a BeautifulSoup
    object for easier manipulation.

    :param content: HTML Content of the legal doc.
    """
    soup = BeautifulSoup(content, "lxml")

    # Convert the site's full URLs to absolute paths
    for link in soup.find_all(href=HREF_PATTERN):
        link['href'] = HREF_PATTERN.sub('', link['href'])

    # Return the HTML fragment as a BeautifulSoup object
    return soup

class PrivacyDocView(LegalDocView):
    def get_legal_doc(self):
        doc = super(PrivacyDocView, self).get_legal_doc()
        doc['content'] = process_legal_doc(doc['content'])
        return doc

class FirefoxPrivacyDocView(PrivacyDocView):
    template_name = 'privacy/notices/base-notice-paragraphs.html'

    def get_legal_doc(self):
        doc = super(FirefoxPrivacyDocView, self).get_legal_doc()
        if len(doc['content'].select('.privacy-header-firefox')) > 0:
            self.template_name = 'privacy/notices/firefox-quantum.html'
        return doc

firefox_notices = FirefoxPrivacyDocView.as_view(
    legal_doc_name='firefox_privacy_notice')

firefox_os_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-os.html',
    legal_doc_name='firefox_os_privacy_notice')

firefox_fire_tv_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-fire-tv.html',
    legal_doc_name='Firefox_FireTV_Privacy_Notice')

firefox_cloud_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-cloud.html',
    legal_doc_name='firefox_cloud_services_PrivacyNotice')

firefox_hello_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-hello.html',
    legal_doc_name='WebRTC_PrivacyNotice')

firefox_focus_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-focus.html',
    legal_doc_name='focus_privacy_notice')

firefox_lite_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-lite.html',
    legal_doc_name='firefox_lite_privacy_notice')

firefox_reality_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-reality.html',
    legal_doc_name='firefox_reality_privacy_notice')

firefox_screenshotgo_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-screenshotgo.html',
    legal_doc_name='firefox_screenshotgo_privacy_notice')

thunderbird_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/thunderbird.html',
    legal_doc_name='thunderbird_privacy_policy')

websites_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/websites.html',
    legal_doc_name='websites_privacy_notice')

facebook_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/facebook.html',
    legal_doc_name='facebook_privacy_info')
facebook_notices = xframe_allow(facebook_notices)

firefox_monitor_notices = PrivacyDocView.as_view(
    template_name='privacy/notices/firefox-monitor.html',
    legal_doc_name='firefox_monitor_terms_privacy')

@cache_page(60 * 60)  # cache for 1 hour
def privacy(request):
    doc = load_legal_doc('mozilla_privacy_policy', l10n_utils.get_locale(request))

    template_vars = {
        'doc': process_legal_doc(doc['content']),
        'active_locales': doc['active_locales'],
    }

    return l10n_utils.render(request, 'privacy/index.html', template_vars)
<|endoftext|>


<commit_before>"""
Fetchartist plugin for beets.
"""
import os
import shutil
import pylast
import requests

from beets import config
from beets import plugins
from beets import ui
from beets import util as beetsutil

from beetsplug import util

CONTENT_TYPES = ["image/png", "image/jpeg"]
FILE_TYPES = ['png', 'jpg']
CONTENT_TYPE_TO_EXTENSION_MAP = {
    "image/png": "png",
    "image/jpeg": "jpg"
}

COVER_NAME_KEY = "cover_name"

class ArtistInfo(object):
    """
    Contains information about an artist, like it's name, paths that point to
    its covers and the cover itself.
    """

    def __init__(self, name):
        self.name = name
        self.paths = set()
        self.cover = None
        self.extension = None
        self.remaining_paths = set()

    def add_path(self, path):
        """
        Adds a cover path to this artist.
        """
        self.paths.add(path)

    def get_write_paths(self, force):
        """
        If there are remaining paths set, only covers at those paths don't
        exist and need to be written. Otherwise all paths are returned. If
        force is set, all covers should be written again.
        """
        write_paths = self.paths
        if not force and self.remaining_paths:
            write_paths = self.remaining_paths
        return [path + "." + self.extension for path in write_paths]

    def __repr__(self):
        return "ArtistInfo<name=%s,paths=%r>" % (self.name, self.paths)

class FetchArtistPlugin(plugins.BeetsPlugin):
    """
    The fetchart plugin.
    """

    def __init__(self):
        super(FetchArtistPlugin, self).__init__()

        self._last_fm = pylast.LastFMNetwork(api_key=plugins.LASTFM_KEY)

        self.config.add({
            COVER_NAME_KEY: ""
        })
        self._process_config()
        self._create_path_templates()

    def _process_config(self):
        self._cover_name = self.config[COVER_NAME_KEY].get().strip()
        if not self._cover_name:
            self._cover_name = None

    def _create_path_templates(self):
        self._library_path = config["directory"].get()
        self._default_template = util.strip_template_path_suffix(
            config["paths"]["default"].get(), "$albumartist")
        self._singleton_template = util.strip_template_path_suffix(
            config["paths"]["singleton"].get(), "$artist")

    def commands(self):
        cmd = ui.Subcommand("fetchartist", help="download artist art")
        cmd.parser.add_option("-f", "--force", dest="force",
                              action="store_true", default=False,
                              help="force overwrite existing artist covers")

        def _func(lib, opts, args):
            self._fetch_artist(lib.items(ui.decargs(args)), opts.force)
        cmd.func = _func
        return [cmd]

    @staticmethod
    def _get_artist_from_item(item):
        """
        Returns an appropriate artist name for the given item. If the given item is
        part of an album, the albumartist, otherwise the artist is returned.
        """
        if item.singleton:
            return item.artist
        return item.albumartist

    def _get_cover_name(self, item):
        if self._cover_name:
            return self._cover_name
        return FetchArtistPlugin._get_artist_from_item(item)

    def _create_cover_path(self, item):
        if item.singleton:
            template = self._singleton_template
        else:
            template = self._default_template

        evaluated_template = item.evaluate_template(template)
        cover_name = self._get_cover_name(item)
        path = os.path.join(evaluated_template, cover_name)
        return os.path.join(self._library_path, beetsutil.sanitize_path(path))

    def _create_artist_infos(self, items):
        artist_infos = dict()

        for item in items:
            artist = FetchArtistPlugin._get_artist_from_item(item)

            artist_info = artist_infos.get(artist)
            if artist_info is None:
                artist_info = ArtistInfo(artist)
                artist_infos[artist] = artist_info

            artist_info.add_path(self._create_cover_path(item))

        return sorted(artist_infos.values(), key=lambda a: a.name)

    @staticmethod
    def _check_for_existing_covers(artist_info):
        existing_paths, missing_paths =\
                util.find_existing_and_missing_files(artist_info.paths,
                                                     FILE_TYPES)

        artist_info.remaining_paths = missing_paths
        # if there is no cover at all, return False
        if not existing_paths:
            return False
        # if there are all covers, return True
        if not missing_paths:
            return True

        # TODO if only some covers exist query the user what to do
        return False

    def _request_cover(self, artist_name):
        artist = self._last_fm.get_artist(artist_name)
        try:
            cover_url = artist.get_cover_image()
        except pylast.WSError:
            return None

        response = requests.get(cover_url, stream=True)

        content_type = response.headers.get('Content-Type')
        if content_type is None or content_type not in CONTENT_TYPES:
            self._log.debug(u"not a supported image: {}",
                            content_type or 'no content type')
            return None

        extension = CONTENT_TYPE_TO_EXTENSION_MAP[content_type]
        return (response.raw, extension)

    def _fetch_cover(self, artist_info):
        result = self._request_cover(artist_info.name)
        if result is None:
            return False

        artist_info.cover, artist_info.extension = result
        return True

    def _write_covers(self, artist_info, force):
        for path in artist_info.get_write_paths(force):
            self._log.debug("saving cover at '{}'".format(path))
            with open(path, "wb") as target:
                shutil.copyfileobj(artist_info.cover, target)

    def _update_cover(self, artist_info, force):
        all_exist = FetchArtistPlugin._check_for_existing_covers(artist_info)
        if force or not all_exist:
            if self._fetch_cover(artist_info):
                self._write_covers(artist_info, force)
                message = ui.colorize('text_success', 'artist cover found')
            else:
                message = ui.colorize('text_error', 'no artist cover found')
        else:
            message = ui.colorize('text_highlight_minor', 'has artist cover')
        self._log.info(u'{0}: {1}', artist_info.name, message)

    def _fetch_artist(self, items, force):
        artist_infos = self._create_artist_infos(items)
        for artist_info in artist_infos:
            self._update_cover(artist_info, force)
<commit_msg>fix encoding error<commit_after>"""
Fetchartist plugin for beets.
"""
import os
import shutil
import pylast
import requests

from beets import config
from beets import plugins
from beets import ui
from beets import util as beetsutil

from beetsplug import util

CONTENT_TYPES = ["image/png", "image/jpeg"]
FILE_TYPES = ['png', 'jpg']
CONTENT_TYPE_TO_EXTENSION_MAP = {
    "image/png": "png",
    "image/jpeg": "jpg"
}

COVER_NAME_KEY = "cover_name"

class ArtistInfo(object):
    """
    Contains information about an artist, like it's name, paths that point to
    its covers and the cover itself.
    """

    def __init__(self, name):
        self.name = name
        self.paths = set()
        self.cover = None
        self.extension = None
        self.remaining_paths = set()

    def add_path(self, path):
        """
        Adds a cover path to this artist.
        """
        self.paths.add(path)

    def get_write_paths(self, force):
        """
        If there are remaining paths set, only covers at those paths don't
        exist and need to be written. Otherwise all paths are returned. If
        force is set, all covers should be written again.
        """
        write_paths = self.paths
        if not force and self.remaining_paths:
            write_paths = self.remaining_paths
        return [path + "." + self.extension for path in write_paths]

    def __repr__(self):
        return "ArtistInfo<name=%s,paths=%r>" % (self.name, self.paths)

class FetchArtistPlugin(plugins.BeetsPlugin):
    """
    The fetchart plugin.
    """

    def __init__(self):
        super(FetchArtistPlugin, self).__init__()

        self._last_fm = pylast.LastFMNetwork(api_key=plugins.LASTFM_KEY)

        self.config.add({
            COVER_NAME_KEY: ""
        })
        self._process_config()
        self._create_path_templates()

    def _process_config(self):
        self._cover_name = self.config[COVER_NAME_KEY].get().strip()
        if not self._cover_name:
            self._cover_name = None

    def _create_path_templates(self):
        self._library_path = config["directory"].get()
        self._default_template = util.strip_template_path_suffix(
            config["paths"]["default"].get(), "$albumartist")
        self._singleton_template = util.strip_template_path_suffix(
            config["paths"]["singleton"].get(), "$artist")

    def commands(self):
        cmd = ui.Subcommand("fetchartist", help="download artist art")
        cmd.parser.add_option("-f", "--force", dest="force",
                              action="store_true", default=False,
                              help="force overwrite existing artist covers")

        def _func(lib, opts, args):
            self._fetch_artist(lib.items(ui.decargs(args)), opts.force)
        cmd.func = _func
        return [cmd]

    @staticmethod
    def _get_artist_from_item(item):
        """
        Returns an appropriate artist name for the given item. If the given item is
        part of an album, the albumartist, otherwise the artist is returned.
        """
        if item.singleton:
            return item.artist
        return item.albumartist

    def _get_cover_name(self, item):
        if self._cover_name:
            return self._cover_name
        return FetchArtistPlugin._get_artist_from_item(item)

    def _create_cover_path(self, item):
        if item.singleton:
            template = self._singleton_template
        else:
            template = self._default_template

        evaluated_template = item.evaluate_template(template)
        cover_name = self._get_cover_name(item)
        path = os.path.join(evaluated_template, cover_name)
        return os.path.join(self._library_path, beetsutil.sanitize_path(path))

    def _create_artist_infos(self, items):
        artist_infos = dict()

        for item in items:
            artist = FetchArtistPlugin._get_artist_from_item(item)

            artist_info = artist_infos.get(artist)
            if artist_info is None:
                artist_info = ArtistInfo(artist)
                artist_infos[artist] = artist_info

            artist_info.add_path(self._create_cover_path(item))

        return sorted(artist_infos.values(), key=lambda a: a.name)

    @staticmethod
    def _check_for_existing_covers(artist_info):
        existing_paths, missing_paths =\
                util.find_existing_and_missing_files(artist_info.paths,
                                                     FILE_TYPES)

        artist_info.remaining_paths = missing_paths
        # if there is no cover at all, return False
        if not existing_paths:
            return False
        # if there are all covers, return True
        if not missing_paths:
            return True

        # TODO if only some covers exist query the user what to do
        return False

    def _request_cover(self, artist_name):
        artist = self._last_fm.get_artist(artist_name)
        try:
            cover_url = artist.get_cover_image()
        except pylast.WSError:
            return None

        response = requests.get(cover_url, stream=True)

        content_type = response.headers.get('Content-Type')
        if content_type is None or content_type not in CONTENT_TYPES:
            self._log.debug(u"not a supported image: {}",
                            content_type or 'no content type')
            return None

        extension = CONTENT_TYPE_TO_EXTENSION_MAP[content_type]
        return (response.raw, extension)

    def _fetch_cover(self, artist_info):
        result = self._request_cover(artist_info.name)
        if result is None:
            return False

        artist_info.cover, artist_info.extension = result
        return True

    def _write_covers(self, artist_info, force):
        for path in artist_info.get_write_paths(force):
            self._log.debug(u"saving cover at '{}'".format(path))
            with open(path, "wb") as target:
                shutil.copyfileobj(artist_info.cover, target)

    def _update_cover(self, artist_info, force):
        all_exist = FetchArtistPlugin._check_for_existing_covers(artist_info)
        if force or not all_exist:
            if self._fetch_cover(artist_info):
                self._write_covers(artist_info, force)
                message = ui.colorize('text_success', 'artist cover found')
            else:
                message = ui.colorize('text_error', 'no artist cover found')
        else:
            message = ui.colorize('text_highlight_minor', 'has artist cover')
        self._log.info(u'{0}: {1}', artist_info.name, message)

    def _fetch_artist(self, items, force):
        artist_infos = self._create_artist_infos(items)
        for artist_info in artist_infos:
            self._update_cover(artist_info, force)
<|endoftext|>