def fact(n):
    if n==1:
        return 1
    if n==0:
        return 1
    return n*fact(n-1)
arr=list(map(int,input().split()))
neg_count=0
zero_count=0
pos_count=0
total_pair=0

for i in range(len(arr)):
    if arr[i]<0:
        neg_count+=1
    elif arr[i]>0:
        pos_count+=1
    else:
        zero_count+=1
pos=0
for i in range(1,pos_count+1):
    pos+=int(fact(pos_count)/(fact(pos_count-i)*fact(i)))
if neg_count>=1:
    for i in range(1,neg_count+1,2):
        total_pair+=int(fact(neg_count)/(fact(neg_count-i)*fact(i)))*pos

else:
    print(total_pair)

print(total_pair+neg_count)


# -*- coding: utf-8 -*-

def dijkstra(src, dest, dico_voisins) :
    # Initilisation
    covered = []
    dist = {}
    pred = {}
    for cellule in dico_voisins.keys() :
        if cellule == src :
            dist[cellule] = 0
        else :
            dist[cellule] = -1
    pivot = src

    # Etape fondamentale
    while pivot != dest :
        covered.append(pivot)
        for voisin, distance in dico_voisins[pivot].items() :
            if voisin not in covered :
                if ( (dist[pivot] + distance) < dist[voisin] ) or ( dist[voisin] == -1) :
                    dist[voisin] = (dist[pivot] + distance)
                    pred[voisin] = pivot

        # déterminer le nouveau pivot (le sommet le plus proche du pivot)
        plus_proche = (None, -1)
        for cellule, distance in dist.items() :
            if cellule not in covered :
                if (distance != -1) :
                    if (plus_proche[1] > distance ) or (plus_proche[1] == -1) :
                        plus_proche = (cellule, distance)
        pivot = plus_proche[0]
    return pred

def set_rang(src, dst, dico_voisins):
    pred = dijkstra(src, dst, dico_voisins)
    rang = 1
    current = pred[dst]
    while (current != src) :
        current = pred[current]
        rang += 1
    return rang


# coding: utf-8

# In[1]:

from flask import Flask,render_template,session,url_for,request,redirect
from flask_pymongo import PyMongo
from flask_bcrypt import Bcrypt
from flask import jsonify,json
import os
import gspread

from oauth2client.service_account import ServiceAccountCredentials
import pprint
import datetime
import argparse
import pickle
from model import create_model

import pprint
import datetime

app1 = Flask(__name__)

nn4_small2_pretrained = create_model()

# In[2]:

nn4_small2_pretrained.load_weights('weights/nn4.small2.v1.h5')
# arguments
parser = argparse.ArgumentParser()
parser.add_argument("-c","--course",help="Course ID for attendance")
args = vars(parser.parse_args())
print(args['course'])

# In[3]:

import numpy as np
import os.path

class IdentityMetadata():
    def __init__(self, base, name, file):
        # dataset base directory
        self.base = base
        # identity name
        self.name = name
        # image file name
        self.file = file

    def __repr__(self):
        return self.image_path()

    def image_path(self):
        return os.path.join(self.base, self.name, self.file)

def load_metadata(path):
    metadata = []
    for i in os.listdir(path):
        for f in os.listdir(os.path.join(path, i)):
            # Check file extension. Allow only jpg/jpeg' files.
            ext = os.path.splitext(f)[1]
            if ext == '.jpg' or ext == '.jpeg' or ext=='.png':
                metadata.append(IdentityMetadata(path, i, f))
    return np.array(metadata)

metadata = load_metadata('images')
print('metadata created')
print(metadata)

# In[4]:

import cv2
import matplotlib.pyplot as plt
import matplotlib.patches as patches

from align import AlignDlib

# get_ipython().run_line_magic('matplotlib', 'inline')

def load_image(path):
    img = cv2.imread(path, 1) #BGR
    return img[...,::-1] #RGB

alignment = AlignDlib('shape_predictor_68_face_landmarks.dat')

#combined transformation
def align_image(img):
    return alignment.align(96, img, alignment.getLargestFaceBoundingBox(img),
                           landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE)

# In[5]:

import pickle
embedded = np.zeros((metadata.shape[0], 128))

real_name = {}

embeddings = open('embeddings.pkl','rb')
embedded = pickle.load(embeddings)
embeddings.close()

for i, m in enumerate(metadata):
#     img = load_image(m.image_path())
#     img = align_image(img)
    # scale RGB values to interval [0,1]
#     if img is not None:
#         img = (img / 255.).astype(np.float32)
        # obtain embedding vector for image
#         embedded[i] = nn4_small2_pretrained.predict(np.expand_dims(img, axis=0))[0]
    real_name[os.path.dirname(m.image_path()[7:])] = embedded[i]
    print(i)
    print(m.name)

# embeddings = open('embeddings.pkl','wb')
# pickle.dump(embedded,embeddings)
# embeddings.close()

def real_names():
    return real_name

# In[6]:

def distance(emb1, emb2):
    return np.sum(np.square(emb1 - emb2))

# In[7]:

def show_pair(idx1, idx2):
    plt.figure(figsize=(6,3))
    plt.suptitle(f'Distance = {distance(embedded[idx1], embedded[idx2]):.2f}')
    plt.subplot(121)
    plt.imshow(load_image(metadata[idx1].image_path()))
    plt.subplot(122)
    plt.imshow(load_image(metadata[idx2].image_path()));

show_pair(78, 76)
show_pair(78, 17)

# In[8]:

def recognize(embedded):
    min_dist = 100
    _id = None
    for name,emb in real_name.items():
        dist = np.sum(np.square(emb - embedded))

        if dist < min_dist:
            min_dist = dist
            _id = name
    if min_dist > 0.58:
        print(min_dist)
        return None
    else:
        print(min_dist)
        return _id

# In[9]:

from sklearn.metrics import f1_score, accuracy_score

distances = [] # squared L2 distance between pairs
identical = [] # 1 if same identity, 0 otherwise

num = len(metadata)

for i in range(num - 1):
    for j in range(1, num):
        distances.append(distance(embedded[i], embedded[j]))
        identical.append(1 if metadata[i].name == metadata[j].name else 0)

distances = np.array(distances)
identical = np.array(identical)

thresholds = np.arange(0.3, 1.0, 0.01)

f1_scores = [f1_score(identical, distances < t) for t in thresholds]
acc_scores = [accuracy_score(identical, distances < t) for t in thresholds]

opt_idx = np.argmax(f1_scores)
# Threshold at maximal F1 score
opt_tau = thresholds[opt_idx]
# Accuracy at maximal F1 score
opt_acc = accuracy_score(identical, distances < opt_tau)

# Plot F1 score and accuracy as function of distance threshold
plt.plot(thresholds, f1_scores, label='F1 score');
plt.plot(thresholds, acc_scores, label='Accuracy');
plt.axvline(x=opt_tau, linestyle='--', lw=1, c='lightgrey', label='Threshold')
plt.title(f'Accuracy at threshold {opt_tau:.2f} = {opt_acc:.3f}');
plt.xlabel('Distance threshold')
plt.legend();

# In[10]:

embedded = np.zeros((1, 128))

def recognize_image(image_path):
    img = load_image(image_path)
    img = align_image(img)
    if img is not None:
        img = (img / 255.).astype(np.float32)
        # obtain embedding vector for image
        embedded = nn4_small2_pretrained.predict(np.expand_dims(np.array(img), axis=0))[0]
        name = recognize(embedded)
        print(name)
        return name
    return None

cap = cv2.VideoCapture(1)

# def webcam_recognize():
#     while(True):
#         ret, frame = cap.read()
#         cv2.imwrite('temp.jpg',frame)
#         cv2.waitKey(20)
#         if 'temp.jpg' is not None:
#             name = recognize_image('temp.jpg')
#             cv2.imshow('temp',frame)
#             print(name)
#         if cv2.waitKey(1) & 0xFF == ord('q'):
#             break

#     cap.release()
#     cv2.destroyAllWindows()

   # webcam_recognize()
# recognize_image('.jpg')

#students list for gspread updations
students = []

from mtcnn.mtcnn import MTCNN
import pickle
def multiple_recognize():
    while(True):
        ret,frame = cap.read()
        cv2.imwrite('temp.jpg',frame)
        detector = MTCNN()
        image = load_image('temp.jpg')
        faces = detector.detect_faces(image)
        print(faces)
        if faces is not None:
            for face in faces:
                (x,y,w,h) = face['box']
                cv2.imwrite('temp.jpg',frame)
                if 'temp.jpg' is not None:
                    name = recognize_image('temp.jpg')
                    cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),5)
                    cv2.imshow('Faces',frame)
                    if name!=None and name not in students:
                        students.append(name)
        stud_names = open('present.pickle','wb')
        pickle.dump(students, stud_names,protocol=2)
        stud_names.close()

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()

#                 cv2.imshow('parts',image[y:y+h,x:x+w])

multiple_recognize()

# In[11]:

# import datetime
# import os
# import subprocess as s

# s.call("python mark_attendance.py", shell=True)
# today = datetime.date.today()
# formatted_date = today.strftime("%m-%d-%Y")
# print(formatted_date)

# from mark_attendance import mark_attendance

# mark_attendance(students)
import datetime

today = datetime.date.today()
formatted_date = today.strftime("%m-%d-%Y")
print(formatted_date)

from mark_attendance import mark_attendance

mark_attendance(students,args['course'])


# Import the necessary libraries
import math

# Define a function to check if a point is inside a circle
def is_point_in_circle(point, center, radius):
    x, y = point
    cx, cy = center
    return math.sqrt((x - cx) ** 2 + (y - cy) ** 2) <= radius

# Define a function to find the circle that covers the most uncovered points
def find_best_circle(points, uncovered, centers, radius):
    best_circle = None
    max_coverage = 0
    for center in centers:
        coverage = 0
        for point in points:
            if point in uncovered and is_point_in_circle(point, center, radius):
                coverage += 1
        if coverage > max_coverage:
            best_circle = center
            max_coverage = coverage
    return best_circle

# Define a function to solve the maximum coverage problem
def solve_maximum_coverage(points, M, radius):
    # Initialize the list of uncovered points and the list of circle centers
    uncovered = points[:]
    centers = []

    # Repeatedly find the best circle and remove its coverage from the list of uncovered points
    for _ in range(M):
        center = find_best_circle(points, uncovered, centers, radius)
        if center is None:
            break
        centers.append(center)
        uncovered = [point for point in uncovered if not is_point_in_circle(point, center, radius)]

    # Return the resulting list of circle centers
    return centers


from PyQt5.QtWidgets import QMainWindow
from ui.MainUI import Ui_MainWindow
from service import globalValue
import time

class MainWindow(QMainWindow,Ui_MainWindow):
    def __init__(self,parent=None):
        """
        传入staff全局变量
        :param parent:
        """
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.staff = globalValue.get_staff()
        print(self.staff.sname[0])
        self.welcome.setText(self.staff.sname + ',你好。你的权限为：' + self.staff.srole + '。今天是' + time.strftime("%Y-%m-%d", time.localtime()))
        self.staffbutton.clicked.connect(self.gotoStaff)
        self.roombutton.clicked.connect(self.gotoRoom)
        self.clientbutton.clicked.connect(self.gotoClient)
        self.orderbutton.clicked.connect(self.gotoOrder)
        self.chartbutton.clicked.connect(self.gotoChart)
        self.modifyPwd.clicked.connect(self.modifyPasswd)

    def modifyPasswd(self):
        from service.modifyPwd import mpWindow
        self.mpWindow = mpWindow()
        self.close()
        self.mpWindow.show()

    def gotoChart(self):
        from service.chartOp import ChartOp
        self.ChartOp = ChartOp()
        self.ChartOp.show()

    def gotoOrder(self):
        from service.orderOp import OrderOp
        self.OrderOp = OrderOp()
        self.OrderOp.show()

    def gotoClient(self):
        from service.clientOp import ClientOp
        self.ClientOp = ClientOp()
        self.ClientOp.show()

    def gotoRoom(self):
        from service.roomOp import RoomOp
        self.RoomOp = RoomOp()
        self.RoomOp.show()

    def gotoStaff(self):
        from service.staffOp import StaffOP
        self.StaffOP = StaffOP()
        self.StaffOP.show()


import os

# 遍历目录 --- 深度遍历
def getAllDirFile(sourcePath):
#     创建一个空的列表
    listPath = []
    listPath.append(sourcePath)
    while True:
        if len(listPath) == 0:
            break
        path = listPath.pop()
        fileNameList = os.listdir(path)
        for fileName in fileNameList:
            absPath = os.path.join(path,fileName)
            if os.path.isdir(absPath):#是否是目录
            #     添加到栈中继续遍历
                listPath.append(absPath)
                # print("目录:",fileName)
            if os.path.isfile(absPath): #是否是文件
                # pass
                # print("文件:",fileName)
                parseData(absPath)

# 用来解析文件
def parseData(filePath):
    targetPath = r"C:\Users\wenyucheng\Desktop\资料\作业\res"
#     打开一个文件
    with open(filePath,"r",encoding="utf-8",errors="ignore") as rf:
#         一行一行的读取文件
        while True:
            lineData = rf.readline()
            if len(lineData) == 0: #读完了
                break
#            一行数据 :  xiaozhuc-o-o-l@163.com----henry19891010
#            将账号和秘密分割
            userStr = lineData.split("----")[0]
            # 找到 "@" 的位置
#             indexA = userStr.rfind("@")
# #           找到 .的位置
#             indexD =  userStr.rfind(".")
# #           截取类型
#             mailType = userStr[indexA+1:indexD]
#             print(mailType)

# 另外一种方式
#               xiaozhuc-o-o-l@163.com.cn
#           获取到邮箱类型
            mailType =  userStr.split("@")[1].split(".")[0]
            # print(mailType)

#            拼接目录
            typeDir = os.path.join(targetPath,mailType)
#           判断该类型对应的目录是否存在
            if not os.path.exists(typeDir): #如果该目录不存在则创建该目录
                # mkdir
                os.makedirs(typeDir)

#            拼接文件路径
            typeFilePath = os.path.join(typeDir, mailType + ".txt")
#           将邮箱追加到文件中去
            with open(typeFilePath,"a",encoding="utf-8") as af:
                af.write(userStr + "\n")
                af.flush()
def main():
    # pass
    sourcePath = r"C:\Users\wenyucheng\Desktop\资料\作业\newdir"
    getAllDirFile(sourcePath)

if __name__ == '__main__':
    main()

文件操作/06邮箱分类作业.py


import argparse
import os.path
import time

from service import registry

def common_parser(script_name):
	parser = argparse.ArgumentParser(prog=script_name)
	service_name = os.path.splitext(os.path.basename(script_name))[0]
	parser.add_argument(
			"--grpc-port",
			help="port to bind gRPC service to",
			default=registry[service_name]['grpc'],
			type=int,
			required=False
		)
	return parser

# from gRPC docs:
# because start() does not block and if need to sleep-loop there is nothing
# else code to do while serving
def main_loop(grpc_handler, args):
	server = grpc_handler(port=args.grpc_port)
	server.start()
	try:
		while True:
			time.sleep(1)
	except KeyboardInterrupt:
		server.stop(0)


import os
import re
import sys
import struct

# parameter
out_test_en = 1
write_file_en = 1

# argument
arg_num = len(sys.argv)
if arg_num == 2:
    fi = sys.argv[1]
    print('INFO file name: %s' % (fi))
else:
    fi = ''
    print('ERROR: no input file!!!')
    sys.exit(0)

# function
def my_int2hex(i_int):
    if i_int < 10: # 0~9
        i_int = i_int + 48
    else: # A~F
        i_int = i_int + 65 - 10
    i_chr = chr(i_int)
    return i_chr

def my_bytes2char(bt):
    i_int = int.from_bytes(bt, byteorder='big')
    bl = i_int & 15
    bh = (i_int >> 4) & 15
    bl_chr = my_int2hex(bl)
    bh_chr = my_int2hex(bh)
    o_chr = bh_chr + bl_chr
    return o_chr

def my_charproc(ch):
    if ch == 'O' or ch == 'o':
        ch = '0'
    elif ch == 'L' or ch == 'l':
        ch = '1'
    return ch

# size
fs = 0
fs = os.path.getsize(fi)
print('INFO file size: %0d bytes' % (fs))

# read
line_str = ''
with open(fi, 'r') as f:
    for l in f:
        l = l.strip()
        line_str = line_str + l

# out test
if out_test_en:
    # rename
    res = re.search('\.', fi)
    if res:
        sp = res.start()
        fo = fi[:sp-1] + '_hexout' + fi[sp:]
    else:
        fo = fi + '_hexout'
    # write
    with open(fo, 'w') as f:
        f.write(line_str)
    print('INFO test file: %s write finish.' % fo)

# write file
if write_file_en:
    # rename
    res = re.search('\.', fi)
    if res:
        sp = res.start()
        fo = fi[:sp-1] + '_bin' + fi[sp:]
    else:
        fo = fi + '_bin'
    # write
    with open(fo, 'wb') as f:
        for i in range(len(line_str)):
            if i&1 == 0:
                bh = line_str[i]
                bh = my_charproc(bh)
            else:
                bl = line_str[i]
                bl = my_charproc(bl)
                # to int
                bh = int(bh, 16)
                bl = int(bl, 16)
                bt = bh*16 + bl
                bt = bt.to_bytes(1, byteorder='big', signed=False)
                f.write(bt)
    print('INFO write bin file: %s write finish.' % fo)


#encoding:utf-8

from utils import get_url

t_channel = '@reddit_all'
subreddit = 'all'

def send_post(submission, r2t):
    what, url, ext = get_url(submission)
    title = submission.title
    link = submission.shortlink
    sub = submission.subreddit
    upvotes = submission.score

    if what == 'text':
        punchline = submission.selftext
        text = '{title}\n\n{main_text}\n\n{votes} upvotes\n/r/{subreddit}\n{link}'.format(
            title=title,
            main_text=punchline,
            subreddit=sub,
            link=link,
            votes=upvotes)
        if len(text) > 2345:
            return False
        return r2t.send_text(text)

    if what == 'other':
        base_url = submission.url
        text = '{title}\n{base_url}\n\n{votes} upvotes\n/r/{subreddit}\n{link}'.format(
            title=title,
            base_url=base_url,
            subreddit=sub,
            link=link,
            votes=upvotes)
        return r2t.send_text(text)

    if what == 'album':
        base_url = submission.url
        text = '{title}\n{base_url}\n\n{votes} upvotes\n/r/{subreddit}\n{link}'.format(
            title=title,
            base_url=base_url,
            subreddit=sub,
            link=link,
            votes=upvotes)
        r2t.send_text(text)
        r2t.send_album(url)
        return True

    if what in ('gif', 'img'):
        text = '{title}\n\n{votes} upvotes\n/r/{subreddit}\n{link}'.format(
            title=title,
            subreddit=sub,
            link=link,
            votes=upvotes)
        return r2t.send_gif_img(what, url, ext, text)

    return False


from typing import Optional, NamedTuple

import numpy as np

class RolloutBufferSamples(NamedTuple):
    loc: np.ndarray
    weight: np.ndarray
    actions: np.ndarray
    old_values: np.ndarray
    old_log_prob: np.ndarray
    advantages: np.ndarray
    returns: np.ndarray

class RolloutBuffer():
    """
    Rollout buffer used in on-policy algorithms like A2C/PPO.
    It corresponds to ``buffer_size`` transitions collected
    using the current policy.
    This experience will be discarded after the policy update.
    In order to use PPO objective, we also store the current value of each state
    and the log probability of each taken action.

    The term rollout here refers to the model-free notion and should not
    be used with the concept of rollout used in model-based RL or planning.
    Hence, it is only involved in policy and value function training but not action selection.

    Buffer is used to save all agents' transitions, so all agents share the same rewards and states.
    """

    def __init__(
        self,
        buffer_size: int,
        vehicle_num: int,
        weight_shape: int,
        gae_lambda: float = 1,
        gamma: float = 0.99,
    ):
        '''
        :param buffer_size: Max number of element in the buffer
        :param weight_shape: Observation weight shape
        :param gae_lambda: Factor for trade-off of bias vs variance for Generalized Advantage Estimator
        Equivalent to classic advantage when set to 1.
        :param gamma: Discount factor
        '''
        self.buffer_size = buffer_size
        self.vehicle_num = vehicle_num
        self.weight_shape = weight_shape
        self.pos = 0
        self.full = False
        self.gae_lambda = gae_lambda
        self.gamma = gamma
        self.loc, self.weight, self.actions, self.rewards, self.advantages = None, None, None, None, None
        self.returns, self.dones, self.values, self.log_probs = None, None, None, None
        self.generator_ready = False
        self.reset()

    def reset(self) -> None:
        self.loc = np.zeros((self.buffer_size, self.vehicle_num * 2), dtype=np.float32)
        self.weight = np.zeros((self.buffer_size, self.weight_shape), dtype=np.float32)
        self.actions = np.zeros((self.buffer_size, self.vehicle_num), dtype=np.float32)
        self.rewards = np.zeros((self.buffer_size, self.vehicle_num), dtype=np.float32)
        self.returns = np.zeros((self.buffer_size, self.vehicle_num), dtype=np.float32)
        self.dones = np.zeros((self.buffer_size, self.vehicle_num), dtype=np.float32)
        self.values = np.zeros((self.buffer_size, self.vehicle_num), dtype=np.float32)
        self.log_probs = np.zeros((self.buffer_size, self.vehicle_num), dtype=np.float32)
        self.advantages = np.zeros((self.buffer_size, self.vehicle_num), dtype=np.float32)
        self.generator_ready = False
        self.pos = 0
        self.full = False

    def add(
        self, obs: list, actions: np.ndarray, reward: np.ndarray,
            done: bool, value: np.ndarray, log_prob: np.ndarray,
    ) -> None:
        """
        :param obs: Observation, [0] is loc, [1] is weight
        :param actions: Action
        :param reward:
        :param done: End of episode signal.
        :param value: estimated value of the current state
            following the current policy.
        :param log_prob: log probability of the action
            following the current policy.
        """

        self.loc[self.pos] = obs[0].copy()
        self.weight[self.pos] = obs[1].copy()
        self.actions[self.pos] = actions.copy()
        self.rewards[self.pos] = reward.copy()
        self.dones[self.pos] = np.array(done).copy()
        self.values[self.pos] = value.copy()
        self.log_probs[self.pos] = log_prob.copy()
        self.pos += 1
        if self.pos == self.buffer_size:
            self.full = True

    def compute_returns_and_advantage(self, last_values: np.ndarray, done: bool) -> None:
        """
        Post-processing step: compute the returns (sum of discounted rewards)
        and GAE advantage.
        Adapted from Stable-Baselines PPO2.

        Uses Generalized Advantage Estimation (https://arxiv.org/abs/1506.02438)
        to compute the advantage. To obtain vanilla advantage (A(s) = R - V(S))
        where R is the discounted reward with value bootstrap,
        set ``gae_lambda=1.0`` during initialization.

        :param last_values: shape = (1, self.vehicle_num)
        :param done:

        """
        # convert to numpy
        last_values = last_values.copy()
        done = np.array([done] * self.vehicle_num)

        last_gae_lam = np.zeros(self.vehicle_num)
        for step in reversed(range(self.buffer_size)):
            if step == self.buffer_size - 1:
                next_non_terminal = 1.0 - done
                next_values = last_values
            else:
                next_non_terminal = 1.0 - self.dones[step + 1]
                next_values = self.values[step + 1]
            delta = self.rewards[step] + self.gamma * next_values * next_non_terminal - self.values[step]
            last_gae_lam = delta + self.gamma * self.gae_lambda * next_non_terminal * last_gae_lam
            self.advantages[step] = last_gae_lam
        self.returns = self.advantages + self.values

    def copy_or_not(self, array: np.ndarray, copy: bool = True) -> np.ndarray:
        """
        Convert a numpy array to a PyTorch tensor.
        Note: it copies the data by default

        :param array:
        :param copy: Whether to copy or not the data
            (may be useful to avoid changing things be reference)
        :return:
        """
        if copy:
            return array.copy()
        return array

    def _get_samples(self, batch_inds: np.ndarray):
        data = (
            self.loc[batch_inds],
            self.weight[batch_inds],
            self.actions[batch_inds],
            self.values[batch_inds],
            self.log_probs[batch_inds],
            self.advantages[batch_inds],
            self.returns[batch_inds],
        )
        return RolloutBufferSamples(*tuple(map(self.copy_or_not, data)))

    def get(self, batch_size: Optional[int] = None):
        assert self.full, 'Must fill the container if you want to sample from container'
        indices = np.random.permutation(self.buffer_size)

        # Return everything, don't create minibatches
        if batch_size is None:
            batch_size = self.buffer_size

        start_idx = 0
        while start_idx < self.buffer_size:
            yield self._get_samples(indices[start_idx: start_idx + batch_size])
            start_idx += batch_size


# display book
# lend book
# add book
# return book
class online_lib:
    def __init__(self, name):
        self.name = name

    # def lend_bood(self):
    print("This is the online library made by Theven")
    nname = str(input("Please enter your name : "))
    int(input("Press 1 for Displaying the book\n"
              "2 for lending the book\n"
              "3 for adding the book\n"
              "4 for returning the book :::::: "))


# -*- coding:utf-8 -*-
"""
剑指offer第31题
问题：
    求连续子数组之和最大的值
思路：
 从左到右计算前n项和,记住当前总和，如果总和小于0，就从第n+1项开始重新计和，并记录遇到过的最大值
"""

def maxsum_subarr(arr):
    maxsum = 0
    cursum = 0
    for i in range(len(arr)):
        if cursum<=0:
            cursum = arr[i]
        else:
            cursum += arr[i]
        if(cursum > maxsum):
            maxsum = cursum
    return maxsum

if __name__ == "__main__":
    arr = [-2,4,5,-2,5,-7,6,3,-1]
    print(maxsum_subarr(arr))

第31题_最大连续子数组.py


import tkinter as tk

class View(tk.Frame):
    def __init__(self, parent):

        super().__init__(parent)

        self.label = tk.Label(self, text='User:')
        self.label.grid(row=1, column=0)

        self.input_var = tk.StringVar()
        self.input_entry = tk.Entry(self, textvariable=self.input_var, width=30)
        self.input_entry.grid(row=1, column=1, sticky=tk.NSEW)

        self.add_button = tk.Button(self, text='Add', command=self.add_button_clicked)
        self.add_button.grid(row=1, column=2, padx=10)
        self.del_button = tk.Button(self, text='Del', command=self.del_button_clicked)
        self.del_button.grid(row=2, column=2, padx=10)
        self.show_button = tk.Button(self, text='Show', command=self.show_button_clicked)
        self.show_button.grid(row=3, column=2, padx=10)

        self.output_label = tk.Label(self, text='Users:', foreground='red')
        self.output_label.grid(row=4, column=1, sticky=tk.W)

        self.controller = None

    def set_controller(self, controller):
        self.controller = controller

    def add_button_clicked(self):
        if self.controller:
            self.controller.add(self.input_var.get())

    def del_button_clicked(self):
        if self.controller:
            self.controller.delete()

    def show_button_clicked(self):
        if self.controller:
            self.controller.show()

    def show(self, user_list):
        self.output_label['text'] = user_list
        self.output_label['foreground'] = 'green'
        self.output_label.after(9000, self.hide_user_list)

        self.input_entry['foreground'] = 'black'
        self.input_var.set('')

    def hide_user_list(self):
        self.output_label['text'] = ''


import ssl
from http.client import HTTPSConnection, HTTPConnection
from urllib.parse import urlparse

class Host(object):
    def __init__(self, hostname: str):
        self.hostname = hostname
        self.protocol = None
        self.is_connectable = None
        self.check_host()

    def check_host(self):
        is_http = self.check_http_url()
        is_https = self.check_https_url()

        if is_https:
            self.protocol = 'HTTPS'
            self.is_connectable = True
        else:
            if is_http:
                self.protocol = 'HTTP'
                self.is_connectable = True
            else:
                self.is_connectable = False

    def check_https_url(self):
        https_url = f'https://{self.hostname}'
        try:
            https_url = urlparse(https_url)
            connection = HTTPSConnection(https_url.netloc, timeout=5)
            connection.request('HEAD', https_url.path)
            if connection.getresponse():
                return True
            else:
                return False
        except ssl.CertificateError as error:
            return True
        except Exception as error:
            return False

    def check_http_url(self):
        http_url = f'http://{self.hostname}'
        try:
            http_url = urlparse(http_url)
            connection = HTTPConnection(http_url.netloc)
            connection.request('HEAD', http_url.path)
            if connection.getresponse():
                return True
            else:
                return False
        except Exception as error:
            return False


# -*- coding: utf-8 -*-
from django.conf.urls import patterns, url

from bbs.views import (
    threads,
    thread,
    create_thread,
    tag,
    create_tag,
)

urlpatterns = patterns(
    'bbs.views',

    url(
        regex=r'^threads/?$',
        view=threads,
        name='threads',
    ),
    url(
        regex=r'^thread/(?P<id>\d+)/?$',
        view=thread,
        name='thread',
    ),
    url(
        regex=r'^thread/create/?$',
        view=create_thread,
        name='create_thread',
    ),
    url(
        regex=r'^tag/(?P<id>\d+)/?$',
        view=tag,
        name='tag',
    ),
    url(
        regex=r'^tag/create/?$',
        view=create_tag,
        name='create_tag',
    ),
)


#!/usr/bin/env python
# coding: utf-8

# In[6]:

import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from PIL import Image #Python Imaging Library

# In[7]:

A_gray = Image.open(r"C:\Users\SAHYADRI\Pictures\Saved Pictures\small-fuffy-dog-breeds-1623362663.jpg")
A_gray = A_gray.convert('L')

temp = np.asarray(A_gray.getdata(), dtype=np.float64).reshape((A_gray.size[1], A_gray.size[0]))
A_gr = np.asarray(temp, dtype=np.uint8)

# In[8]:

plane7 = A_gr & 128*np.ones(np.shape(A_gr)).astype('uint8')
plane6 = A_gr &  64*np.ones(np.shape(A_gr)).astype('uint8')
plane5 = A_gr &  32*np.ones(np.shape(A_gr)).astype('uint8')
plane4 = A_gr &  16*np.ones(np.shape(A_gr)).astype('uint8')
plane3 = A_gr &   8*np.ones(np.shape(A_gr)).astype('uint8')
plane2 = A_gr &   4*np.ones(np.shape(A_gr)).astype('uint8')
plane1 = A_gr &   2*np.ones(np.shape(A_gr)).astype('uint8')
plane0 = A_gr &   1*np.ones(np.shape(A_gr)).astype('uint8')

# In[9]:

plt.figure(figsize=(20,7))
plt.subplot(2, 4, 1)
plt.imshow(plane7, cmap='gray')
plt.title('Plane 7 (MSB)')
plt.subplot(2, 4, 2)
plt.imshow(plane6, cmap='gray')
plt.title('Plane 6')
plt.subplot(2, 4, 3)
plt.imshow(plane5, cmap='gray')
plt.title('Plane 5')
plt.subplot(2, 4, 4)
plt.imshow(plane4, cmap='gray')
plt.title('Plane 4')
plt.subplot(2, 4, 5)
plt.imshow(plane3, cmap='gray')
plt.title('Plane 3')
plt.subplot(2, 4, 6)
plt.imshow(plane2, cmap='gray')
plt.title('Plane 2')
plt.subplot(2, 4, 7)
plt.imshow(plane1, cmap='gray')
plt.title('Plane 1')
plt.subplot(2, 4, 8)
plt.imshow(plane0, cmap='gray')
plt.title('Plane 0 (LSB)');

# In[ ]:


import os
import sys
import javapackage

base_path = os.path.expanduser("~/sct-master/git/statecharts/plugins")

lib = javapackage.PackageLibrary()

dirwalker = javapackage.DirWalker(base_path, ["xtend-gen", "bin", "src-gen", ".settings", "org.yakindu.sct.doc.user"])

files = [f for f in dirwalker.get_file_paths() if os.path.splitext(f)[1] in [".xtend", ".java"]]

for f in files:
    consumer = javapackage.CodeConsumer(f)
    print(f)
    print('\t' + consumer.package)
    package = lib.construct_from_string(consumer.package)
    for imp in consumer.imports:
        print('\t' + imp)
        package.add_dependency(lib.construct_from_string(imp))

for package in lib.packages:
    print(package.get_full_name())


import sys
import os
import json
import argparse
from pathlib import Path
from glob import glob
import charts
from io import StringIO
import shutil
from zipfile import ZipFile

OUTPUT_DIR = 'out'

GREEDY_ALGS = [
    'Greedy-ContinuousNBStart-HeuristicInit',
    'Greedy-ContinuousNBStart-RandomInit',
    'Greedy-RandomNBStart-RandomInit',
    'Greedy-RandomNBStart-HeuristicInit',
    'Greedy-ZeroNBStart-HeuristicInit',
    'Greedy-ZeroNBStart-RandomInit',
    ]

STEEPEST_ALGS = [
    'Steepest-ContinuousNBStart-HeuristicInit',
    'Steepest-ContinuousNBStart-RandomInit',
    'Steepest-RandomNBStart-RandomInit',
    'Steepest-RandomNBStart-HeuristicInit',
    'Steepest-ZeroNBStart-HeuristicInit',
    'Steepest-ZeroNBStart-RandomInit'
    ]

SUMMARY_ALGS_ALL = [
    'Random',
    'Heuristic',
    'Steepest-ZeroNBStart-HeuristicInit',
    'Greedy-ZeroNBStart-HeuristicInit',
    'SimulatedAnnealing-HeuristicInit_increaseRatio1',
    'TabuSearch-HeuristicInit'
    ]
SUMMARY_ALGS_SELECTED = [
    'Steepest-ZeroNBStart-HeuristicInit',
    'Greedy-ZeroNBStart-HeuristicInit',
    'SimulatedAnnealing-HeuristicInit_increaseRatio1',
    'TabuSearch-HeuristicInit'
    ]
RANDOM_ALGS_SELECTED = ['Greedy-RandomNBStart-RandomInit', 'Steepest-RandomNBStart-RandomInit']
SUMMARY_ALGS_SA = [
    'SimulatedAnnealing-RandomInit_increaseRatio3',
    'SimulatedAnnealing-HeuristicInit_increaseRatio4',
    'SimulatedAnnealing-HeuristicInit_increaseRatio3',
    'SimulatedAnnealing-HeuristicInit_increaseRatio2',
    'SimulatedAnnealing-HeuristicInit_increaseRatio1'
    ]
SUMMARY_ALGS_TS = [
    'TabuSearch-RandomInit',
    'TabuSearch-HeuristicInit',
    'TabuSearch-HeuristicInit-BreakTabu',
    'TabuSearch-HeuristicInit-NoCollisions',
    'TabuSearch-HeuristicInit-NoCollisions_BreakTabu'
]

def test_ts(instances):
    efiiciency = charts.generate('efficiency_SA',
'{{Porównanie efektywności algorytmów SA}}',
instances, charts.CType.TIME_EFF, alg_types=SUMMARY_ALGS_SA, map_alg_name=False)

def test_sa(instances):
    efiiciency = charts.generate('efficiency_TS',
'{{Porównanie efektywności algorytmów TS}}',
instances, charts.CType.TIME_EFF, alg_types=SUMMARY_ALGS_TS, map_alg_name=False)

def summary_charts(instances):
    charts.generate('avg_cmp_greedy',
"Wpływ przyjętej strategi przeglądania oraz początkowego rozwiązania na \\textbf{{jakość}} rozwiązania końcowego dla heurystyki \\textit{{Local Search}} typu \\textbf{{\\textit{{Greedy}}}}",
instances, charts.CType.AVG, GREEDY_ALGS, map_alg_name=False)
    charts.generate('avg_cmp_steepest',
"Wpływ przyjętej strategi przeglądania oraz początkowego rozwiązania na \\textbf{{jakość}} rozwiązania końcowego dla heurystyki \\textit{{Local Search}} typu \\textbf{{\\textit{{Steepest}}}}",
instances, charts.CType.AVG, STEEPEST_ALGS, map_alg_name=False)
    charts.generate('efficiency_cmp_greedy',
"Wpływ przyjętej strategi przeglądania oraz początkowego rozwiązania na \\textbf{{efektywność}} rozwiązania końcowego dla heurystyki \\textit{{Local Search}} typu \\textbf{{\\textit{{Greedy}}}}",
instances, charts.CType.TIME_EFF, GREEDY_ALGS, map_alg_name=False)
    charts.generate('efficiency_cmp_steepest',
"Wpływ przyjętej strategi przeglądania oraz początkowego rozwiązania na \\textbf{{efektywność}} rozwiązania końcowego dla heurystyki \\textit{{Local Search}} typu \\textbf{{\\textit{{Steepest}}}}",
instances, charts.CType.TIME_EFF, STEEPEST_ALGS, map_alg_name=False)

    max_scores = charts.generate('best_cmp',
"Porównanie badanych algorytmów pod względem miary jakości (wzór \\ref{{eq:quality}}) dla \\textbf{{najlepszego}} ostatecznie uzyskanego wyniku",
instances, charts.CType.MIN, SUMMARY_ALGS_ALL)

    min_scores = charts.generate('worst_cmp',
"Porównanie badanych algorytmów pod względem miary jakości (wzór \\ref{{eq:quality}}) dla \\textbf{{najgorszego}} ostatecznie uzyskanego wyniku",
instances, charts.CType.MAX, SUMMARY_ALGS_ALL)

    avg_scores = charts.generate('avg_cmp',
"Porównanie badanych algorytmów pod względem miary jakości (wzór \\ref{{eq:quality}}) dla \\textbf{{średniej}} z ostatecznie uzyskanych wyników",
instances, charts.CType.AVG, SUMMARY_ALGS_ALL)

    avg_times = charts.generate('times_cmp',
"Uśredniony czas przetwarzania badanych algorytmów do momentu spełnienia indywidualnych warunków stopu dla próbki co najmniej 10 uruchomień oraz minimalnego trwania poszczególnej serii wynoszącego 1 sekundę",
instances, charts.CType.TIME, SUMMARY_ALGS_ALL)

    efiiciency = charts.generate('efficiency_cmp',
'{{Porównanie efektywności algorytmów dla wszystkich instancji}}',
instances, charts.CType.TIME_EFF, alg_types=SUMMARY_ALGS_ALL)

    avg_steps = charts.generate('steps_cmp',
"Porównanie średniej liczby kroków algorytmów \\textit{{Greedy}} i \\textit{{Steepest}} dla wszystkich instancji",
instances, charts.CType.AVG_STEPS, alg_types=SUMMARY_ALGS_SELECTED)

def single_charts(instances, instance):
    charts.generate("{}_progress_avg".format(instance),
"Wpływ liczby restartów w konfiguracji \\textit{{multi-random start}} na \\textbf{{średnie}} znalezione rozwiązanie dla algorytmów \\textit{{Greedy}} i \\textit{{Steepest}} oraz instancji \\textbf{{{}}}".format(instance),
instances, charts.CType.PROGRESS_AVG, alg_types=RANDOM_ALGS_SELECTED, instance=instance,
        xlabel='Liczba restartów', ylabel='Średnie rozwiązanie')

    charts.generate("{}_progress_best".format(instance),
"Wpływ liczby restartów w konfiguracji \\textit{{multi-random start}} na \\textbf{{najlepsze}} znalezione rozwiązanie dla algorytmów \\textit{{Greedy}} i \\textit{{Steepest}} oraz instancji \\textbf{{{}}}".format(instance),
instances, charts.CType.PROGRESS_BEST, alg_types=RANDOM_ALGS_SELECTED, instance=instance,
        xlabel='Liczba restartów', ylabel='Najlepsze rozwiązanie')

    charts.generate("{}_begend".format(instance),
"Jakość rozwiązania początkowego i końcowego dla instancji \\textbf{{{}}}".format(instance),
 instances, charts.CType.BEG_END, alg_types=RANDOM_ALGS_SELECTED, instance=instance,
        ylabel='Początkowe rozwiązanie', xlabel='Końcowe rozwiązanie')

    charts.generate("{}_similaritygi".format(instance),
"Wpływ jakości na podobieństwo znajdowanych rozwiązań dla instancji \\textbf{{{}}}".format(instance),
instances, charts.CType.SIMILARITY, alg_types=RANDOM_ALGS_SELECTED, instance=instance,
        xlabel='Jakość', ylabel='Podobieństwo')

def generate():
    instances = dict()

    # Load summary files to dict
    for f in sorted(glob('{}/*.sum'.format(args.path), recursive=True)):
            with open(f) as json_file:
                summary = json.load(json_file)
                if summary['name'] not in instances:
                    instances[summary['name']] = dict()
                instances[summary['name']][summary['type']] = summary

# SA, TS
    test_ts(instances)
    test_sa(instances)

    # Generate global instances charts
    summary_charts(instances)

    # Generate single instances charts
    for instance in instances.keys():
        print(instance)

        # single_charts(instances, instance)

parser = argparse.ArgumentParser()
parser.add_argument('--path', type=Path,
                    required=True,
                    help='Path for .sum files')

args = parser.parse_args()

if os.path.exists(OUTPUT_DIR):
    shutil.rmtree(OUTPUT_DIR)
os.makedirs(OUTPUT_DIR)

generate()

file_name = os.path.join("report.zip")

# Create zip file with latex sources
with ZipFile(file_name, 'w') as zipObj:
    for f in glob('{}/**'.format(OUTPUT_DIR), recursive=True):
        zipObj.write(f)


import os
import logging
from pathlib import Path
import collections
import re
from typing import List

import h5py
import tomopy
import dxchange
import dxchange.reader as dxreader
import dxfile.dxtomo as dx
import numpy as np
from scipy.interpolate import LSQUnivariateSpline
import yaml

from tomopy_cli import __version__
from tomopy_cli import find_center
from tomopy_cli import config
from tomopy_cli import beamhardening

__author__ = "Francesco De Carlo, Viktor Nikitin, Alan Kastengren, Mark Wolfman"
__credits__ = "Pavel Shevchenko"
__copyright__ = "Copyright (c) 2020, UChicago Argonne, LLC."
__docformat__ = 'restructuredtext en'
__all__ = ['read_tomo', 'blocked_view', 'binning', 'flip_and_stitch', 'patch_projection',
           'get_dx_dims', 'file_base_name', 'path_base_name', 'auto_read_dxchange', 'read_rot_center',
           'read_filter_materials', 'read_filter_materials_tomoscan', 'read_pixel_size',
           'read_scintillator', 'read_bright_ratio', 'check_item_exists_hdf', 'convert',
           'write_hdf5', 'yaml_file_list']

log = logging.getLogger(__name__)

def read_tomo(sino, proj, params, ignore_flip = False):
    """
    Read in the tomography data.

    Parameters
    ----------
    sino : tuple of (start_row, end_row) rows to be read in
    proj : tuple of (start_proj, end_proj) projections to be read in

    params : parameters for reconstruction

    Returns
    -------
    ndarray
        3D tomographic data.
    ndarray
        3D flat field data.
    ndarray
        3D dark field data.
    ndarray
        1D theta in radian.
    float
        location of the rotation axis
    """
    if (params.file_type == 'standard' or params.file_type == 'double_fov' or
            (params.file_type == 'flip_and_stich' and ignore_flip)):
        # Read APS 32-BM raw data.
        log.info("  *** loading a stardard data set: %s" % params.file_name)
        proj, flat, dark, theta = _read_tomo(params, sino=sino, proj=proj)
    elif params.file_type == 'flip_and_stich':
        log.info("   *** loading a 360 deg flipped data set: %s" % params.file_name)
        proj360, flat360, dark360, theta360 = _read_tomo(params, sino=sino, proj=proj)
        proj, flat, dark, theta = flip_and_stitch(params, proj360, flat360, dark360, theta360)
    else: # params.file_type == 'mosaic':
        log.error("   *** loading a mosaic data set is not supported yet")
        exit()

    if params.correct_camera_nonlinearity:
        log.info("  *** correcting camera nonlinearity in flat fields")
        flat = camera_nonlinearity_correct(flat, params)
        log.info("  *** correcting camera nonlinearity in dark fields")
        dark = camera_nonlinearity_correct(dark, params)
        log.info("  *** correcting camera nonlinearity in projection fields")
        proj = camera_nonlinearity_correct(proj, params)

    if params.reverse:
        log.info("  *** correcting for 180-0 data collection")
        step_size = (theta[1] - theta[0])
        theta_size = _read_theta_size(params)
        theta = np.linspace(np.pi, (0+step_size), theta_size)

    proj, theta = blocked_view(proj, theta, params)
    proj, flat, dark = binning(proj, flat, dark, params)
    rotation_axis = params.rotation_axis / np.power(2, float(params.binning))
    log.info("  *** rotation center: %f" % rotation_axis)
    return proj, flat, dark, theta, rotation_axis

def _read_theta_size(params):
    if (str(params.file_format) in {'dx', 'aps2bm', 'aps7bm', 'aps32id'}):
        theta_size = dxreader.read_dx_dims(params.file_name, 'data')[0]
    else:
        log.error("  *** %s is not a supported file format" % params.file_format)
        exit()
    return theta_size

def _read_tomo(params, sino, proj):

    if (str(params.file_format) in {'dx', 'aps2bm', 'aps7bm', 'aps32id'}):
        # temporary work around
        #flat_file = '/local/data/2020-10/PazPuente/flat_samp2_417.h5'
        #print('flat fields are taken from:', flat_file)
#        proj_bad, flat, dark, theta_bad = dxchange.read_aps_32id(flat_file, sino=sino)
#        proj, flat_bad, dark_bad, theta = dxchange.read_aps_32id(params.file_name, sino=sino)
        proj, flat, dark, theta = dxchange.read_aps_32id(params.file_name, sino=sino, proj=proj)
        log.info("  *** %s is a valid dx file format" % params.file_name)
        # Check if the flat and dark fields are single images or sets
        if len(flat.shape) == len(proj.shape):
            log.info('  *** median filter flat images')
            # Do a median filter on the first dimension
            flat = np.median(flat, axis=0, keepdims=True).astype(flat.dtype)
        if dark is None:
            dark = np.zeros_like(proj[0,...])
        if len(dark.shape) == len(proj.shape):
            log.info('  *** median filter dark images')
            # Do a median filter on the first dimension
            dark = np.median(dark, axis=0, keepdims=True).astype(dark.dtype)
    else:
        log.error("  *** %s is not a supported file format" % params.file_format)
        exit()
    return proj, flat, dark, theta

def blocked_view(proj, theta, params):
    log.info("  *** correcting for blocked view data collection")
    if params.blocked_views:
        log.warning('  *** *** ON')
        # miss_angles = [params.blocked_views_start, params.blocked_views_end]
        # # Manage the missing angles:
        # proj = np.concatenate((proj[0:miss_angles[0],:,:], proj[miss_angles[1]:,:,:]), axis=0)
        # theta = np.concatenate((theta[0:miss_angles[0]], theta[miss_angles[1]:]))

        # easier managing of missing angles: Viktor
        st = params.blocked_views_start
        end = params.blocked_views_end
        log.warning('%f %f',st,end)
        ids = np.where(((theta)%np.pi<st) + ((theta-st)%np.pi>end-st))[0]
        proj = proj[ids]
        theta = theta[ids]
        print(theta)

    else:
        log.warning('  *** *** OFF')

    return proj, theta

def binning(proj, flat, dark, params):
    """
    Bin the tomography data.

    Parameters
    ----------
    proj : projection data, 3D Numpy array
    flat : projection flatfield data, 2D Numpy array

    dark : projection dark field data, 2D Numpy array

    params : parameters for reconstruction

    Returns
    -------
    ndarray
        3D binned projection data.
    ndarray
        2D binned flat field data.
    ndarray
        2D binned dark field data.
    """
    log.info("  *** binning")
    if(params.binning == 0):
        log.info('  *** *** OFF')
    else:
        log.warning('  *** *** ON')
        log.warning('  *** *** binning: %d' % params.binning)
        proj = _binning(proj, params)
        flat = _binning(flat, params)
        dark = _binning(dark, params)
    return proj, flat, dark

def _binning(data, params):

    data = tomopy.downsample(data, level=int(params.binning), axis=2)
    data = tomopy.downsample(data, level=int(params.binning), axis=1)

    return data

def flip_and_stitch(params, img360, flat360, dark360, theta360):
    """
    Stitch together data for flip-and-stitch (0-360 degree offset center) scan.

    Parameters
    ----------
    params : dict of reconstruction parameters
    img360 : projection data from 0-360 degrees, 3D Numpy array
    flat360 : flatfield data, 2D Numpy array

    dark360 : dark field data, 2D Numpy array

    params : parameters for reconstruction

    Returns
    -------
    ndarray
        3D binned projection data stitched together, 0-180 degree domain
    ndarray
        2D binned flat field data stitched together, 0-180 degree domain
    ndarray
        2D binned dark field data stitched together, 0-180 degree domain
    ndarray
        1D array of valid theta values, 0-180 degree domain
    """
    theta_0_180, good_0_180, good_180_360 = reconcile_flip_and_stitch_angles(theta360)
    num_stitched_angles = np.sum(good_0_180)
    new_width = int(2 * np.max([img360.shape[2] - params.rotation_axis_flip - 0.5,
                            params.rotation_axis_flip + 0.5]))
    log.info('  *** *** new image width = {:d}, rotation_axis_flip = {:f}'.format(
                new_width, params.rotation_axis_flip))
    img = np.zeros([num_stitched_angles,img360.shape[1], new_width],dtype=np.float32)
    flat = np.zeros([flat360.shape[0],flat360.shape[1], new_width],dtype=np.float32)
    dark = np.zeros([dark360.shape[0],dark360.shape[1], new_width],dtype=np.float32)
    # Just add both images, keeping an array to record whether there was an overlap
    weight = np.zeros((1,1,new_width))
    # Array to blend the overlap region smoothly between 0-180 and 180-360 degrees
    wedge = np.arange(img360.shape[2], 0, -1)
    # Take care of case where rotation axis is on the left edge of the image
    if params.rotation_axis_flip < img360.shape[2] - 1:
        img[:,:,:img360.shape[2]] = img360[good_180_360,:,::-1] * wedge
        flat[:,:,:img360.shape[2]] = flat360[...,::-1] * wedge
        dark[:,:,:img360.shape[2]] = dark360[...,::-1] * wedge
        weight[0,0,:img360.shape[2]] += wedge
        img[:,:,-img360.shape[2]:] += img360[good_0_180,:,:] * wedge[::-1]
        flat[:,:,-img360.shape[2]:] += flat360 * wedge[::-1]
        dark[:,:,-img360.shape[2]:] += dark360 * wedge[::-1]
        weight[0,0,-img360.shape[2]:] += wedge[::-1]
    else:
        img[:,:,:img360.shape[2]] = img360[good_0_180,:,:] * wedge
        flat[:,:,:img360.shape[2]] = flat360 * wedge
        dark[:,:,:img360.shape[2]] = dark360 * wedge
        weight[0,0,:img360.shape[2]] += wedge
        img[:,:,-img360.shape[2]:] += img360[good_180_360,:,::-1] * wedge[::-1]
        flat[:,:,-img360.shape[2]:] += flat360[...,::-1] * wedge[::-1]
        dark[:,:,-img360.shape[2]:] += dark360[...,::-1] * wedge[::-1]
        weight[-img360.shape[2]:] += wedge[::-1]

    # Divide through by the weight to take care of doubled regions
    img = (img / weight).astype(img360.dtype)
    flat = (flat / weight).astype(img360.dtype)
    dark = (dark / weight).astype(img360.dtype)

    params.rotation_axis = img.shape[2]/2 - 0.5

    return img, flat, dark, theta_0_180

def reconcile_flip_and_stitch_angles(theta360):
    """
    Figure out the valid angles where we have images from both 0-180 and 180-360
    This is necessary in case their are missing angles.
    """
    theta360deg = np.degrees(theta360)
    good_180_360 = np.full((theta360deg.shape[0]), False, dtype=np.bool_)
    good_0_180 = np.full((theta360deg.shape[0]), False, dtype=np.bool_)
    #Loop across the entries from 0 to 180 degrees from the start point
    for i in theta360deg[(theta360deg - theta360deg[0]) <= 180]:
        #If there is a match between 0-180 and 180-360, indicate this is good
        comparison_array = np.abs(theta360deg - i - 180.)
        if np.min(comparison_array) < 0.001:
            good_0_180[np.argmin(np.abs(theta360deg - i))] = True
            good_180_360[np.argmin(comparison_array)] = True
    return theta360[good_0_180], good_0_180, good_180_360

def patch_projection(data, miss_angles):

    fdatanew = np.fft.fft(data,axis=2)

    w = int((miss_angles[1]-miss_angles[0]) * 0.3)

    fdatanew[miss_angles[0]:miss_angles[0]+w,:,:] = np.fft.fft(data[miss_angles[0]-1,:,:],axis=1)
    fdatanew[miss_angles[0]:miss_angles[0]+w,:,:] *= np.reshape(np.cos(np.pi/2*np.linspace(0,1,w)),[w,1,1])

    fdatanew[miss_angles[1]-w:miss_angles[1],:,:] = np.fft.fft(data[miss_angles[1]+1,:,:],axis=1)
    fdatanew[miss_angles[1]-w:miss_angles[1],:,:] *= np.reshape(np.sin(np.pi/2*np.linspace(0,1,w)),[w,1,1])

    fdatanew[miss_angles[0]+w:miss_angles[1]-w,:,:] = 0
    # lib.warning("  *** %d, %d, %d " % (datanew.shape[0], datanew.shape[1], datanew.shape[2]))

    lib.warning("  *** patch_projection")
    slider(np.log(np.abs(fdatanew.swapaxes(0,1))), axis=0)
    a = np.real(np.fft.ifft(fdatanew,axis=2))
    b = np.imag(np.fft.ifft(fdatanew,axis=2))

    slider(a.swapaxes(0,1), axis=0)
    slider(b.swapaxes(0,1), axis=0)
    return np.real(np.fft.ifft(fdatanew,axis=2))

def get_dx_dims(params):
    """
    Read array size of a specific group of Data Exchange file.

    Parameters
    ----------
    fname : str
        String defining the path of file or file name.
    dataset : str
        Path to the dataset inside hdf5 file where data is located.
    Returns
    -------
    ndarray
        Data set size.
    """

    dataset='data'

    grp = '/'.join(['exchange', dataset])
    with h5py.File(params.file_name, "r") as f:
        try:
            data = f[grp]
        except KeyError:
            return None

        shape = data.shape

    return shape

def file_base_name(fname):
    if '.' in fname:
        separator_index = fname.index('.')
        base_name = fname[:separator_index]
        return base_name
    else:
        return fname

def path_base_name(path):
    fname = os.path.basename(path)
    return file_base_name(fname)

def auto_read_dxchange(params):
    log.info('  *** Auto parameter reading from the HDF file.')
    params = read_pixel_size(params)
    params = read_filter_materials(params)
    params = read_scintillator(params)
    params = read_bright_ratio(params)
    params = read_rot_center(params)
    log.info('  *** *** Done')
    return params

def read_rot_center(params):
    """
    Read the rotation center from /process group in the HDF file.
    Return: rotation center from this dataset or None if it doesn't exist.
    """
    log.info('  *** *** rotation axis')
    # Handle case of manual only: this is the easiest
    if params.rotation_axis_auto == 'manual':
        log.info('  *** *** Force use of config file value = {:f}'.format(params.rotation_axis))
    elif params.rotation_axis_auto == 'auto':
        log.info('  *** *** Force auto calculation without reading config value')
        log.info('  *** *** Computing rotation axis')
        params = find_center.find_rotation_axis(params)
    else:
        # Try to read from HDF5 file
        log.info('  *** *** Try to read rotation center from file {}'.format(params.file_name))
        with h5py.File(params.file_name, 'r') as file_name:
            try:
                dataset = '/process/tomopy-cli-{}/find-rotation-axis/rotation-axis'.format(__version__)
                params.rotation_axis = float(file_name[dataset][0])
                dataset = '/process/tomopy-cli-{}/find-rotation-axis/rotation-axis-flip'.format(__version__)
                params.rotation_axis_flip = float(file_name[dataset][0])
                log.info('  *** *** Rotation center read from HDF5 file: {0:f}'.format(params.rotation_axis))
                log.info('  *** *** Rotation center flip read from HDF5 file: {0:f}'.format(params.rotation_axis_flip))
                return params
            except (KeyError, ValueError):
                log.warning('  *** *** No rotation center stored in the HDF5 file')
        # If we get here, we need to either find it automatically or from config file.
        log.warning('  *** *** No rotation axis stored in the HDF file')
        if (params.rotation_axis_auto == 'read_auto'):
            log.warning('  *** *** fall back to auto calculation')
            log.warning('  *** *** Computing rotation axis')
            params = find_center.find_rotation_axis(params)
        else:
            log.warning('  *** *** using config file value of {:f}'.format(params.rotation_axis))
    return params

def read_filter_materials(params):
    '''Read the beam filter configuration.
    This discriminates between files created with tomoScan and
    the previous meta data format.
    '''
    if check_item_exists_hdf(params.file_name, '/measurement/instrument/attenuator_1'):
        return read_filter_materials_tomoscan(params)
    else:
        return read_filter_materials_old(params)

def read_filter_materials_tomoscan(params):
    '''Read the beam filter configuration from the HDF file.

    If params.filter_{n}_auto for n in [1,2,3] is True,
    then try to read the filter configuration recorded during
    acquisition in the HDF5 file.

    Parameters
    ==========
    params

      The global parameter object, should have *filter_n_material*,
      *filter_n_thickness*, and *filter_n_auto* for n in [1,2,3]

    Returns
    =======
    params
      An equivalent object to the *params* input, optionally with
      *filter_n_material* and *filter_n_thickness*
      attributes modified to reflect the HDF5 file.

    '''
    log.info('  *** auto reading filter configuration')
    # Read the relevant data from disk
    filter_path = '/measurement/instrument/attenuator_{idx}'
    param_path = 'filter_{idx}_{attr}'
    for idx_filter in range(1,4,1):
        if not check_item_exists_hdf(params.file_name, filter_path.format(idx = idx_filter)):
            log.warning('  *** *** Filter {idx} not found in HDF file.  Set this filter to none'
                                    .format(idx = idx_filter))
            setattr(params, param_path.format(idx=idx_filter, attr='material'), 'Al')
            setattr(params, param_path.format(idx=idx_filter, attr='thickness'), 0.0)
            continue
        filter_auto = getattr(params, param_path.format(idx=idx_filter, attr='auto'))
        if filter_auto != 'True' and filter_auto != True:
            log.warning('  *** *** do not auto read filter {n}'.format(n=idx_filter))
            continue
        log.warning('  *** *** auto reading parameters for filter {0}'.format(idx_filter))
        # See if there are description and thickness fields
        if check_item_exists_hdf(params.file_name, filter_path.format(idx = idx_filter) + '/description'):
            filt_material = config.param_from_dxchange(params.file_name,
                                        filter_path.format(idx=idx_filter) + '/description',
                                        char_array = True, scalar = False)
            filt_thickness = int(config.param_from_dxchange(params.file_name,
                                        filter_path.format(idx=idx_filter) + '/thickness',
                                        char_array = False, scalar = True))
        else:
            #The filter info is just the raw string from the filter unit.
            log.warning('  *** *** filter {idx} info must be read from the raw string'
                            .format(idx = idx_filter))
            filter_str = config.param_from_dxchange(params.file_name,
                                        filter_path.format(idx=idx_filter) + '/setup/filter_unit_text',
                                        char_array = True, scalar = False)
            if filter_str is None:
                log.warning('  *** *** Could not load filter %d configuration from HDF5 file.' % idx_filter)
                filt_material, filt_thickness = _filter_str_to_params('Open')
            else:
                filt_material, filt_thickness = _filter_str_to_params(filter_str)

        # Update the params with the loaded values
        setattr(params, param_path.format(idx=idx_filter, attr='material'), filt_material)
        setattr(params, param_path.format(idx=idx_filter, attr='thickness'), filt_thickness)
        log.info('  *** *** Filter %d: (%s %f)' % (idx_filter, filt_material, filt_thickness))
    return params

def read_filter_materials_old(params):
    '''Read the beam filter configuration from the HDF file.

    If params.filter_1_material and/or params.filter_2_material are
    'auto', then try to read the filter configuration recorded during
    acquisition in the HDF5 file.

    Parameters
    ==========
    params

      The global parameter object, should have *filter_1_material*,
      *filter_1_thickness*, *filter_2_material*, and
      *filter_2_thickness* attributes.

    Returns
    =======
    params
      An equivalent object to the *params* input, optionally with
      *filter_1_material*, *filter_1_thickness*, *filter_2_material*,
      and *filter_2_thickness* attributes modified to reflect the HDF5
      file.

    '''
    log.info('  *** auto reading filter configuration')
    # Read the relevant data from disk
    filter_path = '/measurement/instrument/filters/Filter_{idx}_Material'
    param_path = 'filter_{idx}_{attr}'
    for idx_filter in (1, 2):
        filter_param = getattr(params, param_path.format(idx=idx_filter, attr='material'))
        if filter_param == 'auto':
            # Read recorded filter condition from the HDF5 file
            filter_str = config.param_from_dxchange(params.file_name,
                                                    filter_path.format(idx=idx_filter),
                                                    char_array=True, scalar=False)
            if filter_str is None:
                log.warning('  *** *** Could not load filter %d configuration from HDF5 file.' % idx_filter)
                material, thickness = _filter_str_to_params('Open')
            else:
                material, thickness = _filter_str_to_params(filter_str)
            # Update the params with the loaded values
            setattr(params, param_path.format(idx=idx_filter, attr='material'), material)
            setattr(params, param_path.format(idx=idx_filter, attr='thickness'), thickness)
            log.info('  *** *** Filter %d: (%s %f)' % (idx_filter, material, thickness))
    return params

def _filter_str_to_params(filter_str):
    # Any material with zero thickness is equivalent to being open
    open_filter = ('Al', 0.)
    if filter_str == 'Open':
        # No filter is installed
        material, thickness = open_filter
    else:
        # Parse the filter string to get the parameters
        filter_re = '(?P<material>[A-Za-z_]+)_(?P<thickness>[0-9.]+)(?P<unit>[a-z]*)'
        match = re.match(filter_re, filter_str)
        if match:
            material, thickness, unit = match.groups()
        else:
            log.warning('  *** *** Cannot interpret filter "%s"' % filter_str)
            material, thickness = open_filter
            unit = 'um'
        # Convert strings into numbers
        thickness = float(thickness)
        factors = {
            'nm': 1e-3,
            'um': 1,
            'mm': 1e3,
        }
        try:
            factor = factors[unit]
        except KeyError:
            log.warning('  *** *** Cannot interpret filter unit in "%s"' % filter_str)
            factor = 1
        thickness *= factor
    return material, thickness

def read_pixel_size(params):
    '''
    Read the pixel size and magnification from the HDF file.
    Use to compute the effective pixel size.
    '''
    log.info('  *** auto pixel size reading')
    if params.pixel_size_auto != True:
        log.info('  *** *** OFF')
        return params

    if check_item_exists_hdf(params.file_name,
                                '/measurement/instrument/detection_system/objective/resolution'):
        params.pixel_size = config.param_from_dxchange(params.file_name,
                                            '/measurement/instrument/detection_system/objective/resolution')
        log.info('  *** *** effective pixel size = {:6.4e} microns'.format(params.pixel_size))
        return(params)
    if check_item_exists_hdf(params.file_name,
                                '/measurement/instrument/detector/actual_pixel_size_x'):
        params.pixel_size = config.param_from_dxchange(params.file_name,
                                            '/measurement/instrument/detector/actual_pixel_size_x')
        log.info('  *** *** effective pixel size = {:6.4e} microns'.format(params.pixel_size))
        return(params)
    log.warning('  *** tomoScan resolution parameter not found.  Try old format')
    pixel_size = config.param_from_dxchange(params.file_name,
                                            '/measurement/instrument/detector/pixel_size_x')
    mag = config.param_from_dxchange(params.file_name,
                                    '/measurement/instrument/detection_system/objective/magnification')
    #Handle case where something wasn't read right
    if not (pixel_size and mag):
        log.warning('  *** *** problem reading pixel size from the HDF file')
        return params
    #What if pixel size isn't in microns, but in mm or m?
    for i in range(3):
        if pixel_size < 0.5:
            pixel_size *= 1e3
        else:
            break
    params.pixel_size = pixel_size / mag
    log.info('  *** *** effective pixel size = {:6.4e} microns'.format(params.pixel_size))
    return params

def read_scintillator(params):
    '''Read the scintillator type and thickness from the HDF file.
    '''
    if params.scintillator_auto:
        log.info('  *** auto reading scintillator params')
        possible_names = ['/measurement/instrument/detection_system/scintillator/scintillating_thickness',
                        '/measurement/instrument/detection_system/scintillator/active_thickness']
        for pn in possible_names:
            if check_item_exists_hdf(params.file_name, pn):
                val = config.param_from_dxchange(params.file_name,
                                         pn, attr=None,
                                         scalar=True,
                                         char_array=False)
                params.scintillator_thickness = float(val)
                break
        log.info('  *** *** scintillator thickness = {:f}'.format(params.scintillator_thickness))
        possible_names = ['/measurement/instrument/detection_system/scintillator/name',
                        '/measurement/instrument/detection_system/scintillator/type',
                        '/measurement/instrument/detection_system/scintillator/description']
        scint_material_string = ''
        for pn in possible_names:
            if check_item_exists_hdf(params.file_name, pn):
                scint_material_string = config.param_from_dxchange(params.file_name,
                                            pn, scalar = False, char_array = True)
                break
        else:
            log.warning('  *** *** no scintillator material found')
            return(params)
        if scint_material_string.lower().startswith('luag'):
            params.scintillator_material = 'LuAG_Ce'
        elif scint_material_string.lower().startswith('lyso'):
            params.scintillator_material = 'LYSO_Ce'
        elif scint_material_string.lower().startswith('yag'):
            params.scintillator_material = 'YAG_Ce'
        else:
            log.warning('  *** *** scintillator {:s} not recognized!'.format(scint_material_string))
        log.info('  *** *** using scintillator {:s}'.format(params.scintillator_material))
    return params

def read_bright_ratio(params):
    '''Read the ratio between the bright exposure and other exposures.
    '''
    log.info('  *** *** %s' % params.flat_correction_method)
    if params.flat_correction_method != 'standard' or (not params.scintillator_auto):
        log.warning('  *** *** skip finding exposure ratio')
        params.bright_exp_ratio = 1
        return params
    log.info('  *** *** Find bright exposure ratio params from the HDF file')
    try:
        possible_names = ['/measurement/instrument/detector/different_flat_exposure',
                        '/process/acquisition/flat_fields/different_flat_exposure']
        for pn in possible_names:
            if check_item_exists_hdf(params.file_name, pn):
                diff_bright_exp = config.param_from_dxchange(params.file_name, pn,
                                    attr = None, scalar = False, char_array = True)
                break
        if diff_bright_exp.lower() == 'same':
            log.error('  *** *** used same flat and data exposures')
            params.bright_exp_ratio = 1
            return params
        possible_names = ['/measurement/instrument/detector/exposure_time_flat',
                        '/process/acquisition/flat_fields/flat_exposure_time',
                        '/measurement/instrument/detector/brightfield_exposure_time']
        for pn in possible_names:
            if check_item_exists_hdf(params.file_name, pn):
                bright_exp = config.param_from_dxchange(params.file_name, pn,
                                    attr = None, scalar = True, char_array = False)
                break
        log.info('  *** *** %f' % bright_exp)
        norm_exp = config.param_from_dxchange(params.file_name,
                                    '/measurement/instrument/detector/exposure_time',
                                    attr = None, scalar = True, char_array = False)
        log.info('  *** *** %f' % norm_exp)
        params.bright_exp_ratio = bright_exp / norm_exp
        log.info('  *** *** found bright exposure ratio of {0:6.4f}'.format(params.bright_exp_ratio))
    except:
        log.warning('  *** *** problem getting bright exposure ratio.  Use 1.')
        params.bright_exp_ratio = 1
    return params

def check_item_exists_hdf(hdf_filename, item_name):
    '''Checks if an item exists in an HDF file.
    Inputs
    hdf_filename: str filename or pathlib.Path object for HDF file to check
    item_name: name of item whose existence needs to be checked
    path: str path to check.  Default to None
    '''
    with h5py.File(hdf_filename, 'r') as hdf_file:
        return item_name in hdf_file

def convert(params):

    head_tail = os.path.split(params.old_projection_file_name)

    new_hdf_file_name = head_tail[0] + os.sep + os.path.splitext(head_tail[1])[0] + '.h5'

    print('converting data file: %s in new format: %s' % (params.old_projection_file_name, new_hdf_file_name))
    print('using %s as dark and %s as white field' %(params.old_dark_file_name, params.old_white_file_name))
    exchange_base = "exchange"

    tomo_grp = '/'.join([exchange_base, 'data'])
    flat_grp = '/'.join([exchange_base, 'data_white'])
    dark_grp = '/'.join([exchange_base, 'data_dark'])
    theta_grp = '/'.join([exchange_base, 'theta'])
    tomo = dxreader.read_hdf5(params.old_projection_file_name, tomo_grp)
    flat = dxreader.read_hdf5(params.old_white_file_name, flat_grp)
    dark = dxreader.read_hdf5(params.old_dark_file_name, dark_grp)
    theta = dxreader.read_hdf5(params.old_projection_file_name, theta_grp)

    # Open DataExchange file
    f = dx.File(new_hdf_file_name, mode='w')

    f.add_entry(dx.Entry.data(data={'value': tomo, 'units':'counts'}))
    f.add_entry(dx.Entry.data(data_white={'value': flat, 'units':'counts'}))
    f.add_entry(dx.Entry.data(data_dark={'value': dark, 'units':'counts'}))
    f.add_entry(dx.Entry.data(theta={'value': theta, 'units':'degrees'}))

    f.close()

def write_hdf5(data, fname, dname='volume', dtype=None,
               dest_idx=None, maxsize=None, overwrite=False):
    """Write data to hdf5 file in a specific dataset.

    This function supports partial writing of data through af
    combination of *maxsize* and *dest_idx* options. For example, to
    write slices 10 to 16 of a (32, 32, 32) volume::

        assert data.shape == (32, 32, 32)
        file_io.write_hdf5(data[10:16], maxsize=data.shape, dest_idx=slice(10,16), ...)

    Parameters
    ----------
    data : ndarray
        Array data to be saved.
    fname : str
        File name to which the data is saved. ``.h5`` extension
        will be appended if it does not already have one.
    dname : str, optional
        Name for dataset where data will be written.
    dtype : data-type, optional
        By default, the data-type is inferred from the input data.
    dest_idx : optional
        A valid index for the dataset such that ``dataset[target_idx]
        = data`` will properly write the data to the dataset.
    maxsize : int, optional
        Maximum size that the dataset can be resized to along the
        given axis.

    """
    # Extract default values if not given
    if maxsize is None:
        maxsize = data.shape
    if dtype is None:
        dtype = data.dtype
    if dest_idx is None:
        dest_idx = ()
    # Create parent directory if necessary
    Path(fname).parent.mkdir(parents=True, exist_ok=True)
    # Open the HDF5 file so we can save data to it
    with h5py.File(fname, mode='a') as h5fp:
        # Delete the dataset if it already exists and is being overwritten
        if dname in h5fp.keys() and overwrite:
            del h5fp[dname]
        # Create a new dataset if necessary
        try:
            ds = h5fp.require_dataset(dname, shape=maxsize, dtype=dtype, fillvalue=np.nan, exact=True)
        except TypeError as e:
            msg = str(e) + ". Use *overwrite=True* to overwrite existing dataset."
            raise type(e)(msg)
        # Save the data
        ds[dest_idx] = data

def yaml_file_list(file_path: Path)->List[Path]:
    """Open a YAML file and return the list of files within.
    This function does not parse the parameters contained inside,
    merely returns a list of the files that are referenced. For
    updating parameters on a per-file basis, use
    ``config.yaml_args()``.
    Parameters
    ==========
    file_path
      A pathlib Path object pointing to the file to open.
    Returns
    =======
    file_list
      The list of file names found. There is no guarantee that these
      files are suitable for reconsturction, or even exist at all.
    """
    with open(file_path, mode='r') as fp:
        yaml_data = yaml.safe_load(fp.read())
    file_list = [Path(k) for k in yaml_data.keys()]
    return file_list

def camera_nonlinearity_correct(data, params):
    '''Corrects for nonlinearity in the camera
    Takes data from params to form a spline fit, then uses the spline
    fit to correct for the camera nonlinearity.
    Inputs:
    data: numpy array of camera data
    params: parameters from config file and command line
    '''
    log.info('*** correcting for camera nonlinearity')
    #Parse the params.camera_signal and params.corrected_signal
    camera_signal = np.array([float(i) for i in params.camera_signal.split(",")])
    corrected_signal = np.array([float(i) for i in params.corrected_signal.split(",")])
    knots = np.linspace(camera_signal[1], camera_signal[-2], len(camera_signal)//2)
    spline_obj = LSQUnivariateSpline(
                                    camera_signal,
                                    corrected_signal,
                                    knots,
                                    check_finite = True)
    temp = spline_obj(data)
    temp[temp > np.iinfo(data.dtype).max] = np.iinfo(data.dtype).max
    temp[temp < 0] = 0
    return temp.astype(data.dtype)


#!/usr/bin/python3

#---------------------------------#
#- Author: Stamoulias Ioannis    -#
#- email: jstamoulias@gmail.com  -#
#---------------------------------#

import os
# from sympy import true
import sys
import numpy
import ctypes
import linecache

accel = ctypes.CDLL('./accel_libs/accellib.so')

def parameters():
    MTIMESTEPS = 64
    FEATURES = 1
    IUNITS = 128
    OUNITS = 128
    iFRAQ = 10
    oFRAQ = 12
    iFRAQ_CONV = 1024
    oFRAQ_CONV = 4096
    return [MTIMESTEPS,FEATURES,IUNITS,OUNITS,iFRAQ,oFRAQ,iFRAQ_CONV,oFRAQ_CONV]

def init_device(xclbin_name):
    krnl_nm = xclbin_name #"krnl_lstm.xclbin"
    xclbin = ctypes.c_char_p(krnl_nm.encode('utf-8'))
    accel.device_init(xclbin)

def init_buffers(timesteps):
    PARAM=parameters()
    accel.lstm_io(int(timesteps), PARAM[1], PARAM[2], PARAM[3])

def read_file(fl_name, size):
    V = numpy.empty(size, float)
    with open(fl_name, "r") as fl:
        V = [float(next(fl)) for x in range(size)]
    return V

def read_file2(fl, size):
    V = numpy.empty(size, float)
    V = [float(next(fl)) for x in range(size)]
    return V

def write_file(fl_name,ar_var,ar_len):
    with open(fl_name, "w") as fl:
        for i in range(ar_len):
            fl.write(str(ar_var[i]))
            fl.write("\n")

def rmv_empty_ln(fl_name):
    with open(fl_name, "r") as flr, open(fl_name, "r+") as flw:
        for line in flr:
            if line.strip():
                flw.write(line)
        flw.truncate()

def fl2short_weights(W1, W2, rw, ln, fraq):
    accel.fixp.restype = ctypes.c_short
    #x = 0
    #y = 0
    buf_W = numpy.empty((2*rw*ln),ctypes.c_short)
    for i in range(2*rw*ln):
        #if y == (ln):
        #    if x == (rw-1):
        #        x = 0
        #    else:
        #        x = x + 1
        #    y = 0
        if (i<rw*ln):
            buf_W[i] = accel.fixp(ctypes.c_float(W1[i]),fraq) #accel.fixp(ctypes.c_float(W1[x][y]),fraq)
        else:
            buf_W[i] = accel.fixp(ctypes.c_float(W2[i-rw*ln]),fraq) #accel.fixp(ctypes.c_float(W2[x][y]),fraq)
       # y = y + 1
    return buf_W

def fl2short_biases(b1, b2, rw, fraq):
    accel.fixp.restype = ctypes.c_short
    x = 0
    buf_b = numpy.empty((2*rw),ctypes.c_short)
    for i in range(2*rw):
        if x == rw:
            x = 0
        if (i < rw):
            buf_b[i] = accel.fixp(ctypes.c_float(b1[x]),fraq)
        else:
            buf_b[i] = accel.fixp(ctypes.c_float(b2[x]),fraq)
        x = x + 1
    return buf_b

def fl2short_dense(Wd, bd, rw, fraq):
    accel.fixp.restype = ctypes.c_short
    buf_W = numpy.empty((rw),ctypes.c_short)
    buf_b = numpy.empty((1),ctypes.c_short)
    for i in range(rw):
        buf_W[i] = accel.fixp(ctypes.c_float(Wd[i]),fraq) #accel.fixp(ctypes.c_float(Wd[0][i]),fraq)
    buf_b[0] = accel.fixp(ctypes.c_float(bd[0]),fraq)
    return [buf_W, buf_b]

def weights_biases_accel(W1i,W2i,b1i,b2i,W1f,W2f,b1f,b2f,W1c,W2c,b1c,b2c,W1o,W2o,b1o,b2o,Wd,bd,rw,ln,fraq):
    shp = ctypes.POINTER(ctypes.c_short)
    buf_Wi = fl2short_weights(W1i, W2i, rw, ln, fraq)
    Wi_ptr = buf_Wi.ctypes.data_as(shp)
    buf_Wf = fl2short_weights(W1f, W2f, rw, ln, fraq)
    Wf_ptr = buf_Wf.ctypes.data_as(shp)
    buf_Wc = fl2short_weights(W1c, W2c, rw, ln, fraq)
    Wc_ptr = buf_Wc.ctypes.data_as(shp)
    buf_Wo = fl2short_weights(W1o, W2o, rw, ln, fraq)
    Wo_ptr = buf_Wo.ctypes.data_as(shp)
    buf_bi = fl2short_biases(b1i, b2i, rw, fraq)
    bi_ptr = buf_bi.ctypes.data_as(shp)
    buf_bf = fl2short_biases(b1f, b2f, rw, fraq)
    bf_ptr = buf_bf.ctypes.data_as(shp)
    buf_bc = fl2short_biases(b1c, b2c, rw, fraq)
    bc_ptr = buf_bc.ctypes.data_as(shp)
    buf_bo = fl2short_biases(b1o, b2o, rw, fraq)
    bo_ptr = buf_bo.ctypes.data_as(shp)
    [buf_W, buf_b] = fl2short_dense(Wd, bd, rw, fraq)
    W_ptr = buf_W.ctypes.data_as(shp)
    b_ptr = buf_b.ctypes.data_as(shp)
    return [Wi_ptr,bi_ptr,Wf_ptr,bf_ptr,Wc_ptr,bc_ptr,Wo_ptr,bo_ptr,W_ptr,b_ptr]

def weights_biases():
    PARAM=parameters()
    Wi1_f=read_file("./weights_biases/Wi1",PARAM[2]*(PARAM[2]+PARAM[3]))
    Wf1_f=read_file("./weights_biases/Wf1",PARAM[2]*(PARAM[2]+PARAM[3]))
    Wc1_f=read_file("./weights_biases/Wc1",PARAM[2]*(PARAM[2]+PARAM[3]))
    Wo1_f=read_file("./weights_biases/Wo1",PARAM[2]*(PARAM[2]+PARAM[3]))
    Wi2_f=read_file("./weights_biases/Wi2",PARAM[2]*(PARAM[2]+PARAM[3]))
    Wf2_f=read_file("./weights_biases/Wf2",PARAM[2]*(PARAM[2]+PARAM[3]))
    Wc2_f=read_file("./weights_biases/Wc2",PARAM[2]*(PARAM[2]+PARAM[3]))
    Wo2_f=read_file("./weights_biases/Wo2",PARAM[2]*(PARAM[2]+PARAM[3]))
    bi1_f=read_file("./weights_biases/bi1",PARAM[2])
    bf1_f=read_file("./weights_biases/bf1",PARAM[2])
    bc1_f=read_file("./weights_biases/bc1",PARAM[2])
    bo1_f=read_file("./weights_biases/bo1",PARAM[2])
    bi2_f=read_file("./weights_biases/bi2",PARAM[2])
    bf2_f=read_file("./weights_biases/bf2",PARAM[2])
    bc2_f=read_file("./weights_biases/bc2",PARAM[2])
    bo2_f=read_file("./weights_biases/bo2",PARAM[2])
    W_f=read_file("./weights_biases/W",PARAM[2])
    b_f=read_file("./weights_biases/b",1)
    ptrs = weights_biases_accel(Wi1_f,Wi2_f,bi1_f,bi2_f,Wf1_f,Wf2_f,bf1_f,bf2_f,Wc1_f,Wc2_f,bc1_f,bc2_f,Wo1_f,Wo2_f,bo1_f,bo2_f,W_f,b_f,PARAM[2],(PARAM[2]+PARAM[3]),PARAM[4])
    return ptrs

def fl2short_input(data, max_steps, timesteps, features, fraq):
    accel.fixp.restype = ctypes.c_short
    buf_data = numpy.empty((max_steps*features),ctypes.c_short)
    for i in range(max_steps*features):
        if (i < timesteps*features):
            buf_data[i] = accel.fixp(ctypes.c_float(data[i]),fraq)
        else:
            buf_data[i] = 0
    return buf_data

def out_ptr():
    PARAM = parameters()
    shp = ctypes.POINTER(ctypes.c_short)
    buf_res = numpy.zeros((PARAM[0]*PARAM[1]),ctypes.c_short)
    res_ptr = buf_res.ctypes.data_as(shp)
    return res_ptr

def in_ptr(data_in, timesteps):
    PARAM = parameters()
    shp = ctypes.POINTER(ctypes.c_short)
    buf_data = fl2short_input(data_in, PARAM[0], int(timesteps), PARAM[1], PARAM[4])
    data_ptr = buf_data.ctypes.data_as(shp)
    return data_ptr

def lstm(data_ptr, res_ptr, wb_ptrs, timesteps, flag):
    PARAM = parameters()
    accel.lstm_accel(data_ptr,res_ptr,wb_ptrs[0],wb_ptrs[1],wb_ptrs[2],wb_ptrs[3],wb_ptrs[4],wb_ptrs[5],wb_ptrs[6],wb_ptrs[7],wb_ptrs[8],wb_ptrs[9],int(timesteps),flag,PARAM[1],PARAM[2],PARAM[3])

def results(res_ptr, timesteps):
    PARAM = parameters()
    R = numpy.empty(int(timesteps), float)
    for i in range(int(timesteps)):
            R[i] = (res_ptr[i]/PARAM[7])
    return R

def print_res(res_ptr, res, timesteps):
    PARAM = parameters()
    #res = results(res_ptr, timesteps)
    if(res[(int(timesteps)-1)]>0.5):
        # ret ="Attack detected : " + str(res[(int(timesteps)-1)])
        ret=True
        print("Anomaly=yes : (%.2f)" % (res[(int(timesteps)-1)]))
        # print("Attack detected : ",res[(int(timesteps)-1)])
    else:
        # ret ="Normal process : " + str(res[(int(timesteps)-1)])
        ret=False
        print("Anomaly=no : (%.2f)" % (res[(int(timesteps)-1)]))
    # print(ret)
    return ret

def accel_end():
    accel.lstm_end()


from collections import deque
arr = [4, 2, 7, 1, 3, 6, 9]

class Node:
  def __init__(self, val):
    self.val = val
    self.left = None
    self.right = None

def make_tree(arr):
  nodes = [False] * (len(arr) + 1)
  for i, val in enumerate(arr):
    if not val:
      continue
    nodes[i+1] = Node(val)

  for i in range(1, len(nodes)):
    if 2*i < len(nodes) and nodes[2*i]:
      nodes[i].left = nodes[2*i]
    if 2*i+1 < len(nodes) and nodes[2*i+1]:
      nodes[i].right = nodes[2*i+1]

  return nodes[1]

root = make_tree(arr)

def invert_tree(root):
  if root:
    root.left, root.right = invert_tree(root.right), invert_tree(root.left)
    return root

  return None

def invert_tree_q(root):
  q = deque([root])
  while q:
    node = q.popleft()

    if node:
      node.left, node.right = node.right, node.left

      q.append(node.left)
      q.append(node.right)

  return root

inverted = invert_tree_q(root)

def print_tree_pre(root):
  if not root:
    return

  print(root.val)
  if root.left:
    print_tree_pre(root.left)
  if root.right:
    print_tree_pre(root.right)

print_tree_pre(inverted)


from discord import Embed
import itertools
from random import randint

from .modules.common import *
from .modules.player import get_players, get_participants, sum_points

# /valo team ignore:
def valo_team(ctx, ignore_text: str, random: bool):

    # Error: Not in a voice channel.
    try:
        channel = ctx.user.voice.channel
        players = get_players(channel)
    except AttributeError:
        embed = Embed(title=LIST_TTL_ERR, color=ERROR)
        return [embed]

    # Warning: If one member of the voice channel is able to get.
    if players:
        participants = get_participants(players, ignore_text)
    else:
        embed = Embed(title=LIST_TTL_WAR, color=WARNING)
        embed.set_footer(text=LIST_FOOT_WAR)
        return [embed]

    # Process: When the appropriate number of participants.
    if participants:
        r = int(len(participants)//2)
        teams = list(itertools.combinations(participants, r))
        teams_point = [sum_points(team) for team in teams]
    else:
        num, value = len(players), ''
        title = f'参加者の数が不正です　({num}/10人)'
        for i in range(num):
            player = players[i]
            value += f'{i} - {player.emoji}<@{player.id}>\n'
        embed = Embed(title=title, color=ERROR)
        embed.add_field(name='IgnoreID', value=value, inline=True)
        embed.set_footer(text=LIST_FOOT_SUC)
        return [embed]

    # ProcessBranch: Completely random / War power reference
    if random:
        i = randint(0, len(teams)-1)
        j = -1*(i+1)
        atk_team, def_team = teams[i], teams[j]
        oth_desc=''
    else:
        now_diff = 20210801
        for i in range(len(teams)):
            j = -1*(i+1)
            diff = abs(teams_point[i]-teams_point[j])
            if diff < now_diff:
                now_diff = diff
                picklist = [(teams[i], teams[j])]
            elif diff == now_diff:
                picklist.append((teams[i], teams[j]))
            else:
                continue
        half = int(len(picklist)//2)
        pick = randint(1, half)
        if randint(0, 1):
            atk_team, def_team = picklist[pick-1]
        else:
            atk_team, def_team = picklist[pick*-1]
        oth_desc=f'チーム戦力の差：{now_diff}\n組み合わせ候補：{pick}/{half}'

    # Process: Show Team member
    atk_desc, def_desc = '', ''
    for i in range(r):
        ap, dp = atk_team[i], def_team[i]
        if ap.id != -1:
            atk_desc += f'{ap.emoji}<@{ap.id}>\n'
        if dp.id != -1:
            def_desc += f'{dp.emoji}<@{dp.id}>\n'

    # Success:
    atk_embed = Embed(title='Attacker Side', description=atk_desc, color=ATTACKER)
    def_embed = Embed(title='Defender Side', description=def_desc, color=DEFENDER)
    other_embed = Embed(title='Information', description=oth_desc, color=OTHER)
    if random:
        return [atk_embed, def_embed]
    else:
        return [atk_embed, def_embed, other_embed]


import pandas as pd
from collections import defaultdict

streaming_path = 'data/streamingData_1M.csv'
processed_path = 'data/processed.txt'

def normalize(x, u, l, nu, nl):
    x = max(l, x)
    x = min(u, x)
    x = (x - l) / (u - l)
    x = nl + (nu - nl) * x
    return x

def preprocess4surprise():
    """Preprocess streaming data for surprise reader"""
    df = pd.read_csv(streaming_path, sep=',')
    L = df.shape[0]
    df = df.iloc[:int(.8 * L)]
    pair2min = defaultdict(int)
    pair2rate = dict()

    for uid, mid, mi, rating in zip(df['user_id'].values, df['movie_id'].values, df['minute'].values, df['rating'].values):
        if not pd.isnull(mi):
            pair2min[(uid, mid)] += 1
        if not pd.isnull(rating):
            pair2rate[(uid, mid)] = rating
    for pair, mi in pair2min.items():
        if pair not in pair2rate:
            pair2rate[pair] = normalize(mi, 0, 110, 5, 1)
    out = []
    for (uid, mid), rate in pair2rate.items():
        out.append(';'.join((str(uid), mid, str(rate))))
    with open(processed_path, 'w') as f:
        f.write('\n'.join(out))

if __name__ == '__main__':
    preprocess4surprise()