##########################################################
# データバッチ運用見据えて
# - Contents
# - 更新履歴
#   - 20220220 初版作成
##########################################################
from ast import Constant
from numpy import disp
from torch import batch_norm_elemt
from batch_Past import *
from batch_Scraper import *
from batch_Day import *
from batch_Lake import *
from batch_MergeCalc import *
from batch_Predict import *
import gc

class CONSTS():
    MODEL_SHEET =  ('choose_model', 'models')
    BATCH_ALL =  {
        'PastBase'  : PastBaseCreator,
        'PastTenji'  : PastTenjiCreator,
        'PastMerge'  : PastMerger,

        'LakeDaily'  : LakeCreatorDaily,
        'LakeChokuzen'  : LakeCreatorChokuzen,
        'DataMerger'  : DataMerger,
        'DataCalc'  : DataCalculater,
        'predict'  : Predicter,
        'betPicker'  : BetPicker,
        'result'  : ResultCalculater,
    }

class GspreadReader(CONSTS):
    def read_modelSheet(self):
        gs  = pd_readSheet(*self.MODEL_SHEET)
        self.modelSheet = gs[gs['is_run']=='1']

class BatchExecuterBase(CONSTS):
    NOTIFY_FILE = 'df_result.pkl'
    def __init__(self, batch_info, opdt=None):
        gc.collect()
        self.batch_info = batch_info
        self.mid = batch_info.model_id
        self.bid = batch_info.batch_id
        self.opdt = opdt
        self.batch_list = self.setBatch()

    def FILTER(self):
        pass

    def setBatch(self):
        batch_list = []
        set_mid = self.mid
        for col, CLASS_ in self.BATCH_ALL.items():
            if self.batch_info[col]=='':
                continue
            if self.batch_info[col]=='1':
                if col in self.FILTER:
                    batch_list.append(CLASS_(self.opdt, set_mid, batch_id=self.bid))
                set_mid = self.mid
            else:
                set_mid = self.batch_info[col]
        print(f'batch_id {self.bid}: n_batch:{len(batch_list)}')
        return batch_list

    def _notify(self):
        file_path = f'{"pipe_data"}/{self.NOTIFY_FILE}'
        self.df_notify = pd.read_pickle(file_path)
        notify_dataframe(self.df_notify)

class BatchExecuterMorning(BatchExecuterBase):
    FILTER = ['LakeDaily']

class BatchExecuterEachRace(BatchExecuterBase):
    FILTER = ['LakeChokuzen', 'LastCalc', 'predict']

class BatchExecuterAll(BatchExecuterBase):
    FILTER = list(CONSTS.BATCH_ALL.keys())

class BatchManager(GspreadReader):
    def __init__(self, opdt):
        self.opdt = opdt

    def setMorning(self):
        self.BEs = []
        for i, batch_info in self.modelSheet.iterrows():
            self.BEs.append(BatchExecuterMorning(batch_info, self.opdt))

    def setEachRace(self):
        self.BEs = []
        for i, batch_info in self.modelSheet.iterrows():
            self.BEs.append(BatchExecuterEachRace(batch_info, self.opdt))

    def setAll(self):
        self.BEs = []
        for i, batch_info in self.modelSheet.iterrows():
            self.BEs.append(BatchExecuterAll(batch_info, self.opdt))

    def executeBatch(self):
        for BE in self.BEs:
            for cls_ in BE.batch_list:
                cls_.main()
            BE._notify()

D_FMT = '%Y%m%d'
class BatchPast():
    Batchers =[
        PastTenjiScraper,
        PastTenjiConverter,

        PastBaseCreator,
        PastTenjiCreator,
        PastTenjiCreator,
        PastMerger,
    ]

    def __init__(self, opdt):
        # opdt = (datetime.strptime(opdt, D_FMT) - timedelta(days=1)).strftime(D_FMT)
        self.opdt = opdt

    def main(self):
        for Batcher in self.Batchers:
            Batcher(self.opdt).main()

class BatchDay():
    Batchers =[
        DayCreator,
        LakeCreatorDaily,
        LakeCreatorMotor,
    ]

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

    def main(self):
        for Batcher in self.Batchers:
            Batcher(self.opdt, self.mid).main()

class BatchEach():
    Batchers =[
        DayCreatorChokuzen,
        LakeCreatorChokuzen,
        LakeCreatorJyo,

    ]
    def __init__(self, opdt):
        self.opdt = opdt

    def main(self):
        for Batcher in self.Batchers:
            Batcher(self.opdt).main()

class BatchPredict():
    Batchers =[
        DataMerger,
        DataCalculater,
        Predicter,
    ]

    def __init__(self, opdt, mid):
        self.opdt = opdt
        self.mid = mid

    def main(self):
        for Batcher in self.Batchers:
            Batcher(self.opdt, self.mid).main()

class BatchResult():
    def __init__(self, opdt, mid, isChokuzen=True):
        self.opdt = opdt
        self.mid = mid
        self.isChokuzen =isChokuzen

    def main(self):
        try:
            # Expecter(self.opdt, self.mid).main()
            Expecter(self.opdt, self.mid, isChokuzen=self.isChokuzen).main()
            for bet_thresh in [1, 1.5, 2]:
                BetPicker(opdt=self.opdt, mid=self.mid, bet_thresh=bet_thresh).main()
            ResultCalculater(self.opdt, self.mid).main()
        except:
            pass

def show_all_result(pipe='result'):

    df = pd.concat(map(pdreadpickle,glob.glob(f'pipe_data/{pipe}/*.pkl')))

    return df


import os
import sys
import zipfile
import tempfile
import subprocess
from typing import List

from cloudminer.logger import logger
from cloudminer.exceptions import CloudMinerException

RESOURCES_DIRECTORY = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))), "resources")
PROJECT_BANNER = """
  /$$$$$$  /$$                           /$$ /$$      /$$ /$$                                        /$$
 /$$__  $$| $$                          | $$| $$$    /$$$|__/                                      /$$$$$$
| $$  \__/| $$  /$$$$$$  /$$   /$$  /$$$$$$$| $$$$  /$$$$ /$$ /$$$$$$$   /$$$$$$   /$$$$$$        /$$__  $$
| $$      | $$ /$$__  $$| $$  | $$ /$$__  $$| $$ $$/$$ $$| $$| $$__  $$ /$$__  $$ /$$__  $$      | $$  \__/
| $$      | $$| $$  \ $$| $$  | $$| $$  | $$| $$  $$$| $$| $$| $$  \ $$| $$$$$$$$| $$  \__/      |  $$$$$$
| $$    $$| $$| $$  | $$| $$  | $$| $$  | $$| $$\  $ | $$| $$| $$  | $$| $$_____/| $$             \____  $$
|  $$$$$$/| $$|  $$$$$$/|  $$$$$$/|  $$$$$$$| $$ \/  | $$| $$| $$  | $$|  $$$$$$$| $$             /$$  \ $$
 \______/ |__/ \______/  \______/  \_______/|__/     |__/|__/|__/  |__/ \_______/|__/            |  $$$$$$/
                                                                                \_  $$_/
                                                                                \__/

\n-- CloudMiner: v1.0.0 (SafeBreach Labs) --\n"""

def get_temp_file_path(file_name: str) -> str:
    """
    Returns the user temp directory
    """
    temp_dir = tempfile.gettempdir()
    return os.path.join(temp_dir, file_name)

def get_file_extension(file_path: str):
    """
    Returns the file extension from file path
    """
    return os.path.basename(os.path.splitext(file_path)[1])

def get_file_name(file_path: str):
    """
    Returns the file name from full file path
    """
    return os.path.basename(os.path.splitext(file_path)[0])

def zip_file(source_file: str, file_name_within_archive: str = None, zip_path: str = None) -> str:
    """
    Zip a file

    :param source_file: File path to zip
    :param file_name_within_archive: Name of file within the zip archive
    :param zip_path: Path to the zipped file. If None, will use the temp directory

    :return: zip file path
    """
    src_file_name = get_file_name(source_file)
    if zip_path is None:
        zip_path = get_temp_file_path(f"{src_file_name}.zip")

    if file_name_within_archive is None:
        file_name_within_archive = src_file_name

    with zipfile.ZipFile(zip_path, "w") as zipf:
        zipf.write(source_file, arcname=file_name_within_archive)

    logger.debug(f"Zip file created at '{zip_path}'")
    return zip_path

def run_command(cmd: List[str], shell: bool = True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs) -> subprocess.CompletedProcess:
    """
    Helper function to run commands
    """
    logger.debug(f"Running command: '{subprocess.list2cmdline(cmd)}'")
    return subprocess.run(cmd,
                          shell=shell,
                          text=text,
                          stdout=stdout,
                          stderr=stderr,
                          **kwargs)

def package_to_whl(package_path: str) -> str:
    """
    Create a whl file from a given Python package
        whl file wil lbe saved in the dist directory

    :raises CloudMinerException: If failed to create whl file

    :return: whl file path
    """
    package_name = os.path.basename(package_path)
    setup_file = os.path.join(package_path, "setup.py")
    dist_dir_path = os.path.join(package_path, "dist")

    logger.debug(f"Creating a .whl file for package - '{package_name}'")
    process = run_command(["python", setup_file, "bdist_wheel"], cwd=package_path)
    if process.returncode != 0:
        raise CloudMinerException(f"Failed to create .whl file. Error: {process.stderr}")

    try:
        whl_file_name = os.listdir(dist_dir_path)[0]
    except IndexError:
        raise CloudMinerException(f"Failed to find the creatd .whl file in folder '{dist_dir_path}'.")

    logger.info(f"whl file successfully created - '{whl_file_name}'")
    return os.path.join(dist_dir_path, whl_file_name)


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

import csv
import platform
from utilities import get_output_path

'''
输入表格：
0:部门 1:姓名 2:考勤号码 3:日期时间 4:机器号 5:编号(空) 6:比对方式 7:卡号

输出表格：
0:部门 1:姓名 2:考勤号码 3:工作日 4:上班时间 5:下班时间 6:比对方式 7:出勤情况 8:工时

缺勤统计表：
0:部门 1:姓名 2:考勤号码 3:工作日 4:上班时间 5:下班时间 6:比对方式 7:出勤情况 8:工时
'''

'''
读取统计表，查找缺勤日期，最后生成一张统计表记录一个月期间不正常打卡的记录
:param input_path - 统计表文件 statistics.csv 所在路径
:return workdays - 工作日列表
'''
def find_absence(input_path, workdays, encoding='gbk'):

    # 字典类型，记录员工出勤日期，键为姓名，值为一个列表，存储了有打卡记录的日期
    staff_attendance = dict()
    # 字典类型，记录员工的信息，键为姓名，值为一个元组: (部门名称, 考勤号)
    staff_info = dict()
    # 出勤表，记录Excel表格中每一行的信息
    attendance_list = []

    # 读取前面生成的统计表
    with open('/Users/hayden/Desktop/statistics.csv', 'r', encoding=encoding) as csv_file:
        reader = csv.reader(csv_file)
        for row in reader:
            if row[0] != '部门':
                # 将记录一条加入到出勤表中
                attendance_list.append(row)
                # 将出勤天数加入到字典中
                if row[1] not in staff_attendance:
                    staff_info[row[1]] = (row[0], row[2])
                    staff_attendance[row[1]] = [row[3]]
                else:
                    staff_attendance[row[1]].append(row[3])

    # 用当月工作日减去实际出勤天数，得出每个员工当月未上班天数
    for staff in staff_attendance:
        staff_attendance[staff] = list(set(workdays) - set(staff_attendance[staff]))

    # print('缺勤记录: ')
    for staff in staff_attendance:
        if staff_attendance[staff]:
            # 对未出勤日期进行排序
            staff_attendance[staff] = sorted(staff_attendance[staff], key=lambda x: int(x.split('/')[2]))
            # print('%s: 缺勤%d天 %s' % (staff, len(dic[staff]), sorted(dic[staff], key=lambda x: int(x.split('/')[2]))))

    '''
    遍历attancanceList，同时向其中添加未打卡的天数的信息
    '''
    absence_list = attendance_list[:]

    for (staff, dates) in staff_attendance.items():
        for date in dates:
            absence_list.append(
                [staff_info[staff][0], staff, staff_info[staff][1], date, '00:00:00', '00:00:00', 0, '未打卡'])

    # 各条记录排序: 1.按部门 2.按姓名 3.按日期
    absence_list = sorted(absence_list, key=lambda x: (x[0], x[1], int(x[3].split('/')[2])))

    '''
    前一天晚上奋斗到22点以后的，抵扣第二天的迟到
    '''
    print('豁免记录：')
    for index in range(1, len(absence_list)):
        if '迟到' in absence_list[index][7]:
            # 加班豁免时间按部门而定，技术部为22点以后，其他部门为21点以后。
            overtime = 22 if absence_list[index][0] == '技术部' else 21
            # 检查前一天
            if absence_list[index - 1][1] == absence_list[index][1] \
                    and int(absence_list[index - 1][5].split(':')[0]) >= overtime:
                # 打印一下抵扣迟到的记录
                print('%s (%s, %s): 前一天下班时间 %s, 当天上班时间 %s'
                      % (absence_list[index][3], absence_list[index][0], absence_list[index][1], absence_list[index - 1][5], absence_list[index][4]))
                # print(absence_list[index])
                # 如果只有迟到的，标为正常。如果当天还有别的记录，仅仅抹去迟到
                if absence_list[index][7] == '迟到':
                    absence_list[index][7] = '正常'
                else:
                    absence_list[index][7] = absence_list[index][7][2:]

    # 移去正常的记录，只保留不正常的
    absence_list = [x for x in absence_list if x[7] != '正常']
    # print(absence_list)

    # 移去工作日仍然上班的记录
    # print(workdays)
    # print(len(absence_list))
    absence_list = list(filter(lambda x: x[3] in workdays, absence_list))
    # print(len(absence_list))

    # TODO: 如果不是一整周的话，多输入一天以计算缺勤记录，最后要把这一天删去
    ##########
    # absence_list = list(filter(lambda x: x[3] not in '2017/6/8', absence_list))
    ##########

    '''
    写入到csv文件中
    '''
    # 生成输出路径
    csv_path = get_output_path(input_path, 'absence.csv')

    with open(csv_path, 'w', encoding=encoding) as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow(['部门', '姓名', '考勤号码', '工作日', '上班时间', '下班时间', '工时', '出勤情况'])
        # 循环将每一行依次写入到csv文件中
        for row in absence_list:
            writer.writerow(row)


n,k = list(map(int,input().split()))
count  = 0
while n != 1:
    a = n % k
    if a == 0:
        n = n//k
        count += 1
    else:
        n = n - a
        count += a
print(count)


#matrix.py

import numpy as np

ALPHABET_SIZE = 27
MATRIX_SIZE = 2

SMALL_ALPHABET = [" ", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
CAPITAL_ALPHABET = [" ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
ALPHABET_VALUES = list(range(ALPHABET_SIZE))

def string_to_values(input_string):
    values = []
    for char in input_string:
        if char in SMALL_ALPHABET:
            values.append(ALPHABET_VALUES[SMALL_ALPHABET.index(char)])
        elif char in CAPITAL_ALPHABET:
            values.append(ALPHABET_VALUES[CAPITAL_ALPHABET.index(char)])
        else:
            raise ValueError(f"Invalid character '{char}' in input string")
    return values

def pad_values(values):
    if len(values) % MATRIX_SIZE == 1:
        values.append(ALPHABET_VALUES[0])
    return values

def values_to_matrix(values):
    n = len(values) // MATRIX_SIZE
    return np.array(values[:n*MATRIX_SIZE]).reshape((MATRIX_SIZE, n))

def matrix_to_values(matrix):
    return matrix.flatten().tolist()

def encrypt(input_string, encoding_matrix):
    values = string_to_values(input_string)
    padded_values = pad_values(values)
    matrix = values_to_matrix(padded_values)
    encrypted_matrix = np.matmul(encoding_matrix, matrix)
    encrypted_values = matrix_to_values(encrypted_matrix)
    return encrypted_values

def decrypt(encrypted_values, encoding_matrix):
    decrypted_values = np.matmul(np.linalg.inv(encoding_matrix), np.array(encrypted_values).reshape((MATRIX_SIZE, -1)))
    decrypted_values = [int(round(val)) for val in decrypted_values.flatten()]
    decrypted_values = [val % ALPHABET_SIZE for val in decrypted_values]
    decrypted_string = "".join([SMALL_ALPHABET[val] for val in decrypted_values])
    return decrypted_string

        # a, b, c, d = map(int, input("Enter the encoding matrix as comma-separated values: ").split(","))
        # encoding_matrix = np.array([[a, b], [c, d]])
        # encrypted_values = list(map(int, input("Enter the encrypted form matrix as comma-separated values: ").split(",")))
        # decrypted_string = decrypt(encrypted_values, encoding_matrix)
        # print(f"Decrypted form = {decrypted_string}")


"""
1.piemērs
a=25
b=0.35
# izvadīt terminālu, kurā ir iekļauti mainīgo vērtības!
print("Eimija nopirka", a, "saldējumus.")

# 2.piemers
vards=Eimija
vecums=4
# Izmantojot f-string (sākot ar Python 3.6+)
print(f"Mani sauc {vards} un man ir {vecums} gadi.")

# 3.piemers
# Izmantojot format mtodi
vards="Eimija"
vecums=4
zinama_virkne="Mani sauc {} un man ir {} gadi."
print(zinama_virkne.format(vards,vecums))
"""
#4.piemers
#Izmantojot % operātoru (vecais vids, taču tas joprojām darbojas)
vards = "Anna"
vecums = 30

zinama_virkne = "Mani sauc %s un man ir %d gadi." % (vards, vecums)
print(zinama_virkne)


from couchdb import Server
from couchdb.design import ViewDefinition
from config import COUCHDB_URL

_DOCBASES = ['rating']

class Activ(Server):
    "Active database"
    rating = {}

    def __init__(self, url):
        Server.__init__(self, url)
        act = self
        test_and_create = lambda doc: doc in act and act[doc] or act.create(doc)
        for attribute in _DOCBASES:
            setattr(Activ, attribute, test_and_create(attribute))

    def erase_database(self):
        'erase tables'
        for table in _DOCBASES:
            try:
                del self[table]
            except:
                pass

__ACTIV = Activ(COUCHDB_URL)
RATING = __ACTIV.rating

################################################
# CouchDB Permanent Views
################################################
#
# Retorna todas as avaliações de um objeto ou
# Retorna o valor da avaliação que um usuário fez de um objeto
#
# Retorno:
# valor da avaliação e data em que foi feita
#
# Uso: database.RATING.view('rating/partial_data',startkey=[tipo, escopo, objeto], endkey=[tipo, escopo, objeto, {}])
#
rating_partial_data = ViewDefinition('rating','partial_data', \
                               '''function(doc) {
                                     emit([doc.tipo, doc.escopo, doc.objeto, doc.user, doc._id], {rating:doc.rating, data_cri:doc.data_cri});
                                   }
                               ''')

# Retorna a média das avaliações de um objeto
#
# Retorno:
# média das avaliações
#
# Uso: database.RATING.view('rating/mean_by_object',startkey=[tipo, escopo, objeto],endkey=[tipo, escopo, objeto, {}], group="true")

rating_mean_by_object = ViewDefinition('rating','mean_by_object', \
                              u'''
                                    // map
                                    function(doc){
                                        emit([doc.tipo, doc.escopo, doc.objeto], parseInt(doc.rating,10));
                                    }
                              ''',
                              u'''
                                    // reduce
                                    function(keys, values, rereduce) {
                                        if (!rereduce){
                                            var length = values.length
                                            return [sum(values) / length, length]
                                        }else{
                                            var length = sum(values.map(function(v){return v[1]}))
                                            var avg = sum(values.map(function(v){
                                                return v[0] * (v[1] / length)
                                                }))
                                            return [avg, length]
                                        }
                                    }
                              ''')

ViewDefinition.sync_many(RATING, [rating_partial_data, rating_mean_by_object])


'''
Reads product_name from .json files, identifies their cat/sub_cat and populates mongodb
Note: Make sure you change vendor name and path before running this file. Default vendor name is : 'HQ-Data'
'''
import os
import re
import sys
import numpy as np
#import logging
import json
import traceback
from multiprocessing import Pool
import csv

from pymongo import MongoClient
client = MongoClient()
db = client.products_db

result_file = "/data/cat_multi/results_"
from flask import Flask, request, Response
from sklearn.externals import joblib
#from config_details import second_level_cat_names
#from logging.handlers import RotatingFileHandler

#from Train_Model.train import ngrams
PARENT_DIR_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
sys.path.append(PARENT_DIR_PATH)

second_level_cat_names=\
            ["Beauty Products and Personal Care",
                  "Camera and Photos",
                  "Mobile Phone, Tablets and Accesories",
                  "Apparel & Accessories",
                  "Watches, Eyewear and Jewellery",
                  "Electronics and Appliances",
                  "Home and Kitchen",
                  "Computers and Laptops",
                  "Shoes and Footwear"
                 ]

second_level_cat_names_set=set(second_level_cat_names)

#logging file path
#LOGGING_PATH='/var/log/cat_subcat_logs/cat_subcat.log'

app = Flask(__name__)
"""
handler = RotatingFileHandler(LOGGING_PATH,maxBytes=10000000,backupCount=5)
formatter = logging.Formatter("%(asctime)s;%(levelname)s;%(message)s")
handler.setFormatter(formatter)
app.logger.addHandler(handler)
app.logger.setLevel(logging.INFO)

app.logger.info("Loading Process Started")
"""
vectorizer=joblib.load(PARENT_DIR_PATH+'/Models/vectorizer.pkl')
clf_bayes=joblib.load(PARENT_DIR_PATH+'/Models/clf_bayes.pkl')
clf_chi=joblib.load(PARENT_DIR_PATH+'/Models/clf_chi.pkl')
clf_fpr=joblib.load(PARENT_DIR_PATH+'/Models/clf_fpr.pkl')

second_level_vectorizer={}
second_level_clf_bayes={}
second_level_clf_fpr={}
for cat_name in second_level_cat_names_set:
    second_level_vectorizer[cat_name]=joblib.load(PARENT_DIR_PATH+'/Models/SubModels/Vectorizer_'+cat_name)
    second_level_clf_bayes[cat_name]=joblib.load(PARENT_DIR_PATH+'/Models/SubModels/clf_bayes_'+cat_name)
    second_level_clf_fpr[cat_name]=joblib.load(PARENT_DIR_PATH+'/Models/SubModels/clf_fpr_'+cat_name)

#app.logger.info("Loading Process Complete")

def predict_category(product_name):
#    app.logger.info("Request received {}".format(str(product_name)))

    try:
        l_product_name = product_name.lower()
        class1 = clf_bayes.predict(vectorizer.transform([l_product_name]))[0]
        class2_prob_vector = clf_chi.predict_proba(vectorizer.transform([l_product_name]))[0]
        class3_prob_vector = clf_fpr.predict_proba(vectorizer.transform([l_product_name]))[0]

        if len(np.unique(class2_prob_vector))==1:
            class2 = "NA"
        else:
            class2 = clf_bayes.classes_[np.argmax(class2_prob_vector)]
        if len(np.unique(class3_prob_vector))==1:
            class3 = "NA"
        else:
            class3 = clf_bayes.classes_[np.argmax(class2_prob_vector)]

        if class3 == "Delhivery_Others":
            if class1 == class2:
                first_level = class1
            elif class1 == "Delhivery_Others":
                first_level = class2
            elif class2 == "Delhivery_Others":
                first_level = class1
            else:
                first_level = class2
        else:
            first_level = class3

        second_level = ""

        if first_level in second_level_cat_names_set:
            prob_vector = second_level_clf_fpr[first_level].predict_proba(
                second_level_vectorizer[first_level].transform([l_product_name]))[0]
            if len(np.unique(prob_vector))==1:
                second_level = second_level_clf_bayes[first_level].predict(
                    second_level_vectorizer[first_level].transform([l_product_name]))[0]
            else:
                second_level = second_level_clf_bayes[first_level].classes_[np.argmax(prob_vector)]

        # prob_vector= second_level_clf[class_name].predict_proba(
            #second_level_vectorizer[class_name].transform([product_name.lower()]))[0]

        #app.logger.info("Result produced {} --> {}".format(
            #str(first_level), str(second_level)))
    except Exception as err:
        print(
            'Traceback: {}'.format(traceback.format_exc()))
        print(
            'Exec Info: {}'.format(sys.exc_info())[0])
        print(
            'Exception {} occurred against product: {}'.format(
                err, product_name))
    return (first_level,second_level)

#@app.route('/get_category', methods = ['POST'])
def get_category(products):
    try:
        list_product_names = products
        #app.logger.info("Request Received {}".format (str(list_product_names)))
	    #print "Working in Process #%d" % (os.getpid())

        for product_name_dict in list_product_names:
            response = {}
            response['category'], response['sub_category'] = predict_category(
                    product_name_dict.get('product_name',"").encode('ascii','ignore'))

            if response['sub_category']:
                try:
                    subcat = response['sub_category'].split('->')[1]
                except IndexError:
                    subcat= response['sub_category'].split('->')[0]
            else:
                subcat = None

            if 'product_url' not in product_name_dict:
                product_name_dict['product_url'] = None
            if 'retail_price' in product_name_dict:
                product_name_dict['retail_price'] = re.findall(r'\d+',product_name_dict['retail_price'])
                product_name_dict['retail_price'] = int(product_name_dict['retail_price'][0])
            else:
                product_name_dict['retail_price'] = None

            db.products_new.insert({"product_name": product_name_dict['product_name'],
                                "vendor": 'HQ-Data',
                                "category": response['category'],
                                "vendor_cat": response['category'],
                                "sub_category": subcat,
                                "vendor_subcat": subcat,
                                "price": product_name_dict['retail_price'],
                                "product_url": product_name_dict['product_url']})

    except Exception as err:
        print(
            'Exception {} occurred against payload: {}'.format(
                err, list_product_names))
        print(str(err))

def create_pool(records, start, end, diff):
    p = Pool(processes=16)
    try:
        while start < end:
            #print records[start:start+diff]
            print(start, diff, end)
            p.apply_async(get_category, args=(records[start:start+diff], ))
            start = start + diff
        p.close()
        p.join()
    except (KeyboardInterrupt, SystemExit):
        print("Exiting...")
        p.terminate()
        p.join()

if __name__ == "__main__":
    path_to_json = os.path.join(os.path.dirname(__file__), 'data/outsource_data/snapdeal/sep/')
    json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]
    print(json_files, len(json_files))

    data = []
    records = []
    for js in json_files:
        with open(os.path.join(path_to_json, js)) as json_file:
            for line in json_file:
                data.append(json.loads(line))

    # print(data)
    for i in data:
        for j in i:
            try:
                records.append({'product_name': j['record']['product_name'],
                                'product_url': j['record']['product_url'],
                                'retail_price': j['record']['retail_price']
                                })
            except Exception as e:
                pass
    start = 0
    end = len(records)
    diff = int(end/16)
    create_pool(records, start, end, diff)


import sys as s
def disarium():
    print("Disarium number:")
    for i in range(1,100):
        arr =[int(num) for num in str(i)]
        sum=0
        for j,n in enumerate(arr):
            sum+=n**(j+1)
        if sum==int(i):
            print(sum)

def ceaser(var):
    print("Ceaser Cipher")
    text=var
    cipher=" "
    for char in text:
        var=ord(char)+3
        if(text.isupper() and var > ord('Z'))or(text.islower() and var>ord('z')):
            var=var-26
        cipher+=chr(var)
    print(cipher)

def program():
    print("1.Finding disarium Number\n2.Finding Cipher text\n3.exit")
    ch=int(input("Enter Your Choice..."))
    if ch==1:
        disarium()
    elif ch==2:
        plaintext=input("Enter any string to encrypt\n")
        ceaser(plaintext)
    elif ch==3:
        s.exit()
    else:
        print ("Invalid")

program()


import numpy as np

class Pila:
    __tope = None
    __cant = None
    __arreglo = None
    def __init__(self, cant = 0):
        self.__cant = cant
        self.__tope = -1
        self.__arreglo = np.empty(self.__cantidad, dtype=int)
    def vacia(self):
        return self.__tope == -1
    def insertar(self, numero):
        if(self.__tope < self.__cant-1):
            self.__tope += 1
            self.__arreglo[self.__tope] = numero
            return numero
        else:
            return 0
    def suprimir(self):
        if(self.vacia()):
            print('Pila vacía')
            return 0
        else:
            self.__tope-= 1
            x = self.__arreglo[self.__tope]
            np.delete(self.__arreglo, self.__tope) #añadido
            return x
    def mostrar(self):
        if(not self.vacia()):
            for i in range(self.__tope, 0):
                elemento = self.suprimir()
                if(elemento != 0):
                    print(elemento)


"""
Get detail data of github repository using GitHub GraphQL API v4
More information about Github GraphQL API v4 is here https://developer.github.com/v4/
"""
import os

from dotenv import load_dotenv
import requests

# Prepare api keys and endpoint
env_path = os.path.join(os.path.dirname(__file__), ".env")
load_dotenv(env_path)
GITHUB_API_KEY = os.environ.get('GITHUB_API_KEY')
ENDPOINT = 'https://api.github.com/graphql'

def execute_query(api_key, query_string, endpoint='https://api.github.com/graphql'):
    """
    Execute query to GitHub GraphQL API.
    :param api_key: GitHub api key. You can use personal access token.
    :param query_string:  the GitHub GraphQL query consists of a multi-line string.
    :param endpoint: GitHub GraphAL endpoint. default setting.
    :return: json type response
    """
    headers = {'Authorization': f'token {api_key}'}
    response = requests.post(endpoint, json={'query': query_string}, headers=headers)
    if response.status_code == 200:
        return response.json()
    else:
        raise Exception("Query failed to run by returning code of {}. {}".format(response.status_code, query))

def create_query_to_repo(owner_name, repo_name):
    """
    Create query to repository.
    :param owner_name: repository owner name
    :param repo_name: repository name
    :return: query
    """
    query = f"""
    query {{
      repository(owner: {owner_name}, name: {repo_name}){{
        name,
        nameWithOwner,
        description,
        createdAt,
        forkCount,
        homepageUrl
        }}
    }}
    """
    return query

query = create_query_to_repo(owner_name="crimnut02", repo_name="aframe-mmd")

json_data = execute_query(GITHUB_API_KEY, query, ENDPOINT)

print(json_data)


#! /usr/bin/python

import sys
sys.path.insert(0,'/usr/local/lib/python2.7/site-packages')
import cv2
from skimage import io

if "__main__" == __name__:
    img = io.imread("data/comic.jpg")
    border = 0
    for line in open("r.txt"):
        parts = [int(o) for o in line.strip().split(" ") if o.isdigit()]
        if len(parts) < 5:
            continue
        #img = cv2.rectangle(img,(parts[1],parts[2]),(parts[3],parts[4]),[255,0,255])
        img = cv2.rectangle(img,(parts[2]-border,parts[1]-border),(parts[4]+border,parts[3]+border),[255,0,255])
    io.imsave("data/get.jpg",img)
    #io.imshow(img)
    #io.show()


# -*- coding: utf-8 -*-
"""
TEST PLOT EPS
"""

from pathlib import Path
import pickle
import os
import sys
import pandas as pd
from plottingUtils import imagesc
import numpy as np
import matplotlib.pyplot as plt
sys.path.insert(1, 'c:\\Users\\bknorris\\Documents\\Scripts\\Postdoc\\Paper2_OptimizingRestoration\\DataAnalysis\\loadModelFiles\\')
from analysisUtils import find_files

plt.close('all')
# Define file paths:
csv_source = Path('c:/Users/bknorris/Documents/Models/Paper2_OptimizingRestoration/ModelRuns/Scenarios/')
model_source = Path('c:/Users/bknorris/Documents/Models/Paper2_OptimizingRestoration/ModelRuns/Scenarios/postProcessed')
save_fig_dir = Path('c:/Users/bknorris/Documents/Models/Paper2_OptimizingRestoration/Figures')
csv_file = 'modelPostProcessing_imagesc.csv'
version_no = 'V3'
save_figures = False

def extents(f):
    delta = f[1] - f[0]
    return [f[0] - delta / 2, f[-1] + delta / 2]

# Load CSV
csv_file = csv_source / csv_file
model_info = pd.read_csv(csv_file.open())

# Create figure
plt.style.use('_mpl-gallery')
f1 = plt.figure(figsize=(8, 10.5))
axes1 = f1.add_axes([0.1, 0.03, 0.8, 0.15])
axes2 = f1.add_axes([0.1, 0.19, 0.8, 0.15])
axes3 = f1.add_axes([0.1, 0.35, 0.8, 0.15])
axes4 = f1.add_axes([0.1, 0.51, 0.8, 0.15])
axes5 = f1.add_axes([0.1, 0.67, 0.8, 0.15])
axes6 = f1.add_axes([0.1, 0.83, 0.8, 0.15])

ax = [axes1, axes2, axes3,
      axes4, axes5, axes6]
caxis = [1e-7, 5e-5]
c_map = 'viridis'

# Load binary results file
filenames = next(os.walk(str(model_source)))[2]  # For later, [1] is dirs and [2] is files
data = dict()  # Initialize Dictionary
for idx, scenario in enumerate(model_info['orgScenarioNumber']):
    # Check to see if models have been processed already; unzip if no; load if yes
    model_files = find_files(model_source, f'Scenario_{scenario}_*_{version_no}*', 'files')

    file = open(model_source / model_files[0], 'rb')
    print(f'Reading {model_files[0]}')
    avg_fields = pickle.load(file)
    file.close()

    eps = np.stack(avg_fields['eps'])
    xBins = avg_fields['xBins'][0]
    zBins = avg_fields['zBins'][0]

    ax[idx].imshow(eps, aspect='auto', extent=extents(xBins) + extents(zBins),
                   vmin=caxis[0], vmax=caxis[1], origin='lower', cmap=c_map)

[ax[x].xaxis.set_ticklabels([]) for x in range(1,6)]

fname = 'Eps_Tw_30_Hs_0_05.png'
plt.savefig(save_fig_dir / fname, dpi=300, format=None, metadata=None,
            bbox_inches=None, pad_inches=0.1,
            facecolor='auto', edgecolor='auto',
            backend=None)


import sys
import csv
import xml.etree.ElementTree as ET

from ItemHarvester import ItemHarvester

"""
Library of Congress harvester

    - Drops and recreates all database tables at runtime
    - Fetches collection sitemaps for list of collection pages to be fetch and retries on failure
    - Fetches collection pages [and indefinitely re-tries on failure] to retrieve a list of item
        URLs to harvest. Writes item URLs out as to a CSV file
    - ItemHarvester.py reads item URLs to be harvested from CSV file
    - Fetches item metadata from URLs in CSV file. Indefinitely retries on failure except for 4xx errors
    - Harvested item metadata is stored in an `items` table in the sqlite3 database

"""

class LcCollectionHarvester:
    sitemap_ns = 'http://www.sitemaps.org/schemas/sitemap/0.9'
    user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) ' \
                 'Chrome/35.0.1916.47 Safari/537.36 '

    def getItemUrls(self, collection_page_data):
        item_pages = []
        for page in collection_page_data:
            for p in self.parse_items_from_response(page):
                item_pages.append([p])
        return item_pages

    def parse_items_from_response(self, rsp):
        items = []
        xml = ET.ElementTree(ET.fromstring(rsp))
        for i in xml.getroot().findall('.//{%s}loc' % self.sitemap_ns):
            if i.text.startswith('http://www.loc.gov/item/'):  # filter out non-items
                items.append(i.text + "?fo=json&at=item")  # append parameters
        return items

    def writeUrlsToCsv(self, file, data):
        fos = open(file, "w")
        with fos:
            csv_writer = csv.writer(fos)
            csv_writer.writerows(data)

    def harvestCollections(self, db, tbl, input_csv, output_csv):
        """

        :param db:
        :param tbl:
        :param input_csv:
        :param output:
        :return:
        """

        item_harvester = ItemHarvester(db)

        # request collection pages via ItemHarvester
        item_harvester.run(tbl, input_csv)

        # get the harvested collection page data
        collection_page_data = item_harvester.getData(tbl)

        # Extract item urls to harvest from collection_page_data
        item_urls = self.getItemUrls(collection_page_data)

        # Write the item urls to a CSV file
        self.writeUrlsToCsv(output_csv, item_urls)

        print('%s item URLs written to %s' % (len(item_urls), output_csv))

        item_harvester.cleanup()

def main(args):
    if len(args) != 5:
        print("Not enough arguments passed. Expecting: input data CSV [page URLs], output CSV [item URLs], "
              "database path, table name")
        sys.exit(-1)

    input_csv = args[1]
    print("Input CSV: %s" % input_csv)

    output_csv = args[2]
    print("Output CSV: %s" % output_csv)

    db = args[3]
    print("DB: %s" % db)

    tbl = args[4]
    print("Table name: %s" % db)

    lc = LcCollectionHarvester()

    lc.harvestCollections(db, tbl, input_csv, output_csv)

if __name__ == '__main__':
    main(sys.argv)


from __future__ import annotations

import json
import math
import shutil
import subprocess

import pytest

import scitbx.matrix
from cctbx import sgtbx, uctbx
from dxtbx.model import Crystal, Experiment, ExperimentList, Scan
from dxtbx.serialize import load

from dials.algorithms.symmetry.cosym._generate_test_data import (
    generate_experiments_reflections,
)
from dials.array_family import flex
from dials.command_line import symmetry
from dials.command_line.symmetry import (
    apply_change_of_basis_ops,
    change_of_basis_ops_to_minimum_cell,
    eliminate_sys_absent,
    get_subset_for_symmetry,
    median_unit_cell,
)
from dials.util.exclude_images import exclude_image_ranges_from_scans
from dials.util.multi_dataset_handling import assign_unique_identifiers
from dials.util.phil import parse

def test_symmetry_laue_only(dials_data, tmp_path):
    """Simple test to check that dials.symmetry completes"""

    lcyst_data = dials_data("l_cysteine_dials_output", pathlib=True)
    result = subprocess.run(
        [
            shutil.which("dials.symmetry"),
            lcyst_data / "20_integrated_experiments.json",
            lcyst_data / "20_integrated.pickle",
            lcyst_data / "25_integrated_experiments.json",
            lcyst_data / "25_integrated.pickle",
            "systematic_absences.check=False",
        ],
        cwd=tmp_path,
        capture_output=True,
    )
    assert not result.returncode and not result.stderr
    assert tmp_path.joinpath("symmetrized.refl").is_file()
    assert tmp_path.joinpath("symmetrized.expt").is_file()
    exps = load.experiment_list(tmp_path / "symmetrized.expt", check_format=False)
    assert str(exps[0].crystal.get_space_group().info()) == "P 2 2 2"
    joint_reflections = flex.reflection_table.from_file(tmp_path / "symmetrized.refl")
    # check that there are 2 unique id and imageset_ids, and that these
    # correctly correspond to each experiment
    assert len(set(joint_reflections["id"])) == 2
    assert len(set(joint_reflections["imageset_id"])) == 2
    for id_ in range(2):
        sel = joint_reflections["id"] == id_
        assert set(joint_reflections["imageset_id"].select(sel)) == {id_}

def test_symmetry_basis_changes_for_C2(tmp_path):
    """Test the correctness of change of basis operations in dials.symmetry

    Supply the unit cell of beta-lactamase, which triggers a change of
    basis from input to minimum during symmetry analysis."""
    unit_cell = (53.173, 61.245, 69.292, 90.0, 93.04675, 90.0)
    space_group = sgtbx.space_group_info("C 2").group()
    experiments, reflections, _ = generate_experiments_reflections(
        space_group=space_group,
        unit_cell=unit_cell,
        sample_size=1,
        map_to_minimum=False,
    )
    experiments.as_json(tmp_path / "tmp.expt")
    joint_table = flex.reflection_table()
    for r in reflections:
        joint_table.extend(r)
    joint_table.as_file(tmp_path / "tmp.refl")

    result = subprocess.run(
        [
            shutil.which("dials.symmetry"),
            tmp_path / "tmp.expt",
            tmp_path / "tmp.refl",
            "json=symmetry.json",
        ],
        cwd=tmp_path,
        capture_output=True,
    )
    assert not result.returncode and not result.stderr
    assert tmp_path.joinpath("symmetrized.refl").is_file()
    symmetrized_expt_file = tmp_path / "symmetrized.expt"
    assert symmetrized_expt_file.is_file()

    expts = load.experiment_list(symmetrized_expt_file, check_format=False)
    for v, expected in zip(expts[0].crystal.get_unit_cell().parameters(), unit_cell):
        assert v == pytest.approx(expected)

    # Using the change of basis ops from the json output we should be able to
    # reindex the input experiments to match the output experiments
    with tmp_path.joinpath("symmetry.json").open() as f:
        d = json.load(f)
    cs = experiments[0].crystal.get_crystal_symmetry()
    cb_op_inp_min = sgtbx.change_of_basis_op(str(d["cb_op_inp_min"][0]))
    cb_op_min_best = sgtbx.change_of_basis_op(str(d["subgroup_scores"][0]["cb_op"]))
    assert cs.change_basis(cb_op_min_best * cb_op_inp_min).is_similar_symmetry(
        expts[0].crystal.get_crystal_symmetry()
    )

@pytest.mark.parametrize("option", ["", "exclude_images=0:1500:1800"])
def test_symmetry_with_absences(dials_data, tmp_path, option):
    """Simple test to check that dials.symmetry, with absences, completes"""

    lcyst_data = dials_data("l_cysteine_dials_output", pathlib=True)
    cmd = [
        shutil.which("dials.symmetry"),
        lcyst_data / "20_integrated_experiments.json",
        lcyst_data / "20_integrated.pickle",
        lcyst_data / "25_integrated_experiments.json",
        lcyst_data / "25_integrated.pickle",
    ]
    if option:
        cmd.append(option)

    result = subprocess.run(cmd, cwd=tmp_path, capture_output=True)
    assert not result.returncode and not result.stderr
    assert (tmp_path / "symmetrized.refl").is_file()
    assert (tmp_path / "symmetrized.expt").is_file()
    expts = load.experiment_list(tmp_path / "symmetrized.expt", check_format=False)
    assert str(expts[0].crystal.get_space_group().info()) == "P 21 21 21"

def test_symmetry_with_laue_group_override(dials_data, tmp_path):
    """Simple test to check that dials.symmetry, with overridden laue group, completes"""

    lcyst_data = dials_data("l_cysteine_dials_output", pathlib=True)
    result = subprocess.run(
        [
            shutil.which("dials.symmetry"),
            "laue_group=P121",
            "change_of_basis_op=-b,-a,-c",
            lcyst_data / "20_integrated_experiments.json",
            lcyst_data / "20_integrated.pickle",
            lcyst_data / "25_integrated_experiments.json",
            lcyst_data / "25_integrated.pickle",
        ],
        cwd=tmp_path,
        capture_output=True,
    )
    assert not result.returncode and not result.stderr
    assert tmp_path.joinpath("symmetrized.refl").is_file()
    assert tmp_path.joinpath("symmetrized.expt").is_file()
    expts = load.experiment_list(tmp_path / "symmetrized.expt", check_format=False)
    assert str(expts[0].crystal.get_space_group().info()) == "P 1 21 1"
    # Verify that the unit cell has been reindexed correctly
    assert expts[0].crystal.get_unit_cell().parameters() == pytest.approx(
        (8.21578444269, 5.4815363434, 12.1457047712, 90.0, 90.0, 90.0)
    )

@pytest.mark.parametrize("method", ["direct", "fourier"])
def test_symmetry_absences_only(dials_data, tmp_path, method):
    """Test the command line script with real data. Proteinase K in P41"""
    location = dials_data("vmxi_proteinase_k_sweeps", pathlib=True)

    command = [
        shutil.which("dials.symmetry"),
        "laue_group=None",
        location / "experiments_0.json",
        location / "reflections_0.pickle",
        f"method={method}",
    ]
    result = subprocess.run(command, cwd=tmp_path, capture_output=True)
    assert not result.returncode and not result.stderr
    assert tmp_path.joinpath("dials.symmetry.html").is_file()
    assert tmp_path.joinpath("symmetrized.expt").is_file()
    exps = load.experiment_list(tmp_path / "symmetrized.expt", check_format=False)
    assert str(exps[0].crystal.get_space_group().info()) == "P 41"

    # Now try with a d_min
    command += ["d_min=4.0"]
    result = subprocess.run(command, cwd=tmp_path, capture_output=True)
    assert not result.returncode and not result.stderr
    exps = load.experiment_list(tmp_path / "symmetrized.expt", check_format=False)
    assert str(exps[0].crystal.get_space_group().info()) == "P 41"

def test_map_to_minimum_cell():
    # Input and expected output
    input_ucs = [
        (39.7413, 183.767, 140.649, 90, 90, 90),
        (40.16, 142.899, 92.4167, 90, 102.48, 90),
        (180.613, 40.1558, 142.737, 90, 90.0174, 90),
    ]
    input_sgs = ["C 2 2 21", "P 1 2 1", "C 1 2 1"]
    input_hkl = [
        [(1, -75, -71), (1, -73, -70), (1, -71, -69)],
        [(14, -37, -36), (-2, -35, -46), (-3, -34, -47)],
        [(-31, -5, -3), (-25, -3, -3), (-42, -8, -2)],
    ]
    expected_ucs = [
        (39.7413, 94.00755450320204, 140.649, 90.0, 90.0, 77.79717980856927),
        (40.16, 92.46399390642911, 142.899, 90.0, 90.0, 77.3882749092846),
        (
            40.1558,
            92.51154528306184,
            142.73699999999997,
            89.9830147351441,
            90.0,
            77.46527404307477,
        ),
    ]
    expected_output_hkl = [
        [(-1, 37, -71), (-1, 36, -70), (-1, 35, -69)],
        [(-14, 22, 37), (2, 48, 35), (3, 50, 34)],
        [(-5, 13, -3), (-3, 11, -3), (-8, 17, -2)],
    ]

    # Setup the input experiments and reflection tables
    expts = ExperimentList()
    reflections = []
    for uc, sg, hkl in zip(input_ucs, input_sgs, input_hkl):
        uc = uctbx.unit_cell(uc)
        sg = sgtbx.space_group_info(sg).group()
        B = scitbx.matrix.sqr(uc.fractionalization_matrix()).transpose()
        expts.append(Experiment(crystal=Crystal(B, space_group=sg, reciprocal=True)))
        refl = flex.reflection_table()
        refl["miller_index"] = flex.miller_index(hkl)
        reflections.append(refl)

    # Actually run the method we are testing
    cb_ops = change_of_basis_ops_to_minimum_cell(
        expts, max_delta=5, relative_length_tolerance=0.05, absolute_angle_tolerance=2
    )
    cb_ops_as_xyz = [cb_op.as_xyz() for cb_op in cb_ops]
    # Actual cb_ops are machine dependent (sigh)
    assert cb_ops_as_xyz == [
        "-x+y,-2*y,z",
        "-x+z,-z,-y",
        "x+y,-2*x,z",
    ] or cb_ops_as_xyz == ["x-y,2*y,z", "x-z,z,-y", "-x-y,2*x,z"]

    expts_min, reflections = apply_change_of_basis_ops(expts, reflections, cb_ops)
    # Verify that the unit cells have been transformed as expected
    for expt, uc in zip(expts, expected_ucs):
        assert expt.crystal.get_unit_cell().parameters() == pytest.approx(uc, abs=4e-2)

    # Space group should be set to P1
    assert [expt.crystal.get_space_group().type().number() for expt in expts_min] == [
        1,
        1,
        1,
    ]

    # Verify that the reflections have been reindexed as expected
    # Because the exact choice of minimum cell can be platform-dependent,
    # compare the magnitude, but not the sign of the output hkl values
    for refl, expected_hkl in zip(reflections, expected_output_hkl):
        for hkl, e_hkl in zip(refl["miller_index"], expected_hkl):
            assert [abs(h) for h in hkl] == [abs(eh) for eh in e_hkl]

def _make_input_for_exclude_tests(exclude_images=True):
    """Generate input data, that upon exclusion should leave only the first
    reflection."""
    if exclude_images:
        exclude_images = [["0:360:720"], ["1:360:720"]]
    expt1 = Experiment(scan=Scan(image_range=(0, 720), oscillation=(0.0, 1.0)))
    expt2 = Experiment(scan=Scan(image_range=(0, 720), oscillation=(0.0, -1.0)))
    refls1 = flex.reflection_table()
    refls2 = flex.reflection_table()
    refls1["xyzobs.mm.value"] = flex.vec3_double(
        [(0.0, 0.0, 10.0 * math.pi / 180.0), (0.0, 0.0, 370.0 * math.pi / 180.0)]
    )
    refls1["xyzobs.px.value"] = flex.vec3_double([(0.0, 0.0, 10.0), (0.0, 0.0, 370.0)])
    refls1["i"] = flex.int([0, 1])
    refls2["xyzobs.mm.value"] = flex.vec3_double(
        [(0.0, 0.0, -10.0 * math.pi / 180.0), (0.0, 0.0, -370.0 * math.pi / 180.0)]
    )
    refls2["xyzobs.px.value"] = flex.vec3_double([(0.0, 0.0, 10.0), (0.0, 0.0, 370.0)])
    refls2["i"] = flex.int([0, 1])
    expts = ExperimentList([expt1, expt2])
    tables = [refls1, refls2]
    expts, tables = assign_unique_identifiers(expts, tables)
    return exclude_images, expts, tables

def test_get_subset_for_symmetry_image_range():
    """Test that first 360 degrees are selected from each sweep with an exclude
    images command."""
    exclude_images, expts, tables = _make_input_for_exclude_tests(exclude_images=True)
    refls = get_subset_for_symmetry(expts, tables, exclude_images)
    assert refls[0]["i"].all_eq(0)
    assert refls[1]["i"].all_eq(0)

def test_get_subset_for_symmetry_prior_image_range():
    """Test that first 360 degrees are selected from each sweep with an exclude
    images command."""
    exclude_images, expts, tables = _make_input_for_exclude_tests(exclude_images=True)

    # Explicitly exclude a different image range
    expts = exclude_image_ranges_from_scans(tables, expts, [["0:1:360"], ["1:1:360"]])
    refls = get_subset_for_symmetry(expts, tables)
    assert refls[0]["i"].all_eq(1)
    assert refls[1]["i"].all_eq(1)

    # Exclude_images should be cumulative, i.e. the range exclude above should
    # be excluded in addition to the new range provided explicitly to the
    # function
    refls = get_subset_for_symmetry(expts, tables, exclude_images)
    assert len(refls[0]) == 0
    assert len(refls[1]) == 0

def test_get_subset_for_symmetry_first_360():
    """Test that first 360 degrees are selected from each sweep"""
    exclude_images, expts, tables = _make_input_for_exclude_tests(exclude_images=False)
    refls = get_subset_for_symmetry(expts, tables, exclude_images)
    assert refls[0]["i"].all_eq(0)
    assert refls[1]["i"].all_eq(0)

def test_change_of_basis_ops_to_minimum_cell_1037(mocker):
    # See https://github.com/dials/dials/issues/1037

    input_ucs = [
        (
            4.805202948916906,
            12.808064769657364,
            16.544899201125446,
            106.45808502003258,
            90.0065567098825,
            100.77735674275475,
        ),
        (
            4.808011343212577,
            12.821894835790472,
            16.557339561965573,
            106.48431244651402,
            90.0252848479048,
            100.77252933676507,
        ),
        (
            4.8096632137789985,
            12.815648858527567,
            16.55931712239122,
            106.48990701341536,
            90.01703141314147,
            100.80397887485773,
        ),
        (
            4.807294085194974,
            12.822386757910516,
            16.560411742466663,
            106.43185845358086,
            90.02067929544215,
            100.79522302759383,
        ),
    ]

    # Setup the input experiments and reflection tables
    expts = ExperimentList()
    for uc in input_ucs:
        uc = uctbx.unit_cell(uc)
        sg = sgtbx.space_group_info("P1").group()
        B = scitbx.matrix.sqr(uc.fractionalization_matrix()).transpose()
        expts.append(Experiment(crystal=Crystal(B, space_group=sg, reciprocal=True)))

    # We want to spy on the return value of this function
    mocker.spy(symmetry, "unit_cells_are_similar_to")

    # Actually run the method we are testing
    cb_ops = change_of_basis_ops_to_minimum_cell(
        expts, max_delta=5, relative_length_tolerance=0.05, absolute_angle_tolerance=2
    )
    import pytest_mock

    if getattr(pytest_mock, "version", "").startswith("1."):
        assert symmetry.unit_cells_are_similar_to.return_value is True
    else:
        assert symmetry.unit_cells_are_similar_to.spy_return is True
    cb_ops_as_xyz = [cb_op.as_xyz() for cb_op in cb_ops]
    assert len(set(cb_ops_as_xyz)) == 1
    # Actual cb_ops are machine dependent (sigh)
    assert cb_ops_as_xyz[0] in ("x,y,z", "-x,y,-z", "x-y,-y,-z")

def test_change_of_basis_ops_to_minimum_cell_mpro():
    input_ucs = [
        (46.023, 55.001, 64.452, 64.744, 78.659, 89.824),
        (44.747, 53.916, 62.554, 114.985, 99.610, 90.736),
    ]

    # Setup the input experiments and reflection tables
    expts = ExperimentList()
    for uc in input_ucs:
        uc = uctbx.unit_cell(uc)
        sg = sgtbx.space_group_info("P1").group()
        B = scitbx.matrix.sqr(uc.fractionalization_matrix()).transpose()
        expts.append(Experiment(crystal=Crystal(B, space_group=sg, reciprocal=True)))

    # Actually run the method we are testing
    cb_ops = change_of_basis_ops_to_minimum_cell(
        expts, max_delta=5, relative_length_tolerance=0.05, absolute_angle_tolerance=2
    )
    expts.change_basis(cb_ops, in_place=True)
    assert symmetry.unit_cells_are_similar_to(
        expts,
        median_unit_cell(expts),
        relative_length_tolerance=0.05,
        absolute_angle_tolerance=2,
    )

from cctbx import crystal

def test_change_of_basis_ops_to_minimum_cell_with_outlier():
    symmetries = [
        crystal.symmetry(unit_cell=uc, space_group="P1")
        for uc in (
            (52.8868, 52.8868, 333.522, 90, 90, 120),
            (52.6503, 53.0292, 333.783, 89.9872, 89.2247, 60.8078),
            (52.9571, 53.0005, 334.255, 90.0493, 90.0042, 119.893),
            (
                54.4465,
                56.5677,
                355.775,
                93.4376,
                90.0999,
                118.256,
            ),  # This is an outlier
            (52.9235, 52.9235, 335.296, 90, 90, 120),
            (53.4531, 53.4531, 322.909, 90, 90, 120),
        )
    ]

    # Setup the input experiments and reflection tables
    expts = ExperimentList()
    for cs in symmetries:
        B = scitbx.matrix.sqr(cs.unit_cell().fractionalization_matrix()).transpose()
        expts.append(
            Experiment(
                crystal=Crystal(B, space_group=cs.space_group(), reciprocal=True)
            )
        )

    # Actually run the method we are testing
    cb_ops = change_of_basis_ops_to_minimum_cell(
        expts, max_delta=5, relative_length_tolerance=0.05, absolute_angle_tolerance=2
    )
    assert cb_ops.count(None) == 1
    assert cb_ops[3] is None
    expts = ExperimentList([expt for expt, cb_op in zip(expts, cb_ops) if cb_op])
    cb_ops = [cb_op for cb_op in cb_ops if cb_op]

    expts.change_basis(cb_ops, in_place=True)
    assert symmetry.unit_cells_are_similar_to(
        expts,
        median_unit_cell(expts),
        relative_length_tolerance=0.05,
        absolute_angle_tolerance=2,
    )

def test_median_cell():
    unit_cells = [
        uctbx.unit_cell(uc)
        for uc in [
            (10, 11, 11.9, 90, 85, 90),
            (10.1, 11.2, 12, 90, 85.5, 90),
            (10.2, 11.1, 12, 90, 84.7, 90),
        ]
    ]
    expts = ExperimentList()
    for uc in unit_cells:
        sg = sgtbx.space_group_info("P1").group()
        B = scitbx.matrix.sqr(uc.fractionalization_matrix()).transpose()
        expts.append(Experiment(crystal=Crystal(B, space_group=sg, reciprocal=True)))

    median = median_unit_cell(expts)
    assert median.parameters() == pytest.approx((10.1, 11.1, 12, 90, 85, 90))

def test_eliminate_sys_absent():
    refl = flex.reflection_table()
    refl["miller_index"] = flex.miller_index(
        [(-31, -5, -3), (-25, -3, -3), (0, 1, 0), (-42, -8, -2)]
    )
    sgi = sgtbx.space_group_info("C121")
    uc = sgi.any_compatible_unit_cell(volume=1000)
    B = scitbx.matrix.sqr(uc.fractionalization_matrix()).transpose()
    expt = Experiment(crystal=Crystal(B, space_group=sgi.group(), reciprocal=True))
    reflections = eliminate_sys_absent([expt], [refl])
    assert list(reflections[0]["miller_index"]) == [
        (-31, -5, -3),
        (-25, -3, -3),
        (-42, -8, -2),
    ]

def test_few_reflections(dials_data, run_in_tmp_path):
    """
    Test that dials.symmetry does something sensible if given few reflections.

    Use some example integrated data generated from a ten-image 1° sweep.  These
    contain a few dozen integrated reflections.

    Args:
        dials_data: DIALS custom Pytest fixture for access to test data.
        run_in_tmp_path: DIALS custom Pytest fixture to run this test in a temporary
                         directory.
    """
    # Get and use the default parameters for dials.symmetry.
    params = symmetry.phil_scope.fetch(source=parse("")).extract()

    # Use the integrated data from the first ten images of the first sweep.
    data_dir = dials_data("l_cysteine_dials_output", pathlib=True)
    experiments = ExperimentList.from_file(data_dir / "11_integrated.expt")
    reflections = [flex.reflection_table.from_file(data_dir / "11_integrated.refl")]

    # Run dials.symmetry on the above data files.
    symmetry.symmetry(experiments, reflections, params)

def test_x4wide(dials_data, tmp_path):
    """Verify reflections are reindexed before systematic absence analysis.

    Exercise a bug fix in https://github.com/dials/dials/pull/2183

    Expected space group is P 43 21 2 (or its enantiomorphic equivalent, P 41 21 2)
    See also https://www.rcsb.org/structure/3QF8
    """
    x4wide = dials_data("x4wide_processed", pathlib=True)
    result = subprocess.run(
        [
            shutil.which("dials.symmetry"),
            x4wide / "AUTOMATIC_DEFAULT_scaled.expt",
            x4wide / "AUTOMATIC_DEFAULT_scaled.refl",
        ],
        cwd=tmp_path,
    )
    assert not result.returncode and not result.stderr
    assert tmp_path.joinpath("symmetrized.refl").is_file()
    assert tmp_path.joinpath("symmetrized.expt").is_file()
    exps = load.experiment_list(tmp_path / "symmetrized.expt", check_format=False)
    assert str(exps[0].crystal.get_space_group().info()) == "P 41 21 2"


n = int(input())

time_dict = dict()

for i in range(n):
    q = input().split(', ')
    minute, second = [int(e) for e in q[3].split(':')]
    if not time_dict.get(q[2]):
        time_dict[q[2]] = [0, 0]
    time_dict[q[2]] = [time_dict[q[2]][0] + minute, time_dict[q[2]][1] + second]

convert_time = time_dict.values()

for p in convert_time:
    temp = p[1] // 60
    p[1] %= 60
    p[0] += temp

list_time = list(time_dict.items())

list_time.sort(key=lambda x: (x[1][0], x[1][1]), reverse=True)

for e in range(0, min(3, len(list_time))):
    print('{} --> {}:{:02d}'.format(list_time[e][0], list_time[e][1][0], list_time[e][1][1]))


import yaml

class Config(object):
    """
    A configuration class that allows to refer parameters as object attributes
    """

    def __init__(self, config):
        """
        config: dict like config parameters
        """

        assert isinstance(config, dict), 'Required dict like object'

        for key, value in config.items():
            if isinstance(value, dict):
                value = Config(value)

            self.__dict__[key] = value

    @staticmethod
    def fromYamlFile(path):
        """
        path: path to json file
        """
        with open(path) as lines:
            return Config(yaml.safe_load(lines))


import os, sys
import dataloader as dd
from keras.optimizers import *
from keras.callbacks import *

itokens, otokens = dd.MakeS2SDict('data/en2de.s2s.txt', dict_file='data/en2de_word.txt')
Xtrain, Ytrain = dd.MakeS2SData('data/en2de.s2s.txt', itokens, otokens, h5_file='data/en2de.h5')
Xvalid, Yvalid = dd.MakeS2SData('data/en2de.s2s.valid.txt', itokens, otokens, h5_file='data/en2de.valid.h5')

print('seq 1 words:', itokens.num())
print('seq 2 words:', otokens.num())
print('train shapes:', Xtrain.shape, Ytrain.shape)
print('valid shapes:', Xvalid.shape, Yvalid.shape)

'''
from rnn_s2s import RNNSeq2Seq
s2s = RNNSeq2Seq(itokens,otokens, 256)
s2s.compile('rmsprop')
s2s.model.fit([Xtrain, Ytrain], None, batch_size=64, epochs=30, validation_data=([Xvalid, Yvalid], None))
'''

from transformer import Transformer, LRSchedulerPerStep, LRSchedulerPerEpoch

d_model = 512
s2s = Transformer(itokens, otokens, len_limit=70, d_model=d_model, d_inner_hid=512, \
				   n_head=8, d_k=64, d_v=64, layers=2, dropout=0.1)

mfile = 'models/en2de.model.h5'

lr_scheduler = LRSchedulerPerStep(d_model, 4000)   # there is a warning that it is slow, however, it's ok.
#lr_scheduler = LRSchedulerPerEpoch(d_model, 4000, Xtrain.shape[0]/64)  # this scheduler only update lr per epoch
model_saver = ModelCheckpoint(mfile, save_best_only=True, save_weights_only=True)

s2s.compile(Adam(0.001, 0.9, 0.98, epsilon=1e-9))
s2s.model.summary()
try: s2s.model.load_weights(mfile)
except: print('\n\nnew model')

if 'test' in sys.argv:
	print(s2s.decode_sequence_fast('A black dog eats food .'.split(), delimiter=' '))
	while True:
		quest = input('> ')
		print(s2s.decode_sequence_fast(quest.split(), delimiter=' '))
		rets = s2s.beam_search(quest.split(), delimiter=' ')
		for x, y in rets: print(x, y)
else:
	s2s.model.fit([Xtrain, Ytrain], None, batch_size=64, epochs=30, \
				validation_data=([Xvalid, Yvalid], None), \
				callbacks=[lr_scheduler, model_saver])


#!/usr/bin/python3
""" class Rectangle that defines a rectangle"""

class Rectangle:
    """ Class Rectangle"""
    def __init__(self, width=0, height=0):
        self.width = width
        self.height = height

    @property
    def width(self):
        """ method width"""
        return self.__width

    @width.setter
    def width(self, value):
        """ setter width"""
        if not isinstance(value, int):
            raise TypeError("width must be an integer")
        if (value < 0):
            raise ValueError("width must be >= 0")
        self.__width = value

    @property
    def height(self):
        """ height"""
        return self.__height

    @height.setter
    def height(self, value):
        """ setter height"""
        if not isinstance(value, int):
            raise TypeError("height must be an integer")
        if (value < 0):
            raise ValueError("height must be >= 0")
        self.__height = value

    def area(self):
        """ area"""
        return self.width * self.height

    def perimeter(self):
        """ perimeter"""
        if self.width == 0 or self.height == 0:
            return 0
        return (2 * (self.width + self.height))

    def __str__(self):
        """ str """
        recstr = ""
        if self.width == 0 or self.height == 0:
            return (recstr)
        else:
            for j in range(self.height):
                recstr += "{}".format('#'*self.__width)
                if j != (self.height - 1):
                    recstr += "\n"
        return recstr


import sys
import os
import signal
import time
import logging

sys.path.append(os.path.abspath('..'))
import vxi11_server as Vxi11

#
# A simple instrument server.
#
# creates an InstrumentServer with the name INSTR
# adds a device handler with the name inst1
# this instrument simply responds with the current time when queried by
# a vxi11 client.
#
# 'TIME' may not be a legal vxi11 instrument name, but seems to work well.
# allowing some introspection on a device you havent used (and didnt document)
# in some time.
#

def signal_handler(signal, frame):
    logger.info('Handling Ctrl+C!')
    instr_server.close()
    sys.exit(0)

class TimeDevice(Vxi11.InstrumentDevice):

    def device_init(self):
        #print('TimeDevice: device_init()')
        return

    def device_read(self, request_size, term_char, flags, io_timeout):
        '''respond to the device_read rpc: refer to section (B.6.4)
        of the VXI-11 TCP/IP Instrument Protocol Specification'''
        error = Vxi11.Error.NO_ERROR
        reason = Vxi11.ReadRespReason.END

        # opaque_data is a bytes array, so encode correctly!
        data = time.strftime("%H:%M:%S +0000", time.gmtime())
        opaque_data = data.encode("ascii")

        return error, reason, opaque_data

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger(__name__)

    signal.signal(signal.SIGINT, signal_handler)
    print('Press Ctrl+C to exit')
    logger.info('starting time_device')

    # create a server, attach a device, and start a thread to listen for requests
    instr_server = Vxi11.InstrumentServer()
    #name = 'TIME'
    name = 'inst1'
    instr_server.add_device_handler(TimeDevice, name)
    instr_server.listen()

    # sleep (or do foreground work) while the Instrument threads do their job
    while True:
        time.sleep(1)


#execute.py

account = '3ddba2981c42cecdfee9e845e5615ffc721f1daa'

import curl_API as cl
import json

begin = 5 # 截止块，截止后的区块链都是严格按照格式生成的可检索的块
username = 'lover_test' # 公用区块链的私有用户

def init_acccount():
  resultjs = cl.invoke('addUser',
              '''["lover_test","20"]''',
              account)
  print( resultjs )

def add_relationship(user_id1, user_id2, info):
  resultjs = cl.invoke('addAsset',
              '''["{}&#@{}&#@{}","test","to record","ZIG","1","lover_test"]'''.format(user_id1,user_id2,info),
              account)
  print( resultjs )

def del_relationship(user_id1, user_id2, info):
  resultjs = cl.invoke('deleteAsset',
              '''["{}&#@{}&#@{}"]'''.format(user_id1,user_id2,info),
              account)
  print( resultjs )

from datetime import datetime
from datetime import timedelta

def search_relationship(user_id):
  d = []
  global begin
  resultjs = cl.blocks()
  result = json.loads(resultjs)

  for block in result:

    if block["id"]<begin:
      continue

    transactions = block["transactions"][0]
    if transactions["args"][-1]!=username:
      continue
    elif transactions["args"][0] == "addAsset":
      branch = 'add'
    elif transactions["args"][0] == "deleteAsset":
      branch = 'del'
    else:
      continue

    when =  transactions["timestamp"]
    # "2018-05-12T15:37:14.000Z"
    when = ( datetime.strptime(when,"%Y-%m-%dT%H:%M:%S.000Z") + timedelta(hours = 8) ).strftime("%Y.%m.%d %H:%M")
    # "2018.5.12 15:37"
    try:
      who1,who2,why = transactions["args"][1].split('&#@')
    except IndexError:
      print(block)
      raise(IndexError)
    if who1 == user_id:
      who = who2
    elif who2 == user_id:
      who = who1
    else:
      continue

    d.append( {"id":block["id"],"when":when,"who":who,"why":why,"type":branch} )

  d = sorted(d,key = lambda x:x["id"],reverse = True)
  answer = {"datas":d}
  print(json.dumps(answer, ensure_ascii=False ,indent=2))


## Creates plots for articles by source, articles of time, article length, and wordcloud

import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
import os
import spacy
from wordcloud import WordCloud

mpl.rcParams['figure.dpi'] = 150
mpl.rcParams['figure.autolayout'] = True
mpl.style.use('ggplot')

# plt.set_cmap(sns.diverging_palette(220, 20, as_cmap=True))

path = 'C:/Users/Jakob/Documents/RLI Nuclear Energy'

df = pd.read_excel(os.path.join(path, 'rli-articles-clean.xlsx'))

# Number of articles by source
df.groupby('source_agg')['text'].count().sort_values().plot(kind='barh')
plt.ylabel('Source')
plt.xlabel('Number of articles')
# plt.xticks(rotation=60, ha="right")
plt.savefig(os.path.join(path, 'Plots', 'number-of-articles-by-source.pdf'))
plt.show()

# Number of articles over time
df.groupby(df.date.dt.to_period('Y'))['text'].count().plot.bar()
plt.xlabel('Year')
plt.ylabel('Number of articles')
plt.xticks(rotation=45, ha="right")
plt.savefig(os.path.join(path, 'Plots', 'number-of-articles-over-time.pdf'))
plt.show()

# Length of articles
cutoff, tick_interval = 3000, 500
df['plot_length'] = df.length
df.loc[df.plot_length > cutoff, 'plot_length'] = cutoff
df.plot_length.hist(bins=50)
ticks = [i*tick_interval for i in range(cutoff//tick_interval + 1)]
plt.xticks(ticks=ticks, labels=ticks[:-1] + ['>' + str(cutoff)])
plt.ylabel('Number of articles')
plt.xlabel('Article length in words')
plt.savefig(os.path.join(path, 'Plots', 'articles-by-length.pdf'))
plt.show()

# Length of articles by source
df.groupby('source_agg').length.mean().sort_values().plot(kind='barh')
plt.ylabel('Source')
plt.xlabel('Average length in words')
plt.savefig(os.path.join(path, 'Plots', 'mean-article-length-by-source.pdf'))
plt.show()

# word cloud
nlp = spacy.load("nl_core_news_sm")
# stopwords = nlp.Defaults.stop_words
stopwords = stop_words
stopwords += ['oorspronkelijke', 'pagina', 'staat', 'Staaten']
wordcloud = WordCloud(
    #background_color='white',
    colormap='Set1',
    max_words=100,
    max_font_size=80,
    width=1000, height=700, background_color='white',
    stopwords=stopwords).generate(' '.join(df.text))

plt.figure(figsize=(15, 10))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.savefig(os.path.join(path, 'Plots', 'word-cloud-overall.pdf'), bbox_inches='tight')
plt.show()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import os
import sys

parser = argparse.ArgumentParser(description="qu can")
parser.add_argument( '--g2t',required=False, help='ref_genome.gtf.gene2transcript.txt')
parser.add_argument( '--tmp',required=True, help='the file end of tmp ')
parser.add_argument( '--detail',required=True, help='ouput file fasta')
parser.add_argument( '--family',required=True, help='ouput file fasta')
parser.add_argument( '--Species',required=False, help='ouput file fasta')
args = parser.parse_args()

def getfamily(listgoat):
    listgoat = listgoat.strip().split("\t")
    des = str(listgoat[2])
    faml = des.strip().split("|")[1]
    geneid = str(listgoat[0])
    dic2.setdefault(faml,[]).append(geneid)

dic2 = {}
#dic2.setdefault(key,[]).append(value)
#dic.setdefault('a',[]).append(2)
#gou zao zi dian
#dic.setdefault(key,{})[value] =1
dic1 = {}
if args.g2t:
    with open(args.g2t) as g2t:
        for line in g2t:
            lines = line.strip().split("\t")
            dic1[lines[1]] = lines[0]

with open(args.tmp) as tmp,open(args.detail,"w") as det,open(args.family,"w") as fam:
    det.write("Query-Name\tHit-Name\tHIt-Description\tE-Value\tScore\tIdentical\tSimilar\t"\
                 "Match_Length\tQuery_Length\tQuery_start\tQuery_End\tHit_Length\tHit_start\tHit_End\n")
    fam.write("family\tnum\tgene\n")
    if not args.Species:
        for line in tmp:
            if "Query-Name" not in line:
                lines = line.strip().split("\t")
                goatgene = str(lines[0])
                listgoat = [goatgene,]
                listgoat.extend(lines[1:-3])
                listline = "\t".join(listgoat)+"\n"
                getfamily(listline)
                det.write(str(listline))
        for i in dic2.keys():
            len_num = len(dic2[i])
            gene_list = ";".join(dic2[i])
            pyprint = str(i)+"\t"+str(len_num)+"\t"+gene_list+"\n"
            fam.write(pyprint)
    else:
        for line in tmp:
            if "Query-Name" not in line:
                if args.Species in line:
                    lines = line.strip().split("\t")
                    goatgene = str(lines[0])
                    listgoat = [goatgene,]
                    listgoat.extend(lines[1:-3])
                    listline = "\t".join(listgoat)+"\n"
                    getfamily(listline)
                    det.write(str(listline))
        for i in dic2.keys():
            len_num = len(dic2[i])
            gene_list = ";".join(dic2[i])
            pyprint = str(i)+"\t"+str(len_num)+"\t"+gene_list+"\n"
            fam.write(pyprint)


import copy

n = int(input())
lst = [list(map(int, input().split())) for _ in range(n-1)]

if n == 1:
    print(0)
else:
    node = {}
    for x, y, weight in lst:
        if x not in node:
            node[x] = {y: weight}
        else:
            node[x][y] = weight

        if y not in node:
            node[y] = {x: weight}
        else:
            node[y][x] = weight

    start = [[lst[0][0], 0]]
    start2 = []
    start3 = []
    find_child = []
    visit_lst = [0] * (n + 1)
    visit_lst[1] = 1
    visit_lst[0] = 1
    while start:
        target, weight = start.pop()

        if target in node:
            for key, value in node[target].items():
                if visit_lst[key] == 0:
                    visit_lst[key] = 1
                    start2.append([key, weight+value])
                    start3.append([key, weight + value])
        if not start and start2:
            start = copy.deepcopy(start2)
            start2 = []

        if sum(visit_lst) == n+1:
            find_child = start[:]
            break

    start3.sort(key=lambda x:x[1])
    new_start = [[start3[-1][0] , 0]]
    new_start2 = []
    visit_lst = [0] * (n + 1)
    visit_lst[start3[-1][0]] = 1
    visit_lst[0] = 1
    find_child2 = []
    new_start3 = []

    while new_start:

        target, weight = new_start.pop()

        if target in node:
            for key, value in node[target].items():
                if visit_lst[key] == 0:
                    visit_lst[key] = 1
                    new_start2.append([key, weight+value])
                    new_start3.append([key, weight+value])
        if not new_start and new_start2:
            new_start = copy.deepcopy(new_start2)
            new_start2 = []

        if sum(visit_lst) == n+1:
            find_child2 = start[:]
            break
    new_start3.sort(key=lambda x:x[1])
    print(new_start3[-1][-1])

8주차/트리의 지름_종석.py

트리의 지름_종석.py


def apresentarGraficoDeLinha():
    # importing modules
    import csv
    import matplotlib.pyplot as plt
    # data import csv --> Upload the data in GitHub
    x = []
    y = []

    with open('sample-line.csv', 'r') as csvfile:
        plots = csv.reader(csvfile, delimiter=',')
        for row in plots:
            x.append(int(row[0]))
            y.append(int(row[1]))
    #definindo tamando do grafico
    plt.figure(figsize=(6, 5))
    plt.plot(x, y, label='Loaded from file!')
    #axis X
    plt.xlabel('Game Number')
    #axis y
    plt.ylabel('Game Length')
    #titulo do gráfico
    plt.title('Relação entre Game Number e Game Length')
    plt.legend()
    #salvar em png
    plt.savefig('graph_linha.png')
    plt.show()

#modificar plt.show() para plt.close() para somente salvar o arquivo do gráfico.
    #95%


"""
IPSA Projet Chat: vue
"""
import tkinter as tk

fenetre=tk.Tk()
console=tk.Text(fenetre)
console.pack()

var_texte = tk.StringVar()
ligne_texte = tk.Entry(fenetre, textvariable=var_texte, width=30)
ligne_texte.pack()

if __name__ == "__main__":
    #controler part...just for testing...
    fenetre.bind('<Return>',lambda e : console.insert('end',var_texte.get()))


"""
    Author: Jon Ander Gomez Adrian (jon@dsic.upv.es, http://personales.upv.es/jon)
    Version: 1.0
    Date: October 2021
    Universitat Politecnica de Valencia
    Technical University of Valencia TU.VLC

"""

import sys
import time
import math
import numpy

try:
    from pyspark.rdd import RDD
except:
    RDD = None

from BallTree import BallTree

class KNN_Classifier:
    """
    This class implements a classifier based on K-Nearest Neighbours.

    The purpose is to classify each sample according to the class with majority within the set of K-Nearest samples.

    """

    def __init__(self, K = 5, num_classes = 2):
        self.num_classes = num_classes
        self.K = K
        self.balltree = None
    # ------------------------------------------------------------------------------

    def fit(self, X, y, min_samples_to_split = None):
        if RDD is not None and (isinstance(X, RDD) or isinstance(y, RDD)):
            raise Exception('RDD objects not supported for training')

        self.balltree = BallTree(min_samples_to_split = min_samples_to_split)
        self.balltree.fit(X, y)
        #
        return self
    # ------------------------------------------------------------------------------

    # ------------------------------------------------------------------------------
    def predict(self, sample):
        #
        if type(sample) == list or type(sample) == numpy.ndarray and len(sample.shape) >= 2:
            probs = self.predict_probs(sample)
            return probs.argmax(axis = 1)

        elif type(sample) == numpy.ndarray and len(sample.shape) == 1:
            probs = self.predict_probs(sample)
            return probs.argmax()

        elif RDD is not None and isinstance(sample, RDD):
            probs = sample.map(lambda t: (t[0], self.compute_probs_one_sample(t[1])))
            return probs.map(lambda t: (t[0], t[1].argmax())).collect()

        else:
            raise Exception(f'Not accepted data type: {type(sample)}')
    # ------------------------------------------------------------------------------

    def compute_probs_one_sample(self, x):
        if self.balltree is not None:
            if len(x.shape) == 2:
                y = numpy.zeros(self.num_classes)
                for n in range(len(x)):
                    knn = self.balltree.get_knn(x[n], self.K)
                    if len(knn) > self.K: knn = knn[:self.K]
                    for t in knn: y[t[0]] += 1
            elif len(x.shape) == 1:
                knn = self.balltree.get_knn(x, self.K)
                if len(knn) > self.K: knn = knn[:self.K]
                y = numpy.zeros(self.num_classes)
                for t in knn: y[t[0]] += 1
            else:
                raise Exception(f'Unexpected shape of a sample {x.shape}')
            return y / (1.0e-6 + y.sum())

    def predict_probs(self, sample):
        #
        if type(sample) == list or type(sample) == numpy.ndarray and len(sample.shape) >= 2:
            densities = list()
            i = 1
            for x in sample:
                densities.append(self.compute_probs_one_sample(x))
                print("predicted %10d samples" % i, end = '\r')
                i += 1
            #
            return numpy.array(densities)

        elif type(sample) == numpy.ndarray and len(sample.shape) == 1:
            return self.compute_probs_one_sample(sample)

        elif RDD is not None and isinstance(sample, RDD):
            return sample.map(lambda t: (t[0], self.compute_probs_one_sample(t[1]))).collect()

        else:
            raise Exception(f'Not accepted data type: {type(sample)}')
    # ------------------------------------------------------------------------------


from model import DenseNet
import torchvision
import torch
import time
import copy
import matplotlib.pyplot as plt
import numpy as np
from model.utils import *

device = torch.device('cuda:0' if torch.cuda.is_available() else "cpu")
print(device)
weight_decay = 0.0005
batch_size = 64
num_epochs = 60
save_path = './record/'

import os

if os.path.exists(save_path) == False:
    os.mkdir(save_path)

def train_model(model, dataloaders, criterion, optimizer, num_epochs):
    since = time.time()
    val_acc_history = []
    train_acc_history = []
    best_model_wts = copy.deepcopy(model.state_dict())
    best_acc = 0.0

    for epoch in range(num_epochs):
        print('Epoch {}/{}'.format(epoch + 1, num_epochs))
        print('-' * 10)
        if epoch == 40:
            optimizer.param_groups[0]['lr'] /= 10
        if epoch == 53:
            optimizer.param_groups[0]['lr'] /= 10

        for phase in ['train', 'val']:
            if phase == 'train':
                model.train()
            else:
                model.eval()

            running_loss = 0.0
            running_corrects = 0.0

            for inputs, labels in dataloaders[phase]:
                inputs = inputs.to(device)
                labels = labels.to(device)

                optimizer.zero_grad()

                with torch.set_grad_enabled(phase == 'train'):
                    outputs = model(inputs)
                    loss = criterion(outputs, labels)

                    _, preds = torch.max(outputs, 1)

                    if (phase == 'train'):
                        loss.backward()
                        optimizer.step()

                running_loss += loss.item() * inputs.size(0)
                running_corrects += torch.sum(preds == labels.data).item()

            epoch_loss = running_loss / len(dataloaders[phase].dataset)
            epoch_acc = running_corrects / len(dataloaders[phase].dataset)

            info = {'Epoch': [epoch + 1],
                    'Loss': [epoch_loss],
                    'Acc': [epoch_acc],
                    }
            record_info(info, 'record/' + phase + '.csv')

            if phase == 'val' and epoch_acc > best_acc:
                best_acc = epoch_acc
                best_model_wts = copy.deepcopy(model.state_dict())
            if phase == 'val':
                val_acc_history.append(epoch_acc)
            else:
                train_acc_history.append(epoch_acc)
        print()

    time_elapsed = time.time() - since
    print('Training complete in {:.0f}m {:0f}s'.format(time_elapsed // 60, time_elapsed % 60))
    print('Best val Acc: {:4f}'.format(best_acc))

    model.load_state_dict(best_model_wts)
    return model, val_acc_history, train_acc_history, best_acc

def savehis(val_hist, train_hist, path, name, best_acc, lr=None):
    plt.title("Accuracy vs. Number of Training Epochs")
    plt.xlabel('Training Epochs')
    plt.ylabel('Accuracy')
    plt.plot(range(1, num_epochs + 1), val_hist, label=name + ' val:' + '%.4f' % (best_acc))
    plt.plot(range(1, num_epochs + 1), train_hist, label=name + ' training')
    plt.ylim(0, 1.0)
    plt.xticks(np.arange(0, num_epochs + 1, 20))
    plt.legend()
    plt.savefig(path)

def main():
    transform = torchvision.transforms.Compose([
        torchvision.transforms.Pad(4),
        torchvision.transforms.RandomCrop((32, 32)),
        torchvision.transforms.RandomHorizontalFlip(),
        torchvision.transforms.ToTensor(),
    ])
    train_dataset = torchvision.datasets.CIFAR10('/home/qx/project/data/cifar10-data', train=True, transform=transform)
    test_dataset = torchvision.datasets.CIFAR10('/home/qx/project/data/cifar10-data', train=False,
                                                transform=torchvision.transforms.Compose([
                                                    torchvision.transforms.ToTensor(),
                                                ]))
    dataloaders_dict = {
        'train': torch.utils.data.DataLoader(
            train_dataset, batch_size=batch_size, shuffle=True, num_workers=4
        ),
        'val': torch.utils.data.DataLoader(
            test_dataset, batch_size=batch_size, shuffle=True, num_workers=4
        )
    }

    all_net = {
        #'DenseNet29': DenseNet.densenet29(),
        #'DenseNet45': DenseNet.densenet45(),
        'DenseNet85': DenseNet.densenet85()
    }
    lr = 0.1
    for name, net in all_net.items():
        net = net.to(device)
        optimizer = torch.optim.SGD(net.parameters(), lr=lr, momentum=0.9, weight_decay=weight_decay)
        criterion = torch.nn.CrossEntropyLoss()
        net, val_hist, train_hist, best_acc = train_model(net, dataloaders_dict, criterion, optimizer, num_epochs)
        # torch.save(net.state_dict(), save_path+name+'.pth')
        val_hist = [h for h in val_hist]
        train_hist = [h for h in train_hist]
        savehis(val_hist, train_hist, save_path + name + '.png', name, best_acc)

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 24 14:09:36 2018

@author: bijayamanandhar
"""

def subsets(s):

    if s == []: return [s]

    sets = [s]
    for i in range(len(s)):
        temp_subsets = subsets(s[:i] + s[i+1:])
        for subset in temp_subsets:
            if subset not in sets:
                sets.append(subset)
    return sets

print(subsets(['a', 'b', 'c']))
print(subsets([1, 2, 3]))


from collections import deque

# read input
n = int(input())
levels = []
for _ in range(n):
    input()
    level = []
    for _ in range(n):
        row = input().strip()
        level.append(list(row))
        if 'S' in row:
            start_x, start_y, start_z = _, row.index('S'), len(levels)
    levels.append(level)

# BFS
q = deque([(start_x, start_y, start_z, 0)])
visited = set([(start_x, start_y, start_z)])
steps = -1
while q:
    x, y, z, dist = q.popleft()
    if z == 0:
        # found exit to surface
        steps = dist
        break
    for dx, dy, dz in [(0, 0, 1), (0, 0, -1), (0, 1, 0), (0, -1, 0), (1, 0, 0), (-1, 0, 0)]:
        nx, ny, nz = x + dx, y + dy, z + dz
        if 0 <= nx < n and 0 <= ny < n and 0 <= nz < n and levels[nz][nx][ny] == '.' and (nx, ny, nz) not in visited:
            visited.add((nx, ny, nz))
            q.append((nx, ny, nz, dist+1))

if steps == -1:
    print(-1)
else:
    print(steps)

39. Путь спелеолога.py


"""Processors for the model training step of the worklow."""
import logging
import os.path as op

from sklearn.pipeline import Pipeline

from ta_lib.core.api import (
    get_dataframe,
    get_feature_names_from_column_transformer,
    get_package_path,
    load_dataset,
    load_pipeline,
    register_processor,
    save_pipeline,
    DEFAULT_ARTIFACTS_PATH
)
from ta_lib.regression.api import SKLStatsmodelOLS

logger = logging.getLogger(__name__)

@register_processor("model-gen", "train-model")
def train_model(context, params):
    """Train a regression model."""
    artifacts_folder = DEFAULT_ARTIFACTS_PATH

    input_features_ds = "train/sales/features"
    input_target_ds = "train/sales/target"

    # load training datasets
    train_X = load_dataset(context, input_features_ds)
    train_y = load_dataset(context, input_target_ds)

    # load pre-trained feature pipelines and other artifacts
    curated_columns = load_pipeline(op.join(artifacts_folder, "curated_columns.joblib"))
    features_transformer = load_pipeline(op.join(artifacts_folder, "features.joblib"))

    # sample data if needed. Useful for debugging/profiling purposes.
    sample_frac = params.get("sampling_fraction", None)
    if sample_frac is not None:
        logger.warn(f"The data has been sample by fraction: {sample_frac}")
        sample_X = train_X.sample(frac=sample_frac, random_state=context.random_seed)
    else:
        sample_X = train_X
    sample_y = train_y.loc[sample_X.index]

    # transform the training data
    train_X = get_dataframe(
        features_transformer.fit_transform(train_X, train_y),
        get_feature_names_from_column_transformer(features_transformer),
    )
    train_X = train_X[curated_columns]

    # create training pipeline
    reg_ppln_ols = Pipeline([("estimator", SKLStatsmodelOLS())])

    # fit the training pipeline
    reg_ppln_ols.fit(train_X, train_y.values.ravel())

    # save fitted training pipeline
    save_pipeline(
        reg_ppln_ols, op.abspath(op.join(artifacts_folder, "train_pipeline.joblib"))
    )