import os
import zipfile
from pathlib import Path
import warnings
from shutil import rmtree
import time

import pandas as pd
import numpy as np
import SimpleITK as sitk

from utils import compute_scores, get_np_volume_from_sitk, resample

class HecktorEvaluator:
    def __init__(self,
                 ground_truth_folder,
                 bounding_boxes_filepath,
                 extraction_folder="data/extraction/",
                 round_number=1):
        """Evaluator for the Hecktor Challenge

        Args:
            ground_truth_folder (str): the path to the folder
                                       containing the ground truth segmentation.
            bounding_boxes_filepath (str): the path to the csv file which defines
                                           the bounding boxes for each patient.
            extraction_folder (str, optional): the path to the folder where the
                                               extraction of the .zip submission
                                               will take place. Defaults to "data/tmp/".
                                               This folder has to be created beforehand.
            round_number (int, optional): the round number. Defaults to 1.
        """
        self.groud_truth_folder = Path(ground_truth_folder)
        self.round = round_number
        self.extraction_folder = Path(extraction_folder)
        self.bounding_boxes_filepath = Path(bounding_boxes_filepath)

    def _evaluate(self, client_payload, _context={}):
        """
        `client_payload` will be a dict with (atleast) the following keys :
            - submission_file_path : local file path of the submitted file
            - aicrowd_submission_id : A unique id representing the submission
            - aicrowd_participant_id : A unique id for participant/team submitting (if enabled)
        """
        submission_file_path = client_payload["submission_file_path"]
        aicrowd_submission_id = client_payload["aicrowd_submission_id"]
        aicrowd_participant_uid = client_payload["aicrowd_participant_id"]
        submission_extraction_folder = self.extraction_folder / (
            'submission' + str(aicrowd_submission_id) + '/')

        submission_extraction_folder.mkdir(parents=True, exist_ok=True)
        with zipfile.ZipFile(str(Path(submission_file_path).resolve()),
                             "r") as zip_ref:
            zip_ref.extractall(str(submission_extraction_folder.resolve()))

        groundtruth_paths = [
            f for f in self.groud_truth_folder.rglob("*.nii.gz")
        ]
        bb_df = pd.read_csv(str(
            self.bounding_boxes_filepath.resolve())).set_index("PatientID")

        score_columns = ["dice_score", "sensitivity", "precision"]
        results_df = pd.DataFrame(columns=score_columns)

        missing_patients = list()
        unresampled_patients = list()
        for path in groundtruth_paths:
            patient_id = path.name.split(".")[0]
            prediction_files = [
                f for f in submission_extraction_folder.rglob(patient_id +
                                                              "*.nii.gz")
            ]
            if len(prediction_files) > 1:
                raise Exception(
                    "There is too many prediction files for patient {}".format(
                        patient_id))
            elif len(prediction_files) == 0:
                results_df = results_df.append(
                    {
                        "dice_score": 0,
                        "sensitivity": 0,
                        "precision": 0,
                    },
                    ignore_index=True)

                missing_patients.append(patient_id)
                continue

            sitk_image_gt = sitk.ReadImage(str(path.resolve()))
            sitk_image_pred = sitk.ReadImage(str(
                prediction_files[0].resolve()))

            np_volume_pred, px_spacing_pred, origin_pred = get_np_volume_from_sitk(
                sitk_image_pred)
            np_volume_gt, px_spacing_gt, origin_gt = get_np_volume_from_sitk(
                sitk_image_gt)

            if np.sum(np.where((np_volume_pred != 0) & (np_volume_pred != 1))) != 0:
                raise Exception(
                    "The mask provided contain other values than 0 or 1, please provide binary masks")
            np_volume_pred = np_volume_pred != 0
            np_volume_gt = np_volume_gt != 0

            bb = (bb_df.loc[patient_id, "x1"], bb_df.loc[patient_id, "y1"],
                  bb_df.loc[patient_id, "z1"], bb_df.loc[patient_id, "x2"],
                  bb_df.loc[patient_id, "y2"], bb_df.loc[patient_id, "z2"])

            if sitk_image_gt.GetSpacing() != sitk_image_pred.GetSpacing():
                unresampled_patients.append(patient_id)

            # Crop to the bonding box and/or resample to the original spacing
            np_volume_pred = resample(np_volume_pred, origin_pred,
                                      px_spacing_pred, px_spacing_gt, bb)
            np_volume_gt = resample(np_volume_gt, origin_gt, px_spacing_gt,
                                    px_spacing_gt, bb)

            dice_score, sensitivity, precision = compute_scores(
                np_volume_gt, np_volume_pred)
            results_df = results_df.append(
                {
                    "dice_score": dice_score,
                    "sensitivity": sensitivity,
                    "precision": precision,
                },
                ignore_index=True)

        _result_object = dict(results_df.mean())

        rmtree(str(submission_extraction_folder.resolve()))
        messages = list()
        if len(unresampled_patients) > 0:
            messages.append(
                "The following patient(s) was/were not resampled back"
                " to the original resolution: {patients}."
                "\nWe applied a nearest neighbor resampling.\n".format(
                    patients=unresampled_patients))

        if len(missing_patients) > 0:
            messages.append(
                "The following patient(s) was/were missing: {patients}."
                "\nA score of 0 was attributed to them".format(
                    patients=missing_patients))
        _result_object["message"] = "".join(messages)
        return _result_object

if __name__ == "__main__":
    ground_truth_folder = "data/ground_truth/"
    bounding_boxes_file = "data/bboxes.csv"
    _client_payload = {}
    _client_payload[
        "submission_file_path"] = "data/sample_submission.zip"
    _client_payload["aicrowd_submission_id"] = 1123
    _client_payload["aicrowd_participant_id"] = 1234

    # Instantiate a dummy context
    _context = {}
    # Instantiate an evaluator
    aicrowd_evaluator = HecktorEvaluator(ground_truth_folder,
                                         bounding_boxes_file)
    # Evaluate
    start = time.process_time()
    result = aicrowd_evaluator._evaluate(_client_payload, _context)
    print("Time to compute the sample {}".format(time.process_time() - start))
    print(result)
    print(result["message"])


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

pd.set_option('display.width',2000)

# 绘制相关系数的条形图
def draw_corr(name):
    pit = corr[name]
    x = np.arange(len(pit.index))
    t = plt.figure(1, (25, 7))
    plt.bar(x, pit.values)
    plt.title('corr_'+name)
    plt.ylabel('corr')
    plt.xticks([])
    for t in range(len(pit.index)):
        plt.annotate(pit.index[t], xytext=(-0.5 + t, pit.values[t]), xy=(0, 0.9))
    plt.savefig(r'../piture/corr/'+name)
    plt.show()

if __name__=='__main__':

    data=pd.read_csv(r'../analyed_data/data_user_balance.csv',index_col='report_date')
    # 相关系数
    corr=data.corr()
    # 消费总量相关系数
    for name in data.columns:
        draw_corr(name)


from sklearn.model_selection import train_test_split

def spliting(df):
    x = df.drop(['Live Birth Occurrence'], axis = 1)
    y = df['Live Birth Occurrence'].astype(int)

    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=6)
    x_test, x_valid, y_test, y_valid = train_test_split(x_test, y_test, test_size=0.2, random_state=6)

    return x_train, x_test, x_valid, y_train, y_test, y_valid


import math
from typing import List

class Solution:
    # 定义骰子最大点数
    g_maxValue = 6

    # 方法一：基于递归求骰子点数，时间效率不够高
    def twoSum(self, n: int) -> List[float]:

        if n < 1:
            return []

        # 定义n个骰子的最大点数
        maxSum = n * self.g_maxValue

        # 所有可能的值出现的次数保存在列表中
        pProbabilities = [0] * (maxSum - n + 1)

        # 递归中的第一个骰子有6种情况
        for i in range(1, self.g_maxValue + 1):
            # 递归剩余的（n-1）个骰子
            self.getProbability(n, n, i, pProbabilities)

        # n个骰子的所有点数的排列数为6的n次方
        total = math.pow(self.g_maxValue, n)

        res = list(map(lambda x: x / total, pProbabilities))
        return list(map(lambda x: round(x, 5), res))

    def getProbability(self, original, current, sum, pProbabilities):
        '''
        :param original: 表示n个骰子仍在地上的n
        :param current: 剩余需要递归的骰子数
        :param sum: 骰子点数相加的数
        :param pProbabilities:
        :return:
        '''
        # 递归结束条件
        if current == 1:
            pProbabilities[sum - original] += 1
        else:
            for i in range(1, self.g_maxValue + 1):
                self.getProbability(original, current - 1, i + sum, pProbabilities)

    # 方法二：动态规划解法
    def twoSum2(self, n: int) -> List[float]:
        # 初始化二维数组
        dp = [[0 for _ in range(6 * n + 1)] for _ in range(n + 1)]

        # 初始化第一行
        for i in range(1, 7):
            dp[1][i] = 1

        for i in range(2, n + 1):
            for j in range(i, i * 6 + 1):
                for k in range(1, 7):
                    if j >= k + 1:
                        dp[i][j] += dp[i - 1][j - k]

        res = []
        for i in range(n, n * 6 + 1):
            res.append(dp[n][i] * 1.0 / 6 ** n)

        return list(map(lambda x: round(x, 5), res))

if __name__ == "__main__":
    sol = Solution()
    print(sol.twoSum2(2))

Data Structure and Algorithm/剑指offer代码/43. n个骰子的点数/numRollsToTarget.py


import os
import gmplot
import csv
import pandas as pd

api_key = ""

def get_stop(filename):
    df = pd.read_csv(f"csv/imu_raw_{filename}.csv", header=0)
    min_linear_acc_x = df['linear_acc_x'].min()
    row_with_min_linear_acc_x = df[df['linear_acc_x'] == min_linear_acc_x]
    return row_with_min_linear_acc_x.iloc[0]['t']

def read_csv(filename):
    t = []
    lat_data = []
    lon_data = []
    with open(f'csv/gnss_{filename}.csv', 'r') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            t.append(float(row['t']))
            lat_data.append(float(row['latitude']))
            lon_data.append(float(row['longitude']))

    return t, lat_data, lon_data

def plot_gnss_single(latitude_list, longitude_list, filename):
    initial_location = (42.300710, -83.698257)
    # gmap = gmplot.GoogleMapPlotter(initial_location[0], initial_location[1], 18)
    gmap = gmplot.GoogleMapPlotter(initial_location[0], initial_location[1], 18, apikey=api_key)
    gmap.map_type = "satellite"

    gmap.scatter(latitude_list, longitude_list, size=0.1, marker=False, color='blue')
    gmap.draw(f'deliverables/{filename}.html')

def plot_gnss(latitude_list, longitude_list, ref_lat_list, ref_lon_list, filename, t, stop_time):
    initial_location = (42.300710, -83.698257)
    # gmap = gmplot.GoogleMapPlotter(initial_location[0], initial_location[1], 18)
    gmap = gmplot.GoogleMapPlotter(initial_location[0], initial_location[1], 18, apikey=api_key)
    gmap.map_type = "satellite"

    gmap.scatter(ref_lat_list, ref_lon_list, size=0.1, marker=False, color='blue')

    stop_idx = -1
    for i in range(len(t)):
        if t[i] >= stop_time:
            stop_idx = i
            break

    gmap.scatter(latitude_list[:stop_idx], longitude_list[:stop_idx], size=0.1, marker=False, color='green')
    gmap.scatter(latitude_list[stop_idx:], longitude_list[stop_idx:], size=0.1, marker=False, color='red')
    gmap.draw(f'deliverables/{filename}.html')

if __name__ == "__main__":
    bag_files = os.listdir("bags")

    for bag_file in bag_files:
        if bag_file == "path19":
            _, lat_data, lon_data = read_csv(bag_file)
            plot_gnss_single(lat_data, lon_data, bag_file)

    # _, ref_lat_data, ref_lon_data = read_csv('path01_loop2')
    # for bag_file in bag_files:
    #     if bag_file[0:5] == "route":
    #         stop_time = get_stop(bag_file)
    #         t, lat_data, lon_data = read_csv(bag_file)
    #         plot_gnss(lat_data, lon_data, ref_lat_data, ref_lon_data, bag_file, t, stop_time)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  app.py
#
#  Copyleft 2017 Mali Media Group
#  <http://malimedia.be>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################
#
#  CampaignEntries - campaign_entries
#
#  Chalice app to allow for adding of emailadressess/contacts via campaigns.
#  Campaigns can be both internal of external.
#
#  It checks for data-validity, email existence in the db and responds.
#  It saves the provided data as JSON to S3.
#
#############################################################################
#
#   TODO:
#   - improve HTTP status codes in responses
#   - streamline creation of response format
#
#############################################################################

from datetime import datetime
import logging
import json

from chalice import Chalice
from chalice.app import ChaliceError
import boto3
from botocore.client import Config
from botocore.exceptions import ClientError

from chalicelib import DEA_DOMAIN_LIST

app = Chalice(app_name='campaign_entries')
app.debug = True
log = app.log
log.setLevel(logging.DEBUG)

# Create some custom Exceptions
class CustomChaliceError(ChaliceError):
    STATUS_CODE = 201
    def __init__(self, msg=''):
        super(CustomChaliceError, self).__init__(
            self.__class__.__name__ + ': %s' % msg)

class DuplicateEntryError(CustomChaliceError):
    STATUS_CODE = 409

# Initialize the response as a global variable
class Response(object):
    pass

# Initialize the DDB-client per container
ddb_client = boto3.client('dynamodb', region_name='eu-central-1',
                endpoint_url="https://dynamodb.eu-central-1.amazonaws.com")

dynamodb_table_name = 'Emails'

## ---------------------- FUNCTIONS ---------------------- ##

def get_required_fields(campaign):
    return set(campaign['RequiredFields']['SS'])

def has_required_fields(data, campaign):
    required_fields = get_required_fields(campaign)
    missing = required_fields.difference(set(data.keys()))
    log.debug("Missing fields: %s", missing)
    if not missing:
        return {'success': True}
    else:
        data = {"reason": "Missing field(s)"}
        data.update({f: "This field is required" for f in missing})
        return {
            'success': False,
            'response': {
                "status" : "fail",
                "data" : data,
            }
        }

def get_contact_from_dynamodb(email=None):
    response = ddb_client.get_item(
        TableName=dynamodb_table_name,
        Key={'Email': {'S': email}}
    )
    try:
        item = response['Item']
    except KeyError as e:
        log.info('Email not found: "%s".', email)
        return {'success': False}
    else:
        log.info('Email found: "%s".', item)
        return {'success': True,
                'response': item['Email']['S']}

def add_to_dynamodb(email=None, time_stamp=None):
    response = ddb_client.put_item(
        TableName=dynamodb_table_name,
        Item={
            'Email': {'S': email},
            'TimeStamp': {'S': time_stamp}
        }
    )
    log.info('Item added with email "%s", RequestId was: %s.',
             email,
             response['ResponseMetadata']['RequestId'])

def lookup_email_in_simpledb(email):
    sdb_client = boto3.client('sdb')
    response = sdb_client.get_attributes(
        DomainName = dynamodb_table_name,
        ItemName   = email,
    )
    try:
        item = response['Attributes']
    except KeyError as e:
        return {'success': False}
    else:
        return {'success': True,
                'response': item}

def add_contact_to_S3(json_body):
    bucket = 'bdm-events'
    prefix = 'leads/%s/' % json_body['campaign']['CampaignShortName']
    log.debug('Prefix is: %s', prefix)
    s3_client = boto3.client('s3', config=Config(signature_version='s3v4'))
    # Key = email + timestamp 2016-10-31T14:33:58.152256Z (ms precision)
    key = prefix + '_'.join([
        datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
        json_body['data']['email'],
        ]) + '.json'
    log.debug('Key is %s', key)
    func_params = {
        'ACL'   : 'private',
        'Bucket': 'bdm-events',
        #~ 'ServerSideEncryption' : 'AES256'|'aws:kms', TODO: implement data encryption at rest!
        'Key'   : key,
        'ContentType' : 'application/json',
        'ContentEncoding' : 'utf-8',
        'Body' : json.dumps(json_body, indent=4, separators=(',', ': ')),
    }
    try:
        s3_client.put_object(**func_params)
    except Exception as e:
        log.warn(e)
        return False
    else:
        return True

def validate_structure(data, campaign):
    result = has_required_fields(data, campaign)
    if not result['success']:
        return result
    else:
        return {'success': True}

def validate_data(data):
    if data['email'].split('@')[1].lower() in DEA_DOMAIN_LIST:
        return {
            'success': False,
            'response': {
                "status" : "fail",
                "data" : {
                    "lead": "rejected",
                    "reason": "Disposable email address detected: {d}".format(d=data['email'].split('@')[1]),
                    "email": data['email']
                }
            }
        }
    return {'success': True}

def canonicalize(data):
    data['email'] = data['email'].lower().strip()
    return {'success': True}

def lookup_email(data):
    result = get_contact_from_dynamodb(email=data['email'].lower().strip())
    #~ result = lookup_email_in_simpledb(email=data['email'].lower().strip())
    return result

def post_to_S3(data):
    add_contact_to_S3(data)

def handle_data(data, campaign):
    time_stamp = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ")
    # Validate structure
    result = validate_structure(data, campaign)
    if not result['success']:
        return result['response']
    # Validate data
    result = validate_data(data)
    if not result['success']:
        return result['response']
    # Canonicalize
    result = canonicalize(data)
    if not result['success']:
        return result['response']
    # Lookup email
    result = lookup_email(data)
    if result['success']:
        log.info('Duplicate email: %s.', data['email'])
        lead_data = {
            "status" : "fail",
            "data" : {
                "lead": "rejected",
                "reason": "Duplicate contact",
                "email": result['response']
            }
        }
        #~ raise DuplicateEntryError('Duplicate entry: email "%s" already exists.' % data['email'])
    else:
        log.info('All good: %s will be added.', data['email'])
        add_to_dynamodb(email=data['email'].lower().strip(), time_stamp=time_stamp)
        lead_data = {
            "status": "success",
            "data" : {
                "lead": "accepted",
                "email":  data['email'],
            }
        }
    # Post to S3
    post_data = {
        'data': data,
        'lead': lead_data,
        'campaign': {
            'CampaignShortName': campaign['CampaignShortName']['S'],
            'CampaignDecimal'  : campaign['CampaignDecimal']['N'],
        },
        'meta': {
            'time_stamp': time_stamp,
            'context': app.current_request.context,
            'headers': dict(app.current_request.headers)
        }
    }
    post_to_S3(post_data)
    return lead_data

## ---------------------- ROUTES    ---------------------- ##

@app.route('/')
def index():
    return {'hello': 'world'}

@app.route('/contacts', methods=['POST'], api_key_required=True)
def post_contacts():
    """ Main function. Should always get (via POST) a JSON-body as such:
        ```json
        {"data":
            {"firstname": "John",
             "lastname": "Doe",
             "email": "john@example.com",
             "dob": "1990-10-20",
             "source_ip": "123.45.67.89",
            },
        }
        ```
        Ie., one (and only one, for now) Contact
    """
    # Initialize response object
    #~ response = Response()
    # Get the requests payload as JSON
    jb    = app.current_request.json_body
    log.debug('JSON: %s', jb)
    log.debug('Headers: %s', app.current_request.headers)
    campaign_token = app.current_request.headers['x-api-key']
    try:
        data = jb['data']
    except KeyError as e:
        m = 'Invalid JSON structure: no root element "data" provided.'
        log.warn(m)
        return {
            'success': False,
            'response': {
                "status" : "fail",
                "data" : {"data": m}
            }
        }
    # Get Campaign parameters for this token
    response = ddb_client.get_item(
        TableName='EntryCampaigns',
        Key={'CampaignToken': {'S': campaign_token}}
    )
    try:
        campaign = response['Item']
    except KeyError as e:
        # This exception should already have been "caught" by the API Gateway
        log.warn('CampaignToken not found: "%s".', campaign_token)
        raise Exception('CampaignToken not found: "%s".' % campaign_token)
    else:
        log.debug('Campaign is: %s', campaign['CampaignShortName']['S'])
    log.info('Handling record: %s', data)
    response = handle_data(data, campaign)
    return response

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4


# importing the libraries

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

# importing the dataset

data = pd.read_csv('train.csv')
x= data.iloc[ :,1 : -1 ].values
y= data.iloc[: , -1 :].values

# data preprocessing

# taking log of the dataset in order to make it linear

target = np.log( data.SalePrice)
print('Skewness of the target price array is ' , target.skew())
plt.hist( target , color = 'green')

# working with the numeric types only

numeric_features = data.select_dtypes( include = [np.number] )
target = numeric_features['SalePrice']

# finding the correlation bw the numeric_features and the target

corr = numeric_features.corr()
corr = corr['SalePrice']
corr = corr.sort_values( ascending = False )
overallqual = numeric_features['OverallQual'].unique()

# visualising the relationship bw overallQual and target

quality_pivot = data.pivot_table( index = 'OverallQual' , values = 'SalePrice' , aggfunc = np.median )
quality_pivot.plot( kind = 'bar' , color = 'blue' )

# visualising the relation ship bw GrLivArea and Sale price

quality_pivot2 = data.pivot_table( index = 'GrLivArea' , values = 'SalePrice' , aggfunc = np.median )
plt.scatter( data['SalePrice'] , data['GrLivArea'] )
plt.xlabel('GrLivArea' )
plt.ylabel('Sale price')

# visuailsing the realationship bw the GarageCars

plt.scatter( data['GarageCars'] , data['SalePrice'])

# visulasing the realtionship bw the garage area and the sale price

plt.scatter( data['GarageArea'] , data['SalePrice'])
plt.scatter( data['MoSold'], data['SalePrice'])
plt.hist( data['BedroomAbvGr'] )

# handaling the outliers in the dataset

#data = data[ data['GarageArea'] < 1200 ]
#plt.scatter( data['GarageArea'] , data['SalePrice'])
#data = data[ data['LotFrontage'] < 200 ]
#data = data[ data['BsmtFinSF2'] < 1200 ]
#data = data[ data['TotalBsmtSF']< 3000 ]

################### handaling the missing values ######################

nulls = pd.DataFrame( data.isnull().sum().sort_values( ascending = False )[ : 25] )
nulls.index.name = 'Features'
nulls.columns = [ 'Null Count' ]
misc = data['MiscFeature'].unique()

# for numerical values we are imputing the missing values with mean values

from sklearn.preprocessing import Imputer, StandardScaler
imputer = Imputer( missing_values = "NaN" , strategy = "mean" )
numeric_features = imputer.fit_transform( numeric_features )

sc_x = StandardScaler()
sc_y = StandardScaler()
numeric_features = sc_x.fit_transform( numeric_features )
y = sc_y.fit_transform( y )

# handaling the categorical values

categoricals = data.select_dtypes( exclude = [np.number] )
desc = categoricals.describe()

# encoding the categorical data
d1 = pd.get_dummies( categoricals.MSZoning , drop_first = True , dummy_na = True )
for i in range( 1 , 43 ):
    d1 = np.append( d1 , pd.get_dummies( categoricals.iloc[ : , i ].values , dummy_na = True , drop_first = False ) , axis = 1 )

# final dataset after cleaning

clean_data = np.append( d1 , numeric_features , axis = 1 )

# fitting the decison tree model to the dataset

from sklearn.svm import SVR
regressor = SVR( kernel = 'rbf' )
regressor.fit( clean_data , y )

############ applying the same data preprocessing to the test set ###########

test = pd.read_csv( 'test.csv')
test_categoricals = test.select_dtypes( exclude = [ np.number ] )
test_numericals = test.select_dtypes( include = [ np.number ] )

# excluding the ID index from the arry
test_numericals = test_numericals.iloc[: , 1: ].values

# taking care of missing numerical value by replacing it with average

imputer2 = Imputer( )
test_numericals = imputer2.fit_transform( test_numericals )

test_numericals = sc_x.fit_transform( test_numericals )

# making dummy variabels for categorical data

d2 = pd.get_dummies( test_categoricals.iloc[ : , 0 ].values ,dummy_na = True , drop_first = True )
for i in range( 1, 43 ):
    d2 = np.append( d2 , pd.get_dummies( test_categoricals.iloc[: , i ].values , dummy_na = True , drop_first = True  ) ,axis = 1 )

# creating a clean test set4
clean_test_set = np.append( d2 , test_numericals , axis = 1 )

# now making the shape of clean_test_set and clean_data same

clean_test_set = np.append( clean_test_set , np.zeros( shape = ( 1 , np.shape( clean_test_set)[1] ) ) , axis = 0 )
clean_test_set = np.append( clean_test_set ,np.zeros(shape = (np.shape(clean_data)[0] , np.shape( clean_data )[1] - np.shape( clean_test_set)[1]) ,
                                    dtype = np.int64 ) ,axis = 1 )

######################### predicting the test results #############################

y_pred = pd.DataFrame( )
y_pred['Id'] = test.Id
predictions = regressor.predict( clean_test_set )
predictions = sc_y.inverse_transform( predictions )
predictions = predictions[:-1 ]
y_pred['SalePrice'] = predictions
y_pred.to_csv('submission2.csv' , index = False )


from time import sleep
from random import choice
from math import floor
import argparse
from tkinter import Tk, Label, Message

TONES = [
    'C', 'F', 'Bb',
    'Eb', 'Ab', 'Db',
    'F#', 'B', 'E',
    'A', 'D', 'G',
    'A#', 'D#', 'G#',
    'C#', 'Gb'
]

IONIAN_TONES = [
    'C', 'F', 'Bb',
    'Eb', 'Ab', 'Db',
    'Gb', 'F#', 'G',
    'D', 'A', 'E',
    'B'
]

# Les tons éoliens diffèrent : on ne dira jamais Gb dorien car ça supposerait qu'on est en Fab majeur.
DORIAN_TONES = [
    'A', 'D', 'G',
    'C', 'F', 'Bb',
    'Eb', 'Ab', 'G#',
    'C#', 'F#', 'B',
    'E'
]

MIXOLYDIAN_TONES = [
    'C', 'F', 'Bb',
    'Eb', 'Ab', 'Db',
    'C#', 'F#', 'B',
    'E', 'A', 'D',
    'G'
]

EOLIAN_TONES = [
    'A', 'D', 'G',
    'C', 'F', 'Bb',
    'Eb', 'D#', 'G#',
    'C#', 'F#', 'B',
    'E'
]

MAJOR_MODES_NAMES = [
    'Ionian', 'Dorian', 'Phrygian',
    'Lydian', 'Mixolydian', 'Eolian',
    'Locrian'
]

INTERVALS = [
    'b9', '9th', 'min 3rd',
    'Maj 3rd', '11th', 'b5',
    '5th', 'b13', ' 13th',
    'min 7th', 'Maj 7th'
    ]

def name_to_content(name: str):
    mapping_name_content = {

        # Intervalles, modes et armures
        'major_tones': IONIAN_TONES,
        'minor_tones': [t + '-' for t in EOLIAN_TONES],
        'major_modes': [tone + ' ' + m for tone in TONES for m in MAJOR_MODES_NAMES],
        'all_intervals': [interval + '\n of \n ' + tone for interval in INTERVALS for tone in TONES],
        'triton': ['b5 of \n ' + e for e in TONES],
        '11th': ['11th of \n ' + e for e in TONES],
        '9th': ['9th of \n ' + e for e in TONES],
        '11th': ['11th of \n ' + e for e in TONES],
        '13th': ['13th of \n ' + e for e in TONES],
        'minor_13th': ['b13 of \n ' + e for e in TONES],
        'all_13th': ['13th of \n ' + e for e in TONES] + ['b13 of \n ' + e for e in TONES],
        'ionian_modes': [t + ' Ionian' for t in IONIAN_TONES],
        'dorian_modes': [t + ' Dorian' for t in DORIAN_TONES],
        'mixolydian_modes': [t + ' Mixolydian' for t in MIXOLYDIAN_TONES],
        'eolian_modes': [t + ' Eolian' for t in EOLIAN_TONES],

        # Triades
        'major_triads': TONES,
        'minor_triads': [e + '-' for e in TONES],
        'diminished': [e + ' °' for e in TONES],
        'augmented': [e + ' aug' for e in TONES],

        # Accords non altérés
        'minor_major_7': [e + ' mM7' for e in TONES],
        'minor_7': [e + '-7' for e in TONES],
        '7': [e + '7' for e in TONES],
        'major_7': [e + 'Maj7' for e in TONES],
        'semi_diminished': [e + ' Ø' for e in TONES],
        'minor_6': [e + '- 6' for e in TONES],
        'sus': [e + ' sus4' for e in TONES],
        'phryg': [e + ' Phryg' for e in TONES],
        'min11': [e + ' min11' for e in TONES],
        'diminished 7': [e + ' ° 7' for e in TONES],

        # Accords altérés
        '7_b9': [e + '7 b9' for e in TONES],
        '7_#9': [e + '7 #9' for e in TONES],
        '7_#11': [e + '7 #11' for e in TONES],
        '7_b13': [e + '7 b13' for e in TONES],
        '7_b9_b13': [e + '7 b9 b13' for e in TONES],
        'major7_#11': [e + ' Maj7 #11' for e in TONES],
        'major7_#5': [e + ' Maj7 #5' for e in TONES]
    }

    return mapping_name_content[name]

def mode_to_name(var_mode: int) -> list:

    mapping_mode_name = {
        # Intervalles, modes et armures
        '9': 'major_modes',
        '10': 'all_intervals',
        '11': 'triton',
        '12': '11th',
        '13': 'minor_13th',
        '15': 'major_tones',
        '16': 'minor_tones',
        '17': '13th',
        '19': 'all_13th',
        '29': 'ionian_modes',
        '30': 'dorian_modes',
        '31': 'mixolydian_modes',
        '32': 'eolian_modes',

        # Triades
        '1': 'major_triads',
        '2': 'minor_triads',
        '7': 'diminished',
        '8': 'augmented',

        # Accords non altérés
        '0': 'minor_major_7',
        '3': 'minor_7',
        '4': '7',
        '5': 'major_7',
        '6': 'semi_diminished',
        '14': 'minor_6',
        '21': 'sus',
        '27': 'phryg',
        '28': 'min11',
        '33': 'diminished 7',

        # Accords altérés
        '24': '7_b9',
        '20': '7_#9',
        '25': '7_#11',
        '26': '7_b13',
        '18': '7_b9_b13',

        '22': 'major7_#11',
        '23': 'major7_#5'
    }

    return mapping_mode_name[str(var_mode)]

def train(mode: int, dt, nb_repetitions=2, subtitle=""):
    """ Display chords every dt seconds. Once all the chords have been played once, another round can begin."""

    my_list = name_to_content(mode_to_name(mode))
    elements_already_chosen = []
    n = len(my_list)

    for i in range(nb_repetitions * n):

        if len(elements_already_chosen) == n:
            elements_already_chosen = []

        el = choice(my_list)

        # Check that the element has not been played already in the current cycle.
        while el in elements_already_chosen:
            el = choice(my_list)

        # Update list of already played elements
        elements_already_chosen.append(el)

        # Display
        root = Tk()
        root.attributes('-fullscreen', True)
        root.after(floor(dt * 1000), lambda: root.destroy())
        msg = Message(root, text=el)
        msg.config(font=('times', 100, 'italic bold underline'))
        msg.pack()

        # Optional message
        msg1 = Message(root, text=subtitle)
        msg1.config(font=('times', 50))
        msg1.pack()

        root.mainloop()

if __name__ == "__main__":

    # Parse arguments

    parser = argparse.ArgumentParser()
    parser.add_argument('-m',
                        '--mode',
                        action="store",
                        dest="mode",
                        type=int,
                        help="Mode used for the training. For example, 3 is minor 7 chords, and 5 is major7 chords.")

    parser.add_argument('-d',
                        '--delta',
                        action="store",
                        dest="delta_t",
                        type=float,
                        help="Time beetween 2 chords. The smaller, the harder the training is.")

    parser.add_argument('-n',
                        '--nb_cycles',
                        action="store",
                        dest="nb_cycles",
                        type=int,
                        help="Number of times the list will be browsed.")

    parser.add_argument('-s',
                        '--sleep_time',
                        action="store",
                        dest="sleep_time",
                        type=int,
                        default=10,
                        help="Sleep time before launching training.")

    parser.add_argument('-t',
                        '--text',
                        action="store",
                        dest="subtitle",
                        type=str,
                        default="",
                        help="Subtitle to print while training.")

    args = parser.parse_args()
    mode = args.mode
    delta_t = args.delta_t
    nb_cycles = args.nb_cycles
    sleep_time = args.sleep_time
    subtitle = args.subtitle

    # Launch training

    double_line = "\n\n" + "-" * 422 + "\n\n"

    print(double_line)
    print(f"You have asked this training :\n\nmode : {mode_to_name(mode)}     |"
          f"       delta_t : {delta_t}        |"
          f"       nb_cycles : {nb_cycles}   \n\n")

    sleep(sleep_time)

    try:
        train(mode=mode, dt=delta_t, nb_repetitions=nb_cycles, subtitle=subtitle)
    except KeyboardInterrupt:
        print("                                                            INTERRUPTION OF TRAINING    ")
    finally:
        print(double_line)


import yfinance as yf
import pandas as pd
from openpyxl import load_workbook
import datetime as dt
from stock_config import momentum_stock_list, sample_stock_list

def get_valid_date(start_date, end_date):
    valid = False
    num_tries = 0
    while not valid and num_tries <=5:
        valid_check_data = yf.download('RELIANCE.NS', start = start_date, end=end_date)
        try:
            open_price = valid_check_data.iloc[0]['Open']
            valid = True
            num_tries = 0
        except:
            start_date = (pd.to_datetime(start_date) + pd.DateOffset(days=1)).strftime('%Y-%m-%d')
            end_date = (pd.to_datetime(end_date) + pd.DateOffset(days=1)).strftime('%Y-%m-%d')
            valid = False
            num_tries += 1
    return start_date, end_date

def main_logic(eval_date, stocklist, filename):
    start_date=eval_date
    stock_list = stocklist
    print(f"Total stocks in the list {len(stock_list)}")

    end_date = (pd.to_datetime(start_date) + pd.DateOffset(days=1)).strftime('%Y-%m-%d')
    start_date, end_date = get_valid_date(start_date, end_date)

    data = yf.download(stock_list, start = start_date, end=end_date)
    this_month_close = pd.DataFrame(data.iloc[0]['Adj Close'])

    start_date=(pd.to_datetime(eval_date) - pd.DateOffset(months=1)).strftime('%Y-%m-%d')
    end_date = (pd.to_datetime(start_date) + pd.DateOffset(days=1)).strftime('%Y-%m-%d')

    start_date, end_date = get_valid_date(start_date, end_date)

    data = yf.download(stock_list, start = start_date, end=end_date)
    last_month_close = pd.DataFrame(data.iloc[0]['Adj Close'])

    final_df = last_month_close.join(this_month_close)
    final_df.rename(columns =lambda t: t.strftime('%Y-%m-%d'), inplace=True)

    ## Calculate Momentum
    last_month_date = final_df.columns[0]
    this_month_date = final_df.columns[1]
    final_df['Change'] = final_df[this_month_date] - final_df[last_month_date]
    final_df['% Change'] = (final_df['Change'] / final_df[last_month_date])*100

    ## Final Output
    final_df.sort_values(by=['% Change'], ascending=False, inplace=True)
    final_df.rename_axis('MOMENTUM_ATH', inplace=True)
    final_df.rename_axis('', axis='columns', inplace=True)

    ## Output to File
    ### Create an empty excel file as below filename before running below code
    try:
        book = load_workbook(filename)
        writer = pd.ExcelWriter(filename, engine = 'openpyxl')
        writer.book = book
    except FileNotFoundError as fileError:
        print(f"{fileError}. Create an empty file manually.")
        return -1

    final_df.round(2).to_excel(writer, sheet_name=eval_date)
    writer.save()
    writer.close()

if __name__ == "__main__":

    start_date = "2020-11-06"       #Start date should be always FRIDAY's date
    end_date = "2020-11-28"
    filename = r'data/output/momentum_monthly_output_nov2020.xlsx'

    dt_start_date = dt.datetime.strptime(start_date, '%Y-%m-%d')
    dt_end_date = dt.datetime.strptime(end_date, '%Y-%m-%d')

    eval_date = dt_start_date
    while eval_date <= dt_end_date:
        eval_date = eval_date.strftime('%Y-%m-%d')
        print("~" * 80)
        print(eval_date)
        result = main_logic(eval_date, momentum_stock_list, filename)
        print("~" * 80)
        if(result == -1):
            break;
        eval_date = (pd.to_datetime(eval_date) + pd.DateOffset(days=7))


from typing import List

class Solution:
    def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int:
        sum = 0
        for i in range(1, len(points)):
            sum += max(abs(points[i][0] - points[i-1][0]), abs(points[i][1] - points[i-1][1]))
        return sum

points = [[1,1],[3,4],[-1,0]]
sol1 = Solution()
result = sol1.minTimeToVisitAllPoints(points)
print(result)


from django.contrib.auth.models import User
from django.core import mail
from django.test import TestCase
from hc.api.models import Check

class LoginTestCase(TestCase):

    def test_it_sends_link(self):
        check = Check()
        check.save()

        session = self.client.session
        session["welcome_code"] = str(check.code)
        session.save()

        form = {"email": "alice@example.org"}

        # assert the user doesn't exist before post operation
        self.assertEqual(len(User.objects.filter(email=form["email"])), 0)
        self.old_user_count = User.objects.count()

        response = self.client.post("/accounts/login/", form)
        self.assertEqual(response.status_code, 302)

        # Assert that a user was created
        self.new_user_count = User.objects.count()
        self.test_user_list = User.objects.filter(email=form["email"])
        self.assertEqual(self.test_user_list[0].email, form["email"])
        self.assertEqual(self.new_user_count - self.old_user_count, 1)

        # And email sent
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Log in to healthchecks.io')
        # Assert contents of the email body
        self.assertIn("To log into healthchecks.io, please open the link below",
                      mail.outbox[0].body)

        # Assert that check is associated with the new user
        self.test_check = Check.objects.get(user=self.test_user_list[0].id)
        self.assertEqual(self.test_user_list[0].id, self.test_check.user_id)

    def test_it_pops_bad_link_from_session(self):
        self.client.session["bad_link"] = True
        self.client.get("/accounts/login/")
        assert "bad_link" not in self.client.session


import cv2
import numpy as np

read_img = cv2.imread("files/fmh2.jpg")

# define a kernel #np.ones- all value is 1, size of matrix = 5x5
kernel = np.ones((5,5), np.uint8) # unsigned int of 8 bits(0-255)

# # convert img in greyscale
# # cvtColor() converts img into different color spaces
# imgGray = cv2.cvtColor(read_img, cv2.COLOR_BGR2GRAY)  # cv2.COLOR - color space you want to convert into

# imgBlur = cv2.GaussianBlur(imgGray, (7,7), 0) #ksize - kernal size - has to be odd no, sigmax =0

# finding edges in our image - we will use Canny func
imgCanny = cv2.Canny(read_img, 100, 100)  # threshold values 100,100 - can be any no.
# to increase thickness of edge, iteration= how much thickness we need
imgDialation = cv2.dilate(imgCanny,kernel, iterations = 1 ) # add a kernel - size of and value of(use matrix)

# erosion - to make img edge thiner
imgEroded = cv2.erode(imgDialation, kernel, iterations=1)

# cv2.imshow("Gray Image", imgGray)
# cv2.imshow("Blur Image", imgBlur)
cv2.imshow("Blur Image", imgCanny)
cv2.imshow("Dialation Image", imgDialation)
cv2.imshow("Eroded Image", imgEroded)
cv2.waitKey(0)


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

df=pd.read_csv('https://gist.githubusercontent.com/pravalliyaram/5c05f43d2351249927b8a3f3cc3e5ecf/raw/8bd6144a87988213693754baaa13fb204933282d/Mall_Customers.csv')
print(df)

#Let us perform grouping of the customers based on Annual Income and Spending score

x=df.iloc[:,3:]
x2=df.iloc[:,3:]
x3=df.iloc[:,3:]
print(x)

#Let us find the number of clusters k is required
wcss=[]

for i in range(1,15):
    alg_kmean=KMeans(n_clusters=i,init='k-means++',random_state=30)
    alg_kmean.fit(x)
    wcss.append(alg_kmean.inertia_) # inertia is = within cluster summation square)

print(wcss)
#Let us build the elbow graph to find the number of k
plt.plot(range(1,15),wcss)
plt.show()

#From the elbow graph we can find the k as 5

alg_Kmean2=KMeans(n_clusters=5,init='k-means++',random_state=30)
alg_Kmean2.fit_predict(x)

x['cluster_numbers']=alg_Kmean2.fit_predict(x)
print(x)

print(x[x['cluster_numbers']==4])

print(x[x['cluster_numbers']==0])

print(alg_Kmean2.predict([[55,31]]))

#Let us see using mini batch Kmean

from sklearn.cluster import MiniBatchKMeans
alg_minibatch=MiniBatchKMeans(n_clusters=5)
alg_minibatch.fit_predict(x2)

x2['mini_labels']=alg_minibatch.fit_predict(x2)
print(x2)
print(x2[x2['mini_labels']==2])
print(alg_minibatch.predict([[55,31]]))

#Let us perform using DBSCAN

from sklearn.cluster import DBSCAN
alg_dbscan=DBSCAN(eps=1,min_samples=3)
alg_dbscan.fit(x3)
print(alg_dbscan.labels_)
#Total number of clusters
print(set(alg_dbscan.labels_))
#Total lenght of the clusters
print(len(set(alg_dbscan.labels_)))
#Group -1 indicates that it is a noise or outlier where it is not able to accupi it to any of the cluster

x3['dbscan_labels']=alg_dbscan.labels_
print(x3)

#Let us see the accuracy of the cluster algorithm

from sklearn.metrics import jaccard_score,accuracy_score,rand_score,adjusted_rand_score

#Take Kmean out put as client label or ground truth table
print(jaccard_score(x['cluster_numbers'],x3['dbscan_labels'],average='macro'))


import numpy as np
import cv2
import os
from planarH import computeH
from matplotlib import pyplot as plt

def compute_extrinsics(K, H):
    '''
    INPUTS:
        K - intrinsic parameters matrix
        H - estimated homography
    OUTPUTS:
        R - relative 3D rotation
        t - relative 3D translation

    '''

    #############################
    # We want to get the new homography that eliminatesthe effect of the intrinsic parameters
    # by pre-multiplying the estimated homography by the inverse of the intrinsic matrix
    # Follow instructions in the Prince textbook
    H_new = np.dot(np.linalg.inv(K), H)
    U, L, Vt = np.linalg.svd(H_new[:, 0:2], full_matrices=True)
    w = np.dot(U, np.array([[1, 0], [0, 1], [0, 0]]))
    w = np.dot(w, Vt)

    w = np.concatenate((w, np.cross(w[:, 0], w[:, 1]).reshape((3,1))),axis=1)

    if np.linalg.det(w) == -1:
        w[:, 2] = w[:, 2] * (-1)

    sum = 0
    for i in range(3):
        for j in range(2):
            sum += H_new[i, j] / w[i, j]
    scale = sum / 6

    R = w
    t = (H_new[:, 2] / scale).T

    return R, t

def project_extrinsics(K, W, R, t):
    '''
    INPUTS:
        K - intrinsic parameters matrix
        W - 3D planar points of textbook
        R - relative 3D rotation
        t - relative 3D translation
    OUTPUTS:
        X - computed projected points
    '''

    #############################
    extrinsic = np.concatenate((R, t), axis=1)
    intrin_extrin = np.dot(K, extrinsic)
    x = np.dot(intrin_extrin, W)

    proj = np.zeros((2, W.shape[1]))
    for i in range(x.shape[1]):
        # remember scaling
        proj[0, i] = int(x[0, i] / x[2, i])
        proj[1, i] = int(x[1, i] / x[2, i])

    return proj

if __name__ == "__main__":
    # image
    im = cv2.imread('../data/prince_book.jpeg')
    book_im = cv2.imread('../data/prince_book.jpeg')
    # plt.imshow(im, cmap='gray')
    # plt.show()
    #############################
    # TO DO ...
    # perform required operations and plot sphere

    # Four corners of book in real life
    W = np.array([[0, 18.2, 18.2, 0], [0, 0, 26, 26], [0, 0, 0, 0]])
    # Four corners of the book on image
    X = np.array([[483, 1704, 2175, 67], [810, 781, 2217, 2286]])
    # Intrinsic matrix for image
    K = np.array([[3043.72, 0, 1196], [0, 3043.72, 1604], [0, 0, 1]])

    H = computeH(X, W[0:2, :])
    R, t = compute_extrinsics(K, H)

    # Open sphere file
    file = open('../data/sphere.txt', "r")
    str = file.read()
    str_array = str.split('\n')
    sphere_x = str_array[0].split('  ')
    sphere_y = str_array[1].split('  ')
    sphere_z = str_array[2].split('  ')
    tmp_coord_list = []
    for i in range(1, len(sphere_x)):
        tmp_coord_list.append([float(sphere_x[i]), float(sphere_y[i]), float(sphere_z[i])])

    W_sphere = np.array(tmp_coord_list).T
    W_sphere = np.concatenate((W_sphere, np.ones((1, W_sphere.shape[1]))), axis=0)

    # With the sphere coordinates, project extrinsics!
    proj_sphere = project_extrinsics(K, W_sphere, R, t.reshape(3, 1)) + (np.array((320, 650))).T.reshape(2, 1)

    fig = plt.figure()
    plt.imshow(im)
    plt.plot(proj_sphere[0, :], proj_sphere[1, :], 'y.', markersize=2)
    plt.draw()
    plt.waitforbuttonpress(0)
    plt.close(fig)


#!/usr/bin/python3
""" device_wemo.py:
"""

# Import Required Libraries (Standard, Third Party, Local) ****************************************
import copy
import datetime
import logging
from .device import Device
from rpihome.modules.message import Message

# Authorship Info *********************************************************************************
__author__ = "Christopher Maue"
__copyright__ = "Copyright 2016, The RPi-Home Project"
__credits__ = ["Christopher Maue"]
__license__ = "GPL"
__version__ = "1.0.0"
__maintainer__ = "Christopher Maue"
__email__ = "csmaue@gmail.com"
__status__ = "Development"

# Device class ************************************************************************************
class DeviceWemo(Device):
    def __init__(self, name, address, msg_out_queue, logger=None):
        # Configure logger
        self.logger = logger or logging.getLogger(__name__)
        super().__init__(name, msg_out_queue, self.logger)
        self.name = name
        self.address = address
        self.msg_out_queue = msg_out_queue
        self.discover_device()

    def discover_device(self):
        self.logger.debug("Sending command to wemo gateway to find device at address: %s", self.address)
        self.msg_to_send = Message(source="11", dest="16", type="160", name=self.name, payload=self.address)
        self.msg_out_queue.put_nowait(self.msg_to_send.raw)

    def command(self):
        """ This method is used to send a single command to various devices when necessary instead
        of sending the same command over and over again """
        if self.state != self.state_mem:
            if self.state is True:
                self.msg_to_send = Message(source="11", dest="16", type="161", name=self.name, payload="on")
                self.msg_out_queue.put_nowait(self.msg_to_send.raw)
                self.logger.debug("Sending command to wemo gateway to turn ON device: %s", self.name)
            else:
                self.msg_to_send = Message(source="11", dest="16", type="161", name=self.name, payload="off")
                self.msg_out_queue.put_nowait(self.msg_to_send.raw)
                self.logger.debug("Sending command to wemo gateway to turn OFF device: %s", self.name)
            pass
            # Snapshot new device state in memory so the command is only sent once
            self.state_mem = copy.copy(self.state)


import os
import openpyxl
from lxml import etree

class HtmlProcess(object):
    """处理SAP.ZDER里面导出的htm文件，并把结果写入到对应的excel文件中"""

    def __init__(self):
        self.dc_list = ["A668", "A672", "A673", "A680", "A681", "A715", "A716", "B194"]

    def get_data(self, fname, code_list):
        """接受文件名和Code列表来处理数据"""

        src = open(fname).read()
        htm = etree.HTML(src)
        p = htm.xpath("//blockquote/p[2]")[0]
        trs = p.findall(".//tr")
        # 开头行是表头，结尾行是表尾，中间还有位于第2页的表头，是第41个tr
        # 这3行都不要
        trs = trs[1:41] + trs[42:50]

        dc_list = self.dc_list
        # 创建字典以存放库存数据
        result_dict = {}
        for d in dc_list:
            result_dict[d] = {}
            for c in code_list:
                result_dict[d][c] = {"dc": 0, "intransit": 0}

        for tr in trs:
            code = tr.find(".//td[3]/font/nobr").text.strip()                         # Material Code
            dc = tr.find(".//td[5]/font/nobr").text.strip()                           # DC
            unres_inv = int(float(tr.find(".//td[7]/font/nobr").text.strip().replace(",", "")))        # Unrestrict Inventory
            qi_inv = int(float(tr.find(".//td[9]/font/nobr").text.strip().replace(",", "")))           # Quality Inventory
            block_inv = int(float(tr.find(".//td[10]/font/nobr").text.strip().replace(",", "")))       # Block Inventory
            intransit_inv = int(float(tr.find(".//td[13]/font/nobr").text.strip().replace(",", "")))   # Intransit Inventory
            result_dict[dc][code]["dc"] = unres_inv + qi_inv + block_inv
            result_dict[dc][code]["intransit"] = intransit_inv

        return result_dict

    def get_ws(self, fname):
        wb = openpyxl.load_workbook(fname, keep_vba=True)
        ws = wb.get_sheet_by_name("Sheet2")
        return (wb, ws)

    def write_to_excel(self, ws, code_list, result_dict):
        '''把结果写入到ws中，ws是一个Excel的工作表对象'''
        dc_list = self.dc_list
        for col, code in zip(range(2, 14, 2), code_list):
            for r, dc in zip(range(8, 16), dc_list):
                ws.cell(row=r, column=col).value = result_dict[dc][code]["dc"]
                ws.cell(row=r, column=col+1).value = result_dict[dc][code]["intransit"]

if __name__ == "__main__":
    htmlProcess = HtmlProcess()

    # 处理koala数据
    koala_htm = "C:/users/xian.jr/Documents/SAP/SAP GUI/ka.htm"
    koala_inv = "C:/Users/xian.jr/Documents/PG/Jacqueline/Koala/Koala Inventory Template.xlsm"
    koala_code_list = ["82261753", "82261756", "82261762", "82261951", "82263780", "82264483"]
    koala_result_dict = htmlProcess.get_data(koala_htm, koala_code_list)
    koala_inv_wb, koala_inv_ws = htmlProcess.get_ws(koala_inv)
    htmlProcess.write_to_excel(koala_inv_ws, koala_code_list, koala_result_dict)
    koala_inv_wb.save(koala_inv)

    # 处理tampax数据
    # tampax_htm = "C:/users/xian.jr/Documents/SAP/SAP GUI/ta.htm"
    # tampax_inv = "C:/Users/xian.jr/Documents/PG/Jacqueline/Tampax/Inventory Template.xlsm"
    # tampax_code_list = ["82255634", "82258740", "82259122", "82258746", "82258747", "82261076"]
    # tampax_result_dict = htmlProcess.get_data(tampax_htm, tampax_code_list)
    # tampax_inv_wb, tampax_inv_ws = htmlProcess.get_ws(tampax_inv)
    # htmlProcess.write_to_excel(tampax_inv_ws, tampax_code_list, tampax_result_dict)
    # tampax_inv_wb.save(tampax_inv)


# Non-proportional venn diagram for all correctly assigned
from matplotlib import pyplot as plt
import numpy as np
from matplotlib_venn import venn3, venn3_circles, venn3_unweighted
plt.figure(figsize=(10, 10))
vd = venn3_unweighted(subsets=(61, 37, 39, 6, 41, 31, 1158))
for text in vd.subset_labels:
    text.set_weight('bold')
for text in vd.subset_labels:
    text.set_color('black')
for text in vd.subset_labels:
    text.set_fontsize(20)
vd.get_label_by_id('A').set_text('Support Vector Machines')
vd.get_label_by_id('A').set_fontsize(20)
vd.get_label_by_id('A').set_weight('bold')
vd.get_label_by_id('B').set_text('Random Forest')
vd.get_label_by_id('B').set_fontsize(20)
vd.get_label_by_id('B').set_weight('bold')
vd.get_label_by_id('C').set_text('Bayes Net')
vd.get_label_by_id('C').set_fontsize(20)
vd.get_label_by_id('C').set_weight('bold')
vd.get_patch_by_id('100').set_alpha(1.0)
vd.get_patch_by_id('100').set_color('#594046')
vd.get_patch_by_id('110').set_alpha(1.0)
vd.get_patch_by_id('110').set_color('#DE646B')
vd.get_patch_by_id('010').set_alpha(1.0)
vd.get_patch_by_id('010').set_color('#D74A50')
vd.get_patch_by_id('101').set_alpha(1.0)
vd.get_patch_by_id('101').set_color('#FCB064')
vd.get_patch_by_id('111').set_alpha(1.0)
vd.get_patch_by_id('111').set_color('#F7DADC')
vd.get_patch_by_id('011').set_alpha(1.0)
vd.get_patch_by_id('011').set_color('#DD646B')
vd.get_patch_by_id('001').set_alpha(1.0)
vd.get_patch_by_id('001').set_color('#FAA451')
plt.text(-1.1,-0.75, 'Number of correctly assigned observations: 1363', fontsize=14, fontweight='bold')
plt.text(-1.1,-0.8, 'Total number of observations: 1470', fontsize=14, fontweight='bold')
plt.show()


list(map(int, input().split()))

map(int, input().split())

###A
A, B = map(int, input().split())
print(A**B+B**A)

###B
S = input()

def max_palindrome_length(s: str) -> int:
    n = len(s)
    res = 1

    for i in range(n):
        l, r = i, i
        while l >= 0 and r < n and s[l] == s[r]:
            res = max(res, r - l + 1)
            l -= 1
            r += 1

    for i in range(n - 1):
        l, r = i, i + 1
        while l >= 0 and r < n and s[l] == s[r]:
            res = max(res, r - l + 1)
            l -= 1
            r += 1

    return res
print(max_palindrome_length(S))

###C
M = int(input())
S1 = list(map(int, list(input())))*3
S2 = list(map(int, list(input())))*3
S3 = list(map(int, list(input())))*3
sec = []
for i in range(M):
    for j in range(i+1, M*2):
        if S2[j] == S1[i]:
            for k in range(j+1, M*3):
                if S3[k] == S1[i]:
                    sec.append(k)

for i in range(M):
    for j in range(i+1, M*2):
        if S3[j] == S1[i]:
            for k in range(j+1, M*3):
                if S2[k] == S1[i]:
                    sec.append(k)

for i in range(M):
    for j in range(i+1, M*2):
        if S1[j] == S2[i]:
            for k in range(j+1, M*3):
                if S3[k] == S2[i]:
                    sec.append(k)

for i in range(M):
    for j in range(i+1, M*2):
        if S3[j] == S2[i]:
            for k in range(j+1, M*3):
                if S1[k] == S2[i]:
                    sec.append(k)

for i in range(M):
    for j in range(i+1, M*2):
        if S1[j] == S3[i]:
            for k in range(j+1, M*3):
                if S2[k] == S3[i]:
                    sec.append(k)

for i in range(M):
    for j in range(i+1, M*2):
        if S2[j] == S3[i]:
            for k in range(j+1, M*3):
                if S1[k] == S3[i]:
                    sec.append(k)

if len(sec)==0:
    print(-1)
else:
    print(min(sec))

###D
from collections import defaultdict, deque

def find_coordinates(N, M, relations):
    graph = defaultdict(list)
    for a, b, x, y in relations:
        graph[a].append((b, x, y))
        graph[b].append((a, -x, -y))

    visited = set()
    coords = [(float('inf'), float('inf'))] * (N + 1)
    coords[1] = (0, 0)
    queue = deque([1])

    while queue:
        current = queue.popleft()
        x_current, y_current = coords[current]

        for neighbor, dx, dy in graph[current]:
            if neighbor in visited:
                if coords[neighbor] != (x_current + dx, y_current + dy):
                    return ["undecidable"] * N
            else:
                visited.add(neighbor)
                coords[neighbor] = (x_current + dx, y_current + dy)
                queue.append(neighbor)

    return coords[1:]

N, M = map(int, input().split())
relations = [tuple(map(int, input().split())) for _ in range(M)]
results = find_coordinates(N, M, relations)

for res in results:
    if res[0]==float('inf'):
        print("undecidable")
    else:
        print(res[0], res[1])

###E
from collections import deque
import heapq

def get_noodles_amount(N, M, events):
    noodles = [0] * N
    queue = deque(list(range(N)))
    outside = {}
    events = sorted(events, key=lambda x: x[0])

    for t, w, s in events:
        # Append back people who have returned till time t
        return_times = list(outside.keys())
        for return_time in sorted(return_times):
            if return_time <= t:
                queue.appendleft(outside.pop(return_time))

        if queue:
            person = queue.popleft()
            noodles[person] += w
            outside[t + s] = person

    return noodles

N, M = map(int, input().split())
events = [tuple(map(int, input().split())) for _ in range(M)]

results = get_noodles_amount(N, M, events)

for res in results:
    print(res)


#! /usr/bin/python3
import os
import time
import pyqrcode
from pyqrcode import QRCode
from googlesearch import search
web= input("please enter topic: ")
# print(search(web))
url=[]
u=0
for i in search(web, stop=3):
	print(i)
	time.sleep(1)
	url.append(i)

print(url)

for i in range(3):
    qr= pyqrcode.create(url[u])
    qr.svg("url"+str(i)+".png", scale=8)
    u+=1
    print(qr.terminal())

os.system('mkdir /var/www/html/qr')

os.system('mv *.png /var/www/html/qr')


from PyQt5.QtCore import (Qt)

# Change whether you want to use Tab to change formats
tabFormat = True
# Change whether you want Enter to automatically change formats
enterFormat = True

# Change the default author of scripts
defaultAuthor = "Josh Davis"

# Change whether scripts should start with default character suggestions
startWithDefaultCharacters = False
# Change the default characters in your scripts
defaultCharacters = ["ALEX", "CHARLIE", "FRANKIE", "JESSIE"]

# Alternative (and customizable) shortcuts for formatting
## Refer to https://doc.qt.io/qtforpython-5/PySide2/QtCore/Qt.html
actionFormat = Qt.ALT + Qt.Key_1
characterFormat = Qt.ALT + Qt.Key_2
dialogueFormat = Qt.ALT + Qt.Key_3
paranthesisFormat = Qt.ALT + Qt.Key_4
headingFormat = Qt.ALT + Qt.Key_5
transitionFormat = Qt.ALT + Qt.Key_6
# Cycle through formats
changeFormat = Qt.ALT + Qt.Key_QuoteLeft
# Add a new line AND change formats
newLineFormat = Qt.ALT + Qt.Key_Return


# -*- coding: utf-8 -*-
import getpass
import logging
import os
import shutil
import xml.dom.minidom

import maya.cmds as mc
import maya.mel as mel
import maya.utils as mu

import pipeGlobal
from miraLibs.mayaLibs import set_image_size, get_maya_version

logger = logging.getLogger(u"MIRA MAIN")
logger.setLevel(logging.DEBUG)

conf_dir = pipeGlobal.conf_dir

# ------------------------------------------------------------------------------------------#
# ----------------------------------load menu  ---------------------------------------------#
# ------------------------------------------------------------------------------------------#

def get_menu_conf_path():
    menu_conf_path = os.path.join(conf_dir, "mayaMenu.xml")
    return menu_conf_path

def run_mel_cmd(mel_path):
    mel.eval("source \"%s\"" % mel_path)

def load_menu():
    try:
        mc.delteUI("[MIRA]")
    except:
        pass
    menu_conf_path = get_menu_conf_path()
    dom_tree = xml.dom.minidom.parse(menu_conf_path)
    collection = dom_tree.documentElement
    maya_version = get_maya_version.get_maya_version()
    main_maya_menu = mc.menu('test', label=collection.getAttribute("name"), tearOff=True,
                             parent='MayaWindow', version=maya_version)

    sub_menus = collection.getElementsByTagName("submenu")
    for sub_menu in sub_menus:
        name = sub_menu.getAttribute("name")
        menu_type = sub_menu.getAttribute("type")
        if menu_type == "submenu":
            sub_maya_menu = mc.menuItem(sm=1, label=name, tearOff=True, parent=main_maya_menu)
            cmd_menues = sub_menu.getElementsByTagName("command")
            for cmd_menu in cmd_menues:
                cmd_menu_name = cmd_menu.getAttribute("name")
                cmd = cmd_menu.getAttribute("cmd")
                mode = cmd_menu.getAttribute("mode")
                if mode == "python":
                    mc.menuItem(label=cmd_menu_name, tearOff=True, parent=sub_maya_menu, c=cmd)
                elif mode == "mel":
                    mel_path = os.path.join(pipeGlobal.mira_dir, cmd)
                    mel_path = mel_path.replace("\\", "/")
                    mc.menuItem(label=cmd_menu_name, tearOff=True, parent=sub_maya_menu,
                                c=lambda *args: run_mel_cmd(mel_path))

        elif menu_type == "command":
            mc.menuItem(label=name, tearOff=True, parent=main_maya_menu, c=sub_menu.getAttribute('cmd'))

        elif menu_type == "separator":
            mc.menuItem(parent=main_maya_menu, divider=1)
    logger.debug("Load menu done")

# ------------------------------------------------------------------------------------------#
# ----------------------------------reload shelves -----------------------------------------#
# ------------------------------------------------------------------------------------------#

def reload_shelves():
    from miraPipeline.maya.shelf import reload_shelves
    reload_shelves.main()
    logger.info("Reload shelves done.")

# ------------------------------------------------------------------------------------------#
# ----------------------------------init user setup-----------------------------------------#
# ------------------------------------------------------------------------------------------#

def init_user_setup():
    init_maya_background_style()
    init_maya_saver_timer()
    # close_panel_when_open_and_save()
    init_wireframe_when_open()
    init_current_project()
    load_plugins()
    init_render_setting()
    init_scene_break_down()
    add_system_python_path_env()
    # init_shotgun()
    open_port()
    # remove_invalid_clipboard_data()

def init_maya_saver_timer():
    from miraPipeline.maya.maya_save_timer import maya_save_timer
    maya_save_timer.maya_save_timer()
    logger.info("Initialize maya save timer done.")

def init_scene_break_down():
    from miraPipeline.maya.scene_break_down import auto_tip_update
    auto_tip_update.auto_tip_update()
    logger.info("Initialize scene break down done.")

def init_render_setting():
    import pipeGlobal
    current_project = pipeGlobal.current_project
    resolution = pipeGlobal.Project(current_project).resolution
    set_image_size.set_image_size(*resolution)
    # renderer = pipeGlobal.Project(current_project).renderer
    # user = getpass.getuser()
    # if user in ["lvxiaolong", "xuwenliang", "liwenyang", "fengjingshi", "heshuai"]:
    #     mel.eval('unifiedRenderGlobalsWindow')
    #     mc.setAttr("defaultRenderGlobals.ren", renderer, type="string")
    #     logger.info("Initialize render settings done.")

def close_panel_when_open_and_save():
    from miraLibs.mayaLibs import close_panel_open_and_save
    close_panel_open_and_save.close_panel_open_and_save()
    logger.info("Initialize close panel done.")

def remove_invalid_clipboard_data():
    from miraLibs.mayaLibs import remove_invalid_clipboard_data
    remove_invalid_clipboard_data.remove_invalid_clipboard_data()
    logger.info("Can copy from PyCharm to maya done")

def init_wireframe_when_open():
    from miraLibs.mayaLibs import display_wireframe
    import maya.OpenMaya as OpenMaya
    OpenMaya.MEventMessage.addEventCallback("SceneOpened", display_wireframe.display_wireframe)
    logger.info("Initialize when open file wireframe display done.")

def init_maya_background_style():
    mc.displayPref(displayGradient=1)
    logger.info("Initialize maya background style done.")

def load_plugins():
    from miraLibs.pipeLibs.pipeMaya.plugins import plugins
    plugins.load_plugins()
    plugins.unload_plugins()

def init_current_project():
    from miraPipeline.maya.switch_project import switch_project
    switch_project.main()
    logger.info("Initialize current project done.")

def open_port():
    import subprocess
    from miraPipeline.pipeline.port_operation import add_user_info_to_db
    add_user_info_to_db.add_user_info_to_db()
    open_port_path = os.path.join(pipeGlobal.mira_dir, "miraPipeline/pipeline/port_operation/run_open_port.py")
    open_port_path = open_port_path.replace("\\", "/")
    if not os.path.isfile(open_port_path):
        return
    os.popen("tskill pythonw")
    python_path = "C:/Python27/pythonw"
    command = "%s %s" % (python_path, open_port_path)
    subprocess.Popen(command)
    user = getpass.getuser()
    startup_dir = "C:/Users/%s/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup" % user
    startup_path = "%s/%s" % (startup_dir, "mira.bat")
    if os.path.isfile(startup_path):
        os.remove(startup_path)
    with open(startup_path, "w") as f:
        f.write("start %s" % command)
    logger.info("Initialize open port done.")

def add_system_python_path_env():
    user = getpass.getuser()
    if user in ["heshuai", "zhaopeng"]:
        return
    startup_dir = "C:/Users/%s/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup" % user
    mira_batch_dir = pipeGlobal.batch_dir
    python_path_bat = os.path.join(mira_batch_dir, "PYTHONPATH.bat").replace("\\", "/")
    startup_python_path_bat = os.path.join(startup_dir, "PYTHONPATH.bat")
    try:
        os.system(python_path_bat)
        shutil.copyfile(python_path_bat, startup_python_path_bat)
        logger.info("add system python path env done.")
    except:
        logger.info("add system python path env failed.")

def init_shotgun():
    from miraPipeline.maya.init_shotgun import init_shotgun
    init_shotgun.main()
    logger.info("Initialize shotgun done.")

# ------------------------------------------------------------------------------------------#
# -------------------------------------main-------------------------------------------------#
# ------------------------------------------------------------------------------------------#

def main():
    mu.executeDeferred("import mira_main;mira_main.load_menu()")
    mu.executeDeferred("import mira_main;mira_main.init_user_setup()")
    mu.executeDeferred("import mira_main;mira_main.reload_shelves()")

if __name__ == "__main__":
    main()


import numpy as np
import utils
from collections import Counter
import os
from sklearn.model_selection import train_test_split
try:
    from google.colab.patches import cv2_imshow
except ImportError:
    from cv2 import imshow as cv2_imshow

class DataGenerator:
    def __init__(self, base_dir, batch_size, rst, max_size=500,
                normalize=True, preprocessing=True):
        BATCH_FILES = 4
        self.base_dir = base_dir
        self.batch_size = batch_size
        self.rst = rst
        self.normalize = normalize
        self.max_size = max_size
        self.preprocessing = preprocessing
        self.x = self.get_content_images()

        if self.preprocessing:
            self.x = utils.preprocess(self.x)

        if normalize:
            self.x = utils.norm(self.x)

    def get_content_images(self):
        return utils.pickle_load(
            os.path.join(self.base_dir, 'dataset/content_imgs_{}.pkl'.format(self.rst)))[:self.max_size]

    def next_batch(self):
        x = self.x

        indices = np.arange(x.shape[0])
        np.random.shuffle(indices)
        max_id = x.shape[0] - self.batch_size + 1
        for start_idx in range(0, max_id, self.batch_size):
            access_pattern = indices[start_idx:start_idx + self.batch_size]

            yield x[access_pattern, ...]

    def get_random_sample(self, test=True):
        if test:
            idx = np.random.randint(0, self.x_test.shape - 1)
            return self.x_test[idx], self.y_test[idx]

        idx = np.random.randint(0, self.x.shape - 1)
        return self.x[idx], self.y[idx]

    def random_show(self, option='style'):
        """
        option: ['style', 'content']
        """
        idx = np.random.randint(0, self.x.shape - 1)
        if option == 'style':
            return cv2_imshow(utils.de_norm(self.y[idx]))

        return cv2_imshow(utils.de_norm(self.x[idx]))

    def show_imgs(self, img):
        if len(img.shape) == 4:
            return utils.show_images(img, self.normalize, self.preprocessing)

        if self.normalize:
            img = utils.de_norm(img)
        if self.preprocessing:
            img = utils.deprocess(img)

        cv2_imshow(img)


import argparse

from cosmpy.aerial.client import LedgerClient, NetworkConfig
from cosmpy.aerial.contract import LedgerContract
from cosmpy.aerial.wallet import LocalWallet
from cosmpy.crypto.address import Address
from cosmpy.crypto.keypairs import PrivateKey

def _parse_commandline():
    parser = argparse.ArgumentParser()
    parser.add_argument("contract_path", help="The path to the contract to upload")
    parser.add_argument(
        "contract_address",
        nargs="?",
        type=Address,
        help="The address of the contract is already deployed",
    )
    return parser.parse_args()

def main():
    args = _parse_commandline()

    wallet = LocalWallet(PrivateKey("T7w1yHq1QIcQiSqV27YSwk+i1i+Y4JMKhkpawCQIh6s="))

    ledger = LedgerClient(NetworkConfig.latest_stable_testnet())

    contract = LedgerContract(args.contract_path, ledger, address=args.contract_address)
    contract.deploy({}, wallet)

    print(f"Contract deployed at: {contract.address}")

    result = contract.query({"get": {"owner": str(wallet.address())}})
    print("Initial state:", result)

    contract.execute({"set": {"value": "foobar"}}, wallet).wait_to_complete()

    result = contract.query({"get": {"owner": str(wallet.address())}})
    print("State after set:", result)

    contract.execute({"clear": {}}, wallet).wait_to_complete()

    result = contract.query({"get": {"owner": str(wallet.address())}})
    print("State after clear:", result)

if __name__ == "__main__":
    main()


"""Filesystem utility functions."""
from __future__ import absolute_import
import os, glob
import errno
import torch
import logging

from ..config import cfg

def save_checkpoint(args, model, epoch, optimizer=None, lr_scheduler=None, is_best=False):
    """Save Checkpoint"""
    directory = os.path.expanduser(cfg.TRAIN.MODEL_SAVE_DIR)
    if not os.path.exists(directory):
        os.makedirs(directory)
    model_state_dict = model.module.state_dict() if hasattr(model, 'module') else model.state_dict()
    filename = '{}_epoch_{}.pth'.format(cfg.TIME_STAMP, str(epoch))
    if is_best:
        best_filename = 'best_model.pth'
        best_filename = os.path.join(directory, best_filename)
        torch.save(model_state_dict, best_filename)
    else:
        pre_filename = glob.glob('{}*.pth'.format(cfg.TIME_STAMP))
        try:
            for p in pre_filename:
                os.remove(p)
        except OSError as e:
            logging.info(e)

        # save epoch
        save_state = {
            'epoch': epoch,
            'state_dict': model_state_dict,
            'optimizer': optimizer.state_dict(),
            'lr_scheduler': lr_scheduler.state_dict()
        }
        filename = os.path.join(directory, filename)
        if not args.distributed or (args.distributed and args.local_rank % args.num_gpus == 0):
            torch.save(save_state, filename)
            # logging.info('Epoch {} model saved in: {}'.format(epoch, filename))

def makedirs(path):
    """Create directory recursively if not exists.
    Similar to `makedir -p`, you can skip checking existence before this function.
    Parameters
    ----------
    path : str
        Path of the desired dir
    """
    try:
        os.makedirs(path)
    except OSError as exc:
        if exc.errno != errno.EEXIST:
            raise


"""Parse output from clang-tidy's stdout"""
import argparse
import pathlib
import json
from typing import Dict, Union, List
import logging

log_commander = logging.getLogger("PylintMatcher")
log_commander.setLevel(logging.DEBUG)  # be sure that log commands are output
console_handler = logging.StreamHandler()  # Create special stdout stream handler
console_handler.setFormatter(logging.Formatter("%(message)s"))  # no formatted log cmds
log_commander.addHandler(console_handler)  # Use special handler for log_commander
log_commander.propagate = False  # prevent duplicate messages in the parent logger obj

def annotate_pylint_note(obj: Dict[str, Union[int, str]]) -> str:
    """Translate a 1 notification from pylint to github's checks API.

    :param dict obj: The JSON object output by pylint (for 1 notification).
        A typical JSON object output from pylint looks like:

        .. code-block:: json

            {
                "type": "error",
                "module": "basic_test",
                "obj": "",
                "line": 3,
                "column": 19,
                "path": "tests/basic_test.py",
                "symbol": "syntax-error",
                "message": "invalid syntax (<unknown>, line 3)",
                "message-id": "E0001"
            }

    :Returns:
        A `str` that can be used by github's workflow log commands.
    """
    priority = {
        "convention": "notice",
        "refactor": "notice",
        "warning": "warning",
        "error": "error",
        "fatal": "error",
    }
    return (
        "::{level} file={path},line={line},title={path}:{line}:{col} {symbol} [{code}]"
        "::{msg}".format(
            level=priority[obj["type"]],
            path=obj["path"],
            line=obj["line"],
            col=obj["column"],
            symbol=obj["symbol"],
            code=obj["message-id"],
            msg=obj["message"],
        )
    )

if __name__ == "__main__":
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument("json_output", type=pathlib.Path)
    args = arg_parser.parse_args()

    pylint_result: List[Dict[str, Union[int, str]]] = json.loads(
        pathlib.Path(args.json_output).read_text(encoding="utf-8")
    )
    for note in pylint_result:
        log_commander.info(annotate_pylint_note(note))


from itertools import count
from tkinter import *
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.backends import default_backend
from tkinter import messagebox

import os

class EncryptDecryptFolder:
    def __init__(self, controller):
        # Instance Controlleur
        self.controller = controller
        self.stop_process_bool = False

    def stop_process(self):
        self.stop_process_bool = True

    def start_encrypt_folder(self, path, type_encryption, key_size, mode, delete, key):
        try:
            # Si le dossier n'existe pas on lance une exception
            if not os.path.exists(path):
                raise FileNotFoundError("Folder path does not exist: {}".format(path))

            # Si la clé est False alors on génére
            if not key:
                # Générer une clé de cryptage
                key_size = int(key_size)
                key = os.urandom(key_size // 8)

                # Sauvegarder la clé dans un fichier
                key_file_path = self.controller.save_key_file()

                with open(key_file_path, 'wb') as key_file:
                    key_file.write(key)

            # Parcourir tous les fichiers et dossiers récursivement
            for root, dirs, files in os.walk(path):
                for file_name in files:
                    # Si l'utilisateur arrête le processus on arrête
                    if self.stop_process_bool:
                        raise Exception("The process was terminated by the user.")

                    # Construire le chemin complet du fichier
                    file_path = os.path.join(root, file_name)

                    # Encrypter le fichier
                    self.encrypt_file(file_path, type_encryption, key, mode, delete)

            self.controller.update_task(100, 'Encryption complete.')

            if delete:
                self.controller.show_message(
                    'Finished task', 'The files have been successfully encrypted and the original files have been deleted.')
            else:
                self.controller.show_message(
                    'Finished task', 'The files were successfully encrypted, but the original files were not deleted.')
            # Arrêter la barre de progression
            self.controller.stop_task()
            self.stop_process_bool = False

        except Exception as e:
            # Stopper la barre
            self.controller.stop_task()
            # Si l'utilisateur a arrêté le processus on rentre
            if self.stop_process_bool:
                self.controller.show_message('Stopping the process', str(e))
            else:
                messagebox.showerror('Error', str(e))
            self.stop_process_bool = False

    # Méthode qui sera appelée pour chaque fichier
    def encrypt_file(self, file_path, type_encryption, key, mode, delete):
        try:

            self.controller.update_task(10, 'Encrypting: ' + file_path + ' ...')

            # Extraire le nom de fichier et l'extension
            file_name_without_extension, file_extension = os.path.splitext(file_path)

            # Initialiser le vecteur d'initialisation (IV) aléatoire ou nonce
            iv_nonce = os.urandom(16)  # Générer un IV aléatoire

            # Ouvrir le fichier source en mode lecture binaire
            with open(file_path, 'rb') as file:
                plaintext = file.read()

            self.controller.update_task(50, 'Encrypting: ' + file_path + ' ...')

            # Initialiser le chiffreur avec le mode de cryptage sélectionné
            if type_encryption == "AES":
                if mode in ['CBC', 'CFB', 'OFB']:
                    cipher = Cipher(algorithms.AES(key), getattr(modes, mode)(iv_nonce), backend=default_backend())
                else:
                    cipher = Cipher(algorithms.AES(key), getattr(modes, mode)(), backend=default_backend())

                # Remplir le texte en clair pour qu'il soit un multiple de la taille du bloc
                padder = padding.PKCS7(algorithms.AES.block_size).padder()
                padded_plaintext = padder.update(plaintext) + padder.finalize()

                encryptor = cipher.encryptor()
                ciphertext = encryptor.update(padded_plaintext) + encryptor.finalize()

            elif type_encryption == "ChaCha20":
                cipher = Cipher(algorithms.ChaCha20(key, iv_nonce), mode=None, backend=default_backend())
                encryptor = cipher.encryptor()
                ciphertext = encryptor.update(plaintext) + encryptor.finalize()

            # Construire le nouveau nom de fichier chiffré
            encrypted_file_name = f"{file_name_without_extension}{file_extension}.enc"

            # Chemin complet du fichier chiffré
            encrypted_file_path = os.path.join(os.path.dirname(file_path), encrypted_file_name)

            # Écrire le fichier chiffré
            with open(encrypted_file_path, 'wb') as file:
                if mode in ['CBC', 'CFB', 'OFB']:
                    # Écrire le vecteur d'initialisation en premier
                    file.write(iv_nonce)

                file.write(ciphertext)

            self.controller.update_task(100, 'Encrypting: ' + file_path + ' ...')
            # Supprimer le fichier source si l'option de suppression est activée
            if delete:
                os.remove(file_path)

        except Exception as e:
            # Stopper la barre
            self.controller.stop_task()
            # Si l'utilisateur a arrêté le processus on rentre
            if self.stop_process_bool:
                self.controller.show_message('Stopping the process', str(e))
            else:
                messagebox.showerror('Error', str(e))
            self.stop_process_bool = False

    # Décrypter les dossiers
    def start_decrypt_folder(self, path, type_encryption, key, mode, delete):
        try:
            # Ouvrir le fichier contenant la clé
            with open(key, 'rb') as key_file:
                key = key_file.read()

            # Parcourir récursivement tous les fichiers et dossiers
            for root, dirs, files in os.walk(path):
                # Parcourir tous les fichiers du dossier actuel
                for file_name in files:
                    # Si l'utilisateur arrête le processus on arrête
                    if self.stop_process_bool:
                        raise Exception("The process was terminated by the user.")

                    # Chemin complet du fichier source
                    file_path = os.path.join(root, file_name)

                    self.controller.update_task(10, 'Decrypting: ' + file_path + ' ...')

                    # Vérifier si le chemin correspond à un fichier avec l'extension .enc
                    if file_name.endswith(".enc"):
                        # Extraire le nom de fichier et l'extension
                        file_name_without_extension, _ = os.path.splitext(file_name)

                        # Ouvrir le fichier chiffré en mode lecture binaire
                        with open(file_path, 'rb') as file:
                            # Le vecteur d'initialisation est stocké dans les 16 premiers octets
                            iv_nonce = file.read(16)
                            ciphertext = file.read()

                        self.controller.update_task(50, 'Decrypting: ' + file_path + ' ...')

                        if type_encryption == "AES":
                            # Initialiser le déchiffreur AES avec la clé et le mode choisi
                            cipher = Cipher(algorithms.AES(key), getattr(modes, mode)(iv_nonce), backend=default_backend())
                            # Créer le déchiffreur AES en mode de déchiffrement
                            decryptor = cipher.decryptor()
                            # Déchiffrer le texte chiffré
                            padded_plaintext = decryptor.update(ciphertext) + decryptor.finalize()
                            # Créer un unpadder pour enlever le padding du texte déchiffré
                            unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()
                            # Enlever le padding du texte déchiffré
                            plaintext = unpadder.update(padded_plaintext) + unpadder.finalize()

                        elif type_encryption == "ChaCha20":
                            # Initialiser le déchiffreur ChaCha20 avec la clé et le nonce
                            cipher = Cipher(algorithms.ChaCha20(key, iv_nonce), mode=None, backend=default_backend())
                            # Créer le déchiffreur ChaCha20 en mode de déchiffrement
                            decryptor = cipher.decryptor()
                            # Déchiffrer le texte chiffré
                            plaintext = decryptor.update(ciphertext) + decryptor.finalize()

                        self.controller.update_task(70, 'Decrypting: ' + file_path + ' ...')

                                                # Construire le nouveau nom de fichier déchiffré
                        decrypted_file_name = f"{file_name_without_extension}"

                        # Chemin complet du fichier déchiffré
                        decrypted_file_path = os.path.join(root, decrypted_file_name)

                        # Écrire le fichier déchiffré
                        with open(decrypted_file_path, 'wb') as file:
                            file.write(plaintext)

                        self.controller.update_task(90, 'Decrypting: ' + file_path + ' ...')

                        # Supprimer le fichier chiffré si l'option de suppression est activée
                        if delete:
                            os.remove(file_path)

            self.controller.update_task(100, 'Decryption complete.')
             # Message box
            if delete:
                self.controller.show_message(
                    'Finished task', 'The files were successfully decrypted and the encrypted file was deleted.')
            else:
                self.controller.show_message(
                    'Finished task', 'The files were successfully decrypted, but the encrypted files were not deleted.')

            # Stopper la barre
            self.controller.stop_task()
            self.stop_process_bool = False

        except Exception as e:
            # Stopper la barre
            self.controller.stop_task()
            # Si l'utilisateur a arrêté le processus on rentre
            if self.stop_process_bool:
                self.controller.show_message('Stopping the process', str(e))
            else:
                messagebox.showerror('Error', str(e))
            self.stop_process_bool = False


import torch
import argparse
import numpy as np
import torch.nn as nn
import torch.optim as optim

from net import GraphSage
from data import CoraData
from sampling import multihop_sampling

# 训练setting，超参设置
parser = argparse.ArgumentParser()
parser.add_argument('--no-cuda', action='store_true', default=False,
                    help='无法用cuda训练')
parser.add_argument('--fastmode', action='store_true', default=False,
                    help='使用训练网络来求验证集的正确率')
parser.add_argument('--seed', type=int, default=42,
                    help='torch的随机种子')
parser.add_argument('--epochs', type=int, default=20,
                    help='训练代数')
parser.add_argument('--btach_size', type=int, default=16,
                    help='批处理大小')
parser.add_argument('--num_batch_per_epoch', type=int, default=20,
                    help='每个epoch循环的批次数')
parser.add_argument('--hidden_dim', type=list, default=[128, 7],
                    help='隐藏单元节点数')
parser.add_argument('--num_neighbors_list', type=list, default=[10, 10],
                    help='每阶采样邻居的节点数')
parser.add_argument('--weight_decay', type=float, default=5e-4,
                    help='权重衰减，L2正则化前面的参数')
parser.add_argument('--lr', type=float, default=0.01,
                    help='初始学习率')

# 如果程序不禁止cuda且当前主机有的gpu可调用,arg.cuda就为True
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()

DEVICE = "cuda" if args.cuda else "cpu"

# Note: 采样的邻居阶数需要与GCN的层数保持一致
assert len(args.hidden_dim) == len(args.num_neighbors_list)

np.random.seed(args.seed)
torch.manual_seed(args.seed)

if args.cuda:
    torch.cuda.manual_seed(args.seed)

data = CoraData().data

x = data.x / data.x.sum(1, keepdims=True)  # 归一化数据，使得每一行和为1

train_index = np.where(data.train_mask)[0]
train_label = data.y
test_index = np.where(data.test_mask)[0]
val_index = np.where(data.val_mask)[0]

input_dim = x.shape[1]    # 输入维度

model = GraphSage(input_dim=input_dim,
                  hidden_dim=args.hidden_dim,
                  num_neighbors_list=args.num_neighbors_list).to(DEVICE)
print(model)
criterion = nn.CrossEntropyLoss().to(DEVICE)
optimizer = optim.Adam(model.parameters(),
                       lr=args.lr,
                       weight_decay=args.weight_decay)

def train():
    model.train()
    for e in range(args.epochs):
        for batch in range(args.num_batch_per_epoch):
            batch_src_index = np.random.choice(train_index,
                                               size=(args.btach_size,))
            batch_src_label = torch.from_numpy(train_label[batch_src_index]).long().to(DEVICE)
            batch_sampling_result = multihop_sampling(
                src_nodes=batch_src_index,
                sample_nums=args.num_neighbors_list,
                neighbor_table=data.adjacency_dict)
            batch_sampling_x = [torch.from_numpy(x[idx]).float().to(DEVICE)
                                for idx in batch_sampling_result]
            batch_train_logits = model(batch_sampling_x)
            loss_train = criterion(batch_train_logits, batch_src_label)
            accuarcy_train = torch.eq(batch_train_logits.max(1)[1], batch_src_label).float().mean().item()
            optimizer.zero_grad()  # 先将梯度归零
            loss_train.backward()  # 反向传播计算得到每个参数的梯度值
            optimizer.step()  # 最后通过梯度下降执行一步参数更新
            accuarcy_val, loss_val = val()

            print('Epoch: {:03d}'.format(e),
                  'Batch: {:03d}'.format(batch),
                  'Loss_train: {:.4f}'.format(loss_train.item()),
                  'accuarcy_train：{:.4f}'.format(accuarcy_train),
                  'Loss_val: {:.4f}'.format(loss_val),
                  'acc_val：{:.4f}'.format(accuarcy_val))
        if e % 10 == 0:
            torch.save(model.state_dict(), 'model/model.pkl')
            print('第%d epoch，保存模型' % e)
    torch.save(model.state_dict(), 'model/model.pkl')

def val():
    with torch.no_grad():
        val_sampling_result = multihop_sampling(
            src_nodes=val_index,
            sample_nums=args.num_neighbors_list,
            neighbor_table=data.adjacency_dict)
        val_x = [torch.from_numpy(x[idx]).float().to(DEVICE)
                 for idx in val_sampling_result]
        val_label = torch.from_numpy(data.y[val_index]).long().to(DEVICE)
        if not args.fastmode:
            # 使用完整网络来求验证集的正确率
            model.eval()
            # 不启用 Batch Normalization 和 Dropout
            val_logits = model(val_x)
        else:
            val_logits = model(val_x)

        loss_val = criterion(val_logits, val_label).item()
        predict_y = val_logits.max(1)[1]
        accuarcy = torch.eq(predict_y, val_label).float().mean().item()
        return accuarcy, loss_val

def test():
    model.eval()
    with torch.no_grad():
        checkpoint = torch.load('model/model.pkl')
        model.load_state_dict(checkpoint)

        # 强制之后的内容不进行计算图构建
        test_sampling_result = multihop_sampling(
            src_nodes=test_index,
            sample_nums=args.num_neighbors_list,
            neighbor_table=data.adjacency_dict)
        test_x = [torch.from_numpy(x[idx]).float().to(DEVICE)
                  for idx in test_sampling_result]
        test_logits = model(test_x)
        test_label = torch.from_numpy(data.y[test_index]).long().to(DEVICE)
        predict_y = test_logits.max(1)[1]
        accuarcy = torch.eq(predict_y, test_label).float().mean().item()
        print("Test Accuracy: ", accuarcy)

if __name__ == '__main__':
    train()
    test()


import os
import cv2
import numpy
from PIL import Image
import wsq
import shutil
import sys

class FingerData:

  def __init__(self, sample, file):
    self.main = file
    self.sample = sample
    # self.home = homeDir

  # Compares sample file with fingerprint entries
  def match(self):

    # sampleFile = os.listdir(self.destination)   # assigning sample file a variable
    sampleFile = numpy.array(Image.open("Temp/"+self.sample))
    best_score = 0.0

    # # Group finger data into an array
    # self.fileCount = len(os.listdir(self.dir))

    # if (self.fileCount%self.modulo):
    #     splitNum = self.fileCount%self.modulo
    # else:
    #     splitNum = (self.fileCount)/self.modulo

    # self.dataList = numpy.array_split(os.listdir(self.dir), splitNum)

    # for i in range(len(self.dataList)):
    #   # print("Scanning Array: "+str(i))
    #   if (os.path.exists(self.home+"Temp")):
    #     shutil.rmtree(self.home+"Temp")

    #   for file in self.dataList[i]:
    #     filePath = self.dir+file
    #     self.sample_conversion(filePath, "Temp")

    #     filePath = self.home+"Temp/"+file+".png"
    dataFile = numpy.array(Image.open("Temp/"+self.main))

    # Matching Algorithm
    sift = cv2.SIFT_create()

    keypoints_1, descriptors_1 = sift.detectAndCompute(sampleFile, None)
    keypoints_2, descriptors_2 = sift.detectAndCompute(dataFile, None)

    matches = cv2.FlannBasedMatcher({'algorithm': 1, 'trees': 10},
                                            {}).knnMatch(descriptors_1, descriptors_2, k=2)

    match_points = []
    for p, q in matches:
        if p.distance < 0.1 * q. distance:
            match_points.append(p)

    keypoints = 0
    if len (keypoints_1) < len (keypoints_2):
        keypoints = len(keypoints_1)
    else:
        keypoints = len(keypoints_2)

    if len (match_points) / keypoints * 100 > best_score:
        best_score = len(match_points) / keypoints * 100

    if best_score > 80.0:
      return {"response": 200, "score": best_score, "Name": self.main}

    return {"response": 404, "score": best_score, "Name": self.main}

  def clean(self):
    shutil.rmtree("Temp")

class FingerConversion:
  def __init__(self, file):
    self.file = file
    self.destination = "Temp"

  def sample_conversion(self):
    finger_image = Image.open(self.file)
    finger_image = finger_image.convert('L')

    try:
        os.mkdir(self.destination)
    except:
        print(self.destination+" folder exists.")

    name, ext = os.path.splitext(self.file)
    newName = name+".png"
    finger_image.save(newName)

    try:
      shutil.move(newName, self.destination)
      return str(newName)
    except:
      os.remove(newName)
      sys.exit(name+" is already in "+ self.destination + " folder, delete and restart process")


import curses
import time

import RPi.GPIO as GPIO

MOTOR_RIGHT_IN1 = 31
MOTOR_RIGHT_IN2 = 29
MOTOR_RIGHT_ENABLE = 33
SENSOR_RIGHT = 37

MOTOR_LEFT_IN1 = 38
MOTOR_LEFT_IN2 = 40
MOTOR_LEFT_ENABLE = 32
SENSOR_LEFT = 36

GPIO.setmode(GPIO.BOARD)
GPIO.setup(MOTOR_RIGHT_IN1, GPIO.OUT)
GPIO.setup(MOTOR_RIGHT_IN2, GPIO.OUT)
GPIO.setup(MOTOR_RIGHT_ENABLE, GPIO.OUT)
GPIO.setup(MOTOR_LEFT_IN1, GPIO.OUT)
GPIO.setup(MOTOR_LEFT_IN2, GPIO.OUT)
GPIO.setup(MOTOR_LEFT_ENABLE, GPIO.OUT)

GPIO.setup(SENSOR_RIGHT, GPIO.IN)
GPIO.setup(SENSOR_LEFT, GPIO.IN)

PWM_RIGHT_ENABLE = GPIO.PWM(MOTOR_RIGHT_ENABLE, 1000)  # 1Khz
PWM_RIGHT_ENABLE.start(0)

PWM_LEFT_ENABLE = GPIO.PWM(MOTOR_LEFT_ENABLE, 1000)  # 1Khz
PWM_LEFT_ENABLE.start(0)

GPIO.output(MOTOR_RIGHT_IN1, GPIO.LOW)
GPIO.output(MOTOR_RIGHT_IN2, GPIO.LOW)
PWM_RIGHT_ENABLE.ChangeDutyCycle(0)

GPIO.output(MOTOR_LEFT_IN1, GPIO.LOW)
GPIO.output(MOTOR_LEFT_IN2, GPIO.LOW)
PWM_LEFT_ENABLE.ChangeDutyCycle(0)

# Get the curses window, turn off echoing of keyboard to screen, turn on
# instant (no waiting) key response, and use special values for cursor keys
screen = curses.initscr()
curses.noecho()
curses.cbreak()

screen.timeout(400)
screen.keypad(True)

PWM_DUTY_CYCLE_MINIMUM_PERCENT = 50
PWM_DUTY_CYCLE_MAXIMUM_PERCENT = 100
PWM_DUTY_CYCLE_PERCENT = 50

def left_motor_forward():
    GPIO.output(MOTOR_LEFT_IN1, GPIO.HIGH)
    GPIO.output(MOTOR_LEFT_IN2, GPIO.LOW)
    PWM_LEFT_ENABLE.ChangeDutyCycle(PWM_DUTY_CYCLE_PERCENT)

def left_motor_stop():
    GPIO.output(MOTOR_LEFT_IN1, GPIO.LOW)
    GPIO.output(MOTOR_LEFT_IN2, GPIO.LOW)
    PWM_LEFT_ENABLE.ChangeDutyCycle(0)

def left_motor_backward():
    GPIO.output(MOTOR_LEFT_IN1, GPIO.LOW)
    GPIO.output(MOTOR_LEFT_IN2, GPIO.HIGH)
    PWM_LEFT_ENABLE.ChangeDutyCycle(PWM_DUTY_CYCLE_PERCENT)

def right_motor_forward():
    GPIO.output(MOTOR_RIGHT_IN1, GPIO.HIGH)
    GPIO.output(MOTOR_RIGHT_IN2, GPIO.LOW)
    PWM_RIGHT_ENABLE.ChangeDutyCycle(PWM_DUTY_CYCLE_PERCENT)

def right_motor_stop():
    GPIO.output(MOTOR_RIGHT_IN1, GPIO.LOW)
    GPIO.output(MOTOR_RIGHT_IN2, GPIO.LOW)
    PWM_RIGHT_ENABLE.ChangeDutyCycle(0)

def right_motor_backward():
    GPIO.output(MOTOR_RIGHT_IN1, GPIO.LOW)
    GPIO.output(MOTOR_RIGHT_IN2, GPIO.HIGH)
    PWM_RIGHT_ENABLE.ChangeDutyCycle(PWM_DUTY_CYCLE_PERCENT)

try:
    while True:
        char = screen.getch()

        if char == curses.KEY_UP:
            print("up")
            left_motor_forward()
            right_motor_forward()
        elif char == curses.KEY_DOWN:
            print("down")
            left_motor_backward()
            right_motor_backward()
        elif char == curses.KEY_RIGHT:
            print("right")
            left_motor_forward()
            right_motor_stop()
        elif char == curses.KEY_LEFT:
            print("left")
            right_motor_forward()
            left_motor_stop()
        elif char == curses.KEY_PPAGE:
            print("DUTY CYCLE " + str(PWM_DUTY_CYCLE_PERCENT))
            PWM_DUTY_CYCLE_PERCENT += 1
            if PWM_DUTY_CYCLE_PERCENT >= PWM_DUTY_CYCLE_MAXIMUM_PERCENT:
                PWM_DUTY_CYCLE_PERCENT = PWM_DUTY_CYCLE_MAXIMUM_PERCENT
            time.sleep(0.1)
        elif char == curses.KEY_NPAGE:
            print("DUTY CYCLE " + str(PWM_DUTY_CYCLE_PERCENT))
            PWM_DUTY_CYCLE_PERCENT -= 1
            if PWM_DUTY_CYCLE_PERCENT <= PWM_DUTY_CYCLE_MINIMUM_PERCENT:
                PWM_DUTY_CYCLE_PERCENT = PWM_DUTY_CYCLE_MINIMUM_PERCENT
            time.sleep(0.1)
        else:
            print("stop motor")
            left_motor_stop()
            right_motor_stop()

finally:
    # Close down curses properly, inc turn echo back on!
    curses.nocbreak();
    screen.keypad(0);
    curses.echo()
    curses.endwin()

    left_motor_stop()
    right_motor_stop()


from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
from .views import UserPublicProfile

app_name = "users"

urlpatterns=[
	path('login/',auth_views.LoginView.as_view(template_name='users/login.html'),
												name='userLogin'),

	path('logout/',views.userLogout,name='userLogout'),
	path('signup/',views.userSignup,name='userSignup'),
	path('profile/',views.userProfile,name='userProfile'),
	path('profiles/<str:username>',UserPublicProfile.as_view(),
									name='userPublicProfile'),

	path('profile/update/',views.userProfileUpdate,name='userProfileUpdate'),

]