# -*- coding: utf-8 -*-
"""
Created on Fri Nov 12 15:46:47 2021

@author: naisf
"""

import datetime
import numpy               as np
import matplotlib.gridspec as gridspec
import scipy.constants     as cst

from matplotlib   import pyplot as plt
from tqdm         import tqdm
from astropy.time import Time

import fun

as_strided = np.lib.stride_tricks.as_strided

lw      = .5
alpha   = 1
police  = 14
i_fig   = 1
plt.close('all')

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
""" Load the data """
#______________________________________________________________________________

data        = np.loadtxt('./data_demo')
tt_all      = Time(data[0], format='jd')           # Time Vector
data        = data[1:]                             # Loaded data

dt          = tt_all[1:] - tt_all[:-1]              # Time step
dt.format   = 'sec'
dt          = np.mean(dt.value)
tt = tt_all.datetime
                                                   # Retrieve the relevant variables
B, B_mag, n_p, V, V_mag, T, P, qf = fun.read_data(data)

                                                   # ! Important !
                                                   # Check that the strides of B and v are
                                                   # (8, 24)
                                                   # (Otherwise, the algorithm does not work)
if B.strides[0]!=8:
    B = B[:, np.ones_like(B[0], dtype=bool)]
    V = V[:, np.ones_like(B[0], dtype=bool)]

N_points    = len(tt)                               # Number of measurements

Va          = fun.Alfven_speed(B, B_mag, n_p, P)    # Alfvén speed computation

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
''' Inputs '''
#______________________________________________________________________________

sigma_0     = 2. #km/s
epsilon     = 0.1 #km/s / points
prior       = 0.01
dV_min      =.3
current_lim = 0.04

print('__________________________________________________')
print('___________________ PARAMETERS ___________________')
print('sigma = ' + str(sigma_0))
print('eps = ' + str(epsilon))
print('prior = ' + str(prior))
print('dV = ' + str(dV_min))
print('current = ' + str(current_lim))

scale   = np.linspace(25,850,100)         # Scales (in seconds) to span
print(
      '\nInvestigating scales from '
    + str(scale[0])  + ' to '
    + str(scale[-1]) + ' seconds'
)

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
""" Test the Walen relation """
#______________________________________________________________________________

tt_jet      = []                          # Initialise the jet list

for j in tqdm(range(len(scale))):         # Sweep the different scales

    h = int(scale[j] / dt)                # Number of points in the window (should be ODD)
    if h % 2 == 0:
        h += 1

    sigma   = (
        sigma_0 +                  #Define the noise profile sigma
        epsilon * h * (1 - np.exp(-0.5 * (np.linspace(-h/2, h/2, h))**2 / h**2) )
    )

                                          # Reshape the vectors into (..., h, N-h+1) Arrays
                                          # to facilitate the analysis
    Va_reshaped = as_strided(Va, (3,h,N_points-h+1), (8, 24, 24))
    V_reshaped  = as_strided(V,  (3,h,N_points-h+1), (8, 24, 24))
    tt_reshaped = as_strided(tt, (h,N_points-h+1),   (8, 8))

                                          # Substract the central value for each window
    dV          = V_reshaped - V_reshaped[:, h//2, :][:, None]
    dVa         = Va_reshaped - Va_reshaped[:, h//2, :][:, None]

                                          # Compute the posterior ratio
    post_ratio  = fun.compare_posteriors(prior, dVa, dV, h, sigma)

                                          # Memorise the post ratio of extreme scales
                                          #(for plotting purposes)
    if j==0:
        post_ratio_min_scale  = fun.compare_posteriors(prior, dVa, dV, h, sigma)
    elif j==len(scale)-1:
        post_ratio_max_scale  = fun.compare_posteriors(prior, dVa, dV, h, sigma)

                                         # find potential jets
                                         # (post_ratio>1 over two conescutive points)
    i_jet_temp  = np.argwhere(post_ratio > 1)[:, 0]
    i_jet       = fun.consecutive(i_jet_temp, 2)

    n_jets      = len(i_jet)             # Number of potential jets flagged
    if n_jets != 0:
        if len(i_jet[0]) == 0:
            n_jets = 0

    for p in range(n_jets):              # Check the flagged intervals

        add_points  = h - len(i_jet[p])  # adjust to window length
        i_0         = i_jet[p][0] - add_points//2
        i_f         = np.min((i_jet[p][-1] + add_points//2 + 1, len(tt) - 1))

                                         # Rotate to the lmn frame
        lmn_matrix  = np.asarray(
            fun.mva(B[:, i_0: i_f].T, noprint=True).results['Matrice_Passage']
        )
        B_mva       = np.dot(lmn_matrix.T, B[:,i_0:i_f])
        V_mva       = np.dot(lmn_matrix.T, V[:,i_0:i_f])

        #If the interval was not detected at lower scales
        if fun.not_detected(tt_jet, tt, i_0, i_f):

            #If B_L reverses consistently over the interval
            if fun.Bl_reversal(B_mva[0]):
                if fun.stable_sign(B_mva[0], h):

                    #If a velocity jet is observed
                    if fun.velocity_jet(V_mva, dV_min, h,):

                        #If a current sheet is observed
                        if fun.current_sheet(B_mva[0], V_mva[2], scale[j], current_lim, h):

                            #Then add the interval to the detection list
                            tt_jet.append(
                                [[Time(tt[i_0]).jd],
                                 [Time(tt[i_f]).jd],
                                 [np.log(np.max(post_ratio[i_jet[p]]))]]
                                )

list.sort(tt_jet, key=lambda x: x[0])     # Sort the detected intervals
tt_jet = np.array(tt_jet)

if len(tt_jet)==0:                       # Case where no jet is detected
    N_JETS=0
    final_tt_jet = []

else:                                    # Else, concatenate the overlapping intervals
    tt_jet = tt_jet[:, :, 0]

    tt_jet_temp = Time(tt_jet[:, :2], format='jd').datetime
    proba_temp = tt_jet[:, 2]

    detection_vect = np.zeros(N_points)  # Creation of a detection vector, size (N,),
                                         # 1 if in a jet, 0 elsewhere
    proba = np.zeros(N_points)           # initialize the associated log-posterior probability vector

    for i in (range(len(tt_jet[:, 0]))):
        detection_vect[
              (tt >= tt_jet_temp[i, 0])
            & (tt <= tt_jet_temp[i, 1])
        ] = 1

        proba[
              (tt >= tt_jet_temp[i, 0])
            & (tt <= tt_jet_temp[i, 1])
        ] = max(
            proba_temp[i],
            max(
                proba[
                      (tt >= tt_jet_temp[i, 0])
                    & (tt <= tt_jet_temp[i, 1])
                ]
            )
        )

                                          # Number of jets
    i_detection = fun.consecutive(np.argwhere(detection_vect == 1)[:,0],1)
    N_JETS = len(i_detection)

    final_tt_jet = []                     # Final jet timetable
    final_proba  = []                     # Final associated log-posterior probability

    for p in range(N_JETS):               # Re-perform the most obvious checks after concatenation:

        i_0         = i_detection[p][0]   # Start and End of the interval
        i_f         = i_detection[p][-1]

                                          # Transform to the lmn frame
        lmn_matrix  = np.asarray(
            fun.mva(B[:, i_0: i_f].T, noprint=True).results['Matrice_Passage']
        )
        B_mva       = np.dot(lmn_matrix.T, B[:, i_0:i_f])
        V_mva       = np.dot(lmn_matrix.T, V[:, i_0:i_f])
        size = len(B_mva[0])

        # If B_L reverses consistently over the interval
        if fun.Bl_reversal(B_mva[0]):
            if fun.stable_sign(B_mva[0], size):

                # If the slopes of Vl are consistent with a velocity jet
                pente_1 = V_mva[0, size//2] - V_mva[0,  0]
                pente_2 = V_mva[0, size//2] - V_mva[0, -1]
                if np.sign(pente_1) * np.sign(pente_2) == 1:

                    # Then add the interval to the detection list
                    final_tt_jet.append(
                        [[Time(tt[i_detection[p][0]]).jd],
                         [Time(tt[i_detection[p][-1]]).jd]]
                    )
                    final_proba.append(
                        np.max(
                            (proba[i_detection[p][ 0]],
                             proba[i_detection[p][-1]])
                        )
                    )

    final_tt_jet = np.array(final_tt_jet)
    final_proba  = np.array(final_proba)

    #Convert jd to datetime
    final_tt_jet_time = Time(final_tt_jet[:, :, 0], format = 'jd')
    final_tt_jet = final_tt_jet_time.datetime
    N_JETS = len(final_tt_jet)

print('__________________________________________________\n')
print('########## ! ' +str(N_JETS) + ' jet(s) detected ! ##########')
print('__________________________________________________\n')

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
''' Visualize the results '''
#______________________________________________________________________________

fig = plt.figure(i_fig, figsize = (13,9))
i_fig +=1
gs = gridspec.GridSpec(7,1)

ax1 = fig.add_subplot(gs[0])
# ax1.set_title(str(N_JETS) + ' jet(s) detected !')
ax1.plot(tt, B[0],  lw=lw, c='b', alpha = alpha, label = r'$B_R$')
ax1.plot(tt, B_mag, lw=lw, c='k', alpha = alpha, label = r'$B_{mag}$')
plt.setp(ax1.get_xticklabels(), visible=False)

ax2 = fig.add_subplot(gs[2], sharex = ax1)
ax2.plot(tt, B[1],  lw=lw, c='g', alpha = alpha, label = r'$B_T$')
ax2.plot(tt, B_mag, lw=lw, alpha = alpha, c='k')
plt.setp(ax2.get_xticklabels(), visible=False)

ax3 = fig.add_subplot(gs[4], sharex = ax1)
ax3.plot(tt, B[2],  lw=lw, c='r', alpha = alpha, label = r'$B_N$')
ax3.plot(tt, B_mag, lw=lw, alpha = alpha, c='k')
plt.setp(ax3.get_xticklabels(), visible=False)

ax4 = fig.add_subplot(gs[1], sharex = ax1)
ax4.plot(tt, V[0], lw=lw, c='b', alpha = alpha, label = r'$V_R$')
plt.setp(ax4.get_xticklabels(), visible=False)

ax5 = fig.add_subplot(gs[3], sharex = ax1)
ax5.plot(tt, V[1], lw=lw, c='g', alpha = alpha, label = r'$V_T$')
plt.setp(ax5.get_xticklabels(), visible=False)

ax6 = fig.add_subplot(gs[5], sharex = ax1)
ax6.plot(tt, V[2],   lw=lw, c='r', alpha = alpha, label = r'$V_N$')
ax1.set_xlim(tt[0], tt[-1])
plt.setp(ax6.get_xticklabels(), visible=False)

ax7 = fig.add_subplot(gs[6], sharex = ax1)
ax7.plot(tt, n_p,   lw=.5, c='k', alpha = alpha, label = r'$n$')
ax7.set_ylabel(r'${cm^{-3}}$', fontsize=police)
ax77 = ax7.twinx()
ax77.plot(tt, T,   lw=lw, c='purple', alpha = alpha, label = r'$T$')
ax77.set_ylabel(r'${eV}$', fontsize=police, c='purple')

ax1.set_xlim(tt[0], tt[-1])

[ax.set_ylabel(r'${nT}$', fontsize=police)   for ax in [ax1, ax2, ax3]]
[ax.set_ylabel(r'${km/s}$', fontsize=police) for ax in [ax4, ax5, ax6]]
[ax.axhline(0,c='grey', lw=lw)          for ax in [ax1, ax2, ax3]]
[ax.legend(prop={'size': police},  loc='center left',
            bbox_to_anchor=(1,0.5)) for ax in [ax1, ax2, ax3, ax4, ax5, ax6]]

alpha=.2

if N_JETS>0:
    for i in range(final_tt_jet[:,0].size):
        duration = Time(final_tt_jet[i,1]) - Time(final_tt_jet[i,0])
        duration.format = 'sec'
        print(
            '#' + str(i) + ' - '
                + str(int(np.round(duration.value))) + 's - '
                + str(Time(final_tt_jet).iso[i, 0])  + ' - '
                + str(Time(final_tt_jet).iso[i, 1])  + ' - '
                + str(np.round(final_proba[i], 2))
        )
        [ax.axvspan(
            final_tt_jet[i,0],final_tt_jet[i,1], color = 'b', alpha=alpha
        ) for ax in [ax1, ax2, ax3, ax4, ax5, ax6, ax7]]

alpha = 1

[ax.tick_params(labelsize = police) for ax in [ax1, ax2, ax3, ax4, ax5, ax6, ax7]]
plt.tight_layout()
plt.subplots_adjust(hspace=0.3)
plt.show()


from flask import Response
from flask_restful import Resource
from html5lib import serialize
from models import LikePost, db
import json
from . import can_view_post
from my_decorators import post_id_is_integer_or_400_error, id_is_integer_or_400_error, test
import flask_jwt_extended

class PostLikesListEndpoint(Resource):

    def __init__(self, current_user):
        self.current_user = current_user

    @flask_jwt_extended.jwt_required()
    @post_id_is_integer_or_400_error
    def post(self, post_id):
        if not can_view_post(post_id, self.current_user):
            return Response(json.dumps({'message': 'Like does not exist'}), mimetype="application/json", status=404)
        like = LikePost(self.current_user.id, post_id)
        db.session.add(like)
        db.session.commit()
        return Response(json.dumps(like.to_dict()), mimetype="application/json", status=201)

class PostLikesDetailEndpoint(Resource):

    def __init__(self, current_user):
        self.current_user = current_user

    @flask_jwt_extended.jwt_required()
    def delete(self, post_id, id):
        try:
            int(post_id)
            int(id)
        except:
            return Response(
                json.dumps({'message': '{0} must be an integer.'.format(id)}),
                mimetype="application/json",
                status=400
            )
        like = LikePost.query.get(id)
        if not like or like.user_id != self.current_user.id or not can_view_post(post_id, self.current_user):
            return Response(json.dumps({'message': 'Like does not exist'}), mimetype="application/json", status=404)

        LikePost.query.filter_by(id=id).delete()
        db.session.commit()
        serialized_data = {
            'message': 'Like {0} successfully deleted.'.format(id)
        }
        return Response(json.dumps(serialized_data), mimetype="application/json", status=200)

def initialize_routes(api):
    api.add_resource(
        PostLikesListEndpoint,
        '/api/posts/<post_id>/likes',
        '/api/posts/<post_id>/likes/',
        resource_class_kwargs={'current_user': flask_jwt_extended.current_user}
    )

    api.add_resource(
        PostLikesDetailEndpoint,
        '/api/posts/<post_id>/likes/<id>',
        '/api/posts/<post_id>/likes/<id>/',
        resource_class_kwargs={'current_user': flask_jwt_extended.current_user}
    )


'''
Created on 25 Jan 2013

@author: Dave Wilson
'''

import wx
import enhanced_status_bar

FRAME_STYLE = (wx.FRAME_FLOAT_ON_PARENT | wx.SYSTEM_MENU | wx.CAPTION |
                        wx.CLOSE_BOX | wx.RESIZE_BORDER | wx.MAXIMIZE_BOX |
                        wx.CLIP_CHILDREN | wx.FRAME_NO_TASKBAR)

FRAME_DIALOG_STYLE = (wx.FRAME_FLOAT_ON_PARENT | wx.CAPTION |
                      wx.FRAME_NO_TASKBAR)
FRAME_DIALOG_STYLE2 = (FRAME_DIALOG_STYLE | wx.SYSTEM_MENU | wx.CLOSE_BOX)

FRAME_DIALOG_STYLE3 = (FRAME_DIALOG_STYLE2 | wx.RESIZE_BORDER |
                       wx.MAXIMIZE_BOX | wx.RESIZE_BOX)

class GaugeStatusBar(enhanced_status_bar.EnhancedStatusBar):
    def __init__(self, parent):
        super(GaugeStatusBar, self).__init__(parent, style=wx.SB_FLAT)
        self.SetSize((-1, 23))
        self.SetFieldsCount(2)
        self.SetStatusWidths([-1, 100])

        self.progress_gauge = wx.Gauge(self, size=(100, 15))
        self.progress_gauge.Show(False)
        self.AddWidget(self.progress_gauge, pos=1)

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self._on_timer)

    def _on_timer(self, event):
        self.progress_gauge.Pulse()

    def pulse_gauge(self, pulse=True):
        isRunning = self.timer.IsRunning()
        if not isRunning and pulse:
            self.timer.Start(500)
        elif isRunning and not pulse:
            self.timer.Stop()
        self.progress_gauge.Show(pulse)

class GaugeFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(GaugeFrame, self).__init__(*args, **kwargs)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(self.sizer)
        self.panel = wx.Panel(self)
        self.sizer.Add(self.panel, 1, wx.EXPAND)
        self.statusBar = GaugeStatusBar(self)
        self.SetStatusBar(self.statusBar)

    @classmethod
    def modal(cls, *args, **kwargs):
        if not kwargs.get('style'):
            kwargs['style'] = FRAME_DIALOG_STYLE2
        gauge_frame = cls(*args, **kwargs)
        gauge_frame.CenterOnParent()
        gauge_frame.MakeModal()
        gauge_frame.Bind(wx.EVT_CLOSE, gauge_frame.close_modal)
        return gauge_frame

    @classmethod
    def modal_resize(cls, *args, **kwargs):
        kwargs['style'] = FRAME_DIALOG_STYLE3
        return cls.modal(*args, **kwargs)

    def close_modal(self, event):
        event.Skip()
        self.MakeModal(False)
        self.Destroy()

    def set_panel(self, panel):
        self.Freeze()
        self.panel.Destroy()
        self.panel = panel
        self.sizer.Add(self.panel, 1, wx.EXPAND)
        self.Layout()
        self.Thaw()

    def set_gauge_pulse(self, pulse=True):
        self.statusBar.pulse_gauge(pulse)

    def set_status_text(self, status_text=''):
        self.statusBar.SetStatusText(status_text)

    def set_gauge_and_status_text(self, pulse=True, status_text=''):
        self.set_gauge_pulse(pulse)
        self.set_status_text(status_text)

    def show_error_dialog(self, title_text, error_text, icon=None):
#         title_text, error_text = str(title_text), str(error_text)
        if not icon:
            icon = wx.ICON_ERROR
        elif icon == 'information':
            icon = wx.ICON_INFORMATION
        dialog = wx.MessageDialog(self, error_text, title_text,
                                  icon | wx.OK | wx.CENTER)
        dialog.ShowModal()
        dialog.Destroy()

    def set_panel_enable(self, enable=True):
        self.panel.Enable(enable)

if __name__ == '__main__':
    wx_app = wx.App(None)
    frame = GaugeFrame(None, title='Testing GaugeFrame')
    frame.Show()
    frame.set_gauge_and_status_text(True, 'Test status')
    wx_app.MainLoop()


from django.shortcuts import render
from lxml import etree
import urllib.request
from BaseXClient import BaseXClient
import xmltodict

# Create your views here.
xml = etree.parse("app/xml_files/cursos.xml")

# create session
session = BaseXClient.Session('localhost', 1984, 'admin', 'admin')

def cursos2(request):
    info = dict()
    cursos = xml.xpath('//curso')
    for c in cursos:
        info[c.find('guid').text] = c.find('nome').text

    tparams = {
        'cursos': info,
        'frase': "Cursos da Universidade de Aveiro"
    }

    return render(request, 'cursos.html', tparams)

def cursos(request):
    query = "xquery <root>{for $c in collection('cursosUA')//curso return <elem> {$c/guid} {$c/nome} </elem>}</root>"
    exe = session.execute(query)
    #print("exe: ", exe)

    output = xmltodict.parse(exe)
    print("out: ", output)

    info = dict()
    for c in output["root"]["elem"]:
        info[c["guid"]] = c["nome"]

    tparams = {
        'cursos': info,
        'frase': "Cursos da Universidade de Aveiro"
    }

    return render(request, 'cursos.html', tparams)

def curso_details(request, guid):
    info = dict()

    curso = xml.find("//curso[guid = '{}']".format(guid))

    info['guid'] = guid
    info['nome'] = curso.find('nome').text
    info['codigo'] = curso.find('codigo').text
    info['grau'] = curso.find('grau').text

    info['departamentos'] = []
    for depart in curso.xpath(".//departamentos/departamento"):
        info['departamentos'].append(depart.text)

    info['areas'] = []
    for a in curso.xpath(".//areascientificas/areacientifica"):
        info['areas'].append(a.text)

    info['local'] = curso.find('local').text

    tparams = {
        'curso': info
    }
    return render(request, 'detalhes.html', tparams)

def more_details(request, guid):
    url = "http://acesso.ua.pt/xml/curso.v5.asp?i=" + str(guid)
    document = urllib.request.urlopen(url).read()
    root = etree.fromstring(document)

    xsl_file = etree.parse('app/xml_files/curso.xsl')
    tranform = etree.XSLT(xsl_file)
    html = tranform(root)

    tparams = {
        't': html
    }

    return render(request, "fullDetails.html", tparams)

def by_grau(request):
    grau = request.GET.get("grau")
    cursos = xml.xpath("//curso[grau = '{}']".format(grau))

    info = dict()

    for c in cursos:
        info[c.find('guid').text] = c.find('nome').text

    tparams = {
        'cursos': info,
        'frase': 'Cursos do grau ' + grau + ':'
    }

    return render(request, 'cursos.html', tparams)

def by_depart(request):
    depart = request.GET.get("depart")
    info = dict()
    cursos = xml.xpath("//curso[departamentos//departamento = '{}']".format(depart))

    for c in cursos:
        info[c.find('guid').text] = c.find('nome').text

    tparams = {
        'cursos': info,
        'frase': 'Cursos do ' + depart + ':'
    }

    return render(request, 'cursos.html', tparams)

def by_area(request):
    area = request.GET.get("area")
    info = dict()
    cursos = xml.xpath("//curso[areascientificas//areacientifica = '{}']".format(area))

    for c in cursos:
        info[c.find('guid').text] = c.find('nome').text

    tparams = {
        'cursos': info,
        'frase': 'Cursos da área ' + area + ':'
    }

    return render(request, 'cursos.html', tparams)

def by_local(request):
    local = request.GET.get("local")
    cursos = xml.xpath("//curso[local = '{}']".format(local))

    info = dict()

    for c in cursos:
        info[c.find('guid').text] = c.find('nome').text

    print("cursos", cursos)

    tparams = {
        'cursos': info,
        'frase': 'Cursos em ' + local + ':'
    }

    return render(request, 'cursos.html', tparams)

def departamentos(request):
    departamentos = xml.xpath("//cursos//departamentos//departamento")

    info = set([d.text for d in departamentos])

    tparams = {
        'departs': info
    }
    return render(request, 'departamentos.html', tparams)

def areas(request):
    areas = xml.xpath("//curso//areascientificas//areacientifica")

    info = set([a.text for a in areas])

    tparams ={
        'areas': info
    }
    return render(request, 'areas.html', tparams)

def locais(request):
    locais = xml.xpath("//curso//local")

    info = set([l.text for l in locais])

    tparams = {
        'locais': info
    }

    return render(request, 'locais.html', tparams)


def mul_matriz(m1, m2):
    matriz = []
    for i in range(len(m1)):
        matriz.append([0] * len(m2[0]))

    for i in range(len(m1)):
        for j in range(len(m2[0])):
            for k in range(len(m2)):
                matriz[i][j] += m1[i][k] * m2[k][j]

    for l in matriz:
        print(" ".join([str(elem) for elem in l]))

def main():
    #
    m1 = input()
    m1_row, m1_col = m1.split("x")
    m1_row, m1_col = int(m1_row), int(m1_col)
    #
    m2 = input()
    m2_row, m2_col = m2.split("x")
    m2_row, m2_col = int(m2_row), int(m2_col)

    if m1_col != m2_row:
        return print("Las columnas de la primera tabla deben ser iguales a las filas de la segunda tabla")

    matriz_1 = []
    matriz_2 = []
    for i in range(m1_row):
        aux = []
        for k in range(m1_col):
            n = int(input())
            aux.append(n)

        matriz_1.append(aux)

    for i in range(m2_row):
        aux = []
        for k in range(m2_col):
            n = int(input())
            aux.append(n)

        matriz_2.append(aux)

    mul_matriz(matriz_1, matriz_2)

main()


from __future__ import annotations

from dataclasses import dataclass
from typing import Any

import docker

@dataclass
class PythonCommand:
    """Represents a python command.

    Parameters
    ----------
    script : name
        Python script to invocate.

    args : dict
        Python scripts arguments.

    name : str, default="python"
        Python command name.
    """

    script: str
    args: dict | None = None
    name: str = "python"

    def list(self) -> list[str]:
        args = self.args or {}
        args_list = [f"--{k}={v}" for k, v in args.items()]
        return [self.name, self.script] + args_list

class DockerRunner:
    """Runs Docker containers.

    Parameters
    ----------
    image_uri : str
        The image to run.

    command : str, default=None
        The command to run in the container.

    environment : dict, default=None
        Environment variables to set inside the container, as a dictionary.

    entrypoint : str or list of str, default=None
        The entrypoint for the container

    volumes : list of str or dict, default=None
        A dictionary to configure volumes mounted inside the container.
        Or a list of strings which each one of its elements specifies a mount
        volume.

    name : str, default=None
        Name of the container.

    detach : bool, default=False
        Run container in the background and return a Container object.
    """

    def __init__(
        self,
        image_uri: str,
        command: str | list[str] | None = None,
        environment: dict[str, str] | None = None,
        entrypoint: str | list[str] | None = None,
        volumes: list[str] | dict[str, str] | None = None,
        ports: dict | None = None,
        name: str | None = None,
        detach: bool = False,
    ) -> None:
        self.image_uri = image_uri
        self.command = command
        self.environment = environment
        self.entrypoint = entrypoint
        self.volumes = volumes
        self.ports = ports
        self.name = name
        self.detach = detach

        #: Actual Docker Python client.
        self.docker_client = docker.from_env()

    def run(self) -> None | docker.models.containers.Container:
        """Runs container.

        Returns
        -------
        If detach is True, a Container object is returned instead else None.
        """
        return self.docker_client.containers.run(
            image=self.image_uri,
            command=self.command,
            entrypoint=self.entrypoint,
            environment=self.environment,
            volumes=self.volumes,
            ports=self.ports,
            name=self.name,
            detach=self.detach,
        )

class DockerScriptRunner:
    """Runs Python scripts inside Docker containers.

    Parameters
    ----------
    script: str
        Path to the python script

    image_uri : str
        Docker image to run.

    environment : dict
        Environment variables to set inside the container.

    args : dict
        Script args. These are passed to the script as arguments.
    """

    #: Script volume to be mounted in the container.
    SCRIPT_VOLUME = "{script}:/tmp/{script}"

    def __init__(
        self,
        script: str,
        image_uri: str,
        environment: dict[str, str] | None = None,
        args: dict[str, Any] | None = None,
    ) -> None:
        self.script = script
        self.image_uri = image_uri
        self.environment = environment
        self.args = args

    @property
    def script_volume(self) -> str:
        return self.SCRIPT_VOLUME.format(script=self.script)

    def run(self, **kwargs) -> None:
        """Runs script inside Docker container.

        Parameters
        ----------
        kwargs : keyword args
            Additonal arguments to pass to :meth:`docker.client.containers.run`.
        """
        docker_runner = self.create_docker_runner()
        docker_runner.run(**kwargs)

    def create_docker_runner(self) -> DockerRunner:
        """Creates Docker runner.

        Returns
        -------
        runner : DockerRunner
        """
        volumes = [self.script_volume]
        command = self.create_command().list()
        return DockerRunner(
            image_uri=self.image_uri,
            command=command,
            environment=self.environment,
            volumes=volumes,
        )

    def create_command(self) -> PythonCommand:
        # Script path inside container.
        script = self.script_volume.split(":")[-1]
        return PythonCommand(script=script, args=self.args)


import sys
from collections import deque

def bfs(i, j):
    di = [1, 0, 0, -1]
    dj = [0, 1, -1, 0]
    visited = [[-1] * M for _ in range(N)]
    queue = deque()
    queue.append([i, j])
    visited[i][j] = 0
    result = []
    while queue:
        x, y = queue.popleft()
        for d in range(4):
            ni = x + di[d]
            nj = y + dj[d]
            if 0 <= ni < N and 0 <= nj < M:
                if maze[ni][nj] == 1 and visited[ni][nj] == -1:
                    queue.append([ni, nj])
                    visited[ni][nj] = visited[x][y] + 1
                if ni == N - 1 and nj == M - 1:
                    result.append(visited[x][y] + 1)
    return min(result) + 1

N, M = map(int, sys.stdin.readline().split())

maze = [[int(i) for i in sys.stdin.readline().rstrip()] for _ in range(N)]

print(bfs(0, 0))


from torchvision import models
from torch import nn
from classBlock import ClassBlock
import torch

class resnet50(nn.Module):
    def __init__(self, class_num, pretrained=False, model_name='resnet50'):
        super(resnet50, self).__init__()
        self.model_name = model_name
        self.class_num = class_num
        self.pretrained = pretrained

        model_ft = getattr(models, self.model_name)(pretrained=self.pretrained)
        model_ft.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        model_ft.fc = nn.Sequential()
        self.features = model_ft
        self.num_ftrs = 2048

        for c in range(self.class_num):
            self.__setattr__('class_%d' % c, ClassBlock(input_dim=self.num_ftrs, class_num=1, activ='sigmoid') )

    def forward(self, x):
        x = self.features(x)
        x = x.view(x.size(0), -1)
        pred_label = [self.__getattr__('class_%d' % c)(x) for c in range(self.class_num)]
        pred_label = torch.cat(pred_label, dim=1)
        return pred_label

if __name__ == "__main__":
    #model = resnet50(class_num=11).cuda()
    #input = torch.randn(size=(1,3,224,224), dtype=torch.float32)
    #print(model)
    # from torchsummary import summary
    # summary(model, (3,224,224))
    #
    model = resnet50(class_num=40)
    tet = torch.Tensor(2, 3, 224, 224)

    print(torch.ones_like(tet) * 0.6)
    #print(model(tet))         * 0.6


import numpy as np
import pandas as pd
import sys

from collections import Counter
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor
from operator import mul
from queue import PriorityQueue
from statistics import mean, median

def get_input(filename):
    with open(filename, "r") as fin:
        s = fin.read()
    s = s.split("\n")
    m = [["." for i in range(len(s[0]))] for j in range(len(s))]
    for i, line in enumerate(s):
        for j, c in enumerate(line):
            m[i][j] = c
    return m

def get_next(m, x, y):
    if m[x][y] == ">":
        return x, (y + 1) % len(m[0])
    elif m[x][y] == "v":
        return (x + 1) % len(m), y

def move(m, x, y, moved):
    if m[x][y] == ".":
        return False
    nx, ny = get_next(m, x, y)
    #if m[nx][ny] != "." or nm[nx][ny] != ".":
    if m[nx][ny] != "." or (nx, ny) in moved:
        #nm[x][y] = m[x][y]
        return False
    #print(m[x][y], x, y, nx, ny)
    m[nx][ny] = m[x][y]
    m[x][y] = "."
    #moved[nx][ny] = True
    #nm[nx][ny] = m[nx][ny]
    moved.add((x, y))
    moved.add((nx, ny))
    return True

def solve1(filename):
    m = get_input(filename)
    print(np.array(m))

    step = 0
    is_moving = True
    while is_moving:
        is_moving = False
        #moved = [[False for i in range(len(m[0]))] for j in range(len(m))]
        #nm = [["." for i in range(len(m[0]))] for j in range(len(m))]
        moved = set()
        for i in range(len(m)):
            for j in range(len(m[0])):
                if (i, j) not in moved and m[i][j] == ">" and move(m, i, j, moved):
                    is_moving = True
        #print(np.array(m))
        moved = set()
        for i in range(len(m)):
            for j in range(len(m[0])):
                if (i, j) not in moved and m[i][j] == "v" and move(m, i, j, moved):
                    is_moving = True
        step += 1
        #m = nm
        #print(np.array(m))
    print(step)

def solve2(filename):
    pass

if __name__ == "__main__":
    if sys.argv[1] == "1":
        solve1(sys.argv[2])
    elif sys.argv[1] == "2":
        solve2(sys.argv[2])
    else:
        print("Bad command")

"""

"""


from vosk import Model, KaldiRecognizer
import pyaudio

import socket
s = socket.socket()
print('socket created')
s.bind(('localhost',9999))
s.listen(2)
print('waiting for connections')
c,addr = s.accept()
print("Connected with",addr)

model = Model(r"C:\Users\samya\Documents\HALProject\Working_files\vosk-model-en-in-0.5")
recognizer = KaldiRecognizer(model,16000)

mic = pyaudio.PyAudio()
stream = mic.open(format=pyaudio.paInt16, channels=1, rate= 16000, input=True, frames_per_buffer=8192 )
stream.start_stream()

while True:
    data = stream.read(4096)
    # if len(data) == 0;
    # break
    if recognizer.AcceptWaveform(data):
        text = recognizer.Result()
        print(text[14:-3])
        c.send(bytes(text[14:-3],'utf-8'))


# ---------------------- PROBLEM 48 (RANDOM) ----------------------------------#
# A Graph is a non-linear data structure consisting of nodes and edges.
# The nodes are sometimes also referred to as vertices and the edges are lines or
# arcs that connect any two nodes in the graph.
# USES: Social Networks, Location/ Mapping, Routing Algos, Visual Hierarchy, File System Oraganizations.
class Graph:
	def __init__(self):
		self.adjacencyList = {}

# ----------------METHOD 01---------------------#
	# COMPLEXITY = TIME: O(1), SPACE: O(|V| + |E|)
	def addVertex(self, vertex_name):
		if vertex_name not in self.adjacencyList:
			self.adjacencyList[vertex_name] = []
		return self.adjacencyList
# ----------------METHOD 01---------------------#

# ----------------METHOD 02---------------------#
	# COMPLEXITY = TIME: O(1), SPACE: O(|V| + |E|)
	def addEdge(self, vertex1, vertex2):
		self.adjacencyList[vertex1].append(vertex2)
		self.adjacencyList[vertex2].append(vertex1)
		return self.adjacencyList
# ----------------METHOD 02---------------------#

# ----------------METHOD 03---------------------#
	# COMPLEXITY = TIME: O(E), SPACE: O(1)
	def removeEdge(self, vertex1, vertex2):
		self.adjacencyList[vertex1].remove(vertex2)
		self.adjacencyList[vertex2].remove(vertex1)
		return self.adjacencyList
# ----------------METHOD 03---------------------#

# ----------------METHOD 04---------------------#
	# COMPLEXITY = TIME: O(|V| + |E|), SPACE: O(1)
	def removeVertex(self, vertex2):
		lst = self.adjacencyList[vertex2].copy()
		for vertex1 in lst:
			self.removeEdge(vertex1, vertex2)
		del self.adjacencyList[vertex2]
		return self.adjacencyList
# ----------------METHOD 04---------------------#

# ----------------METHOD 05---------------------#
	# COMPLEXITY = TIME: O(|V| + |E|), SPACE: O(V)
	def dfsRecursive(self, vertex, results=[]):
		lst = self.adjacencyList

		if not lst[vertex]: return None

		results.append(vertex)

		for node in self.adjacencyList[vertex]:
			if node not in results:
				self.dfsRecursive(node, results)
		return results
# ----------------METHOD 05---------------------#

# ----------------METHOD 06---------------------#
	# COMPLEXITY = TIME: O(|V| + |E|), SPACE: O(V)
	def dfsIterative(self, vertex):
		lst = self.adjacencyList
		if not lst[vertex]: return None

		results = []
		stack = []

		stack.append(vertex)
		while len(stack) != 0:
			node = stack.pop()
			results.append(node)
			for neigh in lst[node]:
				if neigh not in results and neigh not in stack:
					stack.append(neigh)
		return results
# ----------------METHOD 06---------------------#

# ----------------METHOD 07---------------------#
	# COMPLEXITY = TIME: O(|V| + |E|), SPACE: O(V)
	def bfsIterative(self, vertex):
		lst = self.adjacencyList
		if not lst[vertex]: return None

		results = []
		queue = [vertex]
		while len(queue) != 0:
			node = queue.pop(0)
			results.append(node)
			for neigh in lst[node]:
				if neigh not in queue and neigh not in results:
					queue.append(neigh)
		return results
# ----------------METHOD 07---------------------#

class WeightedGraph:
	def __init__(self):
		self.adjacencyList = {}

# ----------------METHOD 01---------------------#
	# COMPLEXITY = TIME: O(1), SPACE: O(|V| + |E|)
	def addVertex(self, vertex_name):
		if vertex_name not in self.adjacencyList:
			self.adjacencyList[vertex_name] = []
		return self.adjacencyList
# ----------------METHOD 01---------------------#

# ----------------METHOD 02---------------------#
	# COMPLEXITY = TIME: O(1), SPACE: O(|V| + |E|)
	def addEdge(self, vertex1, vertex2, weight):
		self.adjacencyList[vertex1].append({"node":vertex2, "weight": weight})
		self.adjacencyList[vertex2].append({"node":vertex1, "weight": weight})
		return self.adjacencyList
# ----------------METHOD 02---------------------#

graph = Graph()
graph.addVertex('Tokyo')
graph.addVertex('Dallas')
graph.addVertex('Aspen')
graph.addVertex('Hong Kong')
graph.addVertex('Los Angeles')
graph.addEdge('Hong Kong','Tokyo')
graph.addEdge('Aspen','Dallas')
graph.addEdge('Los Angeles','Dallas')
graph.addEdge('Hong Kong','Dallas')
graph.addEdge('Dallas','Tokyo')
print(graph.addEdge('Los Angeles','Hong Kong'))
# {'Tokyo': ['Hong Kong', 'Dallas'],
# 'Dallas': ['Aspen', 'Los Angeles', 'Hong Kong', 'Tokyo'],
# 'Aspen': ['Dallas'],
# 'Hong Kong': ['Tokyo', 'Dallas', 'Los Angeles'],
# 'Los Angeles': ['Dallas', 'Hong Kong']}

# print(graph.removeEdge('Hong Kong','Dallas'))
print(graph.removeVertex("Hong Kong"))
# {'Tokyo': ['Dallas'],
# 'Dallas': ['Aspen', 'Los Angeles', 'Tokyo'],
# 'Aspen': ['Dallas'], 'Los Angeles': ['Dallas']}

print('****************************')

graph_rec = Graph()
graph_rec.addVertex('A')
graph_rec.addVertex('B')
graph_rec.addVertex('C')
graph_rec.addVertex('D')
graph_rec.addVertex('E')
graph_rec.addVertex('F')
graph_rec.addEdge('A', 'B')
graph_rec.addEdge('A', 'C')
graph_rec.addEdge('B', 'D')
graph_rec.addEdge('C', 'E')
graph_rec.addEdge('D', 'E')
graph_rec.addEdge('D', 'F')
print(graph_rec.addEdge('F', 'E'))
print(graph_rec.dfsRecursive('A'))  # ['A', 'B', 'D', 'E', 'C', 'F']
print(graph_rec.dfsIterative('A'))  # ['A', 'C', 'E', 'F', 'D', 'B']
print(graph_rec.bfsIterative('A'))  # ['A', 'B', 'C', 'D', 'E', 'F']

print('****************************')

weightedGraph = WeightedGraph()
weightedGraph.addVertex("A")
weightedGraph.addVertex("B")
weightedGraph.addVertex("C")
print(weightedGraph.addEdge("A", "B", 250)) # {'A': [{'node': 'B', 'weight': 250}], 'B': [{'node': 'A', 'weight': 250}], 'C': []}
print(weightedGraph.addEdge("C", "B", 30)) # {'A': [{'node': 'B', 'weight': 250}], 'B': [{'node': 'A', 'weight': 250}, {'node': 'C', 'weight': 30}], 'C': [{'node': 'B', 'weight': 30}]}


import logging
import os

import torch
import wordninja
from PIL import Image
from torchvision import transforms

logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s -   %(message)s',
                    datefmt='%m/%d/%Y %H:%M:%S',
                    level=logging.INFO)
logger = logging.getLogger(__name__)

class InputExample(object):
    def __init__(self, text, img_id, label=None):
        """Constructs an InputExample."""
        self.text = text
        self.img_id = img_id
        self.label = label

class MMInputFeatures(object):
    def __init__(self, input_ids,
                 input_mask,
                 added_input_mask,
                 img_feat,
                 hashtag_input_ids,
                 hashtag_input_mask,
                 label_id):
        self.input_ids = input_ids
        self.input_mask = input_mask
        self.added_input_mask = added_input_mask
        self.img_feat = img_feat
        self.hashtag_input_ids = hashtag_input_ids
        self.hashtag_input_mask = hashtag_input_mask
        self.label_id = label_id

class Processer():
    def __init__(self, data_dir, image_path, model_select, max_seq_length, max_hashtag_length):
        self.model_select = model_select
        self.max_seq_length = max_seq_length
        self.max_hashtag_length = max_hashtag_length
        self.image_path = image_path
        self.data_dir = data_dir

    def get_train_examples(self):
        return self._create_examples(os.path.join(self.data_dir, "train.txt"))

    def get_eval_examples(self):
        return self._create_examples(os.path.join(self.data_dir, "valid.txt"))

    def get_test_examples(self):
        return self._create_examples(os.path.join(self.data_dir, "test.txt"))

    def get_labels(self):
        return [0, 1]

    def _create_examples(self, data_file):
        """Creates examples for the training and dev sets."""
        examples = []
        with open(data_file) as f:
            for line in f.readlines():
                lineLS = eval(line)
                tmpLS = lineLS[1].split()
                if "sarcasm" in tmpLS:
                    continue
                if "sarcastic" in tmpLS:
                    continue
                if "reposting" in tmpLS:
                    continue
                if "<url>" in tmpLS:
                    continue
                if "joke" in tmpLS:
                    continue
                if "humour" in tmpLS:
                    continue
                if "humor" in tmpLS:
                    continue
                if "jokes" in tmpLS:
                    continue
                if "irony" in tmpLS:
                    continue
                if "ironic" in tmpLS:
                    continue
                if "exgag" in tmpLS:
                    continue
                img_id = lineLS[0]
                text = lineLS[1]
                label = int(lineLS[-1])
                examples.append(InputExample(text=text, img_id=img_id, label=label))
        return examples

    def image_process(self, image_path, transform):
        image = Image.open(image_path).convert('RGB')
        image = transform(image)
        return image

    def get_image_text(self):
        image_text = {}
        with open(self.image_path) as f:
            for line in f.readlines():
                sp = line.strip().split()
                if sp[0] not in image_text.keys():
                    image_text[sp[0]] = " ".join(sp)
        return image_text

    def convert_mm_examples_to_features(self, examples, label_list, tokenizer):
        label_map = {label: i for i, label in enumerate(label_list)}
        features = []

        transform = transforms.Compose([
            transforms.Resize((224, 224)),
            transforms.ToTensor(),
            transforms.Normalize((0.485, 0.456, 0.406),
                                 (0.229, 0.224, 0.225))])

        for (ex_index, example) in enumerate(examples):

            hashtags = []
            tokens = []

            sent = example.text.split()
            i = 0
            while i < len(sent):
                if sent[i] == "#" and i < len(sent) - 1:
                    while sent[i] == "#" and i < len(sent) - 1:
                        i += 1
                    if sent[i] != "#":
                        temp = wordninja.split(sent[i])
                        for _ in temp:
                            hashtags.append(_)
                else:
                    if sent[i] != "#":
                        temp = wordninja.split(sent[i])
                        for _ in temp:
                            tokens.append(_)
                    i += 1
            tokens = " ".join(tokens)
            hashtags = " ".join(hashtags) if len(hashtags) != 0 else "None"
            tokens = tokenizer.tokenize(tokens)
            hashtags = tokenizer.tokenize(hashtags)

            #####
            # image_text = None
            # image_text_dic = get_image_text()

            # if example.img_id in image_text_dic:
            #     image_text = list(image_text_dic[example.img_id])
            # else:
            #     image_text = ["None"]
            #####

            if len(tokens) > self.max_seq_length - 2:
                tokens = tokens[:(self.max_seq_length - 2)]
            if len(hashtags) > self.max_hashtag_length - 2:
                hashtags = hashtags[:(self.max_hashtag_length - 2)]

            tokens = ["[CLS]"] + tokens + ["[SEP]"]
            input_ids = tokenizer.convert_tokens_to_ids(tokens)
            input_mask = [1] * len(input_ids)
            added_input_mask = [1] * (len(input_ids) + 49)
            padding = [0] * (self.max_seq_length - len(input_ids))
            input_ids += padding
            input_mask += padding
            added_input_mask += padding

            hashtags = ["[CLS]"] + hashtags + ["[SEP]"]
            hashtag_input_ids = tokenizer.convert_tokens_to_ids(hashtags)
            hashtag_input_mask = [1] * len(hashtag_input_ids)
            hashtag_padding = [0] * (self.max_hashtag_length - len(hashtag_input_ids))
            hashtag_input_ids += hashtag_padding
            hashtag_input_mask += hashtag_padding
            assert len(input_ids) == self.max_seq_length
            assert len(input_mask) == self.max_seq_length

            assert len(hashtag_input_ids) == self.max_hashtag_length
            assert len(hashtag_input_mask) == self.max_hashtag_length
            label_id = label_map[example.label]

            # process images
            image_name = example.img_id
            image_path = os.path.join(self.image_path, image_name + ".jpg")
            image = self.image_process(image_path, transform)  # 3*224*224

            features.append(MMInputFeatures(input_ids=input_ids,
                                            input_mask=input_mask,
                                            added_input_mask=added_input_mask,
                                            img_feat=image,
                                            hashtag_input_ids=hashtag_input_ids,
                                            hashtag_input_mask=hashtag_input_mask,
                                            label_id=label_id))
            if ex_index % 1000 == 0:
                logger.info("processed image num: " + str(ex_index) + " **********")
        all_input_ids = torch.tensor([f.input_ids for f in features], dtype=torch.long)
        all_input_mask = torch.tensor([f.input_mask for f in features], dtype=torch.long)
        all_added_input_mask = torch.tensor([f.added_input_mask for f in features], dtype=torch.long)
        all_img_feats = torch.stack([f.img_feat for f in features])
        all_hashtag_input_ids = torch.tensor([f.hashtag_input_ids for f in features], dtype=torch.long)
        all_hashtag_input_mask = torch.tensor([f.hashtag_input_mask for f in features], dtype=torch.long)
        all_label_ids = torch.tensor([f.label_id for f in features], dtype=torch.long)
        return all_input_ids, all_input_mask, all_added_input_mask, all_img_feats, all_hashtag_input_ids, all_hashtag_input_mask, all_label_ids


# -*- coding: utf-8 -*-
"""
Created on Thu Mar 14 16:35:22 2019

@author:
"""

import mne
from mne.time_frequency import psd_welch
import numpy as np

def ffff(fname, tmin, tmax, fmin, fmax, n_fft = 2048):
    ##ffff
    return psds
psdsrrr = ffff(fname='')
#------------------------------------------------------------------------------
#READ MEG DATA FOR BASE CLOSED CONDITION DURING THE FIRST HALF OF DATA
#------------------------------------------------------------------------------
raw = mne.io.read_raw_fif('head_movement\\190131\\base_closed_raw.fif')
raw.info['bads'] += ['MEG1433','MEG1822','MEG1843','MEG1412','MEG0943','MEG1033']
picks = mne.pick_types(raw.info, meg='grad', eeg=False, eog=False,
                           stim=False, exclude='bads')
tmin, tmax = 0, 60  # use the first 120s of data
fmin, fmax = 290, 294  # look at frequencies between
raw.plot_psd(picks=picks, tmin=tmin, tmax=tmax)
n_fft = 2048  # the FFT size (n_fft). Ideally a power of 2
psds, freqs = psd_welch(raw, picks=picks, tmin=tmin, tmax=tmax,
                        fmin=fmin, fmax=fmax)
psds = 20 * np.log10(psds)

#------------------------------------------------------------------------------
#READ MEG DATA FOR BASE CLOSED CONDITION DURING THE FIRST HALF OF DATA
#------------------------------------------------------------------------------
raw2 = mne.io.read_raw_fif('head_movement\\190131\\base_closed_raw.fif')
raw2.info
raw2.info['bads'] += ['MEG1433','MEG1822','MEG1843','MEG1412','MEG0943','MEG1033']
picks2 = mne.pick_types(raw2.info, meg='grad', eeg=False, eog=False,
                           stim=False, exclude='bads')
#raw.plot_psd(picks=picks, tmin=tmin, tmax=tmax)
tmin2, tmax2 = 60, 120  # use the first 120s of data
fmin, fmax = 290, 294  # look at frequencies between
n_fft = 2048  # the FFT size (n_fft). Ideally a power of 2
psds2, freqs2 = psd_welch(raw2, picks=picks2, tmin=tmin2, tmax=tmax2,
                        fmin=fmin, fmax=fmax)
psds2 = 20 * np.log10(psds2)

#CALCULATE THE CORRELATION BETWEEN CHANNELS POWER
first_half = np.ravel(psds)
second_half = np.ravel(psds2)
result1 = np.corrcoef(first_half, second_half)
corr1 = result1[0,1]
print(corr1)

#READ MEG DATA FOR FRONT CLOSED CONDITION DURING THE FIRST HALF OF DATA
raw3 = mne.io.read_raw_fif('head_movement\\190131\\front_closed_raw.fif')
raw3.info['bads'] += ['MEG1433','MEG1822','MEG1843','MEG1412','MEG0943','MEG1033']
picks3 = mne.pick_types(raw3.info, meg='grad', eeg=False, eog=False,
                           stim=False, exclude='bads')
#raw3.plot_psd(picks=picks, tmin=tmin, tmax=tmax)
tmin3, tmax3 = 0, 60  # use the first 120s of data
fmin, fmax = 290, 294  # look at frequencies between
n_fft = 2048  # the FFT size (n_fft). Ideally a power of 2
psds3, freqs3 = psd_welch(raw3, picks=picks3, tmin=tmin3, tmax=tmax3,
                        fmin=fmin, fmax=fmax)
psds3 = 20 * np.log10(psds3)

#CALCULATE THE CORRELATION BETWEEN CHANNELS POWER
base = np.ravel(psds)
front = np.ravel(psds3)
result2 = np.corrcoef(base, front)
corr2 = result2[0,1]
print(corr2)

#READ MEG DATA FOR MOVE CLOSED CONDITION DURING THE FIRST HALF OF DATA
raw4 = mne.io.read_raw_fif('head_movement\\190131\\base_closed_raw.fif')
raw4.info['bads'] += ['MEG1433','MEG1822','MEG1843','MEG1412','MEG0943','MEG1033']
picks4 = mne.pick_types(raw4.info, meg='grad', eeg=False, eog=False,
                           stim=False, exclude='bads')
#raw3.plot_psd(picks=picks, tmin=tmin, tmax=tmax)
tmin4, tmax4 = 0, 60  # use the first 120s of data
fmin, fmax = 290, 294  # look at frequencies between
n_fft = 2048  # the FFT size (n_fft). Ideally a power of 2
psds4, freqs4 = psd_welch(raw4, picks=picks4, tmin=tmin4, tmax=tmax4,
                        fmin=fmin, fmax=fmax)
psds4 = 20 * np.log10(psds4)

#CALCULATE THE CORRELATION BETWEEN CHANNELS POWER
base = np.ravel(psds)
move = np.ravel(psds4)
result3 = np.corrcoef(base, move)
corr3 = result3[0,1]
print(corr3)


#coding:utf-8
from django.shortcuts import render
import subprocess
import os, sys
from django.views.generic.base import TemplateView
import dazhu
import tools.webTools as tools
from django.shortcuts import redirect
from multiprocessing.dummy import Pool as ThreadPool

class index(TemplateView):
    template_name = "pan/index.html"

    def get(self, request, *args, **kwargs):
        return TemplateView.get(self, request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = {}
        class tempFile(object):
            pass

        result = []
        subfolder = dazhu.settings.BASE_DIR + "/dazhu/static/pan/"
        files = os.listdir(subfolder)
        for item in files:
            tools.debug("pan index get_context_data item", item)
            if os.path.isfile(subfolder + item):
                stdin_encoding = sys.stdin.encoding
                if stdin_encoding == None:
                    stdin_encoding = "ANSI_X3.4-1968"
                item = item.decode(stdin_encoding)
                tempfile = tempFile()
                tempfile.filename = item
                tempfile.filelength = os.path.getsize(subfolder + item)
                result.append(tempfile)

        context['downloadfiles'] = result
        context['title'] = u'下载'
        return context

pool = ThreadPool(4)
def download_file(request):
    subfolder = dazhu.settings.BASE_DIR + "/dazhu/static/pan/"
    dp_url = request.GET["dp"]
    tools.debug("download_file dp_url = ", dp_url)
    # out_bytes = subprocess.check_output('wget {}'.format(download_path), shell=True)
    def __download(url):
        tools.debug("__download url", url)
        out_bytes = subprocess.check_output('wget {} -P {}'.format(url, subfolder), shell=True)
        tools.debug("download_file", out_bytes)

    pool.apply_async(__download, (dp_url,))

    return redirect("/pan")

def del_file(request):
    del_path = request.GET["dp"]
    subfolder = dazhu.settings.BASE_DIR + "/dazhu/static/pan/"
    tools.debug("del_file path = ", del_path)
    os.remove(subfolder + del_path)
    # out_bytes = subprocess.check_output('wget {}'.format(download_path), shell=True)
    return redirect("/pan")


#!/usr/bin/python3

from application import app, db
from application.models import Parent, Player, Add, AddPlayer, Delete, Update
from flask import Flask, render_template, request, redirect, url_for
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField

@app.route('/home', methods=['GET', 'POST'])
@app.route('/', methods = ['GET', 'POST'])
def home():
    if request.method =="GET":
       return render_template("index.html", query=Parent.query.all())

@app.route('/add', methods=['GET', 'POST'])
def add():
    form=Add()
    if request.method == 'POST':
        new_team = Parent(name=form.name.data, league=form.league.data)
        if not form.validate_on_submit():
            return render_template('adderror.html', form=form)
        else:
            db.session.add(new_team)
            db.session.commit()
            return redirect(url_for("home"))
    return render_template('add.html', form=form, title="New Team")

@app.route('/parent/<id>', methods = ["GET", "POST"])
def parent(id):
    if request.method == "GET":
        return render_template("update.html", parent= Parent.query.filter_by(id=id).first(), query=Player.query.filter_by(parent_id=id))

@app.route('/player/<id>', methods = ["GET", "POST"])
def player(id):
    form=AddPlayer()
    if request.method == "POST":
        new_player=Player(name=form.name.data, age=form.age.data, position=form.position.data, parent_id=id)
        if not form.validate_on_submit():
            return render_template('playererror.html', form=form)
        else:
            db.session.add(new_player)
            db.session.commit()
            return redirect(url_for("parent", id=new_player.parent_id))
    return render_template('player.html', form=form)

@app.route('/delete/<parent_id>/<id>')
def delete(parent_id, id):
    play = Player.query.filter_by(id=id).first()
    i = play.id
    shay = Player.query.filter_by(parent_id=parent_id).first()
    j = shay.parent_id
    if play is not None:
        db.session.delete(play)
        db.session.commit()
        return redirect(url_for("parent", id=j))

@app.route('/edit/<parent_id>/<id>', methods = ["GET", "POST"])
def edit(parent_id, id):
    form=AddPlayer()
    playeredit = Player.query.filter_by(id=id).first()
    if request.method =="POST":
        playeredit.name=form.name.data
        playeredit.age=form.age.data
        playeredit.position=form.position.data
        if not form.validate_on_submit():
            return render_template('editerror.html', form=form, playeredit=playeredit)
        else:
            db.session.commit()
            return redirect(url_for("parent", id=playeredit.parent_id))
    return render_template("edit.html", form=form, playeredit=playeredit)


# https://leetcode.com/problems/time-based-key-value-store/

from collections import defaultdict

#! failed test cases
class TimeMap1:

    def __init__(self):
        self.store = defaultdict(dict)

    def set(self, key: str, value: str, timestamp: int) -> None:
        self.store[key][timestamp] = value

    def get(self, key: str, timestamp: int) -> str:
        timestamps = self.store.get(key,{})

        if not timestamps:
            return ''

        value = timestamps.get(timestamp)
        if value:
            return value

        return self.get_prev_timestamp(timestamps, timestamp)

    def get_prev_timestamp(self, timestamps: dict[int], target: int):
        timestamps_sorted = sorted(list(timestamps.keys()))

        l,r = 0, len(timestamps_sorted) - 1
        i = (l + r) // 2
        while l <= r:
            i = (l + r) // 2
            if timestamps_sorted[i] > target:
                r -= 1
            else: # it is not gonna be equal
                l += 1
        prev_timestamp = timestamps_sorted[i]
        return timestamps.get(prev_timestamp)

class TimeMap2:

    def __init__(self):
        self.store = defaultdict(list)

    def set(self, key: str, value: str, timestamp: int) -> None:
        self.store[key].append([value, timestamp])

    def get(self, key: str, timestamp: int) -> str:
        result = ""
        valuePairs =  self.store.get(key, [])

        l , r = 0, len(valuePairs) - 1
        while l<=r:
            i = (l+r) // 2
            if valuePairs[i][1] <= timestamp:
                result = valuePairs[i][0]
                l = i + 1
            else:
                r = i -1
        return result


from PIL import Image
from os import listdir
from os import remove

def resizeImages(path, size=(255,255) ):
    try:
        for file in listdir(path):
            target = path+'/'+file

            print(f'[Resizing]\t {target}')
            img = Image.open(target).convert('RGB')
            img.resize(size, Image.Resampling.LANCZOS)
            img.save('./data_process/'+file)
            print(f'[Saving]\t ./data_prcesses/{file}')
            img.close()
            print(f'[Deleting]\t {target}')
            remove(target)
    except Exception as e:
        print(f'Error in rezise process:\n{e}')


import os
import subprocess
from pathlib import Path

from jinja2 import Environment, PackageLoader

from fika.templates.util import (
    _create_dir,
    _create_project_dir,
    _get_model_type_kwarg,
)

class TemplateGenerator(object):

    # Prepare environment and source data
    env = Environment(
        loader=PackageLoader("fika", "templates"),
        trim_blocks=True,
        lstrip_blocks=True,
    )

    project_dir = _create_dir()

    @classmethod
    def generate_service(cls, name: str, filename: str, model):
        """
        Generates the necessary files to run your model as a service.
        Generates the app.py, Dockerfile and requirements.txt file.

        Parameters
        ----------
        name : str
            Project name
        filename : str
            Model file name
        """

        _create_project_dir(cls.project_dir, name=name)

        files = ["main.py", "Dockerfile", "requirements.txt"]
        model_kwargs = _get_model_type_kwarg(model)

        for file in files:
            script = cls.env.get_template("files/" + file.replace(".py", "")).render(
                name=name, filename=filename, service=True, **model_kwargs,
            )

            if file.endswith(".py") or file.endswith(".txt"):
                with open(
                    os.path.join(cls.project_dir, name, "app", file),
                    "w",
                    encoding="utf8",
                ) as f:
                    f.write(script)
            else:
                with open(
                    os.path.join(cls.project_dir, name, file), "w", encoding="utf8"
                ) as f:
                    f.write(script)

        print(f"Deployment files can be found at {cls.project_dir}/{name}.")
        print()


import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

url = 'https://datahub.io/core/co2-ppm/r/co2-mm-mlo.csv'
url2='https://pkgstore.datahub.io/JohnSnowLabs/nasa-global-temperature-anomalies-time-series-1880-2017/nasa-global-temperature-anomalies-time-series-1880-2017-csv_csv/data/d0f07b1b1f0f66bd16e07848ac8794cd/nasa-global-temperature-anomalies-time-series-1880-2017-csv_csv.csv'
url3='https://datahub.io/core/sea-level-rise/r/epa-sea-level.csv'
url4='https://pkgstore.datahub.io/core/co2-fossil-global/global_csv/data/09015539c2fc32bb3c4afead7df461b5/global_csv.csv'
co2_ppm = pd.read_csv(url)
temp = pd.read_csv(url2)
sea= pd.read_csv(url3)
fossil=pd.read_csv(url4)

plt.figure(figsize=(10, 5))
x = range(0,co2_ppm.shape[0])
y = co2_ppm['Interpolated']
y2=co2_ppm['Trend']
plt.plot( x, y, marker='', color='blue', linewidth=2, linestyle='dashed')
plt.plot( x, y2, marker='', color='red', linewidth=2)
plt.xlabel('Month')
plt.ylabel('Atmospheric CO2 concentration (PPM)')
plt.title('Trends in Atmospheric Carbon Dioxide 1958-2016')
plt.legend()
plt.show()

newco2_ppm=co2_ppm.iloc[10:707,3]
x2 = range(0,newco2_ppm.shape[0])
y3 = newco2_ppm
newtemp=temp.iloc[::-1]
y4=[]
for i in range(479,822,6):
    for j in range(2,14):
        y4.append(newtemp.iloc[i,j])
#print(newco2_ppm.shape)
#print(len(y4))
fig, ax1 = plt.subplots()
ax1.set_xlabel('Month')
ax1.set_ylabel('Atmospheric CO2 concentration (PPM)', color='blue')
ax1.plot(x2, y3, color='blue',linewidth=1)
ax1.tick_params(axis='y', labelcolor='blue')
ax2 = ax1.twinx()
ax2.set_ylabel('The average temperature (in Celsius degrees)', color='red')
ax2.plot(x2, y4, color='red',linewidth=1)
ax2.tick_params(axis='y', labelcolor='red')
plt.title('Logarithmic dependence of temperature on CO2 levels 1949-2016')
fig.tight_layout()
plt.show()

x3 = range(0,sea.shape[0])
y5 = sea['CSIRO Adjusted Sea Level']
y6 = sea['Upper Error Bound']
y7 = sea['Lower Error Bound']
plt.figure(figsize=(10, 7))
plt.plot(y6, '--', y7, '--',linewidth=1)
plt.plot( x3, y5, marker='', color='red', linewidth=2)
plt.xlabel('Year')
plt.ylabel('Comulative changes (in inches) in sea level for the world’s oceans(inches)')
plt.title('Global Average Absolute Sea Level Change 1880-2014')
plt.legend(['Upper Error Bound', 'Lower Error Bound', 'CSIRO Adjusted Sea Level'])
plt.gca().fill_between(range(len(x3)),y6, y7,facecolor='blue',alpha=0.25)
plt.show()

y8 = []
y9 = []
y10 = []
y11 = []
y12 = []
for ii in range(fossil.shape[0]):
    y8.append(fossil.iloc[ii,2])
    y9.append(fossil.iloc[ii,3])
    y10.append(fossil.iloc[ii,4])
    y11.append(fossil.iloc[ii,5])
    y12.append(fossil.iloc[ii,6])
yy8=sum(y8)
yy9=sum(y9)
yy10=sum(y10)
yy11=sum(y11)
yy12=sum(y12)
y13=[yy8,yy9,yy10,yy11,yy12]
plt.figure(figsize=(10, 5))
expl = [0,0,0.1,0,0]
labels=['Gas Fuel','Liquid Fuel','Solid Fuel','Cement','Gas Flaring']
plt.pie(y13, explode=expl, labels=labels, autopct='%1.1f%%',pctdistance=0.8, shadow=True)
plt.title('The proportion of Fossil Fuels consumption and cement production for global CO2 emissions 1751-2010', bbox={'facecolor':'0.8', 'pad':5})
plt.show()

x = range(0,fossil.shape[0])
y8 = fossil['Gas Fuel']
y9 = fossil['Liquid Fuel']
y10 = fossil['Solid Fuel']
y11 = fossil['Cement']
y12 = fossil['Gas Flaring']
plt.figure(figsize=(10, 7))
plt.plot( x, y8, marker='', color='blue', linewidth=2)
plt.plot( x, y9, marker='', color='green', linewidth=2)
plt.plot( x, y10, marker='', color='red', linewidth=2)
plt.plot( x, y11, marker='', color='yellow', linewidth=2)
plt.plot( x, y12, marker='', color='brown', linewidth=2)
plt.xlabel('Year')
plt.ylabel('Carbon emissions from fossil fuel consumption and cement production (million metric tons of C)')
plt.title('Global CO2 Emissions from Fossil Fuels consumption and cement production 1751-2010')
plt.legend()
plt.show()


import numpy as np
import gym

from common_files.utils import T
import matplotlib.pyplot as plt
from common_files.gym_to_gif import combine_gifs, label_with_episode_number, plot_from_saved_metrics
import seaborn as sns; sns.set()
import os
import imageio
import numpy as np
import json

np.seterr('raise')

AGENT = 'sac'

if AGENT == 'td3':
    from td3.agent import Agent
elif AGENT == 'sac':
    from sac.agent import Agent

def plot_Q_vals(actor):
    # Plot Q function for some action
    Q_vals = np.zeros((200,100))
    ii=0
    for xx in range(-1, 1, 200):
        for yy in range(0, 1, 100):
            Q_vals[ii%100][ii//100] = actor.critic_model_1(T([xx/100, yy/100, 0,0,0,0,0,0]).unsqueeze(0), T([0,0]).unsqueeze(0)).item()
            ii+=1
    ax = sns.heatmap(Q_vals.T)
    plt.show()

def main(epi):
    epi_reward = 0
    frames = []
    env = gym.make(env_name)
    obs_init = env.reset()
    actor = Agent(obs_init.shape[0], env.action_space.sample().shape[0], lambda: gym.make(env_name), max_env_steps=1000, logger=None)
    actor.load_Q_model(best_reward=False, step=None)
    # plot_Q_vals(actor)
    ii = 0
    print(f'Episode: {epi}')
    while True:
        action, _ = actor.actor_model(T(obs_init), deterministic=True)
        Q_val = actor.critic_model_1(T(obs_init).unsqueeze(0), action.unsqueeze(0))
        frame = env.render(mode="rgb_array")
        frames.append(label_with_episode_number(frame, epi, ii))
        obs, rew, done, _ = env.step(action.tolist())
        epi_reward += rew
        obs_init = obs
        print(f"Step: {ii}    Action: {[round(i,2) for i in action.cpu().detach().tolist()]}    Q_val: {Q_val.item():.3f}     Reward: {rew:.3f}")

        ii+=1
        if done:
            break
    env.close()
    epi_rewards_dic[epi] = epi_reward
    imageio.mimwrite(f'./{AGENT}/{env_name}/{epi}/result.gif', frames, fps=15)

if __name__ == "__main__":
    env_name = 'LunarLanderContinuous-v2'
    epi_list = [10*i for i in range(1, 21)]
    epi_rewards_dic = {}

    for epi in epi_list:
        main(epi)

    json.dump(epi_rewards_dic, open(f"./{AGENT}/{env_name}/epi_rewards_dic.json", 'w'))
    combine_gifs(AGENT, env_name, start_epoch=10, end_epoch=200, interval=10, num_rows=4, num_columns=5)
    plot_from_saved_metrics(AGENT, env_name)


"""The Splitter Class."""
from m4b_util.helpers import cover_utils
from m4b_util.helpers.parallel_ffmpeg import ParallelFFmpeg

def split(
        input_path,
        output_dir_path,
        segment_list,
        output_pattern="segment_{i:04d}.mp3"
):
    """Split a file into multiple, based on segments."""
    cover_utils.extract_cover(input_path, output_dir_path / "cover.png")

    # Generate task list
    tasks = list()
    for i, segment in enumerate(segment_list):
        time = segment.end_time - segment.start_time

        output_dir_path.mkdir(exist_ok=True)
        output_path = output_dir_path / output_pattern.format(i, i=i, title=segment.title)

        cmd = ["ffmpeg", "-ss", str(segment.start_time), "-t", str(time), "-i", input_path,
               "-map", "0:a", "-map_chapters", "-1", "-y"]
        if segment.title:
            cmd.extend(["-metadata", f"title={segment.title}"])
        cmd.append(output_path)

        name = f"Splitting segment {i}"
        if segment.title:
            name += f" - {segment.title}"
        tasks.append({
            "name": name,
            "command": cmd
        })

    # Process splits in parallel
    p = ParallelFFmpeg(f"Splitting '{input_path.name}'")
    p.process(tasks)


# load libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import seaborn as sns
import sklearn
sns.set()
# load dataset

train_df = pd.read_csv('../input/train.csv')
test_df = pd.read_csv('../input/test.csv')

train_df.shape
test_df.shape
train_df.columns.values
train_df.head()
train_df.isnull().sum()[train_df.isnull().sum() > 0]
import missingno as msno
msno.matrix(train_df[['v2a1', 'v18q1', 'rez_esc', 'meaneduc', 'SQBmeaned']], color = (0.211, 0.215, 0.274))
plt.show()
sns.countplot(train_df.loc[(pd.isnull(train_df['v2a1'])), 'tipovivi1'])
plt.title("Ownership")
plt.show()
train_df.loc[(pd.isnull(train_df['v2a1']) & train_df['tipovivi1'] == 1), 'v2a1'] = 0
train_df = train_df.loc[pd.notnull(train_df['v2a1'])]
train_df['v18q1'].dropna().value_counts()
train_df.loc[(pd.isnull(train_df['v18q1'])), 'v18q1'] = 0
train_df['rez_esc'].dropna().value_counts()
# statistical measures of those with rez_esc

train_df.loc[pd.notnull(train_df['rez_esc']),('age')].describe()
# statistical measures of those with missing rez_esc

train_df.loc[pd.isnull(train_df['rez_esc']),('age')].describe()
train_df.drop(columns='rez_esc', inplace = True)
train_df.loc[pd.isnull(train_df['meaneduc']), ('edjefa', 'edjefe', 'escolari', 'meaneduc')]
train_df.loc[pd.isnull(train_df['meaneduc']), 'meaneduc'] = train_df.loc[pd.isnull(train_df['meaneduc']), 'escolari']
train_df.loc[pd.isnull(train_df['SQBmeaned']), 'SQBmeaned'] = train_df.loc[pd.isnull(train_df['SQBmeaned']), 'meaneduc']**2
len(train_df.loc[train_df['age'] == 0].index)
train_df = train_df.loc[train_df['age']!=0]
msno.matrix(train_df[['v2a1', 'v18q1', 'meaneduc', 'SQBmeaned']], color = (0.211, 0.215, 0.274))
plt.show()
train_df.groupby('dependency').size()
mode = train_df.loc[(train_df['dependency'] != 'yes') & (train_df['dependency'] != 'no'), 'dependency'].astype(float).mode()
mode
def mutate_columns(df):
    df.loc[df['dependency']=='no', 'dependency'] = 0
    df.loc[df['dependency']=='yes', 'dependency'] = 0.5 # TODO computation of dependency
    df['dependency'] = df['dependency'].astype('float16')

    #The same applies with edjefe and edjefa EXCEPT that the 'yes' value doesn't make any sense? Does it to you?
    #Anyway, let's just impute meaneduc for 'yes' values

    df.loc[df['edjefe']=='no', 'edjefe'] = 0
    df.loc[df['edjefe']=='yes', 'edjefe'] = df['meaneduc']
    df['edjefe'] = df['edjefe'].astype('uint8')

    df.loc[df['edjefa']=='no', 'edjefa'] = 0
    df.loc[df['edjefa']=='yes', 'edjefa'] = df['meaneduc']
    df['edjefa'] = df['edjefa'].astype('uint8')
mutate_columns(train_df)
mutate_columns(test_df)

plt.figure(figsize = (10,5))
sns.countplot(x='Target', data=train_df, palette="OrRd_r")
plt.xticks([0,1,2,3],['extreme poverty','moderate poverty','vulnerable households','non vulnerable households'])
plt.xlabel('')
plt.ylabel('')
plt.title("Poverty Levels", fontsize = 14)

plt.show()
tdf = train_df[['Target']]
n_train_df = train_df
for col in ['v18q', 'refrig', 'computer', 'television', 'mobilephone', 'v14a']:
    n_train_df[col] = n_train_df[col].astype('category')
dfcat = pd.get_dummies(n_train_df[[ 'v18q', 'refrig', 'computer', 'television', 'mobilephone', 'v14a']])
df_ = pd.concat([dfcat, tdf], axis=1)

df_ = df_.groupby(['Target']).sum()
df_.reset_index(inplace = True)
plt.figure(figsize=(12,4))
groups = ['extreme','moderate','vulnerable','non-vulnerable']

ordered_df = df_.sort_values(by='Target')
my_range=range(1,len(df_.index)+1)

plt.scatter(ordered_df['v18q_1'], my_range, color='#0055a4', label='Present', s=200)
plt.scatter(ordered_df['v18q_0'], my_range, color='#9b1d20' , label='Not present', s=200)
plt.legend(loc = 4, prop={'size': 10})
plt.hlines(y=my_range, xmin=ordered_df['v18q_1'], xmax=ordered_df['v18q_0'], alpha=0.5)
plt.yticks(np.arange(1,5),groups)
plt.xlabel("Number of household")
plt.title("Tablet ownership", fontsize = 14)
plt.show()

plt.figure(figsize=(12, 4))

plt.scatter(ordered_df['refrig_1'], my_range, color='#0055a4', label='Present', s=200)
plt.scatter(ordered_df['refrig_0'], my_range, color='#9b1d20' , label='Not present', s=200)
plt.legend(loc = 4, prop={'size': 10})
plt.hlines(y=my_range, xmin=ordered_df['refrig_1'], xmax=ordered_df['refrig_0'], alpha=0.5)
plt.yticks(np.arange(1,5),groups)
plt.xlabel("Number of household")
plt.title("Refrigerator ownership", fontsize = 14)
plt.show()

plt.figure(figsize=(12, 4))

plt.scatter(ordered_df['computer_1'], my_range, color='#0055a4', label='Present', s=200)
plt.scatter(ordered_df['computer_0'], my_range, color='#9b1d20' , label='Not present', s=200)
plt.legend(loc = 4, prop={'size': 10})
plt.hlines(y=my_range, xmin=ordered_df['computer_1'], xmax=ordered_df['computer_0'], alpha=0.5)
plt.yticks(np.arange(1,5),groups)
plt.xlabel("Number of household")
plt.title("Computer ownership", fontsize = 14)
plt.show()

plt.figure(figsize=(12, 4))

plt.scatter(ordered_df['television_1'], my_range, color='#0055a4', label='Present', s=200)
plt.scatter(ordered_df['television_0'], my_range, color='#9b1d20' , label='Not present', s=200)
plt.legend(loc = 4, prop={'size': 10})
plt.hlines(y=my_range, xmin=ordered_df['television_1'], xmax=ordered_df['television_0'], alpha=0.5)
plt.yticks(np.arange(1,5),groups)
plt.xlabel("Number of household")
plt.title("Television ownership", fontsize = 14)
plt.show()

plt.figure(figsize=(12, 4))

plt.scatter(ordered_df['mobilephone_1'], my_range, color='#0055a4', label='Present', s=200)
plt.scatter(ordered_df['mobilephone_0'], my_range, color='#9b1d20' , label='Not present', s=200)
plt.legend(loc = 4, prop={'size': 10})
plt.hlines(y=my_range, xmin=ordered_df['mobilephone_1'], xmax=ordered_df['mobilephone_0'], alpha=0.5)
plt.yticks(np.arange(1,5),groups)
plt.xlabel("Number of household")
plt.title("Mobile phone ownership", fontsize = 14)
plt.show()

df_ = train_df[['Target', 'male', 'female', 'age']]
df_.loc[(train_df['male'] == 1), 'sex'] = 'male'
df_.loc[(train_df['female'] == 1), 'sex'] = 'female'

plt.figure(figsize = (10,8))
sns.violinplot(x='Target',y='age', data=df_, hue='sex', split=True)
plt.xticks(np.arange(0,5),groups)
plt.show()

plt.figure(figsize = (15,15))
gs = gridspec.GridSpec(4, 2, hspace=0.3)

plt.subplot(gs[0,0])
g = sns.countplot(train_df['r4h3'], hue=train_df['Target'], color="#3274a1")
plt.title("Total males in a household", fontsize = 14)
plt.xlabel('')
plt.ylabel('')
legend = g.get_legend()
legend.set_title("Income group")
new_labels = ['extreme', 'moderate', 'vulnerable', 'non-vulnerable']
for t, l in zip(legend.texts, new_labels): t.set_text(l)

plt.subplot(gs[0,1])
g = sns.countplot(train_df['r4m3'], hue=train_df['Target'], color="#d32d41")
plt.title("Total females in a household", fontsize = 14)
plt.xlabel('')
plt.ylabel('')
legend = g.get_legend()
legend.set_title("Income group")
new_labels = ['extreme', 'moderate', 'vulnerable', 'non-vulnerable']
for t, l in zip(legend.texts, new_labels): t.set_text(l)

plt.subplot(gs[1,0])
sns.countplot(train_df['r4h1'], hue=train_df['Target'], color="#3274a1")
plt.title("Males < 12 years old", fontsize = 14)
plt.xlabel('')
plt.ylabel('')
plt.legend('')

plt.subplot(gs[1,1])
sns.countplot(train_df['r4m1'], hue=train_df['Target'], color="#d32d41")
plt.title("Females < 12 years old", fontsize = 14)
plt.xlabel('')
plt.legend('')

plt.subplot(gs[2,0])
sns.countplot(train_df['r4h2'], hue=train_df['Target'], color="#3274a1")
plt.title("Males >= 12 years old", fontsize = 14)
plt.xlabel('')
plt.ylabel('')
plt.legend('')

plt.subplot(gs[2,1])
sns.countplot(train_df['r4m2'], hue=train_df['Target'], color="#d32d41")
plt.title("Females >= 12 years old", fontsize = 14)
plt.xlabel('')
plt.ylabel('')
plt.legend('')

plt.show()
df_q = train_df[['Target', 'epared1', 'epared2', 'epared3', 'etecho1', 'etecho2', 'etecho3', 'eviv1', 'eviv2', 'eviv3']]
df_q.loc[df_q['epared1'] == 1, 'wall'] = 'Bad'
df_q.loc[df_q['epared2'] == 1, 'wall'] = 'Regular'
df_q.loc[df_q['epared3'] == 1, 'wall'] = 'Good'

df_q.loc[df_q['etecho1'] == 1, 'roof'] = 'Bad'
df_q.loc[df_q['etecho2'] == 1, 'roof'] = 'Regular'
df_q.loc[df_q['etecho3'] == 1, 'roof'] = 'Good'

df_q.loc[df_q['eviv1'] == 1, 'floor'] = 'Bad'
df_q.loc[df_q['eviv2'] == 1, 'floor'] = 'Regular'
df_q.loc[df_q['eviv3'] == 1, 'floor'] = 'Good'

df_q = df_q[['Target', 'wall', 'roof', 'floor']]
print("Roof quality")
print("==============================================================================================================================")
df_q.loc[df_q['Target'] == 1, 'Target'] = 'Extreme'
df_q.loc[df_q['Target'] == 2,'Target'] = 'Moderate'
df_q.loc[df_q['Target'] == 3,'Target'] = 'Vulnerable'
df_q.loc[df_q['Target'] == 4,'Target'] = 'Non-Vulnerable'
ax = sns.catplot(x = 'roof', col = 'Target', data = df_q, kind="count", col_order=['Extreme', 'Moderate', 'Vulnerable', 'Non-Vulnerable']).set_titles("{col_name}")
ax.fig.set_size_inches(15,4)
ax.set(ylabel = '')
plt.show()

print("Wall quality")
print("==============================================================================================================================")

ax = sns.catplot(x = 'wall', col = 'Target', data = df_q, kind="count" ,col_order=['Extreme', 'Moderate', 'Vulnerable', 'Non-Vulnerable'], order = ['Bad', 'Regular', 'Good']).set_titles("{col_name}")
ax.fig.set_size_inches(15,4)
ax.set(ylabel = '')
plt.show()

print("Floor quality")
print("==============================================================================================================================")

ax = sns.catplot(x = 'floor', col = 'Target', data = df_q, kind="count", col_order=['Extreme', 'Moderate', 'Vulnerable', 'Non-Vulnerable']).set_titles("{col_name}")
ax.fig.set_size_inches(15,4)
ax.set(ylabel = '')
plt.show()
# bars1 = [12, 28, 1, 8, 22]
# bars2 = [28, 7, 16, 4, 10]
# bars3 = [25, 3, 23, 25, 17]

# # Heights of bars1 + bars2 (TO DO better)
# bars = [40, 35, 17, 12, 32]

# # The position of the bars on the x-axis
# r = [0,1,2,3,4]

# # Names of group and bar width
# names = ['A','B','C','D','E']
# barWidth = 1

# # Create brown bars
# plt.bar(r, bars1, color='#7f6d5f', edgecolor='white', width=barWidth)
# # Create green bars (middle), on top of the firs ones
# plt.bar(r, bars2, bottom=bars1, color='#557f2d', edgecolor='white', width=barWidth)
# # Create green bars (top)
# plt.bar(r, bars3, bottom=bars, color='#2d7f5e', edgecolor='white', width=barWidth)

# # Custom X axis
# plt.xticks(r, names, fontweight='bold')
# plt.xlabel("group")

plt.figure(figsize = (10,6))
plt.subplot(111)
sns.boxplot(x = 'Target', y = 'overcrowding', data = train_df)
plt.title('Person per room')
plt.xlabel('')
plt.ylabel('')
plt.xticks(np.arange(0,4), ['extreme','moderate','vulnerable','non-vulnerable'])
plt.show()
temp_train = train_df
temp_test = test_df
train_df = temp_train
test_df = temp_test
def feature_engineer(x):

    x['refrig'] = x['refrig'].astype(int)
    x['computer'] = x['computer'].astype(int)
    x['television'] = x['television'].astype(int)
    x['mobilephone'] = x['mobilephone'].astype(int)
    x['v18q'] = x['v18q'].astype(int)
    x['epared1'] = x['epared1'].astype(int)
    x['epared2'] = x['epared2'].astype(int)
    x['epared3'] = x['epared3'].astype(int)
    x['etecho1'] = x['etecho1'].astype(int)
    x['etecho2'] = x['etecho2'].astype(int)
    x['etecho3'] = x['etecho3'].astype(int)
    x['eviv1'] = x['eviv1'].astype(int)
    x['eviv2'] = x['eviv2'].astype(int)
    x['eviv3'] = x['eviv3'].astype(int)
    x['abastaguadentro'] = x['abastaguadentro'].astype(int)
    x['abastaguafuera'] = x['abastaguafuera'].astype(int)
    x['abastaguano'] = x['abastaguano'].astype(int)
    x['abastaguano'] = x['abastaguano'].astype(int)
    x[['instlevel1', 'instlevel2', 'instlevel3', 'instlevel4', 'instlevel5', 'instlevel6', 'instlevel7', 'instlevel8', 'instlevel9']] = x[['instlevel1', 'instlevel2', 'instlevel3', 'instlevel4', 'instlevel5', 'instlevel6', 'instlevel7', 'instlevel8', 'instlevel9']].apply(pd.to_numeric)

    x['appliances'] = (x['refrig'] + x['computer'] + x['television'])

    x['rent_by_hhsize'] = x['v2a1'] / x['hhsize'] # rent by household size
    x['rent_by_people'] = x['v2a1'] / x['r4t3'] # rent by people in household
    x['rent_by_rooms'] = x['v2a1'] / x['rooms'] # rent by number of rooms
    x['rent_by_living'] = x['v2a1'] / x['tamviv'] # rent by number of persons living in the household
    x['rent_by_minor'] = x['v2a1'] / x['hogar_nin']
    x['rent_by_adult'] = x['v2a1'] / x['hogar_adul']
    x['rent_by_dep'] = x['v2a1'] / x['dependency']
    x['rent_by_head_educ'] = x['v2a1'] / (x['edjefe'] + x['edjefa'])
    x['rent_by_educ'] = x['v2a1'] / x['meaneduc']
    x['rent_by_numPhone'] = x['v2a1'] / x['qmobilephone']
    x['rent_by_gadgets'] = x['v2a1'] / (x['computer'] + x['mobilephone'] + x['v18q'])
    x['rent_by_num_gadgets'] = x['v2a1'] / (x['v18q1'] + x['qmobilephone'])
    x['rent_by_appliances'] = x['v2a1'] / x['appliances']

    x['tablet_density'] = x['v18q1'] / x['r4t3']
    x['phone_density'] = x['qmobilephone'] / x['r4t3']

    x['wall_qual'] = x['epared3'] - x['epared1']
    x['roof_qual'] = x['etecho3'] - x['etecho1']
    x['floor_qual'] = x['eviv3'] - x['eviv1']
    x['water_qual'] = x['abastaguadentro'] - x['abastaguano']

    x['house_qual'] = x['wall_qual'] + x['roof_qual'] + x['floor_qual']

    x['person_per_room'] = x['hhsize'] / x['rooms']
    x['person_per_appliances'] = x['hhsize'] / x['appliances']

    x['educ_qual'] = (1 * x['instlevel1']) + (2 * x['instlevel2']) + (3 * x['instlevel3']) + (4 * x['instlevel4']) + (5 * x['instlevel5']) + (6 * x['instlevel6']) + ( 7 * x['instlevel7']) + (8 * x['instlevel8']) + (9 * x['instlevel9'])

    def reverse_label_encoding(row, df):
        for c in df.columns:
            if row[c] == 1:
                return int(c[-1])

    def rate_sanitary(row, df):
        c = df.columns.tolist()[0]

        if row[c] == 'sanitario2':
            return 3
        elif row[c] == 'sanitario3':
            return 2
        elif row[c] == 'sanitario5':
            return 1
        else:
            return 0

    def rate_cooking(row, df):
        c = df.columns.tolist()[0]

        if row[c] == 'energcocinar2':
            return 3
        elif row[c] == 'energcocinar3':
            return 2
        elif row[c] == 'energcocinar4':
            return 1
        else:
            return 0

    def rate_rubbish(row, df):
        c = df.columns.tolist()[0]

        if row[c] == 'elimbasu1':
            return 1
        elif row[c] == 'elimbasu2':
            return 2
        else:
            return 0

    x['sanitary'] = x.apply(lambda q: reverse_label_encoding(q, x[['sanitario1', 'sanitario2', 'sanitario3', 'sanitario5', 'sanitario6']]), axis=1)
    x['cooking'] =  x.apply(lambda q: reverse_label_encoding(q, x[['energcocinar1', 'energcocinar2', 'energcocinar3', 'energcocinar4']]), axis=1)
    x['rubbish'] = x.apply(lambda q: reverse_label_encoding(q, x[['elimbasu1', 'elimbasu2', 'elimbasu3', 'elimbasu4', 'elimbasu5', 'elimbasu6']]), axis=1)
    x['region'] = x.apply(lambda q: reverse_label_encoding(q, x[['lugar1', 'lugar2', 'lugar3', 'lugar4', 'lugar5', 'lugar6']]), axis=1)

    x['sanitary_i'] = x.apply(lambda q: rate_sanitary(q, x[['sanitary']]), axis = 1)
    x['cooking_i'] = x.apply(lambda q: rate_cooking(q, x[['cooking']]), axis = 1)
    x['rubbish_i'] = x.apply(lambda q: rate_rubbish(q, x[['rubbish']]), axis = 1)

    x['zone'] = x['area1'] - x['area2']

    x.replace([np.inf, -np.inf], 0, inplace = True)
    x.fillna(0, inplace = True)
feature_engineer(train_df)
feature_engineer(test_df)
# def agg_features(y):
#     agg_feat = ['hacdor', 'v18q1', 'dis', 'r4h3', 'r4m3', 'age', 'hogar_nin', 'hogar_adul', 'hogar_total', 'dependency',
#                 'appliances', 'phone_density', 'tablet_density', 'house_qual', 'person_per_appliances', 'educ_qual'
#                ]
#     # https://www.kaggle.com/gaxxxx/exploratory-data-analysis-lightgbm
#     for group in ['idhogar', 'zone', 'region']:
#         for feat in agg_feat:
#             for agg_m in ['mean','sum']:
#                 id_agg = y[feat].groupby(y[group]).agg(agg_m).reset_index()[[feat, group]]
# #                 id_agg = y[feat].groupby(y[group]).agg(agg_m).reset_index()
#                 new_col = feat + '_' + agg_m + '_' + group
#                 id_agg.rename(columns = {feat : new_col} , inplace = True)
#                 y = y.merge(id_agg, how = 'left', on = group)

#     drop_ = ['sanitary', 'cooking', 'rubbish', 'instlevel1', 'instlevel2', 'instlevel3', 'instlevel4', 'instlevel5', 'instlevel6', 'instlevel7', 'instlevel8', 'instlevel9',
#             'sanitario1', 'sanitario2', 'sanitario3', 'sanitario5', 'sanitario6', 'elimbasu1', 'elimbasu2', 'elimbasu3', 'elimbasu4', 'elimbasu5', 'elimbasu6',
#             'lugar1', 'lugar2', 'lugar3', 'lugar4', 'lugar5', 'lugar6']

#     y.drop((drop_), inplace = True, axis = 1)
#     y.replace([np.inf, -np.inf], 0, inplace = True)
#     y.fillna(0, inplace = True)
#     return y
def agg_features(y):
    mean_list = ['rez_esc', 'dis', 'male', 'female', 'estadocivil1', 'estadocivil2', 'estadocivil3', 'estadocivil4', 'estadocivil5', 'estadocivil6', 'estadocivil7', 'parentesco2',
             'parentesco3', 'parentesco4', 'parentesco5', 'parentesco6', 'parentesco7', 'parentesco8', 'parentesco9', 'parentesco10', 'parentesco11', 'parentesco12']
    o_list = ['escolari', 'age', 'escolari_age']

    for group in ['idhogar', 'zone', 'region']:
        for feat in mean_list:
            for agg_m in ['mean']:
                id_agg = y[feat].groupby(y[group]).agg(agg_m)#.reset_index()[[feat, group]]
#                 id_agg = y[feat].groupby(y[group]).agg(agg_m).reset_index()
                new_col = feat + '_' + agg_m + '_' + group
                #id_agg.rename(columns = {feat : new_col} , inplace = True)
                #y = y.merge(id_agg, how = 'left', on = group)
                y[new_col] = id_agg

    for item in o_list:
        for agg_m in ['mean','std','min','max','sum']:
            id_agg = y[feat].groupby(y[group]).agg(agg_m)#.reset_index()[[feat, group]]
#                 id_agg = y[feat].groupby(y[group]).agg(agg_m).reset_index()
            new_col = feat + '_' + agg_m + '_' + group
                #id_agg.rename(columns = {feat : new_col} , inplace = True)
                #y = y.merge(id_agg, how = 'left', on = group)
            y[new_col] = id_agg

    return y
train_df = agg_features(train_df)
test_df = agg_features(test_df)
train_df.fillna(value=0, inplace=True)
train_df = train_df.loc[train_df['parentesco1'] == 1]
y = train_df[['Target']]
x = train_df.drop(['Target','Id','idhogar'], axis = 1)

from sklearn.ensemble import RandomForestClassifier
import xgboost as xgb
clf = RandomForestClassifier()
clf.fit(x, y)

imp = clf.feature_importances_
name = np.array(x.columns.values.tolist())

df_imp = pd.DataFrame({'feature':name, 'importance':imp})
df_imp = df_imp.sort_values(by='importance', ascending=False)
plt.figure(figsize=(8,20))
sns.barplot(df_imp.loc[(df_imp['importance'] > 0.01),'importance'], y = df_imp.loc[(df_imp['importance'] > 0.01),'feature'])
plt.title('Important features')
plt.show()
important_cols = df_imp['feature']
x_ = x[important_cols]
# plt.figure(figsize = (20,16))
# sns.heatmap(x_.corr(), cmap='YlOrRd')
# plt.show()
# x_ = x[['meaneduc', 'dependency', 'person_per_room', 'qmobilephone', 'overcrowding', 'hogar_nin', 'age', 'r4t2', 'rooms', 'cielorazo', 'r4h3', 'r4h2', 'r4m3', 'v2a1', 'rent_by_hhsize', 'r4t1', 'escolari', 'v18q', 'r4m1', 'bedrooms', 'edjefe', 'eviv3', 'epared3', 'hogar_adul', 'etecho3', 'r4m2', 'tamviv']]
from sklearn.preprocessing import StandardScaler
# scaler = StandardScaler()
# x_ = scaler.fit_transform(x_)
from sklearn.model_selection import train_test_split

features = [c for c in x_.columns if c not in ['Target']]
target = train_df[['Target']]
from imblearn.combine import SMOTETomek
smote_tomek = SMOTETomek(random_state=0)
X_resampled, y_resampled = smote_tomek.fit_sample(x_, target)
sns.countplot(y_resampled)
plt.show()
X_train, X_valid, y_train, y_valid = train_test_split(X_resampled,  y_resampled, test_size=0.1, random_state=1)
print("foo")
import xgboost as xgb
model = xgb.XGBClassifier( objective='multiclass', n_jobs=4, n_estimators=5000, class_weight='balanced', learning_rate=0.1, scale_pos_weight = 1).fit(X_resampled, y_resampled.ravel())
y_trainpred_xg = model.predict(X_train)
y_testpred_xg = model.predict(X_valid)
def evaluate(y_train, y_valid, y_trainpred, y_testpred):
    from sklearn.metrics import mean_absolute_error
    from sklearn.metrics import accuracy_score
    from sklearn.metrics import confusion_matrix
    from sklearn.metrics import f1_score

    print("MAE :: ", mean_absolute_error(y_train, y_trainpred))
    print("Train Accuracy :: ", accuracy_score(y_train, y_trainpred)*100)
    print("Test Accuracy  :: ", accuracy_score(y_valid, y_testpred) * 100)
    print("Confusion matrix :: \n", confusion_matrix(y_valid, y_testpred))
evaluate(y_train, y_valid, y_trainpred_xg, y_testpred_xg)
predict = model.predict(test_df[features].values)
predict.shape
submission = pd.DataFrame()
submission['Id'] = test_df['Id']
submission['Target'] = predict
submission.to_csv('submissions.csv', index=False)


"""
너무 복잡하게 푼 것 같다.
cnt를 처음에는 [0] * len(bridge_length)로 선언하여 풀려고 했는데,
index 확인이 어려워서 dependency가 아예 동일하게 q와 cnt를 둘다 deque()로 통일시켜 풀었다.
"""

from collections import deque

def solution(bridge_length, weight, truck_weights):
    answer = 0
    q = deque() # 다리를 건너는 트럭 queue
    cnt = deque() # 다리 위에서 몇 초나 있었는지 확인하는 queue - dependency가 q와 동일함
    truck_weights = deque(truck_weights) # 대기트럭 queue
    while True:
        if len(truck_weights) == 0 and len(q) == 0:
            break
        for i in range(len(q)):
            cnt[i] += 1
        if len(q) != 0:
            if cnt[0] == bridge_length:
                q.popleft()
                cnt.popleft()
        if len(truck_weights) != 0:
            if truck_weights[0] + sum(q) <= weight:
                q.append(truck_weights.popleft())
                cnt.append(0)
        answer += 1
    return answer

2-다리를지나는트럭.py


def read_and_process(data_path: str):
    """
    Create list of input integers
    """

    f = open(data_path, "r")
    out = None
    for x in f:
        out = [int(c) for c in x.strip().split()]
    return out

def get_metadata_sum(tree: list, metadata_sum: int):
    """
    Calculate the sum of metadata
    """
    nodes = tree.pop(0)
    meta = tree.pop(0)
    for i in range(nodes):
        tree, metadata_sum = get_metadata_sum(tree, metadata_sum)
    for i in range(meta):
        metadata_sum += tree.pop(0)
    return tree, metadata_sum

def part1(data_path="input"):
    """
    Calculate the sum of metadata and process data
    """
    tree = read_and_process(data_path)

    return get_metadata_sum(tree, 0)[1]

def get_child_sum(tree: list):
    """
    Calculate the sum of metadata of children
    """
    nodes = tree.pop(0)
    metas = tree.pop(0)
    child_metas = []
    out = 0
    for i in range(nodes):
        tree, child_meta = get_child_sum(tree)
        child_metas.append(child_meta)
    for i in range(metas):
        val = tree.pop(0)
        if nodes == 0:
            out += val
        elif val <= nodes:
            out += child_metas[val - 1]

    return tree, out

def part2(data_path="input"):
    """
    Calculate the sum of metadata and process data
    """
    tree = read_and_process(data_path)

    return get_child_sum(tree)[1]

if __name__ == "__main__":

    print(f"Sum of metadata is {part1()}")
    print(f"Sum of child metadata is {part2()}")