def main():
    num1 = int(input("Insira o primeiro número: "))
    num2 = int(input("Insira o segundo número: "))
    opcao = int(input("A opção das operações:\n1 – Adição\n2 – Subtração\n3 – Multiplicação\n4 – Divisão\n"))

    print(chamar_oper(num1, num2, opcao))

def chamar_oper(num1, num2, opcao):
        if opcao == 1:
            return "O seu valor de soma",num1 + num2
        elif opcao == 2:
            return "O seu valor de subtração",num1 - num2
        elif opcao == 3:
            return "O seu valor de multiplicação",num1 * num2
        elif opcao == 4:
            return "O seu valor de divisão",num1 / num2
        else:
            return "Operação Inválida"

main()


import csv
import json

filename = 'data.csv'
sum = 0

data = []
out = {}

with open(filename, 'r') as csvfile:
    csvreader = csv.reader(csvfile)
    fields = next(csvreader)
    for row in csvreader:
        amount = float(row[6].replace(',', ''))
        if amount > 9.5:
            sum += amount
            data.append([row[4], float(row[6].replace(',', ''))])

for d in data:
    # print(d[0], d[1]/sum)
    out.update({d[0]: d[1]/sum})

with open('early-users-mobius.json', 'w+') as f:
    json.dump(out, f)
    # sum = 0
    # for num in out.values():
    #     sum += num

    # print(sum)


import libreria

def AgregarPrecio():
    # 1. Pedir producto
    # 2. Pedir precio
    # 3. Guardar datos en precios.txt
    producto=libreria.pedir_nombre("Ingrese producto: ")
    precio=libreria.pedir_entero("ingrese precio:")
    contenido = producto + "-" + "s/." + str(precio) + "\n"
    libreria.agregar_datos("precios.txt", contenido,"a")
    print("se agrego el precio de un nuevo producto")

def MostrarPrecios():
    # 1. Abrir el archivo precios.txt y mostrar sus datos
    datos=libreria.obtener_datos_lista("precios.txt")
    # 2. Comprobar si hay datos
    if ( datos != ""):
        for item in datos:
            producto, precio= item.split("-")
            msg="{} cuesta {} nuevo(s) sol(es)" \
                ""
            producto=producto.replace("\n","")
            precio=precio.replace("\n","")
            print(msg.format(producto, precio))
        #fin_for
    else:
        print("sin datos")

opc=""
max=3
while(opc!=max):
    print("############### MENU ##############")
    print("#1. Agregar precio de producto    #")
    print("#2. Mostrar precios               #")
    print("#3. Salir                         #")
    print("###################################")
    opc=libreria.pedir_numero("ingrese opcion: ",1,3)

    if( opc==1):
        AgregarPrecio()
    if(opc==2):
        MostrarPrecios()
    #fin if
#fin while
print("fin")


#!/usr/bin/python3

from pathlib import Path
import tkinter as tk

from .styles import BUTTON_STYLE
from brownie.project.sources import is_inside_offset

class _Toggle(tk.Button):

    def __init__(self, parent, text, keybind=None):
        self.active = False
        super().__init__(parent, text=text, command=self.toggle)
        self.root = self._root()
        self.configure(**BUTTON_STYLE)
        if keybind:
            self.root.bind(keybind, self.toggle)

    def toggle(self, event=None):
        if self.active:
            self.toggle_off()
            self.configure(relief="raised", background="#272727")
        else:
            if not self.toggle_on():
                return
            self.configure(relief="sunken", background="#383838")
        self.active = not self.active

    def toggle_on(self):
        pass

    def toggle_off(self):
        pass

class ScopingToggle(_Toggle):

    def __init__(self, parent):
        super().__init__(parent, "Scope", "s")
        self.oplist = self.root.main.oplist

    def toggle_on(self):
        try:
            op = self.oplist.selection()[0]
        except IndexError:
            return False
        if self.oplist.item(op, 'tags')[0] == "NoSource":
            return False
        pc = self.root.pcMap[op]
        for key, value in sorted(self.root.pcMap.items(), key=lambda k: int(k[0])):
            if (
                'path' not in value or value['path'] != pc['path'] or
                not is_inside_offset(value['offset'], pc['offset'])
            ):
                self.oplist.detach(key)
            else:
                self.oplist.move(key, '', key)
        self.oplist.see(op)
        self.root.main.note.apply_scope(*pc['offset'])
        return True

    def toggle_off(self):
        self.root.main.note.clear_scope()
        for i in sorted(self.root.pcMap, key=lambda k: int(k)):
            self.oplist.move(i, '', i)
        if self.oplist.selection():
            self.oplist.see(self.oplist.selection()[0])

class ConsoleToggle(_Toggle):

    def __init__(self, parent):
        super().__init__(parent, "Console", "c")
        self.console = self.root.main.console

    def toggle_on(self):
        self.console.config(height=10)
        return True

    def toggle_off(self):
        self.console.config(height=1)

class HighlightsToggle(_Toggle):

    def __init__(self, parent):
        super().__init__(parent, "Highlights", "h")
        self.note = self.root.main.note

    def toggle_on(self):
        if not self.root.active_report or not self.root.report_key:
            return False
        contract = self.root.get_active()
        if contract not in self.root.active_report['highlights'][self.root.report_key]:
            return False
        report = self.root.active_report['highlights'][self.root.report_key][contract]
        for path, item in [(k, x) for k, v in report.items() for x in v]:
            label = Path(path).name
            self.note.mark(label, item[2], item[0], item[1])
        return True

    def toggle_off(self):
        self.note.unmark_all('green', 'red', 'yellow', 'orange')

    def reset(self):
        self.toggle_off()
        self.configure(relief="raised", background="#272727")
        self.active = False
        self.toggle()


'''
Created on 2011-09-22

@author: skardan
'''
EXPERIMENT ='CSP'
if EXPERIMENT == 'CSP':

    NUMBEROFPROBLEMS = 2

    NUMBEROFEXTRAHEADERLINES = 8 #specific to study

    FIXATIONHEADERLINES = 19
    ALLDATAHEADERLINES = 26
    EVENTSHEADERLINES = 19

    ACTIONHEADERLINES = 0
    MEDIA_OFFSET = (0, 0)

    featurelist = ['numsegments','length','numfixations','fixationrate','meanabspathangles','meanfixationduration','meanpathdistance','meanrelpathangles','stddevabspathangles','stddevfixationduration','stddevpathdistance','stddevrelpathangles']#'numsamples','sumabspathangles','sumfixationduration','sumpathdistance','sumrelpathangles']
    aoigeneralfeat = ['_fixationrate','_numfixations','_totaltimespent','_proportionnum','_proportiontime','_longestfixation']#'_timetofirstfixation','_timetolastfixation',
    aoitransfrom = ['_numtransfrom_Bottom','_numtransfrom_Graph','_numtransfrom_Toolbar','_numtransfrom_Top']
    #aoitransto = ['_numtransto_Bottom','_numtransto_Graph','_numtransto_Toolbar','_numtransto_Top']
    aoiproportion = ['_proptransfrom_Bottom','_proptransfrom_Graph','_proptransfrom_Toolbar','_proptransfrom_Top']
    #,'_proptransto_Bottom','_proptransto_Graph','_proptransto_Toolbar','_proptransto_Top']
    aoinames = ['Top','Bottom','Graph','Toolbar']
    aoifeaturelist =[]
    for aoin in aoinames:
        aoifeaturelist.extend(map(lambda x:aoin+x, aoigeneralfeat))
        aoifeaturelist.extend(map(lambda x:aoin+x, aoitransfrom))
        #aoifeaturelist.extend(map(lambda x:aoin+x, aoitransto))
        aoifeaturelist.extend(map(lambda x:aoin+x, aoiproportion))

    # Validity Threshold for segments
    VALID_PROP_THRESH = 0.85
    VALID_TIME_THRESH = 3000
    MAX_SEG_TIMEGAP = 300 #maximum gap size (ms) allowable in a segment with auto-partition option
    VALIDITY_METHOD = 3 #1: porportion; 2:time gap; 3: fixation based porportion

    MINSEGSIZE = 300 #minimum segment size in ms

    INCLUDE_HALF_FIXATIONS = False

    DEBUG = False

    ACTIIONS = [1,2,3,4,5,6,7,8,9,10,11,12,13]
    #Features

    NONTEMP_FEATURES_SEG = ['meanfixationduration', 'stddevfixationduration',
    'fixationrate', 'meanpathdistance', 'stddevpathdistance', 'meanabspathangles', 'stddevabspathangles',
    'meanrelpathangles', 'stddevabspathangles']

    NONTEMP_FEATURES_AOI = ['longestfixation', 'proportionnum', 'proportiontime',
    'proptransto', 'proptransfrom']
elif EXPERIMENT == 'PC':
    NUMBEROFEXTRAHEADERLINES = 0 #specific to study

    FIXATIONHEADERLINES = 19
    ALLDATAHEADERLINES = 24
    EVENTSHEADERLINES = 17

    ACTIONHEADERLINES = 0
    MEDIA_OFFSET = (0, 0) #for rest (0,0)
    #MEDIA_OFFSET = (-21,-97) # for subject 8-12
    #MEDIA_OFFSET = (-171, -104) #For subject 7

    # Validity Threshold for segments
    VALID_PROP_THRESH = 0.75 #(as we want to include 75%, then you need to have it one less)
    VALID_TIME_THRESH = 3000
    MAX_SEG_TIMEGAP = 300 #maximum gap size (ms) allowable in a segment with auto-partition option
    VALIDITY_METHOD = 3 #1: porportion; 2:time gap; 3: fixation based porportion

    MINSEGSIZE = 300 #minimum segment size in ms

    INCLUDE_HALF_FIXATIONS = True

    DEBUG = False

    featurelist = ['length','numfixations','fixationrate','meanabspathangles','meanfixationduration',
                   'meanpathdistance','meanrelpathangles','stddevabspathangles','stddevfixationduration',
                   'stddevpathdistance','stddevrelpathangles','numsamples','sumabspathangles',
                   'sumfixationduration','sumpathdistance','sumrelpathangles']
    aoigeneralfeat = ['_fixationrate','_numfixations','_totaltimespent','_proportionnum',
                      '_proportiontime','_longestfixation','_timetofirstfixation','_timetolastfixation']

    NONTEMP_FEATURES_AOI = ['longestfixation', 'proportionnum', 'proportiontime',
                            'proptransto', 'proptransfrom']
    NONTEMP_FEATURES_SEG = ['meanfixationduration', 'stddevfixationduration', 'fixationrate',
                            'meanpathdistance', 'stddevpathdistance', 'meanabspathangles',
                            'stddevabspathangles','meanrelpathangles', 'stddevabspathangles']

    aoitransfrom = ['_numtransfrom_Tool','_numtransfrom_Hint','_numtransfrom_HintClose','_numtransfrom_Mountain']
    aoitransto = ['_numtransto_Tool','_numtransto_Hint','_numtransto_HintClose','_numtransto_Mountain']
    aoiproportion = ['_proptransfrom_Tool','_proptransfrom_Hint','_proptransfrom_HintClose','_proptransfrom_Mountain',
                     '_proptransto_Tool','_proptransto_Hint','_proptransto_HintClose','_proptransto_Mountain']
    aoinames = ['Tool','Hint','HintClose','Mountain']
    aoifeaturelist =[]
    for aoin in aoinames:
        aoifeaturelist.extend(map(lambda x:aoin+x, aoigeneralfeat))
        aoifeaturelist.extend(map(lambda x:aoin+x, aoitransfrom))
        aoifeaturelist.extend(map(lambda x:aoin+x, aoitransto))
        aoifeaturelist.extend(map(lambda x:aoin+x, aoiproportion))


import pytesseract,cv2

def textRecog(frame):
	roi1 = frame.copy()
	roi1 = cv2.cvtColor(roi1, cv2.COLOR_BGR2GRAY)

	roi1 = cv2.resize(roi1,None,fx=2,fy=2,interpolation=cv2.INTER_LINEAR)
	level,roi1 = cv2.threshold(roi1,0,255,c2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
	config = '--oem 1 --psm 7'
	text = pytesseract.image_to_string(roi1, config=config)

	try:
		return text
	except:
		return 'failed'


#!/usr/bin/env python
import rospy
from std_msgs.msg import String

# create a node
# publish your command to /cmd

def get_input():
	key_input = raw_input("What's your move? ")

	return key_input

def main():
	rospy.init_node('talker', anonymous=True)
	pub = rospy.Publisher('cmd', String, queue_size=10)
	rate = rospy.Rate(10) # 10hz
	while not rospy.is_shutdown():
		command = get_input()
		pub.publish(command)
		rate.sleep()

if __name__ == '__main__':
    main()


import turtle, random

def square():
    for sides in range(4):
        turtle.forward(sideL)
        turtle.left(90)

def triangle():
    for sides in range(3):
        turtle.forward(sideL)
        turtle.left(120)

def pentagon():
    for sides in range(5):
        turtle.forward(sideL)
        turtle.left(72)

def hexagon():
    for sides in range(6):
        turtle.forward(sideL)
        turtle.left(60)

def circle():
    turtle.circle(sideL
                  )

def setColor():
    colorList = ["red", "green", "blue", "yellow", "orange", "black", "purple", "pink", "cyan"]
    turtle.pencolor(random.choice(colorList))
    turtle.fillcolor(random.choice(colorList))

def setStart():
    turtle.penup()
    turtle.goto(random.randint(-300, 300), random.randint(-300, 300))
    turtle.pendown()

def setOrientation():
    turtle.left(random.randint(0, 360))

def collectInputs():
    numberShapes = int(input("Enter the number of shapes desired: "))
    return numberShapes

if __name__ == "__main__":
    numberShapes = collectInputs()
    turtle.pensize(3)
    shapeList = [square, triangle, pentagon, hexagon, circle]
    for x in range(numberShapes):
        setColor()
        setStart()
        setOrientation()
        sideL = random.randint(10, 300)
        turtle.begin_fill()
        random.choice(shapeList)()
        turtle.end_fill()
    turtle.done()


from deprecated import google_cloud_setup

def delete_blob(bucket_name, blob_name):
    """Deletes a blob from the bucket."""
    bucket = google_cloud_setup.storage_client.get_bucket(bucket_name)
    blob = bucket.blob(blob_name)
    blob.delete()
    print('Blob {} deleted.'.format(blob_name))

# delete_blob('richa-cloud-516','MapReduce.docx')


#!/usr/bin/env python
# -*- encoding: utf-8 -*-

import torch.nn as nn
import torch
from loguru import logger

from .yolo_shuffle_shared_head import YOLOXSSHead
from .yolo_shuffle_pafpn import YOLOSPAFPN
from ..utils import get_model_info
from torchstat import stat

class YOLOXSS(nn.Module):
    """
    YOLOX model module. The module list is defined by create_yolov3_modules function.
    The network returns loss values from three YOLO layers during training
    and detection results during test.
    """

    def __init__(self, backbone=None, head=None):
        super().__init__()
        if backbone is None:
            backbone = YOLOSPAFPN(width=0.375)
        if head is None:
            head = YOLOXSSHead(num_classes=1, width=0.375, depthwise=True)

        self.backbone = backbone
        self.head = head

    def forward(self, x, targets=None):
        # fpn output content features of [dark3, dark4, dark5]
        fpn_outs = self.backbone(x)

        if self.training:
            assert targets is not None
            loss, iou_loss, conf_loss, cls_loss, l1_loss, num_fg = self.head(
                fpn_outs, targets, x
            )
            outputs = {
                "total_loss": loss,
                "iou_loss": iou_loss,
                "l1_loss": l1_loss,
                "conf_loss": conf_loss,
                "cls_loss": cls_loss,
                "num_fg": num_fg,
            }
        else:
            outputs = self.head(fpn_outs)

        return outputs

if __name__ == "__main__":
    model = YOLOXSS()
    img_size = (384, 224)
    img_size2 = (3, 384, 224)
    logger.info("shared-head yolox-nano-shuffle-ss detector Model Summary")
    stat(model, img_size2)
    logger.info("shared-head yolox-nano-shuffle-ss detector Model Summary:{}".format(get_model_info(model, img_size)))
    #model.cuda()
    #model.eval()

   # test_data = torch.rand(1, 3, 320, 320)
   # with torch.no_grad():
   #     test_outputs = model(test_data)
   #     print(f"the size of head is:{test_outputs.size()}")


from discord.ext import commands
import discord
import cogs.utils.checks as checks

class Edit():
    def __init__(self, bot):
        self.bot = bot

    @commands.command(hidden=True, no_pm=True)
    @checks.is_dev()
    async def edit(self, ctx, channel, id, *, new: str):
        if ctx.guild.id == 281968634086031363:
            msg = await self.bot.get_channel(int(channel)).get_message(id)
            if msg.author.id == self.bot.user.id:
                await msg.edit(content=new)
                try:
                    await ctx.message.delete()
                    await ctx.send(":thumbsup:")
                except discord.Forbidden: return
            else:
                try: await ctx.message.delete()
                except discord.Forbidden: return
                await ctx.send(self.bot.blank + "You can't give the ID of a message sent by another user!")

def setup(bot):
    bot.add_cog(Edit(bot))


#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      kol
#
# Created:     17.01.2020
# Copyright:   (c) kol 2020
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import os
import tensorflow as tf
import numpy as np

from argparse import ArgumentParser
from pathlib import Path
from cc.item_classifier import BoardItemClassifier
#from cc.board_areas import SampleBoardItemClassifier

class BoardParser:
    def __init__(self, root_dir):
        self.root_dir = root_dir
        self.img_dirs = { 'crossing': str(Path(root_dir).joinpath('cc', 'crossings')),
                          'stone': str(Path(root_dir).joinpath('cc', 'stones')),
                          'joined': str(Path(root_dir).joinpath('cc', 'joined'))
                        }

        self.model_dirs = { 'crossing': str(Path(root_dir).joinpath('tf', 'crossing_classifier')),
                            'stone': str(Path(root_dir).joinpath('tf', 'stone_classifier')),
                            'joined': str(Path(root_dir).joinpath('tf', 'joined_classifier'))}

        self.log_dir =  str(Path(root_dir).joinpath('tf', '_logs'))

        self.file_name = None
        self.n_train = None
        self.n_predict = None
        self.n_epochs = 20
        self.show_samples = False
        self.test_mode = False

        self.models = {}
        for k in self.model_dirs:
            self.models[k] = BoardItemClassifier(self.model_dirs[k], self.img_dirs[k], log_dir = self.log_dir)

    def arg_parse(self):
        model_names = [x for x in self.model_dirs]
        model_names.extend(['all'])

        parser = ArgumentParser()
        parser.add_argument("-n", "--name",
            help = 'Name of board image file')
        parser.add_argument('-i', "--show_samples",
            action = "store_true",
            help = 'Display some random images from all datasets')
        parser.add_argument('-t', "--train",
            choices = model_names,
            nargs = "*",
            help = 'Train one or several models')
        parser.add_argument('-p', "--predict",
            choices = model_names,
            nargs = "*",
            help = 'Does prediction on sample dataset images for one or several models')
        parser.add_argument('-e', "--epochs",
            type=int,
            default=20,
            help = 'Number of epochs in training')

##        parser.add_argument("--test",
##            action = "store_true",
##            help = 'Testing mode')

        args = parser.parse_args()
        self.file_name = args.name
        self.n_train = args.train
        self.n_predict = args.predict
        self.n_epochs = args.epochs
        self.show_samples = args.show_samples
        self.test_mode = False ##args.test

        if self.file_name is not None or self.n_train is not None or \
           self.show_samples or self.n_predict is not None:
            return True
        else:
            parser.print_help()
            return False

    def run(self):
        if not self.arg_parse():
            return

        if self.show_samples:
            # Show some random samples
            for k in self.models:
                self.models[k].init_datasets()
            for k in self.models:
                self.models[k].display_sample_images()

        if self.n_train is not None:
            # Retraing the models
            if len([k for k in self.n_train if k.lower() == 'all']) > 0:
                # Train all models
                for k in self.models:
                    self.train(k)
            else:
                # Train specifc models
                for k in self.n_train:
                    self.train(k)

        if self.n_predict is not None:
            # Predict the models
            if len([k for k in self.n_predict if k.lower() == 'all']) > 0:
                # Train all models
                for k in self.models:
                    self.predict(k)
            else:
                # Train specifc models
                for k in self.n_predict:
                    self.predict(k)

        if self.test_mode:
            self.test_edges()

    def train(self, name, display_history = False):
        self.models[name].init_datasets()
        self.models[name].train(epochs = self.n_epochs)
        self.models[name].save()
        if display_history:
            self.models[name].display_history()

    def predict(self, name):
        if not self.models[name].exists():
            print("Model '{}' does not exist".format(name))
        else:
            self.models[name].load()
            self.models[name].predict(display_predictions=True)

    def test_edges(self):
        pass
##        img_dir = str(Path(__file__).absolute().parent.parent)
##        self.edge_model = SampleBoardItemClassifier('crossing_classifier', img_dir)
##        self.edge_model.load()
##        self.edge_model.predict(num_samples = 1, display_predictions = True)


#!/usr/bin/env python2.7

from __future__ import division

import os, sys
import datetime, urllib2, csv, multiprocessing
import time

OHOL_PATH="%s" % os.getcwd()
sys.path.append(OHOL_PATH)

from ohol import FindOHOLStocks, MIS

# Important parameters
CAPITAL=50000
cap=CAPITAL*(80/100)
SLPct=(1.25/100)
ProfitPct=(1.25/100)

def GetTicks(price):
    tgt=round(0.05 * round(float(price * ProfitPct)/0.05), 2)
    sl=round(0.05 * round(float(price * SLPct)/0.05), 2)
    tsl=round(0.05 * round(float(price * 0.005)/0.05), 2)
    return (tgt, sl, tsl)

(BuySyms, SellSyms) = FindOHOLStocks()

if (len(BuySyms.keys())+len(SellSyms.keys())) > 0:
    cps = int(cap/(len(BuySyms.keys())+len(SellSyms.keys())))
    for bsym,value in BuySyms.items():
        n = int((cps*int(MIS[bsym]))/value)
        (tgt, sl, tsl) = GetTicks(value)
        print('Buy %d %s @ %.2f SL=%.2f Tgt=%.2f TSL=%.2f' % (n, bsym, value, tgt, sl, tsl))

    manager = multiprocessing.Manager()
    for ssym,value in SellSyms.items():
        n = int((cps*int(MIS[ssym]))/value)
        (tgt, sl, tsl) = GetTicks(value)
        print('Sell %d %s @ %.2f SL=%.2f Tgt=%.2f TSL=%.2f' % (n, ssym, value, tgt, sl, tsl))
else:
    print('No stocks shortlisted')


import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

from app import app
from apps import Formation_Player_Compasison, Team_Statistics,main_page,errorbars,Toy_Example

app.layout = html.Div([
    dcc.Location(id='url', refresh=False),
    html.Div(id='page-content')
])

@app.callback(Output('page-content', 'children'),
              [Input('url', 'pathname')])
def display_page(pathname):
    if pathname == '/formation':
         return Formation_Player_Compasison.layout
    elif pathname == '/statistics/clusters':
         return Team_Statistics.layout
    elif pathname == '/statistics':
         return errorbars.layout
    elif pathname == '/index':
        return main_page.layout
    elif pathname == '/example':
        return Toy_Example.layout
    else:
        return '404'

if __name__ == '__main__':
    app.run_server(debug=True)


#!/usr/bin/python

import threading
import time
import os
from scapy.all import *

ssid_info={}

if(len(sys.argv) > 1):
	iface=sys.argv[1]
else:
	print("please give interface name")
	exit(0)
ch=0

def pkt_handler(pkt):
	if pkt.haslayer(Dot11):
		if pkt.type == 0  and pkt.subtype == 8 :

			try:
				power=round(10*(math.log10(pkt.notdecoded[10]*pow(10,-7))),1)
			except ValueError:
				power=ssid_info[pkt.addr2][1]

			if pkt.addr2 in ssid_info:
				ssid_info[pkt.addr2][1]=power
				ssid_info[pkt.addr2][2]=ssid_info[pkt.addr2][2]+1
			else:
				ssid_info[pkt.addr2]=[pkt.info,power,1]

def sniffer():
#ssid sniffer
	sniff(iface=iface,prn=pkt_handler)

def displayer():
#displayer
	while 1:

		time.sleep(2)
		os.system("clear")
		print("%30s %18s \t\t%s\t%s" % ("SSID","BSSID","Power","Beacons"))
		for bssid,iinfo in ssid_info.items():
			print("%30s %18s \t\t%s\t%s" % (iinfo[0].decode('utf-8'),bssid,iinfo[1],iinfo[2]))
		#print(ssid_info)

t1=threading.Thread(target=sniffer,args=())
t2=threading.Thread(target=displayer,args=())

t1.start()
t2.start()


import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from scipy.optimize import minimize
from spikefit_gen_dev import get_data
import pdb

pre_vals = ['ibk7','ibti','ic1k','ica1']
end_vals = ['03010','03020','03030','03040']

guesses = [(600.,658.),(644.,622.),(650.,623.),(373.,453.)]

def gauss2d(x,y,A,xc,yc,sig,o):
    num = (x-xc)**2. + (y-yc)**2.
    denom = 2*sig**2.
    return A*np.exp(-num/denom)+o

def cen_guess(params,img):
    assert isinstance(img,np.ndarray)
    xc = params[0]
    yc = params[1]
    A = params[2]
    sig = params[3]
    o = params[4]
    xn,yn = np.round([xc,yc])
    xmin = xn-10.
    xmax = xn+10.
    ymin = yn-10.
    ymax = yn+10.
    xs = np.arange(xmin,xmax+1.)
    ys = np.arange(ymin,ymax+1.)
    sq_diffs = 0.
    for x in xs:
        for y in ys:
            image_val = img[y,x]
            calc_val = gauss2d(x,y,A,xc,yc,sig,o)
            sq_diffs += (image_val-calc_val)**2.
    return sq_diffs

#initial guess

A = 1800.
sig = 1.
o = 1.5
results = []
fnames = []
for i,pre in enumerate(pre_vals):
    #if i>0: continue
    for j,end in enumerate(end_vals):
        #if j>0: continue
        img,drz = get_data('F:/sirius_astrometry/'+pre+end+'_drz.fits',index=1,drz=False)
        xc = guesses[i][0]
        yc = guesses[i][1]
        x0 = np.array([xc,yc,A,sig,o])
        bounds = ((xc-10,xc+10),(yc-10,yc+10),(None,None),(None,None),(None,None))
        result = minimize(cen_guess,x0,args=(img,),bounds=bounds)
        results.append(result.x)
        fnames.append(pre+end+'_drz.fits')

np.savez_compressed('sir_B_cens',results=results,fnames=fnames)

pdb.set_trace()


# -*- coding: utf-8 -*-
"""
Iridas .cube LUT Format Input / Output Utilities
================================================

Defines *Iridas* *.cube* *LUT* Format related input / output utilities objects.

-   :func:`colour.io.read_LUT_IridasCube`
-   :func:`colour.io.write_LUT_IridasCube`

References
----------
-   :cite:`AdobeSystems2013b` : Adobe Systems. (2013). Cube LUT Specification.
    https://drive.google.com/open?id=143Eh08ZYncCAMwJ1q4gWxVOqR_OSWYvs
"""

import numpy as np

from colour.constants import DEFAULT_INT_DTYPE
from colour.io.luts import LUT1D, LUT3x1D, LUT3D, LUTSequence
from colour.io.luts.common import path_to_title
from colour.utilities import as_float_array, usage_warning

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013-2021 - Colour Developers'
__license__ = 'New BSD License - https://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = 'colour-developers@colour-science.org'
__status__ = 'Production'

__all__ = ['read_LUT_IridasCube', 'write_LUT_IridasCube']

def read_LUT_IridasCube(path):
    """
    Reads given *Iridas* *.cube* *LUT* file.

    Parameters
    ----------
    path : unicode
        *LUT* path.

    Returns
    -------
    LUT3x1D or LUT3d
        :class:`LUT3x1D` or :class:`LUT3D` class instance.

    References
    ----------
    :cite:`AdobeSystems2013b`

    Examples
    --------
    Reading a 3x1D *Iridas* *.cube* *LUT*:

    >>> import os
    >>> path = os.path.join(
    ...     os.path.dirname(__file__), 'tests', 'resources', 'iridas_cube',
    ...     'ACES_Proxy_10_to_ACES.cube')
    >>> print(read_LUT_IridasCube(path))
    LUT3x1D - ACES Proxy 10 to ACES
    -------------------------------
    <BLANKLINE>
    Dimensions : 2
    Domain     : [[ 0.  0.  0.]
                  [ 1.  1.  1.]]
    Size       : (32, 3)

    Reading a 3D *Iridas* *.cube* *LUT*:

    >>> path = os.path.join(
    ...     os.path.dirname(__file__), 'tests', 'resources', 'iridas_cube',
    ...     'Colour_Correct.cube')
    >>> print(read_LUT_IridasCube(path))
    LUT3D - Generated by Foundry::LUT
    ---------------------------------
    <BLANKLINE>
    Dimensions : 3
    Domain     : [[ 0.  0.  0.]
                  [ 1.  1.  1.]]
    Size       : (4, 4, 4, 3)

    Reading a 3D *Iridas* *.cube* *LUT* with comments:

    >>> path = os.path.join(
    ...     os.path.dirname(__file__), 'tests', 'resources', 'iridas_cube',
    ...     'Demo.cube')
    >>> print(read_LUT_IridasCube(path))
    LUT3x1D - Demo
    --------------
    <BLANKLINE>
    Dimensions : 2
    Domain     : [[ 0.  0.  0.]
                  [ 1.  2.  3.]]
    Size       : (3, 3)
    Comment 01 : Comments can go anywhere
    """

    title = path_to_title(path)
    domain_min, domain_max = np.array([0, 0, 0]), np.array([1, 1, 1])
    dimensions = 3
    size = 2
    table = []
    comments = []

    with open(path) as cube_file:
        lines = cube_file.readlines()
        for line in lines:
            line = line.strip()

            if len(line) == 0:
                continue

            if line.startswith('#'):
                comments.append(line[1:].strip())
                continue

            tokens = line.split()
            if tokens[0] == 'TITLE':
                title = ' '.join(tokens[1:])[1:-1]
            elif tokens[0] == 'DOMAIN_MIN':
                domain_min = as_float_array(tokens[1:])
            elif tokens[0] == 'DOMAIN_MAX':
                domain_max = as_float_array(tokens[1:])
            elif tokens[0] == 'LUT_1D_SIZE':
                dimensions = 2
                size = DEFAULT_INT_DTYPE(tokens[1])
            elif tokens[0] == 'LUT_3D_SIZE':
                dimensions = 3
                size = DEFAULT_INT_DTYPE(tokens[1])
            else:
                table.append(tokens)

    table = as_float_array(table)
    if dimensions == 2:
        return LUT3x1D(
            table,
            title,
            np.vstack([domain_min, domain_max]),
            comments=comments)
    elif dimensions == 3:
        # The lines of table data shall be in ascending index order,
        # with the first component index (Red) changing most rapidly,
        # and the last component index (Blue) changing least rapidly.
        table = table.reshape([size, size, size, 3], order='F')

        return LUT3D(
            table,
            title,
            np.vstack([domain_min, domain_max]),
            comments=comments)

def write_LUT_IridasCube(LUT, path, decimals=7):
    """
    Writes given *LUT* to given  *Iridas* *.cube* *LUT* file.

    Parameters
    ----------
    LUT : LUT3x1D or LUT3d or LUTSequence
        :class:`LUT3x1D`, :class:`LUT3D` or :class:`LUTSequence` class instance
        to write at given path.
    path : unicode
        *LUT* path.
    decimals : int, optional
        Formatting decimals.

    Returns
    -------
    bool
        Definition success.

    Warnings
    --------
    -   If a :class:`LUTSequence` class instance is passed as ``LUT``, the
        first *LUT* in the *LUT* sequence will be used.

    References
    ----------
    :cite:`AdobeSystems2013b`

    Examples
    --------
    Writing a 3x1D *Iridas* *.cube* *LUT*:

    >>> from colour.algebra import spow
    >>> domain = np.array([[-0.1, -0.2, -0.4], [1.5, 3.0, 6.0]])
    >>> LUT = LUT3x1D(
    ...     spow(LUT3x1D.linear_table(16, domain), 1 / 2.2),
    ...     'My LUT',
    ...     domain,
    ...     comments=['A first comment.', 'A second comment.'])
    >>> write_LUT_IridasCube(LUT, 'My_LUT.cube')  # doctest: +SKIP

    Writing a 3D *Iridas* *.cube* *LUT*:

    >>> domain = np.array([[-0.1, -0.2, -0.4], [1.5, 3.0, 6.0]])
    >>> LUT = LUT3D(
    ...     spow(LUT3D.linear_table(16, domain), 1 / 2.2),
    ...     'My LUT',
    ...     np.array([[-0.1, -0.2, -0.4], [1.5, 3.0, 6.0]]),
    ...     comments=['A first comment.', 'A second comment.'])
    >>> write_LUT_IridasCube(LUT, 'My_LUT.cube')  # doctest: +SKIP
    """

    if isinstance(LUT, LUTSequence):
        LUT = LUT[0]
        usage_warning('"LUT" is a "LUTSequence" instance was passed, '
                      'using first sequence "LUT":\n'
                      '{0}'.format(LUT))

    assert not LUT.is_domain_explicit(), '"LUT" domain must be implicit!'

    if isinstance(LUT, LUT1D):
        LUT = LUT.as_LUT(LUT3x1D)

    assert (isinstance(LUT, LUT3x1D) or
            isinstance(LUT, LUT3D)), '"LUT" must be a 1D, 3x1D or 3D "LUT"!'

    is_3x1D = isinstance(LUT, LUT3x1D)

    size = LUT.size
    if is_3x1D:
        assert 2 <= size <= 65536, '"LUT" size must be in domain [2, 65536]!'
    else:
        assert 2 <= size <= 256, '"LUT" size must be in domain [2, 256]!'

    def _format_array(array):
        """
        Formats given array as an *Iridas* *.cube* data row.
        """

        return '{1:0.{0}f} {2:0.{0}f} {3:0.{0}f}'.format(decimals, *array)

    with open(path, 'w') as cube_file:
        cube_file.write('TITLE "{0}"\n'.format(LUT.name))

        if LUT.comments:
            for comment in LUT.comments:
                cube_file.write('# {0}\n'.format(comment))

        cube_file.write('{0} {1}\n'.format(
            'LUT_1D_SIZE' if is_3x1D else 'LUT_3D_SIZE', LUT.table.shape[0]))

        default_domain = np.array([[0, 0, 0], [1, 1, 1]])
        if not np.array_equal(LUT.domain, default_domain):
            cube_file.write('DOMAIN_MIN {0}\n'.format(
                _format_array(LUT.domain[0])))
            cube_file.write('DOMAIN_MAX {0}\n'.format(
                _format_array(LUT.domain[1])))

        if not is_3x1D:
            table = LUT.table.reshape([-1, 3], order='F')
        else:
            table = LUT.table

        for row in table:
            cube_file.write('{0}\n'.format(_format_array(row)))

    return True


import numpy as np
import random as rd
from math import factorial

def cholesky(A):
    n=len(A)
    T=[[0 for k in range(n)]for k in range(n)]
    for i in range(n):
        for j in range(n):
            S=0 #Calculate the sum inside the calculation
            for k in range(i):
                S+=T[i][k]*T[j][k]
            if i==j: #Choose the right calculation between elements in the matrix
                T[i][i]=np.sqrt(A[i][i]-S)
            elif j>=i:
                T[j][i]=(A[i][j]-S)/T[i][i]
    return T

#Function which is creating a random matrix
def gen_mat(n,nb):
    M=np.eye(n,n)
    M[0][0]=0
    while np.linalg.det(M)==0:
        M=np.eye(n,n)
        c=0
        if (nb%2==1):
            nb-=1
        while c!=nb:
            r=rd.randint(1,100)
            i=rd.randint(0,n-1)
            j=rd.randint(0,n-1)
            while (M[i][j]!=0):
                i=rd.randint(0,n-1)
                j=rd.randint(0,n-1)
            M[i][j]=r
            M[j][i]=r
            c+=2
    return M

# https://fr.wikipedia.org/wiki/Matrice_%C3%A0_diagonale_dominante
def gen_mat_diag_dom(nb):
    n= int(np.sqrt(nb*2)) * 2 #make sure we have a matrix big enough
    M = np.zeros(shape=(n,n))
    nb_elements_extra_diag = sum(range(1,n))
    nb_elements_non_libres = nb_elements_extra_diag
    for k in range(nb):
        i = 1
        j = 0
        position = 0
        test = nb_elements_non_libres
        while i < n and position == 0: #where to place the number
            if M[i][j] == 0:
                r = rd.randint(1,int(nb_elements_extra_diag*2/test)) #try to place it rather randomly
                if r >= test:   #put a number
                    M[i][j] = rd.randint(1,100)
                    M[j][i] = M[i][j]   #symmetry
                    position = 1
                    nb_elements_non_libres -= 1
                else:
                    test = test - 1 #at the very end test = 1 thus the number will be placed for sure
                    j += 1
                    if j == i:
                        i += 1
                        j = 0
            else:  #the spot is already taken
                j += 1
                if j == i:
                    i += 1
                    j = 0

    for i in range(n): #filling the diagonals
        for j in range(n):
            if i != j:
                M[i][i] += M[i][j]
        M[i][i] += 1
    return M


import scrapy
from wa_dor.items import ReportDataItem

class DorSpider(scrapy.Spider):
    name = "dorspider"
    allowed_domains = ["wa.gov"]
    start_urls = ["https://fortress.wa.gov/dor/efile/MyAccount/TaxIncentivePublicDisclosure/"]
    #start_urls = ["https://fortress.wa.gov/dor/efile/MyAccount/TaxIncentivePublicDisclosure/ReportPublicDisclosure.aspx?TaxYear=2013"]

    def parse(self, response):
        reports = response.css("h3+ul")[1]
        for href in reports.css('li > a::attr(href)'):
            url = response.urljoin(href.extract())
            yield scrapy.Request(url, callback=self.parse_reports_list)

    def parse_reports_list(self, response):
        reportyear = response.css("h2").xpath("text()").extract()[0].strip().split()
        year = reportyear[len(reportyear)-1]
        table = response.css('table')[1]
        rows = iter(table.css("tr"))
        next(rows)
        for sel in rows:
            cells = sel.css("td")
            item = {}
            item['biz_id'] = cells[0].xpath("text()").extract()[0].strip()
            item['name'] = cells[1].xpath("text()").extract()[0].strip()
            item['url'] = cells[2].css('a::attr(href)').extract()[0]
            item['site'] = cells[2].css('a').xpath("text()").extract()[0].strip()
            item['year'] = year
            url = "https://fortress.wa.gov" + item['url']
            request = scrapy.Request(url, callback=self.parse_reports)
            request.meta['item'] = item
            yield request

    def parse_reports(self, response):
        item = response.meta['item']
        dataItem = ReportDataItem()
        employees = response.xpath("//tr")[3].css("span").xpath("text()").extract()[0]
        incentives = response.css("legend+table > tr")[1].css("table span")
        incentiveList = []
        for i in incentives:
            incentiveList.append(i.xpath("text()").extract()[0])
        finalIncentives = "; ".join(incentiveList)
        street = response.css("fieldset:nth-of-type(2)")[0].css("tr:nth-of-type(3) > td > span").xpath("text()").extract()
        city, zipcode, state = tuple(response.css("fieldset:nth-of-type(2)")[0].css("tr:nth-of-type(5) > td > span").xpath("text()").extract())
        dataItem['name'] = item['name']
        dataItem['site'] = item['site']
        dataItem['street'] = street
        dataItem['city'] = city
        dataItem['state'] = state
        dataItem['zipcode'] = zipcode
        dataItem['year'] = item['year']
        dataItem['biz_id'] = item['biz_id']
        dataItem['employees'] = employees
        dataItem['incentives'] = finalIncentives
        return dataItem


from __future__ import unicode_literals, division, absolute_import
import logging

from flexget.plugin import register_plugin, priority, DependencyError
from flexget.utils.template import RenderError, render_from_task

log = logging.getLogger('notify_osd')

class OutputNotifyOsd(object):

    def validator(self):
        from flexget import validator
        config = validator.factory()
        config.accept('boolean')
        advanced = config.accept('dict')
        advanced.accept('text', key='title_template')
        advanced.accept('text', key='item_template')
        advanced.accept('integer', key='timeout')
        return config

    def prepare_config(self, config):
        if isinstance(config, bool):
            config = {}
        config.setdefault('title_template', '{{task.name}}')
        config.setdefault('item_template', '{{title}}')
        config.setdefault('timeout', 4)
        return config

    def on_task_start(self, task, config):
        try:
            from gi.repository import Notify
        except ImportError as e:
            log.debug('Error importing Notify: %s' % e)
            raise DependencyError('notify_osd', 'gi.repository',
                'Notify module required. ImportError: %s' % e)

    @priority(0)
    def on_task_output(self, task, config):
        """
        Configuration::
            notify_osd:
                title_template: Notification title, supports jinja templating, default {{task.name}}
                item_template: Notification body, suports jinja templating, default {{title}}
                timeout: Set how long the Notification is displayed, this is an integer default = 4 seconds, Default: 4
        """
        from gi.repository import Notify

        if not task.accepted:
            return

        config = self.prepare_config(config)
        body_items = []
        for entry in task.accepted:
            try:
                body_items.append(entry.render(config['item_template']))
            except RenderError as e:
                log.error('Error setting body message: %s' % e)
        log.verbose("Send Notify-OSD notification about: %s", " - ".join(body_items))

        title = config['title_template']
        try:
            title = render_from_task(title, task)
            log.debug('Setting bubble title to :%s', title)
        except RenderError as e:
            log.error('Error setting title Notify-osd message: %s' % e)

        if not Notify.init("Flexget"):
            log.error('Unable to init libnotify.')
            return

        n = Notify.Notification.new(title, '\n'.join(body_items), None)
        timeout = (config['timeout'] * 1000)
        n.set_timeout(timeout)

        if not n.show():
            log.error('Unable to send notification for %s', title)
            return

register_plugin(OutputNotifyOsd, 'notify_osd', api_ver=2)


from xai.brain.wordbase.nouns._stylist import _STYLIST

#calss header
class _STYLISTS(_STYLIST, ):
	def __init__(self,):
		_STYLIST.__init__(self)
		self.name = "STYLISTS"
		self.specie = 'nouns'
		self.basic = "stylist"
		self.jsondata = {}


"""
build test_db.sqlite3 from res/test_db_words.txt
"""

import logging
import xml.etree.ElementTree as ET
from typing import Set

from tqdm import tqdm

import morphodict.analysis
from CreeDictionary.DatabaseManager.xml_importer import find_latest_xml_file
from CreeDictionary.utils import crkeng_xml_utils, fst_analysis_parser, shared_res_dir
from CreeDictionary.utils.crkeng_xml_utils import extract_l_str
from CreeDictionary.utils.fst_analysis_parser import partition_analysis
from CreeDictionary.utils.profiling import timed

logger = logging.getLogger(__name__)

def get_test_words() -> Set[str]:
    """
    get manually specified test words from res/test_db_words.txt

    >>> assert "wâpamêw" in get_test_words()
    """
    lines = (shared_res_dir / "test_db_words.txt").read_text().splitlines()
    words = set()
    for line in lines:
        line = line.strip()
        if line and line[0] != "#":
            words.add(line)

    return words

@timed()
def build_test_xml():
    """
    Determine relevant entries in crkeng.xml and build a smaller xml file for testing.
    """

    crkeng_file_path = find_latest_xml_file(shared_res_dir / "dictionaries")

    print(f"Building test dictionary files using {crkeng_file_path.name}")

    crkeng_root = ET.parse(str(crkeng_file_path)).getroot()

    # relevant entries in crkeng.xml file we want to determine
    relevant_xml_ls: Set[str] = set()

    xml_ls: Set[str] = set()
    crkeng_entries = crkeng_root.findall(".//e")
    for element in crkeng_entries:
        xml_l = extract_l_str(element)
        xml_ls.add(xml_l)

    test_words = get_test_words()

    print(f"Analyzing xml l elements and test words")
    word_to_analyses = morphodict.analysis.relaxed_analyzer().bulk_lookup(
        xml_ls | test_words
    )
    print("Analysis done")

    test_word_lemmas: Set[str] = set()

    for test_word in test_words:
        for analysis in word_to_analyses[test_word]:
            lemma = fst_analysis_parser.extract_lemma(analysis)
            if lemma is None:
                logger.warn(
                    "Skipping test word: %s. "
                    "Could not extract lemma from its analysis: %s",
                    test_word,
                    analysis,
                )
                continue
            test_word_lemmas.add(lemma)

    for xml_l in tqdm(xml_ls, desc="screening relevant entries in crkeng.xml"):
        if xml_l in test_words:
            relevant_xml_ls.add(xml_l)
            continue
        for xml_l_analysis in word_to_analyses[xml_l]:
            xml_lemma = partition_analysis(xml_l_analysis)[1]
            for test_word_lemma in test_word_lemmas:
                if test_word_lemma == xml_lemma:
                    relevant_xml_ls.add(xml_l)
                    break

    relevant_crkeng_entries = []

    for element in crkeng_entries:
        xml_l = extract_l_str(element)
        if xml_l in relevant_xml_ls:
            relevant_crkeng_entries.append(element)

    crkeng_xml_utils.write_xml_from_elements(
        list(crkeng_root.findall(".//source")) + relevant_crkeng_entries,
        shared_res_dir / "test_dictionaries" / "crkeng.xml",
    )


import sys

def lcs(s1,s2):
    len1=len(s1)+1
    len2=len(s2)+1
    max1=0

    arr=[]
    for i in range(0,len1):
        arr.append([])
        for j in range(0,len2):
            arr[i].append(0)

    for i in range(1,len1):
        for j in range(1,len2):
            if(s1[i-1]==s2[j-1]):
                arr[i][j]=1+arr[i-1][j-1]
                if(arr[i][j]>max1):
                    max1=arr[i][j]
                    p1=i
                    p2=j
            else:
                arr[i][j]=max(arr[i-1][j],arr[i][j-1])
                if(arr[i][j]>max1):
                    max1=arr[i][j]
                    p1=i
                    p2=j
    s=''
    ele=arr[p1][p2]
    while(ele!=0):
     if(s1[p1-1]==s2[p2-1]):
        s=s1[p1-1]+s
        ele=arr[p1-1][p2-1]
        p1-=1
        p2-=1
     elif(ele==arr[p1-1][p2]):
        ele=arr[p1-1][p2]
        p1-=1
     elif(ele==arr[p1][p2-1]):
        ele=arr[p1][p2-1]
        p2-=1

    return str(s)

def main(a):
    s1=input("Enter first string:")
    #s2=input("Enter Second string:")
    s2=list(s1)
    s2=s2.sort()
    print(s2)
    s21=''
    for l in s2:
    	s21+=l
    str1=lcs(s1,s21)
    print("Longest common subsequence is:"+str1)

if __name__=='__main__':
    main(sys.argv)


import os
import numpy as np
import cv2
import picamera
"""
  Formas de crear un modulo o una plicacion modular para, donde para
  implementarlos en el codigo se pueden hacer de dos formas que son:
"""
# Forma 1
# from manejo_cordenadas import saludar
# saludar("Este mensaje lo mando desde el modulo")
# Forma 2
import manejo_cordenadas as mcs
# mcs.saludar('Modulo activado')

file_xml = os.getcwd()+'/haarcascade_frontalface_default.xml'
# print("Rura unificada {}".format(file_xml))

load_haar = cv2.CascadeClassifier(file_xml)

color_rectangle = (82,250,150)

def found_faces(img):
    img_copy = img.copy()
    rectangle = load_haar.detectMultiScale(img_copy)
    for (x, y, w, h) in rectangle:
        cv2.rectangle(img_copy, (x, y), (x + w, y + h), 2)
        mcs.getCordenadas(x, y, w, h)
    return img_copy

cap = cv2.VideoCapture(0)

while True:
    _, video = cap.read()
    video = found_faces(video)
    # Mostramos el video
    cv2.imshow('Detectar Rostros', video)
    tecla = cv2.waitKey(1)
    if tecla == 27:
        break

cap.release()
cv2.destroyAllWindows()


import torch

class Conv1d(torch.nn.Module):
    def __init__(self, in_channle, out_channel, kernel_size=1):
        super(Conv1d, self).__init__()
        self.conv = torch.nn.Conv1d(in_channle, out_channel, kernel_size)
        self.bn = torch.nn.BatchNorm1d(out_channel)
        self.relu = torch.nn.ReLU()

    def forward(self, x):
        x = self.relu(self.bn(self.conv(x)))
        return x

class Linear(torch.nn.Module):
    def __init__(self, in_channle, out_channel):
        super(Linear, self).__init__()
        self.fc = torch.nn.Linear(in_channle, out_channel)
        self.bn = torch.nn.BatchNorm1d(out_channel)
        self.relu = torch.nn.ReLU()

    def forward(self, x):
        x = self.relu(self.bn(self.fc(x)))
        return x

class T_net(torch.nn.Module):
    def __init__(self, n_feature, npoints):
        super(T_net, self).__init__()
        self.n_feature = n_feature
        self.mlp = torch.nn.Sequential(
            Conv1d(n_feature, 64),
            Conv1d(64, 128),
            Conv1d(128, 1024)
        )
        self.mp = torch.nn.MaxPool1d(npoints)
        self.fc = torch.nn.Sequential(
            Linear(1024, 512),
            Linear(512, 256),
            torch.nn.Linear(256, n_feature * n_feature)
        )

    def forward(self, x):
        x = self.mlp(x)
        x = self.mp(x)
        x = x.view(-1, 1024)
        x = self.fc(x)
        x = x.view(-1, self.n_feature, self.n_feature)
        return x

class Feature_net(torch.nn.Module):
    def __init__(self, npoints, cls=True):
        super(Feature_net, self).__init__()
        self.cls = cls
        self.t_net3 = T_net(3, npoints)
        self.mlp1 = torch.nn.Sequential(
            Conv1d(3, 64),
            Conv1d(64, 64),
        )
        self.t_net64 = T_net(64, npoints)
        self.mlp2 = torch.nn.Sequential(
            Conv1d(64, 64),
            Conv1d(64, 128),
            Conv1d(128, 1024)
        )
        self.mp = torch.nn.MaxPool1d(npoints)

    def forward(self, x):  # x--> batch_size * npoints * 3
        x = x.transpose(1, 2)
        t_net3 = self.t_net3(x)
        x = torch.bmm(x.transpose(1, 2), t_net3).transpose(1, 2)
        x = self.mlp1(x)
        t_net64 = self.t_net64(x)
        x = torch.bmm(x.transpose(1, 2), t_net64).transpose(1, 2)
        local_feature = x
        x = self.mlp2(x)
        x = self.mp(x)
        global_feature = x.view(-1, 1024)
        if self.cls:
            return global_feature
        else:
            return local_feature, global_feature

class Cls_net(torch.nn.Module):
    def __init__(self, npoints, cls_num):
        super(Cls_net, self).__init__()
        self.feature_net = Feature_net(npoints, cls=True)
        self.fc = torch.nn.Sequential(
            Linear(1024, 512),
            Linear(512, 256),
            torch.nn.Linear(256, cls_num)
        )

    def forward(self, x):  # x--> batch_size * npoints * 3
        x = self.feature_net(x)
        x = self.fc(x)
        return x

class Seg_net(torch.nn.Module):
    def __init__(self, npoints, seg_num):
        self.npoints = npoints
        super(Seg_net, self).__init__()
        self.feature_net = Feature_net(npoints, cls=False)
        self.mlp = torch.nn.Sequential(
            Conv1d(1088, 512),
            Conv1d(512, 256),
            Conv1d(256, 128),
            torch.nn.Conv1d(128, seg_num, 1)
        )

    def forward(self, x):  # x--> batch_size * npoints * 3
        local_feature, global_feature = self.feature_net(x)
        global_feature = global_feature.view(-1, 1024, 1).repeat(
            1, 1, self.npoints)
        x = torch.cat([local_feature, global_feature], 1)
        x = self.mlp(x)
        x = x.transpose(1, 2).contiguous()
        return x

if __name__ == "__main__":
    model = Cls_net()
    print(model)


#!/bin/env python
"""
Copyright 2015 Mikołaj 'lich' Halber <lich@cock.li>
Distributed under the terms of MIT License.
See License.md for the full license text.
"""
import re
import random
import urllib
import shlex
import os
from subprocess import Popen, PIPE, check_output
try:
    check_output(["gpg", "-h"])
    ENCRYPTION_DISABLED = False
except FileNotFoundError:
    ENCRYPTION_DISABLED = True
try:
    import requests
    import argparse
except ImportError:
    print('Install argparse and request libraries.')
    exit()

def upload_files(selected_file, selected_host, only_link, file_name):
    """
    Uploads selected file to the host, thanks to the fact that
    every pomf.se based site has pretty much the same architecture.
    """
    try:
        answer = requests.post(
            url=selected_host[0]+"upload.php",
            files={'files[]':selected_file})
        if only_link:
            return selected_host[1]+(
                re.findall('"url":"(.+)",', answer.text)[0])
        else:
            return file_name+" : "+selected_host[1]+(
                re.findall('"url":"(.+)",', answer.text)[0])
    except requests.exceptions.ConnectionError:
        print(file_name + ' couldn\'t be uploaded to ' + selected_host[0])

def encrypt_files(selected_host, only_link, file_name):
    """
    Encrypts file with gpg and random generated password
    """
    if ENCRYPTION_DISABLED:
        print('For encryption please install gpg')
        exit()
    passphrase = '%030x' % random.randrange(16**30)
    source_filename = file_name
    cmd = 'gpg --batch --symmetric --cipher-algo AES256 --passphrase-fd 0 ' \
          '--output - {}'.format(source_filename)
    encrypted_output = Popen(shlex.split(cmd), stdout=PIPE, stdin=PIPE, stderr=PIPE)
    encrypted_data = encrypted_output.communicate(passphrase.encode())[0]
    return upload_files(encrypted_data, selected_host, only_link, file_name)+'#'+passphrase

def decrypt_files(file_link):
    """
    Decrypts file from entered links
    """
    if ENCRYPTION_DISABLED:
        print('For decryption please install gpg')
        exit()
    try:
        parsed_link = re.findall(r'(.*/(.*))#(.{30})', file_link)[0]
        req = urllib.request.Request(
            parsed_link[0],
            data=None,
            headers={
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) ' \
                ' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
                }
        )
        #downloads the file using fake useragent
        file_response = urllib.request.urlopen(req)
        file_to_decrypt = file_response.read()
        #decrypts the data using piping to ggp
        decrypt_r, decrypt_w = os.pipe()
        cmd = 'gpg --batch --decrypt --passphrase-fd {}'.format(decrypt_r)
        decrypt_output = Popen(shlex.split(cmd), stdout=PIPE, stdin=PIPE, stderr=PIPE, \
                         pass_fds=(decrypt_r,))
        os.close(decrypt_r)
        open(decrypt_w, 'w').write(parsed_link[2])
        decrypted_data, stderr = decrypt_output.communicate(file_to_decrypt)
        with open(parsed_link[1], 'wb') as decrypted_file:
            decrypted_file.write(decrypted_data)
        return parsed_link[1] + ' is decrypted and saved.'
    except IndexError:
        return 'Please enter valid link'

def main():
    """
    Creates arguments, and list of working clones
    """
    parser = argparse.ArgumentParser(
        description='Uploads selected file to working pomf.se clone')
    parser.add_argument('files', metavar='file', nargs='+', type=str,
                        help='Files to upload')
    parser.add_argument('-c', metavar='host number', type=int,
                        dest='host', default=-1,
                        help=('Select hosting: 0 - 1339.cf, 1 - bucket.pw,'
                              ' 2 - xpo.pw, 3 - pomf.cat, 4 - pomf.hummingbird.moe.'))
    parser.add_argument('-l', dest='only_link', action='store_const',
                        const=True, default=False,
                        help='Changes output to just link to the file')
    parser.add_argument('-e', dest='encrypt', action='store_const',
                        const=True, default=False,
                        help='Encrypts then uploads the files.')
    parser.add_argument('-d', dest='decrypt', action='store_const',
                        const=True, default=False,
                        help='Decrypts files from links with encrypted files')
    args = parser.parse_args()
    #fixme check if clone is active or not.
    clone_list = [
        ["http://1339.cf/", "http://a.1339.cf/"],
        ["https://bucket.pw/", "https://dl.bucket.pw/"],
        ["http://xpo.pw/", "http://u.xpo.pw/"],
        ["http://pomf.cat/", "http://a.pomf.cat/"],
        ["http://pomf.hummingbird.moe/", "http://a.pomf.hummingbird.moe/"]
    ]
    #upload every file selected to random or chosen host
    if args.decrypt:
        for i in args.files:
            print(decrypt_files(i))
            exit()
    try:
        for i in args.files:
            if args.host+1 and not args.encrypt:
                print(upload_files(open(i, 'rb'), \
                      clone_list[args.host], args.only_link, i))
            elif args.encrypt and args.host+1:
                print(encrypt_files(clone_list[args.host], args.only_link, i))
            elif args.encrypt:
                print(encrypt_files(clone_list[random.randrange( \
                        0, len(clone_list))], args.only_link, i))
            else:
                print(upload_files(open(i, 'rb'), clone_list[random.randrange( \
                        0, len(clone_list))], args.only_link, i))
        exit()
    except IndexError:
        print('Please enter valid server number.')
        exit()
    except FileNotFoundError:
        print('Plese enter valid file.')

if __name__ == '__main__':
    main()


import datetime
import gzip
import os
import sys
import unittest

import pytz
import numpy as np
import pandas as pd

from cerebralcortex.data_processor.feature.rip_rsa import rip_window_feature_computation
from cerebralcortex.data_processor.signalprocessing.rip import compute_peak_valley

from cerebralcortex.data_processor.feature.ecg import ecg_feature_computation, lomb, heart_rate_power
from cerebralcortex.data_processor.signalprocessing.ecg import compute_rr_intervals

from cerebralcortex.kernel.datatypes.datapoint import DataPoint
from cerebralcortex.kernel.datatypes.datastream import DataStream
from cerebralcortex.data_processor.signalprocessing.window import window_sliding

from pprint import pprint
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.style.use('ggplot')

from collections import OrderedDict

import warnings
warnings.simplefilter("error")

class TestAllFeatures(unittest.TestCase):
	@classmethod
	def setUpClass(cls):
		super(TestAllFeatures, cls).setUpClass()
		tz = pytz.timezone('US/Eastern')
		# Load RIP
		rip = []
		rip_sampling_frequency = 64.0 / 3
		with gzip.open(os.path.join(os.path.dirname(__file__), 'res/rip.csv.gz'), 'rt') as f:
			for l in f:
				values = list(map(int, l.split(',')))
				rip.append(
					DataPoint.from_tuple(datetime.datetime.fromtimestamp(values[0] / 1000000.0, tz=tz), values[1]))
		# Load ECG
		ecg = []
		ecg_sampling_frequency = 64.0
		with gzip.open(os.path.join(os.path.dirname(__file__), 'res/ecg.csv.gz'), 'rt') as f:
			for l in f:
				values = list(map(int, l.split(',')))
				ecg.append(
					DataPoint.from_tuple(datetime.datetime.fromtimestamp(values[0] / 1000000.0, tz=tz), values[1]))
		# Dataframe
		ecg_date = np.array([np.nan]*len(ecg)).astype(datetime.datetime)
		ecg_sample = np.array([np.nan]*len(ecg))
		rip_date = np.array([np.nan]*len(rip)).astype(datetime.datetime)
		rip_sample = np.array([np.nan]*len(rip))
		for i in range(len(ecg)):
			ecg_date[i] = ecg[i].start_time
			ecg_sample[i] = ecg[i].sample
		for i in range(len(rip)):
			rip_date[i] = rip[i].start_time
			rip_sample[i] = rip[i].sample

		df_ecg = pd.DataFrame(index=np.arange(len(ecg)),
							  data = ecg_sample,
							  columns= ['sample'])
		df_ecg['Date'] = ecg_date
		df_rip = pd.DataFrame(index=np.arange(len(rip)),
							  data = rip_sample,
							  columns =['sample'])
		df_rip['Date'] = rip_date

		print (df_rip.head())
		print (df_rip.tail())
		print (df_ecg.head())
		print (df_ecg.tail())

		cls.df_ecg = df_ecg
		cls.df_rip = df_rip

		cls.sampling_frequency = {}
		cls.sampling_frequency['ecg'] = 64.0
		cls.sampling_frequency['rip'] = 64.0 / 3

	def test_features_window(self):
		# path include stress intervention
		path_stressInter = '/home/hyeok/research/md2k/code/stressIntervension'
		sys.path.append(path_stressInter)
		from util.explore import features_window

		window_size = 60
		window_offset = 30

		features = OrderedDict()
		for key, data in features_window(self, self.df_ecg, self.df_rip, window_size, window_offset):
			features[key] = data

		items = list(features.items())
		pprint (items[0])
		pprint (items[1])
		pprint (items[-2])
		pprint (items[-1])

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


# William Leiby
# Project 6
# Due Date: 03/12/19
# Submission Date: 03/12/19

# Psedocode

# Import os and shutil modules.

# Assign previous_album to the empty string("").
# Open index file, call the file object fin.
# Read first line of input file.
# While input file not empty
# Use split to obtain fields.
# Assign zeroth field to folder_name.
# Assign first field to original_photo_name
# If previous_album != folder_name
# Use os.mkdir to create new folder named folder_name.
# Initialize counter to 1.
# Assign folder_name to previous_album
# End.

# Next two lines are literal Python statements.
# new_photo_name = "{:s}/{:03d}-{:s}". \
#     format(folder_name, counter, original_photo_name)
# Use shutil.copy method to copy "originals/" + original_photo_name
# to new_photo_name.
# Add one to counter.
# Read next line of input file.
# End.

# imports
import os
import shutil

# Assign previous_album to the empty string("").
previous_album = ""
# Open index file, call the file object fin.
fin = open("index.txt", "r")
# Read first line of input file.
line = fin.readline()
# While input file not empty
while line != "":
    # Use split to obtain fields.
    fields = line.split(",")
    # Assign zeroth field to folder_name.
    folder_name = fields[0].strip()
    # Assign first field to original_photo_name
    original_photo_name = fields[1].strip()
    # If previous_album != folder_name
    if previous_album != folder_name:
        # Use os.mkdir to create new folder named folder_name.
        os.mkdir(folder_name)
        # Initialize counter to 1.
        counter = 1
        # Assign folder_name to previous_album
        previous_album = folder_name
    # Next two lines are literal Python statements.
    new_photo_name = "{:s}/{:03d}-{:s}".format(
        folder_name, counter, original_photo_name)
    # Use shutil.copy method to copy "originals/" + original_photo_name to new_photo_name
    shutil.copy("originals/" + original_photo_name, new_photo_name)
    # Add one to counter.
    counter += 1
    # Read next line of input file.
    fin.readline()


class Solution:
    def countPyramids(self, grid) -> int:
        m, n = len(grid), len(grid[0])
        if m < 2 or n < 3:
            return 0

        def helper(a):
            dp = [[0 for j in range(n)] for i in range(m)]
            for i in range(1, m):
                for j in range(1, n - 1):
                    if a[i][j] == 0 or a[i - 1][j - 1] == 0 or a[i - 1][j] == 0 or a[i - 1][j + 1] == 0:
                        continue
                    dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j + 1]) + 1
            return sum([sum(r) for r in dp])

        return helper(grid) + helper([[grid[i][j] for j in range(n - 1, -1, -1)] for i in range(m - 1, -1, -1)])

s = Solution()
# print(s.countPyramids([[0, 0, 1, 0, 0],
#                        [0, 1, 1, 1, 0],
#                        [1, 1, 1, 1, 1]]))
print(s.countPyramids([[1, 1, 1, 1, 0],
                       [1, 1, 1, 1, 1],
                       [1, 1, 1, 1, 1],
                       [0, 1, 0, 0, 1]]))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 26 10:55:35 2019

@author: tungutokyo
"""

import pandas as pd
import numpy as np

pd.options.mode.chained_assignment = None
pd.set_option("display.max_columns", 60)

import matplotlib.pyplot as plt
import seaborn as sns

from IPython.core.pylabtools import figsize
import plotly
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as ff
import plotly.tools as tls
import cufflinks as cf

###############################################################################
######################### Data Cleaning and Formatting ########################
###############################################################################

data_train = pd.read_csv("train.csv")
data_test = pd.read_csv("test.csv")
data_train.info()

###############################################################################
########################## Data Preprocessing #################################
###############################################################################

data_train = data_train.drop(columns = ["item_id", "item_tag_hash"])

data_train["category_label"] = data_train["category_class"].apply(lambda x: "Cat_0"
                                                            if x == 0 else "Cat_1"
                                                            if x == 1 else "Cat_2"
                                                            if x == 2 else "Cat_3"
                                                            if x == 3 else "Cat_4")
data_train["category_label"] = pd.Categorical(data_train["category_label"],
                                              categories=["Cat_0", "Cat_1", "Cat_2", "Cat_3", "Cat_4"])

data_train["time"] = pd.to_datetime(data_train["listing_at"], errors="coerce")
data_train["month"] = data_train["time"].dt.month
data_train["weekofyear"] = data_train["time"].dt.weekofyear
data_train["dayofweek"] = data_train["time"].dt.weekday
data_train["hourofday"] = data_train["time"].dt.hour

data_train["Month"] = data_train["month"].apply(lambda x: "Jan"
                                                  if x == 1 else "Feb")
data_train["Month"] = pd.Categorical(data_train["Month"],
                                      categories=["Jan", "Feb"])

data_train["WeekofYear"] = data_train["weekofyear"].apply(lambda x: "4th_week"
                                                      if x == 4 else "5th_week")
data_train["WeekofYear"] = pd.Categorical(data_train["WeekofYear"],
                                              categories=["4th_week", "5th_week"])

data_train["DayofWeek"] = data_train["dayofweek"].apply(lambda x: "Mon"
                                                          if x == 0 else "Tue"
                                                          if x == 1 else "Wed"
                                                          if x == 2 else "Thu"
                                                          if x == 3 else "Fri"
                                                          if x == 4 else "Sat"
                                                          if x == 5 else "Sun")
data_train["DayofWeek"] = pd.Categorical(data_train["DayofWeek"],
                              categories=["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"])

data_train["HourofDay"] = data_train["hourofday"].apply(lambda x: "0-5AM"
                                                          if x <= 5 else "6-12AM"
                                                          if x <= 12 else "1-5PM"
                                                          if x <= 17 else "6-12PM")
data_train["HourofDay"] = pd.Categorical(data_train["HourofDay"],
                              categories=["0-5AM", "6-12AM", "1-5PM", "6-12PM"])

data_train["TypeofPrice"] = data_train["price"].apply(lambda x: "Cheap"
                                                          if x <= 948 else "Middle"
                                                          if x <= 1193.75 else "Expensive")
data_train["TypeofPrice"] = pd.Categorical(data_train["TypeofPrice"],
                              categories=["Cheap", "Middle", "Expensive"])

data_train["TypeofSize"] = data_train["size"].apply(lambda x: "Small"
                                                  if x <= 4 else "Middle"
                                                  if x <= 12 else "Large")
data_train["TypeofSize"] = pd.Categorical(data_train["TypeofSize"],
                          categories=["Small", "Middle", "Large"])

data_train_final = data_train.drop(columns = ["category_class", "sold_price", "listing_at",
                                              "time", "month", "weekofyear", "dayofweek",
                                              "hourofday"])

###############################################################################
############################ Cluster Analysis #################################
###############################################################################

"""
Subset data without time varibles
"""
numeric_pre_subset = data_train[["price", "size"]]
categorial_pre_subset = data_train_final[["category_label", "condition", "area_name"]]
categorial_pre_subset = pd.get_dummies(categorial_pre_subset)
features_pre = pd.concat([categorial_pre_subset, numeric_pre_subset], axis=1)

"""
Subset data with time variables
"""
numeric_subset = data_train_final[["price", "size"]]
categorial_subset = data_train_final[["category_label", "condition", "area_name",
                                      "Month", "WeekofYear", "DayofWeek", "HourofDay"]]
categorial_subset = pd.get_dummies(categorial_subset)
features = pd.concat([categorial_subset, numeric_subset], axis=1)

X = features.drop(columns = ["category_label_Cat_0", "category_label_Cat_1", "category_label_Cat_2",
                             "category_label_Cat_3", "category_label_Cat_4"])

from sklearn import cluster
cluster_pred = cluster.KMeans(n_clusters = 3, max_iter = 1000, random_state=42, n_jobs=-1).fit_predict(X)

"""
Hierarchical clustering - Agglomerative Clustering
"""
cluster_pred = cluster.AgglomerativeClustering(n_clusters = 3, linkage="ward").fit_predict(X)

"""
Gaussian Mixture Model
"""
from sklearn import mixture
gmm = mixture.GaussianMixture(n_components = 3, covariance_type = "full",
                                init_params = "kmeans", max_iter = 10000, random_state=42).fit(X)
cluster_pred = gmm.predict(X)

data_train = data_train.assign(Price_Size = cluster_pred)
data_train["Group"] = data_train["Price_Size"].apply(lambda x: "A"
                                                      if x == 0 else "B"
                                                      if x == 1 else "C"
                                                      )
data_train["Group"] = pd.Categorical(data_train["Group"],
                      categories=["A", "B", "C"])

data_train_final = data_train.drop(columns = ["category_class", "sold_price", "listing_at",
                                              "time", "month", "weekofyear", "dayofweek", "Price_Size",
                                              "hourofday"])

###############################################################################
################### Lower dimensional visualization ###########################
###############################################################################

from sklearn import decomposition
from datetime import datetime as dt

def standardize(data):
    from sklearn.preprocessing import StandardScaler
    data_std = StandardScaler().fit_transform(data)
    return data_std

def pca_vis(data, labels):
    st = dt.now()
    pca = decomposition.PCA(n_components=6)
    pca_reduced = pca.fit_transform(data)

    print("The shape of transformed data", pca_reduced.shape)
    print(pca_reduced[0:6])

    pca_data = np.vstack((pca_reduced.T, labels)).T
    print("The shape of data with labels", pca_data.shape)

    pca_df = pd.DataFrame(data=pca_data, columns=("1st_principal_component", "2nd_principal_component",
                                                  "3rd_principal_component", "4th_principal_component",
                                                  "5th_principal_component", "6th_principal_component", "labels"))
    print(pca_df.head())

    sns.FacetGrid(pca_df, hue="labels", height=8).map(sns.scatterplot,
            "1st_principal_component", "2nd_principal_component", edgecolor="w").add_legend()
    plt.xlabel("1st_principal_component ({}%)".format(round(pca.explained_variance_ratio_[0]*100),2), fontsize=15)
    plt.ylabel("2nd_principal_component ({}%)".format(round(pca.explained_variance_ratio_[1]*100),2), fontsize=15)

    sns.FacetGrid(pca_df, hue="labels", height=8).map(sns.scatterplot,
            "3rd_principal_component", "4th_principal_component", edgecolor="w").add_legend()
    plt.xlabel("3rd_principal_component ({}%)".format(round(pca.explained_variance_ratio_[2]*100),2), fontsize=15)
    plt.ylabel("4th_principal_component ({}%)".format(round(pca.explained_variance_ratio_[3]*100),2), fontsize=15)

    sns.FacetGrid(pca_df, hue="labels", height=8).map(sns.scatterplot,
            "5th_principal_component", "6th_principal_component", edgecolor="w").add_legend()
    plt.xlabel("5th_principal_component ({}%)".format(round(pca.explained_variance_ratio_[4]*100),2), fontsize=15)
    plt.ylabel("6th_principal_component ({}%)".format(round(pca.explained_variance_ratio_[5]*100),2), fontsize=15)
    plt.show()
    print("Time taken to perform Principal Component Analysis: {}".format(dt.now()-st))
    return pca_df

def pca_redu(data, num_components):
    pca = decomposition.PCA(n_components=num_components)
    pca_data = pca.fit_transform(data)
    plt.figure(figsize=(15,10))
    plt.plot(np.cumsum(pca.explained_variance_ratio_))
    plt.axis('tight')
    plt.grid()
    plt.axhline(0.95, c='r')
    plt.xlabel("Number of components", fontsize=15)
    plt.ylabel("Cumulative explained variance", fontsize=15)
    plt.legend()
    return pca_data

numeric_subset = data_train_final[["price", "size"]]
categorial_subset = data_train_final[["category_label", "condition", "area_name",
                                      "Month", "WeekofYear", "DayofWeek", "HourofDay"]]
categorial_subset = pd.get_dummies(categorial_subset)
features = pd.concat([categorial_subset, numeric_subset], axis=1)

"""
With time variable
"""
X = features.drop(columns = ["category_label_Cat_0", "category_label_Cat_1", "category_label_Cat_2",
                             "category_label_Cat_3", "category_label_Cat_4"])
#y = data_train_final["category_label"]
y = data_train_final["Group"]

X = standardize(X)
pca_full_result = pd.DataFrame(pca_redu(X,4))
pca_result = pca_vis(X,y)

Python for Data Science/Cargo_shipping_categories/Data_Analysis_Cluster.py


# -*- coding: utf-8 -*-
#  @date    161021 - Fixing format error by machine model
"""
APIs for machine management.
"""
import time
import datetime

from django.conf import settings
from mongoengine.errors import NotUniqueError, ValidationError

from machine.models import (
    Machine, MachinePerformance,
    MachineMaintainLog, MachineMaintainPlan,
    MachineCapacity, MachineDailyLossRate
)
from worker.models import Worker
from product.models import Product
from auth_sys.models import MyUser
from utils.static_data import (
    MACHINE_TYPE_NAME, MACHINE_MAINTENANCE_MAP, TYPE_4_MACHINE_ID,
    MACHINE_TYPE_MAP)
from utils import data_exportor

def get_list(usr_input):
    """Get machine data."""
    option = usr_input.get('option', 'level')
    result = []

    if option == 'level':
        db_data = Machine.objects
    elif option == 'perf':
        db_data = MachinePerformance.objects
    else:
        return False, result

    for row in db_data:
        data = {
            'id': str(row.id),
            'machine_id': row.machine_id,
            'updated_at': row.updated_at.strftime("%Y-%m-%d %H:%M:%S"),
        }

        if option == 'level':
            data['machine_model'] = row.machine_model
            data['machine_type'] = MACHINE_TYPE_MAP.get(row.machine_type, '未知')
            data['level_a_lower_limit'] = row.level_a_lower_limit
            data['level_b_lower_limit'] = row.level_b_lower_limit
            data['level_c_lower_limit'] = row.level_c_lower_limit

        if option == 'perf':
            data['product_code'] = row.product_code
            data['daily_target_count'] = row.daily_target_count
            data['daily_perf_count'] = row.daily_perf_count

        result += [data]

    return True, result

def add_one(usr_input):
    """Add a machine."""
    machine_id = usr_input.get('machine_id', '')
    machine_model = usr_input.get('machine_model', '')
    machine_type = usr_input.get('machine_type', '')

    # format check
    try:
        if int(machine_type) in range(-1, 6):
            machine_type = int(machine_type)
        else:
            raise ValueError

        l_a_lower_limit = int(usr_input.get('level_a', None))
        l_b_lower_limit = int(usr_input.get('level_b', None))
        l_c_lower_limit = int(usr_input.get('level_c', None))
    except (ValueError, TypeError):
        return False

    # value check
    if not machine_id or (
       l_a_lower_limit < 0 or l_b_lower_limit < 0 or l_c_lower_limit < 0):
        return False

    try:
        Machine(
            machine_id=machine_id,
            machine_model=machine_model,
            machine_type=machine_type,
            level_a_lower_limit=l_a_lower_limit,
            level_b_lower_limit=l_b_lower_limit,
            level_c_lower_limit=l_c_lower_limit
        ).save()
    except (NotUniqueError, ValidationError):
        return False

    return True

def update_one(usr_input):
    """Update a machine information."""
    machine_id = usr_input.get('machine_id', '')
    machine_model = usr_input.get('machine_model', '')
    machine_type = usr_input.get('machine_type', '')

    # format check
    try:
        if int(machine_type) in range(-1, 6):
            machine_type = int(machine_type)
        else:
            raise ValueError

        l_a_lower_limit = int(usr_input.get('level_a', None))
        l_b_lower_limit = int(usr_input.get('level_b', None))
        l_c_lower_limit = int(usr_input.get('level_c', None))
    except (ValueError, TypeError):
        return False

    # value check
    if not machine_id or (
       l_a_lower_limit < 0 or l_b_lower_limit < 0 or l_c_lower_limit < 0):
        return False

    try:
        my_machine = Machine.objects(machine_id=machine_id).get()
        my_machine.machine_model = machine_model
        my_machine.machine_type = machine_type
        my_machine.level_a_lower_limit = l_a_lower_limit
        my_machine.level_b_lower_limit = l_b_lower_limit
        my_machine.level_c_lower_limit = l_c_lower_limit
        my_machine.save()
    except (Machine.DoesNotExist, ValidationError):
        return False

    return True

def delete_one(usr_input):
    """Delete a machine information."""
    machine_id = usr_input.get('machine_id', '')

    if machine_id:
        Machine.objects(machine_id=machine_id).delete()

    return True

def get_maintenance_log(usr_input, format=None):
    """Get maintenance log by month."""
    def merge_logs(db_data):
        """
        Merge maintenance log if maintenance start time is the same by machine.
        """
        maintain_log_group = {}
        result = []
        for row in db_data:
            # who fixing
            try:
                who_control = Worker.objects(id=row.worker_db_id).first()
            except ValidationError:
                who_control = None
            # -who

            # mapping machine id and maintenance code
            m_type = ''
            m_code = ''
            if row.machine_id in TYPE_4_MACHINE_ID:
                m_type = '選別'
            else:
                m_type = MACHINE_TYPE_NAME.get(row.machine_id[0], '')

            if m_type:
                m_code = MACHINE_MAINTENANCE_MAP[m_type].get(
                    row.maintenance_code, '維修代碼未定義')
            else:
                m_code = '機器未定義'
            # -mapping

            # calculate total time
            fix_diff = (datetime.datetime.utcfromtimestamp(
                row.end_maintain_at) -
                datetime.datetime.utcfromtimestamp(row.start_maintain_at))
            # -calculate

            g_key = "{}|{}".format(row.machine_id, row.start_maintain_at)

            if g_key in maintain_log_group:
                # merge
                log_group = maintain_log_group[g_key]
                log_group['m_code'] = "{}<br>{}".format(
                    log_group['m_code'], m_code)
                log_group['end_time'] = time.strftime(
                    '%Y-%m-%d %H:%M:%S',
                    time.localtime(row.end_maintain_at + settings.UTC_OFFSET))
                log_group['who_end'] = (
                    who_control.name if who_control else '查無此員工')
                log_group['total_time'] = str(datetime.timedelta(
                    seconds=fix_diff.total_seconds())),

            else:
                # new record
                maintain_log_group[g_key] = {
                    'machine_id': row.machine_id,
                    'm_code': m_code,
                    'who_start': who_control.name if who_control else '查無此員工',
                    'who_end': who_control.name if who_control else '查無此員工',
                    'start_time': time.strftime(
                        '%Y-%m-%d %H:%M:%S',
                        time.localtime(
                            row.start_maintain_at + settings.UTC_OFFSET)),
                    'end_time': time.strftime(
                        '%Y-%m-%d %H:%M:%S',
                        time.localtime(row.end_maintain_at)),
                    'total_time': str(datetime.timedelta(
                        seconds=fix_diff.total_seconds())),
                }

        for log in maintain_log_group.values():
            result += [log]

        return result

    def get_all_logs(db_data):
        """Get all logs for display."""
        result = []
        for row in db_data:
            try:
                who_control = Worker.objects(id=row.worker_db_id).first()
            except ValidationError:
                who_control = None

            # mapping machine id and maintenance code
            m_type = ''
            m_code = ''
            if row.machine_id in ('GT-33', 'GT-34', 'GT-35'):
                m_type = '選別'
            else:
                m_type = MACHINE_TYPE_NAME.get(row.machine_id[0], '')

            if m_type:
                m_code = MACHINE_MAINTENANCE_MAP[m_type].get(
                    row.maintenance_code, '維修代碼未定義')
            else:
                m_code = '機器未定義'
            # -mapping

            fix_diff = (datetime.datetime.utcfromtimestamp(
                row.end_maintain_at) -
                datetime.datetime.utcfromtimestamp(row.start_maintain_at))
            result += [{
                'machine_id': row.machine_id,
                'm_code': m_code,
                'who_start': who_control.name if who_control else '查無此員工',
                'who_end': who_control.name if who_control else '查無此員工',
                'start_time': time.strftime(
                    '%Y-%m-%d %H:%M:%S',
                    time.localtime(
                        row.start_maintain_at + settings.UTC_OFFSET)),
                'end_time': time.strftime(
                    '%Y-%m-%d %H:%M:%S',
                    time.localtime(
                        row.end_maintain_at + settings.UTC_OFFSET)),
                'total_time': str(datetime.timedelta(
                    seconds=fix_diff.total_seconds())),
            }]

        return result

    result = []
    query_date = usr_input.get('query_date',
                               datetime.datetime.utcnow().strftime("%Y-%m"))
    db_data = MachineMaintainLog.objects(fix_date__startswith=query_date)
    result = merge_logs(db_data)

    if format == 'csv':
        output = data_exportor.get_maintenance_log(log_list=result)
        return True, output

    return True, result

def get_m_plan(usr_input):
    """Get maintain plan list."""
    result = []

    total_quality_goods = 0
    new_count = 0

    for row in MachineMaintainPlan.objects:
        if row.is_done:
            continue

        my_machine = MachineCapacity.objects(machine_id=row.machine_id).first()
        if my_machine:
            total_quality_goods = my_machine.total_of_product

        if row.last_replace_qt_count == 0:
            new_count = total_quality_goods + row.countdown_qty
        else:
            new_count = row.last_replace_qt_count + row.countdown_qty

        result += [{
            'id': str(row.id),
            'machine_id': row.machine_id,
            'process': row.process,
            'next_count': new_count,
            'desc': row.desc,
            'total_quality_goods': total_quality_goods,
            'next_alerm_count': row.countdown_qty + total_quality_goods,
            'is_done': row.is_done,
            'countdown_qty': row.countdown_qty,
            'done_counter': row.done_counter,
            'qt_after_last_time': (
                row.last_replace_qt_count - total_quality_goods),
        }]

    return True, result

def add_m_plan(usr_input):
    """Added maintenance plan."""
    machine_id = usr_input.get('machine_id', None)
    process = usr_input.get('process', '')
    countdown_qty = usr_input.get('countdown_qty', 0)
    desc = usr_input.get('desc', '')

    # format verification
    try:
        countdown_qty = int(countdown_qty)
    except (TypeError, ValueError):
        return False

    # get last_replace_qt_count value:
    last_replace_qt_count = 0

    my_machine = MachineCapacity.objects(machine_id=machine_id).first()
    if my_machine:
        last_replace_qt_count = my_machine.total_of_product
    # -last_replace_qt_count

    MachineMaintainPlan(
        machine_id=machine_id,
        process=process,
        countdown_qty=countdown_qty,
        desc=desc,
        last_replace_qt_count=last_replace_qt_count,
        is_done=False,
        done_counter=0,    # default is 0
        done_worker_id='',
        done_at=None,
    ).save()

    return True

def update_m_plan(usr_input, username):
    """Updated maintenance plan."""
    plan_id = usr_input.get('id', None)
    is_done = usr_input.get('is_done', None)

    try:
        my_plan = MachineMaintainPlan.objects(id=plan_id).first()
        if my_plan:
            try:
                if is_done == '1':
                    # Update plan status to finish
                    my_machine = MachineCapacity.objects(
                        machine_id=my_plan.machine_id).first()
                    if my_machine:
                        my_plan.last_replace_qt_count = (
                            my_machine.total_of_product)

                    my_plan.is_done = True
                    my_plan.done_counter += 1
                    my_plan.done_at = datetime.datetime.utcnow()

                    my_user = MyUser.objects.filter(username=username).get()
                    my_plan.done_worker_id = my_user.id

                elif is_done == '0':
                    my_plan.is_done = False
                else:
                    raise ValueError

                for item in ['process', 'countdown_qty', 'desc']:
                    value = usr_input.get(item, None)
                    if value:
                        if item == 'countdown_qty':
                            setattr(my_plan, item, int(value))
                        else:
                            setattr(my_plan, item, value)

                my_plan.save()
                return True
            except (TypeError, ValueError):
                pass
            except MyUser.DoesNotExist:
                pass
    except ValidationError:
        pass

    return False

def delete_m_plan(usr_input):
    """Delete machine maintenance plan."""
    plan_id = usr_input.get('id', None)
    try:
        MachineMaintainPlan.objects(id=plan_id).delete()
    except ValidationError:
        return False

    return True

def add_p_plan(usr_input):
    """Added machine performance plan."""
    machine_id = usr_input.get('machine_id', None)
    product_code = usr_input.get('product_code', '')
    daily_target_count = usr_input.get('daily_target_count', 0)
    daily_perf_count = usr_input.get('daily_perf_count', 0)

    # format verification
    try:
        daily_target_count = int(daily_target_count)
        daily_perf_count = int(daily_perf_count)
    except (TypeError, ValueError):
        return False

    # checking duplication
    my_machine = MachinePerformance.objects(machine_id=machine_id,
                                            product_code=product_code).first()
    if my_machine:
        return False
    # -checking

    MachinePerformance(
        machine_id=machine_id,
        product_code=product_code,
        daily_target_count=daily_target_count,
        daily_perf_count=daily_perf_count,
    ).save()

    return True

def delete_p_plan(usr_input):
    """Delete machine performance standard plan."""
    plan_id = usr_input.get('id', None)

    try:
        MachinePerformance.objects(id=plan_id).delete()
    except ValidationError:
        return False

    return True

def update_p_plan(usr_input):
    """Update machine perofrmance standard plan."""
    plan_id = usr_input.get('id', None)
    product_code = usr_input.get('product_code', None)
    daily_target_count = usr_input.get('daily_target_count', None)
    daily_perf_count = usr_input.get('daily_perf_count', None)

    # format verification
    try:
        daily_target_count = int(daily_target_count)
        daily_perf_count = int(daily_perf_count)
    except (TypeError, ValueError):
        return False

    try:
        my_machine = MachinePerformance.objects(id=plan_id).get()
    except (ValidationError, MachinePerformance.DoesNotExist):
        return False

    is_updated = False
    try:
        if product_code:
            my_machine.product_code = product_code
            is_updated = True

        if daily_target_count is not None:
            my_machine.daily_target_count = int(daily_target_count)
            is_updated = True

        if daily_perf_count is not None:
            my_machine.daily_perf_count = int(daily_perf_count)
            is_updated = True

    except (TypeError, ValueError):
        return False

    if is_updated:
        my_machine.save()
        return True

    return False

def get_worker_machine_report(usr_input, format='xls'):
    """Supported report by worker and machine for download."""
    # output = data_exportor.get_w_m_performance_report(file_format=format)
    # return True, output

    now = datetime.datetime.now()
    orig_path = (
        '{path}{year}-{month:02}.xls'.format(
            path=settings.MACHINE_WORKER_PERF_XLS_PATH,
            year=now.year, month=now.month)
    )
    try:
        return True, open(orig_path, 'rb')
    except OSError:
        pass

    return False, None

def get_daily_loss_rate(usr_input, format=None):
    """Get machine's daily loss rate in production."""
    machine_type = usr_input.get('machine_type', '')
    start_date = usr_input.get('start_date',
                               datetime.datetime.utcnow().strftime("%Y-%m-%d"))
    end_date = usr_input.get('end_date',
                             datetime.datetime.utcnow().strftime("%Y-%m-%d"))

    def merge_daily(db_data):
        # get product cost
        product_cost_map = {}
        for p in Product.objects:
            product_cost_map[p.p_code] = p.cost
        # -get

        # get machine type
        machine_inf = {}
        for r in Machine.objects:
            machine_inf[r.machine_id] = r.machine_type
        # -get

        machine_mapping = {}
        for row in db_data:
            p_code = row.part_number[10:15]
            single_cost = product_cost_map.get(p_code, -1)

            key = row.machine_id
            if key not in machine_mapping:
                machine_mapping[key] = {
                    'machine_type': machine_inf.get(key, '')
                    if not machine_type else machine_type,
                    'count_qty': row.count_qty,
                    'event_qty': row.event_qty,
                    'total_loss_money': row.event_qty * single_cost
                }
            else:
                machine_mapping[key]['count_qty'] += row.count_qty
                machine_mapping[key]['event_qty'] += row.event_qty
                machine_mapping[key]['total_loss_money'] = (
                    row.event_qty * single_cost)

        return machine_mapping

    result = []
    if machine_type:
        db_data = MachineDailyLossRate.objects(
            machine_type=machine_type,
            record_date__gte=start_date, record_date__lte=end_date)
    else:
        db_data = MachineDailyLossRate.objects(
            record_date__gte=start_date, record_date__lte=end_date)
    merge_data = merge_daily(db_data)

    for key, value in merge_data.items():
        result += [{
            'machine_id': key,
            'machine_type': value['machine_type'],
            'count_qty': value['count_qty'],
            'event_qty': value['event_qty'],
            'loss_rate': "{0:.5f}".format(value['event_qty'] / (
                value['count_qty'] + value['event_qty'])),
            'total_loss_money': (
                value['total_loss_money'] if
                value['total_loss_money'] >= 0 else '未定義成本')
        }]

    if format == 'csv':
        output = data_exportor.get_loss_rate_report(result)
        return True, output

    return True, result

def get_daily_loss_rate_detail(usr_input, format=None):
    """Get detail (raw) data from loss rate db."""
    result = []
    machine_id = usr_input.get('machine_id', '')
    start_date = usr_input.get('start_date',
                               datetime.datetime.utcnow().strftime("%Y-%m-%d"))
    end_date = usr_input.get('end_date',
                             datetime.datetime.utcnow().strftime("%Y-%m-%d"))

    db_data = MachineDailyLossRate.objects(
        machine_id=machine_id,
        record_date__gte=start_date, record_date__lte=end_date)

    if db_data:
        # cost
        single_cost = 0
        p_code = db_data[0].part_number[10:15]
        output_product = Product.objects(p_code=p_code).first()
        if output_product:
            single_cost = output_product.cost
        # -cost

        # generate output data
        for r in db_data:
            loss_money = r.event_qty * single_cost
            result += [{
                'record_date': r.record_date,
                'count_qty': r.count_qty,
                'event_qty': r.event_qty,
                'loss_rate': "{0:.5f}".format(r.event_qty / (
                    r.count_qty + r.event_qty)),
                'total_loss_money': (
                    loss_money if loss_money >= 0 else '未定義成本')
            }]

        if format == 'csv':
            output = data_exportor.get_loss_rate_detail_report(result)
            return True, output

    return True, result


"""
needs:
* DB
* date
* channel

all_rows = self.db.find_episodes_by_date(self.date)

    data_handler:
        * date
        * episode
        * recording

    feature_analyzer
        * directory
        * episode_path

"""

import sys
import logging
import os

import AudioDataHandler
import MusicFeatureAnalyzer
import Database
import RadioDNS
from datetime import date, timedelta

logging.basicConfig(level=logging.INFO)

def main(argv=None):
    if argv is None:
        argv = sys.argv

    helpText ='''
    Please enter the following argumets
    [1] channel name
    [2] directory
    [3] date
    like: "./digsting_runner.py Br_Klassik /User/Data 2018-02-10 "
    '''
    # check number of arguments
    if len(argv) < 3 or len(argv) > 4:
        print(helpText)
        print(len(argv))
        sys.exit()

    channel_name = argv[1]
    directory = argv[2]

    print(argv)
    print(len(argv))

    if len(argv) == 4:
        yesterday = argv[3]
    else:
        yesterday = str(date.today() - timedelta(1))
        print(yesterday)
        print(type(yesterday))

    digest_daily_blob(directory, yesterday, channel_name)

def digest_daily_blob(directory, date, channel_name):
    db = Database.Database(directory)
    # find all recordigns from date
    all_recordings = db.find_captures_by_date_and_channel(date, channel_name)
    logging.info("{} recordings with date:{} from channel:{} \n {}".format(len(all_recordings),date, channel_name, all_recordings))
    for recording in all_recordings:
        # get recording entity
        recording_id = recording[0]
        recording = db.find_recording(recording_id)
        logging.debug("Recording found: {}".format(recording))
        # if(recording.channel == channel_name):
        # start radioDNS parsing
        radioDNS = RadioDNS.RadioDNS(directory, channel_name, recording, date)
        radioDNS.get_radioDNS_metadata()

    # find all episodes from date
    all_rows = db.find_episodes_by_date(date)
    logging.debug("trying to find episodes with date: {}".format(date))

    for episode in all_rows:
        #get episode entity
        episode_id = episode[0]
        episode = db.find_episode_by_id(episode_id)
        logging.debug("found episode with id: {}".format(episode_id))

        # find corresponding recording
        recording = db.find_recording(episode.recording_id)
        logging.debug("Recording found: {}".format(recording))

        try:
            if recording.is_episode is 1: #is episode to is_analized
                logging.debug("recording is already an episode")
                continue

            audio_data_handler = AudioDataHandler.AudioDataHandler(directory, date, channel_name)
            extracted_episode = audio_data_handler.extract_episode(episode, recording)

            if extracted_episode is not None:
                music_feature_analyzer = MusicFeatureAnalyzer.MusicFeatureAnalyzer(directory)
                music_feature_analyzer.analyze_episode(extracted_episode)
            else:
                logging.error("episoded could not be extracted")

        except RuntimeError as e:
          exit("ERROR: {}".format(e.message))

    logging.info("Digesting process finished \n \n \n")
    sys.exit(0)

if __name__ == "__main__":
    main()