import torch as t
import torch.nn as nn
from torch import  optim

net = Net()
# 为不同子网络设置不同的学习率，在finetune中经常用到
# 如果对某个参数不指定学习率，就使用最外层的默认学习率
optimizer =optim.SGD([
                {'params': net.features.parameters()}, # 学习率为1e-5
                {'params': net.classifier.parameters(), 'lr': 1e-2}
            ], lr=1e-5)
#只为两个全连接层设置较大的学习率，其余层的学习率较小
special_layers = nn.ModuleList([net.classifier[0], net.classifier[3]])
special_layers_params = list(map(id, special_layers.parameters()))
base_params = filter(lambda p: id(p) not in special_layers_params,
                     net.parameters())

optimizer = t.optim.SGD([
            {'params': base_params},
            {'params': special_layers.parameters(), 'lr': 0.01}
        ], lr=0.001 )

# 方法1: 调整学习率，新建一个optimizer
old_lr = 0.1
optimizer1 =optim.SGD([
                {'params': net.features.parameters()},
                {'params': net.classifier.parameters(), 'lr': old_lr*0.1}
            ], lr=1e-5)

# 首先定义一个LeNet网络
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.features = nn.Sequential(
                    nn.Conv2d(3, 6, 5),
                    nn.ReLU(),
                    nn.MaxPool2d(2,2),
                    nn.Conv2d(6, 16, 5),
                    nn.ReLU(),
                    nn.MaxPool2d(2,2)
        )
        self.classifier = nn.Sequential(
            nn.Linear(16 * 5 * 5, 120),
            nn.ReLU(),
            nn.Linear(120, 84),
            nn.ReLU(),
            nn.Linear(84, 10)
        )

    def forward(self, x):
        x = self.features(x)
        x = x.view(-1, 16 * 5 * 5)
        x = self.classifier(x)
        return x


# Copyright (c) 2012-2018 SoftBank Robotics. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the COPYING file.
import pytest

from qitoolchain.feed import is_url, tree_from_feed, ToolchainFeedParser

from qisrc.test.conftest import git_server  # pylint: disable=unused-import

# pylint: disable=redefined-outer-name

#
# Commons variables
#
default_oss_xml = """\
<feed>
    <package name="boost" url="boost.zip" />
</feed>
"""

default_third_part_xml = """\
<feed>
  <package name="oracle-jdk" url="jdk.zip" />
</feed>
"""

# feed url is either absolute or relative to the parent feed url
default_full_xml = """ \
<feed>
    <feed name="oss" path="feeds/oss.xml" />
    <feed name="3rdpart" url="3rdpart.xml" />
</feed>
"""

def test_is_url():
    assert not is_url(r"c:\foo\bar")
    assert is_url("http://foo.com/bar.xml")
    assert is_url("ftp://foo.com.bar.xml")

def test_parse_non_exising_path():
    # pylint: disable-msg=E1101
    with pytest.raises(Exception) as e:
        tree_from_feed("does/not/exists")
    assert "not an existing path" in e.value.message
    assert "nor an url" in e.value.message

def _generic_test_git(_git_server, _feed, full_xml, oss_xml, third_part_xml):
    _git_server.create_repo("toolchains.git")

    _git_server.push_file("toolchains.git", "feeds/oss.xml", oss_xml)
    _git_server.push_file("toolchains.git", "feeds/3rdpart.xml", third_part_xml)
    _git_server.push_file("toolchains.git", "feeds/full.xml", full_xml)

    git_url = _git_server.get_repo("toolchains.git").clone_url

    parser = ToolchainFeedParser("foo")
    parser.parse(git_url, name="full", branch="master")

    names = [x.name for x in parser.packages]
    assert names == ["boost", "oracle-jdk"]

def test_git(git_server, feed):
    _generic_test_git(git_server, feed, default_full_xml, default_oss_xml, default_third_part_xml)

def test_git_missing_url_and_path(git_server, feed):
    full_xml = """ \
<feed>
    <feed name="oss" />
    <feed name="3rdpart" url="3rdpart.xml" />
</feed>
"""
    with pytest.raises(AssertionError) as e:
        _generic_test_git(git_server, feed, full_xml, default_oss_xml, default_third_part_xml)
    assert "attributes must be set" in e.value.message
    assert "url" in e.value.message
    assert "path" in e.value.message

def test_git_bad_url(git_server, feed):
    # URL is bad because relative url are relative to the parent URL, and this one will add one more 'feeds' prefix.
    # The resulting built URL will end with share/qi/toolchains/foo.git/feeds/feeds/3rdpart.xml
    # and the double feeds/feeds/ makes it unknown
    full_xml = """ \
<feed>
    <feed name="oss" path="feeds/oss.xml" />
    <feed name="3rdpart" url="feeds/3rdpart.xml" />
</feed>
"""

    with pytest.raises(Exception) as e:
        _generic_test_git(git_server, feed, full_xml, default_oss_xml, default_third_part_xml)
    assert "not parse" in e.value.message
    assert "not an existing path" in e.value.message
    assert "nor an url" in e.value.message


from .lib import Instagram_functions
from .._common_lib import common_functions
from instagram.client import InstagramAPI

import time
from collections import Counter
from operator import itemgetter

class key_phrases_per_post_caption:

	def execute(self,**params):

		"""parse the parameters"""
		if ('instagram_access_token' in params) and ('instagram_client_secret' in params):
			api = InstagramAPI(access_token=params['instagram_access_token'], client_secret=params['instagram_client_secret'])
		else:
			return True, "[key_phrases_per_post]: Missing instagram token or instagram_client_secret information"

		if ('instagram_post_id' in params):
			post_id = params['instagram_post_id']
		else:
			return True, "[key_phrases_per_post]:  Missing instagram post identifier"

		if ('microsoft_base_url' in params):
			base_url = params['microsoft_base_url']
		else:
			return True, "[key_phrases_per_post]:  Missing microsoft base url"

		if ('microsoft_required_headers' in params):
			headers = params['microsoft_required_headers']
		else:
			return True, "[key_phrases_per_post]:  Missing microsof required headers"

		if ('microsoft_account_key' in params):
			account_key = params['microsoft_account_key']
		else:
			return True, "[key_phrases_per_post]:  Missing microsof account_key"

		return self.Trending_topics_in_post_caption(api,post_id,base_url,headers)

	def Trending_topics_in_post_caption(self,api,post_id,base_url,headers):
		listofkeyphrases=[]
		listresult=[]
		try:
			post=api.media(post_id)
			caption=[{'id':post.id,'text':post.caption.text}]
		except Exception as er:
			return True,str(er)

		result=common_functions.get_topic_in_commentslist(caption,base_url,headers)
		keyphrases=Counter([keyphrase
							for keyphrase in result
								])
		keyphrases=dict(keyphrases)
		for key in keyphrases:
			listresult.append({'key':key,'count':keyphrases[key]})
		listresult=sorted(listresult, key=itemgetter('count'), reverse=True)
		return False,listresult


# -*- coding: utf-8 -*-
from odoo import api, fields, models, exceptions, _
import logging
import queue

_logger = logging.getLogger(__name__)

class HrRfidAccessGroup(models.Model):
    _name = 'hr.rfid.access.group'
    _inherit = ['mail.thread']
    _description = 'Access Group'

    def access_group_generate_name(self):
        env = self.env['hr.rfid.access.group'].search([])

        if len(env) == 0:
            return 'Access Group 1'
        else:
            return 'Access Group ' + str(env[-1].id + 1)

    name = fields.Char(
        string='Name',
        help="Une étiquette pour aider à différencier les groupes d'accès",
        default=access_group_generate_name,
        limit=32,
        track_visibility='onchange',
    )

    employee_ids = fields.One2many(
        'hr.rfid.access.group.employee.rel',
        'access_group_id',
        string='Utilisateurs',
        help="Utilisateurs faisant partie de ce groupe d'accès",
    )

    contact_ids = fields.One2many(
        'hr.rfid.access.group.contact.rel',
        'access_group_id',
        string='Contacts',
        help="Contacts faisant partie de ce groupe d'accès"
    )

    door_ids = fields.One2many(
        'hr.rfid.access.group.door.rel',
        'access_group_id',
        string='Portes',
        help="Portes incluses dans ce groupe d'accès",
    )

    default_department_ids = fields.One2many(
        'hr.department',
        'hr_rfid_default_access_group',
        string='Département (Par Défaut)',
        help="Départements qui ont ce groupe d'accès par défaut",
    )

    department_ids = fields.Many2many(
        'hr.department',
        string='Départements',
        help="Départements qui ont ce groupe d'accès par défaut",
    )

    inherited_ids = fields.Many2many(
        comodel_name='hr.rfid.access.group',
        relation='access_group_inheritance',
        column1='inheritor',
        column2='inherited',
        string="Groupes d'accès hérités",
    )

    inheritor_ids = fields.Many2many(
        comodel_name='hr.rfid.access.group',
        relation='access_group_inheritance',
        column1='inherited',
        column2='inheritor',
        string='Héritiers',
        help="Accéder aux groupes qui ont hérité de celui-ci",
    )

    all_door_ids = fields.Many2many(
        'hr.rfid.access.group.door.rel',
        string='Toutes les portes',
        help="Toutes les portes, y compris celles héritées",
        compute='_compute_all_doors',
    )

    all_employee_ids = fields.Many2many(
        'hr.rfid.access.group.employee.rel',
        string='Tous les employés',
        help="Tous les employés qui utilisent ce groupe d'accès, y compris ceux des héritiers",
        compute='_compute_all_employees',
    )

    all_contact_ids = fields.Many2many(
        'hr.rfid.access.group.contact.rel',
        string='Tous les contacts',
        help="Tous les contacts qui utilisent ce groupe d'accès, y compris ceux des héritiers",
        compute='_compute_all_contacts',
    )

    def add_doors(self, door_ids, time_schedule=None):
        if time_schedule is None:
            time_schedule = self.env.ref('hr_rfid.hr_rfid_time_schedule_0')

        rel_env = self.env['hr.rfid.access.group.door.rel']
        for door in door_ids:
            res = rel_env.search([ ('access_group_id', '=', self.id),
                                   ('door_id', '=', door.id) ])
            if len(res) == 1:
                if res.time_schedule_id != time_schedule:
                    res.time_schedule_id = time_schedule
            else:
                # if controller is of type iCON130 or iCON180
                if door.controller_id.hw_version in ['17', '10'] and time_schedule.number > 3:
                    raise exceptions.ValidationError(_('La porte %s ne peut utiliser que les 3 premiers horaires') %
                                                     door.name)
                rel_env.create([{
                    'access_group_id': self.id,
                    'door_id': door.id,
                    'time_schedule_id': time_schedule.id,
                }])
        self.check_for_ts_inconsistencies()

    def del_doors(self, door_ids):
        rel_env = self.env['hr.rfid.access.group.door.rel']
        for door in door_ids:
            rel_env.search([
                ('access_group_id', '=', self.id),
                ('door_id', '=', door.id)
            ]).unlink()

    @api.returns('hr.rfid.door')
    def get_all_doors(self):
        return self.mapped('all_door_ids').mapped('door_id')

    @api.returns('hr.employee')
    def get_all_employees(self):
        return self.mapped('all_employee_ids').mapped('employee_id')

    @api.returns('res.partner')
    def get_all_contacts(self):
        return self.mapped('all_contact_ids').mapped('contact_id')

    @api.constrains('door_ids')
    def door_ids_constrains(self):
        for acc_gr in self:
            door_id_list = []
            for rel in acc_gr.door_ids:
                if rel.door_id.id in door_id_list:
                    raise exceptions.ValidationError("Impossible de lier le groupe d'accès à une porte"
                                                      "il est déjà lié à.")

            relay_doors = dict()
            for rel in acc_gr.all_door_ids:
                ctrl = rel.door_id.controller_id
                if ctrl.is_relay_ctrl():
                    if ctrl in relay_doors and ctrl.mode == 3:
                        un_message = 'Les portes "%s" et "%s" appartiennent toutes deux à un contrôleur qui ne peut pas donner accès à plusieurs portes en même temps.'
                        raise exceptions.ValidationError(
                            _(un_message)
                            % (relay_doors[ctrl].name, rel.door_id.name)
                        )
                    relay_doors[ctrl] = rel.door_id

                door_id_list.append(rel.door_id.id)

    @api.depends('door_ids', 'inherited_ids')
    def _compute_all_doors(self):
        for acc_gr in self:
            door_ids = set()
            HrRfidAccessGroup._check_all_doors_rec(door_ids, [], acc_gr)
            acc_gr.all_door_ids = self.env['hr.rfid.access.group.door.rel'].browse(list(door_ids))

    @api.depends('employee_ids', 'inheritor_ids')
    def _compute_all_employees(self):
        for acc_gr in self:
            employee_ids = set()
            HrRfidAccessGroup._check_all_employees_rec(employee_ids, [], acc_gr)
            acc_gr.all_employee_ids = self.env['hr.rfid.access.group.employee.rel'].browse(list(employee_ids))

    @api.depends('contact_ids', 'inheritor_ids')
    def _compute_all_contacts(self):
        for acc_gr in self:
            contact_ids = set()
            HrRfidAccessGroup._check_all_contacts_rec(contact_ids, [], acc_gr)
            acc_gr.all_contact_ids = self.env['hr.rfid.access.group.contact.rel'].browse(list(contact_ids))

    @staticmethod
    def _check_all_doors_rec(door_ids: set, checked_ids: list, acc_gr):
        if acc_gr.id in checked_ids:
            return
        checked_ids.append(acc_gr.id)
        for door in acc_gr.door_ids:
            door_ids.add(door.id)
        for rec_gr in acc_gr.inherited_ids:
            HrRfidAccessGroup._check_all_doors_rec(door_ids, checked_ids, rec_gr)

    @staticmethod
    def _check_all_employees_rec(emp_ids: set, checked_ids: list, acc_gr):
        if acc_gr.id in checked_ids:
            return
        checked_ids.append(acc_gr.id)
        for employee in acc_gr.employee_ids:
            emp_ids.add(employee.id)
        for rec_gr in acc_gr.inheritor_ids:
            HrRfidAccessGroup._check_all_employees_rec(emp_ids, checked_ids, rec_gr)

    @staticmethod
    def _check_all_contacts_rec(contact_ids: set, checked_ids: list, acc_gr):
        if acc_gr.id in checked_ids:
            return
        checked_ids.append(acc_gr.id)
        for contact in acc_gr.contact_ids:
            contact_ids.add(contact.id)
        for rec_gr in acc_gr.inheritor_ids:
            HrRfidAccessGroup._check_all_contacts_rec(contact_ids, checked_ids, rec_gr)

    def check_for_ts_inconsistencies(self):
        def get_highest_acc_grs(_gr):
            if len(_gr.inheritor_ids) == 0:
                return _gr

            _hg = self.env['hr.rfid.access.group']
            for _acc_gr in _gr.inheritor_ids:
                if len(_acc_gr.inheritor_ids) == 0:
                    _hg += _acc_gr
                else:
                    _hg += get_highest_acc_grs(_acc_gr)
            return _hg

        def check_tses(_gr1, _gr2):
            _doors1 = _gr1.door_ids
            _doors2 = _gr2.door_ids
            _door_rel_env = self.env['hr.rfid.access.group.door.rel']
            _door_rel_env.check_for_ts_inconsistencies(_doors1, _doors2)

        def iterate_acc_grs(_gr, _checked_groups):
            for _gr2 in _gr.inherited_ids:
                if _gr2 in _checked_groups:
                    continue
                _checked_groups.append(_gr2)
                check_tses(_gr, _gr2)
                iterate_acc_grs(_gr2, _checked_groups)

        highest_groups = get_highest_acc_grs(self)
        for acc_gr in highest_groups:
            iterate_acc_grs(acc_gr, [])
        employees = self.all_employee_ids.mapped('employee_id')
        contacts = self.all_contact_ids.mapped('contact_id')
        employees.mapped(lambda r: r.check_for_ts_inconsistencies())
        contacts.mapped(lambda r: r.check_for_ts_inconsistencies())

    @api.constrains('inherited_ids')
    def _check_inherited_ids(self):
        env = self.env['hr.rfid.access.group']
        for acc_gr in self:
            group_order = []
            ret = HrRfidAccessGroup._check_inherited_ids_rec(acc_gr, [], group_order)
            if ret is True:
                err2 = ''
                for acc_gr_id in group_order:
                    acc_gr = env.browse(acc_gr_id)
                    err2 += '\n-> '
                    err2 += acc_gr.name
                err = _("Référence circulaire trouvée dans les groupes d'accès hérités : %s") % err2
                raise exceptions.ValidationError(err)

            acc_gr.check_for_ts_inconsistencies()

    @staticmethod
    def _check_inherited_ids_rec(acc_gr, visited_groups: list, group_order: list, orig_id=None):
        group_order.append(acc_gr.id)
        if acc_gr.id == orig_id:
            return True
        if orig_id is None:
            orig_id = acc_gr.id

        if acc_gr.id in visited_groups:
            return False

        visited_groups.append(acc_gr.id)

        for inh_gr in acc_gr.inherited_ids:
            res = HrRfidAccessGroup._check_inherited_ids_rec(inh_gr, visited_groups,
                                                                       group_order, orig_id)
            if res is True:
                return True

        group_order.pop()
        return False

    def _create_add_door_commands(self, doors):
        card_rel_env = self.env['hr.rfid.card.door.rel']
        employees = self.all_employee_ids.mapped('employee_id')
        contacts = self.all_contact_ids.mapped('contact_id')
        cards = employees.mapped('hr_rfid_card_ids') + contacts.mapped('hr_rfid_card_ids')
        for door in doors:
            for card in cards:
                card_rel_env.check_relevance_fast(card, door)

    def _create_remove_door_commands(self, doors):
        card_rel_env = self.env['hr.rfid.card.door.rel']
        employees = self.all_employee_ids.mapped('employee_id')
        contacts = self.all_contact_ids.mapped('contact_id')
        cards = employees.mapped('hr_rfid_card_ids') + contacts.mapped('hr_rfid_card_ids')
        for door in doors:
            for card in cards:
                card_rel_env.check_relevance_slow(card, door)

    def write(self, vals):
        for acc_gr in self:
            old_doors = self.all_door_ids.mapped('door_id')

            super(HrRfidAccessGroup, acc_gr).write(vals)

            new_doors = self.all_door_ids.mapped('door_id')

            added_doors = new_doors - old_doors
            removed_doors = old_doors - new_doors

            env = self.env['hr.rfid.access.group']
            completed_groups = [ ]
            acc_gr_to_complete = queue.Queue()
            acc_gr_to_complete.put(acc_gr.id)

            while not acc_gr_to_complete.empty():
                inh_id = acc_gr_to_complete.get()
                if inh_id in completed_groups:
                    continue
                completed_groups.append(inh_id)
                inh = env.browse(inh_id)
                HrRfidAccessGroup._create_add_door_commands(inh, added_doors)
                HrRfidAccessGroup._create_remove_door_commands(inh, removed_doors)

                for upper_inh in inh.inheritor_ids:
                    acc_gr_to_complete.put(upper_inh.id)

        return True

    def unlink(self):
        for acc_gr in self:
            acc_gr.door_ids.unlink()  # Unlinks the relations
            acc_gr.employee_ids.unlink()  # Unlinks the relations
            acc_gr.contact_ids.unlink()  # Unlinks the relations
        return super(HrRfidAccessGroup, self).unlink()

class HrRfidAccessGroupDoorRel(models.Model):
    _name = 'hr.rfid.access.group.door.rel'
    _description = 'Relation between access groups and doors'

    def _get_cur_access_group_id(self):
        return self.env['hr.rfid.access.group'].browse(self._context.get('active_id'))

    def _get_cur_door_id(self):
        return self.env.context.get('door_id', None)

    access_group_id = fields.Many2one(
        'hr.rfid.access.group',
        string="Groupe d'Accès",
        default=_get_cur_access_group_id,
        required=True,
    )

    door_id = fields.Many2one(
        'hr.rfid.door',
        string='Porte',
        default=_get_cur_door_id,
        required=True,
        ondelete='cascade',
    )

    time_schedule_id = fields.Many2one(
        'hr.rfid.time.schedule',
        string='Horaire',
        help="Horaire de la combinaison porte/groupe d'accès",
        default=lambda self: self.env.ref('hr_rfid.hr_rfid_time_schedule_0').id,
        required=True,
        ondelete='cascade',
    )

    @api.model
    def check_for_ts_inconsistencies(self, rels1, rels2):
        for door_rel in rels1:
            for door_rel2 in rels2[:]:
                if door_rel.door_id == door_rel2.door_id:
                    if door_rel.time_schedule_id != door_rel2.time_schedule_id:
                        un_message = "L'horaire ne correspond pas à la porte" + "'%s' dans les groupes d'accès '%s' et '%s'"
                        raise exceptions.ValidationError(
                            _(un_message)
                            % (door_rel.door_id.name, door_rel.access_group_id.name,
                               door_rel2.access_group_id.name))
                    rels2 -= door_rel2

    @api.model_create_multi
    @api.returns('self', lambda value: value.id)
    def create(self, vals):
        card_door_rel_env = self.env['hr.rfid.card.door.rel']

        records = super(HrRfidAccessGroupDoorRel, self).create(vals)

        records.mapped('access_group_id').door_ids_constrains()

        for rel in records:
            card_door_rel_env.update_door_rels(rel.door_id, rel.access_group_id)

        return records

    def write(self, vals):
        raise exceptions.ValidationError('Pas autorisé à écrire ici (hr.rfid.access.group.door.rel)')

    def unlink(self):
        card_door_rel_env = self.env['hr.rfid.card.door.rel']
        for rel in self:
            door = rel.door_id
            cards = door.get_potential_cards(access_groups=rel.access_group_id)
            super(HrRfidAccessGroupDoorRel, rel).unlink()
            for card, ts in cards:
                card_door_rel_env.check_relevance_slow(card, door)

###
# Possible to have 'hr.rfid.access.group.employee.rel' and '...contact.rel' inherit another model,
# but then we'll have an empty table in the database. Better to just copy-paste a few fields i guess.
# Maybe there is a model that does not create a database table?
###

class HrRfidAccessGroupEmployeeRel(models.Model):
    _name = 'hr.rfid.access.group.employee.rel'
    _description = 'Relation between access groups and employees'

    access_group_id = fields.Many2one(
        'hr.rfid.access.group',
        string="Groupe d'Accès",
        required=True,
    )

    employee_id = fields.Many2one(
        'hr.employee',
        string='Employé',
        required=True,
    )

    expiration = fields.Datetime(
        string='Date Expiration',
        help="Le groupe d'accès se retirera de l'employé"
              "à la date d'expiration. N'expirera jamais si vide.",
        index=True,
    )

    @api.model
    def _check_expirations(self):
        self.search([
            ('expiration', '<=', fields.Datetime.now())
        ]).unlink()

    @api.constrains('employee_id', 'access_group_id')
    def _check_for_duplicates(self):
        for rel in self:
            duplicates = self.search([
                ('access_group_id', '=', rel.access_group_id.id),
                ('employee_id', '=', rel.employee_id.id),
            ])
            if len(duplicates) > 1:
                raise exceptions.ValidationError(
                    _("Les employés (%s) ne peuvent pas avoir deux fois le même groupe d'accès (%s) !")
                    % (rel.employee_id.name, rel.access_group_id.name))

    @api.model
    @api.model_create_multi
    def create(self, vals_list):
        card_door_rel_env = self.env['hr.rfid.card.door.rel']

        records = super(HrRfidAccessGroupEmployeeRel, self).create(vals_list)
        records.mapped('employee_id').check_access_group()

        for rel in records:
            cards = rel.employee_id.hr_rfid_card_ids
            for card in cards:
                card_door_rel_env.update_card_rels(card, rel.access_group_id)

        return records

    def write(self, vals):
        if 'employee_id' in vals:
            raise exceptions.ValidationError("Impossible de changer l'employé de la relation !")
        card_door_rel_env = self.env['hr.rfid.card.door.rel']
        for rel in self:
            old_acc_gr = rel.access_group_id
            super(HrRfidAccessGroupEmployeeRel, rel).write(vals)
            new_acc_gr = rel.access_group_id

            rel.employee_id.check_access_group()

            if new_acc_gr != old_acc_gr:
                # Potentially remove old rels
                cards = rel.employee_id.hr_rfid_card_ids
                doors = old_acc_gr.all_door_ids.mapped('door_id')
                for card in cards:
                    for door in doors:
                        card_door_rel_env.check_relevance_slow(card, door)

                # Potentially create new rels
                cards = rel.employee_id.hr_rfid_card_ids
                for card in cards:
                    card_door_rel_env.update_card_rels(card, new_acc_gr)

    def unlink(self):
        card_door_rel_env = self.env['hr.rfid.card.door.rel']

        for rel in self:
            cards = rel.employee_id.hr_rfid_card_ids
            doors = rel.access_group_id.all_door_ids.mapped('door_id')
            super(HrRfidAccessGroupEmployeeRel, rel).unlink()
            for card in cards:
                for door in doors:
                    card_door_rel_env.check_relevance_slow(card, door)

class HrRfidAccessGroupContactRel(models.Model):
    _name = 'hr.rfid.access.group.contact.rel'
    _description = 'Relation between access groups and contacts'

    access_group_id = fields.Many2one(
        'hr.rfid.access.group',
        string="Groupe d'Accès",
        required=True,
    )

    contact_id = fields.Many2one(
        'res.partner',
        string='Contact',
        required=True,
    )

    expiration = fields.Datetime(
        string="Date Expiration",
        help="Le groupe d'accès se retirera du contact "
              "à la date d'expiration. N'expirera jamais si vide.",
        index=True,
    )

    @api.model
    def _check_expirations(self):
        self.search([
            ('expiration', '<=', fields.Datetime.now())
        ]).unlink()

    @api.constrains('contact_id', 'access_group_id')
    def _check_for_duplicates(self):
        for rel in self:
            duplicates = self.search([
                ('access_group_id', '=', rel.access_group_id.id),
                ('contact_id', '=', rel.contact_id.id),
            ])
            if len(duplicates) > 1:
                raise exceptions.ValidationError(
                    _("Les employés (%s) ne peuvent pas avoir le même groupe d'accès (%s) deux fois !")
                    % (rel.contact_id.name, rel.access_group_id.name))

    @api.model
    @api.model_create_multi
    def create(self, vals_list):
        card_door_rel_env = self.env['hr.rfid.card.door.rel']

        records = super(HrRfidAccessGroupContactRel, self).create(vals_list)
        records.mapped('contact_id').check_access_group()

        for rel in records:
            cards = rel.contact_id.hr_rfid_card_ids
            for card in cards:
                card_door_rel_env.update_card_rels(card, rel.access_group_id)

        return records

    def write(self, vals):
        if 'contact_id' in vals:
            raise exceptions.ValidationError("Impossible de changer l'employé de la relation !")
        card_door_rel_env = self.env['hr.rfid.card.door.rel']
        for rel in self:
            old_acc_gr = rel.access_group_id
            super(HrRfidAccessGroupContactRel, rel).write(vals)
            new_acc_gr = rel.access_group_id

            rel.contact_id.check_access_group()

            if new_acc_gr != old_acc_gr:
                # Potentially remove old rels
                cards = rel.contact_id.hr_rfid_card_ids
                doors = old_acc_gr.all_door_ids.mapped('door_id')
                for card in cards:
                    for door in doors:
                        card_door_rel_env.check_relevance_slow(card, door)

                # Potentially create new rels
                cards = rel.contact_id.hr_rfid_card_ids
                for card in cards:
                    card_door_rel_env.update_card_rels(card, new_acc_gr)

    def unlink(self):
        card_door_rel_env = self.env['hr.rfid.card.door.rel']

        for rel in self:
            cards = rel.contact_id.hr_rfid_card_ids
            doors = rel.access_group_id.all_door_ids.mapped('door_id')
            super(HrRfidAccessGroupContactRel, rel).unlink()
            for card in cards:
                for door in doors:
                    card_door_rel_env.check_relevance_slow(card, door)

class HrRfidAccessGroupWizard(models.TransientModel):
    _name = 'hr.rfid.access.group.wizard'
    _description = 'Add or remove doors to the access group'

    def _default_acc_gr(self):
        return self.env['hr.rfid.access.group'].browse(self._context.get('active_ids'))

    def _default_acc_gr_doors(self):
        acc_gr = self._default_acc_gr()
        doors = self.env['hr.rfid.door']
        for door_rel in acc_gr.door_ids:
            doors += door_rel.door_id
        return doors

    acc_gr_id = fields.Many2many(
        'hr.rfid.access.group',
        string="Groupe d'Accès",
        required=True,
        default=_default_acc_gr,
    )

    door_ids = fields.Many2many(
        'hr.rfid.door',
        'my_door_ids',
        'wiz',
        'door',
        string='Portes',
        help="Quelles portes ajouter au groupe d'accès",
        required=True,
    )

    acc_gr_doors = fields.Many2many(
        'hr.rfid.door',
        'custom_door_ids',
        'wiz',
        'door',
        string="Toutes les portes de groupe d'accès",
        default=_default_acc_gr_doors,
    )

    time_schedule_id = fields.Many2one(
        'hr.rfid.time.schedule',
        string='Horaire',
        help="Horaire de la combinaison porte/groupe d'accès",
        required=True,
        default=lambda self: self.env.ref('hr_rfid.hr_rfid_time_schedule_0').id,
    )

    def add_doors(self):
        self.ensure_one()
        self.acc_gr_id.add_doors(self.door_ids, self.time_schedule_id)

    def del_doors(self):
        self.ensure_one()
        self.acc_gr_id.del_doors(self.door_ids)


#!/usr/bin/env python3
#
# (c) 2019 Yoichi Tanibayashi
#
"""
IrRecv.py

"""
__author__ = 'Yoichi Tanibayashi'
__date__   = '2019'

import pigpio
import time
import queue
import threading
from MyLogger import get_logger

#####
class IrRecv:
    """
    赤外線信号の受信
    """
    GLITCH_USEC     = 250     # usec
    LEADER_MIN_USEC = 1000

    INTERVAL_MAX    = 999999  # usec

    WATCHDOG_MSEC   = INTERVAL_MAX / 1000 / 2    # msec
    WATCHDOG_CANCEL = 0

    VAL_ON          = 0
    VAL_OFF         = 1
    VAL_STR         = ['pulse', 'space', 'timeout']

    MSG_END         = ''

    def __init__(self, pin, glitch_usec=GLITCH_USEC, verbose=False,
                 debug=False):
        """
        Parameters
        ----------
        pin: int
        glitch_usec: int
        verbose: bool
        debug: bool
        """
        self._dbg = debug
        self._log = get_logger(__class__.__name__, self._dbg)
        self._log.debug('pin=%d, glitch_usec=%d', pin, glitch_usec)

        self.pin = pin
        self.tick = 0

        self.pi = pigpio.pi()
        self.pi.set_mode(self.pin, pigpio.INPUT)
        self.pi.set_glitch_filter(self.pin, self.GLITCH_USEC)

        self.receiving = False
        self.raw_data = []
        self.verbose = verbose

        self.msgq = queue.Queue()

    def set_watchdog(self, ms):
        """
        受信タイムアウトの設定

        Parameters
        ----------
        ms: int
          msec
        """
        self._log.debug('ms=%d', ms)
        self.pi.set_watchdog(self.pin, ms)

    def cb_func_recv(self, pin, val, tick):
        """
        信号受信用コールバック関数

        受信用GPIOピン``pin``が変化するか、タイムアウトすると呼び出される。

        変化を検知すると、メッセージキューに格納し、
        最小限の処理にとどめて、ほとんどの処理はサブスレッドに任せる。

        """
        self._log.debug('pin=%d, val=%d, tick=%d', pin, val, tick)

        if not self.receiving:
            self._log.debug('reciving=%s .. ignore', self.receiving)
            return

        self.msgq.put([pin, val, tick])

        if val == pigpio.TIMEOUT:
            # 受信終了処理
            self.set_watchdog(self.WATCHDOG_CANCEL)
            self.cb_recv.cancel()
            self.receiving = False

            self.msgq.put(self.MSG_END)

            self._log.debug('timeout!')
            return

        self.set_watchdog(self.WATCHDOG_MSEC)

    def worker(self):
        """
        サブスレッド

        メッセージキューから``msg``(GPIOピンの変化情報)を取出し、処理する。
        実際の処理は``proc_msg()``で行う。

        """
        self._log.debug('')

        while True:
            msg = self.msgq.get()
            self._log.debug('msg=%s', msg)
            if msg == self.MSG_END:
                break

            self.proc_msg(msg)

        self._log.debug('done')

    def proc_msg(self, msg):
        """
        GPIOの値の変化に応じて、
        赤外線信号のON/OFF時間を``self.raw_data``に記録する。

        self.raw_data: [[pulse1, space1], [pulse2, space2], ..]

        Parameters
        ----------
        msg: [pin, val, tick]
          GPIOピンの状態変化

        """
        self._log.debug('msg=%s', msg)

        if type(msg) != list:
            self._log.waring('invalid msg:%s .. ignored', msg)
            return
        if len(msg) != 3:
            self._log.waring('invalid msg:%s .. ignored', msg)
            return

        [pin, val, tick] = msg

        interval = tick - self.tick
        self._log.debug('interval=%d', interval)
        self.tick = tick

        if val == pigpio.TIMEOUT:
            self._log.debug('timeout!')
            if len(self.raw_data) > 0:
                if len(self.raw_data[-1]) == 1:
                    self.raw_data[-1].append(interval)
            self._log.debug('end')
            return

        if interval > self.INTERVAL_MAX:
            interval = self.INTERVAL_MAX
            self._log.debug('interval=%d', interval)

        if val == IrRecv.VAL_ON:
            # end of space
            if self.raw_data == []:
                self._log.debug('start raw_data')
                return
            else:
                self.raw_data[-1].append(interval)

        else:  # val == IrRecv.VAL_OFF
            # end of pulse
            if self.raw_data == [] and interval < self.LEADER_MIN_USEC:
                self.set_watchdog(self.WATCHDOG_CANCEL)
                self._log.debug('%d: leader is too short .. ignored',
                                   interval)
                return
            else:
                self.raw_data.append([interval])

        self._log.debug('raw_data=%s', self.raw_data)

    def recv(self):
        """
        赤外線信号の受信

        受信処理に必要なコールバックとサブスレッド``th_worker``を生成し、
        サブスレッドが終了するまで待つ。

        """
        self._log.debug('')

        self.raw_data  = []
        self.receiving = True

        self.th_worker = threading.Thread(target=self.worker, daemon=True)
        self.th_worker.start()

        self.cb_recv = self.pi.callback(self.pin, pigpio.EITHER_EDGE,
                                        self.cb_func_recv)

        if self.verbose:
            print('Ready')

        """
        while self.receiving:
            time.sleep(0.1)
        """
        # スレッドが終了するまで待つ
        self.th_worker.join()
        self.cb_recv.cancel()

        if self.verbose:
            print('Done')

        return self.raw_data

    def end(self):
        """
        終了処理

        コールバックをキャンセルし、
        ``worker``スレッドがaliveの場合は、終了メッセージを送り、終了を待つ。
        """
        self._log.debug('')
        self.cb_recv.cancel()
        self.pi.stop()

        if self.th_worker.is_alive():
            self.msgq.put(self.MSG_END)
            self._log.debug('join()')
            self.th_worker.join()

        self._log.debug('done')

    def raw2pulse_space(self, raw_data=None):
        """
        リスト形式のデータをテキストに変換。

        Parameters
        ----------
        raw_data: [[p1, s1], [p2, s2], ..]
          引数で与えられなかった場合は、最後に受信したデータを使用する。

        Returns
        -------
        pulse_space: str
          pulse p1
          space s1
          pulse p2
          space s2
          :
        """
        self._log.debug('row_data=%s', raw_data)

        if raw_data is None:
            raw_data = self.raw_data
            self._log.debug('raw_data=%s', raw_data)

        pulse_space = ''

        for (p, s) in raw_data:
            pulse_space += '%s %d\n' % (self.VAL_STR[0], p)
            pulse_space += '%s %d\n' % (self.VAL_STR[1], s)

        return pulse_space

    def print_pulse_space(self, raw_data=None):
        """
        ``raw_data``の内容を下記の書式でテキスト出力する。

        pulse p1
        space s1
        pulse p2
        space s2
        :

        Parameters
        ----------
        raw_data: list
          [[p1, s1], [p2, s2], .. ]

        """
        self._log.debug('raw_data=%s', raw_data)

        if raw_data is None:
            raw_data = self.raw_data
            self._log.debug('raw_data=%s', raw_data)

        print(self.raw2pulse_space(raw_data), end='')

#####
class App:
    def __init__(self, pin, debug=False):
        self._dbg = debug
        self._log = get_logger(__class__.__name__, self._dbg)
        self._log.debug('pin=%d', pin)

        self.r = IrRecv(pin, verbose=True, debug=self._dbg)

    def main(self):
        self._log.debug('')

        while True:
            print('# -')
            raw_data = self.r.recv()
            self.r.print_pulse_space(raw_data)
            print('# /')
            time.sleep(.5)

    def end(self):
        self._log.debug('')
        self.r.end()

#####
DEF_PIN = 27

import click
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])

@click.command(context_settings=CONTEXT_SETTINGS,
               help='IR signal receiver')
@click.argument('pin', type=int, default=DEF_PIN)
@click.option('--debug', '-d', 'debug', is_flag=True, default=False,
              help='debug flag')
def main(pin, debug):
    logger = get_logger(__name__, debug)
    logger.debug('pin: %d', pin)

    app = App(pin, debug=debug)
    try:
        app.main()
    finally:
        logger.debug('finally')
        app.end()

if __name__ == '__main__':
    main()


"""Citra URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from main import populate, views
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('populate/', populate.populate),
    path("", views.home, name="home"),
    path("home/", views.home, name="home"),
    path("threads/", views.threads_list, name="threads"),
    path('threads/<id_thread>', views.forumMessages_list),
    path("login/", views.login, name="login"),
    path("logout/", views.logout, name="logout"),
    path('product/<id_product>/promotionweekproduct', views.promotion_week_product, name="promotion_week_product"),
    path('product/<id_product>/promotionmonthproduct', views.promotion_month_product, name="promotion_month_product"),
    path("products/<id_product>", views.product_details, name="products"),
    path('shops/<id_shop>/promotionweekshop', views.promotion_week_shop, name="promotion_week_shop"),
    path('shops/<id_shop>/promotionmonthshop', views.promotion_month_shop, name="promotion_month_shop"),
    path('shops/', views.list_shop, name="shops"),
    path('shop/bookings/acceptbookings/', views.accept_booking),
    path('shops/<id_shop>', views.shop_details, name="shop"),
    path('shop/chat_new/<id_shop>', views.get_chat_new, name='newChat'),
    path('chats/', views.get_chats_list, name='chats'),
    path('shop/chat/<id_chat>', views.get_chat, name='chat'),
    path('shops/booking/', views.booking),
    path('error/', views.error),
    path('forbidden/', views.forbidden),
    path('booking/',views.booking),
    path('shops/<id_shop>/reviews/', views.review_list, name='reviews'),
    path('shops/<id_shop>/reviews/new/', views.review_form, name='review'),
    path('shop/bookings/',views.list_booking_owner,name="list_booking_owner"),
    path('user/bookings/',views.list_booking_user,name="list_booking_user"),
    path('about/',views.about,name="about"),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

handler404 = 'main.views.error_404'


from mpi4py import MPI
import numpy as np
import matplotlib.pyplot as plt

comm = MPI.COMM_WORLD
p = comm.Get_size()
r = comm.Get_rank()

xi = 0
xf = 1
dx = 0.01
nx = int(((xf - xi) / dx) + 1)
ti = 0
tf = 1000
dt = 0.1
cfl = 0.45 #should be less than or equal to .5 for convergence
nt = int(((tf - ti) / dt) + 1)
t = int(np.ceil(1.0*nx/p))

num = (min(nx,t*(r+1))) - (t*r+1) + 1
if r==0:
	# u = np.zeros([nt,nx])
	curr = np.zeros(num+1)
	curr[:] = 300
	next = curr
elif r==p-1:
	curr = np.zeros(num+1)
	curr[:] = 300
	curr[-1] = 500
	next = curr
else:
	curr = np.zeros(num+2)
	curr[:] = 300
	next = curr
comm.barrier()

for i in range(1,nt):
	next[1:-1] = curr[1:-1] + cfl*(curr[0:-2] - (2*curr[1:-1]) + curr[2:])
	if r>0 and r<(p-1):
		comm.send(next[1],dest=r-1)
		comm.send(next[-2],dest=r+1)
		next[0] = comm.recv(source=r-1)
		next[-1] = comm.recv(source=r+1)
	elif r==0:
		next[-1] = comm.recv(source=r+1)
		comm.send(next[-2],dest=r+1)
	else:
		next[0] = comm.recv(source=r-1)
		comm.send(next[1],dest=r-1)
	comm.barrier()
	curr = next
if r==0:
	T = curr[0:-1]
elif r==p-1:
	T = curr[1:]
else:
	T = curr[1:-1]
T_final = np.array(comm.gather(T, root=0))

if r==0:
	T_final_plot = T_final[0]
	for i in range(1,p):
		T_final_plot = np.append(T_final_plot,T_final[i])
	x = np.arange(xi,xf+dx,dx)
	plt.plot(x,T_final_plot)
	plt.show()


import os
from tensorflow.python.client import device_lib
import urllib.request
from tqdm import tqdm
import pathlib
from .latest import latest_models

# load latest paths
dirpath = os.getcwd()

def show_last_models():
    print(latest_models)

def get_mode():
    local_device_protos = device_lib.list_local_devices()
    for x in local_device_protos:
        if x.device_type == 'GPU':
            return "gpu"
    return "cpu"

device_mode = get_mode()

def ls():
    models_list = []
    for r, d, f in os.walk(os.path.join(os.path.dirname(os.path.realpath(__file__)), "./models")):
        for file in f:
            models_list.append(file)
    return models_list

def rm(model_name):
    models_list = []
    for r, d, f in os.walk(os.path.join(os.path.dirname(os.path.realpath(__file__)), "./models")):
        for file in f:
            if file == model_name:
                os.remove(os.path.join(r, file))
                return True
    return False

class DownloadProgressBar(tqdm):
    def update_to(self, b=1, bsize=1, tsize=None):
        if tsize is not None:
            self.total = tsize
        self.update(b * bsize - self.n)

def download_url(url, output_path):
    with DownloadProgressBar(unit='B', unit_scale=True,
                             miniters=1, desc=url.split('/')[-1]) as t:
        urllib.request.urlretrieve(url, filename=output_path, reporthook=t.update_to)

def download_latest_model(detector, model_name, ext="h5", mode=device_mode):
    info = latest_models[detector][model_name][ext]
    info["path"] = os.path.join(os.path.dirname(os.path.realpath(__file__)), "./models", detector, model_name,
                                os.path.basename(info[mode]))

    p = pathlib.Path(os.path.dirname(info["path"]))
    p.mkdir(parents=True, exist_ok=True)

    if not os.path.exists(info["path"]):
        # print("downloading model {} to {} ...".format(info[mode], info["path"]))
        download_url(info[mode], info['path'])

    return info


"""PCA analysis of mapping parameters.

Perform a PCA analysis of a fitted mapping.
By hovering over the data points, the corresponding spectra
show up.
"""
import gc
import os
import shutil

import numpy as np

from PIL import Image
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from matplotlib.colors import to_rgba, CSS4_COLORS, ListedColormap
from matplotlib.ticker import MaxNLocator
from matplotlib import rcdefaults, rcParams

from sklearn import preprocessing
from sklearn.decomposition import PCA
from sklearn.cluster import OPTICS, SpectralClustering

from spectrum_analysis import mapping as mp
from spectrum_analysis import data
from peaknames import peaknames

mapFolderList = [
    os.path.join('testdata', '1'),
    os.path.join('testdata', '2'),
    ]

# number of clusters (needed for SpectralClustering)
n_clusters = [
    4,
    3,
    ]

dims = [
        (4, 4),
        (8, 2)
        ]

stepsize = [
            10,
            10
            ]

# images need to be in folders specified in mapFolderList
# best is to use png backgrounds, but jpgs work as well
backgrounds = [
               'bg_test.png',
               'bg_test.jpg'
              ]

msizes = [
          1.04,
          1.04,
]

components = 3    # number of PCA components
component_x = 0   # component to plot on x axis
component_y = 1   # component to plot on x axis

show_hover_plot = True  # set True if interactive plot should be displayed
display_parameter_values = True    # show fitting values at hovering
print_PCA_results = True           # print PCA results to command line
print_PC_components = False        # print the principal components
plot_parameter_directions = True   # plot direction of parameters in PC space
plot_only_dirs = {'fwhm': {'linestyle': '-',
                           'plot_label': True},
                  'center': {'linestyle': '--',
                             'plot_label': True},
                  'height': {'linestyle': '-.',
                             'plot_label': True},
                  }
clustering = 'SpectralClustering'  # SpectralClustering (or OPTICS)

additional_fitplot_folder = 'testdata/2/results/plot'  # additional fit data
show_both_images = False                               # True to display both
                                                       # fits in hovering plot
shift_second_image = [0.8, 0]

numberOfSamples = 2   # minimal number of samples (needed for OPTICS)
brim = 0.25           # minimal brim around plotted data

imagesize = (150, 150)   # size of hovering image
imageshift = (100, -50)  # shift of hovering image

plot_clustered_fitlines = False  # plot summed raw data if False
histogramm_parameters = ['breit_wigner_p1_fwhm', 'breit_wigner_p1_center',
                         'lorentzian_p1_fwhm', 'lorentzian_p1_center']
bins = 10  # number of bins for histogrammed parameters

linebreaker = '============================================================'

def addPoint(scat, new_point, c='k'):
    """Add point to scatter plot."""
    old_off = scat.get_offsets()
    new_off = np.concatenate([old_off, np.array(new_point, ndmin=2)])
    old_c = scat.get_facecolors()
    new_c = np.concatenate([old_c, np.array(to_rgba(c), ndmin=2)])

    scat.set_offsets(new_off)
    scat.set_facecolors(new_c)

    scat.axes.autoscale_view()
    scat.axes.figure.canvas.draw_idle()

def scaleParameters(params):
    """Scale parameters to [0, 1]."""
    scaled_params = np.zeros_like(params)
    for idx, param in enumerate(params):
        min_max_scaler = preprocessing.MinMaxScaler()
        scaled_param = min_max_scaler.fit_transform(
            param.reshape(-1, 1))
        scaled_params[idx] = scaled_param.reshape(1, -1)
    return scaled_params

def createCluster(method, n_clust=3, min_samples=5):
    """Create cluster and plot the corresponding scatter plot."""
    if method == 'SpectralClustering':
        clust = SpectralClustering(n_clusters=n_clust)
        clust.fit(PC)
        scat = plt.scatter(-100, -100, zorder=2)
    elif method == 'OPTICS':
        clust = OPTICS(min_samples=min_samples)
        clust.fit(PC)
        scat = plt.scatter(PC[clust.labels_ == -1, 0],
                           PC[clust.labels_ == -1, 1], c='k')
    return clust, scat

def get_image(pltdir, label):
    """Get image."""
    img_name = f'{pltdir}{os.sep}fitplot_{label}.png'
    img = Image.open(img_name)
    img.thumbnail(imagesize, Image.ANTIALIAS)  # resize the image
    return img

def printPCAresults(pc_ana, param_list, print_components=False):
    """Print results of PCA analysis to command line."""
    print(f'explained variance ratio '
          f'({pc_ana.components_.shape[0]} components): '
          f'{sum(pc_ana.explained_variance_ratio_):2.2f} '
          f'({pc_ana.explained_variance_ratio_.round(2)})')
    if print_components:
        for j, principal_component in enumerate(pc_ana.components_):
            print(f'Principal component {j+1}')
            for idx, lbl in enumerate(param_list):
                print(f'{principal_component[idx]: 2.4f} * {lbl}')
            print()

def plotCluster(axes, clst_lbl, clst, rawspec, prnt=True):
    spectra = [clst_lbl == clst[1]]
    clusterspectra = [name for j, name in enumerate(rawspec)
                      if spectra[0][j]]
    clust_x, clust_y = data.GetAllData(clusterspectra)
    if prnt:
        print(f'Cluster {clst[1]}, containing {clst[0]} spectra.')
    axes.plot(clust_x[0], sum(clust_y)/len(clust_y),
              color=colors[clst[1]])
    # plot histogrammed fwhm and position of each cluster into plot
    axs_twin = axes.twinx()
    for param in histogramm_parameters:
        param_idx = parameterList.index(param)
        color = CSS4_COLORS[list(CSS4_COLORS)[param_idx+20]]
        hist_params = parameters[param_idx][spectra[0]]
        peakname = '_'.join(param.split('_')[:-1])
        parametername = param.split("_")[-1]
        label = peaknames[peakname][parametername]['name'].split(' ')[-1]
        axs_twin.hist(hist_params[hist_params != mapp.missingvalue],
                      label=label,
                      bins=bins, histtype='step', color=color)
        axs_twin.yaxis.tick_left()
        axs_twin.tick_params(axis='y', labelsize=7)

    axes.yaxis.set_major_locator(MaxNLocator(prune='both',
                                             nbins=3))
    axs_twin.yaxis.set_major_locator(MaxNLocator(prune='both',
                                                 nbins=3,
                                                 integer=True))
    axs_twin.set_ylabel('Spectra')
    axs_twin.yaxis.set_label_position('left')
    axs_twin.text(0.05, 0.85, f'C{clst[1]}, {clst[0]} S',
                  transform=axes.transAxes, fontsize=8,
                  bbox=dict(color='white', alpha=0.75, pad=3))
    __, y_max_val = axs_twin.get_ylim()
    if y_max_val < 1:
        axs_twin.yaxis.set_ticks([])
        axs_twin.set_ylabel('')

    return axs_twin, (clst[1], clst[0])

def selectSpecType(mappng, plt_clust=False):
    if plt_clust:
        spectra, __ = data.GetFolderContent(mappng.fitdir, 'dat',
                                            quiet=True)
    else:
        spectra, __ = data.GetFolderContent(mappng.folder, 'txt',
                                            quiet=True)
    return spectra

def plotClusterOverview(mapping, ax_main, ax_arr, rank_clust, clust_lbl,
                        plt_clust_fits=False):
    spectra = selectSpecType(mapping, plt_clust_fits)
    print('The clusters are')
    minimum = min(len(ax_arr), len(rank_clust))
    for i in range(0, minimum):
        ax_twin, __ = plotCluster(ax_arr[i], clust_lbl, rank_clust[i], spectra)
        if i == 1:
            hnd, lbl = ax_twin.get_legend_handles_labels()
            ax_main.legend(hnd, lbl, ncol=len(histogramm_parameters),
                           bbox_to_anchor=(0, 1.01),  # legend on top pca plot
                           loc='lower left', prop={'size': 6},
                           borderaxespad=0.)

if not os.path.exists(clustering):
    os.makedirs(clustering)

if not os.path.exists(f'{clustering}{os.sep}allclusters'):
    os.makedirs(f'{clustering}{os.sep}allclusters')
else:
    shutil.rmtree(f'{clustering}{os.sep}allclusters')
    os.makedirs(f'{clustering}{os.sep}allclusters')

if plot_parameter_directions:
    if not os.path.exists(os.path.join(clustering, 'directions')):
        os.makedirs(os.path.join(clustering, 'directions'))

print(linebreaker + '\n' + linebreaker)

for folder in mapFolderList:
    index = mapFolderList.index(folder)
    print(f'Mapping {index + 1} of {len(mapFolderList)}\n')
    print(f'{folder} mappings are plotted now.')
    mapdims = dims[index]
    step = stepsize[index]
    background = folder + os.sep + backgrounds[index]
    msize = msizes[index]

    mapp = mp.mapping(foldername=folder, plot=True, peaknames=peaknames)

    # get fit data
    peakFileList, numberOfPeakFiles = data.GetFolderContent(
        mapp.pardir_peak,
        filetype='dat',
        objects='peakparameter')
    parameters, errors = data.GetAllData(peakFileList)
    parameterList = mapp.CreatePeakList(peakFileList)

    # preprocessing data
    # https://scikit-learn.org/stable/modules/preprocessing.html#preprocessing
    # scale all data to [0,1]
    scaled_parameters = scaleParameters(parameters)

    # transpose data, so all parameters of one spectrum are in one array
    transposed = np.transpose(scaled_parameters)

    # perform PCA analysis and print results to commmand line
    pca = PCA(n_components=components)
    analyzed = pca.fit(transposed).transform(transposed)
    if print_PCA_results:
        printPCAresults(pca, parameterList,
                        print_components=print_PC_components)

    # plot everything and annotate each datapoint
    fig = plt.figure()
    ax_sum = [plt.subplot2grid((4, 3), (0, 2)),
              plt.subplot2grid((4, 3), (1, 2)),
              plt.subplot2grid((4, 3), (2, 2)),
              plt.subplot2grid((4, 3), (3, 2))]
    ax = plt.subplot2grid((4, 3), (0, 0), colspan=2, rowspan=4)

    x = analyzed[:, component_x]
    y = analyzed[:, component_y]
    PC = np.vstack((x, y)).transpose()
    cluster, sc = createCluster(clustering, n_clusters[index])

    # plot clustered data
    colors = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple',
              'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan']
    clustertypes = set(cluster.labels_)
    clustersizes = []
    for klass, color in zip(range(0, len(clustertypes)), colors):
        PC_k = PC[cluster.labels_ == klass]
        clustersizes.append(len(PC_k))
        for point in PC_k:
            addPoint(sc, point, color)

    # get four biggest clusters and plot the sum spectrum
    rankedcluster = sorted(zip(clustersizes, clustertypes), reverse=True)
    plotClusterOverview(mapp, ax, ax_sum, rankedcluster, cluster.labels_,
                        plt_clust_fits=plot_clustered_fitlines)

    # create labels and delete empty plots
    ax_copy = ax_sum.copy()
    for axs in ax_copy:
        if axs.lines:
            axs.tick_params(axis='y', labelsize=7)
            axs.tick_params(axis='x', labelsize=7)
            axs.yaxis.tick_right()
            axs.yaxis.set_label_position('right')
            axs.set_ylabel('Intensity')
        else:
            fig.delaxes(axs)
            ax_sum.remove(axs)
    for axs in ax_sum[0:-1]:
        axs.get_xaxis().set_ticks([])
    ax_sum[0].set_title('Cluster mean spectra')
    ax_sum[-1].set_xlabel('Wavenumber (cm$^{-1}$)')

    # set center, min and max of the plot
    xmin, xmax, ymin, ymax = [min(x)-brim, max(x)+brim,
                              min(y)-brim, max(y)+brim]
    xcenter, ycenter = [(xmax-abs(xmin))/2, (ymax-abs(ymin))/2]
    ax.set_xlim((xmin, xmax))
    ax.set_ylim((ymin, ymax))
    ax.axhline(0, linestyle='-', color=CSS4_COLORS['lightgrey'],
               zorder=1)
    ax.axvline(0, linestyle='-', color=CSS4_COLORS['lightgrey'],
               zorder=1)

    # add composing directions
    if plot_parameter_directions:
        pcx = pca.components_[component_x]
        pcy = pca.components_[component_y]
        plot_lines = []
        for i, parameter in enumerate(parameterList):
            peaknumber = int(parameter.split('_')[-2][-1])
            peaktype = parameter[0][0]
            param = parameter.split('_')[-1]
            if param in plot_only_dirs.keys():
                if peaktype == 'b':
                    color = 'b'
                elif peaktype == 'l':
                    color = 'r'
                elif peaktype == 'g':
                    color = 'g'
                elif peaktype == 'v':
                    color = 'c'
                color = (np.array(to_rgba(color))
                         - np.array((0, 0, 0, 0.3 * (peaknumber-1))))
                line = ax.plot((0, pcx[i]), (0, pcy[i]), color=color, zorder=3,
                               linestyle=plot_only_dirs[param]['linestyle'])
                plot_lines.append(line)
                if plot_only_dirs[param]['plot_label']:
                    ax.text(pcx[i]*1.15, pcy[i]*1.15,
                            f'{peaktype}{peaknumber}_{param[0:3]}',
                            ha='center', va='center', fontsize=7, zorder=4)

    # create annotation text
    annot = ax.annotate('', xy=(0, 0), xytext=(20, 20),
                        textcoords='offset points',
                        fontfamily='monospace',
                        bbox=dict(boxstyle='round', fc='w'),
                        arrowprops=dict(arrowstyle='->'))
    annot.set_visible(False)

    # create annotation image
    label = mapp.listOfFiles[0].split(os.sep)[-1].split('.')[0]
    image = get_image(mapp.pltdir, label)
    imagebox = OffsetImage(image)
    ab = AnnotationBbox(imagebox, (0, 0), xybox=imageshift,
                        boxcoords="offset points")
    if show_both_images:
        additional_image = get_image(additional_fitplot_folder, label)
        additional_imagebox = OffsetImage(additional_image)
        ac = AnnotationBbox(additional_imagebox, (0, 0), xybox=imageshift,
                            boxcoords="offset points")

    # https://stackoverflow.com/questions/7908636/possible-to-make-labels-appear-when-hovering-over-a-point-in-matplotlib
    def update_annot(ind):
        """Update annotation and image."""
        # update text annotation
        pos = sc.get_offsets()[ind["ind"][0]]
        annot.xy = pos
        idxlist = []
        for element in PC:
            idxlist.append(np.allclose(element, pos))
        idx = idxlist.index(True)
        annotation_string = f'{idx + 1}\n'
        if display_parameter_values:
            for i, label in enumerate(parameterList):
                annotation_string += (f'{parameters[i, idx]: 10.2f} '
                                      f'+/- {errors[i, idx]:8.2f} '
                                      f'({label})\n')
        annot.set_text(annotation_string[:-1])
        annot.get_bbox_patch().set_alpha(0.4)

        # update immage annotation
        label = mapp.listOfFiles[idx].split(os.sep)[-1].split('.')[0]
        image = get_image(mapp.pltdir, label)
        ab.xy = pos
        ab.offsetbox = OffsetImage(image)
        ax.add_artist(ab)
        if show_both_images:
            additional_image = get_image(additional_fitplot_folder, label)
            ac.xy = pos + shift_second_image
            ac.offsetbox = OffsetImage(additional_image)
            ax.add_artist(ac)

    def hover(event):
        """Hovering behavoir."""
        vis = annot.get_visible()
        if event.inaxes == ax:
            cont, ind = sc.contains(event)
            if cont:
                update_annot(ind)
                annot.set_visible(True)
                fig.canvas.draw_idle()
            else:
                if vis:
                    # annot.set_visible(False)
                    # ab = AnnotationBbox(None, (0,0))
                    fig.canvas.draw_idle()

    fig.canvas.mpl_connect("motion_notify_event", hover)

    figManager = plt.get_current_fig_manager()  # get current figure
    figManager.full_screen_toggle()             # show it maximized

    ax.set_xlabel(f'principal component {component_x + 1}')
    ax.set_ylabel(f'principal component {component_y + 1}')
    fig.tight_layout()
    fig.subplots_adjust(hspace=0.001)

    if plot_parameter_directions:
        plt.savefig(
            (f'{clustering}{os.sep}directions{os.sep}'
             f'{mapp.folder.replace(os.sep, "_")}'
             f'_pc{component_x}_pc{component_y}_dirs.png'),
            dpi=300)
        plt.savefig(f'{mapp.pltdir}{os.sep}pca_analysis'
                    f'_pc{component_x}_pc{component_y}_dirs.png', dpi=300)
        plt.savefig(f'{mapp.pltdir}{os.sep}pca_analysis'
                    f'_pc{component_x}_pc{component_y}_dirs.pdf')
    else:
        plt.savefig(
            (f'{clustering}{os.sep}{mapp.folder.replace(os.sep, "_")}'
             f'_pc{component_x}_pc{component_y}.png'),
            dpi=300)
        plt.savefig(f'{mapp.pltdir}{os.sep}pca_analysis'
                    f'_pc{component_x}_pc{component_y}.png', dpi=300)
        plt.savefig(f'{mapp.pltdir}{os.sep}pca_analysis'
                    f'_pc{component_x}_pc{component_y}.pdf')
    ax.set_title(f'PCA Analysis of {folder}')

    if show_hover_plot:
        plt.show()

    plt.close()

    for i, clust in enumerate(rankedcluster):
        fig, ax = plt.subplots()
        spectra = selectSpecType(mapp, plot_clustered_fitlines)
        ax_twin, (clst, spec) = plotCluster(ax, cluster.labels_, clust,
                                            spectra, prnt=False)
        ax.yaxis.tick_right()
        ax.yaxis.set_label_position('right')
        ax.set_xlabel('Wavenumber (cm$^{-1}$)')
        ax.set_ylabel('Intensity')
        ax_twin.yaxis.set_label_position('left')
        ax_twin.tick_params(axis='y', labelsize=12)
        ax_twin.legend(ncol=len(histogramm_parameters),
                       bbox_to_anchor=(0, 1.01), loc='lower left',
                       borderaxespad=0., prop={'size': 7})
        fig.tight_layout()
        plt.savefig(
            (f'{clustering}{os.sep}allclusters{os.sep}'
             f'{mapp.folder.replace(os.sep, "_")}'
             f'_pc{component_x}_pc{component_y}_S{spec:03}_C{clst}.png'),
            dpi=300)
        #plt.show()
        plt.close()

    cmap = ListedColormap(colors[0:max(cluster.labels_)+1])
    mapp.PlotMapping('params', cluster.labels_+2, mapdims, step, name='pca',
                     colormap=cmap)
    mapp.PlotMapping('params', cluster.labels_+2, mapdims, step,
                     name='grid_pca', alpha=0.75, grid=True,
                     background=background, msize=msize,
                     plot_missing=False, area=None, colormap=cmap)
    # set rcParams to default, as PlotMapping modifies it
    rcdefaults()

    print(linebreaker + '\n' + linebreaker)
    gc.collect()


# -*- coding: utf-8 -*-

import os
import mimetypes
from os import path

import requests
from docutils import nodes
from sphinx.builders.html import JSONHTMLBuilder
from sphinx.util import jsonimpl
from sphinx.config import Config
from deconstrst.config import Configuration

# Tell Sphinx about the deconst_default_layout key.
Config.config_values["deconst_default_layout"] = ("default", "html")

class DeconstSerialJSONBuilder(JSONHTMLBuilder):
    """
    Custom Sphinx builder that generates Deconst-compatible JSON documents.
    """

    implementation = jsonimpl
    name = 'deconst'
    out_suffix = '.json'

    def init(self):
        JSONHTMLBuilder.init(self)

        self.deconst_config = Configuration(os.environ)

        if os.path.exists("_deconst.json"):
            with open("_deconst.json", "r", encoding="utf-8") as cf:
                self.deconst_config.apply_file(cf)

        self.should_submit = not self.deconst_config.skip_submit_reasons()

    def finish(self):
        """
        We need to write images and static assets *first*.

        Also, the search indices and so on aren't necessary.
        """

    def dump_context(self, context, filename):

        """
        Override the default serialization code to save a derived metadata
        envelope, instead.
        """

        envelope = {
            "body": context["body"],
            "title": context["deconst_title"],
            "layout_key": context["deconst_layout_key"]
        }

        n = context.get("next")
        p = context.get("prev")

        if n:
            envelope["next"] = {
                "url": n["link"],
                "title": n["title"]
            }
        if p:
            envelope["previous"] = {
                "url": p["link"],
                "title": p["title"]
            }

        if context["display_toc"]:
            envelope["toc"] = context["toc"]

        super().dump_context(envelope, filename)

    def handle_page(self, pagename, ctx, *args, **kwargs):
        """
        Override the default serialization code to save a derived metadata
        envelope, instead.
        """

        meta = self.env.metadata[pagename]
        ctx["deconst_layout_key"] = meta.get(
            "deconstlayout", self.config.deconst_default_layout)
        ctx["deconst_title"] = meta.get("deconsttitle", ctx["title"])

        super().handle_page(pagename, ctx, *args, **kwargs)

    def post_process_images(self, doctree):
        """
        Publish images to the content store. Modify the image reference with
        the
        """

        JSONHTMLBuilder.post_process_images(self, doctree)

        if self.should_submit:
            for node in doctree.traverse(nodes.image):
                node['uri'] = self._publish_entry(node['uri'])

    def _publish_entry(self, srcfile):
        (content_type, _) = mimetypes.guess_type(srcfile)

        auth = 'deconst apikey="{}"'.format(
            self.deconst_config.content_store_apikey)
        headers = {"Authorization": auth}

        url = self.deconst_config.content_store_url + "assets"
        basename = path.basename(srcfile)
        if content_type:
            payload = (basename, open(srcfile, 'rb'), content_type)
        else:
            payload = open(srcfile, 'rb')
        files = {basename: payload}

        response = requests.post(url, files=files, headers=headers)
        response.raise_for_status()
        return response.json()[basename]


# app.py

from flask import Flask
from flask import request, render_template, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from config import BaseConfig

app = Flask(__name__)
app.config.from_object(BaseConfig)
db = SQLAlchemy(app)

from models import *

# Query for id of a specific data row
def get_id_by_name(model, **kwargs):
    result = model.query.filter_by(**kwargs).first().id
    return result

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        device_name       = request.form['device_name']
        device_address    = request.form['device_address']
        device_type_id    = get_id_by_name(DeviceTypes, device_type=request.form['device_type'])
        connect_method_id = get_id_by_name(ConnectMethods, connect_method=request.form['connect_method'])
        device = Inventory(device_name, device_address, device_type_id, connect_method_id)
        db.session.add(device)
        db.session.commit()
        return redirect(url_for('index')) # redirect to prevent POSTDATA error on refresh

    devices = Inventory.query.order_by(Inventory.name).all()
    device_types = DeviceTypes.query.order_by(DeviceTypes.device_type.desc()).all()
    connect_methods = ConnectMethods.query.order_by(ConnectMethods.connect_method.desc()).all()
    return render_template('index.html', inventory=devices, device_types=device_types,
                           connect_methods=connect_methods)

if __name__ == '__main__':
    app.run()


import datetime

weekday_counter = [0] * 7   # for answer

while True: # loop until "BREAK"
    input_str = input()
    if input_str == "BREAK":    # End of input, output the answer
        for weekday in range(7):
            print(str(weekday + 1) + " " + str(weekday_counter[weekday]))
        break
    birthday = input_str.split('/') # split by '/'
    if len(birthday) != 3:  # if true, means input is not "x/x/x"
        print("DATA_ERROR")
        break
    try:    # check for number. Year, month, day should all be number
        year = float(birthday[0])
        month = float(birthday[1])
        day = float(birthday[2])
    except:
        print("DATA_ERROR")
        break

    # convert to integer
    year_int = int(year)
    month_int = int(month)
    day_int = int(day)
    if year != year_int or month != month_int or day != day_int: # if not equal, there is a fraction part, which is invalid.
        print("DATA_ERROR")
        break
    try:
        birthday_datetime = datetime.datetime(year_int, month_int, day_int) # check for valid date. Days in a month, leap year, etc.
    except:
        print("DATA_ERROR")
        break

    weekday_counter[birthday_datetime.weekday()] += 1   # increase the corresponding weekday count


import os
from datetime import datetime

import config as config
import tensorflow as tf

from custom_callbacks import final_model_logger as fml

def define_model_callbacks():

    cwd = os.getcwd()

    exps_dir = os.path.join(cwd, 'classification_experiments')
    if not os.path.exists(exps_dir):
        os.makedirs(exps_dir)

    now = datetime.now().strftime('%b%d_%H-%M-%S')

    model_name = 'CNN'

    exp_dir = os.path.join(exps_dir, model_name + '_' + str(now))
    if not os.path.exists(exp_dir):
        os.makedirs(exp_dir)

    callbacks = []

    # Model checkpoint
    # ----------------
    ckpt_dir = os.path.join(exp_dir, 'ckpts')
    if not os.path.exists(ckpt_dir):
        os.makedirs(ckpt_dir)

    ckpt_callback = tf.keras.callbacks.ModelCheckpoint(filepath=os.path.join(ckpt_dir, 'cp_{epoch:02d}.ckpt'),
                                                       save_weights_only=True)  # False to save the model directly
    callbacks.append(ckpt_callback)

    # Visualize Learning on Tensorboard
    # ---------------------------------
    tb_dir = os.path.join(exp_dir, 'tb_logs')
    if not os.path.exists(tb_dir):
        os.makedirs(tb_dir)

    # By default shows losses and metrics for both training&validation&validation and validation
    tb_callback = tf.keras.callbacks.TensorBoard(log_dir=tb_dir,
                                                 profile_batch=0,
                                                 histogram_freq=1)  # if 1 shows weights histograms
    callbacks.append(tb_callback)

    # Early Stopping
    # --------------
    early_stop = config.cnn_early_stop

    if early_stop:
        es_callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True,
                                                       verbose=1)
        callbacks.append(es_callback)

    fml_callback = fml.FinalModelLogger(monitor='val_loss')

    callbacks.append(fml_callback)

    return callbacks


import fenics as fc
from scipy.interpolate import interp1d
import numpy as np

'''
Function: dataToHDF5
Argument list: fileName,distanceData, thicknessPathData, bedPathData, surfacePathData, smbPathData, velocityPathData,
resolution(ideal resolution is 1000 for our dataset)
Purpose: Write our data that is pre determined to an HDF5 File
Return types, values:
Dependencies: interp1d from scipy, fenics package
Creator: James Stauder
Date created:2/23/18
Last edited: 2/23/18
'''

def dataToHDF5(fileName,distanceData, thicknessPathData, bedPathData, surfacePathData, smbPathData, velocityPathData, resolution=1000):
    '''
    Create interps for each of our variables
    '''
    thickness1dInterp = interp1d(distanceData, thicknessPathData)
    bed1dInterp = interp1d(distanceData, bedPathData)
    surface1dInterp = interp1d(distanceData, surfacePathData)
    smb1dInterp = interp1d(distanceData, smbPathData)
    velocity1dInterp = interp1d(distanceData, velocityPathData)

    numberOfPoints = int(np.floor(distanceData[-1] / float(resolution)))

    x = np.arange(0, (numberOfPoints + 1) * resolution, resolution)

    mesh = fc.IntervalMesh(numberOfPoints, 0, resolution * numberOfPoints)

    thicknessModelData = thickness1dInterp(x)
    bedModelData = bed1dInterp(x)
    surfaceModelData = surface1dInterp(x)
    smbModelData = smb1dInterp(x)
    velocityModelData = velocity1dInterp(x)

    THICKLIMIT = 10  # Minimum thickness of the ice

    H = surfaceModelData - bedModelData

    surfaceModelData[H <= THICKLIMIT] = bedModelData[H <= THICKLIMIT]

    hdfName = fileName
    hfile = fc.HDF5File(mesh.mpi_comm(), hdfName, "w")
    V = fc.FunctionSpace(mesh, "CG", 1)

    functThickness = fc.Function(V, name="Thickness")
    functBed = fc.Function(V, name="Bed")
    functSurface = fc.Function(V, name="Surface")
    functSMB = fc.Function(V, name="SMB")
    functVelocity = fc.Function(V, name="Velocity")

    functThickness.vector()[:] = thicknessModelData
    functBed.vector()[:] = bedModelData
    functSurface.vector()[:] = surfaceModelData
    functSMB.vector()[:] = smbModelData
    functVelocity.vector()[:] = velocityModelData

    hfile.write(functThickness.vector(), "/thickness")
    hfile.write(functBed.vector(), "/bed")
    hfile.write(functSurface.vector(), "/surface")
    hfile.write(functSMB.vector(), "/smb")
    hfile.write(functVelocity.vector(), "/velocity")
    hfile.write(mesh, "/mesh")
    hfile.close()


import cv2, numpy as np

# # # # # # The purpose of this script is to make it easy to determine what HSV values are
# appropriate in trying to locate markers in a MoCap video file. Can use images
# or video.

# be sure to change this to coincide with OS
vid = '/home/godfreap/Desktop/Scripts/LeftLower_Trimmed.mp4'
cap = cv2.VideoCapture(vid)
# uncomment following line to debug with webcam
# cap = cv2.VideoCapture(0)

cv2.namedWindow('HSVColorBars')
cv2.namedWindow('trackbars')

# single image debug
# img = '/home/godfreap/Desktop/Scripts/LeftLower_Cropped.png'

def emptyCallback():
	pass

# get video properties
totalFrames = cap.get(7)
fps = cap.get(5)

# set up sliders
hh = 'H-Hue'
hl = 'L-Hue'
sh = 'H-Sat'
sl = 'L-Sat'
vh = 'H-Val'
vl = 'L-Val'
wnd = 'HSVColorBars'
trk = 'trackbars'
cv2.createTrackbar(hl, trk, 0, 179, emptyCallback)
cv2.createTrackbar(hh, trk, 0, 179, emptyCallback)
cv2.createTrackbar(sl, trk, 0, 255, emptyCallback)
cv2.createTrackbar(sh, trk, 0, 255, emptyCallback)
cv2.createTrackbar(vl, trk, 0, 255, emptyCallback)
cv2.createTrackbar(vh, trk, 0, 255, emptyCallback)

# set trackbar defaults for 'high' to max so image doesn't start blacked out
cv2.setTrackbarPos(hh,trk, 179)
cv2.setTrackbarPos(sh, trk, 255)
cv2.setTrackbarPos(vh, trk, 255)

while(1):
	# read the video, reset if last frame
	curFrame = cap.get(1)
	if curFrame == totalFrames:
		cap.set(1, 0)
	ret, frame = cap.read()

	# test with an image first, see if htat works
	# frame = cv2.imread(img)

	frame = cv2.GaussianBlur(frame, (5, 5), 0)
	hsvimg = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
	# get values from the trackbar
	hul = cv2.getTrackbarPos(hl, trk)
	huh = cv2.getTrackbarPos(hh, trk)
	sal = cv2.getTrackbarPos(sl, trk)
	sah = cv2.getTrackbarPos(sh, trk)
	val = cv2.getTrackbarPos(vl, trk)
	vah = cv2.getTrackbarPos(vh, trk)

	hsvlow = np.array([hul, sal, val])
	hsvhigh = np.array([huh, sah, vah])

	mask = cv2.inRange(hsvimg, hsvlow, hsvhigh)
	res = cv2.bitwise_and(hsvimg, hsvimg, mask = mask)

	cv2.imshow(wnd, res)
	k = cv2.waitKey(5) & 0xFF
	if k == ord('q'):
		break

cv2.destroyAllWindows()


from openpyxl import load_workbook
import numpy as np

def myPrint(name,var):
    try:
        print(name+":"+var)
    except :
        print(name+":"+str(var))

def intergrate(arr,padding):
    ret = []
    for x in range(padding,arr.shape[0]-padding):
        temp = []
        temp = np.array(temp)
        for y in range(x-padding,x+padding+1):
            temp = np.append(temp,arr[y])
        ret.append(temp)
    return np.array(ret)

def readXlsx(path):
    workbook = load_workbook(path, data_only=True)    #找到需要xlsx文件的位置
    booksheet = workbook.active                 #获取当前活跃的sheet,默认是第一个sheet

    #如果想获取别的sheet页采取下面这种方式，先获取所有sheet页名，在通过指定那一页。
    # sheets = workbook.get_sheet_names()  # 从名称获取sheet
    # booksheet = workbook.get_sheet_by_name(sheets[0])

    #获取sheet页的行数据
    rows = booksheet.rows
    #获取sheet页的列数据
    # columns = booksheet.columns
    # myPrint("rows",str(rows))
    # myPrint('col',str(columns))
    i = 0
    # 迭代所有的行
    retdata = []
    for row in rows:
        i = i + 1
        line = [col.value for col in row]
        retdata.append(line)
        # cell_data_1 = booksheet.cell(row=i, column=3).value               #获取第i行1 列的数据
        # cell_data_2 = booksheet.cell(row=i, column=4).value               #获取第i行 2 列的数据
        # cell_data_3 = booksheet.cell(row=i, column=8).value                   #获取第i行 3 列的数据
        # cell_data_4 = booksheet.cell(row=i, column=18).value                   #获取第i行 4 列的数据
        # print (cell_data_1, cell_data_2, cell_data_3, cell_data_4)
    return retdata

def isset(v):
    try:
        type (eval(v))
    except:
        return 0
    else:
        return 1

if __name__ == '__main__':
    arr = [[0,0,0],[1,1,1],[2,2,2],[3,3,3],[4,4,4]]
    arr = np.array(arr)
    x  = intergrate(arr,1)
    print(x)


"""Administration configuration for the events application."""

from django.contrib import admin
from django.db import models
from events.models import (
    Event,
    ThirdPartyEvent,
    Location,
    Session,
    Series,
    Sponsor,
    Resource,
    LocationImage,
    EventImage,
)
from django_google_maps import widgets as map_widgets
from django_google_maps import fields as map_fields
from tinymce.widgets import TinyMCE

class SessionAdmin(admin.ModelAdmin):
    """Admin interface for Session model."""

    exclude = ("slug",)
    formfield_overrides = {
        models.TextField: {'widget': TinyMCE()},
    }
    list_display = ("name", "event")
    search_fields = ["name", "event"]
    list_filter = ("event",)
    filter_vertical = ("resources", "locations",)

class SessionInline(admin.StackedInline):
    """Admin interface for Session model when displayed inline."""

    model = Session
    extra = 5
    exclude = ("slug",)
    formfield_overrides = {
        models.TextField: {'widget': TinyMCE()},
    }
    filter_vertical = ("resources", "locations",)

class LocationAdmin(admin.ModelAdmin):
    """Admin interface for Location model."""

    exclude = ("slug",)
    formfield_overrides = {
        map_fields.AddressField: {"widget": map_widgets.GoogleMapsAddressWidget},
        models.TextField: {'widget': TinyMCE()},
    }

class ResourceAdmin(admin.ModelAdmin):
    """Admin interface for Resource model."""

    exclude = ("slug",)
    formfield_overrides = {
        models.TextField: {'widget': TinyMCE()},
    }
    list_display = ("name", "url")

class EventAdmin(admin.ModelAdmin):
    """Admin interface for Event model."""

    fieldsets = [
        (
            None,
            {
                "fields": ["name", "description", "start_date", "end_date", "series", "location", "sponsors"]
            }
        ),
        (
            "Visibility",
            {
                "fields": ["is_published"]
            }
        ),
        (
            "Advanced",
            {
                "fields": ["slug"],
                "classes": ("grp-collapse", "grp-closed")
            }
        ),
    ]
    inlines = [SessionInline]
    formfield_overrides = {
        models.TextField: {'widget': TinyMCE()},
    }
    list_display = ("name", "series", "location", "start_date", "end_date")
    list_filter = ("is_published",)
    search_fields = ["name"]
    filter_vertical = ("sponsors",)

class ThirdPartyEventAdmin(admin.ModelAdmin):
    """Admin interface for ThirdPartyEvent model."""

    fieldsets = [
        (
            None,
            {
                "fields": [
                    "name",
                    "url",
                    "start_date",
                    "end_date",
                    "description",
                    "location",
                ]
            }
        ),
        (
            "Visibility",
            {
                "fields": ["is_published"]
            }
        ),
    ]
    formfield_overrides = {
        models.TextField: {'widget': TinyMCE()},
    }
    list_display = ("name", "location", "start_date", "end_date")
    list_filter = ("is_published",)
    search_fields = ["name"]

admin.site.register(Event, EventAdmin)
admin.site.register(ThirdPartyEvent, ThirdPartyEventAdmin)
admin.site.register(Session, SessionAdmin)
admin.site.register(Location, LocationAdmin)
admin.site.register(Resource, ResourceAdmin)
admin.site.register(Series)
admin.site.register(Sponsor)
admin.site.register(LocationImage)
admin.site.register(EventImage)


import os
import sys
import numpy as np
import argparse
import h5py
import librosa
from scipy import signal
import matplotlib.pyplot as plt
import time
import math
import pandas as pd
import random
import config

import soundfile

mini_data=True
labels=["Acoustic_guitar", "Applause", "Bark", "Bass_drum", "Burping_or_eructation", "Bus", "Cello", "Chime", "Clarinet", "Computer_keyboard", "Cough", "Cowbell", "Double_bass", "Drawer_open_or_close", "Electric_piano", "Fart", "Finger_snapping", "Fireworks", "Flute", "Glockenspiel", "Gong", "Gunshot_or_gunfire", "Harmonica", "Hi-hat", "Keys_jangling", "Knock", "Laughter", "Meow", "Microwave_oven", "Oboe", "Saxophone", "Scissors", "Shatter", "Snare_drum", "Squeak", "Tambourine", "Tearing", "Telephone", "Trumpet", "Violin_or_fiddle", "Writing"]
def create_folder(fd):
    if not os.path.exists(fd):
        os.makedirs(fd)
metadata_path = "/home/liuzhuangzhuang/Free sound dataset/FSDKaggle2018.meta/train_post_competition.csv"
feature_path = "/home/liuzhuangzhuang/pycharm_P/task5-多任务0.0/work_space/features/logmel_64frames_64melbins/pre_train.h5"
audio_path="/home/hxf/dcase2018Task2Data/freesound-audio-tagging/audio_train/"
create_folder(os.path.dirname(feature_path))

class LogMelExtractor(object):
    def __init__(self, sample_rate, window_size, hop_size, mel_bins, fmin, fmax):
        '''Log mel feature extractor.

        Args:
          sample_rate: int
          window_size: int
          hop_size: int
          mel_bins: int
          fmin: int, minimum frequency of mel filter banks
          fmax: int, maximum frequency of mel filter banks
        '''

        self.window_size = window_size
        self.hop_size = hop_size
        self.window_func = np.hanning(window_size)

        self.melW = librosa.filters.mel(
            sr=sample_rate,
            n_fft=window_size,
            n_mels=mel_bins,
            fmin=fmin,
            fmax=fmax).T
        '''(n_fft // 2 + 1, mel_bins)'''

    def transform(self, audio):
        '''Extract feature of a singlechannel audio file.

        Args:
          audio: (samples,)

        Returns:
          feature: (frames_num, freq_bins)
        '''

        window_size = self.window_size
        hop_size = self.hop_size
        window_func = self.window_func

        # Compute short-time Fourier transform                     经过STFT
        stft_matrix = librosa.core.stft(
            y=audio,
            n_fft=window_size,
            hop_length=hop_size,
            window=window_func,
            center=True,
            dtype=np.complex64,
            pad_mode='reflect').T
        '''(N, n_fft // 2 + 1)'''

        # Mel spectrogram#np.dot 矩阵乘积
        mel_spectrogram = np.dot(np.abs(stft_matrix) ** 2, self.melW)  # 将STFT平方点乘mel谱

        # Log mel spectrogram
        logmel_spectrogram = librosa.core.power_to_db(
            mel_spectrogram, ref=1.0, amin=1e-10,
            top_db=None)

        logmel_spectrogram = logmel_spectrogram.astype(np.float32)

        return logmel_spectrogram
# 计算特征
def calculate_feature_for_all_audio_files(mini_data):
    '''Calculate feature of audio files and write out features to a single hdf5
    file.

    Args:
      dataset_dir: string
      workspace: string
      data_type: 'train' | 'validate' | 'evaluate'
      mini_data: bool, set True for debugging on a small part of data
    '''

    # Arguments & parameters
    sample_rate = config.sample_rate
    window_size = config.window_size
    hop_size = config.hop_size
    mel_bins = config.mel_bins
    fmin = config.fmin
    fmax = config.fmax
    frames_per_second = config.frames_per_second
    frames_num = config.frames_num
    total_samples = config.total_samples

    # Paths
    if mini_data:
        prefix = 'minidata_'
    else:
        prefix = ''

    metadata_path = '/home/liuzhuangzhuang/Free sound dataset/FSDKaggle2018.meta/train_1.csv'

    audios_dir ="/home/hxf/dcase2018Task2Data/freesound-audio-tagging/audio_train/"

    feature_path = "/home/liuzhuangzhuang/pycharm_P/task5-多任务0.0/work_space/features/logmel_64frames_64melbins/pre_train.h5"

    # Feature extractor
    feature_extractor = LogMelExtractor(
        sample_rate=sample_rate,
        window_size=window_size,
        hop_size=hop_size,
        mel_bins=mel_bins,
        fmin=fmin,
        fmax=fmax)

    # Read metadata
    print('Extracting features of all audio files ...')
    extract_time = time.time()

    df = pd.read_csv(metadata_path, sep=',')
    audio_names = np.array(sorted(list(set(df['fname']))))
    label_targets = []
    if mini_data:
        random_state = np.random.RandomState(1234)
        random_state.shuffle(audio_names)
        audio_names = audio_names[0: 10]
    for audio_name in audio_names:
        label_target = np.zeros(len(labels))
        df_label = df[df['fname'] == audio_name]
        for i, label in enumerate(labels):
            df_ = df_label[df_label['label'] == label]  # 名字和标签对应则不为空
            if not df_.empty:
                label_target[i] = 1

        label_targets.append(label_target)

    label_targets = np.array(label_targets)
    meta_dict = {
        'audio_name': audio_names,
        'label_targetst': label_targets}

    # Hdf5 containing features and targets
    hf = h5py.File(feature_path, 'w')

    hf.create_dataset(
        name='audio_name',
        data=[audio_name.encode() for audio_name in meta_dict['audio_name']],
        dtype='S32')

    if 'label_targetst' in meta_dict.keys():
        hf.create_dataset(
            name='label_targetst',
            data=meta_dict['label_targetst'],
            dtype=np.float32)

    hf.create_dataset(
        name='feature',
        shape=(0, frames_num, mel_bins),
        maxshape=(None, frames_num, mel_bins),
        dtype=np.float32)

    for (n, audio_name) in enumerate(meta_dict['audio_name']):
        audio_path = os.path.join(audios_dir, audio_name)
        print(n, audio_path)

        # Read audio
        (audio, fs) = soundfile.read(audio_path)
        if audio.ndim > 1:
            audio = np.mean(audio, axis=1)#一维
        if sample_rate is not None and fs != sample_rate:
            audio = librosa.resample(audio, orig_sr=fs, target_sr=sample_rate)#重新采样

        # Pad or truncate audio recording
        if len(audio) < total_samples:
            audio = np.concatenate((audio, np.zeros(total_samples - len(audio))))
        else:
            audio =audio[0: total_samples]

        # Extract feature
        feature = feature_extractor.transform(audio)

        # Remove the extra frames caused by padding zero
        feature = feature[0: frames_num]

        hf['feature'].resize((n + 1, frames_num, mel_bins))
        hf['feature'][n] = feature

    hf.close()

    print('Write hdf5 file to {} using {:.3f} s'.format(
        feature_path, time.time() - extract_time))

if __name__ == '__main__':
    calculate_feature_for_all_audio_files(False)

.github/多任务/utils/Generate external data characteristics.py


import pyowm
import discord
from discord.ext import commands as eolas
from functools import partial

owm_key = ('YOUR_OWM_API_KEY')

class Weather:
    def __init__(self, eolas):
        self.eolas = eolas

        # ?meteo <location> - Print the forecast of a specific location.
        @eolas.command()
        async def meteo(*, name):
            """<location> - Print the forecast of a specific location."""
            owm = pyowm.OWM(owm_key, language='fr')

            observation = owm.weather_at_place(name)
            weather = observation.get_weather()
            location = observation.get_location()
            get_temperature = weather.get_temperature(unit='celsius')
            get_wind = weather.get_wind()

            await eolas.say('Lieu: {}'.format(location.get_name())
                            + '\n'
                            + ('Température: {}'.format(get_temperature['temp'])
                               + u'\N{DEGREE SIGN}C')
                            + '\n'
                            + ('Vitesse du vent: {}'.format(get_wind['speed']) + ' m/s')
                            + '\n'
                            + ('Description: {}'.format(weather.get_detailed_status()))
                            )

def setup(eolas):
    eolas.add_cog(Weather(eolas))


import unittest
import json
from grano.test.util import make_app, teardown_app
from grano.model import User

class UsersTestCase(unittest.TestCase):

    def setUp(self):
        self.app = make_app()

    def tearDown(self):
        teardown_app(self.app)

    def test_user_from_browserid(self):
        user = User.from_browserid({'email': 'fake@something.com', 'issuer': 'pudo.org'})
        assert user is not None, user
        assert User.objects.count()==1, User.objects.all()

    def test_user_session(self):
        user = User.from_browserid({'email': 'fake@something.com', 'issuer': 'pudo.org'})

        data = json.loads(self.app.get('/api/1/sessions').get_data())
        assert not data.get('logged_in'), data

        with self.app.session_transaction() as sess:
            sess['user_id'] = user.email
            sess['_fresh'] = True

        data = json.loads(self.app.get('/api/1/sessions').get_data())
        assert data.get('logged_in'), data
        assert data.get('user').get('email')==user.email


import re
import pickle
import time
import datetime
import threading
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
from kivy.config import Config
from kivy.uix.listview import ListItemButton
from kivy.properties import ObjectProperty, ListProperty
from kivy.core.clipboard import Clipboard
from kivy.clock import Clock
from functools import partial
from pyautogui import typewrite, press

Config.set("graphics", "width", "600")
Config.set("graphics", "height", "360")

class CtrlButton(ListItemButton):
    pass

class MyWidget(BoxLayout):
    carousel = ObjectProperty()
    ctrlc = ObjectProperty()
    tlacitka = ObjectProperty()
    last = ""
    event1 = None
    event2 = None
    ob = datetime.datetime(2018, 1, 1, 0, 0, 0)
    pr = datetime.datetime(2018, 1, 1, 0, 0, 0)
    pa = datetime.datetime(2018, 1, 1, 0, 0, 0)

    def __init__(self, **kwargs):
        super(MyWidget, self).__init__(**kwargs)

    def clipboard_cleaner(self):
        self.ctrlc.cb_list.adapter.data = []

    def clipboard_starter(self, button):
        threading.Thread(target=self.clipboard_checker, args=button).start()

    def clipboard_checker(self, *args):
        button = "".join(args)
        try:
            Clock.unschedule(self.event2)
        except:
            pass
        self.event2 = Clock.schedule_interval(partial(self.clipboard_paster, button), 1.0)

    def clipboard_paster(self, button, dt=None):
        text = Clipboard.paste()

        if text != self.last and button == "automat":
            text = Clipboard.paste().strip()
            Clipboard.copy(text)
        elif button == "automat bez mezer":
            self.zkratit()
            text = Clipboard.paste()

        if text != self.last:
            self.ctrlc.cb_list.adapter.data.extend([text])
            self.last = text

    def time_counter(self, *button):
        button = button[0]
        if button == "prace":
            self.pr += datetime.timedelta(seconds=1)
            self.tlacitka.prace = self.pr.strftime("%H:%M:%S")
        elif button == "obed":
            self.ob += datetime.timedelta(seconds=1)
            self.tlacitka.obed = self.ob.strftime("%H:%M:%S")
        else:
            self.pa += datetime.timedelta(seconds=1)
            self.tlacitka.pauza = self.pa.strftime("%H:%M:%S")

    def time_starter(self, button):
        threading.Thread(target=self.time_checker, args=button.text).start()

    def time_checker(self, *args):
        button = "".join(args)
        try:
            Clock.unschedule(self.event1)
        except:
            pass
        self.event1 = Clock.schedule_interval(partial(self.time_counter, button), 1.0)

    def zkrat_ref(self):
        b = Clipboard.paste()
        ref = re.compile("\d{10}")
        c = ref.search(b)
        if c:
            Clipboard.copy(c.group()[5:])
        else:
            pass

    def low(self):
        b = Clipboard.paste()
        Clipboard.copy(b.lower())

    @staticmethod
    def zvetsit():
        b = Clipboard.paste()
        try:
            o = re.compile("(\d{12})")
            mo = o.findall(b)
            if mo:
                if mo[0][:3] == "420":
                    i = 0
                    e = []
                    for j in mo[0]:
                        e.append(j)
                        i += 1
                        if i % 3 == 0:
                            e.append(" ")
                    Clipboard.copy("".join(e[:-1]))

            c = re.compile("(\d{9})")
            mo1 = c.findall(b)
            if mo1:
                if mo1[0][:3] != "420":
                    i = 0
                    e = []
                    for j in mo1[0]:
                        e.append(j)
                        i += 1
                        if i % 3 == 0:
                            e.append(" ")
                    Clipboard.copy("".join(e[:-1]))

        except IndexError:
            pass

    @staticmethod
    def zkratit():
        d = []
        b = Clipboard.paste()
        c = re.compile(
            """
        (\d{3})
        (\s|-|\.)?
        (\d{3})
        (\s|-|\.)?
        (\d{3})
        (\s|-|\.)?
        (\d{3})
        """,
            re.VERBOSE,
        )
        mo = c.findall(b)
        for i in mo:
            if i[0] == "420":
                num = "".join([i[2], i[4], i[6]])
                d.append(num)
        e = re.compile(
            """
        (\d{3})
        (\s|-|\.)?
        (\d{3})
        (\s|-|\.)?
        (\d{3})
        """,
            re.VERBOSE,
        )
        mo1 = e.findall(b)
        for j in mo1:
            if j[0] != "420":
                num = "".join([j[0], j[2], j[4]])
                d.append(num)

        Clipboard.copy("\r\n".join(d))

    def save_pass(self):
        with open("qwerqwr.dat", "wb") as target:
            login = self.tlacitka.log.text
            heslo = self.tlacitka.pas.text
            a = (login, heslo)
            self.tlacitka.log.text = ""
            self.tlacitka.pas.text = ""
            pickle.dump(a, target)

    def log_in_starter(self):
        threading.Thread(target=self.log_in).start()

    def log_in(self):
        time.sleep(2)
        with open("qwerqwr.dat", "rb") as source:
            a = pickle.load(source)
            login = a[0]
            heslo = a[1]

        typewrite("\t")
        typewrite(login)
        typewrite("\t")
        typewrite(heslo)
        press("enter")

class WidgetApp(App):
    def build(self):
        self.title = "Pomocnik"
        return MyWidget()

if __name__ == "__main__":
    WidgetApp().run()