# =============================================================================
# Complexity of this algortihm is O(log i)
# where, i is the index of the element being searched for.
# This is better than O(log n)
# where, n is the number of elements in the array.
# Hence, this performs better than Binary Search.
# =============================================================================

def exponential_search(search_list, left, right, target):
    if search_list[0] == target:
        return "Found at 0"

    i = 1
    while i < len(search_list) and search_list[i] <= target:
        i *= 2

    index = binary_search(search_list, i//2, min(i, len(search_list)-1), target)
    return index

def binary_search(search_list, left, right, target):
    if left > right:
        return "Not Found!"

    middle = (left + right) // 2

    if search_list[middle] == target:
        return "Found at " + str(middle)
    elif search_list[middle] > target:
        return binary_search(search_list, left, middle - 1, target)
    else:
        return binary_search(search_list, middle + 1, right, target)

def main():
    from random import sample

    search_list = sorted(sample([i for i in range(30)], 10))
    search_value = 10

    print(search_list, end = "\n\n")
    print(exponential_search(search_list, 0, len(search_list)-1, search_value))

main()


import numpy as np

def p_vec_from(n, sp_idxs):
    p_vec = -np.ones((n, ), dtype=np.int32)
    for i in range(len(sp_idxs)):
        P_idxs = sp_idxs[i]
        p_vec[P_idxs] = i
    return p_vec

def get_points_neighbours(sp_idxs, sndrs, rcvrs):
    sortation = np.argsort(sndrs)
    senders = sndrs[sortation]
    receivers = rcvrs[sortation]
    uni_senders, uni_idxs, uni_counts = np.unique(senders, return_index=True, return_counts=True)

    point_idxs_list = []
    neighbours_list = []

    for i in range(uni_senders.shape[0]):
        node = uni_senders[i]
        start = uni_idxs[i]
        stop = start + uni_counts[i]
        P_idxs = sp_idxs[i]
        neighbours = np.unique(receivers[start:stop])
        point_idxs_list.append(P_idxs)
        neighbours_list.append(neighbours)
    pns = (point_idxs_list, neighbours_list)
    return pns


#matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
#import control

#import warnings
#warnings.filterwarnings('ignore')

import math

#from pylab import *

# Requisitos de projeto do equalizador parametrico

fo = 8000 # frequencia central em Hz
bf = 500  # banda para projeto do rejeita banda em Hz
gdB = 10 # ganho em dB na frequencia fo

fa = 48000 # frequencia de amostragem em Hz

lbt = 500 # largura de banda de transicao para calculo da ordem do filtro
fsi = fo - bf/2 - lbt # frequencia limite da banda de passagem inferior em Hz
fpi = fo - bf/2       # frequencia inferior da banda de rejeicao em Hz
fps = fo + bf/2       # frequencia superior da banda de rejeicao em Hz
fss = fo + bf/2 + lbt  # frequencia limite da banda de passagem superior em Hz

# Janela de Blackman: ap = 0.002 dB e as = 74 dB
# Ver formulario de consulta disponível no Moodle

# Frequencias normalizadas (w = 2*pi*f/fa)
wsi = 2*math.pi*fsi/fa
wpi = 2*math.pi*fpi/fa
wps = 2*math.pi*fps/fa
wss = 2*math.pi*fss/fa

# Fequencia de corte do filtro passa-baixas
w1 = (wpi+wsi)/2
w2 = (wss+wps)/2
wc = (w2-w1)/2

# Frequencia central do passa-banda
w0 = (w2+w1)/2

# Ordem N do filtro (ver formulario de consulta disponível no Moodle)

# Largura da banda de transicao para projeto do rejeita banda
dw = min((wpi-wsi),(wss-wps))

# N = 4*pi/dw  # janela retangular
# N= 2*ceil(N/2) # ordem deve ser par
# wn = boxcar(N+1)

# N = 8*pi/dw  # janela de von Hann
# N= 2*ceil(N/2) # ordem deve ser par
# wn = hanning(N+1)

# N = 8*pi/dw  # janela de von Hann
# N= 2*ceil(N/2) # ordem deve ser par
# wn = hamming(N+1)

deeee = (12*math.pi/dw)  # janela de Blackman
N = 2*math.ceil(deeee/2) # ordem deve ser par
#wn = blackman(N+1)
wn = list(range(N))
for i in range(1,N):
    wn[i] = 0.42-0.5*math.cos((2*math.pi*i)/((N+1)-1))+0.08*math.cos((4*math.pi*i)/((N+1)-1))

# Resposta do filtro ideal atrasada de N/2 amostras para ficar causal
# Atencao para a indeterminacao em n = N/2!
# Ver formulario de consulta disponível no Moodle
hlp = list(range(N))
hbp = list(range(N))
hbr = list(range(N))
hn = list(range(N))
for n in range(1,N):
    hlp[n] = math.sin(wc*(n+1e-8-N/2))/(n+1e-8-N/2)/math.pi # passa-baixas
for n in range(1,N):
    hbp[n] = 2*math.cos(w0*(n-N/2))*hlp[n] # passa-banda
for n in range(1,N):
    hbr[n] = hbp[n]*(-1)

gg = (N/2)+1
gg=math.ceil(gg)
#print(hlp)
print(N)
print(gg)
#print(hbr)
hbr[gg] = 1 + hbr[gg] # rejeita-banda
gdb = 10^(math.ceil(gdB/20))
for n in range(1,N):
    hn[n] = hbr[n] + gdb*hbp[n]

# Resposta a amostra unitaria (impulso) do filtro a ser implementado
for n in range(1,N):
    hn[n] = hn[n]*wn[n]

print(hn)

#mag,phase,omega = control.bode(hn)
# Visualizacao da resposta do filtro
#fvtool(hn, 'fs',fa)


#!/usr/bin/env python3
import os
import utils.mojo_utils as mojo_utils
import netaddr

from zaza import model
from zaza.openstack.utilities import (
    cli as cli_utils,
    juju as juju_utils,
    openstack as openstack_utils,
)

class VipPool():
    def __init__(self, prov_net_id=None):
        try:
            cacert = os.path.join(os.environ.get('MOJO_LOCAL_DIR'),
                                  'cacert.pem')
            os.stat(cacert)
        except FileNotFoundError:
            cacert = None
        keystone_session = openstack_utils.get_undercloud_keystone_session(
            verify=cacert)
        neutronc = openstack_utils.get_neutron_session_client(
            keystone_session)
        if prov_net_id:
            net = neutronc.list_networks(id=prov_net_id)['networks'][0]
        else:
            net = openstack_utils.get_admin_net(neutronc)
        subnet_id = net['subnets'][0]
        subnet = neutronc.list_subnets(id=subnet_id)['subnets'][0]
        allocation_pools = subnet['allocation_pools']
        self.cidr = subnet['cidr']
        self.highest_assigned = netaddr.IPAddress(allocation_pools[0]['end'])
        # XXX look away now, nothing to see here, move along.
        #     If there is less than 30 free ips in the network after the top
        #     dhcp ip then eat into the top of the dhcp range
        available_ips = []
        for element in list(netaddr.IPNetwork(self.cidr)):
            if element == netaddr.IPAddress(self.highest_assigned) or \
                    available_ips:
                available_ips.append(element)
        if len(available_ips) < 30:
            self.highest_assigned = self.highest_assigned - 30

    def get_next(self):
        next_ip = self.highest_assigned + 1
        if next_ip in list(netaddr.IPNetwork(self.cidr)):
            self.highest_assigned = self.highest_assigned + 1
            return next_ip
        else:
            raise Exception("vip pool exhausted")

cli_utils.setup_logging()
vp = VipPool()
juju_status = juju_utils.get_full_juju_status()
for application in juju_status.applications.keys():
    if 'vip' in model.get_application_config(application).keys():
        model.set_application_config(application, {'vip': vp.get_next()})
mojo_utils.juju_wait_finished()


#!/usr/bin/env python3
# -*- coding=utf-8 -*-
# 本脚由亁颐堂现任明教教主编写，用于乾颐盾Python课程！
# 教主QQ:605658506
# 亁颐堂官网www.qytang.com
# 教主技术进化论拓展你的技术新边疆
# https://ke.qq.com/course/271956?tuin=24199d8a
import re
from jinja2 import Template
tem_path = './jinja2_template/'

def netconf_if_ip(interface, address, mask):
    interface_name, interface_no = re.match('([a-zA-Z]*)([0-9].*)', interface).groups()
    with open(tem_path + 'interface_ip.xml') as f:
        netconf_template = Template(f.read())
    netconf_payload = netconf_template.render(if_type=interface_name,
                                              if_no=interface_no,
                                              ip_address=address,
                                              net_mask=mask)
    return netconf_payload

def netconf_if_no_shutdown(interface, status=True):
    with open(tem_path + 'interface_no_shutdown.xml') as f:
        netconf_template = Template(f.read())
    netconf_payload = netconf_template.render(interface_name=interface, interface_status='true' if status else 'false')
    return netconf_payload

def netconf_ospf_router_id(process_id, router_id):
    with open(tem_path + 'ospf_router_id.xml') as f:
        netconf_template = Template(f.read())
    netconf_payload = netconf_template.render(process_id=process_id, router_id=router_id)
    return netconf_payload

def netconf_ospf_network(process_id, ip, mask, area):
    with open(tem_path + 'ospf_network.xml') as f:
        netconf_template = Template(f.read())
    netconf_payload = netconf_template.render(process_id=process_id, network=ip, wild_mask=mask, area=area)
    return netconf_payload

def netconf_monitor_cpu(monitor_type):
    with open(tem_path + 'monitor_cpu.xml') as f:
        netconf_template = Template(f.read())
    netconf_payload = netconf_template.render(monitor_type_use=monitor_type)
    return netconf_payload

if __name__ == '__main__':
    import lxml.etree as ET
    if_ip_str = netconf_if_ip('Loopback0', '61.128.1.1', '255.255.255.0')
    print(if_ip_str)
    # 模板替换结果就是一个字符串
    print(type(if_ip_str))
    parser = ET.XMLParser(recover=True)
    # 从字符串fromstring, 转换为XML
    tree = ET.ElementTree(ET.fromstring(if_ip_str, parser=parser))
    # 已经转换XML对象
    print(type(tree))
    # 写入文件
    with open("./jinja2_xml/if_ip_xml.xml", "wb") as f:
        f.write(ET.tostring(tree))


import pyrealsense2 as rs
import numpy as np

class RGB_Depth:
    def __init__(self):
        pipeline = rs.pipeline()
        config = rs.config()

        # Get device product line for setting a supporting resolution
        pipeline_wrapper = rs.pipeline_wrapper(pipeline)
        pipeline_profile = config.resolve(pipeline_wrapper)
        device = pipeline_profile.get_device()
        device_product_line = str(device.get_info(rs.camera_info.product_line))

        found_rgb = False
        for s in device.sensors:
            if s.get_info(rs.camera_info.name) == 'RGB Camera':
                found_rgb = True
                break
        if not found_rgb:
            print("The demo requires Depth camera with Color sensor")
            exit(0)

        config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
        config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)

        # Start streaming
        profile = pipeline.start(config)

        # Getting the depth sensor's depth scale (see rs-align example for explanation)
        depth_sensor = profile.get_device().first_depth_sensor()
        depth_scale = depth_sensor.get_depth_scale()
        # print("Depth Scale is: " , depth_scale)

        # We will be removing the background of objects more than
        #  clipping_distance_in_meters meters away
        clipping_distance_in_meters = 1 #1 meter
        clipping_distance = clipping_distance_in_meters / depth_scale

        # Create an align object
        # rs.align allows us to perform alignment of depth frames to others frames
        # The "align_to" is the stream type to which we plan to align depth frames.
        align_to = rs.stream.color
        align = rs.align(align_to)

        self.pipeline = pipeline
        self.align = align

    def grab(self):
        # Get frameset of color and depth
        frames = self.pipeline.wait_for_frames()
        # frames.get_depth_frame() is a 640x360 depth image

        # Align the depth frame to color frame
        aligned_frames = self.align.process(frames)

        # Get aligned frames
        aligned_depth_frame = aligned_frames.get_depth_frame() # aligned_depth_frame is a 640x480 depth image
        color_frame = aligned_frames.get_color_frame()

        # Validate that both frames are valid
        if not aligned_depth_frame or not color_frame:
            print("Unable to read video frame")
            return None

        depth_data = np.asanyarray(aligned_depth_frame.get_data())
        color_image = np.asanyarray(color_frame.get_data())

        return color_image, depth_data

    def __del__(self):
        self.pipeline.stop()


from logging import raiseExceptions
import sys
encodeBits=12
class Solution:

    def getSumPositive(self, a: int, b: int) -> int: # deal with positives only

        aBin=bin(a)[2:]
        bBin=bin(b)[2:]
        diff=len(aBin)-len(bBin)
        maxlen=max(len(aBin),len(bBin))
        if diff>0:
            bBin="0"*diff+bBin
        else:
            aBin="0"*diff+aBin

        ret=[0]*(maxlen+1)
        carry="0"

        for i in range(maxlen-1,-1,-1):
            num1=[aBin[i],bBin[i],carry].count("1")
            if num1==0:
                ret[i+1]=0
                carry="0"
            elif num1==1:
                ret[i+1]=1
                carry="0"
            elif num1==2:
                ret[i+1]=0
                carry="1"
            elif num1==3:
                ret[i+1]=1
                carry="1"
            else:
                raise Exception("exception occurs")

        if carry=="1":
            ret[0]="1"

        retNbr=int(ret,2)
        return retNbr

    def encode(self,decimal) -> str: # encode 12 bits
        binDecimal=bin(decimal)

        if binDecimal[0]=="-":
            temp=binDecimal[3:]
            temp="0"*(encodeBits-len(temp))+temp
            #ret=["0"]*encodeBits
            ret=[char for char in temp]
            met1=False
            for i in range(encodeBits-1,-1,-1):
                if met1:
                    if temp[i]=="1":
                        ret[i]="0"
                    else:
                        ret[i]="1"
                if temp[i]=="1":
                    met1=True
            retStr=""
            for i in ret:
                retStr+=i
            return retStr

        else:
            ret=binDecimal[2:]
            ret="0"*(encodeBits-len(ret))+ret
            return ret

    def decode(self,comp)->int: #return decimal
        if comp[0]=="0":
            return int(comp,2)
        else:
            #ret=["0"]*encodeBits
            ret=[char for char in comp]

            met1=False
            #print(comp)
            for i in range(encodeBits-1,-1,-1):
                #print(ret)
                if met1:
                    if comp[i]=="1":
                        ret[i]="0"
                    else:
                        ret[i]="1"
                if comp[i]=="1":
                    met1=True
            retStr=""
            for i in ret:
                retStr+=i
            return (-1)*(int(retStr,2))

    def add(self,aComp,bComp)->str:
        carry="0"
        ret=["0"]*encodeBits
        for i in range(encodeBits-1,-1,-1):
            num1=[aComp[i],bComp[i],carry].count("1")
            if num1==0:
                ret[i]="0"
                carry="0"
            elif num1==1:
                ret[i]="1"
                carry="0"
            elif num1==2:
                ret[i]="0"
                carry="1"
            elif num1==3:
                ret[i]="1"
                carry="1"
            else:
                raise Exception("exception occurs")

        # if carry=="1":
        #     ret[0]="1"

        retStr=""
        for i in ret:
            retStr+=i

        return retStr

    def getSum(self, a: int, b: int) -> int:
        encodedA=self.encode(a)
        encodedB=self.encode(b)
        #print(encodedA,encodedB)
        suminComp=self.add(encodedA,encodedB)
        ret=self.decode(suminComp)
        return ret

class Solution2:

    def twos(self,val):
        b=val.to_bytes(4,byteorder=sys.byteorder,signed=False)
        return int.from_bytes(b,byteorder=sys.byteorder,signed=True)

    def getSum(self, a: int, b: int) -> int:
        a=a&0xFFFFFFFF
        b=b&0xFFFFFFFF
        carry=0
        while b:
            carry=a&b
            a=a^b
            b=(carry<<1)&0xFFFFFFFF

        return self.twos(a)
        #if a&(1<<31)==1:
            #return
        #return a

    def getComp(self,a):
        if a>=0:
            return bin(a)[2:]
        else:
            return bin(~(-a-1))

sol=Solution2()
print(sol.getComp(-5))
print(bin(-5&0xFFFFFFFF))


import os
import shutil
import csv
import logging
import gzip
import glob
import re
import multiprocessing
from amromics.libs.bioseq import read_sequence_file
from Bio import SeqIO
from Bio.Seq import Seq
from amromics.utils.command import run_command
from amromics.utils.utils import get_open_func
logger = logging.getLogger(__name__)
NUM_CORES_DEFAULT = multiprocessing.cpu_count()
def annotate_prokka(prefix_name,assembly,genus=None,species=None, strain=None,gram=None, base_dir='.',  overwrite=False,timing_log=None, threads=0):
    """
        Run annotation process using prokka
        :param read_data: result holder
        :param base_dir: working directory
        :return: path to output file in result holder
    """
    if threads == 0:
        threads = NUM_CORES_DEFAULT

    path_out = os.path.join(base_dir, prefix_name+'_prokka' )
    if not os.path.exists(path_out):
        os.makedirs(path_out)

    annotation_gbk=  os.path.join(path_out, prefix_name + '.gbk.gz')
    annotation_gff= path_out+'/'+str(prefix_name)+'.gff.gz'
    annotation_faa = path_out+'/'+str(prefix_name)+'.faa.gz'
    annotation_ffn = path_out+'/'+str(prefix_name)+'.ffn.gz'
    annotation_fna = path_out+'/'+str(prefix_name)+'.fna.gz'
    if os.path.isfile(annotation_gff) and os.path.isfile(annotation_gbk) and (not overwrite):
        # Dont run again if gff/gbk file exists
        logger.info('GFF and GBK files found, skip annotating')
        return annotation_gff,annotation_faa,annotation_ffn,annotation_fna,annotation_gbk
    gunzip_fasta=assembly
    if assembly.endswith('.gz'):
        gunzip_fasta = os.path.join(path_out, prefix_name + '.fin')
        cmd = 'gunzip -c {} > {}'.format(assembly, gunzip_fasta)
        run_command(cmd)
    cmd = 'prokka --force --cpus {threads} --addgenes --mincontiglen 200'.format(threads=threads)
    cmd += ' --prefix {sample_id} --locus {sample_id} --outdir {path} '.format(sample_id=prefix_name, path=path_out)
    if not genus ==None and genus:
        cmd += ' --genus ' +genus
    if not species  ==None and species:
        species = species.replace(' ','_')
        cmd += ' --species ' + species
    if not strain  ==None and strain:
        cmd += ' --strain ' + strain
    if not gram  ==None and gram:
        cmd += ' --gram ' + gram
    cmd += ' ' + gunzip_fasta
    cmd = "bash -c '{}'".format(cmd)
    ret = run_command(cmd, timing_log)
    if ret != 0:
        raise Exception('Command {} returns non-zero ()!'.format(cmd, ret))

    for file_name in glob.glob(os.path.join(path_out, '*')):
        ext = file_name[-3:]
        if ext in ['gff', 'gbk', 'ffn','faa','fna']: # fna?
            run_command('gzip {}'.format(file_name))
        else:
            os.remove(file_name)

    return annotation_gff,annotation_faa,annotation_ffn,annotation_fna,annotation_gbk
def parseGFF(sample_id,gff_file_in,base_dir,overwrite=False):

    path_out = os.path.join(base_dir, sample_id+'_prokka' )

    if not os.path.exists(path_out):
        os.makedirs(path_out)
    annotation_gff= path_out+'/'+str(sample_id)+'.gff.gz'
    annotation_ffn = path_out+'/'+str(sample_id)+'.ffn.gz'
    annotation_fna = path_out+'/'+str(sample_id)+'.fna.gz'
    annotation_faa = path_out+'/'+str(sample_id)+'.faa.gz'
    if os.path.isfile(annotation_gff)  and (not overwrite):
        # Dont run again if gff/gbk file exists
        logger.info('GFF files found, skip annotating')
        return annotation_gff,annotation_faa,annotation_ffn,annotation_fna
    found_fasta = False
    open_func = get_open_func(gff_file_in)
    fasta_file=open(path_out+'/'+str(sample_id)+'.fna','w')
    bed_records = []
    with open_func(gff_file_in,'rt') as in_fh,gzip.open(annotation_gff, 'wt') as gff_re:
        last_cds=""
        id_number=0

        gene_index = 0
        seq_id = None
        suffix = 1
        for line in in_fh:

            if found_fasta == True:
                gff_re.write(line.replace('/','_'))
                fasta_file.write(line)
                continue
            if re.match(r"^##FASTA", line) != None:
                found_fasta = True
                gff_re.write(line)
                continue
            if re.match(r"^#", line) != None:
                gff_re.write(line)
                continue
            line = line.rstrip('\n')
            cells = line.split('\t')
            if cells[2] != 'CDS':
                continue

            ##print(line)
            #print(gff_file_in)
            strand = cells[6]
            start = int(cells[3])
            end = int(cells[4])
            length = end - start + 1
            if length % 3 != 0:
                continue
            cells[0] = cells[0].replace('-','_')
            if seq_id != cells[0]:
                seq_id = cells[0]
                gene_index = 0
            tags=cells[8].split(';')
            gene_id=None
            gene_name = ''
            gene_product = ''
            for tag in tags:
                gene = re.match(r"^gene=(.+)", tag)
                if gene != None:
                    gene_name = gene.group(1)
                    gene_name = re.sub(r'\W', '_', gene_name)
                    continue
                product = re.match(r"^product=(.+)", tag)
                if product != None:
                    gene_product = product.group(1)
            gene_id = sample_id + '_' +f'{suffix:05d}'
            #print(gene_id)
            suffix += 1
            newdes="ID="+gene_id+";"
            for i in range(2,len(tags)):
                newdes=newdes+tags[i]+";"
            newline=""
            for i in range(len(cells)-1):
                newline=newline+cells[i]+"\t"
            newline=newline+newdes+"\n"
            gff_re.write(newline)
            bed_records.append((cells[0].strip(), start - 1, end, gene_id, strand,gene_product))
            gene_index += 1
    fasta_file.close()
    seqs = {}
    #count_contigs=0
    for seq in read_sequence_file(path_out+'/'+str(sample_id)+'.fna'):
        seqs[seq.name] = seq
        #    count_contigs=count_contigs+1
        #print(count_contigs)
        #print(len(seqs.keys()))
    gene_seqs = []
    protein_seqs = []
    for bed_record in bed_records:
        (seq_id, start, end, gene_id, strand,gene_product) = bed_record
        if seq_id not in seqs.keys():
            print(seq_id+" not in fna")
            continue
        seq = seqs[seq_id]
        subseq = Seq(str(seq.sequence)[start:end])
        gene_seq = SeqIO.SeqRecord(seq=subseq,id= gene_id,description=gene_product)
        gene_seqs.append(gene_seq)
        protein_seq = gene_seq.translate(table=11, stop_symbol='')
        protein_seq.id = gene_id
        protein_seq.description = gene_product
        protein_seqs.append(protein_seq)
    with gzip.open(annotation_ffn, 'wt') as ffn_fh:
        SeqIO.write(gene_seqs, ffn_fh, 'fasta')
    with gzip.open(annotation_faa, 'wt') as faa_fh:
        SeqIO.write(protein_seqs, faa_fh, 'fasta')
    for file_name in glob.glob(os.path.join(path_out, '*')):
        ext = file_name[-3:]
        if ext in ['fna']: # fna?
            run_command('gzip {}'.format(file_name))

    return annotation_gff,annotation_faa,annotation_ffn,annotation_fna


# Last Edited 01/19/18
import tkinter as tk
from tkinter import ttk
import tkinter.messagebox as tkmb
import tkinter.filedialog as tkfd
import sqlite3
import collections
import datetime as dt
import re
import os
import os.path

from guiBlocks import *
from generalDataLogFunctions import *

bellaCareClasses=["None","A few hours or less","Most of the day","Evening","Overnight","Note"]
bellaSleepClasses=["None","Stayed in Bed (We woke her)","Late Morning (Post-0700)","Mid Morning (0500-0700)","Early Morning (pre-0500)","Single Bed"]

bellaData = collections.namedtuple('bellaData','date wakeTime wakeNote')
bellaCareData = collections.namedtuple('bellaCareData','date careTime careNote')

bellaDataDefault = bellaData(curDate(),"Stayed in bed","Woke her when I left for work at 7:00")
bellaCareDefault = bellaCareData(curDate(),"A few hours","None")

#
#   Arabella Record Data
#

class bellaDataFrm(tk.Frame):
    def __init__(self,master = None, dbConn = None, outerTabs = None, initVals = (bellaDataDefault,bellaCareDefault)):
        tk.Frame.__init__(self,master)
        self.careClasses=bellaCareClasses
        self.wakeClasses=bellaSleepClasses
        self.grid()
        self.date = dateFieldLeft(self,"Date",0,0,2)

        self.wakeFrame = formFrame(self,"Wake Data",1,0,3)
        self.recentFrame = formFrame(self,"Recent Dates",2,0,3,3)
        self.careFrame = formFrame(self,"Care Data",1,3,3,2)

        self.wakeTime = dataComboBox(self.wakeFrame.frame,"Wake Time Category",self.wakeClasses,0,0,1)
        self.wakeNote = dataTextBox(self.wakeFrame.frame,"Wake Time Note",1,0,3,boxW=40)

        self.careTime = dataComboBox(self.careFrame.frame,"Care Time Category",self.careClasses,0)
        self.careNote = dataTextBox(self.careFrame.frame,"Care Note",1,0,3,boxW=40)
        self.saveCareOnly = actionBtn(self.careFrame.frame,"Save Care Only",lambda event:self.saveCare(),2)

        self.startDate = dateFieldLeft(self.recentFrame.frame,"Start Date",0,initVal=deltaDate(-14))
        #self.dateView = messageListbox(self.recentFrame.frame,1,0,2)

        self.refreshView = actionBtn(self,"Refresh View",lambda event:self.popWakeView(),0,4)
        #self.dateView.bindField("<ButtonRelease-1>",self.pickDate)

        #self.careFrame = formFrame(self,None,1,5,1,5)
        self.careViewCfg = [
            treeColTpl("time",100,"Time"),
            treeColTpl("note",100,"Note")]
        self.careView = treeView(self.careFrame.frame,"Bella Care",self.careViewCfg,3,0,1,4)
        self.careView.setIconWidth(dateColWidth)
        self.careView.bindField("<<TreeviewSelect>>",self.pickCare)
        self.careView.bindField("<Double-Button-1>",self.delCare)

        self.wakeViewCfg = [
            treeColTpl("wakeCat",150,"Wake Category"),
            treeColTpl("wakeNote",250,"Wake Note")]
        self.wakeView = treeView(self.recentFrame.frame,"Wake Data",self.wakeViewCfg,1,0,2)
        self.wakeView.setIconWidth(dateColWidth)
        self.wakeView.bindField("<<TreeviewSelect>>",self.pickDate)
        self.wakeView.bindField("<Double-Button-1>",self.delWake)

        buttonRow = 9
        self.saveBellaBtn = actionBtn(self,"Save Arabella Data",lambda event:self.saveBella(),buttonRow,0)
        self.clrBtn = actionBtn(self,"Clear Data",lambda event:self.clearData(),buttonRow,1)
        self.saveFlag = dataCheckBox(self,"Save Data",False,buttonRow,2)
        self.refreshView = actionBtn(self,"Refresh View",lambda event:self.popWakeView(),buttonRow,3)

        self.journalJump = actionBtn(self,"Journal Data",lambda event:self.jumpTabs(-1),buttonRow,4)
        self.defconJump = actionBtn(self,"Defcon Data",lambda event:self.jumpTabs(1),buttonRow,5)

        self.dbConn = dbConn
        self.tabs = outerTabs
        self.dataType = "Arabella"
        self.dataTable = "BellaData"
        self.dataCols = ["Date","WakeTime","SleepNote"]
        self.careTable = "BellaCareTracking"
        self.careCols = ["Date","CareTime","CareNote"]

        self.popWakeView()
        self.setData(initVals)

    def pickDate(self,Event=None):
        curLine = self.wakeView.getSelection()[0]
        curDate = self.wakeView.getText(curLine)
        (wakeData,careData)=self.fetchArabellaData(curDate)
        self.setWake(wakeData)
        print(careData)
        #should also set the current line in careView to the appropriate date
    def delWake(self,event=None):
        curLine = self.wakeView.getSelection()[0]
        curDate = self.wakeView.getText(curLine)
        (wakeData,careData)=self.fetchArabellaData(curDate)
        curID = self.careView.getDataID(selIID)

    def pickCare(self,event=None):
        selIID = self.careView.getSelection()[0]
        selCare = self.careView.getValues(selIID)
        careData = self.careView.getValues(selIID)
        if len(careData)<2:
            print("Date line",careData)
            return

        careTime,careTxt = careData
        self.careTime.setVal(careTime)
        self.careNote.setVal(careTxt)
        dateIID = self.careView.getCurParent(selIID)
        wkDate = self.careView.getText(dateIID)
        self.date.setDateText(wkDate)
        (wakeData,careData)=self.fetchArabellaData(wkDate)
        self.setWake(wakeData)

        # should pick the date, fetch the appropriate care data and populate the gui
        # should also call up the appropriate date in the wake view.
        # Maybe use ctrl-click to delete the line, to eliminate duplicates (similar to the book view

    def delCare(self,event=None):
        # First, identify the record to be deleted
        # Second, find the
        selIID = self.careView.getSelection()[0]
        careData = self.careView.getValues(selIID)
        print(careData)
        if len(careData)<2:
            return
        curID = self.careView.getDataID(selIID)
        print(curID)
        sqlStr = "DELETE FROM {} WHERE {} = ?".format(self.careTable,"BellaCareID")
        self.dbConn.execute(sqlStr,[curID])
        self.dbConn.commit()
        self.popWakeView()

    def connectStatus(self,statusBox):
        self.statusBox = statusBox

    def popWakeView(self):
        self.wakeView.clearTree()
        tmpStr=str.format("SELECT * FROM {} WHERE Date>=? ORDER BY Date",self.dataTable)
        res=self.dbConn.execute(tmpStr,[self.startDate.getDateStamp()]).fetchall()
        for ln in res:
            self.wakeView.addLineID("",dateFromStamp(ln["Date"]),[ln["WakeTime"],ln["SleepNote"]],dataID=ln["ArabellaID"])

        self.careView.clearTree()
        tmpStr=str.format("SELECT * FROM {} WHERE Date>=? ORDER BY Date",self.careTable)
        res=self.dbConn.execute(tmpStr,[self.startDate.getDateStamp()]).fetchall()
        for ln in res:
            wkID = ln["BellaCareID"]
            wkDate = dateFromStamp(ln["Date"])
            lineIID=self.careView.getIID(wkDate)
            if lineIID == "":
                lineIID=self.careView.addLine(lineIID,wkDate,[])
            self.careView.addLineID(lineIID,"",[ln["CareTime"],ln["CareNote"]],dataID=ln["BellaCareID"])

    def saveBella(self,event=None):
        self.saveFlag.setVal(True)
        self.saveData()
        self.saveFlag.setVal(False)

    def jumpTabs(self,delta):
        self.tabs.select(3+delta)

    def getData(self):

        newWake = bellaData(
           self.date.getDateText(),
           self.wakeTime.getVal(),
           self.wakeNote.getVal())
        newCare = bellaCareData(
           self.date.getDateText(),
           self.careTime.getVal(),
           self.careNote.getVal())
        return (newWake,newCare)

    def saveData(self):
        if not saveNotify("Arabella",self.saveFlag.getVal()):
            return False

        if not self.saveFlag.getVal():
            return True

        wake,care = self.getData()
        wkStamp = self.date.getDateStamp()
        saveWake = wake._replace(date=wkStamp)
        saveCare = care._replace(date=wkStamp)

        replaceSql(self.dbConn,self.dataTable,self.dataCols,saveWake)
        if self.careTime.getVal()!="None":
            insertSql(self.dbConn,self.careTable,self.careCols,saveCare)
            self.popWakeView()

        self.statusBox.addLine("Saved Bella Data for {}".format(self.date.getDateText()))

        return True

    def saveCare(self,event=None):
        if self.careTime.getVal()=="None":
            return
##        sqlStr = str.format("INSERT into {} ({}) VALUES ({})",self.careTable,",".join(self.careCols),"?"+",?"*(len(self.careCols)-1))
##        self.dbConn.execute(sqlStr,saveCare)
        wake,care= self.getData()
        dateStamp = self.date.getDateStamp()
        newWake = wake._replace(date = dateStamp)
        newCare = care._replace(date = dateStamp)
        insertSql(self.dbConn,self.careTable,self.careCols,newCare)
        self.addCareToView(care)
        self.statusBox.addLine("Saved Care Only Data for {}".format(self.date.getDateText()))

    def setWake(self,newWake):
        self.date.setDateText(newWake.date)
        self.wakeTime.setVal(newWake.wakeTime)
        self.wakeNote.setVal(newWake.wakeNote)

    def setData(self,newData):
        newWake,newCare = newData
        self.date.setDateText(newWake.date)
        self.careTime.setVal(newCare.careTime)
        self.careNote.setVal(newCare.careNote)
        self.wakeTime.setVal(newWake.wakeTime)
        self.wakeNote.setVal(newWake.wakeNote)
        return

    def clearData(self):
        self.date.setDateText(curDate())
        self.careTime.setVal("")
        self.careNote.setVal("")
        self.wakeTime.setVal("")
        self.wakeNote.setVal("")
        return

    def setDate(self,newDate):
        self.date.setDateText(newDate)
        #tkmb.showinfo(self,self.fetchArabellaData(newDate))
        # Need to decide what to do here when the date is changed;
        # What data to display; what warnings if no existing data

    def fetchArabellaData(self,date):
        wkDate = stampFromDate(date)
        print("Fetching Date ",date," or stamp ",wkDate)
        cur = self.dbConn.execute("SELECT * FROM BellaData WHERE Date=(?)",(wkDate,))
        res = cur.fetchone()
        if res != None:
            newBella = bellaData(dateFromStamp(res["Date"]),
                                   res["WakeTime"],
                                   res["SleepNote"])
        else:
            newBella = bellaDataDefault

        cur = self.dbConn.execute("SELECT * FROM BellaCareTracking WHERE Date=(?)",(wkDate,))
        res = cur.fetchone()
        if res != None:
            newCare = bellaCareData(dateFromStamp(res["Date"]),
                                    res["CareTime"],
                                    res["CareNote"])
        else:
            newCare = bellaCareDefault

        return (newBella,newCare)

    def addCareToView(self,newCare):
        # Interesting -- this version of the careView lists the caretaker as the text, with the duration
        # and the note text as the values.  This is not preserved when the view is populated.
        # I honestly don't know which protocol I prefer.  It should be consistent, at least, however
        dateIID = self.careView.getIID(newCare.date)
        parseNote = re.match(r"(?P<caretaker>.*) ?\((?P<note>.*)\)",newCare.careNote)
        if dateIID == "":
            newIID = self.careView.addLine(dateIID,newCare.date,["",""])
            self.careView.addLine(newIID,parseNote.group("caretaker"),(newCare[1],parseNote.group("note")))
        else:
            self.careView.addLine(dateIID,parseNote.group("caretaker"),(newCare[1],parseNote.group("note")))


"""This module contains plugins with admin interface checks."""

from teatime.plugins import Context, JSONRPCPlugin, NodeType
from teatime.reporting import Issue, Severity

class GethDatadir(JSONRPCPlugin):
    """Try to fetch Geth's data directory.

    Severity: Low

    Geth: https://geth.ethereum.org/docs/rpc/ns-admin#admin_datadir
    """

    INTRUSIVE = False

    def _check(self, context: Context):
        if context.node_type != NodeType.GETH:
            return

        payload = self.get_rpc_json(context.target, method="admin_datadir")
        context.report.add_issue(
            Issue(
                title="Admin datadir access",
                description=(
                    "The datadir directory path can be fetched "
                    "using the admin_datadir RPC call."
                ),
                raw_data=payload,
                severity=Severity.LOW,
            )
        )

class GethNodeInfo(JSONRPCPlugin):
    """Try to fetch admin info about the node.

    Severity: Low

    Geth: https://geth.ethereum.org/docs/rpc/ns-admin#admin_nodeinfo
    """

    INTRUSIVE = False

    def _check(self, context: Context) -> None:
        if context.node_type != NodeType.GETH:
            return

        payload = self.get_rpc_json(context.target, method="admin_nodeInfo")
        context.report.add_issue(
            Issue(
                title="Admin Node Info Leaks",
                description=(
                    "Admin-only information can be fetched using "
                    "the admin_nodeInfo RPC call."
                ),
                raw_data=payload,
                severity=Severity.LOW,
            )
        )

class ParityDevLogs(JSONRPCPlugin):
    """Try to fetch the node's developer logs.

    Severity: Critical

    Parity/OpenEthereum: https://openethereum.github.io/wiki/JSONRPC-parity-module#parity_devlogs
    """

    INTRUSIVE = False

    def _check(self, context: Context) -> None:
        if context.node_type != NodeType.PARITY:
            return

        payload = self.get_rpc_json(context.target, method="parity_devLogs")
        context.report.add_issue(
            Issue(
                title="Developer log information leak",
                description=(
                    "The node's developer logs can be fetched "
                    "using the parity_devLogs RPC call."
                ),
                raw_data=payload,
                severity=Severity.CRITICAL,
            )
        )

class PeerlistLeak(JSONRPCPlugin):
    """Try to fetch peer list information.

    Severity: Medium

    Geth: https://geth.ethereum.org/docs/rpc/ns-admin#admin_peers
    Parity/OpenEthereum: https://openethereum.github.io/wiki/JSONRPC-parity-module#parity_netpeers
    """

    INTRUSIVE = False

    def _check(self, context: Context) -> None:
        if context.node_type == NodeType.PARITY:
            payload = self.get_rpc_json(context.target, method="parity_netPeers")
            context.report.add_issue(
                Issue(
                    title="Peer list information leak",
                    description=(
                        "Admin-only peer list information can be fetched "
                        "with the parity_netPeers RPC call."
                    ),
                    raw_data=payload,
                    severity=Severity.MEDIUM,
                )
            )
        elif context.node_type == NodeType.GETH:
            payload = self.get_rpc_json(context.target, method="admin_peers")
            context.report.add_issue(
                Issue(
                    title="Admin Peerlist Access",
                    description=(
                        "Admin-only information about the peer list can be "
                        "fetched using the admin_peers RPC call."
                    ),
                    raw_data=payload,
                    severity=Severity.MEDIUM,
                )
            )


from __future__ import absolute_import,division,print_function

import gzip
import os
import time
import sys
import math
from six.moves import xrange
import numpy as np
from CNN import CNN
from data_helper import load_train,load_test

# hyper parameter
IMAGE_SIZE=28
NUM_CHANNELS=1
PIXEL_DEPTH=255
BATCH_SIZE=64
NUM_LABELS=10
VAL_SIZE=64
MAX_EPOCHS = 2
SEED=2330
EVAL_FREQUENCY = 50
EVAL_BATCH_SIZE = 64

def onehot(targets, num):
    result = np.zeros((num, 10))
    for i in range(num):
        result[i][targets[i]] = 1
    return result

validation_size = 5000
train_data,train_labels,validation_data,validation_labels = load_train(validation_size,IMAGE_SIZE, NUM_CHANNELS, PIXEL_DEPTH)
test_data,test_labels = load_test(IMAGE_SIZE, NUM_CHANNELS, PIXEL_DEPTH)

train_labels = onehot(train_labels, 55000)
validation_labels = onehot(validation_labels, validation_size)
train_size = train_data.shape[0]

# calculate accuracy
def cal_acc(predictions, labels):
    return (100.0 * np.sum(predictions == labels) /predictions.shape[0])

# predict validation data and test data
def eval_in_batches(data,cnn):
    size = data.shape[0]
    if size < EVAL_BATCH_SIZE:
        raise ValueError("batch size for evals larger than dataset: %d" % size)
    predictions = np.zeros(size)
    for begin in xrange(0, size, EVAL_BATCH_SIZE):
        end = begin + EVAL_BATCH_SIZE
        if end <= size:
            x = data[begin:end, ...]
            predictions[begin:end] = cnn.cal_forward(x)
        else:
            x = data[-EVAL_BATCH_SIZE:, ...]
            batch_predictions = cnn.cal_forward(x)
            predictions[begin:] = batch_predictions[begin - size:]
    return predictions

if __name__ == "__main__":
    cnn = CNN(num_labels=10, batch_size=64, image_size=28, num_channels=1, seed=SEED)
    learning_rate = 0.01

    start_time = time.time()
    # train
    print("="*20, "start training","="*20)
    for step in xrange(int(MAX_EPOCHS * train_size) // BATCH_SIZE):
        offset = (step * BATCH_SIZE) % (train_size - BATCH_SIZE)
        batch_x = train_data[offset:(offset + BATCH_SIZE), ...]
        batch_y = train_labels[offset:(offset + BATCH_SIZE)]

        loss, predictions = cnn.fit(batch_x,batch_y,learning_rate)

        if step % EVAL_FREQUENCY == 0:
            acc = cal_acc(predictions, np.argmax(batch_y, axis=1))
            print('Step %d (epoch %.2f)' % (step, float(step) * BATCH_SIZE / train_size))
            print('Minibatch loss: %.3f' % loss)
            print('Minibatch acc:  %.1f%%' % acc)
            val_predictions = eval_in_batches(validation_data[0:1000, ...],cnn)
            val_acc = cal_acc(val_predictions, np.argmax(validation_labels[0:1000], axis=1))
            print('Validation error: %.1f%%' % val_acc)

    # test
    print("="*20, "start testing","="*20)
    test_predictions = eval_in_batches(test_data, cnn)
    test_acc = cal_acc(test_predictions, test_labels)
    print("test data acc: %.1f%%" % test_acc)


from honeybee_radiance_command.pcomb import Pcomb
import pytest
import honeybee_radiance_command._exception as exceptions

def test_defaults():
    """Test command."""
    pcomb = Pcomb()

    assert pcomb.command == 'pcomb'
    assert pcomb.options.to_radiance() == ''

def test_assignment():
    """Test assignment."""
    pcomb = Pcomb()

    pcomb.input = ['image1.hdr', 'image2.hdr']
    assert pcomb.input == 'image1.hdr image2.hdr'
    assert pcomb.to_radiance() == 'pcomb image1.hdr image2.hdr'
    pcomb.output = 'combined.hdr'
    assert pcomb.output == 'combined.hdr'
    assert pcomb.to_radiance() == 'pcomb image1.hdr image2.hdr > combined.hdr'

def test_assignment_options():
    """Test assigning options."""
    pcomb = Pcomb()

    pcomb.input = ['image1.hdr', 'image2.hdr']
    pcomb.options.h = True
    pcomb.options.c = (0.125, 0.133, 0.250)
    assert pcomb.to_radiance() == 'pcomb -c 0.125 0.133 0.25 -h image1.hdr image2.hdr'
    with pytest.raises(AssertionError):
        # Making sure less than zero values are not able to be assigned to resolution
        pcomb.options.x = 0.12

def test_stdin():
    """Test stdin."""
    pcomb = Pcomb()

    pcomb.input = ['image1.hdr', 'image2.hdr']
    pcomb.output = 'combined.hdr'
    assert pcomb.to_radiance(stdin_input=True) == ('pcomb > combined.hdr')

def test_validation():
    """Validate error for missing argument."""
    pcomb = Pcomb()

    with pytest.raises(exceptions.MissingArgumentError):
        pcomb.to_radiance()

    pcomb.input = ['image1.hdr', 'image2.hdr']
    assert pcomb.to_radiance() == 'pcomb image1.hdr image2.hdr'


import numpy as np
import SimpleITK as sitk
import torch
import torch.nn.functional as F
from typing import Iterable, List, Tuple, Union

from med_query.utils.frame import Frame3d, world_box
from med_query.utils.framed_tensor import FramedTensor

def resample_itkimage_torai(
    sitk_im: sitk.Image,
    spacing: Union[List, Tuple, np.ndarray],
    interpolator: str = "nearest",
    pad_value: Union[int, float] = -1024,
) -> sitk.Image:
    """
    resample an image to RAI coordinate system

    :param sitk_im: input image
    :param spacing: destination spacing
    :param interpolator: interpolation method, can be 'nearest' and linear, defaults to 'nearest'
    :param pad_value: value for voxels extroplated
    :return: the resampled SimpleITK image object
    """

    min_corner, max_corner = world_box(sitk_im)

    origin = min_corner.tolist()
    direction = np.eye(3, dtype=np.double).flatten().tolist()
    size = ((max_corner - min_corner) / (np.array(spacing))).round().astype(np.int32).tolist()

    return resample_base(
        sitk_im,
        origin=origin,
        direction=direction,
        spacing=spacing,
        size=size,
        interpolator=interpolator,
        pad_value=pad_value,
    )

def resample_itkimage_withsize(
    itkimage: sitk.Image,
    new_size: Union[List, Tuple, np.ndarray],
    interpolator: str = "nearest",
    pad_value: Union[int, float] = -1024,
) -> sitk.Image:
    """
    Image resize with size by sitk resampleImageFilter.

    :param itkimage: input itk image or itk volume.
    :param new_size: the target size of the resampled image, such as [120, 80, 80].
    :param interpolator: for mask used nearest, for image linear is an option.
    :param pad_value: the value for the pixel which is out of image.
    :return: resampled itk image.
    """

    # get resize factor
    origin_size = np.array(itkimage.GetSize())
    new_size = np.array(new_size)
    factor = origin_size / new_size

    # get new spacing
    origin_spcaing = itkimage.GetSpacing()
    new_spacing = factor * origin_spcaing

    itkimg_resampled = resample_base(
        itkimage,
        itkimage.GetOrigin(),
        itkimage.GetDirection(),
        new_spacing,
        new_size,
        interpolator,
        pad_value,
    )

    return itkimg_resampled

def resample_itkimage_withspacing(
    itkimage: sitk.Image,
    new_spacing: Union[List, Tuple, np.ndarray],
    interpolator: str = "nearest",
    pad_value: Union[int, float] = -1024,
) -> sitk.Image:
    """
    Image resize with size by sitk resampleImageFilter.

    :param itkimage: input itk image or itk volume.
    :param new_spacing: the target spacing of the resampled image, such as [1.0, 1.0, 1.0].
    :param interpolator: for mask used nearest, for image linear is an option.
    :param pad_value: the value for the pixel which is out of image.
    :return: resampled itk image.
    """

    # get resize factor
    origin_spacing = itkimage.GetSpacing()
    new_spacing = np.array(new_spacing, float)
    factor = new_spacing / origin_spacing

    # get new image size
    origin_size = itkimage.GetSize()
    new_size = origin_size / factor
    new_size = new_size.astype(np.int)

    itkimg_resampled = resample_base(
        itkimage,
        itkimage.GetOrigin(),
        itkimage.GetDirection(),
        new_spacing,
        new_size,
        interpolator,
        pad_value,
    )

    return itkimg_resampled

def resample_base(
    sitk_im: sitk.Image,
    origin: Union[List, Tuple, np.ndarray],
    direction: Union[List, Tuple],
    spacing: Union[List, Tuple, np.ndarray],
    size: Union[List, Tuple, np.ndarray],
    interpolator: str = "nearest",
    pad_value: Union[int, float] = -1024,
) -> sitk.Image:
    """
    the base resample function, can be used to resample a small patch out of the original image
    or to resample to sample patch back to the original image, and of course, to resize a volume

    :param sitk_im: input image
    :param origin: the origin of the resampled volume
    :param direction: the direction of the resampled volume
    :param spacing: the spacing of the resampled volume
    :param size: the output size of the resampled volume
    :param interpolator: interpolation method, can be 'nearest' and linear, defaults to 'nearest'
    :param pad_value: value for voxels extroplated
    :return: the resampled SimpleITK image object
    """
    size = [int(s) for s in size]
    SITK_INTERPOLATOR_DICT = {
        "nearest": sitk.sitkNearestNeighbor,
        "linear": sitk.sitkLinear,
        "gaussian": sitk.sitkGaussian,
        "label_gaussian": sitk.sitkLabelGaussian,
        "bspline": sitk.sitkBSpline,
        "hamming_sinc": sitk.sitkHammingWindowedSinc,
        "cosine_windowed_sinc": sitk.sitkCosineWindowedSinc,
        "welch_windowed_sinc": sitk.sitkWelchWindowedSinc,
        "lanczos_windowed_sinc": sitk.sitkLanczosWindowedSinc,
    }

    assert (
        interpolator in SITK_INTERPOLATOR_DICT.keys()
    ), "`interpolator` should be one of {}".format(SITK_INTERPOLATOR_DICT.keys())

    sitk_interpolator = SITK_INTERPOLATOR_DICT[interpolator]

    resample_filter = sitk.ResampleImageFilter()
    resample_filter.SetSize(size)
    resample_filter.SetOutputSpacing(np.array(spacing).tolist())
    resample_filter.SetOutputOrigin(np.array(origin).tolist())
    resample_filter.SetOutputDirection(direction)
    resample_filter.SetOutputPixelType(sitk_im.GetPixelID())
    resample_filter.SetDefaultPixelValue(pad_value)
    resample_filter.SetInterpolator(sitk_interpolator)

    img = resample_filter.Execute(sitk_im)

    return img

def resample_itkimage_withspacing_by_torch(
    itkimage: sitk.Image,
    new_spacing: Union[List, Tuple, np.ndarray],
    interpolator: str = "nearest",
    pad_value: Union[int, float] = -1024,
) -> sitk.Image:

    mode = "nearest"
    if interpolator == "linear":
        mode = "trilinear"

    dtype_conversion = False
    if itkimage.GetPixelID() != 8:
        img = sitk.Cast(itkimage, sitk.sitkFloat32)
        dtype_conversion = True
    else:
        img = itkimage

    im_tensor = torch.from_numpy(sitk.GetArrayFromImage(img)).unsqueeze(0).unsqueeze(0)
    if torch.cuda.is_available():
        im_tensor = im_tensor.cuda()
    ratio = np.array(itkimage.GetSpacing()) / new_spacing
    ratio = ratio[::-1]
    im_tensor = F.interpolate(im_tensor, scale_factor=ratio, mode=mode, align_corners=False)
    im_tensor = im_tensor.squeeze().cpu()
    img = sitk.GetImageFromArray(im_tensor.numpy())
    img.SetSpacing(np.array(new_spacing).tolist())
    img.SetOrigin(itkimage.GetOrigin())

    if dtype_conversion:
        img = sitk.Cast(img, itkimage.GetPixelID())

    return img

def crop_roi_with_center(
    itk_img: sitk.Image,
    center_w: Iterable[float],
    spacing: Iterable[float],
    x_axis: Iterable[float],
    y_axis: Iterable[float],
    z_axis: Iterable[float],
    size: Iterable[int],
    interpolator: str = "nearest",
    pad_value=-1024,
):
    frame = Frame3d()
    frame.origin = list(center_w)
    frame.direction = np.vstack([x_axis, y_axis, z_axis]).transpose().flatten().tolist()
    frame.spacing = spacing
    size = np.array(size).reshape(3)
    true_origin = frame.voxel_to_world(-size / 2)
    roi = resample_base(
        itk_img, true_origin, frame.direction, frame.spacing, size, interpolator, pad_value
    )
    return roi

def resample_framed_tensor(
    framed_tensor: FramedTensor,
    origin: Union[List, Tuple, np.ndarray],
    direction: Union[List, Tuple],
    spacing: Union[List, Tuple, np.ndarray],
    size: Union[List, Tuple, np.ndarray],
    interpolator: str = "nearest",
    pad_value: Union[int, float] = -1024,
    use_gpu: bool = True,
    work_with_float16=False,
):
    working_dtype = torch.float32
    if work_with_float16:
        working_dtype = torch.float16
    work_device = torch.device("cpu")
    if use_gpu and torch.cuda.is_available():
        work_device = framed_tensor.device
    else:
        framed_tensor = framed_tensor.cpu()
        # "arange_cpu" not implemented for 'Half'
        working_dtype = torch.float32
    original_dtype = framed_tensor.tensor.dtype

    # if framed_tensor.tensor.dtype not in [torch.float32, torch.float16]:
    #     print(
    #         "warning: as torch.grid_sample only support float tensors,\n"
    #         "it's better to convert the input tensor to float beforehand.\n"
    #         "this function will automatically convert non-float input to float \n"
    #         "and then convert the dtype of the output back to the same as input"
    #     )

    framed_tensor = framed_tensor.type(working_dtype)

    range_tensors = [
        torch.arange(0, s, dtype=working_dtype, device=work_device) for s in size[::-1]
    ]
    if "indexing" in torch.meshgrid.__code__.co_varnames:
        output_voxel = torch.meshgrid(range_tensors, indexing="ij")
    else:
        output_voxel = torch.meshgrid(range_tensors)
    output_voxel = torch.stack(output_voxel, dim=0).flip(0).view(3, -1)
    output_frame = Frame3d()
    output_frame.spacing = spacing
    output_frame.origin = origin
    output_frame.direction = direction
    output_world = output_frame.voxel_to_world(output_voxel).type(working_dtype)
    voi_x, voi_y, voi_z = framed_tensor.frame.world_to_voxel(output_world, is_round=False).type(
        working_dtype
    )
    voi_x = voi_x.view(*size[::-1])
    voi_y = voi_y.view(*size[::-1])
    voi_z = voi_z.view(*size[::-1])

    VZ, VY, VX = framed_tensor.tensor.size()
    voi_x = voi_x / (VX - 1) * 2 - 1
    voi_y = voi_y / (VY - 1) * 2 - 1
    voi_z = voi_z / (VZ - 1) * 2 - 1
    grid_on_input = torch.stack([voi_x, voi_y, voi_z], dim=3).unsqueeze(0).type(working_dtype)

    mode = "bilinear"
    if interpolator == "nearest":
        mode = "nearest"
    input_tensor = (framed_tensor.tensor - pad_value).type(working_dtype).unsqueeze(0).unsqueeze(0)
    if torch.cuda.is_available() and input_tensor.device == torch.device("cpu"):
        input_tensor = input_tensor.cuda()
        grid_on_input = grid_on_input.cuda()

    output_tensor = (
        F.grid_sample(
            input_tensor, grid_on_input, mode=mode, padding_mode="zeros", align_corners=True
        )
        + pad_value
    )
    if output_tensor.dtype != original_dtype:
        if original_dtype in [torch.uint8, torch.int8, torch.int16, torch.int32, torch.int64]:
            output_tensor = output_tensor.round()
        output_tensor = output_tensor.type(original_dtype)
    output = FramedTensor(output_tensor.squeeze(), output_frame)
    output = output.to(framed_tensor.device)

    return output

def merge_mask_by_prob_patches(
    output_frame: Frame3d,
    output_size: List[int],
    framed_mask_crops: List[FramedTensor],
    framed_prob_crops: List[FramedTensor],
    labels: List[int] = None,
    use_gpu=True,
    work_with_float16=False,
    pad_on_cpu=True,
) -> Tuple[FramedTensor, FramedTensor, Tuple[np.array, np.array]]:
    """
    merge masks patches by prob patches, which generated in the detection-then-segmentation paradigm

    :param output_frame: output frame
    :param output_size: output size
    :param framed_mask_crops: list of FramedTensors for mask crops
    :param framed_prob_crops: list of FramedTensors for prob crops
    :param labels: list of labels, can ben None for binary segmentation
    :param use_gpu: whether to use gpu for resampling
    :param work_with_float16: whether to work with float16 to save gpu memroy
    :param pad_on_cpu: whether to copy roi data to cpu then pad or pad directly on gpu,
                       will save memory but a bit slower
    :return: FramedTensor for merged mask, FramedTensor for merged probability map,
             a tuple of numpy array for roi start voxel and end voxel, in x, y, z order
    """
    if len(framed_mask_crops) == 0 or len(framed_prob_crops) == 0:
        raise ValueError("input framed mask and prob crops lists should not be empty!")
    if labels is None:
        labels = np.ones(len(framed_prob_crops), dtype=np.uint8)
    assert len(framed_prob_crops) == len(labels), "length of patches must be equal to len(labels)"

    working_dtype = torch.float32
    if work_with_float16:
        working_dtype = torch.float16
    framed_prob_crops = [t.type(working_dtype) for t in framed_prob_crops]

    work_device = torch.device("cpu")
    if use_gpu and torch.cuda.is_available():
        work_device = framed_prob_crops[0].device
    else:
        framed_prob_crops = [t.cpu() for t in framed_prob_crops]
        framed_mask_crops = [t.cpu() for t in framed_mask_crops]

    eps, sps = [], []
    for idx, framed_prob_crop in enumerate(framed_prob_crops):
        start = [0, 0, 0]
        end = framed_prob_crop.tensor.size()[::-1]
        coords = list(zip(start, end))
        corners = []
        for i in range(2):
            for j in range(2):
                for k in range(2):
                    corner_on_crop_v = [coords[0][i], coords[1][j], coords[2][k]]
                    corner_on_crop_w = framed_prob_crop.frame.voxel_to_world(corner_on_crop_v)
                    corner_on_out_v = output_frame.world_to_voxel(corner_on_crop_w)
                    corners.append(corner_on_out_v)
        corners = np.vstack(corners)
        sp = np.maximum([0, 0, 0], corners.min(axis=0))
        ep = np.minimum(output_size, corners.max(axis=0))
        sps.append(sp)
        eps.append(ep)
    roi_sp, roi_ep = np.array(sps).min(axis=0), np.array(eps).max(axis=0)
    roi_size = list(roi_ep - roi_sp)

    roi_mask_tensor = torch.zeros(roi_size[::-1], device=work_device, dtype=torch.uint8)
    roi_prob_tensor = torch.zeros(roi_size[::-1], device=work_device, dtype=working_dtype)

    for idx, framed_prob_crop in enumerate(framed_prob_crops):
        if torch.cuda.is_available():
            torch.cuda.empty_cache()
        sp, ep = sps[idx], eps[idx]
        resample_origin = output_frame.voxel_to_world(sp).tolist()
        resample_size = ep - sp

        resampled_prob = resample_framed_tensor(
            framed_prob_crop,
            resample_origin,
            output_frame.direction,
            output_frame.spacing,
            resample_size,
            "linear",
            0,
            use_gpu,
            work_with_float16,
        ).tensor
        resampled_mask = resample_framed_tensor(
            framed_mask_crops[idx],
            resample_origin,
            output_frame.direction,
            output_frame.spacing,
            resample_size,
            "nearest",
            0,
            use_gpu,
            work_with_float16,
        ).tensor
        sp, ep = sp - roi_sp, ep - roi_sp
        prob_buffer = roi_prob_tensor[sp[2] : ep[2], sp[1] : ep[1], sp[0] : ep[0]]
        mask_buffer = roi_mask_tensor[sp[2] : ep[2], sp[1] : ep[1], sp[0] : ep[0]]
        buffer_index = resampled_prob > prob_buffer
        mask_buffer[buffer_index] = resampled_mask[buffer_index] * labels[idx]
        prob_buffer[buffer_index] = resampled_prob[buffer_index]

    pad_size = []
    # note that the pad_size for torch starts from last dimension, i.e. X axis in patient coord
    for ps in zip(roi_sp, np.array(output_size) - roi_ep):
        pad_size.extend(ps)
    if pad_on_cpu:
        roi_mask_tensor = roi_mask_tensor.cpu()
        roi_prob_tensor = roi_prob_tensor.cpu()
    output_mask_tensor = torch.constant_pad_nd(roi_mask_tensor, pad_size, 0)
    output_prob_tensor = torch.constant_pad_nd(roi_prob_tensor, pad_size, 0)
    output_mask = FramedTensor(output_mask_tensor, output_frame)
    output_prob = FramedTensor(output_prob_tensor, output_frame).type(torch.float32)

    return output_mask, output_prob, (roi_sp, roi_ep)


import numpy
import matplotlib.pyplot
import glob
data = numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',')
print(data)
print(data.mean())
print('maximum inflammation:', data.max())
print('minimum inflammation:', data.min())
print('standard deviation:', data.std())

patient_0 = data[0, :] # 0 on the first axis, everything on the second
print('maximum inflammation for patient 0:', patient_0.max())

print(data.mean(axis=0))

print(data.mean(axis=1))

image  = matplotlib.pyplot.imshow(data)
matplotlib.pyplot.show(image)

ave_inflammation = data.mean(axis=0)
ave_plot = matplotlib.pyplot.plot(ave_inflammation)
matplotlib.pyplot.show(ave_plot)

fig = matplotlib.pyplot.figure(figsize=(10.0, 3.0))

axes1 = fig.add_subplot(1, 3, 1)
axes2 = fig.add_subplot(1, 3, 2)
axes3 = fig.add_subplot(1, 3, 3)

axes1.set_ylabel('average')
axes1.plot(data.mean(axis=0))

axes2.set_ylabel('max')
axes2.plot(data.max(axis=0))

axes3.set_ylabel('min')
axes3.plot(data.min(axis=0))

fig.tight_layout()

matplotlib.pyplot.show(fig)

filenames = glob.glob('data/inflammation*.csv')
filenames = filenames[0:3]
for f in filenames:
    print(f)

    data = numpy.loadtxt(fname=f, delimiter=',')

    fig = matplotlib.pyplot.figure(figsize=(10.0, 3.0))

    axes1 = fig.add_subplot(1, 3, 1)
    axes2 = fig.add_subplot(1, 3, 2)
    axes3 = fig.add_subplot(1, 3, 3)

    axes1.set_ylabel('average')
    axes1.plot(data.mean(axis=0))

    axes2.set_ylabel('max')
    axes2.plot(data.max(axis=0))

    axes3.set_ylabel('min')
    axes3.plot(data.min(axis=0))

    fig.tight_layout()
    matplotlib.pyplot.show(fig)


from django import forms
from search.models import Products

class StoreForm(forms.Form):

    PRICE_CHIOCES = (
        ("<300","<Less"),
        ("300-600","300-600"),
        (">600",">600")
    )
    price_range = forms.ChoiceField(choices=PRICE_CHIOCES)

class BrandForm(forms.Form):

    PRICE_CHIOCES = (
        ("<300","<300"),
        ("300-600","300-600"),
        (">600",">600")
    )
    price_range = forms.ChoiceField(choices=PRICE_CHIOCES)

class TitleForm(forms.ModelForm):

    def __init__(self,*args,**kwargs):
        super(TitleForm,self).__init__(*args,**kwargs)
        self.fields['in_stock'] = forms.ChoiceField(choices=Products.INSTOCK_CHOICES,widget=forms.RadioSelect)
        self.fields['store'].required = False

    class Meta:

        model = Products
        exclude=('title','price')


import unittest

from multiline.sensors import parse_sensor

instrument_data = [
    "Multi 3630 IDS; 19410634;1;29.04.2020 10:35:33;414.3;FNU;TRB;;;;;;;VisoTurb 900-P; 19B103894;",
    "Multi 3630 IDS; 19410634;1;29.04.2020 10:35:34;7.702;;pH;16.6;°C;Temp;;100%;;SenTix 940; C200817020;",
    "Multi 3630 IDS; 19410634;1;29.04.2020 10:35:34;183.9;µS/cm;Cond;16.5;°C;Temp;AR;;C = 0.475 1/cm   Tref25   nLF;TetraCon 925; 19411430;",
    "Multi 3630 IDS; 19410634;;15.04.2020 11:03:48;97.0;%;Ox;19.4;°C;Temp;AR;;SC-FDO   19391671;;FDO 925; 19411244;",
    "Multi 3630 IDS; 19410634;;15.04.2020 11:03:48;8.74;mg/l;Ox;19.4;°C;Temp;AR;;SC-FDO   19391671;;FDO 925; 19411244;"
]

class TestParse(unittest.TestCase):

    def test_parse_instrument_names(self):
        instrument_names = [parse_sensor(datum)["instrument_name"] for datum in instrument_data]
        self.assertEqual(instrument_names, ["Multi 3630 IDS"] * len(instrument_data))

    def test_parse_quantities(self):
        units = [parse_sensor(datum)["quantity"] for datum in instrument_data]
        self.assertEqual(units, ["turbidity", "pH", "conductivity", "dissolved_oxygen_saturation", "dissolved_oxygen_concentration"])

    def test_parse_values(self):
        values = [parse_sensor(datum)["value"] for datum in instrument_data]
        values = [float(value) for value in values]
        self.assertEqual(values, [414.3, 7.702, 183.9, 97.0, 8.74])

    def test_parse_units(self):
        units = [parse_sensor(datum)["unit"] for datum in instrument_data]
        self.assertEqual(units, ["FNU", "", "µS/cm", "%","mg/l"])

    def test_parse_temperatures(self):
        temps = [parse_sensor(datum)["temp"] for datum in instrument_data]
        temps = [temp for temp in temps if temp.strip()]
        temps = [float(temp) for temp in temps]
        self.assertEqual(sum(temps), 71.9)

if __name__ == "__main__":
    unittest.main()


from django.shortcuts import render
from .forms import ContactForm
from django.core.mail import send_mail
from django.conf import settings

# Create your views here.
def index(request):
    form = ContactForm()
    if request.method=='POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data

            subject = 'message from concert travel.com'
            name = cd.get('name')
            from_form_email = cd.get('from_email')
            body = cd.get('message') + '\n' + 'From:' + name + '\n' + 'Email:' + from_form_email
            cust_email = 'kellyjona@gmail.com'
            form = ContactForm()  #   clear the form

            try:
                send_mail(
                subject,
                body,
                settings.DEFAULT_FROM_EMAIL,
                [cust_email]
                )

                # copy the sender
                subject = 'Copy of Message from Contact Us Page @ ConcertTravel.com'
                cust_email = from_form_email
                send_mail(
                subject,
                body,
                settings.DEFAULT_FROM_EMAIL,
                [cust_email]
                )
            except Exception as e:
                print(e)

    template = 'home/index.html'
    context = {
        'form': form,
    }
    return render(request, template, context)


from pathlib import Path
from argparse import ArgumentParser

# -
from composable_logs import version_string
from composable_logs.helpers import read_json, write_json
from composable_logs.opentelemetry_helpers import Spans
from composable_logs.opentelemetry_task_span_parser import parse_spans

# -
from .mermaid_graphs import (
    make_mermaid_dag_inputfile,
    make_mermaid_gantt_inputfile,
)
from .common_helpers.utils import ensure_dir_exist

def _status_summary(span_dict) -> str:
    if span_dict["status"]["status_code"] == "OK":
        return "OK"
    else:
        return "FAILED"

def safe_path(filepath: Path):
    assert str(filepath).startswith("/")
    assert ".." not in str(filepath)
    return filepath

def outcome(is_success: bool) -> str:
    if is_success:
        return "OK"
    else:
        return "FAILED"

def write_spans_to_output_directory_structure(spans: Spans, out_basepath: Path):
    """
    Write out tasks/runs/artefacts found in spans into a directory structure for
    inspection using a file browser.

    Any notebooks logged are written to the directory structure both in
    ipynb and html formats.
    """
    print(" - Writing tasks in spans to ", out_basepath)

    workflow_summary = parse_spans(spans)

    write_json(
        safe_path(out_basepath / "run-time-metadata.json"), workflow_summary.as_dict()
    )

    for task_run_summary in workflow_summary.task_runs:
        # -- write json with task-specific data --
        if not task_run_summary.attributes["task.type"] in ["python", "jupytext"]:
            raise Exception(f"Unknown task type for {task_run_summary.attributes}")

        task_dir: str = "--".join(
            [
                f"""{task_run_summary.attributes["task.type"]}-task""",
                (
                    task_run_summary.attributes["task.id"]  # type: ignore
                    .replace("/", "-")  # type: ignore
                    .replace(".", "-")  # type: ignore
                ),
                task_run_summary.span_id,
                outcome(task_run_summary.is_success()),
            ]
        )

        write_json(
            safe_path(out_basepath / task_dir / "run-time-metadata.json"),
            task_run_summary.as_dict(),
        )

        for artifact in task_run_summary.logged_artifacts:
            out_path: Path = safe_path(
                ensure_dir_exist(out_basepath / task_dir / "artifacts" / artifact.name)
            )
            artifact.write(out_path)

# --- cli tool implementation ---

def args():
    parser = ArgumentParser()
    parser.add_argument(
        "--input_span_file",
        required=True,
        type=Path,
        help="JSON file with logged OpenTelemetry spans",
    )
    parser.add_argument(
        "--output_directory",
        required=False,
        type=Path,
        help="base output directory for writing tasks and logged artefacts",
    )
    parser.add_argument(
        "--output_filepath_mermaid_gantt",
        required=False,
        type=Path,
        help="output file path for Mermaid Gantt diagram input file (eg. gantt.mmd)",
    )
    parser.add_argument(
        "--output_filepath_mermaid_dag",
        required=False,
        type=Path,
        help="output file path for Mermaid DAG diagram input file (eg. dag.mmd)",
    )
    return parser.parse_args()

def entry_point():
    print(f"--- pynb_log_parser cli {version_string()} ---")
    print(f"--- Expand a log into a local directory tree to inspect log output")

    spans: Spans = Spans(read_json(args().input_span_file))
    print(f"Number of spans loaded {len(spans)}")

    if args().output_directory is not None:
        write_spans_to_output_directory_structure(spans, args().output_directory)

    if args().output_filepath_mermaid_gantt is not None:
        (
            ensure_dir_exist(args().output_filepath_mermaid_gantt)
            # -
            .write_text(make_mermaid_gantt_inputfile(spans))
        )

    if args().output_filepath_mermaid_dag is not None:
        dag_output_path: Path = ensure_dir_exist(args().output_filepath_mermaid_dag)
        assert dag_output_path.suffix == ".mmd"

        dag_output_path.write_text(
            make_mermaid_dag_inputfile(spans, generate_links=True)
        )

        nolinks_output_path: Path = dag_output_path.with_name(
            dag_output_path.name.replace(".mmd", "-nolinks.mmd")
        )
        nolinks_output_path.write_text(
            make_mermaid_dag_inputfile(spans, generate_links=False)
        )

    print(" - Done")


import tensorflow as tf
from pathlib import Path
from collections import defaultdict
import json

def __bytes_feature(value):
    """Returns a bytes_list from a string/byte."""
    if isinstance(value, type(tf.constant(0))):
        value = value.numpy()  # BytesList won't unpack a string from an EagerTensor.
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def __int64_feature(value):
    """Returns an int64_list from a bool / enum / int / uint. Used for label."""
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

def __serialize_example(filepath, label):
    """Serializes a (img_filepath, label)-tuple. The raw image and the filename are saved as a byte feature, the
     label as a int64-feature.
     @return: the serialized example."""
    img_bytes = open(filepath, 'rb').read()
    filepath_bytes = str(Path(*filepath.parts[-2:])).encode('utf-8')
    feature = {
        'image_raw': __bytes_feature(img_bytes),
        'label': __int64_feature(label),
        'filename': __bytes_feature(filepath_bytes)
    }

    return tf.train.Example(features=tf.train.Features(feature=feature))

def __write_tf_record(files, savepath, count_classes_dict):
    """
    Serializes all 'files' to a tfRecord specified by 'path_tf_record'.
    See: https://www.tensorflow.org/tutorials/load_data/tfrecord
    @param files: List of (filepath,label)-pair. filepath is a string. Label is an int representing the class.
    @param savepath: A string representing the file name to save the tf record to.
    @param count_classes_dict: dictionary with class frequencies.
    @return Updated dictionary with class frequencies.
    """

    with tf.io.TFRecordWriter(savepath) as writer:
        for filepath, label in files:
            count_classes_dict[label] += 1
            example = __serialize_example(filepath, label)
            # Write serialized example to TFRecord file
            writer.write(example.SerializeToString())

    return count_classes_dict

def __write_json(savepath, dictionary):
    """ Saves a dictionary to 'savepath' as a json file."""
    with open(savepath, "w") as file:
        json.dump(dictionary, file)

def write_train_test_val_json(dataset, train_files, val_files, test_files):
    """ Writes train_files, test_files and val_files to a tf record.
    Create count classes dict with frequency per class. """
    count_classes_dict = defaultdict()

    for x in dataset.dictionary:
        count_classes_dict[x] = 0

    count_classes_dict = __write_tf_record(train_files, dataset.tf_record_train,
                                           count_classes_dict)
    count_classes_dict = __write_tf_record(val_files, dataset.tf_record_val,
                                           count_classes_dict)
    count_classes_dict = __write_tf_record(test_files, dataset.tf_record_test,
                                           count_classes_dict)
    __write_json(dataset.count_json, count_classes_dict)

def write_train_test_val_json_without_test(dataset, train_files, val_files, test_files):
    """ Writes train_files, test_files and val_files to a tf record.
    Create count classes dict with frequency per class. Test files are not counted. This is used for a test
    set without known labels."""
    count_classes_dict = defaultdict()

    for x in dataset.dictionary:
        count_classes_dict[x] = 0

    count_classes_dict = __write_tf_record(train_files, dataset.tf_record_train,
                                           count_classes_dict)
    count_classes_dict = __write_tf_record(val_files, dataset.tf_record_val,
                                           count_classes_dict)
    __write_json(dataset.count_json, count_classes_dict)
    __write_tf_record(test_files, dataset.tf_record_test, count_classes_dict)


import pandas as pd
from rdkit import Chem
from rdkit.Chem import PandasTools
import tkinter as tk
from tkinter import filedialog

# Open tkinter
root = tk.Tk()
# Close tkinter unusable window
root.withdraw()
# Choose vina results file (made from concatinated output.txt files)
file_path = filedialog.askopenfilename(initialdir="~", title="Pasirinkite Vina rezultatų failą",
                                       filetypes=(("CSV files", "*.csv"), ("all files", "*.*")))

f = open(file_path, "r")

#Choose output files directory
out_path = filedialog.askdirectory(initialdir="~", title="Išsaugoti failus")
fn = open(out_path+"/Vina_results_all.csv", "w")

#Skip some lines and add only needed once
for line in f:
    if line.startswith(("#", "Reading input", "Setting up", "Analyzing the binding", "Using random", "Performing", "Refining", "Writing out")):
        pass
    else:
        fn.write(line)
fn.close()

fn = open(out_path+"/Vina_results_all.csv", "r")

names = []
first_mode_affinities = []

#Retrieve the best pose conformations affinity scores

for line in fn:
    if line.startswith("Out"):
        name = line[14:]
        for i in range(len(name)):
            if name[i] == "_":
                name1 = name[:i]
                names.append(name1)

    if line.startswith(" "):
        line = line.strip()
        if line.startswith("1"):
            line_split = line.split()
            first_mode_affinity = line_split[1]
            first_mode_affinities.append(first_mode_affinity)

#Choose smiles file of molecules

smiles_path = filedialog.askopenfilename(initialdir="~", title="Pasirinkite smiles rezultatų failą",
                                       filetypes=(("TXT files", "*.txt"), ("all files", "*.*")))

fsmiles = open(smiles_path, "r")
smiles = []
for smile in fsmiles:
    for i in range(len(smile)):
        if smile[i:i+1] == "\n":
            smile1 = smile[:i]
            smiles.append(smile1)

d = {"Name": pd.Series(names), "1st affinity": pd.Series(first_mode_affinities), "Smiles": pd.Series(smiles)}

s = pd.DataFrame(d)

#Create structured data table

Chem.PandasTools.AddMoleculeColumnToFrame(s, smilesCol='Smiles', molCol='Molecule')
Chem.PandasTools.SaveXlsxFromFrame(s, out_path + '/Docking_results.xlsx', molCol='Molecule')

print(s)

s.to_csv(out_path+"/S1PR1_vina_results.csv", index=False, header=True)

fn.close()

Vina output.txt unstructured data structurization.py


import sys

#sys.stdin = open("C:\\Users\\82103\\pythonprogramming\\algorithm\\input.txt","r")

n,f = map(int ,input().split())

def dfs(L,sum):
    if L==n and sum==f:
        for x in p:
            print(x, end=' ')
        sys.exit(0)
    else:
        for i in range(1,n+1):
            if ch[i]==0:
                ch[i]=1
                p[L]= i
                dfs(L+1,sum+i*b[L])
                ch[i]=0

p = [0]*n
b = [1]*n
ch = [0]* (n+1)
for i in range(1,n):
    b[i] = b[i-1]*(n-i)//i

dfs(0,0)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import django.utils.timezone

class Migration(migrations.Migration):

    dependencies = [
        ('blog', '0004_auto_20150912_1512'),
    ]

    operations = [
        migrations.CreateModel(
            name='Message',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('content', models.TextField(verbose_name=b'content')),
                ('name', models.CharField(max_length=30, null=True, verbose_name=b'Name', blank=True)),
                ('date', models.DateField(default=django.utils.timezone.now)),
            ],
        ),
    ]


a=10
class Exercise:
    def __init__(self,a):
        self.a=a
    def obj(self):
        a=int(input("how many elements you want in an object ..."))
        self.a=[]
        for i in range(0,a):
            element=input("Enter an element : ")
            self.a.append(element)
        print('Given list : ',self.a)
    def elements(self):
        print("Elements---")
        for i in self.a:
            print(i)
    def indices(self):
        print("Indices---")
        for i in self.a:
            print(self.a.index(i))
    def sum(self):
        print("Length of object---\n",len(self.a))
        sum=0
        for i in self.a:
            sum=sum+int(i)
        sum=sum+a                               #a is global variable
        print("Sum of elements---\n",sum)
l=Exercise(1)
l.obj()
print(type(l.obj),type(l.sum),type(Exercise))
l.elements()
l.indices()
print(type(l))
print(l.sum())

_07_core_python/_12_Oops/self codes/list creation and funtions with class.py


from JobPage import JobPage
from time import *
import os
import sys
import pwd
import string
import pickle

import pyARC

import Lap
import LapWeb
import LapAbaqus
import LapUtils

class AbaqusJobPage(JobPage):

	def onCreateNewTask(self):

		LapUtils.log.msg("AbaqusJobPage", "onCreateNewTask")

		return LapAbaqus.AbaqusTask()

	def onCreateNewJobForm(self, task):

		LapUtils.log.msg("AbaqusJobPage", "onCreateNewJobForm")

		form = LapWeb.Form(self, "testform", "AbaqusJobPage", "Create a Abaqus job")

		attribs = task.getAttributes()
		xrslAttribs = task.getXRSLAttributes()

		form.addFile("Input file", "inputFile", "")
		form.addText("License server", "licenseServer", attribs["licenseServer"])
		form.addSeparator()
		form.addText("CPU time (s)", "cpuTime", xrslAttribs["cpuTime"])
#		form.addText("Job name", "jobName", xrslAttribs["jobName"])
		form.addText("Job name", "jobName", "AbaqusJob")
		form.addText("Email notification", "email", xrslAttribs["notify"])
#		form.addText(os.getcwd(), "cwd", "")

		return form

	def onCreateEditJobForm(self, task):

		LapUtils.log.msg("AbaqusJobPage", "onCreateEditJobForm")

		form = LapWeb.Form(self, "testform", "AbaqusJobPage", "Edit Abaqus job")

		attribs = task.getAttributes()
		xrslAttribs = task.getXRSLAttributes()

		form.addFile("Input file", "inputFile", attribs["inputFile"])
		form.addReadonlyText("Current file", "prevFile", attribs["inputFile"])
		form.addText("License server", "licenseServer", attribs["licenseServer"])
		form.addSeparator()
		form.addText("CPU time (s)", "cpuTime", xrslAttribs["cpuTime"])
		form.addText("Job name", "jobName", xrslAttribs["jobName"])
		form.addText("Email notification", "email", xrslAttribs["notify"])
		form.addHidden("", "oldJobName", xrslAttribs["jobName"])

		return form

	def onValidate(self, request):

		LapUtils.log.msg("AbaqusJobPage", "onValidate")

		field = {}

		field["prevFile"] = self.getField(request, 'prevFile')
		field["licenseServer"] = self.getField(request, 'licenseServer')

		field["cpuTime"] = int(self.getField(request, 'cpuTime'))
		field["jobName"] = self.getField(request, 'jobName')
		field["oldJobName"] = self.getField(request, 'oldJobName')
		field["email"] = self.getField(request, 'email')

		return ("", field)

	def onHandleFileTransfers(self, request, destDir):

		LapUtils.log.msg("AbaqusJobPage", "onHandleFileTransfers")

		if self.request().hasField("inputFile"):
			ok = True
			try:
				f = self.request().field("inputFile")
				fileContent = f.file.read()
				inputFile = file(os.path.join(destDir, f.filename), "w")
				inputFile.write(fileContent)
				inputFile.close()
			except:
				ok = False
				pass

			return ok
		else:
			return False

	def onAssignAttribs(self, task, field, request):

		LapUtils.log.msg("AbaqusJobPage", "onAssignTaskAttribs")

		attribs = task.getAttributes()

		attribs["licenseServer"] = field["licenseServer"]

		if self.request().hasField("inputFile"):

			f = self.request().field("inputFile")

			try:
				filename = f.filename
			except:
				filename = field["prevFile"]

			LapUtils.log.msg("AbaqusJobPage", "onAssignTaskAttribs", "f.filename = %s" % filename)

			task.setInputFile(filename)


#피보나치 수열의 일반항 공식을 사용하였다.
#fib(n)=((1+sqrt5)**n-(1-sqrt5)**n)/(sqrt5*2**n)
#(단,이 공식에서 sqrt5는 5의 제곱근이다)

M=1_000_000_007
inv2=500000004

assert (2*inv2)%M==1

def mypow2(real:int,pow5:int)->tuple[int,int]:
    return (real**2+5*pow5**2)%M,(2*real*pow5)%M

def mypow(real:int,pow5:int,n:int)->tuple[int,int]:
    if n==0:
        return 1,0
    if n==1:
        return real,pow5
    if n%2==0:
        return mypow2(*mypow(real,pow5,n//2))
    else:
        newreal,newpow5=mypow2(*mypow(real,pow5,n//2))
        return (newreal*real+5*pow5*newpow5)%M,(newreal*pow5+real*newpow5)%M

def fib(n:int)->int:
    _,p1=mypow(1,1,n)
    _,p2=mypow(1,-1,n)
    return ((p1-p2)*pow(inv2,n,M))%M

n=int(input())
print(fib(n))


#! /usr/bin/env python
"""
Script to split a dataset into chunks of equal time
"""
import argparse
from argparse import RawTextHelpFormatter
import casacore.tables as pt
import numpy as np
import sys
import os
import shutil

def main(dataset, blockl, local_dir=None, clobber=True):
    """
    Split dataset into time chunks

    Parameters
    ----------
    dataset : str
        Name of MS file to split
    blockl : int
        Number of time slots per chunk
    local_dir : str, optional
        Path to local directory for output of t1.copy(). The file is then
        copied to the original output directory
    clobber : bool, optional
        If True, existing files are overwritten

    """
    if type(clobber) is str:
        if clobber.lower() == 'true':
            clobber = True
        else:
            clobber = False

    blockl = int(blockl)
    if blockl < 1:
        blockl = 1

    # Get time per sample and number of samples
    t = pt.table(dataset, readonly=True, ack=False)
    for t2 in t.iter(["ANTENNA1","ANTENNA2"]):
        if (t2.getcell('ANTENNA1',0)) < (t2.getcell('ANTENNA2',0)):
            timepersample = t2[1]['TIME']-t2[0]['TIME'] # sec
            nsamples = t2.nrows()
            break
    t.close()

    nchunks = int(np.ceil((np.float(nsamples) / np.float(blockl))))

    # Don't allow more than 15 chunks for performance reasons
    while nchunks > 15:
        blockl *= 2
        nchunks = int(np.ceil((np.float(nsamples) / np.float(blockl))))

    tlen = timepersample * np.float(blockl) / 3600.0 # length of block in hours
    tobs = timepersample * nsamples / 3600.0 # length of obs in hours

    # Copy to local directory if needed
    dataset_original = dataset
    if local_dir is not None:
        dataset = os.path.join(local_dir, os.path.basename(dataset_original))
        os.system('/usr/bin/rsync -a {0} {1}'.format(dataset_original, local_dir))

    files = []
    for c in range(nchunks):
        chunk_file = '{0}_chunk{1}.ms'.format(os.path.splitext(dataset_original)[0], c)
        files.append(chunk_file)
        t0 = tlen * np.float(c) # hours
        t1 = t0 + tlen # hours
        if c == 0:
            t0 = -0.1 # make sure first chunk gets first slot
        if c == nchunks-1 and t1 < tobs:
            t1 = tobs + 0.1 # make sure last chunk gets all that remains
        split_ms(dataset, chunk_file, t0, t1, local_dir, clobber=clobber)

    if local_dir is not None and not os.path.samefile(dataset, dataset_original):
        shutil.rmtree(dataset)

    return {'files': '[{0}]'.format(','.join(files))}

def split_ms(msin, msout, start_out, end_out, local_dir, clobber=True):
    """
    Splits an MS between start and end times in hours relative to first time

    Parameters
    ----------
    msin : str
        Name of MS file to split
    msout : str
        Name of output MS file
    start_out : float
        Start time in hours relative to first time
    end_out : float
        End time in hours relative to first time
    local_dir : str
        Path to local directory for output of t1.copy(). The file is then
        copied to the original output directory
    clobber : bool, optional
        If True, existing files are overwritten

    """
    if os.path.exists(msout):
        if clobber:
            os.system('rm -rf {0}'.format(msout))
        else:
            return

    msout_original = msout
    if local_dir is not None:
        msout = os.path.join(local_dir, os.path.basename(msout_original))
        if os.path.exists(msout):
            os.system('rm -rf {0}'.format(msout))

    t = pt.table(msin, ack=False)
    starttime = t[0]['TIME']

    t1 = t.query('TIME >= ' + str(starttime+start_out*3600.0) + ' && '
      'TIME < ' + str(starttime+end_out*3600.0), sortlist='TIME,ANTENNA1,ANTENNA2')

    t1.copy(msout, True)
    t1.close()
    t.close()

    if local_dir is not None:
        msout_destination_dir = os.path.dirname(msout_original)
        os.system('/usr/bin/rsync -a {0} {1}'.format(msout, msout_destination_dir))
        if not os.path.samefile(msout, msout_original):
            shutil.rmtree(msout)

if __name__ == '__main__':
    descriptiontext = "Chunk a dataset in time.\n"

    parser = argparse.ArgumentParser(description=descriptiontext, formatter_class=RawTextHelpFormatter)
    parser.add_argument('ms_filename', help='Dataset name')
    parser.add_argument('-w', '--width', help='width of chunks in number of samples', type=int, default=10)

    args = parser.parse_args()
    main(args.ms_filename, blockl=args.width)


import os
import argparse
import time
import random
import math
import itertools

import numpy as np
import torch
import torch.nn as nn
from torch.utils import data
from torch import optim
from tqdm import tqdm
from torchtext.data.metrics import bleu_score

from dataset import Raw_Video_Dataset
from models import transformer
from utils import utils

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
torch.manual_seed(0)
np.random.seed(0)

def validation(dataloader, encoder, decoder, criterion):

    encoder.eval()
    decoder.eval()

    with torch.no_grad():
        running_loss = 0.0

        for idx, x in enumerate(tqdm(dataloader)):

            real_vid, real_targ, real_dp = x

            real_vid = real_vid.to(device)
            real_targ = real_targ.to(device)

            enc_src = encoder(real_vid)

            #enc_src = [batch size, src len, hid dim]
            output, attention = decoder(real_targ[:,:-1], enc_src)

            output = output.contiguous().view(-1, 30)
            real_targ = real_targ[:,1:].contiguous().view(-1)

            seq_loss = criterion(output, real_targ)

            running_loss += seq_loss.item()

    return running_loss / (len(dataloader))

def prediction(dataloader, encoder, decoder, target_alphabet,  SOS_TOKEN = 0, EOS_TOKEN = 29, MAX_LENGTH = 70, PAD_TOKEN = 1):

    target_alphabet = dict( (v,k) for k,v in target_alphabet.items())

    encoder.eval()
    decoder.eval()

    all_trgs = []
    all_pred_trgs = []

    all_trgs_f = []
    all_pred_trgs_f = []

    for idx , x in enumerate(tqdm(dataloader)):

        vid, targ, dp = x

        vid = vid.to(device)
        targ = targ.to(device)

        with torch.no_grad():
            encoded, _ = encoder(vid)

        trg_indexes = [SOS_TOKEN] # sos token

        for i in range(MAX_LENGTH):

            trg_tensor = torch.LongTensor(trg_indexes).unsqueeze(0).to(device)

            with torch.no_grad():
                output, _ = decoder(trg_tensor, encoded)

            pred_token = output.argmax(2)[:,-1].item()

            trg_indexes.append(pred_token)

            if pred_token == EOS_TOKEN:
                break

        trg_tokens = [target_alphabet[i] for i in trg_indexes]
        trg_tokens.pop(0)
        trg_tokens.pop(-1)
        trg_tokens_joined = ''.join(trg_tokens) #gt

        trg_np = targ.squeeze().detach().cpu().numpy().tolist()
        trg_english = [target_alphabet[i] for i in trg_np if not i == PAD_TOKEN] #prediction
        trg_english.pop(0)
        trg_english.pop(-1)
        trg_english_joined = ''.join(trg_english)

        all_trgs.append([trg_tokens_joined.split(' ')])
        all_pred_trgs.append(trg_english_joined.split(' '))

        all_trgs_f.append(trg_tokens_joined)
        all_pred_trgs_f.append(trg_english_joined)

        print ("Predicted sentence: {0}".format(trg_tokens_joined))
        print ("Ground truth sentence: {0}".format(trg_english_joined))

    # utils.write_to_f(all_pred_trgs, all_trgs, fname = 'synthetic_translations.txt')
    bleu= bleu_score(all_pred_trgs, all_trgs)
    print (bleu)
    exit()
    return bleu

if __name__ == '__main__':

    parser = argparse.ArgumentParser()

    #model hyperparameters
    parser.add_argument("--hid_dim", type = int, default = 256)
    parser.add_argument("--emb_dim", type = int, default = 128)
    parser.add_argument("--n_layers", type = int, default = 4)
    parser.add_argument("--output_dim", type = int, default = 30)
    parser.add_argument("--dropout", type = float, default = 0.2)
    parser.add_argument("--input_size", type = int, default = 128)

    #optimizer hyperparameters
    parser.add_argument("--transformer_lr", type = float, default = 0.0001)
    parser.add_argument("--max_epochs", type = int, default = 500)

    #dataset hyperparameters
    parser.add_argument('--batch_size', type=int, default=4)
    parser.add_argument("--num_workers", type = int, default = 8)

    #data paths
    parser.add_argument("--synthetic_train_data_path", type = str, default = '/data/synthetic_preprocessed_split/train')
    parser.add_argument("--synthetic_test_data_path", type = str, default = '/data/synthetic_preprocessed_split/val')
    parser.add_argument("--real_train_data_path", type = str, default = 'data/real_life_videos_preprocessed/train/')
    parser.add_argument("--real_test_data_path", type = str, default = 'data/real_life_videos_preprocessed/test/')
    parser.add_argument('--output_path', type = str, default = 'weights/synthetic_transformer_recon')
    #training helpers
    args = parser.parse_args()

    if not os.path.exists(args.output_path):
        os.makedirs(args.output_path)

    syn_train_dataset = Raw_Video_Dataset(args.synthetic_train_data_path, synthetic = True, training = True, video_length = 300)
    syn_train_dataloader = data.DataLoader(syn_train_dataset, batch_size = args.batch_size, num_workers = args.num_workers, shuffle = True)

    syn_test_dataset = Raw_Video_Dataset(args.synthetic_test_data_path, synthetic = False, training = False, video_length = 300)
    syn_test_dataloader = data.DataLoader(syn_test_dataset, batch_size = args.batch_size, num_workers = args.num_workers, shuffle = False)
    evaluation_dataloader = data.DataLoader(syn_test_dataset, batch_size = 1, num_workers = args.num_workers, shuffle = False)

    seq_criterion = nn.CrossEntropyLoss(ignore_index = 1)
    mse_criterion = nn.MSELoss()

    '''Encoder'''
    encoder = transformer.Encoder(args.input_size, args.hid_dim, args.n_layers, 4, args.emb_dim, args.dropout, device, max_length = 300)
    cnn = transformer.ConvNet()
    # cnn.load_state_dict(torch.load('best_source_cnn_extractor_.pth'))
    transformer_encoder = transformer.CNN_Transformer_Encoder(cnn, encoder, device)
    transformer_encoder = nn.DataParallel(transformer_encoder)
    transformer_encoder.to(device)

    '''Decoder'''
    decoder = transformer.Decoder(args.output_dim, args.hid_dim, args.n_layers, 4, args.emb_dim, args.dropout, device)
    decoder = nn.DataParallel(decoder)
    decoder.to(device)

    #predict here
    transformer_encoder.load_state_dict(torch.load('weights/synthetic_transformer/350_encoder_model.pt'))
    decoder.load_state_dict(torch.load('weights/synthetic_transformer/350_decoder_model.pt'))

    transformer_encoder.module.cnn.load_state_dict(torch.load('best_source_cnn_extractor_.pth'))

    prediction(evaluation_dataloader, transformer_encoder, decoder, syn_test_dataset.alphabet)
    exit()

    optimizer = torch.optim.Adam(
                                itertools.chain(
                                    transformer_encoder.parameters(),
                                    decoder.parameters(),
                                    recon.parameters(),
                                ),
                                lr = args.transformer_lr)

    print (optimizer)
    for epoch_iter in range(args.max_epochs):

        transformer_encoder.train()
        decoder.train()

        running_seq_loss = 0.0

        for idx, x in enumerate(tqdm(syn_train_dataloader)):
            optimizer.zero_grad()

            syn_vid, syn_targ, _ = x
            syn_vid = syn_vid.to(device)
            syn_targ = syn_targ.to(device)

            syn_encoded, _ = transformer_encoder(syn_vid)
#
            syn_output, _ = decoder(syn_targ[:,:-1], syn_encoded )
            syn_output = syn_output.contiguous().view(-1, 30)
            syn_targ = syn_targ[:,1:].contiguous().view(-1)

            seq_loss = seq_criterion(syn_output, syn_targ)

            running_seq_loss += seq_loss.item()

            seq_loss.backward()
            optimizer.step()

        print ("Running Validation")
        this_seq_loss = running_seq_loss / len(syn_train_dataloader)
        if (epoch_iter % 25) == 0:
            print ("Saving")
            torch.save(transformer_encoder.state_dict(), os.path.join(args.output_path, '{}_encoder_model.pt'.format(epoch_iter)))
            torch.save(decoder.state_dict(), os.path.join(args.output_path, '{}_decoder_model.pt'.format(epoch_iter)))
        syn_val_loss = validation(syn_test_dataloader, transformer_encoder, decoder, seq_criterion)
        print ("Epoch: {} | Train Loss: {} | Syn Val Loss: {}|" .format(epoch_iter, this_seq_loss,  syn_val_loss))


import os

BOT_NAME = 'harvest'
SPIDER_MODULES = ['harvest.spiders']
NEWSPIDER_MODULE = 'harvest.spiders'

DOWNLOAD_DELAY = 3
CONCURRENT_REQUESTS = 4
COOKIES_ENABLED = False

_src = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
_target = os.path.join(os.path.dirname(_src), 'target')
JOBDIR = os.path.join(_target, 'resume')
FEED_URI = os.path.join(_target, 'crawled')
FEED_FORMAT = 'csv'


import sys
import isceobj
from iscesys.Component.Component import Component
from mroipac.grass.grass import Grass
import os

# giangi: taken Piyush code grass.py and adapted

def runUnwrap(self):

    wrapName = os.path.join( self._insar.mergedDirname, self._insar.filtFilename)
    unwrapName = os.path.join( self._insar.mergedDirname, self._insar.unwrappedIntFilename)
    corName = os.path.join(self._insar.mergedDirname, self._insar.coherenceFilename)

    intImage = isceobj.createImage()
    intImage.load(wrapName + '.xml')
    intImage.setAccessMode('READ')

    cohImage = isceobj.createImage()
    cohImage.load(corName + '.xml')
    cohImage.setAccessMode('READ')

    unwImage = isceobj.createImage()
    unwImage.bands = 2
    unwImage.scheme = 'BIL'
    unwImage.dataType = 'FLOAT'
    unwImage.setFilename(unwrapName)
    unwImage.setWidth(intImage.getWidth())
    unwImage.setAccessMode('WRITE')

    grs=Grass(name='topsapp_grass')
    grs.configure()
    grs.wireInputPort(name='interferogram',
                    object=intImage)
    grs.wireInputPort(name='correlation',
                    object=cohImage)
    grs.wireInputPort(name='unwrapped interferogram',
                    object=unwImage)
    grs.unwrap()

    unwImage.renderHdr()

    return None


__author__ = 'jinguangzhou'
from django.conf.urls import url, include

from .views import EmployerCreateView, EmployerDetailView, jobs, dashboard, EmployerUpdateView

urlpatterns = [
    url(r'^', include('django.contrib.auth.urls')),
    url(r'^registration/', EmployerCreateView.as_view(), name='create'),
    url(r'^(?P<pk>[0-9]+)/$', EmployerDetailView.as_view(), name='detail'),
    url(r'^jobs/$', jobs, name='jobs'),
    url(r'^dashboard', dashboard, name='dashboard'),
    url(r'^(?P<pk>[0-9]+)/update/$', EmployerUpdateView.as_view(), name='update'),
]