def __init__(self, args, desc, parent=None): if isinstance(args, basestring): command = '%s %s' % (self.program, args) else: command = (self.program,) + tuple(args) Process.__init__(self, command, self.title, desc, parent)
def main(): p=Process() print(p.proces('5+6-(2*6-3)/3')) print(p.proces('12+2*6-(15/3*2)-7')) print(p.proces('5/1-5+2*(7+2/2*7)-4/2')) print(p.proces('(8/2*6)-4+(14*2)-50')) return
def __init__(self,context,id): Process.__init__(self,context,id) if 'config' in context: self.__config__ = context['config'] else: raise Exception('WestGridStatusProcess','no config found in context.') if 'process' in self.__config__: if 'host' in self.__config__['process']: self.__host__ = self.__config__['process']['host'] else: raise Exception('WestGridProcess','required field [process]:host not found in workfile') if 'user' in self.__config__['process']: self.__user__ = self.__config__['process']['user'] else: self.__user__ = environ['USER'] if 'key' in self.__config__['process']: self.__key__ = self.__config__['process']['key'] else: if 0 == system( 'touch ~/.ssh/id_rsa &> /dev/null' ): self.__key__ = '~/.ssh/id_rsa' else: raise Exception('WestGridProcess','required field [process]:key="~/.ssh/id_rsa" not found in workfile') else: raise Exception('WestGridProcess','no process section found in workfile') self.__last_time__ = 0 self.__jobs__ = {} task = WG_DeployTask( self.id(), None, self.__config__ ) task.execute()
def mount(parent, device): mountpoint, options = query_mount(parent, device) if not mountpoint: return None command = ('mount', '-t', 'ocfs2', device, mountpoint) if options: command = list(command) command[1:1] = ('-o', options) p = Process(command, 'Mount', 'Mounting...', parent, spin_now=True) success, output, killed = p.reap() if not success: if killed: error_box(parent, 'mount died unexpectedly! Your system is ' 'probably in an inconsistent state. You ' 'should reboot at the earliest opportunity') else: error_box(parent, '%s: Could not mount %s' % (output, device)) return None else: return mountpoint
def __init__(self, *args, **kwargs): Process.__init__(self) if (len(args) > 1 and not any(isinstance(arg, (TableExpression, list, tuple)) for arg in args)): args = (args,) self.args = args suffix = kwargs.pop('suffix', '') fname = kwargs.pop('fname', None) mode = kwargs.pop('mode', 'w') if kwargs: kwarg, _ = kwargs.popitem() raise TypeError("'%s' is an invalid keyword argument for csv()" % kwarg) if fname is not None and suffix: raise ValueError("csv() can't have both 'suffix' and 'fname' " "arguments") if fname is None: suffix = "_" + suffix if suffix else "" fname = "{entity}_{period}" + suffix + ".csv" self.fname = fname if mode not in ('w', 'a'): raise ValueError("csv() mode argument must be either " "'w' (overwrite) or 'a' (append)") self.mode = mode
def __init__(self,context,pid): Process.__init__(self,context,pid) if 'config' in context: self._config = context['config'] #display( OUTPUT_DEBUG, str(self.__config__) ) else: raise Exception('RandomTestProcess','no config found in context.') self._start = time() self._tasktime = 1 self._nextid = 0 if 'process' in self._config: if 'tasktime' in self._config['process']: self._tasktime = float(self._config['process']['tasktime']) if 'period' in self._config['process']: self._period = float(self._config['process']['period']) if 'ctrltasktime' in self._config['process']: self._ctrltasktime = float(self._config['process']['ctrltasktime']) if 'maxtasks' in self._config['process']: self._maxtasks = int(self._config['process']['maxtasks']) self.__queueRandomTestTask__(10) self.determineState()
class Skandal(): '''Change config, capture, process.''' def __init__(self): self.cf = load_config("./scan.ini") self.cap = Capture(self.cf) self.proc = Process(self.cf) def change_config(self): pass def set_cam_position(self): self.cap.set_cam_position() def shot(self): self.cap.shot() def process_images(self): self.proc.get_laser_line() def process_PLY(self): self.proc.get_PLY() def scan(self): self.shot() self.process_images() self.process_PLY()
def start(self): Process.start(self) msg = RegisterExecutorMessage() msg.framework_id.MergeFrom(self.framework_id) msg.executor_id.MergeFrom(self.executor_id) print 'RegisterExecutorMessage', msg return self.send(self.slave, msg)
def __init__(self,context,id): Process.__init__(self,context,id) if 'config' in context: self.__config__ = context['config'] #display( OUTPUT_DEBUG, str(self.__config__) ) else: raise Exception('ExperimentProcess','no config found in context.') repeat = 0 runs = 0 if 'process' in self.__config__: if 'repeat' in self.__config__['process']: self.__repeat__ = int(self.__config__['process']['repeat']) else: raise Exception('ExperimentProcess','no repeats field found in process section of workfile') if 'runs' in self.__config__['process']: runs = int(self.__config__['process']['runs']) else: raise Exception('ExperimentProcess','no runs field found in process section of workfile') else: raise Exception('ExperimentProcess','no process section found in workfile') self.__first__ = False self.__parameters__ = int( runs / self.__repeat__ ) self.display( OUTPUT_MAJOR, '%s initialized for %d parameters totalling %d runs' % (self.__config__['process']['config'], self.__parameters__, runs) ) self.__queueAnalysis__()
def __init__(self, env, me): Process.__init__(self) self.ballot_number = None self.accepted = PValueSet() self.me = me self.env = env self.env.addProc(self)
def __init__(self, name , priority, message, numero): Process.__init__(self, name, 4, priority) self.message = message self.numero = numero self.timer = int(0.02* len(message))+1 # 0 no usa , 1 usa , 2 bloquea self.external = {'Pantalla': 0, 'Audifono': 1, 'Microfono': 0, 'GPS': 0, 'Enviar Info': 1, 'Recibir Info': 1}
def run_simulation(req): ''' launch the ros node that calls gazebo using the appropriate world and urdf model on the commandline, this command would look like this: roslaunch atlas_utils atlas_cga.launch gzworld:=/home/ben/atlas_cga.world gzrobot:=/home/ben/Desktop/gz_walk_dynopt/gz_walking/models/atlas_cga/model2.urdf we grab the output of this node from stdout and analyze it ''' # req may contain a preset non-clashing gazebo simulation ID for parallel execution trqTotal = 0 global world_loc, model_loc cmd = "roslaunch atlas_utils atlas_cga.launch gzworld:=%s gzrobot:=%s" % (world_loc, model_loc) proc = Process(cmd.split(), stdout=True) while True: try: line = proc.readline('stdout', block=True) except KeyboardInterrupt, rospy.ROSInterruptException: proc.kill() break if line and line.startswith('~~~'): trq = float(line.split()[4]) trqTotal += trq*trq if line and line.startswith('=== END '): print line print 'Total Torque Squared', trqTotal break
def __init__(self,config,pid): Process.__init__(self,config,pid) self._interval = 300 self._last = time() - self._interval self._data = None self.display( OUTPUT_VERBOSE, 'status process activated' )
def __init__(self, env, id, config): Process.__init__(self, env, id) self.ballot_number = BallotNumber(0, self.id) self.active = False self.proposals = {} self.timeout = 1.0 self.config = config self.env.addProc(self)
def __init__(self,name,priority,numero,duracion): Process.__init__(self,name,2,priority) self.numero = numero; self.hist = Historial() self.duracion = int(duracion) self.timer = int(duracion) # 0 no usa , 1 usa , 2 bloquea self.external = {'Pantalla': 1, 'Audifono': 2, 'Microfono': 2, 'GPS': 0, 'Enviar Info': 1, 'Recibir Info': 1}
def __init__(self, env, me, leader, acceptors, ballot_number): Process.__init__(self) self.env = env self.me = me self.leader = leader self.acceptors = acceptors self.ballot_number = ballot_number self.env.addProc(self)
def clone(self): """ Clones opponent's source code from Github. """ print Colors.OKBLUE + Colors.BOLD + 'CLONING \t ' + self.name + Colors.ENDC ps = Process(['git', 'clone', self.repo_url, self.clone_path], TIMEOUT_GIT) ps.Run() return ps.output()
def __init__(self, outQueue, errQueue, proc_count=10, headers={}): self.outQueue = outQueue self.errQueue = errQueue self.headers = {} self.timeOut = 5 Process.__init__(self, proc_count)
def __init__(self): Process.__init__(self) self.block_render = True self.ticks = 0 self.page = 0 self.dest_page = 0 self.state = 0 # 0 = zoom out, 1 = control, 2 = zoom in, 3 = change page
def produce(self, recipe, location): process = Process(recipe) # todo: move to process batch = Batch(self, recipe.materials, location) process.materials = batch # TODO: round down duration to days or weeks process.job = location.buy(self, recipe.professional, 1, 10) self.processing.append(process)
def compile(self): """Run the compiler and return the stdout""" flags = [self.compiler] + self.flags + [self.main_file] process = Process(flags) stdout = process.read_stdout() return stdout
def __init__(self, env, id, config): Process.__init__(self, env, id) self.slot_in = self.slot_out = 1 self.proposals = {} self.decisions = {} self.requests = [] self.config = config self.env.addProc(self)
def start(self): Process.start(self) self.zk.start() try: ChildrenWatch(self.zk, '', self.choose) except ZookeeperError: self.send(self.sched, NoMasterDetectedMessage()) self.stop()
def __init__(self, *args, **kwargs): Process.__init__(self) self.args = args self.print_exprs = kwargs.pop('print_exprs', False) if kwargs: kwarg, _ = kwargs.popitem() raise TypeError("'%s' is an invalid keyword argument for show()" % kwarg)
def stop(self, failover=False): if self.connected and not failover: msg = DeactivateFrameworkMessage() msg.framework_id.MergeFrom(self.framework_id) self.send(self.master, msg) if self.detector: self.detector.stop() Process.stop(self)
def start(self): Process.start(self) uri = self.master_uri if uri.startswith('zk://') or uri.startswith('zoo://'): from .detector import MasterDetector self.detector = MasterDetector(uri[uri.index('://') + 3:], self) self.detector.start() else: self.onNewMasterDetectedMessage('master@%s' % uri)
def __init__(self,driver,projId,userCount='*****@*****.**',userPass='******'): ''' 根据项目的projId初始化,默认完成工序0的用户是管理员 :param projId: :param userCount: :param userPass: :return: ''' Process.__init__(self,driver=driver,projId=projId,tpltId=0,userCount=userCount,userPass=userPass)
def __init__(self, env, me, config): Process.__init__(self) self.active = False self.proposals = {} self.env = env self.me = me self.ballot_number = BallotNumber(0, self.me) self.config = config self.env.addProc(self)
def run(self): #wrong operation if(RM.PI == 1): print("wrong operation in ", RM.current_vm.PAGE) Interrupt.kill_vm() #division by zero elif(RM.PI == 2): print("division by zero in ", RM.current_vm.PAGE) Interrupt.kill_vm() #error while loading user's program elif(RM.PI == 3): print("error while trying to load ", Load.filename) #success loading user's program elif(RM.PI == 4): Process.find_by_name("Main").state = State.READY #test elif(RM.PI == 5): Process.find_by_name("Load").state = State.READY #perhaps those two will be optional if(RM.SI == 1): pass #read elif(RM.SI == 2): pass #halt elif(RM.SI == 3): RM.current_vm.state = State.FINISHED RM.current_vm = None RM.TI = 0 #watchdog elif(RM.SI == 4): Interrupt.kill_vm() #timer if(RM.TI == 0): #get all active vms vms = VM.get_active() #for vm in vms: # print(vm, " ", vm.PAGE) if vms != []: #this vm already worked vms[0].state = State.BLOCKED #rotate vms list VM.rotate() #make first ready vms[0].state = State.READY #set timer for vm RM.TI = TIMER_PERIOD #turn on watchdog Process.find_by_name("Watchdog").state = State.READY else: #no need to use watchdog if there is no user's programs Process.find_by_name("Watchdog").state = State.BLOCKED #clear RM.PI = 0 RM.SI = 0 self.state = State.READY
def __init__(self, driver, projId, userCount="*****@*****.**", userPass="******"): """ 根据projId初始化 :param driver: :param projId: :param userCount: :param userPass: :return: """ Process.__init__(self, driver=driver, projId=projId, tpltId=2, userCount=userCount, userPass=userPass)
main = hx8kboard.main() # Generate the SCLK signal (12 MHz/4 = 3 MHz) clk_counter = mantle.Counter(2) sclk = clk_counter.O[-1] # Initialize Modules # ArduCAM cam = ArduCAM() m.wire(main.CLKIN, cam.CLK) m.wire(sclk, cam.SCK) m.wire(main.J2_8, cam.MISO) # Pre-processing process = Process() m.wire(main.CLKIN, process.CLK) m.wire(sclk, process.SCK) m.wire(cam.DATA, process.DATA) m.wire(cam.VALID, process.VALID) # Rescaling image rescale = Rescale() m.wire(main.CLKIN, rescale.CLK) m.wire(process.PXV, rescale.DATA) m.wire(sclk, rescale.SCK) m.wire(process.LOAD, rescale.LOAD) # Feed image to BNN pipeline pipeline = Pipeline() m.wire(sclk, pipeline.CLK)
def main(): # Initialize some variables that will be used later on table_size = 100 scheduling_quantum = 1 num_threads = 1 scheduler = None # First read the input file if (len(sys.argv) != 2) : sys.exit("Usage: " + sys.argv[0] + " <input file>") input = {} with open(sys.argv[1]) as f: input = json.load(f) # Process the options section. # The only current option is the table size if 'options' in input: options = input['options'] if 'table_size' in options: table_size = options['table_size'] # Initialize the process table with the given size table = ProcessTable(table_size) # Figure out which scheduling algorithm we should use if 'schedule' in input and 'scheduling_algorithm' in input['schedule'] : algorithm = input['schedule']['scheduling_algorithm'] if algorithm == 'fcfs' : scheduler = CpuSchedulerFcfs() elif algorithm == 'priority_round_robin': scheduler = CpuSchedulerPriorityRoundRobin() else : sys.exit("Error: schedule/scheduling_algorithm must be specified") # Get the the scheduling quantum, if it's found # (if not, we will just use the default of 1 from above) if 'scheduling_quantum' in input['schedule'] : scheduling_quantum = input['schedule']['scheduling_quantum'] # TODO: There is a new parameter called "num_threads" in the "schedule" # section that indicates how many threads the program should create. # Retrieve that value here and assign it to num_threads. # Load the processes from the input into the process table and the scheduler for process in input['processes']: p = Process(process['process_name'], -1, process['priority'], process['cpu_needed']) p.setProcessId(table.add(p)) scheduler.addProcess(p) # Print the process table one time after all processes are loaded print(table) # Now we will start the threads that will perform the actual execution # of the processes threads = [] for threadNum in range(0, num_threads) : # TODO: Add the appropriate parameters to the ScheduleThread constructor here newThread = ScheduleThread() threads.append(newThread) newThread.start() # Wait for all threads to complete for t in threads: t.join() return 0
def format_partition(parent, device): partitions = [] def add_partition(device, fstype): partitions.append((device, fstype)) partition_list(add_partition, unmounted=True) if not partitions: error_box(parent, 'No unmounted partitions') return False def sort_partition(x, y): return cmp(x[0], y[0]) partitions.sort(sort_partition) dialog = Dialog(parent=parent, title='Format', buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OK, gtk.RESPONSE_OK)) dialog.set_alternative_button_order((gtk.RESPONSE_OK, gtk.RESPONSE_CANCEL)) dialog.set_default_response(gtk.RESPONSE_OK) table = gtk.Table(rows=5, columns=2) set_props(table, row_spacing=6, column_spacing=6, border_width=6, parent=dialog.vbox) widgets = [] for row, widget_type in enumerate(entries): widget = widget_type() label = gtk.Label() label.set_text_with_mnemonic(widget_type.label + ':') label.set_mnemonic_widget(widget) set_props(label, xalign=0.0) table.attach(label, 0, 1, row, row + 1) if widget_type == Device: widget.fill(partitions, device) if isinstance(widget, gtk.Entry): widget.set_activates_default(True) if isinstance(widget, gtk.SpinButton): attach_widget = gtk.HBox() attach_widget.pack_start(widget, expand=False, fill=False) else: attach_widget = widget table.attach(attach_widget, 1, 2, row, row + 1) widgets.append(widget) widgets[0].grab_focus() dialog.show_all() while 1: if dialog.run() != gtk.RESPONSE_OK: dialog.destroy() return False dev = widgets[0].get_device() msg = 'Are you sure you want to format %s?' % dev ask = gtk.MessageDialog(parent=dialog, flags=gtk.DIALOG_DESTROY_WITH_PARENT, type=gtk.MESSAGE_QUESTION, buttons=gtk.BUTTONS_YES_NO, message_format=msg) if ask.run() == gtk.RESPONSE_YES: break else: ask.destroy() command = list(base_command) for widget in widgets[1:]: arg = widget.get_arg() if arg: command.extend(arg) command.append(dev) dialog.destroy() mkfs = Process(command, 'Format', 'Formatting...', parent, spin_now=True) success, output, k = mkfs.reap() if not success: error_box(parent, 'Format error: %s' % output) return False return True
while True: num_burst = math.floor(generator.drand()*100)+1 if num_burst > upper_bound: continue else: break burst = [] io = [] for y in range(num_burst-1): a = checkUpperBound(upper_bound) b = checkUpperBound(upper_bound) burst.append(a) io.append(b) c = checkUpperBound(upper_bound) burst.append(c) process = Process(pid,arr,burst,io) processes.append(process) processes1 = deepcopy(processes) processes2 = deepcopy(processes) processes3 = deepcopy(processes) processes4 = deepcopy(processes) FCFS(processes1, t_cs/2) print() SJF(processes2, t_cs/2) print() SRT(processes3,t_cs/2) print() RR(processes4,t_cs/2,t_slice,rradd) FCFS_burst = 0 FCFS_total_burst = 0
def addProcess(self, isCoordiantor): LogicalRing.activeProcesses.append( Process(self.nextProcess(), isCoordiantor))
def renderScene(self, outFileName, renderDir, mitsubaPath, oiiotoolPath, mtsDir, keepTempFiles, geometryFiles, animation=False, frame=1, verbose=False, renderSettings=None): imageDir = os.path.join(os.path.split(renderDir)[0], 'images') os.chdir(imageDir) sceneName = self.getScenePrefix() imagePrefix = cmds.getAttr("defaultRenderGlobals.imageFilePrefix") if imagePrefix is None: imagePrefix = sceneName writePartialResults = False writePartialResultsInterval = -1 blockSize = 32 threads = 0 if renderSettings: extension = getImageExtension(renderSettings) writePartialResults = cmds.getAttr( "%s.%s" % (renderSettings, "writePartialResults")) writePartialResultsInterval = cmds.getAttr( "%s.%s" % (renderSettings, "writePartialResultsInterval")) blockSize = cmds.getAttr("%s.%s" % (renderSettings, "blockSize")) threads = cmds.getAttr("%s.%s" % (renderSettings, "threads")) print("Render Settings - Partial Results : %s" % writePartialResults) print("Render Settings - Results Interval : %s" % writePartialResultsInterval) print("Render Settings - Block Size : %s" % blockSize) if threads: print("Render Settings - Threads : %s" % threads) if animation: extensionPadding = cmds.getAttr( "defaultRenderGlobals.extensionPadding") logName = os.path.join( imageDir, imagePrefix + "." + str(frame).zfill(extensionPadding) + ".log") imageName = os.path.join( imageDir, imagePrefix + "." + str(frame).zfill(extensionPadding) + "." + extension) else: logName = os.path.join(imageDir, imagePrefix + ".log") imageName = os.path.join(imageDir, imagePrefix + "." + extension) args = [] if verbose: args.append('-v') if writePartialResults: args.extend(['-r', str(writePartialResultsInterval)]) if threads: args.extend(['-p', str(threads)]) args.extend(['-b', str(blockSize), '-o', imageName, outFileName]) if ' ' in mtsDir: env = {"LD_LIBRARY_PATH": str("\"%s\"" % mtsDir)} else: env = {"LD_LIBRARY_PATH": str(mtsDir)} mitsubaRender = Process(description='render an image', cmd=mitsubaPath, args=args, env=env) def renderLogCallback(line): if "Writing image" in line: imageName = line.split("\"")[-2] # Display the render if not cmds.about(batch=True): MitsubaRendererUI.showRender(imageName) mitsubaRender.log_callback = renderLogCallback #mitsubaRender.echo = False mitsubaRender.execute() mitsubaRender.write_log_to_disk(logName, format='txt') print("Render execution returned : %s" % mitsubaRender.status) if oiiotoolPath != "": self.resetImageDataWindow(imageName, oiiotoolPath) if not keepTempFiles: #Delete all of the temp file we just made os.chdir(renderDir) for geometryFile in geometryFiles: try: #print( "Removing geometry : %s" % geometryFile ) os.remove(geometryFile) except: print("Error removing temporary file : %s" % geometryFile) #print( "Removing mitsuba scene description : %s" % outFileName ) os.remove(outFileName) #os.remove(logName) else: print("Keeping temporary files") return imageName
class MemWorker(object): def __init__(self, process_name, end_offset=None, start_offset=None): logger.info('opening process %s ...' % process_name) self.process = Process() self.process.open_debug_from_name(process_name) si = self.process.GetSystemInfo() if end_offset: self.end_offset = end_offset else: self.end_offset = si.lpMaximumApplicationAddress if start_offset: self.start_offset = start_offset else: self.start_offset = si.lpMinimumApplicationAddress def Address(self, value, default_type='uint'): """ wrapper to instanciate an Address class for the memworker.process""" return Address(value, process=self.process, default_type=default_type) def get_process(self): return self.process def search_address(self, address): address = int(address) for m in self.process.list_modules(): for addr in self.mem_search(address, ftype='ulong', start_offset=m.modBaseAddr, end_offset=m.modBaseSize): logger.debug('found module %s => addr %s' % (m.szModule, addr)) def umem_replace(self, regex, replace): """ like search_replace_mem but works with unicode strings """ regex = utils.re_to_unicode(regex) replace = replace.encode('utf-16-le') return self.mem_replace(re.compile(regex, re.UNICODE), replace) def mem_replace(self, regex, replace): """ search memory for a pattern and replace all found occurrences """ allWritesSucceed = True for start_offset in self.mem_search(regex, ftype='re'): if self.process.write_bytes(start_offset, replace) == 1: logger.debug('Write at offset %s succeeded !' % start_offset) else: allWritesSucceed = False logger.debug('Write at offset %s failed !' % start_offset) return allWritesSucceed def umem_search(self, regex): """ like mem_search but works with unicode strings """ regex = utils.re_to_unicode(regex) for i in self.mem_search(str(regex), ftype='re'): yield i def group_search(self, group, start_offset=None, end_offset=None): regex = '' for value, type in group: if type == 'f' or type == 'float': f = struct.pack('<f', float(value)) regex += '..' + f[2:4] else: raise NotImplementedError('unknown type %s' % type) return self.mem_search(regex, ftype='re', start_offset=start_offset, end_offset=end_offset) def search_address(self, addr): a = '%08X' % addr logger.debug('searching address %s' % a) regex = '' for i in range(len(a) - 2, -1, -2): regex += binascii.unhexlify(a[i:i + 2]) return self.mem_search(re.escape(regex), ftype='re') def mem_search(self, value, ftype='match', protec=PAGE_READWRITE | PAGE_READONLY, start_offset=None, end_offset=None): """ iterator returning all indexes where the pattern has been found """ ftype = ftype.lower().strip() if type(value) is list: ftype = 'group' if ftype == 're': if type(value) is str: regex = re.compile(value) else: regex = value if start_offset is None: offset = self.start_offset else: offset = start_offset if end_offset is None: end_offset = self.end_offset if ftype == 'float': structtype, structlen = utils.type_unpack(ftype) elif ftype != 'match' and ftype != 'group' and ftype != 're': structtype, structlen = utils.type_unpack(ftype) value = struct.pack(structtype, value) while True: if offset >= end_offset: break totalread = 0 mbi = self.process.VirtualQueryEx(offset) offset = mbi.BaseAddress chunk = mbi.RegionSize protect = mbi.Protect state = mbi.State if state & MEM_FREE or state & MEM_RESERVE: offset += chunk continue if protec: if not protect & protec or protect & PAGE_NOCACHE or protect & PAGE_WRITECOMBINE or protect & PAGE_GUARD: offset += chunk continue b = '' #b = self.process.read_bytes(offset, chunk) try: b = self.process.read_bytes(offset, chunk) totalread = len(b) except Exception as e: #print(e) offset += chunk continue if b: if ftype == 're': duplicates_cache = set() for res in regex.findall(b): index = b.find(res) while index != -1: soffset = offset + index if soffset not in duplicates_cache: duplicates_cache.add(soffset) yield self.Address(soffset, 'bytes') index = b.find(res, index + len(res)) elif ftype == 'float': for index in range(0, len(b)): try: tmpval = struct.unpack(structtype, b[index:index + 4])[0] if int(value) == int(tmpval): soffset = offset + index yield self.Address(soffset, 'float') except Exception as e: print(e) else: index = b.find(value) while index != -1: soffset = offset + index yield self.Address(soffset, 'bytes') index = b.find(value, index + 1) offset += totalread
def train(model=None, re_train=False): word_model = CustomEmbedding( re_train=re_train ) # To prevent creating embeddings if re_train is True train_set, valid_set, indexes = word_model.train_set, word_model.valid_set, word_model.indexes w2idx, la2idx = indexes['w2idx'], indexes['la2idx'] idx2w, idx2la = indexes['idx2w'], indexes['idx2la'] n_classes = len(idx2la) n_vocab = len(idx2w) train_x, train_label = train_set valid_x, valid_label = valid_set log("Processing word indexes... ") words_val = [list(map(lambda x: idx2w[x], w)) for w in valid_x] groundtruth_val = [list(map(lambda x: idx2la[x], y)) for y in valid_label] log("Done processing word indexes!") if re_train == False: ''' DEFINE MODEL ''' model = Sequential() model.add(word_model.EmbeddingLayer()) model.add(Conv1D(128, 5, padding="same", activation='relu')) model.add(Dropout(Config.DROPOUT)) model.add( Bidirectional( LSTM(units=Config.EMBEDDING_SIZE, dropout=Config.DROPOUT, recurrent_dropout=Config.DROPOUT, kernel_initializer=he_normal(), return_sequences=True))) model.add(SeqSelfAttention(attention_activation='sigmoid')) # model.add(GRU(units=Config.EMBEDDING_SIZE, # dropout=Config.DROPOUT, # recurrent_dropout=Config.DROPOUT, # kernel_initializer=he_normal(), # return_sequences=True)) model.add(TimeDistributed(Dense(n_classes, activation='softmax'))) model.add(CRF(n_classes, sparse_target=False, learn_mode='join')) model.compile(Config.OPTIMIZER, Config.LOSS, metrics=[crf_viterbi_accuracy]) process = Process(model) max_f1 = 0 try: for i in range(Config.N_EPOCHS): log("Epoch " + str(i + 1), display=False) highlight('violet', 'Epoch ' + str(i + 1)) partition(80) log("Training ") process.train(train_set) log("Validating ") predword_val = process.validate(valid_set) # Accuracy tests here using (predword_val, groundtruth_val, words_val) and save best model metrics = conlleval(predword_val, groundtruth_val, words_val, 'diff.txt') log('Precision = {}, Recall = {}, F1 = {}'.format( metrics['precision'], metrics['recall'], metrics['f1'])) if metrics['f1'] > max_f1: max_f1 = metrics['f1'] process.save('trained_model_' + str(Config.FILE_PATTERN) + '_' + str(max_f1)) log("New model saved!", display=False) highlight('white', 'Best validation F1 score : ' + str(max_f1)) log('Best validation F1 score : ' + str(max_f1), display=False) log('Cleaning /trained_model folder...') clean() log('Removed all other saved models, kept the best model only!') except KeyboardInterrupt: # If in case ctrl + c pressed, needs to clean up and exit log("\nTraining interrupted with ctrl + c ...") log('Cleaning /trained_model folder...') clean() log('Removed all other saved models, kept the best model only!') sys.exit()
def test(process=None, sentences=None, read_file=True): start = time.time() if read_file: best_model_filename = getBestSavedModelToTest() process = Process() # Load trained model process.load(best_model_filename) f = open('tests/test_sentances.txt', 'r') sentences = f.readlines() f.close() # Clean loaded sentances from file - removing '\n' from each sentance sentences = process_sentances(sentences) f = open('tests/slots_' + str(Config.FILE_PATTERN) + '.txt', 'w') arr_slots = [] for sentence in sentences: words, BIO = process.test(sentence) # Test on sentance f.write(str(sentence) + "\n\n") slots = {} slot_type = None value = '' prev_slot_type = None for idx, slot in enumerate(BIO): if slot != 'O': f.write(str(words[idx]) + " - " + str(slot) + "\n") ''' Grouping the slots san - B-toloc.city_name francisco - I-toloc.city_name ------------------------------ Returns -> {'toloc.city_name': ['san francisco']} ''' slot_type = slot.split("-")[1] pos = slot.split("-")[0] if pos == 'B': if slot_type not in slots: if prev_slot_type is not None: slots[prev_slot_type].append(value.strip()) value = words[idx] + ' ' slots[slot_type] = [] prev_slot_type = slot_type else: slots[prev_slot_type].append(value.strip()) value = words[idx] + ' ' else: # pos == 'I' value += words[idx] + ' ' slots[slot_type].append(value.strip()) log('Slots compiled into groups...') f.write(partition(80) + "\n") arr_slots.append(slots) end = time.time() f.close() highlight('green', 'Output can be found in `slots.txt` file!') response_time = end - start if not read_file: return (response_time, arr_slots[0] ) # As we're sending only one sentance in API URL
def __init__(self, options): self.options = options self.process = Process()
# output directory summarize_out = config.summarize['out_dir'] if not path.exists(summarize_out): os.mkdir(summarize_out) # get args arg_order = ["regressors", "mask", "tvals", "out_dir"] args = [config.summarize[arg] for arg in arg_order] arg3 = "" for tval_info in eval(args[2]): arg3 += "%s %s " % tuple(tval_info.split()) args[2] = arg3[0:-1] # execute command p = Process("./glm_summarize.R %s" % " ".join(args)) print p.stdout if p.pid != 0: print p.stderr raise Exception("glm_summarize.R failed" ) # note will fail but seems like stuff is ok ## Combine GLM Summary with MDMR from glob import glob arg_order = ["mask", "mdmr_zstats", "glm_summary", "out_dir"] args = [config.dataframe[arg] for arg in arg_order] args[2] = " ".join(glob(args[2])) p = Process("./glm_dataframe.R %s" % " ".join(args)) print p if p.pid != 0: print p.stderr
def __init__(self, filename): self.process = Process() # Creats an object self.reader = FileReader(filename, self.process) # Creates an object self.reader.run() # call the run method self.process.totalCost()
class AutoCraft: def __init__(self, json_data=None, mode=None): self.opt_help = 0 self.opt_foodbuff = 0 self.opt_potbuff = 0 self.opt_collectable = 0 self.opt_limit = 0 self.opt_notify = 0 self.opt_repair = 0 self.flask = 0 if mode == "Flask": self.flask = 1 self.food_time = 0 self.craft_amount = 0 self.pot_time = 0 self.json_data = json_data if self.json_data: self.ffxiv = Process(self.json_data["process_name"]) self.macro_amount = self.json_data["macro_amount"] self.timer_list = ["m1", "m2", "m3", "m4"] self.button_list = ["k1", "k2", "k3", "k4"] else: print("ERROR: JSON file not found. Try running setup.py") if self.flask == 0: self.crafter() def args_checker(self): accepted_args = [ "repair", "foodbuff", "potbuff", "collectable", "limit", "notify", "--help" ] args = sys.argv[1:] all_ok = 0 if "flask" in set(args): self.flask = 1 try: with open("automation_settings.json", "r") as f: data = json.load(f) self.food_time = data["temp_food_time"] self.craft_amount = data["temp_craft_amount"] self.pot_time = data["temp_pot_time"] except FileNotFoundError: print("ERROR: Requires automation_settings.json") quit() for arg in args: if arg == "flask": all_ok += 1 if arg == "--help": self.opt_help = 1 all_ok += 1 if arg == "repair": self.opt_repair = 1 all_ok += 1 if arg == "foodbuff": self.opt_foodbuff = 1 all_ok += 1 if arg == "potbuff": self.opt_potbuff = 1 all_ok += 1 if arg == "collectable": self.opt_collectable = 1 all_ok += 1 if arg == "limit": self.opt_limit = 1 all_ok += 1 if arg == "notify": self.opt_notify = 1 all_ok += 1 if all_ok != len(args): print("Incorrect argument(s). Choose from this list:\n{}\n".format( accepted_args)) quit() def notifier(self, title=None, message=None): toaster = ToastNotifier() toaster.show_toast(title, message, duration=7) def int_validator(self, value=None): if value != None: try: value = int(value) except ValueError: print( "Error: Input needs to be a number. Restart ffxiv_jidoucraft.py." ) quit() return value def time_estimater(self, json_data, craft_amount): m_time = 0 for i in range(self.macro_amount): m_time += self.json_data[self.timer_list[i]] m_time += 8 # Numpad 0 presses estimated amount of time it takes res = (craft_amount * m_time) / 60 print(" -> Estimated completion time: {:.2f} minutes.".format(res)) def auto_end(self, craft_count, craft_amount): print("Crafted {} times, quitting program.".format(craft_amount)) if self.opt_notify == 1: notify_message = "Crafted {} times.".format(craft_amount) self.notifier("Crafting Batch Finished", notify_message) quit() def auto_foodbuff(self): print(" -> Standing up by pressing ESC") for esc_counter in range(12): self.ffxiv.press_key("{VK_ESCAPE}") sleep(2) print(" -> Eating food") for food_counter in range(2): self.ffxiv.press_key(self.json_data["food_key"]) sleep(7) def auto_potbuff(self): print(" -> Standing up by pressing ESC") for esc_counter in range(12): self.ffxiv.press_key("{VK_ESCAPE}") sleep(2) print(" -> Eating pot") for food_counter in range(2): self.ffxiv.press_key(self.json_data["pot_key"]) sleep(7) def auto_repair(self): print(" -> Standing up by pressing ESC") for esc_counter in range(12): self.ffxiv.press_key("{VK_ESCAPE}") sleep(2) print(" -> Pressing repair menu") self.ffxiv.press_key("q") sleep(0.1) self.ffxiv.press_key("{VK_NUMPAD0}") sleep(0.1) self.ffxiv.press_key("{VK_NUMPAD6}") self.ffxiv.press_key("{VK_NUMPAD0}") self.ffxiv.press_key("{VK_ESCAPE}") sleep(8) def crafter(self): if self.flask == 0: self.args_checker() if self.opt_help == 1: with open("help.txt", "r") as f: helper = f.read() print(helper) quit() just_buffed = 0 repair_limiter = 0 foodbuff = 0 food_time = None food_limiter = 0 potbuff = 0 pot_time = None pot_limiter = 0 collectable = 0 craft_counter = 0 craft_amount = None notify = 0 if self.opt_notify == 1: notify = 1 if self.opt_foodbuff == 1: print("Adding foodbuffer to crafting automation...") if self.flask == 1: food_time = self.food_time else: food_time = input("What is your current food buff time?\n") food_time = self.int_validator(food_time) foodbuff = 1 if self.opt_potbuff == 1: print("Adding potbuffer to crafting automation...") if self.flask == 1: pot_time = self.pot_time else: pot_time = input("What is your current pot buff time?\n") pot_time = self.int_validator(pot_time) potbuff = 1 if self.opt_collectable == 1: print("Collectable mode activated...") if self.opt_limit == 1: print("How many crafts do you want to limit to?") if self.flask == 1: craft_amount = self.craft_amount else: craft_amount = input() try: craft_amount = int(craft_amount) except ValueError: print( " -> ERROR: Enter a number. Run ffxiv_jidoucraft.py again." ) quit() self.time_estimater(self.json_data, craft_amount) if self.opt_repair == 1: print("Auto repair activated...") # Regular auto-craft print("Starting crafting automation...") print("TO QUIT: PRESS CTRL+C") while True: if self.opt_collectable == 1: if craft_counter != 0: print(" -> Collectable mode selected, please wait.") sleep(5) self.ffxiv.press_key("{VK_ESCAPE}") sleep(2) food_limiter += 7 pot_limiter += 7 repair_limiter += 7 if self.opt_limit == 1: if craft_counter >= craft_amount: self.auto_end(craft_counter, craft_amount) print("Craft #{}".format(craft_counter + 1)) if self.opt_repair == 1: if repair_limiter >= 2280: self.auto_repair() repair_limiter = 0 if foodbuff == 1: if food_limiter >= food_time: self.auto_foodbuff() food_limiter = 0 food_time = 1770 # Default 30 min pot buff minus 30 seconds just_buffed += 1 if potbuff == 1: if pot_limiter >= pot_time: self.auto_potbuff() pot_limiter = 0 pot_time = 870 # Default 15 min pot buff minus 30 seconds just_buffed += 1 if just_buffed >= 1: print(" -> Selecting Craft") self.ffxiv.press_key(self.json_data["craft_key"]) just_buffed = 0 sleep(0.01) print(" -> Pressing + Selecting 'Synthesis'") for i in range(4): self.ffxiv.press_key("{VK_NUMPAD0}") sleep(0.5) food_limiter += 2 pot_limiter += 2 repair_limiter += 2 for i in range(self.macro_amount): self.ffxiv.press_key(self.json_data[self.button_list[i]]) print(" -> Pressing Macro {}".format(i + 1)) print(" -> Waiting {} seconds.".format( self.json_data[self.timer_list[i]])) sleep(self.json_data[self.timer_list[i]]) food_limiter += self.json_data[self.timer_list[i]] pot_limiter += self.json_data[self.timer_list[i]] repair_limiter += self.json_data[self.timer_list[i]] sleep(3) food_limiter += 3 pot_limiter += 3 repair_limiter += 3 craft_counter += 1
def __init__(self, process=None): self.process = process or Process(env=self.get_env()) self.python = Python()
def main(): process_list = [Process(address, verbose=False) for address in ADDRESS_LIST] driver = Driver(HOME) driver.verbose=True easy_least_squares(driver)
def main(run_load_from_file=False): # load MNIST images images, labels = dataset.load_train_images() # config opt = Operation() opt.check_dir(config.ckpt_dir, is_restart=False) opt.check_dir(config.log_dir, is_restart=True) # setting max_epoch = 510 num_trains_per_epoch = 500 batch_size_l = 100 batch_size_u = 100 # create semi-supervised split num_validation_data = 10000 num_labeled_data = 100 num_types_of_label = 10 training_images_l, training_labels_l, training_images_u, validation_images, validation_labels = dataset.create_semisupervised( images, labels, num_validation_data, num_labeled_data, num_types_of_label) # training with tf.device(config.device): h = build_graph() sess_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=True) sess_config.gpu_options.allow_growth = True sess_config.gpu_options.per_process_gpu_memory_fraction = 0.9 saver = tf.train.Saver(max_to_keep=2) with tf.Session(config=sess_config) as sess: ''' Load from checkpoint or start a new session ''' if run_load_from_file: saver.restore(sess, tf.train.latest_checkpoint(config.ckpt_dir)) training_epoch_loss, _ = pickle.load( open(config.ckpt_dir + '/pickle.pkl', 'rb')) else: sess.run(tf.global_variables_initializer()) training_epoch_loss = [] # Recording loss per epoch process = Process() for epoch in range(max_epoch): process.start_epoch(epoch, max_epoch) ''' Learning rate generator ''' learning_rate = opt.ladder_learning_rate(epoch + len(training_epoch_loss)) # Recording loss per iteration training_loss_set = [] sum_loss_reconstruction = 0 sum_loss_supervised = 0 sum_loss_discrminator = 0 sum_loss_generator = 0 process_iteration = Process() for i in range(num_trains_per_epoch): process_iteration.start_epoch(i, num_trains_per_epoch) # sample from data distribution images_l, label_onehot_l, label_id_l = dataset.sample_labeled_data( training_images_l, training_labels_l, batch_size_l) images_u = dataset.sample_unlabeled_data( training_images_u, batch_size_u) # reconstruction_phase _, loss_reconstruction = sess.run([h.opt_r, h.loss_r], feed_dict={ h.x: images_u, h.lr: learning_rate }) z_true_u = sampler.gaussian(batch_size_u, config.ndim_z, mean=0, var=1) y_true_u = sampler.onehot_categorical(batch_size_u, config.ndim_y) # adversarial phase for discriminator _, loss_discriminator_y = sess.run([h.opt_dy, h.loss_dy], feed_dict={ h.x: images_u, h.y: y_true_u, h.lr: learning_rate }) _, loss_discriminator_z = sess.run([h.opt_dz, h.loss_dz], feed_dict={ h.x: images_u, h.z: z_true_u, h.lr: learning_rate }) loss_discriminator = loss_discriminator_y + loss_discriminator_z # adversarial phase for generator _, loss_generator_y, loss_generator_z = sess.run( [h.opt_e, h.loss_gy, h.loss_gz], feed_dict={ h.x: images_u, h.lr: learning_rate }) loss_generator = loss_generator_y + loss_generator_z # supervised phase _, loss_generator_supervised = sess.run([h.opt_ey, h.loss_ey], feed_dict={ h.x: images_l, h.y_s: label_id_l, h.lr: learning_rate }) training_loss_set.append([ loss_reconstruction, loss_discriminator, loss_discriminator_y, loss_discriminator_z, loss_generator, loss_generator_z, loss_generator_y, loss_generator_supervised ]) sum_loss_reconstruction += loss_reconstruction / batch_size_u sum_loss_discrminator += loss_discriminator sum_loss_generator += loss_generator sum_loss_supervised += loss_generator_supervised if i % 1000 == 0: process_iteration.show_table_2d( i, num_trains_per_epoch, { 'reconstruction': sum_loss_reconstruction / (i + 1), 'discriminator': sum_loss_discrminator / (i + 1), 'generator': sum_loss_generator / (i + 1), 'supervise': sum_loss_supervised / (i + 1) }) # In end of epoch, summary the loss average_training_loss_per_epoch = np.mean( np.array(training_loss_set), axis=0) # validation phase images_v_segments = np.split(validation_images, num_validation_data // 1000) labels_v_segments = np.split(validation_labels, num_validation_data // 1000) sum_accuracy = 0 for images_v, labels_v in zip(images_v_segments, labels_v_segments): y_v = sess.run(h.y_r, feed_dict={h.x: images_v}) prediction = np.argmax(y_v, 1) equality = np.equal(prediction, labels_v) accuracy = np.mean(equality.astype(np.float32)) sum_accuracy += accuracy validation_accuracy = sum_accuracy / len(images_v_segments) # append validation accuracy to the training loss average_loss_per_epoch = np.append(average_training_loss_per_epoch, validation_accuracy) training_epoch_loss.append(average_loss_per_epoch) loss_name_per_epoch = [ 'reconstruction', 'discriminator', 'discriminator_y', 'discriminator_z', 'generator', 'generator_z', 'generator_y', 'supervised', 'validation_accuracy' ] if epoch % 1 == 0: process.show_bar( epoch, max_epoch, { 'loss_r': average_loss_per_epoch[0], 'loss_d': average_loss_per_epoch[1], 'loss_g': average_loss_per_epoch[4], 'loss_v': average_loss_per_epoch[8] }) plt.tile_images(sess.run(h.x_, feed_dict={h.x: images_u}), dir=config.log_dir, filename='x_rec_epoch_{}'.format( str(epoch).zfill(3))) if epoch % 10 == 0: saver.save(sess, os.path.join(config.ckpt_dir, 'model_ckptpoint'), global_step=epoch) pickle.dump((training_epoch_loss, loss_name_per_epoch), open(config.ckpt_dir + '/pickle.pkl', 'wb')) plt.plot_double_scale_trend(config.ckpt_dir)
def start(self): Process.start(self) self.isolation.initialize(self) self.register() # master detector TODO
class GUI(QMainWindow, QThread): def __init__(self): super(GUI, self).__init__() self.initUI() self.webcam = Webcam() self.video = Video() self.input = self.webcam self.dirname = "" print("Input: webcam") self.statusBar.showMessage("Input: webcam", 5000) self.btnOpen.setEnabled(False) self.process = Process() self.status = False self.frame = np.zeros((10, 10, 3), np.uint8) #self.plot = np.zeros((10,10,3),np.uint8) self.bpm = 0 def initUI(self): #set font font = QFont() font.setPointSize(16) #widgets self.btnStart = QPushButton("Start", self) self.btnStart.move(440, 520) self.btnStart.setFixedWidth(200) self.btnStart.setFixedHeight(50) self.btnStart.setFont(font) self.btnStart.clicked.connect(self.run) self.btnOpen = QPushButton("Open", self) self.btnOpen.move(230, 520) self.btnOpen.setFixedWidth(200) self.btnOpen.setFixedHeight(50) self.btnOpen.setFont(font) self.btnOpen.clicked.connect(self.openFileDialog) self.cbbInput = QComboBox(self) self.cbbInput.addItem("Webcam") self.cbbInput.addItem("Video") self.cbbInput.setCurrentIndex(0) self.cbbInput.setFixedWidth(200) self.cbbInput.setFixedHeight(50) self.cbbInput.move(20, 520) self.cbbInput.setFont(font) self.cbbInput.activated.connect(self.selectInput) #------------------- self.lblDisplay = QLabel(self) #label to show frame from camera self.lblDisplay.setGeometry(10, 10, 640, 480) self.lblDisplay.setStyleSheet("background-color: #000000") self.lblROI = QLabel(self) #label to show face with ROIs self.lblROI.setGeometry(660, 10, 200, 200) self.lblROI.setStyleSheet("background-color: #000000") self.lblHR = QLabel(self) #label to show HR change over time self.lblHR.setGeometry(900, 20, 300, 40) self.lblHR.setFont(font) self.lblHR.setText("Frequency: ") self.lblHR2 = QLabel(self) #label to show stable HR self.lblHR2.setGeometry(900, 70, 300, 40) self.lblHR2.setFont(font) self.lblHR2.setText("Heart rate: ") # self.lbl_Age = QLabel(self) #label to show stable HR # self.lbl_Age.setGeometry(900,120,300,40) # self.lbl_Age.setFont(font) # self.lbl_Age.setText("Age: ") # self.lbl_Gender = QLabel(self) #label to show stable HR # self.lbl_Gender.setGeometry(900,170,300,40) # self.lbl_Gender.setFont(font) # self.lbl_Gender.setText("Gender: ") #dynamic plot self.signal_Plt = pg.PlotWidget(self) self.signal_Plt.move(660, 220) self.signal_Plt.resize(480, 192) self.signal_Plt.setLabel('bottom', "Signal") self.fft_Plt = pg.PlotWidget(self) self.fft_Plt.move(660, 425) self.fft_Plt.resize(480, 192) self.fft_Plt.setLabel('bottom', "FFT") self.timer = pg.QtCore.QTimer() self.timer.timeout.connect(self.update) self.timer.start(200) self.statusBar = QStatusBar() self.statusBar.setFont(font) self.setStatusBar(self.statusBar) #event close self.c = Communicate() self.c.closeApp.connect(self.close) #event change combobox index #config main window self.setGeometry(100, 100, 1160, 640) #self.center() self.setWindowTitle("Heart rate monitor") self.show() def update(self): #z = np.random.normal(size=1) #u = np.random.normal(size=1) self.signal_Plt.clear() self.signal_Plt.plot(self.process.samples[20:], pen='g') self.fft_Plt.clear() self.fft_Plt.plot(np.column_stack( (self.process.freqs, self.process.fft)), pen='g') def center(self): qr = self.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) def closeEvent(self, event): reply = QMessageBox.question(self, "Message", "Are you sure want to quit", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if reply == QMessageBox.Yes: event.accept() self.input.stop() cv2.destroyAllWindows() else: event.ignore() def selectInput(self): self.reset() if self.cbbInput.currentIndex() == 0: self.input = self.webcam print("Input: webcam") self.btnOpen.setEnabled(False) self.statusBar.showMessage("Input: webcam", 5000) elif self.cbbInput.currentIndex() == 1: self.input = self.video print("Input: video") self.btnOpen.setEnabled(True) self.statusBar.showMessage("Input: video", 5000) def mousePressEvent(self, event): self.c.closeApp.emit() # def make_bpm_plot(self): # plotXY([[self.process.times[20:], # self.process.samples[20:]], # [self.process.freqs, # self.process.fft]], # labels=[False, True], # showmax=[False, "bpm"], # label_ndigits=[0, 0], # showmax_digits=[0, 1], # skip=[3, 3], # name="Plot", # bg=None) # fplot = QImage(self.plot, 640, 280, QImage.Format_RGB888) # self.lblPlot.setGeometry(10,520,640,280) # self.lblPlot.setPixmap(QPixmap.fromImage(fplot)) def key_handler(self): """ cv2 window must be focused for keypresses to be detected. """ self.pressed = waitKey(1) & 255 # wait for keypress for 10 ms if self.pressed == 27: # exit program on 'esc' print("[INFO] Exiting") self.webcam.stop() sys.exit() def openFileDialog(self): self.dirname = QFileDialog.getOpenFileName( self, 'OpenFile', r"C:\Users\uidh2238\Desktop\test videos") self.statusBar.showMessage("File name: " + self.dirname, 5000) def reset(self): self.process.reset() self.lblDisplay.clear() self.lblDisplay.setStyleSheet("background-color: #000000") def main_loop(self): frame = self.input.get_frame() self.process.frame_in = frame self.process.run() #cv2.imshow("Processed", frame) self.frame = self.process.frame_out #get the frame to show in GUI self.f_fr = self.process.frame_ROI #get the face to show in GUI #print(self.f_fr.shape) self.bpm = self.process.bpm #get the bpm change over the time self.frame = cv2.cvtColor(self.frame, cv2.COLOR_RGB2BGR) cv2.putText(self.frame, "FPS " + str(float("{:.2f}".format(self.process.fps))), (20, 460), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 255, 255), 2) img = QImage(self.frame, self.frame.shape[1], self.frame.shape[0], self.frame.strides[0], QImage.Format_RGB888) self.lblDisplay.setPixmap(QPixmap.fromImage(img)) self.f_fr = cv2.cvtColor(self.f_fr, cv2.COLOR_RGB2BGR) #self.lblROI.setGeometry(660,10,self.f_fr.shape[1],self.f_fr.shape[0]) self.f_fr = np.transpose(self.f_fr, (0, 1, 2)).copy() f_img = QImage(self.f_fr, self.f_fr.shape[1], self.f_fr.shape[0], self.f_fr.strides[0], QImage.Format_RGB888) self.lblROI.setPixmap(QPixmap.fromImage(f_img)) self.lblHR.setText("Freq: " + str(float("{:.2f}".format(self.bpm)))) if self.process.bpms.__len__() > 50: if ( max(self.process.bpms - np.mean(self.process.bpms)) < 5 ): #show HR if it is stable -the change is not over 5 bpm- for 3s self.lblHR2.setText( "Heart rate: " + str(float("{:.2f}".format(np.mean(self.process.bpms)))) + " bpm") #self.lbl_Age.setText("Age: "+str(self.process.age)) #self.lbl_Gender.setText("Gender: "+str(self.process.gender)) #self.make_bpm_plot()#need to open a cv2.imshow() window to handle a pause #QtTest.QTest.qWait(10)#wait for the GUI to respond self.key_handler() #if not the GUI cant show anything def run(self, input): self.reset() input = self.input self.input.dirname = self.dirname if self.input.dirname == "" and self.input == self.video: print("choose a video first") self.statusBar.showMessage("choose a video first", 5000) return if self.status == False: self.status = True input.start() self.btnStart.setText("Stop") self.cbbInput.setEnabled(False) self.btnOpen.setEnabled(False) self.lblHR2.clear() while self.status == True: self.main_loop() elif self.status == True: self.status = False input.stop() self.btnStart.setText("Start") self.cbbInput.setEnabled(True)
class Demonstrator(): ''' Runs both files in one go process and filereader ''' def __init__(self, filename): self.process = Process() # Creats an object self.reader = FileReader(filename, self.process) # Creates an object self.reader.run() # call the run method self.process.totalCost() def displayRow(self): return self.process.getRowCount() def displayTotalSum(self): return '\u00A3%0.2f' % (self.process.getTotalSum()) def displayCostDate(self): return self.process.dateList() def displayStaff_Names(self): return self.process.getListOfName() def displayStaffCost(self): return self.process.getStaffCost() def displaySpendings(self, ID): return self.process.spendingType(ID) #Last Method Run def total_Cost(self): return self.process.totalCost() #Must be run after def displayStaffSum(self): return self.process.getStaffCost() def staffName(self): return self.process.getStaff()
import options from process import Process import art import list if __name__ == '__main__': art.header() option = options.get() tests = option[0] resources = option[2] if tests: process = Process(option[1]) process.run(tests, option[1]) elif resources: list.simple(option[3], resources) else: print( "execute_tests.py: error: invalid input - check specified resource exists and is spelled correctly" )
import cv2 import numpy as np from process import Process import matplotlib.pyplot as plt from skimage import morphology, filters from skan import draw, skeleton_to_csgraph, Skeleton, summarize kernel = np.ones((5, 5), np.uint8) imgb = cv2.imread('./img/Coral before.png', cv2.IMREAD_UNCHANGED) #h, w = imgb.shape[:2] #imgb = cv2.resize(imgb,(h,h), interpolation=cv2.INTER_CUBIC) pbef = Process(imgb) pbef.lowp = np.array([150, 60, 100]) pbef.highp = np.array([170, 255, 255]) pbef.loww = np.array([90, 15, 150]) pbef.highw = np.array([115, 255, 255]) maskb = pbef.mask(kernel) resb = pbef.res(maskb) maskb = cv2.blur(maskb, (5, 5)) binaryb = maskb > filters.threshold_otsu(maskb) skeletonb = morphology.skeletonize(binaryb) fig, ax = plt.subplots() draw.overlay_skeleton_2d(maskb, skeletonb, dilate=1, axes=ax) #graphb = csgraph_from_masked(binaryb) #plt.imshow(graphb) gb, cb, db = skeleton_to_csgraph(skeletonb) draw.overlay_skeleton_networkx(gb, cb, image=maskb) branch_datab = summarize(Skeleton(skeletonb))
class MyGame(arcade.Window): BUTTON_NAMES = [ "A", "B", "X", "Y", "LB", "RB", "VIEW", "MENU", "LSTICK", "RSTICK", None, None, None, None, None, None, ] # ---------------------------------- # PRIVATE METHODS FOR INPUT MANAGEMENT # ---------------------------------- def __onButtonPressed(self, _gamepad, button): idx = self.gamepads[_gamepad] self.onButtonPressed(idx, MyGame.BUTTON_NAMES[button]) def __onButtonReleased(self, _gamepad, button): idx = self.gamepads[_gamepad] self.onButtonReleased(idx, MyGame.BUTTON_NAMES[button]) def __onCrossMove(self, _gamepad, x, y): idx = self.gamepads[_gamepad] self.onCrossMove(idx, x, -y) def __onAxisMove(self, _gamepad, axis, value): idx = self.gamepads[_gamepad] self.onAxisMove(idx, axis, value) # ---------------------------------- # CONSTRUCTOR # ---------------------------------- def __init__(self, width, height, title): #init application window super().__init__(width, height, title) # init process object self.process = Process() # set application window background color arcade.set_background_color(arcade.color.BOTTLE_GREEN) # Store gamepad list self.gamepads = arcade.get_joysticks() # check every connected gamepad if self.gamepads: for g in self.gamepads: #link all gamepad callbacks to the current class methods g.open() g.on_joybutton_press = self.__onButtonPressed g.on_joybutton_release = self.__onButtonReleased g.on_joyhat_motion = self.__onCrossMove g.on_joyaxis_motion = self.__onAxisMove # transform list into a dictionary to get its index faster self.gamepads = { self.gamepads[idx]: idx for idx in range(len(self.gamepads)) } else: print("There are no Gamepad connected !") self.gamepads = None # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ # SETUP your game here # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ def setup(self): #- - - - - - - - - - - - - - - - - - - - - - - - -# self.process.setup() #- - - - - - - - - - - - - - - - - - - - - - - - -# # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ # DRAW your game elements here # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ def on_draw(self): #- - - - - - - - - - - - - - - - - - - - - - - - -# arcade.start_render() self.process.draw() #- - - - - - - - - - - - - - - - - - - - - - - - -# # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ # UPDATE your game model here # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ def update(self, delta_time): #- - - - - - - - - - - - - - - - - - - - - - - - -# self.process.update(delta_time) #- - - - - - - - - - - - - - - - - - - - - - - - -# # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ # KEY PRESSED events # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ def on_key_press(self, key, modifiers): #- - - - - - - - - - - - - - - - - - - - - - - - -# # Close application if ESCAPE key is hit if key == arcade.key.ESCAPE: self.close() self.process.onKeyEvent(key, True) #- - - - - - - - - - - - - - - - - - - - - - - - -# # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ # KEY RELEASED events # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ def on_key_release(self, key, modifiers): #- - - - - - - - - - - - - - - - - - - - - - - - -# self.process.onKeyEvent(key, False) #- - - - - - - - - - - - - - - - - - - - - - - - -# # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ # GAMEPAD BUTTON PRESSED events # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ def onButtonPressed(self, gamepadNum, buttonNum): #- - - - - - - - - - - - - - - - - - - - - - - - -# self.process.onButtonEvent(gamepadNum, buttonNum, True) #- - - - - - - - - - - - - - - - - - - - - - - - -# # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ # GAMEPAD BUTTON RELEASED events # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ def onButtonReleased(self, gamepadNum, buttonNum): #- - - - - - - - - - - - - - - - - - - - - - - - -# self.process.onButtonEvent(gamepadNum, buttonNum, False) #- - - - - - - - - - - - - - - - - - - - - - - - -# # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ # GAMEPAD CROSSPAD events # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ def onCrossMove(self, gamepadNum, xValue, yValue): #- - - - - - - - - - - - - - - - - - - - - - - - -# self.process.onAxisEvent(gamepadNum, "x", xValue) self.process.onAxisEvent(gamepadNum, "y", yValue) #- - - - - - - - - - - - - - - - - - - - - - - - -# # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ # GAMEPAD AXIS events # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ def onAxisMove(self, gamepadNum, axisName, analogValue): #- - - - - - - - - - - - - - - - - - - - - - - - -# if axisName == "z": analogValue = -analogValue self.process.onAxisEvent(gamepadNum, axisName.upper(), analogValue) #- - - - - - - - - - - - - - - - - - - - - - - - -# # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ # MOUSE MOTION events # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ def on_mouse_motion(self, x, y, dx, dy): self.process.onMouseMotionEvent(x, y, dx, dy) # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ # MOUSE BUTTON PRESSED events # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ def on_mouse_press(self, x, y, button, modifiers): self.process.onMouseButtonEvent(x, y, button, True) # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ # MOUSE BUTTON RELEASED events # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ def on_mouse_release(self, x, y, button, modifiers): self.process.onMouseButtonEvent(x, y, button, False)
def create_process(self, args, **kwargs): self.trace(' running: {}'.format(' '.join(args))) return Process(args, **kwargs)
def __init__(self, env, id, config, nreq): Process.__init__(self, env, id) self.config = config self.nreq = nreq self.env.addProc(self)
def __init__(self, env, id, verbose): Process.__init__(self, env, id) self.ballot_number = None self.verbose = verbose self.accepted = set() self.env.addProc(self)
from process import Process import random no_of_process = 8 processes = [] head = None # Create the processes for i in range(no_of_process): # Get a random sleep time before checking whether i have the token sleep_time = round(random.uniform(0, 3), 2) p = Process(i, f'process {i}', sleep_time) processes.append(p) # Assign neighbours to the processes for idx, process in enumerate(processes): if idx == 0: head = process if idx == (no_of_process - 1): process.neighbour = head else: process.neighbour = processes[idx + 1] # Confirm that the ring is correct print('Process confirmation: ... \n') for process in processes: print(f"{process.name} -> neighbour: {process.neighbour.name}")
from process import Process from save import Save from extract import Extract if __name__ == "__main__": url = 'http://books.toscrape.com/index.html' extractMainPage = Extract(url) extractMainPage.get_soup() mainPageProcess = Process(extractMainPage.soup) for counter, category in enumerate( mainPageProcess.soup_PagePrincipal_process()): list_of_book = [] category_extract = Extract(category) category_extract.get_soup() category_process = Process(category_extract.soup) for page in range(1, category_process.number_of_page() + 1): if page == 1: page_extract = Extract(f"{category_extract.url}index.html") else: page_extract = Extract( f"{category_extract.url}page-{str(page)}.html") page_extract.get_soup() category_process = Process(page_extract.soup)
def add_process(self, process_name: str) -> None: process = Process(process_name) file_manager.append_binary_file(self.PROCESSES_FILE, process) self.inactive_processes.append(process)
def main(): if len(sys.argv) < 3: usage() sys.exit(2) diagram = ClassDiagram() classes = [] messageAssociations = [] tasks = [] for i in range(2, len(sys.argv)): process = Process(sys.argv[i]) classes += list(process.data_stores.values()) + list( process.data_objects.values()) + list( process.participants.values()) messageAssociations += list(process.messages.values()) tasks += list(process.tasks.values()) class_diagram_classes = {} for obj in classes: if obj.name not in class_diagram_classes: if hasattr(obj, 'pool'): new_class = Class_Diagram_Class(obj.name, {obj._id}, obj.properties, obj.pool) else: new_class = Class_Diagram_Class(obj.name, {obj._id}, obj.properties, None) class_diagram_classes[obj.name] = new_class else: class_diagram_classes[obj.name].ids.add(obj._id) class_diagram_classes[obj.name].properties |= obj.properties if hasattr( obj, 'pool') and class_diagram_classes[obj.name].parent is None: class_diagram_classes[obj.name].parent = obj.pool diagram.classes = class_diagram_classes for obj in messageAssociations: # avoid having multiple Associations between the same two classes message = set( (obj.source if isinstance(obj.source, Participant) else obj.source.participant, obj.target if isinstance( obj.target, Participant) else obj.target.participant)) message = tuple(message) if (message not in diagram.messageAssociations): diagram.messageAssociations[message] = [obj.name] else: if obj.name not in diagram.messageAssociations[message]: diagram.messageAssociations[message].append(obj.name) for task in tasks: inputs = task.data_in outputs = task.data_out associatedData = inputs + outputs if associatedData: for do in inputs: dataReadAssociation = set((task.participant.name, do.name)) if (dataReadAssociation in diagram.dataWriteAssociations): diagram.dataReadWriteAssociations.append( dataReadAssociation) diagram.dataWriteAssociations.remove(dataReadAssociation) elif (diagram.association_not_included(dataReadAssociation)): diagram.dataReadAssociations.append(dataReadAssociation) for do in outputs: dataWriteAssociation = set((task.participant.name, do.name)) if (dataWriteAssociation in diagram.dataReadAssociations): diagram.dataReadWriteAssociations.append( dataWriteAssociation) diagram.dataReadAssociations.remove(dataWriteAssociation) elif (diagram.association_not_included(dataWriteAssociation)): diagram.dataWriteAssociations.append(dataWriteAssociation) if inputs and outputs: for do_in in inputs: for do_out in outputs: if do_in.name != do_out.name: data_association = set((do_in, do_out)) diagram.data_associations.append(data_association) dot2png(diagram.as_dot(), replace_extension(sys.argv[1], "png"))
from process import Process import random processes = [] cid = 7 # Coordinator ID coordinator = None # Variable to store the coordinator for convinience # Create the 8 processes and store them into array for i in range(8): # Get a random wait time before checking whether the coordinator is still up wait_time = round(random.uniform(0, 3), 2) processes.append(Process(i, f'process{i}', wait_time)) # Start the process and link it to all other classes. for idx, process in enumerate(processes): # Link the processes here because the class attribute is shared across instances of the class process.neighbours.append(process) # Initialize original coordinator if idx == cid: process.coordinator['obj'] = process coordinator = process for idx, process in enumerate(processes): # Start process once everything is setup process.start() print('\n') # Stop the coordinator to trigger an election