#Uses python3

import sys
from collections import deque

def number_of_components(adj):

    def walk(node):
        component = set([node])
        queue = deque([node])
        while queue:
            cur = queue.pop()
            for node in adj[cur]:
                if node not in component:
                    queue.append(node)
                    component.add(node)
        return component

    result = 0
    visited = set()

    for node in range(len(adj)):
        if node not in visited:
            component = walk(node)
            visited = visited.union(component)
            result += 1

    return result

if __name__ == '__main__':
    input = sys.stdin.read()
    data = list(map(int, input.split()))
    n, m = data[0:2]
    data = data[2:]
    edges = list(zip(data[0:(2 * m):2], data[1:(2 * m):2]))
    adj = [[] for _ in range(n)]
    for (a, b) in edges:
        adj[a - 1].append(b - 1)
        adj[b - 1].append(a - 1)
    print(number_of_components(adj))

Data Structures and Algorithms/Algorithm on Graph/week1/09_graph_decomposition_starter_files_1/connected_components/connected_components.py


import cv2
import face_recognition as fr
import numpy as np
import os
import image_to_numpy as im_np

#-----------Based on ageitgey/face_recognition demo-----------#

def main():

    #----------Load Known values----------#
    known_encodings = [] #arrays for encodings and associated names
    known_names = []

    for filename in os.listdir("./images"):  #loop through images folder for known names and images
        if filename.startswith("."): #ignore .ds_store and other hiddens
            continue
        image_file = fr.load_image_file("./images/" + filename)
        image_encodings = fr.face_encodings(image_file)[0]
        known_encodings.append(image_encodings)
        name = filename.split(".")[0] #better hope there are no periods in name
        known_names.append(name)

    face_locations, face_encodings, face_names = [], [], [] #arrays for processing sent frames
    process_frame = 0 #counter to allow processing every n frames
    n = 1

    #----------Process Captured Frames----------#
    for fname in os.listdir("./captured_frames"): #loop through frames stored in captured_frames folder
        if fname.startswith("."): #ignore .ds_store and other hiddens
            continue
        frame = im_np.load_image_file("./captured_frames/" + fname)
        RESIZE = 2
        #resize for faster processing
        small_frame = cv2.resize(frame, (0,0), fx=1/RESIZE, fy=1/RESIZE)
        rgb_small = small_frame[:, :, ::-1]

        #process every few frames
        if process_frame % n == 0:
            face_locations = fr.face_locations(rgb_small)
            face_encodings = fr.face_encodings(rgb_small, face_locations)

            #check each set of encodings for a match
            for face_encoding in face_encodings:
                matches = fr.compare_faces(known_encodings, face_encoding)
                name = "Unknown"

                #get "closest" match
                face_distances = fr.face_distance(known_encodings, face_encoding)
                best_match_index = np.argmin(face_distances)
                if matches[best_match_index]:
                    name = known_names[best_match_index]

                #append name to list
                if not (name in face_names):
                    face_names.append(name)
        process_frame += 1
    tot_names = len(face_names)
    if tot_names > 0: #append names to response string
        response = "I have found "
        c = 1
        for name in face_names:
            response += name
            if c < tot_names:
                response += " and "
            c += 1
        print(response)
    else:
        print("I have found no one")

main()


import pytest
import connexion
from s3_io.s3io_api import create_app
flask_app = create_app()
#flask_app.add_api('api/s3io-api.yaml', arguments={'title': 'Swarm s3'})

@pytest.fixture(scope='module')
def client():
    with flask_app.app.test_client() as c:
        yield c

def test_health(client):
    response = client.get('/v1.0/health')
    assert response.status_code == 200


import sys
from collections import deque

input = sys.stdin.readline

M, N = map(int, input().split())
maze = []
for i in range(N):
    maze.append(list(map(int, list(input().rstrip()))))

# 상 하 좌 우
mx = [-1, 1, 0, 0]
my = [0, 0, -1, 1]

q = deque()
q.append((0, 0))
INF = float('inf')
# 가로 : M, 세로 : N
dist = [[INF] * (M) for _ in range(N)]
dist[0][0] = 0
while q:
    x, y = q.popleft()
    for i in range(4):
        nx = x+mx[i]
        ny = y+my[i]
        if 0 <= nx < N and 0 <= ny < M:
            if dist[nx][ny] == INF:
                if maze[nx][ny] == 0:
                    dist[nx][ny] = dist[x][y]
                    q.appendleft((nx, ny))
                else:
                    dist[nx][ny] = dist[x][y] + 1
                    q.append((nx, ny))

print(dist[N-1][M-1])


"""
@author: magician
@file:   flags_threadpool.py
@date:   2021/12/14
"""
from concurrent import futures

from fluent_python.future.flags import get_flags, show, save_flag, main

POP20_CC = ('CN IN US ID BR PK NG BD RU JP '
            'MX PH VN ET EG DE IR TR CD FR').split()
BASE_URL = 'http://flupy.org/data/flags'
MAX_WORKERS = 20

def download_one(cc):
    """

    @param cc:
    @return:
    """
    image = get_flags(cc)
    show(cc)
    save_flag(image, cc.lower() + '.gif')

    return cc

def download_many(cc_list):
    """

    @param cc_list:
    @return:
    """
    workers = min(MAX_WORKERS, len(cc_list))
    with futures.ThreadPoolExecutor(workers) as executor:
        res = executor.map(download_one, sorted(cc_list))

    return len(list(res))

if __name__ == '__main__':
    main(download_many)


from contextlib import contextmanager

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from planner.model import Base

class TransactionFactory(object):
    def __init__(self, path=None, bind=None, expire_on_commit=False,
                 create_all=False):
        if (not bind and not path) or (bind and path):
            raise ValueError
        if not bind:
            bind = create_engine(path)
        if create_all:
            Base.metadata.create_all(bind)
        self.sessionmaker = sessionmaker(expire_on_commit=expire_on_commit,
                                         bind=bind)

    @contextmanager
    def __call__(self, rollback=False):
        try:
            session = self.sessionmaker()
            yield session
            if rollback:
                session.rollback()
            session.commit()
        finally:
            session.close()


"""
Texas has CSV files containing county-level and statewide results for all offices
for all years back to 2000. All of the files are pre-processed and available on Github at
https://github.com/openelections/openelections-data-tx.
"""
from future import standard_library
standard_library.install_aliases()
from builtins import str
from os.path import join
import json
import datetime
import urllib.parse

from openelex import PROJECT_ROOT
from openelex.base.datasource import BaseDatasource
from openelex.lib import build_raw_github_url

class Datasource(BaseDatasource):

    # PUBLIC INTERFACE
    def mappings(self, year=None):
        """Return array of dicts containing source url and
        standardized filename for raw results file, along
        with other pieces of metadata
        """
        mappings = []
        for yr, elecs in list(self.elections(year).items()):
            mappings.extend(self._build_metadata(yr, elecs))
        return mappings

    def target_urls(self, year=None):
        "Get list of source data urls, optionally filtered by year"
        return [item['raw_url'] for item in self.mappings(year)]

    def filename_url_pairs(self, year=None):
        return [(mapping['generated_filename'], self._url_for_fetch(mapping))
                for mapping in self.mappings(year)]

    def _url_for_fetch(self, mapping):
        try:
            return mapping['pre_processed_url']
        except KeyError:
            return mapping['raw_url']

    def mappings_for_url(self, url):
        return [mapping for mapping in self.mappings() if mapping['raw_url'] == url]

    # PRIVATE METHODS

    def _build_metadata(self, year, elections):
        meta = []
        year_int = int(year)
        for election in elections:
            # each election has a statewide and a county pre-processed file.
            statewide_filename = self._generate_statewide_filename(election)
            meta.append({
                "generated_filename": statewide_filename,
                "raw_url": election['direct_links'][0],
                "pre_processed_url": build_raw_github_url(self.state, str(year), statewide_filename),
                "ocd_id": 'ocd-division/country:us/state:tx',
                "name": 'Texas',
                "election": election['slug']
            })

            county_filename = self._generate_county_filename(election)
            meta.append({
                "generated_filename": county_filename,
                "raw_url": election['direct_links'][0],
                "pre_processed_url": build_raw_github_url(self.state, str(year), county_filename),
                "ocd_id": 'ocd-division/country:us/state:tx',
                "name": 'Texas',
                "election": election['slug']
            })

        return meta

    def _generate_statewide_filename(self, election):
        bits = [
            election['start_date'].replace('-',''),
            self.state,
        ]
        if election['special'] == True:
            bits.extend(['special'])
        bits.extend(
            election['race_type'].split('-')
        )
        filename = "__".join(bits) + '.csv'
        return filename

    def _generate_county_filename(self, election):
        bits = [
            election['start_date'].replace('-',''),
            self.state,
        ]
        if election['special'] == True:
            bits.extend(['special'])
        bits.extend(
            election['race_type'].split('-')
        )
        bits.extend(['county'])
        filename = "__".join(bits) + '.csv'
        return filename

    def _jurisdictions(self):
        """Texas counties"""
        m = self.jurisdiction_mappings()
        mappings = [x for x in m if x['county'] != ""]
        return mappings

    def _url_for_fetch(self, mapping):
        if mapping['pre_processed_url']:
            return mapping['pre_processed_url']
        else:
            return mapping['raw_url']


import os
import numpy as np
import pyqtgraph as pg
from PyQt5.QtCore import QThread, QObject, pyqtSignal, QRunnable, QThreadPool
from PyQt5.QtGui import QImage
from PyQt5.QtWidgets import QAction, QSpinBox, QLabel, QDoubleSpinBox, QMenu, QTableWidget, QPushButton, QWidget, \
    QVBoxLayout, QHBoxLayout
from mne.viz import plot_topomap

from scipy.signal import savgol_filter
from scipy.fft import fft, fftfreq, rfft
from PyQt5 import QtCore, QtGui, QtWidgets
from datetime import datetime
import mne
from scipy.fftpack import rfftfreq

from pynfb.generators import ch_names32
from pynfb.generators import ch_names27
from pynfb.inlets.montage import Montage

from pynfb.postprocessing.utils import runica
from pynfb.widgets import edf_saver
from vispy import scene

class WorkerSignals(QObject):
    finished = pyqtSignal()
    error = pyqtSignal(tuple)
    result = pyqtSignal(int, object, object)
    progress = pyqtSignal(int)

class DPHandler(QRunnable):
    def __init__(self, y, c, scaler, shift):
        super().__init__()
        self.y = y
        self.c = c
        self.scaler = scaler
        self.shift = shift
        self.signals = WorkerSignals()

    def run(self):
        signal_buffer = (self.y - self.c) * self.scaler + 50 * self.shift
        spectral_buffer = rfft(self.y)
        self.signals.result.emit(self.shift, signal_buffer, spectral_buffer)

class CrossButtonsWidget(QtWidgets.QWidget):
    names = ['left', 'right', 'up', 'down']
    symbols = ['<', '>', '^', 'v']
    positions = [(1, 0), (1, 2), (0, 1), (2, 1)]

    def __init__(self, parent=None):
        super(CrossButtonsWidget, self).__init__(parent)
        buttons = dict([(name, QtWidgets.QPushButton(symbol)) for name, symbol in zip(self.names, self.symbols)])
        layout = QtWidgets.QGridLayout(self)
        self.clicked_dict = {}
        for name, pos in zip(self.names, self.positions):
            buttons[name].setAutoRepeat(True)
            buttons[name].setAutoRepeatDelay(100)
            layout.addWidget(buttons[name], *pos)
            self.clicked_dict[name] = buttons[name].clicked

class LSLPlotDataItem(pg.PlotDataItem):
    def getData(self):
        x, y = super(LSLPlotDataItem, self).getData()
        if self.opts['fftMode']:
            return x, y / (max(y) - min(y) + 1e-20) * 0.75
        return x, y

class PopupWidget(QPushButton):
    def __init__(self, label, elements, checklist, parent=None):
        super(PopupWidget, self).__init__(parent)
        self.parent = parent
        self.setText(label)
        self.menu = QMenu(self)
        self.checklist = checklist
        for i in range(0, len(elements)):
            Action = QAction(elements[i], self.menu)
            Action.triggered.connect(lambda chk, item=i: self.open_spectral(item))
            self.menu.addAction(Action)

    def mouseReleaseEvent(self, event):
        self.menu.exec_(event.globalPos())

    def open_spectral(self, index):
        self.parent.open_spectral(index)

    def getChecklist(self):
        mlen = len(self.menu.actions())
        checklist = np.zeros(mlen)
        for i in range(0, mlen):
            checklist[i] = self.menu.actions()[i].isChecked()
        return checklist

class SpectralWindow(QWidget):
    def __init__(self, name, n, freq):
        super().__init__()
        layout = QHBoxLayout()
        self.setWindowTitle(name)

        color = np.ones((n, 4), dtype=np.float32)
        color[:, 0] = np.linspace(0, 1, n)
        color[:, 1] = color[::-1, 0]
        canvas = scene.SceneCanvas(keys='interactive', show=True)
        grid = canvas.central_widget.add_grid(spacing=0)
        self.viewbox = grid.add_view(row=0, col=1, camera='panzoom')
        x_axis = scene.AxisWidget(orientation='bottom')
        x_axis.stretch = (1, 0.1)
        grid.add_widget(x_axis, row=1, col=1)
        x_axis.link_view(self.viewbox)
        y_axis = scene.AxisWidget(orientation='left')
        y_axis.stretch = (0.1, 1)
        grid.add_widget(y_axis, row=0, col=0)
        y_axis.link_view(self.viewbox)

        self.pos = np.zeros((n, 2), dtype=np.float32)
        self.pos[:, 0] = rfftfreq(n, 1/freq)
        self.line = scene.Line(self.pos, color, parent=self.viewbox.scene)

        self.viewbox.camera.set_range()
        layout.addWidget(canvas.native)
        self.setLayout(layout)

    def set_data(self, y):
        self.pos[:, 1] = y
        self.line.set_data(pos=self.pos)

class RhythmsWindow(QWidget):
    def __init__(self, n, freq):
        super().__init__()
        layout = QHBoxLayout()
        self.setWindowTitle("Rhythms indicators")
        self.n = n
        self.freq = freq

        self.bar1 = pg.BarGraphItem(x=[1], height=1, width=0.2, brush='g')
        self.bar2 = pg.BarGraphItem(x=[2], height=2, width=0.2, brush='w')
        self.bar3 = pg.BarGraphItem(x=[3], height=3, width=0.2, brush='y')
        self.plot = pg.PlotWidget()
        self.plot.addItem(self.bar1)
        self.plot.addItem(self.bar2)
        self.plot.addItem(self.bar3)
        layout.addWidget(self.plot)
        self.setLayout(layout)

        self.update_counter = 1001

    def get_indicator(self, data, num, f, k2):
        x = rfftfreq(self.n, 1/self.freq)
        buffer = rfft(data[:, num])
        sy0 = 0
        sy1 = 0
        sy2 = 0
        sx = 0
        B = []

        for i in range(0, len(x), 1):
            if f[0] < x[i] < f[3]:
                B.append(np.abs(buffer)[i])

        k1 = len(B)-1 if len(B)%2 == 0 else len(B)
        fb = savgol_filter(B, k1, k2)
        #pg.plot(fb)
        kx = len(fb)/(f[3]-f[0])
        for i in range(0, int(kx*(f[1]-f[0])), 1):
            sy0 += fb[i]
        for i in range(int(kx*(f[1]-f[0])), int(kx*(f[2]-f[0])), 1):
            sy1 += fb[i]
        for i in range(int(kx*(f[2]-f[0])), len(fb), 1):
            sy2 += fb[i]

        # print(kx)
        # print(sy1)
        # print(sy0)
        # print(sy2)

        if sy1:
            sx = 1 - (sy0/sy1+sy2/sy1)/2
        if sx < -1:
            sx = -1
        return sx

    def set_data(self, data):
        self.update_counter += 1
        if self.update_counter <= 10:
            return
        else:
            self.update_counter = 0

        ai = self.get_indicator(data[0], 19, [7, 9, 11, 13], 6) # OZ, alpha
        ti = self.get_indicator(data[1], 3, [3, 5, 7, 9], 6) # F7, theta
        di = self.get_indicator(data[2], 10, [0.4, 1.4, 2.4, 3.4], 7) # CZ, delta
        self.bar1.setOpts(height=ai)
        self.bar2.setOpts(height=ti)
        self.bar3.setOpts(height=di)

class RawViewer(QWidget):
    def __init__(self, fs, channels_labels, parent=None, overlap=False, mode=0, toolbar=None, freqbar=None, widgets=None):
        super(RawViewer, self).__init__(parent)
        n_channels = len(channels_labels)
        #print(channels_labels)
        self.n_channels = n_channels
        self.calib_values = np.zeros(self.n_channels)
        self.calib_flag = False
        self.visible_flags = np.zeros(self.n_channels)
        self.channelsanalyze_flags = np.zeros(self.n_channels)
        self.spectraldata = []
        self.tw = None
        self.sw = [QWidget]*n_channels
        for i in range(0, n_channels):
            #self.sw.append(None)
            self.visible_flags[i] = True
            self.channelsanalyze_flags[i] = False

        print("Signal painter started")

        self.channels_labels = channels_labels
        self.toolbar = toolbar
        #self.freqbar = freqbar
        self.widgets = widgets
        self.fs = fs
        self.current_pos = 0
        self.std = None
        self.raw_buffer = None
        self.filtered_buffer1 = None
        self.filtered_buffer2 = None
        self.filtered_buffer3 = None
        self.scaler = 0.0041
        self.step = 0.0001
        self.curves = []
        self.lines = []
        self.x_mesh = None
        #self.setYRange(0, n_channels)
        self.mode = mode
        self.n_samples_to_display = 0
        #self.notch_filter = NotchFilter(0, fs, n_channels)
        #self.butter_filter = ButterFilter((0.5, 45), fs, n_channels)

        print("FS: "+str(fs))
        self.pool = QThreadPool.globalInstance()

        if self.mode == 0:
            buffer_time_sec = 10
            self.n_samples = int(buffer_time_sec*fs)
            self.n_samples_to_display = self.n_samples
            self.raw_buffer = np.zeros((self.n_samples, self.n_channels))
            self.x_mesh = np.linspace(0, self.n_samples_to_display / self.fs, self.n_samples_to_display)
            #self.setXRange(0, self.x_mesh[self.n_samples_to_display - 1])
            self.create_surface()
            self.rw = RhythmsWindow(int(self.n_samples/2), self.fs)

        self.saveAction = QAction(self)
        self.saveAction.setText("Save data...")
        self.recalibAction = QAction(self)
        self.recalibAction.setText("Recalibrate")
        self.scaleupAction = QAction(self)
        self.scaleupAction.setText(" ^ ")
        self.scaledownAction = QAction(self)
        self.scaledownAction.setText(" v ")
        self.saveAction.triggered.connect(lambda: self.save_data())
        self.recalibAction.triggered.connect(lambda: self.calibrate())
        self.scaleupAction.triggered.connect(lambda: self.update_scaler(increase=True))
        self.scaledownAction.triggered.connect(lambda: self.update_scaler(increase=False))
        self.toolbar.addAction(self.saveAction)
        self.toolbar.addAction(self.recalibAction)
        self.toolbar.addAction(self.scaleupAction)
        self.toolbar.addAction(self.scaledownAction)
        self.stepSpinBox = QDoubleSpinBox()
        self.ampLabel = QLabel()
        #font = self.stepSpinBox.font()
        #font.setPointSizeF(20.25)
        #self.stepSpinBox.setFont(font)

        #self.stepSpinBox.setFloatDomain(True)
        self.stepSpinBox.setDecimals(5)
        self.stepSpinBox.setMaximum(1000)
        self.stepSpinBox.setValue(self.step)
        self.stepSpinBox.setFixedWidth(100)
        self.ampLabel.setText("Amplitude scale: x"+str(self.scaler))
        self.ampLabel.setFixedWidth(150)
        self.toolbar.addWidget(self.ampLabel)
        self.toolbar.addWidget(self.stepSpinBox)

        # self.visMenu = QMenu("menu")
        # self.visMenu.addAction(QAction('50%', self.visMenu, checkable=True))
        # self.visMenu.addAction(QAction('50%', self.visMenu, checkable=True))
        # self.visMenu.addAction(QAction('50%', self.visMenu, checkable=True))
        # self.visMenu.addAction(QAction('50%', self.visMenu, checkable=True))

        #self.vis_widget = PopupWidget('Channel visibility', channels_labels, self.visible_flags, parent=self)
        #self.toolbar.addWidget(self.vis_widget)

        self.model = QtGui.QStandardItemModel()
        self.listView = QtWidgets.QListView()
        self.alglist = QtWidgets.QListView()
        self.algmodel = QtGui.QStandardItemModel()

        if channels_labels is not None:
            for i in range(len(channels_labels)):
                item = QtGui.QStandardItem(channels_labels[i])
                item.setCheckable(True)
                #check = QtCore.Qt.Checked if checked else QtCore.Qt.Unchecked
                item.setCheckState(QtCore.Qt.Checked)

                self.model.appendRow(item)
        self.listView.setModel(self.model)
        algnames = []
        # algnames = ["[preprocessing] Butterworth filter", "[analyze] SVM"]
        for i in range(len(algnames)):
            item = QtGui.QStandardItem(algnames[i])
            item.setCheckable(True)
            item.setCheckState(QtCore.Qt.Checked)
            self.algmodel.appendRow(item)

        self.alglist.setModel(self.algmodel)

        #vbox.addWidget(self.listView)
        #vbox.addStretch(1)
        self.listView.clicked.connect(self.process_vischeck)

        self.listwidget = QWidget()
        self.listlayout = QVBoxLayout(self)
        self.listlayout.addWidget(self.listView)
        self.listlayout.addWidget(self.alglist)
        ab1 = QPushButton()
        ab1.setText("Add")
        ab2 = QPushButton()
        ab2.setText("Edit")
        ab3 = QPushButton()
        ab3.setText("Delete")
        self.listlayout.addWidget(ab1)
        self.listlayout.addWidget(ab2)
        self.listlayout.addWidget(ab3)
        self.listwidget.setLayout(self.listlayout)
        self.widgets.setWidget(self.listwidget)

        self.channelsfilter_widget = PopupWidget('Spectral plots', channels_labels, self.channelsanalyze_flags, parent=self)
        self.toolbar.addWidget(self.channelsfilter_widget)

        self.rwopenAction = QAction(self)
        self.rwopenAction.setText("Rhythms")
        self.rwopenAction.triggered.connect(lambda: self.rw.show())
        self.toolbar.addAction(self.rwopenAction)

        self.twopenAction = QAction(self)
        self.twopenAction.setText("Topography")
        self.twopenAction.triggered.connect(lambda: self.tw.showMaximized())
        self.toolbar.addAction(self.twopenAction)

        #self.setLayout(vbox)

        #self.channelsfilter_widget = PopupWidget('Channels to filter', channels_labels, self.channelsfilter_flags, parent=self)
        #self.toolbar.addWidget(self.channelsfilter_widget)

        self.show_levels_flag = False
        self.overlap = overlap
        if overlap:
            self.show_levels_flag = True
            self.show_levels()

    def create_surface(self):
        N = self.n_samples_to_display
        color = np.ones((N, 4), dtype=np.float32)
        color[:, 0] = np.linspace(0, 1, N)
        color[:, 1] = color[::-1, 0]
        canvas = scene.SceneCanvas(keys='interactive', show=True)
        grid = canvas.central_widget.add_grid(spacing=0)
        self.viewbox = grid.add_view(row=0, col=1, camera='panzoom')

        x_axis = scene.AxisWidget(orientation='bottom')
        x_axis.stretch = (1, 0.1)
        grid.add_widget(x_axis, row=1, col=1)
        x_axis.link_view(self.viewbox)
        y_axis = scene.AxisWidget(orientation='left')
        y_axis.stretch = (0.1, 1)
        grid.add_widget(y_axis, row=0, col=0)
        y_axis.link_view(self.viewbox)
        self.setLayout(QVBoxLayout())
        self.layout().addWidget(canvas.native)
        for i in range(0, self.n_channels):
            pos = np.zeros((N, 2), dtype=np.float32)
            pos[:, 0] = self.x_mesh[:self.n_samples_to_display]
            if i:
                pos[:, 1] = pos[:, 1] + 50*i
            c = scene.Line(pos, color, parent=self.viewbox.scene)
            self.curves.append(pos)
            self.lines.append(c)
        self.viewbox.camera.set_range()

    def open_spectral(self, index):
        if not self.channelsanalyze_flags[index]:
            self.sw[index] = SpectralWindow(self.channels_labels[index], int(self.n_samples / 2), self.fs)
            self.channelsanalyze_flags[index] = True
        self.sw[index].set_data(np.abs(self.spectraldata)[:int(self.n_samples/2)])
        self.sw[index].show()

    def process_vischeck(self, index):
        self.visible_flags[index.row()] = self.listView.model().itemData(index)[10]
        if not self.visible_flags[index.row()]:
            self.curves[index.row()][:, 1] = np.ones(self.n_samples_to_display) + 50 * index.row()

        self.update_data()

    def update_std(self, chunk):
        if self.std is None:
            self.std = np.std(chunk)
        else:
            self.std = 0.5 * np.std(chunk) + 0.5 * self.std

    def set_chunk(self, signal, chunk):
        n_samples = len(chunk)

        if self.mode == 1:
            self.raw_buffer = np.zeros((n_samples, self.n_channels))
            self.n_samples_to_display += n_samples
            self.x_mesh = np.linspace(0, self.n_samples_to_display / self.fs, self.n_samples_to_display)
            self.n_samples = n_samples
            self.create_surface()
            # for i in range(0, self.n_channels):
            #     self.sw.append(SpectralWindow(self.channels_labels[i], int(self.n_samples/2), self.fs))
            self.rw = RhythmsWindow(int(self.n_samples/2), self.fs)

        self.raw_buffer[:-n_samples] = self.raw_buffer[n_samples:]
        self.raw_buffer[-n_samples:] = chunk
        if not self.calib_flag:
            self.calibrate()

        if signal is not None:
            # runica(self.raw_buffer, self.fs, self.channels_labels)
            #montage = Montage(ch_names32)
            # ch_names32 = ['EEG FP1', 'EEG FPZ', 'EEG FP2', 'EEG  F7', 'EEG  F3', 'EEG  FZ', 'EEG  F4', 'EEG  F8', 'EEG  T3',
            #               'EEG  C3', 'EEG  CZ', 'EEG C4', 'EEG T4', 'EEG  T5', 'EEG P3', 'EEG PZ', 'EEG P4', 'EEG T6', 'EEG O1',
            #               'EEG OZ', 'EEG O2', 'EEG PBH', 'EEG NOS', 'EEG SHL', 'EEG SHH', 'EEG A1', 'EEG A2', '', '', '', '', '']
            #montage = Montage(ch_names32)
            #print(self.channels_labels)
            #a = ch_names27.conjugate
            #print(np.shape(self.channels_labels))
            #print(np.shape(ch_names27))
            #b = self.channels_labels.conjugate

            #print(montage.get_names())
            # spatial, topo = runica(np.random.normal(size=(100000, 32)), 1000, montage.get_names(), mode='csp')
            if False:
                self.tw = runica(self.raw_buffer, self.fs, self.channels_labels, mode='csp')
                topocanvas = self.tw.table.topographies_items[0].topo
                width, height = topocanvas.get_width_height()
                print("canvas pixels size "+str(width)+"x"+str(height))
                for x in range(0, width):
                    for y in range(0, height):
                        pixel = QImage(topocanvas.buffer_rgba(), width, height, QImage.Format_ARGB32).pixel(x, y)
                        #print(QtGui.QColor(pixel).getRgb()[:-1])
                #print("topography data: ", self.tw.topographies)
                # plot_topomap(spatial, montage.get_pos())
                # plot_topomap(topo, montage.get_pos())

            self.filtered_buffer1 = self.raw_buffer.copy()
            self.filtered_buffer2 = self.raw_buffer.copy()
            self.filtered_buffer3 = self.raw_buffer.copy()
            signal.fit_model(self.filtered_buffer1, [8, 13], self.channels_labels)
            self.filtered_buffer1 = signal.apply(self.filtered_buffer1)

            signal.fit_model(self.filtered_buffer2, [4, 8], self.channels_labels)
            self.filtered_buffer2 = signal.apply(self.filtered_buffer2)

            signal.fit_model(self.filtered_buffer3, [1, 4], self.channels_labels)
            self.filtered_buffer3 = signal.apply(self.filtered_buffer3)
        self.update_data()

    def set_data(self, i, data, spectraldata):
        if self.visible_flags[i]:
            self.curves[i][:, 1] = data
            self.lines[i].set_data(pos=self.curves[i])
        self.spectraldata = spectraldata
        if self.mode == 1 and self.channelsanalyze_flags[i]:
            self.sw[i].set_data(np.abs(spectraldata)[:int(self.n_samples/2)])

    def update_data(self):
        for i in range(0, self.n_channels, 1):
            runnable = DPHandler(self.filtered_buffer3[:, i], self.calib_values[i], self.scaler, i)
            runnable.signals.result.connect(self.set_data)
            self.pool.start(runnable)
        self.rw.set_data([self.filtered_buffer1, self.filtered_buffer2, self.filtered_buffer3])
        self.ampLabel.setText("Amplitude scale: x" + str(self.scaler))

    def update_scaler(self, increase=False):
        self.step = self.stepSpinBox.value()
        self.scaler += self.step if increase else -self.step
        if self.scaler < 0:
            self.scaler = 0
        self.update_data()
        self.update_levels()

    def update_n_samples_to_display(self, increase=False):
        step = int(self.fs * 0.5)
        self.n_samples_to_display += step if increase else -step
        if self.n_samples_to_display < 10:
            self.n_samples_to_display = 10
        elif self.n_samples_to_display > self.n_samples:
            self.n_samples_to_display = self.n_samples
        self.update_data()
        self.update_levels()

    def show_levels(self):
        self.levels = {'zero': [], 'p1': [], 'm1': []}
        for i in (range(self.n_channels) if not self.overlap else [0]):
            for level, val in zip(['zero', 'p1', 'm1'], [0, 1, -1]):
                c = LSLPlotDataItem()
                c.setPen(pg.mkPen('g'))
                #self.addItem(c)
                if not self.overlap:
                    c.setPos(0, i + 1)
                #self.levels[level].append(c)
        self.update_levels()

    def update_levels(self):
        if self.show_levels_flag:
            for i in (range(self.n_channels) if not self.overlap else [0]):
                for level, val in zip(['zero', 'p1', 'm1'], [0, 1, -1]):
                    self.levels[level][i].setData(self.x_mesh[:self.n_samples_to_display],
                              self.x_mesh[:self.n_samples_to_display]*0 + val * self.scaler / (self.std or 1))

    def save_data(self):
        info = mne.create_info(ch_names=self.channels_labels,
                               ch_types=['misc'] * len(self.channels_labels),
                               sfreq=self.fs)
        raw_buffer_t = np.zeros((len(self.channels_labels), self.n_samples_to_display))
        for i in range(0, self.raw_buffer.shape[1]):
            raw_buffer_t[i, :] = self.raw_buffer[:, i]
        raw = mne.io.RawArray(raw_buffer_t, info)
        now = datetime.now()
        dt_string = now.strftime("%Y%m%d-%H_%M_%S")
        print("Timestamp: ", dt_string)
        edf_saver.write_mne_edf(raw, os.path.realpath(os.path.dirname(os.path.realpath(__file__)))+"\\..\\..\\saved_eeg\\"+dt_string+".edf", overwrite=True)

    def calibrate(self):
        valnum = len(self.raw_buffer)
        for i in range(0, self.n_channels):
            av = 0
            for j in range(0, valnum):
                av += self.raw_buffer[j][i]
            self.calib_values[i] = av / valnum
        self.calib_flag = True
        self.scaler = 9e-1 / abs(self.raw_buffer[-1, 0]) if self.raw_buffer[-1, 0] else 1
        self.step = self.scaler / 10
        self.stepSpinBox.setValue(self.step)
        print("Calibration data:")
        print(self.calib_values)
        #print(self.scaler)

if __name__ == '__main__':
    a = QtWidgets.QApplication([])
    plot_widget = RawViewer(250, ['ef', 'sf', 'qwr']*3)

    plot_widget.set_chunk(np.sin(np.arange(20)).reshape(20, 1).dot(np.ones((1, 9))))
    plot_widget.show()
    a.exec_()


import module_manager
module_manager.review()
import pyaudio
import numpy
import pygame
import os
import aubio
import random
from collections import deque
import time
import math
import pygame.gfxdraw
import wave

import sys
from aubio import source, onset

#adapted from the aubio demo code
def getOnsets(filename):
    win_s = 1024*8 # fft size
    hop_s = win_s // 2           # hop size
    samplerate=0
    sys.argv.append(filename)
    sys.argv.append(samplerate)
    if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
    s = source(filename, samplerate, hop_s)
    samplerate = s.samplerate
    o = onset("default", win_s, hop_s, samplerate)
    # list of onsets, in samples
    onsets = []
    # total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        if o(samples):
            onsets.append(o.get_last_s())
        total_frames += read
        if read < hop_s: break
    return onsets

class Button1(pygame.sprite.Sprite):
    def __init__(self, path, x, y, width, height):
    	super(Button1, self).__init__()
    	(self.x, self.y) = (x, y)
    	(self.width, self.height) = (width, height)
    	self.image = pygame.image.load(path)
    	self.image = self.image.convert_alpha()
    	self.rect = pygame.Rect(self.x, self.y, self.width, self.height)

class BeatCircle(pygame.sprite.Sprite):
	white=(255, 255, 255)
	def __init__(self, x, y, color, number):
		super(BeatCircle, self).__init__()
		self.x=x
		self.y=y
		self.color=color
		self.number=number
		self.radius=50
		self.border=4
		self.clock=0.1
		self.outerRadius=self.radius*4
		self.ring=self.outerRadius
		self.outerWidth=3
		self.shrinkSize=(self.outerRadius//60)-(self.radius//60)
		self.rect=pygame.Rect(self.x - self.outerRadius, self.y - self.outerRadius,
                                2 * self.outerRadius, 2 * self.outerRadius)
		self.image=pygame.Surface((2 * self.outerRadius, 2 * self.outerRadius),
                                    pygame.SRCALPHA|pygame.HWSURFACE)
		self.outline=4
		self.fontSize=50
		self.missTime=0.2
		self.missClock=None
		self.draw()

	def shrinkRing(self, sec):
		self.clock+=sec
		if self.missClock!=None: #calculating
			self.missClock-=sec #when ring should disappear
			if self.missClock<=0:
				self.kill()
		if self.ring>self.radius:
			self.ring-=self.shrinkSize
		self.draw()

	def draw(self): #draws the beats
		self.image.fill((255,255, 255,0))
		pygame.draw.circle(self.image, BeatCircle.white, (self.outerRadius, self.outerRadius), self.ring, self.outerWidth)
		radius=2*self.radius
		outline=2*self.outline
		newWindow=pygame.Surface((2*radius, 2*radius), pygame.SRCALPHA|pygame.HWSURFACE)
		pygame.draw.circle(newWindow, BeatCircle.white, (radius, radius), radius)
		pygame.draw.circle(newWindow, self.color, (radius, radius), radius-outline)
		width=radius
		height=radius
		newWindow=pygame.transform.smoothscale(newWindow, (width, height))
		start=self.outerRadius-self.radius
		self.image.blit(newWindow, (start, start))
		self.drawText() #draws the numbers
		if self.missClock!=None: #beats fade for smoother gameplay
			#pygame example https://stackoverflow.com/questions/15177568/pygame-surface-fade-in-out
			a=max(int((self.missClock/self.missTime)*255), 0)
			a=max(a, 0)
			fillSpace=(start, start, width, height)
			self.image.fill((255,255,255, a), fillSpace, pygame.BLEND_RGBA_MIN)

	def drawText(self): #draws number on beats
		font=pygame.font.SysFont("Arial", self.fontSize)
		text=font.render(str(self.number), 1, BeatCircle.white)
		position=text.get_rect()
		position.centerx=self.image.get_rect().centerx
		position.centery=self.image.get_rect().centery
		self.image.blit(text, position)

	def getPosition(self):
		return self.x, self.y

	def draining(self): #calculates when beats should disappear
		self.missClock=self.missTime

class MouseLocation(pygame.sprite.Sprite):
	def __init__(self, x, y):
		super(MouseLocation, self).__init__()
		self.radius=1
		self.x=x
		self.y=y
		self.rect=pygame.Rect(self.x-self.radius, self.y-self.radius, 2*self.radius, 2*self.radius)

class FadingText(pygame.sprite.Sprite):
	white=(255, 255, 255)
	font="Tahoma"
	def __init__(self, window, text, size, x, y, anchor="nw", color=white, font=font):
		super(FadingText, self).__init__()
		self.x=x
		self.y=y
		self.text=text
		self.anchor=anchor
		self.color=color
		self.window=window
		self.time=0
		self.font=pygame.font.SysFont(font, size)
		self.width, self.height=self.font.size(self.text)
		self.initPosition()
		self.rect=pygame.Rect(self.areaX, self.areaY, self.width, self.height)
		self.image=pygame.Surface((self.width, self.height), pygame.HWSURFACE)
		self.missTime=0.2
		self.missClock=None
		self.draw()
		self.window.blit(self.image, (self.areaX, self.areaY))

	def initPosition(self):
		if self.anchor=="nw":
			self.areaX=self.x
			self.areaY=self.y
		if self.anchor=="ne":
			self.areaX=self.x-self.width
			self.areaY=self.y
		if self.anchor=="sw":
			self.areaX=self.x
			self.areaY=self.y-self.height
		if self.anchor=="se":
			self.areaX=self.x-self.width
			self.areaY=self.y-self.height
		if self.anchor=="center":
			self.areaX=self.x-self.width//2
			self.areaY=self.y-self.height//2

	def draw(self):
		text=self.font.render(self.text, 1, self.color)
		self.image.blit(text, (0,0))
		if self.missClock!=None: #text fades out smoother
			a=int((self.missClock/self.missTime)*255)
			a=max(a, 0)
			self.image.set_alpha(a)

	def update(self, sec=0):
		self.time+=sec
		if self.missClock!=None:
			self.missClock-=sec
			if self.missClock<=0:
				self.kill() #calculates when to kill text
		self.draw()
		self.window.blit(self.image, (self.areaX, self.areaY))

	def draining(self):
		self.missClock=self.missTime

class PermText(FadingText):
	white=(255,255,255,0)
	bg=(255,182,193)
	font="Tahoma"
	def __init__(self, window, text, size, x, y, anchor="nw"):
		super().__init__(window, text, size, x, y, anchor)

	def draw(self):
		super().draw()
		text=self.font.render(self.text, 1, PermText.white, PermText.bg)
		self.image.blit(text, (0,0))

class BlitzBeatGame(object):
	def __init__(self, width=1400, height=900, fps=60, title="Blitz Beat"):
		self.width=width
		self.height=height
		self.fps=fps
		self.title=title
		self.modes()
		self.makeBeats()
		self.tracking()
		self.PLAYBACK_END=pygame.USEREVENT+1
		self.startDelay=-1.45
		self.timePassed=self.startDelay
		self.endDelay=2.0
		self.countdown=None
		pygame.mixer.pre_init(48000,-16,1, 1024)
		pygame.mixer.init()
		pygame.init()
		pygame.font.init()

	def tracking(self):
		self.combostreak=0
		self.maxComboStreak=0
		self.score=0
		self.prevAdd=0
		self.hits=pygame.sprite.Group()
		self.hitKill=0.5 #when to make the hit marks disappear

	def modes(self):
		self.inGame=True
		self.splashScreen=True
		self.songSelect=False
		self.helpScreen=False
		self.playGame=False
		self.scoreBoard=False
		self.isPaused=False

	def makeSplashScreen(self):
		backgroundPicture="triangles-download (3).png"
		self.backgroundPicture=pygame.image.load(backgroundPicture)
		self.backgroundPicture=pygame.transform.scale(self.backgroundPicture,(1600,900))
		self.backgroundPicture.convert()
		self.splashScreenButtons=pygame.sprite.Group()
		self.makeSplashScreenButtons()

	def makePauseScreen(self):
		self.pauseScreenButtons=pygame.sprite.Group()
		continueX, continueY=(self.width/2-175, 300)
		continueWidth, continueHeight=(901, 122)
		continueFile="ContinueButton.png"
		self.continueButton=Button1(continueFile, continueX, continueY, continueWidth, continueHeight)
		retryX, retryY=(self.width/2-175, 500)
		retryWidth, retryHeight = 901, 120
		retryFile="RetryButton.png"
		self.retryButton=Button1(retryFile, retryX, retryY, retryWidth, retryHeight)
		backX, backY=self.width/2-175, 700
		backWidth, backHeight=901, 123
		backFile="LongBackButton.png"
		self.longBackButton=Button1(backFile, backX, backY, backWidth, backHeight)
		self.continueButton.add(self.pauseScreenButtons)
		self.retryButton.add(self.pauseScreenButtons)
		self.longBackButton.add(self.pauseScreenButtons)

	def makeSplashScreenButtons(self):
		(titleX, titleY)=(510, 200)
		(titleWidth, titleHeight)=(0, 0)
		titleFile="Title Name.png"
		title=Button1(titleFile, titleX, titleY, titleWidth, titleHeight)
		playButtonX, playButtonY=self.width/2-60, self.height/2-10
		playWidth, playHeight=675, 91
		playFile="PlayButton.png"
		self.playButton=Button1(playFile, playButtonX, playButtonY, playWidth, playHeight)
		helpX, helpY=self.width/2-60, self.height/2+100
		helpWidth, helpHeight=675, 91
		helpFile="HelpButton.png"
		self.helpButton=Button1(helpFile, helpX, helpY, helpWidth, helpHeight)
		settingsX, settingsY=self.width/2-60, self.height/2+210
		settingsWidth, settingsHeight=677, 92
		settingsFile="SettingsButton.png"
		self.settings=Button1(settingsFile, settingsX, settingsY, settingsWidth, settingsHeight)
		exitX, exitY=self.width/2-60, self.height/2+320
		exitWidth, exitHeight=677, 92
		exitFile="ExitButton.png"
		self.exitButton=Button1(exitFile, exitX, exitY, exitWidth, exitHeight)
		title.add(self.splashScreenButtons)
		self.playButton.add(self.splashScreenButtons)
		self.helpButton.add(self.splashScreenButtons)
		self.settings.add(self.splashScreenButtons)
		self.exitButton.add(self.splashScreenButtons)

	def backgroundMusic(self):
		backgroundMusicPath="WHITE-ALBUM-off-Vocals.wav"
		pygame.mixer.music.load(backgroundMusicPath)
		pygame.mixer.music.play(loops=-1)

	def makeBeats(self):
		self.radius=50
		self.beats=pygame.sprite.Group()
		self.beatQueue=deque()
		self.colors=[(126, 178, 245), (255, 192, 203), (220, 162, 249), (114, 230, 199)]
		self.beatColor=(0, 0, 0)
		self.chooseColor()
		self.oldX=None
		self.oldY=None
		self.maxDistance=200
		self.minDistance=100
		self.startingNum=1
		self.endingNum=4
		self.beatScoring()
		self.startingScore()

	def chooseColor(self):
		newColor=random.choice(self.colors)
		while newColor==self.beatColor:
			newColor=random.choice(self.colors)
		self.beatColor=newColor

	#how osu scores
	def startingScore(self):
		self.scoreOK=50
		self.scoreGood=100
		self.scorePerfect=300
		self.miss=0
		self.missCount=0
		self.OKCount=0
		self.goodCount=0
		self.perfectCount=0

	#based on how osu calculates timing from osu.ppy
	#https://osu.ppy.sh/help/wiki/Guides/How_to_Time_Songs
	def beatScoring(self): #calculating hit scores
		self.comingUp=1.0
		self.winWidth=0.06
		self.perfect=self.comingUp-self.winWidth
		self.goodLate=self.comingUp+self.winWidth
		self.OKLate=self.goodLate+self.winWidth
		self.missLate=self.OKLate+self.winWidth
		self.beatKill=self.missLate
		self.goodEarly=self.perfect-self.winWidth
		self.OKEarly=self.goodEarly-self.winWidth
		self.missEarly=self.OKEarly-self.winWidth

	#creates combo and score on playGame mode
	def createComboScore(self):
		score=str(self.score)
		scoreX, scoreY=350, 200
		scoreSize=50
		printScoreText=FadingText(self.screen, score, scoreSize, scoreX, scoreY, "nw")
		combo=str(self.combostreak)+"x"
		comboX, comboY=350, 900
		comboSize=80
		printComboText=FadingText(self.screen, combo, comboSize, comboX, comboY, "sw")

	def startSong(self, path):
		self.path=path
		start=time.time()
		self.onsets=getOnsets(self.path)
		self.nextBeat=self.onsets.pop(0)
		pygame.mixer.music.load(self.path)
		end=time.time()
		offsetTime=abs(end-start)
		self.timePassed-=offsetTime

	def makeHelpScreen(self):
		self.helpButtons=pygame.sprite.Group()
		helpX=550
		helpY=200
		helpWidth=904
		helpHeight=673
		helpFile="Instructions.png"
		helpScreen=Button1(helpFile, helpX, helpY, helpWidth, helpHeight)
		backX=350
		backY=200
		backWidth=182
		backHeight=102
		backFile="BackButton.png"
		self.backButton=Button1(backFile, backX, backY, backWidth, backHeight)
		helpScreen.add(self.helpButtons)
		self.backButton.add(self.helpButtons)

	#makes the scoreBoard screen
	def makeScoreBoard(self):
		self.scoreButtons=pygame.sprite.Group()
		backX, backY=350, 750
		backWidth, backHeight= 182, 102
		backFile="BackButton.png"
		self.backInScore=Button1(backFile, backX, backY, backWidth, backHeight)
		self.backInScore.add(self.scoreButtons)

	#creates score and combo on scoreboard
	def createScoreText(self):
		width, height=self.screen.get_size()
		score="Score: "+str(self.score)
		scoreX, scoreY=400, 290
		scoreSize=70
		finalScoreText=PermText(self.screen, score, scoreSize, scoreX, scoreY)
		combo="Combo: "+str(self.maxComboStreak)+"x"
		comboX, comboY=400, 200
		comboSize=70
		comboText=PermText(self.screen, combo, comboSize, comboX, comboY)
		self.createTimedScore()

	#creates number of perfects, goods, oks, and misses on scoreboard
	def createTimedScore(self):
		numPerfects="Perfect: "+str(self.perfectCount)
		numPerfX, numPerfY=620, 445
		textSize=50
		numPerfText=PermText(self.screen, numPerfects, textSize, numPerfX, numPerfY)
		numGoods="Good: "+str(self.goodCount)
		numGoodX, numGoodY=1190, 445
		numGoodText=PermText(self.screen, numGoods, textSize, numGoodX, numGoodY)
		numOKs="OK: "+str(self.OKCount)
		numOKX, numOKY=620, 540
		numOKText=PermText(self.screen, numOKs, textSize, numOKX, numOKY)
		numMisses="Miss: "+str(self.missCount)
		numMissX, numMissY=1190, 540
		numMissText=PermText(self.screen, numMisses, textSize, numMissX, numMissY)

	def runGame(self):
		gameClock=pygame.time.Clock()
		self.screen=pygame.display.set_mode((0,0), pygame.FULLSCREEN)
		pygame.display.set_caption(self.title)
		self.makeSplashScreen()
		self.makeHelpScreen()
		self.makePauseScreen()
		while self.inGame:
			self.gameLoop(gameClock)
		pygame.font.quit()
		pygame.mixer.quit()
		pygame.quit()

	def gameLoop(self, clock):
		if not pygame.mixer.music.get_busy():
			self.backgroundMusic()
		while self.splashScreen:
			self.splashScreenLoop(clock)
		while self.helpScreen:
			self.helpScreenLoop(clock)
		if self.playGame:
			pygame.mixer.music.play()
			pygame.mixer.music.set_endevent(self.PLAYBACK_END)
		while self.playGame:
			self.playGameLoop(clock)
		while self.scoreBoard:
			self.scoreBoardLoop(clock)
		for event in pygame.event.get():
			if event.type==pygame.QUIT:
				self.inGame=False
		self.gameLoopUpdate()

	def splashScreenLoop(self, clock):
		clock.tick(self.fps)
		self.screen.blit(self.backgroundPicture, (0,0))
		self.splashScreenButtons.draw(self.screen)
		pygame.display.flip()
		for event in pygame.event.get():
			if event.type==pygame.QUIT:
				self.inGame=False
				self.splashScreen=False
			if event.type == pygame.KEYDOWN:
				if event.key==pygame.K_ESCAPE:
					pygame.quit()
			if event.type==pygame.MOUSEBUTTONDOWN and event.button==1:
				self.mousePressed()

	def helpScreenLoop(self, clock):
		clock.tick(self.fps)
		self.screen.blit(self.backgroundPicture, (0,0))
		self.helpButtons.draw(self.screen)
		pygame.display.flip()
		for event in pygame.event.get():
			if event.type==pygame.QUIT:
				self.inGame=False
				self.helpScreen=False
			if event.type==pygame.KEYDOWN:
				if event.key==pygame.K_ESCAPE:
					self.helpScreen=False
					self.splashScreen=True
			if event.type==pygame.MOUSEBUTTONDOWN and event.button==1:
				self.mousePressed()

	def pauseScreenLoop(self, clock):
		clock.tick(self.fps)
		self.screen.blit(self.backgroundPicture, (0, 0))
		self.pauseScreenButtons.draw(self.screen)
		pygame.display.flip()
		for event in pygame.event.get():
			if event.type==pygame.QUIT:
				self.inGame=False
				self.isPaused=False
			if event.type==pygame.KEYDOWN:
				if event.key==pygame.K_ESCAPE:
					self.splashScreen=True
			if event.type==pygame.MOUSEBUTTONDOWN and event.button==1:
				self.mousePressed()

	def scoreBoardLoop(self, clock):
		clock.tick(self.fps)
		if not pygame.mixer.music.get_busy():
			self.backgroundMusic()
		for event in pygame.event.get():
			if event.type==pygame.QUIT:
				self.inGame=False
				self.scoreBoard=False
			if event.type==pygame.KEYDOWN:
				if event.key==pygame.K_ESCAPE:
					self.scoreBoard=False
					self.splashScreen=True
					self.resetGame()
			if event.type==pygame.MOUSEBUTTONDOWN and event.button==1:
				self.mousePressed()
		self.screen.fill((255,182,193))
		self.scoreButtons.draw(self.screen)
		self.createScoreText()
		pygame.display.flip()

	def updateNumber(self):
		self.startingNum+=1
		if self.startingNum>self.endingNum:
			self.startingNum=1
			self.chooseColor()

	def addBeat(self): #keeps beat on screen and have some pattern
		if self.oldX==None and self.oldY==None:
			self.oldX=random.randint(350, 1200)
			self.oldY=random.randint(400, 600)
		if self.oldX<800:
			xDirection=1
		elif self.oldX>1200:
			xDirection=-1
		else:
			xDirection=random.choice([-1,1])
		if self.oldY<400:
			yDirection=1
		elif self.oldY>600:
			yDirection=-1
		else:
			yDirection=random.choice([-1,1])
		dx=random.randint(self.minDistance, self.maxDistance)*xDirection
		dy=random.randint(self.minDistance, self.maxDistance)*yDirection
		x, y =self.oldX+dx, self.oldY+dy
		self.oldX, self.oldY= x, y
		beat=BeatCircle(x, y,self.beatColor, self.startingNum)
		beat.add(self.beats)
		self.beatQueue.append(beat)
		self.updateNumber()

	def mistake(self, beat):
		missColor=(255, 0, 0)
		text="x"
		size=70
		x,y=beat.getPosition()
		if self.combostreak>self.maxComboStreak:
			self.maxComboStreak=self.combostreak
		self.combostreak=0
		missText=FadingText(self.screen, text, size, x, y, "center", missColor)
		missText.add(self.hits)
		self.missCount+=1

	def gameTimerFired(self, time, sec):
		if time+self.comingUp>=self.nextBeat:
			if len(self.onsets)>0:
				self.addBeat()
				self.nextBeat=self.onsets.pop(0)
		for beat in self.beats:
			beat.shrinkRing(sec)
			if beat.missClock==None and beat.clock>=self.beatKill:
				beat.draining()
				self.beatQueue.remove(beat)
				self.mistake(beat)
		for hit in self.hits:
			hit.update(sec)
			if hit.missClock==None and hit.time>=self.hitKill:
				hit.draining()

	def playGameLoop(self, clock):
		sec=clock.tick_busy_loop(self.fps)/1000
		if not self.isPaused:
			pygame.mixer.music.unpause()
			self.timePassed+=sec
			self.gameTimerFired(self.timePassed, sec)
		for event in pygame.event.get():
			self.doAction(event)
		if self.isPaused:
			pygame.mixer.music.pause()
		while self.isPaused:
			self.pauseScreenLoop(clock)
		if self.countdown!=None:
			self.countdown-=sec
			if self.countdown<=0:
				self.playGame=False
				self.scoreBoard=True

		self.playGameLoopUpdate()

	def gameLoopUpdate(self):
		self.screen.fill((0,0,0))
		pygame.display.flip()

	def addScore(self, time, beat):
		self.add=0
		if time>=self.missLate:
			return True
		elif time>=self.OKLate:
			self.add=self.scoreOK
		elif time>=self.goodLate:
			self.add=self.scoreGood
		elif time>=self.perfect:
			self.add=self.scorePerfect
		elif time>=self.goodEarly:
			self.add=self.scoreGood
		elif time>=self.OKEarly:
			self.add=self.scoreOK
		elif time>=self.missEarly:
			return True
		else:
			return None

		self.keepScore()

		return False

	def keepScore(self):
		multiplier=1+self.combostreak/25
		self.score=int(self.score+(self.add*multiplier))
		self.prevAdd=self.add
		if self.add==self.scoreOK:
			self.OKCount+=1
		if self.add==self.scoreGood:
			self.goodCount+=1
		if self.add==self.scorePerfect:
			self.perfectCount+=1

	def addHit(self, beat):
		colorPerfect=(125, 200, 255)
		colorGood=(88, 255, 88)
		colorOK=(255, 226, 125)
		x, y = beat.getPosition()
		text=str(self.prevAdd)
		if self.prevAdd==self.scorePerfect:
			color=colorPerfect
		elif self.prevAdd==self.scoreGood:
			color=colorGood
		elif self.prevAdd==self.scoreOK:
			color=colorOK
		else: return
		size=50
		hitText=FadingText(self.screen, text, size, x, y, "center", color)
		hitText.add(self.hits)

	def playGameLoopUpdate(self):
		if not self.isPaused:
			black=(0, 0, 0)
			self.screen.fill(black)
			self.createComboScore()
			self.hits.draw(self.screen)
			self.beats.draw(self.screen)
			if self.countdown!=None: #makes the game fade out
				displayFade=pygame.Surface((self.width, self.height)) #?
				displayFade.fill((0,0,0))
				a=int((1-self.countdown/self.endDelay)*255)
				a=max(a,0)
				displayFade.set_alpha(a)
				self.screen.blit(displayFade, (0,0))
		if self.isPaused:
			self.screen.blit(self.backgroundPicture, (0,0))

		pygame.display.flip()

	def beatHit(self):
		if len(self.beatQueue)==0:
			return
		x,y=pygame.mouse.get_pos()
		click=MouseLocation(x,y)
		beat=self.beatQueue[0] #only hit the oldest beat
		if pygame.sprite.collide_circle(beat,click):
			miss=self.addScore(beat.clock, beat)
			if miss==None:
				return
			elif miss:
				self.mistake(beat)
			else:
				self.combostreak+=1
			beat.draining()
			self.beatQueue.popleft()
			self.addHit(beat)

	def doAction(self, event):
		if event.type==pygame.QUIT:
			self.inGame=False
			self.playGame=False
		elif event.type==pygame.KEYDOWN:
			if event.key==pygame.K_ESCAPE:
				self.isPaused=not self.isPaused
			if self.isPaused:
				if event.key==pygame.K_r:
					self.resetGame()
					self.splashScreen=True
					return
		if not self.isPaused:
			if event.type==pygame.MOUSEBUTTONDOWN:
				self.beatHit()
			if event.type==self.PLAYBACK_END:
				if self.combostreak>self.maxComboStreak:
					self.maxComboStreak=self.combostreak
				self.makeScoreBoard()
				self.countdown=self.endDelay

	def mousePressed(self):
		x, y=pygame.mouse.get_pos()
		click=MouseLocation(x,y)
		if self.splashScreen:
			self.checkSplashScreenCollision(click)
		if self.helpScreen:
			self.checkHelpScreenCollision(click)
		if self.scoreBoard:
			self.checkScoreBoardCollision(click)
		if self.isPaused:
			self.checkPauseScreenCollision(click)

	def checkSplashScreenCollision(self, click):
		if pygame.sprite.collide_rect(self.playButton, click):
			self.startGame()
			self.splashScreen=False
			self.playGame=True
		if pygame.sprite.collide_rect(self.helpButton, click):
			self.helpScreen=True
			self.splashScreen=False
		if pygame.sprite.collide_rect(self.settings, click):
			pass
		if pygame.sprite.collide_rect(self.exitButton, click):
			pygame.quit()

	def checkHelpScreenCollision(self, click):
		if pygame.sprite.collide_rect(self.backButton, click):
			self.helpScreen=False
			self.splashScreen=True

	def checkScoreBoardCollision(self, click):
		if pygame.sprite.collide_rect(self.backInScore, click):
			self.scoreBoard=False
			self.playGame=False
			self.resetGame()
			self.splashScreen=True

	def checkPauseScreenCollision(self, click):
		if pygame.sprite.collide_rect(self.continueButton, click):
			self.isPaused=not self.isPaused
		if pygame.sprite.collide_rect(self.retryButton, click):
			pass
		if pygame.sprite.collide_rect(self.longBackButton, click):
			self.isPaused=False
			self.playGame=False
			self.splashScreen=True
			self.resetGame()

	def startGame(self):
		self.screen.blit(self.backgroundPicture, (1280, 1024))
		pygame.display.flip()
		try:
			self.startSong("060o0-fqkan.wav")
		except:
			self.error=True
			return
		pygame.mixer.music.stop()
		self.splashScreen=False
		self.playGame=True

	def resetGame(self):
		pygame.mixer.music.stop()
		self.error=False
		self.countdown=None
		self.playGame=False
		self.isPaused=False
		self.combostreak=0
		self.maxComboStreak=0
		self.score=0
		self.prevAdd=0
		self.hits=pygame.sprite.Group()
		self.timePassed=self.startDelay
		self.beats=pygame.sprite.Group()
		self.beatQueue=deque()
		self.startingNum=1
		self.startingScore()
		pygame.mixer.music.set_endevent()

game=BlitzBeatGame()
game.runGame()


from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
from django.conf import settings
from geopy import distance

class Location(models.Model):
    zip = models.PositiveIntegerField(
        'Индекс',
        validators=[MinValueValidator(100), MaxValueValidator(99999)],
        primary_key=True,
    )
    city = models.CharField('Город', max_length=40)
    state = models.CharField('Штат', max_length=30)
    lat = models.DecimalField(
        'Широта',
        max_digits=7,
        decimal_places=5,
        validators=[MinValueValidator(-90), MaxValueValidator(90)],
    )
    lon = models.DecimalField(
        'Долгота',
        max_digits=8,
        decimal_places=5,
        validators=[MinValueValidator(-180), MaxValueValidator(180)],
    )

    def __str__(self):
        return f'{self.zip} {self.city}, {self.state}'

class ShipmentQuerySet(models.QuerySet):
    def with_cars_nearby(
            self,
            radius=settings.CARS_LOOKUP_RADIUS_MI,
            min_weight=None,
            max_weight=None,
    ):
        all_cars = Car.objects.select_related('location')
        shipments = self.select_related('pick_up', 'delivery')
        if min_weight:
            shipments = shipments.filter(weight__gte=float(min_weight))
        if max_weight:
            shipments = shipments.filter(weight__lte=float(max_weight))
        for shipment in shipments:
            cars_nearby = sum(1 if distance.distance(
                (shipment.pick_up.lat, shipment.pick_up.lon),
                (car.location.lat, car.location.lon)
            ).mi <= int(radius) and shipment.weight <= car.carrying
                              else 0 for car in all_cars)
            shipment.cars_nearby = cars_nearby
        return shipments

class Shipment(models.Model):
    pick_up = models.ForeignKey(
        Location,
        on_delete=models.SET_NULL,
        related_name='pick_ups',
        verbose_name='Локация отправки',
        null=True,
    )
    delivery = models.ForeignKey(
        Location,
        on_delete=models.SET_NULL,
        related_name='deliveries',
        verbose_name='Локация доставки',
        null=True,
    )
    weight = models.PositiveSmallIntegerField(
        'Вес',
        validators=[MinValueValidator(1), MaxValueValidator(1000)],
        db_index=True,
    )
    description = models.TextField('Описание', blank=True)

    objects = ShipmentQuerySet.as_manager()

    def __str__(self):
        return f'{self.pick_up} - {self.delivery}, {self.weight}kg'

class CarQuerySet(models.QuerySet):
    def get_numbers_with_distances(self, lat, lon):
        cars = self.select_related('location')
        numbers_with_distances = [
            {
                'number': car.number,
                'distance':  distance.distance(
                    (car.location.lat, car.location.lon),
                    (lat, lon),
                ).mi
             } for car in cars]
        return numbers_with_distances

class Car(models.Model):
    number = models.CharField(
        'Номер',
        max_length=5,
        db_index=True,
        unique=True,
    )
    location = models.ForeignKey(
        Location,
        on_delete=models.SET_NULL,
        related_name='cars',
        verbose_name='Текущая локация',
        null=True,
    )
    carrying = models.PositiveSmallIntegerField(
        'Грузоподъемность',
        validators=[MinValueValidator(1), MaxValueValidator(1000)],
        db_index=True,
    )

    objects = CarQuerySet.as_manager()

    def __str__(self):
        return f'{self.number}, {self.carrying}kg'


b, i, j, flag = 0, 0, 0, 0
dict={2:'Prime'}
b = int(input("Enter upper bound of the interval : "))
print("Required Dictionary is : ")
for i in range(3, b + 1):
        flag = 1
        for j in range(2, i // 2 + 1):
                if (i % j == 0):
                        flag = 0
        if (flag == 1):
                dict[i] = 'Prime'
print(dict)


import chess.pgn
import re

games_separator_regex = re.compile(r"(\[.*\]\W)+\n(.*\W(?!\[))+")

class Game:
    """This class is used to manage games loaded from .pgn files."""

    def __init__(self, pgn_filepath):
        self.games = []
        try:
            self.load_games(pgn_filepath)
        except Exception as e:
            print("Oops! Can't load the game: {}".format(e))

    def load_games(self, pgn_filepath):
        import io
        with open(pgn_filepath, encoding="utf-8-sig", errors='ignore') as data:
            raw_pgn = data.read()
            pgn_games = games_separator_regex.finditer(raw_pgn)

            for pgn in pgn_games:
                pgn_io = io.StringIO(pgn.group())
                self.games.append(chess.pgn.read_game(pgn_io))

            print(f'"{len(self.games)}" games loaded.')


import pandas as pd
import streamlit as st
import graphviz

st.title("Streamlit DAG Maker")

st.write("Funcs available: [foo, bar, confuser]")

df = pd.DataFrame(
    [['foo', 'bar'], ['foo', 'confuser']],
    columns=["Source", "Target"],
)

edited_df = st.experimental_data_editor(
    df,
    use_container_width=True,
    num_rows="dynamic",
)

st.write("Graph so far:")

def mk_graph(df):
    graph = graphviz.Digraph()
    for row in df.itertuples():
        graph.edge(row.Source, row.Target)
    return graph

st.graphviz_chart(mk_graph(edited_df))


for i in range(int(input())):
    n = int(input())
    A = list(map(int,input().split()))
    ecount = ocount = 0
    for j in range(n):
        if A[j]%2==0:
            ecount+=1
        else:
            ocount+=1
    if ecount==n:

        ma = 999999999999999999999999
        for i in range(n):
            count = 0
            while A[i]%2!=1:
                A[i] = (A[i]/2)
                count+=1
            if ma>count:
                ma = count
        print(ma)
    else:
        print("0")


""""
config

* Update variables used by unit tests as msAI evolves

"""

import pytest
from collections import namedtuple

# Attributes that should exist in the MSfile interface and have values provided by subclass implementations (mzMLfile)
MSfile_attributes = ['_run_id',
                     '_run_date',
                     '_ms_file_version',
                     '_spectrum_count',
                     '_peak_count',
                     '_tic_sum',
                     '_peaks',
                     '_spectra']

# Add a failing test for an attribute
# MSfile_fail_test_attribute = pytest.param("_stuff", marks=pytest.mark.xfail(strict=True))
# MSfile_attributes.append(MSfile_fail_test_attribute)

# Properties that should exist in the MSfile interface and have values provided by subclass implementations (mzMLfile)
MSfile_properties = ['run_id',
                     'run_date',
                     'ms_file_version',
                     'spectrum_count',
                     'peak_count',
                     'tic_sum',
                     'peaks',
                     'spectra']

# Add a failing test for a property
# MSfile_fail_test_property = pytest.param("stuff", marks=pytest.mark.xfail(strict=True))
# MSfile_properties.append(MSfile_fail_test_property)

# Properties to value test (omits peaks and spectra, which are value tested later)
MSfile_test_properties = ['run_id',
                          'run_date',
                          'ms_file_version',
                          'spectrum_count',
                          'peak_count',
                          'tic_sum']

MSfile_spectrum_values = ['rt',
                          'peak_count',
                          'tic',
                          'ms_lvl',
                          'filters']

MSfile_peak_values = ['rt',
                      'mz',
                      'i']

TestMSspectrum = namedtuple('TestMSpectrum', ['run_id',
                                              'spec_id',
                                              'rt',
                                              'peak_count',
                                              'tic',
                                              'ms_lvl',
                                              'filters'])

TestMSpeak = namedtuple('TestMSpeak', ['run_id',
                                       'spec_id',
                                       'peak_number',
                                       'rt',
                                       'mz',
                                       'i'])

MSfileKey = namedtuple('MSfileKey', MSfile_test_properties)


# coding:utf-8
import numpy as np
##import pylab as pl
from matplotlib import pyplot as pl
import random as rd
import imageio
import pandas as pd
from pandas.core.frame import DataFrame
from  pandas.testing import assert_frame_equal

'''
标准化数据
'''
def fit_transform(df):
    df = pd.read_csv('./case1.csv',header=0,index_col=0,sep=',')
    df=df.astype(float)#转化数据类型
    df = df.dropna() #删除缺失数据的字段
    df = df.T
    df =df[8:139] #选取每日数据
    std = df.std() #计算标准差, 返回列的标准差
    mean = df.mean()#计算平均值, 返回每列的平均数
    df = (df-mean)/std#标准化数据
    df = df.T
    return df
'''
根据特征值计算“距离”
'''
def distance(a, b):#a,b是Series类型
    #raise Exception
    dis = 0
    for i in range(a.shape[0]):
        dis += (a[i]-b[i])**2
    #print(dis)
    #raise Exception
    return dis

'''
K均值算法
k_point存储每个中心点
'''
def k_means(df, k_count):
    count = df.shape[0]      #行数
    #随机选择K个点
    k = rd.sample(range(count), k_count)
    k_point = DataFrame([df.iloc[i,] for i in k])   #保证有序, 第一组中心点选取从0开始
    #k_point.sort() #以x递增排序
    while True:
        km = [[] for i in range(k_count)]      #存储不同类别
        #遍历所有点
        for i in range(count):
            cp = df.iloc[i,] #当前行
            #计算cp点到所有质心的距离
            _sse = [distance(k_point.iloc[j,], cp) for j in range(k_count)]
            #保存距cp电最近的质心
            min_index = _sse.index(min(_sse))
            #跟新cp点当前距离最短质心
            km[min_index].append(i)

        #一轮跟新结束, 更换质心
        k_new = []
        #for i in range(k_count):
        #    #_x = sum([x[j] for j in km[i]]) / len(km[i])
        #    #_y = sum([y[j] for j in km[i]]) / len(km[i])
        #    for j in range(pd.shape(1)):
        #print(pd.iloc[0,])
        #raise Exception
        for i in range(k_count):#按列计算平均值
            k_new.append(df.iloc[km[i],].mean())
        #k_new.sort()        #排序

        #print(list(k_new.index))
        #print("---")
        #print(list(k_point.index))
        k_new=DataFrame(k_new)
        #input()
        try:
            #if (k_new != k_point):#一直循环直到聚类中心没有变化
            #    k_point = k_new
            #else:
            #    return km
            assert_frame_equal(k_point,k_new)#相等时退出
            return km
        except Exception :
            k_point = k_new

df = pd.read_csv('./case1.csv', header=0, index_col=0,sep=',')
df = fit_transform(df)
k_count = 4
km = k_means(df,k_count)
for i in range(len(km)):
    print('第{0}类:'.format(i+1),df.iloc[km[i],].index.values.tolist())


"""Defines URL patterns for store."""

from django.urls import path, include

from . import views

app_name = 'store'

urlpatterns = [
    # Home page
    path('', views.products, name = 'products'),
    # New use registration page.
    path('users/register', views.register, name ='register'),
    # Individual product's page.
    path('<int:pro_id>', views.product, name= 'product'),
    # Product Name By Search Page
    path('store/search/',views.searchByProductName,name='searchByProductName'),
    # Confirm and place order
    path('store/confirm/<int:pro_id>', views.confirm, name='confirm'),
    # Category Wise Show
    path('store/categorywise/',views.categorywise,name='categorywise'),
    #afterOrderRedirectPage
    path('store/afterOrderRedirect',views.afterOrderRedirect,name='afterOrderRedirect'),
]


"""Adapted from https://github.com/zzzace2000/GAMs_models/.

It implements the bagging of GAM models. Unlike sklearn implmementation of BaggingClassifier, it
averages the logits instead of the probability to make sure the bagging of GAMs is still a GAM. It
also implements the get_GAM_df() that automatically takes average of the GAMs under bagging.

Usage:
>>> from nodegam.gams.MyXGB import MyXGBClassifier
>>> from nodegam.gams.MyBagging import MyBaggingClassifier
>>> base_model = MyXGBClassifier()
>>> # Train an XGB-GAM with 10 times bagging
>>> bag_model = MyBaggingClassifier(base_model=base_model, n_estimators=10)
>>> bag_model.fit(X, y)
>>> df = bag_model.get_GAM_df()
"""

import numpy as np
from sklearn.ensemble import BaggingClassifier, BaggingRegressor

from .EncodingBase import LabelEncodingRegressorMixin, LabelEncodingClassifierMixin, \
    OnehotEncodingClassifierMixin, OnehotEncodingRegressorMixin
from .base import eps, MyGAMPlotMixinBase, MyCommonBase
from .utils import get_GAM_df_by_models, sigmoid

class MyBaggingMixin(MyGAMPlotMixinBase):
    def get_GAM_df(self, x_values_lookup=None, get_y_std=True):
        """Get the GAM graph parameter.

        Args:
            x_values_lookup: a dictionary of mapping feature name to its correpsonding unique
                increasing x. E.g. {'BUN': [1.1, 1.5, 3.1, 5.0], 'cancer': [0, 1]}.
            get_y_std: to get the error bar of the y. It's slower if this is set to true.
                Default: True

        Return:
            A dataframe of GAM graph.
        """

        assert self.is_GAM, 'Only supports visualization when it is a GAM'

        if not get_y_std: # Use the predict function to derive the GAM
            df = super().get_GAM_df(x_values_lookup)

            # Modify the feature name to be the outer feature name
            df.feat_name = ['offset'] + self.feature_names
            return self.revert_dataframe(df)

        # Since the submodel is fitted by np array, the x_values_lookup need to change
        if x_values_lookup is None:
            x_values_lookup = {
                'f%d' % idx : np.array(list(self.X_values_counts[feat_name].keys()))
                for idx, feat_name in enumerate(self.feature_names)
            }

        if 'f0' not in x_values_lookup:
            x_values_lookup = {
                'f%d' % idx : x_values_lookup[feat_name]
                for idx, feat_name in enumerate(self.feature_names)
            }

        all_old_dfs = get_GAM_df_by_models(self.estimators_, x_values_lookup, aggregate=False)

        # Loop through all_dfs, making them into the original form
        all_dfs = []
        for df in all_old_dfs:
            df.feat_name = ['offset'] + self.feature_names
            all_dfs.append(self.revert_dataframe(df))

        # aggregate them
        first_df = all_dfs[0]
        all_ys = [np.concatenate(df.y) for df in all_dfs]

        split_pts = first_df.y.apply(lambda x: len(x)).cumsum()[:-1]
        first_df['y'] = np.split(np.mean(all_ys, axis=0), split_pts)
        first_df['y_std'] = np.split(np.std(all_ys, axis=0), split_pts)

        # Calculate the importances
        importances = [-1.]
        for feat_name, x, y in zip(first_df.feat_name.iloc[1:].values, first_df.x.iloc[1:].values,
                                   first_df.y.iloc[1:].values):
            if feat_name in self.X_values_counts:
                model_xs = np.unique(list(self.X_values_counts[feat_name].keys()))

                # TODO: only caculate importance on X values that's the same this model for now.
                if len(model_xs) != len(x) or np.any(model_xs != x):
                    importance = np.nan
                else:
                    # new_map = pd.Series(y, index=x)
                    # model_ys = new_map[model_xs]
                    importance = np.average(np.abs(y),
                                            weights=list(self.X_values_counts[feat_name].values()))
            else:
                print('No counts recorded for feature: %s. Skip' % feat_name)
                importance = np.nan
            importances.append(importance)

        first_df['importance'] = importances

        return first_df

    @property
    def is_GAM(self):
        return hasattr(self.base_estimator, 'is_GAM') and self.base_estimator.is_GAM

class MyBaggingClassifierBase(MyBaggingMixin, BaggingClassifier):
    def __init__(self,
                 base_estimator=None,
                 n_estimators=10,
                 max_samples=1.0,
                 max_features=1.0,
                 bootstrap=True,
                 bootstrap_features=False,
                 oob_score=False,
                 warm_start=False,
                 n_jobs=None,
                 random_state=None,
                 verbose=0):
        super().__init__(
            base_estimator,
            n_estimators=n_estimators,
            max_samples=max_samples,
            max_features=max_features,
            bootstrap=bootstrap,
            bootstrap_features=bootstrap_features,
            oob_score=oob_score,
            warm_start=warm_start,
            n_jobs=n_jobs,
            random_state=random_state,
            verbose=verbose)

        # Tell the encoding no need to do reversion in get_GAM_df()
        self.not_revert = True

    def predict_proba(self, X, parallel=False):
        """Modify it to be using the average of the log-odds instead of avg probobability.

        The predicted class probabilities of an input sample is computed as
        the mean predicted class probabilities of the base estimators in the
        ensemble. If base estimators do not implement a ``predict_proba``
        method, then it resorts to voting and the predicted class probabilities
        of an input sample represents the proportion of estimators predicting
        each class.

        Args:
            X: {array-like, sparse matrix} of shape = [n_samples, n_features]
                The training input samples. Sparse matrices are accepted only if they are supported
                by the base estimator.
            parallel: if True, predict outputs using parallel threads to speed up. But in xgboost,
                the base estimator already uses multiple threads so it actually slows down.

        Returns:
            p:array of shape = [n_samples, n_classes].
                The class probabilities of the input samples. The order of the classes corresponds
                to that in the attribute `classes_`.
        """

        from sklearn.ensemble.bagging import _parallel_predict_proba
        from sklearn.ensemble.base import _partition_estimators
        from sklearn.utils.validation import check_array, check_is_fitted
        from sklearn.utils._joblib import Parallel, delayed

        check_is_fitted(self, "classes_")
        # Check data
        X = check_array(
            X, accept_sparse=['csr', 'csc'], dtype=None,
            force_all_finite=False
        )

        if self.n_features_ != X.shape[1]:
            raise ValueError("Number of features of the model must "
                             "match the input. Model n_features is {0} and "
                             "input n_features is {1}."
                             "".format(self.n_features_, X.shape[1]))

        # Single thread loop
        if not parallel:
            all_proba = [est.predict_proba(X) for est in self.estimators_]
        else: # Parallel loop
            n_jobs, n_estimators, starts = _partition_estimators(self.n_estimators,
                                                                self.n_estimators)

            all_proba = Parallel(n_jobs=n_jobs, verbose=self.verbose)(
                delayed(_parallel_predict_proba)(
                    self.estimators_[starts[i]:starts[i + 1]],
                    self.estimators_features_[starts[i]:starts[i + 1]],
                    X,
                    self.n_classes_)
                for i in range(n_jobs))

        # Reduce
        all_proba = np.stack(all_proba).astype(np.float64)
        mean_log_odds = np.nanmean(np.log(all_proba + eps) - np.log(1. - all_proba + eps), axis=0)
        if np.any(np.isnan(mean_log_odds)):
            mean_log_odds = np.where(np.isnan(mean_log_odds), np.nanmean(mean_log_odds, axis=0),
                                     mean_log_odds)

        proba = sigmoid(mean_log_odds)

        return proba

class MyBaggingLabelEncodingClassifier(LabelEncodingClassifierMixin, MyBaggingClassifierBase,
                                       MyCommonBase):
    pass

class MyBaggingClassifier(OnehotEncodingClassifierMixin, MyBaggingClassifierBase, MyCommonBase):
    """The bagging for the base estimator GAM.

    It overwrites the sklearn.ensemble.BaggingClassifier to (1) do ensemble on the logits and NOT
         the probabilities to make it still as a GAM, and (2) support GAM df extraction.

    Args:
        base_estimator: the base estimator model.
        n_estimators: how many number of estimators to do bagging.
        max_samples (int or float) = 1.0: the number of samples to draw from X to train each base
            estimator (with replacement by default, see `bootstrap` for more details).
            - If int, then draw `max_features` features.
            - If float, then draw `max_features * X.shape[1]` features.
        max_features (int or float) = 1.0: The number of features to draw from X to train each base
            estimator (without replacement by default, see `bootstrap_features` for more details).
            - If int, then draw `max_features` features.
            - If float, then draw `max_features * X.shape[1]` features.
        bootstrap (bool) = True: Whether samples are drawn with replacement. If False, sampling
            without replacement is performed.
        bootstrap_features (bool) = False: Whether features are drawn with replacement.
        oob_score (bool) = False: Whether to use out-of-bag samples to estimate
            the generalization error. Only available if bootstrap=True.
        warm_start (bool) = False: When set to True, reuse the solution of the previous call to fit
            and add more estimators to the ensemble, otherwise, just fit
            a whole new ensemble.
        n_jobs (int) = None: The number of jobs to run in parallel for both :meth:`fit` and
            :meth:`predict`. ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
            ``-1`` means using all processors.
        random_state: random state.
        verbose: verbose.
    """
    pass

class MyBaggingRegressorBase(MyBaggingMixin, BaggingRegressor):
    def __init__(self,
                 base_estimator=None,
                 n_estimators=10,
                 max_samples=1.0,
                 max_features=1.0,
                 bootstrap=True,
                 bootstrap_features=False,
                 oob_score=False,
                 warm_start=False,
                 n_jobs=None,
                 random_state=None,
                 verbose=0):
        super().__init__(
            base_estimator,
            n_estimators=n_estimators,
            max_samples=max_samples,
            max_features=max_features,
            bootstrap=bootstrap,
            bootstrap_features=bootstrap_features,
            oob_score=oob_score,
            warm_start=warm_start,
            n_jobs=n_jobs,
            random_state=random_state,
            verbose=verbose)

        # Tell the encoding no need to do reversion in get_GAM_plot_df()
        self.not_revert = True

class MyBaggingLabelEncodingRegressor(LabelEncodingRegressorMixin, MyBaggingRegressorBase,
                                      MyCommonBase):
    pass

class MyBaggingRegressor(OnehotEncodingRegressorMixin, MyBaggingRegressorBase, MyCommonBase):
    """The bagging for the base estimator GAM regressor.

    It overwrites the sklearn.ensemble.BaggingRegressor to support GAM df extraction.

    Args:
        base_estimator: the base estimator model.
        n_estimators: how many number of estimators to do bagging.
        max_samples (int or float) = 1.0: the number of samples to draw from X to train each base
            estimator (with replacement by default, see `bootstrap` for more details).
            - If int, then draw `max_features` features.
            - If float, then draw `max_features * X.shape[1]` features.
        max_features (int or float) = 1.0: The number of features to draw from X to train each base
            estimator (without replacement by default, see `bootstrap_features` for more details).
            - If int, then draw `max_features` features.
            - If float, then draw `max_features * X.shape[1]` features.
        bootstrap (bool) = True: Whether samples are drawn with replacement. If False, sampling
            without replacement is performed.
        bootstrap_features (bool) = False: Whether features are drawn with replacement.
        oob_score (bool) = False: Whether to use out-of-bag samples to estimate
            the generalization error. Only available if bootstrap=True.
        warm_start (bool) = False: When set to True, reuse the solution of the previous call to fit
            and add more estimators to the ensemble, otherwise, just fit
            a whole new ensemble.
        n_jobs (int) = None: The number of jobs to run in parallel for both :meth:`fit` and
            :meth:`predict`. ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
            ``-1`` means using all processors.
        random_state: random state.
        verbose: verbose.
    """
    pass


### Funcion zip ###

#zip une listas elemento a elemento

a = [1, 2]
b = ["Uno", "Dos"]
c = zip(a, b)

print(list(c))

#Es util para iterar

for numero, texto in zip(a, b):
    print("Número", numero, "Letra", texto)

#con n argumentos

numeros = [1, 2]
espanol = ["Uno", "Dos"]
ingles = ["One", "Two"]
frances = ["Un", "Deux"]
c = zip(numeros, espanol, ingles, frances)

for n, e, i, f in zip(numeros, espanol, ingles, frances):
    print(n, e, i, f)

#Diferentes longitudes
#Termina cuando la lista mas pequena acaba

numeros = [1, 2, 3, 4, 5]
espanol = ["Uno", "Dos"]

for n, e in zip(numeros, espanol):
    print(n, e)

#Para usar zip en diccionarios usamos la funcion item

esp = {'1': 'Uno', '2': 'Dos', '3': 'Tres'}
eng = {'1': 'One', '2': 'Two', '3': 'Three'}

for (k1, v1), (k2, v2) in zip(esp.items(), eng.items()):
    print(k1, v1, v2)


from os import path

file_path = path.join('.', 'file_handling', 'text_files', 'numbers.txt')

with open(file_path, 'r') as file:
    lines = file.readlines()

result = sum([int(line.strip()) for line in lines])

print(result)


class Solution:
    def average(self, salary: List[int]) -> float:
        n = len(salary)
        maxi, mini = float("-inf"), float("inf")
        sums = 0
        for i in salary:
            sums += i
            maxi = max(maxi, i)
            mini = min(mini, i)
        sums -= (maxi + mini)
        return sums / (n - 2)


"""The bridge data file definitions and functions for any horde worker type."""

from __future__ import annotations

import pathlib
import re
import urllib.parse

from loguru import logger
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
from strenum import StrEnum

from horde_sdk.ai_horde_api.consts import ALCHEMY_FORMS
from horde_sdk.ai_horde_worker.locale_info.bridge_data_fields import BRIDGE_DATA_FIELD_DESCRIPTIONS
from horde_sdk.generic_api.consts import ANON_API_KEY

_UNREASONABLE_NUMBER_OF_MODELS = 1000
"""1000"""

class MetaInstruction(StrEnum):
    ALL_REGEX = r"all$"

    TOP_N_REGEX = r"TOP (\d+)"
    """The regex to use to match the top N models. The number is in a capture group on its own."""

    BOTTOM_N_REGEX = r"BOTTOM (\d+)"

class BaseHordeBridgeData(BaseModel):
    """The base bridge data file for all worker types."""

    model_config = ConfigDict(extra="allow")

    @model_validator(mode="after")  # type: ignore # FIXME: https://github.com/python/mypy/issues/15620
    def validate_extra_params_warning(self) -> BaseHordeBridgeData:
        """Warn on extra parameters being passed."""
        if not self.model_extra:
            return self

        for key in self.model_extra:
            logger.warning(f"Unknown parameter {key} in bridge data file.")

        return self

class SharedHordeBridgeData(BaseHordeBridgeData):
    """The base bridge data file for all worker types."""

    #
    # Shared Worker-type Settings
    #

    api_key: str = Field(
        default=ANON_API_KEY,
        title="API Key",
    )
    """The API key to use to authenticate with the Horde API. Defaults to the anonymous API key."""

    default_worker_name: str = Field("An Awesome AI Horde Worker", alias="worker_name")
    """The default name for any worker type. This is used to easily identify the worker.

    Note that if the user switches to a different worker type after having run a different worker type, the name will
    be rejected by the Horde API. This is because the name is unique to the worker type.
    """

    models_folder_parent: str = Field(
        default="./",
        alias="cache_home",
    )
    """The directories to use to store the model folders. The spirit of this derives from `XDG_CACHE_HOME`, but it is
    not a *volatile* cache. It is the ordinary location for the models. Defaults to the current working directory."""

    temp_dir: str = "./tmp/"
    """The directory to use for temporary files such as model caches. Defaults to `./tmp/`."""

    allow_unsafe_ip: bool = True
    """Whether to allow img2img from unsafe IP addresses. This is only relevant if img2img is enabled."""

    priority_usernames: list[str] = Field(default_factory=list)
    """A list of usernames to prioritize. The API always considers this worker's user to be on this list."""

    stats_output_frequency: int = Field(default=30, json_schema_extra={"unit": "seconds"})
    """The frequency at which to output stats to the console, in seconds. Set to 0 to disable."""
    disable_terminal_ui: bool = False
    """Whether to disable the terminal UI. This is useful for running the worker in a container."""
    always_download: bool = True
    """Whether to always download models, without prompting for permission for each one."""
    suppress_speed_warnings: bool = False
    """Whether to suppress warnings about slower than ideal generations."""
    horde_url: str = ""  # TODO: consts.DEFAULT_HORDE_API_URL
    """The URL of the Horde API to use. Defaults to the official API."""

    @field_validator("horde_url")
    def validate_url(cls, v: str) -> str:
        """Validate the URL is valid."""
        parsed_url = urllib.parse.urlparse(v)
        if not parsed_url.scheme or not parsed_url.netloc:
            raise ValueError(f"Invalid URL: {v}")
        return v

    @field_validator("api_key")
    def validate_api_key_length(cls, v: str) -> str:
        """Validate that the API key is the correct length."""
        if v == ANON_API_KEY:
            return v
        if len(v) != 22:
            raise ValueError("API key must be 22 characters long")
        return v

    @field_validator("models_folder_parent", "temp_dir")
    def validate_is_dir(cls, v: str) -> str:
        """Validate that the path is a directory."""
        paths_list = []
        # Does it specific multiple directories?
        if ";" in v:
            for sub_v in v.split(";"):
                paths_list.append(sub_v)
        else:
            paths_list.append(v)
        for sub_v in paths_list:
            # See if it's a well-formed path, but don't check if it exists
            pathlib.Path(sub_v).absolute()

        return v

class ImageWorkerBridgeData(SharedHordeBridgeData):
    """The bridge data file for a Dreamer or Alchemist worker."""

    #
    # Dreamer
    #

    allow_controlnet: bool = False
    """Whether to allow the use of ControlNet. This requires img2img to be enabled."""

    allow_img2img: bool = True
    """Whether to allow the use of img2img."""

    allow_lora: bool = False
    """Whether to allow the use of LoRA."""

    max_lora_cache_size: int = Field(
        default=10,
        ge=10,
        le=2048,  # I do not foresee anyone having a 2TB lora cache
        json_schema_extra={"unit": "GB"},
    )
    """The maximum size of the LoRA cache, in gigabytes."""

    allow_inpainting: bool = Field(
        default=False,
        alias="allow_painting",
    )
    """Whether to allow the use of inpainting."""

    allow_post_processing: bool = False
    """Whether to allow the use of post-processing."""

    blacklist: list[str] = Field(
        default_factory=list,
    )
    """A list of terms or phrases to blacklist. Jobs containing these terms will be not be assigned to this worker."""

    censor_nsfw: bool = False
    """Whether to censor NSFW content."""

    censorlist: list[str] = Field(
        default_factory=list,
    )
    """A list of terms to allow through the censor. Jobs containing these terms will not be censored for those words
    alone."""

    disable_disk_cache: bool = False
    """Whether to disable the disk cache."""

    dreamer_worker_name: str = Field(
        default="An Awesome Dreamer",
        alias="dreamer_name",
    )

    dynamic_models: bool = False
    """Whether to dynamically switch the models loaded to correspond to the models with the longest queues."""

    max_dynamic_models_to_download: int = Field(
        default=10,
        ge=0,
        le=_UNREASONABLE_NUMBER_OF_MODELS,
        alias="max_models_to_download",
    )
    """The maximum number of models to download."""

    number_of_dynamic_models_to_load: int = Field(
        default=1,
        ge=0,
        le=_UNREASONABLE_NUMBER_OF_MODELS,
        alias="number_of_dynamic_models",
    )
    """The number of dynamic models to load at a time."""

    max_power: int = Field(
        default=8,
        ge=1,
        le=512,
        json_schema_extra={"unit": "* (8 * 64 * 64) pixels"},
    )
    """The factor in the equation `max_power * (8 * 64 * 64)`. This will be the maximum number of pixels that can be
    generated (with inference) in a single job by this worker."""

    image_models_to_load: list[str] = Field(
        default_factory=list,
        alias="models_to_load",
    )

    """A list of models to load. This can be a list of model names, or a list of model loading instructions, such as
    `top 100` or `all models`."""

    image_models_to_skip: list = Field(
        default_factory=list,
        alias="models_to_skip",
    )
    """When a meta model loading instruction is given, like `top 100`, models in this list will be skipped."""

    nsfw: bool = True
    """Whether to allow NSFW content."""

    queue_size: int = Field(default=1, ge=0, le=4)
    """The number of jobs to queue."""

    max_threads: int = Field(
        default=1,
        ge=1,
        le=16,  # 8 already seems incredibly out of reach for most people, but I have set it to 16 just in case
    )
    """The number of threads to use for inference."""

    require_upfront_kudos: bool = False
    """Whether to require upfront kudos for jobs. This effectively makes the worker reject jobs from
    the anonymous API key, or from keys with no kudos."""

    ram_to_leave_free: str = Field(
        default="80%",
        pattern=r"^\d+%$|^\d+$",
        json_schema_extra={"unit": "Percent or MB"},
    )
    """The amount of RAM to leave free for the system to use. This is a percentage or a number in megabytes."""

    vram_to_leave_free: str = Field(
        default="80%",
        pattern=r"^\d+%$|^\d+$",
        json_schema_extra={"unit": "Percent or MB"},
    )
    """The amount of VRAM to leave free for the system to use. This is a percentage or a number in megabytes."""

    #
    # Alchemist Settings
    #

    alchemist_name: str = "An Awesome Alchemist"
    """The name of the worker if it is an alchemist. This is used to easily identify the worker."""

    forms: list[str] = ["caption", "nsfw", "interrogation", "post-process"]
    """The type of services or processing an alchemist worker will provide."""

    @model_validator(mode="after")  # type: ignore # FIXME: https://github.com/python/mypy/issues/15620
    def validate_param_conflict(self) -> ImageWorkerBridgeData:
        """Validate that the parameters are not conflicting."""
        if not self.allow_img2img and self.allow_controlnet:
            logger.warning(
                (
                    "allow_controlnet is set to True, but allow_img2img is set to False. "
                    "ControlNet requires img2img to be enabled. Setting allow_controlnet to false."
                ),
            )
            self.allow_controlnet = False
        return self

    _meta_load_instructions: list[str] | None = None

    @property
    def meta_load_instructions(self) -> list[str] | None:
        """The meta load instructions."""
        return self._meta_load_instructions

    @model_validator(mode="after")
    def handle_meta_instructions(self) -> ImageWorkerBridgeData:
        # See if any entries are meta instructions, and if so, remove them and place them in _meta_load_instructions
        for instruction_regex in MetaInstruction.__members__.values():
            for i, model in enumerate(self.image_models_to_load):
                if re.match(instruction_regex, model, re.IGNORECASE):
                    if self._meta_load_instructions is None:
                        self._meta_load_instructions = []
                    self._meta_load_instructions.append(model)
                    self.image_models_to_load.pop(i)

        return self

    @field_validator("image_models_to_load")
    def validate_models_to_load(cls, v: list) -> list:
        """Validate and parse the models to load."""
        if not isinstance(v, list):
            v = [v]
        if not v:
            v = ["top 2"]
            logger.warning("No models specified in bridgeData.yaml. Defaulting to top 2 models.")
        return v

    @field_validator("ram_to_leave_free", "vram_to_leave_free")
    def validate_ram_to_leave_free(cls, v: str | int | float) -> str | int | float:
        """Validate VRAM/RAM to leave free."""
        if isinstance(v, str):
            if v.isdigit():
                v = int(v)
            elif v.endswith("%"):
                _ = float(v[:-1])
                if _ < 0 or _ > 100:
                    raise ValueError("RAM to leave free must be between 0 and 100%")
        else:
            if v < 0:
                raise ValueError("RAM to leave free must be greater than or equal to 0")
        return v

    @field_validator("forms")
    def validate_alchemy_forms(cls, v: list) -> list:
        """Validate the alchemy forms (services offered)."""
        if not isinstance(v, list):
            raise ValueError("forms must be a list")
        validated_forms = []
        for form in v:
            form = str(form).lower()
            form = form.replace("-", "_")
            if form not in ALCHEMY_FORMS.__members__:
                raise ValueError(f"Invalid form: {form}")
            validated_forms.append(form)
        return validated_forms

class ScribeBridgeData(SharedHordeBridgeData):
    """The bridge data file subset for a Scribe worker."""

    # Scribe Settings
    scribe_name: str = "An Awesome Scribe"
    kai_url: str = "http://localhost:5000"
    """The URL of the KoboldAI API to use. Defaults to the official API."""

    max_context_length: int = Field(
        default=1024,
        ge=1,
        le=16384,  # This is just a sanity check, I doubt anyone would want to go this high
        json_schema_extra={"unit": "tokens"},
    )
    """The maximum number of tokens to use for context with a scribe job."""

    max_length: int = Field(
        default=80,
        ge=1,
        le=16384,  # This is just a sanity check, I doubt anyone would want to go this high
        json_schema_extra={"unit": "tokens"},
    )
    """The maximum number of tokens to generate for a scribe job."""

    branded_model: bool = True
    """If true, this is a custom scribe model, and the horde API will identify uniquely to this worker's API key."""

class CombinedHordeBridgeData(ImageWorkerBridgeData, ScribeBridgeData):
    """The bridge data file for a worker bridge that supports all worker types."""

# Get all BaseModels from the current file
ALL_DATA_FILES_TYPES: list[type[BaseModel]] = [
    SharedHordeBridgeData,
    ImageWorkerBridgeData,
    ScribeBridgeData,
    CombinedHordeBridgeData,
]

# Dynamically add descriptions to the fields of all the base models
for data_file_type in ALL_DATA_FILES_TYPES:
    for field_name, field in data_file_type.model_fields.items():
        if field_name in BRIDGE_DATA_FIELD_DESCRIPTIONS:
            field.description = BRIDGE_DATA_FIELD_DESCRIPTIONS[field_name]