from unittest.mock import Mock

from fdk_rdf_parser import parse_data_services
from fdk_rdf_parser.classes import (
    Catalog,
    DataService,
    DCATContactPoint,
    HarvestMetaData,
    MediaTypeOrExtent,
    MediaTypeOrExtentType,
    Publisher,
    ReferenceDataCode,
    SkosConcept,
)

def test_parse_multiple_data_services(
    mock_reference_data_client: Mock,
) -> None:
    expected = {
        "https://testutgiver.no/data-services/2": DataService(
            publisher=Publisher(
                uri="https://organizations.fellesdatakatalog.digdir.no/organizations/123456789",
                id="123456789",
                name="Digitaliseringsdirektoratet",
                orgPath="/STAT/987654321/123456789",
                prefLabel={
                    "nb": "Digitaliseringsdirektoratet",
                    "nn": "Digitaliseringsdirektoratet",
                    "en": "Norwegian Digitalisation Agency",
                },
                organisasjonsform="ORGL",
            ),
            title={
                "nb": "Kaffe API",
                "en": "Kaffe API Nynorsk ENGELSK",
                "nn": "Kaffe API Nynorsk",
            },
            description={
                "nn": "Beskrivelse for å benytte seg av en kaffemaskin NYNORSK",
                "nb": "Beskrivelse for å benytte seg av en kaffemaskin",
                "en": "Beskrivelse for å benytte seg av en kaffemaskin ENGELSK",
            },
            descriptionFormatted={
                "nn": "Beskrivelse for å benytte seg av en kaffemaskin NYNORSK",
                "nb": "Beskrivelse for å benytte seg av en kaffemaskin",
                "en": "Beskrivelse for å benytte seg av en kaffemaskin ENGELSK",
            },
            uri="https://testutgiver.no/data-services/2",
            contactPoint=[
                DCATContactPoint(
                    fullname="Contact information",
                    email="kaffe@epost.no",
                    organizationName={"nb": "Kaffehuset"},
                    hasURL="http://www.kaffehuset.no",
                )
            ],
            id="2",
            harvest=HarvestMetaData(
                firstHarvested="2020-08-07T09:56:01Z", changed=["2020-08-07T10:02:26Z"]
            ),
            endpointDescription={
                "http://example.com/Dette%20er%20en%20test",
                "http://example.com/dette%20skal%20v%C3%A6re%20en%20lenke",
            },
            endpointURL={"http://kaffe.no", "https://kaffemaskin.no"},
            mediaType=[
                ReferenceDataCode(
                    uri="https://www.iana.org/assignments/media-types/text/turtle",
                    code="text/turtle",
                    prefLabel={"nb": "text/turtle"},
                ),
            ],
            fdkFormat=[
                MediaTypeOrExtent(
                    uri="https://www.iana.org/assignments/media-types/text/turtle",
                    name="turtle",
                    code="text/turtle",
                    type=MediaTypeOrExtentType.MEDIA_TYPE,
                ),
            ],
            servesDataset={"http://testutgiver.no/datasets/abc"},
            conformsTo=[
                SkosConcept(
                    uri="https://data.norge.no/def/serviceType#CUSTOMER_RELATIONS"
                )
            ],
            page=["https://data4.norge.no"],
            catalog=Catalog(
                id="d07885d9-0925-339f-bb22-58c28dc30409",
                uri="https://testutgiver.no/catalogs/123456789",
                title={"en": "Data service catalog"},
                publisher=Publisher(
                    uri="https://organizations.fellesdatakatalog.digdir.no/organizations/123456789",
                    id="123456789",
                    name="Digitaliseringsdirektoratet",
                    orgPath="/STAT/987654321/123456789",
                    prefLabel={
                        "nb": "Digitaliseringsdirektoratet",
                        "nn": "Digitaliseringsdirektoratet",
                        "en": "Norwegian Digitalisation Agency",
                    },
                    organisasjonsform="ORGL",
                ),
            ),
        ),
        "https://testutgiver.no/dataservices/0": DataService(
            publisher=Publisher(
                uri="https://organization-catalog.fellesdatakatalog.brreg.no/organizations/987654321",
            ),
            uri="https://testutgiver.no/dataservices/0",
            id="d1d698ef-267a-3d57-949f-b2bc44657f3e",
            harvest=HarvestMetaData(
                firstHarvested="2020-06-22T13:39:27Z", changed=["2020-06-22T13:39:27Z"]
            ),
            endpointDescription={
                "https://raw.githubusercontent.com/Informasjonsforvaltning/fdk-api-harvester/master/src/main/resources/specification/fdk-api-harvester.yaml"
            },
            catalog=Catalog(
                id="d6199127-8835-33e1-9108-233cd81e92f9",
                uri="https://testutgiver.no/catalogs/987654321",
                title={"nb": "Dataservicekatalog2 for Digitaliseringsdirektoratet"},
                publisher=Publisher(
                    uri="https://organization-catalog.fellesdatakatalog.brreg.no/organizations/987654321",
                ),
            ),
        ),
        "https://testutgiver.no/dataservices/1": DataService(
            publisher=Publisher(
                uri="https://organization-catalog.fellesdatakatalog.brreg.no/organizations/987654321",
            ),
            title={"nb": "Testing Testing"},
            uri="https://testutgiver.no/dataservices/1",
            contactPoint=[
                DCATContactPoint(
                    fullname="Contact information",
                )
            ],
            id="1",
            harvest=HarvestMetaData(
                firstHarvested="2020-06-22T13:39:27Z", changed=["2020-06-22T13:39:27Z"]
            ),
            endpointDescription={"http://example.com/"},
            endpointURL={"https://vg.no"},
            catalog=Catalog(
                id="d6199127-8835-33e1-9108-233cd81e92f9",
                uri="https://testutgiver.no/catalogs/987654321",
                title={"nb": "Dataservicekatalog2 for Digitaliseringsdirektoratet"},
                publisher=Publisher(
                    uri="https://organization-catalog.fellesdatakatalog.brreg.no/organizations/987654321",
                ),
            ),
        ),
    }

    with open("tests/test_data/dataservice0.ttl", "r") as src:
        assert parse_data_services(src.read()) == expected


import numpy as np
from .baseMetric import BaseMetric
from lsst.sims.maf.utils import m52snr

__all__ = ['PhaseGapMetric', 'PeriodicQualityMetric']

class PhaseGapMetric(BaseMetric):
    """
    Measure the maximum gap in phase coverage for observations of periodic variables.

    Parameters
    ----------
    col: str, opt
        Name of the column to use for the observation times (MJD)
    nPeriods: int, opt
        Number of periods to test
    periodMin: float, opt
        Minimum period to test, in days.
    periodMax: float, opt
        Maximum period to test, in days
    nVisitsMin: int, opt
        Minimum number of visits necessary before looking for the phase gap.
    """
    def __init__(self, col='observationStartMJD', nPeriods=5, periodMin=3., periodMax=35., nVisitsMin=3,
                 metricName='Phase Gap', **kwargs):
        self.periodMin = periodMin
        self.periodMax = periodMax
        self.nPeriods = nPeriods
        self.nVisitsMin = nVisitsMin
        super(PhaseGapMetric, self).__init__(col, metricName=metricName, units='Fraction, 0-1', **kwargs)

    def run(self, dataSlice, slicePoint=None):
        if len(dataSlice) < self.nVisitsMin:
            return self.badval
        # Create 'nPeriods' evenly spaced periods within range of min to max.
        step = (self.periodMax-self.periodMin)/self.nPeriods
        if step == 0:
            periods = np.array([self.periodMin])
        else:
            periods = np.arange(self.nPeriods)
            periods = periods/np.max(periods)*(self.periodMax-self.periodMin)+self.periodMin
        maxGap = np.zeros(self.nPeriods, float)

        for i, period in enumerate(periods):
            # For each period, calculate the phases.
            phases = (dataSlice[self.colname] % period)/period
            phases = np.sort(phases)
            # Find the largest gap in coverage.
            gaps = np.diff(phases)
            start_to_end = np.array([1.0 - phases[-1] + phases[0]], float)
            gaps = np.concatenate([gaps, start_to_end])
            maxGap[i] = np.max(gaps)

        return {'periods':periods, 'maxGaps':maxGap}

    def reduceMeanGap(self, metricVal):
        """
        At each slicepoint, return the mean gap value.
        """
        return np.mean(metricVal['maxGaps'])

    def reduceMedianGap(self, metricVal):
        """
        At each slicepoint, return the median gap value.
        """
        return np.median(metricVal['maxGaps'])

    def reduceWorstPeriod(self, metricVal):
        """
        At each slicepoint, return the period with the largest phase gap.
        """
        worstP = metricVal['periods'][np.where(metricVal['maxGaps'] == metricVal['maxGaps'].max())]
        return worstP

    def reduceLargestGap(self, metricVal):
        """
        At each slicepoint, return the largest phase gap value.
        """
        return np.max(metricVal['maxGaps'])

#  To fit a periodic source well, you need to cover the full phase, and fit the amplitude.
class PeriodicQualityMetric(BaseMetric):
    def __init__(self, mjdCol='observationStartMJD', period=2., m5Col='fiveSigmaDepth',
                 metricName='PhaseCoverageMetric', starMag=20, **kwargs):
        self.mjdCol = mjdCol
        self.m5Col = m5Col
        self.period = period
        self.starMag = starMag
        super(PeriodicQualityMetric, self).__init__([mjdCol, m5Col], metricName=metricName,
                                                    units='Fraction, 0-1', **kwargs)

    def _calc_phase(self, dataSlice):
        """1 is perfectly balanced phase coverage, 0 is no effective coverage.
        """
        angles = dataSlice[self.mjdCol] % self.period
        angles = angles/self.period * 2.*np.pi
        x = np.cos(angles)
        y = np.sin(angles)

        snr = m52snr(self.starMag, dataSlice[self.m5Col])
        x_ave = np.average(x, weights=snr)
        y_ave = np.average(y, weights=snr)

        vector_off = np.sqrt(x_ave**2+y_ave**2)
        return 1.-vector_off

    def _calc_amp(self, dataSlice):
        """Fractional SNR on the amplitude, testing for a variety of possible phases
        """
        phases = np.arange(0, np.pi, np.pi/8.)
        snr = m52snr(self.starMag, dataSlice[self.m5Col])
        amp_snrs = np.sin(dataSlice[self.mjdCol]/self.period*2*np.pi + phases[:, np.newaxis])*snr
        amp_snr = np.min(np.sqrt(np.sum(amp_snrs**2, axis=1)))

        max_snr = np.sqrt(np.sum(snr**2))
        return amp_snr/max_snr

    def run(self, dataSlice, slicePoint=None):
        amplitude_fraction = self._calc_amp(dataSlice)
        phase_fraction = self._calc_phase(dataSlice)
        return amplitude_fraction * phase_fraction


from django.urls import path

from blogs import views

app_name = 'blogs'

urlpatterns = [
    path('', views.home_page),
    path('post/<slug:slug>', views.PostView.as_view(), name='post'),
    path('featured/', views.FeaturedListView.as_view(), name='featured'),
    path('category/<slug:slug>', views.CategoryListView.as_view(), name='category'),
    path('search/', views.SearchResultsView.as_view(), name='search'),
]


"""
Adib Hasan, Angelos Pelecanos
This code is part of a project for the class
6.890 - Learning-Augmented Algorithms
Spring 2019
"""

from item import Item
from model import Model
from dummy_list import DummyList
from math import ceil, log
from print_heap import printHeapTree

class AugmentedBinaryHeap:
    def __init__(self, model_function):
        """
        Initialization Function
        """
        self.heap_list = [None]         # list of items
        self.current_size = 0
        self.model = model_function     # model used to predict
        self.dummy_list = None          # this will save the dummy list

    #######################################################################################################
    # Regular Heap Functions (Augmented)
    #######################################################################################################

    def build_heap(self, element_list):
        """
        Builds a Max Heap out of element_list.
        Takes linear time
        Also samples extra n dummy values
        """

        item_list = self.itemize_elements(element_list)  # convert every element into an Item object
        self.model.set_train_data(element_list)       # models will be trained on this data
        self.heap_list = [None] + item_list
        self.current_size = len(self.heap_list)       # initialize size correctly
        self.resize_heap()                            # add dummies to the heap

    def resize_heap(self):
        """`
        adds n new dummies to the bottom of the heap in O(n) time
        """
        # print('Rebuilding Heap...')
        true_size = self.count_nondummy()           # count non-dummy (true) values in the heap
        dummy_size = self.current_size - true_size - 1  # number of existing dummies
        dummy_values = self.model.generate(true_size - dummy_size) # samples remaining dummies

        self.dummy_list = None                      # this dummy list is no longer valid
                                                    # heap has 2 * true size elements now
        self.heap_list += dummy_values              # add the dummy values
        self.current_size = len(self.heap_list)     # initialize size correctly

        # build heap
        n = true_size
        while (n > 0):                              # heapify the elements one by one
            self.swap_down(n)
            n = n - 1
        self.initialize_dummy_list()

        X, y = self.get_training_samples()          # train model on these training samples
        self.model.train(X, y)

    def swap_up(self, i):
        """
        Swapping Up Function
        Given the index of an element in the list, it swaps it up until it satisfies the heap property
        """
        i_prev = i # position before swap
        while i // 2 > 0:
            if self.heap_list[i] > self.heap_list[i // 2]:  # if the element is smaller than its parent
                self.swap(i, i // 2)
                i = i // 2      # update position of current element
            else: # else the element does satisfy heap property and we are done
                break

        self.update_ancestors_argmax(i_prev, i)  # update argmax of the ancestors

    def swap_down(self, i):
        """
        Swapping Down Function
        Given the index of an element in the list, it swaps it down
        until it satisfies the heap property
        """
        i_prev = i
        while (i * 2) < self.current_size:
            child = self.max_child(i)
            if self.heap_list[i] < self.heap_list[child]:   # if the element is greater than its child
                self.swap(i, child) # swap values at index i and child
                i = child   # update position of current element
            else: # else element does satisfy heap property and we are done
                break
        self.update_ancestors_argmax(i, i_prev)  # update argmax of the ancestors

    def insert(self, k):
        """
        Insertion Function
        Given a new element, predict the index of the dummy value in the dummy value list
        Insert at that index and propagate up and down as needed
        """
        dummy_index = self.model.predict([[k]])                         # get the index of the dummy value
        target_index = self.dummy_list.get_next_dummy(dummy_index)  # get the index of an empty dummy
        if target_index is not None:
            self.heap_list[target_index].value = k
            self.heap_list[target_index].dummy = False
            self.heap_list[target_index].argmax = target_index
            self.swap_up(target_index)                  # try swapping up
            self.swap_down(target_index)                # try swapping down (will not do anything if it has moved)
        else:
            self.resize_heap()
            self.insert(k)

    def get_max(self, node=1):
        """
        Get maximum value while avoiding dummies
        """
        argmax = self.heap_list[node].argmax
        if argmax > -1:
            return self.heap_list[argmax]

    def extract_max(self):
        """
        Extract Maximum Function
        Returns and deletes the maximum element from the heap
        """
        if self.current_size == 1: return None          # heap is empty
        retval = self.heap_list[1].argmax               # maximum element is at index 1
        if self.is_dummy(retval):
            raise ValueError("A dummy is being extracted.")
        if retval > -1:                                 # argmax is valid
            self.swap(retval, self.current_size - 1)    # move last element to the top
            self.current_size = self.current_size - 1   # decrease size
            maxval = self.heap_list[-1]
            self.heap_list.pop()                        # delete last element

            self.update_ancestors_argmax(self.current_size // 2, self.current_size // 2)
            if retval != self.current_size:
                self.swap_down(retval)                    # swap down top element
                self.swap_up(retval)                    # swap down top element
            return maxval

    #######################################################################################################
    # Helper Functions
    #######################################################################################################

    def count_nondummy(self):
        """
        count the number of non-dummies in the heap
        """
        count = 0
        for i in range(1, self.current_size):
            if not self.is_dummy(i): count += 1
        return count

    def itemize_elements(self, element_list):
        """
        Given a list of elements, create a list of non-dummy elements
        """
        item_list = []
        for element in element_list:
            item_list.append(Item(element, False, -1))
            item_list[-1].argmax = len(item_list) - 1 # for a true value argmax is its index in the heap
        return item_list

    def initialize_dummy_list(self):
        """
        This function is called after the building to set up the dummy list
        """
        dummy_positions = []                    # the index that the dummy value is
        for i in range(1, self.current_size):   # check all values if they are dummy
            if self.is_dummy(i):
                dummy_positions.append(i)
                # a dummy element should remember its position in dummy list
                self.heap_list[i].update_list_position(len(dummy_positions)-1)
        self.dummy_list = DummyList(dummy_positions)

    def get_training_samples(self):
        """
        training sample is (dummy value --> its index in the dummy list)
        """
        X = [] # input
        y = [] # label
        # for heap position of each dummy
        for pos in self.dummy_list.heap_position:
            X.append([self.heap_list[pos].value])
            y.append(self.heap_list[pos].get_list_position())
        return X, y

    def is_dummy(self, i):
        """
        Is the item at index i dummy?
        """
        return self.heap_list[i].is_dummy()

    def children(self, i):
        """
        Given the index of an element, returns the indices of
        its children as a list. If there is no child, returns an empty list
        """
        if i * 2 >= self.current_size:
            return []
        elif i * 2 + 1 >= self.current_size:
            return [i * 2]
        else:
            return [i * 2, i * 2 + 1]

    def max_child(self, i):
        """
        Given the index of an element, returns the index of
        its max_child
        """
        if i * 2 + 1 >= self.current_size:
            return i * 2
        else:
            if self.heap_list[i*2] > self.heap_list[i*2+1]:
                return i * 2
            else:
                return i * 2 + 1

    def get_children_argmax(self, i):
        """
        argmax-es over the children of node i
        """

        argmax = -1
        for child in self.children(i):
            if child < 0 or child >= self.current_size:
                raise ValueError("invalid child")
            j = self.heap_list[child].argmax if self.is_dummy(child) else child # subtree max of child
            if j >= self.current_size:
                raise ValueError("invalid argmax")
            if j > -1:                         # this child has valid argmax
                if argmax <= -1: argmax = j    # current argmax is invalid, so set current argmax
                elif self.heap_list[j] > self.heap_list[argmax]: argmax = j # j has better argmax
        return argmax

    def update_ancestors_argmax(self, i, j):
        """
        given the index of a non-dummy node, it updates argmax of all its ancestors
        until it finds a nondummy node
        starts from node i, always goes up to ancestor j, then stops first time it finds
        two ancestor sharing same argmax
        """
        while i > 0:
            if self.heap_list[i].is_dummy():
                parent_argmax = self.heap_list[i].argmax
                child_argmax = self.get_children_argmax(i) # best argmax among all children
                if child_argmax != parent_argmax: # parent is dummy, so must have same as best child's argmax
                    self.heap_list[i].argmax = child_argmax
                elif i < j:# parent, child have same argmax and checked upto ancestor j
                    break # no point looking above parent
            else: # parent is a non dummy value
                self.heap_list[i].argmax = i
            i = i // 2      # update position of current element

    def swap(self, i, j):
        """
        swaps values of node i and j in the heap
        """
        # swaps values at i and j
        item_i = self.heap_list[i]
        item_j = self.heap_list[j]

        if self.dummy_list is not None:
            if item_i.is_dummy():
                # item_i is going to position j, so update that in the dummy list
                self.dummy_list.update_heap_position(item_i.get_list_position(), j)

            if item_j.is_dummy():
                # item_j is going to position i, so update that in the dummy list
                self.dummy_list.update_heap_position(item_j.get_list_position(), i)

        # swap values
        self.heap_list[i], self.heap_list[j] = item_j, item_i


# https://leetcode.com/problems/4sum/
# 18. 4Sum
# Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
# Note:
# The solution set must not contain duplicate quadruplets.

# Example:
# Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
# A solution set is:
# [
  # [-1,  0, 0, 1],
  # [-2, -1, 1, 2],
  # [-2,  0, 0, 2]
# ]

class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:

        def find(l,r,target,cnt,result,op):
            if r-l+1 < cnt or cnt < 2 or target < nums[l]*cnt or target > nums[r]*cnt:
                return
            if cnt == 2:
                #print(nums[l:r+1])
                my_dict = collections.Counter(nums[l:r+1])
                for i in range(l,r+1):
                    r = target-nums[i]
                    ins = False
                    if r == nums[i]:
                        if my_dict[r] >= 2:
                            ins = True
                    elif r in my_dict:
                        ins = True
                    if ins:
                        t = sorted([nums[i],r])
                        #print(nums[i],r,target,my_dict)
                        op[(tuple(result+t))] = 1
            else:
                for i in range(l,r+1):
                    if i == l or (i > l and nums[i-1] != nums[i]):
                        find(i+1,r,target-nums[i],cnt-1,result+[nums[i]],op)

        nums.sort()
        op = dict()
        find(0,len(nums)-1,target,4,[],op)
        return op


'''
OccamLab: ARKit ROS Bridge - Extract UDP Data
'''

import socket

def extractUDP(udp_port):
    ''' Gets UDP data from a given port. '''

    UDP_IP = "0.0.0.0"
    UDP_PORT = udp_port

    sock = socket.socket(socket.AF_INET, # Internet
                         socket.SOCK_DGRAM) # UDP
    sock.bind((UDP_IP, UDP_PORT))

    while True:
        data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
        return(data)


# -*- coding: utf-8 -*-
import os,json
import numpy as np
import pandas as pd
import torch
import argparse
from torch.utils.data import Dataset, DataLoader
# Importing the T5 modules from huggingface/transformers
from transformers import T5Tokenizer , AutoConfig
from transformers.generation import utils
from rich.table import Column, Table
from rich import box
from rich.console import Console
import argparse
import tqdm
import json,pylcs
import prettytable as pt
from modeling_MTA import T5ForConditionalGenerationMTA
from script.eval import evaluate_pclue_fn_no_mrc
from script.forward_val import forward_predict
from predict import predict

import logging

parser = argparse.ArgumentParser()
parser.add_argument("--local_rank",type=int,default=0)
parser.add_argument("--csv_path",type=str,default="project/data/clean_data_ori/english_train_new.csv",help="训练数据转为csv")
parser.add_argument("--model_save_path",type=str,default="./result/",help="模型存储的位置")
parser.add_argument("--model_name",type=str,default="for_debug",help="模型存储的文件夹名")
parser.add_argument("--model_for_train",type=str,default='t5-base')
parser.add_argument("--batch_size",type=int,default=4)
parser.add_argument("--train_epoch",type=int,default=1)
parser.add_argument("--val_epoch",type=int,default=1)
parser.add_argument("--accumulate",type=int,default=None)
parser.add_argument("--val_json_path",type=str,help="验证集地址",default="project/data/clean_data_ori/english_val_new_shuffle.json")
parser.add_argument("--config_path",type=str,help="验证集地址",default="models/test_config/t5_base_config.json")
parser.add_argument("--eval_json_path",type=str,default="project/data/clean_data_ori/english_val_new_shuffle.json")
parser.add_argument("--source_max_len",type=int,default=512)
parser.add_argument("--random_seed",type=int,default=42)
parser.add_argument("--lr",type=float,default=1e-4)
args = parser.parse_args()

# model parameters
model_for_train =args.model_for_train
lr = args.lr
source_max_len = args.source_max_len
train_epoch = args.train_epoch
batch_size = args.batch_size
val_epoch = args.val_epoch
eval_json_path = args.eval_json_path
random_seed = args.random_seed
config_path = args.config_path
val_json_path = args.val_json_path

accumulate = args.accumulate
csv_path = args.csv_path
model_name = args.model_name
model_save_path = args.model_save_path

# 定义模型的参数 let's define model parameters specific to T5
model_params = {
    "MODEL": model_for_train,  # model_type
    "TRAIN_BATCH_SIZE": batch_size,  # training batch size, 8
    "TRAIN_EPOCHS": train_epoch,  # number of training epochs
    "VAL_EPOCHS": val_epoch,  # number of validation epochs
    "LEARNING_RATE": lr,  # learning rate
    "MAX_SOURCE_TEXT_LENGTH": source_max_len,  # max length of source text, 512
    "MAX_TARGET_TEXT_LENGTH": 64,  # max length of target text,64
    "SEED": random_seed,  # set seed for reproducibility
}

# define a rich console logger
console = Console(record=True)

# to display dataframe in ASCII format
def display_df(df):
    """display dataframe in ASCII format"""

    console = Console()
    table = Table(
        Column("source_text", justify="center"),
        Column("target_text", justify="center"),
        title="Sample Data",
        pad_edge=False,
        box=box.ASCII,
    )

    for i, row in enumerate(df.values.tolist()):
        table.add_row(row[0], row[1])

training_logger = Table(
    Column("Epoch", justify="center"),
    Column("Steps", justify="center"),
    Column("Loss", justify="center"),
    title="Training Status",
    pad_edge=False,
    box=box.ASCII,
)

class YourDataSetClass(Dataset):
    """
    创建一个自定义的数据集，用于训练，必须包括两个字段：输入(如source_text)、输出（如target_text）
    Creating a custom dataset for reading the dataset and
    loading it into the dataloader to pass it to the
    neural network for finetuning the model

    """

    def __init__(
        self, dataframe, tokenizer, source_len, target_len, source_text, target_text
    ):
        """
        Initializes a Dataset class

        Args:
            dataframe (pandas.DataFrame): Input dataframe
            tokenizer (transformers.tokenizer): Transformers tokenizer
            source_len (int): Max length of source text
            target_len (int): Max length of target text
            source_text (str): column name of source text
            target_text (str): column name of target text
        """
        self.tokenizer = tokenizer
        self.data = dataframe
        self.source_len = source_len
        self.summ_len = target_len
        self.target_text = self.data[target_text]
        self.source_text = self.data[source_text]
        self.type_text = self.data["type"]

    def __len__(self):
        """returns the length of dataframe"""

        return len(self.target_text)

    def __getitem__(self, index):
        """return the input ids, attention masks and target ids"""

        source_text = str(self.source_text[index])
        target_text = str(self.target_text[index])
        type_label = str(self.type_text[index])

        # cleaning data so as to ensure data is in string type
        source_text = " ".join(source_text.split())
        target_text = " ".join(target_text.split())
        type_label = " ".join(type_label.split())

        source = self.tokenizer.batch_encode_plus(
            [source_text],
            max_length=self.source_len,
            pad_to_max_length=True,
            truncation=True,
            padding="max_length",
            return_tensors="pt",
        )
        target = self.tokenizer.batch_encode_plus(
            [target_text],
            max_length=self.summ_len-1,
            pad_to_max_length=True,
            truncation=True,
            padding="max_length",
            return_tensors="pt",
        )

        source_ids = source["input_ids"].squeeze()
        source_mask = source["attention_mask"].squeeze()
        target_ids = target["input_ids"].squeeze()
        target_mask = target["attention_mask"].squeeze()

        return {
            "source_ids": source_ids.to(dtype=torch.long),
            "attention_mask": source_mask.to(dtype=torch.long),
            "target_ids": target_ids.to(dtype=torch.long),
            "target_ids_y": target_ids.to(dtype=torch.long),
            "type_ids":  type_label,
        }

def train(epoch, tokenizer, model, device, loader, optimizer,path_final,val_json_path,new_token):

    """
    用于训练的方法
    Function to be called for training with the parameters passed from main function

    """
    print("start train")
    eval_step = 7000
    score_temp = 0
    select_top = -1
    for i, data in enumerate(tqdm.tqdm(loader), 0):
        model.train()

        data["target_ids"] = torch.cat((torch.tensor([[new_token]]*data["target_ids"].shape[0]),data["target_ids"]),1)

        y = data["target_ids"].to(device, dtype=torch.long)
        y_ids = y[:, :-1].contiguous() # target, from start to end(except end of token, ). e.g. "你好吗？"
        lm_labels = y[:, 1:].clone().detach() # target, for second to end.e.g."好吗？"
        lm_labels[y[:, 1:] == tokenizer.pad_token_id] = -100 # releted to pad_token and loss. for detail, check here: https://github.com/Shivanandroy/T5-Finetuning-PyTorch/issues/3
        ids = data["source_ids"].to(device, dtype=torch.long) # input. e.g. "how are you?"
        mask = data["attention_mask"].to(device, dtype=torch.long)
        outputs = model(
            input_ids=ids,
            attention_mask=mask,
            decoder_input_ids=y_ids,
            labels=lm_labels,
            type_label=data["type_ids"],
        )
        loss = outputs[0]

        if (i+1)%300 == 0:
            print(str(epoch), str(i), str(loss))

        loss.backward()
        optimizer.step()
        optimizer.zero_grad()

        if (i) % eval_step == 0 and (epoch % 2 == 1):
            model.eval()
            with torch.no_grad():
                file_path = os.path.join(path_final,"{step}_predictions.json".format(step=i+1))
                forward_predict(source_file=val_json_path,target_file=file_path,tokenizer=tokenizer,model=model,select_top=select_top)
                score = evaluate_pclue_fn_no_mrc(file_path,val_json_path,select_top=select_top)
                if score > score_temp:
                    model.save_pretrained(os.path.join(path_final,"best_model"))
                    tokenizer.save_pretrained(os.path.join(path_final,"best_model"))
                    score_temp = score
    print("select_top:",select_top)
# 训练类：整合数据集类、训练方法、验证方法，加载数据进行训练并验证训练过程的效果
def T5Trainer(
    local_rank,dataframe,source_text, target_text, model_params,val_json_path,eval_json_path
):
    """
    T5 trainer
    """
    device = local_rank

    # Set random seeds and deterministic pytorch for reproducibility
    torch.manual_seed(model_params["SEED"])  # pytorch random seed
    np.random.seed(model_params["SEED"])  # numpy randdsdom seed
    torch.backends.cudnn.deterministic = True

    # logging
    console.log(f"""[Model]: Loading {model_params["MODEL"]}...\n""")

    config = AutoConfig.from_pretrained(config_path)
    config.foo = 'bar'

    model = T5ForConditionalGenerationMTA.from_pretrained(model_params["MODEL"],config=config)
    tokenizer = T5Tokenizer.from_pretrained(model_params["MODEL"])
    model.Copyweight()

    model = model.to(device)

    tokenizer.add_special_tokens({'additional_special_tokens':["<e>"]})
    new_token = tokenizer.encode(["<e>"])[0]

    path_final = model_save_path + model_name
    print(torch.cuda.current_device())
    if torch.cuda.current_device() == 0:
        print(path_final)
        assert not os.path.exists(path_final) , "模型存储路径已存在需要修改"
        os.mkdir(path_final)

    # logging
    console.log(f"[Data]: Reading data...\n")

    train_dataset = dataframe

    # 打印数据集相关日志：数据量、训练步数
    console.print(f"FULL Dataset: {dataframe.shape}")
    console.print(f"TRAIN Dataset: {train_dataset.shape}")
    total_train_steps=int((train_dataset.shape[0] * model_params["TRAIN_EPOCHS"])/model_params["TRAIN_BATCH_SIZE"])
    console.print(f"Total Train Steps: {total_train_steps}\n")

    # Creating the Training and Validation dataset for further creation of Dataloader
    training_set = YourDataSetClass(
        train_dataset,
        tokenizer,
        model_params["MAX_SOURCE_TEXT_LENGTH"],
        model_params["MAX_TARGET_TEXT_LENGTH"],
        source_text,
        target_text,
    )

    # Defining the parameters for creation of dataloaders
    train_params = {
        "batch_size": model_params["TRAIN_BATCH_SIZE"],
        "shuffle": True,
        "num_workers": 0,
    }

    training_loader = DataLoader(training_set, **train_params)

    # Defining the optimizer that will be used to tune the weights of the network in the training session.
    optimizer = torch.optim.Adam(
        params=model.parameters(), lr=model_params["LEARNING_RATE"]
    )

    # Training loop
    console.log(f"[Initiating Fine Tuning]...\n")

    for epoch in range(model_params["TRAIN_EPOCHS"]):
        train(epoch, tokenizer, model, device, training_loader,optimizer ,path_final ,val_json_path,new_token)
        console.log(f"[Saving Model]...\n")
        path = os.path.join(path_final, "final")
        model.save_pretrained(path)
        tokenizer.save_pretrained(path)

    with torch.no_grad():
        eval_best_model = os.path.join(path_final,"best_model")
        predict(eval_best_model,eval_json_path)

df = pd.read_csv(csv_path)  # 数据量：1200k数据。

T5Trainer(
    local_rank=args.local_rank,
    dataframe=df,
    source_text="input",
    target_text="target",
    val_json_path=val_json_path,
    eval_json_path=eval_json_path,
    model_params=model_params,
)
print("train finished end..")


#!/usr/bin/python3
"""Onion Omega2 port of RPi powerbank using bq25895
https://github.com/tjohn327/raspberry_pi_ups/blob/master/src/powerpi.py
"""
from OmegaExpansion import onionI2C
import time
import sys
import logging

print("Loaded I2C Libraries")

class PowerOmega:
    PORT = 0
    ADDRESS = 0x6A      #I2C address of the ups
    #Refer to http://www.ti.com/lit/ds/symlink/bq25895.pdf for register maps

    REG_WATCHDOG = 0x07
    BYTE_WATCHDOG_STOP =  0b10001101 #Stop Watchdog timer
    REG_SYSMIN = 0x03
    BYTE_SYSMIN = 0b00110000
    REG_ILIM = 0x00 #ILIM register

    ####Edit this section to suit your needs######
    BYTE_ILIM =  0b00000000 #Input limit disabled
    #BYTE_ILIM =  0b01101000 #2A input current limit
    #BYTE_ILIM =  0b01111111 #3.25A input current limit
    BYTE_ICHG =  0b00001000 #.5A charging current limit
    #BYTE_ICHG =  0b00010000 #1A charging current limit
    BAT_CAPACITY = 1800 #Battery capacity in mAh
    CURRENT_DRAW = 2000 #Current draw in mAh approximately
    VBAT_LOW = 3.2
    ###############################################

    REG_ICHG = 0x04
    REG_ICHGR = 0x12
    REG_CONV_ADC = 0x02
    REG_BATFET = 0x09
    BYTE_BATFET = 0b01001000 #delay before battery is disconnected

    REG_CONV_ADC = 0x02
    BYTE_CONV_ADC_START = 0b10011101
    BYTE_CONV_ADC_STOP = 0b00011101
    REG_BATFET_DIS = 0x09
    BYTE_BATFET_DIS = 0b01101000
    REG_STATUS = 0x0B #address of status register
    REG_VBAT = 0x0E
    REG_FAULT = 0x0C
    REG_IBAT = 0x12
    REG_VBUS = 0x11
    VBAT_MAX = 4.208

    def __init__(self):
        pass

    def initialize(self):
        try:
            self.bus = onionI2C.OnionI2C(self.PORT)
            self.bus.writeByte(self.ADDRESS, self.REG_WATCHDOG, self.BYTE_WATCHDOG_STOP)
            self.bus.writeByte(self.ADDRESS, self.REG_ILIM,self.BYTE_ILIM)
            self.bus.writeByte(self.ADDRESS, self.REG_ICHG, self.BYTE_ICHG)
            self.bus.writeByte(self.ADDRESS, self.REG_BATFET, self.BYTE_BATFET)
            self.bus.writeByte(self.ADDRESS, self.REG_SYSMIN, self.BYTE_SYSMIN)
            logging.info("UPS initialized")
            return 0
        except Exception as ex:
            logging.error("Initialization failed, check connection to the UPS:"+ str(ex))
            return 1

    def _int_to_bool_list(self,num):
        return [bool(num & (1<<n)) for n in range(8)]

    def _vbat_convert(self,vbat_byte):
        vbat_bool = self._int_to_bool_list(vbat_byte)
        vbat = 2.304
        vbat += vbat_bool[6] * 1.280
        vbat += vbat_bool[5] * 0.640
        vbat += vbat_bool[4] * 0.320
        vbat += vbat_bool[3] * 0.160
        vbat += vbat_bool[2] * 0.08
        vbat += vbat_bool[1] * 0.04
        vbat += vbat_bool[0] * 0.02
        return vbat

    def _ibat_convert(self,ibat_byte):
        ibat_bool = self._int_to_bool_list(ibat_byte)
        ibat = 0
        ibat += ibat_bool[6] * 3200
        ibat += ibat_bool[5] * 1600
        ibat += ibat_bool[4] * 800
        ibat += ibat_bool[3] * 400
        ibat += ibat_bool[2] * 200
        ibat += ibat_bool[1] * 100
        ibat += ibat_bool[0] * 50
        return ibat

    def _vbus_convert(self,vbus_byte):
        vbus_bool = self._int_to_bool_list(vbus_byte)
        vbus = 2.6
        vbus += vbus_bool[6] * 6.4
        vbus += vbus_bool[5] * 3.2
        vbus += vbus_bool[4] * 1.6
        vbus += vbus_bool[3] * 0.8
        vbus += vbus_bool[2] * 0.4
        vbus += vbus_bool[1] * 0.2
        vbus += vbus_bool[0] * 0.1
        return vbus

    def _calc_bat_charge_percent(self,vbat):
        bat_charge_percent = (vbat-self.VBAT_LOW)/(self.VBAT_MAX - self.VBAT_LOW)
        if bat_charge_percent < 0:
            bat_charge_percent = 0
        elif bat_charge_percent > 1:
            bat_charge_percent = 1
        return bat_charge_percent

    def _calc_time_left(self,vbat):
        time_left = int(self._calc_bat_charge_percent(vbat) * 60 * self.BAT_CAPACITY / self.CURRENT_DRAW)
        if time_left < 0:
            time_left = 0
        return time_left

    def read_status(self, clear_fault=False):
        try:
            if clear_fault:
                self.bus.readBytes(self.ADDRESS, self.REG_FAULT, 1)
            self.bus.writeByte(self.ADDRESS, self.REG_CONV_ADC, self.BYTE_CONV_ADC_START)
            time.sleep(2)
            bat_fet = self.bus.readBytes(self.ADDRESS, self.REG_BATFET,1)[0]
            status = self.bus.readBytes(self.ADDRESS, self.REG_STATUS,1)[0]
            status = self._int_to_bool_list(int(status))
            vbat = self._vbat_convert(self.bus.readBytes(self.ADDRESS, self.REG_VBAT, 1)[0])
            ibat = self._ibat_convert(self.bus.readBytes(self.ADDRESS, self.REG_ICHGR, 1)[0])
            vbus = self._vbus_convert(self.bus.readBytes(self.ADDRESS, self.REG_VBUS, 1)[0])
            self.bus.writeByte(self.ADDRESS, self.REG_CONV_ADC, self.BYTE_CONV_ADC_STOP)
        except Exception as ex:
            logging.error("An exception occurred while reading values from the UPS: " + str(ex))
            time.sleep(2)
            return 1, None

        if status[2]:
            power_status = "Connected"
            time_left = -1
        else:
            power_status = "Not Connected"
            time_left = self._calc_time_left(vbat)

        if status[3] and status[4]:
            charge_status = "Charging done"
        elif status[4] and  not status[3]:
            charge_status = "Charging"
        elif not status[4] and status[3]:
            charge_status = "Pre-Charge"
        else:
            charge_status = "Not Charging"

        data = {
            'PowerInputStatus': power_status,
            'InputVoltage' : vbus,
            'ChargeStatus' : charge_status,
            'BatteryVoltage' : vbat,
            "BatteryPercentage" : int(self._calc_bat_charge_percent(vbat)*100),
            'ChargeCurrent' : ibat,
            'TimeRemaining' : int(time_left)
        }

        return 0, data

    def bat_disconnect(self):
        for i in (0,3):
            try:
                self.bus.writeByte(self.ADDRESS, self.REG_BATFET_DIS, self.BYTE_BATFET_DIS)
                return 0
            except:
                time.sleep(1)
        return 1

if __name__=="__main__":
    ups = PowerOmega()
    # Initial Charge start
    ups.initialize()
    print(ups.read_status(True))

    if len(sys.argv) > 1 and sys.argv[1] == "cycle":
        # Cycle BATFET to enable charge start
        # If powered from battery only this will switch off the device
        ups.bat_disconnect()
        time.sleep(10)
        print(ups.read_status(True))
        time.sleep(10)
        # Re-initialize and see if charge starts
        ups.initialize()
        time.sleep(10)
        print(ups.read_status(True))


from data.nvPrivate import *
# from utils.augmentations import SSDAugmentation
from layers.modules.multibox_loss_blaze import BlazeMultiBoxLoss
# from ssd import build_ssd
from blazeface import build_blazeface
from data.config import celeba

import os
import sys
import time
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.optim as optim
import torch.backends.cudnn as cudnn
import torch.nn.init as init
import torch.utils.data as data
import numpy as np
import argparse

def str2bool(v):
    return v.lower() in ("yes", "true", "t", "1")

parser = argparse.ArgumentParser(
    description='Single Shot MultiBox Detector Training With Pytorch')
train_set = parser.add_mutually_exclusive_group()
parser.add_argument('--dataset', default='VOC', choices=['VOC', 'COCO', 'CELEBA', 'NVP3D'],
                    type=str, help='VOC or COCO or CELEBA')
parser.add_argument('--dataset_root', default=NVP_ROOT,
                    help='Dataset root directory path')
parser.add_argument('--basenet', default='vgg16_reducedfc.pth',
                    help='Pretrained base model')
parser.add_argument('--batch_size', default=10, type=int,
                    help='Batch size for training')
parser.add_argument('--resume', default=None, type=str,
                    help='Checkpoint state_dict file to resume training from')
parser.add_argument('--start_iter', default=0, type=int,
                    help='Resume training at this iter')
parser.add_argument('--num_workers', default=1, type=int,
                    help='Number of workers used in dataloading')
parser.add_argument('--cuda', default=True, type=str2bool,
                    help='Use CUDA to train model')
parser.add_argument('--lr', '--learning-rate', default=1e-3, type=float,
                    help='initial learning rate')
parser.add_argument('--momentum', default=0.9, type=float,
                    help='Momentum value for optim')
parser.add_argument('--weight_decay', default=5e-4, type=float,
                    help='Weight decay for SGD')
parser.add_argument('--gamma', default=0.1, type=float,
                    help='Gamma update for SGD')
parser.add_argument('--visdom', default=False, type=str2bool,
                    help='Use visdom for loss visualization')
parser.add_argument('--save_folder', default='weights/',
                    help='Directory for saving checkpoint models')
args = parser.parse_args()

if torch.cuda.is_available():
    if args.cuda:
        torch.set_default_tensor_type('torch.cuda.FloatTensor')
    if not args.cuda:
        print("WARNING: It looks like you have a CUDA device, but aren't " +
              "using CUDA.\nRun with --cuda for optimal training speed.")
        torch.set_default_tensor_type('torch.FloatTensor')
else:
    torch.set_default_tensor_type('torch.FloatTensor')

if not os.path.exists(args.save_folder):
    os.mkdir(args.save_folder)

def train():
    if args.dataset == 'COCO':
        if args.dataset_root == VOC_ROOT:
            if not os.path.exists(COCO_ROOT):
                parser.error('Must specify dataset_root if specifying dataset')
            print("WARNING: Using default COCO dataset_root because " +
                  "--dataset_root was not specified.")
            args.dataset_root = COCO_ROOT
        cfg = coco
        dataset = COCODetection(root=args.dataset_root,
                                transform=SSDAugmentation(cfg['min_dim'],
                                                          MEANS))
    elif args.dataset == 'VOC':
        if args.dataset_root == COCO_ROOT:
            parser.error('Must specify dataset if specifying dataset_root')
        cfg = voc
        dataset = VOCDetection(root=args.dataset_root,
                               transform=SSDAugmentation(cfg['min_dim'],
                                                         MEANS))

    elif args.dataset == 'CELEBA':
        # if args.dataset_root == CELEBA_ROOT:
        #     parser.error('Must specify dataset if specifying dataset_root')
        cfg = celeba
        dataset = CelebaDetection(root=args.dataset_root)

    elif args.dataset == 'NVP3D':
        # if args.dataset_root == CELEBA_ROOT:
        #     parser.error('Must specify dataset if specifying dataset_root')
        cfg = celeba
        dataset = NvpDetection(root=args.dataset_root)

    if args.visdom:
        import visdom
        viz = visdom.Visdom()

    # ssd_net = build_ssd('train', cfg['min_dim'], cfg['num_classes'])
    # net = ssd_net
    blaze_net = build_blazeface('train')
    net = blaze_net

    if args.cuda:
        net = torch.nn.DataParallel(blaze_net)
        cudnn.benchmark = True

    if args.resume:
        print('Resuming training, loading {}...'.format(args.resume))
        blaze_net.load_weights(args.resume)
    # else:
    #     vgg_weights = torch.load(args.save_folder + args.basenet)
    #     print('Loading base network...')
    #     blaze_net.load_state_dict(vgg_weights)

    if args.cuda:
        net = net.cuda()

    if not args.resume:
        print('Initializing weights...')
        # initialize newly added layers' weights with xavier method
        # blaze_net.extras.apply(weights_init)
        # blaze_net.loc.apply(weights_init)
        # blaze_net.conf.apply(weights_init)

    optimizer = optim.SGD(net.parameters(), lr=args.lr, momentum=args.momentum,
                          weight_decay=args.weight_decay)
    criterion = BlazeMultiBoxLoss(cfg['num_classes'], 0.5, True, 0, True, 3, 0.5,
                             False, args.cuda)

    net.train()
    # loss counters
    loc_loss = 0
    conf_loss = 0
    epoch = 0
    print('Loading the dataset...')

    epoch_size = len(dataset) # args.batch_size
    print('Training SSD on:', dataset.name)
    print('Using the specified args:')
    print(args)

    step_index = 0

    if args.visdom:
        vis_title = 'SSD.PyTorch on ' + dataset.name
        vis_legend = ['Loc Loss', 'Conf Loss', 'Total Loss']
        iter_plot = create_vis_plot('Iteration', 'Loss', vis_title, vis_legend)
        epoch_plot = create_vis_plot('Epoch', 'Loss', vis_title, vis_legend)

    data_loader = data.DataLoader(dataset, args.batch_size,
                                  num_workers=args.num_workers,
                                  shuffle=True,
                                  pin_memory=True)
    # create batch iterator
    iteration_count = 0
    min_loss = 3.0
    while (1):

        batch_iterator = iter(data_loader)

        with_landmark = ""
        save_count = 0
        iter_count = 0
        for iteration in range(args.start_iter, cfg['max_iter']):
            if args.visdom and iteration != 0 and (iteration % epoch_size == 0):
                update_vis_plot(epoch, loc_loss, conf_loss, epoch_plot, None,
                                'append', epoch_size)
                # reset epoch loss counters
                loc_loss = 0
                conf_loss = 0
                epoch += 1

            iteration_count += 1

            if iteration_count % 20000 == 0:
                print ("ajust step...")
                step_index += 1
                adjust_learning_rate(optimizer, args.gamma, step_index)

            # load train data
            try:
                images, targets = next(batch_iterator)
            except StopIteration:
                iter_count = iter_count + 1
                print ("new iteration ", str(iter_count))
                break
            # print ("dataset: ", images.shape, targets.shape)

            # zmask = targets[:, :, 10] == 0
            # if torch.sum(zmask).data > 0:
            #     continue

            # print ("zmask", torch.sum(zmask))

            if args.cuda:
                images = Variable(images.cuda())
                # targets = [Variable(ann.cuda(), volatile=True) for ann in targets]
                targets = Variable(targets.cuda())
            else:
                images = Variable(images)
                # targets = [Variable(ann, volatile=True) for ann in targets]
                targets = Variable(targets.cuda())

            # forward
            t0 = time.time()
            out1  = net(images)
            out2 = out1[:, :, 10:12]
            priors = blaze_net.getPriors()

            # print ("output: ")
            # print (out1.shape)
            # print (out2.shape)
            # print (priors.shape)
            # backprop
            optimizer.zero_grad()
            loss_l, loss_c, loss_land = criterion((out1, out2, priors), targets)

            # print ("loss_l:", loss_l.shape)
            # print ("loss_c:", loss_c.shape)

            loss = loss_l + loss_c + loss_land[0] + loss_land[1]
            loss_with_land = loss_land
            # print (loss_with_land)
            # if (len(loss_with_land.data) > 0):
            #     with_landmark = "with_land_"

            # loss = loss.view(-1, 1)
            # loss_with_land = loss_with_land.view(-1, 1)
            # print ("loss:", loss, loss.shape)
            # print ("loss_with_land: ", loss_with_land.shape)

            # loss = torch.cat([loss, loss_with_land], 1)
            # print ("loss:", loss, loss.shape)
            # loss = 0.1 * loss.mean()  + loss_with_land.mean()
            # loss = 0.8 * loss  + loss_with_land
            # loss.backward(torch.FloatTensor([loss.size(0), loss.size(1)]))
            loss.sum().backward()
            # loss_with_land.backward()

            optimizer.step()
            t1 = time.time()
            # print ("loss_l", loss_l.shape)
            loc_loss += loss_l.data[0]  #loss
            # conf_loss += loss_c.data[0] #loss

            if iteration % 10 == 0:
                print('timer: %.4f sec.' % (t1 - t0))
                # print('iter ' + repr(iteration) + ' || Loss: %.4f ||' % (loss.data[0]))
                print('iter ' + repr(iteration) + ' || Loss: %.6f %.6f %.6f %.6f %.6f ||' % (loss.sum().data, loss_l.sum().data, loss_c.sum().data, loss_land[0].sum().data, loss_land[1].sum().data))

            if args.visdom:
                update_vis_plot(iteration, loss_l.data[0], loss_c.data[0],
                                iter_plot, epoch_plot, 'append')

            if (iteration != 0 and iteration_count % 10000  == 0) or (min_loss > float(loss.sum().data)):
                min_loss = float(loss.sum().data)
                save_count += 1
                print('Saving state, iter:', iteration_count)
                torch.save(blaze_net.state_dict(), 'weights/blaze_face_' + with_landmark +
                        repr(iteration_count) + "_loss_" + str(float(loss.sum().data)) + '.pth')

        torch.save(blaze_net.state_dict(),
                args.save_folder + '' + args.dataset + '.pth')

def adjust_learning_rate(optimizer, gamma, step):
    """Sets the learning rate to the initial LR decayed by 10 at every
        specified step
    # Adapted from PyTorch Imagenet example:
    # https://github.com/pytorch/examples/blob/master/imagenet/main.py
    """
    lr = args.lr * (gamma ** (step))
    print ("lr ", lr)
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr

def xavier(param):
    init.xavier_uniform(param)

def create_vis_plot(_xlabel, _ylabel, _title, _legend):
    return viz.line(
        X=torch.zeros((1,)).cpu(),
        Y=torch.zeros((1, 3)).cpu(),
        opts=dict(
            xlabel=_xlabel,
            ylabel=_ylabel,
            title=_title,
            legend=_legend
        )
    )

def update_vis_plot(iteration, loc, conf, window1, window2, update_type,
                    epoch_size=1):
    viz.line(
        X=torch.ones((1, 3)).cpu() * iteration,
        Y=torch.Tensor([loc, conf, loc + conf]).unsqueeze(0).cpu() / epoch_size,
        win=window1,
        update=update_type
    )
    # initialize epoch plot on first iteration
    if iteration == 0:
        viz.line(
            X=torch.zeros((1, 3)).cpu(),
            Y=torch.Tensor([loc, conf, loc + conf]).unsqueeze(0).cpu(),
            win=window2,
            update=True
        )

if __name__ == '__main__':
    train()


import matplotlib.pyplot as plt

a = input("Escribe el numero de personas que usan vehiculo: \n")
A = int(a)

while A < 0:
    print("¡Ha escrito un número negativo! Inténtelo de nuevo")
    a = input("Escribe el numero de personas que usan vehiculo: \n")
    A = int(a)

b = input("Escribe el numero de personas que usan motocicleta: \n")
B = int(b)

while B < 0:
    print("¡Ha escrito un número negativo! Inténtelo de nuevo")
    b = input("Escribe el numero de personas que usan motocicletas: \n")
    B = int(b)

c = input("Escribe el numero de personas que usan bicicletas: \n")
C = int(c)

while C < 0:
    print("¡Ha escrito un número negativo! Inténtelo de nuevo")
    c = input("Escribe el numero de personas que usan bicicletas: \n")
    C = int(c)

d = input("Escribe el numero de personas que usan el metro: \n")
D = int(d)

while D < 0:
    print("¡Ha escrito un número negativo! Inténtelo de nuevo")
    d = input("Escribe el numero de personas que usan el metro: \n")
    D = int(d)

Dato1 = float(A/(A+B+C+D)*100)
Dato2 = float(B/(A+B+C+D)*100)
Dato3 = float(C/(A+B+C+D)*100)
Dato4 = float(D/(A+B+C+D)*100)
print(Dato1, Dato2, Dato3, Dato4)
medios_transporte= "Vehiculo", "Motocicleta", "Bicicleta", "Metro"
sizes= [Dato1, Dato2,Dato3, Dato4]

fig1, ax1 =plt.subplots()
ax1.pie(sizes, labels=medios_transporte, autopct='%1.1f%%', shadow=True, startangle=90)

ax1.axis('equal')

plt.title("Principal Medio de transporte")
plt.legend()
plt.savefig('Grafica_pastel.png')
plt.show()


import RPi.GPIO as GPIO
from time import sleep

class ServoMotor:
	def __init__(self):
		self.pin = 3

	def setAngle(self, angle):
		GPIO.setmode(GPIO.BOARD)
		GPIO.setup(self.pin, GPIO.OUT)
		pwm=GPIO.PWM(self.pin, 50)
		#
		pwm.start(0)
		duty = (angle+90) / 18 + 2
		GPIO.output(self.pin, True)
		pwm.ChangeDutyCycle(duty)
		sleep(1)
		GPIO.output(self.pin, False)
		pwm.ChangeDutyCycle(0)
		# Clean up
		pwm.stop()
		GPIO.cleanup()


#!/usr/bin/Python2
# --coding:utf-8--

import unicodedata, re, os
from bs4 import BeautifulSoup
from bing_search_api import BingSearchAPI

def bing_search_total(_verbose, _search_phrase, _bing_api_key):

    _search_phrase_parsed = "%22" + _search_phrase.replace(' ', '+').strip(' ') + "%22" # %22 acts as quotes, facilitating a phrase search
    _bing_search = BingSearchAPI(_bing_api_key)
    _bing_parameters = {'$format': 'json', '$top': 2}

    try:
        res = _bing_search.search('web', _search_phrase_parsed, _bing_parameters).json()
        total_search_results = res["d"]["results"][0]["WebTotal"]
        total = int(total_search_results)
        if(isinstance(total, int)):
            if _verbose:
                print('\t' + _search_phrase_parsed.replace('+', ' ').replace('%22', '') + total)
                pass
            return total
    except Exception as e:
        if _verbose:
            print('\tERROR: in bing.search() - search total\n\t' + str(e))
        print('\tERROR: in bing.search() - search total\n\t' + str(e))
        print("[Errno {0}] {1}".format(e.errno, e.strerror))
        return 0
# Test
#bing_search_total(True, "Natural Language Processing", "U5Px9AJDGqcMOJVfwZLFa5GfCHi1e6DLwCl+0tt/NX4")