# coding: utf-8

"""
CRUD refererente à classe de recurso TRAINING
"""

from crud.company import CompanyCRUD
from library.common.governance import LEVEL_SOFIER, LEVEL_COMPANY, ProhibitedOperation, DEVELOPER_SESSION
from library.common.paging_info import PagingInfo
from library.crud.crud_base import CRUDBase, ResourceNotFound
from library.storage.relation import RelationStorage
from scheme.training import SCHEME_TRAINING

class TrainingCRUD(CRUDBase):
    """
    CRUD refererente à classe de recurso TRAINING
    """

    resource_class = 'training'

    scheme = SCHEME_TRAINING

    CYPHER = """
    MERGE (t:TRAINING {{name: '{node}'}})
    WITH t
    MATCH (c:COMPANY {{name: '{company}'}})
    MERGE (c)-[:AVAILABLE]->(t)
    """

    def build_relationship(self, document: dict):
        """
        Registra o relacionamento

        :param document:
            Documento que engatilhou
        """
        RelationStorage().run(TrainingCRUD.CYPHER.format(node=document['name'], company=document['company']))

    def before_document_is_valid(self, document: dict) -> dict:
        """
        Pré validação em especial no tocante à pemissividade

        :param document:
            Documento que engatilhou
        :return:
            Documento eventualmente modificado
        """
        if not CompanyCRUD(PagingInfo(), DEVELOPER_SESSION).exists(document['company']):
            raise ResourceNotFound(document['company'], 'company')

        if self.session.level == LEVEL_SOFIER:
            raise ProhibitedOperation(self.session.user)

        if self.session.level == LEVEL_COMPANY:
            if self.session.company != document['company']:
                raise ProhibitedOperation(self.session.user)

        return document


import numpy as np
import matplotlib.pyplot as plt
import math
from numpy.linalg import inv

class Estimation:

    def __init__(self, init_values):
        self.real = []
        self.init_values    = init_values
        self.sensorDistance = 10
        self.errorSensores  = 0.003
        self.errorPuntoY    = 0.001

        self.deltaT = 1
        self.errors_x_values = (0.01, 0.4) # V_value and Q_value
        self.errors_polar = (0.25, 0.5) # R_value and T_value

        """
            INICIALIZACIÓN DE LAS MATRICES
        """
        self.MATRIX_F = np.array(
            [[1, 0, self.deltaT, 0],
            [0, 1, 0, self.deltaT],
            [0, 0, 1, 0],
            [0, 0, 0, 1]
        ])

        self.MATRIX_Q = np.array([
            [self.errors_x_values[1], 0, 0, 0],
            [0, self.errors_x_values[1], 0, 0],
            [0, 0, self.errors_x_values[0], 0],
            [0, 0, 0, self.errors_x_values[0]]
        ])

        self.MATRIX_H = np.array([
            [1, 0, 0, 0],
            [0, 1, 0, 0]
        ])

        self.MATRIX_R = np.array([
            [self.errors_x_values[1] * 1.2, 0],
            [0, self.errors_x_values[1] * 1.2]
        ])

        """
            INICIALIZACIÓN DE ARRAYs
            xp  = ARRAY DE VALORES PARA LA PREDICCIÓN
            x_p = MATRIX DE ERRORES
        """
        errores       = self.__Get_Errors()
        self.array_xp = self.init_values + errores
        self.array_p      = self.MATRIX_Q

    def procesarKalmanValores(self, R1, R2, init_values):
        """
            SE OBTIENE LOS VALORES DEL REAL
        """
        errorR1 = np.random.normal(0, scale = self.errorSensores )
        errorR2 = np.random.normal(0, scale = self.errorSensores )
        print(R1, R2)
        C = ( ( ( self.sensorDistance /2 ) ** 2) + ( R1 ** 2 ) ) ** (1/2)
        self.real.append(C)

        """
            INICIA PREDICCION Y FILTRADO
        """
        self.array_xp = self.__Get_Prediction(self.array_xp)
        values_array_p.append(self.array_xp)

        self.array_xp, self.array_p = self.__Update_Filter(self.array_xp, self.array_p, array_r)
        value_array_f.append(self.array_xp)

    def __Get_Errors(self, option='n'):
        vx, vy = self.errors_x_values # errores para la velocidad de X y Y

        distancia1_error  = np.random.normal(0, scale=self.errorSensores)
        distancia2_error  = np.random.normal(0, scale=self.errorPuntoY)
        velocidadX_error  = np.random.normal(0, scale=vx)
        velocidadY_error  = np.random.normal(0, scale=vy)

        return np.array([ distancia1_error, distancia2_error, velocidadX_error, velocidadY_error ])

    def __Update_Real_Value(self, aux_array_x):
        array_x = np.copy(aux_array_x)
        array_error = self.__Get_Errors()
        array_x     = np.dot(self.MATRIX_F , array_x) +  array_error
        return array_x

    def __Get_Prediction(self, aux_array_x):
        array_x = np.copy(aux_array_x)
        array_x = np.sum( self.MATRIX_F * array_x, axis=1)
        return array_x

    def __Update_Filter(self, array_r):
        """
            SE CALCULAN LOS N ESTADOS PARA Xk+1,K Y SE PASAN POR LA FUNCIÓN H
        """
        watcher_array_points = self.__Get_Points(self.array_xp, self.array_p)
        watcher_polar_values = self.__Funcion_H(watcher_array_points, False)

        """
            SE OBTIENE EL PROMEDIO Y LA SUMATORIA DE LOS ESTADOS Y LOS VALORES Z
        """
        sum_polar, sum_points, avr_polar, avr_points = self.__Get_Summations(watcher_polar_values, watcher_array_points)
        matrix_gain = np.dot(sum_points, np.linalg.inv(sum_polar))

        Real_polar_values = self.__Funcion_H(array_r, True)

        """
            ACTUALIZACIÓN DE VALORES
        """
        self.array_xp = self.array_xp  + np.dot (matrix_gain, (Real_polar_values - avr_polar))
        self.array_p  = self.array_p - np.dot(np.dot(matrix_gain, sum_polar), np.transpose(matrix_gain))
        self.array_p  = self.MATRIX_F * self.array_p * np.transpose(self.MATRIX_F) + self.MATRIX_Q

    def __Get_Points(self, value_x, array_p):
        puntosCartesianos = value_x.copy()

        for i in range(2):
            puntoAuxiliar = value_x.copy()
            puntoAuxiliar[i] += np.sqrt(np.abs(array_p[i,i]))
            puntosCartesianos=np.vstack([puntosCartesianos,puntoAuxiliar])

            puntoAuxiliar = value_x.copy()
            puntoAuxiliar[i] -= np.sqrt(np.abs(array_p[i,i]))
            puntosCartesianos=np.vstack([puntosCartesianos,puntoAuxiliar])

        return puntosCartesianos

    def __Funcion_H(self, array_points, single):
        array_values = []

        if(single == False):
            for values in (array_points):
                x, y, vx, vy = values
                array_values.append(
                    np.array ([
                        x**2 + (y - (self.sensorDistance/2) )**2,
                        x**2 + (y + (self.sensorDistance/2) )**2
                    ])
                )
        else:
            x, y, vx, vy = array_points

            return np.array (
                [
                    x**2 + (y - (self.sensorDistance/2) )**2,
                    x**2 + (y + (self.sensorDistance/2) )**2
                ]
            )

        return np.array(array_values)

    def __Get_Summations(self, watcher_polar_values, wacher_array_points):
        amount_polar, amount_points = len(watcher_polar_values), len(wacher_array_points)
        average_poinst, average_polar = self.__Get_Averages(watcher_polar_values, wacher_array_points)

        print('Watcher_polar_values')
        print(watcher_polar_values)

        print('Watcher_array_values')
        print(wacher_array_points)

        polarP= np.zeros((2,2))
        for x in range( amount_polar ):
            polarP+=(watcher_polar_values[x] - average_polar.reshape(1,2))*np.transpose(watcher_polar_values[x] - average_polar.reshape(1,2))

        cartesianosP = np.zeros((4,2))
        for x in range( amount_points ):
            cartesianosP += ( np.transpose((( wacher_array_points[x] - average_poinst).reshape((-1, 4)) ) * np.transpose((watcher_polar_values[x] - average_polar).reshape(1,2) )))

        return polarP, cartesianosP, average_polar, average_poinst

    def __Get_Averages(self, watcher_polar_values, wacher_array_points ):
        average_poinst = self.__Get_Average(wacher_array_points)
        average_polar  = self.__Get_Average(watcher_polar_values)
        return average_poinst, average_polar

    def __Get_Average(self, array_values ):
        average = np.mean( array_values, axis=0)
        return average

    def Get_Values(self, iterations):
        # Tuple
        values_array_r, values_array_p, value_array_f = [], [], []

        # Initialization of Array
        array_r   = np.array(self.init_values)
        values_array_r.append(array_r)

        for i in range(iterations):
            #-------------------Real------------------#
            array_r = self.__Update_Real_Value(array_r)
            values_array_r.append(array_r)

        for i in range(iterations+1):
            """
                INICIA PREDICCION Y FILTRADO
            """
            self.array_xp = self.__Get_Prediction(self.array_xp)
            values_array_p.append(self.array_xp)

            self.__Update_Filter(values_array_r[i])
            value_array_f.append(self.array_xp)

        return values_array_r, values_array_p, value_array_f


from xai.brain.wordbase.nouns._toothpick import _TOOTHPICK

#calss header
class _TOOTHPICKS(_TOOTHPICK, ):
	def __init__(self,):
		_TOOTHPICK.__init__(self)
		self.name = "TOOTHPICKS"
		self.specie = 'nouns'
		self.basic = "toothpick"
		self.jsondata = {}


from data_source import StoredOrderbook
from trading_strategy_executor import BacktestExecutpr
from trading_strategy import HodlStrategy, RandomActionStrategy
from common import Assets

from time import sleep
import logging
import sys

def evaluate_assets(initial_assets, current_assets, initial_market_state, market_state):
	if initial_market_state == None or market_state == None:
		return
	initial_usd = initial_assets.price_as_usd(initial_market_state)
	current_usd = current_assets.price_as_usd(market_state)
	profit = float(current_usd - initial_usd) / initial_usd * 100
	logging.info('Initial USD: {0}, Current USD: {1}, Profit: {2}'.format(initial_usd, current_usd, profit))

def trade(interval, assets, data_source, trading_strategy, strategy_executor, market_constants):
	initial_assets = Assets(asset_obj=assets)
	initial_market_state = None
	market_state = None
	while True:
		if data_source.data_exhausted():
			break

		latest_market_data = []
		if data_source.has_new_data():
			latest_market_data = data_source.get_latest_market_data()

		logging.debug('Ingesting {0} new market data entries'.format(len(latest_market_data)))
		if len(latest_market_data) > 0:
			logging.info('Latest market data entry: {0}'.format(str(latest_market_data[-1])))
			market_state = latest_market_data[-1]
			if initial_market_state == None:
				# If a price hasn't beven assinged yet.
				initial_market_state = latest_market_data[0]

		# largest_market_data must be sorted by time with oldest entries at the lowest
		# index.
		for market_data in latest_market_data:
			trading_strategy.ingest_new_market_data(market_data, assets)
			action = trading_strategy.get_current_action()
			strategy_executor.execute_action(action, assets, market_data, market_constants['buy_fee'], market_constants['sell_fee'])

		evaluate_assets(initial_assets, assets, initial_market_state, market_state)
		logging.info('Current assets: {0}'.format(assets))

		sleep(interval)

if __name__ == '__main__':

	logging.basicConfig(stream=sys.stdout,level=logging.INFO)

	market_constants = {
		'buy_fee': 0.0025,
		'sell_fee': 0.0025
	}
	initial_assets = Assets()
	initial_assets.btc = 1

	executor = BacktestExecutpr()
	orderbook_ds = StoredOrderbook('data/coinbase_orderbook-may6-new', 1)
	strategy = RandomActionStrategy(0.0001)
	trade(0.0001, initial_assets, orderbook_ds, strategy, executor, market_constants)


from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import logging
logging.basicConfig(format='%(asctime)s: %(name)s - %(levelname)s - %(message)s',
	level=logging.INFO,
	filename='bot.log'
	)

def greet_user(bot, update):

	text = 'Вызван /start'
	print(text)
	print(update)
	update.message.reply_text(text)

def talk_to_me(bot, update):
	user_text = update.message.text
	print(user_text)
	#update.message.reply_text(user_text[::-1].swapcase())
	update.message.reply_text(user_text[::-1])

def main():
	updater = Updater("")

	dp = updater.dispatcher
	dp.add_handler(CommandHandler("start", greet_user))
	dp.add_handler(MessageHandler(Filters.text, talk_to_me))

	updater.start_polling()
	updater.idle()

main()


#===================================================================================================
# Link Window UI
# Corsi Christophe
#===================================================================================================
from PyQt5.QtWidgets import QMainWindow
from EditWindow_Ui import *

class EditWindow(QMainWindow):
    '''
    Container class for the name editing window
    '''
    def __init__(self, modelBlock, scene):
        '''
        Default constructor for the edit window container
        @param modelBlock: BlockItem
        @param scene: QGraphicScene
        '''
        super(self.__class__, self).__init__()
        self.modelBlock = modelBlock
        self.scene = scene
        self.ui = Ui_EditName()
        self.ui.setupUi(self)
        self.linkButtons()
        self.ui.lineEdit.setText(self.modelBlock.block.name)

    def linkButtons(self):
        '''
        method that links the buttons
        '''
        self.ui.buttonCancel.clicked.connect(self.exit)
        self.ui.buttonOK.clicked.connect(self.ok)

    def ok(self):
        '''
        method that takes the parameter entered in the lineEdit and inject it into the block of the graphic view,
        and refreshes it
        '''
        self.modelBlock.block.name = self.ui.lineEdit.text()
        self.modelBlock.rebuild(self.scene)
        self.close()

    def exit(self):
        '''
        Close the edit window and cancel the changes
        '''
        self.close()


from tkinter import*
import tkinter.messagebox
import os

back=Tk()
back.geometry('900x800')

def level_1():
    back.destroy()
    os.system('python simple1.py')

def instruction():
    message1=tkinter.messagebox.showinfo('Instructions', '1)   Here, we will provide more than 3 chances to guess an english word \n2)   you will have to guess that word carfully \n\n3)   If your guessed abphabet will not be found in the word, your \t     chances will be decreased. \n\n4)   As the level goes up, difficulty level will be increased.\n\n5)   The will be 4 rounds in each level, consisting the same number of \t       alphabets. \n\n6)   One you will guess all alphabets correctly, will be winner. \n\n7)   otherwise You will be lost, and should be ready to be hanged!')

frame01=Frame(back)

pic01=PhotoImage(file='pic013.png')
label01=Label(back, image=pic01)
label01.pack(side='bottom')

label02=Label(frame01, text='Congratulations!', font=('arial','35'))
label02.pack(pady=(50,15))

button01=Button(frame01, bg='#142d39', fg='#FFFFFF',  font=('aria','13') , text='You know how to play', width='35',height='2', command=instruction).pack(pady=(0,15))
button02=Button(frame01, bg='#142d39', fg='#FFFFFF', font=('aria','13') , text='Play and Enjoy Again.!', width='35',height='2', command=level_1)
button02.pack(pady=(0,15))

frame01.pack()

back.mainloop()


from .features import FeatureExtractor
from .model import TaggerModel, DatesSettingsModel
from .dateutility import  EnddateParser, EntityParser, RulesParser, DateAttributes

BASE_STEP = ["B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8"]
RULE_STEP = ["R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12"]

class DateParserFromText(object):

    def __init__(self, ):
        self.TEXT = ""

    def getFeatures(self):
        fe = FeatureExtractor(self.TEXT)
        self.FEATURES = fe.extract()
        self.WORDS = fe.WORDS

    def getPatterns(self):
        tm = TaggerModel(self.FEATURES)
        self.PATTERNS = tm.predict()

    def getWordsandPatterns(self):
        self.NEW_WORDS    = []
        self.NEW_PATTERNS = []
        for i, p in enumerate(self.PATTERNS):
            if p == "UNK":
                continue

            if p in ["WORD_MIDDLE", "WM"] and len(self.NEW_PATTERNS) == 0:
                continue

            self.NEW_PATTERNS.append(p)
            self.NEW_WORDS.append(self.WORDS[i])
        print("New Patterns", self.NEW_PATTERNS, self.NEW_WORDS)

    def splitpatterns(self):
        self.SEGMENTS = []
        self.MIDDLERS = []
        self.IS_RANGE = False
        CONSTANTS = ["C_YEAR", "C_QUARTER", "C_MONTH", "C_WEEK", "C_DAY", "MONTHNAME", "DAYNAME", 'QUARTERNO']
        try:
            idx = self.NEW_PATTERNS.index("WORD_MIDDLE")
            print("WORD_MIDDLE")
            if self.NEW_PATTERNS[idx - 1] in CONSTANTS:
                print("SEGEMENT TRUE")
                self.IS_RANGE = True
                temp = []
                words = []
                for i, p in enumerate(self.NEW_PATTERNS):
                    if i < idx:
                        temp.append(p)
                        words.append(self.NEW_WORDS[i])

                self.SEGMENTS.append({ "patterns" : temp, "words": words })

                temp = []
                words = []
                for i, p in enumerate(self.NEW_PATTERNS):
                    if i > idx:
                        temp.append(p)
                        words.append(self.NEW_WORDS[i])

                self.SEGMENTS.append({"patterns": temp, "words": words})

            else:
                self.SEGMENTS.append({"patterns": self.NEW_PATTERNS, "words": self.NEW_WORDS})

        except:
            print( "Error Occured in Segmentation")
            self.SEGMENTS.append({"patterns": self.NEW_PATTERNS, "words": self.NEW_WORDS })

    def process(self):
        self.OUTPUT = {}
        print("SEGMENTS", self.SEGMENTS)
        for i, segment in enumerate(self.SEGMENTS):
            self.OUTPUT[i] = DateParser(segment["words"], segment["patterns"]).process()

    def parse(self, stext=""):

        if stext == "":
            raise "Empty text"

        self.TEXT = stext
        self.getFeatures()
        self.getPatterns()
        self.getWordsandPatterns()
        self.splitpatterns()
        self.process()
        START_DATE = END_DATE = ""
        START_DATE_TEXT = END_DATE_TEXT = ""
        for k, v in self.OUTPUT.items():
            try:
                if START_DATE == "":
                    START_DATE = v["START_DATE"]["DATE"]
                    START_DATE_TEXT = v["START_DATE"]["TEXT"]
                END_DATE = v["END_DATE"]["DATE"]
                END_DATE_TEXT = v["END_DATE"]["TEXT"]
            except:
                continue

        if START_DATE > END_DATE:
            START_DATE_TEXT, END_DATE_TEXT = END_DATE_TEXT, START_DATE_TEXT

        print(START_DATE, END_DATE)
        return { "START_DATE": START_DATE_TEXT, "END_DATE": END_DATE_TEXT }

class DateParser(object):

    def __init__(self, words=[], patterns=[]):
        self.WORDS = words
        self.PATTERNS = patterns

    def getDateSettings(self):
        ds = DatesSettingsModel(self.PATTERNS)
        self.DATESETTINGS = list(ds.predict()[0])
        print("DATE SETTINGS", self.DATESETTINGS )

        for settings in self.DATESETTINGS:
            if settings in BASE_STEP:
                self.BASE_STEP = settings
                continue
            if settings in RULE_STEP:
                self.RULE_STEP = settings
                continue

            self.END_RULE = settings

    def getEnityParser(self):
        ep = EntityParser(self.WORDS, self.PATTERNS)
        self.ENTITY = ep.parser()

    def getStartDate(self):
        rule = self.RULE_STEP if "RULE_STEP" in self.__dict__ else "R4"
        base = self.BASE_STEP if "BASE_STEP" in self.__dict__ else "B5"
        rs = RulesParser(step=base, rule=rule, **self.ENTITY)
        self.START_DATE = rs.parser()
        print("START DATE", self.START_DATE)

    def getEndDate(self):
        num = 0
        if "YEARS" in self.ENTITY:
            num = int(self.ENTITY["YEARS"])

        if "MONTHS" in self.ENTITY:
            num = int(self.ENTITY["MONTHS"])

        if "WEEKS" in self.ENTITY:
            num = int(self.ENTITY["WEEKS"])

        if "DAYS" in self.ENTITY:
            num = int(self.ENTITY["DAYS"])

        if "QUARTERS" in self.ENTITY:
            num = int(self.ENTITY["QUARTERS"])

        print("NUM", abs(num))
        erule = self.END_RULE if "END_RULE" in self.__dict__ else "0D"
        ep = EnddateParser(self.START_DATE, erule=erule, num=abs(num))
        self.END_DATE = ep.parser()
        print("ENDDATE", self.END_DATE)

    def prepareoutput(self):
        sdate_da = DateAttributes(self.START_DATE).getDA()
        edate_da = DateAttributes(self.END_DATE).getDA()

        data = {
            "START_DATE": {
                "TEXT": str(sdate_da["CURR.DAY"]) + "," + sdate_da["CURR.MONTHNAME"] + " " + str(sdate_da["CURR.YEAR"]),
                "DAY": sdate_da["CURR.DAYNAME"],
                "DATE": self.START_DATE
            },
            "END_DATE": {
                "TEXT": str(edate_da["CURR.DAY"]) + "," + edate_da["CURR.MONTHNAME"] + " " + str(edate_da["CURR.YEAR"]),
                "DAY": edate_da["CURR.DAYNAME"],
                "DATE": self.END_DATE
            }
        }
        print("PREPARE", data)
        return data

    def process(self):
        self.getDateSettings()
        self.getEnityParser()
        self.getStartDate()
        self.getEndDate()
        return self.prepareoutput()


#!/usr/bin/env python3

import requests

def schema():
    # Dictionary object with data type for
    # https://www.gov.uk/guidance/about-the-price-paid-data#explanations-of-column-headers-in-the-ppd
    dict_data_types = {
        'transaction_id': 'string',
        'price': 'long',
        'date_of_transfer': 'timestamp',
        'postcode': 'string',
        'property_type': 'string',
        'old_or_new': 'string',
        'duration': 'string',
        'paon': 'string',
        'saon': 'string',
        'street': 'string',
        'locality': 'string',
        'town_city': 'string',
        'district': 'string',
        'county': 'string',
        'ppd_category': 'string',
        'record_status': 'string'
    }

    return dict_data_types

def main():

    print('Grabbing price paid data')

    price_paid_latest = r'http://prod.publicdata.landregistry.gov.uk.s3-website-eu-west-1.amazonaws.com/' \
                     r'pp-monthly-update-new-version.csv'

    price_paid_all = r'http://prod.publicdata.landregistry.gov.uk.s3-website-eu-west-1.amazonaws.com/' \
                     r'pp-complete.csv'

    response = requests.get(price_paid_latest)
    text = response.text

    with open('latest.csv', 'w+') as f:
        f.writelines(text)

if __name__ == '__main__':
    main()


import sys                          #argv
import numpy as np
import matplotlib.pyplot as plt

N      = sys.argv[1]
pf     = sys.argv[2]
k      = sys.argv[3]                #trajectory index
mcmove = sys.argv[4]

root = "/home/paolo/baldan/Documents/codici_di_prova/codice_tesi_riscritto/output/SWAP/recap/"
filename = "traj"+k+"pf"+pf+"N"+N+".dat"
filein = root+mcmove+filename
t,msd,msd_err,ovlp,ovlp_err,pf,pf_err,pres,pres_err = np.loadtxt(filein, unpack=True)

fig = plt.figure(1)
fig.suptitle(filename)
plt.tight_layout()

msd_ax = plt.subplot(221)
msd_ax.errorbar(t,msd,yerr=msd_err,label="msd",fmt='o')
msd_ax.set_ylabel(r"$\Delta$")
msd_ax.set_xlabel("t")
msd_ax.set_xscale("log")
msd_ax.set_yscale("log")
msd_ax.legend()

ovlp_ax = plt.subplot(222)
ovlp_ax.errorbar(t,ovlp,yerr=ovlp_err,label="overlap",fmt='o')
ovlp_ax.set_ylabel("O")
ovlp_ax.set_xlabel("t")
ovlp_ax.set_xscale("log")
ovlp_ax.legend()

pf_ax = plt.subplot(223)
pf_ax.errorbar(t,pf,yerr=pf_err, label="pf",fmt='o')
pf_ax.set_xlabel("t")
pf_ax.set_ylabel(r"$\varphi$")
pf_ax.set_xscale("log")
pf_ax.legend()

pres_ax = plt.subplot(224)
#the commented syntax prints out a weird number on top of the subplot
pres_ax.errorbar(x=t,y=pres, yerr=0, label="pressure",fmt='o',capsize=8)
pres_ax.set_xlabel("t")
pres_ax.set_ylabel("Z")
pres_ax.set_xscale("log")
pres_ax.legend()

plt.show()


import unittest
from MODIS_Aggregation import *
import glob
import time
import xarray as xr
import netCDF4

class GetOutput(unittest.TestCase):

    def test_check_value(self):
        M03_dir = "resources/data/sample_input_data/MYD03/"
        M06_dir = "resources/data/sample_input_data/MYD06_L2/"
        print(M06_dir)
        print(M03_dir)
        M03_files = sorted(glob.glob(M03_dir + "MYD03.A2008*"))
        M06_files = sorted(glob.glob(M06_dir + "MYD06_L2.A2008*"))
        print(M03_files)
        print(M06_files)
        t0 = time.time()
        # calculate cloud fraction
        cf = calculateCloudFraction(M03_files, M06_files)
                # calculate execution time
        t1 = time.time()
        total = t1 - t0
        print("total execution time (Seconds):" + str(total))
                # display the output
        result = cf[127, 196]
        print("result:", result)
        expected = 0.12883436
        self.assertAlmostEqual(expected, result, places=8, msg=None)

        displayOutput(xr.DataArray(cf))
        result2 = xr.open_dataset("monthlyCloudFraction-file-level-for-loop.nc")[
                           '__xarray_dataarray_variable__'][127, 196].values
        print("result2:", result2)
        self.assertAlmostEqual(expected, result2, places=8, msg=None)

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


from command import Command
from utils import *

import rospy
from std_msgs.msg import String
from geometry_msgs.msg import Twist
from sensor_msgs.msg import Joy, JointState

class SkidSteer(Command):
    """ Control a model with a SkidSteer plugin """

    def __init__(self, name, skid_steer_topic, speed=5, rot_multiplier=1, clamp_below=0.1):
        """ Args:
        speed: max speed
        rot_multiplier: rotation multiplier
        """
        super(SkidSteer, self).__init__(name)
        self.pub = rospy.Publisher(skid_steer_topic, Twist, queue_size=10)
        self.speed = speed
        self.rot_multiplier = rot_multiplier
        self.clamp_below = clamp_below

    def evaluate_speed_rot(self, x, y):
        rot = x*self.rot_multiplier
        speed = y*self.speed
        return (speed, rot)

    def makemsg(self, speed, rot):
        twist = Twist()
        twist.linear.x = speed
        twist.angular.z = rot
        return twist

    def oncallback(self, cmd_data):
        x, y = cmd_data['x'], cmd_data['y']
        x, y = clamp_center(x, self.clamp_below), clamp_center(y, self.clamp_below)
        speed, rot = self.evaluate_speed_rot(x, y)
        msg = self.makemsg(speed, rot)
        self.pub.publish(msg)


import sys, os, re
def usage_message():
        if len(sys.argv)!=2:
                sys.exit("Usage: %s takes  one txt file in, and computes the batting average of each player listed."%sys.argv[0])
        filename = sys.argv[1]
        if not os.path.exists(filename):
                sys.exit("Error: File '%s' not found"%sys.argv[1])
def getName(line):
        name_regex = re.compile(r"^\w+\s\w+")
        name = name_regex.match(line)
        if(name is None):
                return None
        else:
                return name.group()
def getBats(line):
        bats_regex = re.compile(r"\d+\stime")
        times = bats_regex.search(line)
        if (times is None):
                return None
        times = times.group()
        bats = getNum(times)

        return bats
def getHits(line):
        hit_regex = re.compile(r"\d+\shit")
        hit = hit_regex.search(line)
        if( hit is None):
                return None
        hit = hit.group()
        num_hits = getNum(hit)
        return num_hits

def getNum(string):
        #Takes in a string that starts with a number, returns that number
        num_regex = re.compile(r"^\d+")
        num = num_regex.match(string)
        num = num.group()
        num = float(num)
        return num

class Player:
        def __init__(self,name, bats, hit):
                self.name = name
                self.at_bat = bats
                self.hits = hit
        def batting_ave(self):
                if(self.at_bat == 0):
                        return 0
                else:
                        return (self.hits / self.at_bat)


#!/usr/bin/env python
#coding: utf-8

#python2のコードをPython3用に使用したい場合に__future__を書く
from __future__ import print_function
#ネットワーク・プログラミングをするためのAPI
import socket
#with 構文にかかわる一般的なタスクのためのユーティリティ(補助機能)
from contextlib import closing

def main():
	host = '160.12.172.59'
	port = 8080
	bufsize = 90*1024
	#テキストファイル(b:バイナリデータ)
	f = open('test.jpg',mode='rb')
	data = f.read()
	#テキストデータ
	text = u"Hellow World!!"

	#ソケット関係のシステムコールはsocket()関数で生成する。（socketオブジェクトのメソッドであるためsocket.socket()と書く）
	#インスタンスみたいなもの？
	#AF_INET:IPv4による通信を行う。（アドレスファミリを指定する変数）
	#SOCK_STREAM:ソケットタイプを指定するらしい。だいたいは、SOCK_STREAMかSOCK_DGRAM
	sock1 = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
	#with 構文は、ある機能を利用する場合により安全かつ簡潔に機能を使用する、らしい
	#例)wfp = open('msg.log','w') → with open ('msg.log', 'w') as wfp
	#上記のように書くと、close()をかってによびだしてくれるらしい
	with closing(sock1):
		#リストとして、アドレスとポート番号を引数として渡す。
		#アドレスで示されているリモートソケット(ホスト側)に接続する
		sock1.connect((host,port))
		#bはバイトリテラルらしい、リテラル（変更されないことを前提とした値）
		#ソケットにデータを送信する。戻り値として、送信したバイト数を返す。
#		sock1.send(b'Hello World')
		sock1.send(data)
		sock1.send(text)
		f.close()
		print(sock1.recv(bufsize))
		print(sock1.recv(bufsize))
	return

#ここで、main()関数を再起呼び出し
if __name__ == '__main__':
	main()


import os
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns

from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler

import umap
from scipy.sparse.csgraph import connected_components

from preprocessing import preprocessing

def exploratory_data_analysis( args, df_train, df_test ):
    # deep copy
    df_train = df_train.copy()
    df_test = df_test.copy()

    # 目的変数
    target_name = "SalePrice"

    # 前処理済みデータ
    df_train_preprocess, df_test_preprocess = preprocessing( args, df_train, df_test )

    #--------------------------
    # データ構造確認
    #--------------------------
    print( "df_train.head() : \n", df_train.head() )
    print( "df_test.head() : \n", df_test.head() )
    print( "df_train.shape : \n", df_train.shape )
    print( "df_test.shape : \n", df_test.shape )

    for col in df_train.columns:
        print( "df_train[{}].dtypes ] : {}".format(col, df_train[col].dtypes))

    #--------------------------
    # 基本統計量
    #--------------------------
    print( df_train.describe() )
    print( df_test.describe() )

    #--------------------------
    # 目的変数の分布
    #--------------------------
    fig, axis = plt.subplots()
    sns.distplot( df_train[target_name] )
    plt.grid()
    plt.savefig( os.path.join(args.results_dir, args.exper_name, "target_dist.png"), dpi = 200, bbox_inches = 'tight' )

    #--------------------------
    # 説明変数の分布
    #--------------------------
    fig, axis = plt.subplots()
    df_train.hist( bins=50, figsize=(80,60) )
    plt.grid()
    plt.savefig( os.path.join(args.results_dir, args.exper_name, "train_hist_all_features.png"), dpi = 200, bbox_inches = 'tight' )

    #--------------------------
    # 目的変数と説明変数の関係
    #--------------------------
    # 数値データ
    feats = list( df_train.dtypes[ df_train.dtypes != "object" ].index )
    n_rows, n_cols = 20, 5
    fig, axis = plt.subplots(n_rows, n_cols, figsize=(n_cols*4,n_rows*3) )
    for r in range(0, n_rows):
        for c in range(0, n_cols):
            i = r * n_cols + c
            if i < len(feats):
                df_data = pd.concat([df_train[target_name], df_train[feats[i]]], axis=1)
                df_data.plot.scatter( x=feats[i], y=target_name, ax = axis[r][c] )
                plt.grid()

    plt.tight_layout()
    plt.savefig( os.path.join(args.results_dir, args.exper_name, "target_vs_numuric_features.png"), dpi = 100, bbox_inches = 'tight' )

    # カテゴリーデータ
    feats = list( df_train.dtypes[df_train.dtypes == "object"].index )
    n_rows, n_cols = 30, 2
    fig, axis = plt.subplots(n_rows, n_cols, figsize=(n_cols*6,n_rows*3))
    for r in range(0, n_rows):
        for c in range(0,n_cols):
            i = r * n_cols+c
            if i < len(feats):
                df_data = pd.concat([df_train[target_name], df_train[feats[i]]], axis=1)
                sns.boxplot( x=feats[i], y=target_name, data=df_data, ax = axis[r][c] )

    plt.tight_layout()
    plt.savefig( os.path.join(args.results_dir, args.exper_name, "target_vs_categorical_features.png"), dpi = 100, bbox_inches = 'tight' )

    #--------------------------
    # 全特徴量同士の相関をヒートマップで表示
    #--------------------------
    fig, axis = plt.subplots()
    sns.heatmap(df_train.corr(), square=True, vmax=1, vmin=-1, center=0.0 )
    plt.savefig( os.path.join(args.results_dir, args.exper_name, "train_heatmap_all_features.png"), dpi = 300, bbox_inches = 'tight' )

    #--------------------------
    # 目的変数に対する相関をヒートマップで表示
    #--------------------------
    fig, axis = plt.subplots()
    n_heat = 30     # number of variables for heatmap
    cols = df_train.corr().nlargest(n_heat, target_name)[target_name].index
    cm = np.corrcoef(df_train[cols].values.T)
    sns.set( font_scale=0.25 )
    sns.heatmap(cm, cbar=True, annot=True, square=True, vmax=1, vmin=-1, center=0.0, fmt='.2f', annot_kws={'size': 2}, yticklabels=cols.values, xticklabels=cols.values)
    plt.savefig( os.path.join(args.results_dir, args.exper_name, "heatmap_target_vs_features.png"), dpi = 300, bbox_inches = 'tight' )

    #--------------------------
    # PCA
    #--------------------------
    pass

    #--------------------------
    # UMAP（回帰問題では無効）
    #--------------------------
    """
    # データは標準化されている必要あり
    um = umap.UMAP()
    df_train_umap = um.fit_transform( df_train_preprocess )

    fig, axis = plt.subplots()
    plt.scatter(df_train_umap[:, 0], df_train_umap[:, 1], s=10, alpha=0.5)
    axis.legend( bbox_to_anchor=(1.00, 1), loc='upper left' )
    plt.grid()
    plt.savefig( os.path.join(args.results_dir, args.exper_name, "umap.png"), dpi = 300, bbox_inches = 'tight' )
    """

    return


"""车辆自造信息用例"""
import os
import time
import unittest
import xlrd
from Page.uniform_entry_page import UniformEntryPage
from Util.DriverUtil import DriverUtils, get_screenshot_page
from config import BASE_DIR

class TestVehicleManufacturing(unittest.TestCase):
    __driver = None
    @classmethod
    def setUpClass(cls) -> None:
        cls.driver = DriverUtils.get_driver_file()
        cls.uniformEntryPage = UniformEntryPage(cls.driver)

    @classmethod
    def tearDownClass(cls) -> None:
        time.sleep(3)
        DriverUtils.quit_driver()

    def setUp(self) -> None:
        self.driver.get("http://10.100.81.181/#/login")
        # self.driver.get("http://10.100.81.154/#/login")
        # self.driver.get("http://10.100.81.154/#/homepage")
        self.uniformEntryPage.get_login_proxy().login("18701397590", "66666666")  # 登录
        self.uniformEntryPage.get_home_proxy().Energy_prepare()  # 能源准备

    def test_vehicle_manufacturing_download_file(self):
        """下载模板"""
        path = BASE_DIR + "\File\车辆制造信息模板.xlsx"
        # 判断路径中是否有重复内容
        if os.path.exists(path):
            # 删除
            os.remove(path)
            self.uniformEntryPage.get_VehicleManufacturingPage().vehicle_manufacturing_download_file()  # 下载模板
            # 获取文件名
            filename = os.path.basename(path)
            print("文件名称: %s", filename)
            # 获取文件类型（扩展名）
            filetype = os.path.splitext(path)
            print("文件扩展名：%s", filetype)
            try:
                self.assertIn("车辆制造信息模板", filename)
                self.assertIn(".xlsx", filetype)
                self.uniformEntryPage.get_home_proxy().quit_login()  # 退出
            except AssertionError:
                get_screenshot_page(self.driver, "vehicle_manufacturing_download_file_bug")
                self.uniformEntryPage.get_home_proxy().quit_login()  # 退出

        else:
            self.uniformEntryPage.get_VehicleManufacturingPage().vehicle_manufacturing_download_file()  # 下载模板
            # 获取文件名
            filename = os.path.basename(path)
            print("文件名称: %s", filename)
            # 获取文件类型（扩展名）
            filetype = os.path.splitext(path)
            print("文件扩展名：%s", filetype)
            try:
                self.assertIn("车辆制造信息模板", filename)
                self.assertIn(".xlsx", filetype)
                self.uniformEntryPage.get_home_proxy().quit_login()  # 退出
            except AssertionError:
                get_screenshot_page(self.driver, "vehicle_manufacturing_download_file_bug")
                self.uniformEntryPage.get_home_proxy().quit_login()  # 退出

    def test_vehicle_manufacturing_export_file(self):
        """导出"""
        path = BASE_DIR + "\File\车辆制造信息.xlsx"
        if os.path.exists(path):
            os.remove(path)
            self.uniformEntryPage.get_VehicleManufacturingPage().vehicle_manufacturing_export_file()
            filename = os.path.basename(path)
            print("文件名称：{}".format(filename))
            fileType = os.path.splitext(path)
            print("文件扩展名：{}".format(fileType))
            try:
                self.assertIn("车辆制造信息", filename)
                self.assertIn(".xlsx", fileType)
                self.uniformEntryPage.get_home_proxy().quit_login()
            except AssertionError:
                get_screenshot_page(self.driver, "vehicle_manufacturing_export_file_bug")
                self.uniformEntryPage.get_home_proxy().quit_login()  # 退出

        else:
            self.uniformEntryPage.get_VehicleManufacturingPage().vehicle_manufacturing_export_file()  # 导出
            # 获取文件名
            filename = os.path.basename(path)
            print("文件名称: %s", filename)
            # 获取文件类型（扩展名）
            fileType = os.path.splitext(path)
            print("文件扩展名：%s", fileType)
            try:
                self.assertIn("车辆制造信息", filename)
                self.assertIn(".xlsx", fileType)
                self.uniformEntryPage.get_home_proxy().quit_login()  # 退出
            except AssertionError:
                get_screenshot_page(self.driver, "vehicle_manufacturing_export_file_bug")
                self.uniformEntryPage.get_home_proxy().quit_login()  # 退出

    def test_vehicle_manufacturing_import_file(self):
        """导入"""
        excelfilepath = BASE_DIR + "\Data\车辆制造信息模板.xlsx"
        data = xlrd.open_workbook(excelfilepath, encoding_override="utf-8")  # 转化文件格式必须为xlsx格式
        table = data.sheet_by_name("导入模版-车辆制造信息")  # 表名
        keys = table.row_values(1)  # 第2行作为键
        rowNum = table.nrows  # sheet行数
        colNum = table.ncols  # sheet列数
        data_list = []
        for i in range(2, rowNum):
            sheet_data = {}
            for j in range(colNum):
                sheet_data[keys[j]] = table.row_values(i)[j]
                data_list.append((sheet_data))
        if data_list:
            # 存在值
            self.uniformEntryPage.get_VehicleManufacturingPage().vehicle_manufacturing_import_file(BASE_DIR + "\Data\车辆制造信息模板.xlsx")  # 导入
            try:
                text = self.uniformEntryPage.get_InboundOutboundInfoPage().get_inbound_outbound_import_error_text()
                self.assertIn("导入成功", text)
                self.uniformEntryPage.get_home_proxy().quit_login()
            except AssertionError:
                # 截图
                get_screenshot_page(self.driver, "vehicle_manufacturing_import_file_bug")
                self.uniformEntryPage.get_home_proxy().quit_login()  # 登出
        else:
            # data_list为空
            self.uniformEntryPage.get_VehicleManufacturingPage().vehicle_manufacturing_import_file(BASE_DIR + "\Data\车辆制造信息模板.xlsx")  # 导入
            try:
                text = self.uniformEntryPage.get_VehicleManufacturingPage().get_vehicle_manufacturing_error_text()
                self.assertIn("工作簿【导入模版-车辆制造信息】解析错误： 工作簿【导入模版-车辆制造信息】为空", text)
                self.uniformEntryPage.get_home_proxy().quit_login()
            except AssertionError:
                # 截图
                get_screenshot_page(self.driver, "vehicle_manufacturing_import_file_bug")
                self.uniformEntryPage.get_home_proxy().quit_login()  # 登出


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

from __future__ import (division, print_function, absolute_import,
                        unicode_literals)

from itertools import product
from hw3_runner import main
from multiprocessing import Pool

pool = Pool()

def baseline():
    cmd = "--stupid --greedy --context --thresh {0}"
    thresholds = [1, 5, 10, 50, 100, 1000, 10000]
    cmds = [cmd.format(t).split() for t in thresholds]
    results = pool.map(main, cmds)
    with open("baseline.txt", "w") as f:
        for t, r in zip(thresholds, results):
            args = (map("{0}".format, [t, r[0]])
                    + ["{0:.3f}".format(v*100) for v in r[1:]])
            f.write(" & ".join(args) + "\\\\\n")

def trigram():
    cmd = "--stupid --greedy --thresh {0} --lambda2 {1} --lambda3 {2}"
    thresholds = [2, 5, 10]
    lambda2s = [0.2, 0.3, 0.4]
    lambda3s = [0.5, 0.6, 0.7]
    args = [(t, l2, l3)
            for t, l2, l3 in product(thresholds, lambda2s, lambda3s)
            if l2+l3 < 1.0]
    cmds = [cmd.format(*a).split() for a in args]
    results = pool.map(main, cmds)
    with open("trigram.txt", "w") as f:
        for a, r in zip(args, results):
            cols = (["{0}".format(a[0])] + map("{0:.1f}".format, a[1:])
                    + ["{0:.3f}".format(v*100) for v in r[1:]])
            f.write(" & ".join(cols) + "\\\\\n")

def viterbi():
    cmd = "--stupid --thresh {0} --lambda2 {1} --lambda3 {2}"
    thresholds = [2, 5, 10]
    lambda2s = [0.2, 0.3, 0.4]
    lambda3s = [0.5, 0.6, 0.7]
    args = [(t, l2, l3)
            for t, l2, l3 in product(thresholds, lambda2s, lambda3s)
            if l2+l3 < 1.0]
    cmds = [cmd.format(*a).split() for a in args]
    results = pool.map(main, cmds)
    with open("viterbi.txt", "w") as f:
        for a, r in zip(args, results):
            cols = (["{0}".format(a[0])] + map("{0:.1f}".format, a[1:])
                    + ["{0:.3f}".format(v*100) for v in r[1:]])
            f.write(" & ".join(cols) + "\\\\\n")

def unknown():
    cmd = ("--thresh {0} --lambda2 0.2 --lambda3 0.7 --theta1 {1} "
           "--theta2 {2} --theta3 {3}")
    thresholds = [2, 5, 10]
    theta1s = [0.01, 0.05, 0.1]
    theta2s = [0.3, 0.5, 0.7]
    theta3s = [0.3, 0.5, 0.7]
    args = [(t, t1, t2, t3)
            for t, t1, t2, t3 in product(thresholds, theta1s, theta2s, theta3s)
            if t1+t2+t3 < 1.0]
    cmds = [cmd.format(*a).split() for a in args]
    results = pool.map(main, cmds)
    with open("unknown.txt", "w") as f:
        for a, r in zip(args, results):
            cols = (["{0}".format(a[0])] + map("{0:.1f}".format, a[1:])
                    + ["{0:.3f}".format(v*100) for v in r[1:]])
            f.write(" & ".join(cols) + "\\\\\n")

if __name__ == "__main__":
    # baseline()
    # trigram()
    # viterbi()
    unknown()


from django.contrib import messages
from django.http import HttpResponseRedirect
from django.urls import reverse_lazy

from . import models

def existe_questao(view):
    def wrap(request, *args, **kwargs):
        if request.user.professor.questoes.exists():
            return view(request, *args, **kwargs)
        else:
            aviso = 'É necessário que você tenha cadastrado ao mínimo ' \
                'uma questão para poder criar uma prova!'
            messages.info(request, aviso)
            return HttpResponseRedirect(
                reverse_lazy(
                    'enadum_prof:disciplina',
                    args=kwargs['pk'],
                )
            )
    return wrap


import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import random

def _init_weight(m):
    if isinstance(m, nn.Conv2d):
        torch.nn.init.xavier_uniform_(m.weight.data)
        torch.nn.init.constant_(m.bias.data, 0)
    elif isinstance(m, nn.Linear):
        torch.nn.init.xavier_uniform_(m.weight.data)
        torch.nn.init.constant_(m.bias.data, 0)

class _NoisyLinear(nn.Linear):
    def __init__(self, in_features, out_features, bias=True, mode="Factorised"):
        super(_NoisyLinear, self).__init__(in_features, out_features, bias)
        assert mode == "Factorised" or mode == "Independent"
        self.mode = mode

    def forward(self, input):
        weight, bias = self.noisy_parameters()
        return F.linear(input, weight, bias)

    def noisy_parameters(self):
        if self.mode == "Factorised":
            q = torch.randn((self.out_features, 1), dtype=torch.float32).cuda()
            p = torch.randn((1, self.in_features), dtype=torch.float32).cuda()
            q = torch.sign(q) * torch.sqrt(torch.abs(q))
            p = torch.sign(p) * torch.sqrt(torch.abs(p))
            weight = self.weight * q * p
            if self.bias is not None:
                bias = self.bias * q.squeeze()
            else:
                bias = None
        else:
            raise NotImplementedError
        return weight, bias

class NoisyLinear(nn.Module):
    def __init__(self, in_features, out_features, bias=True, mode="Factorised"):
        super(NoisyLinear, self).__init__()
        self.noisy_linear = _NoisyLinear(in_features, out_features, bias, mode)
        self.linear = nn.Linear(in_features, out_features, bias)

    def forward(self, input):
        return self.linear.forward(input) + self.noisy_linear.forward(input)
class BasicNet(nn.Module):
    def __init__(self, shape, na):
        """
        shape: observation shape
        na:    action number
        """
        super(BasicNet, self).__init__()
        self.shape = shape
        self.na = na
        self.convs = nn.Sequential(
            nn.Conv2d(shape[0], 32, 8, 4),
            nn.ReLU(inplace=True),
            nn.Conv2d(32, 64, 4, 2),
            nn.ReLU(inplace=True),
            nn.Conv2d(64, 64, 3, 1),
            nn.ReLU(inplace=True)
        )

    def forward(self, x):
        return self.convs(x)

    def action(self, x):
        raise NotImplementedError

    def value(self, x, a=None):
        raise NotImplementedError

    def loss_fn(self, x, target):
        raise NotImplementedError

    def init(self):
        self.apply(_init_weight)

    def _feature_size(self):
        x = torch.zeros((1,) + self.shape, dtype=torch.float)
        y = self.convs(x)
        return np.prod(y.shape)

class DQN(BasicNet):
    def __init__(self, shape, na):
        super(DQN, self).__init__(shape, na)
        self.fc = nn.Sequential(
            nn.Linear(self._feature_size(), 512),
            nn.ReLU(inplace=True)
        )
        self.output = nn.Linear(512, na)

    def forward(self, x):
        assert x.shape[1:] == self.shape
        x = self.convs(x)
        x = x.view(x.shape[0], -1)
        x = self.fc(x)
        x = self.output(x)
        return x

    def action(self, x):
        return self.forward(x).max(1, True)[1]

    def value(self, x, a=None):
        assert a is None or a.shape == (x.shape[0], 1)
        return self.forward(x).max(1, True)[0] if a is None else self.forward(x).gather(1, a)

    def loss_fn(self, x, target):
        return F.smooth_l1_loss(x, target)

class DuelDQN(BasicNet):
    """A Duelling Q Network"""
    def __init__(self, shape, na):
        super(DuelDQN, self).__init__(shape, na)
        self.fc_v = nn.Sequential(
            nn.Linear(self._feature_size(), 512),
            nn.ReLU(inplace=True)
        )
        self.fc_ad = nn.Sequential(
            nn.Linear(self._feature_size(), 512),
            nn.ReLU(inplace=True)
        )
        self.output_v = nn.Linear(512, 1)
        self.output_ad = nn.Linear(512, na)

    def forward(self, x):
        assert x.shape[1:] == self.shape
        x = self.convs(x)
        x = x.view(x.shape[0], -1)
        x_v = self.output_v(self.fc_v(x))
        x_ad = self.output_ad(self.fc_ad(x))
        return x_v + x_ad - x_ad.mean(1, True)

    def action(self, x):
        return self.forward(x).max(1, True)[1]

    def value(self, x, a=None):
        assert a is None or a.shape == (x.shape[0], 1)
        return self.forward(x).max(1, True)[0] if a is None else self.forward(x).gather(1, a)

    def loss_fn(self, x, target):
        return F.smooth_l1_loss(x, target)

def _noisilize(m):
    """replace nn.Linear with NoisyLinear"""
    for key, item in m.named_children():
        if isinstance(item, nn.Linear):
            if hasattr(m, "_modules"):
                m._modules[key] = NoisyLinear(int(item.in_features), int(item.out_features), item.bias is not None)
            elif isinstance(m, nn.Sequential):
                m[int(key)] = NoisyLinear(int(item.in_features), int(item.out_features), item.bias is not None)
            else:
                m[key] = NoisyLinear(int(item.in_features), int(item.out_features), item.bias is not None)
class NoisyNet(BasicNet):
    """Noisilize input model, return a NoisyNet"""
    def __init__(self, model):
        for key, item in model.__dict__.items():
            setattr(self, key, item)
        self.apply(_noisilize)

        self.forward = model.forward
        self.action  = model.action
        self.value   = model.value
        self.loss_fn = model.loss_fn

if __name__ == "__main__":
    import gym
    import numpy as np
    import cv2

    torch.manual_seed(0)
    env = gym.make("PongNoFrameskip-v4")
    if isinstance(env.action_space, gym.spaces.Discrete):
        na = env.action_space.n
    if isinstance(env.observation_space, gym.spaces.Box):
        h, w = env.observation_space.shape[0:2]

    policy_net = DuelDQN((4,h,w), na)
    policy_net = NoisyNet(policy_net)
    # print(policy_net._modules)
    policy_net.forward(0)
    # policy_net.apply(_print)
    # print("++++++++++++++++++++++++++++++++++")
    # print(policy_net._modules)


# SPDX-License-Identifier: BSD-3-Clause AND Apache-2.0
# Copyright 2018 Regents of the University of California
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Copyright 2019 Blue Cheetah Analog Design Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

from pybag.core import BBox, BBoxArray, Transform
from pybag.enum import Orientation, Orient2D

arr_data = [
    ((2, 4, 5, 10), 2, 3, 7, 12),
    ((-6, 0, 0, 3), 3, 2, -12, 7),
    ((-4, 4, 2, 7), 3, 2, -12, 7),
]

transform_data = [
    (((0, 0, 3, 6), 2, 3, 7, 12), 2, 4, "R0", ((2, 4, 5, 10), 2, 3, 7, 12)),
    (((0, 0, 3, 6), 2, 3, 7, 12), 0, 0, "R90", ((-6, 0, 0, 3), 3, 2, -12, 7)),
    (((0, 0, 3, 6), 2, 3, 7, 12), 2, 4, "R90", ((-4, 4, 2, 7), 3, 2, -12, 7)),
]

def make_bbox_array(barr_info):
    base = BBox(barr_info[0][0], barr_info[0][1], barr_info[0][2], barr_info[0][3])
    return BBoxArray(base, barr_info[1], barr_info[2], barr_info[3], barr_info[4])

@pytest.mark.parametrize("box_data, nx, ny, spx, spy", arr_data)
def test_constructor(box_data, nx, ny, spx, spy):
    base = BBox(*box_data)
    ref = BBoxArray(base, nx, ny, spx, spy)
    assert ref.base == base
    assert ref.nx == nx
    assert ref.ny == ny
    assert ref.spx == spx
    assert ref.spy == spy

    a1 = BBoxArray(base, Orient2D.x, nx, spx, ny, spy)
    a2 = BBoxArray(base, Orient2D.y, ny, spy, nx, spx)
    assert a1 == ref
    assert a2 == ref

@pytest.mark.parametrize("barr1_info, dx, dy, orient, barr2_info", transform_data)
def test_transform(barr1_info, dx, dy, orient, barr2_info):
    barr1 = make_bbox_array(barr1_info)
    barr2 = make_bbox_array(barr2_info)
    xform = Transform(dx, dy, Orientation[orient])
    a = barr1.get_transform(xform)
    assert a == barr2
    assert a is not barr1

    b = barr1.transform(xform)
    assert barr1 == barr2
    assert b is barr1


#!/usr/bin/python3
import xml.etree.ElementTree as ET
import sqlite3
from hashlib import sha1
import os
import shutil

groups = {'Training':1, 'Testing':2}
labels = {}
descriptions = {}

def open_db():
    global db
    db = sqlite3.connect('ImageNet.dsx')

    # Insert Groups
    cursor = db.cursor()
    cursor.execute("insert or ignore into groups (name,id) values (?,?)", ('Training', groups['Training'],))
    cursor.execute("insert or ignore into groups (name,id) values (?,?)", ('Testing', groups['Testing'],))

    # Insert Labels
    for l,i in labels.items():
        cursor.execute("insert or ignore into labels (name, id, description) values (?,?,?)", (l,i,descriptions[i]))

def close_db():
    db.commit()
    db.close()

def parse_labels():
    with open('synset_words.txt') as f:
        idx = 1
        for l in f:
            pair = l.split(' ', 1)
            labels[pair[0]] = idx
            descriptions[idx] = pair[1]
            idx = idx + 1

def insert_val():
    group = 'Testing'
    with open('val.txt') as f:
        for l in f:
            filename, label = l.split(' ')
            name, ext = os.path.splitext(filename)
            label = int(label.strip('\n')) + 1

            # Source file path
            src = os.path.join('ILSVRC2012', filename)

            # Destination file path
            checksum = sha1(name.encode()).hexdigest()
            dst = os.path.join('data', checksum[0:2])
            dst = os.path.join(dst, checksum[2:4])
            if not os.path.exists(dst):
                os.makedirs(dst)
            dst = os.path.join(dst, name + ext)

            shutil.copyfile(src, dst)
            cursor = db.cursor()
            # Insert File into database
            cursor.execute("insert into files (name, ext) values (?,?)", (name, ext))
            fileid = cursor.lastrowid

            # Insert Images with ROI into the database
            cursor.execute("insert into boxed_annotations \
                        (group_id, file_id, label_id, x, y, w, h) values (?,?,?,?,?,?,?)",
                        (groups[group], fileid, label, 0, 0, 0, 0, ))

def main():
    print("Start progress\n")
    parse_labels()
    open_db()
    insert_val()
    close_db()

if __name__ == '__main__':
    try:
        main()
    except Exception as err:
        print('Error: {}'.format(err))


import numpy as np

a = np.array([10, 20, 30, 40])
b = np.arange(4)

c = a - b # a\b元素逐个相减
d = a + b

e = b**2 # 求b每个元素的平方

f = 10*np.sin(a) # 求a每个元素的sin值(其他三角函数同)

#print(b < 3) # 判断b中哪些元素小于3

#print(b == 3)

g = np.array([[10, 20],
              [11, 12]])
h = b.reshape( (2, 2) )

c1 = g*h # 元素逐个直接相乘
c_dot = np.dot(h, g) # 两个矩阵进行标准的矩阵乘法

#print(c1)
#print(c_dot)

arr = np.random.random( (2, 4) ) # 随机生成一个2行4列的矩阵，每个元素的值在(0, 1)之间
print(arr)

print(np.sum(arr)) # 矩阵所有元素相加求和
print(np.sum(arr, axis=1)) # axis=1 行求和， axis=0 列求和
print(np.max(arr)) # 矩阵所有元素的最大值， axis参数同上
print(np.min(arr)) # 矩阵所有元素的最小值， axis参数同上


# ch13_9.py
from tkinter import *
from  tkinter.ttk  import *
def printSelection():                               # 列印選項
    print(var.get())

root = Tk()
root.title("ch13_9")                                # 視窗標題
root.geometry("300x120")

var = StringVar()
cb = Combobox(root,textvariable=var)                # 建立Combobox
cb["value"] = ("Python","Java","C#","C")            # 設定選項內容
cb.current(0)                                       # 設定預設選項
cb.pack(pady=10)

btn = Button(root,text="Print",command=printSelection) # 建立按鈕
btn.pack(pady=10,anchor=S,side=BOTTOM)

root.mainloop()

_4.python/__code/Python GUI 設計活用 tkinter之路/ch13/ch13_9.py


#coding: UTF-8
######################################################################################################
#
# Cuerpo del código del robot, se definen las funciones base del robot.
#
# RLP 2019
#
######################################################################################################

from core.hexapod_core import HexapodCore
from time import sleep

class HexapodFull(HexapodCore):

	def boot_up(self):

		self.look()
		self.lie_down()
		self.curl_up()
		self.lie_flat()
		self.get_up()

	def shut_down(self):

		self.look()
		self.lie_down()
		self.lie_flat()
		self.curl_up(die = True)

	def curl_up(self, die = False, t = 0.2):

		for leg in self.legs:
			leg.pose(hip_angle = 0,
					 knee_angle = -(leg.knee.max + leg.knee.leeway),
					 ankle_angle = leg.ankle.max)

		sleep(t)

		if die: self.off()

	def lie_flat(self, t = 0.15):

		for leg in self.legs:
			leg.pose()

		sleep(t)

	def lie_down(self, maxx = 50, step = 4, t = 0.15):

		for angle in xrange(maxx, -(maxx + 1), -step):
			self.squat(angle)

		sleep(t)

	def get_up(self, maxx = 70, step = 4):

		for angle in xrange(-maxx, maxx + 1, step):
			self.squat(angle)

		self.default()

	def look(self, angle = 0, t = 0.05):
		self.neck.pose(angle)
		sleep(t)

	def twist_hip(self, angle = 0, t = 0.1):

		for hip in self.hips:
			hip.pose(angle)

		sleep(t)

	def squat(self, angle, t = 0):

		for leg in self.legs:
			leg.move(knee_angle = angle)

		sleep(t)

	def walk(self, offset = 25 , swing =  25, raised = -30, floor = 50, repetitions = 4, t = 0.2):
		""" if swing > 0, hexy moves forward else backward """

		swings = [offset - swing, swing, -(offset + swing)]
		reverse_swings = [-x for x in swings]

		for r in xrange(repetitions):
			self.stride(self.tripod1, self.tripod2, swings, raised, floor, t)
			self.stride(self.tripod2, self.tripod1, reverse_swings, raised, floor, t)

	def rotate(self, offset = 40, raised = -30, floor = 50, repetitions = 5, t = 0.2):
		""" if offset > 0, hexy rotates left, else right """

		for r in xrange(repetitions):

			#replant tripod2 with an offset
			self.uniform_move(self.tripod2, None, raised, t)
			self.uniform_move(self.tripod2, offset, floor, t)

			#raise tripod1
			self.uniform_move(self.tripod1, -offset, raised)

			#swing tripod2's hips to an -offset
			self.uniform_move(self.tripod2, -offset, None, t)

			#lower tripod1
			self.uniform_move(self.tripod1, 0, floor, t)

	def stride(self, first_tripod, second_tripod, swing, raised, floor, t):
		""" first_tripod's legs replant to propel towards a direction while
			second_tripod's legs retrack by swinging to the opposite direction """

		self.simultaneous_move(first_tripod, knee_angle = raised)
		sleep(t)

		self.simultaneous_move(second_tripod, swing[::-1])
		self.simultaneous_move(first_tripod, swing, floor)
		sleep(t)

	def tilt_side(self, left_angle = 50, right_angle = 0, t = 0.2):
		""" if left_angle > right_angle, left side is higher than right side """

		self.uniform_move(legs = self.left_legs, knee_angle = left_angle)
		self.uniform_move(legs = self.right_legs, knee_angle = right_angle)
		sleep(t)

	def tilt(self, front_angle = 50, middle_angle = 25, back_angle = 0, t = 0.2):
		""" if front_angle > middle_angle > back_angle hexy's front is higher than his back """

		self.right_front.move(knee_angle = front_angle)
		self.left_front.move(knee_angle = front_angle)

		self.right_middle.move(knee_angle = middle_angle)
		self.left_middle.move(knee_angle = middle_angle)

		self.right_back.move(knee_angle = back_angle)
		self.left_back.move(knee_angle = back_angle)

		sleep(t)

	def default(self, offset = 45, floor = 60, raised = -30,  t = 0.2):
		""" Hexy's default pose, offset > 0 brings the front and back legs to the side """

		swings = [offset, 0, -offset]

		self.look()
		self.squat(floor, t)

		self.simultaneous_move(self.tripod1, swings, raised, t)
		self.simultaneous_move(self.tripod1, swings, floor, t)
		self.simultaneous_move(self.tripod2, swings[::-1], raised, t)
		self.simultaneous_move(self.tripod2, swings[::-1], floor, t)

	def uniform_move(self, legs, hip_angle = None, knee_angle = None, t = 0):
		""" moves all legs with hip_angle, knee_angle """

		for leg in legs:
			leg.move(knee_angle, hip_angle)

		sleep(t)

	def simultaneous_move(self, legs, swings = [None, None, None], knee_angle = None, t = 0):
		""" moves all legs with knee_angle to the respective hip angles at 'swing' """

		for leg, hip_angle in zip(legs, swings):
			leg.move(knee_angle, hip_angle)

		sleep(t)

	def shake_head(self, maxx = 60, repetitions = 5, t = 0.2):

		for r in xrange(repetitions):
			self.look(maxx, t)
			self.look(-maxx, t)

		self.look()

	def point(self, t = 0.75):

		self.left_front.hip.pose(-45)
		self.left_front.knee.pose(-50)
		self.left_front.ankle.pose(-55)

		sleep(t)

	def wave(self, repetitions = 5, t = 0.2):

		self.left_front.ankle.pose()
		self.left_front.knee.pose(-50)

		for r in xrange(repetitions):
			self.left_front.hip.pose(-45)
			sleep(t)
			self.left_front.hip.pose(45)
			sleep(t)

	def dance_twist(self, maxx = 45, step = 5, repetitions = 3, t = 0.01):

		self.squat(60, t)

		for r in xrange(repetitions):

			for angle in xrange(-maxx, maxx, step):
				self.twist_hip(angle, t)

			for angle in xrange(maxx, -maxx, -step):
				self.twist_hip(angle, t)

		self.twist_hip()
		self.squat(60, t)

	def lean_back(self, offset = 45, back_knee = 0, middle_knee = 40, raised = -30, t = 0.2):
		""" brings the back legs even further to the back and the middle legs to the front
			and then brings his front legs up in the air """

		self.left_back.replant(raised, back_knee, offset, t)
		self.right_back.replant(raised, back_knee, -offset, t)
		self.left_middle.replant(raised, middle_knee, -offset, t)
		self.right_middle.replant(raised, middle_knee, offset, t)

		self.left_front.pose(-offset, 0, 0)
		self.right_front.pose(offset, 0, 0)

		sleep(t)

	def type_stuff(self, up = -40, down = 40, repetitions = 5, t = 0.2):

		self.lean_back()

		for r in xrange(repetitions):

			self.left_front.knee.pose(up)
			self.right_front.knee.pose(down)
			sleep(t)

			self.right_front.knee.pose(up)
			self.left_front.knee.pose(down)
			sleep(t)

		sleep(t)

	def tilt_left_and_right(self, raised = 60, floor = 20, repetitions = 5, t = 0.15):

		for r in xrange(repetitions):
			self.tilt_side(left_angle = floor, right_angle = raised)
			self.tilt_side(left_angle = raised, right_angle = floor)

		self.squat(raised, t)

	def tilt_front_and_back(self, up = 60, mid = 40, down = 20, repetitions = 5, t = 0.15):

		for r in xrange(repetitions):
			self.tilt(up, mid, down)
			self.tilt(down, mid, up)

		self.squat(up, t)

	def dance_tilt(self, raised = 60, mid = 40, floor = 20, repetitions = 3, t = 0.15):

		for r in xrange(repetitions):

			self.tilt(floor, mid, raised, t) # front
			self.tilt_side(raised, floor, t) # right
			self.tilt(raised, mid, floor, t) # back
			self.tilt_side(floor, raised, t) # left

		self.squat(raised, t)

	def rock_body(self,  offset = 45, floor = 50, repetitions = 7):

		for r in xrange(repetitions):
			self.uniform_move(self.left_legs, offset, floor, 0)
			self.uniform_move(self.right_legs, -offset, floor, 0.2)
			self.uniform_move(self.left_legs, -offset, floor, 0)
			self.uniform_move(self.right_legs, offset, floor, 0.2)

	def light(self, numLed, RGB = "R", onOff = True):

		if RGB == "R":
			self.leds[numLed].red(onOff)
		elif RGB =="G":
			self.leds[numLed].green(onOff)
		elif RGB == "B":
			self.leds[numLed].blue(onOff)

	def lightOff(self, numLed):
		self.leds[numLed].off()

	def afraid(self):
		self.light(0)
		self.lie_down()
		self.lie_flat()
		self.lightOff(0)

	# Dancing

	def prepare(self, offset = 45, back_knee = 0, middle_knee = 50, front_knee = 60, raised = -30, t = 0.2):
		""" brings the back legs even further to the back and the middle legs to the front
			and then brings his further to the front """

		self.left_back.replant(raised, back_knee, offset, t)
		self.right_back.replant(raised, back_knee, -offset, t)
		self.left_middle.replant(raised, middle_knee, -offset, t)
		self.right_middle.replant(raised, middle_knee, offset, t)

		self.left_front.replant(raised, front_knee, -offset, t)
		self.right_front.replant(raised, front_knee, offset, t)

		self.neck.pose()

		sleep(t)

	def wave_right_arm_up(self):

		self.right_front.knee.pose(-60)
		self.right_front.ankle.pose(0)
		self.right_front.hip.pose(-45)
		self.neck.pose(-40)

	def wave_right_arm_down(self):
		self.right_front.knee.pose(50)
		self.right_front.ankle.pose(-50)
		self.right_front.hip.pose(45)
		self.neck.pose(0)

	def dip_body(self, mid = 50, back = 0):

		self.left_middle.move(knee_angle = mid)
		self.right_middle.move(knee_angle = mid)
		self.left_back.move(knee_angle = -back)
		self.right_back.move(knee_angle = -back)

	def raise_body(self, mid = 70, back = 20):

		self.left_middle.move(knee_angle = mid)
		self.right_middle.move(knee_angle = mid)
		self.left_back.move(knee_angle = back)
		self.right_back.move(knee_angle = back)

	def night_fever(self):

		self.prepare()

		for r in xrange(4):
			self.wave_right_arm_up()
			self.left_front.move(knee_angle = 40)
			self.dip_body()
			sleep(0.4)
			self.wave_right_arm_down()
			self.left_front.move(knee_angle = 60)
			self.raise_body()
			sleep(0.4)

	def arms_up_left(self):
		self.right_front.pose(knee_angle = -60, ankle_angle = -80, hip_angle = -45)
		self.left_front.pose(knee_angle = -60, ankle_angle = -80, hip_angle = -45)
		self.neck.pose(-45)

	def arms_up_right(self):
		self.right_front.pose(knee_angle = -60, ankle_angle = -80, hip_angle = 45)
		self.left_front.pose(knee_angle = -60, ankle_angle = -80, hip_angle = 45)
		self.neck.pose(45)

	def arms_down_center(self):
		self.right_front.pose(knee_angle = 30, ankle_angle = -60, hip_angle = 0)
		self.left_front.pose(knee_angle = 30, ankle_angle = -60, hip_angle = 0)
		self.neck.pose()

	def thriller_routine0(self):
		self.arms_down_center()
		self.raise_body()
		sleep(0.3)

	def thriller_routine1(self):
		self.thriller_routine0()
		self.arms_up_left()
		self.dip_body()
		sleep(0.3)

	def thriller_routine2(self):
		self.thriller_routine0()
		self.arms_up_right()
		self.dip_body()
		sleep(0.3)

	def thriller(self):

		self.prepare()

		for r in xrange(3):
			self.thriller_routine1()
			self.thriller_routine2()


# Daily coding problem 4 -- but I mislabeled, so its 5 now
# Source: Stripe
# Given an array of integers, find the first missing positive
# integer in linear time and constant space. In other words,
# find the lowest positive integer that does not exist in the array.
# The array can contain duplicates and negative numbers as well.

# For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.

#### First solution: apparently this is O(nlogn + n) time
# 1- I would get rid of the negative numbers
# 2- Then sort the array -- potentially most time consuming part
# 3- then see whats the next expected number
# 4- compare the expected number with the one thats there
# 5- if its the same then continue
# 6- otherwise return that number

def first_lost(myarray):

    #append the value in myarray to newarray if its positive
    newarray = [x for x in myarray if x >= 0]
    newarray = store_sort(newarray)
    for i,num in enumerate(newarray):
        if newarray[i+1] != num+1: return num+1
        else: continue

# This is the simplest form of the sorting algorithm, but it quiet slow
# it will run the loop as many times as the n*(len)

def sort_array(newarray):
    swap = True
    x=-1
    while swap:
        swap = False
        x=x+1
        for i in range(1,len(newarray)-x):
            if newarray[i] < newarray[i-1]:
                newarray[i], newarray[i-1] = newarray[i-1], newarray[i]
                swap = True
    return newarray

# Lets try sorting in another way: by storing minimum and just moving that around
# this will loop through the array length (array) times, doesnt use too much extra space
def store_sort(newarray):
    for i in range(0, len(newarray)):
        min = newarray[i]
        index=i
        for j in range(i+1,len(newarray)):
            if newarray[j] < min:
                newarray[j], newarray[index] = newarray[index], newarray[j]
                min = newarray[i]
                index = i
    return newarray

# test sorting
b = [3, 4, -1, 1]
print(store_sort(b))

#test the code itself
#print(first_lost(b))


# Pablo Fernández R.
import math

def main():
    with open("gen_data.csv", 'w') as outfile:
        outfile.write(
            "latitude, longitude, time, temp, hum, alt, PM2.5, CO2, CO, NO2, NH3 \n")
        radius = 0.0005
        steps = 50.0
        increment = 2.0*math.pi/steps
        angle = 0.0
        while not math.isclose(angle, 2.0*math.pi):
            x = math.sin(angle)*radius
            y = math.cos(angle)*radius
            value = angle
            outfile.write("{},{},17:48:44.00,24,40,634,80,13,{},10,4".format(
                40.4106902+x, -3.6938668+y, value/math.pi)+"\n")
            angle += increment

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
import csv
import io
import pickle
import re
import nltk
from nltk.classify.scikitlearn import SklearnClassifier
from sklearn.linear_model import LogisticRegression

def processTweet(tweet):
    # Convert to lower case
    tweet = tweet.lower()
    # Convert www.* or https?://* to URL
    tweet = re.sub('((www\.[^\s]+)|(https?://[^\s]+))', 'URL', tweet)
    # Convert @username to AT_USER
    tweet = re.sub('@[^\s]+', 'AT_USER', tweet)
    # Remove additional white spaces
    tweet = re.sub('[\s]+', ' ', tweet)
    # Replace #word with word
    tweet = re.sub(r'#([^\s]+)', r'\1', tweet)

    # tweet = re.findall(r'[^,;\s]+',tweet)

    # try to split hashtag words
    # tweet = re.findall('#[A-Z][^A-Z]*',tweet)
    # tweet = ' '.join(tweet)

    tweet = re.sub('@[^/]+', ' ', tweet)

    # Remove numbers from string
    # tweet = re.sub(r'-?[0-9]', '', tweet)

    # trim
    tweet = tweet.strip('\'"')

    # drop text begin with '(@'
    tweet = re.sub('[(]', '', tweet)

    return tweet

def replaceTwoOrMore(s):
    # detect 2 or more repetitions of character and replace with the character itself
    pattern = re.compile(r"(.)\1+", re.DOTALL)
    return pattern.sub(r"\1\1", s)

def getStopWordList(stopWordListFileName):
    # read the stopwords
    stopWords = ['AT_USER', 'URL']

    fp = io.open(stopWordListFileName, 'r', encoding='utf-8')
    line = fp.readline()
    while line:
        word = line.strip()
        stopWords.append(word)
        line = fp.readline()
    fp.close()
    return stopWords

def getFeatureVector(tweet, stopWords):
    featureVector = []

    # remove , . from the tweets
    tweet = re.findall(r'[^,;\s]+', tweet)
    tweet = ' '.join(tweet)
    tweet = re.findall(r'[^.;\s]+', tweet)
    tweet = ' '.join(tweet)

    # remove ' from tweets
    # tweet = re.findall(r"^[^*$<,>?!']*$", tweet)
    # tweet = ' '.join(tweet)

    # split tweet into words
    words = tweet.split()
    for w in words:

        # replace two or more with two occurrences
        w = replaceTwoOrMore(w)
        # strip punctuation
        w = w.strip('\'"?,.')
        # check if the word stats with an turkish alphabet
        # val = re.search(r"^[a-zA-Z0-9ğüşöçİĞÜŞÖÇ]+$", w)

        # ignore if it is a stop word
        if w in stopWords:
            continue
        else:
            # print w
            featureVector.append(w.lower())
    return featureVector

def extract_features(tweet):
    tweet_words = set(tweet)
    features = {}
    for word in featureList:
        features['contains(%s)' % word] = (word in tweet_words)
    return features

# Read the tweets one by one and process it
inpTweets = csv.reader(open('data/newData.csv', 'rb'), delimiter=';')
stopWords = getStopWordList('data/stopWords.txt')

featureList = []
tweets = []
tempList = []
count = 0

for row in inpTweets:
    tweet = row[0].decode('ISO-8859-9')  # tweet from csv file
    sentiment = row[1].decode('ISO-8859-9')  # sentiment of tweet from csv file

    processedTweet = processTweet(tweet)
    featureVector = getFeatureVector(processedTweet, stopWords)
    featureVectorForBigram = []

    # convert to bigram
    # tweetToBigram = ' '.join(featureVector)
    # templist.append(tweetToBigram)
    # bigrams = ngrams(tweetToBigram.split(),2)
    # trigrams = ngrams(tweetToBigram.split(),3)
    # for grams in bigrams:
    #     doString = ' '.join((grams[0],grams[1]))
    #     featureVectorForBigram.append((doString))
    # doString =' '.join((grams[1],grams[0]))
    # featureVectorForBigram.append((doString))

    # for grams in trigrams:
    #     doString = ' '.join((grams[0],grams[2]))
    #     featureVectorForBigram.append((doString))
    # doString = ' '.join((grams[2],grams[0]))
    # featureVectorForBigram.append(doString)

    # while converting to bigram change featureVector to featureVectorForBigram

    featureList.extend(featureVector)
    tweets.append((featureVector, sentiment))

# CountVectorizer codes
# vectorizer = CountVectorizer(analyzer='word', ngram_range=(1, 2))
# x = vectorizer.fit_transform(templist)
# newFeatures = vectorizer.get_feature_names()
# to shuffle tweets
# random.shuffle(tweets)

length = int(len(tweets) * 0.90)

training_tweets = tweets[:length]  # 90% to train
testing_tweets = tweets[length:]  # 10% to learn
del tweets

# Remove featureList duplicates
featureList = list(set(featureList))

# Extract feature vector for all tweets
training_set = nltk.classify.util.apply_features(extract_features, training_tweets)
testing_set = nltk.classify.util.apply_features(extract_features, testing_tweets)

# Logistic Regression
LogisticRegression_classifier = SklearnClassifier(LogisticRegression())
LogisticRegression_classifier.train(training_set)

# save_classifier = open("data/pickled_algos/LogisticRegressionclassifier.pickle", "wb")
# pickle.dump(LogisticRegression_classifier, save_classifier)
# save_classifier.close()

print ("Logistic Regression Accuracy Percent : ", (nltk.classify.accuracy(LogisticRegression_classifier, testing_set)) * 100)

# # Write wrong tweets with keywords
# for counter in range(0, len(testing_tweets)):
#     a = (testing_tweets[counter][0])
#     sent = testing_tweets[counter][1]
#     testTweet = ' '.join(a)
#     processedTestTweet = processTweet(testTweet)
#     actualResult = LogisticRegression_classifier.classify(
#         extract_features(getFeatureVector(processedTestTweet, stopWords)))
#     if actualResult != sent:
#         print testTweet, "Predicted : ", actualResult, "Expected : ", sent
#         stripped = testTweet.split()
#         for word in stripped:
#             for c in range(0, 10):
#                 a = mostList[c]
#                 if a[0] == word and a[1] == True:
#                     print word, a[2], a[3]
#         print "\n"
#
#
# # to print most informative words
# mostList = []
# mostList2 = NBClassifier.show_most(100)
# for counter in range(0, 100):
#     contains = mostList2[counter][0]
#     contains = contains[contains.find("(") + 1:contains.find(")")]
#     isContain = mostList2[counter][1]
#     sent = mostList2[counter][2]
#     ratio = mostList2[counter][3]
#     mostList.append((contains, isContain, sent, ratio))

# Test the classifier
# testTweet = ''
# processedTestTweet = processTweet(testTweet)
# print testTweet
# print NBClassifier.classify(extract_features(getFeatureVector(processedTestTweet,stopWords)))

# print("Original Naive Bayes Algorithm Accuracy Percent:",(nltk.classify.accuracy(NBClassifier,testing_set))*100)
#
# save_classifier = open("data/pickled_algos/originalNaiveBayes.pickle","wb")
# pickle.dump(NBClassifier,save_classifier)
# save_classifier.close()

# Multinomial Naive Bayes
# MNB_classifier = SklearnClassifier(MultinomialNB())
# MNB_classifier.train(training_set)
# print ("MNB_classifier accuracy percent: ", (nltk.classify.accuracy(MNB_classifier, testing_set)) * 100)
#
# save_classifier = open("data/pickled_algos/MNBclassifier.pickle","wb")
# pickle.dump(MNB_classifier, save_classifier)
# save_classifier.close()

# Bernoulli Naive Bayes
# BernoulliNB_classifier = SklearnClassifier(BernoulliNB())
# BernoulliNB_classifier.train(training_set)
# print ("BernoulliNB accuracy percent: ", (nltk.classify.accuracy(BernoulliNB_classifier, testing_set)) * 100)
#
# save_classifier = open("data/pickled_algos/BernoulliNBclassifier.pickle","wb")
# pickle.dump(BernoulliNB_classifier, save_classifier)
# save_classifier.close()

# Bernoulli RBM
# model = BernoulliRBM()
# model.fit_transform(training_set).toarray()

# count_vect = CountVectorizer()
# X_train_counts= count_vect.fit_transform(featureList)
# model = BernoulliRBM()
# model.fit(X_train_counts)

# Print all wrongly estimated tweets for Logistic Regression
# for counter in range(0,len(testing_tweets)):
#     a=testing_tweets[counter][0]
#
#     testTweet = ' '.join(a)
#     processedTestTweet = processTweet(testTweet)
#     actualResult = LogisticRegression_classifier.classify(extract_features(getFeatureVector

# test the classifier with tweets from the txt file
# with open("data/resultYeni.txt") as f:
#   data = f.readlines()
#
# for counter in range(0,len(data)):
#     a=data[counter].decode('utf-8')
#     testTweet = ''.join(a)
#     processedTestTweet = processTweet(testTweet)
#     actualResult = LogisticRegression_classifier.classify(extract_features(getFeatureVector(processedTestTweet,stopWords)))
#     with open("labelledbyMachine.csv", "ab")as csv_file:
#         writer = csv.writer(csv_file, delimiter=';')
#         a = testTweet.encode('utf-8'), actualResult.encode('utf-8')
#         writer.writerow(a)

# Test the Logistic Regression classifier with one tweet
# testTweet = 'ah bu hayat çekilmez'
# processedTestTweet = processTweet(testTweet)
# print testTweet
# print LogisticRegression_classifier.classify(extract_features(getFeatureVector(processedTestTweet,stopWords)))

# # LinearSVC classifier
# LinearSVC_classifier = SklearnClassifier(LinearSVC())
# LinearSVC_classifier.train(training_set)
# print ("Linear SVC accuracy percent", (nltk.classify.accuracy(LinearSVC_classifier, testing_set)) * 100)
#
# save_classifier = open("data/pickled_algos/LinearSVCclassifier.pickle", "wb")
# pickle.dump(LinearSVC_classifier, save_classifier)
# save_classifier.close()

# # SGDC classifier
# SGDC_classifier = SklearnClassifier(SGDClassifier())
# SGDC_classifier.train(training_set)
# print ("SGDClassifier accuracy percent", (nltk.classify.accuracy(SGDC_classifier,testing_set))*100)
#
# save_classifier = open("data/pickled_algos/SGDC_classifier.pickle", "wb")
# pickle.dump(SGDC_classifier, save_classifier)
# save_classifier.close()

# # DecisionTree classifier
# DecisionTree_Classifier = SklearnClassifier(DecisionTreeClassifier())
# DecisionTree_Classifier.train(training_set)
# print ("DecisionTree accuracy percent", (nltk.classify.accuracy(DecisionTree_Classifier,testing_set))*100)
#
# save_classifier = open("data/pickled_algos/DecisionTree_Classifier.pickle", "wb")
# pickle.dump(DecisionTree_Classifier, save_classifier)
# save_classifier.close()


import pandas as pd
from sklearn import ensemble
from sklearn.feature_extraction import DictVectorizer
import locale
import datetime
import numpy as np
import matplotlib.pyplot as plt
import pylab as py
from sklearn.metrics import r2_score
from scipy.stats import spearmanr, pearsonr
from sklearn.cross_validation import train_test_split
# xl = pd.ExcelFile('C:\\Users\\Chetan.Chougle\\Desktop\\Book1.xlsx')
# df = xl.parse("Sheet1")
# print(df.head())
def score_eval(regressor , X_train ,y_train):
    print('evaluating scores')
    print(X_train)
    print(y_train)
    scores = []
    estimators = np.arange(10, 200, 10)
    for n in estimators:
        regressor.set_params(n_estimators=100)
        regressor.fit(X_train, y_train)
        scores.append(regressor.score([[12, 2017]], [10040]))
        print("sdhfsdjhfeii\n")
        print(regressor.score([[12, 2017]], [10040]  ))
    py.title("Effect of n_estimators")
    py.xlabel("n_estimator")
    py.ylabel("score")
    py.plot(estimators, scores)
    py.show()

def r2_eval2(rf,X_train, X_test, y_train, y_test):
    predicted_train = rf.predict(X_train)
    predicted_test = rf.predict(X_test)
    test_score = r2_score(y_test, predicted_test)
    spearman = spearmanr(y_test, predicted_test)
    pearson = pearsonr(y_test, predicted_test)
    print(test_score)
    print('Out-of-bag R-2 score estimate: '+ str(rf.oob_score_))
    print('Test data R-2 score: '+ str(test_score))
    print('Test data Spearman correlation: '+ str(spearman[0]) )
    print('Test data Pearson correlation: '+ str(pearson[0]) )

def predict_heuristic(previous_predict, month_predict, actual_previous_value, actual_value):
    """ Heuristic that tries to mark the deviance from real and prediction as
        suspicious or not.
    """
    if (
         actual_value < month_predict and
         abs((actual_value - month_predict) / month_predict) > .3):
        if (
             actual_previous_value < previous_predict and
             abs((previous_predict - actual_previous_value) / actual_previous_value) > .3):
            return False
        else:
            return True
    else:
        return False

def get_dataframe():
    xl = pd.ExcelFile('D:\\customerinsight\\Book2.xlsx')
    # df = xl.parse("Sheet1")
    # rows = df

    df = pd.DataFrame.from_records(
        xl.parse("Sheet1"),
        columns=['CustomerName', 'Sales', 'Month', 'Year','PreviousMonthSales']
    )
    #df["CustomerName"] = df["CustomerName"].astype('category')
    # print(df)
    #print(df['CustomerName'].unique().tolist())
    return df

def missed_customers():
    """ Returns a list of tuples of the customer name, the prediction, and
        the actual amount that the customer has bought.
    """

    raw = get_dataframe()
    vec = DictVectorizer()
    today = datetime.date.today()
    currentMonth = today.month
    currentYear = today.year
    lastMonth = (today.replace(day=1) - datetime.timedelta(days=1)).month
    lastMonthYear = (today.replace(day=1) - datetime.timedelta(days=1)).year
    results = []

    # Exclude this month's value
    #df = raw.loc[(raw['Month'] != currentMonth) & (raw['Year'] != currentYear)]
    df = raw
    print('aa')
    #print(df['CustomerName'].unique())
    #print(df['CustomerName'].unique().tolist())
    for customer in set(df['CustomerName'].unique().tolist()):
        # compare this month's real value to the prediction
        actual_value = 0.0
        actual_previous_value = 0.0
        # print("here2")
        # Get the actual_value and actual_previous_value
        # print("sddjd")
        # print(raw.loc[(raw['CustomerName'] == customer) & (raw['Year'] ==currentYear ) ]['Sales'])
        # print("sdfs")
        # new_raw = raw.loc[(raw['CustomerName'] == customer)     , 'Sales']
        # new_raw2 = new_raw.loc[(raw['Year'] == currentYear)   ]
        # print( new_raw.iloc[0] )
        # print( raw.loc[(raw['CustomerName'] == customer )['Sales']])
        # print("\n")
        # print("Current year")
        # print(currentYear)
        print("currentMonth")
        print(currentMonth)
        # print("last month")
        # print(lastMonth)
        # print("lastMonthYear")
        # print(lastMonthYear)
        print("sales")
        # print(raw.loc[
        #     (raw['CustomerName'] == customer) &
        #     (raw['Year'].astype(float) == currentYear) &
        #     (raw['Month'].astype(float) == currentMonth)
        # ]['Sales'])
        #
        # print(float(pd.to_numeric( raw.loc[
        #     (raw['CustomerName'] == customer) &
        #     (raw['Year'].astype(float) == currentYear) &
        #     (raw['Month'].astype(float) == currentMonth)
        # ]['Sales'])))
        # actual_previous_value = float(raw.loc[    (raw['CustomerName'] == customer) &
        #     (raw['Year'].astype(float) == currentYear )  & (raw['Month'] == int(currentMonth)) ]['Sales'])
        # print(actual_previous_value)
        # print('before me')
        try:
            actual_previous_value = float(
                raw.loc[
                    (raw['CustomerName'] == customer) &
                    (raw['Year'] == currentYear) &
                    (raw['Month'] == currentMonth)
                ]['Sales']
            )
            actual_value = float(
                raw[
                    (raw['CustomerName'] == customer) &
                    (raw['Year'] == lastMonthYear) &
                    (raw['Month'] == lastMonth)
                ]['Sales']
            )
        except TypeError:
            # If the customer had no sales in the target month, then move on
            continue

        # Transforming Data
        print('Data')
        print(actual_previous_value)
        print(actual_value)
        print('before me')
        temp = df.loc[df['CustomerName'] == customer]
        targets = temp['Sales']

        del temp['CustomerName']
        del temp['Sales']
        del temp['PreviousMonthSales']
        #print(temp)
        #print(targets)
        X_train, X_test, y_train, y_test = train_test_split(temp, targets, train_size=0.8, random_state=42)
        records = temp.to_dict(orient="records")
        vec_data = vec.fit_transform(records).toarray()
        print('\ntemp\n')
        #print(temp)
        #print(records)
        #print(vec_data)
        #print(targets)
        # Fitting the regressor, and use all available cores
        regressor = ensemble.RandomForestRegressor(n_jobs=-1 , oob_score=True , max_features=0.33)
        regressor.fit(vec_data, targets)
        #score_eval(regressor ,vec_data , targets )
        print('CustomerName ')
        print(customer)
        r2_eval2(regressor ,X_train, X_test, y_train, y_test)

        # Predict the past two months using the regressor
        previous_predict = regressor.predict(vec.transform({
            'Year': lastMonthYear,
            'Month': lastMonth
        }).toarray())[0]
        month_predict = regressor.predict(vec.transform({
            'Year': currentYear,
            'Month': currentMonth
        }).toarray())[0]
        print('bb')
        print(previous_predict)
        print('cc')
        print(month_predict)
        if (predict_heuristic(previous_predict, month_predict, actual_previous_value, actual_value)):
            results.append((
                customer,
                month_predict,
                actual_previous_value
            ))

    return results

if __name__ == '__main__':
    locale.setlocale(locale.LC_ALL, '')
    customers = missed_customers()
    print("here")
    # print(customers)
    for customer in  set(customers):
        print("{} was predicted to buy around {}, they bought only {}".format(
            customer[0],
            locale.currency(customer[1], grouping=True),
            locale.currency(customer[2], grouping=True)
        ))


#!/usr/bin/env python
# coding: utf-8

# In[6]:

import tensorflow as tf
import keras
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import cv2
from tensorflow.keras.preprocessing import image

# In[7]:

img = image.load_img("D:/dataset/flipkart/train/jeans/jeans (72).jpeg")
plt.imshow(img)

# In[8]:

cv2.imread("D:/dataset/flipkart/train/jeans/jeans (72).jpeg").shape

# In[9]:

cv2.imread("D:/dataset/flipkart/train/jeans/jeans (72).jpeg")

# In[10]:

train = ImageDataGenerator(rescale = 1/255)
validation = ImageDataGenerator(rescale = 1/255)

# In[11]:

train_dataset = train.flow_from_directory("D:/dataset/flipkart/train",
                                     target_size = (200,200),
                                     batch_size = 10,
                                     class_mode = 'binary')
validation_dataset = train.flow_from_directory("D:/dataset/flipkart/validation",
                                     target_size = (200,200),
                                     batch_size = 10,
                                     class_mode = 'binary')

# In[12]:

train_dataset.class_indices

# In[13]:

train_dataset.classes

# In[14]:

from tensorflow.keras import optimizers

# In[15]:

model = tf.keras.models.Sequential([tf.keras.layers.Conv2D(16,(3,3), activation = 'relu',input_shape = (200,200,3)),
                                                           tf.keras.layers.MaxPool2D(2,2),
                                                           tf.keras.layers.Conv2D(32,(3,3),activation = 'relu'),
                                                            tf.keras.layers.MaxPool2D(2,2),
                                                            tf.keras.layers.Conv2D(64,(3,3),activation = 'relu'),
                                                            tf.keras.layers.MaxPool2D(2,2),
                                                            tf.keras.layers.Flatten(),
                                                            tf.keras.layers.Dense(128,activation = 'relu'),
                                                            tf.keras.layers.Dense(1,activation = 'sigmoid')])

# In[16]:

model.compile(loss= 'binary_crossentropy',
            optimizer= tf.keras.optimizers.RMSprop(lr=0.001),
             metrics =['accuracy'])

# In[17]:

print(model.summary())

# In[18]:

from keras import backend as k

# In[19]:

model_fit = model.fit(train_dataset, steps_per_epoch = 100, epochs =15 , validation_data = validation_dataset)

# In[20]:

plt.plot(model_fit.history['acc'])
plt.plot(model_fit.history['val_acc'])
plt.title('model acc')
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.legend(['train','validation'], loc='upper left')
plt.show()

# In[22]:

dir_path = 'D:/dataset/flipkart/test'
for i in os.listdir(dir_path):
    img = image.load_img(dir_path+'//'+ i, target_size = (200,200))
    plt.imshow(img)
    plt.show()
    x = image.img_to_array(img)
    x =np.expand_dims(x,axis = 0)
    images = np.vstack([x])
    pred = model.predict(images)
    if pred == 0:
        print('its a jeans')
    else:
        print('its a trouser')

# In[ ]:


import cv2
import numpy as np
import matplotlib.pyplot as plt

path = "C:\\Users\\jadin\\Downloads\\misc\\misc\\"
path1 = path+"4.1.04.tiff"

img= cv2.imread(path1,0)
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

output1 = cv2.equalizeHist(img)

clahe1 = cv2.createCLAHE()
clahe2 = cv2.createCLAHE(clipLimit = 2.0,tileGridSize =(8,8))

output2 = clahe1.apply(img)
output3 = clahe2.apply(img)

output = [img,output1,output2,output3]
titles = ["original", "output1","output2","output3"]

for i in range(len(output)):
    plt.subplot(2,2,i+1)
    plt.imshow(output[i],cmap='gray')
    plt.title(titles[i])
    plt.xticks([])
    plt.yticks([])
plt.show()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='domain',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('domain_name', models.CharField(max_length=100)),
            ],
        ),
        migrations.CreateModel(
            name='glossary',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('GlossaryRef', models.CharField(max_length=100)),
                ('OriginName', models.CharField(max_length=100)),
                ('OriginURl', models.URLField()),
                ('OriginDomain', models.ManyToManyField(to='dbview.domain')),
            ],
        ),
        migrations.CreateModel(
            name='term',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('textTerm', models.CharField(max_length=100)),
            ],
        ),
        migrations.CreateModel(
            name='textConcept',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('concept', models.TextField()),
                ('concept_glossary', models.ForeignKey(to='dbview.glossary')),
                ('concept_term', models.ForeignKey(to='dbview.term')),
            ],
        ),
    ]


# Remove collectors where field contains some specified string
#
# python rm-collectors.py <accessId> <accessKey> <field> <string>

import sys

from sumologic import SumoLogic

args = sys.argv
sumo = SumoLogic(args[1], args[2])
field, string = args[3], args[4]
cs = sumo.collectors()

for c in cs:
	if field in c and string in c[field]:
		cv, _ = sumo.collector(c['id'])
		print(sumo.delete_collector(cv).text)