<commit_before># -*- encoding: utf-8 -*-
##############################################################################
#
#    Author: Nicolas Bessi. Copyright Camptocamp SA
#    Donors: Hasa Sàrl, Open Net Sàrl and Prisme Solutions Informatique SA
#
#    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 time
import sys
import os
import re

from mako.template import Template
from mako.lookup import TemplateLookup
from mako import exceptions

from report import report_sxw
from report_webkit import webkit_report
from report_webkit import report_helper

from osv import osv
from osv.osv import except_osv

from tools import mod10r
from tools.translate import _
from tools.config import config

import wizard
import addons
import pooler

from openerp.addons.invoice_webkit.report.account_invoice import account_invoice_report

class l10n_ch_report_webkit_html(account_invoice_report):
    def __init__(self, cr, uid, name, context):
        super(l10n_ch_report_webkit_html, self).__init__(cr, uid, name, context=context)
        self.localcontext.update({
            'time': time,
            'cr': cr,
            'uid': uid,
            'user':self.pool.get("res.users").browse(cr, uid, uid),
            'mod10r': mod10r,
            '_space': self._space,
            '_get_ref': self._get_ref,
            'comma_me': self.comma_me,
            'police_absolute_path': self.police_absolute_path,
            'bvr_absolute_path': self.bvr_absolute_path,
            'headheight': self.headheight,
            'company_vat' : self._get_company_vat,

        })

    _compile_get_ref = re.compile('[^0-9]')
    _compile_comma_me = re.compile("^(-?\d+)(\d{3})")
    _compile_check_bvr = re.compile('[0-9][0-9]-[0-9]{3,6}-[0-9]')
    _compile_check_bvr_add_num = re.compile('[0-9]*$')

    def set_context(self, objects, data, ids, report_type=None):
        user = self.pool.get('res.users').browse(self.cr, self.uid, self.uid)
        company = user.company_id
        if not company.invoice_only:
            self._check(ids)
        return super(l10n_ch_report_webkit_html, self).set_context(objects, data, ids, report_type=report_type)

    def _get_company_vat(self):
        res_users_obj=pooler.get_pool(self.cr.dbname).get('res.users')
        company_vat = res_users_obj.browse(self.cr, self.uid,self.uid).company_id.partner_id.vat
        if company_vat:
            return company_vat
        else:
            return False

    def police_absolute_path(self, inner_path) :
        """Will get the ocrb police absolute path"""
        path = addons.get_module_resource(os.path.join('l10n_ch', 'report', inner_path))
        return  path

    def bvr_absolute_path(self) :
        """Will get the ocrb police absolute path"""
        path = addons.get_module_resource(os.path.join('l10n_ch', 'report', 'bvr1.jpg'))
        return  path

    def headheight(self):
        report_id = self.pool.get('ir.actions.report.xml').search(self.cr, self.uid, [('name','=', 'BVR invoice')])[0]
        report = self.pool.get('ir.actions.report.xml').browse(self.cr, self.uid, report_id)
        return report.webkit_header.margin_top

    def comma_me(self, amount):
        """Fast swiss number formatting"""
        if  isinstance(amount, float):
            amount = str('%.2f'%amount)
        else :
            amount = str(amount)
        orig = amount
        new = self._compile_comma_me.sub("\g<1>'\g<2>", amount)
        if orig == new:
            return new
        else:
            return self.comma_me(new)

    def _space(self, nbr, nbrspc=5):
        """Spaces * 5.

        Example:
            >>> self._space('123456789012345')
            '12 34567 89012 345'
        """
        return ''.join([' '[(i - 2) % nbrspc:] + c for i, c in enumerate(nbr)])

    def _get_ref(self, inv):
        """Retrieve ESR/BVR reference form invoice in order to print it"""
        res = ''
        if inv.partner_bank_id.bvr_adherent_num:
            res = inv.partner_bank_id.bvr_adherent_num
        invoice_number = ''
        if inv.number:
            invoice_number = self._compile_get_ref.sub('', inv.number)
        return mod10r(res + invoice_number.rjust(26-len(res), '0'))

    def _check(self, invoice_ids):
        """Check if the invoice is ready to be printed"""
        if not invoice_ids:
            invoice_ids = []
        cursor = self.cr
        pool = self.pool
        invoice_obj = pool.get('account.invoice')
        ids = invoice_ids
        for invoice in invoice_obj.browse(cursor, self.uid, ids):
            invoice_name = "%s %s" %(invoice.name, invoice.number)
            if not invoice.partner_bank_id:
                raise except_osv(_('UserError'),
                        _('No bank specified on invoice:\n%s' %(invoice_name)))
            if not self._compile_check_bvr.match(
                    invoice.partner_bank_id.post_number or ''):
                raise except_osv(_('UserError'),
                        _(('Your bank BVR number should be of the form 0X-XXX-X! '
                          'Please check your company '
                          'information for the invoice:\n%s')
                          %(invoice_name)))
            if invoice.partner_bank_id.bvr_adherent_num \
                    and not self._compile_check_bvr_add_num.match(
                            invoice.partner_bank_id.bvr_adherent_num):
                raise except_osv(_('UserError'),
                        _(('Your bank BVR adherent number must contain only '
                          'digits!\nPlease check your company '
                          'information for the invoice:\n%s') %(invoice_name)))
        return ''

def mako_template(text):
    """Build a Mako template.

    This template uses UTF-8 encoding
    """
    tmp_lookup  = TemplateLookup() #we need it in order to allow inclusion and inheritance
    return Template(text, input_encoding='utf-8', output_encoding='utf-8', lookup=tmp_lookup)

class BVRWebKitParser(webkit_report.WebKitParser):

    bvr_file_path = os.path.join('l10n_ch','report','bvr.mako')

    def create_single_pdf(self, cursor, uid, ids, data, report_xml, context=None):
        """generate the PDF"""
        context = context or {}
        if report_xml.report_type != 'webkit':
            return super(WebKitParser,self).create_single_pdf(cursor, uid, ids, data, report_xml, context=context)
        self.parser_instance = self.parser(cursor,
                                            uid,
                                            self.name2,
                                            context=context)
        self.pool = pooler.get_pool(cursor.dbname)
        objs = self.getObjects(cursor, uid, ids, context)
        self.parser_instance.set_context(objs, data, ids, report_xml.report_type)
        template =  False
        if report_xml.report_file :
            path = addons.get_module_resource(report_xml.report_file)
            if os.path.exists(path) :
                template = file(path).read()
        if not template and report_xml.report_webkit_data :
            template =  report_xml.report_webkit_data
        if not template :
            raise except_osv(_('Error'),_('Webkit Report template not found !'))
        header = report_xml.webkit_header.html
        footer = report_xml.webkit_header.footer_html
        if not header and report_xml.header:
          raise except_osv(
                _('No header defined for this Webkit report!'),
                _('Please set a header in company settings')
            )
        if not report_xml.header :
            header = ''
            default_head = addons.get_module_resource('report_webkit', 'default_header.html')
            with open(default_head,'r') as f:
                header = f.read()
        css = report_xml.webkit_header.css
        if not css :
            css = ''
        user = self.pool.get('res.users').browse(cursor, uid, uid)
        company = user.company_id
        body_mako_tpl = mako_template(template)
        #BVR specific
        bvr_path = addons.get_module_resource(self.bvr_file_path)
        body_bvr_tpl = mako_template(file(bvr_path).read())
        helper = report_helper.WebKitHelper(cursor, uid, report_xml.id, context)
        ##BVR Specific
        htmls = []
        for obj in objs :
            self.parser_instance.localcontext['objects'] = [obj]
            if not company.bvr_only:
                try:
                    html = body_mako_tpl.render(helper=helper,
                                                css=css,
                                                _=self.translate_call,
                                                **self.parser_instance.localcontext)
                except Exception, e:
                   raise Exception(exceptions.text_error_template().render())
                htmls.append(html)
            if not company.invoice_only:
                try:
                    bvr = body_bvr_tpl.render(helper=helper,
                                              css=css,
                                              _=self.translate_call,
                                              **self.parser_instance.localcontext)
                except Exception, e:
                   raise Exception(exceptions.text_error_template().render())
                htmls.append(bvr)
        head_mako_tpl = Template(header, input_encoding='utf-8', output_encoding='utf-8')
        try:
            head = head_mako_tpl.render(helper=helper,
                                        css=css,
                                        _debug=False,
                                        _=self.translate_call,
                                        **self.parser_instance.localcontext)
        except Exception, e:
           raise Exception(exceptions.text_error_template().render())
        foot = False
        if footer and company.invoice_only :
            foot_mako_tpl = Template(footer, input_encoding='utf-8', output_encoding='utf-8')
            try:
                foot = foot_mako_tpl.render(helper=helper,
                                            css=css,
                                            _=self.translate_call,
                                            **self.parser_instance.localcontext)
            except Exception, e:
               raise Exception(exceptions.text_error_template().render())
        if report_xml.webkit_debug :
            try:
                deb = head_mako_tpl.render(helper=helper,
                                            css=css,
                                            _debug=html,
                                            _=self.translate_call,
                                            **self.parser_instance.localcontext)
            except Exception, e:
               raise Exception(exceptions.text_error_template().render())
            return (deb, 'html')
        bin = self.get_lib(cursor, uid)
        pdf = self.generate_pdf(bin, report_xml, head, foot, htmls)
        return (pdf, 'pdf')

BVRWebKitParser('report.invoice_web_bvr',
               'account.invoice',
               'addons/l10n_ch/report/account_invoice.mako',
               parser=l10n_ch_report_webkit_html)

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<commit_msg>[FIX] wrong inheritance of invoice report<commit_after># -*- encoding: utf-8 -*-
##############################################################################
#
#    Author: Nicolas Bessi. Copyright Camptocamp SA
#    Donors: Hasa Sàrl, Open Net Sàrl and Prisme Solutions Informatique SA
#
#    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 time
import sys
import os
import re

from mako.template import Template
from mako.lookup import TemplateLookup
from mako import exceptions

from report import report_sxw
from report_webkit import webkit_report
from report_webkit import report_helper

from osv import osv
from osv.osv import except_osv

from tools import mod10r
from tools.translate import _
from tools.config import config

import wizard
import addons
import pooler

class l10n_ch_report_webkit_html(report_sxw.rml_parse):
    def __init__(self, cr, uid, name, context):
        super(l10n_ch_report_webkit_html, self).__init__(cr, uid, name, context=context)
        self.localcontext.update({
            'time': time,
            'cr': cr,
            'uid': uid,
            'user':self.pool.get("res.users").browse(cr, uid, uid),
            'mod10r': mod10r,
            '_space': self._space,
            '_get_ref': self._get_ref,
            'comma_me': self.comma_me,
            'police_absolute_path': self.police_absolute_path,
            'bvr_absolute_path': self.bvr_absolute_path,
            'headheight': self.headheight,
            'company_vat' : self._get_company_vat,

        })

    _compile_get_ref = re.compile('[^0-9]')
    _compile_comma_me = re.compile("^(-?\d+)(\d{3})")
    _compile_check_bvr = re.compile('[0-9][0-9]-[0-9]{3,6}-[0-9]')
    _compile_check_bvr_add_num = re.compile('[0-9]*$')

    def set_context(self, objects, data, ids, report_type=None):
        user = self.pool.get('res.users').browse(self.cr, self.uid, self.uid)
        company = user.company_id
        if not company.invoice_only:
            self._check(ids)
        return super(l10n_ch_report_webkit_html, self).set_context(objects, data, ids, report_type=report_type)

    def _get_company_vat(self):
        res_users_obj=pooler.get_pool(self.cr.dbname).get('res.users')
        company_vat = res_users_obj.browse(self.cr, self.uid,self.uid).company_id.partner_id.vat
        if company_vat:
            return company_vat
        else:
            return False

    def police_absolute_path(self, inner_path) :
        """Will get the ocrb police absolute path"""
        path = addons.get_module_resource(os.path.join('l10n_ch', 'report', inner_path))
        return  path

    def bvr_absolute_path(self) :
        """Will get the ocrb police absolute path"""
        path = addons.get_module_resource(os.path.join('l10n_ch', 'report', 'bvr1.jpg'))
        return  path

    def headheight(self):
        report_id = self.pool.get('ir.actions.report.xml').search(self.cr, self.uid, [('name','=', 'BVR invoice')])[0]
        report = self.pool.get('ir.actions.report.xml').browse(self.cr, self.uid, report_id)
        return report.webkit_header.margin_top

    def comma_me(self, amount):
        """Fast swiss number formatting"""
        if  isinstance(amount, float):
            amount = str('%.2f'%amount)
        else :
            amount = str(amount)
        orig = amount
        new = self._compile_comma_me.sub("\g<1>'\g<2>", amount)
        if orig == new:
            return new
        else:
            return self.comma_me(new)

    def _space(self, nbr, nbrspc=5):
        """Spaces * 5.

        Example:
            >>> self._space('123456789012345')
            '12 34567 89012 345'
        """
        return ''.join([' '[(i - 2) % nbrspc:] + c for i, c in enumerate(nbr)])

    def _get_ref(self, inv):
        """Retrieve ESR/BVR reference form invoice in order to print it"""
        res = ''
        if inv.partner_bank_id.bvr_adherent_num:
            res = inv.partner_bank_id.bvr_adherent_num
        invoice_number = ''
        if inv.number:
            invoice_number = self._compile_get_ref.sub('', inv.number)
        return mod10r(res + invoice_number.rjust(26-len(res), '0'))

    def _check(self, invoice_ids):
        """Check if the invoice is ready to be printed"""
        if not invoice_ids:
            invoice_ids = []
        cursor = self.cr
        pool = self.pool
        invoice_obj = pool.get('account.invoice')
        ids = invoice_ids
        for invoice in invoice_obj.browse(cursor, self.uid, ids):
            invoice_name = "%s %s" %(invoice.name, invoice.number)
            if not invoice.partner_bank_id:
                raise except_osv(_('UserError'),
                        _('No bank specified on invoice:\n%s' %(invoice_name)))
            if not self._compile_check_bvr.match(
                    invoice.partner_bank_id.post_number or ''):
                raise except_osv(_('UserError'),
                        _(('Your bank BVR number should be of the form 0X-XXX-X! '
                          'Please check your company '
                          'information for the invoice:\n%s')
                          %(invoice_name)))
            if invoice.partner_bank_id.bvr_adherent_num \
                    and not self._compile_check_bvr_add_num.match(
                            invoice.partner_bank_id.bvr_adherent_num):
                raise except_osv(_('UserError'),
                        _(('Your bank BVR adherent number must contain only '
                          'digits!\nPlease check your company '
                          'information for the invoice:\n%s') %(invoice_name)))
        return ''

def mako_template(text):
    """Build a Mako template.

    This template uses UTF-8 encoding
    """
    tmp_lookup  = TemplateLookup() #we need it in order to allow inclusion and inheritance
    return Template(text, input_encoding='utf-8', output_encoding='utf-8', lookup=tmp_lookup)

class BVRWebKitParser(webkit_report.WebKitParser):

    bvr_file_path = os.path.join('l10n_ch','report','bvr.mako')

    def create_single_pdf(self, cursor, uid, ids, data, report_xml, context=None):
        """generate the PDF"""
        context = context or {}
        if report_xml.report_type != 'webkit':
            return super(WebKitParser,self).create_single_pdf(cursor, uid, ids, data, report_xml, context=context)
        self.parser_instance = self.parser(cursor,
                                            uid,
                                            self.name2,
                                            context=context)
        self.pool = pooler.get_pool(cursor.dbname)
        objs = self.getObjects(cursor, uid, ids, context)
        self.parser_instance.set_context(objs, data, ids, report_xml.report_type)
        template =  False
        if report_xml.report_file :
            path = addons.get_module_resource(report_xml.report_file)
            if os.path.exists(path) :
                template = file(path).read()
        if not template and report_xml.report_webkit_data :
            template =  report_xml.report_webkit_data
        if not template :
            raise except_osv(_('Error'),_('Webkit Report template not found !'))
        header = report_xml.webkit_header.html
        footer = report_xml.webkit_header.footer_html
        if not header and report_xml.header:
          raise except_osv(
                _('No header defined for this Webkit report!'),
                _('Please set a header in company settings')
            )
        if not report_xml.header :
            header = ''
            default_head = addons.get_module_resource('report_webkit', 'default_header.html')
            with open(default_head,'r') as f:
                header = f.read()
        css = report_xml.webkit_header.css
        if not css :
            css = ''
        user = self.pool.get('res.users').browse(cursor, uid, uid)
        company = user.company_id
        body_mako_tpl = mako_template(template)
        #BVR specific
        bvr_path = addons.get_module_resource(self.bvr_file_path)
        body_bvr_tpl = mako_template(file(bvr_path).read())
        helper = report_helper.WebKitHelper(cursor, uid, report_xml.id, context)
        ##BVR Specific
        htmls = []
        for obj in objs :
            self.parser_instance.localcontext['objects'] = [obj]
            if not company.bvr_only:
                try:
                    html = body_mako_tpl.render(helper=helper,
                                                css=css,
                                                _=self.translate_call,
                                                **self.parser_instance.localcontext)
                except Exception, e:
                   raise Exception(exceptions.text_error_template().render())
                htmls.append(html)
            if not company.invoice_only:
                try:
                    bvr = body_bvr_tpl.render(helper=helper,
                                              css=css,
                                              _=self.translate_call,
                                              **self.parser_instance.localcontext)
                except Exception, e:
                   raise Exception(exceptions.text_error_template().render())
                htmls.append(bvr)
        head_mako_tpl = Template(header, input_encoding='utf-8', output_encoding='utf-8')
        try:
            head = head_mako_tpl.render(helper=helper,
                                        css=css,
                                        _debug=False,
                                        _=self.translate_call,
                                        **self.parser_instance.localcontext)
        except Exception, e:
           raise Exception(exceptions.text_error_template().render())
        foot = False
        if footer and company.invoice_only :
            foot_mako_tpl = Template(footer, input_encoding='utf-8', output_encoding='utf-8')
            try:
                foot = foot_mako_tpl.render(helper=helper,
                                            css=css,
                                            _=self.translate_call,
                                            **self.parser_instance.localcontext)
            except Exception, e:
               raise Exception(exceptions.text_error_template().render())
        if report_xml.webkit_debug :
            try:
                deb = head_mako_tpl.render(helper=helper,
                                            css=css,
                                            _debug=html,
                                            _=self.translate_call,
                                            **self.parser_instance.localcontext)
            except Exception, e:
               raise Exception(exceptions.text_error_template().render())
            return (deb, 'html')
        bin = self.get_lib(cursor, uid)
        pdf = self.generate_pdf(bin, report_xml, head, foot, htmls)
        return (pdf, 'pdf')

BVRWebKitParser('report.invoice_web_bvr',
               'account.invoice',
               'addons/l10n_ch/report/account_invoice.mako',
               parser=l10n_ch_report_webkit_html)

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<|endoftext|>


<commit_before># Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
# tree. An additional intellectual property rights grant can be found
# in the file PATENTS.  All contributing project authors may
# be found in the AUTHORS file in the root of the source tree.

{
  'includes': [
    '../../../../common_settings.gypi', # Common settings
  ],
  'targets': [
    {
      'target_name': 'audio_coding_module',
      'type': '<(library)',
      'dependencies': [
        '../../codecs/CNG/main/source/cng.gyp:CNG',
        '../../codecs/G711/main/source/g711.gyp:G711',
        '../../codecs/G722/main/source/g722.gyp:G722',
        '../../codecs/iLBC/main/source/ilbc.gyp:iLBC',
        '../../codecs/iSAC/main/source/isac.gyp:iSAC',
        '../../codecs/iSAC/fix/source/isacfix.gyp:iSACFix',
        '../../codecs/PCM16B/main/source/pcm16b.gyp:PCM16B',
        '../../NetEQ/main/source/neteq.gyp:NetEq',
        '../../../../common_audio/resampler/main/source/resampler.gyp:resampler',
        '../../../../common_audio/signal_processing_library/main/source/spl.gyp:spl',
        '../../../../common_audio/vad/main/source/vad.gyp:vad',
        '../../../../system_wrappers/source/system_wrappers.gyp:system_wrappers',
      ],
      'include_dirs': [
        '../interface',
        '../../../interface',
      ],
      'direct_dependent_settings': {
        'include_dirs': [
          '../interface',
          '../../../interface',
        ],
      },
      'sources': [
        # TODO: Remove files from here and P4 when ACM is slimmed down.
        '../interface/audio_coding_module.h',
        '../interface/audio_coding_module_typedefs.h',
        'acm_amr.cc',
        'acm_amr.h',
        'acm_amrwb.cc',
        'acm_amrwb.h',
        'acm_cng.cc',
        'acm_cng.h',
        'acm_codec_database.cc',
        'acm_codec_database.h',
        'acm_dtmf_detection.cc',
        'acm_dtmf_detection.h',
        'acm_dtmf_playout.cc',
        'acm_dtmf_playout.h',
        'acm_g722.cc',
        'acm_g722.h',
        'acm_g7221.cc',
        'acm_g7221.h',
        'acm_g7221c.cc',
        'acm_g7221c.h',
        'acm_g729.cc',
        'acm_g729.h',
        'acm_g7291.cc',
        'acm_g7291.h',
        'acm_generic_codec.cc',
        'acm_generic_codec.h',
        'acm_gsmfr.cc',
        'acm_gsmfr.h',
        'acm_ilbc.cc',
        'acm_ilbc.h',
        'acm_isac.cc',
        'acm_isac.h',
        'acm_isac_macros.h',
        'acm_neteq.cc',
        'acm_neteq.h',
        'acm_opus.cc',
        'acm_opus.h',
        'acm_speex.cc',
        'acm_speex.h',
        'acm_pcm16b.cc',
        'acm_pcm16b.h',
        'acm_pcma.cc',
        'acm_pcma.h',
        'acm_pcmu.cc',
        'acm_pcmu.h',
        'acm_red.cc',
        'acm_red.h',
        'acm_resampler.cc',
        'acm_resampler.h',
        'audio_coding_module.cc',
        'audio_coding_module_impl.cc',
        'audio_coding_module_impl.h',
      ],
    },
    {
      'target_name': 'audio_coding_module_test',
      'type': 'executable',
      'dependencies': [
        'audio_coding_module',
        '../../../../system_wrappers/source/system_wrappers.gyp:system_wrappers',
      ],
      'sources': [
           '../test/ACMTest.cpp',
           '../test/APITest.cpp',
           '../test/Channel.cpp',
           '../test/EncodeDecodeTest.cpp',
           '../test/EncodeToFileTest.cpp',
           '../test/iSACTest.cpp',
           '../test/PCMFile.cpp',
           '../test/RTPFile.cpp',
           '../test/SpatialAudio.cpp',
           '../test/TestAllCodecs.cpp',
           '../test/Tester.cpp',
           '../test/TestFEC.cpp',
           '../test/TestStereo.cpp',
           '../test/TestVADDTX.cpp',
           '../test/TimedTrace.cpp',
           '../test/TwoWayCommunication.cpp',
           '../test/utility.cpp',
      ],
      'conditions': [
        ['OS=="linux"', {
          'cflags': [
            '-fexceptions', # enable exceptions
          ],
        }],
      ],
    },
  ],
}

# Local Variables:
# tab-width:2
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=2 shiftwidth=2:
<commit_msg>exclude the ACM test target from building with chromium.<commit_after># Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
# tree. An additional intellectual property rights grant can be found
# in the file PATENTS.  All contributing project authors may
# be found in the AUTHORS file in the root of the source tree.

{
  'includes': [
    '../../../../common_settings.gypi', # Common settings
  ],
  'targets': [
    {
      'target_name': 'audio_coding_module',
      'type': '<(library)',
      'dependencies': [
        '../../codecs/CNG/main/source/cng.gyp:CNG',
        '../../codecs/G711/main/source/g711.gyp:G711',
        '../../codecs/G722/main/source/g722.gyp:G722',
        '../../codecs/iLBC/main/source/ilbc.gyp:iLBC',
        '../../codecs/iSAC/main/source/isac.gyp:iSAC',
        '../../codecs/iSAC/fix/source/isacfix.gyp:iSACFix',
        '../../codecs/PCM16B/main/source/pcm16b.gyp:PCM16B',
        '../../NetEQ/main/source/neteq.gyp:NetEq',
        '../../../../common_audio/resampler/main/source/resampler.gyp:resampler',
        '../../../../common_audio/signal_processing_library/main/source/spl.gyp:spl',
        '../../../../common_audio/vad/main/source/vad.gyp:vad',
        '../../../../system_wrappers/source/system_wrappers.gyp:system_wrappers',
      ],
      'include_dirs': [
        '../interface',
        '../../../interface',
      ],
      'direct_dependent_settings': {
        'include_dirs': [
          '../interface',
          '../../../interface',
        ],
      },
      'sources': [
        # TODO: Remove files from here and P4 when ACM is slimmed down.
        '../interface/audio_coding_module.h',
        '../interface/audio_coding_module_typedefs.h',
        'acm_amr.cc',
        'acm_amr.h',
        'acm_amrwb.cc',
        'acm_amrwb.h',
        'acm_cng.cc',
        'acm_cng.h',
        'acm_codec_database.cc',
        'acm_codec_database.h',
        'acm_dtmf_detection.cc',
        'acm_dtmf_detection.h',
        'acm_dtmf_playout.cc',
        'acm_dtmf_playout.h',
        'acm_g722.cc',
        'acm_g722.h',
        'acm_g7221.cc',
        'acm_g7221.h',
        'acm_g7221c.cc',
        'acm_g7221c.h',
        'acm_g729.cc',
        'acm_g729.h',
        'acm_g7291.cc',
        'acm_g7291.h',
        'acm_generic_codec.cc',
        'acm_generic_codec.h',
        'acm_gsmfr.cc',
        'acm_gsmfr.h',
        'acm_ilbc.cc',
        'acm_ilbc.h',
        'acm_isac.cc',
        'acm_isac.h',
        'acm_isac_macros.h',
        'acm_neteq.cc',
        'acm_neteq.h',
        'acm_opus.cc',
        'acm_opus.h',
        'acm_speex.cc',
        'acm_speex.h',
        'acm_pcm16b.cc',
        'acm_pcm16b.h',
        'acm_pcma.cc',
        'acm_pcma.h',
        'acm_pcmu.cc',
        'acm_pcmu.h',
        'acm_red.cc',
        'acm_red.h',
        'acm_resampler.cc',
        'acm_resampler.h',
        'audio_coding_module.cc',
        'audio_coding_module_impl.cc',
        'audio_coding_module_impl.h',
      ],
    },
  ],
  # Exclude the test targets when building with chromium.
  'conditions': [
    ['build_with_chromium==0', {
      'targets': [
        {
          'target_name': 'audio_coding_module_test',
          'type': 'executable',
          'dependencies': [
            'audio_coding_module',
            '../../../../system_wrappers/source/system_wrappers.gyp:system_wrappers',
          ],
          'sources': [
             '../test/ACMTest.cpp',
             '../test/APITest.cpp',
             '../test/Channel.cpp',
             '../test/EncodeDecodeTest.cpp',
             '../test/EncodeToFileTest.cpp',
             '../test/iSACTest.cpp',
             '../test/PCMFile.cpp',
             '../test/RTPFile.cpp',
             '../test/SpatialAudio.cpp',
             '../test/TestAllCodecs.cpp',
             '../test/Tester.cpp',
             '../test/TestFEC.cpp',
             '../test/TestStereo.cpp',
             '../test/TestVADDTX.cpp',
             '../test/TimedTrace.cpp',
             '../test/TwoWayCommunication.cpp',
             '../test/utility.cpp',
          ],
          'conditions': [
            ['OS=="linux"', {
              'cflags': [
                '-fexceptions', # enable exceptions
              ],
            }],
          ],
        },
      ],
    }],
  ],
}

# Local Variables:
# tab-width:2
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=2 shiftwidth=2:
<|endoftext|>


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

"""
 Created by Chouayakh Mahdi
 06/08/2010
 The package contains functions perform the anaphora processing
 Functions:
    delete_redon_nom_group : to delete the redundancy in the list of nominal groups
    delete_unuse_nom_gr : to delete the pronoun from the list of the nominal groups
    recover_nom_gr_list : to return the list of the nominal groups used by anaphora processing
"""
from sentence import *

"""
Statement of lists
"""
pronoun_list=['you', 'I', 'we', 'he', 'she', 'me', 'it', 'he', 'they', 'yours', 'mine', 'him']

def delete_redon_nom_group(nom_gr_list):
    """
    This function delete the redundancy in the list of nominal groups
    Input=nominal group list                Output=nominal group list
    """

    #init
    i=0
    j=0

    #We have to loop the list twice
    while i < len(nom_gr_list):
        j=i+1
        while j < len(nom_gr_list):
            #If same id => same nominal group
            if nom_gr_list[j].id==nom_gr_list[i].id:
                nom_gr_list=nom_gr_list[:j]+nom_gr_list[j+1:]
                #When we delete => we increment j
                j=j-1
            j=j+1
        i=i+1

    return nom_gr_list

def delete_unuse_nom_gr(nom_gr_list):
    """
    This function delete the pronoun from the list of the nominal groups
    Input=nominal group list                Output=nominal group list
    """

    #init
    i=0

    while i < len(nom_gr_list):
        for j in pronoun_list:
            #If the nominal group is a pronoun
            if [j]==nom_gr_list[i].noun and nom_gr_list[i].det==[]:
                nom_gr_list=nom_gr_list[:i]+nom_gr_list[i+1:]
                #When we delete => we increment i
                i=i-1
                break
        i=i+1
    return nom_gr_list

def recover_nom_gr_list(sentences):
    """
    This function return the list of the nominal groups used by anaphora processing
    Input=nominal group list                Output=nominal group list
    """

    #init
    nom_gr_list=[]

    #We recover all nominal groups of the sentences
    for i in sentences:
        nom_gr_list=nom_gr_list+i.sn
        for j in i.sv:
            nom_gr_list=nom_gr_list+j.d_obj
            for x in j.i_cmpl:
                nom_gr_list=nom_gr_list+x.nominal_group
            nom_gr_list=nom_gr_list+recover_nom_gr_list(j.vrb_sub_sentence)

    #We perform deletions
    nom_gr_list=delete_redon_nom_group(nom_gr_list)
    nom_gr_list=delete_unuse_nom_gr(nom_gr_list)

    #If sentence is empty, we return an empty list
    return nom_gr_list

def unit_tests():

if __name__ == '__main__':
    unit_tests()
    <commit_msg>done the first replacement with 'it'<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
 Created by Chouayakh Mahdi
 06/08/2010
 The package contains functions perform the anaphora processing
 Functions:
    delete_redon_nom_group : to delete the redundancy in the list of nominal groups
    delete_unuse_nom_gr : to delete the pronoun from the list of the nominal groups
    recover_nom_gr_list : to return the list of the nominal groups used by anaphora processing
"""
from sentence import *

"""
Statement of lists
"""
pronoun_list=['you', 'I', 'we', 'he', 'she', 'me', 'it', 'he', 'they', 'yours', 'mine', 'him']

def delete_redon_nom_group(nom_gr_list):
    """
    This function delete the redundancy in the list of nominal groups
    Input=nominal group list                Output=nominal group list
    """

    #init
    i=0
    j=0

    #We have to loop the list twice
    while i < len(nom_gr_list):
        j=i+1
        while j < len(nom_gr_list):
            #If same id => same nominal group
            if nom_gr_list[j].id==nom_gr_list[i].id:
                nom_gr_list=nom_gr_list[:j]+nom_gr_list[j+1:]
                #When we delete => we increment j
                j=j-1
            j=j+1
        i=i+1

    return nom_gr_list

def delete_unuse_nom_gr(nom_gr_list):
    """
    This function delete the pronoun from the list of the nominal groups
    Input=nominal group list                Output=nominal group list
    """

    #init
    i=0

    while i < len(nom_gr_list):
        for j in pronoun_list:
            #If the nominal group is a pronoun
            if [j]==nom_gr_list[i].noun and nom_gr_list[i].det==[]:
                nom_gr_list=nom_gr_list[:i]+nom_gr_list[i+1:]
                #When we delete => we increment i
                i=i-1
                break
        i=i+1
    return nom_gr_list

def recover_nom_gr_list(sentences):
    """
    This function return the list of the nominal groups used by anaphora processing
    Input=nominal group list                Output=nominal group list
    """

    #init
    nom_gr_list=[]

    #We recover all nominal groups of the sentences
    for i in sentences:
        nom_gr_list=nom_gr_list+i.sn
        for j in i.sv:
            nom_gr_list=nom_gr_list+j.d_obj
            for x in j.i_cmpl:
                nom_gr_list=nom_gr_list+x.nominal_group
            nom_gr_list=nom_gr_list+recover_nom_gr_list(j.vrb_sub_sentence)

    #We perform deletions
    nom_gr_list=delete_redon_nom_group(nom_gr_list)
    nom_gr_list=delete_unuse_nom_gr(nom_gr_list)

    #If sentence is empty, we return an empty list
    return nom_gr_list

def first_replacement(nom_gr_list, current_nom_gr):

    if current_nom_gr.noun==['it']:
        return nom_gr_list[0]

def unit_tests():

    """
    Function to perform unit tests
    """

    """
    ## Aim of this test : To use the complement of the noun and the duplication with 'and'
    """
    print ''
    print ('######################## test 1.1 ##############################')

    utterance="Jido's blue bottle is on the table. I'll play a guitar, a piano and a violon."
    print 'The object of our test is this utterance :'
    print utterance
    print '#################################################################'
    print ''
    sentences=[Sentence('statement', '',
            [Nominal_Group(['the'],['bottle'],['blue'],[Nominal_Group([],['Jido'],[],[],[])],[])],
            [Verbal_Group(['be'], [],'present simple',
                [],
                [Indirect_Complement(['on'],[Nominal_Group(['the'],['table'],[],[],[])])],
                [], [] ,'affirmative',[])]),
        Sentence('statement', '',
            [Nominal_Group([],['I'],[],[],[])],
            [Verbal_Group(['play'], [],'future simple',
                [Nominal_Group(['a'],['guitar'],[],[],[]),Nominal_Group(['a'],['piano'],[],[],[]),Nominal_Group(['a'],['violon'],[],[],[])],
                [],
                [], [] ,'affirmative',[])])]

    sentences[0].sn[0].id='azeaz'
    sentences[1].sn[0].id='eaz'
    sentences[1].sv[0].d_obj[0].id='s'
    sentences[1].sv[0].d_obj[1].id='z'
    sentences[1].sv[0].d_obj[2].id='e'

    list_gr=recover_nom_gr_list(sentences)

    c_gr=Nominal_Group([],['it'],[],[],[])
    gr = first_replacement(list_gr, c_gr)
    print gr.id
    print (str(gr))

    """
    """
    ## Aim of this test : Present the duality between the direct and indirect complement
    """
    print ''
    print ('######################## test 1.3 ##############################')

    utterance="It's on the table. give me the bottle. I don't give the bottle to you."
    print 'The object of our test is this utterance :'
    print utterance
    print '#################################################################'
    print ''

    sentences=[Sentence('statement', '',
            [Nominal_Group([],['it'],[],[],[])],
            [Verbal_Group(['be'], [],'present simple',
                [],
                [Indirect_Complement(['on'],[Nominal_Group(['the'],['table'],[],[],[])])],
                [], [] ,'affirmative',[])]),
        Sentence('imperative', '',
            [],
            [Verbal_Group(['give'], [],'present simple',
                [Nominal_Group(['the'],['bottle'],[],[],[])],
                [Indirect_Complement([],[Nominal_Group([],['me'],[],[],[])])],
                [], [] ,'affirmative',[])]),
        Sentence('statement', '',
            [Nominal_Group([],['I'],[],[],[])],
            [Verbal_Group(['give'], [],'present simple',
                [Nominal_Group(['the'],['bottle'],[],[],[])],
                [Indirect_Complement(['to'],[Nominal_Group([],['you'],[],[],[])])],
                [], [] ,'negative',[])])]

    sentences[0].sn[0].id='azeaz'
    sentences[0].sv[0].i_cmpl[0].nominal_group[0].id="4"
    sentences[1].sv[0].d_obj[0].id='sghj'
    sentences[1].sv[0].i_cmpl[0].nominal_group[0].id="9"
    sentences[2].sn[0].id='eaz'
    sentences[2].sv[0].d_obj[0].id='9'
    sentences[2].sv[0].i_cmpl[0].nominal_group[0].id="6"

    list_gr=recover_nom_gr_list(sentences)

    c_gr=Nominal_Group([],['it'],[],[],[])
    gr = first_replacement(list_gr, c_gr)
    print gr.id
    print (str(gr))
    """
if __name__ == '__main__':
    unit_tests()
    <|endoftext|>


<commit_before>import unittest2
import random
from functools import wraps
from monocyte.handler import s3 as s3_handler
from monocyte.handler import Resource

class S3Tests(unittest2.TestCase):

    def setUp(self):
        self.s3_handler = s3_handler.Bucket(lambda region_name: region_name not in [
            'cn-north-1', 'us-gov-west-1'])
        self.prefix = 'test-' + hex(random.randint(0, 2**48)) + '-'
        self.our_buckets = []

        def my_filter(old_function):
            @wraps(old_function)
            def new_function(*args):
                conn, bucket, checked_buckets = args
                if not bucket.name.startswith(self.prefix):
                    print("skipping bucket {}".format(bucket.name))
                    return
                else:
                    print("processing {}".format(bucket.name))
                checked_buckets = []
                return old_function(conn, bucket, checked_buckets)

            return new_function

        self.s3_handler.check_if_unwanted_resource = my_filter(
            self.s3_handler.check_if_unwanted_resource
        )

    def tearDown(self):
        for resource in self.our_buckets:
            print("to be deleted {}".format(resource.wrapped.name))
            self.s3_handler.dry_run = False
            self.s3_handler.delete(resource)

    def test_search_unwanted_resources_dry_run(self):
        self._given_bucket_mock('test-bucket', 'eu-west-1')
        self.s3_handler.dry_run = True

        resources = self.s3_handler.fetch_unwanted_resources()
        uniq_resources = self._uniq(resources)
        self.assertEqual(len(uniq_resources), 1)

        self.s3_handler.delete(uniq_resources[0])

        resources = self.s3_handler.fetch_unwanted_resources()
        uniq_resources = self._uniq(resources)
        self.assertEqual(len(uniq_resources), 1)

    def test_search_unwanted_resources_no_dry_run(self):
        self._given_bucket_mock('test-bucket', 'eu-west-1')
        # self.s3_handler.dry_run = False

        resources = self.s3_handler.fetch_unwanted_resources()
        uniq_resources = self._uniq(resources)
        self.assertEqual(len(uniq_resources), 1)

        self.s3_handler.delete(uniq_resources[0])

        resources = self.s3_handler.fetch_unwanted_resources()
        uniq_resources = self._uniq(resources)
        self.assertEqual(len(uniq_resources), 1)

    def _given_bucket_mock(self, bucket_name, region_name, create_key=False):
        conn = self.s3_handler.connect_to_region(region_name)
        bucket = conn.create_bucket(self.prefix + bucket_name,
                                    location=region_name)
        resource = Resource(resource=bucket,
                            resource_type='s3.Bucket',
                            resource_id='42',
                            creation_date='2015-11-23',
                            region=self.s3_handler.map_location(
                                region_name))
        self.our_buckets.append(resource)
        return bucket

    def _uniq(self, resources):
        found_names = []
        uniq_resources = []
        for resource in resources:
            name = resource.wrapped.name
            if name in found_names:
                continue
            uniq_resources.append(resource)
            found_names.append(name)

        return uniq_resources

if __name__ == "__main__":
    unittest2.main()
<commit_msg>Skip the '0x' part of the hex() output<commit_after>import unittest2
import random
from functools import wraps
from monocyte.handler import s3 as s3_handler
from monocyte.handler import Resource

class S3Tests(unittest2.TestCase):

    def setUp(self):
        self.s3_handler = s3_handler.Bucket(lambda region_name: region_name not in [
            'cn-north-1', 'us-gov-west-1'])
        self.prefix = 'test-' + hex(random.randint(0, 2**48))[2:] + '-'
        self.our_buckets = []

        def my_filter(old_function):
            @wraps(old_function)
            def new_function(*args):
                conn, bucket, checked_buckets = args
                if not bucket.name.startswith(self.prefix):
                    print("skipping bucket {}".format(bucket.name))
                    return
                else:
                    print("processing {}".format(bucket.name))
                checked_buckets = []
                return old_function(conn, bucket, checked_buckets)

            return new_function

        self.s3_handler.check_if_unwanted_resource = my_filter(
            self.s3_handler.check_if_unwanted_resource
        )

    def tearDown(self):
        for resource in self.our_buckets:
            print("to be deleted {}".format(resource.wrapped.name))
            self.s3_handler.dry_run = False
            self.s3_handler.delete(resource)

    def test_search_unwanted_resources_dry_run(self):
        self._given_bucket_mock('test-bucket', 'eu-west-1')
        self.s3_handler.dry_run = True

        resources = self.s3_handler.fetch_unwanted_resources()
        uniq_resources = self._uniq(resources)
        self.assertEqual(len(uniq_resources), 1)

        self.s3_handler.delete(uniq_resources[0])

        resources = self.s3_handler.fetch_unwanted_resources()
        uniq_resources = self._uniq(resources)
        self.assertEqual(len(uniq_resources), 1)

    def test_search_unwanted_resources_no_dry_run(self):
        self._given_bucket_mock('test-bucket', 'eu-west-1')
        # self.s3_handler.dry_run = False

        resources = self.s3_handler.fetch_unwanted_resources()
        uniq_resources = self._uniq(resources)
        self.assertEqual(len(uniq_resources), 1)

        self.s3_handler.delete(uniq_resources[0])

        resources = self.s3_handler.fetch_unwanted_resources()
        uniq_resources = self._uniq(resources)
        self.assertEqual(len(uniq_resources), 1)

    def _given_bucket_mock(self, bucket_name, region_name, create_key=False):
        conn = self.s3_handler.connect_to_region(region_name)
        bucket = conn.create_bucket(self.prefix + bucket_name,
                                    location=region_name)
        resource = Resource(resource=bucket,
                            resource_type='s3.Bucket',
                            resource_id='42',
                            creation_date='2015-11-23',
                            region=self.s3_handler.map_location(
                                region_name))
        self.our_buckets.append(resource)
        return bucket

    def _uniq(self, resources):
        found_names = []
        uniq_resources = []
        for resource in resources:
            name = resource.wrapped.name
            if name in found_names:
                continue
            uniq_resources.append(resource)
            found_names.append(name)

        return uniq_resources

if __name__ == "__main__":
    unittest2.main()
<|endoftext|>


<commit_before>"""
    This is an elementary first shot at developing a protocol for controlling an Vernier shield from an Arduino Uno
"""

import serial
import time
#cfg="/dev/ttyACM0  115200,8,N,1"

commands = { 'HALT':0x2C, 'GO':0x28, 'BTN':0x24,
             'DIGMDE1':0x31, 'DIGMDE2': 0x35,
             'ANAMDE1': 0x42, 'ANAMDE2': 0x46, 'ANAMDEB': 0x4A,
             'TRIGMODE': 0x52,
             'IMMEDA1': 0x60, 'IMMEDA2': 0x64, 'IMMEDD1': 0x70, 'IMMEDD2': 0x74,
             'STATUS': 0x50,
             }

class VernierCommunicate:
    """
    Create a communications toolkit for talking to our command protocol
    """
    serPort = 0

    # establish the virtual port through the USB port.
    def __init__(self):
        self.serPort = serial.Serial('/dev/ttyACM0', baudrate=115200, timeout=1)

    # send a command
    def sendCmd(self, pred, param1=0, param2=0):
        # send the predicate
        self.serPort.write(pred.to_bytes(1,byteorder='big'))
        if (pred&0b11)==1:  # 1 parameter to complete command
            self.serPort.write(param1.to_bytes(1,byteorder='big'))
        if (pred&0b11)==2:  # 2 parameters (send the 2nd one first)
            self.serPort.write(param2.to_bytes(1,byteorder='big'))
            self.serPort.write(param1.to_bytes(1,byteorder='big'))
        return self.getAck()

    # capture the handshake character right after we send the command
    def getAck(self):
        cc = self.serPort.read(1)
        if cc==b'!':
            return True
        if cc==b'?':
            return False
        else:
            raise Exception("Don't understand ack/nak response.",cc)

    # clear the buffer if something goes wrong.
    def clearBuffer(self):
        l = self.serPort.readline()
        while l!=b'':
            l = self.serPort.readline()

    # get a line of values
    def getNextLine(self):
        return self.serPort.readline().decode()

    # if we restarted out machine then this is the string that signals the Arduino is ready
    def waitForStart(self):
        try:
            for i in range(3):  # Take three shots at this.
                if self.sendCmd(commands['HALT']):
                    self.clearBuffer() # clear any other noise
                    return True
        except:
            return False

    # close the serial port so things can be cleaned up.
    def close(self):
        self.serPort.close()

def main():

    # Note that the first time we make this connection the Arduino sometimes makes
    ver = VernierCommunicate()

    try:
        if not ver.waitForStart():
                raise Exception( 'Arduino not responding.',0)
        print( 'begin...' )

        p1=1
        p2=31

        for acmd in commands:
            p1+=1
            p2+=5
            print( "Testing response to: {0} [0x{1:X}] p1: 0x{2:02X} p2: 0x{3:02X}".format(acmd,commands[acmd],p1,p2) )
            if ver.sendCmd(commands[acmd],param1=p1,param2=p2):
                l = ver.getNextLine()
                print(l.split('\r')[0])
                l = ver.getNextLine()
                print(l.split('\r')[0])
                print(" ----- ")

    except Exception as err:
        print( err.args )
        pass

    ver.close()

if __name__ == '__main__':
   main()<commit_msg>clean up<commit_after><|endoftext|>


<commit_before>#!/bin/env python

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

#
# oe_generate_empty_tile.py
# The OnEarth Empty Tile Generator.
#
#
# Global Imagery Browse Services
# NASA Jet Propulsion Laboratory
# 2014

import sys
import urllib
import xml.dom.minidom
from optparse import OptionParser
import png

toolName = "oe_generate_empty_tile.py"
versionNumber = "v0.6.3"

class ColorMap:
    """ColorMap metadata"""

    def __init__(self, units, colormap_entries, style):
        self.units = units
        self.colormap_entries = colormap_entries
        self.style = str(style).lower()

    def __repr__(self):
        if self.units != None:
            xml = '<ColorMap units="%s">' % (self.units)
        else:
            xml = '<ColorMap>'
        for colormap_entry in self.colormap_entries:
            xml = xml + '\n    ' + colormap_entry.__repr__()
        xml = xml + '\n</ColorMap>'
        return xml

    def __str__(self):
        return self.__repr__().encode(sys.stdout.encoding)

class ColorMapEntry:
    """ColorMapEntry values within a ColorMap"""

    def __init__(self, red, green, blue, transparent, source_value, value, label, nodata):
        self.red = int(red)
        self.green = int(green)
        self.blue = int(blue)
        self.transparent = transparent
        self.source_value = source_value
        self.value = value
        self.label = label
        self.nodata = nodata
        self.color = [float(red)/255.0,float(green)/255.0,float(blue)/255.0]

    def __repr__(self):
        if self.value != None:
            xml = '<ColorMapEntry rgb="%d,%d,%d" transparent="%s" nodata="%s" sourceValue="%s" value="%s" label="%s"/>' % (self.red, self.green, self.blue, self.transparent, self.nodata, self.source_value, self.value, self.label)
        else:
            xml = '<ColorMapEntry rgb="%d,%d,%d" transparent="%s" nodata="%s" sourceValue="%s" label="%s"/>' % (self.red, self.green, self.blue, self.transparent, self.nodata, self.source_value, self.label)
        return xml

    def __str__(self):
        return self.__repr__().encode(sys.stdout.encoding)

def parse_colormap(colormap_location, verbose):

    try:
        if verbose:
            print "Reading color map:", colormap_location
        colormap_file = open(colormap_location,'r')
        dom = xml.dom.minidom.parse(colormap_file)
        colormap_file.close()
    except IOError:
        print "Accessing URL", colormap_location
        try:
            dom = xml.dom.minidom.parse(urllib.urlopen(colormap_location))
        except:
            msg = "URL " + colormap_location + " is not accessible"
            print >> sys.stderr, msg
            raise Exception(msg)

    colormap_element = dom.getElementsByTagName("ColorMap")[0]
    try:
        units = colormap_element.attributes['units'].value
    except KeyError:
        units = None
    if verbose:
        print "ColorMap units:", units

    style = "discrete"
    colormap_entries = []
    colormapentry_elements = colormap_element.getElementsByTagName("ColorMapEntry")
    for colormapentry in colormapentry_elements:
        rgb = colormapentry.attributes['rgb'].value
        red, green, blue = rgb.split(',')
        try:
            value = colormapentry.attributes['value'].value
            if "(" in value or "[" in value:
                style = "range"
        except KeyError:
            value = None
            style = "classification"
        try:
            transparent = True if colormapentry.attributes['transparent'].value.lower() == 'true' else False
        except KeyError:
            transparent = False
        try:
            source_value = colormapentry.attributes['sourceValue'].value
        except KeyError:
            source_value = value
        try:
            label = colormapentry.attributes['label'].value
        except KeyError:
            label = value
        try:
            nodata = True if colormapentry.attributes['nodata'].value.lower() == 'true' else False
        except KeyError:
            nodata = False

        colormap_entries.append(ColorMapEntry(red, green , blue, transparent, source_value, value, label, nodata))

    colormap = ColorMap(units, colormap_entries, style)
    if verbose:
        print "ColorMap style:", style
        print colormap

    return colormap

#-------------------------------------------------------------------------------

print toolName + ' ' + versionNumber + '\n'

usageText = toolName + " --colormap [file] --output [file] --height [int] --width [int] --type [palette]"

# Define command line options and args.
parser=OptionParser(usage=usageText, version=versionNumber)
parser.add_option('-c', '--colormap',
                  action='store', type='string', dest='colormap',
                  help='Full path or URL of colormap filename.')
parser.add_option('-f', '--format',
                  action='store', type='string', dest='format', default = 'png',
                  help='Format of output file. Supported formats: png')
parser.add_option('-i', '--index',
                  action='store', type='string', dest='index',
                  help='The index of the color map to be used as the empty tile palette entry, overrides nodata value')
parser.add_option('-o', '--output',
                  action='store', type='string', dest='output',
                  help='The full path of the output file')
parser.add_option('-t', '--type',
                  action='store', type='string', dest='type', default = 'palette',
                  help='The image type: rgba or palette. Default: palette')
parser.add_option('-u', '--sigevent_url',
                  action='store', type='string', dest='sigevent_url',
                  default=
                  'http://localhost:8100/sigevent/events/create',
                  help='Default:  http://localhost:8100/sigevent/events/create')
parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
                  default=False, help="Print out detailed log messages")
parser.add_option('-x', '--width',
                  action='store', type='string', dest='width', default = '512',
                  help='Width of the empty tile')
parser.add_option('-y', '--height',
                  action='store', type='string', dest='height', default = '512',
                  help='Height of the empty tile')

# read command line args
(options, args) = parser.parse_args()

if options.colormap:
    colormap_location = options.colormap
else:
    print "colormap file must be specified...exiting"
    exit()
if options.output:
    output_location = options.output
else:
    print "output file must be specified...exiting"
    exit()

color_index = 0

# parse colormap and get color entry
try:
    colormap = parse_colormap(colormap_location, options.verbose)
    colormap_entry = colormap.colormap_entries[color_index] # default to first entry if none specified
    if options.index != None:
        colormap_entry = colormap.colormap_entries[int(options.index)]
        color_index = int(options.index)
    else:
        for index,entry in enumerate(colormap.colormap_entries):
            if entry.nodata == True:
                colormap_entry = entry
                color_index = index
                break # use first nodata entry found
except Exception, e:
    print >> sys.stderr, toolName + ": ERROR: " + str(e) + "\n"
    sys.exit(1)

# generate empty_tile
try:
    if options.verbose:
        print "Using index " + str(color_index) + " with entry:\n" + str(colormap_entry)

    f = open(output_location, 'wb')

    if options.type == "palette":

        palette = []
        for j in range (0, 256):
            try:
                entry = colormap.colormap_entries[j]
                if entry.transparent == True:
                    alpha = 0
                else:
                    alpha = 255
                palette.append((entry.red,entry.green,entry.blue,alpha))
            except IndexError: # pad with zeroes
                palette.append((0,0,0,0))

        rows = []
        img = []
        for i in range (1, (int(options.width))+1):
            rows.append(color_index)
        for i in range (0, int(options.height)):
            img.append(rows)

        w = png.Writer(int(options.width), int(options.height), palette=palette, bitdepth=8)
        w.write(f, img)

    else: # use RGBA

        rows = []
        img = []
        for i in range (1, (int(options.width)*4)+1):
            if i%4 == 1:
                rows.append(colormap_entry.red)
            elif i%4 == 2:
                rows.append(colormap_entry.green)
            elif i%4 == 3:
                rows.append(colormap_entry.blue)
            elif i%4 == 0:
                if colormap_entry.transparent == True:
                    rows.append(0)
                else:
                    rows.append(255)

        for i in range (0, int(options.height)):
            img.append(rows)

        w = png.Writer(int(options.width), int(options.height), alpha=True)
        w.write(f, img)

    f.close()

    print "\nSuccessfully generated empty tile " + output_location + " of size: " + str(options.width) + " by " + str(options.height)

except IOError,e:
    print >> sys.stderr, toolName + ": " + str(e)
    sys.exit(1)

exit()
<commit_msg>GIBS-888 Empty tile tool now uses all color map entries in XML input<commit_after>#!/bin/env python

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

#
# oe_generate_empty_tile.py
# The OnEarth Empty Tile Generator.
#
#
# Global Imagery Browse Services
# NASA Jet Propulsion Laboratory
# 2015

import sys
import urllib
import xml.dom.minidom
from optparse import OptionParser
import png

toolName = "oe_generate_empty_tile.py"
versionNumber = "v0.6.4"

class ColorMap:
    """ColorMap metadata"""

    def __init__(self, units, colormap_entries, style):
        self.units = units
        self.colormap_entries = colormap_entries
        self.style = str(style).lower()

    def __repr__(self):
        if self.units != None:
            xml = '<ColorMap units="%s">' % (self.units)
        else:
            xml = '<ColorMap>'
        for colormap_entry in self.colormap_entries:
            xml = xml + '\n    ' + colormap_entry.__repr__()
        xml = xml + '\n</ColorMap>'
        return xml

    def __str__(self):
        return self.__repr__().encode(sys.stdout.encoding)

class ColorMapEntry:
    """ColorMapEntry values within a ColorMap"""

    def __init__(self, red, green, blue, transparent, source_value, value, label, nodata):
        self.red = int(red)
        self.green = int(green)
        self.blue = int(blue)
        self.transparent = transparent
        self.source_value = source_value
        self.value = value
        self.label = label
        self.nodata = nodata
        self.color = [float(red)/255.0,float(green)/255.0,float(blue)/255.0]

    def __repr__(self):
        if self.value != None:
            xml = '<ColorMapEntry rgb="%d,%d,%d" transparent="%s" nodata="%s" sourceValue="%s" value="%s" label="%s"/>' % (self.red, self.green, self.blue, self.transparent, self.nodata, self.source_value, self.value, self.label)
        else:
            xml = '<ColorMapEntry rgb="%d,%d,%d" transparent="%s" nodata="%s" sourceValue="%s" label="%s"/>' % (self.red, self.green, self.blue, self.transparent, self.nodata, self.source_value, self.label)
        return xml

    def __str__(self):
        return self.__repr__().encode(sys.stdout.encoding)

def parse_colormap(colormap_location, verbose):

    try:
        if verbose:
            print "Reading color map:", colormap_location
        colormap_file = open(colormap_location,'r')
        dom = xml.dom.minidom.parse(colormap_file)
        colormap_file.close()
    except IOError:
        print "Accessing URL", colormap_location
        try:
            dom = xml.dom.minidom.parse(urllib.urlopen(colormap_location))
        except:
            msg = "URL " + colormap_location + " is not accessible"
            print >> sys.stderr, msg
            raise Exception(msg)

#     colormap_element = dom.getElementsByTagName("ColorMap")[0]
#     try:
#         units = colormap_element.attributes['units'].value
#     except KeyError:
#         units = None
#     if verbose:
#         print "ColorMap units:", units

    style = "discrete"
    colormap_entries = []
    colormapentry_elements = dom.getElementsByTagName("ColorMapEntry")
    for colormapentry in colormapentry_elements:
        rgb = colormapentry.attributes['rgb'].value
        red, green, blue = rgb.split(',')
        try:
            value = colormapentry.attributes['value'].value
            if "(" in value or "[" in value:
                style = "range"
        except KeyError:
            value = None
            style = "classification"
        try:
            transparent = True if colormapentry.attributes['transparent'].value.lower() == 'true' else False
        except KeyError:
            transparent = False
        try:
            source_value = colormapentry.attributes['sourceValue'].value
        except KeyError:
            source_value = value
        try:
            label = colormapentry.attributes['label'].value
        except KeyError:
            label = value
        try:
            nodata = True if colormapentry.attributes['nodata'].value.lower() == 'true' else False
        except KeyError:
            nodata = False

        colormap_entries.append(ColorMapEntry(red, green , blue, transparent, source_value, value, label, nodata))

    colormap = ColorMap(None, colormap_entries, style)
    if verbose:
        print "ColorMap style:", style
        print colormap

    return colormap

#-------------------------------------------------------------------------------

print toolName + ' ' + versionNumber + '\n'

usageText = toolName + " --colormap [file] --output [file] --height [int] --width [int] --type [palette]"

# Define command line options and args.
parser=OptionParser(usage=usageText, version=versionNumber)
parser.add_option('-c', '--colormap',
                  action='store', type='string', dest='colormap',
                  help='Full path or URL of colormap filename.')
parser.add_option('-f', '--format',
                  action='store', type='string', dest='format', default = 'png',
                  help='Format of output file. Supported formats: png')
parser.add_option('-i', '--index',
                  action='store', type='string', dest='index',
                  help='The index of the color map to be used as the empty tile palette entry, overrides nodata value')
parser.add_option('-o', '--output',
                  action='store', type='string', dest='output',
                  help='The full path of the output file')
parser.add_option('-t', '--type',
                  action='store', type='string', dest='type', default = 'palette',
                  help='The image type: rgba or palette. Default: palette')
parser.add_option('-u', '--sigevent_url',
                  action='store', type='string', dest='sigevent_url',
                  default=
                  'http://localhost:8100/sigevent/events/create',
                  help='Default:  http://localhost:8100/sigevent/events/create')
parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
                  default=False, help="Print out detailed log messages")
parser.add_option('-x', '--width',
                  action='store', type='string', dest='width', default = '512',
                  help='Width of the empty tile')
parser.add_option('-y', '--height',
                  action='store', type='string', dest='height', default = '512',
                  help='Height of the empty tile')

# read command line args
(options, args) = parser.parse_args()

if options.colormap:
    colormap_location = options.colormap
else:
    print "colormap file must be specified...exiting"
    exit()
if options.output:
    output_location = options.output
else:
    print "output file must be specified...exiting"
    exit()

color_index = 0

# parse colormap and get color entry
try:
    colormap = parse_colormap(colormap_location, options.verbose)
    colormap_entry = colormap.colormap_entries[color_index] # default to first entry if none specified
    if options.index != None:
        colormap_entry = colormap.colormap_entries[int(options.index)]
        color_index = int(options.index)
    else:
        for index,entry in enumerate(colormap.colormap_entries):
            if entry.nodata == True:
                colormap_entry = entry
                color_index = index
                break # use first nodata entry found
except Exception, e:
    print >> sys.stderr, toolName + ": ERROR: " + str(e) + "\n"
    sys.exit(1)

# generate empty_tile
try:
    if options.verbose:
        print "Using index " + str(color_index) + " with entry:\n" + str(colormap_entry)

    f = open(output_location, 'wb')

    if options.type == "palette":

        palette = []
        for j in range (0, 256):
            try:
                entry = colormap.colormap_entries[j]
                if entry.transparent == True:
                    alpha = 0
                else:
                    alpha = 255
                palette.append((entry.red,entry.green,entry.blue,alpha))
            except IndexError: # pad with zeroes
                palette.append((0,0,0,0))

        rows = []
        img = []
        for i in range (1, (int(options.width))+1):
            rows.append(color_index)
        for i in range (0, int(options.height)):
            img.append(rows)

        w = png.Writer(int(options.width), int(options.height), palette=palette, bitdepth=8)
        w.write(f, img)

    else: # use RGBA

        rows = []
        img = []
        for i in range (1, (int(options.width)*4)+1):
            if i%4 == 1:
                rows.append(colormap_entry.red)
            elif i%4 == 2:
                rows.append(colormap_entry.green)
            elif i%4 == 3:
                rows.append(colormap_entry.blue)
            elif i%4 == 0:
                if colormap_entry.transparent == True:
                    rows.append(0)
                else:
                    rows.append(255)

        for i in range (0, int(options.height)):
            img.append(rows)

        w = png.Writer(int(options.width), int(options.height), alpha=True)
        w.write(f, img)

    f.close()

    print "\nSuccessfully generated empty tile " + output_location + " of size: " + str(options.width) + " by " + str(options.height)

except IOError,e:
    print >> sys.stderr, toolName + ": " + str(e)
    sys.exit(1)

exit()
<|endoftext|>


<commit_before>import unittest

import chainer
from chainer.backends import cuda
from chainer import distributions
from chainer import testing
from chainer.testing import attr
import numpy

@testing.parameterize(*testing.product({
    'shape': [(3, 2), (1,)],
    'is_variable': [True, False],
}))
@testing.fix_random()
class TestKLDivergence(unittest.TestCase):

    def check_kl(self, dist1, dist2):
        kl = chainer.kl_divergence(dist1, dist2).data
        if isinstance(kl, cuda.ndarray):
            kl = kl.get()

        sample = dist1.sample(300000)
        mc_kl = dist1.log_prob(sample).data - dist2.log_prob(sample).data
        if isinstance(mc_kl, cuda.ndarray):
            mc_kl = mc_kl.get()
        mc_kl = numpy.nanmean(mc_kl, axis=0)

        testing.assert_allclose(kl, mc_kl, atol=1e-2, rtol=1e-2)

    def encode_params(self, params, is_gpu=False):
        if is_gpu:
            params = {k: cuda.to_gpu(v) for k, v in params.items()}

        if self.is_variable:
            params = {k: chainer.Variable(v) for k, v in params.items()}

        return params

    def make_bernoulli_dist(self, is_gpu=False):
        p = numpy.random.uniform(0, 1, self.shape).astype(numpy.float32)
        params = self.encode_params({"p": p}, is_gpu)
        return distributions.Bernoulli(**params)

    def make_beta_dist(self, is_gpu=False):
        a = numpy.random.uniform(0.5, 10, self.shape).astype(numpy.float32)
        b = numpy.random.uniform(0.5, 10, self.shape).astype(numpy.float32)
        params = self.encode_params({"a": a, "b": b}, is_gpu)
        return distributions.Beta(**params)

    def make_categorical_dist(self, is_gpu=False):
        p = numpy.random.normal(size=self.shape+(3,)).astype(numpy.float32)
        p = numpy.exp(p)
        p /= numpy.expand_dims(p.sum(axis=-1), axis=-1)
        params = self.encode_params({"p": p}, is_gpu)
        return distributions.Categorical(**params)

    def make_dirichlet_dist(self, is_gpu=False):
        alpha = numpy.random.uniform(
            1, 10, self.shape + (3,)).astype(numpy.float32)
        params = self.encode_params({"alpha": alpha}, is_gpu)
        return distributions.Dirichlet(**params)

    def make_laplace_dist(self, is_gpu=False):
        loc = numpy.random.uniform(-1, 1, self.shape).astype(numpy.float32)
        scale = numpy.exp(
            numpy.random.uniform(-1, 1, self.shape)).astype(numpy.float32)
        params = self.encode_params({"loc": loc, "scale": scale}, is_gpu)
        return distributions.Laplace(**params)

    def make_log_normal_dist(self, is_gpu=False):
        mu = numpy.random.uniform(-1, 1, self.shape).astype(numpy.float32)
        sigma = numpy.exp(
            numpy.random.uniform(-1, 1, self.shape)).astype(numpy.float32)
        params = self.encode_params({"mu": mu, "sigma": sigma}, is_gpu)
        return distributions.LogNormal(**params)

    def make_normal_dist(self, is_gpu=False, use_log_scale=False):
        loc = numpy.random.uniform(-1, 1, self.shape).astype(numpy.float32)
        if use_log_scale:
            log_scale = numpy.random.uniform(
                -1, 1, self.shape).astype(numpy.float32)
            params = self.encode_params(
                {"loc": loc, "log_scale": log_scale}, is_gpu)
        else:
            scale = numpy.exp(
                numpy.random.uniform(-1, 1, self.shape)).astype(numpy.float32)
            params = self.encode_params({"loc": loc, "scale": scale}, is_gpu)
        return distributions.Normal(**params)

    def make_multivariatenormal_dist(self, is_gpu=False):
        loc = numpy.random.uniform(
            -1, 1, self.shape + (3,)).astype(numpy.float32)
        cov = numpy.random.normal(size=(numpy.prod(self.shape),) + (3, 3))
        cov = [cov_.dot(cov_.T) for cov_ in cov]
        cov = numpy.vstack(cov).reshape(self.shape + (3, 3))
        scale_tril = numpy.linalg.cholesky(cov).astype(numpy.float32)
        params = self.encode_params(
            {"loc": loc, "scale_tril": scale_tril}, is_gpu)
        return distributions.MultivariateNormal(**params)

    def make_uniform_dist(self, is_gpu=False, low=None, high=None,
                          loc=None, scale=None, use_loc_scale=False):
        if use_loc_scale:
            if loc is None:
                loc = numpy.random.uniform(
                    -3, 0, self.shape).astype(numpy.float32)
            if scale is None:
                scale = numpy.random.uniform(
                    1, 5, self.shape).astype(numpy.float32)
            params = self.encode_params({"loc": loc, "scale": scale}, is_gpu)
        else:
            if low is None:
                low = numpy.random.uniform(
                    -3, 0, self.shape).astype(numpy.float32)
            if high is None:
                high = numpy.random.uniform(
                    low + 1, low + 6, self.shape).astype(numpy.float32)
            params = self.encode_params({"low": low, "high": high}, is_gpu)
        return distributions.Uniform(**params)

    def test_bernoulli_bernoulli_cpu(self):
        dist1 = self.make_bernoulli_dist()
        dist2 = self.make_bernoulli_dist()
        self.check_kl(dist1, dist2)

    @attr.gpu
    def test_bernoulli_bernoulli_gpu(self):
        dist1 = self.make_bernoulli_dist(True)
        dist2 = self.make_bernoulli_dist(True)
        self.check_kl(dist1, dist2)

    @testing.with_requires('scipy')
    def test_beta_beta_cpu(self):
        dist1 = self.make_beta_dist()
        dist2 = self.make_beta_dist()
        self.check_kl(dist1, dist2)

    @testing.with_requires('scipy')
    @attr.gpu
    def test_beta_beta_gpu(self):
        dist1 = self.make_beta_dist(True)
        dist2 = self.make_beta_dist(True)
        self.check_kl(dist1, dist2)

    @testing.with_requires('numpy>=1.11')
    def test_categorical_categorical_cpu(self):
        dist1 = self.make_categorical_dist()
        dist2 = self.make_categorical_dist()
        self.check_kl(dist1, dist2)

    @attr.gpu
    def test_categorical_categorical_gpu(self):
        dist1 = self.make_categorical_dist(True)
        dist2 = self.make_categorical_dist(True)
        self.check_kl(dist1, dist2)

    @testing.with_requires('scipy')
    def test_dirichlet_dirichlet_cpu(self):
        dist1 = self.make_dirichlet_dist()
        dist2 = self.make_dirichlet_dist()
        self.check_kl(dist1, dist2)

    @testing.with_requires('scipy')
    @attr.gpu
    def test_dirichlet_dirichlet_gpu(self):
        dist1 = self.make_dirichlet_dist(True)
        dist2 = self.make_dirichlet_dist(True)
        self.check_kl(dist1, dist2)

    def test_laplace_laplace_cpu(self):
        dist1 = self.make_laplace_dist()
        dist2 = self.make_laplace_dist()
        self.check_kl(dist1, dist2)

    @attr.gpu
    def test_laplace_laplace_gpu(self):
        dist1 = self.make_laplace_dist(True)
        dist2 = self.make_laplace_dist(True)
        self.check_kl(dist1, dist2)

    def test_log_normal_log_normal_cpu(self):
        dist1 = self.make_log_normal_dist()
        dist2 = self.make_log_normal_dist()
        self.check_kl(dist1, dist2)

    @attr.gpu
    def test_log_normal_log_normal_gpu(self):
        dist1 = self.make_log_normal_dist(True)
        dist2 = self.make_log_normal_dist(True)
        self.check_kl(dist1, dist2)

    def test_normal_normal_cpu(self):
        for use_log_scale1 in [True, False]:
            for use_log_scale2 in [True, False]:
                dist1 = self.make_normal_dist(use_log_scale=use_log_scale1)
                dist2 = self.make_normal_dist(use_log_scale=use_log_scale2)
                self.check_kl(dist1, dist2)

    @attr.gpu
    def test_normal_normal_gpu(self):
        for use_log_scale1 in [True, False]:
            for use_log_scale2 in [True, False]:
                dist1 = self.make_normal_dist(
                    True, use_log_scale=use_log_scale1)
                dist2 = self.make_normal_dist(
                    True, use_log_scale=use_log_scale2)
                self.check_kl(dist1, dist2)

    @testing.with_requires('scipy')
    def test_multivariatenormal_multivariatenormal_cpu(self):
        dist1 = self.make_multivariatenormal_dist()
        dist2 = self.make_multivariatenormal_dist()
        self.check_kl(dist1, dist2)

    @attr.gpu
    def test_multivariatenormal_multivariatenormal_gpu(self):
        dist1 = self.make_multivariatenormal_dist(True)
        dist2 = self.make_multivariatenormal_dist(True)
        self.check_kl(dist1, dist2)

    def test_uniform_uniform_cpu(self):
        for use_loc_scale1 in [True, False]:
            for use_loc_scale2 in [True, False]:
                dist1 = self.make_uniform_dist(use_loc_scale=use_loc_scale1)
                dist2 = self.make_uniform_dist(use_loc_scale=use_loc_scale2)
                self.check_kl(dist1, dist2)

    @attr.gpu
    def test_uniform_uniform_gpu(self):
        for use_loc_scale1 in [True, False]:
            for use_loc_scale2 in [True, False]:
                dist1 = self.make_uniform_dist(
                    True, use_loc_scale=use_loc_scale1)
                dist2 = self.make_uniform_dist(
                    True, use_loc_scale=use_loc_scale2)
                self.check_kl(dist1, dist2)

testing.run_module(__name__, __file__)
<commit_msg>Fix kl test conditions<commit_after>import unittest

import chainer
from chainer.backends import cuda
from chainer import distributions
from chainer import testing
from chainer.testing import attr
import numpy

@testing.parameterize(*testing.product({
    'shape': [(3, 2), (1,)],
    'is_variable': [True, False],
}))
@testing.fix_random()
class TestKLDivergence(unittest.TestCase):

    def check_kl(self, dist1, dist2):
        kl = chainer.kl_divergence(dist1, dist2).data
        if isinstance(kl, cuda.ndarray):
            kl = kl.get()

        sample = dist1.sample(300000)
        mc_kl = dist1.log_prob(sample).data - dist2.log_prob(sample).data
        if isinstance(mc_kl, cuda.ndarray):
            mc_kl = mc_kl.get()
        mc_kl = numpy.nanmean(mc_kl, axis=0)

        testing.assert_allclose(kl, mc_kl, atol=1e-2, rtol=1e-2)

    def encode_params(self, params, is_gpu=False):
        if is_gpu:
            params = {k: cuda.to_gpu(v) for k, v in params.items()}

        if self.is_variable:
            params = {k: chainer.Variable(v) for k, v in params.items()}

        return params

    def make_bernoulli_dist(self, is_gpu=False):
        p = numpy.random.uniform(0, 1, self.shape).astype(numpy.float32)
        params = self.encode_params({"p": p}, is_gpu)
        return distributions.Bernoulli(**params)

    def make_beta_dist(self, is_gpu=False):
        a = numpy.random.uniform(0.5, 10, self.shape).astype(numpy.float32)
        b = numpy.random.uniform(0.5, 10, self.shape).astype(numpy.float32)
        params = self.encode_params({"a": a, "b": b}, is_gpu)
        return distributions.Beta(**params)

    def make_categorical_dist(self, is_gpu=False):
        p = numpy.random.normal(size=self.shape+(3,)).astype(numpy.float32)
        p = numpy.exp(p)
        p /= numpy.expand_dims(p.sum(axis=-1), axis=-1)
        params = self.encode_params({"p": p}, is_gpu)
        return distributions.Categorical(**params)

    def make_dirichlet_dist(self, is_gpu=False):
        alpha = numpy.random.uniform(
            0.5, 10, self.shape + (3,)).astype(numpy.float32)
        params = self.encode_params({"alpha": alpha}, is_gpu)
        return distributions.Dirichlet(**params)

    def make_laplace_dist(self, is_gpu=False):
        loc = numpy.random.uniform(-1, 1, self.shape).astype(numpy.float32)
        scale = numpy.exp(
            numpy.random.uniform(-1, 1, self.shape)).astype(numpy.float32)
        params = self.encode_params({"loc": loc, "scale": scale}, is_gpu)
        return distributions.Laplace(**params)

    def make_log_normal_dist(self, is_gpu=False):
        mu = numpy.random.uniform(-1, 1, self.shape).astype(numpy.float32)
        sigma = numpy.exp(
            numpy.random.uniform(-1, 1, self.shape)).astype(numpy.float32)
        params = self.encode_params({"mu": mu, "sigma": sigma}, is_gpu)
        return distributions.LogNormal(**params)

    def make_normal_dist(self, is_gpu=False, use_log_scale=False):
        loc = numpy.random.uniform(-1, 1, self.shape).astype(numpy.float32)
        if use_log_scale:
            log_scale = numpy.random.uniform(
                -1, 1, self.shape).astype(numpy.float32)
            params = self.encode_params(
                {"loc": loc, "log_scale": log_scale}, is_gpu)
        else:
            scale = numpy.exp(
                numpy.random.uniform(-1, 1, self.shape)).astype(numpy.float32)
            params = self.encode_params({"loc": loc, "scale": scale}, is_gpu)
        return distributions.Normal(**params)

    def make_multivariatenormal_dist(self, is_gpu=False):
        loc = numpy.random.uniform(
            -1, 1, self.shape + (3,)).astype(numpy.float32)
        cov = numpy.random.normal(size=(numpy.prod(self.shape),) + (3, 3))
        cov = [cov_.dot(cov_.T) for cov_ in cov]
        cov = numpy.vstack(cov).reshape(self.shape + (3, 3))
        scale_tril = numpy.linalg.cholesky(cov).astype(numpy.float32)
        params = self.encode_params(
            {"loc": loc, "scale_tril": scale_tril}, is_gpu)
        return distributions.MultivariateNormal(**params)

    def make_uniform_dist(self, is_gpu=False, low=None, high=None,
                          loc=None, scale=None, use_loc_scale=False):
        if use_loc_scale:
            if loc is None:
                loc = numpy.random.uniform(
                    -3, 0, self.shape).astype(numpy.float32)
            if scale is None:
                scale = numpy.random.uniform(
                    1, 5, self.shape).astype(numpy.float32)
            params = self.encode_params({"loc": loc, "scale": scale}, is_gpu)
        else:
            if low is None:
                low = numpy.random.uniform(
                    -3, 0, self.shape).astype(numpy.float32)
            if high is None:
                high = numpy.random.uniform(
                    low + 1, low + 6, self.shape).astype(numpy.float32)
            params = self.encode_params({"low": low, "high": high}, is_gpu)
        return distributions.Uniform(**params)

    def test_bernoulli_bernoulli_cpu(self):
        dist1 = self.make_bernoulli_dist()
        dist2 = self.make_bernoulli_dist()
        self.check_kl(dist1, dist2)

    @attr.gpu
    def test_bernoulli_bernoulli_gpu(self):
        dist1 = self.make_bernoulli_dist(True)
        dist2 = self.make_bernoulli_dist(True)
        self.check_kl(dist1, dist2)

    @testing.with_requires('scipy')
    def test_beta_beta_cpu(self):
        dist1 = self.make_beta_dist()
        dist2 = self.make_beta_dist()
        self.check_kl(dist1, dist2)

    @attr.gpu
    def test_beta_beta_gpu(self):
        dist1 = self.make_beta_dist(True)
        dist2 = self.make_beta_dist(True)
        self.check_kl(dist1, dist2)

    @testing.with_requires('numpy>=1.11')
    def test_categorical_categorical_cpu(self):
        dist1 = self.make_categorical_dist()
        dist2 = self.make_categorical_dist()
        self.check_kl(dist1, dist2)

    @attr.gpu
    def test_categorical_categorical_gpu(self):
        dist1 = self.make_categorical_dist(True)
        dist2 = self.make_categorical_dist(True)
        self.check_kl(dist1, dist2)

    @testing.with_requires('scipy')
    def test_dirichlet_dirichlet_cpu(self):
        dist1 = self.make_dirichlet_dist()
        dist2 = self.make_dirichlet_dist()
        self.check_kl(dist1, dist2)

    @attr.gpu
    def test_dirichlet_dirichlet_gpu(self):
        dist1 = self.make_dirichlet_dist(True)
        dist2 = self.make_dirichlet_dist(True)
        self.check_kl(dist1, dist2)

    def test_laplace_laplace_cpu(self):
        dist1 = self.make_laplace_dist()
        dist2 = self.make_laplace_dist()
        self.check_kl(dist1, dist2)

    @attr.gpu
    def test_laplace_laplace_gpu(self):
        dist1 = self.make_laplace_dist(True)
        dist2 = self.make_laplace_dist(True)
        self.check_kl(dist1, dist2)

    def test_log_normal_log_normal_cpu(self):
        dist1 = self.make_log_normal_dist()
        dist2 = self.make_log_normal_dist()
        self.check_kl(dist1, dist2)

    @attr.gpu
    def test_log_normal_log_normal_gpu(self):
        dist1 = self.make_log_normal_dist(True)
        dist2 = self.make_log_normal_dist(True)
        self.check_kl(dist1, dist2)

    def test_normal_normal_cpu(self):
        for use_log_scale1 in [True, False]:
            for use_log_scale2 in [True, False]:
                dist1 = self.make_normal_dist(use_log_scale=use_log_scale1)
                dist2 = self.make_normal_dist(use_log_scale=use_log_scale2)
                self.check_kl(dist1, dist2)

    @attr.gpu
    def test_normal_normal_gpu(self):
        for use_log_scale1 in [True, False]:
            for use_log_scale2 in [True, False]:
                dist1 = self.make_normal_dist(
                    True, use_log_scale=use_log_scale1)
                dist2 = self.make_normal_dist(
                    True, use_log_scale=use_log_scale2)
                self.check_kl(dist1, dist2)

    @testing.with_requires('scipy')
    def test_multivariatenormal_multivariatenormal_cpu(self):
        dist1 = self.make_multivariatenormal_dist()
        dist2 = self.make_multivariatenormal_dist()
        self.check_kl(dist1, dist2)

    @attr.gpu
    def test_multivariatenormal_multivariatenormal_gpu(self):
        dist1 = self.make_multivariatenormal_dist(True)
        dist2 = self.make_multivariatenormal_dist(True)
        self.check_kl(dist1, dist2)

    def test_uniform_uniform_cpu(self):
        for use_loc_scale1 in [True, False]:
            for use_loc_scale2 in [True, False]:
                dist1 = self.make_uniform_dist(use_loc_scale=use_loc_scale1)
                dist2 = self.make_uniform_dist(use_loc_scale=use_loc_scale2)
                self.check_kl(dist1, dist2)

    @attr.gpu
    def test_uniform_uniform_gpu(self):
        for use_loc_scale1 in [True, False]:
            for use_loc_scale2 in [True, False]:
                dist1 = self.make_uniform_dist(
                    True, use_loc_scale=use_loc_scale1)
                dist2 = self.make_uniform_dist(
                    True, use_loc_scale=use_loc_scale2)
                self.check_kl(dist1, dist2)

testing.run_module(__name__, __file__)
<|endoftext|>