import sys
import os
import signal
import pygame
import pygame.surface
import pygame.joystick
import explorerhat

# Setup PyGame and create the link to the Steam controller

os.environ["SDL_VIDEODRIVER"] = "dummy"

signal.signal(signal.SIGINT, signal.default_int_handler)

pygame.init()
os.putenv('SDL_VIDEODRIVER', 'fbcon')
pygame.display.init()

pygame.joystick.quit()
pygame.joystick.init()

joystick_count = pygame.joystick.get_count()

if joystick_count < 1:
    print("No joysticks found. sc-xbox.py not running?")
    sys.exit()

print("Found " + str(joystick_count) + " joysticks.")

joy = pygame.joystick.Joystick(0)
joy.init()

# Global control variables

trimming = 0.2
powerScalar = 0.5

steer = 0
drive = 0

endProg = False

# Loop until the 'A' button is pressed
print("")
print("Controls:")
print("The `A` button exits the script.")
print("The joystick controls left/right steering.")
print("Left trigger applies forward drive.")
print("Right trigger applies backward drive.")

while not endProg:
    try:
        timeStart = pygame.time.get_ticks()

        for event in pygame.event.get():
            # A button pressed?
            if event.type == pygame.JOYBUTTONDOWN and joy.get_button(0) == 1:
                endProg = True

        # Left and Right triggers
        joydrive = (joy.get_axis(5) * 0.5 + 0.5) - (joy.get_axis(2) * 0.5 + 0.5)

        # Joystick steering control
        joysteer = joy.get_axis(0)

        steer = joysteer
        drive = joydrive

        left = min(1.0, max(-1.0, drive * 0.5 - drive * steer * 0.5))
        right = min(1.0, max(-1.0, drive * 0.5 + drive * steer * 0.5))

        sendLeftMotor = left
        sendRightMotor = right

        trimMulLeft = 1.0 - max(0.0, -trimming)
        trimMulRight = 1.0 - max(0.0, trimming)

        explorerhat.motor[0].speed(int(sendLeftMotor * trimMulLeft * powerScalar * 100))
        explorerhat.motor[1].speed(-int(sendRightMotor * trimMulRight * powerScalar * 100))

        timeEnd = pygame.time.get_ticks()

        deltaTime = timeEnd - timeStart

        pygame.time.delay(max(0, 100 - deltaTime))

    except KeyboardInterrupt:
        endProg = True
        break

pygame.quit()


import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
import numpy as np

X = np.array([[1, 2],
              [1.5, 1.8],
              [5, 8 ],
              [8, 8],
              [1, 0.6],
              [9,11]])

plt.scatter(X[:,0], X[:,1], s=150)
plt.show()

colors = 10*["g","r","c","b","k"]

class K_Means:
    #init method is similar to a constructor
    def __init__(self, k=2, tol=0.001, max_iter=300):
        self.k = k
        self.tol = tol
        self.max_iter = max_iter

    def fit(self,data):

        self.centroids = {}
        #Randomly choose centroids

        for i in range(self.k):
            self.centroids[i] = data[i]
        #CLuster for a maximum of max iterations no of times

        for i in range(self.max_iter):
            self.classifications = {}

            for i in range(self.k):
                self.classifications[i] = []
            #Calculate the distances
            #Assign new points to the cluster with minimal centroid distance
            for featureset in data:
                distances = [np.linalg.norm(featureset-self.centroids[centroid]) for centroid in self.centroids]
                classification = distances.index(min(distances))
                self.classifications[classification].append(featureset)

            prev_centroids = dict(self.centroids)

            for classification in self.classifications:
                self.centroids[classification] = np.average(self.classifications[classification],axis=0)

            optimized = True
            #As long as the cenroid movments are more than tolerance, call optimized as false
            for c in self.centroids:
                original_centroid = prev_centroids[c]
                current_centroid = self.centroids[c]
                if np.sum((current_centroid-original_centroid)/original_centroid*100.0) > self.tol:
                    print(np.sum((current_centroid-original_centroid)/original_centroid*100.0))
                    optimized = False

            if optimized:
                break
    #Predict for new data to find the centroids`
    def predict(self,data):
        distances = [np.linalg.norm(data-self.centroids[centroid]) for centroid in self.centroids]
        classification = distances.index(min(distances))
        return classification
clf = K_Means()
clf.fit(X)

for centroid in clf.centroids:
    plt.scatter(clf.centroids[centroid][0], clf.centroids[centroid][1],
                marker="o", color="k", s=150, linewidths=5)

for classification in clf.classifications:
    color = colors[classification]
    for featureset in clf.classifications[classification]:
        plt.scatter(featureset[0], featureset[1], marker="x", color=color, s=150, linewidths=5)

plt.show()

unknowns = np.array([[1,3],
                     [8,9],
                     [0,3],
                     [5,4],
                     [6,4],])

for unknown in unknowns:
    classification = clf.predict(unknown)
    plt.scatter(unknown[0], unknown[1], marker="*", color=colors[classification], s=150, linewidths=5)

plt.show()


""" HIERARCHY MODULE

    Contains methods for managing the hierarchy of our library.

    NOTE: The new API was added rather late, so isn't fully implemented.
    There are plenty of cases where this could fail to do what a user wants.
    However, the hierarchy should always stay in a valid state.

    KNOWN LIMITATIONS OF CURRENT IMPLEMENTATION.
    - A node HAS to added to each channel hierarchy for each channel that is being analysed.
    - The root node is fixed, and can't be removed.
    - Debugging the structure isn't easy atm, as the structure is dumped out as a string.

    For detailed information on configuring the Hierachy, please see our Readme on our Github.
    https://github.com/RTMAAI/CO600-Musical-Analysis
"""
import logging
from rtmaii.coordinator import Coordinator
from rtmaii.worker import Worker
from rtmaii.exporter import Exporter
LOGGER = logging.getLogger()
class Hierarchy(object):
    """ Builds a hierarchy for the musical analysis tasks.

        Attributes:
            config (Config): Configuration object of library to pass to nodes.
            custom_nodes (list):
            root (dict): multi-level dictionary storing hierarchy configuration.
    """
    def __init__(self, config: object, custom_nodes: list):
        self.config = config
        """ Custom node config, when the hierarchy is recreated,
            we will re-add any nodes in the custom_nodes dictionary.
        """
        self.custom_nodes = {}
        if custom_nodes:
            for key, value in custom_nodes.items():
                # Prepare custom node data.
                self.custom_nodes[key] = {
                    'class_name': value['class_name'],
                    'parent': value['parent'] if 'parent' in value else 'root',
                    'init_args': value['init_args'] if 'init_args' in value else (),
                    'kwargs': value['kwargs'] if 'kwargs' in value else {},
                }
                __validate_node__(self.custom_nodes[key])
        self.reset_hierarchy()

    def reset_hierarchy(self):
        """ Reset hierarchy back to library defaults based on config settings.
            This is quite expensive, as we need to rebuild the entire hierarchy.

            This method is mainly reserved for changes with amount of channels
            being analysed and initial creation of the hierarchy.
        """
        LOGGER.debug('Creating new hierarchy.')
        self.root = {
            'channels': []
        }
        self.root['thread'] = node_factory('RootCoordinator', **{'config': self.config})
        self.root['peer_list'] = self.root['thread'].peer_list
        self.channels = (
            1 if self.config.get_config('merge_channels') else self.config.get_config('channels')
        )

        for channel in range(self.channels):
            self.root['channels'].append({'root':{'peer_list': []}})
            self.root['peer_list'].append(self.root['channels'][channel]['root']['peer_list'])

        self.default_hierarchy()
        for key, value in self.custom_nodes.items():
            self.add_node(value['class_name'], key, value['parent'],
                          *value['init_args'], **value['kwargs'])
        # Cleanup Hierarchy, removing any of our Coordinators that didn't have a child attached.
        self.clean_hierarchy()
        LOGGER.debug('Created hierarchy with config: %s', self.root)

    def default_hierarchy(self):
        """ Create hierarchy tree based on task config provided with the library. """
        LOGGER.debug('Adding inbuilt nodes based on tasks configured.')
        pitch_algorithm = self.config.get_config('pitch_algorithm')
        beat_algorithm = self.config.get_config('beat_algorithm')
        tasks = self.config.get_config('tasks') # The tasks that have been enabled.

        ## COORDINATORS ##
        self.add_node('FrequencyCoordinator')
        self.add_node('SpectrumCoordinator', parent_id='FrequencyCoordinator')
        self.add_node('FFTSCoordinator')
        self.add_node('SpectrogramCoordinator', parent_id='FFTSCoordinator')
        self.add_node('EnergyBPMCoordinator')
        self.add_node('BPMCoordinator')

        ## WORKERS ##
        if tasks['beat']:
            if beat_algorithm == 'ed':
                self.add_node('BPMWorker', parent_id='EnergyBPMCoordinator')
            elif beat_algorithm == 'dc':
                self.add_node('BPMWorker', parent_id='BPMCoordinator')
        if tasks['bands']:
            self.add_node('BandsWorker', parent_id='SpectrumCoordinator')
        if tasks['pitch']:
            if pitch_algorithm == 'hps':
                self.add_node('HPSWorker', parent_id='SpectrumCoordinator')
            elif pitch_algorithm == 'zc':
                self.add_node('ZeroCrossingWorker', parent_id='FrequencyCoordinator')
            elif pitch_algorithm == 'fft':
                self.add_node('FFTWorker', parent_id='SpectrumCoordinator')
            else:
                self.add_node('AutoCorrelationWorker', parent_id='FrequencyCoordinator')
        if tasks['genre']:
            if tasks['export_spectrograms']:
                args = (Exporter(),)
            self.add_node('GenrePredictorWorker', None, 'SpectrogramCoordinator', *args)

    def clean_hierarchy(self):
        """ Removes any coordinators without peers from the hierarchy, saving processing time. """
        LOGGER.debug('Removing inbuilt coordinators without any peers.')
        node_removed = True
        while node_removed:
            node_removed = self.remove_empty_coordinators()

    def remove_empty_coordinators(self):
        """ If there are no peers in a coordinator's peer list remove them.

            NOTE: this is a destructive method!

            I.e. coordinators will be removed if they have an empty peer_list.

            Custom added nodes are preserved, but our inbuilt nodes will be cleaned up.
            This may be changed, however, a user could easily re-add the coordinator they need.
        """
        for node_id, node in self.root['channels'][0].items():
            # Stop removal of nodes added by users in cleanup.
            if not node_id in self.custom_nodes:
                # Only need to remove from one channel as remove_node function will clear both.
                if 'thread' in node:
                    if hasattr(node['thread'], 'peer_list'):
                        if len(node['thread'].get_peer_list()) <= 0:
                            self.remove_node(node_id)
                            return True
        LOGGER.debug('Finished removing inbuilt coordinators without any peers.')
        return False # No nodes were removed this iteration.

    def update_nodes(self):
        """ Propagate updated config settings to nodes of Hierarchy. """
        self.root['thread'].reset_attributes()
        LOGGER.debug('Updating hierarchy nodes.')
        for channel in self.root['channels']:
            for _, peer in channel.items():
                if 'thread' in peer:
                    peer['thread'].reset_attributes()

    def add_custom_node(self, class_name: str, node_id: str = None,
                        parent_id: str = 'root', *init_args: list, **kwargs: dict):
        """ API Based method, stores custom nodes in list,
            so they can be readded if the hierarchy is reset.

            A user could disable all our tasks in the config,
            and create their own unique hierarchy using the API.

            Args:
                - class_name: class_name to instantiate as a string.
                - node_id: unique id to give the node in hierarchy.
                - parent_id: id of parent node to attach to.
                - *init_args: positional arguments to pass to node instantiation.
                - **kwargs: kwargs to pass to node instatiation
        """
        uid = node_id if node_id else class_name
        if uid in self.custom_nodes or uid in self.root['channels'][0]:
            raise AttributeError('Node id {} already exists in hierarchy, please use a unique ID.'
                                 .format(uid))

        self.custom_nodes[uid] = {'class_name': class_name,
                                  'parent': parent_id,
                                  'init_args': init_args,
                                  'kwargs': kwargs}
        __validate_node__(self.custom_nodes[uid])
        self.add_node(class_name, node_id, parent_id, *init_args, **kwargs)

    def add_node(self, class_name: str, node_id: str = None,
                 parent_id: str = 'root', *init_args: list, **kwargs: dict):
        """ Add a new node to the hierarchy on each channel tree.

            Args:
                - class_name: class_name to instantiate as a string.
                - node_id: unique id to give the node in hierarchy.
                - parent_id: id of parent node to attach to.
                - *init_args: positional arguments to pass to node instantiation.
                - **kwargs: kwargs to pass to node instatiation
        """
        uid = node_id if node_id else class_name
        # Add to each channel hierarchy.
        for channel in range(self.channels):
            kwargs['channel_id'] = channel
            kwargs['config'] = self.config
            node_thread = node_factory(class_name, *init_args, **kwargs)
            channel_hierarchy = self.root['channels'][channel]
            channel_hierarchy[uid] = {
                'thread': node_thread
            }
            if parent_id != 'root':
                if parent_id in channel_hierarchy:
                    try:
                        channel_hierarchy[parent_id]['thread'].add_peer(
                            channel_hierarchy[uid]['thread'])
                        channel_hierarchy[uid]['parent'] = parent_id
                    except AttributeError:
                        print('Parent node {} does not have a peer_list.'.format(parent_id))
                        raise
                else:
                    raise KeyError('Could not find specified parent node {} in hierarchy.'
                                   .format(parent_id))
            else:
                channel_hierarchy[parent_id]['peer_list'].append(channel_hierarchy[uid]['thread'])
                channel_hierarchy[uid]['parent'] = 'root'
        LOGGER.debug('Added node %s to hierarchy with node parent %s.', uid, parent_id)

    def remove_node(self, node_id: str):
        """ Remove a node from the hierarchy tasks.

            Args:
                - node_id: unique id of the node to remove.
        """
        if not node_id in self.root['channels'][0]:
            raise KeyError('Node with id {} could not be found in hierarchy. '.format(node_id))

        if node_id == 'root':
            raise ValueError('The root node cannot be removed from the hierarchy!')

        if node_id in self.custom_nodes:
            del self.custom_nodes[node_id]

        # Remove node from each channel.
        for channel in range(self.channels):
            if node_id in self.root['channels'][channel]:
                node = self.root['channels'][channel][node_id]

                # Remove any children from node, if deleting a node with children.
                if hasattr(node['thread'], 'peer_list'):
                    peers = node['thread'].get_peer_list()
                    for peer in peers:
                        LOGGER.debug('Removing child node %s of %s from channel hierarchy %d',
                                     peer, node_id, channel)
                        self.remove_node(peer.__class__.__name__)

                parent = node['parent']
                if parent == 'root':
                    self.root['channels'][channel]['root']['peer_list'].remove(node['thread'])
                else:
                    self.root['channels'][channel][parent]['thread'].remove_peer(node['thread'])

                del self.root['channels'][channel][node_id]

                LOGGER.debug('Removed node %s from channel hierarchy %d', node_id, channel)
            else:
                LOGGER.error('Node %s does not exist in channel hierarchy %d', node_id, channel)

    def put(self, data: object):
        """ Push data to root node of hierarchy.

            Args:
                - data: data to push to root thread's queue.
        """
        self.root['thread'].queue.put(data)

def __validate_node__(node: dict):
    """ Validate that a given nodes parameters are valid.

        Expected signature:
            {'class_name': 'NewWorker', 'parent': 'SpectrumCoordinator',
            'args': (), 'kwargs':{}}

        Args:
            - node: node signature to validate.
    """
    if not isinstance(node['class_name'], str):
        raise TypeError('Class name given {} is not type str.'
                        .format(node['class_name']))
    if not isinstance(node['parent'], str):
        raise TypeError('Parent given {} is not of type str.'
                        .format(node['parent']))
    if not isinstance(node['init_args'], (list, tuple)):
        raise TypeError('Init_args {} is not of type list or tuple.'
                        .format(node['init_args']))
    if not isinstance(node['kwargs'], dict):
        raise TypeError('Kwargs {} is not of type dict.'
                        .format(node['kwargs']))

def node_factory(node_class: str, *args: list, **kwargs: dict):
    """ Create a new node of the given type.
        The node must inherit from either a worker or coordinator base class.

        Args:
            - node_class: class of node to instantiate.
            - *args: positional arguments to pass to node instantiation.
            - **kwargs: kwargs to pass to node instatiation
    """
    nodes = {subclass.__name__ : subclass for subclass in Worker.__subclasses__()}
    nodes.update({subclass.__name__ : subclass for subclass in Coordinator.__subclasses__()})
    if node_class in nodes:
        return nodes[node_class](*args, **kwargs)
    else:
        raise ValueError("{} does not inherit from Worker or Coordinator.".format(node_class))


# -*- coding: utf-8 -*-
"""
Created on Wed Nov 14 18:57:28 2018

@author: conted
"""
#
import os
#dir = os.getcwd()
#print(dir)
#for name in os.listdir():
#    if len(name) <= 9:
#        print(name)
#name = input("input file name")
#if os.path.isfile(name):
#    print("exists")
#else:
#    print("doesn't exist")
#
#
#os.chdir("..\\Big_data_course")
#print(os.getcwd())

#os.chdir("..\\Python")
f= open("test.txt",'r')
for i in f:
    print(i,end='')
f.close
f=open("test.txt",'a')
string = input("- ")
while string.lower() !='q':
    f.write("\n")
    f.write(string)
    string= input("- ")
f.flush
f.close()
#f= open("test.txt",'r')
#f.close


# import necessary libraries
import pandas as pd
from feature_engine.encoding import OneHotEncoder
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.feature_selection import SelectKBest, f_regression,\
  mutual_info_regression
pd.set_option('display.width', 75)
pd.set_option('display.max_columns', 20)
pd.set_option('display.max_rows', 100)
pd.options.display.float_format = '{:,.3f}'.format

# load the NLS data
nls97wages = pd.read_csv("data/nls97wages.csv")

feature_cols = ['satverbal','satmath','gpascience',
  'gpaenglish','gpamath','gpaoverall','gender','motherhighgrade',
  'fatherhighgrade','parentincome','completedba']

X_train, X_test, y_train, y_test =  \
  train_test_split(nls97wages[feature_cols],\
  nls97wages[['wageincome']], test_size=0.3, random_state=0)

# encode and scale the data
ohe = OneHotEncoder(drop_last=True, variables=['gender'])
X_train_enc = ohe.fit_transform(X_train)
scaler = StandardScaler()
standcols = X_train_enc.iloc[:,:-1].columns
X_train_enc = \
  pd.DataFrame(scaler.fit_transform(X_train_enc[standcols]),
  columns=standcols, index=X_train_enc.index).\
  join(X_train_enc[['gender_Male']])

y_train = \
  pd.DataFrame(scaler.fit_transform(y_train),
  columns=['wageincome'], index=y_train.index)

# select 5 best features for predicting wage income
ksel = SelectKBest(score_func=f_regression, k=5)
ksel.fit(X_train_enc, y_train.values.ravel())
selcols = X_train_enc.columns[ksel.get_support()]
selcols
pd.DataFrame({'score': ksel.scores_,
  'feature': X_train_enc.columns},
   columns=['feature','score']).\
   sort_values(['score'], ascending=False)

# select the 5 best features using mutual information
from functools import partial
ksel = SelectKBest(score_func=\
  partial(mutual_info_regression, random_state=0),
  k=5)
ksel.fit(X_train_enc, y_train.values.ravel())
selcols = X_train_enc.columns[ksel.get_support()]
selcols
pd.DataFrame({'score': ksel.scores_,
  'feature': X_train_enc.columns},
   columns=['feature','score']).\
   sort_values(['score'], ascending=False)


class TopTwo:
    """
    Processes file with Juniper CLI output showing input/output packets,
    returns top X utilization report

    Sample CLI output:
    www.juniper.net/documentation/en_US/junos/topics/task/operational/security-real-time-interface-information-displaying.html
    """

    def __init__(self, file, number_of_interfaces_in_top):
        self.file = file
        self.number = number_of_interfaces_in_top
        self.top_utilization = dict()

    def compare_utilization(self, port, utilization):
        """Fills an intermediate dictionary with port/utilization;
        Takes care of future report size
        """

        if len(self.top_utilization) < self.number:
            self.top_utilization[utilization] = port
        else:
            for util, prt in self.top_utilization.items():
                if utilization > util:
                    self.top_utilization.pop(util)
                    # utilization is the key to ease sorting later
                    self.top_utilization[utilization] = port
                    return self.top_utilization

    def process_file(self):
        """Processes the file"""
        with open(self.file, 'r') as f:
            while True:
                line = f.readline()
                if not line:
                    break
                else:
                    line_splitted = line.split()
                    # processes only lines with data, ignores other lines
                    if len(line_splitted) == 6 and line_splitted[2].isdigit() and line_splitted[4].isdigit():
                        utilization = int(line_splitted[2]) + int(line_splitted[4])
                        if utilization > 0:
                            port_number = line_splitted[0]
                            self.compare_utilization(port_number, utilization)

    def get_report(self):
        """Generates console report form 'top_utilization' dictionary"""
        self.process_file()
        # print('TYPE:', type(self.top_utilization))
        # it is a dictionary
        srtd = sorted(self.top_utilization.items())
        # print('TYPE:', type(srtd))
        # after sorted applied, it is a list
        srtd.reverse()
        print('Top', self.number, 'interfaces')
        for tpl in srtd:
            print('Interface:', tpl[1], '  utilization:', tpl[0])

x = TopTwo('monitor_interface_traffic.txt', 6)
x.get_report()


import PySimpleGUI as sg
from keras.models import load_model
import tictactoe_env
import cpu
import utils

def show_result():
    """
    勝敗を文字列に直して返す関数

    Returns
    -------
    - : str
        勝敗を示す文字列
    """
    if info['x_win']:
        return 'Xの勝ちです'
    elif info['o_win']:
        return 'Oの勝ちです'
    else:
        return '引き分けです'

# 学習済みモデルを読み込み
model = load_model('../train/my_model.h5')
# OXゲームクラスをインスタンス化
env = tictactoe_env.TictactoeEnv()

# レイアウトとウィンドウの生成
img_e = './img/-.png'
img_o = './img/o.png'
img_x = './img/x.png'

layout =  [
    [sg.Button('',
               size=(1,1),
               key=(i,j),
               pad=(0,0),
               image_filename=img_e,
               image_size=(1,1),
               image_subsample=4,
              ) for j in range(env.BOARD_SIZE)] for i in range(env.BOARD_SIZE)
]

window = sg.Window('AI×OXゲーム', layout)

# イベントループ
while True:
    event, values = window.read()

    # ウィンドウを閉じる
    if event is None:
        print('exit')
        break

    print(event, values)

    # プレーヤのターン
    index, is_fin, info, valid = env.put(utils.conv2dto1d(event[0], event[1], env.BOARD_SIZE))
    if not valid:
        sg.popup('すでに置かれています！')
        continue
    window[event].update('', image_filename=img_x, image_size=(1,1), image_subsample=4)

    print(env.board.reshape(3, 3))
    print(info)

    if is_fin:
        sg.popup(show_result(), custom_text=('もう一回'), title='対戦結果')
        env = tictactoe_env.TictactoeEnv()
        [window[utils.conv1dto2d(i, env.BOARD_SIZE)].update('', image_filename=img_e, image_size=(1,1), image_subsample=4) for i in range(env.BOARD_SIZE * env.BOARD_SIZE)]
        continue

    # CPUのターン
    index, is_fin, info, valid = env.put(cpu.cpu(env, model))
    window[utils.conv1dto2d(index, env.BOARD_SIZE)].update('',
                                                           image_filename=img_o,
                                                           image_size=(1,1),
                                                           image_subsample=4
                                                          )

    print(env.board.reshape(3, 3))
    print(info)

    if is_fin:
        sg.popup(show_result(), custom_text=('もう一回'), title='対戦結果')
        env = tictactoe_env.TictactoeEnv()
        [window[utils.conv1dto2d(i, env.BOARD_SIZE)].update('', image_filename=img_e, image_size=(1,1), image_subsample=4) for i in range(env.BOARD_SIZE * env.BOARD_SIZE)]
        continue

# ウィンドウの破棄と終了
window.close()


import socketio
import requests

sio = socketio.Client()

@sio.event
def connect():
    print('Connection established with Offensive Mamba Server')

@sio.event
def message(data):
    print('message received with ', data)
    sio.emit('message', {'response': 'my response'})

@sio.event
def disconnect():
    print('disconnected from server')

sio.connect('http://115.186.176.141:8080', headers={})
sio.wait()


# title: Sherlock and the valid string
# a string is valid if all characters of the string appear the same number of times
# it is also valid if he can remove just 1 character at 1 index in the string
# and remaining characters will occur the same numver of times.
from collections import Counter

s = input('enter string: ')
prev_letter = s[0]
s_count = Counter(s)
prev_value = s_count[prev_letter]
for key,value in s_count.items():
    if value == prev_value:
        continue
    elif abs(value - prev_value) > 1:
        print(False)
        break


'''
@Description: BTC市值走势绘制
@Author: Yang Boyu
@Email: bradleyboyuyang@gmail.com
'''

import pandas as pd
from pyecharts.charts import Line
import pyecharts.options as opts

pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
pd.set_option('display.width', 180)

df = pd.read_csv("btc_mv.csv")
print(df.head(20))

# 市值的单位是亿美元
x_data = df['datetime'].tolist()
y_data = df['mv'].round(1).tolist()

# 绘制折线图
line = Line()
line.add_xaxis(xaxis_data=x_data)
line.add_yaxis(
    series_name="BTC/USDT",
    y_axis=y_data,
    symbol="emptyCircle",
    is_symbol_show=True,
    areastyle_opts=opts.AreaStyleOpts(opacity=0.5, color='#00FFFF'),  # 设置图形透明度与填充颜色
    label_opts=opts.LabelOpts(is_show=False),   # 标签配置项
    markpoint_opts=opts.MarkPointOpts(         # 标记点配置项
        data=[
                opts.MarkPointItem(type_="max", name="max"),
                opts.MarkPointItem(type_="average", name="avg")
        ]
    ),
    markline_opts=opts.MarkLineOpts(           # 标记线配置项
        data=[opts.MarkLineItem(type_="average", name="avg")])
)
line.set_global_opts(
    title_opts=opts.TitleOpts(title='Bitcoin Market Value (2013-2021)')
)
line.render("btc_mv_plot.html")


#!/usr/bin/env python

# Just a little utility to help make words.js from a file containing words.

import sys

PER_LINE = 12

with open(sys.argv[1]) as f:
    data = [line[:-1] for line in f]

i = 0
while i < len(data):
    print(f'"{" ".join(data[i:i+PER_LINE])}",')
    i += PER_LINE


from django.template.loader import render_to_string
from django.core.exceptions import ObjectDoesNotExist
from django.core import serializers

from dajax.core import Dajax
from dajaxice.decorators import dajaxice_register

from models import Area, Location, SubLocation, Inscription

MAX_LOCATIONS_ON_INDEX = 15

def get_area_and_locations(area_id):
    area = None
    locations = None

    if area_id is not None:
        id = int(area_id)
        try:
            area = Area.objects.get(id=id)
            if 1 != id:
                locations = area.location_set.all()
            else:
                # randomly get locations at most
                locations = Location.objects.all().order_by('?')
                locations = locations[:MAX_LOCATIONS_ON_INDEX]

        except ObjectDoesNotExist:
            area = None

        return [area, locations]

@dajaxice_register
def display_area_detail(request, area_id):
    area, locations = get_area_and_locations(area_id)

    dajax = Dajax()
    if locations is not None:
        data = serializers.serialize("json", locations)
        dajax.add_data(data, 'area_detail_add_locations')
        dajax.add_data(None, 'zoom_to_show_all_markers')

    return dajax.json()

@dajaxice_register
def display_area(request, area_id):
    area, locations = get_area_and_locations(area_id)

    render_area = render_to_string('inscription/intra_area_info.html',
                                   {'area': area})
    render_photos = render_to_string('inscription/intra_photos.html',
                                     {'photos': area.photos.all()})
    dajax = Dajax()
    if locations is not None:
        data = serializers.serialize("json", locations)
        dajax.add_data(data, 'add_locations')
        dajax.add_data(None, 'zoom_to_show_all_markers')
    dajax.assign('#area_or_location_info', 'innerHTML', render_area)
    dajax.assign('#photos_or_sublocation_info', 'innerHTML', render_photos)

    return dajax.json()

@dajaxice_register
def display_location(request, location_id):
    id = int(location_id)
    location = None

    try:
        location = Location.objects.get(id=id)
    except ObjectDoesNotExist:
        location = None

    render_location = render_to_string('inscription/intra_location_info.html',
                                       {'location': location})
    render_photos = render_to_string('inscription/intra_photos.html',
                                     {'photos': location.photos.all()})

    dajax = Dajax()
    dajax.assign('#area_or_location_info', 'innerHTML', render_location)
    dajax.assign('#photos_or_sublocation_info', 'innerHTML', render_photos)

    return dajax.json()

@dajaxice_register
def display_sublocation(request, sublocation_id):
    id = int(sublocation_id)
    sublocation = None

    try:
        sublocation = SubLocation.objects.get(id=id)
    except ObjectDoesNotExist:
        sublocation = None

    render = render_to_string('inscription/intra_sublocation_info.html',
                              {'sublocation': sublocation})

    dajax = Dajax()
    dajax.assign('#photos_or_sublocation_info', 'innerHTML', render)
    return dajax.json()

@dajaxice_register
def display_inscription(request, inscription_id):
    id = int(inscription_id)
    inscription = None

    try:
        inscription = Inscription.objects.prefetch_related('photos').get(id=id)
    except ObjectDoesNotExist:
        inscription = None

    render = render_to_string('inscription/intra_inscription_info.html',
                              {'inscription': inscription})

    dajax = Dajax()
    dajax.assign('#inscription_info', 'innerHTML', render)
    return dajax.json()


# Refaça o DESAFIO 051, lendo o primeiro termo e a razão de uma PA,
# mostrando os 10 primeiros termos da progressão usando a estrutura while.

primeiro = int(input('Digite o primeiro numero: '))
razao = int(input('Digite a razao da PA: '))
termo = primeiro
i = 1
total = 0
mais = 10

while mais != 0:
    total = total + mais
    while i <= total:
        print('{} -> '.format(termo), end='')
        termo = termo + razao
        i = i + 1
    print('Pausa')
    mais = int(input('Quantos mais: '))
print('Progressao finalizada com {} termos.'.format(total))


#netflix

class Cliente:
    def __init__(self,nome,plano,email):
        self.nome = nome
        self.email = email
        lista_planos=['Básico','Padrão','Premium']
        if plano in lista_planos:
            self.plano = plano
        else:
            raise Exception('Plano inválido')

    def mudar_plano(self,plano):
        self.plano = plano

cliente = Cliente('Heber','Padrão','exemplo@gmail.com')
print(cliente.nome)


from math import pi

from src.ecs.clocks import delta_time, Clocks
from src.ecs.union import Union
from src.game.default_data_collector import DefaultDataCollector
from src.game.ui_factory import UiFactory
from src.systems.debug import debug
from src.systems.debug.fps.fps_label import FpsLabel
from src.ecs.special_entities.named import Named
from src.systems.gameplay import gameplay
from src.systems.gameplay.ai.ai_module import AiModule
from src.systems.gameplay.cleaning.temporal import Temporal
from src.systems.gameplay.navigation.navigated import Navigated
from src.systems.gameplay.shooting.shooter import Shooter
from src.systems.graphics import graphics
from src.systems.graphics.animation import Animation
from src.systems.graphics.animation.animated import Animated
from src.systems.graphics.camera import Camera
from src.systems.graphics.deep import Deep
from src.systems.graphics.sprites.circle_sprite import CircleSprite
from src.systems.graphics.sprites.image_sprite import ImageSprite
from src.systems.graphics.sprites.line_sprite import LineSprite
from src.systems.graphics.tk_window import TkWindow
from src.systems.graphics.ui.data_container import DataContainer
from src.systems.graphics.ui.player_ui import PlayerUi
from src.systems.physics import physics
from src.systems.physics.collision.circle_collider import CircleCollider
from src.systems.physics.collision.collider import Collider
from src.systems.physics.durability.durable import Durable
from src.systems.physics.gravity.massive import Massive
from src.systems.physics.inertion.movable import Movable
from src.systems.physics.mounting.massive_system import MassiveSystem
from src.systems.physics.mounting.mounted import Mounted
from src.systems.physics.positioned import Positioned
from src.systems.physics.rotated import Rotated
from src.systems.physics.traction.tractor import Tractor
from src.tools import vector
from src.tools.vector import Vector

# Clocks

DEBUG = True

clocks = Clocks(
    DEBUG,
    physics,
    gameplay,
    graphics,
    *(() if not DEBUG else (
        debug,
    )),
)

def create(first, *components):
    e = first if not components and isinstance(first, Union) else Union(first, *components)
    clocks.register_entity(e)
    return e

# Game entities

def asteroid(name, position, radius, mass):
    return (
        Named(name),
        Positioned(position),
        CircleSprite(radius),
        Massive(mass),
        Movable(),
        Collider(resilience_k=0.3),
    )

asteroid1 = create(
    *asteroid("A001-01: snowball", Vector(0, 0), 300, 1e8),
)

create(Union(
    G=1e-3,
    g_min=1e-1,
))

# Player & co

def create_gauss_gun(holder, offset, *additional_components):
    return create(
        ImageSprite("Gauss gun"),
        Movable(),
        Massive(50),
        Mounted(holder, offset),
        Rotated(0),
        Shooter(
            lambda: Union(
                CircleSprite(3),
                Collider(resilience_k=0.8),
                Massive(10),
                Durable(1e4),
                Temporal(60),
            ),
            1000,
            Vector(50, 0),
        ),
        MassiveSystem(),
        *additional_components,
    )

player = create(
    ImageSprite("Drilling ship"),
    Positioned(Vector(500, 500)),
    Movable(),
    Massive(1e4),
    Tractor(Vector(0, -1), 1e5),
    CircleCollider(25, resilience_k=0.7),
    Animated(
        {(lambda self: self.traction_enabled): Animation("drilling_ship/movement", 1.5, 9)}
    ),
    Rotated(0),
    Navigated(asteroid1),
    Durable(1.25e8),
    DefaultDataCollector('assets/texts/ui/default_data_collector.json', 'russian'),
    MassiveSystem(),
)

gun = create_gauss_gun(player, Vector(-4, -20))

landing_module = create(
    Massive(1),
    Movable(),
    Mounted(player, Vector(0, 25)),
    CircleCollider(10, resilience_k=-0.5),
    MassiveSystem(),
)

enemy = create(
    ImageSprite("Turret"),
    Positioned(player.position + vector.one * 300),
    Movable(),
    Massive(1e3),
    CircleCollider(13, 0.7),
    Rotated(45),
    Durable(5e7),
    MassiveSystem(),
)

enemy_gun = create_gauss_gun(enemy, vector.zero, AiModule(300))

# User Interface:

camera = create(
    Camera(None),
    Positioned(Vector(0, 0)),
    Deep(1),
)

display = create(
    TkWindow("Dying space", Vector(1280, 720), camera),
    PlayerUi(None),
)

display.player = player
camera.target = player

if DEBUG:
    fps_label = create(FpsLabel(display.window_root))

def traction_set(value):
    player.traction_enabled = value

def rotate(dir):
    player.rotation += dir * pi * delta_time()

def update_gun_rotation():
    gun.rotation = (display.get_mouse_position() - gun.position).angle()

def shoot():
    gun.shooting_enabled = True

display.bind_action('w', lambda e: traction_set(True))
display.bind_action('s', lambda e: traction_set(False))
display.bind_action('a', lambda e: rotate(-1))
display.bind_action('d', lambda e: rotate(1))
display.bind_action('<Motion>', lambda e: update_gun_rotation())
display.bind_action('<Button-1>', lambda e: shoot())

# speed_vector = create(
#     DataContainer(lambda: display.player.velocity ** 0 * 60, VECTOR),
#     LineSprite(None, "lightgreen", arrow_type=LAST),
#     Mounted(camera, Vector(80, 80))
# )
#
# target_vector = create(
#     DataContainer(lambda: (display.player.navigation_target.position - display.player.position) ** 0 * 60, VECTOR),
#     LineSprite(None, "red", arrow_type=LAST),
#     Mounted(camera, Vector(80, 80))
# )

ui = tuple(UiFactory(
    ui_position=Vector(20, 20),
    compass_size=100,
    blocks_offset=30,
    text_offset=20,
    font_name="Consolas 9",
).produce(display, camera))

for e in ui:
    create(e)


# coding: utf-8
#
# Usage:
#   python examples/basic_nmt.py -m train -c configs/basic_nmt.ini
#   python examples/basic_nmt.py -m eval -c configs/basic_nmt.ini
#
# Purpose:
#   Input some sequence, then predict same sequence(+ EOS token).

import argparse
import os
import sys

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf

from configs.configs import Configs
from data.data import read_data, read_words, batchnize, build_dictionary, sentence_to_onehot, seq2seq
from utils.early_stopping import EarlyStopper
from utils.monitor import Monitor

def main(args):
  # process config
  c = Configs(args.config)
  ROOT = os.environ['TENSOROFLOW']
  model_path = '%s/examples/model/basic_nmt/model' % ROOT
  PAD = c.const['PAD']
  EOS = c.const['EOS']
  train_step = c.option['train_step']
  max_time = c.option['max_time']
  batch_size = c.option['batch_size']
  vocabulary_size = c.option['vocabulary_size']
  input_embedding_size = c.option['embedding_size']
  hidden_units = c.option['hidden_units']
  source_train_data_path = c.data['source_train_data']
  target_train_data_path = c.data['target_train_data']
  source_valid_data_path = c.data['source_valid_data']
  target_valid_data_path = c.data['target_valid_data']
  source_test_data_path = c.data['source_test_data']
  target_test_data_path = c.data['target_test_data']

  # read data
  source_dictionary, source_reverse_dictionary = build_dictionary(read_words(source_train_data_path), vocabulary_size)
  source_train_datas = [sentence_to_onehot(lines, source_dictionary) for lines in read_data(source_train_data_path)]
  target_dictionary, target_reverse_dictionary = build_dictionary(read_words(target_train_data_path), vocabulary_size)
  target_train_datas = [sentence_to_onehot(lines, target_dictionary) for lines in read_data(target_train_data_path)]

  source_valid_datas = [sentence_to_onehot(lines, source_dictionary) for lines in read_data(source_valid_data_path)]
  target_valid_datas = [sentence_to_onehot(lines, target_dictionary) for lines in read_data(target_valid_data_path)]
  source_test_datas = [sentence_to_onehot(lines, source_dictionary) for lines in read_data(source_test_data_path)]
  target_test_datas = [sentence_to_onehot(lines, target_dictionary) for lines in read_data(target_test_data_path)]

  # placeholder
  encoder_inputs = tf.placeholder(shape=(None, None), dtype=tf.int32, name='encoder_inputs')
  decoder_inputs = tf.placeholder(shape=(None, None), dtype=tf.int32, name='decoder_inputs')
  decoder_labels = tf.placeholder(shape=(None, None), dtype=tf.int32, name='decoder_labels')

  # embed
  embeddings = tf.Variable(tf.random_uniform([vocabulary_size, input_embedding_size], -1.0, 1.0), dtype=tf.float32, name='embeddings')
  encoder_inputs_embedded = tf.nn.embedding_lookup(embeddings, encoder_inputs)
  decoder_inputs_embedded = tf.nn.embedding_lookup(embeddings, decoder_inputs)

  # encoder
  encoder_units = hidden_units
  encoder_cell = tf.contrib.rnn.LSTMCell(encoder_units)
  _, encoder_final_state = tf.nn.dynamic_rnn(
      encoder_cell, encoder_inputs_embedded,
      dtype=tf.float32, time_major=True
  )

  # decoder
  decoder_units = encoder_units
  decoder_cell = tf.contrib.rnn.LSTMCell(decoder_units)
  decoder_output, decoder_final_state = tf.nn.dynamic_rnn(
      decoder_cell, decoder_inputs_embedded,
      initial_state=encoder_final_state,
      scope="plain_decoder",
      dtype=tf.float32, time_major=True
  )

  decoder_logits = tf.contrib.layers.linear(decoder_output, vocabulary_size)
  decoder_prediction = tf.argmax(decoder_logits, 2) # max_time: axis=0, batch: axis=1, vocab: axis=2

  # optimizer
  stepwise_cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
      labels=tf.one_hot(decoder_labels, depth=vocabulary_size, dtype=tf.float32),
      logits=decoder_logits,
  )

  loss = tf.reduce_mean(stepwise_cross_entropy)
  train_op = tf.train.AdamOptimizer().minimize(loss)

  saver = tf.train.Saver()
  batch_idx = {'train': 0, 'valid': 0, 'test': 0}
  with tf.Session() as sess:
    if args.mode == 'train':
      # train
      loss_freq = train_step // 100
      loss_log = []
      loss_suffix = ''
      es = EarlyStopper(max_size=5, edge_threshold=0.1)
      m = Monitor(train_step)
      sess.run(tf.global_variables_initializer())
      for i in range(train_step):
        m.monitor(i, loss_suffix)
        source_train_batch, _ = batchnize(source_train_datas, batch_size, batch_idx['train'])
        target_train_batch, batch_idx['train'] = batchnize(target_train_datas, batch_size, batch_idx['train'])
        batch_data = seq2seq(source_train_batch, target_train_batch, max_time, vocabulary_size)
        feed_dict = {encoder_inputs:batch_data['encoder_inputs'],
                     decoder_inputs:batch_data['decoder_inputs'],
                     decoder_labels:batch_data['decoder_labels']}
        sess.run(fetches=[train_op, loss], feed_dict=feed_dict)
        if i % loss_freq == 0:
          source_valid_batch, _ = batchnize(source_valid_datas, batch_size, batch_idx['valid'])
          target_valid_batch, batch_idx['valid'] = batchnize(target_valid_datas, batch_size, batch_idx['valid'])
          batch_data = seq2seq(source_valid_batch, target_valid_batch, max_time, vocabulary_size)
          feed_dict = {encoder_inputs:batch_data['encoder_inputs'],
                       decoder_inputs:batch_data['decoder_inputs'],
                       decoder_labels:batch_data['decoder_labels']}
          loss_val = sess.run(fetches=loss, feed_dict=feed_dict)
          loss_log.append(loss_val)
          loss_suffix = 'loss: %f' % loss_val
          es_status = es(loss_val)
          if i > train_step // 2 and es_status:
            print('early stopping at step: %d' % i)
            break
      saver.save(sess, model_path)
      print('save at %s' % model_path)
      plt.plot(np.arange(len(loss_log)) * loss_freq, loss_log)
      plt.savefig('%s_loss.png' % model_path)
    elif args.mode == 'eval':
      saver.restore(sess, model_path)
      print('load from %s' % model_path)
    else:
      raise

    # evaluate
    loss_val = []
    input_vectors = None
    predict_vectors = None
    for i in range(len(source_test_datas) // batch_size + 1):
      source_test_batch, _ = batchnize(source_test_datas, batch_size, batch_idx['test'])
      target_test_batch, batch_idx['test'] = batchnize(target_test_datas, batch_size, batch_idx['test'])
      batch_data = seq2seq(source_test_batch, target_test_batch, max_time, vocabulary_size)
      feed_dict = {encoder_inputs:batch_data['encoder_inputs'],
                   decoder_inputs:batch_data['decoder_inputs'],
                   decoder_labels:batch_data['decoder_labels']}
      pred = sess.run(fetches=decoder_prediction, feed_dict=feed_dict)
      if predict_vectors is None:
        predict_vectors = pred.T
      else:
        predict_vectors = np.vstack((predict_vectors, pred.T))
      input_ = batch_data['encoder_inputs']
      if input_vectors is None:
        input_vectors = input_.T
      else:
        input_vectors = np.vstack((input_vectors, input_.T))
      loss_val.append(sess.run(fetches=loss, feed_dict=feed_dict))

    input_sentences = ''
    predict_sentences = ''
    for i, (input_vector, predict_vector) in enumerate(zip(input_vectors[:len(source_test_datas)], predict_vectors[:len(target_test_datas)])):
      input_sentences += ' '.join([source_reverse_dictionary[vector] for vector in input_vector if not vector == PAD])
      predict_sentences += ' '.join([target_reverse_dictionary[vector] for vector in predict_vector if not vector == PAD])
      if i < len(source_test_datas) - 1:
        input_sentences += '\n'
        predict_sentences += '\n'

    evaluate_input_path = '%s.evaluate_input' % model_path
    evaluate_predict_path = '%s.evaluate_predict' % model_path
    with open(evaluate_input_path, 'w') as f1, \
         open(evaluate_predict_path, 'w') as f2:
      f1.write(input_sentences)
      f2.write(predict_sentences)

    print('input sequences at {}'.format(evaluate_input_path))
    print('predict sequences at {}'.format(evaluate_predict_path))
    print('mean of loss: %f' % np.mean(loss_val))

  print('finish.')

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument('--mode', '-m', type=str, help='train | eval')
  parser.add_argument('--config', '-c', type=str, help='config file path')
  args = parser.parse_args()
  main(args)


# https://leetcode.com/problems/add-minimum-number-of-rungs/
class Solution:
    def addRungs(self, rungs: List[int], dist: int) -> int:
        mul = rungs[0] / dist
        ans = ceil(mul) - 1 if mul >= 1 else 0
        i = 0

        while (i + 1) < len(rungs):
            mul = (rungs[i + 1] - rungs[i])/ dist
            if mul >= 1:
                ans += ceil(mul) - 1
                i += 1
                continue

            idx = bisect_right(rungs, rungs[i] + dist, i)
            i = idx - 1

        return ans

1936. Add Minimum Number of Rungs - medium.py


count = int(input())
arr = []
for i in range(count):
	arr.append(input())

for i in range(count):
	stack = []
	for j in range(len(arr[i])):
		if(arr[i][0] == ")"):
			stack.append(")")
			break

		if(arr[i][j] == "("):
			stack.append(arr[i][j])
		else:
			# stack.remove(")")
			if(stack != []):
				stack.pop()
			else:
				stack.append(")")
				break

	if(stack == []):
		print("Yes")
	else:
		print("No")


import numpy as np
import nltk
import json
from tqdm import trange
import os
import argparse

'''
This script reads the SQuAD 1.1 dataset as JSON then outputs each sample as a separate .question file.
These files will each contain a paragraph, question and an answer, which can then be used by the Gated Attention Reader.

'''

def squad_parser(test_run=False):
    data_path = "data"
    data_dest = "squad"

    data_path_full = os.path.join(os.getcwd(), data_path, "SQuAD-v1.1.json")
    data_file = open(data_path_full, encoding="utf8")
    json_file = json.loads(data_file.read())
    data = json_file[1][1:]  # The list containing all the sample paragraphs and question/answer pairs

    # Directory for the separated question files
    data_dir = os.path.join(data_path, data_dest)
    if not os.path.exists(data_dir):
        os.mkdir(data_dir)

    N = len(data)

    test_ratio = 0.1  # This portion of the training data will be used for testing
    np.random.seed(42)
    test_array = np.random.rand(N) < test_ratio

    # Progress bar
    if test_run:
        N = 900
    tr = trange(N, desc="Parsing dataset", leave=True, ncols=100, ascii=True)
    for index, sample in enumerate(data):

        paragraph = sample[1][2].replace("'", "")  # The paragraph text
        paragraph = paragraph.replace("\n", " ")  # Replacing new lines with space to have paragraph as a one-line string
        qa_set = sample[2][2][1:]                # The set of all (5) question/answer pairs relating to the paragraph
        validation_role = \
            sample[-1][2].replace("'", "").lower()   # text marking if the sample is training or validation data
        if test_array[index] == 1:  # Overwriting role if index is in test set
            validation_role = "test"

        # Replace validation role during test run to limit number of samples
        if test_run:
            if index == N:
                break
            if index < N/3:
                validation_role = "training"
            elif index < 2*N/3:
                validation_role = "validation"
            else:
                validation_role = "test"

        # Creating/Using the folder for training/validation samples
        file_dir = os.path.join(data_dir, validation_role)
        if not os.path.exists(file_dir):
            os.mkdir(file_dir)

        for qa_pair in qa_set:  # for each question/answer pair we create a separate ".question" file
            question = qa_pair[1][2].replace("'", "")   # question text
            answer = qa_pair[2][2][1].replace("'", "")  # answer text
            answer_start = qa_pair[3][2][1]               # position of answer in paragraph, integer

            # Heuristic method for finding answer end positions in char space
            # We look for the answer in the paragraph starting from the answer start
            # character minus a window (to take care of cases where answer start was wrong)

            # Fix cases where answer_start was chosen to be too high
            try:
                paragraph[answer_start:].index(answer)
            except ValueError:
                answer_start = paragraph.index(answer)

            answer_end = answer_start + paragraph[answer_start:].index(answer) + len(answer)

            # Heuristic method for finding answer start and end index in token space
            # We look for the span of answer words in the paragraph that matches the
            # length of the actual answer span
            doc_tokens = nltk.word_tokenize(paragraph)
            answer_tokens = nltk.word_tokenize(answer)
            ans_mask = [1 if doc_tokens[index] in answer_tokens else 0
                        for index, value in enumerate(doc_tokens)]
            answer_start_token = 0
            answer_end_token = 0
            span = 0
            answer_span = len(answer_tokens)

            for idx, value in enumerate(ans_mask):
                if value == 1:
                    span += 1
                else:
                    span = 0
                if span == answer_span:
                    answer_end_token = idx
                    answer_start_token = idx - answer_span + 1
                    break

            question_id = qa_pair[4][2].replace("'", "")  # unique id for question

            # create .question file with the given question id as name
            file_name = file_dir + "/" + question_id + ".question"
            f = open(file_name, "w+", encoding="utf-8")
            f.write("\n\n")
            f.write(paragraph + "\n\n")
            f.write(question + "\n\n")
            f.write(answer + "\n\n")
            f.write(str(answer_start) + " " + str(answer_end) + "\n\n")
            f.write(str(answer_start_token) + " " + str(answer_end_token) + "\n")
            f.close()

        tr.update()
    tr.close()

    print("Dataset was parsed into {}".format(os.path.join(os.getcwd(), data_dir)))


import bpy

#Todo - Check if already exists, in case multiple objects has the same material

def backup_material_copy(slot):
    material = slot.material
    dup = material.copy()
    dup.name = "." + material.name + "_Original"
    dup.use_fake_user = True

def backup_material_cache(slot, path):
    bpy.ops.wm.save_as_mainfile(filepath=path, copy=True)

def backup_material_cache_restore(slot, path):
    if bpy.context.scene.TLM_SceneProperties.tlm_verbose:
        print("Restore cache")

# def backup_material_restore(obj): #??
#     if bpy.context.scene.TLM_SceneProperties.tlm_verbose:
#         print("Restoring material for: " + obj.name)

#Check if object has TLM_PrevMatArray
#   if yes
#       - check if array.len is bigger than 0:
#           if yes:
#               for slot in object:
#                   originalMaterial = TLM_PrevMatArray[index]
#
#
#           if no:
#               - In which cases are these?

#   if no:
#    - In which cases are there not?
#    - If a lightmapped material was applied to a non-lightmap object?

                # if bpy.data.materials[originalMaterial].users > 0: #TODO - Check if all lightmapped

                #     print("Material has multiple users")

                #     if originalMaterial in bpy.data.materials:
                #         slot.material = bpy.data.materials[originalMaterial]
                #         slot.material.use_fake_user = False
                #     elif "." + originalMaterial + "_Original" in bpy.data.materials:
                #         slot.material = bpy.data.materials["." + originalMaterial + "_Original"]
                #         slot.material.use_fake_user = False

                # else:

                #     print("Material has one user")

                #     if "." + originalMaterial + "_Original" in bpy.data.materials:
                #         slot.material = bpy.data.materials["." + originalMaterial + "_Original"]
                #         slot.material.use_fake_user = False
                #     elif originalMaterial in bpy.data.materials:
                #         slot.material = bpy.data.materials[originalMaterial]
                #         slot.material.use_fake_user = False

def backup_material_restore(obj): #??
    if bpy.context.scene.TLM_SceneProperties.tlm_verbose:
        print("Restoring material for: " + obj.name)

    if "TLM_PrevMatArray" in obj:

        if bpy.context.scene.TLM_SceneProperties.tlm_verbose:
            print("Material restore array found: " + str(obj["TLM_PrevMatArray"]))
        #Running through the slots
        prevMatArray = obj["TLM_PrevMatArray"]
        slotsLength = len(prevMatArray)

        if len(prevMatArray) > 0:
            for idx, slot in enumerate(obj.material_slots): #For each slot, we get the index
                #We only need the index, corresponds to the array index
                try:
                    if bpy.context.scene.TLM_SceneProperties.tlm_verbose:
                        print("Attempting to set material")
                    originalMaterial = prevMatArray[idx]
                except IndexError:
                    if bpy.context.scene.TLM_SceneProperties.tlm_verbose:
                        print("Material restore failed - Resetting")
                    originalMaterial = ""

                if slot.material is not None:
                    #if slot.material.users < 2:
                    #slot.material.user_clear() #Seems to be bad; See: https://developer.blender.org/T49837
                    #bpy.data.materials.remove(slot.material)

                    if "." + originalMaterial + "_Original" in bpy.data.materials:
                        slot.material = bpy.data.materials["." + originalMaterial + "_Original"]
                        slot.material.use_fake_user = False

        else:

            print("No previous material for " + obj.name)

    else:

        print("No previous material for " + obj.name)

def backup_material_rename(obj): #??
    if bpy.context.scene.TLM_SceneProperties.tlm_verbose:
        print("Renaming material for: " + obj.name)

    if "TLM_PrevMatArray" in obj:

        for slot in obj.material_slots:

            if slot.material is not None:
                if slot.material.name.endswith("_Original"):
                    newname = slot.material.name[1:-9]
                    if newname in bpy.data.materials:
                        if bpy.context.scene.TLM_SceneProperties.tlm_verbose:
                            print("Removing material: " + bpy.data.materials[newname].name)
                        #if bpy.data.materials[newname].users < 2:
                            #bpy.data.materials.remove(bpy.data.materials[newname]) #TODO - Maybe remove this
                    slot.material.name = newname

        del obj["TLM_PrevMatArray"]

    else:

        print("No Previous material array for: " + obj.name)


class Solution:
    def findTheWinner(self, n: int, k: int) -> int:
        arr = [i for i in range(1,n+1)]
        delete = 0

        while len(arr) > 1:
            delete = (delete + k - 1) % len(arr)
            del arr[delete]

        return arr[0]


from st2tests.base import BaseActionTestCase

from run import ActionManager
from lib.exception import MissingParameterError
from lib.exception import MissingProfileError
from lib.exception import ValidationFailError

class RunTestCase(BaseActionTestCase):
    """
    Test action and pack configuration
    """
    action_cls = ActionManager

    def test_missing_parameter(self):
        """
        Test that MissingParameterError is raised when a required parameter is
        not specified.
        """
        configs = {
            "action_config": {
                "config_profile": None
            },
            "pack_config": {}
        }
        # Action should have required 'action' parameter
        action = self.get_action_instance(config=configs["pack_config"])
        with self.assertRaises(MissingParameterError):
            action.run(**configs["action_config"])

    def test_missing_profile(self):
        """
        Test that MissingProfileError is raised when a non-existent profile is
        specified.
        """
        # Profile should exist, if defined in action
        configs = {
            "action_config": {
                "action": "some_action",
                "url": "http://localhost:8080",
                "config_profile": "dev"
            },
            "pack_config": {}
        }
        action = self.get_action_instance(config=configs["pack_config"])
        # Unsure why this works when assertRaises(MissingProfileError) doesn't
        with self.assertRaises(Exception) as exc:
            action.run(**configs["action_config"])

            self.assertIsInstance(exc, MissingProfileError)

    def test_validation_failure(self):
        """
        Test that ValidationFailError is raised when required connection
        parameters are not configured.
        """
        # Required connection parameters should exist: url, user, password
        configs = {
            "action_config": {
                "action": "some_action",
                "url": "http://localhost:8080"
            },
            "pack_config": {}
        }
        action = self.get_action_instance(config=configs["pack_config"])
        # Unsure why this works when assertRaises(MissingProfileError) doesn't
        with self.assertRaises(Exception) as exc:
            action.run(**configs["action_config"])

            self.assertIsInstance(exc, ValidationFailError)


import os
import logging
import warnings
import pikaconfig

try:
    from cloghandler import ConcurrentRotatingFileHandler as RotatingFileHandler
except ImportError:
    warnings.warn("cloghandler not available, will use NON-concurrent log handler")
    from logging.handlers import RotatingFileHandler as OrigRotatingFileHandler

    def RotatingFileHandler(*args, **kwargs):
        # Function for conforming with ConcurrentRotatingFileHandler
        # which accepts a debug parameter.
        kwargs.pop('debug', None)
        return OrigRotatingFileHandler(*args, **kwargs)

USE_GELF = getattr(pikaconfig, 'USE_GELF', False)

def setupLogHandlers(fname, formatter=None, **kwargs):
    """
    Create a RotatingFileHandler to be used by a logger, and possibly a
    GELFHandler.

    By default the RotatingFileHandler stores 100 MB before starting a new
    log file and the last 10 log files are kept. The default formatter shows
    the logging level, current time, the function that created the log entry,
    and the specified message.

    :param str fname: path to the filename where logs will be written to
    :param logging.Formatter formatter: a custom formatter for this logger
    :param kwargs: custom parameters for the RotatingFileHandler
    :rtype: tuple
    """
    if formatter is None:
        formatter = logging.Formatter('%(levelname)s [%(asctime)s] %(funcName)s: %(message)s')

    opts = {'maxBytes': 100 * 1024 * 1024, 'backupCount': 10, 'debug': False}
    opts.update(kwargs)
    handler = RotatingFileHandler(os.path.join(pikaconfig.LOG_DIR, fname), **opts)
    handler.setFormatter(formatter)

    handlers = (handler, )
    if USE_GELF:
        gelf_handler = GELFHandler(**USE_GELF)
        gelf_handler.setLevel(logging.INFO)  # Ignore DEBUG messages.
        handlers += (gelf_handler, )

    return handlers


import numpy as np  # 加载numpy模块并取名为np
import matplotlib.pyplot as plt  # 加载matplotlib.pyplot并取名为plt

x = np.arange(-10, 12, 0.01)   # x在-10到12之间，每隔0.01取一个点
y = x ** 2 - 2 * x + 1         # 通过解析式计算列表x对应的列表y的值

plt.plot(x, y)
plt.xlabel('X')  # 设置X轴标题
plt.ylabel('Y')  # 设置Y轴标题
plt.show()       # 将绘制的函数图像窗口显示出来

第2学期/【第22课】走近数据分析②/实践④练习_函数图像绘制.py

实践④练习_函数图像绘制.py


from selenium import webdriver
from selenium.webdriver.common.by import By
import time

def iframe_switch():
    baseurl = "https://learn.letskodeit.com/"
    driver = webdriver.Firefox()
    driver.maximize_window()
    driver.get(baseurl)
    driver.implicitly_wait(10)

    practice_page = driver.find_element_by_link_text("Practice")
    practice_page.click()
    element = driver.find_element(By.ID, "courses-iframe")
    element.location_once_scrolled_into_view
    time.sleep(3)

    # Switch to iframe using ID by giving the ID of the frame as argument
    #driver.switch_to.frame("courses-iframe")

    # Switch to iframe using name by giving the name of the frame as argument
    #driver.switch_to.frame("iframe-name")

    # Switch to iframe using the number(which ifrome in the page to click, here 0)
    driver.switch_to.frame(0)

    # Perform some action in the frame window
    driver.find_element(By.ID, "search-courses").send_keys("python")
    time.sleep(3)

    # Switch back to main window
    driver.switch_to.default_content()
    driver.execute_script("window.scrollBy(0, -1000);")
    time.sleep(3)
    driver.find_element(By.ID, "name").send_keys("testing done")
    time.sleep(3)

    driver.quit()

iframe_switch()


#!/usr/bin/python3
#-*- coding: utf-8 -*-

contentdir = '../static/'

def makepage(*content):
	insert = ''
	page = ''
	for con in content:
		con = open(contentdir+con, 'r')
		insert = insert + con.read()
	template = open(contentdir+'template', 'r')
	stylesheet = open(contentdir+'style.css', 'r')
	for line in  template.readlines():
		if line == '%content%\n':
			line = insert
		if line == '%stylesheet%\n':
			line = stylesheet.read()
		page = page+line
	con.close()
	template.close()
	stylesheet.close()
	return page

def makequestion(q, ans):
	content = "

%s. %s
\n" % (q[0], q[1])
	if q[2] == 1:
		num = 0
		for a in ans:
			num = num + 1
			content = content + "<input type='radio' name='ans%s' value='%s'>%s
\n" % (a[1], num, a[2])
	if q[2] == 2:
		num = 0
		for a in ans:
			num = num + 1
			content = content + "<input type='checkbox' name='ans%s' value='%s'>%s
\n" % (a[1], num, a[2])
	if q[2] == 3:
		content = content + "
Введите ответ: <input type='text' name='ans%s'>
\n" % q[0]
	return content

if __name__=='__main__':
	print (makepage('newuser', 'continue'))


import atexit
import math
import random

import hydra
import numpy as np
import pytorch_lightning as pl
import pytorch_lightning.callbacks as callbacks
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.parallel
import torch.optim as optim
import torch.optim.lr_scheduler as lr_scheduler
import torch.utils.data
import torch.utils.data.distributed
import wandb
from hydra.core.hydra_config import HydraConfig
from omegaconf import DictConfig, OmegaConf
from pytorch_lightning.core import LightningModule
from pytorch_lightning.loggers import WandbLogger
from torchmetrics.classification.accuracy import Accuracy
from torchmetrics.classification.confusion_matrix import ConfusionMatrix
from torchvision.models import resnet50
from torchvision.utils import make_grid

from datasets.utils import get_dm_class
from models.wide_resnet import WideResNet
from utils.checkpoints import checkpoint_chimera, checkpoint_cls
from utils.eta_callback import ETACallback
from utils.normalize_inverse import NormalizeInverse
from utils.tensor_utils import linear_interpolation, resize_to
from utils.transforms import get_test_transforms, get_train_transforms

class ClassifierLightningModel(LightningModule):
    def __init__(self, image_size, num_classes, mean, std, module_mix, **hparams):
        super().__init__()

        self.params = DictConfig(hparams)

        mix_modes = {
            "image_a": 1.0,
            "mix_generator": 0.0,
            "mixup": 0.0,
        }

        if self.params.mixup:
            mix_modes["image_a"] = 0.0
            mix_modes["mixup"] = 1.0

        if module_mix:
            self.params.mix_size = module_mix.params.mix_size
            self.params.noise_interpolation_mode = (
                module_mix.params.noise_interpolation_mode
            )
            mix_modes["mix_generator"] = 1.0

        self.params.mix_modes = mix_modes
        self.save_hyperparameters()

        self.image_size = image_size
        self.num_classes = num_classes
        self.mean = mean
        self.std = std

        self.module_mix = module_mix

        self.mix_mode_keys = list(mix_modes.keys())
        self.mix_mode_probs = np.array(list(mix_modes.values()))
        self.mix_mode_probs = self.mix_mode_probs / np.sum(self.mix_mode_probs)
        print(self.mix_mode_keys)
        print(self.mix_mode_probs)

        if self.params.model == "wideresnet":
            model = WideResNet(
                depth=28, num_classes=self.num_classes, widen_factor=10, dropRate=0.3
            )
        elif self.params.model == "wideresnet_16_8":
            model = WideResNet(
                depth=16, num_classes=self.num_classes, widen_factor=8, dropRate=0.3
            )
        elif self.params.model == "resnet50":
            model = resnet50(pretrained=self.params.pretrained)
            num_ftrs = model.fc.in_features
            model.fc = nn.Linear(num_ftrs, self.num_classes)
        else:
            raise ValueError("unknown model", self.params.model)

        self.model = model

        self.criterion = nn.CrossEntropyLoss()

        self.train_accuracy = Accuracy()
        self.train_accuracy_macro = Accuracy(average="macro", num_classes=num_classes)
        self.train_accuracy_top5 = Accuracy(top_k=5)
        self.val_accuracy = Accuracy()
        self.val_accuracy_macro = Accuracy(average="macro", num_classes=num_classes)
        self.val_accuracy_top5 = Accuracy(top_k=5)
        self.val_confusion = ConfusionMatrix(num_classes=num_classes)

        self.normalize_inv = NormalizeInverse(self.mean, self.std)

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

    def on_epoch_start(self) -> None:

        if self.current_epoch % 1 == 0:
            self.log_next_batch = True
            self.log_next_cam = True
        self.val_confusion.reset()

    def training_step(self, batch, batch_idx):

        dict_a, dict_b = batch
        idx_a, images_a, target_a = dict_a["idx"], dict_a["image"], dict_a["target"]
        idx_b, images_b, target_b = dict_b["idx"], dict_b["image"], dict_b["target"]
        if self.params.mix_mode == "segmentation":
            segmentation_a = dict_a["segmentation"]
            segmentation_b = dict_b["segmentation"]

        target_a = target_a.squeeze()
        bs = images_a.size(0)

        mix_mode = np.random.choice(self.mix_mode_keys, p=self.mix_mode_probs)

        if mix_mode == "mixup":
            alpha = 1.0
            f = np.random.beta(alpha, alpha)
            f = torch.tensor([f]).to(self.device)

            index = torch.randperm(bs).to(self.device)

            f = f.view(1, 1, 1, 1)
            images_mix = linear_interpolation(images_a, images_a[index], f=f)
            images_b = images_a[index]
            target_b = target_a[index]
        elif mix_mode == "mix_generator":
            self.module_mix.eval()
            if self.params.mix_discriminator_filter_repeats > 1:
                target_a_oh = F.one_hot(target_a, num_classes=self.num_classes).float()
                if self.params.mix_mode == "segmentation":
                    noise = self.module_mix.gen_noise(
                        bs * self.params.mix_discriminator_filter_repeats,
                        segmentation_a.tile(
                            self.params.mix_discriminator_filter_repeats, 1, 1
                        ),
                        segmentation_b.tile(
                            self.params.mix_discriminator_filter_repeats, 1, 1
                        ),
                    )
                else:
                    noise = self.module_mix.gen_noise(
                        bs * self.params.mix_discriminator_filter_repeats
                    )
                images_mix, _, _, _ = self.module_mix(
                    images_a.tile(
                        self.params.mix_discriminator_filter_repeats, 1, 1, 1
                    ),
                    images_b.tile(
                        self.params.mix_discriminator_filter_repeats, 1, 1, 1
                    ),
                    noise,
                )
                disc_probs = (
                    self.module_mix.netD(images_mix, target_a_oh)
                    .squeeze()
                    .mean(dim=[-1, -2])
                )
                disc_probs_argmin = torch.argmin(
                    disc_probs.view(self.params.mix_discriminator_filter_repeats, bs),
                    dim=0,
                )
                images_mix = images_mix.view(
                    self.params.mix_discriminator_filter_repeats,
                    bs,
                    *images_mix.shape[1:],
                )
                images_mix = images_mix[disc_probs_argmin, torch.arange(bs)]
            else:
                images_a_resized = resize_to(
                    images_a, target_size=self.module_mix.image_size
                )
                images_b_resized = resize_to(
                    images_b, target_size=self.module_mix.image_size
                )

                f_a = self.module_mix.generator.encode(images_a_resized)
                f_b = self.module_mix.generator.encode(images_b_resized)

                if self.params.mix_mode == "segmentation":
                    segmentation_a_resized = resize_to(
                        segmentation_a,
                        target_size=self.module_mix.image_size,
                        mode="nearest",
                        add_channel=True,
                    )
                    segmentation_b_resized = resize_to(
                        segmentation_b,
                        target_size=self.module_mix.image_size,
                        mode="nearest",
                        add_channel=True,
                    )

                    mask = self.module_mix.gen_noise(
                        bs,
                        segmentation_a=segmentation_a_resized,
                        segmentation_b=segmentation_b_resized,
                        resize=not self.params.mix_direct,
                    )
                else:
                    mask = self.module_mix.gen_noise(
                        bs, resize=not self.params.mix_direct
                    )

                if self.params.mix_direct:
                    mask_a = mask[:, 0, ...][:, None]
                    mask_b = mask[:, 1, ...][:, None]

                    images_mix = mask_a * images_a_resized + mask_b * images_b_resized
                else:
                    images_mix, _, _, _ = self.module_mix.generator.mixDecode(
                        f_a, f_b, mask
                    )

                images_mix = resize_to(images_mix, target_size=self.image_size)

        elif mix_mode == "image_a":
            images_mix = images_a
        else:
            raise ValueError("unknown mix mode", mix_mode)

        target = target_a

        output = self.model(images_mix)

        if mix_mode == "mixup":
            loss = (1 - f) * self.criterion(output, target_a) + f * self.criterion(
                output, target_b
            )
        else:
            loss = self.criterion(output, target)

        self.log(
            "train_loss", loss, on_step=True, on_epoch=True, prog_bar=True, logger=True
        )

        if mix_mode not in ["mixup"]:
            self.train_accuracy(F.softmax(output, dim=1), target)
            self.train_accuracy_macro(F.softmax(output, dim=1), target)
            self.train_accuracy_top5(F.softmax(output, dim=1), target)

            self.log("train_acc", self.train_accuracy, on_step=True, on_epoch=True)
            self.log(
                "train_acc_macro",
                self.train_accuracy_macro,
                on_step=True,
                on_epoch=True,
            )
            self.log(
                "train_acc_top5", self.train_accuracy_top5, on_step=False, on_epoch=True
            )

        if self.log_next_batch:
            n = 8
            images_a = images_a[:n]
            images_mix = images_mix[:n]

            image_list = [images_a]
            if images_b is not None:
                images_b = images_b[:n]
                image_list.append(images_b)

            image_list.append(images_mix)

            data = torch.cat(image_list, dim=0)
            data = self.normalize_inv(data)

            self.logger.experiment.log(
                {
                    "batch": wandb.Image(
                        make_grid(data.float(), normalize=False, nrow=8,), mode="RGB",
                    ),
                },
                commit=False,
            )
            self.log_next_batch = False

        return loss

    def validation_step(self, batch, batch_idx):
        images = batch["image"]
        target = batch["target"]

        output = self.model(images)

        loss_val = self.criterion(output, target)

        self.val_accuracy(F.softmax(output, dim=1), target)
        self.val_accuracy_macro(F.softmax(output, dim=1), target)
        self.val_accuracy_top5(F.softmax(output, dim=1), target)
        self.val_confusion(F.softmax(output, dim=1), target)

        self.log(
            "val_loss",
            loss_val,
            on_step=False,
            on_epoch=True,
            prog_bar=True,
            logger=True,
        )
        self.log("val_acc", self.val_accuracy, on_step=False, on_epoch=True)
        self.log("val_acc_macro", self.val_accuracy_macro, on_step=False, on_epoch=True)
        self.log("val_acc_top5", self.val_accuracy_top5, on_step=False, on_epoch=True)

    def configure_optimizers(self):
        optimizer = optim.SGD(
            self.model.parameters(),
            lr=self.params.lr,
            momentum=self.params.momentum,
            weight_decay=self.params.wd,
            nesterov=True,
        )

        steps_per_epoch = len(self.trainer.datamodule.train_dataloader())
        print("steps_per_epoch", steps_per_epoch)

        if self.params.lr_scheduler == "multi_step_lr":
            milestones = np.array([60, 120, 160])
            milestones *= steps_per_epoch
            milestones *= self.params.num_epoch_repetition
            scheduler = lr_scheduler.MultiStepLR(optimizer, milestones, gamma=0.1)
        elif self.params.lr_scheduler == "step_lr":
            scheduler = lr_scheduler.StepLR(
                optimizer,
                step_size=2 * self.params.num_epoch_repetition * steps_per_epoch,
                gamma=0.9,
            )
        elif self.params.lr_scheduler == "CosineAnnealingLR":
            total_nb_epochs = self.params.num_epoch_repetition * self.params.epochs
            scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
                optimizer, T_max=total_nb_epochs * steps_per_epoch
            )
        else:
            raise ValueError("unknown lr_scheduler", self.params.lr_scheduler)
        return [optimizer], [{"scheduler": scheduler, "interval": "step"}]

@hydra.main(config_path="configs/cls", config_name="base", version_base="1.1")
def main(cfg: DictConfig):

    cfg.epochs = math.ceil(cfg.epochs)

    if isinstance(cfg.tags, str):
        cfg.tags = [cfg.tags]

    print(OmegaConf.to_yaml(cfg))
    print(HydraConfig.get().job.override_dirname)

    if cfg.seed is not None:
        random.seed(cfg.seed)
        np.random.seed(cfg.seed)
        torch.manual_seed(cfg.seed)

    wandb_logger = WandbLogger(
        project="chimeramix-classifier",
        tags=cfg.tags,
        save_dir=hydra.utils.to_absolute_path(""),
        config=OmegaConf.to_container(cfg, resolve=True, enum_to_str=True),
    )
    trainer_callbacks = [
        callbacks.LearningRateMonitor(logging_interval="step"),
        ETACallback(epochs=cfg.epochs),
    ]

    if cfg.checkpoint:
        checkpoint_path = checkpoint_cls(
            model_subdir=cfg.model_subdir,
            dataset=cfg.dataset,
            max_labels_per_class=cfg.max_labels_per_class,
            seed=cfg.seed,
            mix=cfg.mix,
            generator_features=cfg.mix_generator_features,
            generator_blocks=cfg.mix_generator_blocks,
            generator_split=cfg.mix_generator_split,
            discriminator_features=cfg.mix_discriminator_features,
            image_size=cfg.mix_generator_image_size,
            mix_mode=cfg.mix_mode,
            mix_size=cfg.mix_size,
            variant=cfg.mix_variant,
        )

        checkpoint_cb = callbacks.ModelCheckpoint(
            dirpath=hydra.utils.to_absolute_path(checkpoint_path),
            save_last=True,
            every_n_epochs=cfg.epochs * cfg.num_epoch_repetition // 2,
        )
        trainer_callbacks.append(checkpoint_cb)
        print("checkpoint", checkpoint_path)

    trainer = pl.Trainer(
        logger=wandb_logger,
        default_root_dir="tmp/lightning_logs",
        gpus=1,
        max_epochs=cfg.epochs * cfg.num_epoch_repetition,
        check_val_every_n_epoch=cfg.num_epoch_repetition,
        callbacks=trainer_callbacks,
        fast_dev_run=cfg.debug,
        enable_checkpointing=cfg.checkpoint,
        precision=16,
    )

    dm_class = get_dm_class(cfg)

    num_dataset_repeats = (
        max(cfg.num_dataset_repeats_base // int(cfg.max_labels_per_class), 1)
        if cfg.max_labels_per_class is not None
        else None
    )

    mean = (0.5, 0.5, 0.5)
    std = (0.5, 0.5, 0.5)

    transforms_train, metadata = get_train_transforms(
        dataset=cfg.dataset,
        mean=mean,
        std=std,
        aug_cutout=cfg.augmentation_cutout,
        aug_random_erase=cfg.augmentation_random_erase,
        aug_autoaugment=cfg.augmentation_autoaugment,
        aug_trivialaugment=cfg.augmentation_trivialaugment,
    )
    transforms_test = get_test_transforms(dataset=cfg.dataset, mean=mean, std=std)

    dm = dm_class(
        batch_size=cfg.batch_size,
        max_labels_per_class=cfg.max_labels_per_class,
        max_labels_per_class_seed=cfg.seed,
        num_train_dataset_repeats=num_dataset_repeats,
        pairs_train=True,
        transforms_train=transforms_train,
        transforms_test=transforms_test,
        index_train=True,
        segmentation=cfg.mix_mode == "segmentation",
        data_path=hydra.utils.to_absolute_path("tmp/data"),
        train_workers=8,
        val_workers=8,
    )

    module_mix = None

    if cfg.mix:
        from train_generator import ChimeraMixLightningModel

        checkpoint_path = checkpoint_chimera(
            model_subdir=cfg.model_generator_subdir,
            dataset=cfg.dataset,
            max_labels_per_class=cfg.max_labels_per_class,
            seed=cfg.seed,
            generator_features=cfg.mix_generator_features,
            generator_blocks=cfg.mix_generator_blocks,
            generator_split=cfg.mix_generator_split,
            discriminator_features=cfg.mix_discriminator_features,
            image_size=cfg.mix_generator_image_size,
            mix_mode=cfg.mix_mode,
            mix_size=cfg.mix_size,
            variant=cfg.mix_variant,
            add_file="last.ckpt",
        )
        module_mix = ChimeraMixLightningModel.load_from_checkpoint(
            hydra.utils.to_absolute_path(checkpoint_path)
        )

    model = ClassifierLightningModel(
        image_size=metadata["image_size"],
        num_classes=dm.num_classes,
        mean=mean,
        std=std,
        module_mix=module_mix,
        **cfg,
    )

    trainer.fit(model, datamodule=dm)

    return 0

if __name__ == "__main__":
    atexit.register(lambda: print("\x1b[?25h"))
    main()


from nltk.tokenize import *
import nltk.data
from nltk.corpus import inaugural
from nltk.corpus import webtext
from nltk import FreqDist
from nltk.corpus import swadesh
from nltk.corpus import stopwords
from pathlib import Path
from nltk.tokenize import sent_tokenize

class Preprocessor(object):

    def __init__(self, text, text_id):
        """ Reads the file text as a string """

        self.text = text
        self.text_id = text_id

    @classmethod
    def from_nltk_book(cls):
        """ Reads the inaugural text from the nltk book as a string. Note that this is a @classmethod
        which will be used instead of the default constructor when creating the object. Remember to
        return cls(something) """  # -> ok

        return cls(
            ' '.join(
                [
                    inaugural.raw(fileid)
                    for fileid in inaugural.fileids()
                ]
            ),
            'inaugural'
        )

    @classmethod
    def from_text_file(cls, path, text_id):
        """ Reads the text from given file as a string. Note that this is a @classmethod
        which will be used instead of the default constructor when creating the object.
        Remember to return cls(something) """  # -> ok

        with open(path, 'r') as f:
            text = f.read()

        return cls(text, text_id)

    def get_no_of_sents(self):
        """ Split the input text into sentences and return the length of the text in sentences. """  # -> ok

        sent_detector = nltk.data.load(
            (Path('tokenizers') / 'punkt' / 'english.pickle').
            __str__()
        )

        return len(sent_detector.tokenize(
            self.text.strip()
        )
        )

    def get_no_of_words(self):
        """ Split the input text into words and return the length of the text in words. """  # No need to implement this method

        ...

        return

    def tokenize_text(self):
        """ Split the text into tokens. Use an nltk method for that, equivalent to s.split() """  # -> ok

        return word_tokenize(self.text)

    def lemmatize_token(self, token):
        """ Lemmatize the given token, after converting it to lowercase. """  # Not tested

        return nltk.WordNetLemmatizer().lemmatize(
            token.lower()
        )

    def get_20_most_frequent_words(self):
        """ Return the 20 most frequent words of the text after removing the stopwords and all non-alphanumeric characters
         (in other words, remove all punctuation). Note that the stopwords are stored with their lemmata so you
         will have to make sure you check whether the lemma of each word is contained in the stopwords list.
         Make sure you use the method tokenizeText() that you implemented above """  # -> ok

        stop_words = stopwords.words('english')
        words = self.tokenize_text()

        lemmas: list = []
        for i in range(len(words)):

            lemma = self.lemmatize_token(words[i])
            if lemma not in stop_words and lemma.isalpha():
                lemmas.append(words[i])

        return [
            f[0]
            for f in FreqDist(lemmas).
            most_common(20)
        ]

    def get_originality_score(self, most_freq_words):
        """ Return the originality score of a text. This score can be measured by counting how many words contained
        in the swadesh list of nltk are also contained in the list with the 20 most frequent words of the text.
        For example, if 10 words can be found in both lists, the originality score is 10.
        The list with the 20 most frequent words should be given to the method as a parameter. """  # -> ok

        swadesh_words = swadesh.words('en')

        return len(
            [
                word
                for word in most_freq_words
                if word in swadesh_words
            ]
        )

if __name__ == '__main__':
    """ Create two instances of the Preprocessor class. One instance should be created through the text file
    ada_lovelace.txt and the other one through the inaugural text included in the nltk book. """

    """ For each instance of the Preprocessor, do the following:
      a) print out the numbers of sentences of the text
      b) print out the 20 most frequent words of the text
      c) print out the originality score of the text. """

    # Optional: No need to implement this part
    ...


import os
import cv2
import time
import logging
import numpy as np
import sophon.sail as sail
from datetime import datetime
import argparse
from numpy.lib.stride_tricks import as_strided
from ctdet import CtdetDetector
from debugger import Debugger

BASE_DIR = os.path.dirname(os.path.join(os.getcwd(), __file__))
DATA_DIR = os.path.join(os.path.dirname(BASE_DIR), 'data/')

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Demo for CenterNet")
    parser.add_argument('--input',  default=os.path.join(DATA_DIR, 'ctdet_test.jpg'), required=False)
    parser.add_argument('--loops',  default=1,  type=int, required=False)
    parser.add_argument('--tpu_id', default=0,  type=int, required=False)
    parser.add_argument('--bmodel', default=os.path.join(DATA_DIR, 'models/ctdet_coco_dlav0_1output_512_fp32_1batch.bmodel'), type=str, required=False)
    parser.add_argument('--class_path', default=os.path.join(DATA_DIR, 'coco_classes.txt'), type=str, required=False)

    opt = parser.parse_args()

    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s%(msecs)03d[%(levelname)s][%(module)s:%(lineno)s] - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S.')
    logging.info('Start centernet detector sail demo.')

    # Initialize centernet detector instance
    cet_detector = CtdetDetector(
        arch='dlav0', model_path=opt.bmodel, tpu_id=opt.tpu_id, class_path=opt.class_path
    )

    batch_size = cet_detector.get_batchsize()
    logging.info("Input model batch size is {}".format(batch_size))

    input_path        = opt.input
    decoder           = sail.Decoder(input_path, True, opt.tpu_id)
    process_handle    = cet_detector.get_handle()

    for idx in range(opt.loops):
        logging.info('loop start')
        image_ost_list = []

        if batch_size == 1:
            img = sail.BMImage()
            ret = decoder.read(process_handle, img)
            if ret != 0:
                logging.warning('decoder read image failed!')
                continue
            dst_img = cet_detector.bmcv.convert_format(img)
            image_ost_list.append(dst_img)
            logging.info('input format {}'.format(dst_img.format()))
            results = cet_detector.run(dst_img)

        elif batch_size == 4:
            #img = sail.BMImageArray4D()
            for i in range(4):
                rgb_img = cet_detector.bmcv.convert_format(decoder.read(process_handle))
                image_ost_list.append(rgb_img)
                #img[i] = rgb_img.data()
            results = cet_detector.run(image_ost_list)

        else:
            raise NotImplementedError(
                'This demo not supports inference with batch size {}'.format(cet_detector.get_batchsize()))

        debugger = Debugger()
        for b in range(len(results)):
            if results[b] is None:
                logging.info('batch {} detect nothing'.format(b + 1))
                continue
            cv_img    = cet_detector.bmimage_to_cvmat(image_ost_list[b])
            top_label = np.array(results[b][:, 5], dtype='int32')
            top_conf  = results[b][:, 4]
            top_boxes = results[b][:, :4]

            #---------------------------------------------------------#
            #   图像绘制
            #---------------------------------------------------------#
            for i, c in list(enumerate(top_label)):
                predicted_class = cet_detector.class_names[int(c)]
                box             = top_boxes[i]
                score           = top_conf[i]

                top, left, bottom, right = box

                top     = max(0, np.floor(top).astype('int32'))
                left    = max(0, np.floor(left).astype('int32'))
                bottom  = min(image_ost_list[b].height(), np.floor(bottom).astype('int32'))
                right   = min(image_ost_list[b].width(),  np.floor(right).astype('int32'))

                logging.info('[object]:{} -> label {}, top {}, left {}, bottom {}, right {}'.format(score, predicted_class, top, left, bottom, right))
                debugger.add_coco_bbox(cv_img, (left, top, right, bottom), c, score)
                #cet_detector.bmcv.rectangle(image_ost_list[b], left, top, right - left, bottom - top, (255, 0, 0), 3)

            # draw result
            det_filename = 'ctdet_result_{}_b_{}.jpg'.format(datetime.now().strftime('%Y-%m-%d-%H-%M-%S'), b)
            #cet_detector.bmcv.imwrite(det_filename, image_ost_list[b])
            cv2.imwrite(det_filename, cv_img)
            logging.info('Prediction result: {}'.format(det_filename))

    # exit
    logging.info('Demo exit..')