from pyspark import SparkConf
from pyspark.context import SparkContext
from pyspark.mllib.recommendation import ALS, Rating
import random
import itertools
import csv
import time
import math
import sys

start = time.time()
sc = SparkContext.getOrCreate(SparkConf().setMaster("local[*]"))

train_file_path = str(sys.argv[1])
test_file_path = str(sys.argv[2])
case_id = int(sys.argv[3])
output_file = str(sys.argv[4])

trainfile = sc.textFile(train_file_path)
trainfile = trainfile.map(lambda row: row.split(",")).filter(lambda a: a[0]!= "user_id")

testfile = sc.textFile(test_file_path)
testfile = testfile.map(lambda row: row.split(",")).filter(lambda a: a[0]!= "user_id")

def normalize(x):
    prediction = x[1][1]
    if(prediction < 1):
        prediction = 1.0
    elif(prediction > 5):
        prediction = 5
    y = [x[0], (x[1][0], prediction)]
    return y

def find_weights(tuples):
    active_user = tuples[0]
    active_business = tuples[1]
    other_users = tuples[2]

    user_weights = [] #Weights between active user and all other users
    active_user_businesses = businesses_per_user[active_user]

    for eachUser in other_users:
        businesses_for_user_i = businesses_per_user[eachUser]
        common = active_user_businesses.intersection(businesses_for_user_i)
        if len(common) == 0:
            user_weights.append([eachUser, 0, user_business_rating[eachUser][active_business]])
            continue

        ratings_active_user = []
        for eachBusiness in common:
            ratings_active_user.append(user_business_rating[active_user][eachBusiness])

        ratings_other_user = []
        for eachBusiness in common:
            ratings_other_user.append(user_business_rating[eachUser][eachBusiness])

        average_rating_active_user = sum(ratings_active_user)/ len(ratings_active_user)
        average_rating_other_user = sum(ratings_other_user)/ len(ratings_other_user)

        normalised_rating_active_user =[]
        for eachRating in ratings_active_user:
            normalised_rating_active_user.append(eachRating-average_rating_active_user)

        normalised_rating_other_user =[]
        for eachRating in ratings_other_user:
            normalised_rating_other_user.append(eachRating-average_rating_other_user)

        den1=[]
        for eachRating in normalised_rating_active_user:
            den1.append(eachRating*eachRating)

        den= pow(sum(den1),0.5)

        den2=[]
        for eachRating in normalised_rating_other_user:
            den2.append(eachRating*eachRating)

        den = den * pow(sum(den2),0.5)

        num1=[]
        for i in range(len(normalised_rating_other_user)):
            num1.append(normalised_rating_active_user[i] - normalised_rating_other_user[i])
        num = sum(num1)

        weight = 0
        if(den!=0):
            weight = round((num/den),2)

        user_weights.append([eachUser, weight, user_business_rating[eachUser][active_business]])

    return [(active_user, active_business), user_weights]

def find_P(tuples):
    active_user = tuples[0][0]
    active_business = tuples[0][1]
    weights = tuples[1]

    average_active_user = (user_avg_dict[active_user][0]) / user_avg_dict[active_user][1]

    num = 0
    den = 0
    for eachWeight in weights:
        average_for_this_user = user_avg_dict[eachWeight[0]][0]
        rating_for_this_user = user_business_rating[eachWeight[0]][active_business]

        temp = (average_for_this_user - rating_for_this_user) / user_avg_dict[eachWeight[0]][1] - 1
        num += eachWeight[1] * (eachWeight[2] - temp)
        den += abs(eachWeight[1])

    if(den != 0):
        prediction = average_active_user + num / den
    else:
        prediction = average_active_user

    return [(active_user), {active_business: prediction}]

def find_weights_lsh(tuples):

    active_user = tuples[0]
    active_business = tuples[1]
    other_businesses = tuples[2]

    if(active_business in candidates):
        other_businesses = candidates[active_business]

    business_weights = []
    users_for_active_business = users_per_business[active_business]

    sum_rating_active_business =[]
    for eachUser in users_for_active_business:
        sum_rating_active_business.append(user_business_rating[active_business][eachUser])

    average_rating_active_business = sum(sum_rating_active_business) / len(sum_rating_active_business)

    for eachBusiness in other_businesses:
        users_for_this_business = users_per_business[eachBusiness]
        common_users = users_for_this_business.intersection(users_for_active_business)

        if len(common_users) == 0:
            if(active_user in user_business_rating[eachBusiness]):
                business_weights.append([eachBusiness, 0, user_business_rating[eachBusiness][active_user]])
            else:
                business_weights.append([eachBusiness, 0, business_avg_dict[eachBusiness][0]/business_avg_dict[eachBusiness][1]])
            continue

        active_business_ratings =[]
        for eachUser in common_users:
            active_business_ratings.append(user_business_rating[active_business][eachUser])

        other_business_ratings =[]
        for eachUser in common_users:
            other_business_ratings.append(user_business_rating[eachBusiness][eachUser])

        other_business_average_rating_list =[]
        for eachUser in users_for_this_business:
            other_business_average_rating_list.append(user_business_rating[eachBusiness][eachUser])

        other_business_average_rating = sum(other_business_average_rating_list) / len(other_business_average_rating_list)

        normalised_rating_active_business =[]
        for eachRating in active_business_ratings:
            normalised_rating_active_business.append(eachRating - average_rating_active_business)

        normalised_rating_other_business =[]
        for eachRating in other_business_ratings:
            normalised_rating_other_business.append(eachRating - other_business_average_rating)

        den1=[]
        for eachRating in normalised_rating_active_business:
            den1.append(eachRating*eachRating)

        den= pow(sum(den1),0.5)

        den2=[]
        for eachRating in normalised_rating_other_business:
            den2.append(eachRating*eachRating)

        den = den * pow(sum(den2),0.5)

        num1=[]
        for i in range(len(normalised_rating_other_business)):
            num1.append(normalised_rating_active_business[i] - normalised_rating_other_business[i])
        num = sum(num1)

        weight = 0

        if(den!=0):
            weight = num/den

        if (active_user in user_business_rating[eachBusiness]):
            business_weights.append([eachBusiness, weight, user_business_rating[eachBusiness][active_user]])
        else:
            business_weights.append([eachBusiness, weight, business_avg_dict[eachBusiness][0]/float(business_avg_dict[eachBusiness][1])])

    return [(active_user, active_business), business_weights]

def find_P_lsh(tuples):
    active_user = tuples[0][0]
    active_business = tuples[0][1]
    weights = tuples[1]

    num = 0
    den = 0
    for eachWeight in weights:
        if(active_user not in user_business_rating[eachWeight[0]]):
            return [(active_user), {active_business: business_avg_dict[active_business][0]/float(business_avg_dict[active_business][1])}]
        num += eachWeight[1] * eachWeight[2]
        den += abs(eachWeight[1])

    prediction = business_avg_dict[active_business][0]/float(business_avg_dict[active_business][1])

    if (den != 0):
        prediction = num / den

    return [(active_user), {active_business: prediction}]

def Signature_Matrix(businessId_userList):
    businessId_hashedUserList = []
    for i in hashes:
        a = i[0]
        b = i[1]
        p = i[2]
        temp =[]
        for j in businessId_userList[1]:
            temp.append(((a * int(j) + b) % p) % mod)
        businessId_hashedUserList.append(min(temp))
    return businessId_hashedUserList

def DivideInBands(Users_each_business):
    bandedElements =[]
    for i in range(bands):
        bandedElements.append(((i, tuple(Users_each_business[1][i * rows:(i + 1) * rows])), [Users_each_business[0]]))
    return bandedElements

def FindCandidates(bucket):
    allPossiblePairsInBucket = []

    sortedbucket = sorted(list(bucket[1]))

    for i in range(len(bucket[1])):
        for j in range(i + 1, len(bucket[1])):
            allPossiblePairsInBucket.append(((sortedbucket[i], sortedbucket[j]), 1))
    return allPossiblePairsInBucket

if(case_id==1):
    train_users = trainfile.map(lambda x: (x[0], 1)).reduceByKey(lambda x, y: x).map(lambda x: x[0])
    train_businesses = trainfile.map(lambda x: (x[1], 1)).reduceByKey(lambda x, y: x).map(lambda x: x[0])

    test_users = testfile.map(lambda x: (x[0], 1)).reduceByKey(lambda x, y: x).map(lambda x: x[0])
    test_businesses = testfile.map(lambda x: (x[1], 1)).reduceByKey(lambda x, y: x).map(lambda x: x[0])

    all_users = train_users.union(test_users).distinct()
    all_businesses = train_businesses.union(test_businesses).distinct()

    users = all_users.collect()
    businesses = all_businesses.collect()
    business_dict1 = {}
    business_dict2 = {}
    for i, k in enumerate(businesses):
        business_dict1[k] = i
        business_dict2[i] = k

    user_dict1 = {}
    user_dict2 = {}
    for i, k in enumerate(users):
        user_dict1[k] = i
        user_dict2[i] = k

    train_ratings = trainfile.map(lambda x: (user_dict1[x[0]], business_dict1[x[1]], float(x[2])))

    rank = 3
    numIterations = 10
    model = ALS.train(train_ratings, rank, numIterations)

    test_ratings = testfile.map(lambda x: (user_dict1[x[0]], business_dict1[x[1]], x[2]))
    test_data = test_ratings.map(lambda x: (x[0], x[1]))

    prediction = model.predictAll(test_data).map(lambda r: ((r[0], r[1]), r[2]))
    test_ratings_predictions = test_ratings.map(lambda r: ((r[0], r[1]), r[2])).join(prediction)
    test_ratings_predictions = test_ratings_predictions.map(normalize)

    ratings = test_ratings_predictions.collect()

    f = open(output_file, "w")
    f.write("user_id, business_id, prediction\n")

    for i in ratings:
        f.write(user_dict2[i[0][0]] + "," + business_dict2[i[0][1]] + "," + str(i[1][1]) + "\n")

    mean_squared = test_ratings_predictions.map(lambda x: pow(float(x[1][0]) - float(x[1][1]), 2)).mean()
    print("RMSE : " + str(pow(mean_squared, 0.5)))
    end = time.time()
    duration = end - start
    print("Duration : " + str(duration))

elif(case_id==2):
    businesses_rdd = trainfile.map(lambda x: (x[1], 1)).reduceByKey(lambda x, y: x).map(lambda x: x[0])
    businesses = set(businesses_rdd.collect())

    user_business_rating_rdd = trainfile.map(lambda x: (x[0], {x[1]: float(x[2])}))
    user_business_rating_rdd = user_business_rating_rdd.reduceByKey(lambda x, y: {**x, **y})
    user_business_rating = dict(user_business_rating_rdd.collect())

    businesses_per_user_rdd = trainfile.map(lambda row: (row[0], {row[1]})).reduceByKey(lambda x, y: x | y)
    businesses_per_user = dict(businesses_per_user_rdd.collect())

    user_avg = trainfile.map(lambda row: (row[0], (float(row[2]), 1))).reduceByKey(lambda x, y: (x[0] + y[0], x[1] + y[1]))
    user_avg_dict = dict(user_avg.collect())

    users_per_business_rdd = trainfile.map(lambda row: (row[1], {row[0]})).reduceByKey(lambda a, b: a | b)
    users_per_business = dict(users_per_business_rdd.collect())

    test_business = testfile.map(lambda row: (row[0], row[1]))
    test_business_filter = test_business.filter(lambda x: x[1] in businesses)

    test_users = test_business_filter.map(lambda row: (row[0], row[1], users_per_business[row[1]]))

    users_weights = test_users.map(find_weights)

    predicted_ratings_rdd = users_weights.map(find_P).reduceByKey(lambda x, y: {**x, **y})
    predicted_ratings = dict(predicted_ratings_rdd.collect())

    test_user_business_rating_rdd = testfile.map(lambda x: (x[0], {x[1]: float(x[2])})).reduceByKey(lambda x, y: {**x, **y})
    test_user_business_rating = dict(test_user_business_rating_rdd.collect())

    error = 0
    count = 0

    f = open(output_file, "w")
    f.write("user_id, business_id, prediction\n")
    for user in test_user_business_rating:
        for business in test_user_business_rating[user]:
            count += 1
            if user in predicted_ratings and business in predicted_ratings[user]:
                f.write(user + "," + business + "," + str(predicted_ratings[user][business]) + "\n")
                diff = predicted_ratings[user][business] - test_user_business_rating[user][business]
                error += diff * diff
    print("RMSE : ", pow((error / count), 0.5))
    f.close()
    end = time.time()
    duration = end - start
    print("Duration : " + str(duration))

elif(case_id==3):
    Users_Businesses = trainfile.map(lambda row: (row[0],row[1]))
    Users = Users_Businesses.map(lambda a : a[0]).distinct().collect()
    Users.sort()
    Businesses = Users_Businesses.map(lambda a : a[1]).distinct().collect()

    User_dictionary = {}
    for i,k in enumerate(Users):
        User_dictionary[k] = i
    mod = len(User_dictionary)

    Business_dictionary1 = dict()
    for i,k in enumerate(Businesses):
        Business_dictionary1[k] = i

    Users_per_business = Users_Businesses.map(lambda row: (row[1], {User_dictionary[row[0]]})).reduceByKey(lambda x, y: x | y)
    Users_per_business_list = Users_per_business.collect()
    Users_per_business_dict = dict(Users_per_business_list)

    hashes = [[31, 67, 6151], [911, 781, 24593], [14, 23, 769], [387, 552, 98317], [3, 87, 193], [17, 91, 1543],
                  [189, 37, 3079], [14, 53, 6299], [3, 79, 53],
                  [91, 29, 12658], [53, 803, 49157], [8, 109, 389],
                  [443, 487, 899613], [14, 67, 499], [21, 91, 7691],
                  [17, 27, 937], [31, 79, 693],[7, 21, 419], [29, 51, 7691], [19, 19, 11923]]

    signature_matrix_rdd = Users_per_business.map(lambda x: (x[0], Signature_Matrix(x)))

    n = len(hashes)
    bands = 10
    rows = int(n / bands)
    # Dividing into b bands
    candidate_pairs = signature_matrix_rdd.flatMap(DivideInBands).reduceByKey(lambda x, y: x+y).filter(lambda x: len(x[1]) > 1)
    #for x in signature_matrix_rdd.take(2):
    #    print(x)
    #for x in candidate_pairs.take(2):
    #    print(x)
    # Finding all possible pairs
    candidate_pairs = candidate_pairs.flatMap(FindCandidates).reduceByKey(lambda x, y: x).map(lambda x: x[0])
    #candidate_pairs = candidate_pairs.collect()

    User_Business_dict = candidate_pairs.map(lambda x: (x[0], {x[1]}))
    Business_User_dict = candidate_pairs.map(lambda x: (x[1], {x[0]}))

    candidates = User_Business_dict.union(Business_User_dict)
    candidates = dict(candidates.reduceByKey(lambda x, y: x | y).collect())

    business_avg = trainfile.map(lambda x: (x[1], (float(x[2]), 1))).reduceByKey(lambda x, y: (x[0] + y[0], x[1] + y[1]))
    business_avg_dict = dict(business_avg.collect())

    businesses_per_user = Users_Businesses.map(lambda x: (x[0], {x[1]})).reduceByKey(lambda x, y: x | y)
    businesses_per_user = dict(businesses_per_user.collect())

    users_per_business = Users_Businesses.map(lambda x: (x[1], {x[0]})).reduceByKey(lambda x, y: x | y)
    users_per_business = dict(users_per_business.collect())

    user_business_rating_rdd = trainfile.map(lambda x: (x[1], {x[0]: float(x[2])})).reduceByKey(lambda x, y: {**x, **y})
    user_business_rating = dict(user_business_rating_rdd.collect())

    test_business_filter = testfile.map(lambda row: (row[0],row[1])).filter(lambda x: x[1] in Businesses)
    test_businesses = test_business_filter.map(lambda row: (row[0], row[1], businesses_per_user[row[0]]))

    business_weights = test_businesses.map(find_weights_lsh)

    predicted_ratings_rdd = business_weights.map(find_P_lsh).reduceByKey(lambda x, y: {**x, **y})
    predicted_ratings = dict(predicted_ratings_rdd.collect())

    test_user_business_rating_rdd = testfile.map(lambda x: (x[0], {x[1]: float(x[2])})).reduceByKey(lambda x, y: {**x, **y})
    test_user_business_rating = dict(test_user_business_rating_rdd.collect())

    error = 0
    count = 0

    f = open(output_file, "w")
    f.write("user_id, business_id, prediction\n")
    for eachUser in test_user_business_rating:
        for eachBusiness in test_user_business_rating[eachUser]:
            count += 1
            if eachUser in predicted_ratings and eachBusiness in predicted_ratings[eachUser]:
                f.write(eachUser + "," + eachBusiness + "," + str(predicted_ratings[eachUser][eachBusiness]) + "\n")
                diff = predicted_ratings[eachUser][eachBusiness] - test_user_business_rating[eachUser][eachBusiness]
                error += diff * diff

    print("RMSE : " + str(pow((error / count), 0.5)))
    f.close()
    end = time.time()
    duration = end - start
    print("Duration : " + str(duration))


import re
import collections

class LexerError(Exception):
    pass

Token = collections.namedtuple('Token', ['type', 'data'])

class Lexer:
    ALPHA = b'abcdefghijklmnopqrstuvwxyz'
    NUM = b'0123456789'
    SYMB = b" !&|?();[],_+-*/%^=<>:@{}`'~#"

    def __init__(self, src):
        self.cache = []
        self.idx = 0
        self.src = src
        self._preprocess()

    def preprocessed_source(self):
        return self.src

    def has_token(self):
        # Newlines only seperate tokens, just ignore.
        while self._hasc() and self._peekc() == b'\n':
            self.idx += 1
        return bool(self.cache or self._hasc())

    def peek_token(self, ahead=0):
        while len(self.cache) <= ahead:
            self.cache.append(self.get_token(ignore_cache=True))
        return self.cache[ahead]

    def get_token(self, *args, ignore_cache=False):
        if self.cache and not ignore_cache:
            return self.cache.pop(0)

        # Newlines only seperate tokens, just ignore.
        while self._hasc() and self._peekc() == b'\n':
            self.idx += 1

        if not self._hasc():
            raise LexerError('expected character, found EOF')

        c = self._getc()
        if c.lower() in self.ALPHA + self.SYMB:
            return Token('symb', c.decode('utf-8'))

        if c == b'$':
            c = self._getc()
            if c.lower() not in self.ALPHA + self.SYMB + b'$':
                raise LexerError("expected alphanumeric or symbol after '$'")
            return Token('symb', "$" + c.decode('utf-8'))

        if c in b'"\\':
            data = self._getc() if c == b'\\' else self._tok_str()
            return Token('lit', repr(data.decode('utf-8')))

        if c in self.NUM or (c == b'.' and self._peekc() in self.NUM):
            self.idx -= 1  # Push back '.' on character stream.
            return Token('lit', self._tok_num())

        if c == b'.':
            return self._tok_dot()

        raise LexerError(
            'unexpected character while parsing tokens: {:x}'.format(c[0]))

    def _tok_dot(self):
        if self.idx >= len(self.src):
            raise LexerError("expected character after '.', found EOF")

        c = self._getc()
        if c == b'"':
            return Token('lit', repr(list(self._tok_str())))

        return Token('symb', (b'.' + c).decode('utf-8'))

    def _tok_str(self):
        s = b''

        while self._hasc():
            c = self._getc()
            if c == b'"':
                break

            # Handle escape sequences.
            if c == b'\\' and self._hasc() and self._peekc() in b'"\\':
                s += self._getc()
            else:
                s += c

        return s

    def _tok_num(self):
        n = b''

        # Leading zeroes are seperate tokens (in a golf language a leading zero
        # is never useful):
        if self._peekc() == b'0':
            n += self._getc()
            if self._hasc() and self._peekc() in b'.':
                n += self._getc()
        else:
            while self._hasc() and self._peekc() in b'.' + self.NUM:
                if self._peekc() == b'.' and n.count(b'.') != 0:
                    break

                n += self._getc()

        if n.endswith(b'.') and self._hasc() and self._peekc() not in b' \n':
            self.idx -= 1

        return n.decode('utf-8')

    def _hasc(self):
        return self.idx < len(self.src)

    def _peekc(self):
        return self.src[self.idx:self.idx+1]

    def _getc(self):
        self.idx += 1
        return self.src[self.idx-1:self.idx]

    def _preprocess(self):
        # Finite state machine.
        binstring = False
        string = False

        # Meta command results.
        end_meta = None

        lines = [b'']
        while self._hasc():
            c = self._getc()

            # Don't normalize anything in binary strings.
            if binstring:
                lines[-1] += c

                if c == b'\\':
                    lines[-1] += self._getc()
                elif c == b'"':
                    binstring = False

            # Normalize newline.
            elif c in b'\r\n':
                if string:
                    lines[-1] += b'\n'
                else:
                    lines.append(b'')

                # Greedily read \r\n.
                if c == b'\r' and self._peekc() == b'\n':
                    self.idx += 1

            # Handle string state.
            elif string:
                lines[-1] += c

                if c == b'\\' and self._peekc() == b'"':
                    lines[-1] += self._getc()
                elif c == b'"':
                    string = False

            # The default state.
            else:
                # Comments.
                if c == b';' and (not lines[-1] or lines[-1][-1] in b' \t'):
                    # Read until newline.
                    comment = b''
                    while self._hasc():
                        c = self._getc()

                        # Greedily read \r\n, and break if found.
                        if c == b'\r' and self._peekc() == b'\n':
                            self.idx += 1
                        if c in b'\r\n':
                            lines.append(b'')
                            break

                        comment += c

                    # Meta-command.
                    if comment.startswith(b'#'):
                        meta = comment[1:].strip()
                        if meta == b'end' and end_meta is None:
                            end_meta = len(lines) - 1

                # Regular characters.
                else:
                    lines[-1] += c

                    if c == b'"':
                        string = True
                    elif c == b'.' and self._peekc() == b'"':
                        lines[-1] += self._getc()
                        binstring = True
                    elif c == b'\\':
                        if self._hasc():
                            c = self._getc()

                            # Greedily read \r\n.
                            if c == b'\r' and self._peekc() == b'\n':
                                self.idx += 1

                            lines[-1] += b'\n' if c in b'\r\n' else c

        # Handle the end metacommand.
        if end_meta is not None:
            lines = lines[:end_meta]

        self.src = self._preprocess_whitespace(lines)
        self.idx = 0

    def _preprocess_whitespace(self, lines):
        # Strip all trailing whitespace and an even amount of spaces from the
        # beginning.
        lines = [re.sub(b'^((  )|\t)*', b'', line.rstrip()) for line in lines]

        # Remove empty lines.
        lines = [line for line in lines if line.strip()]

        # Concatenate lines, unless a line ends in a number or period and the
        # next line begins in a number (the only time a newline is necessary).
        linenr = 0
        while linenr + 1 < len(lines):
            if not (lines[linenr][-1] in b'.0123456789' and
                    lines[linenr + 1][:1].isdigit()):
                lines[linenr] += lines.pop(linenr + 1)
            else:
                linenr += 1

        return b'\n'.join(lines)


from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.shortcuts import render, get_object_or_404, redirect
from django.utils.text import slugify

from . models import (
	Category,
	SubCategory,
	Prod,
	ProdMedia,
	RequestProd,
	)

from . forms import (
	CreateCategoryForm,
	CreateSubCategoryForm,
	CreateProdForm,
	CreateProdMediaForm,
	)
"""

"""

# def categoryProds(param=None):
# 	if param

def index(request):
	template_name = './main/index.html'
	specials = Prod.objects.filter(special=True)
	subCategories = SubCategory.objects.all()
	hotDeals = Prod.objects.filter(hotDeal=True)
	newArrivals = Prod.objects.all().order_by('-pk')[:8]
	topSellings = Prod.objects.all()[:20]
	prods = Prod.objects.all()[:12]
	context = {
		'subCategories':subCategories,
		'hotDeals':hotDeals,
		'specials':specials,
		'prods':prods,
		'newArrivals':newArrivals,
		'topSellings':topSellings,
		}
	return render(request, template_name, context)

def shop(request, subCategorySlug):
	template_name = './main/shop.html'
	category = request.GET.get('category')
	cat = False
	if category:
		#category = get_object_or_404(Category, name=name)
		prods = Prod.objects.filter(subCategory__category__name=category)
		cat = True
	else:
		subCategory = get_object_or_404(SubCategory, slug=subCategorySlug)
		prods = subCategory.prod_set.all().order_by('-pk')
	# subCategories = subCategory.category.subcategory_set.exclude(slug=subCategorySlug)
	hotDeals = Prod.objects.filter(hotDeal=True)
	context = {'prods':prods, 'hotDeals':hotDeals, 'cat':cat}
	return render(request, template_name, context)

@login_required
def prods(request):
	template_name = './main/prods.html'
	prods = Prod.objects.all().order_by('-pk')[:20]
	search = request.GET.get('subCategory')
	subCategories = SubCategory.objects.all()
	if search:
		subCategory = get_object_or_404(SubCategory, slug=search)
		prods = Prod.objects.filter(subCategory=subCategory)
	context = {'prods':prods, 'subCategories':subCategories}
	return render(request, template_name, context)

def prodDetail(request, prodSlug):
	template_name = './main/prodDetail.html'
	prod = get_object_or_404(Prod, slug=prodSlug)
	relatedProds = prod.subCategory.prod_set.all()
	context = {'prod':prod, 'relatedProds':relatedProds}
	return render(request, template_name, context)

def search(request):
	template_name = './main/search.html'
	param = request.GET.get('search')
	noResult = False
	send = False
	prods = Prod.objects.all()
	pop = False
	filterSet = []
	if param:
		#Giving preferences to names first

		filterSet.extend(prods.filter(
			Q(name__icontains=param)
			).distinct()
			)
		filterSet.extend(prods.filter(
			Q(keywords__icontains=param)|
			Q(subCategory__name__icontains=param)|
			Q(subCategory__category__name__icontains=param)
			).distinct()
			)

	if len(filterSet) == 0:
		noResult = False
		pop = True

	if request.method == "POST":
		prodName = request.POST.get('product')
		phoneNumber = request.POST.get('phoneNumber')
		requestProd = RequestProd(name=prodName)
		if phoneNumber:
			requestProd.phoneNumber = int(phoneNumber)
		requestProd.save()
		noResult = False
		send = True

	context = {'prods':filterSet, 'noResult':noResult, 'send':send, 'pop':pop}
	return render(request, template_name, context)
"""
Category section
"""
#Will display together with Subcategories
@login_required
def categories(request):
	if request.user.is_superuser:
		template_name = './categories.html'
		categories = Category.objects.all()
		context = {'categories':categories}
		return render(request, template_name, context)
	else:
		return redirect('product:noPerm')

@login_required
def createCategory(request):
	if request.user.is_superuser:
		template_name = './form/createCategory.html'
		if request.method == "POST":
			form = CreateCategoryForm(request.POST)
			if form.is_valid():
				newForm = form.save(commit=False)
				newForm.slug = slugify(newForm.name)
				newForm.save()
				return redirect("product:categories")
		else:
			form = CreateCategoryForm()

		context = {"form":form}
		return render(request, template_name, context)
	else:
		return redirect('product:noPerm')

@login_required
def updateCategory(request, categorySlug):
	if request.user.is_superuser:
		template_name = './form/updateCategory.html'
		category = get_object_or_404(Category, slug=categorySlug)
		if request.method == "POST":
			form = CreateCategoryForm(request.POST, request.FILES, instance=category)
			if form.is_valid():
				newForm = form.save()
				newForm.slug = slugify(newForm.name)
				newForm.save()
				return redirect('product:categories')
		else:
			form = CreateCategoryForm(instance=category)
		context = {'form':form}
		return render(request, template_name, context)
	else:
		return redirect('product:noPerm')

@login_required
def deleteCategory(request, categorySlug):
	if request.user.is_superuser:
		category = get_object_or_404(Category, slug=categorySlug)
		category.delete()
		return redirect('product:categories')
	else:
		return redirect('product:noPerm')

"""
Subcategory section
"""
@login_required

@login_required
def createSubCategory(request):
	if request.user.is_superuser:
		template_name = './form/createSubCategory.html'
		if request.method == "POST":
			form = CreateSubCategoryForm(request.POST, request.FILES)
			if form.is_valid():
				newForm = form.save(commit=False)
				newForm.slug = slugify(newForm.name)
				newForm.save()
				return redirect('product:categories')
		else:
			form = CreateSubCategoryForm()
		context = {"form":form}
		return render(request, template_name, context)
	else:
		return redirect('product:noPerm')

@login_required
def updateSubCategory(request, subCategorySlug):
	if request.user.is_superuser:
		template_name = './form/updateSubCategory.html'
		subCategory = get_object_or_404(Subcategory, slug=subCategorySlug)
		if request.method == "POST":
			form = CreateSubCategoryForm(request.POST, request.FILES, instance=subCategory)
			if form.is_valid():
				newForm = form.save(commit=False)
				newForm.slug = slugify(newForm.name)
				newForm.save()
				return redirect('product:categories')
		else:
			form = CreateSubCategoryForm(instance=subCategory)
		context = {'form':form}
		return render(request, template_name, context)
	else:
		return redirect('product:noPerm')

@login_required
def deleteSubCategory(request, subCategorySlug):
	if request.user.is_superuser:
		subCategory = get_object_or_404(Subcategory, slug=subCategorySlug)
		subCategory.delete()
		return redirect('product:categories')
	else:
		return redirect("product:noPerm")

"""
Product section
"""

@login_required
def createProd(request):
	if request.user.is_superuser:
		template_name = './form/createProd.html'
		success = False
		if request.method == "POST":
			form = CreateProdForm(request.POST)
			pic1 = request.FILES.get('pic1', None)
			pic2 = request.FILES.get('pic2', None)
			pic3 = request.FILES.get('pic3', None)
			if form.is_valid():
				newForm = form.save(commit=False)
				last = Prod.objects.last()
				slug = "{0} {1}".format(newForm.name, last.id)
				newForm.slug = slugify(slug)
				try:
					origPrice = form.cleaned_data['origPrice']
					price = form.cleaned_data['price']
					newForm.discount = (origPrice - price)/origPrice *100
				except:
					None
				newForm.save()
				if pic1:
					media1 = ProdMedia(prod=newForm, pic=pic1)
					media1.save()

				if pic2:
					media2 = ProdMedia(prod=newForm, pic=pic2)
					media2.save()

				if pic3:
					media3 = ProdMedia(prod=newForm, pic=pic3)
					media3.save()
				success = True
		else:
			form = CreateProdForm()

		context = {"form":form, 'success':success}
		return render(request, template_name, context)
	else:
		return redirect('product:noPerm')

@login_required
def updateProd(request, prodSlug):
	if request.user.is_superuser:
		template_name = './form/updateProd.html'
		prod = get_object_or_404(Prod, slug=prodSlug)
		success = False
		if request.method == "POST":
			form = CreateProdForm(request.POST, instance=prod)
			pic1 = request.FILES.get('pic1', None)
			pic2 = request.FILES.get('pic2', None)
			pic3 = request.FILES.get('pic3', None)
			if form.is_valid():
				newForm = form.save(commit=False)
				last = Prod.objects.last()
				slug = "{0} {1}".format(newForm.name, last.id)
				newForm.slug = slugify(slug)
				try:
					origPrice = form.cleaned_data['origPrice']
					price = form.cleaned_data['price']
					newForm.discount = (origPrice - price)/origPrice *100
				except:
					None
				newForm.save()
				if pic1:
					try:
						media1 = ProdMedia.objects.get(pk=1)
						media1.pic = pic1
					except:
						media1 = ProdMedia(prod=newForm, pic=pic1)
					media1.save()

				if pic2:
					try:
						media2 = ProdMedia.objects.get(pk=2)
						media2.pic = pic2
					except:
						media2 = ProdMedia(prod=newForm, pic=pic2)
					media2.save()
				if pic3:
					try:
						media3 = ProdMedia.objects.get(pk=3)
						media3.pic = pic3
					except:
						media3 = ProdMedia(prod=newForm, pic=pic3)
					media3.save()
				success = True
		else:
			form = CreateProdForm(instance=prod)

		context = {"form":form, 'success':success}
		return render(request, template_name, context)
	else:
		return redirect('product:noPerm')

@login_required
def deleteProd(request, prodSlug):
	if request.user.is_superuser:
		prod = get_object_or_404(Prod, slug=prodSlug)
		prod.delete()
		return redirect('product:prods')
	else:
		return redirect('product:noPerm')
"""
Prduct Media section
Does not have update if you want just delete the file and add another
"""

@login_required
def createProdMedia(request):
	if request.user.is_superuser:
		template_name = './form/createProdMediaCategory.html'
		if request.method == "POST":
			form = CreateProdMediaForm(request.FILES)
			if form.is_valid():
				form.save()
		else:
			form = CreateProdMediaForm()

		context = {"form":form}
		return render(request, template_name, context)
	else:
		return redirect('product:noPerm')

@login_required
def deleteProdMedia(request, prodMediaSlug):
	if request.user.is_superuser:
		prodMedia = get_object_or_404(ProdMedia, slug=prodMediaSlug)
		prodMedia.delete()
		return redirect('product:products')
	else:
		return redirect('product:noPerm')

def noPerm(request):
	template_name = './errors/noPerm.html'
	return render(request, template_name)

@login_required
def requestProd(request):
	template_name = './main/requestProd.html'
	requestProds = RequestProd.objects.all()

	if request.method == "POST":
		requestProdId = int(request.POST.get('id'))
		requestP = get_object_or_404(RequestProd, pk=requestProdId)
		requestP.delete()

	context= {'requestProds':requestProds}
	return render(request, template_name, context)


import argparse
import requests
import time
import xml.etree.ElementTree as ET

from apiclient import discovery
from apiclient import errors
from oauth2client.client import GoogleCredentials
import strict_rfc3339

#
# Query Massachusetts RMV wait times and publish to GCP Cloud Monitoring as custom metrics.
#

MA_RMV_WAIT_TIME_CUSTOM_METRIC_TYPE = "custom.googleapis.com/ma_rmv_wait_time"
MA_RMV_WAIT_TIME_API = "https://dotfeeds.state.ma.us/api/RMVBranchWaitTime/Index"

def get_client():
    """Builds an http client authenticated with the service account credentials."""
    credentials = GoogleCredentials.get_application_default()
    api_client = discovery.build('monitoring', 'v3', credentials=credentials)
    return api_client

def get_metric_descriptor(api_client, project_id):
    """Fetch the metric descriptor for the wait_time custom metric."""
    try:
        request = api_client.projects().metricDescriptors().get(
            name="projects/%s/metricDescriptors/%s" %
            (project_id, MA_RMV_WAIT_TIME_CUSTOM_METRIC_TYPE))
        response = request.execute()
        return response
    except errors.HttpError as e:
        print("Failed to get metric descriptor: %s" % e)
        return None

def create_metric_descriptor(api_client, project_id):
    """Create the metric descriptor for the wait_time custom metric."""
    md_definition = {
        "type": MA_RMV_WAIT_TIME_CUSTOM_METRIC_TYPE,
        "labels": [
            {
                "key": "branch",
                "value_type": "STRING",
                "description": "The RMV branch providing service."
            },
            {
                "key": "service",
                "value_type": "STRING",
                "description": "The type of RMV service.",
            },
            ],
        "metric_kind": "GAUGE",
        "value_type": "DOUBLE",
        "unit": "min",
        "display_name": "Massachusetts RMV Wait Time",
        "description": "The amount of time to wait for a RMV service by "
            "service type and branch.",
        }
    try:
        print("Creating metric descriptor.")
        request = api_client.projects().metricDescriptors().create(
            name="projects/%s" % project_id, body=md_definition)
        response = request.execute()
        # It can take a few seconds for a new MetricDescriptor to propagate
        # through the system.
        time.sleep(5)
        return response  # response is a MetricDescriptor
    except errors.HttpError as e:
        print("Failed to create metric descriptor: %s" % e)
        return None

def write_data_point(api_client, project_id,
                     branch, service, timestamp, wait_time):
    """Write a data point to the wait_time custom metric."""
    data_point_definition = {
        "time_series": [
            {
            "metric": {
                "type": MA_RMV_WAIT_TIME_CUSTOM_METRIC_TYPE,
                "labels": {
                    "branch": branch,
                    "service": service
                    },
                },
            "resource": {
                "type": "global",
                "labels": {
                    "project_id": project_id,
                    },
                },
            "points": [{
                "interval": {
                    "end_time": timestamp,
                    },
                "value": {
                    "double_value": wait_time,
                    },
                }],
            }]
         }

    try:
        request = api_client.projects().timeSeries().create(
            name="projects/%s" % project_id, body=data_point_definition)
        _ = request.execute()
    except errors.HttpError as e:
        print("Write failed: branch=%s, service=%s, error=%s" %
              (branch, service, e))

def query_wait_time(api_client, project_id, branch=None, service=None):
    """Query the last hour of data for a branch and service."""
    query_filter = 'resource.type="global" AND metric.type="%s"' % (
        MA_RMV_WAIT_TIME_CUSTOM_METRIC_TYPE)
    if branch is not None:
        query_filter = '%s AND metric.label.branch="%s"' % (query_filter, branch)
    if service is not None:
        query_filter = '%s AND metric.label.service="%s"' % (query_filter, service)

    now = int(time.time())
    end_rfc3339 = strict_rfc3339.timestamp_to_rfc3339_utcoffset(now)
    start_rfc3339 = strict_rfc3339.timestamp_to_rfc3339_utcoffset(now-60*60)

    request = api_client.projects().timeSeries().list(
        name="projects/%s" % project_id, filter=query_filter,
        interval_endTime=end_rfc3339, interval_startTime=start_rfc3339)
    response = request.execute()
    print("Query result: %s" % response)

def get_ma_rmv_wait_times():
    """Queries MA RMV wait time API and parses results.

    Returns a dict[<town>][<service_type>] = <time> with <time> reported
    in minutes as a float.
    """
    def parse_wait_time(time_val):
        if time_val in ['Unavailable', 'Error', 'Closed']:
            return 0
        vals = time_val.split(':')
        return int(vals[0])*60 + int(vals[1]) + float(vals[2])/60

    r = requests.get(MA_RMV_WAIT_TIME_API)
    rmv_waits = {}
    branches = ET.fromstring(r.text)
    for branch in branches:
        branch_info = {}
        for attr in branch:
            attr_value = attr.text
            if attr.tag in ["licensing", "registration", ]:
              attr_value = parse_wait_time(attr.text)
            branch_info[attr.tag] = attr_value
        rmv_waits[branch_info['town']] = branch_info
    return rmv_waits

def write_ma_rmv_wait_times(api_client, project_id,
                            ma_rmv_wait_times, timestamp):
    for branch, branch_info in ma_rmv_wait_times.items():
        for service in ["licensing", "registration", ]:
            write_data_point(api_client, project_id, branch, service,
                             timestamp, branch_info[service])

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--project_id", help="Project ID you want to access.", required=True)
    args = parser.parse_args()

    now = int(time.time())
    now_rfc3339 = strict_rfc3339.timestamp_to_rfc3339_utcoffset(now)

    print("Running: time=%s" % now_rfc3339)
    ma_rmv_wait_times = get_ma_rmv_wait_times()

    api_client = get_client()
    md = get_metric_descriptor(api_client, args.project_id)
    if md is None:
        print("Creating custom metric descriptor for MA RMV wait time.")
        md = create_metric_descriptor(api_client, args.project_id)
    write_ma_rmv_wait_times(api_client, args.project_id,
                            ma_rmv_wait_times, now_rfc3339)
    query_wait_time(api_client, args.project_id,
                    branch="Boston",  service="registration")


import numpy as np
import csv
from obspy import UTCDateTime, read, Trace, Stream
import subprocess
import time
import sys

# //////////////////////////////// Parametros //////////////////////////////////
tipoArchivo = sys.argv[1] #1:RC 2:EE
# /////////////////////////////////////////////////////////////////////////////

# ///////////////////////////////// Archivos //////////////////////////////////
archivoConfiguracionMseed = '/home/rsa/configuracion/configuracion_mseed.csv'
archivoNombresArchivosRC = '/home/rsa/tmp/NombreArchivoRegistroContinuo.tmp'
archivoDatosConfiguracion = '/home/rsa/configuracion/DatosConfiguracion.txt'
archivoNombresArchivosEE = '/home/rsa/tmp/NombreArchivoEventoExtraido.tmp'

if tipoArchivo=='1':
    #Archivos registro continuo
    ficheroConfiguracion = open(archivoDatosConfiguracion)
    ficheroNombresArchivos = open(archivoNombresArchivosRC)
    lineasFicheroConfiguracion = ficheroConfiguracion.readlines()
    lineasFicheroNombresArchivos = ficheroNombresArchivos.readlines()
    nombreArchvioRegistroContinuo = lineasFicheroNombresArchivos[1].rstrip('\n')
    archivo_binario = lineasFicheroConfiguracion[2].rstrip('\n') + nombreArchvioRegistroContinuo
    print('Convirtiendo el archivo: %s' %archivo_binario)
elif tipoArchivo=='2':
    #Archivos eventos extraidos
    ficheroNombresArchivos = open(archivoNombresArchivosEE)
    lineasFicheroNombresArchivos = ficheroNombresArchivos.readlines()
    archivo_binario = lineasFicheroNombresArchivos[0]
    print('Convirtiendo el archivo: %s' %archivo_binario)

# /////////////////////////////////////////////////////////////////////////////

def parametros_():
#################################################################
#Método de Extracción de datos de conficuración de la estacion
#Desde el archivo configuracion.csv que debe estar presente en el mismo directorio del ejecutable.

        estaciones_=[]
        with open(archivoConfiguracionMseed,newline='') as f:
            datos=csv.reader(f,delimiter=';',quotechar=';')
            for r in datos:
                estaciones_.append(r)
        nombre_canal_total_=estaciones_[1][1]
        nombre_canal=estaciones_[1][2]
        tipo_canal_=estaciones_[1][3]
        n_canales_=estaciones_[1][4]
        hab_canal=estaciones_[1][5]
        componente_canal=estaciones_[1][6]
        grafico_=estaciones_[1][7]
        hab_plt=estaciones_[1][8]
        gan_plt=estaciones_[1][9]
        ganancia=estaciones_[1][10]
        diez_plt=estaciones_[1][11]
        bits_=estaciones_[1][12]
        calidad_=estaciones_[1][13]
        ubicacion_=estaciones_[1][14]
        tipo_canal=estaciones_[1][15]
        red_=estaciones_[1][16]
        muestreo_=estaciones_[1][17]
        longitud_=estaciones_[1][18]
        latitud_=estaciones_[1][19]
        altura_=estaciones_[1][20]
        ruido_=int(estaciones_[1][21])
        numero_est=int(estaciones_[1][0])
        filtro_=estaciones_[1][22]
        reserva_1=estaciones_[1][23]
        reserva_2=estaciones_[1][24]

        return(nombre_canal_total_, #Canal 0  'NOMBRE'
               nombre_canal,        #Canal 1  'CODIGO'
               tipo_canal_,         #Canal 2  'SENSOR'
               n_canales_,          #Canal 3  'CANALES'
               hab_canal,           #Canal 4  'HAB_CANAL'
               componente_canal,    #Canal 5  'COMPONENTE'
               grafico_,            #Canal 6  'HAB_GRAFICO'
               hab_plt,             #Canal 7  'HAB_PLT'
               gan_plt,             #Canal 8  'GAN_PLT'
               ganancia,            #Canal 9  'GANANCIA'
               diez_plt,            #Canal 10 'DIEZ_PLT'
               bits_,               #Canal 11 'FACTOR_MUL'
               longitud_,           #Canal 12 'LONGITUD'
               latitud_,            #Canal 13 'LATITUD'
               altura_,             #Canal 14 'ALTITUD'
               ruido_,              #Canal 15 'RUIDO'
               calidad_,            #Canal 16 'CALIDAD'
               ubicacion_,          #Canal 17 'UBICACIÓN'
               tipo_canal,          #Canal 18 'CANAL'
               red_,                #Canal 19 'RED'
               muestreo_,           #Canal 20 'MUESTREO'
               numero_est,          #Canal 21 'N° ESTACION'
               filtro_,             #Canal 22 'FILTRO'
               reserva_1,           #Canal 22 'Reserva 1'
               reserva_2)           #Canal 22 'Reserva 2'

def obtenerTraza(nombreCanal,num_canal, data, anio, mes, dia, horas, minutos, segundos, microsegundos, segundos_faltantes=None):

    # Define todas las caracteristicas de la traza
    parametros=parametros_()
    nombreRed=parametros[19]
    nombreEstacion=parametros[1]
    tipoEstacion=parametros[2]
    localizacion=parametros[17]
    nCanal=parametros[18]
    fsample=int(parametros[20])
    calidad=parametros[16]
    if fsample>80:
        nombreCanal='E'
    else:
        nombreCanal='S'
    if tipoEstacion=='SISMICO':
        nombreCanal=nombreCanal+'L'
    else:
        nombreCanal=nombreCanal+'N'
    num_canal=num_canal-3*(int((num_canal-1)/3))
    nombreCanal=nombreCanal+nCanal[num_canal-1:num_canal]
    stats = {'network': nombreRed, 'station': nombreEstacion, 'location': localizacion,
             'channel': nombreCanal, 'npts': len(data), 'sampling_rate': fsample,
             'mseed': {'dataquality': calidad}}
    # Establece el tiempo
    stats['starttime'] = UTCDateTime(anio, mes, dia, horas, minutos, segundos, microsegundos)

    # Crea la traza con los datos y las características
    if segundos_faltantes is not None:

        segundo_inicio = (horas*3600)+(minutos*60)+segundos
        muestras_por_segundo = int(fsample)
        lista_ceros = np.zeros(muestras_por_segundo, dtype=np.int32)
        npts_completo = len(data) + int(len(segundos_faltantes) * muestras_por_segundo)
        data_completo = np.zeros(npts_completo, dtype=np.int32)
        data_completo = data.copy()
        stats['npts'] = npts_completo
        for segundo_faltante in segundos_faltantes:
            tiempo_muestra_faltante = int(segundo_faltante-segundo_inicio)
            indice_muestra_faltante = tiempo_muestra_faltante * muestras_por_segundo
            data_completo = np.insert(data_completo, indice_muestra_faltante, lista_ceros)

        traza = Trace(data=data_completo, header=stats)

    else:

        traza = Trace(data=data, header=stats)

    return traza

def lectura_archivo(archivo):
    datos = [[], [], []]
    contador = 0
    segundo_anterior = None
    segundos_faltantes = []

    with open(archivo, "rb") as f:
        while True:
            tramaDatos = np.fromfile(f, np.int8, 2506)
            if len(tramaDatos) != 2506:
                break

            hora = tramaDatos[2503]
            minuto = tramaDatos[2504]
            segundo = tramaDatos[2505]
            n_segundo = hora * 3600 + minuto * 60 + segundo

            if segundo_anterior is not None:
                if n_segundo != segundo_anterior + 1:
                    segundos_faltantes.extend(range(segundo_anterior + 1, n_segundo))
                elif n_segundo > segundo_anterior + 1:
                    # Si hay un salto mayor, se llenan los segundos faltantes intermedios
                    segundos_faltantes.extend(range(segundo_anterior + 1, n_segundo))

            segundo_anterior = n_segundo

            for j in range(0, 3):
                for i in range(0, 250):
                    dato_1 = tramaDatos[i * 10 + j * 3 + 1]
                    dato_2 = tramaDatos[i * 10 + j * 3 + 2]
                    dato_3 = tramaDatos[i * 10 + j * 3 + 3]
                    xValue = ((dato_1 << 12) & 0xFF000) + ((dato_2 << 4) & 0xFF0) + ((dato_3 >> 4) & 0xF)
                    if (xValue >= 0x80000):
                        xValue = xValue & 0x7FFFF
                        xValue = -1 * (((~xValue) + 1) & 0x7FFFF)
                    datos[j].append(int(xValue))

            contador += 1
            if contador == 864:
                contador = 0

    datos_np = np.asarray(datos)

    if segundos_faltantes:
        print("Segundos faltantes:", segundos_faltantes)

    return datos_np, segundos_faltantes if segundos_faltantes else None

def verificacion_archivo(archivo):
    f = open(archivo, "rb")
    tramaDatos = np.fromfile(f, np.int8, 2506)
    hora = tramaDatos[2503]
    minuto = tramaDatos[2504]
    segundo = tramaDatos[2505]
    n_segundo=hora*3600+minuto*60+segundo
    anio=tramaDatos[2500]
    anio_s= str(anio)
    mes=tramaDatos[2501]
    mes_s=str(mes)
    if(mes<10):
        mes_s='0'+mes_s
    dia=tramaDatos[2502]
    dia_s=str(dia)
    if(dia<10):
        dia_s='0'+dia_s
    hora_s=str(hora)
    if(hora<10):
        hora_s='0'+hora_s
    minuto_s=str(minuto)
    if(minuto<10):
        minuto_s='0'+minuto_s
    segundo_s=str(segundo)
    if(segundo<10):
        segundo_s='0'+segundo_s
    fecha_=(anio,mes,dia,hora,minuto,segundo,n_segundo),(anio_s,mes_s,dia_s,hora_s,minuto_s,segundo_s)
    f.close
    return(fecha_)

def nombre_mseed(tipoArchivo,nombre_,fecha_):
    anio_s=fecha_[1][0]
    mes_s=fecha_[1][1]
    dia_s=fecha_[1][2]
    hora_s=fecha_[1][3]
    minuto_s=fecha_[1][4]
    segundo_s=fecha_[1][5]
    fecha_string=anio_s+mes_s+dia_s
    hora_string=hora_s+minuto_s+segundo_s
    if tipoArchivo=='1':
        #Archivos registro continuo
        fileName = '/home/rsa/resultados/mseed/'+nombre_+fecha_string+"_"+hora_string+".mseed"
    elif tipoArchivo=='2':
        #Archivos eventos extraidos
        fileName = '/home/rsa/resultados/eventos-extraidos/'+nombre_+fecha_string+"_"+hora_string+".mseed"
    return fileName

def conversion_mseed_digital(fileName,fecha_,data_np,segundos_faltantes):
    # Nombre del archivo en funcion del tiempo de inicio
    anio=fecha_[0][0]
    mes=fecha_[0][1]
    dia=fecha_[0][2]
    horas=fecha_[0][3]
    minutos=fecha_[0][4]
    segundos=fecha_[0][5]
    parametros=parametros_()
    nombre_=parametros[2]

    #fileName=self.directorio_registros+fileName

    # Una vez que se tiene los datos, llama al metodo para obtener la traza
    # Todos los parametros que recibe se detallan en el metodo (mas abajo)
    trazaCH1 = obtenerTraza(nombre_,1, data_np[0],(2000 + anio), mes, dia, horas, minutos, segundos, 0, segundos_faltantes)
    trazaCH2 = obtenerTraza(nombre_,2, data_np[1],(2000 + anio), mes, dia, horas, minutos, segundos, 0, segundos_faltantes)
    trazaCH3 = obtenerTraza(nombre_,3, data_np[2],(2000 + anio), mes, dia, horas, minutos, segundos, 0, segundos_faltantes)
    # Crea un objeto Stream con la traza
    #stData = Stream(traces=[trazaCH1])
    # Si se desea varias trazas, esto seria para cuando se tiene 3 canales
    stData = Stream(traces=[trazaCH1, trazaCH2, trazaCH3])

    stData.write(fileName, format = 'MSEED', encoding = 'STEIM1', reclen = 512)

parametros=parametros_()
nombre=parametros[1]
fecha_=verificacion_archivo(archivo_binario)
nombre_= parametros[1]+'_20'
n_mseed=nombre_mseed(tipoArchivo,nombre_,fecha_)
datos_np, segundos_faltantes = lectura_archivo(archivo_binario)
conversion_mseed_digital(n_mseed,fecha_,datos_np,segundos_faltantes)

print('Se ha creado el archivo: %s' %n_mseed)

# Sube los archivos convertidos a Drive
if tipoArchivo=='1':
    subprocess.run(["python3", "/home/rsa/ejecutables/SubirArchivoDrive.py", "3", n_mseed])
    time.sleep(5)
    subprocess.run(["python3", "/home/rsa/ejecutables/SubirArchivoDrive.py", "1", archivo_binario])
elif tipoArchivo=='2':
    subprocess.run(["python3", "/home/rsa/ejecutables/SubirArchivoDrive.py", "2", n_mseed])


from typing import Dict
import random

class Matcher:
    @staticmethod
    def match(list: str, user: Dict) -> Dict:
        random_index_user = random.randrange(0, len(list) - 1)
        random_percent = random.randrange(50, 100)

        random_user = list[random_index_user]

        if random_user['name'] == user['name']:
            random_percent = 100

        return {"match": random_user, "percent": str(random_percent)+"%"}


import time
import os
import shutil

import torch

from configs.defaults import get_args_parser
import util.misc as utils
import util.multiprocess as distrib
from util.engine import train_one_epoch

from datasets import build_dataset
from models.gmot import build as build_model
from models.learning import build_learner

def main(args):
    # Initialize Run
    distrib.init_distributed_mode(args)                 # multi GPU
    utils.set_seed(args.seed + distrib.get_rank())      # reproducibility
    device = torch.device(args.device)

    # Build Model
    model      = build_model(args).to(device)               # transformer tracker
    optimizer, lr_scheduler = build_learner(args, model)    # loss function: models_outputs, ground_truth --> scalar
    tr_dataset = build_dataset('train', args)               # dict(imgs, gt_instances, exemplar)

    # Distribute Model & Dataset (if args.distributed==True)
    sampler, data_loader_train, model_without_ddp, model = distrib.make_distributed(args, model, tr_dataset)

    # Load Weights
    utils.load_checkpoint(args, model_without_ddp, optimizer, lr_scheduler)

    # Training Loop
    model = model.to(device)
    start_time = time.time()
    debug_epochs = {args.start_epoch, (args.epochs+args.start_epoch)//2, args.epochs-1} # when save info images
    if args.debug: print('Creating Visualizations. . . ')
    for epoch in range(args.start_epoch, args.epochs):
        start_e_time = time.time()
        if args.distributed:
            sampler.set_epoch(epoch)

        debug = None
        if args.debug and epoch in debug_epochs:
            debug = f'epoch_{epoch}/img{distrib.get_rank()}_'

        # Train
        train_one_epoch(model, data_loader_train, optimizer, device, epoch, args, debug)
        lr_scheduler.step()

        # Save
        if args.output_dir:
            checkpoint_path = args.output_dir + '/checkpoint.pth'
            distrib.save_on_master({
                'model': model_without_ddp.state_dict(),
                'optimizer': optimizer.state_dict(),
                'lr_scheduler': lr_scheduler.state_dict(),
                'epoch': epoch,
                'args': args,
            }, checkpoint_path)

        utils.print_time_from(start_time, start_e_time)

if __name__=='__main__':
    args = get_args_parser().parse_args()
    if args.output_dir:
        if args.debug:
            shutil.rmtree(f'{args.output_dir}/debug', ignore_errors=True)
        os.makedirs(args.output_dir, exist_ok=True)
    main(args)


"""
Created on Dec 13, 2018
@author: Yuedong Chen
"""

from .base_solver import BaseSolver
import time
import os
import torch

class ResFaceSolver(BaseSolver):
    """docstring for ResFaceSolver"""
    def __init__(self):
        super(ResFaceSolver, self).__init__()

    def train_epoch(self, epoch):
        self.train_model.set_train()
        last_print_losses_freq_t = time.time()
        for idx, batch in enumerate(self.train_dataset):
            self.train_total_steps += 1

            self.train_model.feed_batch(batch)
            self.train_model.optimize_paras()

            if self.train_total_steps % self.opt.print_losses_freq == 0:
                cur_losses = self.train_model.get_latest_losses()
                avg_step_t = (time.time() - last_print_losses_freq_t) / self.opt.print_losses_freq
                last_print_losses_freq_t = time.time()

                info_dict = {'epoch': epoch, 'epoch_len': self.epoch_len,
                            'epoch_steps': idx * self.opt.batch_size, 'epoch_steps_len': len(self.train_dataset),
                            'step_time': avg_step_t, 'cur_lr': self.cur_lr,
                            'log_path': os.path.join(self.opt.ckpt_dir, self.opt.log_file),
                            'losses': cur_losses
                            }
                self.visual.print_losses_info(info_dict)
                if self.visual.display_id > 0:
                    self.visual.display_current_losses(epoch - 1, info_dict['epoch_steps'] / len(self.train_dataset), cur_losses)

            if self.train_total_steps % self.opt.sample_img_freq == 0 and self.visual.display_id > 0:
                cur_vis = self.train_model.get_latest_visuals()
                self.visual.display_online_results(cur_vis, epoch)

    def test_networks(self, opt):
        # go through all the dataset and generate map
        dataset, model = self.init_test_setting(opt)
        results_dict = {'real_img': [], 'gen_resface': []}
        for idx, batch in enumerate(dataset):
            with torch.no_grad():
                model.feed_batch(batch)
                model.forward()

                results_dict['real_img'].append(model.real_img[0].cpu().float().numpy())
                results_dict['gen_resface'].append(model.gen_resface[0].cpu().float().numpy())

        return results_dict


import typing as T
import time
import socket
import asyncio
import contextlib

@contextlib.contextmanager
def get_event_loop():
    is_new_loop = False
    try:
        loop = asyncio.get_event_loop()
    except RuntimeError:
        loop = asyncio.new_event_loop()
        is_new_loop = True
    try:
        yield loop
    finally:
        if is_new_loop:
            loop.close()

def patch_multiprocessing_pickler():
    import pickle
    import cloudpickle
    from multiprocessing import reduction
    pickle.Pickler = cloudpickle.Pickler
    reduction.ForkingPickler = cloudpickle.Pickler

def find_free_port():
    """https://stackoverflow.com/a/45690594/8500469"""
    with contextlib.closing(
            socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
        s.bind(('', 0))
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        port = s.getsockname()[1]
    return port

def parse_address(address: str) -> T.Tuple[str, int]:
    ip, port = address.split(":")
    port_ = int(port)
    return (ip, port_)

def is_port_in_use(host: str, port: int) -> bool:
    """Check if the port is in use.
    https://stackoverflow.com/a/52872579/8500469
    """
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        return s.connect_ex((host, port)) == 0

def wait_until_bind(
        server_addr: T.Union[str, T.Tuple[str, int]],
        time_delta: float = 0.01):
    if isinstance(server_addr, str):
        server_addr = parse_address(server_addr)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    while True:
        if is_port_in_use(*server_addr):
            break
        else:
            time.sleep(time_delta)
    sock.close()


from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from django.shortcuts import render
from django.core.urlresolvers import reverse
from hostlist import models
from saltapi.saltAPI import saltAPI
from hostlist.models import HostList, HostAsset, IdcAsset, Maintainer
from hostlist.asset_info import *
from django.contrib.auth.decorators import login_required
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
from django.core import serializers
import json

# Create your views here.
def idc_list(request):
    """
    list all idc
    """
    try:
        page = request.GET.get('page', 1)
    except PageNotAnInteger:
        page = 1
    idc_list = IdcAsset.objects.all()
    p = Paginator(idc_list, 7, request=request)
    idc_list = p.page(page)

    return render(request, 'idc_list.html', locals())

def host_list(request):
    '''
    list all host
    '''
    try:
        page = request.GET.get('page', 1)
    except PageNotAnInteger:
        page = 1
    all_host = HostList.objects.all()
    idc_list = IdcAsset.objects.all()
    user_list = Maintainer.objects.all()

    idc_id = request.GET.get('idc_id')
    user_id = request.GET.get('user_id')
    hostname = request.POST.get('hostname')
    if idc_id:
        all_host = HostList.objects.filter(idc = idc_id)
    elif user_id:
        all_host = HostList.objects.filter(maintainer = user_id)
    elif hostname:
        all_host = HostList.objects.filter(ip__hostname__icontains = hostname)
    else:
        all_host = all_host.order_by('ip')
    p = Paginator(all_host, 7, request=request)
    all_host = p.page(page)

    return render(request, 'host_list.html', locals())

def get_hostasset(request):
    """
    get all hostasset
    """
    if request.method == 'GET':
        action = request.get_full_path().split('=')[1]
        if action == 'refresh':
            sapi = saltAPI()
            accepted, denied, unaccept, rejected = sapi.list_all_key()
            tgt = [i for i in accepted]
            ret = multitle_collect(tgt)
            for i in ret:
                h = HostAsset.objects.get_or_create(ip=i[0], hostname=i[1], fqdn=i[2], domain=i[3], macaddress=i[4], os=i[5],osarch=i[6], osrelease=i[7], manufacturer=i[8], productname=i[9], serialnumber=i[10], cpu_model=i[11], cpu_nums=i[12], cpu_groups=i[13], mem=i[14], virtual=i[15])
                HostList.objects.get_or_create(ip = h[0])
        return HttpResponseRedirect(reverse('host_list'))

def hostasset_list(request, ip):
    """
    list all hostasset
    """
    all_hostasset = HostAsset.objects.get(ip = ip)
    return render(request, 'hostasset_list.html', locals())


import tempfile

import docker
import pytest
from .base import BaseIntegrationTest, TEST_API_VERSION
from ..helpers import random_name, requires_api_version

class ContainerCollectionTest(BaseIntegrationTest):

    def test_run(self):
        client = docker.from_env(version=TEST_API_VERSION)
        assert client.containers.run(
            "alpine", "echo hello world", remove=True
        ) == b'hello world\n'

    def test_run_detach(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run("alpine", "sleep 300", detach=True)
        self.tmp_containers.append(container.id)
        assert container.attrs['Config']['Image'] == "alpine"
        assert container.attrs['Config']['Cmd'] == ['sleep', '300']

    def test_run_with_error(self):
        client = docker.from_env(version=TEST_API_VERSION)
        with pytest.raises(docker.errors.ContainerError) as cm:
            client.containers.run("alpine", "cat /test", remove=True)
        assert cm.value.exit_status == 1
        assert "cat /test" in cm.exconly()
        assert "alpine" in cm.exconly()
        assert "No such file or directory" in cm.exconly()

    def test_run_with_image_that_does_not_exist(self):
        client = docker.from_env(version=TEST_API_VERSION)
        with pytest.raises(docker.errors.ImageNotFound):
            client.containers.run("dockerpytest_does_not_exist")

    def test_run_with_volume(self):
        client = docker.from_env(version=TEST_API_VERSION)
        path = tempfile.mkdtemp()

        container = client.containers.run(
            "alpine", "sh -c 'echo \"hello\" > /insidecontainer/test'",
            volumes=["%s:/insidecontainer" % path],
            detach=True
        )
        self.tmp_containers.append(container.id)
        container.wait()

        out = client.containers.run(
            "alpine", "cat /insidecontainer/test",
            volumes=["%s:/insidecontainer" % path]
        )
        assert out == b'hello\n'

    def test_run_with_named_volume(self):
        client = docker.from_env(version=TEST_API_VERSION)
        volume = client.volumes.create(name="somevolume")
        self.tmp_volumes.append(volume.id)

        container = client.containers.run(
            "alpine", "sh -c 'echo \"hello\" > /insidecontainer/test'",
            volumes=["somevolume:/insidecontainer"],
            detach=True
        )
        self.tmp_containers.append(container.id)
        container.wait()

        out = client.containers.run(
            "alpine", "cat /insidecontainer/test",
            volumes=["somevolume:/insidecontainer"]
        )
        assert out == b'hello\n'

    def test_run_with_network(self):
        net_name = random_name()
        client = docker.from_env(version=TEST_API_VERSION)
        client.networks.create(net_name)
        self.tmp_networks.append(net_name)

        container = client.containers.run(
            'alpine', 'echo hello world', network=net_name,
            detach=True
        )
        self.tmp_containers.append(container.id)

        attrs = container.attrs

        assert 'NetworkSettings' in attrs
        assert 'Networks' in attrs['NetworkSettings']
        assert list(attrs['NetworkSettings']['Networks'].keys()) == [net_name]

    def test_run_with_none_driver(self):
        client = docker.from_env(version=TEST_API_VERSION)

        out = client.containers.run(
            "alpine", "echo hello",
            log_config=dict(type='none')
        )
        assert out is None

    def test_run_with_json_file_driver(self):
        client = docker.from_env(version=TEST_API_VERSION)

        out = client.containers.run(
            "alpine", "echo hello",
            log_config=dict(type='json-file')
        )
        assert out == b'hello\n'

    @requires_api_version('1.25')
    def test_run_with_auto_remove(self):
        client = docker.from_env(version=TEST_API_VERSION)
        out = client.containers.run(
            'alpine', 'echo hello', auto_remove=True
        )
        assert out == b'hello\n'

    @requires_api_version('1.25')
    def test_run_with_auto_remove_error(self):
        client = docker.from_env(version=TEST_API_VERSION)
        with pytest.raises(docker.errors.ContainerError) as e:
            client.containers.run(
                'alpine', 'sh -c ">&2 echo error && exit 1"', auto_remove=True
            )
        assert e.value.exit_status == 1
        assert e.value.stderr is None

    def test_run_with_streamed_logs(self):
        client = docker.from_env(version=TEST_API_VERSION)
        out = client.containers.run(
            'alpine', 'sh -c "echo hello && echo world"', stream=True
        )
        logs = [line for line in out]
        assert logs[0] == b'hello\n'
        assert logs[1] == b'world\n'

    def test_get(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run("alpine", "sleep 300", detach=True)
        self.tmp_containers.append(container.id)
        assert client.containers.get(container.id).attrs[
            'Config']['Image'] == "alpine"

    def test_list(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container_id = client.containers.run(
            "alpine", "sleep 300", detach=True).id
        self.tmp_containers.append(container_id)
        containers = [c for c in client.containers.list() if c.id ==
                      container_id]
        assert len(containers) == 1

        container = containers[0]
        assert container.attrs['Config']['Image'] == 'alpine'

        container.kill()
        container.remove()
        assert container_id not in [c.id for c in client.containers.list()]

class ContainerTest(BaseIntegrationTest):

    def test_commit(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run(
            "alpine", "sh -c 'echo \"hello\" > /test'",
            detach=True
        )
        self.tmp_containers.append(container.id)
        container.wait()
        image = container.commit()
        assert client.containers.run(
            image.id, "cat /test", remove=True
        ) == b"hello\n"

    def test_diff(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run("alpine", "touch /test", detach=True)
        self.tmp_containers.append(container.id)
        container.wait()
        assert container.diff() == [{'Path': '/test', 'Kind': 1}]

    def test_exec_run_success(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run(
            "alpine", "sh -c 'echo \"hello\" > /test; sleep 60'", detach=True
        )
        self.tmp_containers.append(container.id)
        exec_output = container.exec_run("cat /test")
        assert exec_output[0] == 0
        assert exec_output[1] == b"hello\n"

    def test_exec_run_failed(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run(
            "alpine", "sh -c 'sleep 60'", detach=True
        )
        self.tmp_containers.append(container.id)
        exec_output = container.exec_run("docker ps")
        assert exec_output[0] == 126

    def test_kill(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run("alpine", "sleep 300", detach=True)
        self.tmp_containers.append(container.id)
        while container.status != 'running':
            container.reload()
        assert container.status == 'running'
        container.kill()
        container.reload()
        assert container.status == 'exited'

    def test_logs(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run("alpine", "echo hello world",
                                          detach=True)
        self.tmp_containers.append(container.id)
        container.wait()
        assert container.logs() == b"hello world\n"

    def test_pause(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run("alpine", "sleep 300", detach=True)
        self.tmp_containers.append(container.id)
        container.pause()
        container.reload()
        assert container.status == "paused"
        container.unpause()
        container.reload()
        assert container.status == "running"

    def test_remove(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run("alpine", "echo hello", detach=True)
        self.tmp_containers.append(container.id)
        assert container.id in [c.id for c in client.containers.list(all=True)]
        container.wait()
        container.remove()
        containers = client.containers.list(all=True)
        assert container.id not in [c.id for c in containers]

    def test_rename(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run("alpine", "echo hello", name="test1",
                                          detach=True)
        self.tmp_containers.append(container.id)
        assert container.name == "test1"
        container.rename("test2")
        container.reload()
        assert container.name == "test2"

    def test_restart(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run("alpine", "sleep 100", detach=True)
        self.tmp_containers.append(container.id)
        first_started_at = container.attrs['State']['StartedAt']
        container.restart()
        container.reload()
        second_started_at = container.attrs['State']['StartedAt']
        assert first_started_at != second_started_at

    def test_start(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.create("alpine", "sleep 50", detach=True)
        self.tmp_containers.append(container.id)
        assert container.status == "created"
        container.start()
        container.reload()
        assert container.status == "running"

    def test_stats(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run("alpine", "sleep 100", detach=True)
        self.tmp_containers.append(container.id)
        stats = container.stats(stream=False)
        for key in ['read', 'networks', 'precpu_stats', 'cpu_stats',
                    'memory_stats', 'blkio_stats']:
            assert key in stats

    def test_stop(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run("alpine", "top", detach=True)
        self.tmp_containers.append(container.id)
        assert container.status in ("running", "created")
        container.stop(timeout=2)
        container.reload()
        assert container.status == "exited"

    def test_top(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run("alpine", "sleep 60", detach=True)
        self.tmp_containers.append(container.id)
        top = container.top()
        assert len(top['Processes']) == 1
        assert 'sleep 60' in top['Processes'][0]

    def test_update(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run("alpine", "sleep 60", detach=True,
                                          cpu_shares=2)
        self.tmp_containers.append(container.id)
        assert container.attrs['HostConfig']['CpuShares'] == 2
        container.update(cpu_shares=3)
        container.reload()
        assert container.attrs['HostConfig']['CpuShares'] == 3

    def test_wait(self):
        client = docker.from_env(version=TEST_API_VERSION)
        container = client.containers.run("alpine", "sh -c 'exit 0'",
                                          detach=True)
        self.tmp_containers.append(container.id)
        assert container.wait()['StatusCode'] == 0
        container = client.containers.run("alpine", "sh -c 'exit 1'",
                                          detach=True)
        self.tmp_containers.append(container.id)
        assert container.wait()['StatusCode'] == 1


from tkinter import *
import numpy as np
import os
import matplotlib as mpl
import matplotlib.pyplot as plt
import tkinter.messagebox
from tkinter import filedialog as fd

directory_files = "main/saves/" #saves directory
lst = [] #list user drawing
root = Tk()
root.title('DROWA GRAPHICS 1.0') #title

def savePosn(event):
    global lastx, lasty
    lastx, lasty = event.x, event.y

def addLine(event):
    canvas.create_line((lastx, lasty, event.x, event.y))
    savePosn(event)
    lst.append((event.x,event.y))

def create_table():
    tl1 = Toplevel(root)
    tl1.title("TABLE")
    lstbox = Listbox(tl1,width=30,height=200)
    for i in range(len(lst)):
        lstbox.insert(END,f"x={lst[i][0]} y={lst[i][1]}")
    lstbox.pack(side='left',fill='both')

def create_table_load():
    tl2 = Toplevel(root)
    tl2.title("TABLE LOAD DATA")
    lstboxLD = Listbox(tl2,width=30,height=200)
    for i in range(len(lstload)):
        lstboxLD.insert(END,f"x={lstload[i][0]} y={lstload[i][1]}")
    lstboxLD.pack(side='left',fill='both')

def clear_canvas():
    canvas.delete("all")
    lst.clear()

def saving_data():
    files = os.listdir(directory_files)
    np.save(f"main/saves/data{len(files)}",lst[1:])
    tkinter.messagebox.showinfo(message=f"data saved in main/saves/data{len(files)}")

def create_from_load():
    clear_canvas()
    for k in range(len(lstload)):
        if (k < len(lstload)):
            canvas.create_line((lstload[k][0],lstload[k][1], lstload[k+1][0],lstload[k+1][1]))

def graphic_show():
    X = []
    for i in range(len(lstload)):
        X.append(lstload[i][0])
    Y = []
    for i in range(len(lstload)):
        Y.append(lstload[i][1])
    X = np.array(X)
    Y = np.array(Y)
    fig, ax = plt.subplots()
    ax.plot(X,Y,color='C1')
    ax.grid()
    plt.show()

def choose_file():
    name = fd.askopenfilename(title='numpy files',filetypes=[('Numpy files', '.npy')])
    global lstload
    lstload = []
    lstload.clear()
    lstload = np.load(f"{name}")
    btn6.configure(state=NORMAL)
    btn7.configure(state=NORMAL)
    tkinter.messagebox.showinfo(message=f"data load from {name}")

#INTERFACE
canvas = Canvas(root,bg='white',width=1100,height=500)
canvas.grid(column=0, row=0,sticky=(E))
canvas.bind("<Button-1>", savePosn)
canvas.bind("<B1-Motion>", addLine)

btn1 = Button(root,width=15, height=5,text='create table', command=lambda:create_table())
btn1.grid(column=0,row=1)

btn2 = Button(root,width=15, height=5,text='clear', command=lambda:clear_canvas())
btn2.grid(column=0,row=2)

btn3 = Button(root,width=15, height=5,text='save', command=lambda:saving_data())
btn3.grid(column=0,row=3)

btn6 = Button(root,width=15, height=5,text='print', state=DISABLED, command=lambda:create_from_load())
btn6.grid(column=1,row=0)

btn7 = Button(root,width=15, height=5,text='show', state=DISABLED, command=lambda:graphic_show())
btn7.grid(column=1,row=1)

btn8 = Button(root,width=15, height=5,text='choose file', command=lambda:choose_file())
btn8.grid(column=2,row=0)

btn9 = Button(root,width=15, height=5,text='create load data table', command=lambda:create_table_load())
btn9.grid(column=2,row=1)

root.mainloop()


"""change column to BigInteger

Revision ID: 97f2d8b76246
Revises: 18239a3befe0
Create Date: 2016-02-25 20:38:21.952978

"""

# revision identifiers, used by Alembic.
revision = '97f2d8b76246'
down_revision = '18239a3befe0'
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('user', 'id', type_=sa.BigInteger, existing_type=sa.Integer)
    ### end Alembic commands ###

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('user', 'id', type_=sa.Integer, existing_type=sa.BigInteger)
    ### end Alembic commands ###


from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn import tree, ensemble
import matplotlib.pyplot as plt
import numpy as np

class Evaluation:
    def __init__(self):
        # data set
        self.cancer = load_breast_cancer()
        # split data set into train set and test set at ratio 1:9
        # X: input, Y: output
        self.X_train, self.X_test, self.y_train, self.y_test \
            = train_test_split(self.cancer.data, self.cancer.target, test_size=0.9)
        self.tree_score = 0
        self.bagging_score = {}
        self.boost_score = {}
        self.forest_score = {}

    def decision_tree(self):
        t1 = tree.DecisionTreeClassifier(criterion='gini').fit(self.X_train, self.y_train)
        self.tree_score = t1.score(self.X_test, self.y_test)

    def bagging(self):
        for bag_count in range(2, 82, 4):
            bagging = ensemble.BaggingClassifier(tree.DecisionTreeClassifier(), n_estimators=bag_count).fit(
                self.X_train, self.y_train)
            self.bagging_score[bag_count] = bagging.score(self.X_test, self.y_test)
        plt.scatter(x=self.bagging_score.keys(), y=self.bagging_score.values())
        plt.xlabel('n_estimators')
        plt.ylabel('Scores')
        plt.title('Bagging')
        plt.show()

    def forest(self):
        for feature_count in range(3, 31, 3):
            for bag_count in range(5, 56, 5):
                forest = ensemble.RandomForestClassifier(tree.DecisionTreeClassifier())
                forest.set_params(max_features=feature_count, n_estimators=bag_count)
                forest.fit(self.X_train, self.y_train)
                self.forest_score[(feature_count, bag_count)] = forest.score(self.X_test, self.y_test)
        keys = list(zip(*self.forest_score.keys()))
        fig, ax = plt.subplots()
        scatter = ax.scatter(
            #x=keys[1],
            x=np.array(keys[1]),
            #y=self.forest_score.values(),
            y=[i for i in self.forest_score.values()],
            #s=keys[0]
            s=np.array(keys[0])
        )
        plt.title("Random Forest")
        plt.xlabel("n_estimators")
        plt.ylabel("Scores")
        handles, labels = scatter.legend_elements(prop="sizes", alpha=0.5)
        box = ax.get_position()
        ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
        ax.legend(handles, labels, loc="center left", bbox_to_anchor=(1, 0.5), title="Feature Counts")
        plt.show()

    def boost(self):
        for bag_count in range(2, 82, 4):
            boost = ensemble.AdaBoostClassifier(tree.DecisionTreeClassifier(), n_estimators=bag_count).fit(
                self.X_train, self.y_train)
            self.boost_score[bag_count] = boost.score(self.X_test, self.y_test)
        plt.scatter(x=self.boost_score.keys(), y=self.boost_score.values())
        plt.xlabel('n_estimators')
        plt.ylabel('Scores')
        plt.title('AdaBoost')
        plt.show()

    def summary(self):
        print('Tree score:', self.tree_score)
        print('Bagging score:', self.bagging_score)
        print('Boost score:', self.boost_score)
        print('Forest score:', self.forest_score)

if __name__ == '__main__':
    exp = Evaluation()
    exp.decision_tree()
    exp.bagging()
    exp.forest()
    exp.boost()
    exp.summary()


'''
  File name: carv.py
  Author:
  Date created:
'''

'''
  File clarification:
    Aimed to handle finding seams of minimum energy, and seam removal, the algorithm
    shall tackle resizing images when it may be required to remove more than one seam,
    sequentially and potentially along different directions.

    - INPUT I: n × m × 3 matrix representing the input image.
    - INPUT nr: the numbers of rows to be removed from the image.
    - INPUT nc: the numbers of columns to be removed from the image.
    - OUTPUT Ic: (n − nr) × (m − nc) × 3 matrix representing the carved image.
    - OUTPUT T: (nr + 1) × (nc + 1) matrix representing the transport map.
'''
import numpy as np
from genEngMap import genEngMap
from cumMinEngVer import cumMinEngVer
from cumMinEngHor import cumMinEngHor
from rmVerSeam import rmVerSeam
from rmHorSeam import rmHorSeam
import imageio

def carv(I, nr, nc):
  # Your Code Here
  T=np.zeros((nr+1,nc+1))
  P=np.zeros((nr+1,nc+1))
  Imgcopy=np.copy(I)
  ImageArray=np.zeros((nc+1),dtype=object)
  T[0,0]=0
  P[0,0]=0
  ImageArray[0]=Imgcopy

  for j in range(1,nc+1):
    P[0,j]=0
    e=genEngMap(Imgcopy)
    Mx,Tbx=cumMinEngVer(e)
    Imgcopy,E=rmVerSeam(Imgcopy,Mx,Tbx)
    T[0,j]=T[0,j-1]+E
    ImageArray[j]=Imgcopy

  # print(ImageArray.shape)

  for i in range(1,nr+1):
    #print(i)
    ImgCopy=np.copy(ImageArray[0])
    e=genEngMap(ImgCopy)
    My,Tby=cumMinEngHor(e)
    ImgCopy,E=rmHorSeam(ImgCopy,My,Tby)
    ImageArray[0]=ImgCopy
    T[i,0]=T[i-1,0]+E
    P[i,0]=1
    for j in range(1,nc+1):
      # upar se
      ImgCopy1=np.copy(ImageArray[j])
      e1=genEngMap(ImgCopy1)
      My,Tby=cumMinEngHor(e1)
      ImgCopy1,E1=rmHorSeam(ImgCopy1,My,Tby)
      # left se
      ImgCopy2=np.copy(ImageArray[j-1])

      e2=genEngMap(ImgCopy2)
      Mx,Tbx=cumMinEngVer(e2)
      ImgCopy2,E2=rmVerSeam(ImgCopy2,Mx,Tbx)

      if((T[i,j-1]+E2)<(T[i-1,j]+E1)):
        T[i,j]=T[i,j-1]+E2
        P[i,j]=0
        ImageArray[j]=ImgCopy2
      else:
        T[i,j]=T[i-1,j]+E1
        P[i,j]=1
        ImageArray[j]=ImgCopy1
  # print(T)
  # print(P)

  np.pad(T,((1,0),(1,0)),'constant',constant_values=float('inf'))
  #------------------------------------------------------------------------------------
  # P=np.zeros((nr+1,nc+1))
  # ind_i=nr
  # ind_j=nc
  # # P[ind_i,ind_j]=1
  # while(True):
  #     # print("test")
  #     P[ind_i,ind_j]=1
  #     if(T[ind_i-1,ind_j]<T[ind_i,ind_j-1]):
  #         ind_i=ind_i-1
  #     else:
  #         ind_j=ind_j-1
  #     if(ind_i==0 and ind_j==0):
  #         break
  # # while(ind_i>0 and ind_j>0):
#--------------------------------------------------------------------------------------

  # for k in reversed(range(nr+nc-1)):
    # print(ind_i,ind_j)
    # print(ind_i-1,ind_j)
    # print(ind_j)

    # if(T[ind_i,ind_j-1])<=(T[ind_i-1,ind_j]):
    #   P[ind_i,ind_j-1]=1
    #   ind_j=ind_j-1
    # else:
    #   P[ind_i-1,ind_j]=1
    #   ind_i=ind_i-1

  # print(P)

  # indices=np.argwhere(P==1)
  # print(indices.shape)
  ImageList=[]
  ImageList.append(I)
  ImgCopy3=np.copy(I)

  indi=nr
  indj=nc
  # points=np.empty((0,2))
  points=np.empty((0,2))
  # print("ponits",points.shape)
  while (True):
      if(P[indi,indj]==1):
          indi=indi-1
          temp=np.array([indi,indj])
          temp=np.reshape(temp,(1,2))
          points=np.vstack((points,temp))
          # points=np.append(points,np.array([indi,indj]),1)
          # print(temp.shape)
          # points.append(np.array([indi,indj]))
      elif (P[indi,indj]==0):
          indj=indj-1
          temp=np.array([indi,indj])
          temp=np.reshape(temp,(1,2))
          points=np.vstack((points,temp))
          # points=np.append(points,np.array([indi,indj]),1)
          # points=np.vstack(np.array([indi,indj]))
          # print(np.array([indi,indj]))
      if indi==0 and indj==0:
          break

  # print("points shape",points.shape)
  # print("points",points)

  points=np.flip(points,axis=0)
  for i in range(points.shape[0]-1):
    # print("bye")
    if(points[i+1][0]>points[i][0]):
      e=genEngMap(ImgCopy3)
      My,Tby=cumMinEngHor(e)
      ImgCopy3,E=rmHorSeam(ImgCopy3,My,Tby)
      ImageList.append(ImgCopy3)
      # print("hi")
    elif (points[i+1][1]>points[i][1]):
      e=genEngMap(ImgCopy3)
      Mx,Tbx=cumMinEngVer(e)
      ImgCopy3,E=rmVerSeam(ImgCopy3,Mx,Tbx)
      ImageList.append(ImgCopy3)
      # print("hihi")

  #print(len(ImageList))
  print(len(ImageList))
  imageio.mimsave('carved_output.gif',ImageList,duration=0.05)
  Ic=ImgCopy3

  return  Ic,T


import unittest
import requests
import threading

from nmoscommon.webapi import WebAPI, route, abort
from nmoscommon.logger import Logger
from nmoscommon.utils import getLocalIP

HOST = getLocalIP()
logger = Logger(__name__)

class MockAPI(WebAPI):

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

    @route('/error', methods=["HEAD", "GET"])
    def _error(self):
        abort(400, "some error")

    @route('/internal_error', methods=["HEAD", "GET"])
    def _internal_error(self):
        raise Exception("Uh oh")

    @route('/json')
    def _json(self):
        return {'a': 1, 'b': [2, 'three']}

    @route('/tuple', methods=['GET', 'HEAD'], auto_json=False)
    def _tuple(self):
        return ("ok", 200)

class MockService(threading.Thread):
    daemon = True

    def __init__(self):
        threading.Thread.__init__(self)
        self.api = MockAPI()

    def run(self):
        self.api.app.run(port=5555)

    def stop(self):
        pass

class FacadeProxyTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls._srv = MockService()
        cls._srv.start()

    def assert_has_cors(self, response):
        self.assertIn('Access-Control-Allow-Origin', response.headers)
        self.assertIn('Access-Control-Allow-Methods', response.headers)
        self.assertIn('Access-Control-Max-Age', response.headers)
        self.assertIn('Access-Control-Allow-Headers', response.headers)

    def test_error_get(self):
        response = requests.get("http://localhost:5555/error")
        self.assertEqual(400, response.status_code)
        self.assertEqual("application/json", response.headers.get('Content-Type'))
        response_content = response.json()
        self.assertEqual("some error", response_content['error'])
        self.assert_has_cors(response)

    def test_error_head(self):
        response = requests.head("http://localhost:5555/error")
        self.assertEqual(400, response.status_code)
        self.assertEqual("0", response.headers.get('Content-Length'))
        self.assertEqual("", response.text)
        self.assert_has_cors(response)

    def test_auto_json(self):
        response = requests.get("http://localhost:5555/json")
        self.assertEqual(200, response.status_code)
        self.assertEqual("application/json", response.headers.get('Content-Type'))
        self.assertEqual({'a': 1, 'b': [2, 'three']}, response.json())
        self.assert_has_cors(response)

    def test_internal_error_get(self):
        response = requests.get("http://localhost:5555/internal_error")
        self.assertEqual(500, response.status_code)
        response_content = response.json()
        self.assertEqual(500, response_content['code'])
        self.assertEqual("Internal Error", response_content['error'])
        self.assertEqual(["Exception: Uh oh\n"], response_content['debug']['exception'])
        self.assert_has_cors(response)

    def test_internal_error_head(self):
        response = requests.head("http://localhost:5555/internal_error")
        self.assertEqual(500, response.status_code)
        self.assertEqual("0", response.headers.get('Content-Length'))
        self.assertEqual("", response.text)
        self.assert_has_cors(response)

    def test_no_json_tuple(self):
        response = requests.get("http://localhost:5555/tuple")
        self.assertEqual(200, response.status_code)
        self.assertEqual("ok", response.text)
        self.assertEqual("text/html; charset=utf-8", response.headers.get('Content-Type'))
        self.assert_has_cors(response)

    def test_head(self):
        response = requests.head("http://localhost:5555/tuple")
        self.assertEqual(200, response.status_code)
        self.assertEqual("", response.text)
        self.assertEqual("2", response.headers.get('Content-Length'))
        self.assertEqual("text/html; charset=utf-8", response.headers.get('Content-Type'))
        self.assert_has_cors(response)

if __name__ == '__main__':
    unittest.main()


from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtDesigner import QPyDesignerCustomWidgetPlugin

from tempswitchbutton import TempSwitchButton

class TempSwitchButtonPlugin(QPyDesignerCustomWidgetPlugin):

    # The __init__() method is only used to set up the plugin and define its
    # initialized variable.
    def __init__(self, parent=None):

        super(TempSwitchButtonPlugin, self).__init__(parent)

        self.initialized = False

    # The initialize() and isInitialized() methods allow the plugin to set up
    # any required resources, ensuring that this can only happen once for each
    # plugin.
    def initialize(self, core):

        if self.initialized:
            return

        self.initialized = True

    def isInitialized(self):

        return self.initialized

    # This factory method creates new instances of our custom widget with the
    # appropriate parent.
    def createWidget(self, parent):
        return TempSwitchButton(parent)

    # This method returns the name of the custom widget class that is provided
    # by this plugin.
    def name(self):
        return "TempSwitchButton"

    # Returns the name of the group in Qt Designer's widget box that this
    # widget belongs to.
    def group(self):
        return "PyQt Examples"

    # Return the icon used to represent the custom widget in Designer's widget
    # box.
    def icon(self):
        return QIcon(_logo_pixmap)

    # Returns a short description of the custom widget for use in a tool tip.
    def toolTip(self):
        return ""

    # Returns a short description of the custom widget for use in a "What's
    # This?" help message for the widget.
    def whatsThis(self):
        return ""

    # Returns True if the custom widget acts as a container for other widgets;
    # otherwise returns False. Note that plugins for custom containers also
    # need to provide an implementation of the QDesignerContainerExtension
    # interface if they need to add custom editing support to Qt Designer.
    def isContainer(self):
        return False

    # Returns the module containing the custom widget class. It may include
    # a module path.
    def includeFile(self):
        return "tempswitchbutton"

# Define the image used for the icon.
_logo_16x16_xpm = [
"16 16 46 1",
". c #a5a5dc",
"l c #a69fd6",
"k c #a7a5da",
"h c #a7a6dc",
"a c #a7a7de",
"Q c #a8a5da",
"s c #a9a7d7",
"R c #a9a9e0",
"z c #abaad4",
"E c #afafda",
"M c #afafdb",
"K c #b0a8e2",
"o c #b1afe4",
"p c #b2b2d7",
"# c #b2b2ed",
"i c #b39eb6",
"F c #b3b3e1",
"e c #b4b4ef",
"t c #b58bab",
"d c #b6b6f2",
"n c #b798b8",
"P c #b798b9",
"c c #b8b6f2",
"D c #b8b89c",
"m c #b9648d",
"J c #ba84b0",
"A c #bdbdfb",
"f c #bfbffe",
"g c #c06996",
"b c #c0c0ff",
"B c #cbb889",
"L c #cbb989",
"O c #cfcf87",
"I c #d09585",
"w c #d0cf86",
"x c #dede81",
"G c #e8e87c",
"q c #edde7b",
"N c #f1e07b",
"v c #f2e07b",
"H c #f6e57c",
"j c #fb917e",
"u c #ffb580",
"r c #ffda80",
"C c #fffe80",
"y c #ffff80",
".##############a",
"#bbbbbbbbcdbbbbe",
"#bbbbbbbfghbbbbe",
"#bbbbbbbijkbbbbe",
"#blmnobpqrsbbbbe",
"#bbtuvwxyyzbbbbe",
"#bbABCyyyyDEfbbe",
"#bbbFGyyyyyHIJKe",
"#bbbFGyyyyyHIJKe",
"#bbALCyyyyDMfbbe",
"#bbtuNOxyyzbbbbe",
"#blmPobpqrsbbbbe",
"#bbbbbbbijQbbbbe",
"#bbbbbbbfghbbbbe",
"#bbbbbbbbcdbbbbe",
"aeeeeeeeeeeeeeeR"]

_logo_pixmap = QPixmap(_logo_16x16_xpm)


import os
import json
import yaml
import pickle
import datetime
import numpy as np
from collections import OrderedDict

import torch
from model import TransformerXL

def main():
    # gen config
    modelConfig, trainConfig = get_configs()

    # load dictionary
    event2word, word2event = pickle.load(open(os.path.join(trainConfig['ROOT'],'dictionary.pkl'), 'rb'))

    # load train data
    training_data = np.load(os.path.join(trainConfig['ROOT'],'train_data_XL.npz'))

    device = torch.device("cuda:{}".format(trainConfig['gpuID']) if not trainConfig["no_cuda"] and torch.cuda.is_available() else "cpu")
    os.environ['CUDA_VISIBLE_DEVICES'] = trainConfig['gpuID']

    print('Device to train:', device)

    resume = trainConfig['resume_training_model']

    # declare model
    model = TransformerXL(
            modelConfig,
            device,
            event2word=event2word,
            word2event=word2event,
            is_training=True)

    # train
    model.train(training_data,
                trainConfig,
                device,
                resume)

def get_configs():
    cfg = yaml.full_load(open("config.yml", 'r'))

    modelConfig = cfg['MODEL']
    trainConfig = cfg['TRAIN']

    cur_date = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
    experiment_Dir = os.path.join(trainConfig['output_dir'],cur_date)
    if not os.path.exists(experiment_Dir):
        print('experiment_Dir:', experiment_Dir)
        os.makedirs(experiment_Dir)
    print('Experiment: ', experiment_Dir)
    trainConfig.update({'experiment_Dir': experiment_Dir})

    with open(os.path.join(experiment_Dir, 'config.yml'), 'w') as f:
        doc = yaml.dump(cfg, f)

    print('='*5, 'Model configs', '='*5)
    print(json.dumps(modelConfig, indent=1, sort_keys=True))
    print('='*2, 'Training configs', '='*5)
    print(json.dumps(trainConfig, indent=1, sort_keys=True))
    return modelConfig, trainConfig

if __name__ == '__main__':
    main()


import argparse
import json

import pandas as pd
from utils import compute_subgroup_auc

def convert_dataframe_to_bool(df):
    bool_df = df.copy()
    for la in LANGS:
        bool_df[la] = df.lang == la
    return bool_df

def compute_lang_metrics_for_model(dataset, subgroups, model, label_col):
    """Computes per-subgroup metrics for all subgroups and one model."""
    records = []
    for subgroup in subgroups:
        record = {
            "subgroup": subgroup,
            "subgroup_size": len(dataset[dataset[subgroup]]),
        }
        record[SUBGROUP_AUC] = compute_subgroup_auc(dataset, subgroup, label_col, model)
        records.append(record)
    return pd.DataFrame(records).sort_values("subgroup_auc", ascending=True)

def main():
    with open(TEST) as f:
        results = json.load(f)

    test_private_path = "jigsaw_data/jigsaw-multilingual-toxic-comment-classification/updated_test.csv"
    test_private = pd.read_csv(test_private_path)
    test_private = convert_dataframe_to_bool(test_private)

    test_private[MODEL_NAME] = [s[0] for s in results["scores"]]

    lang_metrics_df = compute_lang_metrics_for_model(test_private, LANGS, MODEL_NAME, TOXICITY_COLUMN)
    print(lang_metrics_df)
    lang_metrics_df.to_csv(TEST[:-9] + "_lang_breakdown.csv")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--res_path",
        type=str,
        help="path to the saved results file",
    )
    args = parser.parse_args()
    TEST = args.res_path

    LANGS = ["es", "fr", "pt", "ru", "it", "tr"]

    TOXICITY_COLUMN = "toxic"
    MODEL_NAME = "XLM-R"
    SUBGROUP_AUC = "subgroup_auc"

    main()


def handleWebhook(request):

    import json
    from random import choice

    f = open('meals.json')
    meals_dict = json.load(f)

    req = request.get_json()

    responseText = ""
    intent = req["queryResult"]["intent"]["displayName"]

    if intent == "Default Welcome Intent":
        responseText = "來自Cloud Function的誠摯歡迎"
    elif intent == "Default Fallback Intent":
        fallbacks = ["再講一次可以嗎?","對不起，我不清楚你所表達的意思","試著換個方式問看看吧","我剛剛恍神了，再說一次好嗎?"]
        responseText = choice(fallbacks)
    elif intent == "輸出想到的美食":
        types = req["queryResult"]["parameters"]["input"]
        if types in ["午餐","晚餐"]:
            types = "午晚餐"

        if types in list(meals_dict.keys()):
            results = choice(meals_dict[types])
            responseText = f" 「{results}」如何呢? "
        else:
            fallbacks = ["我當機了，讓我再想一下","再幫我撐十秒","等待靈感降落中..."]
            responseText = choice(fallbacks)

    # You can also use the google.cloud.dialogflowcx_v3.types.WebhookRequest protos instead of manually writing the json object
    res = {"fulfillmentMessages": [{"text": {"text": [responseText]}}]}

    return res