Best Python code snippet using playwright-python
dir_gui_trial.py
Source:dir_gui_trial.py
1'''2Created on Aug 26 20173@author: Greg Salomons4GUI interface for DirScan.5Classes6 DirGui:7 Primary GUI window8 sub class of TKinter.Frame9'''10import tkinter as tk11from tkinter import messagebox12from pathlib import Path13from functools import partial14from dir_scan_parameters import ParameterError15from dir_scan_parameters import ParameterWarning16from dir_scan_parameters import DirScanParameters17class DirGuiFrameBase(tk.Frame):18 '''Base class for DirGui frames containing defaults and parameters.19 '''20 def __init__(self, scan_param=None, master_frame=None, **kwargs):21 '''initialize the TK frame and link it with the frame it is22 embedded in.23 '''24 if not scan_param:25 scan_param = DirScanParameters()26 self.data = scan_param27 self.update_master_frame(master_frame)28 self.build_instructions = list()29 self.child_frames = list()30 super().__init__(self.master_frame, **kwargs)31 def update_master_frame(self, master_frame):32 ''' Update the calling TK frame.33 '''34 if not master_frame:35 self.master_frame = tk.Tk()36 elif hasattr(master_frame, 'tk'):37 self.master_frame = master_frame38 else:39 err_msg = 'master_frame must be a TK widget or frame.'40 '\n\t Got type {}'41 raise TypeError(err_msg.format(type(master_frame)))42 def add_build_instruction(self, subgui, build_method='pack', kwargs: dict=None):43 '''A build instruction is a length 3 tuple:44 subgui: a TK.frame or widget object,45 build_method: One of 'pack' or 'grid',46 kwargs: keyword dict with 'pack' or 'grid' parameters.47 '''48 self.child_frames.append(subgui)49 if not kwargs and 'pack' in build_method:50 kwargs = {'fill': tk.X, 'side': tk.LEFT, 'padx': 10, 'pady': 5}51 instruction = (subgui, build_method, kwargs)52 self.build_instructions.append(instruction)53 def get_data(self):54 '''Initialize all child frames.55 '''56 for child in child_frames:57 if hasattr(child, get_data):58 child.get_data()59 def refresh(self):60 '''Update all child frames.61 '''62 for child in child_frames:63 if hasattr(child, refresh):64 child.refresh()65 def action_message(self, message_type=None, **kwargs):66 '''Generic message box67 message_type can be one of:68 'showinfo' (default)69 'showwarning'70 'showerror'71 'askretrycancel'72 Parameter options are:73 title='This is a message box',74 message='This is the message',75 detail='Some more details',76 '''77 if not message_type:78 message = messagebox.showinfo(parent=self, **kwargs)79 elif 'askretrycancel' in message_type:80 message = messagebox.askretrycancel(parent=self, **kwargs)81 elif 'showerror' in message_type:82 message = messagebox.showerror(parent=self, **kwargs)83 elif 'showwarning' in message_type:84 message = messagebox.showwarning(parent=self, **kwargs)85 elif 'showinfo' in message_type:86 message = messagebox.showinfo(parent=self, **kwargs)87 else:88 raise ValueError('message_type must be one of:\t\nshowinfo\t'89 '(default)\n\t'90 'showwarning\n\t'91 'showerror\n\t'92 'askretrycancel')93 return message94 def build(self):95 '''Configure the GUI frame and add sub-frames.96 This method may be overwritten for sub-classes.97 Parameter:98 build_instructions: Type list of length 3 tuples.99 Each tuple contains:100 (sub-GUI object, 'pack'|'grid', pack or grid keyword dict)101 '''102 if self.build_instructions:103 for (subgui, method, kwargs) in self.build_instructions:104 try:105 build_method = subgui.__getattribute__(method)106 except AttributeError as err:107 raise err('{} is not a valid method of {}'.format(108 str(method),str(subgui)))109 build_method(**kwargs)110class DirGuiElementFrame(DirGuiFrameBase):111 '''DirGui Base class for selecting or changing a specific112 DirScanParameters element.113 '''114 # TK variable types to link with parameter values115 var_type_dict = {116 'string': tk.StringVar,117 'int': tk.IntVar118 }119 def __init__(self, parameter_name=None, var_type='string', **kwargs):120 '''Build a frame and link the access variable to a specific parameter.121 '''122 super().__init__(**kwargs)123 var_select = self.var_type_dict[var_type]124 self.select_var = var_select()125 self.parameter = parameter_name126 def set(self, select_value):127 '''Set the frame variable.128 '''129 self.select_var.set(select_value)130 def get(self):131 '''Get the frame variable.132 '''133 return self.select_var.get()134 def refresh(self):135 '''Initialize the Gui variable from the initial scan_param values.136 '''137 value = self.data.__getattribute__(self.parameter)138 self.set(value)139 def get_data(self):140 '''Update the scan_param data element from the Gui variable.141 '''142 param = {self.parameter: self.get()}143 try:144 self.data.update_parameters(**param)145 except ParameterError as err:146 self.action_message('showerror', title=type(err), message=err)147 except ParameterWarning as err:148 self.action_message('showwarning', title=type(err), message=err)149 def build(self, build_instructions=None):150 '''Configure the GUI frame and add sub-frames.151 This method may be overwritten for sub-classes.152 Parameter:153 build_instructions: Type list of length 3 tuples.154 Each tuple contains:155 (sub-GUI object, 'pack'|'grid', pack or grid keyword dict)156 '''157 self.refresh()158 super().build()159class StatusTextFrame(DirGuiElementFrame):160 '''GUI frame for indicating current status of the Actions.161 '''162 def __init__(self, **kwargs):163 kwargs.update({'parameter_name': 'status'})164 super().__init__(**kwargs)165 status_box = tk.Label(self, textvariable=self.select_var)166 self.add_build_instruction(status_box)167 self.build()168class ActionButtonsFrame(DirGuiElementFrame):169 '''GUI frame containing a single button.170 '''171 def __init__(self, button_text='', action_method=None, **kwargs):172 kwargs.update({'parameter_name': 'action_text'})173 super().__init__(**kwargs)174 action_button = tk.Button(self, text=button_text, command=action_method)175 #action_button = tk.Button(self, textvariable=self.select_var)176 self.add_build_instruction(action_button)177 self.build()178class TestFrame(DirGuiFrameBase):179 '''GUI frame for indicating current status of the Actions.180 '''181 def __init__(self, frame_label='', master_frame=None, **kwargs):182 super().__init__(master_frame=master_frame, **kwargs)183 labeled_frame = tk.LabelFrame(self, text=frame_label)184 status_box = StatusTextFrame(master_frame=labeled_frame, **kwargs)185 def update_label(widget=status_box, text='changed'):186 widget.set(text)187 test_button = ActionButtonsFrame(master_frame=labeled_frame,188 button_text='try me',189 action_method=update_label)190 warning_button = ActionButtonsFrame(master_frame=labeled_frame,191 button_text='warning',192 action_method=self.show_warning)193 self.add_build_instruction(status_box)194 self.add_build_instruction(test_button)195 self.add_build_instruction(warning_button)196 self.add_build_instruction(labeled_frame)197 self.build()198 def show_warning(self, message_type='showwarning',199 title='test warning',200 message='test warning message'):201 self.action_message('showwarning', title=title, message=message)202class DirGui(DirGuiFrameBase):203 '''TKinter GUI class used for the DIR Scan program main GUI.204 '''205 def __init__(self, scan_param=None, **kwargs):206 '''Create the DIR Scan GUI and set the initial parameters207 '''208 super().__init__(scan_param, **kwargs)209 #status = StatusTextFrame(scan_param=scan_param, master_frame=self)210 status = TestFrame(scan_param=scan_param, master_frame=self,211 frame_label='test label')212 self.add_build_instruction(status, 'grid',213 {'column': 1, 'row': 2, 'columnspan': 3,214 'padx': 10, 'sticky': tk.W})215 self.add_build_instruction(self)216 self.window_format()217 self.build()218 def window_format(self):219 '''Format and label main GUI window.220 Add a window title,221 Add a window icon,222 Add a header label223 '''224 root = self._root()225 root.title("Directory Scan")226 # Add a window icon227 ico_pict = r'.\Icon test.png'228 root.iconphoto(root, tk.PhotoImage(file=ico_pict))229 #Add Top header230 header = tk.Label(self, text='Directory Scan')231 header.config(font=('system', 20, 'bold'))232 header.grid(column=1, row=1, columnspan=3)233 def activate(self):234 self.master_frame.mainloop()235def main():236 '''Test the activate_gui function call.237 '''238 def param_string(scan_param: DirScanParameters):239 '''Display the parameters in a pop-up message a test function.240 '''241 prams_to_display = [242 ('base_path = ', 'base_path'),243 ('directory to scan = ', 'directory_to_scan'),244 ('file to parse = ', 'file_to_scan'),245 ('Scan a directory = ', 'do_dir_scan'),246 ('Parse dir data = ', 'parse_dir_data'),247 ('File to save directory scan = ', 'directory_scan_output'),248 ('Save scan output = ', 'save_scan_output'),249 ('File-data output file = ', 'file_data_output'),250 ('Save file data = ', 'output_file_data'),251 ('Dir-data output file = ', 'dir_data_output'),252 ('Save dir data = ', 'output_dir_data')253 ]254 param_text = 'Scan Parameters'255 for (text_str, parameter) in prams_to_display:256 value = scan_param.__getattribute__(parameter)257 param_text += '\n' + text_str + str(value)258 return param_text259 def test_message(scan_param: DirScanParameters):260 '''Display a message box containing parameter info.261 '''262 message_text = param_string(scan_param)263 results = messagebox.showinfo(title='Parameters',264 message=message_text)265 test_scan_param = DirScanParameters(\266 base_path=Path('.'),267 file_to_scan='Test_Files.txt',268 time_type="C",269 source='Test Files',270 top_dir=Path('.'),271 file_data_output='Test_files_data.csv',272 output_dir_data=False,273 dir_data_output='Test_dir_data.csv',274 status='Test of GUI')275 action_param = dict(276 action_methods={277 'Test_Action': partial(test_message, test_scan_param),278 'Exit': tk._exit279 },280 action_sequences={281 'Test_Action': ('Test_Action',),282 'Exit': ('Exit',)283 }284 )285 #dir_gui = DirGui(scan_param=test_scan_param)286 #dir_gui.activate()287 test_message(test_scan_param)288if __name__ == '__main__':...
shot_service.py
Source:shot_service.py
1#!/usr/bin/python2from __future__ import with_statement3import sys, signal, pickle, stompy, tempfile, threading, urllib4from datetime import datetime5from PyQt4.QtCore import * #@UnusedWildImport6from PyQt4.QtGui import QImage, QPainter, QApplication7from PyQt4.QtWebKit import QWebPage8from lts.process_manager import ProcessWorker9global logger, cfg10class ScreenshotWorker(QThread):11 def __init__(self, jobdone=None):12 self.task = None13 self.last_timeouted_task = None14 self.webpage = QWebPage()15 self.mutex = QMutex()16 self.processing = QWaitCondition()17 self.timer = QTimer(self.webpage)18 self.jobdone = jobdone19 QThread.__init__(self)20 21 def postSetup(self, name):22 # Called by main after start()23 QObject.connect(self, SIGNAL("open"), 24 self.onOpen, Qt.QueuedConnection)25 QObject.connect(self.timer, SIGNAL("timeout()"),26 self.onTimer, Qt.QueuedConnection)27 self.setObjectName(name)28 def onTimer(self):29 self.mutex.lock()30 if self.task is None:31 logger.warn("%s Timeout without task info", self.objectName())32 else:33 logger.warn("%s Timeout: %s", self.objectName(), self.task['url'])34 if(self.task==self.last_timeouted_task):35 logger.error("%s Duplicated timeout", self.objectName())36 QApplication.instance().exit()37 else:38 # enable task reader39 self.last_timeouted_task = self.task40 self.webpage.triggerAction(QWebPage.Stop)41 self.mutex.unlock() 42 def onLoadFinished(self, result):43 self.mutex.lock()44 try:45 self.last_timeouted_task = None46 self.timer.stop()47 except:48 pass49 if (self.webpage.bytesReceived() == 0) or self.task is None:50 logger.error("%s Request failed", self.objectName())51 if cfg.queues.cancel:52 # failure info53 self.writeMQ(cfg.queues.cancel, self.task)54 else:55 logger.info("%s Page loaded: %s", self.objectName(), self.task['url'])56# logger.info("HTML text: \n%s\n", self.webpage.mainFrame().toHtml())57 # Set the size of the (virtual) browser window58 self.webpage.setViewportSize(self.webpage.mainFrame().contentsSize())59 # Paint this frame into an image60 qs = self.webpage.viewportSize()61 logger.debug("%s View port size: %s", self.objectName(), str(qs))62 if qs.width() > cfg.shot_service.max_width:63 qs.setWidth(cfg.shot_service.max_width)64# if qs.width() < min_width:65# qs.setWidth(min_width)66 if qs.height() > cfg.shot_service.max_height:67 qs.setHeight(cfg.shot_service.max_height)68# if qs.height() < min_height:69# qs.setHeight(min_height)70 logger.debug("%s Size to save: %s", self.objectName(), str(qs))71 image = QImage(qs, QImage.Format_ARGB32)72 painter = QPainter(image)73 logger.debug("%s Rendering URL: %s",74 self.objectName(), self.task['url'])75 self.webpage.mainFrame().render(painter)76 painter.end()77 logger.info("%s Saving file: %s",78 self.objectName(), self.task['filename'])79 image_save_result = image.save(self.task['filename'])80 html_save_result = False81 if(self.task['html_filename']):82 # logger.debug("HTML: \n%s\n", self.webpage.mainFrame().toHtml())83 f = open(self.task['html_filename'],'wb')84 # f.write(fixXml(self.webpage.mainFrame().toHtml().toUtf8()))85 f.write(self.webpage.mainFrame().toHtml().toUtf8())86 f.close()87 self.task['html_title'] = self.webpage.mainFrame().title().toUtf8()88 self.task['html_url'] = self.webpage.mainFrame().url().toString().toUtf8()89 html_save_result = True90 child_frames = self.webpage.mainFrame().childFrames()91 self.task['sub_frame_count'] = len(child_frames)92 for i in range(len(child_frames)):93 f = open(self.task['html_filename']+str(i),'wb')94 # f.write(fixXml(child_frames[i].toHtml().toUtf8()))95 f.write(child_frames[i].toHtml().toUtf8())96 f.close()97 98 self.task['html_url'+str(i)] = child_frames[i].url().toString().toUtf8()99 if image_save_result or html_save_result:100 if self.jobdone:101 self.jobdone()102 103 logger.info("%s File saved: %s %s",104 self.objectName(), self.task['filename'], 105 self.task['html_filename'])106 if cfg.queues.shotted:107 # success info108 self.task['shot_time']=datetime.utcnow()109 # self.task['html'] = self.webpage.mainFrame().toHtml()110 self.writeMQ(cfg.queues.shotted, self.task)111 else:112 logger.error("%s Failed to save file: %s",113 self.objectName(), self.task['filename'])114 if cfg.queues.cancel:115 # failure info116 self.writeMQ(cfg.queues.cancel, self.task)117 # enable task reader118 self.task = None119 try:120 QObject.disconnect(self.webpage, SIGNAL("loadFinished(bool)"), 121 self.onLoadFinished)122 except:123 pass124 self.processing.wakeOne()125 self.mutex.unlock()126 def writeMQ(self, queue, task):127 if not queue:128 return129 try:130 stomp = stompy.simple.Client()131 stomp.connect()132 stomp.put(pickle.dumps(task),133 destination=queue)134 # conn = stomp.Connection()135 # conn.start()136 # conn.connect()137 # conn.send(pickle.dumps(task), destination=queue)138 finally:139 try:140 stomp.disconnect()141 # conn.disconnect()142 except:143 logger.warn("%s Failed to enqueue finished task.",144 self.objectName())145 def onOpen(self, url):146 logger.debug("%s onOpen: [%s]", self.objectName(), url)147 self.webpage.mainFrame().setHtml("<html></html>")148 if(self.task.has_key('canvas_size')):149 self.webpage.setViewportSize(QSize(self.task['canvas_size']['width'],150 self.task['canvas_size']['height']))151 else:152 self.webpage.setViewportSize(QSize(0,0))153 self.timer.start(cfg.shot_service.timeout * 1000)154 # n = 0155 # for i in range(1000000):156 # n+=i157 QObject.connect(self.webpage, SIGNAL("loadFinished(bool)"), 158 self.onLoadFinished, Qt.QueuedConnection)159 self.webpage.mainFrame().load(QUrl(unicode(urllib.unquote(url))))160 def run(self):161 while True:162 self.mutex.lock()163 # wait for task done164 while self.task != None:165 logger.debug("%s Waiting for a running task: %s",166 self.objectName(), self.task['url'])167 self.processing.wait(self.mutex)168 try:169 # persistent stomp is unsafe :(170 stomp = stompy.simple.Client()171 stomp.connect()172 stomp.subscribe(cfg.queues.processed, ack='client')173 except:174 logger.warn("%s STOMP subscribe failed.", self.objectName())175 try:176 stomp.disconnect()177 except:178 pass179 self.mutex.unlock()180 continue181 try:182 m=stomp.get()183 stomp.ack(m)184 except:185 logger.warn("%s STOMP dequeue failed.", self.objectName())186 self.mutex.unlock()187 continue188 finally:189 try:190 stomp.unsubscribe(cfg.queues.processed)191 stomp.disconnect()192 except:193 logger.warn("%s STOMP unsubscribe failed.", self.objectName())194 pass195 self.task = pickle.loads(m.body)196 self.last_timeouted_task = None197 if self.task['filename'] is None:198 if sys.hexversion >= 0x02060000:199 f = tempfile.NamedTemporaryFile(suffix='.png', delete=False)200 else:201 f = tempfile.NamedTemporaryFile(suffix='.png')202 self.task['filename'] = f.name203 f.close()204 if not self.task.has_key('html_filename') or \205 self.task['html_filename'] is None:206 if sys.hexversion >= 0x02060000:207 f = tempfile.NamedTemporaryFile(suffix='.html', delete=False)208 else:209 f = tempfile.NamedTemporaryFile(suffix='.html')210 self.task['html_filename']=f.name211 f.close()212 self.task['sub_frame_count']=0213 logger.info("%s Run: %s", self.objectName(), self.task['url'])214 self.emit(SIGNAL("open"), self.task['url'])215 self.mutex.unlock()216class ShotProcessWorker(ProcessWorker):217 def __init__(self, cfg, logger, id='UNKNOWN', post_fork=None, lifetime=None):218 super(ShotProcessWorker, self).__init__(cfg, logger, id=id,219 post_fork=post_fork)220 self.lifetime = lifetime221 def run(self):222 app = QApplication([])223 signal.signal(signal.SIGINT, signal.SIG_DFL)224 signal.signal(signal.SIGCHLD, signal.SIG_DFL)225 shotter = ScreenshotWorker(jobdone=lambda: self.jobDone())226 shotter.start()227 shotter.postSetup(self.id)228 229 if(self.lifetime):230 threading.Timer(self.lifetime, app.exit).start()231 232 exit(app.exec_())233 234 def clone(self):235 return self.__class__(self.cfg, self.logger, id=self.id,236 post_fork=self.post_fork, lifetime=self.lifetime)...
prepare_reg.py
Source:prepare_reg.py
1import os2import numpy as np 3import pandas as pd 4from scipy.io import loadmat5from sklearn.model_selection import StratifiedKFold, KFold, train_test_split6raw_dir= "/run/user/1435515045/gvfs/smb-share:server=istanbul.psychology.pitt.edu,share=raw_data/TPOT/Video_Data/normalized_raw_final/"7zface_dir ='/run/user/1435515045/gvfs/smb-share:server=istanbul.psychology.pitt.edu,share=ground_truth_data/TPOT/20191111_Converted_pipeline_output/'8raw_list= np.array([name for name in os.listdir(raw_dir)])9zface_list= np.array([name for name in os.listdir(zface_dir) if name.endswith('fit.mat')])10#print (len(zface_list))11#input ('here')12def convert_bstring_to_normal(inputlist):13 outputlist=[]14 for elements in inputlist:15 if isinstance(elements,np.bytes_):16 outputlist.append(elements.decode('utf-8'))17 else:18 outputlist=inputlist19 20 return outputlist21def group_to_category(scores):22 scores=np.array(scores)23 for i in range (len(scores)):24 if scores[i] == 1:25 scores[i]=026 elif scores[i]==2:27 scores[i]=128 return np.array(scores,dtype=int)29def phq_score_to_category(scores):30 scores=np.array(scores)31 for i in range(len(scores)):32 if scores[i] >=20:33 scores[i]=434 elif scores[i] >=15:35 scores[i]=336 elif scores[i] >= 10:37 scores[i] = 238 elif scores[i] >=5:39 scores[i] = 140 else:41 scores[i] = 042 return np.array(scores,dtype=int)43def panas_convert(names,scores):44 #print (scores)45 names=np.array(names)46 blank_indices= np.where (scores==' ')47 48 names=np.delete(names,blank_indices)49 scores=np.delete(scores,blank_indices)50 assert len(names) == len(scores)51 52 scores= np.array(scores).astype('float')53 new_array= scores / 5054 55 return names,new_array56def does_exist(name,name_list,length_list):57 flag=058 length=059 for i in range(len(name_list)):60 elements=name_list[i]61 if name == elements:62 flag=flag+163 length=length_list[i]64 if flag:65 #print ("flag", flag, "Name", name)66 #input ('')67 68 return length69 else:70 return 071def segment_real (total_frames,eliminate_time,slide=0,window_size=0,fps=30,mode='slice'):72 ##--------Variable descriptions ####73 #total_frames : length of the video in terms of frames74 # eliminated_time : Argument passed in minutes. The time we want to exclude from beginning and end of the video. Give 0 in case you don't want to eliminate any75 # window_size: Argument passed in seconds. 76 # slide : Argument passed in seconds to calculate how many seconds to slide77 # mode : can be 'slice' or 'slide'. Use slice for getting equal interval slices. Else use slide for sliding window.78 eliminated_frames=int(eliminate_time*60*fps)79 start=eliminated_frames80 end=total_frames-eliminated_frames81 actual_frames=end-start82 83 window_length=window_size*fps84 index_list=[]85 slide_length=slide*fps86 if mode=='slice':87 factor =int (actual_frames/window_length)88 89 for i in range (factor):90 index_list.append([start,start+window_length])91 start=start+window_length92 else:93 while start < end :94 #print (start,start+window_length)95 if start+window_length < end:96 index_list.append([start,start+window_length])97 98 start=start+slide_length99 #print (len(index_list))100 #print (end)101 102 return index_list103def read_zface(zface_filename):104 mat=loadmat(os.path.join(zface_dir,zface_filename))105 zface_data = mat['fit']106 no_frames=zface_data.shape[1]107 return no_frames108def expand_data(file_list,label_list):109 mother_name=np.empty(0)110 child_name= np.empty(0)111 start_list=np.empty(0)112 end_list=np.empty(0)113 panas_score=np.empty(0)114 family_list=[]115 for idx, family in enumerate(file_list):116 family= str(family)[2:]117 mother_video_name= family + str(2) + '_02_01' 118 child_video_name= family + str(1) + '_02_01'119 mother_exist = raw_list[raw_list==mother_video_name]120 child_exist = raw_list[raw_list==child_video_name]121 122 if len (mother_exist) and len(child_exist) :123 #print (mother_video_name,child_video_name)124 mother_zface= mother_video_name + '_fit.mat'125 child_zface= child_video_name + '_fit.mat'126 try:127 mother_frames,child_frames=read_zface(mother_zface),read_zface(child_zface)128 except:129 continue130 if mother_frames < 20000 or mother_frames > 50000 or child_frames <20000 or child_frames >50000:131 continue 132 mother_seg= np.array(segment_real(mother_frames,2.5,window_size=30))133 #child_seg = segment_real(child_frames,2.5,window_size=30)134 mother_split=np.full(len(mother_seg),mother_video_name)135 child_split=np.full(len(mother_seg), child_video_name)136 label_aug=np.full(len(mother_seg),label_list[idx])137 start,end = mother_seg[:,0] , mother_seg[:,1] 138 139 mother_name=np.hstack([mother_name,mother_split]) if mother_name.size else mother_split140 child_name=np.hstack([child_name,child_split]) if child_name.size else child_split141 start_list=np.hstack([start_list,start]) if start_list.size else start142 end_list=np.hstack([end_list,end]) if end_list.size else end143 panas_score=np.hstack([panas_score,label_aug]) if panas_score.size else label_aug144 family_list.append(family)145 #mother_list.extend() 146 assert len(mother_name) == len(child_name) == len(start_list) == len(end_list) == len(panas_score)147 148 return [mother_name,child_name,start_list,end_list,panas_score,family_list]149 150tpot_filename='../tpot-actual.csv'151tpot_data=pd.read_csv(tpot_filename)152families= tpot_data['FamId']153parent_psi_pos= np.array(tpot_data['m_2t12pp'])154child_psi_pos= np.array(tpot_data['c_7z12pp'])155group= parent_psi_pos.copy()156#group=np.concatenate((parent_psi_pos,child_psi_pos),axis=1)157assert len(families) == len(parent_psi_pos) == len(child_psi_pos)158'''159"Creating 5 folds for families .Uncomment only when you need to create new folds"160kf=StratifiedKFold(n_splits=5, random_state=None, shuffle=False)161for idx,(train_index,test_index) in enumerate(kf.split(families,group)):162 x_train = families[train_index]163 x_test = families[test_index]164 y_train = group[train_index]165 y_test = group[test_index]166 167 x_train,x_valid,y_train,y_valid= train_test_split(x_train,y_train, test_size=0.2)168 d={}169 d['x_train'] = x_train170 d['x_test'] = x_test171 d['y_train']= y_train172 d['y_test'] = y_test173 d['x_valid']= x_valid174 d['y_valid']= y_valid175 np.save ('./kfold_reg/fold_'+str(idx)+'.npy',d)176'''177fold_dir= 'kfold_reg/'178fold_list= sorted ([name for name in os.listdir(fold_dir) if name.startswith('fold')])179name_list_filename='../data_analysis/ground_truth_data/name_list.npy'180length_list_filename='../data_analysis/ground_truth_data/length_list.npy'181name_list=np.load(name_list_filename,allow_pickle=True)182length_list=np.load(length_list_filename,allow_pickle=True)183assert len(name_list) == len(length_list)184for idx, folds in enumerate(fold_list):185 d=np.load(os.path.join(fold_dir,folds),allow_pickle=True).item()186 x_train,y_train = d['x_train'],d['y_train']187 x_valid,y_valid = d['x_valid'],d['y_valid']188 x_test,y_test = d['x_test'],d['y_test']189 x_train,y_train= panas_convert (x_train,y_train)190 x_valid,y_valid= panas_convert (x_valid,y_valid)191 x_test,y_test = panas_convert (x_test,y_test)192 193 train_mother_name,train_child_name,train_start_list,train_end_list,train_panas_score,train_family=expand_data(x_train,y_train)194 valid_mother_name,valid_child_name,valid_start_list,valid_end_list,valid_panas_score,valid_family=expand_data(x_valid,y_valid)195 test_mother_name,test_child_name,test_start_list,test_end_list,test_panas_score,test_family =expand_data(x_test,y_test)196 train_data={}197 train_data['mother_name']=train_mother_name198 train_data['child_name']=train_child_name199 train_data['label']=train_panas_score200 train_data['start_list']=train_start_list201 train_data['end_list']=train_end_list202 train_data['family'] = train_family203 valid_data={}204 valid_data['mother_name']=valid_mother_name205 valid_data['child_name']=valid_child_name206 valid_data['label']=valid_panas_score207 valid_data['start_list']=valid_start_list208 valid_data['end_list']=valid_end_list209 valid_data['family'] = valid_family210 test_data={}211 test_data['mother_name']=test_mother_name212 test_data['child_name']=test_child_name213 test_data['label']=test_panas_score214 test_data['start_list']=test_start_list215 test_data['end_list']=test_end_list216 test_data['family'] = test_family217 train_filename= './kfold_reg/train_data_' + str(idx) + '.npy'218 valid_filename= './kfold_reg/valid_data_' + str(idx) + '.npy'219 test_filename = './kfold_reg/test_data_' + str(idx) + '.npy'220 np.save(train_filename,train_data)221 np.save(valid_filename,valid_data)222 np.save(test_filename,test_data)223 print ('Done for fold %d' %(idx))...
frames.py
Source:frames.py
1from collections import defaultdict2from datetime import datetime3from io import BytesIO4import bimpy5import crossfiledialog6from diff_match_patch import diff_match_patch7from mymcplus import ps2mc8from slimseditor.backends import AbstractBackend, PS2WrappedBinBackend9from slimseditor.hexdump import hexdump10counter = 011def get_next_count():12 global counter13 value, counter = counter, counter + 114 return value15def format_patchline(t, text):16 text = text.strip('\n').replace('\n', '\n ')17 return f"{'+' if t == 1 else '-'} {text}"18class FrameBase:19 def __init__(self):20 self._size = None21 self.opened = bimpy.Bool(True)22 self.click_states = defaultdict(bimpy.Bool)23 def render(self):24 if not self._size:25 self._size = bimpy.Vec2(400, 600)26 bimpy.set_next_window_size(self._size)27 def process_events(self):28 pass29class SaveGameFrame(FrameBase):30 def __init__(self, backend_class, *backend_args, **backend_kwargs):31 super(SaveGameFrame, self).__init__()32 self.backend_class = backend_class33 self.backend_args = backend_args34 self.backend_kwargs = backend_kwargs35 self.load_backend()36 self.name = '{0}##{1}'.format(self.backend.get_friendly_name(), get_next_count())37 self.diff_string = ""38 def load_backend(self):39 self.backend = self.backend_class(*self.backend_args, **self.backend_kwargs) # type: AbstractBackend40 try:41 self.items = self.backend.get_items()42 except KeyboardInterrupt as e:43 raise e44 except Exception as e:45 self.items = dict()46 print(str(e))47 def render(self):48 super(SaveGameFrame, self).render()49 if bimpy.begin(self.name, self.opened,50 flags=bimpy.WindowFlags.NoCollapse | bimpy.WindowFlags.MenuBar):51 if bimpy.begin_menu_bar():52 bimpy.menu_item('Save', 'Cmd+S', self.click_states['save'])53 bimpy.menu_item('Reload', 'Cmd+R', self.click_states['reload'])54 bimpy.menu_item('Export', 'Cmd+E', self.click_states['export'])55 bimpy.menu_item('Reload & Diff', 'Cmd+D', self.click_states['reload_and_diff'])56 bimpy.end_menu_bar()57 if self.diff_string:58 bimpy.columns(2, "hex split")59 bimpy.text('Game: ')60 bimpy.same_line()61 bimpy.text(self.backend.game.value)62 for section_name, section_items in self.items.items():63 if bimpy.collapsing_header(section_name):64 for item in section_items:65 item.render_widget()66 if self.diff_string:67 bimpy.next_column()68 for line in self.diff_string.splitlines():69 bimpy.text(line)70 bimpy.end()71 def reload_and_diff(self):72 pre_reload_hex = hexdump(self.backend.data, print_ascii=False)73 self.load_backend()74 post_reload_hex = hexdump(self.backend.data, print_ascii=False)75 patcher = diff_match_patch()76 patcher.Diff_Timeout = 077 text1, text2, line_array = patcher.diff_linesToChars(pre_reload_hex, post_reload_hex)78 diffs = patcher.diff_main(text1, text2)79 patcher.diff_cleanupSemantic(diffs)80 patcher.diff_charsToLines(diffs, line_array)81 patch = '\n'.join(format_patchline(t, text) for t, text in diffs if t != 0)82 self.diff_string = f'{datetime.now()}\n{patch}\n\n{self.diff_string}'83 def process_events(self):84 if self.click_states['save'].value:85 self.backend.write_all_items(self.items)86 self.backend.write_data()87 self.click_states['save'].value = False88 if self.click_states['reload'].value:89 self.load_backend()90 self.click_states['reload'].value = False91 if self.click_states['reload_and_diff'].value:92 self.reload_and_diff()93 self.click_states['reload_and_diff'].value = False94 if self.click_states['export'].value:95 filename = crossfiledialog.save_file()96 if filename:97 with open(filename, 'wb') as f:98 f.write(self.backend.data)99 self.click_states['export'].value = False100class PS2MCFrame(FrameBase):101 NAME_EXCLUSIONS = [b'.', b'..']102 def __init__(self, path):103 self._size = None104 self.opened = bimpy.Bool(True)105 self.click_states = defaultdict(bimpy.Bool)106 self.path = path107 self.name = '{0}##{1}'.format(path, get_next_count())108 self.child_frames = []109 self.tree = dict()110 self.load_card_data()111 def load_card_data(self):112 with open(self.path, 'rb') as f:113 self.data = BytesIO(f.read())114 self.card = ps2mc.ps2mc(self.data)115 self.tree = dict()116 root_dir = self.card.dir_open('/')117 for entry in root_dir:118 if not ps2mc.mode_is_dir(entry[0]):119 continue120 if entry[-1] not in self.NAME_EXCLUSIONS:121 dirname = entry[-1].decode('ascii')122 subnames = []123 dir_files = self.card.dir_open(dirname)124 for file_entry in dir_files:125 if file_entry[-1] in self.NAME_EXCLUSIONS:126 continue127 decoded_name = file_entry[-1].decode('ascii')128 if decoded_name.endswith('bin'):129 subnames.append((130 decoded_name,131 'Open##{0}/{1}'.format(dirname, decoded_name)132 ))133 self.tree[dirname] = subnames134 dir_files.close()135 root_dir.close()136 def write_card_data(self):137 self.card.flush()138 with open(self.path, 'wb') as f:139 self.data.seek(0)140 f.write(self.data.read())141 def render(self):142 if not self._size:143 self._size = bimpy.Vec2(400, 600)144 bimpy.set_next_window_size(self._size)145 if bimpy.begin(self.name, self.opened,146 flags=bimpy.WindowFlags.NoCollapse | bimpy.WindowFlags.MenuBar):147 if bimpy.begin_menu_bar():148 bimpy.menu_item('Reload', 'Cmd+R', self.click_states['reload'])149 bimpy.end_menu_bar()150 for folder_name, folder_files in self.tree.items():151 if bimpy.collapsing_header(folder_name):152 for item, button_name in folder_files:153 if bimpy.button(button_name):154 item_path = '{0}/{1}'.format(folder_name, item)155 try:156 new_savegame = SaveGameFrame(157 PS2WrappedBinBackend, item_path, self)158 self.child_frames.append(new_savegame)159 except KeyboardInterrupt as e:160 raise e161 except Exception as e:162 print(e)163 bimpy.same_line()164 bimpy.text(item)165 bimpy.end()166 for child_frame in self.child_frames:167 child_frame.render()168 def process_events(self):169 if self.click_states['reload'].value:170 self.load_card_data()171 self.click_states['reload'].value = False172 for child_frame in self.child_frames:173 child_frame.process_events()174 open_frames_to_close = []175 for i, frame in enumerate(self.child_frames):176 if not frame.opened.value:177 open_frames_to_close.append(i)178 for i in sorted(open_frames_to_close, reverse=True):...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!