text:
#!/usr/bin/env python
# coding: utf-8
edtion = 'alpha 1.11.1'

# 外部参数输入

import argparse
import sys
import os

ap = argparse.ArgumentParser(description="Export Premiere Pro XML from timeline file.")
ap.add_argument("-l", "--TimeLine", help='Timeline (and break_point with same name), which was generated by replay_generator.py.',type=str)
ap.add_argument("-d", "--MediaObjDefine", help='Definition of the media elements, using real python code.',type=str)
ap.add_argument("-t", "--CharacterTable", help='This program do not need CharacterTable.',type=str)
ap.add_argument("-o", "--OutputPath", help='Choose the destination directory to save the project timeline and break_point file.',type=str,default=None)
# 增加一个，读取时间轴和断点文件的选项！
ap.add_argument("-F", "--FramePerSecond", help='Set the FPS of display, default is 30 fps, larger than this may cause lag.',type=int,default=30)
ap.add_argument("-W", "--Width", help='Set the resolution of display, default is 1920, larger than this may cause lag.',type=int,default=1920)
ap.add_argument("-H", "--Height", help='Set the resolution of display, default is 1080, larger than this may cause lag.',type=int,default=1080)
ap.add_argument("-Z", "--Zorder", help='Set the display order of layers, not recommended to change the values unless necessary!',type=str,
                default='BG3,BG2,BG1,Am3,Am2,Am1,Bb')
args = ap.parse_args()

media_obj = args.MediaObjDefine #媒体对象定义文件的路径
char_tab = args.CharacterTable #角色和媒体对象的对应关系文件的路径
stdin_log = args.TimeLine #log路径
output_path = args.OutputPath #保存的时间轴，断点文件的目录

screen_size = (args.Width,args.Height) #显示的分辨率
frame_rate = args.FramePerSecond #帧率 单位fps
zorder = args.Zorder.split(',') #渲染图层顺序

try:
    for path in [stdin_log,media_obj]:
        if path == None:
            raise OSError("[ArgumentError]: Missing principal input argument!")
        if os.path.isfile(path) == False:
            raise OSError("[ArgumentError]: Cannot find file "+path)

    if output_path == None:
        pass
    elif os.path.isdir(output_path) == False:
        try:
            os.makedirs(output_path)
        except Exception:
            raise OSError("[SystemError]: Cannot make directory "+output_path)
    output_path = output_path.replace('\\','/')

    # FPS
    if frame_rate <= 0:
        raise ValueError("[ArgumentError]: "+str(frame_rate))
    elif frame_rate>30:
        print("[warning]:",'FPS is set to '+str(frame_rate)+', which may cause lag in the display!')

    if (screen_size[0]<=0) | (screen_size[1]<=0):
        raise ValueError("[ArgumentError]: "+str(screen_size))
    if screen_size[0]*screen_size[1] > 3e6:
        print("[warning]:",'Resolution is set to more than 3M, which may cause lag in the display!')
except Exception as E:
    print(E)
    sys.exit(1)

# 包导入

import pandas as pd
import numpy as np
from PIL import Image,ImageFont,ImageDraw
import re
import time #开发模式，显示渲染帧率
from pygame import mixer
import glob # 匹配路径

# 文字对象

outtext_index = 0
outanime_index = 0
clip_index = 0
file_index = 0

class Text:
    def __init__(self,fontfile='./media/SourceHanSansCN-Regular.otf',fontsize=40,color=(0,0,0,255),line_limit=20,label_color='Lavender'):
        self.color=color
        self.size=fontsize
        self.line_limit = line_limit
        self.fontpath = fontfile
        self.label_color = label_color
    def render(self,tx):
        font_this = ImageFont.truetype(self.fontpath, self.size)
        text_this = Image.new(mode='RGBA',size=(self.size*int(len(tx)*1.5),self.size*2),color=(0,0,0,0)) # 画布贪婪为2x高度，1.5*宽度
        draw_this = ImageDraw.Draw(text_this)
        draw_this.text((0,0),tx,font = font_this,align ="left",fill = self.color)
        return text_this
    def draw(self,text):
        out_text = []
        if ('#' in text) | (text[0]=='^'): #如果有手动指定的换行符
            if text[0]=='^': # 如果使用^指定的手动换行，则先去掉这个字符。
                text = text[1:]
            text_line = text.split('#')
            for tx in text_line:
                out_text.append(self.render(tx))
        elif len(text) > self.line_limit: #如果既没有主动指定，字符长度也超限
            for i in range(0,len(text)//self.line_limit+1):
                out_text.append(self.render(text[i*self.line_limit:(i+1)*self.line_limit]))
        else:
            out_text = [self.render(text)]
        return out_text
    def convert(self):
        pass

class StrokeText(Text):
    def __init__(self,fontfile='./media/SourceHanSansCN-Regular.otf',fontsize=40,color=(0,0,0,255),line_limit=20,edge_color=(255,255,255,255),label_color='Lavender'):
        super().__init__(fontfile=fontfile,fontsize=fontsize,color=color,line_limit=line_limit,label_color=label_color) # 继承
        self.edge_color=edge_color
    def render(self,tx):
        font_this = ImageFont.truetype(self.fontpath, self.size)
        text_this = Image.new(mode='RGBA',size=(self.size*int(len(tx)*1.5),self.size*2),color=(0,0,0,0)) # 画布贪婪为2x高度，1.5*宽度
        draw_this = ImageDraw.Draw(text_this)
        for pos in [(0,0),(0,1),(0,2),(1,0),(1,2),(2,0),(2,1),(2,2)]:
            draw_this.text(pos,tx,font = font_this,align ="left",fill = self.edge_color)
        draw_this.text((1,1),tx,font = font_this,align ="left",fill = self.color)
        return text_this

    # 对话框、气泡、文本框
class Bubble:
    def __init__(self,filepath,Main_Text=Text(),Header_Text=None,pos=(0,0),mt_pos=(0,0),ht_pos=(0,0),align='left',line_distance=1.5,label_color='Lavender'):
        global file_index
        self.path = reformat_path(filepath)
        self.MainText = Main_Text
        self.mt_pos = mt_pos
        self.Header = Header_Text
        self.ht_pos = ht_pos
        self.pos = pos
        self.line_distance = line_distance
        self.size = Image.open(filepath).size
        self.filename = self.path.split('/')[-1]
        self.fileindex = 'BBfile_' + '%d'% file_index
        self.PRpos = PR_center_arg(np.array(self.size),np.array(self.pos))
        self.align = align
        self.label_color = label_color
        file_index = file_index+1
    def display(self,begin,end,text,header=''): # 这段代码是完全没有可读性的屎，但是确实可运行，非必要不要改
        global outtext_index,clip_tplt,clip_index
        # 生成文本图片
        ofile = output_path+'/auto_TX_%d'%outtext_index+'.png'
        canvas = Image.new(mode='RGBA',size=self.size,color=(0,0,0,0))
        if (self.Header!=None) & (header!=''):    # Header 有定义，且输入文本不为空
            ht_text = self.Header.draw(header)[0]
            try:
                p1,p2,p3,p4 = ht_text.getbbox() # 如果是空图的话，getbbox返回None，会发生TypeError
                canvas.paste(ht_text.crop((p1,p2,p3,p4)),(self.ht_pos[0]+p1,self.ht_pos[1]+p2)) # 兼容微软雅黑这种，bbox到处飘的字体
            except TypeError:
                pass
        x,y = self.mt_pos
        for i,mt_text in enumerate(self.MainText.draw(text)):
            try:
                p1,p2,p3,p4 = mt_text.getbbox() # 先按照bboxcrop，然后按照原位置放置
            except TypeError: # 如果遇到了空图导致的TypeError，直接跳过这一循环，走到下一行
                continue
            if self.align == 'left':
                canvas.paste(mt_text.crop((p1,p2,p3,p4)),(x+p1,int(y+i*self.MainText.size*self.line_distance+p2)))
            else: # alpha 1.7.0 兼容居中
                word_w = p3 - p1
                canvas.paste(mt_text.crop((p1,p2,p3,p4)),
                             (x + p1 + (self.MainText.size*self.MainText.line_limit - word_w)//2,
                              int(y+i*self.MainText.size*self.line_distance+p2)
                             )
                            )
        canvas.save(ofile)

        # 生成序列
        width,height = self.size
        pr_horiz,pr_vert = self.PRpos
        clip_bubble = clip_tplt.format(**{'clipid':'BB_clip_%d'%clip_index,
                              'clipname':self.filename,
                              'timebase':'%d'%frame_rate,
                              'ntsc':Is_NTSC,
                              'start':'%d'%begin,
                              'end':'%d'%end,
                              'in':'%d'%90000,
                              'out':'%d'%(90000+end-begin),
                              'fileid':self.fileindex,
                              'filename':self.filename,
                              'filepath':self.path,
                              'filewidth':'%d'%width,
                              'fileheight':'%d'%height,
                              'horiz':'%.5f'%pr_horiz,
                              'vert':'%.5f'%pr_vert,
                              'colorlabel':self.label_color})
        clip_text = clip_tplt.format(**{'clipid':'TX_clip_%d'%clip_index,
                              'clipname':'auto_TX_%d.png'%outtext_index,
                              'timebase':'%d'%frame_rate,
                              'ntsc':Is_NTSC,
                              'start':'%d'%begin,
                              'end':'%d'%end,
                              'in':'%d'%90000,
                              'out':'%d'%(90000+end-begin),
                              'fileid':'auto_TX_%d'%outtext_index,
                              'filename':'auto_TX_%d.png'%outtext_index,
                              'filepath':reformat_path(ofile),
                              'filewidth':'%d'%width,
                              'fileheight':'%d'%height,
                              'horiz':'%.5f'%pr_horiz,
                              'vert':'%.5f'%pr_vert,
                              'colorlabel':self.MainText.label_color})

        outtext_index = outtext_index + 1
        clip_index = clip_index+1
        return (clip_bubble,clip_text)

    def convert(self):
        pass

# 背景图片
class Background:
    def __init__(self,filepath,pos = (0,0),label_color='Lavender'):
        global file_index
        if filepath in cmap.keys(): #对纯色定义的背景的支持
            ofile = output_path+'/auto_BG_'+filepath+'.png'
            Image.new(mode='RGBA',size=screen_size,color=cmap[filepath]).save(ofile)
            self.path = reformat_path(ofile)
            self.size = screen_size
        else:
            self.path = reformat_path(filepath)
            self.size = Image.open(filepath).size
        self.pos = pos
        self.PRpos = PR_center_arg(np.array(self.size),np.array(self.pos))
        self.filename = self.path.split('/')[-1]
        self.fileindex = 'BGfile_%d'% file_index
        self.label_color = label_color
        file_index = file_index+1
    def display(self,begin,end):
        global clip_tplt,clip_index
        width,height = self.size
        pr_horiz,pr_vert = self.PRpos
        clip_this = clip_tplt.format(**{'clipid':'BG_clip_%d'%clip_index,
                              'clipname':self.filename,
                              'timebase':'%d'%frame_rate,
                              'ntsc':Is_NTSC,
                              'start':'%d'%begin,
                              'end':'%d'%end,
                              'in':'%d'%90000,
                              'out':'%d'%(90000+end-begin),
                              'fileid':self.fileindex,
                              'filename':self.filename,
                              'filepath':self.path,
                              'filewidth':'%d'%width,
                              'fileheight':'%d'%height,
                              'horiz':'%.5f'%pr_horiz,
                              'vert':'%.5f'%pr_vert,
                              'colorlabel':self.label_color})
        clip_index = clip_index+1
        return clip_this
    def convert(self):
        pass

# 立绘图片
class Animation:
    def __init__(self,filepath,pos = (0,0),tick=1,loop=True,label_color='Lavender'):
        global file_index
        self.path = reformat_path(glob.glob(filepath)[0]) # 兼容动画Animation，只使用第一帧！
        self.pos = pos
        self.size = Image.open(glob.glob(filepath)[0].replace('\\','/')).size # 兼容动画
        self.filename = self.path.split('/')[-1]
        self.fileindex = 'AMfile_%d'% file_index
        self.PRpos = PR_center_arg(np.array(self.size),np.array(self.pos))
        self.label_color = label_color
        file_index = file_index+1
    def display(self,begin,end):
        global clip_tplt,clip_index
        width,height = self.size
        pr_horiz,pr_vert = self.PRpos
        clip_this = clip_tplt.format(**{'clipid':'AM_clip_%d'%clip_index,
                              'clipname':self.filename,
                              'timebase':'%d'%frame_rate,
                              'ntsc':Is_NTSC,
                              'start':'%d'%begin,
                              'end':'%d'%end,
                              'in':'%d'%90000,
                              'out':'%d'%(90000+end-begin),
                              'fileid':self.fileindex,
                              'filename':self.filename,
                              'filepath':self.path,
                              'filewidth':'%d'%width,
                              'fileheight':'%d'%height,
                              'horiz':'%.5f'%pr_horiz,
                              'vert':'%.5f'%pr_vert,
                              'colorlabel':self.label_color})
        clip_index = clip_index+1
        return clip_this
    def convert(self):
        pass

# a1.6.5 内建动画，这是一个Animation类的子类，重构了构造函数
class BuiltInAnimation(Animation):
    def __init__(self,anime_type='hitpoint',anime_args=('0',0,0,0),screensize = (1920,1080),layer=0,label_color='Mango'):
        global file_index,outanime_index
        self.label_color = label_color
        if anime_type == 'hitpoint':
            # 载入图片
            heart = Image.open('./media/heart.png')
            heart_shape = Image.open('./media/heart_shape.png')
            hx,hy = heart.size
            # 重设图片尺寸，根据screensize[0]
            if screensize[0]!=1920:
                multip = screensize[0]/1920
                heart = heart.resize((int(hx*multip),int(hy*multip)))
                heart_shape = heart_shape.resize((int(hx*multip),int(hy*multip)))
                hx,hy = heart.size
            # 动画参数
            name_tx,heart_max,heart_begin,heart_end = anime_args
            if (heart_end==heart_begin)|(heart_max<max(heart_begin,heart_end)):
                raise MediaError('[BIAnimeError]:','Invalid argument',name_tx,heart_max,heart_begin,heart_end,'for BIAnime hitpoint!')
            elif heart_end > heart_begin: # 如果是生命恢复
                temp = heart_end
                heart_end = heart_begin
                heart_begin = temp # 则互换顺序 确保 begin一定是小于end的

            distance = int(0.026*screensize[0]) # default = 50

            total_heart = int(heart_max/2 * hx + max(0,np.ceil(heart_max/2-1)) * distance) #画布总长
            left_heart = int(heart_end/2 * hx + max(0,np.ceil(heart_end/2-1)) * distance) #画布总长
            lost_heart = int((heart_begin-heart_end)/2 * hx + np.floor((heart_begin-heart_end)/2) * distance)
            # 姓名文本
            BIA_text = ImageFont.truetype('./media/SourceHanSerifSC-Heavy.otf', int(0.0521*screensize[0])) # 1080p:size=100
            test_canvas = Image.new(mode='RGBA',size=screensize,color=(0,0,0,0))
            test_draw = ImageDraw.Draw(test_canvas)
            test_draw.text((0,0), name_tx, font = BIA_text, align ="left",fill = (255,255,255,255))
            try:
                p1,p2,p3,p4 = test_canvas.getbbox()
            except TypeError:
                p1,p2,p3,p4 = (0,0,1,int(0.0521*screensize[0])) # nx=1 ny =fontsize
            nx = p3 - p1
            ny = p4 - p2
            nametx_surf = test_canvas.crop((p1,p2,p3,p4))
            # 开始制图
            if layer==0: # 底层 阴影图
                self.pos = ((screensize[0]-max(nx,total_heart))/2,(4/5*screensize[1]-hy-ny)/2)
                canvas = Image.new(size=(max(nx,total_heart),hy+ny+screensize[1]//5),mode='RGBA',color=(0,0,0,0))
                self.size = canvas.size
                if nx > total_heart:
                    canvas.paste(nametx_surf,(0,0))
                    posx = (nx-total_heart)//2
                else:
                    canvas.paste(nametx_surf,((total_heart-nx)//2,0))
                    posx = 0
                posy = ny+screensize[1]//5
                for i in range(1,heart_max+1): # 偶数，低于最终血量
                    if i%2 == 0:
                        canvas.paste(heart_shape,(posx,posy))
                        posx = posx + hx + distance
                    else:
                        pass
                if heart_max%2 == 1: # max是奇数
                    left_heart_shape = heart_shape.crop((0,0,int(hx/2),hy))
                    canvas.paste(left_heart_shape,(total_heart-int(hx/2),posy))
            elif layer==1: # 剩余的血量
                self.pos = ((screensize[0]-total_heart)/2,3/5*screensize[1]+ny/2-hy/2)
                # 1.6.5 防止报错 剩余血量即使是空图，也要至少宽30pix
                canvas = Image.new(size=(max(30,left_heart),hy),mode='RGBA',color=(0,0,0,0))
                self.size = canvas.size
                posx,posy = 0,0
                for i in range(1,heart_end+1): # 偶数，低于最终血量
                    if i%2 == 0:
                        canvas.paste(heart,(posx,posy))
                        posx = posx + hx + distance
                    else:
                        pass
                if heart_end%2 == 1: # end是奇数
                    left_heart = heart.crop((0,0,int(hx/2),hy))
                    canvas.paste(left_heart,(heart_end//2*(hx + distance),0))
            elif layer==2: # 损失/恢复的血量
                self.pos = (heart_end//2*(hx + distance)+(heart_end%2)*int(hx/2)+(screensize[0]-total_heart)/2,3/5*screensize[1]+ny/2-hy/2)
                canvas = Image.new(size=(lost_heart,hy),mode='RGBA',color=(0,0,0,0))
                self.size = canvas.size
                posx,posy = 0,0
                for i in range(1,heart_begin-heart_end+1):
                    if (i == 1)&(heart_end%2 == 1): # 如果end是奇数，先来半个右边
                        right_heart = heart.crop((int(hx/2),0,hx,hy))
                        canvas.paste(right_heart,(posx,posy))
                        posx = posx + int(hx/2) + distance
                    elif ((i - heart_end%2)%2 == 0): # 如果和end的差值是
                        canvas.paste(heart,(posx,posy))
                        posx = posx + hx + distance
                    elif (i == heart_begin-heart_end)&(heart_begin%2 == 1): # 如果最右边边也是半个心
                        left_heart = heart.crop((0,0,int(hx/2),hy))
                        canvas.paste(left_heart,(posx,posy))
                    else:
                        pass
            else:
                pass
            ofile = output_path+'/auto_BIA_%d'%outanime_index+'.png'
            canvas.save(ofile)

            #剩下的需要定义的
            self.path = reformat_path(ofile) # 兼容动画Animation，只使用第一帧！
            self.filename = 'auto_BIA_%d'%outanime_index+'.png'
            self.fileindex = 'AMfile_%d'% file_index
            #print(np.array(self.size),np.array(self.pos))
            self.PRpos = PR_center_arg(np.array(self.size),np.array(self.pos))
            outanime_index = outanime_index+1
            file_index = file_index+1
        if anime_type == 'dice':
            for die in anime_args:
                try:
                    # 转换为int类型，NA转换为-1
                    name_tx,dice_max,dice_check,dice_face = die
                    dice_max,dice_face,dice_check = map(lambda x:-1 if x=='NA' else int(x),(dice_max,dice_face,dice_check))
                except ValueError as E: #too many values to unpack,not enough values to unpack
                    raise MediaError('[BIAnimeError]:','Invalid syntax:',str(die),E)
                if (dice_face>dice_max)|(dice_check<-1)|(dice_check>dice_max)|(dice_face<0)|(dice_max<=0):
                    raise MediaError('[BIAnimeError]:','Invalid argument',name_tx,dice_max,dice_check,dice_face,'for BIAnime dice!')
            N_dice = len(anime_args)
            if N_dice > 4:
                N_dice=4
                anime_args = anime_args[0:4]# 最多4个
            y_anchor = {4:int(0.1667*screensize[1]),3:int(0.25*screensize[1]),2:int(0.3333*screensize[1]),1:int(0.4167*screensize[1])}[N_dice]
            y_unit = int(0.1667*screensize[1])
            BIA_text = ImageFont.truetype('./media/SourceHanSerifSC-Heavy.otf', int(0.0521*screensize[0]))
            if layer==0: # 底层 名字 /检定
                canvas = Image.new(mode='RGBA',size=screensize,color=(0,0,0,0))
                for i,die in enumerate(anime_args):
                    name_tx,dice_max,dice_check,dice_face = die
                    dice_max,dice_face,dice_check = map(lambda x:-1 if x=='NA' else int(x),(dice_max,dice_face,dice_check))
                    # 渲染
                    test_canvas = Image.new(mode='RGBA',size=screensize,color=(0,0,0,0))
                    test_draw = ImageDraw.Draw(test_canvas)
                    test_draw.text((0,0), name_tx, font = BIA_text, align ="left",fill = (255,255,255,255))
                    try:
                        p1,p2,p3,p4 = test_canvas.getbbox() # 重新包装为函数？
                    except TypeError:
                        p1,p2,p3,p4 = (0,0,1,int(0.0521*screensize[0])) # nx=1 ny =fontsize
                    nx = p3 - p1
                    ny = p4 - p2
                    name_surf = test_canvas.crop((p1,p2,p3,p4))
                    canvas.paste(name_surf,(int(0.3125*screensize[0])-nx//2,y_anchor+i*y_unit+(y_unit-ny)//2)) # 0.3125*screensize[0] = 600
                    if dice_check != -1:
                        test_canvas = Image.new(mode='RGBA',size=screensize,color=(0,0,0,0))
                        test_draw = ImageDraw.Draw(test_canvas)
                        test_draw.text((0,0), '/%d'%dice_check, font = BIA_text, align ="left",fill = (255,255,255,255))
                        try:
                            p1,p2,p3,p4 = test_canvas.getbbox()
                        except TypeError:
                            p1,p2,p3,p4 = (0,0,1,int(0.0521*screensize[0])) # nx=1 ny =fontsize
                        cx = p3 - p1
                        cy = p4 - p2
                        check_surf = test_canvas.crop((p1,p2,p3,p4))
                        canvas.paste(check_surf,(int(0.7292*screensize[0]),y_anchor+i*y_unit+(y_unit-ny)//2)) # 0.7292*screensize[0] = 1400
                self.size = screen_size
                self.pos = (0,0)
            elif layer==1: #无法显示动态，留空白
                canvas = Image.new(mode='RGBA',size=(int(0.1458*screensize[0]),y_unit*N_dice),color=(0,0,0,0))
                self.size = (int(0.1458*screensize[0]),y_unit*N_dice)
                self.pos = (int(0.5833*screensize[0]),y_anchor)
            elif layer==2:
                dice_cmap={3:(124,191,85,255),1:(94,188,235,255),0:(245,192,90,255),2:(233,86,85,255),-1:(255,255,255,255)}
                canvas = Image.new(mode='RGBA',size=(int(0.1458*screensize[0]),y_unit*N_dice),color=(0,0,0,0))
                self.size = (int(0.1458*screensize[0]),y_unit*N_dice)
                self.pos = (int(0.5833*screensize[0]),y_anchor)
                for i,die in enumerate(anime_args):
                    name_tx,dice_max,dice_check,dice_face = die
                    dice_max,dice_face,dice_check = map(lambda x:-1 if x=='NA' else int(x),(dice_max,dice_face,dice_check))
                    significant = 0.05 # 大成功失败阈值
                    if dice_check == -1:
                        color_flag = -1
                    else:
                        color_flag = ((dice_face/dice_max<=significant)|(dice_face/dice_max>(1-significant)))*2 + (dice_face<=dice_check)
                    BIA_color_Text = ImageFont.truetype('./media/SourceHanSerifSC-Heavy.otf', int(0.0651*screensize[0]))
                    test_canvas = Image.new(mode='RGBA',size=(int(0.1458*screensize[0]),y_unit),color=(0,0,0,0))
                    test_draw = ImageDraw.Draw(test_canvas)
                    test_draw.text((0,0),str(dice_face),font=BIA_color_Text,align="left",fill=dice_cmap[color_flag])
                    try:
                        p1,p2,p3,p4 = test_canvas.getbbox()
                    except TypeError:
                        p1,p2,p3,p4 = (0,0,1,int(0.0651*screensize[0])) # nx=1 ny =fontsize
                    fx = p3 - p1
                    fy = p4 - p2
                    face_surf = test_canvas.crop((p1,p2,p3,p4))
                    canvas.paste(face_surf,(int(0.1458*screensize[0]-fx-0.0278*screensize[1]),i*y_unit+(y_unit-fy)//2))
            else:
                pass
            ofile = output_path+'/auto_BIA_%d'%outanime_index+'.png'
            canvas.save(ofile)
            self.path = reformat_path(ofile) # 兼容动画Animation，只使用第一帧！
            self.filename = 'auto_BIA_%d'%outanime_index+'.png'
            self.fileindex = 'AMfile_%d'% file_index
            self.PRpos = PR_center_arg(np.array(self.size),np.array(self.pos))
            outanime_index = outanime_index+1
            file_index = file_index+1

# 音效
class Audio:
    def __init__(self,filepath,label_color='Caribbean'):
        global file_index
        self.path = reformat_path(filepath)
        self.length = get_audio_length(filepath)*frame_rate
        self.filename = self.path.split('/')[-1]
        self.fileindex = 'AUfile_%d'% file_index
        self.label_color = label_color
        file_index = file_index+1

    def display(self,begin):
        global audio_clip_tplt,clip_index
        clip_this = audio_clip_tplt.format(**{'clipid':'AU_clip_%d'%clip_index,
                                              'type':Audio_type,
                                              'clipname':self.filename,
                                              'audiolen':'%d'%self.length,
                                              'timebase':'%d'%frame_rate,
                                              'ntsc':Is_NTSC,
                                              'start':'%d'%begin,
                                              'end':'%d'%(begin+self.length),
                                              'in':'0',
                                              'out':'%d'%self.length,
                                              'fileid':self.fileindex,
                                              'filename':self.filename,
                                              'filepath':self.path,
                                              'colorlabel':self.label_color})
        clip_index = clip_index+1
        return clip_this

    def convert(self):
        pass

# 背景音乐
class BGM:
    def __init__(self,filepath,volume=100,loop=True,label_color='Forest'):
        print('[warning]: BGM '+filepath+' is automatically ignored, you should add BGM manually in Premiere Pro later.')
    def convert(self):
        pass

# 异常定义

class ParserError(Exception):
    def __init__(self,*description):
        self.description = ' '.join(map(str,description))
    def __str__(self):
        return self.description

class MediaError(ParserError):
    pass

# 函数定义

# 获取音频长度
def get_audio_length(filepath):
    mixer.init()
    try:
        this_audio = mixer.Sound(filepath)
    except Exception as E:
        print('[warning]: Unable to get audio length of '+str(filepath)+', due to:',E)
        return np.nan
    return this_audio.get_length()

# 重格式化路径
def reformat_path(path): # alpha 1.9.5 支持unix文件系统路径
    # 获取绝对路径
    path = os.path.abspath(path)
    # 检查非法符号
    if '\\' in path: #是不是反斜杠？
        path = path.replace('\\','/')
    if ('&' in path)|('<' in path)|('>' in path):
        path = path.replace('&','&').replace('<','<').replace('>','>') # aplha1.7.2 xml 转移的bug
    if ('"' in path)|("'" in path):
        path = path.replace('"','"').replace("'",''')
    if '//' in path:
        path = path.replace('//','/')
    # 判断文件系统
    if path[0] == '/': #unix file system
        return 'file://localhost' + path
    elif (path[0].isalpha()) & (path[1]==':'): # windows disklabel
        path = path.replace(':','%3a') # 替换冒号
        return 'file://localhost/' + path
    else:
        raise ValueError('invalid path type')

# 处理bg 和 am 的parser
def parse_timeline(layer):
    global timeline,break_point
    track = timeline[[layer]]
    clips = []
    item,begin,end = 'NA',0,0
    for key,values in track.iterrows():
        #如果item变化了，或者进入了指定的断点
        if (values[layer] != item) | (key in break_point.values):
            if (item == 'NA') | (item!=item): # 如果itme是空
                pass # 则不输出什么
            else:
                end = key #否则把当前key作为一个clip的断点
                clips.append((item,begin,end)) #并记录下这个断点
            item = values[layer] #无论如何，重设item和begin
            begin = key
        else: #如果不满足断点要求，那么就什么都不做
            pass
    # 循环结束之后，最后检定一次是否需要输出一个clips
    #end = key # alpha 1.7.5 debug: 循环结束时的key有可能并不是时间轴的终点
    end = int(break_point.max()) # 因为有可能到终点为止，所有帧都是一样的，而导致被去重略去
    if (item == 'NA') | (item!=item):
        pass
    else:
        clips.append((item,begin,end))
    return clips #返回一个clip的列表

# 处理Bb 的parser
def parse_timeline_bubble(layer):
    global timeline,break_point
    track = timeline[[layer,layer+'_main',layer+'_header']]
    clips = []
    item,begin,end = 'NA',0,0
    for key,values in track.iterrows():
        #如果item变化了，或者进入了指定的断点(这是保证断句的关键！)
        if (values[layer] != item) | (key in break_point.values):
            if (item == 'NA') | (item!=item): # 如果itme是空
                pass # 则不输出什么
            else:
                end = key #否则把当前key作为一个clip的断点
                clips.append((item,main_text,header_text,begin,end)) #并记录下这个断点
            item = values[layer] #无论如何，重设item和begin
            main_text = values[layer + '_main']
            header_text = values[layer + '_header']
            begin = key
        else: #如果不满足断点要求，那么就什么都不做
            pass
        # 然后更新文本内容
        main_text = values[layer + '_main']
        header_text = values[layer + '_header']
    # 循环结束之后，最后检定一次是否需要输出一个clips
    #end = key
    end = int(break_point.max()) # alpha 1.7.5 debug: 而breakpoint的最大值一定是时间轴的终点
    if (item == 'NA') | (item!=item):
        pass
    else:
        clips.append((item,main_text,header_text,begin,end))
    return clips #返回一个clip的列表

# pygame形式的pos转换为PR形式的pos

def PR_center_arg(obj_size,pygame_pos):
    screensize = np.array(screen_size)
    return (pygame_pos+obj_size/2-screensize/2)/obj_size

# 全局变量

cmap = {'black':(0,0,0,255),'white':(255,255,255,255),'greenscreen':(0,177,64,255)}
Is_NTSC = str(frame_rate % 30 == 0)
Audio_type = 'Stereo'
stdin_name = stdin_log.replace('\\','/').split('/')[-1]
occupied_variable_name = open('./media/occupied_variable_name.list','r',encoding='utf8').read().split('\n')

# 载入xml 模板文件

project_tplt = open('./xml_templates/tplt_sequence.xml','r',encoding='utf8').read()
track_tplt = open('./xml_templates/tplt_track.xml','r',encoding='utf8').read()
audio_track_tplt = open('./xml_templates/tplt_audiotrack.xml','r',encoding='utf8').read()
clip_tplt = open('./xml_templates/tplt_clip.xml','r',encoding='utf8').read()
audio_clip_tplt = open('./xml_templates/tplt_audio_clip.xml','r',encoding='utf8').read()

# 载入timeline 和 breakpoint

timeline = pd.read_pickle(stdin_log)
break_point = pd.read_pickle(stdin_log.replace('timeline','breakpoint'))
bulitin_media = pd.read_pickle(stdin_log.replace('timeline','bulitinmedia'))

def main():
    global media_list
    print('[export XML]: Welcome to use exportXML for TRPG-replay-generator '+edtion)
    print('[export XML]: The output xml file and refered png files will be saved at "'+output_path+'"')

    # 载入od文件
    try:
        object_define_text = open(media_obj,'r',encoding='utf-8').read()#.split('\n')
    except UnicodeDecodeError as E:
        print('[DecodeError]:',E)
        sys.exit(1)
    if object_define_text[0] == '\ufeff': # 139 debug
        print('[warning]:','UTF8 BOM recognized in MediaDef, it will be drop from the begin of file!')
        object_define_text = object_define_text[1:]
    object_define_text = object_define_text.split('\n')

    media_list=[]
    for i,text in enumerate(object_define_text):
        if text == '':
            continue
        elif text[0] == '#':
            continue
        else:
            try:
                exec(text) #对象实例化
                obj_name = text.split('=')[0]
                obj_name = obj_name.replace(' ','')
                if obj_name in occupied_variable_name:
                    raise SyntaxError('Obj name occupied')
                elif (len(re.findall('\w+',obj_name))==0)|(obj_name[0].isdigit()):
                    raise SyntaxError('Invalid Obj name')
                media_list.append(obj_name) #记录新增对象名称
            except Exception as E:
                print('[SyntaxError]: "'+text+'" appeared in media define file line ' + str(i+1)+' is invalid syntax:',E)
                sys.exit(1)
    black = Background('black')
    white = Background('white')
    media_list.append('black')
    media_list.append('white')
    # alpha 1.6.5 载入导出的内建媒体
    for key,values in bulitin_media.iteritems():
        exec(values)
        media_list.append(key)

    # 开始生成

    print('[export XML]: Begin to export.')
    video_tracks = []
    audio_tracks = []
    for layer in zorder + ['SE','Voice']:
        if layer == 'Bb':
            track_items = parse_timeline_bubble(layer)
            bubble_clip_list = []
            text_clip_list = []
            for item in track_items:
                bubble_this,text_this = eval('{0}.display(begin ={1},end={2},text="{3}",header="{4}")'
                                             .format(item[0],item[3],item[4],item[1],item[2]))
                bubble_clip_list.append(bubble_this)
                text_clip_list.append(text_this)
            video_tracks.append(track_tplt.format(**{'targeted':'False','clips':'\n'.join(bubble_clip_list)}))
            video_tracks.append(track_tplt.format(**{'targeted':'True','clips':'\n'.join(text_clip_list)}))

        elif layer in ['SE','Voice']:
            track_items = parse_timeline(layer)
            clip_list = []
            for item in track_items:
                if item[0] in media_list:
                    clip_list.append(eval('{0}.display(begin={1})'.format(item[0],item[1])))
                elif os.path.isfile(item[0][1:-1]) == True: # 注意这个位置的item[0]首尾应该有个引号
                    temp = Audio(item[0][1:-1])
                    clip_list.append(temp.display(begin=item[1]))
                else:
                    print("[warning]:",'Audio file',item[0],'is not exist.')
            audio_tracks.append(audio_track_tplt.format(**{'type':Audio_type,'clips':'\n'.join(clip_list)}))

        else:
            track_items = parse_timeline(layer)
            clip_list = []
            for item in track_items:
                clip_list.append(eval('{0}.display(begin={1},end={2})'.format(item[0],item[1],item[2])))
            video_tracks.append(track_tplt.format(**{'targeted':'False','clips':'\n'.join(clip_list)}))

    main_output = project_tplt.format(**{'timebase':'%d'%frame_rate,
                           'ntsc':Is_NTSC,
                           'sequence_name':stdin_name,
                           'screen_width':'%d'%screen_size[0],
                           'screen_height':'%d'%screen_size[1],
                           'tracks_vedio':'\n'.join(video_tracks),
                           'tracks_audio':'\n'.join(audio_tracks)})

    ofile = open(output_path+'/'+stdin_name+'.xml','w',encoding='utf-8')
    ofile.write(main_output)
    ofile.close()
    print('[export XML]: Done! XML path : '+output_path+'/'+stdin_name+'.xml')
if __name__ == '__main__':
    main()


text:
import sys
sys.path.append("python/need_not_convert")
from SurfStatCoord2Ind import *
import surfstat_wrap as sw
import numpy as np
import pytest

sw.matlab_init_surfstat()

def dummy_test(coord, surf):

    try:
        # wrap matlab functions
        Wrapped_ind = sw.matlab_SurfStatCoord2Ind(coord, surf)

    except:
        pytest.skip("Original MATLAB code does not work with these inputs.")

    Python_ind = py_SurfStatCoord2Ind(coord, surf)

    # compare matlab-python outputs
    testout = np.allclose(Wrapped_ind, Python_ind, rtol=1e-05, equal_nan=True)
    assert testout

#### Test 1
def test_surf_coord():
    # coord is 2D array, surf['coord'] is 2D array
    m = np.random.randint(1,100)
    n = np.random.randint(1,100)

    coord = np.random.rand(m,3)
    surf = {}
    surf['coord'] = np.random.rand(3,n)
    dummy_test(coord, surf)

### Test 2
def test_surf_lat_easy():
    # coord is 2D array, surf['lat'] is 3D array of ones
    m = np.random.randint(1,100)

    coord = np.random.rand(m,3)
    surf = {}
    surf['lat'] = np.ones((10,10,10))
    dummy_test(coord, surf)

### Test 3
def test_surf_lat_middle():
    # coord is 2D array, surf['lat'] is 3D array of ones and zeros
    m = np.random.randint(1,100)
    coord = np.random.rand(m,3)
    surf = {}
    surf['lat'] = np.random.choice([0, 1], size=(3,3,3))
    dummy_test(coord, surf)

### Test 4
def test_surf_lat_hard():
    # coord is 2D array, surf['lat'] is 3D array of ones and zeros
    n = np.random.randint(1,100)
    k = np.random.randint(1,100)
    l = np.random.randint(1,100)

    coord = np.array([[0.25684115, 0.45125523, 0.23784246],
       [0.99785513, 0.34429589, 0.97229819],
       [0.40949779, 0.56305147, 0.98313413],
       [0.41883601, 0.20865994, 0.08787483],
       [0.32232554, 0.21137128, 0.88918578]])
    surf = {}
    surf['lat'] = np.random.choice([0, 1], size=(n,k,l))
    dummy_test(coord, surf)

### Test 5
def test_surf_lat_complex():
    # coord is 2D array, surf['lat'] is 3D array of ones and zeros
    m = np.random.randint(1,100)
    n = np.random.randint(1,100)
    k = np.random.randint(1,100)
    l = np.random.randint(1,100)

    coord = np.random.rand(m,3)
    surf = {}
    surf['lat'] = np.random.choice([0, 1], size=(n,k,l))
    dummy_test(coord, surf)

### Test 6
def test_surf_lat_vox_easy():
    # coord is 2D array, surf['lat'] is 3D array of ones, surf['vox'] is 2D
    m = np.random.randint(1,100)
    n = np.random.randint(1,100)
    k = np.random.randint(1,100)
    l = np.random.randint(1,100)

    coord = np.random.rand(m,3)
    surf = {}
    surf['lat'] = np.ones((10,10,10))
    surf['vox'] = np.array([[n,k,l]])
    dummy_test(coord, surf)

### Test 7
def test_surf_lat_vox_middle():
    # coord is 2D array, surf['lat'] is 3D.. of ones & zeros, surf['vox'] is 2D
    m = np.random.randint(1,100)
    n = np.random.randint(1,100)
    k = np.random.randint(1,100)
    l = np.random.randint(1,100)

    coord = np.random.rand(m,3)
    surf = {}
    surf['lat'] = np.random.choice([0, 1], size=(3,3,3))
    surf['vox'] = np.array([[n,k,l]])
    dummy_test(coord, surf)

### Test 8
def test_surf_lat_vox_complex():
    # coord is 2D array, surf['lat'] is 3D.. of ones & zeros, surf['vox'] is 2D
    m = np.random.randint(1,100)
    n = np.random.randint(1,100)
    k = np.random.randint(1,100)
    l = np.random.randint(1,100)

    coord = np.random.rand(m,3)
    surf = {}
    surf['lat'] = np.random.choice([0, 1], size=(n,k,l))
    surf['vox'] = np.array([[l,k,n]])
    dummy_test(coord, surf)

### Test 9
def test_surf_lat_vox_origin_easy():
    # coord is 2D array, surf['lat'] 3D, surf['vox'] 2D, surf[orig] 2D
    m = np.random.randint(1,100)
    n = np.random.randint(1,100)
    k = np.random.randint(1,100)
    l = np.random.randint(1,100)

    coord = np.random.rand(m,3)
    surf = {}
    surf['lat'] = np.ones((10,10,10))
    surf['vox'] = np.array([[n,k,l]])
    surf['origin'] = np.array([[k,l,n]])
    dummy_test(coord, surf)

### Test 10
def test_surf_lat_vox_origin_middle():
    # coord is 2D array, surf['lat'] 3D, surf['vox'] 2D, surf[orig] 2D
    m = np.random.randint(1,100)
    n = np.random.randint(1,100)
    k = np.random.randint(1,100)
    l = np.random.randint(1,100)

    coord = np.random.rand(m,3)
    surf = {}
    surf['lat'] = np.random.choice([0, 1], size=(3,3,3))
    surf['vox'] = np.array([[n,k,l]])
    surf['origin'] = np.array([[k,l,n]])
    dummy_test(coord, surf)

### Test 11
def test_surf_lat_vox_origin_complex():
    # coord is 2D array, surf['lat'] 3D, surf['vox'] 2D, surf[orig] 2D
    m = np.random.randint(1,100)
    n = np.random.randint(1,100)
    k = np.random.randint(1,100)
    l = np.random.randint(1,100)

    coord = np.random.rand(m,3)
    surf = {}
    surf['lat'] = np.random.choice([0, 1], size=(n,k,l))
    surf['vox'] = np.array([[l,k,n]])
    surf['origin'] = np.array([[k+5,l+5,n+5]])
    dummy_test(coord, surf)

ext: py

max_stars_repo_name: rudimeier/BrainStat

max_issues_repo_name: rudimeier/BrainStat

max_forks_repo_name: rudimeier/BrainStat

reason: import numpy


text:
import copy
import time
import configparser
import numpy as np
from typing import NamedTuple
from decimal import Decimal
from datetime import datetime
from types import MappingProxyType

def get_config(type_map, market: str, configfile="config.ini"):
    """Creates a immutable proxy for a configuration a dict from config file
    """
    parser = configparser.ConfigParser()
    parser.read(configfile)

    config_mut = parser.defaults()
    config_mut.update(parser[market])
    config_mut.update(market = market)

    for setting in type_map:
        try:
            config_mut[setting] = type_map[setting](config_mut[setting])
        except KeyError:
            raise Exception("Missing {} setting in the config file".format(setting))
        except ValueError:
            raise Exception("The {} setting should be of type {}".format(setting, type_map[setting]))

    return MappingProxyType(config_mut)

def retry(func, max_tries = 16, timeout = 0.1):
    """Helper to retry functions and preserve exceptions, handy for network calls."""
    acm = 0
    while True:
        try:
            return func()
        except Exception as exception:
            if acm > max_tries:
                raise exception
            time.sleep(timeout * (2 ** acm))
            acm += 1

def parse_positive_decimal(value, label):
    val = Decimal(str(value))
    if val < 0:
        raise Exception("{} must be positive.".format(label))
    return val

def parse_str_option(string, options):
    option = string.strip().upper()
    if option not in options:
        raise Exception("Option must be one of {}".format(option, options))
    return option

class Order:
    """Immutable wrapper for Orders from the API that can save you from mistakes."""

    def __parse_buy_or_sell__(buy_or_sell):
        return parse_str_option(buy_or_sell, ('BUY', 'SELL'))

    def __parse_id__(id):
        return str(int(id))

    def __parse_type__(type):
        return parse_str_option(type, ('LIMIT', 'MARKET', 'STOP_LIMIT', 'STOP_MARKET'))

    def __parse_status__(status):
        return parse_str_option(status, ('CANCELLED', 'FILLED', 'OPEN', 'PENDING'))

    def __parse_policy__(cancellation_policy):
        policies = ('FILL_OR_KILL', 'GOOD_TIL_CANCELLED', 'GOOD_TIL_TIME', 'IMMEDIATE_OR_CANCEL')
        return parse_str_option(cancellation_policy, policies)

    parse_map = {'price': lambda val: parse_positive_decimal(val, 'price'),
                 'amount': lambda val: parse_positive_decimal(val, 'amount'),
                 'buy_or_sell': __parse_buy_or_sell__,
                 'amount_remaining': lambda val: parse_positive_decimal(val, 'amount_remaining'),
                 'id': __parse_id__,
                 'type': __parse_type__,
                 'cancellation_policy': __parse_policy__,
                 'allow_taker': bool,
                 'placed_at': lambda val: val,
                 'status': __parse_status__}

    def __init__(self,
                 price: Decimal,
                 amount: Decimal,
                 buy_or_sell: str,
                 amount_remaining: Decimal = -1,
                 id: str = '-1',
                 type: str = 'LIMIT',
                 cancellation_policy: str = 'GOOD_TIL_CANCELLED',
                 allow_taker: bool = True,
                 placed_at: datetime = None,
                 status: str = 'PENDING'):

        amount_remaining = Decimal(amount_remaining if amount_remaining != -1 else amount)

        object.__setattr__(self, 'price', self.parse_map['price'](price))
        object.__setattr__(self, 'amount', self.parse_map['price'](amount))
        object.__setattr__(self, 'buy_or_sell', self.parse_map['buy_or_sell'](buy_or_sell))
        object.__setattr__(self, 'amount_remaining', self.parse_map['amount_remaining'](amount_remaining))
        object.__setattr__(self, 'id', self.parse_map['id'](id))
        object.__setattr__(self, 'type', self.parse_map['type'](type))
        object.__setattr__(self, 'cancellation_policy', self.parse_map['cancellation_policy'](cancellation_policy))
        object.__setattr__(self, 'allow_taker', self.parse_map['allow_taker'](allow_taker))
        object.__setattr__(self, 'placed_at', self.parse_map['placed_at'](placed_at))
        object.__setattr__(self, 'status', self.parse_map['status'](status))

    def __setattr__(self, *args):
        raise TypeError

    def __delattr__(self, *args):
        raise TypeError

    def replace(self, **kwargs):
        new_order = copy.deepcopy(self)
        for key in kwargs:
            object.__setattr__(new_order, key, self.parse_map[key](kwargs[key]))
        return new_order

    def constrain_price(self, market):
        """ Constrain order price to market settings
        """
        prc = self.price.quantize(Decimal(market.min_trade_increment_b), rounding='ROUND_DOWN')
        return self.replace(price = prc)

    def constrain_amount(self, market, max_amount: Decimal):
        """ Constrain order amount to user and market settings
        """
        amt = min(self.amount, max_amount).quantize(Decimal(market.min_trade_increment), rounding='ROUND_DOWN')
        return self.replace(amount = amt)

    def constrain(self, market, max_amount: Decimal):
        """ Takes an order and constrain it to market and user settings
            order: is the order to be formated
            mkt: is the market object from API call
            max_amount: is the constraint on amount
        """
        return self.constrain_price(market).constrain_amount(market, max_amount)

    def from_api(order):
        """Format the API object to a more usefull format with Decimal."""
        return Order(price = Decimal(order.limit_price.amount),
                     amount = Decimal(order.amount.amount),
                     buy_or_sell = order.buy_or_sell,
                     amount_remaining = Decimal(order.amount_remaining.amount),
                     id = order.id,
                     type = order.type,
                     cancellation_policy = order.cancellation_policy,
                     placed_at = order.placed_at,
                     status = order.status)

class OrderBookSeries(NamedTuple):
    """A fixed size data ordered 3d numpy array and a 1d time array
    return (time[max_obs_size], data[max_obs_size, 25, 4])
    data = (ask prices, askamount, bids, bids_amount)
    """
    t: np.ndarray = np.zeros((0), dtype="datetime64[ns]")
    data: np.ndarray = np.zeros((0, 25), dtype=[('Pask', '<f16'), ('Qask', '<f16'), ('Pbid', '<f16'), ('Qbid', '<f16')])

    def update(self, orderbook, max_obs_size):
        """Return updated OrderBookSeries.
        It adds most recent state and remove oldest from OrderBookSeries if
        size conditions are exceeded keeping memory constraints in check
        """
        # Reverse bids because we want to both tops be the logical next in a deck
        depth = 25
        data = np.full(depth, np.nan, dtype=[('Pask', '<f16'), ('Qask', '<f16'), ('Pbid', '<f16'), ('Qbid', '<f16')])

        if len(orderbook.asks) + len(orderbook.bids):
            for idx, ask in enumerate(orderbook.asks):
                if idx == depth: break
                data['Pask'][idx] = np.longfloat(ask.price.amount)
                data['Qask'][idx] = np.longfloat(ask.amount.amount)

            for idx, bid in enumerate(orderbook.bids[::-1]):
                if idx == depth: break
                data['Pbid'][idx] = np.longfloat(bid.price.amount)
                data['Qbid'][idx] = np.longfloat(bid.amount.amount)

        # Build orderbook series data
        new_obs = OrderBookSeries(np.append(self.t, np.datetime64(time.time_ns(), "ns")),
                                  np.append(self.data, data).reshape(self.data.shape[0] + 1, depth))
        # Drop old data if at size limit
        if new_obs.t.shape[0] <= max_obs_size:
            return new_obs
        return OrderBookSeries(new_obs.t[1:], new_obs.data[1:])

    def bootstrap(api, config):
        """Initiate a OrderBookSeries with data from market."""
        # Populate the initial entries so that temporal derivatives are
        # meaningful and we can compute temporal statistics
        obs = OrderBookSeries()
        update_id = -1
        before = time.time()
        while (len(obs.t) < config["min_history_points"]) and (time.time() - before < config["max_loading_time"]):
            # Guard against network issues
            try:
                orderbook = api.get_order_book(config["market"])
            except:
                continue
            if orderbook.update_id != update_id:
                obs = obs.update(orderbook, config['max_obs_size'])
                update_id = orderbook.update_id
            # Avoid rate limit, give 100ms delay
            time.sleep(0.100)
        return obs

max_stars_repo_name: nash-io/nash-python-makerbot

max_issues_repo_name: nash-io/nash-python-makerbot

max_forks_repo_name: nash-io/nash-python-makerbot


text:
import pickle
import unittest
import unittest.mock
import warnings

import datasets as HFdata
import numpy as np
import pytest
import torch.utils.data as torchdata
from torchvision.transforms import Lambda

from baal.active import ActiveLearningDataset

class MyDataset(torchdata.Dataset):
    def __init__(self, transform=None):
        self.transform = transform
        pass

    def __len__(self):
        return 100

    def __getitem__(self, item):
        feature = item
        if self.transform:
            feature = self.transform(item)
        return (feature, item)

class ActiveDatasetTest(unittest.TestCase):
    def setUp(self):
        self.dataset = ActiveLearningDataset(MyDataset(), make_unlabelled=lambda x: (x[0], -1))

    def test_len(self):
        assert len(self.dataset) == 0
        assert self.dataset.n_unlabelled == 100
        assert len(self.dataset.pool) == 100
        self.dataset.label(0)
        assert len(self.dataset) == self.dataset.n_labelled == 1
        assert self.dataset.n_unlabelled == 99
        assert len(self.dataset.pool) == 99
        self.dataset.label(list(range(99)))
        assert len(self.dataset) == 100
        assert self.dataset.n_unlabelled == 0
        assert len(self.dataset.pool) == 0

        dummy_dataset = ActiveLearningDataset(MyDataset(), labelled=self.dataset.labelled,
                                              make_unlabelled=lambda x: (x[0], -1))
        assert len(dummy_dataset) == len(self.dataset)
        assert len(dummy_dataset.pool) == len(self.dataset.pool)

        dummy_lbl = self.dataset.labelled.astype(np.float32)
        dummy_dataset = ActiveLearningDataset(MyDataset(), labelled=dummy_lbl, make_unlabelled=lambda x: (x[0], -1))
        assert len(dummy_dataset) == len(self.dataset)
        assert len(dummy_dataset.pool) == len(self.dataset.pool)

    def test_pool(self):
        self.dataset._dataset.label = unittest.mock.MagicMock()
        labels_initial = self.dataset.n_labelled

        # Test that we can label without value for research
        self.dataset.can_label = False
        self.dataset.label(0, value=np.arange(1, 10))
        self.dataset._dataset.label.assert_not_called()
        labels_next_1 = self.dataset.n_labelled
        assert labels_next_1 == labels_initial + 1

        # Test that a warning is raised if we dont supply a label.
        self.dataset.can_label = True
        with warnings.catch_warnings(record=True) as w:
            self.dataset.label(np.arange(0, 9))
            assert len(w) == 1 and "The dataset is able to label data, but no label was provided." in str(w[0].message)
        self.dataset._dataset.label.assert_not_called()
        assert self.dataset.n_labelled == labels_next_1

        with pytest.raises(ValueError, match="same length"):
            self.dataset.label(np.arange(0, 9), value=np.arange(0, 10))

        # cleanup
        del self.dataset._dataset.label
        self.dataset.can_label = False

        pool = self.dataset.pool
        assert np.equal([i for i in pool], [(i, -1) for i in np.arange(labels_next_1, 100)]).all()
        assert np.equal([i for i in self.dataset], [(i, i) for i in np.arange(labels_next_1)]).all()

    def test_get_raw(self):
        # check that get_raw returns the same thing regardless of labelling
        # status
        i_1 = self.dataset.get_raw(5)
        self.dataset.label(5)
        i_2 = self.dataset.get_raw(5)
        assert i_1 == i_2

    def test_types(self):
        self.dataset.label_randomly(2)
        assert self.dataset._pool_to_oracle_index(1) == self.dataset._pool_to_oracle_index([1])
        assert self.dataset._oracle_to_pool_index(1) == self.dataset._oracle_to_pool_index([1])

    def test_state_dict(self):
        state_dict_1 = self.dataset.state_dict()
        assert np.equal(state_dict_1["labelled"], np.full((100,), False)).all()

        self.dataset.label(0)
        assert np.equal(
            state_dict_1["labelled"],
            np.concatenate((np.array([True]), np.full((99,), False)))
        ).all()

    def test_load_state_dict(self):
        dataset_1 = ActiveLearningDataset(MyDataset(), random_state=50)
        dataset_1.label_randomly(10)
        state_dict1 = dataset_1.state_dict()

        dataset_2 = ActiveLearningDataset(MyDataset(), random_state=None)
        assert dataset_2.n_labelled == 0

        dataset_2.load_state_dict(state_dict1)
        assert dataset_2.n_labelled == 10

        # test if the second lable_randomly call have same behaviour
        dataset_1.label_randomly(5)
        dataset_2.label_randomly(5)

        assert np.allclose(dataset_1.labelled, dataset_2.labelled)

    def test_transform(self):
        train_transform = Lambda(lambda k: 1)
        test_transform = Lambda(lambda k: 0)
        dataset = ActiveLearningDataset(MyDataset(train_transform), make_unlabelled=lambda x: (x[0], -1),
                                        pool_specifics={'transform': test_transform})
        dataset.label(np.arange(10))
        pool = dataset.pool
        assert np.equal([i for i in pool], [(0, -1) for i in np.arange(10, 100)]).all()
        assert np.equal([i for i in dataset], [(1, i) for i in np.arange(10)]).all()

        with pytest.raises(ValueError) as e:
            ActiveLearningDataset(MyDataset(train_transform), pool_specifics={'whatever': 123}).pool

    def test_random(self):
        self.dataset.label_randomly(50)
        assert len(self.dataset) == 50
        assert len(self.dataset.pool) == 50

    def test_random_state(self):
        seed = None
        dataset_1 = ActiveLearningDataset(MyDataset(), random_state=seed)
        dataset_1.label_randomly(10)
        dataset_2 = ActiveLearningDataset(MyDataset(), random_state=seed)
        dataset_2.label_randomly(10)
        assert not np.allclose(dataset_1.labelled, dataset_2.labelled)

        seed = 50
        dataset_1 = ActiveLearningDataset(MyDataset(), random_state=seed)
        dataset_1.label_randomly(10)
        dataset_2 = ActiveLearningDataset(MyDataset(), random_state=seed)
        dataset_2.label_randomly(10)
        assert np.allclose(dataset_1.labelled, dataset_2.labelled)

        seed = np.random.RandomState(50)
        dataset_1 = ActiveLearningDataset(MyDataset(), random_state=seed)
        dataset_1.label_randomly(10)
        dataset_2 = ActiveLearningDataset(MyDataset(), random_state=seed)
        dataset_2.label_randomly(10)
        assert not np.allclose(dataset_1.labelled, dataset_2.labelled)

    def test_label_randomly_full(self):
        dataset_1 = ActiveLearningDataset(MyDataset())
        dataset_1.label_randomly(99)
        assert dataset_1.n_unlabelled == 1
        assert len(dataset_1.pool) == 1
        dataset_1.label_randomly(1)
        assert dataset_1.n_unlabelled == 0
        assert dataset_1.n_labelled == 100

def test_arrowds():
    dataset = HFdata.load_dataset('glue', 'sst2')['test']
    dataset = ActiveLearningDataset(dataset)
    dataset.label(np.arange(10))
    assert len(dataset) == 10
    assert len(dataset.pool) == 1811
    data = dataset.pool[0]
    assert [k in ['idx', 'label', 'sentence'] for k, v in data.items()]

def test_pickable():
    dataset_1 = ActiveLearningDataset(MyDataset())
    l = len(dataset_1.pool)
    dataset_2 = pickle.loads(pickle.dumps(dataset_1))
    l2 = len(dataset_2.pool)
    assert l == l2

def test_warning_raised_on_label():
    class DS(torchdata.Dataset):
        def __init__(self):
            self.x = [1, 2, 3]
            self.label = [1, 1, 1]

        def __len__(self):
            return len(self.x)

        def __getitem__(self, item):
            return self.x[item], self.y[item]

    with warnings.catch_warnings(record=True) as w:
        al = ActiveLearningDataset(DS())
        assert not al.can_label
        assert len(w) == 1
        assert "label" in str(w[-1].message)

def test_warning_raised_on_deprecation():
    with warnings.catch_warnings(record=True) as w:
        al = ActiveLearningDataset(MyDataset())
        _ = al._labelled
        assert len(w) == 1
        assert w[0].category is DeprecationWarning

def test_labelled_map():
    ds = ActiveLearningDataset(MyDataset())
    assert ds.current_al_step == 0
    ds.label_randomly(10)
    assert ds.current_al_step == 1
    ds.label_randomly(10)
    assert ds.labelled_map.max() == 2 and np.equal(ds.labelled, ds.labelled_map > 0).all()

    st = ds.state_dict()
    ds2 = ActiveLearningDataset(MyDataset(), labelled=st["labelled"])
    assert ds2.current_al_step == ds.current_al_step

def test_last_active_step():
    ds = ActiveLearningDataset(MyDataset(), last_active_steps=1)
    assert len(ds) == 0
    ds.label_randomly(10)
    assert len(ds) == 10
    ds.label_randomly(10)
    # We only iterate over the items labelled at step 2.
    assert len(ds) == 10
    assert all(ds.labelled_map[x] == 2 for x, _ in ds)

if __name__ == '__main__':
    pytest.main()


text:
def jacobi(A):
    """
    Compute the Jacobi preconditioner function for the matrix A.

    Parameters
    -----------
    A: np.array
        Input matrix to compute the Jacobi preconditioner for.

    Returns
    ---------
    precondition_matvec: function
        Function which applies the Jacobi preconditioner to a vector

    Example
    -------
    >>> from pymatting import *
    >>> import numpy as np
    >>> A = np.array([[2, 3], [3, 5]])
    >>> preconditioner = jacobi(A)
    >>> preconditioner(np.array([1, 2]))
    array([0.5, 0.4])
    """
    diagonal = A.diagonal()

    inverse_diagonal = 1.0 / diagonal

    def precondition_matvec(x):
        return x * inverse_diagonal

    return precondition_matvec


text:
import numpy as np

class molecule:
    '''
    Needs molecule string and temperature in [K]
    '''
    def __init__(self, mol_str, Temp):
        self.mol_str = mol_str
        self.Temp = Temp

    def params(self):
        '''
        output units
        B,D in Joules
        d_alpha in CGS
        T in rotational constants

        Output order: B, D, d_alpha, Jmax, T
        '''
        ##rotational constant in ground state in wavenumbers
        B_dict = {'O2': 1.4297, 'N2': 1.99, 'N2O': 0.4190,
                    'CO2': 0.3902, 'D2': 30.442, 'OCS': .2039}
        ##Centrifugal Distortion in ground state in wavenumbers
        D_dict = {'O2': 4.839e-6, 'N2': 5.7e-6, 'N2O': 0.176e-6,
                    'CO2': 0.135e-6, 'D2': 0, 'OCS': 0}
        ##anisotropy of the polarizability in m^3 - but cgs units
        d_alpha_dict = {'O2': 1.1e-30, 'N2': 0.93e-30, 'N2O': 2.8e-30,
                        'CO2': 2.109e-30, 'D2': .282e-30, 'OCS': 4.1e-30}
        ## Jmax for various molecules and laser < 10**13 W cm**-2
        Jmax_dict = {'O2': 20, 'N2': 20, 'N2O': 60,
                    'CO2': 60, 'D2': 35, 'OCS': 70}
        return B_dict[self.mol_str]*1.98648e-23, D_dict[self.mol_str]*1.98648e-23,\
         d_alpha_dict[self.mol_str], Jmax_dict[self.mol_str],\
         1.381e-23*self.Temp/(B_dict[self.mol_str]*1.98648e-23)

    def Boltzmann(self):
        weven_dict = {'O2': 0, 'N2': 2, 'N2O': 1, 'CO2': 1, 'D2': 2, 'OCS': 1}
        wodd_dict =  {'O2': 1, 'N2': 1, 'N2O': 1, 'CO2': 0, 'D2': 1, 'OCS': 1}

        weven = weven_dict[self.mol_str]
        wodd = wodd_dict[self.mol_str]
        pop = np.zeros(int(self.params()[3]))
        T = self.params()[4]
        for J in range(int(self.params()[3])):
            if np.mod(J,2) == 0:
                pop[J] = weven*(2*J+1)*np.exp(-J*(J+1)/T)
            else:
                pop[J] = wodd*(2*J+1)*np.exp(-J*(J+1)/T)

        return pop/np.sum(pop)


text:
from __future__ import division
import numpy as np
import scipy.optimize as opt
import math

#Black - parallel (extraordinary)
#Red   - through plane (ordinary)

#for red line (through plane) works for [0.6...4.2 eV]
def lorentz_E_eps(w1, w0=1.0585527, wp=2.63263661, eps_inf=2.55029383, gamma=0.13258352):
    #Calculates epsilon, given energy[nm]. Other parameters w0,wp,etc can also be changed
    #a = drude_O_eps(500, w0=1.0585527, eps_inf=1.0)
    #print a

    #convert [nm] to [eV] #WIKI correct!!
    w = 1.2398/w1*1E3
    eps_r = (math.pow(wp,2)*(math.pow(w0,2) - math.pow(w,2))) / \
        ((math.pow(w0,2) - math.pow(w,2))**2 + math.pow(w*gamma, 2))+eps_inf
    eps_i = math.pow(wp,2)*gamma*w/((math.pow(w0,2) - math.pow(w,2))**2 + \
        math.pow(w*gamma, 2))
    return [eps_r, eps_i]

#for black line (in plane) works for [0.6...4.2 eV]
#def lorentz_O_eps(w1, w0=1.7211499, wp=8.46042266, eps_inf=0.18658023, gamma=0.98665155):
#    #Calculates epsilon, given energy[nm]. Other parameters w0,wp,etc can also be changed
#    #a = drude_E_eps(500, eps_inf=1.0, gamma = 0.05)
#    #print a
#
#    #convert [nm] to [eV] #WIKI correct!!
#    w = 1.2398/w1*1E3
#    eps_r = (math.pow(wp,2)*(math.pow(w0,2) - math.pow(w,2))) / \
#        ((math.pow(w0,2) - math.pow(w,2))**2 + math.pow(w*gamma, 2))+eps_inf
#    eps_i = math.pow(wp,2)*gamma*w/((math.pow(w0,2) - math.pow(w,2))**2 + \
#        math.pow(w*gamma, 2))
#    return [eps_r, eps_i]

# ========Optimization part:===========
#Black - in plane (ordinary)
#Red   - through plane (extraordinary)

# Array grabbed from plot for red line Bi2Se3 (real and imaginary)
O_w_ri = np.array([
    [0.62624466, 0.6247448],
    [0.7771068, 0.77266574],
    [0.86592436, 0.86809313],
    [0.932381, 0.93081313],
    [0.9519844, 0.9573789],
    [0.9767079, 0.977389],
    [0.9892141, 0.9875487],
    [1.0069029, 1.0043309],
    [1.0277587, 1.021432],
    [1.0539126, 1.0585527],
    [1.0722132, 1.0748636],
    [1.0908935, 1.0908935],
    [1.1022494, 1.1035907],
    [1.1144665, 1.116231],
    [1.1342001, 1.1353745],
    [1.1514072, 1.1546656],
    [1.1809858, 1.1806554],
    [1.2080476, 1.206707],
    [1.2475146, 1.2426565],
    [1.3018408, 1.3049433],
    [1.3982708, 1.3968095],
    [1.5245011, 1.5249014],
    [1.6458377, 1.646428],
    [1.7746327, 1.771249],
    [2.1314154, 2.1358812],
    [2.3246994, 2.3264074],
    [2.5105395, 2.5103636],
    [2.706297, 2.7041655],
    [2.8946295, 2.891416],
    [3.023487, 3.026097],
    [3.191995, 3.1969132],
    [3.3629854, 3.3644445],
    [3.5141416, 3.5122654],
    [3.6578727, 3.6502314],
    [3.7966428, 3.7947674],
    [3.939313, 3.939313],
    [4.123746, 4.126554],
    ])

# Array grabbed from plot for black line Bi2Se3 (real and imaginary)
E_w_ri = np.array([
    [0.6037759, 0.60985625],
    [0.7021417, 0.7010267],
    [0.78033704, 0.7823409],
    [0.86105645, 0.861191],
    [0.93672764, 0.93511295],
    [1.0123988, 1.0114989],
    [1.0805163, 1.0829569],
    [1.1713098, 1.1765914],
    [1.2519844, 1.2505133],
    [1.3376362, 1.3342916],
    [1.3904895, 1.3958932],
    [1.4332349, 1.4328542],
    [1.4709061, 1.4698153],
    [1.5110716, 1.5141684],
    [1.5461738, 1.5437372],
    [1.5862794, 1.5856262],
    [1.6388748, 1.6324435],
    [1.681456, 1.684189],
    [1.7265613, 1.7211499],
    [1.769191, 1.7605749],
    [1.8193818, 1.8197125],
    [1.8796655, 1.8788501],
    [1.9400238, 1.9453799],
    [2.0180733, 2.0119097],
    [2.1087883, 2.1087883],
    [2.1995332, 2.1967146],
    [2.2928433, 2.292813],
    [2.4063277, 2.4086242],
    [2.5122435, 2.517043],
    [2.6282556, 2.620534],
    [2.7417326, 2.7412732],
    [2.8602426, 2.8620124],
    [2.9913511, 2.9901438],
    [3.1149058, 3.110883],
    [3.2636492, 3.263655],
    [3.3846612, 3.3893223],
    [3.5233195, 3.524846],
    [3.659446, 3.6579056],
    [3.793056, 3.7958932],
    [3.916581, 3.9166324],
    [4.0350575, 4.034908],
    [4.1484933, 4.1482544],
    ])

# a loop through energy array[eV] to get epsE and epsO
#~ for i in range(len(E_w_ri)):
    #~ print drude_E_eps(E_w_ri[i][0])
#~ for i in range(len(O_w_ri)):
    #~ print drude_O_eps(O_w_ri[i][0])

O_old_eps_ri = np.array([
    [12.526751, 1.318346],
    [16.254206,2.6698534],
    [21.100832, 4.972083],
    [26.655085, 9.309189],
    [29.372524, 13.376189],
    [30.08233, 17.646776],
    [28.665588, 21.985453],
    [24.4735, 27.069695],
    [11.836687, 36.6963],
    [-0.315187454, 50.72908],
    [-16.684837, 49.101475],
    [-19.34486, 43.47388],
    [-20.34486, 37.16842],
    [-18.218311, 30.049398],
    [-17.095352, 21.981031],
    [-15.381959, 16.014355],
    [-13.431773, 11.877986],
    [-11.009263, 8.622971],
    [-8.763343, 5.9778285],
    [-6.3986254, 4.1454577],
    [-3.7366822, 2.5155919],
    [-1.8410742, 2.1727777],
    [-0.71340847, 1.8979565],
    [0.11932759, 1.7586299],
    [0.9626235, 1.6799271],
    [1.089686, 1.6064318],
    [1.3345139, 1.533133],
    [1.520746, 1.3239466],
    [1.5885242, 1.3861425],
    [1.6535476, 1.3143176],
    [1.7204076, 1.2414118],
    [1.7283274, 1.1686044],
    [1.8534387, 1.0963864],
    [1.860096, 1.0244632],
    [1.9255786, 0.9523435],
    [2.0588393, 1.0158167],
    [2.0588393, 0.9424197],
])

E_old_eps_ri = np.array([
    [20.430677, 17.809725],
    [21.57374, 16.490486],
    [22.596828, 16.186047],
    [23.680172, 16.186047],
    [24.643005, 16.693445],
    [25.605839, 17.657505],
    [26.68963, 19.128963],
    [27.651926, 21.767443],
    [28.011131, 24.81184],
    [27.344297, 29.073996],
    [26.13552, 33.08245],
    [24.50469, 35.41649],
    [22.330935, 37.80127],
    [19.734676, 40.490486],
    [16.776527, 42.012684],
    [13.21475, 43.890064],
    [7.842182, 44.9556],
    [3.5561783, 45.412262],
    [-0.66957, 44.60042],
    [-4.171091, 43.078224],
    [-7.67288, 40.33827],
    [-10.993993, 36.53277],
    [-13.10821, 32.372093],
    [-14.438569, 28.515856],
    [-14.743514, 23.7812433],
    [-14.5657, 19.484144],
    [-13.663837, 16.389006],
    [-12.581656, 13.547568],
    [-11.619897, 11.51797],
    [-10.417116, 9.894292],
    [-9.455625, 8.524313],
    [-8.615003, 7.408034],
    [-7.8351717, 6.5961943],
    [-6.934383, 5.835095],
    [-6.2758684, 5.1754756],
    [-5.7370596, 4.566596],
    [-5.1385317, 4.1606765],
    [-4.720949, 3.653277],
    [-4.2429323, 3.4503171],
    [-3.8249023, 3.1966174],
    [-3.527383, 3.0443974],
    [-3.2296848, 2.9429176],
])

w0O = 1.0585527   #ordinary, red line
w0E = 1.7211499   #extraordinary, black line
FFE = 0.0
FFO = 0.0
E_new_eps_ri = np.zeros((42,2), dtype=float)
O_new_eps_ri = np.zeros((37,2), dtype=float)

#initial values to test ff_initial or for optimization 1st guess
wp =  2.25
eps_inf = 2.72
gamma = 0.082

# IMPORTANT!!! OPTIMIZATION PROCEDURE IS IN EV!!!

for i in range(len(O_w_ri)):
    eps_r = (math.pow(wp,2)*(math.pow(w0O,2) - math.pow(O_w_ri[i][0],2))) / \
        ((math.pow(w0O,2) - math.pow(O_w_ri[i][0],2))**2 + math.pow(O_w_ri[i][0]*gamma, 2))+eps_inf
    eps_i = math.pow(wp,2)*gamma*O_w_ri[i][1]/((math.pow(w0O,2) - math.pow(O_w_ri[i][1],2))**2 + \
        math.pow(O_w_ri[i][1]*gamma, 2))
    O_new_eps_ri[i][0] = eps_r
    O_new_eps_ri[i][1] = eps_i

for i in range(len(E_w_ri)):
    eps_r = (math.pow(wp,2)*(math.pow(w0E,2) - math.pow(E_w_ri[i][0],2))) / \
        ((math.pow(w0E,2) - math.pow(E_w_ri[i][0],2))**2 + math.pow(E_w_ri[i][0]*gamma, 2))+eps_inf
    eps_i = math.pow(wp,2)*gamma*E_w_ri[i][1]/((math.pow(w0E,2) - math.pow(E_w_ri[i][1],2))**2 + \
        math.pow(E_w_ri[i][1]*gamma, 2))
    E_new_eps_ri[i][0] = eps_r
    E_new_eps_ri[i][1] = eps_i

for i in range(len(O_w_ri)):
    FFO += math.pow((O_old_eps_ri[i][0] - O_new_eps_ri[i][0]),2) + math.pow((O_old_eps_ri[i][1] - O_new_eps_ri[i][1]),2)

for i in range(len(E_w_ri)):
    FFE += math.pow((E_old_eps_ri[i][0] - E_new_eps_ri[i][0]),2) + math.pow((E_old_eps_ri[i][1] - E_new_eps_ri[i][1]),2)

#print "Fitness function value: (ordinary, init_guess: wp=2.25, eps_inf=2.72, gamma=0.082):", FFO #1570.7874974
#print "Fitness function value: (extra-ordinary, init_guess: wp = 2.25, eps_inf=2.72, gamma=0.082):", FFE #28652.518502

def objective(x):
    w0 = 1.0585527  #ordinary, red line
    #w0 = 1.7211499  #extraordinary, black line
    FF = 0.0

    wp = x[0]
    eps_inf = x[1]
    gamma = x[2]

    for i in range(len(O_w_ri)):
        eps_r = (math.pow(wp,2)*(math.pow(w0,2) - math.pow(O_w_ri[i][0],2))) / \
            ((math.pow(w0,2) - math.pow(O_w_ri[i][0],2))**2 + math.pow(O_w_ri[i][0]*gamma, 2))+eps_inf
        eps_i = math.pow(wp,2)*gamma*O_w_ri[i][1]/((math.pow(w0,2) - math.pow(O_w_ri[i][1],2))**2 + \
            math.pow(O_w_ri[i][1]*gamma, 2))
        O_new_eps_ri[i][0] = eps_r
        O_new_eps_ri[i][1] = eps_i

    for i in range(len(O_w_ri)):
        FF += math.pow((O_old_eps_ri[i][0] - O_new_eps_ri[i][0]),2) + math.pow((O_old_eps_ri[i][1] - O_new_eps_ri[i][1]),2)

    return FF

#Initial guess obtained manually through Excel
x0 = [2.25, 1.0, 0.082]
# Boundaries for wp, eps_inf and gamma
b1 = (0.0, 5)
b2 = (0.0, 0.3)
bnds = (b1,b1,b2)
#sol = opt.minimize(objective, x0, bounds=bnds)
#print sol
# Red (ordinary, through plane)
# initial,                                 fn = 1570, sol = [2.25, 2.72, 0.082]
# best, bounds(0:5, 0:5, 0:0.3)            fn = 468,  sol = [2.63263661, 2.55029383, 0.13258352]
# compromise?, bounds(0:5, 0:5, 0.03:0.09) fn = 1190, sol = [2.32592269, 2.68246867, 0.09]

# Black (extraordinary, in plane)
# initial,                                 fn = 28652, sol = [2.25, 2.72, 0.082]
# best, bounds(0:10, 0:10, 0:1.0)          fn = 613,  sol = [8.46042266,  0.18658023,  0.98665155]]

reason: import numpy,import scipy


text:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import codecademylib3

null_outcomes = []

for i in range(10000):
  simulated_monthly_visitors = np.random.choice(['y', 'n'], size=500, p=[0.1, 0.9])

  num_purchased = np.sum(simulated_monthly_visitors == 'y')

  null_outcomes.append(num_purchased)

#calculate the 90% interval here:
null_90CI = np.percentile(null_outcomes, [5,95])

print(null_90CI)

max_stars_repo_path: Data Scientist Career Path/8. Hypothesis Testing/1. Testing a Sample Statistics/2. Binomial Test/7. confidence.py

max_stars_repo_name: myarist/Codecademy


text:
# type: ignore[attr-defined]

from typing import Optional

import random
from enum import Enum
from pathlib import Path

import numpy as np
import pandas as pd
import typer
from clustering import __version__
from clustering.clustering import label_clusters

app = typer.Typer(
    name="clustering",
    help="CLI to apply Leiden clustering to an array of values",
    add_completion=False,
)

def version_callback(value: bool):
    """Prints the version of the package."""
    if value:
        console.print(
            f"[yellow]clustering[/] version: [bold blue]{__version__}[/]"
        )
        raise typer.Exit()

@app.command(
    name="",
    context_settings={"allow_extra_args": True, "ignore_unknown_options": True},
)
def main(
    input: str = typer.Argument(
        ...,
        help="Name of the input data matrix, items in rows and observations in columns.",
    ),
    output: str = typer.Argument(..., help="Name to use for output file."),
    resolution: float = typer.Option(
        1.0, help="Resolution at which to cluster."
    ),
    n_neighbors: int = typer.Option(
        30, help="Minimum number of neighbors required for a cluster"
    ),
    version: bool = typer.Option(
        None,
        "-v",
        "--version",
        callback=version_callback,
        is_eager=True,
        help="Prints the version of the clustering package.",
    ),
) -> None:
    input_df = Path(input)

    if not input_df.exists():
        raise FileNotFoundError
    else:
        df = pd.read_csv(input_df)

    output_array = label_clusters(
        data_df=df,
        resolution=resolution,
        n_neighbors=n_neighbors,
    )

    np.savetxt(
        fname=Path(output),
        X=output_array.astype(int),
        fmt="%d",
        delimiter=",",
    )

if __name__ == "main":
    typer.run(app)

max_stars_repo_name: milescsmith/clustering

max_issues_repo_name: milescsmith/clustering

max_forks_repo_name: milescsmith/clustering


text:
from Essential_module import *
import numpy
# height와 width 값 이상하면 순서 바꾸기

class Student:
    firstMatrix=[[0.0]*height]*width
    secondMatrix=[[0.0]*height]*width
    Firstimage = None
    def __init__(self,studentNumber):
        self.studentNumber = studentNumber
    def saveFirstMatrix(self,firstMatrix):
        for x in range(width):
            for y in range(height):
                self.firstMatrix[x][y] = firstMatrix[x][y]
    def saveSecondMatrix(self,secondMatrix):
        for x in range(width):
            for y in range(height):
                self.secondMatrix[x][y] = secondMatrix[x][y]

    def calculateFirstVolume(self):
        Volume=0
        for x in range(width):
            for y in range(height):
                Volume+=(Plate_matrix[x][y]-self.firstMatrix[x][y])*pixelSize
        return Volume

    def calculateSecondVolume(self):
        Volume=0
        for x in range(width):
            for y in range(height):
                Volume+=(self.secondMatrix[x][y]-self.firstMatrix[x][y])*pixelSize
        return Volume


text:
# Same system as in `emi_primal.py` Lagrange multiplier is instroduced
# for the Robin type coupling condition
import sympy as sp
from dolfin import *
from xii import *
import ulfy

def setup_problem(i, mms, parameters, degree):
    '''Solution of the Darcy-emmersed-Stokes test case'''
    mesh0, boundaries0 = mms['get_geometry'](i, 'outer')
    mesh1, boundaries1 = mms['get_geometry'](i, 'inner')
    interface, subdomainsI = mms['get_geometry'](i, 'interface')

    ds0 = Measure('ds', domain=mesh0, subdomain_data=boundaries0)
    n0 = FacetNormal(mesh0)

    ds1 = Measure('ds', domain=mesh1, subdomain_data=boundaries1)
    n1 = FacetNormal(mesh1)

    dx_ = Measure('dx', domain=interface, subdomain_data=subdomainsI)
    n1_ = OuterNormal(interface, [0.5, 0.5])
    n0_ = -n1_   # We have 0 as master

    V0 = FunctionSpace(mesh0, 'CG', degree)
    V1 = FunctionSpace(mesh1, 'CG', degree)
    Q = FunctionSpace(interface, 'CG', degree)
    W = [V0, V1, Q]

    u0, u1, p = map(TrialFunction, W)
    v0, v1, q = map(TestFunction, W)

    Tu0, Tu1 = (Trace(x, interface) for x in (u0, u1))
    Tv0, Tv1 = (Trace(x, interface) for x in (v0, v1))

    # Material parameters
    kappa0, kappa1 = (Constant(parameters[key]) for key in ('kappa0', 'kappa1'))

    a = block_form(W, 2)
    a[0][0] = kappa0*inner(grad(u0), grad(v0))*dx
    a[0][2] = inner(p, Tv0)*dx_
    a[1][1] = kappa1*inner(grad(u1), grad(v1))*dx
    a[1][2] = -inner(p, Tv1)*dx_
    a[2][0] = inner(q, Tu0)*dx_
    a[2][1] = -inner(q, Tu1)*dx_
    a[2][2] = -inner(p, q)*dx

    lm_tags = (1, 2, 3, 4)

    L = block_form(W, 1)
    L[0] = inner(mms['f0'], v0)*dx

    L[1] = (inner(mms['f1'], v1)*dx
            # Multiplier contribution from
            + sum(inner(mms['g_n'][tag], v1)*ds1(tag) for tag in lm_tags)
    )

    #  Multiplier contrib from Robin
    L[2] = sum(inner(mms['g_r'][tag], q)*dx_(tag) for tag in lm_tags)

    # Dirichlet on all outside
    V0_bcs = [DirichletBC(V0, mms['dirichlet_0'][tag], boundaries0, tag) for tag in (5, 6, 7, 8)]
    W_bcs = [V0_bcs, [], []]

    return a, L, W, W_bcs

# --------------------------------------------------------------------

if __name__ == '__main__':
    from common import ConvergenceLog, H1_norm, L2_norm, broken_norm
    from emi_primal import setup_mms
    import sys, argparse

    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    # Decide material parameters ...
    parser.add_argument('--param_kappa0', type=float, default=1, help='outer domain diffusion')
    parser.add_argument('--param_kappa1', type=float, default=1, help='inner domain diffusion')

    parser.add_argument('--degree', type=int, default=1, choices=[1, 2], help='Polynomial degree')
    args, _ = parser.parse_known_args()

    # Reduce verbosity
    set_log_level(40)
    # For checking convergence we pick the solution of the test case ...
    material_params = {k.split('_')[-1] : v for k, v in vars(args).items() if k.startswith('param_')}
    mms = setup_mms(material_params)
    u0_true, u1_true, lm_true = (mms['solution'][k] for k in ('u0', 'u1', 'lm'))

    clog = ConvergenceLog({'u0': (u0_true, H1_norm, '1'),
                           'u1': (u1_true, H1_norm, '1'),
                           'lm': (lm_true.expressions, broken_norm(lm_true.subdomains, L2_norm), '0')})

    print(clog.header())
    for i in range(6):
        a, L, W, bcs = setup_problem(i, mms, parameters=material_params,
                                     degree=args.degree)
        # Use direct solver to get the solution
        A, b = map(ii_assemble, (a, L))
        A, b = apply_bc(A, b, bcs)
        A, b = map(ii_convert, (A, b))

        wh = ii_Function(W)
        LUSolver(A, 'mumps').solve(wh.vector(), b)

        u0h, u1h, lmh = wh

        clog.add((u0h, u1h, lmh))
        print(clog.report_last(with_name=False))

    urates = tuple(clog[var].get_rate()[0] for var in ('u0', 'u1'))
    prate, _ = clog['lm'].get_rate()

    expected = (args.degree, )*2
    passed = all(abs(r-e) < 0.1 for r, e in zip(urates, expected)) and prate > 0.4

    sys.exit(int(passed))

reason: import sympy


text:
# BSD 3-Clause License
#
# Copyright (c) 2017 xxxx
# All rights reserved.
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ============================================================================
import argparse
import os
import random
import numpy as np
import torch.multiprocessing as mp
import torch
from solver_8p import train_8p

def seed_everything(seed):
    random.seed(seed)
    os.environ["PYTHONHASHSEED"]  = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)

def main(config):
    if config.model_type not in ['U_Net','R2U_Net','AttU_Net','R2AttU_Net']:
        print('ERROR!! model_type should be selected in U_Net/R2U_Net/AttU_Net/R2AttU_Net')
        print('Your input for model_type was %s'%config.model_type)
        return

    # Create directories if not exist
    config.result_path = os.path.join(config.result_path,config.model_type)
    if not os.path.exists(config.result_path):
        os.makedirs(config.result_path)

    config.train_path = os.path.join(config.data_path, "train")
    config.valid_path = os.path.join(config.data_path, "valid")
    print(config)

    seed_everything(config.seed)
    os.environ["MASTER_ADDR"] = "localhost"
    os.environ["MASTER_PORT"] = "29504"
    if config.npus > 1:
        mp.spawn(train_8p, nprocs=config.npus, args=(config.npus, config))
    else:
        print("config.npus should be greater than 1.")
        raise RuntimeError

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    # model hyper-parameters
    parser.add_argument('--image_size', type=int, default=224)
    parser.add_argument('--t', type=int, default=3, help='t for Recurrent step of R2U_Net or R2AttU_Net')

    # training hyper-parameters
    parser.add_argument('--img_ch', type=int, default=3)
    parser.add_argument('--output_ch', type=int, default=1)
    parser.add_argument('--num_epochs', type=int, default=100)
    parser.add_argument('--num_epochs_decay', type=int, default=70)
    parser.add_argument('--batch_size', type=int, default=16)
    parser.add_argument('--num_workers', type=int, default=8*8)
    parser.add_argument('--lr', type=float, default=0.0002*8)
    parser.add_argument('--beta1', type=float, default=0.5)        # momentum1 in Adam
    parser.add_argument('--beta2', type=float, default=0.999)      # momentum2 in Adam
    parser.add_argument('--augmentation_prob', type=float, default=0.4)

    parser.add_argument('--log_step', type=int, default=2)
    parser.add_argument('--val_step', type=int, default=2)

    # misc
    parser.add_argument('--mode', type=str, default='train')
    parser.add_argument('--model_type', type=str, default='U_Net', help='U_Net/R2U_Net/AttU_Net/R2AttU_Net')
    parser.add_argument('--data_path', type=str, default='./dataset/')
    parser.add_argument('--result_path', type=str, default='./result_8p')

    parser.add_argument('--npus', type=int, default=8)
    parser.add_argument('--use_apex', type=int, default=1)
    parser.add_argument('--apex_level', type=str, default="O2")
    parser.add_argument('--loss_scale', type=float, default=128.)
    parser.add_argument('--seed', type=int, default=12345)
    parser.add_argument('--world_size', type=int, default=1)
    parser.add_argument('--dist_backend', type=str, default="hccl")
    parser.add_argument('--display_freq', type=int, default=-1)

    config = parser.parse_args()
    main(config)

max_stars_repo_name: Ascend/modelzoo

max_issues_repo_name: Ascend/modelzoo

max_forks_repo_name: Ascend/modelzoo


text:
import numpy as np
from aves.visualization.collections import LabelCollection
from aves.features.utils import tfidf, normalize_rows, normalize_columns

def stacked_areas(ax, df, baseline="zero", color_dict=None, **kwargs):
    stack = np.cumsum(df.T.values, axis=0)
    x = df.index.values
    y = df.T.values
    m = y.shape[0]

    if baseline == "zero":
        first_line = np.zeros(x.shape)
    else:
        first_line = (y * (m - 0.5 - np.arange(m)[:, None])).sum(0)
        first_line /= -m
        first_line -= np.min(first_line)
        stack += first_line

    color = color_dict[df.columns[0]] if color_dict is not None else None
    ax.fill_between(x, first_line, stack[0, :], facecolor=color, **kwargs)

    for i in range(len(y) - 1):

        color = color_dict[df.columns[i + 1]] if color_dict is not None else None
        ax.fill_between(x, stack[i, :], stack[i + 1, :], facecolor=color, **kwargs)

    return x, first_line, stack

def streamgraph(
    ax,
    df,
    baseline="wiggle",
    labels=True,
    label_threshold=0,
    label_args=None,
    fig=None,
    area_colors=None,
    area_args=None,
    avoid_label_collisions=False,
    outline_labels=True,
    label_collision_args=None,
):
    if label_args is None:
        label_args = {}

    if area_args is None:
        area_args = {}

    if label_collision_args is None:
        label_collision_args = dict(
            lim=25, arrowprops=dict(arrowstyle="-", color="k", lw=0.5)
        )

    df = df.fillna(0).astype(np.float)

    stream_x, stream_first_line, stream_stack = stacked_areas(
        ax, df, color_dict=area_colors, baseline=baseline, **area_args
    )

    if labels:
        x_to_idx = dict(zip(df.index, range(len(df))))
        max_x = df.idxmax().map(x_to_idx)

        label_collection = LabelCollection()

        max_idx = max_x.values[0]
        y_value = stream_stack[0, max_idx] - stream_first_line[max_idx]

        if y_value >= label_threshold:
            label_collection.add_text(
                df.columns[0],
                stream_x[max_idx],
                stream_first_line[max_idx] * 0.5 + stream_stack[0, max_idx] * 0.5,
            )

        for i in range(1, len(df.columns)):
            max_idx = max_x.values[i]
            y_value = stream_stack[i, max_idx] - stream_stack[i - 1, max_idx]

            if y_value < label_threshold:
                continue

            label_collection.add_text(
                df.columns[i],
                stream_x[max_idx],
                stream_stack[i, max_idx] * 0.5 + stream_stack[i - 1, max_idx] * 0.5,
            )

        label_collection.render(
            ax,
            fig=fig,
            color=label_args.get("color", "white"),
            fontweight=label_args.get("fontweight", "bold"),
            fontsize=label_args.get("fontsize", "xx-large"),
            outline=outline_labels,
            avoid_collisions=avoid_label_collisions,
            adjustment_args=label_collision_args,
        )

max_stars_repo_name: sergioangulo/aves

max_issues_repo_name: sergioangulo/aves

max_forks_repo_name: sergioangulo/aves


text:
import tensorflow as tf
import os
from model import CycleGAN
import utils
import numpy as np
import nibabel as nib
import SimpleITK as sitk

FLAGS = tf.flags.FLAGS
tf.flags.DEFINE_string('model', 'pretrained/cbct2sct.pb', 'model path (.pb)')
tf.flags.DEFINE_integer('height', '288', 'height, default: 288')
tf.flags.DEFINE_integer('width', '320', 'width, default: 320')
# tf.flags.DEFINE_integer('depth_size', '64', 'image size, default: 64')

cbct_list = sorted(os.listdir('cbct/'))

def inference():
    for i in range(len(cbct_list)):
        filename = 'cbct/' + cbct_list[i]
        # cbct = sitk.ReadImage(filename)
        # cbct_data = sitk.GetArrayFromImage(cbct)
        cbct = nib.load(filename)
        cbct_data = cbct.get_fdata().swapaxes(0, 2)
        cbct_data[cbct_data > 3072] = 3072
        depth = cbct_data.shape[0]
        output_sample = np.zeros((depth, FLAGS.height, FLAGS.width))
        for i in range(depth):
            new_cbct = cbct_data[i, :, :].reshape(FLAGS.height, FLAGS.width, 1)
            new_cbct = new_cbct.astype(np.float32)
            input_image = utils.convert2float(new_cbct)
            graph = tf.Graph()

            with graph.as_default():
                with tf.gfile.FastGFile(FLAGS.model, 'rb') as model_file:
                    graph_def = tf.GraphDef()
                    graph_def.ParseFromString(model_file.read())
                [output_image] = tf.import_graph_def(graph_def, input_map={'input_image': input_image},
                                                     return_elements=['output_image:0'], name='output')
            print('12345')
            with tf.Session(graph=graph) as sess:
                generated = output_image.eval()
                generated = generated.reshape(FLAGS.height, FLAGS.width)
                output_sample[i, :, :] = np.round(generated, 0)
        # out = sitk.GetImageFromArray(output_sample)
        out = nib.Nifti1Image(output_sample.swapaxes(0, 2), cbct.affine)
        # sitk.WriteImage(out, 'sct_' + os.path.basename(filename))
        nib.save(out, 'sct_' + os.path.basename(filename))

def main(unused_argv):
    inference()

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

max_stars_repo_name: damnkk/cycle-GAN

max_issues_repo_name: damnkk/cycle-GAN

max_forks_repo_name: damnkk/cycle-GAN


text:
import os
import unittest
import functools
import traceback
import sys
import pdb
from sympy import symbols
from sympy import Function
from sympy import sin
from sympy import sympify

import pathlib

MOD_DIR = pathlib.Path(__file__).parent.absolute().parents[1]

sys.path.append(str(MOD_DIR))
from modelconstructor.affine import modelConstructorAffine

def main():
    mc = modelConstructorAffine('Eh1DofMb', 2, 1)

    state = mc.getStateVar()
    u = mc.getConstrolVar()
    t = mc.getTimeVar()

    xi = state[0]
    xid = state[1]

    zeta0 = symbols('zeta0')
    omega = symbols('Omega')
    y = symbols('etadd', cls=Function)
    f = [0, 0]

    f[0] = xid
    f[1] = -xi - 2 * (u[0] + zeta0) * xid + y(t)

    power = xid**2 * u[0]

    rc = -power

    fc = 0

    mc.init(f, rc, fc)

    mc.append_aux_func_t('etadd', sin(omega*t))
    mc.appendAuxVar(zeta0, sympify(0.1))
    mc.appendAuxVar(omega, sympify(1.0))
    mc.set_control0_val(0, sympify(0.5))
    mc.set_control_limits(0, 0.0, 10.0)
    mc.create()

if __name__ == '__main__':
    main()

reason: from sympy


text:
import numpy as np
from numpy import linalg as LA

INPUT_FILE_NAME = "hmm-data.txt"
NUM_LINES_BEFORE_GRID = 2
NUM_LINES_BEFORE_TOWER = 4
NUM_LINES_BEFORE_STEPS = 4
GRID_WORLD_SIZE = 10
TOWER_NUM = 4
NUM_OF_STEPS = 11
TOWER_UPPER_BOUND = 1.3
TOWER_LOWER_BOUND = 0.7
NUM_OF_STATES = 10**2
a = 0.1

def readFile(file_name):
    grid_world = []
    tower_location = []
    tower_observation = []

    with open(file_name) as file:
        # Read grid world
        for _ in range(NUM_LINES_BEFORE_GRID):
            file.readline()
        for _ in range(GRID_WORLD_SIZE):
            grid_world.append(file.readline().split())

        # Read tower location
        for _ in range(NUM_LINES_BEFORE_TOWER):
            file.readline()
        for _ in range(TOWER_NUM):
            tower_location.append(file.readline().split(":")[1].split())

        # Read tower observation
        for _ in range(NUM_LINES_BEFORE_STEPS):
            file.readline()
        for _ in range(NUM_OF_STEPS):
            tower_observation.append(file.readline().split())
    return np.array(grid_world).astype(int), np.array(tower_location).astype(int), np.array(tower_observation).astype(float)

def createLabels(num):
    labels = []
    for i in range(num):
        for j in range(num):
            # labels.append({"X": i, "Y": j})
            labels.append([i, j])
    # return np.array(labels).reshape((num, num))
    labels_R = np.arange(num ** 2).reshape((num, num))
    return np.array(labels), labels_R

def createTransitionA(num, labels, labels_R,  grid_world):
    A = np.zeros((num ** 2, num ** 2))
    for i in range(num ** 2):
        X_old = labels[i][0]
        Y_old = labels[i][1]
        possible_next_steps = [[X_old-1, Y_old], [X_old+1, Y_old], [X_old, Y_old-1], [X_old, Y_old+1]]
        if(grid_world[X_old, Y_old] != 0):
            valid_coord_set = []
            for X_new, Y_new in possible_next_steps:
                if(isValidNextStep(X_new, Y_new, grid_world)):
                    valid_coord_set.append({"X": X_new, "Y": Y_new})
            if(len(valid_coord_set) > 0):
                for valid_coord in valid_coord_set:
                    A[i, labels_R[valid_coord["X"], valid_coord["Y"]]] = 1.0  / len(valid_coord_set)
                    # A[i, labels_R[valid_coord["X"], valid_coord["Y"]]] = (1.0 + a)/ (len(valid_coord_set) + a * NUM_OF_STATES)
                    # A[i, labels_R[valid_coord["X"], valid_coord["Y"]]] = np.log(1.0 / len(valid_coord_set))
        # A[A == 0] = a / (len(valid_coord_set) + a * NUM_OF_STATES)
    with open('out.txt', 'w') as f:
        print(A, file=f)
    # A = np.log(A)
    # print(A)
    return A

def isValidNextStep(X_new, Y_new, grid_world):
    if(X_new >= 0 and X_new < GRID_WORLD_SIZE and Y_new >=0 and Y_new < GRID_WORLD_SIZE and grid_world[X_new, Y_new] == 1):
        return True
    return False

def createEmissionB(num, labels, labels_R, tower_location, tower_observation):
    # B = []
    B = np.ones((NUM_OF_STATES, NUM_OF_STEPS))
    # B = np.log(B)
    tower_list = tower_location.tolist()
    for tower_index, tower in enumerate(tower_list):
        observation_list = tower_observation[:, tower_index].tolist()
        b = np.zeros((num ** 2, NUM_OF_STEPS))
        for ob_index, observation in enumerate(observation_list):
            for i in range(num ** 2):
                distance = np.linalg.norm(np.array(tower) - [labels[i]])
                # if(distance == 0):
                #     if(observation == 0):
                #         b[i, ob_index] = 1
                #         continue
                #     else:
                #         b[i, ob_index] = 0
                #         continue
                if (distance == 0):
                    if (observation == 0):
                        b[i, ob_index] = (1 + a)/(1 + NUM_OF_STATES * a)
                        continue
                    else:
                        b[i, ob_index] = a/(1 + NUM_OF_STATES * a)
                        continue
                else:
                    max_dis = np.floor(distance * TOWER_UPPER_BOUND * 10)
                    min_dis = np.ceil(distance * TOWER_LOWER_BOUND * 10)
                    valid_range_length = max_dis - min_dis + 1
                    probability = 1.0 / valid_range_length
                    # probability = (1.0 + a) / (valid_range_length + NUM_OF_STATES * a)
                    if(observation * 10 >= min_dis and observation * 10 <= max_dis):
                        b[i, ob_index] = probability
                        # b[i, ob_index] = 1
                    # b[b == 0] = a / (valid_range_length + a * NUM_OF_STATES)

        # B += np.log(b)
        B *= b
    # B = np.log(B)
    return B

def createInitialPi(grid_world):
    free_count = np.count_nonzero(grid_world == 1)
    pi = np.copy(grid_world).astype(float)
    pi[pi > 0.0] = 1.0 / free_count
    # pi[pi > 0.0] = (1.0 + a)/ (free_count + NUM_OF_STATES * a)
    # pi[pi <= 0.0] = a / (free_count + NUM_OF_STATES * a)
    # pi = np.log(pi)
    print(pi)
    return pi

def viterbi(pi, A, B, labels):
    T_1 = np.zeros((NUM_OF_STATES, NUM_OF_STEPS))
    T_2 = np.zeros((NUM_OF_STATES, NUM_OF_STEPS))
    for i in range(NUM_OF_STATES):
        # T_1[:, 0] = pi.ravel() + B[:, 0]
        T_1[:, 0] = pi.ravel() * B[:, 0]

    for j in range(1, NUM_OF_STEPS):
        for i in range(NUM_OF_STATES):
            # column = T_1[:, j-1] + A[:, i] + B[:, j]
            column = T_1[:, j - 1] * A[:, i] * B[:, j]
            # print(column, file=f)
            # T_1[i, j] = np.max(column[column < 0])
            # T_2[i, j] = np.argmax(column[column < 0])
            T_1[i, j] = np.max(column)
            T_2[i, j] = np.argmax(column)
    # with open('out.txt', 'w') as f:
    #     print(T_1, file=f)
    z = np.zeros((NUM_OF_STEPS,), dtype=int)
    x = np.zeros((NUM_OF_STEPS, 2), dtype=int)
    z[NUM_OF_STEPS - 1] = np.argmax(T_1[:, NUM_OF_STEPS - 1])
    print(np.max(T_1[:, NUM_OF_STEPS - 1]))

    x[NUM_OF_STEPS - 1] = labels[z[NUM_OF_STEPS - 1]]
    for i in range(NUM_OF_STEPS - 1, 0, -1):
        z[i - 1] = T_2[z[i], i]
        x[i - 1] = labels[z[i - 1]]
    # print(z)
    return x

    # return T_1

if __name__ == '__main__':
    grid_world, tower_location, tower_observation = readFile(INPUT_FILE_NAME)
    print(grid_world)

    # labels: 100 items, each being [x, y]
    # labels_R: From 0 - 99
    labels, labels_R = createLabels(GRID_WORLD_SIZE)
    np.set_printoptions(threshold=np.inf)
    A = createTransitionA(GRID_WORLD_SIZE, labels, labels_R, grid_world)
    np.set_printoptions(threshold=np.inf)
    # with open('out.txt', 'w') as f:
    #     print(A, file=f)

    B = createEmissionB(GRID_WORLD_SIZE, labels, labels_R, tower_location, tower_observation)

    # with open('out.txt', 'w') as f:
    #     print("A=\n", file=f)
        # print(A, file=f)
        # print("\n\n\n", file=f)
        # print("B=\n", file=f)
        # print(B, file=f)

    pi = createInitialPi(grid_world)
    # print(pi)

    x = viterbi(pi, A, B, labels)
    print()
    print(x.tolist())

reason: import numpy,from numpy


text:
import numpy
from scipy.optimize import minimize

X = numpy.loadtxt("X.txt")
y = numpy.loadtxt("y.txt")

(N,D) = X.shape

w = numpy.random.rand(D,1)

def ridge(Lambda):
  def ridge_obj(theta):
    return ((numpy.linalg.norm(numpy.dot(X,theta) - y))**2)/(2*N) + Lambda*(numpy.linalg.norm(theta))**2
  return ridge_obj

def compute_loss(Lambda, theta):
  return ((numpy.linalg.norm(numpy.dot(X,theta) - y))**2)/(2*N)

for i in range(-5,6):
  Lambda = 10**i;
  w_opt = minimize(ridge(Lambda), w)
  print Lambda, compute_loss(Lambda, w_opt.x)

max_stars_repo_name: LBJ-Wade/mlcourse

max_issues_repo_name: LBJ-Wade/mlcourse

max_forks_repo_name: LBJ-Wade/mlcourse

reason: import numpy,from scipy