import copy

import lan
import lan.ast_buildingblock as ast_bb
from codegen import snippetgen
from processing import collect_id as ci
from transformation import place_in_local as piloc
from transformation import place_in_reg as pireg

def print_dict_sorted(mydict):
    keys = sorted(mydict)

    entries = ""
    for key in keys:
        value = mydict[key]
        entries += "'" + key + "': " + value.__repr__() + ","

    return "{" + entries[:-1] + "}"

class KernelGen(object):
    def __init__(self, ast, fileprefix):
        self.ast = ast
        self.fileprefix = fileprefix

    def generate_kernels(self):
        # Create base version and possible version with Local and
        # Register optimizations
        name = ci.get_program_name(self.ast)
        funcname = name + 'Base'
        ast = copy.deepcopy(self.ast)
        self._create_base_kernel(funcname, ast)

        pir = pireg.PlaceInReg(ast)
        pir.place_in_reg3()
        if pir.perform_transformation:
            funcname = name + 'PlaceInReg'
            self._create_optimized_kernel(funcname, ast, pir.PlaceInRegCond)

        pil = piloc.PlaceInLocal(ast)
        pil.place_in_local()
        if pir.PlaceInRegFinding and pil.PlaceInLocalArgs:
            raise Exception("""GenerateKernels: Currently unimplemented to perform
                                PlaceInReg and PlaceInLocal together from the analysis""")
        for arg in pil.PlaceInLocalArgs:
            funcname = name + 'PlaceInLocal'
            pil.local_memory3(arg)
            self._create_optimized_kernel(funcname, ast, pil.PlaceInLocalCond)

    def __get_file_name(self, funcname):
        name = ci.get_program_name(self.ast)
        return self.fileprefix + name + '/' + funcname + '.cl'

    def _create_base_kernel(self, funcname, testast):
        ss = snippetgen.SnippetGen(testast)
        ss.in_source_kernel(copy.deepcopy(testast), filename=self.__get_file_name(funcname),
                            kernelstringname=funcname)

    def _create_optimized_kernel(self, funcname, testast, cond):
        ss = snippetgen.SnippetGen(testast)
        ss.in_source_kernel(copy.deepcopy(testast), filename=self.__get_file_name(funcname),
                            kernelstringname=funcname)

class CreateKernels(KernelGen):
    def __init__(self, ast, file_ast):
        super(CreateKernels, self).__init__(ast, "dontcate")
        self.file_ast = file_ast

        # Output
        self.IfThenElse = None

    def create_get_kernel_code(self):

        self.generate_kernels()

        get_kernel_code = ast_bb.EmptyFuncDecl('GetKernelCode', type=['std::string'])
        get_kernel_stats = [self.IfThenElse]
        get_kernel_code.compound.statements = get_kernel_stats
        self.file_ast.append(get_kernel_code)

    def _create_base_kernel(self, funcname, testast):
        sg = snippetgen.SnippetGen(self.ast)
        newast = sg.generate_kernel_ss(copy.deepcopy(testast), funcname)
        self.file_ast.append(newast)
        self.IfThenElse = self.__create_base_kernel_func()

    def __create_base_kernel_func(self):
        program_name = ci.get_program_name(self.ast)
        name = program_name + 'Base'
        func = ast_bb.EmptyFuncDecl(name, type=[])
        return lan.Return(func)

    def __create_optimized_kernel_func(self, funcname, cond):
        returnfunc1 = self.__create_base_kernel_func()
        name = funcname
        func = ast_bb.EmptyFuncDecl(name, type=[])
        returnfunc2 = lan.Return(func)
        ifthenelse = lan.IfThenElse(cond,
                                    lan.Compound([returnfunc2]), lan.Compound([returnfunc1]))
        return ifthenelse

    def _create_optimized_kernel(self, funcname, testast, cond):

        sg = snippetgen.SnippetGen(testast)
        newast = sg.generate_kernel_ss(copy.deepcopy(testast), funcname)
        self.file_ast.append(newast)
        self.IfThenElse = self.__create_optimized_kernel_func(funcname, cond)


# !/bin/python3
# http://www.adventofcode.com/2017/day/7

from utils import read_input

def parse_input(tower_info):
    for info in tower_info:
        info = info.split()
        subtowers = []

        # get basic info
        name = info.pop(0)
        weight = int(info.pop(0)[1:-1])

        # if we have more info, get rid of -> and process rest
        if info:
            info.pop(0)
            for subtower in info:
                subtower = subtower.replace(',', '')
                subtowers.append(subtower)

        yield name, weight, subtowers

def process_input(tower_info):
    return {n: {'weight': w, 'subtowers': s} for n, w, s in tower_info}

def find_tower_base(tower_info):
    subtowers = []
    for tower in tower_info.values():
        for subtower in tower['subtowers']:
            subtowers.append(subtower)

    for tower in tower_info:
        if tower not in subtowers:
            return tower

def traverse_tower(tower_info, name):
    for subtower in tower_info[name]['subtowers']:
        if not tower_info[subtower]['subtowers']:
            continue
        yield from traverse_tower(tower_info, subtower)
    for subtower in tower_info[name]['subtowers']:
        if tower_info[subtower]['subtowers']:
            continue
        yield subtower
    yield name

def check_weights(tower_info, subtower):
    if len(tower_info[subtower]['subtowers']) < 3:
        return

    weights = {}
    weight_list = []
    weight_set = set()

    for tower in tower_info[subtower]['subtowers']:
        weight = 0
        for t in traverse_tower(tower_info, tower):
            weight += tower_info[t]['weight']

        weights[tower] = weight
        weight_list.append(weight)
        weight_set.add(weight)

    if len(weight_set) == 1:
        return

    good_weight = None
    bad_weight = None
    bad_tower = None

    for tower, weight in weights.items():
        if weight_list.count(weight) > 1:
            good_weight = weight
        else:
            bad_weight = weight
            bad_tower = tower
        if good_weight and bad_weight:
            break

    return tower_info[bad_tower]['weight'] - bad_weight + good_weight

def find_fixed_weight(tower_info, tower_base):
    for subtower in traverse_tower(tower_info, tower_base):
        fixed_weight = check_weights(tower_info, subtower)
        if fixed_weight:
            return fixed_weight

TOWER_INFO = process_input([T for T in parse_input(read_input())])
TOWER_BASE = find_tower_base(TOWER_INFO)
print('part a:', TOWER_BASE)
print('part b:', find_fixed_weight(TOWER_INFO, TOWER_BASE))


import glob
import os
import sys
import tempfile

import backend
import frontend

# simulates 3 separate sessions for 1 day
def main(number_of_sessions=3):
    for i in range(number_of_sessions):  # run the front end instance 3 times with appropriate inputs
        # prepare program parameters
        sys.argv = [
            'frontend.py',
            'valid_accounts.txt',
            'new_transactions/transaction_' + str(i) + '.txt']
        frontend.main()
    temp_fd, temp_file = tempfile.mkstemp()
    merged_transaction_file = temp_file
    transaction_files = glob.glob("new_transactions/*.txt")  # list of transaction summary files
    with open(merged_transaction_file, 'w') as wf:  # create merged txn summary file
        for file in transaction_files:
            with open(file, 'r') as tf:
                for line in tf:
                    if line != 'EOS 0000000 000 0000000 ***':
                        wf.write(line)
        wf.write('EOS 0000000 000 0000000 ***')
    for transaction in transaction_files:
        os.remove(transaction)
    # run backend with updated files
    sys.argv = [
        'backend.py',
        'master_accounts.txt',
        merged_transaction_file]
    backend.main()
    os.close(temp_fd)
    os.remove(temp_file)

if __name__ == "__main__":
    main()


import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from pylab import rcParams
rcParams['figure.figsize'] = 10, 10

x = list()
y = list()
z = list()
v = list()

file = open('5000.txt', 'r')
lines = file.readlines()
# 0 1 2: pos, 5 6 7: Acoustrophoretic force, 8 9 10: Total force, 11: Radiation potential
for line in lines:
    tmp = list(map(lambda f: float(f), line.split()))
    if -1e-9 < tmp[2] and tmp[2] < 1e-9:
        x.append(tmp[0])
        y.append(tmp[1])
        z.append(tmp[2])
        v.append(tmp[11])
file.close()

#plt.axes([-0.05, -0.1, 0.25, 0.5])
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_title('force_z')
ax.view_init(elev=18, azim=30)
ax.dist=10
ax.set_xlim3d(-0.05, 0.2)
ax.set_ylim3d(-0.05, 0.2)
ax.set_zlim3d(0, 0.4)

cmap = plt.cm.get_cmap("hot")
l = ax.scatter(x, y, z, cmap=cmap, c=v, s=20)

fig.colorbar(l)
plt.show()


import tensorflow as tf
import numpy as np
from numpy import array
from skimage.io import imread
from skimage.transform import resize
from skimage import color
import matplotlib.pyplot as plt
import CNN as g
import init

f = open(init.dict['test_img_list'], 'r')
testlist, labels = [], []
X_ = tf.placeholder(tf.float32, [None, init.dict['col'], init.dict['row'], 3])

acc = 0.
with tf.Session(graph=g.graph, config=g.config) as sess:
    sess.run(tf.global_variables_initializer())
    checkpoint = tf.train.latest_checkpoint('save')
    # saver = tf.train.import_meta_graph('save/model.ckpt-10.meta')
    # saver.restore(sess, tf.train.latest_checkpoint('save'))

    if checkpoint:
        print('LOAD..')
        g.saver.restore(sess, checkpoint)

    for line in f.readlines():
        path = line.split(' ')[0]
        label = line.split(' ')[1]
        testlist.append(path)
        labels.append(label)

        try:
            img = imread(path)
            img = color.gray2rgb(img)
            img = resize(img, (init.dict['col'], init.dict['row']),
                              mode='constant').astype(np.float32)

        except ValueError as e:
            print("ValueError : {}".format(e))
            continue

        X_ = array(img).reshape(1, 227, 227, 3)
        logit = sess.run(g.out, feed_dict={g.X: X_})

        if np.argmax(logit[0]) == label:
            print('correct')
            print(logit[0], label)
            acc += 1
        else:
            print('wrong')
            print(logit[0], label)

        print(acc/len(testlist))


import numpy as np
import scipy.special
# from math import erf
from scipy.signal import find_peaks
from lmfit import Model
import TraceIO2 as tio
import matplotlib.pyplot as plt
from nptdms import TdmsFile
import os, re
import pandas as pd

# Hide warnings for non-matching keywords
import warnings

warnings.filterwarnings("ignore")

# specify constants
kT = 4.114  # Thermal energy [pN*nm]
nm_bp = 0.34  # length dsDNA [nm/bp]
nm_base = 0.55  # length ssDNA = [nm/base];
omega0 = 2 * np.pi / 3.6  # inverted pitch of dsDNA [nm-1]
Ebp_kT = 1.32  # average basepair melting energy [kT]

def dat_to_h5(filename):
    traces = pd.read_csv(filename, sep='\t')

    filename = filename.split('.')[0] + '.log'
    print(filename)

    with open(filename.split('.')[0] + '.log', "r") as logdata:
        log = logdata.read()
    print(log)

    items = re.findall("resol*", log, re.MULTILINE)
    for x in items:
        print(x)
    return

    with tio.Traces(filename) as data:
        for col_name in traces.columns:
            print(col_name)
        #     par_name = col_name.split()
        #     try:
        #         index = int(par_name[0][1:])
        #         data.write_par('selected', index, 1)
        #         col_names.append(f'{par_name[0][0]} {par_name[1]} > {index}')
        #         # preliminary data processing
        #         if par_name[0][0] == 'Z':
        #             data.write_par('Z0 (um)', index, traces[col_name].quantile(0.05))
        #             data.write_par('dZ (um)', index, traces[col_name].quantile(0.05) - traces[col_name].quantile(0.95))
        #         elif par_name[0][0] == 'X':
        #             data.write_par('X0 (um)', index, traces[col_name].median())
        #         elif par_name[0][0] == 'Y':
        #             data.write_par('Y0 (um)', index, traces[col_name].median())
        #     except:
        #         # data shared by all traces
        #         col_names.append(col_name)
        # print(f'FS>>> saving file: {filename}')
        # traces.columns = col_names
        # data.write_df('data/processed', traces)

    return

def TDMS_to_h5(filename):
    filename_base = os.path.splitext(filename)[0]
    print(f'FS>>> Processing file: {filename_base + ".tdms"}')
    tdms_file = TdmsFile(filename_base + '.tdms')
    traces = tdms_file.object('Processed data').as_dataframe()
    col_names = []

    with tio.Traces(filename) as data:
        for col_name in traces.columns:
            par_name = col_name.split()
            label = par_name[0][1:]
            if label.isdigit():
                data.write_par('selected', label, 1)
                col_names.append(f'{par_name[0][0]} {par_name[1]} > {label}')
                # preliminary data processing
                if par_name[0][0] == 'Z':
                    data.write_par('Z0 (um)', label, traces[col_name].quantile(0.05))
                    data.write_par('dZ (um)', label,
                                   traces[col_name].quantile(0.05) - traces[col_name].quantile(0.95))
                elif par_name[0][0] == 'X':
                    data.write_par('X0 (um)', label, traces[col_name].median())
                elif par_name[0][0] == 'Y':
                    data.write_par('Y0 (um)', label, traces[col_name].median())
            else:
                col_names.append(f'{col_name}')
        print(f'FS>>> saving file: {filename}')
        traces.columns = col_names
        data.write_df('data/processed', traces)
    return

def erf(x):
    # Approximation of the error function
    x = np.array(x)
    a = (8 * (np.pi - 3)) / (3 * np.pi * (4 - np.pi))
    b = -x ** 2 * (4 / np.pi + a * x ** 2) / (1 + a * x ** 2)
    return np.sign(x) * np.sqrt(1 - np.exp(b))

def diff(x, n=1):
    for i in np.arange(n):
        dx = np.diff(x)
        dx = np.insert(dx, 0, dx[0])
    return dx

def coth(x):
    return np.cosh(x) / np.sinh(x)

def Gauss(x, x0=0, w=1, a=1):
    return a * np.exp(-(x - x0) ** 2 / (2 * w ** 2))

def zHooke(f_pN, k_pN__nm=0.3, z0_nm=1.5, L_nm=5):
    z_nm = ((f_pN / k_pN__nm) + z0_nm)
    return z_nm

def gHooke(f_pN, k_pN__nm=0.3, z0_nm=1.5, L_nm=5):
    g_pNnm = ((f_pN ** 2) / (2 * k_pN__nm)) + (f_pN * z0_nm)
    z_nm = ((f_pN / k_pN__nm) + z0_nm)
    g_pNnm[z_nm > L_nm] *= 100
    return g_pNnm / kT

def zFJC(f_pN, k_pN__nm=0.3, z0_nm=0.0, L_nm=25.0, b_nm=None, sigma=None, L_bp=None):
    if b_nm is None:
        b_nm = 3 * kT / (L_nm * k_pN__nm)
    if L_bp is not None:
        L_nm = L_bp * nm_base
    z_nm = L_nm * (coth(b_nm * f_pN / kT) - (kT / (b_nm * f_pN))) + z0_nm
    if sigma is not None:
        z_nm = np.ones_like(sigma) * z_nm
    return z_nm

def gFJC(f_pN, k_pN__nm=0.3, L_nm=25.0, b_nm=None, sigma=None, C_nm=28, L_bp=None, p=16):
    if b_nm is None:
        b_nm = 3 * kT / (L_nm * k_pN__nm)
    if L_bp is not None:
        L_nm = L_bp * nm_base

    g_pNnm = -(kT * L_nm / b_nm) * (np.log(f_pN) - np.log(np.sinh((b_nm * f_pN) / (kT))))

    # Remove offset at f = 0
    f_pN = 1e-9
    g_pNnm -= -(kT * L_nm / b_nm) * (np.log(f_pN) - np.log(np.sinh((b_nm * f_pN) / (kT))))

    if sigma is not None:
        # See Meng et al. 2014 Biophysical Journal:
        # https://www.cell.com/cms/10.1016/j.bpj.2014.01.017/attachment/7b4445f3-d296-43b2-8dc2-d98e13f5f00f/mmc1.pdf

        c = kT * (omega0 ** -2) * C_nm
        if p != 0: g_pNnm += L_bp * 0.5 * c * ((sigma + 1)) ** p
        g_pNnm += L_bp * 0.5 * c * (sigma + 1) ** 2

    return g_pNnm / kT

def zWLC(f_pN, L_bp=1, A_nm=50, S_pN=1000, tw_turns=None):
    z_nm = nm_bp * L_bp * (1 - 0.5 * np.sqrt(kT / (f_pN * A_nm)) + f_pN / S_pN)
    if tw_turns is not None:
        z_nm = np.ones_like(tw_turns) * z_nm
    return z_nm

def gWLC(f_pN, L_bp=1, A_nm=50, S_pN=1000, sigma=None, C_nm=100):
    # definition: g = integral(z(f), df)
    g_pNnm = nm_bp * L_bp * (f_pN - np.sqrt(f_pN * kT / A_nm) + f_pN ** 2 / (2 * S_pN))
    if sigma is not None:
        # See Meng et al. 2014 Biophysical Journal:
        # https://www.cell.com/cms/10.1016/j.bpj.2014.01.017/attachment/7b4445f3-d296-43b2-8dc2-d98e13f5f00f/mmc1.pdf
        C_nm = C_nm * (1 - (C_nm / (4 * A_nm)) * (kT / (A_nm * f_pN)))  # force dependence of twist persistence length
        c = kT * (omega0 ** 2) * C_nm
        g_pNnm += L_bp * 0.5 * c * sigma ** 2
    return g_pNnm / kT

def gPlect(sigma, L_bp=1, C_nm=28):
    c = kT * (omega0 ** 2) * C_nm
    g_pNnm = L_bp * 0.5 * c * sigma ** 2
    return g_pNnm / kT

def zDNA(f_pN, L_bp=1, tw_turns=None):
    zds_bp = zWLC(f_pN, L_bp=1)
    zss_bp = zFJC(f_pN / 2, b_nm=1.5, L_bp=1)
    if tw_turns is None:
        # Torsionally relaxed
        gds = gWLC(f_pN, L_bp=1) - f_pN * zds_bp / kT
        gss = 2 * gFJC(f_pN / 2, b_nm=1.5, L_bp=1) - f_pN * zss_bp / kT + Ebp_kT

        c = np.min([1000, L_bp])  # tweak factor: limiting cooperativity, avoiding overflow
        Pds = np.exp(-gds * c) / (np.exp(-gds * c) + np.exp(-gss * c))
        Pss = 1 - Pds
        Z_nm = L_bp * (zds_bp * Pds + zss_bp * Pss)
    else:
        # Torsionally constraint
        if np.isscalar(tw_turns):
            tw_turns = np.ones_like(f_pN) * tw_turns

        # Range to compute
        Nds_range = np.linspace(0, L_bp, 100)
        TW_range = np.linspace(-3, 3, 50)  # change of twist in dsDNA to evaluate each force step

        # Matrices with all distributions of conformations and twist
        Nds = np.outer(Nds_range, np.ones_like(TW_range))
        Nss = L_bp - Nds
        deltaTWds = np.outer(np.ones_like(Nds_range), TW_range)
        TWds_last = tw_turns[0]  # start with all twist in dsDNA

        Z_nm = []
        for f, tw, zds, zss in zip(f_pN, tw_turns, zds_bp, zss_bp):
            TWds = TWds_last + deltaTWds
            TWss = tw - TWds - Nss / 10.4
            sigma_ds = TWds / (omega0 * Nds * nm_bp)
            Gds = gWLC(f, L_bp=Nds, sigma=sigma_ds) - f * zds * Nds / kT
            sigma_ss = (TWss / (omega0 * Nss * nm_bp))
            Gss = 2 * gFJC(f / 2, b_nm=1.5, L_bp=Nss, sigma=sigma_ss) - (f * zss * Nss / kT) + Ebp_kT * Nss
            G = Gds + Gss

            selected = np.isfinite(G)
            G -= np.min(G[selected])  # Avoid large numbers in exponents
            P = np.exp(-G[selected])
            P /= np.sum(P)  # Normalize P
            Z = P * (Nds[selected] * zds + Nss[selected] * zss)
            Z_nm.append(np.sum(Z))
            TWds_last = np.sum(P * TWds[selected])
    return Z_nm

def drift(time_s, dz_dt_nm__s=0, z0_nm=0):
    return (time_s * dz_dt_nm__s) + z0_nm

def get_fiber_states(Ntot=0, Ntetra=0, degeneracy=1):
    states = []
    fiber_degeneracy = []
    d = np.array([degeneracy, 1, 1, 1])
    for n3 in range(0, int(Ntot) + 1):
        for n2 in range(0, int(Ntot) + 1):
            for n1 in range(0, int(Ntot) + 1):
                n0 = Ntot - n1 - n2 - n3
                if (n2 + n3 >= Ntetra) and (n0 >= 0):
                    states.append(np.asarray([n0, n1, n2, n3]))
                    fiber_degeneracy.append(
                        scipy.special.factorial(np.sum(d * states[-1])) / np.prod(
                            scipy.special.factorial(d * states[-1])))
    return np.array(states), np.array(fiber_degeneracy)

def add_steps(force_pN, zfiber_nm, L_bp=3000, L2_bp=79, Ntot=0):
    # Unwrapping:
    pulling = diff(force_pN, 1) > 0
    funwrap_pN = force_pN[np.argmax(diff(zfiber_nm[pulling], 1))]
    unwrapevents = np.sort(np.random.normal(funwrap_pN, 6, Ntot))
    for i, f in enumerate(unwrapevents):
        selected = np.logical_and(force_pN > f, diff(force_pN) >= 0)
        z = zWLC(force_pN, L_bp - (Ntot - 1 - i) * L2_bp)
        zfiber_nm[selected] = z[selected]
    # wrapping:
    wrapevents = np.sort(np.random.normal(4, 2, Ntot))
    for i, f in enumerate(wrapevents):
        selected = np.logical_and(force_pN > f, diff(force_pN) <= 0)
        z = zWLC(force_pN, L_bp - (Ntot - 1 - i) * L2_bp)
        zfiber_nm[selected] = z[selected]
    return zfiber_nm

def get_steps(force_pN, zfiber_nm, L_bp, Ntot=0, k_pN__nm=0.3, NRL_bp=197, L2_bp=79, time_s=None):
    np.seterr(divide='ignore', invalid='ignore')
    var_track = 5 ** 2;
    df = diff(force_pN)
    length = np.arange(L_bp + 500, 0, -2)
    P = np.zeros_like(length)
    i = 0
    for i, l in enumerate(length):
        if l > L_bp - (Ntot * L2_bp):
            zwlc = zWLC(force_pN, l)
            sigma = np.sqrt(kT / (df / diff(zwlc)) + var_track)
            x = 1 - erf(abs((zwlc - zfiber_nm) / sigma))
            P[i] = np.nansum(x)
        else:
            zwlc = zWLC(force_pN, l)
            z_nuc, _ = zHooke(force_pN, k_pN__nm=k_pN__nm, z0_nm=0)
            n_nuc = (l - (Ntot * L2_bp)) / (NRL_bp - L2_bp)
            z_tot = zwlc + n_nuc * z_nuc
            sigma = np.sqrt(kT / (df / diff(z_tot)) + var_track)
            x = 1 - erf(abs((zwlc - zfiber_nm) / sigma))
            P[i] = np.nansum(x)

    peaks, _ = find_peaks(P, width=10)

    plt.plot(length, P)
    plt.show()

    return length[peaks]

def chromatin(force_pN, L_bp=3000, Ntot=0, Ntetra=0,
              L1_bp=90, L2_bp=79, NRL_bp=197,
              g1_kT=19, g2_kT=6.5, g3_kT=100,
              k_pN__nm=0.3, p_nm=50, S_pN=1000,
              degeneracy=1):
    # Create G(z) curves for each nucleosome conformation
    z_nuc = np.empty((4, len(force_pN)))
    g_nuc = np.empty_like(z_nuc)

    Lwrap_bp = [L1_bp, L2_bp, 0]
    Gunwrap_kT = [g1_kT, g1_kT + g2_kT, g1_kT + g2_kT + g3_kT]
    z_nuc[0] = zHooke(force_pN, k_pN__nm=k_pN__nm, z0_nm=0)
    g_nuc[0] = gHooke(force_pN, k_pN__nm=k_pN__nm, z0_nm=0)

    for i, (l, g) in enumerate(zip(Lwrap_bp, Gunwrap_kT)):
        z_nuc[i + 1] = zWLC(force_pN, NRL_bp - l, p_nm, S_pN)
        g_nuc[i + 1] = gWLC(force_pN, NRL_bp - l, p_nm, S_pN)
        g_nuc[i + 1] += g

    # Calculate extension of the DNA handles
    ztether_nm = zWLC(force_pN, L_bp - (Ntot * NRL_bp), p_nm, S_pN)

    # Get all possible distributions of nucleosome conformations, and their combinatoric degeneracy
    s_fiber, d_fiber = get_fiber_states(Ntot=Ntot, Ntetra=Ntetra, degeneracy=degeneracy)
    # For each force point, add the sum of the extensions of each nucleosome, weighted with its probability
    for i, (f, z) in enumerate(zip(force_pN, z_nuc.T)):
        gf_nuc = g_nuc - f * z_nuc / kT
        g = np.min(gf_nuc, axis=1)
        g_fiber = np.sum(s_fiber * g, axis=1)
        g_fiber -= np.min(g_fiber)
        p_fiber = np.exp(-g_fiber)
        z_fiber = np.sum(s_fiber * z, axis=1)
        ztether_nm[i] += np.sum(p_fiber * z_fiber * d_fiber) / np.sum(p_fiber * d_fiber)

    return ztether_nm

def calculate_force(shift_mm, L1_mm=1.4, L2_mm=0.8, f0_pN=0.01):
    # calculate the force from the magnet position.
    force_pN = (85 * (0.7 * np.exp(-shift_mm / L1_mm) + 0.3 * np.exp(-shift_mm / L2_mm)) + f0_pN)
    return force_pN

def bead_offset(force_pN, phi_rad=0, r_um=1.4, C_pNum_rad=100):
    # Calculate z-offset due to side attachment
    # by equating torsional energy to work.
    # For rotational stiffness bead see Klaue2009PRL.

    rho = np.linspace(0, np.pi / 2 - phi_rad, 1000)  # range of longitudes, given attachment point phi
    theta = []  # actual longitude at applied force
    try:
        for f in force_pN:
            err = -f * r_um * (1 - np.sin(rho)) + 0.5 * C_pNum_rad * (np.pi / 2 - (rho - phi_rad)) ** 2
            theta.append(rho[np.argmin(err ** 2)])
    except:
        # single force point
        err = f * r_um * (1 - np.sin(rho)) - 0.5 * C_pNum_rad * (np.pi / 2 - (rho - phi_rad)) ** 2
        theta = rho[np.argmin(err ** 2)]
    dz_um = -r_um * np.sin(theta)
    return dz_um * 1000

def fix(params):
    for p in params:
        params[p].set(vary=False)
    return

def test_simulate():
    #####################################################################
    # Simulate series of F-z curves
    shift_mm = np.arange(10, 0.9, -0.005)
    shift_mm = np.append(shift_mm, np.flip(shift_mm, axis=0))
    force_pN = calculate_force(shift_mm)

    duration_s = 30.0
    time_s = np.arange(0, duration_s, duration_s / len(force_pN))

    # selected = np.logical_and(force_pN < 10, force_pN > 1)
    selected = time_s < duration_s / 2
    selected *= force_pN < 7

    data.write_channel(data=force_pN, channel='Force (pN)', group='processed', label='')
    data.write_channel(data=time_s, channel='Time (s)', group='processed', label='')

    Ntot = 10
    Ntetra = 0
    L_bp = 3000

    for num, g2_kT in enumerate(np.linspace(0, 10, num=3)):
        zfiber_nm = chromatin(force_pN, L_bp=L_bp, Ntot=Ntot, Ntetra=Ntetra, g2_kT=g2_kT, degeneracy=1)

        # Make data more realistic by adding steps and noise
        zfiber_nm = add_steps(force_pN, zfiber_nm, L_bp, 80, Ntot)
        zfiber_nm += np.random.normal(0, 4, len(force_pN))

        if False:
            # fit force extension curve
            model = Model(chromatin) + Model(drift)
            params = model.make_params()
            fix(params)

            params['Ntot'].set(Ntot, vary=False, min=0)
            params['Ntetra'].set(Ntetra, vary=False, min=0)
            params['g1_kT'].set(10, vary=True, min=0, max=25)
            params['g2_kT'].set(g2_kT, vary=True, min=0, max=10)
            params['k_pN__nm'].set(1, vary=True, min=0.1, max=10)

            result = model.fit(zfiber_nm[selected], force_pN=force_pN[selected], time_s=time_s[selected], params=params)
            best_fit = model.eval(result.params, zfiber_nm=zfiber_nm, force_pN=force_pN, time_s=time_s)

            data.write_channel(data=selected, channel='Selected', group='processed', label=str(num))
            data.write_channel(data=zfiber_nm / 1000, channel='Z (um)', group='measured', label=str(num))
            data.write_fit('Fiber', 'Z (um)', best_fit / 1000, result.params, label=str(num))
            R2 = 1 - result.residual.var() / np.var(zfiber_nm[selected])
            data.write_par('R2_fiber', R2, label=str(num))

            g2_fit = data.read_par('g2_kT', label=str(num))
            print(f'{num}) G2 = {g2_kT:0.2} (kT) -> {g2_fit}')
        plt.plot(zfiber_nm / 1000, force_pN)
    tio.format_plot(r'z ($\mu$m)', 'F (pN)', data.plot_title(), aspect=2, scale_page=0.5, xrange=[0, 1.1])
    return

def test_plot():
    #####################################################################
    # Recover trace #2 and display fit parameters and F(z) plot
    label = '2'
    pars = data.parameters(label=label)

    # Add reference curves for single wrapped nucleosomes
    Ntot, _ = data.read_par('Ntot', label=label)
    L_bp, _ = data.read_par('L_bp', label=label)
    L2_bp, _ = data.read_par('L2_bp', label=label)
    force_pN = data.read_channel(channel='Force (pN)', group='processed', label='')
    selected = force_pN < 7
    selected[int(len(selected) / 2):] = 0

    curves = []
    for n in np.arange(0, Ntot + 1):
        curves.append(zWLC(force_pN, L_bp - (n * L2_bp)))

    zfit = data.read_channel(channel='Z (um)', group='fit_Fiber', label=label) * 1000.
    curves.append(zfit)

    z_nm = data.read_channel(channel='Z (um)', group='measured', label=label) * 1000

    ax = tio.format_plot('z (nm)', 'F (pN)', xrange=[0, L_bp / 2.5], title=data.plot_title(), pars=pars
                         , scale_page=1.0, aspect=5 / 8)

    ax.scatter(z_nm, force_pN, color='grey')
    ax.scatter(z_nm[selected], force_pN[selected], color='blue', marker='o')

    curves.reverse()
    linestyle = '-'
    color = 'black'
    for curve in curves:
        ax.plot(curve, force_pN, color=color, linestyle=linestyle, linewidth=1)
        linestyle = ':'
        color = 'grey'

    # plt.savefig(filename, pad_inches=0, transparent=True)
    plt.show()
    return

def plot_fz(data, index):
    t = data.read_df('data/processed', filter='Time')
    for name, z in data.read_df('data/processed', filter='Z').iteritems():
        offset, _ = data.read_par('Z0 (um)', int(name.split()[-1]))
        plt.plot(t, z - offset)
        tio.format_plot('t (s)', 'z (um)', data.plot_title(name))
    return

def _tmp_PlotFzDNA():
    m = np.linspace(10, 0, 1000)
    f = calculate_force(m)
    L_bp = 6000

    zDNA(f_pN=f, L_bp=L_bp, tw_turns=50)

    phi = 0  # degrees
    phi *= np.pi / 180  # rad

    dz = bead_offset(f, phi, C_pNum_rad=100)
    # i = np.argmin((f - 50) ** 2)
    # dz -= dz[i]

    legend = []
    # twist = np.linspace(0, -L_bp / 2, 6)
    # for t in twist:
    #     plt.plot(zDNA(f_pN=f, L_bp=L_bp, tw_turns=t) , f)
    #     legend.append(rf'$\sigma$ = {t / (omega0 * L_bp * nm_bp):.2f}')

    # dz -= np.min(dz)
    # dz -= 400

    plt.plot(zDNA(f_pN=f, L_bp=L_bp) + dz, f, color='blue')
    plt.plot(zWLC(f_pN=f, L_bp=L_bp) + dz, f, color='blue', linestyle='dashed')
    plt.plot(zFJC(f_pN=f / 2, b_nm=1.5, L_nm=L_bp * nm_base) + dz, f, color='blue', linestyle='dashed')
    dz = 0
    plt.plot(zDNA(f_pN=f, L_bp=L_bp) + dz, f, color='grey')
    plt.plot(zWLC(f_pN=f, L_bp=L_bp) + dz, f, color='grey', linestyle='dashed')
    plt.plot(zFJC(f_pN=f / 2, b_nm=1.5, L_nm=L_bp * nm_base) + dz, f, color='grey', linestyle='dashed')

    tio.format_plot('z (nm)', 'F (pN)', yrange=[0, 79], xrange=[0, L_bp / 1.5], ref='a)', legend=legend, aspect=2 / 3,
                    scale_page=1)
    print('done')

def _tmp_PlotGT():
    f = 100
    L_bp = 1000
    tw = np.linspace(-2 * L_bp, L_bp, 1000)
    sigma = tw / (omega0 * L_bp * nm_bp)

    Ztwisted = zWLC(f_pN=f, L_bp=L_bp, tw_turns=tw)
    Gtwisted = gWLC(f, L_bp=L_bp, sigma=sigma) - f * Ztwisted / kT

    Zmelted = zFJC(f / 2, b_nm=1.5, L_nm=L_bp * nm_base, sigma=sigma)
    Gmelted2 = 2 * gFJC(f / 2, b_nm=1.5, sigma=sigma, L_bp=L_bp, p=0) - f * Zmelted / kT + L_bp * Ebp_kT
    Gmelted = 2 * gFJC(f / 2, b_nm=1.5, sigma=sigma, L_bp=L_bp) - f * Zmelted / kT + L_bp * Ebp_kT

    Gplect = gPlect(sigma, L_bp=L_bp)

    plt.plot(sigma, Gtwisted / L_bp)
    plt.plot(sigma, Gplect / L_bp, color='Green')
    plt.plot(sigma, Gmelted / L_bp, color='Red')
    plt.plot(sigma, Gmelted2 / L_bp, color='Red', linestyle='dashed')
    plt.plot(sigma, Gplect * 0, color='Black', linewidth=1)
    legend = ['twisted', 'plectonemic', 'melted']
    tio.format_plot(r'$\sigma$ (-)', r'g ($k _B T$)', xrange=[-2.5, 1.5], aspect=2 / 6, yrange=[-1.5, 10.5],
                    scale_page=1,
                    ref='a)', legend=legend)

    return

if __name__ == "__main__":
    #   Intended as examples. Make your own script and import this module to your script.
    #   You can copy the code below as a start of your own script

    _tmp_PlotFzDNA()
    # _tmp_PlotGT()

    # test_simulate()

    filenames = [rf'C:\Users\noort\data\ForceTDMS\data_026.tdms']
    filenames = [r'C:\Users\noort\data\ForceTDMS\data_008.dat']
    filenames = [r'C:\Users\noort\LTS\werk\Software\Python\testdata\ForceTDMS\data_021.h5']

    # for filename in filenames:
    #     TDMS_to_h5(filename)
    # dat_to_h5(filename)

    # with tio.Traces(filename) as data:
    #     plot_fz(data, 4)


# -*- coding: utf-8 -*-
"""
Created on Mon Nov 18 23:16:41 2019

@author: User
"""

import torch
from model_dcgan import Discriminator,Generator # 변경
from matplotlib.pyplot import imshow, imsave
import os
from PIL import Image
import numpy as np
import torch
import torch.nn as nn
from torchvision.utils import save_image
import argparse

def get_sample_single_image(G, counts ,n_noise,DEVICE,file_path,save):
    #torch.radn(a,b) => b 크기의 데이터 a개를 만들어라
    z = torch.randn(counts, n_noise, 1, 1).to(DEVICE)
    y_hat = torch.squeeze(G(z), 1) # (100, 28, 28)
#    print(y_hat)
#    result = (y_hat.cpu().data.numpy()*255).astype(int)
#    print(result.shape)
#    print(type(result))
#    print(result)

    if save == True:
        for index in range(y_hat.shape[0]):
            new_image=y_hat.data[index]
            save_image(new_image,file_path+'/%s_%s.png'%(index,opt.cls_num.split('_')[0]))
            print(index)
            #print(new_image)

    return ''

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('--dataset', type=str)
    parser.add_argument('--cls_num', type=str)
    parser.add_argument('--nc', required=True,type=int, help='MNIST 1, cifar10 3')
    parser.add_argument('--ngf', type=int,default=64, help="path to netG (to continue training)")
    parser.add_argument('--counts', type=int)
    parser.add_argument('--model_name', help='Normal_GAN | DC_GAN')
    parser.add_argument('--model_epoch', type=str)
    parser.add_argument('--save', type=bool)
    parser.add_argument('--ngpu', type=int, help='num of gpu', default=1) #추가

    opt = parser.parse_args()

    dir_path=r'./models/'
    path = dir_path+opt.model_name+'/'+opt.dataset+opt.cls_num+'/'+opt.model_epoch
#    path=r'.\models\G.pkl'

    DEVICE=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    n_noise=100

    model=Generator(opt.ngpu, n_noise, ngf=opt.ngf, nc=opt.nc).to(DEVICE) # 변경
    model.load_state_dict(torch.load(path))
    model.eval()

    file_path=r'./oversampled_data/' + opt.model_name + '/' + opt.dataset +'/'+ opt.cls_num
    if not os.path.exists(file_path):
        os.makedirs(file_path)

    counts=opt.counts
    get_sample_single_image(model, opt.counts, n_noise,DEVICE,file_path,opt.save)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 19 17:11:42 2019

@author: sebas
"""

# ídem clase pasada con implementación "más copada"

import torch

# defino la clase del modelo (por ahora con una sola capa oculta)
# el guión bajo _ es el self
# mínimo para def clase: funciones init y forward
# arquitectura y aprendizaje

class mlp(torch.nn.Module):
    def __init__(_, isize, hsize, osize):
        super().__init__() # inicializo la super clase
        _.L1 = torch.nn.Linear(isize, hsize)
        _.L2 = torch.nn.Linear(hsize, osize)

    def forward(_,x):
        h = torch.tanh(_.L1(x))
        y = torch.tanh(_.L2(h))
        return y

#device = torch.device( "cuda:0" if torch.cuda.is_available() else "cpu" )

P = 5000
N = 8
#H = N+1  # capa oculta con N+1 unidades
H = N + N//2 # RECORDAR: el H=N+1 es el MÍNIMO para que funcione
M = 1

x = torch.randn(P,N).sign() ### rand es para distribucion uniforme
z = torch.prod(x,dim=1).view(P,M) # el signo 8 bit como entrenamiento. NO es separable!

#x = x.to(device)
#z = z.to(device) # ¿Quizás adentro del while esto?

lr = 1e-2
E, t = 1.,0 # inicializo un error y un contador de epocas

model = mlp(N,H,M)#.to(device)
costf = torch.nn.MSELoss()

# agregamos optimización por stochastic gradient descent
# otra opción es el RProp o el Adam

optim = torch.optim.SGD(model.parameters(), lr=0.1)

model.train() # lo pongo al modelo en modo entrenamiento. Ver docs, funciona para algunos módulos

while E>0.001 and t<10001:
#    h = torch.cat((x,bias),dim=1).mm(w1).tanh()
#    y = torch.cat((h,bias),dim=1).mm(w2).tanh()
#    print(model)
    y = model(x)
#    y = model.foward(x)

#    error = (y-z).pow(2).sum() ### error para los datos con ruido
#    error.backward() ### calcula gradiente y lo guarda en w

#    model.zero_grad() # tengo que resetear el gradiente antes de recalcular los pesos
    optim.zero_grad() # conviene resetear el gradiente por optim

    error = costf(y,z) # calculo el torch.cuda.is_available()rror entre target y modelo
    error.backward() # calcula el gradiente y lo guarda
#    optim.backward() # calcula el gradiente y lo guarda

#    with torch.no_grad():
#        w1 -= lr*w1.grad
#        w2 -= lr*w2.grad
#        w1.grad.zero_() ### del tensor existente, lo completa con zeros
#        w2.grad.zero_() ### del tensor existente, lo completa con zeros

#        for param in model.parameters():
#            param -= lr*param.grad
    optim.step() #recalcular por gradiente descendiente

    E = error.item()
    t += 1 #paso de época

    if t%100 ==0:
        print(t,E)

model.eval() # lo pongo al modelo en modo validación. Ver docs, funciona para algunos módulos

xp = torch.randn(P,N).sign()
zp = torch.prod(xp,dim=1).view(P,M)

with torch.no_grad():
    yp = model(xp)
    Ep = costf(yp, zp)

print("Error generalización", Ep.item()) #Es muy malo!


import numpy as np
from torch.utils import data
from torch.nn import DataParallel

from OSDN.evt_fitting import *
from OSDN.openmax_utils import *
from config import Config
from data import DataSet
from models import *
from visualize_output import show_histgram

def openmax(input_score, weibull_model, categories, eu_weight=5e-2, alpharank=10, distance_type='eucos'):
    """Re-calibrate scores via OpenMax layer
    Output:
        openmax probability and softmax probability
    """
    nb_classes = len(categories)
    # take index in descending order ( list[::-1] means revers the list)
    ranked_list = input_score.argsort().ravel()[::-1][:alpharank]
    alpha_weights = [((alpharank + 1) - i) / float(alpharank) for i in range(1, alpharank + 1)]
    omega = np.zeros(nb_classes)
    omega[ranked_list] = alpha_weights
    scores, scores_u = [], []
    for channel, input_score_channel in enumerate(input_score):
        score_channel, score_channel_u = [], []
        dist_l = []
        wscore_l = []
        for c, category_name in enumerate(categories):
            mav, dist, model = query_weibull(category_name, weibull_model, distance_type)
            channel_dist = calc_distance(input_score_channel, mav[category_name], eu_weight, distance_type)
            wscore = model[channel].w_score(channel_dist)
            dist_l.append(channel_dist)
            wscore_l.append(wscore)

            modified_score = input_score_channel[c] * (1 - wscore * omega[c])
            score_channel.append(modified_score)
            score_channel_u.append(input_score_channel[c] - modified_score)
        scores.append(score_channel)
        scores_u.append(score_channel_u)

    scores = np.asarray(scores)
    scores_u = np.asarray(scores_u)

    openmax_prob = np.array(compute_openmax_prob(scores, scores_u))
    softmax_prob = softmax(np.array(input_score.ravel()))
    return openmax_prob, softmax_prob

def calc_distance(query_score, mcv, eu_weight, distance_type='eucos'):
    if distance_type == 'eucos':
        query_distance = spd.euclidean(mcv, query_score) * eu_weight + \
                         spd.cosine(mcv, query_score)
    elif distance_type == 'euclidean':
        query_distance = spd.euclidean(mcv, query_score)
    elif distance_type == 'cosine':
        query_distance = spd.cosine(mcv, query_score)
    else:
        raise TypeError("distance type not known: enter either of eucos, euclidean or cosine")
    return query_distance

def compute_openmax_prob(scores, scores_u):
    prob_scores, prob_unknowns = [], []
    for s, su in zip(scores, scores_u):
        channel_scores = np.exp(s)
        channel_unknown = np.exp(np.sum(su))

        total_denom = np.sum(channel_scores) + channel_unknown
        prob_scores.append(channel_scores / total_denom)
        prob_unknowns.append(channel_unknown / total_denom)

    # Take channel mean
    scores = np.mean(prob_scores, axis=0)
    unknowns = np.mean(prob_unknowns, axis=0)
    modified_scores = scores.tolist() + [unknowns]
    return modified_scores

def softmax(x):
    e_x = np.exp(x - np.max(x))
    return e_x / e_x.sum()

def main():
    opt = Config()
    opt.num_classes = len(get_train_labels(opt.train_root, opt.criteria_list))
    opt.metric = 'liner'

    distance_path = opt.distance_path
    mean_path = opt.mean_files_path
    alpha_rank = opt.ALPHA_RAN

    labellist = getlabellist(opt.criteria_list)
    train_labels = get_train_labels(opt.train_root, opt.criteria_list)

    # recreate or first create
    weibull_model = weibull_tailfitting(mean_path, distance_path, train_labels,
                                        tailsize=opt.WEIBULL_TAIL_SIZE, distance_type=opt.distance_type)

    # data loader
    test_dataset = DataSet(opt.test_root, opt.test_list, phase='test', input_shape=opt.input_shape)

    test_loader = data.DataLoader(test_dataset,
                                  batch_size=opt.test_batch_size,
                                  shuffle=True,
                                  num_workers=opt.num_workers)

    # load model , both of feature, fc_modeal
    if opt.backbone == 'resnet18':
        model = resnet_face18(opt.use_se)
    elif opt.backbone == 'resnet34':
        model = resnet34()
    elif opt.backbone == 'resnet50':
        model = resnet50()
    else:
        raise TypeError('backbone: {} is not expected'.format(opt.backbone))

    model = DataParallel(model)
    model.to(device)
    if device == 'cuda':
        model.load_state_dict(opt.test_model_path)
    else:
        model.load_state_dict(torch.load(opt.test_model_path, map_location={'cuda:0': 'cpu'}))
    model.eval()

    if opt.metric == 'add_margin':
        metric_fc = AddMarginProduct(512, opt.num_classes, s=30, m=0.35)
    elif opt.metric == 'arc_margin':
        metric_fc = ArcMarginProduct(512, opt.num_classes, s=30, m=0.5, easy_margin=opt.easy_margin)
    elif opt.metric == 'sphere':
        metric_fc = SphereProduct(512, opt.num_classes, m=4)
    else:
        metric_fc = nn.Linear(512, opt.num_classes)

    metric_fc.to(device)
    metric_fc = DataParallel(metric_fc)
    if device == 'cuda':
        metric_fc.load_state_dict(opt.test_metric_fc_path)
    else:
        metric_fc.load_state_dict(torch.load(opt.test_metric_fc_path, map_location={'cuda:0': 'cpu'}))

    metric_fc.eval()
    print(labellist)
    openmax_preds_list = []
    softmax_preds_list = []
    ans_preds_list = []
    softmax_data_list_known = []
    softmax_data_list_unknown = []

    # # data loader
    # test_dataset = Dataset('estimate_visualize', 't-SNE_test1568010965.919611.png', phase='test', input_shape=opt.input_shape)
    #
    # test_loader = data.DataLoader(test_dataset,
    #                               batch_size=1,
    #                               shuffle=True,
    #                               num_workers=opt.num_workers)
    # # from PIL import Image
    # # img = Image.open('estimate_visualize/t-SNE_test1568010965.919611.png', )
    # # img = np.array(img)
    # # img = img[np.newaxis,:,:]
    # # print(img.shape)
    # # tt = test_dataset.transforms
    # # # for t in tt:
    # # img = tt(img)
    # # # img = img.resize(256,256)
    # # # img = torch.Tensor(img)
    # for i, (imgs, label_ids) in enumerate(test_loader):
    #     # compute feature and estimate score →　create img_preds that contains feature, score
    #     imgs_feature = model(imgs)
    #     # scores = metric_fc(imgs_feature, label_ids)
    #     scores = metric_fc(imgs_feature)
    #     scores = scores.detach().numpy()
    #     print(scores)
    # # ff = model(img)
    # # score = metric_fc(ff)
    # print(scores)
    # print(softmax(scores[0]))

    for i, (imgs, label_ids) in enumerate(test_loader):
        # compute feature and estimate score →　create img_preds that contains feature, score
        imgs_feature = model(imgs)
        # scores = metric_fc(imgs_feature, label_ids)
        scores = metric_fc(imgs_feature)
        scores = scores.detach().numpy()
        scores = np.array(scores)[:, np.newaxis, :]
        temp_labels = [labellist[pid] for pid in label_ids]
        for ii, (score, label) in enumerate(zip(scores, temp_labels)):
            openmax_predict, softmax_predict = openmax(score, weibull_model, train_labels, eu_weight=opt.euc_scale,
                                                       alpharank=alpha_rank, distance_type=opt.distance_type)

            softmax_ans = labellist[np.argmax(softmax_predict)]
            # type 1
            # openmax_ans = labellist[np.argmax(openmax_predict)] if np.argmax(openmax_predict) < len(
            #     train_labels) else 'unknown'

            # type2
            # openmax_ans = softmax_ans if np.sort(score, axis=1)[0][::-1][0] > opt.SCORE_THRESHOLD else 'unknown'

            # type3
            openmax_ans = softmax_ans if np.sort(score, axis=1)[0][::-1][0] / np.linalg.norm(score,
                                                                                ord=2) > opt.SCORE_NORMALIZED else 'unknown'

            # type4
            openmax_ans = softmax_ans if np.sort(score, axis=1)[0][::-1][0] / np.linalg.norm(score[score > 0],
                                                                                ord=2) > opt.SCORE_NORMALIZED else 'unknown'

            ans_label = label if labellist.index(label) < len(train_labels) else 'unknown'
            if ans_label == 'unknown':
                softmax_data_list_unknown.append(np.sort(score, axis=1)[0][::-1][0] / np.linalg.norm(score[score > 0],
                                                                                ord=2))
                if np.sort(score, axis=1)[0][::-1][0] / np.linalg.norm(score[score > 0],ord=2) > 0.7:
                    import matplotlib.pyplot as plt
                    print(label)
                    plt.imshow(np.array(imgs[ii][0]))
                    plt.savefig('estimate_visualize/{}_{}.jpg'.format(i, label))
                    plt.show()
            else:
                softmax_data_list_known.append(np.sort(score, axis=1)[0][::-1][0] / np.linalg.norm(score[score > 0],
                                                                                ord=2))

            # if ans_label == 'unknown':
            #     softmax_data_list_unknown.append(score[0])
            # else:
            #     softmax_data_list_known.append(score[0])
            openmax_preds_list.append(openmax_ans)
            softmax_preds_list.append(softmax_ans)
            ans_preds_list.append(ans_label)
            print('predict_softmax: {}, predict_openmax: {}, answer: {}'.format(softmax_ans, openmax_ans, ans_label))

    # create_mean_graph(softmax_data_list_known)
    # create_mean_graph(softmax_data_list_unknown)
    show_histgram(softmax_data_list_unknown)
    show_histgram(softmax_data_list_known)

    # accuracy check
    soft_acc = accuracy(softmax_preds_list, ans_preds_list)
    open_acc = accuracy(openmax_preds_list, ans_preds_list)
    print('softmax:', soft_acc / len(ans_preds_list))
    print('openmax:', open_acc / len(ans_preds_list))

def accuracy(preds, labels):
    accs = list()
    for i in range(len(preds)):
        accs.append(preds[i] == labels[i])
    print(accs.count(True))
    return accs.count(True)

if __name__ == '__main__':
    main()


from scipy.optimize import linprog
from mip import Model, xsum, maximize, BINARY

p = [20, 40, 20, 15, 30]
w1 = [5, 4, 3, 7, 8]
I1 = range(len(w1))
w2 = [1, 7, 9, 4, 6]
I2 = range(len(w2))
w3 = [8, 10, 2, 1, 10]
I3 = range(len(w3))
c1 = 25
c2 = 25
c3 = 25

m = Model()
x = [m.add_var(var_type=BINARY) for i in I1]
m.objective = maximize(xsum(p[i]*x[i] for i in I1))

m += xsum(w1[i] * x[i] for i in I1) <= c1
m += xsum(w2[i] * x[i] for i in I2) <= c2
m += xsum(w3[i] * x[i] for i in I3) <= c3

selected = [i for i in I1 if x[i].x >= 0.99]


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jul  3 13:49:33 2018

@author: tom verguts
create plots for slides chapter 4, the delta rule
"""
import matplotlib.pyplot as plt
import numpy as np

# plot some vectors
nplot = 3
fig, axs = plt.subplots(nrows=1,ncols=nplot)
v = np.array([[1, 1], [-1, 1], [1, 0]]) # collection of all vectors (not of length unity)
to_be_plotted = [[0, 1], [0, 2], [0, 1, 2]]
for plot_loop in range(nplot):
    for dot_loop in to_be_plotted[plot_loop]:
        axs[plot_loop].scatter(v[dot_loop][0], v[dot_loop][1], c = "black")
    axs[plot_loop].set_xlim(-1.5, 1.5)
    axs[plot_loop].set_ylim(-1.5, 1.5)
    axs[plot_loop].set_xlabel("Input dimension 1")
    axs[plot_loop].set_ylabel("Input dimension 2")


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

import tornado.web
import json, datetime, urllib
from dao.request_dao import RequestDao
from base_handler import BaseHandler
from models import RequestEncoder

class RequestHandler(BaseHandler):
    def initialize(self):
        super(RequestHandler, self).initialize()
        self.__dao = RequestDao(self._session)

    def get(self):
        project_id = self.get_argument("project")
        project_id = int(project_id)

        page_index = self.get_argument("page_index", 1)
        page_index = int(page_index)

        page_size = self.get_argument("page_size", 10)
        page_size = int(page_size)

        key = "project_%s_page_index_%s" % (project_id, page_index)
        vo = self._cache_client.get(key)
        if vo is None:
            vo = self.__dao.get_items(page_index, page_size, project_id)
            self._cache_client.add(key, vo, 7 * 24 * 60 * 60)

        self.render('request.html', requests=vo.get_items(), \
            project_id=project_id)

    def post(self):
        data = self.get_body_argument("data")
        data = urllib.unquote(data)
        data = json.loads(data)

        method = data["method"]
        params = data["params"]

        if (method == "get_item"):
            self.__get_item(params)

        if (method == "modify_item"):
            self.__modify_item(params)

        if (method == "add_item"):
            self.__add_item(params)

        if (method == "delete_item"):
            self.__delete_item(params)

    def __get_item(self, params):
        request_id = params["id"]
        request_id = int(request_id)

        request = self.__dao.get_item(request_id)

        self.write(json.dumps(request, cls=RequestEncoder))

    def __modify_item(self, params):
        request_id = params["id"]
        request_id = int(request_id)

        request = self.__dao.get_item(request_id)
        request.name = params["name"]
        request.method = params["method"]
        request.modify_time = datetime.datetime.now()

        self.__dao.merge_item(request)

        result = {"code": 200}
        self.write(result)

    def __add_item(self, params):
        project_id = params["project_id"]
        name = params["name"]
        method = params["method"]

        self.__dao.add_item(project_id, name, method)

        result = {"code": 200}
        self.write(result)

    def __delete_item(self, params):
        request_id = params["id"]
        request_id = int(request_id)

        self.__dao.delete_item(request_id)

        result = {"code": 200}
        self.write(result)


import sys
import rdkit
from rdkit import Chem

import simtk
import functools

import numpy as np

from openforcefield.utils import toolkits
from openforcefield.typing.engines.smirnoff import ForceField
from openforcefield.topology import ValenceDict

from timemachine.lib import custom_ops

def merge_potentials(nrgs):
    c_nrgs = []
    for a in nrgs:
        a_name = a[0]
        a_args = a[1]
        if a_name == custom_ops.HarmonicBond_f32:
            c_nrgs.append(custom_ops.HarmonicBond_f32(a_args[0], a_args[1]))
        elif a_name == custom_ops.HarmonicAngle_f32:
            c_nrgs.append(custom_ops.HarmonicAngle_f32(a_args[0], a_args[1]))
        elif a_name == custom_ops.PeriodicTorsion_f32:
            c_nrgs.append(custom_ops.PeriodicTorsion_f32(a_args[0], a_args[1]))
        elif a_name == custom_ops.LennardJones_f32:
            c_nrgs.append(custom_ops.LennardJones_f32(a_args[0].astype(np.float32), a_args[1]))
        elif a_name == custom_ops.Electrostatics_f32:
            c_nrgs.append(custom_ops.Electrostatics_f32(a_args[0].astype(np.float32), a_args[1]))
        else:
            raise Exception("Unknown potential", a_name)

    return c_nrgs

# todo generalize to N nrg_functionals
def combiner(
    a_nrgs, b_nrgs,
    a_params, b_params,
    a_param_groups, b_param_groups,
    a_conf, b_conf,
    a_masses, b_masses):
    """
    Combine two systems with two distinct parameter sets into one.
    """

    num_a_atoms = len(a_masses)                     # offset by number of atoms in a
    c_masses = np.concatenate([a_masses, b_masses]) # combined masses
    c_conf = np.concatenate([a_conf, b_conf])       # combined geometry
    c_params = np.concatenate([a_params, b_params]) # combined parameters
    c_param_groups = np.concatenate([a_param_groups, b_param_groups]) # combine parameter groups

    assert len(a_nrgs) == len(b_nrgs)

    a_nrgs.sort(key=str)
    b_nrgs.sort(key=str)

    c_nrgs = []
    for a, b in zip(a_nrgs, b_nrgs):
        a_name = a[0]
        a_args = a[1]
        b_name = b[0]
        b_args = b[1]

        assert a_name == b_name
        if a_name == custom_ops.HarmonicBond_f32:
            bond_idxs = np.concatenate([a_args[0], b_args[0] + num_a_atoms], axis=0)
            bond_param_idxs = np.concatenate([a_args[1], b_args[1] + len(a_params)], axis=0)
            c_nrgs.append((custom_ops.HarmonicBond_f32, (bond_idxs, bond_param_idxs)))
        elif a_name == custom_ops.HarmonicAngle_f32:
            angle_idxs = np.concatenate([a_args[0], b_args[0] + num_a_atoms], axis=0)
            angle_param_idxs = np.concatenate([a_args[1], b_args[1] + len(a_params)], axis=0)
            c_nrgs.append((custom_ops.HarmonicAngle_f32, (angle_idxs, angle_param_idxs)))
        elif a_name == custom_ops.PeriodicTorsion_f32:
            torsion_idxs = np.concatenate([a_args[0], b_args[0] + num_a_atoms], axis=0)
            torsion_param_idxs = np.concatenate([a_args[1], b_args[1] + len(a_params)], axis=0)
            c_nrgs.append((custom_ops.PeriodicTorsion_f32, (torsion_idxs, torsion_param_idxs)))
        elif a_name == custom_ops.LennardJones_f32:
            lj_scale_matrix = np.ones(shape=(len(c_masses), len(c_masses)), dtype=np.float64)
            lj_scale_matrix[:num_a_atoms, :num_a_atoms] = a_args[0]
            lj_scale_matrix[num_a_atoms:, num_a_atoms:] = b_args[0]
            lj_param_idxs = np.concatenate([a_args[1], b_args[1] + len(a_params)], axis=0)
            c_nrgs.append((custom_ops.LennardJones_f32, (lj_scale_matrix, lj_param_idxs)))
        elif a_name == custom_ops.Electrostatics_f32:
            es_scale_matrix = np.ones(shape=(len(c_masses), len(c_masses)), dtype=np.float64)
            es_scale_matrix[:num_a_atoms, :num_a_atoms] = a_args[0]
            es_scale_matrix[num_a_atoms:, num_a_atoms:] = b_args[0]
            es_param_idxs = np.concatenate([a_args[1], b_args[1] + len(a_params)], axis=0)
            c_nrgs.append((custom_ops.Electrostatics_f32, (es_scale_matrix, es_param_idxs)))
        else:
            raise Exception("Unknown potential", a_name)

    return c_nrgs, c_params, c_param_groups, c_conf, c_masses

def to_md_units(q):
    return q.value_in_unit_system(simtk.unit.md_unit_system)

def match_bonds(mol, triples):
    """

    """
    bond_idxs = []
    param_idxs = []

    for smirks, k_idx, length_idx in triples:
        bond_idxs.append([bond.src, bond.dst])
        param_idxs.append([k_idx, length_idx])

    return bond_idxs, param_idxs

def simple_charge_model():
    model = {
        "[#1:1]": 0.0157,
        "[#1:1]-[#6X4]": 0.0157,
        "[#1:1]-[#6X4]-[#7,#8,#9,#16,#17,#35]": 0.0157,
        "[#1:1]-[#6X4](-[#7,#8,#9,#16,#17,#35])-[#7,#8,#9,#16,#17,#35]": 0.0157,
        "[#1:1]-[#6X4](-[#7,#8,#9,#16,#17,#35])(-[#7,#8,#9,#16,#17,#35])-[#7,#8,#9,#16,#17,#35]": 0.0157,
        "[#1:1]-[#6X4]~[*+1,*+2]": 0.0157,
        "[#1:1]-[#6X3]": 0.0150,
        "[#1:1]-[#6X3]~[#7,#8,#9,#16,#17,#35]": 0.0150,
        "[#1:1]-[#6X3](~[#7,#8,#9,#16,#17,#35])~[#7,#8,#9,#16,#17,#35]": 0.0150,
        "[#1:1]-[#6X2]": 0.0150,
        "[#1:1]-[#7]": -0.157,
        "[#1:1]-[#8]": -0.2,
        "[#1:1]-[#16]": 0.0157,
        "[#6:1]": 0.3860,
        "[#6X2:1]": 0.3100,
        "[#6X4:1]": 0.3094,
        "[#8:1]": -0.2100,
        "[#8X2H0+0:1]": -0.1700,
        "[#8X2H1+0:1]": -0.2104,
        "[#7:1]": -0.200,
        "[#16:1]": -0.2500,
        "[#15:1]": -0.2000,
        "[#9:1]": -0.361,
        "[#17:1]": -0.265,
        "[#35:1]": -0.320,
        "[#53:1]": 0.40,
        "[#3+1:1]": 0.0279896,
        "[#11+1:1]": 0.0874393,
        "[#19+1:1]": 0.1936829,
        "[#37+1:1]": 0.3278219,
        "[#55+1:1]": 0.4065394,
        "[#9X0-1:1]": 0.0033640,
        "[#17X0-1:1]": 0.0355910,
        "[#35X0-1:1]": 0.0586554,
        "[#53X0-1:1]": 0.0536816
    }
    return model

def parameterize(mol, forcefield):
    """
    Parameterize an RDKit molecule with a given forcefield.
    """
    # do this in a separate pass later
    global_params = []
    global_param_groups = []

    num_atoms = mol.GetNumAtoms()

    def add_param(p, p_group):
        length = len(global_params)
        global_params.append(p)
        global_param_groups.append(p_group)
        return length

    nrg_fns = []

    for handler in forcefield._parameter_handlers.items():

        handler_name, handler_params = handler

        if handler_name == 'Bonds':

            vd = ValenceDict()
            for p in handler_params.parameters:
                k_idx, l_idx = add_param(to_md_units(p.k), 0), add_param(to_md_units(p.length), 1)
                matches = toolkits.RDKitToolkitWrapper._find_smarts_matches(mol, p.smirks)
                # print(p.smirks, matches)

                for m in matches:
                    vd[m] = (k_idx, l_idx)

            bond_idxs = []
            bond_param_idxs = []

            for k, v in vd.items():
                bond_idxs.append(k)
                bond_param_idxs.append(v)

            nrg_fns.append((
                custom_ops.HarmonicBond_f32,
                (
                    np.array(bond_idxs, dtype=np.int32),
                    np.array(bond_param_idxs, dtype=np.int32)
                )
            ))

        elif handler_name == "Angles":

            vd = ValenceDict()
            for p in handler_params.parameters:
                k_idx, a_idx = add_param(to_md_units(p.k), 2), add_param(to_md_units(p.angle), 3)
                matches = toolkits.RDKitToolkitWrapper._find_smarts_matches(mol, p.smirks)
                for m in matches:
                    vd[m] = (k_idx, a_idx)

            angle_idxs = []
            angle_param_idxs = []

            for k, v in vd.items():
                angle_idxs.append(k)
                angle_param_idxs.append(v)

            nrg_fns.append((
                custom_ops.HarmonicAngle_f32,
                (
                    np.array(angle_idxs, dtype=np.int32),
                    np.array(angle_param_idxs, dtype=np.int32)
                )
            ))

        # TODO: ImproperTorsions
        elif handler_name == "ProperTorsions":

            vd = ValenceDict()
            for all_params in handler_params.parameters:

                matches = toolkits.RDKitToolkitWrapper._find_smarts_matches(mol, all_params.smirks)

                all_k_idxs = []
                all_phase_idxs = []
                all_period_idxs = []

                for k, phase, period in zip(all_params.k, all_params.phase, all_params.periodicity):
                    k_idx, phase_idx, period_idx = add_param(to_md_units(k), 4), add_param(to_md_units(phase), 5), add_param(period, 6),
                    all_k_idxs.append(k_idx)
                    all_phase_idxs.append(phase_idx)
                    all_period_idxs.append(period_idx)

                for m in matches:
                    t_p = []
                    for k_idx, phase_idx, period_idx in zip(all_k_idxs, all_phase_idxs, all_period_idxs):
                        t_p.append((k_idx, phase_idx, period_idx))
                    vd[m] = t_p

            torsion_idxs = []
            torsion_param_idxs = []

            for k, vv in vd.items():
                for v in vv:
                    torsion_idxs.append(k)
                    torsion_param_idxs.append(v)

            nrg_fns.append((
                custom_ops.PeriodicTorsion_f32,
                (
                    np.array(torsion_idxs, dtype=np.int32),
                    np.array(torsion_param_idxs, dtype=np.int32)
                )
            ))
        elif handler_name == "vdW":
            # lennardjones
            vd = ValenceDict()
            for param in handler_params.parameters:
                s_idx, e_idx = add_param(to_md_units(param.sigma), 8), add_param(to_md_units(param.epsilon), 9)
                matches = toolkits.RDKitToolkitWrapper._find_smarts_matches(mol, param.smirks)
                for m in matches:
                    vd[m] = (s_idx, e_idx)

                scale_matrix = np.ones(shape=(num_atoms, num_atoms), dtype=np.float64) - np.eye(num_atoms)

                # fully exclude 1-2, 1-3, tbd: 1-4
                for (src, dst) in bond_idxs:
                    scale_matrix[src][dst] = 0
                    scale_matrix[dst][src] = 0

                for (src, _, dst) in angle_idxs:
                    scale_matrix[src][dst] = 0
                    scale_matrix[dst][src] = 0

                for (src, _, _, dst) in torsion_idxs:
                    scale_matrix[src][dst] = 0
                    scale_matrix[dst][src] = 0

            lj_param_idxs = []

            for k, v in vd.items():
                lj_param_idxs.append(v)

            nrg_fns.append((
                custom_ops.LennardJones_f32,
                (
                    np.array(scale_matrix, dtype=np.int32),
                    np.array(lj_param_idxs, dtype=np.int32)
                )
            ))

    # process charges separately
    model = simple_charge_model()
    vd = ValenceDict()
    for smirks, param in model.items():

        c_idx = add_param(param, 7)
        matches = toolkits.RDKitToolkitWrapper._find_smarts_matches(mol, smirks)

        for m in matches:
            vd[m] = c_idx

        scale_matrix = np.ones(shape=(num_atoms, num_atoms), dtype=np.float64) - np.eye(num_atoms)
        # fully exclude 1-2, 1-3, tbd: 1-4
        for (src, dst) in bond_idxs:
            scale_matrix[src][dst] = 0
            scale_matrix[dst][src] = 0

        for (src, _, dst) in angle_idxs:
            scale_matrix[src][dst] = 0
            scale_matrix[dst][src] = 0

        for (src, _, _, dst) in torsion_idxs:
            scale_matrix[src][dst] = 0
            scale_matrix[dst][src] = 0

    charge_param_idxs = []
    for k, v in vd.items():
        charge_param_idxs.append(v)

    nrg_fns.append((
        custom_ops.Electrostatics_f32,
        (
            np.array(scale_matrix, dtype=np.int32),
            np.array(charge_param_idxs, dtype=np.int32)
        )
    ))

    c = mol.GetConformer(0)
    conf = np.array(c.GetPositions(), dtype=np.float64)
    conf = conf/10 # convert to md_units

    masses = []
    for atom in mol.GetAtoms():
        masses.append(atom.GetMass())
    masses = np.array(masses, dtype=np.float64)

    return nrg_fns, np.array(global_params), np.array(global_param_groups, dtype=np.int32), conf, masses


from django.forms import ModelForm, ModelChoiceField, widgets, CharField

from scrape.fantasy_models import DraftPick
from scrape.nfl_models import Player

class DraftPickForm(ModelForm):
    """player_name = CharField(
        widget=widgets.TextInput(attrs={
            'class': 'prompt',
            'placeholder': 'Player'}))"""
    position = CharField(
        required=False,
        initial='All',
        widget=widgets.Select(
            # choices=[Player.FANTASY_POSITIONS]
        ),
    )

    class Meta:
        model = DraftPick
        fields = ['team', 'player', 'keeper', 'pick']

    def __init__(self, season=None, *args, **kwargs):
        super(DraftPickForm, self).__init__(*args, **kwargs)
        if season:
            pick = season.draft_picks()+1
            team = season.get_draft_pick_team(pick)
            self.fields['team'] = ModelChoiceField(
                queryset=season.teams.filter(id=team.id),
                initial=team.name,
                widget=widgets.Select(attrs={
                    'class': 'ui selection dropdown'
                }))
            self.fields['pick'] = CharField(
                widget=widgets.HiddenInput(),
                initial=pick)
            """self.fields['team'] = CharField(
                widget=widgets.TextInput(),
                initial=season.get_draft_pick_team(pick).id)"""
            self.fields['player'] = ModelChoiceField(
                # Only load top ~ top 60 players in each pos
                queryset=season.undrafted_players(),
                widget=widgets.Select(attrs={
                    # 'placeholder': 'Player',
                    'class': 'ui search selection dropdown',
                })
            )


import builder
from kivy.clock import mainthread
from kivy.properties import ObjectProperty
import tasks
from uix.productpopup import ProductPopup
from uix.tab import Tab

builder.load_file('tabs/bar.kv')

class Bar(Tab):
    """
    Bar tab.

    Attr categories_rv ObjectProperty: Categories RecycleView.
    Attr product_rv ObjectProperty: Products RecycleView.
    """

    categories_rv = ObjectProperty(None)
    products_rv = ObjectProperty(None)

    def __init__(self, **kwargs):
        """
        Param **kwargs dict: See parent doc.
        """
        self._categories = []
        self._category = None  # Selected category.
        self._products = []
        super(Bar, self).__init__(**kwargs)

    def on_categories_rv(self, instance, value):
        """
        Called when categories recycle view is added to the widget tree.

        Param instance Bar.
        Param value WeakProxy.
        """
        self.categories_rv.loading = True
        tasks.GetProductCategories(self.on_categories).start()

    @mainthread
    def on_categories(self, success, result):
        """
        Called when categories are received.

        Param success bool.
        Param result [ProductCategory].
        """
        if success:
            self._categories = result
            if self._categories:
                data = []
                index = 0
                for i, category in enumerate(self._categories):
                    if self._category and self._category.id == category.id:
                        index = i
                    data.append({
                        'text': category.title,
                        'on_selected': self.on_category_selected
                    })
                self.categories_rv.data = data
                self.categories_rv.layout_manager.select_node(index)
                self.on_category_selected(index)
            else:
                self._category = None
                self.categories_rv.data = []

    def on_category_selected(self, index):
        """
        Called when a category is selected.

        Param index int.
        """
        self._category = self._categories[index]
        self.products_rv.loading = True
        tasks.GetProducts(self._category, self.on_products).start()

    @mainthread
    def on_products(self, success, result):
        """
        Called when categories are received.

        Param success bool.
        Param result [ProductCategory].
        """
        if success:
            data = []
            self._products = result
            for product in result:
                data.append({'product': product, 'on_info': self.on_info})
            self.products_rv.data = data

    def on_info(self, index):
        """
        Called when an info button is pressed.

        Param int index.
        """
        product = self._products[index]
        popup = ProductPopup(product=product)
        popup.open()


import requests, unittest, json
from MySupport import MySupport
import pdb

class ContainerTests(unittest.TestCase):
    HOSTNAME = "host"
    PORT = 80

    def suite():
        suite = unittest.TestSuite()
        suite.addTest(ContainerTests('test_main'))
        return suite

    def test_main(self):
        url =  MySupport.url(self.HOSTNAME, self.PORT, "/launch")
        # launch first
        first_config = MySupport.get_dict("first_config")
        first_launch = MySupport.get_dict("first_launch")
        response = requests.post(url, json=first_launch)

        # launch second
        second_config = MySupport.get_dict("second_config")
        second_launch = MySupport.get_dict("second_launch")
        response = requests.post(url, json=second_launch)

        # launch third
        third_config = MySupport.get_dict("third_config")
        third_launch = MySupport.get_dict("third_launch")
        response = requests.post(url, json=third_launch)

        # check first
        url = MySupport.url("localhost", "10000", "/potato/potato.html")
        response = requests.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(response.content)
        self.assertEqual(response.content.decode("utf-8").strip(), "ich bin ein kartoffel")

        # check second
        url = MySupport.url("localhost", "20000", "/cabbage/cabbage.html")
        response = requests.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(response.content)
        self.assertEqual(response.content.decode("utf-8").strip(), "my cabbages!")

        url = MySupport.url("localhost", "20000", "/cabbage/potato/potato.html")
        response = requests.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(response.content)
        self.assertEqual(response.content.decode("utf-8").strip(), "ich bin ein kartoffel")

        # check third
        url = MySupport.url("localhost", "30000", "/potato/potato.html")
        response = requests.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(response.content)
        self.assertEqual(response.content.decode("utf-8").strip(), "ich bin ein kartoffel")

        url = MySupport.url("localhost", "30000", "/tomato/tomato.html")
        response = requests.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(response.content)
        self.assertEqual(response.content.decode("utf-8").strip(), "heinzzz")

        url = MySupport.url("localhost", "30000", "/tomato/cabbage/cabbage.html")
        response = requests.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(response.content)
        self.assertEqual(response.content.decode("utf-8").strip(), "my cabbages!")


from datetime import datetime, date, timedelta
from collections import Counter
from prettytable import PrettyTable

def get_last_2_week(start, end):
    audit_list = []
    for add_day in range(start, end):
        # print(add_day)
        yesterday = (date.today() + timedelta(days=add_day)).strftime("%Y-%m-%d")  # 昨天日期
        # print(yesterday)
        filename = "audit_douyin/audit_douyin_cid.txt." + yesterday
        # filename="/Users/ludanqing/python/qly_project/pyecharts/pyecharts/app/audit_douyin/audit_douyin_cid.txt." + yesterday
        try:
            for line in open(filename):
                user = line.split("\t")[1].split("=")[1]
                audit_list.append(user)
        except:
            pass
    return audit_list
        # get_file_content("audit_douyin/audit_douyin_cid.txt." + yesterday)

def get_current_week():
    monday, sunday = date.today(), date.today()
    one_day = timedelta(days=1)
    while monday.weekday() != 0:
        monday -= one_day
    while sunday.weekday() != 6:
        sunday += one_day

    return monday

def get_stat_content():
    last_week_start = (date.today() - get_current_week()).days + 7
    last_week_end = (date.today() - get_current_week()).days

    today = get_last_2_week(0, 1)
    # print("=======")
    yesterday = get_last_2_week(-1, 0)
    lastyesterday = get_last_2_week(-2, -1)
    lastweek = get_last_2_week(-last_week_start, -last_week_end)
    thisweek = get_last_2_week(-last_week_end, 1)

    today_list = Counter(today).items()
    yesterday_list = Counter(yesterday).items()
    lastyesterday_list = Counter(lastyesterday).items()
    lastweek_list = Counter(lastweek).items()
    thisweek_list = Counter(thisweek).items()

    user_tree = {}
    mset = set()
    for ele, v in yesterday_list:
        mset.add(ele)
    for ele, v in lastyesterday_list:
        mset.add(ele)
    for ele, v in thisweek_list:
        mset.add(ele)
    for ele, v in lastweek_list:
        mset.add(ele)
    for ele, v in today_list:
        mset.add(ele)
    for user in mset:
        user_tree[str(user)] = []
        yesterday_flag = False
        lastyesterday_flag = False
        lastweek_flag = False
        thisweek_flag = False
        today_flag = False
        for ele, v in yesterday_list:
            if str(ele) == str(user):
                yesterday_flag = True
                user_tree[str(user)].append(v)
        if not yesterday_flag:
            user_tree[str(user)].append(0)

        for ele, v in lastyesterday_list:
            if str(ele) == str(user):
                lastyesterday_flag = True
                user_tree[str(user)].append(v)
        if not lastyesterday_flag:
            user_tree[str(user)].append(0)

        for ele, v in thisweek_list:
            if str(ele) == str(user):
                thisweek_flag = True
                user_tree[str(user)].append(v)
        if not thisweek_flag:
            user_tree[str(user)].append(0)

        for ele, v in lastweek_list:
            if str(ele) == str(user):
                lastweek_flag = True
                user_tree[str(user)].append(v)
        if not lastweek_flag:
            user_tree[str(user)].append(0)

        for ele, v in today_list:
            if str(ele) == str(user):
                today_flag = True
                user_tree[str(user)].append(v)
        if not today_flag:
            user_tree[str(user)].append(0)

    x = PrettyTable(["打标人",  "昨日", "前日", "本周", "上周", "今日"])
    v0_total = 0
    v1_total = 0
    v2_total = 0
    v3_total = 0
    v4_total = 0
    for k, v in user_tree.items():
        x.add_row([k, v[0], v[1], v[2], v[3], v[4]])
        v0_total += v[0]
        v1_total += v[1]
        v2_total += v[2]
        v3_total += v[3]
        v4_total += v[4]
    x.add_row(['总计', v0_total, v1_total, v2_total, v3_total, v4_total])
    # f = codecs.open('daily_set.txt', 'a+', 'utf-8')
    # f.write(str(x))
    # f.close()
    # print(set)
    # print(get_last_2_week(-1,0))
    return str(x)

if __name__ == '__main__':

    print(get_stat_content())


import numpy as np
import pandas as pd
import matplotlib.figure
import matplotlib.pyplot as plt
from tkinter import Tk, Frame, Label, Text, Button, END
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

pos = 0
b = 0
mistakes = 0
correct_presses = 0
typing_speed = 0
duration = 0
start_time = 0
end_time = 0
minutes = 0
seconds = 0

def handle_stats():
    global typing_speed, correct_presses, mistakes
    entries = correct_presses + mistakes
    typing_speed = (entries / 5) / 1
    duration_label = Label(
        window,
        text=get_duration(),
        font=("Arial ", 16)
    )
    typing_speed_label = Label(
        window,
        text="Typing speed: " + str(int(typing_speed)) + " WPM",
        font=("Arial ", 16)
    )
    duration_label.pack()
    typing_speed_label.pack()

def draw_pie_chart():
    global mistakes, correct_presses
    labels = "Mistakes", "Correct presses"
    sizes = [mistakes, correct_presses]
    colors = ['red', 'green']
    fig1, ax1 = plt.subplots()
    ax1.pie(sizes, labels=labels, autopct='%1.1f%%',
            startangle=90, colors=colors)
    ax1.axis('equal')
    fig1.set_size_inches(5, 3)
    pie_canvas = FigureCanvasTkAgg(fig1, master=window)
    pie_canvas.get_tk_widget().pack()
    pie_canvas.draw()

def get_duration():
    global end_time, start_time, duration, minutes, seconds
    end_time = pd.datetime.now()
    duration = end_time - start_time
    hours, remainder = divmod(duration.seconds, 3600)
    minutes, seconds = divmod(remainder, 60)
    return "Duration: " + str(minutes) + "min" + " " + str(seconds) + "sec"

def on_key_press(e):
    if (e.keycode != 16):
        global pos, text_widget, b, mistakes, correct_presses, typing_speed
        if (string[pos] != e.char):
            text_widget.tag_add("wrong", str(1) + "." + str(b))
            mistakes += 1
        if (string[pos] == e.char):
            text_widget.tag_add("correct", str(1) + "." + str(b))
            correct_presses += 1
        b += 1
        pos += 1
        if (pos == len(string)):
            draw_pie_chart()
            handle_stats()

def start():
    global start_time
    message.pack()
    text_widget.pack()
    start_btn.destroy()
    start_time = pd.datetime.now()

file = open('text.txt', 'r')
text = file.readlines()
file.close()

string = ""
for i in text:
    for j in range(len(i)):
        string = string + i[j]

window = Tk()
window.wm_title("Keyboard Trainer")

text_widget = Text(window, height=10, width=100,
                   padx=20, pady=20, font=("Arial ", 16))
text_widget.insert(END, string)
text_widget.configure(state="disabled")
text_widget.tag_config("correct", background="green", foreground="white")
text_widget.tag_config("wrong", background="red", foreground="white")

text_widget.bind("<KeyPress>", on_key_press)
text_widget.focus()

message = Label(window,
                text="Start typing",
                font=("Arial ", 24))

start_btn = Button(
    window,
    text="Start",
    width="20",
    height="2",
    padx="1",
    pady="1",
    command=start)

start_btn.pack()

window.mainloop()


import requests
from bs4 import BeautifulSoup

print('This is just a test and convenience script so only use the XXXX XXX format, anything else will just crash it.')

while True:
    print('====================')
    reg = input('Enter car reg : ')
    url = 'https://www.mycarcheck.com/check/?reg_no='+reg[0:4]+'+'+reg[-3:]
    response = requests.get(url)
    soup = BeautifulSoup(response.content, "html.parser")

    make = soup.find('h3', attrs={'class': 'make-model'}).text.strip()
    body = soup.findAll('td', attrs={'class': 'col-xs-9'})[0].text.strip()
    colour = soup.findAll('td', attrs={'class': 'col-xs-9'})[1].text.strip()

    print('Make:', make)
    print('Body:', body)
    print('Colour:', colour)


try:
    from . import ZDCode

except ImportError:
    from zdcode import ZDCode

import zipfile
import tempfile
import pathlib
import subprocess
import shutil

from collections import deque

class BundleDependencyError(Exception):
    pass

class Bundle:
    def __init__(self, *mods, error_handler=None):
        self.mods = list(mods)
        self.error_handler = error_handler

    def _compile_task(self, code, zdc, error_handler, preproc_defs):
        def compile_mod_zdcode():
            with zdc.open() as zdc_fp:
                return code.add(zdc_fp.read(), zdc.name, zdc.parent, error_handler, preproc_defs=preproc_defs)

        return compile_mod_zdcode

    def bundle(self, out_asset_file=None, out_code_file=None, out_dec_file=None, error_handler=None, preproc_defs=()):
        code = ZDCode()
        build_tasks = []
        deps = list(self.mods)
        bundled = set()

        if not (hasattr(preproc_defs, '__getitem__') and hasattr(preproc_defs, '__setitem__')):
            preproc_defs = dict(preproc_defs)

        # Scan input
        with tempfile.TemporaryDirectory() as destname:
            dest = pathlib.Path(destname)

            while deps:
                mod = deps.pop()

                if mod in bundled:
                    continue

                bundled.add(mod)

                mod_path = pathlib.Path(mod)

                if mod_path.is_dir():
                    mod_dest = dest / mod_path.resolve().stem

                    for fn in mod_path.rglob('*'):
                        if fn.is_file():
                            dest_fpath = (mod_dest / fn.relative_to(mod_path))

                            dest_fpath.parent.mkdir(parents=True, exist_ok=True)
                            shutil.copyfile(fn, dest_fpath)

                            if fn.stem.split('.')[0].upper() == 'ZDCODE':
                                build_tasks.append(self._compile_task(code, dest_fpath, error_handler or self.error_handler, preproc_defs))

                    indx_path = mod_dest / 'DEPINDEX'

                    if indx_path.is_file():
                        lines = indx_path.read().splitlines()

                        for l in lines:
                            l = l.strip()

                            if l:
                                dep_path = mod_path.parent / l

                                if not dep_path.is_file():
                                    dep_path = pathlib.Path.cwd() / l

                                if not dep_path.is_file():
                                    raise BundleDependencyError('The file {} depends on {}, which does not exist, neither in the dependent\'s dir, nor in the working one!')

                                deps.append(dep_path)

                elif mod_path.suffix.upper() in ('.PK3', '.PKZ'):
                    mod_dest = dest / mod_path.stem
                    mod_dest.mkdir()

                    with zipfile.ZipFile(mod) as zmod:
                        zmod.extractall(mod_dest)

                        for zdc in mod_dest.glob(''.join('[{}{}]'.format(c.lower(), c) for c in 'ZDCODE') + '*'):
                            build_tasks.append(self._compile_task(code, zdc, error_handler or self.error_handler, preproc_defs))

                        indx_path = mod_dest / 'DEPINDEX'

                        if indx_path.is_file():
                            lines = indx_path.read().splitlines()

                            for l in lines:
                                l = l.strip()

                                if l:
                                    dep_path = mod_path.parent / l

                                    if not dep_path.is_file():
                                        dep_path = pathlib.Path.cwd() / l

                                    if not dep_path.is_file():
                                        raise BundleDependencyError('The file {} depends on {}, which does not exist, neither in the dependent\'s dir, nor in the working one!')

                                    deps.append(dep_path)

                elif mod_path.suffix.upper() == '.ZC2':
                    build_tasks.append(self._compile_task(code, mod_path, error_handler or self.error_handler, preproc_defs))

            # Build ZDCode
            for task in build_tasks:
                if not task():
                    return 1, 'Errors were found during the compilation of the ZDCode lumps!'

        # Bundle assets to zip (pk3)
        if out_asset_file:
            with zipfile.ZipFile(out_asset_file, 'w') as asset_bundle:
                for asset in bundled:
                    asset_path = pathlib.Path(asset)

                    if asset_path.is_dir():
                        for ipath in asset_path.rglob('*'):
                            if ipath.stem.split('.')[0].upper() != 'ZDCODE' and ipath.suffix.upper() != '.ZC2' and ipath.is_file():
                                asset_bundle.write(ipath, ipath.relative_to(asset_path))

                    elif asset_path.suffix.upper() == '.WAD':
                        # Just add wads instead of parsing their content
                        asset_bundle.write(asset_path, asset_path.name)

                    elif asset_path.suffix.upper() in ('.PK3', '.PKZ'):
                        with zipfile.ZipFile(asset) as asset_zip:
                            for info in asset_zip:
                                ipath = pathlib.Path(info.filename)

                                if ipath.stem.split('.')[0].upper() != 'ZDCODE' and ipath.suffix.upper() != '.ZC2':
                                    with asset_zip.open(info) as info_fp:
                                        # check if file already exists in target;
                                        # if so, add extension

                                        # (ignore folders)

                                        p = zipfile.Path(asset_bundle, info.filename)

                                        if p.exists() and p.is_file():
                                            num = 1
                                            p = p.with_suffix(p.suffix + '.' + str(num))

                                            # keep checking until we find a vacant name
                                            while True:
                                                p = p.with_suffix('.' + str(num))

                                                if not (p.exists() and p.is_file()): break
                                                num += 1 # .num exists too, increment num

                                        asset_bundle.writestr(p, info_fp.read())

        # Write compiled output to zip (pk3)
        if out_code_file:
            with zipfile.ZipFile(out_code_file, 'w') as code_bundle:
                code_bundle.writestr('DECORATE', code.decorate(), zipfile.ZIP_DEFLATED)

        # Write compiled output to plain text file
        if out_dec_file:
            out_dec_file.write(code.decorate())

        return 0, 'Bundled successfully!'


#===============================================================================
# Corsi Christophe
#
# This is a subclass of the MainWindow class
#
# The heritage works like the following
#
# MainWindow => MainAcausalGraph=>MainAcausalTree=>MainAcausalTable=>MainAcausalDocumentation=>
# MainCausalGraph=>MainCausalTree=>MainCausalTable=>MainCausalDocumentation=>MainMainTools=> MainInterface
#
# Only the last item of this heritage should be invoked (MainInterface)
#===============================================================================
from copy import copy
import os
import pickle
import sys
import threading

from PyQt5.Qt import QCursor

from CompositionWindow import CompositionWindow
from EditWindow import EditWindow
from ErrorWindow import ErrorWindow
from GraphicalManager import LinkItem, BlockItem
from LinkWindow import LinkWindow
from MainWindow import MainWindow
from SaveWindow import SaveWindow
import Config
import XmlOperator

class MainAcausalGraph( MainWindow ):

    def addAcausalBlockInGraph( self, inputBlock, posX = 0, posY = 0, localMousePosition = False ):
        '''
        Add a block in the graphic view, containing the title of the block given as parameter
        (o) for optionnal
        @param inputBlock: Block This is the Block to add to the graphical view
        @param: posX: int (o) integer with the X position
        @param: posY: int (o) integer with the Y position
        @param: localMousePosition: Boolean (o) If true : posX and posY in global coordinates, else in scene coordinates
        '''
        if type( inputBlock ) != BlockItem:
            if localMousePosition == False:
                # If parameters are given, the function adds the item to the coordinates
                self.aScene.addItem( BlockItem( self.aScene, copy( inputBlock.block ) , posX, posY ) )
            else:
                # If no information is give, the function adds the item to the cursor position
                # pos has its origin on the top left of the screen
                pos = QCursor.pos()
                # pos1 has its origin on the top left of the widget
                pos1 = self.ui.aCompositionView.mapFromGlobal( pos )
                # pos2 has its center in the middle of the item's coordinate system
                pos2 = self.ui.aCompositionView.mapToScene( pos1 )
                newBlock = BlockItem( self.aScene, inputBlock.block, pos2.x(), pos2.y() )
                self.aScene.addItem( newBlock )

    def editFunction( self ):
        '''
        This functions allows a block in the graphical editor to have its name edited
        @param None:
        @return: None
        '''
        if len( self.aScene.selectedItems() ) != 1:
            return
            # we only act if one item is selected

        for item in self.aScene.selectedItems():
            if type( item ) == BlockItem:
                # if it's a block we open the block name editor
                self.aEditWindow = EditWindow( item, self.aScene )
                self.aEditWindow.show()

            elif type( item ) == LinkItem:
                # if it's a link we open the link editor
                self.aLinkWindow = LinkWindow( item )

    def aDeleteFunction( self ):
        '''
        This function deletes the selected items on the acausal graphic view
        '''
        garbage = list()
        for i in self.aScene.selectedItems():
            # we remove the item from the graphical scene
            # different behaviour considering the type of the item
            try:
                if type( i ) == BlockItem:
                    self.aScene.removeItem( i )
            except:
                print( 'LOG: MainAcausalGraph.aDeleteFunction -> ', sys.exc_info()[0], sys.exc_info()[1] )
            if type( i ) == BlockItem:
                garbage.append( i )
                for linkItem in i.linksDict.values():
                    garbage.append( linkItem )
            elif type( i ) == LinkItem:
                i.GBlock1.delLink( i.GBlock2 )
                i.GBlock2.delLink( i.GBlock1 )
        # we now clean the garbage
        garbage = set( garbage )
        for j in garbage:
            if type( j ) == LinkItem:
                try:
                    j.GBlock1.delLink( linkItem.GBlock2 )
                except:
                    if Config.isDebug:
                        print( "first block not possible to reach" )
                        print( "item one has ", j.GBlock1.linksDict )
                        print( "item two has ", j.GBlock2.linksDict )
                try:
                    j.GBlock2.delLink( linkItem.GBlock1 )

                except:
                    if Config.isDebug:
                        print( 'second item not possible to reach' )
                        print( "item two has ", j.GBlock2.linksDict )
                        print( "item one has ", j.GBlock1.linksDict )
                self.aScene.removeItem( j )
            del j

    def aLink( self ):

        '''
        This function links two blocks togeter
        '''
        # we make a new thread
        if len( self.aScene.selectedItems() ) != 1:
            return
        # if only one item has been selected
        # this pattern is used to reduce the complexity of the code
        for item in self.aScene.selectedItems():
            # we will have only one item
            self.AFirstBlockToLink = item
        self.aLinkingMode = True
        self.aLinkThread = threading.Thread( target = self.aLinkingProcess )

    def aLinkingProcess( self ):
        '''
        Function that is stocked in the thread and called when the second block is selected
        '''
        # TODO: add a timer for timeout
        # timeout=Config.defaultTimeOut
        # if timeout is not None:
        #    print("begin timer")
        #    QtCore.QTimer.singleShot(timeout, self.aLinkThread.stop()   )
        if len( self.aScene.selectedItems() ) != 1:
            return
        # we will accept only one item selected
        if self.linkTarget == None:
            return
        line = LinkItem( self.aScene, self.AFirstBlockToLink, self.linkTarget )
        self.aScene.addItem( line )
        self.listLinkItems.append( line )
        self.aLinkingMode = False
        self.lastLinkItem = line

    def callLinkWindow( self ):
        '''
        Function that calls the opening of the link window
        '''
        self.aLinkWindow = LinkWindow( self.lastLinkItem )

    def saveBlock( self ):
        '''
        Function used to save the current block into the tree and write it into the xml files
        '''
        for item in self.aScene.selectedItems():
            if type( item ) == BlockItem:
                onlyItemToSave = item
        self.saveBlockWindow = SaveWindow( onlyItemToSave )

    def callDecomposition( self ):
        '''
        Function only used to be linked with the button
        '''
        self.decomposeABlock()
        return

    def decomposeABlock( self, blockItem = None ):
        '''
        Function that handles the historic view of any compound object
        Opens an error message if it's a primitive block
        This function is based on the presence of a dumping file
        '''
        listDir = os.listdir( Config.defaultDumpingFolder )

        if blockItem == None:
            itemSelected = self.aScene.selectedItems()[0]  # we only take one item
        else:
            itemSelected = blockItem

        if itemSelected == None:
            return

        if type( itemSelected.block.id ) == type( [] ):
            identificator = itemSelected.block.id[0]
        else:
            identificator = itemSelected.block.id
        for i in listDir:
            # looking at the files in the dumping folder
            if identificator == str( i ):
                # we found the dumping file
                unserializer_in = open( Config.defaultDumpedFileName.format( str( i ) ), "rb" )  # rb for 'readingbytes'
                try:

                    decompositionScene = pickle.load( unserializer_in )
                except:
                    print( 'LOG: MainAcausalGraph.decomposeABlock->', sys.exc_info()[1] )
                    return
                unserializer_in.close()
                if type( self.aScene.selectedItems()[0] ) == BlockItem:
                    # This list allows us to have multiple window, which is required in deep structure analysis
                    self.aScene.selectedItems()[0].subSceneComposition = decompositionScene  # It wont erase the previous one if it already has one stocked
                    composition = self.aScene.selectedItems()[0].subSceneComposition
                    decomposedItem = self.aScene.selectedItems()[0]
                    parentItem = self.aScene.selectedItems()[0]
                    # self.decompositionWindowList.append( CompositionWindow( self.aScene.selectedItems()[0].subSceneComposition, self.aScene.selectedItems()[0], self.aScene.selectedItems()[0] ) )
                    self.decompositionWindowList.append( CompositionWindow( composition , decomposedItem, parentItem ) )
                    self.compositionFirstInitialization = False
                return
        self.errorWindow = ErrorWindow( Config.ErrorMessagePrimitiveObject )
        return

    def fusionBlocks( self ):
        '''
        Function called when the regroup button is clicked
        It takes the current selected items, delete them and replace it with a new block made of their unification.
        The equations are merged. If two equations are similar, only one will be kept
        '''
        resultItem = None
        for item in self.aScene.selectedItems():
            if type( item ) == BlockItem:
                if resultItem == None:
                    resultItem = item
                else:
                    resultItem = item.fusionWith( resultItem )
        self.aScene.addItem( resultItem )
        self.aDeleteFunction()
        return

    def mergeBlocks( self ):
        '''
        Takes the currently selected items and merge them into one super block, with double equations if necessary.
        '''
        try:
            resultItem = None
            otherItems = []
            firstItem = None
            links = []
            for item in self.aScene.selectedItems():
                if type( item ) == BlockItem:
                    if firstItem == None:
                        firstItem = item
                    else:
                        otherItems.append( item.block )
                elif type( item ) == LinkItem:
                    links.append( item )

            try:
                resultBlock = firstItem.block.sum( otherItems )
                resultBlock.mergeComposingBlocksLinks( links )
                resultBlock.refreshEquationDict()
                resultItem = BlockItem( self.aScene, resultBlock )
            except:
                print( 'LOG: MainAcausalGraph.mergeBlocks-> Part 1 ', sys.exc_info()[0], sys.exc_info()[1] )
                return
            if resultItem == None:
                return
            resultItem.block.documentationFileName = Config.documentationNameStructure.format( resultItem.block.name )
            resultItem.block.refreshID()
            # we write the documentation of the file
            write = XmlOperator.XmlWriter( resultItem )
            write.createDocumentation( Config.documentationNameStructure.format( resultItem.block.name ) )
            # We dump the whole scene into a file
            # itemsToSerialize = [ x for x in self.aScene.selectedItems() if ( type( x ) == BlockItem or type( x ) == LinkItem ) ]
            try:
                serialized_in = self.aScene.selectedItems()
                # resultItem.block.id is a list containing one value
                if type( resultItem.block.id ) == type( [] ):
                    dumpingName = resultItem.block.id[0]
                elif type( resultItem.block.id ) != type ( [] ) :
                    dumpingName = resultItem.block.id
                # starting serialization
                serialized_out = open( Config.defaultDumpedFileName.format( dumpingName ) , "wb" )  # wb for writting bytes
                pickle.dump( serialized_in, serialized_out )
                serialized_out.close()
            except:
                print( 'LOG: MainAcausalGraph.mergeBlocks -> serialization failed ->  ', sys.exc_info()[0], sys.exc_info()[1] )
            # we finally delete the other old items
            self.aScene.addItem( resultItem )
            self.aDeleteFunction()
            return
        except:
            print( 'LOG: MainAcausalGraph.mergeBlocks-> Part 2 ', sys.exc_info()[0], sys.exc_info()[1] )