#!/usr/bin/env python
import argparse
import re
import os, sys
import pandas as pd
import collections
import csv
from collections import Counter, OrderedDict
import itertools as iter
import numpy as np

class OrderedCounter(Counter, OrderedDict):
    pass

def df_to_dictionary(args, filepath, meta_dict):
    tmp_dict = {}
    table_df = pd.read_csv(filepath, sep="\t", comment="#")

    for row in table_df.itertuples(index=False, name='Pandas'):
        gene_type = getattr(row, "Type")
        unique_id = getattr(row, "Identifier")
        genome = getattr(row, "Genome")
        start = int(getattr(row, "Start"))
        stop = int(getattr(row, "Stop"))
        strand = getattr(row, "Strand")
        gene_name = getattr(row, "locus_tag")
        aa_count = int(getattr(row, "codon_count"))
        peak_height = float(getattr(row, "peak_height"))
        start_codon = getattr(row, "start_codon")
        stop_codon = getattr(row, "stop_codon")
        nt_upstream = getattr(row, "_11")
        nt_seq = getattr(row, "nt_seq")
        aa_seq = getattr(row, "aa_seq")
        relative_density = float(getattr(row, "relative_density"))
        fiveprime = getattr(row, "_15")
        threeprime = getattr(row, "_16")

        tmp_dict[unique_id] = (peak_height, relative_density)
        if unique_id not in meta_dict.keys():
            meta_dict[unique_id] = (gene_name, gene_type, aa_count, start_codon, stop_codon, nt_upstream, nt_seq, aa_seq, fiveprime, threeprime)

    return tmp_dict, meta_dict

#
def parse_input(args):
    """
    Check if each RIBO file has a TIS counterpart and create two lists of dictionaries as well as a meta dictionary
    """

    constrasts = [constrast.split("_") for constrast in args.contrast_list]

    table_path = os.path.dirname(os.path.abspath(args.table_list[0]))
    suffix = "." + ".".join(os.path.splitext(os.path.basename(args.table_list[0]))[0].split(".")[1:]) + ".csv"
    wildcards = [os.path.splitext(os.path.basename(x))[0].split(".")[0] for x in sorted(args.table_list)]

    meta_dict = {}
    wildcard_dicts = {}
    for card in wildcards:
        tmp_dict, meta_dict = df_to_dictionary(args, os.path.join(table_path, card + suffix), meta_dict)
        wildcard_dicts[card] = tmp_dict

    return meta_dict, wildcard_dicts, wildcards, constrasts

def calculate_fold_changes(unique_id, wildcard_dicts, contrasts):
    """
    calculate log2FC of the different samples
    """
    data = []

    for key, wild_dict in wildcard_dicts.items():
        if unique_id in wild_dict:
            peak_height = wild_dict[unique_id][0]
        else:
            peak_height = ""

        data.append(peak_height)

    for contrast in contrasts:
        con1, con2 = contrast
        if unique_id in wildcard_dicts[con1]:
            con1_height = wildcard_dicts[con1][unique_id][0]
        else:
            con1_height = ""

        if unique_id in wildcard_dicts[con2]:
            con2_height = wildcard_dicts[con2][unique_id][0]
        else:
            con2_height = ""

        if con1_height != "" and con2_height != "":
            fc = np.log2(con2_height / con1_height)
        else:
            fc = ""

        data.append(fc)

    return data

def excel_writer(args, data_frames):
    """
    create an excel sheet out of a dictionary of data_frames
    correct the width of each column
    """
    header_only =  ["Aminoacid_seq", "Nucleotide_seq"]
    writer = pd.ExcelWriter(args.out_xlsx, engine='xlsxwriter')
    for sheetname, df in data_frames.items():
        df.to_excel(writer, sheet_name=sheetname, index=False)
        worksheet = writer.sheets[sheetname]
        for idx, col in enumerate(df):
            series = df[col]
            if col in header_only:
                max_len = len(str(series.name)) + 2
            else:
                max_len = max(( series.astype(str).str.len().max(), len(str(series.name)) )) + 1
            print("Sheet: %s | col: %s | max_len: %s" % (sheetname, col, max_len))
            worksheet.set_column(idx, idx, max_len)
    writer.save()

def create_wildcard_header(wildcards, contrasts):
    """
    create the header based on the given wildcards
    """
    header = ["%s_peak_height" % wildcard for wildcard in wildcards]
    header.extend(["%s_%s_log2FC" % (contrast[0], contrast[1]) for contrast in contrasts])
    return header

def get_relative_density(unique_id, wildcard_dicts):
    """
    extract relative_density
    """
    data = []
    for key, wild_dict in wildcard_dicts.items():
        if unique_id in wild_dict:
            rel_density = wild_dict[unique_id][1]
        else:
            rel_density = ""

        data.extend([rel_density])

    return data

def create_excel_file(args):
    """
    generate an overview excel file with the combined results
    """
    meta_dict, wildcard_dicts, wildcards, contrasts = parse_input(args)

    header = ["Identifier", "Genome", "Start", "Stop", "Strand", "Locus_tag", "Gene_type", "Codon_count", "Start_codon", "Stop_codon"] +\
             create_wildcard_header(wildcards, contrasts) + ["15nt upstream", "Nucleotide_seq", "Aminoacid_seq"] +\
             ["%s_relative_density" % (sample) for sample in wildcards] + ["5'distance", "3'distance"]

    name_list = ["s%s" % str(x) for x in range(len(header))]
    nTuple = collections.namedtuple('Pandas', name_list)

    all_sheet = []
    for unique_id in meta_dict.keys():
        meta_info = meta_dict[unique_id]

        chrom, mid, strand = unique_id.split(":")
        start, stop = mid.split("-")

        result = [unique_id, chrom, int(start), int(stop), strand, meta_info[0], meta_info[1], meta_info[2], meta_info[3], meta_info[4]] +\
                 calculate_fold_changes(unique_id, wildcard_dicts, contrasts) + [meta_info[5], meta_info[6], meta_info[7]] +\
                 get_relative_density(unique_id, wildcard_dicts) + [meta_info[8], meta_info[9]]

        all_sheet.append(nTuple(*result))
    all_df = pd.DataFrame.from_records(all_sheet, columns=[header[x] for x in range(len(header))])
    all_df = all_df.astype({"Start" : "int32", "Stop" : "int32"})
    all_df = all_df.sort_values(by=["Genome", "Start", "Stop"])
    dataframe_dict = { "all" : all_df }
    excel_writer(args, dataframe_dict)

def main():
    # store commandline args
    parser = argparse.ArgumentParser(description='create an overview table containing merged RET results.')
    parser.add_argument("-t", "--tables", nargs="+", dest="table_list", required=True, help= "list of input tables.")
    parser.add_argument("--contrasts", nargs="+", dest="contrast_list", required=True, help= "list of contrasts used for log2FC.")
    parser.add_argument("-x", "--xlsx", action="store", dest="out_xlsx", required=True, help= "Output excel file.")

    args = parser.parse_args()

    create_excel_file(args)

if __name__ == '__main__':
    main()


import argparse
import template
import torch
import serverUtility
import data
import model
import loss
from trainer import Trainer

parser = argparse.ArgumentParser(description='EDSR and MDSR')

parser.add_argument('--alpha', type=float, default= -1 ,
                    help='controller')

parser.add_argument('--debug', action='store_true',
                    help='Enables debug mode')
parser.add_argument('--template', default='.',
                    help='You can set various templates in option.py')

# Hardware specifications
parser.add_argument('--n_threads', type=int, default=6,
                    help='number of threads for data loading')
parser.add_argument('--cpu', action='store_true',
                    help='use cpu only')
parser.add_argument('--n_GPUs', type=int, default=1,
                    help='number of GPUs')
parser.add_argument('--seed', type=int, default=1,
                    help='random seed')

# Data specifications
parser.add_argument('--dir_data', type=str, default='/data2/',
                    help='dataset directory')
parser.add_argument('--dir_demo', type=str, default='../test',
                    help='demo image directory')
parser.add_argument('--data_train', type=str, default='DIV2K',
                    help='train dataset name')
parser.add_argument('--data_test', type=str, default='DIV2K',
                    help='test dataset name')
parser.add_argument('--data_range', type=str, default='1-800/801-810',
                    help='train/test data range')
parser.add_argument('--ext', type=str, default='sep',
                    help='dataset file extension')
parser.add_argument('--scale', type=str, default='4',
                    help='super resolution scale')
parser.add_argument('--patch_size', type=int, default=192,
                    help='output patch size')
parser.add_argument('--rgb_range', type=int, default=255,
                    help='maximum value of RGB')
parser.add_argument('--n_colors', type=int, default=3,
                    help='number of color channels to use')
parser.add_argument('--chop', action='store_true',
                    help='enable memory-efficient forward')
parser.add_argument('--no_augment', action='store_true',
                    help='do not use data augmentation')

# Model specifications
parser.add_argument('--model', default='EDSR',
                    help='model name')

parser.add_argument('--act', type=str, default='relu',
                    help='activation function')
parser.add_argument('--pre_train', type=str, default='',
                    help='pre-trained model directory')
parser.add_argument('--extend', type=str, default='.',
                    help='pre-trained model directory')
parser.add_argument('--n_resblocks', type=int, default=16,
                    help='number of residual blocks')
parser.add_argument('--n_feats', type=int, default=64,
                    help='number of feature maps')
parser.add_argument('--res_scale', type=float, default=1,
                    help='residual scaling')
parser.add_argument('--shift_mean', default=True,
                    help='subtract pixel mean from the input')
parser.add_argument('--dilation', action='store_true',
                    help='use dilated convolution')
parser.add_argument('--precision', type=str, default='single',
                    choices=('single', 'half'),
                    help='FP precision for test (single | half)')

# Option for Residual dense network (RDN)
parser.add_argument('--G0', type=int, default=64,
                    help='default number of filters. (Use in RDN)')
parser.add_argument('--RDNkSize', type=int, default=3,
                    help='default kernel size. (Use in RDN)')
parser.add_argument('--RDNconfig', type=str, default='B',
                    help='parameters config of RDN. (Use in RDN)')

# Option for Residual channel attention network (RCAN)
parser.add_argument('--n_resgroups', type=int, default=10,
                    help='number of residual groups')
parser.add_argument('--reduction', type=int, default=16,
                    help='number of feature maps reduction')

# Training specifications
parser.add_argument('--reset', action='store_true',
                    help='reset the training')
parser.add_argument('--test_every', type=int, default=1000,
                    help='do test per every N batches')
parser.add_argument('--epochs', type=int, default=300,
                    help='number of epochs to train')
parser.add_argument('--batch_size', type=int, default=1,
                    help='input batch size for training')
parser.add_argument('--split_batch', type=int, default=1,
                    help='split the batch into smaller chunks')
parser.add_argument('--self_ensemble', action='store_true',
                    help='use self-ensemble method for test')
parser.add_argument('--test_only', action='store_true',
                    help='set this option to test the model')
parser.add_argument('--gan_k', type=int, default=1,
                    help='k value for adversarial loss')

# Optimization specifications
parser.add_argument('--lr', type=float, default=1e-4,
                    help='learning rate')
parser.add_argument('--decay', type=str, default='200',
                    help='learning rate decay type')
parser.add_argument('--gamma', type=float, default=0.5,
                    help='learning rate decay factor for step decay')
parser.add_argument('--optimizer', default='ADAM',
                    choices=('SGD', 'ADAM', 'RMSprop'),
                    help='optimizer to use (SGD | ADAM | RMSprop)')
parser.add_argument('--momentum', type=float, default=0.9,
                    help='SGD momentum')
parser.add_argument('--betas', type=tuple, default=(0.9, 0.999),
                    help='ADAM beta')
parser.add_argument('--epsilon', type=float, default=1e-8,
                    help='ADAM epsilon for numerical stability')
parser.add_argument('--weight_decay', type=float, default=0,
                    help='weight decay')
parser.add_argument('--gclip', type=float, default=0,
                    help='gradient clipping threshold (0 = no clipping)')

# Loss specifications
parser.add_argument('--loss', type=str, default='1*L1',
                    help='loss function configuration')
parser.add_argument('--skip_threshold', type=float, default='1e8',
                    help='skipping batch that has large error')

# Log specifications
parser.add_argument('--save', type=str, default='test',
                    help='file name to save')
parser.add_argument('--load', type=str, default='',
                    help='file name to load')
parser.add_argument('--resume', type=int, default=0,
                    help='resume from specific checkpoint')
parser.add_argument('--save_models', action='store_true',
                    help='save all intermediate models')
parser.add_argument('--print_every', type=int, default=100,
                    help='how many batches to wait before logging training status')
parser.add_argument('--save_results', action='store_true',
                    help='save output results')
parser.add_argument('--save_gt', action='store_true',
                    help='save low-resolution and high-resolution images together')

args = parser.parse_args()

args.data_test = ["Demo"]
args.scale = [4]
args.pre_train = "../experiment/edsr_baseline_x4/model/model_best.pt"
args.test_only = True
args.save_results = True

torch.manual_seed(args.seed)
checkpoint = serverUtility.checkpoint(args)

if args.alpha != -1:
   print('alpha: ',args.alpha)
global model
if args.data_test == ['video']:
   from videotester import VideoTester
   model = model.Model(args, checkpoint)
   t = VideoTester(args, model, checkpoint)
   t.test()
else:
   if checkpoint.ok:

       _loss = loss.Loss(args, checkpoint) if not args.test_only else None
       _model = model.Model(args, checkpoint)

#######################################################################################

#-*- coding:utf-8 -*-
"""
__author__ = BlingBling
建立TCP的基本流程
ss = socket() # 创建服务器套接字
ss.bind() # 套接字与地址绑定
ss.listen() # 监听连接
inf_loop: # 服务器无限循环
    cs = ss.accept() # 接受客户端连接
    comm_loop: # 通信循环
        cs.recv()/cs.send() # 对话（接收/发送）
    cs.close() # 关闭客户端套接字
ss.close() # 关闭服务器套接字#（可选）
"""
#!/usr/bin/env python

import os
from socket import *
from time import ctime

HOST = ''  #对bind（）方法的标识，表示可以使用任何可用的地址
PORT = 21567  #设置端口
BUFSIZ = 1024  #设置缓存区的大小
ADDR = (HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM)  #定义了一个套接字
tcpSerSock.bind(ADDR)  #绑定地址
tcpSerSock.listen(10)     #规定传入连接请求的最大数，异步的时候适用

while True:
    print('waiting for connection...')
    tcpCliSock, addr = tcpSerSock.accept()
    print ('...connected from:', addr)
    while True:
        rec = tcpCliSock.recv(BUFSIZ)
        print("recv:",rec.decode("utf-8"))
        if not rec:
            break

        args.alpha = rec.decode("utf-8")
        print(args.alpha)
        loader = data.Data(args)
        t = Trainer(args, loader, _model, _loss, checkpoint)
        t.test()

        filename = '../experiment/test/results-Demo/'+ 'result.png'
        if os.path.exists(filename):
            filesize = str(os.path.getsize(filename))
            print("文件大小为：",filesize)
            tcpCliSock.send(filesize.encode())
            rec = tcpCliSock.recv(BUFSIZ)   #挂起服务器发送，确保客户端单独收到文件大小数据，避免粘包
            print("开始发送")
            f = open(filename, "rb")
            for line in f:
                tcpCliSock.send(line)
        else:
            tcpCliSock.send("0001".encode())   #如果文件不存在，那么就返回该代码
    tcpCliSock.close()
tcpSerSock.close()

########################################################################################
checkpoint.done()


import os
from numpy import dot, inner, average, zeros
from copy import deepcopy
from random import sample
from scipy.io import loadmat
from scipy.special import expit

class SVM:
    def __init__(self):

        # loading the input file
        self.dataDict = dict()
        inputFile = "C:/Users/Akash/Desktop/MLHW2/data.mat"
        self.dataDict = loadmat(os.path.join(os.getcwd(), inputFile))

        self.MAX_VALUE = float('inf')
        self.MIN_VALUE = -float('inf')

        # segregating the  training and testing data sets
        self.trnData = self.dataDict['X_trn']
        self.tstData = self.dataDict['X_tst']

        # segregating the  training and testing label sets
        self.trnLabels = self.dataDict['Y_trn']
        self.tstLabels = self.dataDict['Y_tst']

        # casting the array to specific type
        self.trnData = self.trnData.astype(float, copy=False)
        self.tstData = self.tstData.astype(float, copy=False)
        self.trnLabels = self.trnLabels.astype(int, copy=False)
        self.tstLabels = self.tstLabels.astype(int, copy=False)

        # find the unique classes present in the given label
        self.classes = self.findClasses()
        self.trainingLabelsForClass = None
        self.testingLabelsForClass = None
        self.createLabelForClass()

        self.trnSampleCount = self.trnData.shape[0]
        self.featuresCount = self.trnData.shape[1]
        self.classCount = len(self.classes)

    # Function to find the unique classes present in the given labels
    def findClasses(self):
        uniqueClassSet = set()
        for lbl in self.trnLabels:
            uniqueClassSet.add(lbl[0])
        for lbl in self.tstLabels:
            uniqueClassSet.add(lbl[0])
        return list(sorted(uniqueClassSet))

    # This function creates the labels for class and assigns 1 if the label
    # belongs to the class else it is assigned -1. This is to differentiate a class from rest of the classes
    def createLabelForClass(self):
        self.trainingLabelsForClass = list()
        self.testingLabelsForClass = list()
        # creating copies of the labels
        for cls in self.classes:
            cpy = deepcopy(self.trnLabels)
            self.trainingLabelsForClass.append(cpy)
            cpy = deepcopy(self.tstLabels)
            self.testingLabelsForClass.append(cpy)
        # Differentiate each class from the rest of the classes
        for cls in self.classes:
            for i in range(self.trnLabels.shape[0]):
                if cls == self.trnLabels[i]:
                    self.trainingLabelsForClass[cls][i] = 1
                else:
                    self.trainingLabelsForClass[cls][i] = -1
            for i in range(self.tstLabels.shape[0]):
                if cls == self.tstLabels[i]:
                    self.testingLabelsForClass[cls][i] = 1
                else:
                    self.testingLabelsForClass[cls][i] = -1

    # computing using (2) from the paper
    def E(self, i, data, labels, alphas, b):
        return self.func(data, b, data[i], labels, alphas) - labels[i]

    def func(self, data, b, dataPoint, labels, alphas):
        op = 0
        for i in range(len(data)):
            op += alphas[i] * labels[i] * inner(data[i], dataPoint)
        op += b
        return op

    def computeB(self, b, i, j, Ei, Ej, C, alphas, prevAlphaI, prevAlphaJ, data, labels):
        t1 = b
        t2 = Ei
        t3 = labels[i] * (alphas[i] - prevAlphaI) * inner(data[i], data[i])
        t4 = labels[j] * (alphas[j] - prevAlphaJ) * inner(data[i], data[j])

        b1 = t1 - t2 - t3 - t4

        t2 = Ej
        t3 = labels[i] * (alphas[i] - prevAlphaI) * inner(data[i], data[j])
        t4 = labels[j] * (alphas[j] - prevAlphaJ) * inner(data[j], data[j])
        b2 = t1 - t2 - t3 - t4

        if ((0 < alphas[i]) and (alphas[i] < C)) and not ((0 < alphas[j]) and (alphas[j] < C)):
            return b1
        elif ((0 < alphas[j]) and (alphas[j] < C)) and not ((0 < alphas[i]) and (alphas[i] < C)):
            return b2
        else:
            return (b1 + b2) / 2

    def computeLH(self, i, j, labels, alphas, C):
        L = self.MIN_VALUE
        H = self.MAX_VALUE
        if (labels[i] != labels[j]):
            L = max(0, alphas[j] - alphas[i])
            H = min(C, C + alphas[j] - alphas[i])
        else:
            L = max(0, alphas[i] + alphas[j] - C)
            H = min(C, alphas[i] + alphas[j])
        return (L, H)

    # compute Eta using (14) in paper
    def computeEta(self, i, j, data):
        op = 2 * inner(data[i], data[j]) - inner(data[i], data[i]) - inner(data[j], data[j])
        return op

    # computing new alpha j using (12) and (15) form paper
    def findNewAJ(self, i, j, alphas, data, labels, Ei, Ej, L, H):
        eta = self.computeEta(i, j, data)
        newAJ = alphas[j] - (labels[j] * (Ei - Ej) / eta)
        if newAJ > H:
            return H
        elif newAJ < L:
            return L
        else:
            return newAJ

    # compute the weight vector for the specific class
    def findWV(self, alphas, inputLabels, data):
        # get the labels from the inputLabels
        labels = inputLabels.transpose().tolist()[0]
        w = alphas[0] * labels[0] * data[0]
        for i in range(1, len(alphas)):
            w += alphas[i] * labels[i] * data[i]
        return w

    def performSmo(self, inData, inputLabels):
        data = inData

        # lagrange multipliers for solution and initializing all alpha to 0
        labels = inputLabels.transpose().tolist()[0]
        alphas = zeros(inputLabels.shape)
        alphas = alphas.transpose().tolist()[0]

        # threshold for solution, initializing to 0
        b = 0
        passes = 0
        MAX_PASSES = 2
        C = 1
        tolerance = 0.01
        trnSamplesIndx = list(range(self.trnSampleCount))

        while passes < MAX_PASSES:

            numberOfChangedAlphas = 0

            for i in trnSamplesIndx:
                Ei = self.E(i, data, labels, alphas, b)
                if ((labels[i] < -1 * tolerance) and (alphas[i] < C)) or (
                            (labels[i] > -1 * tolerance) and (alphas[i] > 0)):
                    # selecting j randomly and j != i
                    j = sample(trnSamplesIndx[:i] + trnSamplesIndx[i + 1:], 1)[0]

                    # Compute Ej using (2) in paper
                    Ej = self.E(j, data, labels, alphas, b)

                    # saving old alphas
                    prevAlphaI = alphas[i]
                    prevAlphaJ = alphas[j]

                    L, H = self.computeLH(i, j, labels, alphas, C)

                    if L == H:
                        continue
                    eta = self.computeEta(i, j, data)
                    if eta >= 0:
                        continue
                    # computing alpha j using (12) and (15) form paper
                    alphas[j] = self.findNewAJ(i, j, alphas, data, labels, Ei, Ej, L, H)

                    if abs(alphas[j] - prevAlphaJ) < 0.00001:
                        continue
                    alphas[i] = alphas[i] + labels[i] * labels[j] * (prevAlphaJ - alphas[j])

                    # compute b using (19) from paper
                    b = self.computeB(b, i, j, Ei, Ej, C, alphas, prevAlphaI, prevAlphaJ, data, labels)
                    numberOfChangedAlphas += 1
            if numberOfChangedAlphas == 0:
                passes += 1
            else:
                passes = 0

        return (alphas, b)

    def softMaxforSMO(self):
        # list of all the weight vectors for a specific class
        weigthVforClass = list()
        # bias for class
        classwiseB = list()
        # predictions for classes
        classPredictions = list()

        for i in range(len(self.classes)):
            labels = self.trainingLabelsForClass[i]
            alphas, b = self.performSmo(self.trnData, labels)

            wtv = self.findWV(alphas, labels, self.trnData)

            # weight vector for class
            weigthVforClass.append(wtv)

            # bias for class
            classwiseB.append(b)

            # prediction for class
            predictions = expit(dot(self.tstData, wtv.transpose()) + b)
            classPredictions.append(predictions)

        actualPredictions = list()

        # computing the predictions
        for i in range(len(self.tstLabels)):
            predictedClass = None
            maxprob = self.MIN_VALUE
            for cIdx in range(len(self.classes)):
                if classPredictions[cIdx][i] > maxprob:
                    maxprob = classPredictions[cIdx][i]
                    predictedClass = self.classes[cIdx]
            actualPredictions.append(predictedClass)

        # print weight vector for class
        print("weigth Vectors for Class: " + str(weigthVforClass))
        # print bias for class
        print("classwise Biases: " + str(weigthVforClass))
        # print the actual prediction
        print("Predictions: " + str(actualPredictions))
        # print testing labels for class
        print("Testing labels: " + str(self.tstLabels.transpose().tolist()))

if __name__ == "__main__":
    obj = SVM()
    obj.softMaxforSMO()


import collections
import functools
import operator
import re
import logging
from typing import List, Union, Callable, Dict, Any

import pandas as pd
import numpy as np

from .merging_functions import merge_with_msmsscanstxt, merge_with_msmstxt, merge_with_summarytxt
from .utils import utils

logger = logging.getLogger(__name__)

PHOSPHO_REGEX = re.compile(r'([STY])\(Phospho \(STY\)\)')
PROBABILITY_REGEX = re.compile(r'\((\d(?:\.?\d+)?)\)')

def transfer(summary_df, max_pep=False, mask=False, ambiguity_decision='majority'):
    """
    Main function for transfers by clustering. Transfers identifications for merged dataframe and adds a column for
    identification type. Transferred columns are Sequence, Modified sequence, Proteins, Gene names, Protein Names,
    Charge, m/z, and Mass.
    :param summary_df: Summary dataframe, merged from cleaned msms.txt and MaRaCluster clusters.tsv file
    :param max_pep: Maximum PEP of a PSM to be considered for transfers
    :param mask: if false, uses "identification" column from summary frame. If set to a value, transfer uses the
    'identification_{mask}' column, needed for the masking analysis
    :param ambiguity_decision: if 'all', returns raw sequences with potential phospho positions when encountering isomer
    clusters; if 'majority', decides for one sequence by majority vote
    :return: DataFrame with transferred identifications resembling MaxQuant msmsScans.txt
    """
    if mask:
        identification_column = f'identification_{mask}'
    else:
        identification_column = 'identification'

    if ambiguity_decision == 'majority':
        # TODO: Generate modified sequence from probability string rather than taking it from the cluster
        mod_seq_func = lambda s: get_consensus_modified_sequence(s, get_most_common_sequence)
    elif ambiguity_decision == 'all':
        mod_seq_func = get_consensus_modified_sequence
    else:
        raise ValueError("The parameter 'ambiguity_decision' has to be set on 'all' or 'majority'!")

    agg_funcs = {'Sequence': utils.get_unique_else_nan,
                 'Modifications': utils.get_unique_else_nan,
                 'Modified sequence': mod_seq_func,
                 'Phospho (STY) Probabilities': calculate_average_probabilities,
                 'Proteins': utils.csv_list_unique,
                 'Gene Names': utils.csv_list_unique,
                 'Protein Names': utils.csv_list_unique,
                 'Charge': utils.get_unique_else_nan,
                 'm/z': 'mean',
                 'Mass': 'mean',
                 'Missed cleavages': utils.get_unique_else_nan,
                 'Length': utils.get_unique_else_nan,
                 'PEP': 'max',
                 'Reverse': utils.get_unique_else_nan}

    identified_scans = summary_df['Modified sequence'].notna()
    pep_filtered = pd.Series(np.ones_like(summary_df.index), dtype='bool')
    if max_pep:
        pep_filtered = summary_df['PEP'].astype(float) <= max_pep / 100
    cluster_info_df = summary_df[identified_scans & pep_filtered].groupby('clusterID', as_index=False).agg(agg_funcs)
    # Mark all clusters with a unique identification as transferred ('t').
    # Identifications by MQ will overwrite this column as direct identification ('d') a few lines below.
    cluster_info_df[identification_column] = np.where(cluster_info_df['Modified sequence'].notna(), 't', None)

    replacement_dict = {k: "grouped_" + k for k in agg_funcs.keys()}
    cluster_info_df.rename(columns=replacement_dict, inplace=True)

    summary_df = pd.merge(left=summary_df, right=cluster_info_df, on=['clusterID'], how='left')
    summary_df.loc[identified_scans, identification_column] = 'd'
    # Now we have 'd' in every ID that came from MaxQuant and 't' in every ID that came from the clustering
    # And now we copy the contents of the 'grouped_...' columns into the original columns for every row where we have a 't'
    summary_df.loc[
        # Aren't all of these cells by definition NaNs? Can we make use of that?
        # Like 'Merge, and if you find two columns with the same name always use the value that is not NaN!'
        # Then we could do this directly in the merge and wouldn't have to go over the df again here
        # Try pd.DataFrame.combine(). For this the dfs would need to be in the same shape though.
        summary_df[identification_column] == 't', replacement_dict.keys()] = summary_df.loc[
        summary_df[identification_column] == 't', replacement_dict.values()].to_numpy()
    # The 'grouped_...' columns have made themselves redundant
    summary_df.drop(columns=replacement_dict.values(), inplace=True)
    return summary_df

def transform_phospho_psp_format(sequences: List[str]) -> List[str]:
    """Replaces phosphorylation modification by lower case letter (PhosphositePlus convention),
    e.g. 'AAAAAAAGDS(Phospho (STY))DS(Phospho (STY))WDADAFSVEDPVRK' => 'AAAAAAAGDsDsWDADAFSVEDPVRK'
    """
    sequences = [re.sub('_', '', x) for x in sequences]
    return [re.sub(PHOSPHO_REGEX, lambda pat: pat.group(1).lower(), x) for x in sequences]

def remove_probabilities(sequences_with_probabilities: List[str]) -> List[str]:
    return [remove_probabilities_from_sequence(s) for s in sequences_with_probabilities]

def remove_probabilities_from_sequence(sequence: str) -> str:
    """remove probability strings noted in parentheses,
    e.g. 'AAAAAAAGDS(0.988)DS(0.012)WDADAFSVEDPVRK' => 'AAAAAAAGDSDSWDADAFSVEDPVRK'
    """
    return re.sub(PROBABILITY_REGEX, '', sequence)

def check_ambiguity(sequences: List[str],
                    transform_sequence: Callable[[List[str]], List[str]] = transform_phospho_psp_format):
    sequences = utils.remove_nan_values(set(sequences))
    if len(sequences) == 0:
        return np.nan, np.nan
    if len(sequences) == 1:
        return sequences[0], np.nan

    transformed_sequences = transform_sequence(sequences)
    unique_sequences = set(map(str.upper, transformed_sequences))
    if len(unique_sequences) != 1:
        return np.NaN, np.nan
    return sequences, transformed_sequences

def is_unambiguous(sequences):
    return (type(sequences) == float and np.isnan(sequences)) or type(sequences) == str

def get_mod_probabilities_dict(probstring: str) -> Dict[int, float]:
    probsplit = re.split(PROBABILITY_REGEX, probstring)
    start = 0
    probdict = dict()
    for amino_acids, prob in zip(probsplit[0::2], probsplit[1::2]):
        start += len(amino_acids)
        probdict[start] = float(prob)
    return probdict

def sum_dictionaries(dictionary):
    return dict(functools.reduce(operator.add, map(collections.Counter, dictionary)))

def average_dictionaries(dictionary):
    summed_dictionary = sum_dictionaries(dictionary)

    num_dictionaries = len(dictionary)

    def get_average_and_round(x):
        return round(x / num_dictionaries, 3)

    return {pos: get_average_and_round(summed_prob) for pos, summed_prob in summed_dictionary.items()}

def add_probabilities_to_sequence(sequence, probability_dict):
    for position, probability in reversed(sorted(list(probability_dict.items()))):
        sequence = sequence[:position] + f'({probability})' + sequence[position:]
    return sequence

def calculate_average_probabilities(mod_probability_sequences):
    mod_probability_sequences, _ = check_ambiguity(mod_probability_sequences, remove_probabilities)
    if is_unambiguous(mod_probability_sequences):
        return mod_probability_sequences

    mod_probabilities = [get_mod_probabilities_dict(p) for p in mod_probability_sequences]
    average_mod_probabilities = average_dictionaries(mod_probabilities)

    sequence = remove_probabilities_from_sequence(mod_probability_sequences[0])
    sequence_with_probabilities = add_probabilities_to_sequence(sequence, average_mod_probabilities)
    # # this does not work for multiple phosphorylations in one sequence
    # if not 0.95 < sum(resdict.values()) < 1.05:
    #     raise ValueError(f'Probability sum deviates from expected value: {resdict}')
    return sequence_with_probabilities

def get_modified_sequence_annotation(sequences, sequences_psp_format):
    # TODO: Make ambiguous localization handling work for acetylation (and p-ac-combinations) as well
    # TODO: If underscores are handed over in the modified seq, the modification positions are incorrect in the end!
    num_mods = len(re.findall(r'[sty]', sequences_psp_format[0]))
    phospho_positions = {m.start() + 1 for x in sequences_psp_format for m in re.finditer(r'[sty]', x)}
    phospho_positions = sorted(list(phospho_positions))
    phopsho_positions_string = "/".join(map(lambda x: f'p{x}', phospho_positions))
    mod_sequence_without_phospho = utils.remove_modifications(sequences[0], remove_phospho_only=True)
    return f"{mod_sequence_without_phospho}.{num_mods}.{phopsho_positions_string}"

def get_most_common_sequence(sequences, sequences_psp_format):
    sequences = utils.remove_nan_values(sequences)
    return collections.Counter(sequences).most_common(1)[0][0]

def get_consensus_modified_sequence(sequences, consensus_function=get_modified_sequence_annotation):
    unique_sequences, sequences_psp_format = check_ambiguity(sequences)
    if is_unambiguous(unique_sequences):
        return unique_sequences
    return consensus_function(sequences, sequences_psp_format)

def flag_ambiguous_clusters(sumdf, rawseq='Sequence', modseq='Modified sequence'):
    """
    adds MIC flags to all scans in MICs
    :param sumdf: Summary dataframe, merged from cleaned msms.txt and MaRaCluster clusters.tsv file
    :param rawseq: Column name of the raw sequence column
    :param modseq: Column name of the modified sequence column
    :return:
    """
    # Store number of unique modified sequences per cluster in group_dataframe
    group_dataframe = sumdf.groupby('clusterID')[modseq].nunique(dropna=True).reset_index(name='seqcount')

    # Filter for clusters with more than one modified sequence and flag with column mod_ambiguous
    group_dataframe = group_dataframe[group_dataframe['seqcount'] >= 2]
    group_dataframe.drop(columns=['seqcount'], axis=1, inplace=True)
    group_dataframe['mod_ambiguous'] = 1

    # merge with initial frame
    merged_dataframe = pd.merge(left=sumdf, right=group_dataframe, on='clusterID', how='left')

    # Store number of unique raw sequences per cluster in group_dataframe
    group_dataframe = sumdf.groupby('clusterID')[rawseq].nunique(dropna=True).reset_index(name='seqcount')

    # Filter for clusters with more than one raw sequence and flag with column raw_ambiguous
    group_dataframe = group_dataframe[group_dataframe['seqcount'] >= 2]
    group_dataframe.drop(columns=['seqcount'], axis=1, inplace=True)
    group_dataframe['raw_ambiguous'] = 1

    # merge with initial frame
    merged_dataframe = pd.merge(left=merged_dataframe, right=group_dataframe, on='clusterID', how='left')

    # remove mod_ambiguous flags if cluster is raw_ambiguous
    merged_dataframe.loc[merged_dataframe['raw_ambiguous'] == 1, 'mod_ambiguous'] = np.nan
    return merged_dataframe


import math, os, pickle, re
import numpy as np
import nltk
from nltk.collocations import *
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

#PriorP_positive=0

class Bayes_Classifier:
    def __init__(self, trainDirectory = "movie_reviews/"):
        #if my pickled file exist then directly load that else go to train method and train data first
        self.sfilename="databasebest.txt"
        if os.path.isfile(self.sfilename) and os.path.getsize(self.sfilename)>0:
            self.load(self.sfilename)

        else:
            f= open(self.sfilename,"w+")

            self.negativeDict = {}
            self.positiveDict = {}
            self.PriorP_positive=0
            self.PriorP_negative=0
            self.cn=0
            self.cp=0
            self.doc_class=""
            self.sText=''
            self.train_set()

    #start train get list of all the file names
    def train_set(self):
        trainDir = "testing/"
        lFileList = []
        for fFileObj in os.walk("testing/"):
            lFileList = fFileObj[2]
            break
        print(" Total training  reviews",len(lFileList))
        #creating list of positive and negative reviews
        positive_list=[]
        negative_list=[]
        length=len(lFileList)
        for rating in range(length):
            if lFileList[rating][7]=="1":
                negative_list.append(lFileList[rating])
                self.doc_class="negative"
                sTxt=self.loadFile(trainDir + lFileList[rating])
                self.tokensize(sTxt,self.doc_class)
            elif lFileList[rating][7]=="5":
                positive_list.append(lFileList[rating])
                self.doc_class="positive"
                sTxt=self.loadFile(trainDir + lFileList[rating])
                self.tokensize(sTxt,self.doc_class)

        self.save()
        self.load(self.sfilename)
        self.prior_probability(negative_list,positive_list)

        print("total negative",self.cn)
        print("total positive",self.cp)

 #reading
    def loadFile(self,filename):

            #f = open(filename, "r")
            f = open(filename, 'r', encoding='latin-1')
            sTxt = f.read()
            f.close()
            return sTxt

    def tokensize(self,sText,doc_class):
        #splitting the text in file
        stop_words = set(stopwords.words('english'))
        lTokens = []
        sToken = ""
        for c in sText:
            if re.match("[a-zA-Z0-9]", str(c)) != None or c == "\'" or c == "_" or c == '-':
                sToken += c

            else:
                if sToken != "":
                    lTokens.append(sToken)
                    sToken = ""
                if c.strip() != "":
                    lTokens.append(str(c.strip()))

        if sToken != "":
            lTokens.append(sToken)
        lTokens = [w for w in lTokens if not w in stop_words]
        self.bigram(lTokens)
   #create pair for tokens extracted from text
    def bigram(self,lTokens):
        bigram_measures = nltk.collocations.BigramAssocMeasures()
        trigram_measures = nltk.collocations.TrigramAssocMeasures()
        finder = BigramCollocationFinder.from_words(lTokens)
        all_pair=finder.nbest(bigram_measures.pmi,30)
        for pair in all_pair:
            self.build_dictionary(pair,self.doc_class)
    #create dictionary add each pair exacted above
    def build_dictionary(self,pair,doc_class):
        if self.doc_class=="negative":
                if self.negativeDict.get(pair,0)==0:
                    self.negativeDict.update({pair:1})
                    self.cn=self.cn+1
                elif self.negativeDict.get(pair,0)>0:
                    self.negativeDict[pair]=self.negativeDict[pair]+1
                    self.cn=self.cn+1

        if self.doc_class=="positive":
            if self.positiveDict.get(pair,0)==0:
                self.positiveDict.update({pair:1})
                self.cp=self.cp+1
            elif self.positiveDict.get(pair,0)>0:
                self.positiveDict[pair]=self.positiveDict[pair]+1
                self.cp=self.cp+1

    def save(self):
        f = open(self.sfilename,"wb")
        pickle.dump(self,f)
        f.close()

    def load(self, sFilename):
        f = open(sFilename, "rb")
        u = pickle.Unpickler(f)
        dObj = u.load()
        self.__dict__.update(dObj.__dict__)
        f.close()
        return dObj

    def prior_probability(self,negative_list,positive_list):
        lengthN=len(negative_list)
        lengthP=len(positive_list)
        print("P length of dict",lengthP)
        print("n length of dict",lengthN)
        totalcount=lengthN+lengthP
        self.PriorP_positive=np.log(lengthP/totalcount)
        self.PriorP_negative=np.log(lengthN/totalcount)
        print("in pp function ","neg",self.PriorP_negative,"\npos",self.PriorP_positive)

    def classify(self,filetext):

        stop_words = set(stopwords.words('english'))
        obj1=self.load(self.sfilename)
        print("positive dict",obj1.positiveDict)
        print("\n \n negaitive dict",obj1.negativeDict)
        self.prior_probability(obj1.negativeDict,obj1.positiveDict)
        class_review=["positive","negative"]
        result=[]
        lTokens = []
        sToken = ""
        for c in filetext:
            if re.match("[a-zA-Z0-9]", str(c)) != None or c == "\'" or c == "_" or c == '-':
                sToken += c

            else:
                if sToken != "":
                    lTokens.append(sToken)
                    sToken = ""
                if c.strip() != "":
                    lTokens.append(str(c.strip()))
        if sToken != "":
            lTokens.append(sToken)
        lTokens = [w for w in lTokens if not w in stop_words]
        bigram_measures = nltk.collocations.BigramAssocMeasures()
        trigram_measures = nltk.collocations.TrigramAssocMeasures()
        finder = BigramCollocationFinder.from_words(lTokens)
        all_pair=finder.nbest(bigram_measures.pmi, 10)

        lenp=len(obj1.positiveDict)
        lenn=len(obj1.negativeDict)

        for x in class_review:
            con_prob_positive=0
            con_prob_negative=0

            if x=="positive":
                for pair in all_pair:
                    count=obj1.positiveDict.get(pair,0)

                    Pword=np.log((count+1)/(self.cp+lenp))
                    con_prob_positive=con_prob_positive+Pword
                result.append(con_prob_positive+self.PriorP_positive)
            elif x=="negative":
                for pair in all_pair:
                    count=obj1.negativeDict.get(pair,0)

                    Pword=np.log((count+1)/(self.cn+lenn))
                    con_prob_negative=con_prob_negative+Pword
                result.append(con_prob_negative+self.PriorP_negative)

        if result[1]-result[0]<1 and result[1]-result[0]>-1:
            return "neutral"
        else:
            if(result[1]<result[0]):
                return "positive"
            if(result[1]>result[0]):
                return "negative"


from flask import Flask, render_template, redirect, url_for, request
from flask_restful import Api, Resource, reqparse
from flask_cors import CORS, cross_origin
import os
import json
import time
APP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(APP_PATH, 'src/pages/')
app = Flask(__name__, template_folder=TEMPLATE_PATH)
cors = CORS(app)

# This file aims to download the user uploaded video to the
# appropriate local directory and update the user on any
# changes to result_update.json

@app.route('/home/results', methods = ['GET'])
def success():
    # Since we are looking for a file modification in the last 2 seconds,
    # delaying for 2 seconds ensures that we won't return results from a
    # previous 'Generate Results' run.
    time.sleep(2)
    file_dir=os.path.join(APP_PATH,'src/utils/AllResultsJSON/result_update.json')
    curr_time=time.time()
    file_mod_time=os.path.getmtime(file_dir)
    while(abs(curr_time-file_mod_time)>2):
        curr_time=time.time()
        file_mod_time=os.path.getmtime(file_dir)
    all_data=json.load(open(file_dir))
    # true->True, false->False... uppercase keywords undefined in JS.
    all_data[2]["beard"]=int(all_data[2]["beard"])
    all_data[3]["shades"]=int(all_data[3]["shades"])
    return {"models" : all_data}

@app.route('/home/upload', methods = ['POST'])
@cross_origin()
def upload_video():
    if request.method == 'POST':
        (request.files["file"]).save(os.path.join(APP_PATH,'backend/example_videos/target.mp4'))
        return "Successfully saved!"

if __name__ == '__main__':
   app.run(port=5000, debug = True)# 3000 , 5000


import os
import yaml

def _parse_obj(obj):
    target = lambda: None
    if isinstance(obj, dict):
        for key, value in obj.items():
            setattr(target, key, _parse_obj(value))
    elif isinstance(obj, list):
        target = [_parse_obj(x) for x in obj]
    else:
        target = obj
    return target

commands_path = os.path.dirname(os.path.abspath(__file__)) + "/commands.yaml"
BashCommand = _parse_obj(yaml.load(open(commands_path).read()))


#!/usr/bin/python
import time
import sys
import argparse
from time import strftime
from os import system
from sys import platform as _platform

# Help String
description_string = "Tomaterminal is a terminal program based on the Pomodoro (Italian for Tomato) method of working. In the Pomodoro method, you take a timer ((frequently tomato shaped) historically used in kitchens) and you set a 25 minute timer for work. After 25 mintues are completed, you set a 5 minute timer for break. Tomaterminal emulates this exact behavior, alerting you after 25 minutes have elapsed, then after your 5 minute break has elapsed."

parser = argparse.ArgumentParser(description=description_string)
parser.add_argument('-t','--task_time', type=int, help='Task Interval (minutes)',required=False)
parser.add_argument('-b','--break_time', type=int, help='Break Interval (minutes)',required=False)
parser.add_argument('-m','--task_name', type=str, help='Task Name (in quotations. ex: "Hello World")',required=False)
parser.add_argument('-p','--pomo_num', type=str, help='Number of last Pomodoro (useful for taking breaks)',required=False)
parser.add_argument('-e','--pomo_est', type=str, help='Estimated number of Pomos',required=False)
args = parser.parse_args()

# Time Definitions
seconds_minute = 1
minutes_hour = 60
hours_day = 24

# Task Definitions
task_time = 25
break_time = 5
task_name = "Current Task"
pomo_num = 1
pomo_est = 1

# Override task/break time if command line arguments passed
if args.task_time is not None:
    task_time = args.task_time
if args.break_time is not None:
    break_time = args.break_time
if args.task_name is not None:
    task_name = args.task_name
if args.pomo_num is not None:
    pomo_num = int(args.pomo_num)
if args.pomo_est is not None:
    pomo_est = int(args.pomo_est)

# UI Definitions
progress_bar_length = 40

def alert():
    print ('\a')

def progress(count, total, suffix=''):
    filled_len = int(round(progress_bar_length * count / float(total)))
    percents = round(100.0 * count / float(total), 1)
    bar = '=' * filled_len + '-' * (progress_bar_length - filled_len)
    sys.stdout.write("\r[%s] %s%s %s" % (bar, percents, '%', suffix)),
    sys.stdout.flush()

# Initial Entry into Program; Clear Screen
while True:

    sys.stdout.write("Task: %sm -- Break: %sm -- Task: %s -- Est_Pomos: %s - Act_Pomos: %s -- Pause/unpause=ctrl-z;fg\n" % (task_time, break_time, task_name, pomo_est, pomo_num)),
    # Task Loop
    progress(0,task_time,'Working: %smin to go - Start-Time: %s' % (task_time, strftime("%Y-%m-%d %H:%M:%S")))
    for i in range(0, task_time):
        ellapsedTime = task_time - i
        if ellapsedTime < 10:
            ellapsedTime = str(ellapsedTime).zfill(2)
        time.sleep(seconds_minute)
        progress(i,task_time,'Working: %smin to go' % ellapsedTime)

    alert()
    if (_platform == "darwin") or (_platform == "linux2"):
        system('say %s minute break starting now. ' % str(break_time))

    # Break Loop
    progress(0,task_time,'Break: %smin left - Start-Time: %s' % (break_time, strftime("%Y-%m-%d %H:%M:%S")))
    for i in range(0, break_time):
        breakTimeEllapsed = break_time - i;
        breakTimeEllapsed = str(breakTimeEllapsed).zfill(2)
        time.sleep(seconds_minute)
        progress(i,break_time,'Break: %smin left' % breakTimeEllapsed)

    alert()
    pomo_num = pomo_num + 1
    print ('')
    if (_platform == "darwin") or (_platform == "linux2"):
        system('say %s minute work session starting now.' % str(task_time))


import json
import os
import argparse
import subprocess
from multiprocessing import Pool
import neurips23.common

def build(tag, args, dockerfile):
    print('Building %s...' % tag)
    if args is not None and len(args) != 0:
        q = " ".join(["--build-arg " + x.replace(" ", "\\ ") for x in args])
    else:
        q = ""

    try:
        command = 'docker build %s --rm -t %s -f' \
                   % (q, tag)
        command += ' %s .' % dockerfile
        print(command)
        subprocess.check_call(command, shell=True)
        return {tag: 'success'}
    except subprocess.CalledProcessError:
        return {tag: 'fail'}

def build_multiprocess(args):
    return build(*args)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        "--proc",
        default=1,
        type=int,
        help="the number of process to build docker images")
    parser.add_argument(
        '--algorithm',
        metavar='NAME',
        help='build only the named algorithm image',
        default=None)
    parser.add_argument(
        '--dockerfile',
        metavar='PATH',
        help='build only the image from a Dockerfile path',
        default=None)
    parser.add_argument(
        '--neurips23track',
        choices=['filter','ood','streaming','sparse','none'],
        default='none'
    )
    parser.add_argument(
        '--build-arg',
        help='pass given args to all docker builds',
        nargs="+")
    args = parser.parse_args()

    print('Building base image...')
    if args.neurips23track != 'none':
        subprocess.check_call(
            'docker build \
            --rm -t %s -f %s .' % (neurips23.common.docker_tag_base(), neurips23.common.dockerfile_path_base()), shell=True)
        track = args.neurips23track

        if args.algorithm: # build a specific algorithm
            algos = [args.algorithm]
        else: # build all algorithms in the track with Dockerfiles.
            algos = filter(lambda entry : os.path.exists(neurips23.common.dockerfile_path(track, entry)),
                            os.listdir(neurips23.common.track_path(track)))
        tags = [neurips23.common.docker_tag(track, algo) for algo in algos]
        dockerfiles = [neurips23.common.dockerfile_path(track, algo) for algo in algos]
    else: # NeurIPS'21
        track_prefix = 'billion-scale-benchmark'
        subprocess.check_call(
            'docker build \
            --rm -t %s -f install/Dockerfile .' % track_prefix, shell=True)
        if args.dockerfile:
            tags = [track_prefix + '-' + os.path.basename(os.path.dirname(args.dockerfile))]
            dockerfiles = args.dockerfile
        else:
            if args.algorithm:
                algos = [args.algorithm]
            elif os.getenv('LIBRARY'):
                algos = [os.getenv('LIBRARY')]
            else:
                algos = [fn.split('.')[-1] for fn in os.listdir('install') if fn.startswith('Dockerfile.') and not 'faissgpu' in fn]
            dockerfiles = ['install/Dockerfile.' +  algo for algo in algos]
            tags = [track_prefix + '-' + algo for algo in algos]

    print('Building algorithm images... with (%d) processes' % args.proc)

    if args.proc == 1:
        install_status = [build(tag, args.build_arg, dockerfile) for (tag, dockerfile) in zip(tags, dockerfiles)]
    else:
        pool = Pool(processes=args.proc)
        install_status = pool.map(build_multiprocess,
                                  [(tag, args.build_arg, dockerfile) for (tag, dockerfile) in zip(tags, dockerfiles)])
        pool.close()
        pool.join()

    print('\n\nInstall Status:\n' + '\n'.join(str(algo) for algo in install_status))


import numpy as np
import matplotlib.pyplot as plt

def get_windows(image):
    """Display the given image and record user selected points.

    Parameters
    ----------

    image : M,N,3 ndarray
        The image to be displayed.

    Returns
    -------

    array : n_points,2
        An array of coordinates in the image. Each row corresponds to the x, y
        coordinates of one point. If an odd number of points are specified, the
        last one will be discarded.

    """
    plt.interactive(True)
    plt.imshow(image)
    plt.show()
    crop = plt.ginput(0)
    plt.close()
    plt.interactive(False)
    # remove last point if an odd number selected
    crop = crop[:-1] if np.mod(len(crop), 2) else crop
    return np.vstack(crop).astype('int')[:, [1, 0]]


import psutil
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import matplotlib.animation as animation
from collections import deque

print(psutil.sensors_battery())
fig = plt.figure()
ax = plt.axes(xlim=(0, 200), ylim=(0, 100))
line, = ax.plot([],[])

y_list = deque([-1]*400)
x_list = deque(np.linspace(200,0,num=400))

def init():
    line.set_data([],[])
    return line,

def animate(i):
    y_list.pop()
    y_list.appendleft(psutil.cpu_percent(None,False))
    line.set_data(x_list,y_list)
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=100, blit=True)

plt.show()


from fastapi import FastAPI
import requests

app = FastAPI()

@app.get("/")
async def index():
  return {
      "info": "Try /pokemon/pikachu for a quick demo.",}

@app.get("/pokemon/{pokemon}")
async def get_types(pokemon: str,):
  pokemon_response = requests.get(f'https://pokeapi.co/api/v2/pokemon/{pokemon}')

  body = pokemon_response.json()
  types_for_pokemon = []

  for type in body['types']:
      types_for_pokemon.append(type['type']['name'])

  types_pokemon_double_damage_from = set()
  types_pokemon_half_damage_from = set()
  types_pokemon_no_damage_from = set()

  for type in types_for_pokemon:
      type_response = requests.get(f'https://pokeapi.co/api/v2/type/{type}')
      type_response_json = type_response.json()
      types_pokemon_double_damage_from.update(type['name'] for type in type_response_json['damage_relations']['double_damage_from'])
      types_pokemon_half_damage_from.update(type['name'] for type in type_response_json['damage_relations']['half_damage_from'])
      types_pokemon_no_damage_from.update(type['name'] for type in type_response_json['damage_relations']['no_damage_from'])

  types_pokemon_double_damage_from = types_pokemon_double_damage_from - types_pokemon_half_damage_from
  types_pokemon_double_damage_from = types_pokemon_double_damage_from - types_pokemon_no_damage_from

  output = {
      'name': pokemon,
      'weaknesses': list(types_pokemon_double_damage_from),
      'resistances': list(types_pokemon_half_damage_from),
      'no_damage_from': list(types_pokemon_no_damage_from)
  }

  if pokemon:
    return f"{output}"

  return {
      "name": pokemon,}


import os
import re
import setuptools

VERSIONFILE = "./jovian/_version.py"
FLAVORFILE = "./jovian/_flavor.py"
verstrline = open(VERSIONFILE, "rt").read()
VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]"
mo = re.search(VSRE, verstrline, re.M)
pkg_name = os.getenv("PKG_NAME", "jovian")

if mo:
    verstr = mo.group(1)
else:
    raise RuntimeError("Unable to find version string in %s." % (VERSIONFILE,))

with open(FLAVORFILE, "w") as f:
    f.write("__flavor__ = \"{}\"".format(pkg_name))

with open("README.md", "rb") as fh:
    long_description = fh.read().decode('utf-8', errors='ignore')

setuptools.setup(
    name=pkg_name,
    version=verstr,
    author="Jovian",
    author_email="hello@jovian.ai",
    entry_points={
        'console_scripts': ['jovian=jovian.__main__:main'],
    },
    description="Jovian Python SDK",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://jovian.ai/?utm_source=pypi",
    packages=setuptools.find_packages(exclude=["*.tests", "*.tests.*"]),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    package_data={"jovian": ['jovian_nb_ext/*']},
    data_files=[("share/jupyter/nbextensions/jovian_nb_ext", ["jovian/jovian_nb_ext/main.js"]),
                ("etc/jupyter/nbconfig/notebook.d", ["jovian/jovian_nb_ext/jovian_nb_ext.json"])],
    include_package_data=True,
    install_requires=['requests', 'uuid', 'pyyaml', 'click']
)


import datetime

class Employee_1:

    raise_amount = 1.04    #class variable
    no_of_employees = 0

    def __init__(self, first_name, last_name, email, pay):    # constructor
        self.first_name = first_name
        self.last_name = last_name
        self.email = email
        self.pay = pay
        Employee_1.no_of_employees += 1

    def fullname(self):
        return '{} {}'.format(self.first_name, self.last_name)

    def display(self):
        print(self.fullname())
        print('{} {}'.format(self.email, str(self.pay)))

    def apply_amount(self):
        self.pay = int(self.pay * Employee_1.raise_amount)

    @classmethod
    def set_raise_amount(cls, amount):
        cls.raise_amount = amount

    @classmethod
    def from_string(cls, emp_str):
        first, last, email, pay = emp_str.split('-')
        return cls(first, last, email, float(pay))

    @staticmethod
    def is_workday(day):
        if day.weekday() == 5 or day.weekday == 6:
            return False
        return True

    def __repr__(self):
        return "Employee('{}', '{}', '{}', '{}')".format(self.first_name, self.last_name, self.email, self.pay)

    def __str__(self):
        return "[{}, {}, {}, {}]".format(self.first_name, self.last_name, self.email, self.pay)

    def __add__(self, other):
        return self.pay + other.pay

emp1 = Employee_1("Venkatram", "veerareddy", "venkat.veerareddy@gmail.com",50000)

emp2 = Employee_1("Srijan", "veerareddy", "srijan.veerareddy@gmail.com",500000)

print("Adding two employees:  " + str(emp1 + emp2))

emp1.apply_amount()

emp1.display()

print(emp1.__dict__)

Employee_1.set_raise_amount(1.2)
emp2.apply_amount()
emp2.display()

emp3 = Employee_1.from_string("Winnie-Veerareddy-Winnie.Veerareddy@gmail.com-60000")
emp3.display()

print("***********")
print(emp3)

my_date = datetime.date(2018, 10, 24)

print("Is today working day? " + str(Employee_1.is_workday(my_date)))


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

# Imports specifically so we can render outputs in Jupyter.
from JSAnimation.IPython_display import display_animation
from matplotlib import animation
from IPython.display import display

def auto_import():
    import numpy as np
    import torch
    import tensorflow as tf
    import gym
    import sys
    from tqdm import tqdm

##### Animation #####
def display_frames_as_gif(frames):
    """
    Displays a list of frames as a gif, with controls
    """
    fig = plt.figure("Animation",figsize=(7,5))
    ax = fig.add_subplot(111)

    #plt.figure(figsize=(frames[0].shape[1] / 72.0, frames[0].shape[0] / 72.0), dpi = 72)
    ims = []
    npad = ((4, 4), (4, 4),(0,0))
    for i in range(len(frames)):
        img = np.pad(frames[i][0], pad_width=npad, mode='constant', constant_values=255.) #pad white
        frame =  ax.imshow(img)
        ax.axis('off')
        t = ax.annotate(frames[i][1],(1,1)) # add text
        t.set_fontsize(12)

        ims.append([frame,t]) # add both the image and the text to the list of artists

    anim = animation.ArtistAnimation(fig, ims, interval=50, blit=True)
    display(display_animation(anim, default_mode='loop'))

def play_render(env,agent,episodes=1,steps=1000,display=False,gif=True):
    policy = agent.test_policy()
    for ep in range(episodes):
        frames = []
        observation = env.reset()
        total_reward = 0
        for t in range(steps):
            observation = agent.featurize_state(observation)
            if display:
                env.render()
            if gif:
                frames.append((env.render(mode = 'rgb_array'),"Cumulative reward for episode {} at time step {}: {}".format(ep,t,total_reward)))
            action, values = policy(observation)
            observation, reward, done, info = env.step(action)
            total_reward += reward
            if done:
                break
        print("Total reward for episode {}: {}".format(ep,total_reward))
        if gif:
            display_frames_as_gif(frames)
    env.close()


from django.shortcuts import render, get_object_or_404, redirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
from blog.views.search import get_query
from blog.models import Instrumentation, Type_Instrumentation, Supplier, Location
from blog.forms import InstrumentationForm

def instrumentation_list_type(request):
    """
    Instrumentation_list_type function docstring.

    This function shows the list of instrument types that are stored in this web app and they are
    ordered by creation date.

    @param request: HTML request page.

    @return: list of instrument types.
    """
    instrumentations = Type_Instrumentation.objects.all()

    return render(request, 'blog/instrumentation_list_type.html',
                  {'instrumentations': instrumentations})

@login_required(login_url='/accounts/signin/')
def instrumentation_list(request, pk):
    """
    Instrumentation_list function docstring.

    This function shows the list of instruments that are stored in this web app and they are
    ordered by creation date.

    @param request: HTML request page.
    @param pk: primary key of instrument types..

    @return: list of instrument types.
    """
    type_instrumentation = Type_Instrumentation.objects.get(pk=pk)
    instrumentation_list = Instrumentation.objects.filter(
        type_instrumentation=type_instrumentation).order_by('location')

    # Show 25 contacts per page
    paginator = Paginator(instrumentation_list, 10)

    page = request.GET.get('page')
    try:
        instrumentations = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        instrumentations = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        instrumentations = paginator.page(paginator.num_pages)

    instrumentBack = True
    type_instrumBack = False

    return render(request, 'blog/instrumentation_list.html', {'instrumentations': instrumentations,
                                                              'instrumentBack': instrumentBack,
                                                              'type_instrumBack': type_instrumBack})

word_to_search = None

@login_required(login_url='/accounts/signin/')
def instrumentation_search(request):
    """
    Instrumentation_search function docstring.

    This function search the instruments that are stored in this web app and they are
    ordered by name.

    @param request: HTML request page.

    @return: list of instruments.
    """
    global word_to_search
    query_string = ''
    found_entries = None

    page = request.GET.get('page')

    if (('searchfield' in request.GET) and request.GET['searchfield'].strip()) or page is not None:
        if page is not None:
            query_string = word_to_search

        else:
            query_string = request.GET['searchfield']
            word_to_search = query_string

        try:
            query_string = Type_Instrumentation.objects.get(name=query_string)
            instrumentation_list = Instrumentation.objects.filter(
                type_instrumentation=query_string.pk).order_by('type_instrumentation')
        except ObjectDoesNotExist:
            try:
                query_string = Supplier.objects.get(name=query_string)
                instrumentation_list = Instrumentation.objects.filter(
                    supplier=query_string.pk).order_by('type_instrumentation')
            except ObjectDoesNotExist:
                try:
                    query_string = Location.objects.get(name=query_string)
                    instrumentation_list = Instrumentation.objects.filter(
                        location=query_string.pk).order_by('type_instrumentation')
                except ObjectDoesNotExist:
                    entry_query = get_query(query_string, ['subtype_instrumentation', 'model', 'quantity',
                                                           'characteristics', 'manufacturer'])
                    instrumentation_list = Instrumentation.objects.filter(entry_query).order_by('type_instrumentation')

    # Show 25 contacts per page
    paginator = Paginator(instrumentation_list, 25)

    try:
        instrumentations = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        instrumentations = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        instrumentations = paginator.page(paginator.num_pages)

    return render(request, 'blog/instrumentation_list.html', {'instrumentations': instrumentations})

@login_required(login_url='/accounts/signin/')
def instrumentation_detail(request, pk):
    """
    Instrumentation_detail function docstring.

    This function shows the information of a instrument.

    @param request: HTML request page.

    @param pk: primary key of the instrument.

    @return: one instrument.

    @raise 404: instrument does not exists.
    """
    instrumentation = get_object_or_404(Instrumentation, pk=pk)
    instrumentBack = False
    type_instrumBack = True

    return render(request, 'blog/instrumentation_detail.html', {'instrumentation': instrumentation,
                                                                'instrumentBack': instrumentBack,
                                                                'type_instrumBack': type_instrumBack
                                                                })

@login_required(login_url='/accounts/signin/')
def instrumentation_new(request):
    """
    Instrumentation_new function docstring.

    This function shows the form to create a new instrument.

    @param request: HTML request page.

    @return: First time, this shows the form to a new instrument. If the form is completed, return
    the details of this new instrument.
    """
    if request.method == "POST":
        form = InstrumentationForm(request.POST)
        if form.is_valid():
            instrumentation = form.save(commit=False)
            instrumentation.author = request.user
            instrumentation_all = Instrumentation.objects.all()

            duplicates = False

            for data in instrumentation_all:
                if data.pk == instrumentation.pk:
                    duplicates = True
                    instrumentation_ex = data

                elif data.model == instrumentation.model and data.manufacturer == instrumentation.manufacturer:
                    if data.location == instrumentation.location:
                        duplicates = True
                        instrumentation_ex = data

            if not duplicates:
                messages.success(request, 'You have added your instrument successfully.')
                instrumentation.save()
            else:
                messages.warning(request,
                                 'Ups!! A instrument with these characteristics already exists. If you want to add a '
                                 'new to the stock, please edit it.')
                instrumentation = instrumentation_ex

            return redirect('blog:instrumentation_detail', pk=instrumentation.pk)
    else:
        form = InstrumentationForm()
    return render(request, 'blog/instrumentation_new.html', {'form': form})

@login_required(login_url='/accounts/signin/')
def instrumentation_edit(request, pk):
    """
    Instrumentation_edit function docstring.

    This function shows the form to modify a instrument.

    @param request: HTML request page.

    @param pk: primary key of the instrument to modify.

    @return: First time, this shows the form to edit the instrument information. If the form is
    completed, return the details of this instrument.

    @raise 404: instrument does not exists.
    """
    instrumentation = get_object_or_404(Instrumentation, pk=pk)
    if request.method == "POST":
        form = InstrumentationForm(data=request.POST, instance=instrumentation)
        if form.is_valid():
            instrumentation = form.save(commit=False)
            instrumentation.edited_by = request.user.username
            instrumentation_all = Instrumentation.objects.all()

            duplicates = False

            for data in instrumentation_all:
                if data.pk != instrumentation.pk:
                    if (data.model == instrumentation.model) and (data.manufacturer == instrumentation.manufacturer):
                        if data.location == instrumentation.location:
                            duplicates = True
                            instrumentation_ex = data

            if not duplicates:
                messages.success(request, 'You have updated your instrumentation.')
                instrumentation.save()
                return redirect('blog:instrumentation_detail', pk=instrumentation.pk)
            else:
                messages.warning(request, 'Already exists an instrumentation with this name.')
                return redirect('blog:instrumentation_edit', pk=instrumentation.pk)

    else:
        form = InstrumentationForm(instance=instrumentation)
    return render(request, 'blog/instrumentation_edit.html', {'form': form})

@login_required(login_url='/accounts/signin/')
def instrumentation_remove(request, pk):
    """
    Instrumentation_remove function docstring.

    This function removes a instrument.

    @param request: HTML request page.

    @param pk: primary key of the instrument to remove.

    @return: list of instruments.

    @raise 404: instrument does not exists.
    """
    instrumentation = get_object_or_404(Instrumentation, pk=pk)
    instrumentation.delete()
    return redirect('blog:instrumentation_list_type')


import logging
import math
import numpy as np
import os.path
import sys
import sklearn.metrics as metrics
import time
import tensorflow as tf

import model as model

#data set
tf.app.flags.DEFINE_string('dataset_dir', '/Users/jianbinlin/DDNN/project/stru2vec_data/garbage_account/', 'The directory where the dataset files are stored.')
tf.app.flags.DEFINE_integer('label_size', 2, 'The number of lable size for each sample.')
tf.app.flags.DEFINE_integer('fea_size', 4000, 'The number of dense feature size.')
tf.app.flags.DEFINE_integer('sample_size', 31682, 'total number of input samples')
tf.app.flags.DEFINE_integer('node_size', 981750, 'The number of nodes in training data.')

#model
tf.app.flags.DEFINE_integer('num_epochs', 1, 'The number of training epoch.')
tf.app.flags.DEFINE_integer('h1', 64, 'The number of 1st hidden node.')
tf.app.flags.DEFINE_integer('embedding_size', 12, 'embedding size')
tf.app.flags.DEFINE_integer('batch_size', 1, 'The number of samples in each batch.')
tf.app.flags.DEFINE_integer('l2', 0.0001, 'l2 factor')

#saver
tf.app.flags.DEFINE_string('check_point', 'model/model', 'the directory of checkpoint')

FLAGS = tf.app.flags.FLAGS

def aucscore(scores):
    y_true = np.array([i[0] for i in scores]).astype(int)
    y_hat = np.array([i[1] for i in scores])

    auc = metrics.roc_auc_score(y_true, y_hat, average=None)

    return max(auc)

def f1score(scores):
    test_f1 = 0.0
    y_true_te = np.array([i[0] for i in scores])
    y_hat_te = np.array([i[1] for i in scores])
    loss = np.array([i[2] for i in scores])
    sample_size = len(loss)

    for j in range(FLAGS.label_size):
        best_te_f1=0
        best_thresh=0
        best_pre = 0
        best_pre_rec = 0
        best_rec = 0
        cur_pre = 0
        cur_rec = 0
        for thresh in np.arange(40, 100, 2):
            thresh_te = np.percentile(y_hat_te[:,j], thresh)
            y_hat_te_tmp = (y_hat_te[:,j]>=thresh_te).astype(int)
            y_true_te_tmp = y_true_te[:,j].astype(int)

            te_f1 = metrics.f1_score(y_true_te_tmp, y_hat_te_tmp, pos_label=1)
            # precision = metrics.precision_score(y_true_te_tmp, y_hat_te_tmp, pos_label=1)
            # recall = metrics.recall_score(y_true_te_tmp, y_hat_te_tmp, pos_label=1)
            precision = 0.0
            recall = 0.0
            if te_f1 > best_te_f1:
                best_te_f1 = te_f1
                best_thresh = thresh
                cur_pre = precision
                cur_rec = recall
            if best_pre < precision:
                best_pre = precision
                best_pre_rec = recall
            if best_rec < recall:
                best_rec = recall

        test_f1 += best_te_f1

    test_f1 = test_f1 / FLAGS.label_size
    ave_loss = sum(loss) / sample_size

    return ave_loss, test_f1

def get_para(path):
    res = ""
    with open(path, 'r') as inf:
        for i in inf.readlines():
            if 'batch count:' not in i:
                res += i

    return res + '\n'

def main(_):

    logging_path = FLAGS.dataset_dir + "output/log/eval_{}.log".format(time.strftime("%m%d-%H_%M_%S", time.localtime()))
    logging.basicConfig(filename=logging_path, level=logging.INFO, format='%(asctime)s %(message)s\t', datefmt='%Y-%m-%d %H:%M:%S')

    train_log = "/Users/jianbinlin/DDNN/project/stru2vec_data/garbage_account/output/log/train_1221-17_32_10.log"
    logging.info(train_log)
    logging.info(get_para(train_log))

    for iter in np.arange(0, 100, 2):
        with tf.Graph().as_default() as g:
            label, fea, neig_id, neig_value = model.inputs(FLAGS.dataset_dir + "test.tfrecord")

            logit, loss = model.inference(label, fea, neig_id, neig_value)

            saver = tf.train.Saver()

            cnt = 0
            reslist = []
            with tf.Session() as sess:
                sess.run(tf.global_variables_initializer())
                sess.run(tf.local_variables_initializer())

                coord = tf.train.Coordinator()
                threads = tf.train.start_queue_runners(coord=coord)

                saver.restore(sess, FLAGS.dataset_dir + 'output/model/model-{}'.format(iter))

                for i in xrange(FLAGS.sample_size):
                    res_y, res_y_, res_loss = sess.run([label, logit, loss])

                    for i in xrange(len(res_y)):
                        reslist.append([res_y[i], res_y_[i], res_loss])

                coord.request_stop()
                coord.join(threads)

            loss, f1 = f1score(reslist)
            # loss, f1 = 0, 0
            auc = aucscore(reslist)
            logging.info("epoch: {}, loss:{:.6f}, f1:{:.6f}, auc:{:.6f}".format(iter, loss, f1, auc))

    logging.info('done')

if __name__ == '__main__':
    tf.app.run()


import json
import base64
import os

def decode_base64_to_pdf(base64_string, output_pdf_path):
    try:
        # Decode the base64 string
        decoded_data = base64.b64decode(base64_string)

        # Write the decoded data to a PDF file
        with open(output_pdf_path, "wb") as pdf_file:
            pdf_file.write(decoded_data)

        print(f"Decoding successful. PDF file saved at: {output_pdf_path}")

    except Exception as e:
        print(f"Error: {e}")

def decode_base64_values_from_json_to_pdfs(json_file_path, output_directory):
    try:
        # Read the JSON content from the input file
        with open(json_file_path, "r") as json_file:
            json_data = json.load(json_file)

        # Ensure the output directory exists
        os.makedirs(output_directory, exist_ok=True)

        # Iterate over each object in the JSON array
        for i, data_entry in enumerate(json_data):
            # Extract the base64-encoded string from the "Value" field
            base64_string = data_entry.get("Value", "")

            # Create a unique output PDF file path based on the index
            output_pdf_path = f"{output_directory}/parcel_{i + 1}.pdf"  #

            # Decode and save as a PDF file
            decode_base64_to_pdf(base64_string, output_pdf_path)

    except Exception as e:
        print(f"Error: {e}")

# Example usage
input_json_file_path = "D:/Coding/PYTHON/PythonProjects/BASE64toPDF/data/input.json"
output_directory = "D:/Coding/PYTHON/PythonProjects/BASE64toPDF/data"

decode_base64_values_from_json_to_pdfs(input_json_file_path, output_directory)


import matplotlib.pyplot as plt
import utils
import pai_io
import numpy as np

if __name__ == '__main__' :
    #filename ='../images/gray/rice.jpg'

    filename ='../images/gray/ten_coins.png'
    image=pai_io.imread(filename)
    image_1 = image.copy()

    bin_image = utils.threshold(image, 100)
    image = utils.set_image_on_lsb(image, bin_image)
    hidden_image = utils.get_image_from_lsb(image)
    fig, xs = plt.subplots(1,3)
    for i in range(3):
        xs[i].set_axis_off()
    xs[0].imshow(image_1, cmap = 'gray')
    xs[0].set_title('Original')
    xs[1].imshow(image, cmap = 'gray')
    xs[1].set_title('LSB')
    xs[2].imshow(hidden_image * 255, cmap = 'gray')
    xs[2].set_title('Hidden')
    plt.show()


import os
import pydicom
import numpy as np
import matplotlib.pyplot as plt
from flask import Flask, request, render_template, url_for, redirect
from magicsauce import *
import keras
import tensorflow as tf
import cv2

model = keras.models.load_model(weights_file, custom_objects={'correct_positive_diagnoses': correct_positive_diagnoses})
model._make_predict_function()
app = Flask(__name__, static_url_path='/static')

@app.route("/")
def fileFrontPage():
    return render_template('base.html')

@app.route("/handleUpload", methods=['POST'])
def handleFileUpload():
    if 'brainscan' in request.files:
        dcm = request.files['brainscan']
        if dcm.filename != ' ':
            ds = pydicom.dcmread(dcm)

        dcmarray = ds.pixel_array
        fname = dcm.filename

        img = window_and_scale_brain_subdural_soft(ds)
        img = cv2.resize(img, INPUT_SHAPE[:2], interpolation=cv2.INTER_LINEAR)
        tensor = np.expand_dims(img, axis=0)

        graph = tf.get_default_graph()
        with graph.as_default():
            p = model.predict(tensor)

        print(p)
        fname = dcm.filename
        max_intensity = np.max(dcmarray)
        plt.imshow(dcmarray, cmap=plt.cm.bone)
        plt.savefig('static/images/'+fname+'_raw_img.png')
        metadata = metaprint(ds)
        bsb= bsb_window(ds, fname)
    return render_template('index.html', max=max_intensity, metadata= metadata,
        url = '/static/images/'+fname+'_raw_img.png', windowing = bsb, prediction=p)

def metaprint(dataset):
    metadata = []
    for data_element in dataset:
        if data_element.name == 'Pixel Data':
            name = data_element.name
            value = 'Array of 524288 elements'
        else:
            name = data_element.name
            value = data_element.value
        metadata.append({'name' : name, 'value': value})
    return metadata

def correct_dcm(dcm):
    x = dcm.pixel_array + 1000
    px_mode = 4096
    x[x>=px_mode] = x[x>=px_mode] - px_mode
    dcm.PixelData = x.tobytes()
    dcm.RescaleIntercept = -1000

def window_image(dcm, window_center, window_width):

    if (dcm.BitsStored == 12) and (dcm.PixelRepresentation == 0) and (int(dcm.RescaleIntercept) > -100):
        correct_dcm(dcm)

    img = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept
    img_min = window_center - window_width // 2
    img_max = window_center + window_width // 2
    img = np.clip(img, img_min, img_max)

    return img

def bsb_window(dcm, filename):
    brain_img = window_image(dcm, 40, 80)
    subdural_img = window_image(dcm, 80, 200)
    bone_img = window_image(dcm, 600, 2800)

    brain_img = (brain_img - 0) / 80
    subdural_img = (subdural_img - (-20)) / 200
    bone_img = (bone_img + 800) / 2800

    plt.imshow(brain_img, cmap=plt.cm.bone)
    plt.title('Brain Matter')
    bmpath = 'static/images/'+filename+'_brain_img.png'
    plt.savefig(bmpath)

    plt.imshow(subdural_img, cmap=plt.cm.bone)
    plt.title('Subdural/Blood')
    sdpath = 'static/images/'+filename+'_subdural_img.png'
    plt.savefig(sdpath)

    #plt.imshow(bone_img, cmap=plt.cm.bone)
    plt.imshow(bone_img, cmap=plt.cm.bone)
    plt.title('Bone')
    bopath = 'static/images/'+filename+'_bone.png'
    plt.savefig(bopath)

    windowing = [
    {'url': bmpath},
    {'url': sdpath},
    {'url': bopath}]

    #bsb_img = np.array([brain_img, subdural_img, soft_img]).transpose(1,2,0)

    return windowing

if __name__ == '__main__':
    app.run(debug=True, port=9999)


from odf import easyliststyle
from odf.opendocument import OpenDocumentText
from odf.style import Style, TextProperties
from odf.text import P, List, ListItem

"""
This program shows the easyliststyle.py module.
It creates a file named "easylist_odfpy.odt"
with a bulleted list, a numbered list, and a
mixed list.
"""

bulletListSpec = '*,>,#,%'
mixedListSpec = u'1.!\u273f!a)'
numberListSpecArray = ('I', '1:', 'a')

itemList = (
    "Cats",
    ">Domestic Shorthair",
    ">Domestic Longhair",
    ">Purebred",
    ">>Russian Blue",
    ">>Siamese",
    ">>>Seal Point",
     ">>>Flame Point",
    "Dogs",
    ">Retrievers",
    ">>Golden Retriever",
    ">>Labrador Retriever",
    ">Poodles",
    ">>Toy Poodle",
    ">>Standard Poodle"
)

def createList(itemList, indentDelim, styleName):
    listArray = []
    listItem = ListItem()
    level = 0
    lastLevel = 0

    for levCount in range(0,10):
        listArray.append(None)
    listArray[0] = List()

    for item in itemList:
        level = 0;
        while (level < len(item) and item[level] == indentDelim):
            level +=1
        item = item[level:]

        if (level > lastLevel):    # open the sub-levels
            for levCount in range(lastLevel+1, level+1):
                listArray[levCount] = List()
        elif (level < lastLevel):    # close off the intervening lists
            for levCount in range(lastLevel, level, -1):
                listArray[levCount-1].childNodes[-1].addElement(listArray[levCount])

        # now that we are at the proper level, add the item.
        listArray[level].setAttribute( 'stylename', styleName );
        listItem = ListItem()
        para = P(text=item);
        listItem.addElement(para);
        listArray[level].addElement(listItem);
        lastLevel = level;

    # close off any remaining open lists
    for levCount in range(lastLevel, 0, -1):
        listArray[levCount-1].childNodes[-1].addElement(listArray[levCount])
    return listArray[0]

textdoc = OpenDocumentText()

s = textdoc.styles
listStyle = easyliststyle.styleFromString('bullet1', bulletListSpec,
    ',', '0.6cm', easyliststyle.SHOW_ONE_LEVEL)
s.addElement(listStyle)

listElement = createList(itemList, '>', 'bullet1')
textdoc.text.addElement(listElement)

para = P(text="-----------------------");
textdoc.text.addElement(para)

listStyle = easyliststyle.styleFromList('num1', numberListSpecArray,
    '0.25in', easyliststyle.SHOW_ALL_LEVELS)
s.addElement(listStyle)

listElement = createList(itemList, '>', 'num1')
textdoc.text.addElement(listElement)

para = P(text="-----------------------");
textdoc.text.addElement(para)

listStyle = easyliststyle.styleFromString('mix1', mixedListSpec,
    '!', '0.8cm', easyliststyle.SHOW_ONE_LEVEL)
s.addElement(listStyle)

listElement = createList(itemList, '>', 'mix1')
textdoc.text.addElement(listElement)

textdoc.save("easylist_odfpy.odt")

# vim: set expandtab sw=4 :