def create_comms(self): this_host = socket.gethostname() numComms = len(self.host_list) self.comms = [] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, self.port)) s.listen(5) print("[%s]listening to port %d" % (self.this_host, self.port)) t0 = time.clock() self.proc_list = [[]] * numComms i = 0 while (len(self.comms) < numComms) and ( (time.clock() - t0) < self.timeout): conn, addr = s.accept() print("[%s] Connected by %s" % (self.this_host, addr)) name_dir = conn.recv(1024) a = name_dir.split(':') if len(a) == 2: logfile = "controller_%s.lst" % (a[0]) print( "[%s] new comm: Communicator(conn, self.job_manager, %s, %s, %s, %s" % (self.num_iters, self.this_host, a[0], a[1], logfile)) new_comm = Communicator.Communicator(conn, self.job_manager, self.num_iters, a[0], a[1], logfile, self.proc_list[i]) self.comms.append(new_comm) #self.comms.append(Communicator(conn, self.job_manager, a[0], a[1], "controller_%s.lst" % (a[0]))) i = i + 1 else: print("[%s] bad request [%s]" % (self.this_host, str(a))) #-- end if #-- end while s.close()
def addFormulaData(self): dochand=dh.DocumentHandler() doc=dochand.createWriterDocument() text = doc.Text #text.insertString("this is the statement") query = self.capitalisePython(doc) communicator = com.APICommunicator() #create the communicator communicator.setConfiguration() #set the configuartion using config file communicator.checkConfiguration() #check whether the configuration is ok communicator.getResult(query) #query from wolfram API fileName = query+".txt" resultReader = wp.WolframMathResultReader(fileName) #read the stored result resultReader.getTotalResult() #get all the resources related to the result #resultReader.parseWolframResultToLibre() #the parsing is not happening yet #create the document text = doc.Text cursor = text.createTextCursor() for res in resultReader.plainTexts: #add formulas try: dochand.addFormulaToWriterDocument(doc,cursor,res) text.insertString( cursor, "\n\n", 0 ) except Exception: print(res) for img in resultReader.imageFileNames: #add images to the document try: imageUrl = "file://"+os.path.dirname(__file__)+"/"+img dochand.addImageToWriterDocument(doc,cursor, imageUrl) text.insertString( cursor, "\n\n", 0 ) except Exception: print(img)
def __init__(self, parent): print '[+] Building GUI environment...' # Inherit from Tkinter's parent class Tkinter.Tk.__init__(self, parent) self.parent = parent self.settings = Settings() self.comm = Communicator() # helper class for creating widgets # send self as desired frame to store widgets into self.widgets = Widgets(frame=self) # Create the GUI application # Initialize label references self.TitleLabel = Tkinter.StringVar() self.FileLabel = Tkinter.StringVar() # Initialize text entry references self.apiEntry = None # Initialize button references self.fileBtn = None self.sfBtn = None self.rfBtn = None self.rptBtn = None self.gearBtn = None # Initialize icon references self.gear_raised = None self.gear_sunken = None self.initializeGUI(self.settings)
def measure(self, msg=False): """temperaturmessung""" try: self._memorysize = SENSOR_MEMORYSIZE except Exception as e: Communicator.SchreibeFehler(e, 'measure@Sensor') print ' Set memorysize hod ned funktionert' try: # Zeilen aus Datei einlesen !!! DAUERT 0.8 SEKUNDEN pro Sensor!!!! messdaten = Communicator.fileReader(dateiName=DATEINAME_MESSDATEN, ordner=self._location, createIfNotExisting=False) if "YES" in messdaten[0]: # Teste ob uebertragung erfolgreich war pos = messdaten[1].find("t=") + 2 # Startort des Temperaturwerts ermitteln Temperatur = float(messdaten[1][pos:-1]) / 1000 #Temperatur ermitteln self.setTemperatur(Temperatur) else: self.setTemperatur(SENSOR_TRANSMIT_ERROR) self.master = socket.gethostname() except IOError: self.setTemperatur(SENSOR_OFFLINE) self.master = None finally: self.memoryInsert(self.getTemperatur()) if msg: print (self)
def _make_communicator(self) -> Communicator.Communicator: ret: tuple[int, tuple] = self.db_interface.retrieve_tokens() if ret[0] == -1: # Failed to retrieve token print(ret[1]) exit(1) refresh_token: str = ret[1][0] unix_timestamp: int = ret[1][1] communicator = Communicator.Communicator(refresh_token, unix_timestamp) return communicator
def __init__(self, dns_server_ip): """ function constructs server args: ip: string port: int """ self.dns_server_ip = dns_server_ip self.communicator = Communicator.Communicator() self.dbsp_client_list = Client_List.Client_List() self.banned_ip_list = []
def selectRankingSource(source, conf=None): ''' Select an appropriate ranking source depending on the configuration. Params: source -- specifies the ranking policy to be used admissible values are: none, samehost, odd_even, sis, sis_simple, ip_pre conf -- (optional) configuration instance (from cache or client) ''' if source == 'samehost': ranking = SameHostPolicy() elif source == 'odd_even': ranking = OddEvenPolicy() elif source == 'geoip': ranking = GeoIPPolicy() elif source == 'sis': import Communicator assert conf != None assert conf.get_sis_client_endpoint() != None assert conf.get_rating_cache_interval() != None ranking = Communicator.Communicator( conf.get_sis_client_endpoint(), simple=False, max_time_in_cache=conf.get_rating_cache_interval()) elif source == 'sis_simple': import Communicator assert conf != None assert conf.get_sis_client_endpoint() != None assert conf.get_rating_cache_interval() != None ranking = Communicator.Communicator( conf.get_sis_client_endpoint(), simple=True, max_time_in_cache=conf.get_rating_cache_interval()) elif source == 'ip_pre': assert conf != None assert conf.get_ip_prefixes() != None ranking = SameIPPrefixPolicy(conf.get_ip_prefixes()) elif source in (None, 'none', 'None'): ranking = DummyPolicy() else: raise Exception("Unsupported ranking type: " + source) return ranking
def isLinked(self): """testet ob der Sensor am aktuellen raspi angeschlossen ist""" w1_master_slaves = Communicator.getSlaveList() # liest SensorePfade ein for line in w1_master_slaves: line = Communicator.LineClean(line) if line == "notfound.": print("Keine Sensoren gefunden!") if self.getID() in w1_master_slaves: return True else: return False
def main(): ap = argparse.ArgumentParser() ap.add_argument("--calibrate", action='store_true', default=False, help='Perform camera calibration') ap.add_argument("--visualize", action='store_true', default=False, help='Visualize the tracking on a preview window') ap.add_argument("--communicate", action='store_true', default=False, help='Transmit data via tcp') ap.add_argument("--record", action='store_true', default=False, help="Record robot coordinates in a file for use by sim") args = ap.parse_args() cal_obj = cal.CameraCalibration(use_precaptured_images=not args.calibrate) if args.calibrate: cal_obj.calibrate_camera() camera_matrix, dist_matrix = cal_obj.extract_existing_calibration_data() del (cal_obj) field_set_obj = fs.FieldSetter(visualization=True) ref_point = field_set_obj.select_playing_field() del (field_set_obj) com_obj = None if args.communicate: com_obj = com.Communicator() rec_obj = None if args.record: rec_obj = rec.DataRecorder() tracker_obj = tracker.ArucoTracker(camera_matrix, dist_matrix, com_obj, rec_obj, ref_point, visualization=args.visualize) tracker_obj.track_aruco_markers()
def main(_): with tf.Session() as sess: world = Communicator.gen_world(1500, 1500) np.random.seed(RANDOM_SEED) tf.set_random_seed(RANDOM_SEED) random.seed(RANDOM_SEED) state_dim = (32, 32, 2) action_dim = (1, ) actor = ActorNetwork(sess, state_dim, action_dim, ACTOR_LEARNING_RATE, TAU) critic = CriticNetwork(sess, state_dim, action_dim, CRITIC_LEARNING_RATE, TAU, actor.get_num_trainable_vars()) train(sess, world, actor, critic, state_dim)
def main(): ap = argparse.ArgumentParser() ap.add_argument("--server", type=str, default='localhost', help='server address (default localhost)') ap.add_argument("--port", type=int, default=10000, help='server port (default 10000)') ap.add_argument("--fps", type=int, default=20, help='data rate in fps for simulator (default 20)') args = ap.parse_args() com_obj = com.Communicator(server_address=(args.server, args.port)) if not os.path.exists(gc.g_sim_data_file_name): sys.exit("No recorded simulator data found. Quitting . . .") data_file = open(gc.g_sim_data_file_name, "r") data_contents = data_file.readlines() data_file.close() print("Transmitting simulator data") for line in data_contents: json_data = json.loads(line) time.sleep((1/args.fps)) com_obj.transmit_data(json_data)
def main(_): print("Training new model") # Define Environment world = Communicator.gen_world(1500, 1500) #env = CatchEnvironment(gridSize) # Define Replay Memory memory = ReplayMemory(dState, dAction, maxMemory, discount) # Add ops to save and restore all the variables. saver = tf.train.Saver() with tf.Session() as sess: tf.initialize_all_variables().run() for i in range(epoch): # Initialize the environment. err = 0 Communicator.init_world(world) #env.reset() isGameOver = False # The initial state of the environment. currentState = np.dot( Communicator.get_screen_pixels(world, dState[0], dState[1]), [0.299, 0.587, 0.114])[np.newaxis, ..., np.newaxis].astype( np.float32) / 256. lastState = currentState #currentState = env.observe() total_reward = 0 j = 0 while (isGameOver != True): q = 0 action = -9999 # action initilization # Decides if we should choose a random action, or an action from the policy network. global epsilon if (randf(0, 1) <= epsilon): action = random.randrange(1, sum(dAction) + 1) else: # Forward the current state through the network. q = sess.run(output_layer, feed_dict={ X: currentState }) #np.concatenate((lastState, currentState), axis=3)}) # Find the max index (the chosen action). index = q.argmax() action = index + 1 # Decay the epsilon by multiplying by 0.999, not allowing it to go below a certain threshold. if (epsilon > epsilonMinimumValue): epsilon = epsilon * 0.999 #nextState, reward, gameOver, stateInfo = env.act(action) Communicator.send_action(world, ACTION_MAP[action], 1) Communicator.send_action(world, "move_forward", 1) nextState = np.dot( Communicator.get_screen_pixels(world, dState[0], dState[1]), [0.299, 0.587, 0.114])[np.newaxis, ..., np.newaxis].astype( np.float32) / 256. reward = Communicator.get_reward(world) Communicator.show(world, 300, 300) gameOver = False if world.robot.x > world.w or world.robot.x < 0 or world.robot.y > world.h or world.robot.y < 0: gameOver = True if j > 100: gameOver = True j += 1 total_reward += reward memory.remember( currentState, action, reward, nextState, gameOver ) #np.concatenate((currentState,nextState),axis=3), gameOver) print(q) # Update the current state and if the game is over. lastState = currentState currentState = nextState isGameOver = gameOver # We get a batch of training data to train the model. inputs, targets = memory.getBatch(output_layer, batchSize, sess, X) # Train the network which returns the error. _, loss = sess.run([optimizer, cost], feed_dict={ X: inputs, Y: targets }) err = err + loss print("Epoch " + str(i) + ": err = " + str(err) + ": reward = " + str(total_reward)) # Save the variables to disk. save_path = saver.save(sess, os.getcwd() + "/model.ckpt") print("Model saved in file: %s" % save_path)
def run(): # Start the communicator: utgid = os.environ.get('UTGID', 'ALIGNITER') com = Communicator(utgid) # FSM loop state = State.NOT_READY com.set_status(state) DefParameters = std.vector('double')() DefParameters.push_back(179.) DefParameters.push_back(240.) DefParameters.push_back(600.) DefParameters.push_back(600.) DefParameters.push_back(900.) DefParameters.push_back(600.) DefParameters.push_back(60.) DefParameters.push_back(900.) LineNames = std.vector('string')() LineNames.push_back('mbias') LineNames.push_back('Bs2Dsmunu') LineNames.push_back('Lb2pmunu') LineNames.push_back('Bs2phigamma') LineNames.push_back('Bs2mumu') LineNames.push_back('Bs2Kstee') LineNames.push_back('Bd2Kstmumu') LineNames.push_back('Bd2Kpi') LineNames.push_back('Bs2phiphi') LineNames.push_back('DD') LineNames.push_back('Bs2DsPi') LineNames.push_back('Bs2JpsiPhi') LineNames.push_back('Bs2Jpsikkpipi') LineNames.push_back('D2kkpi') LineNames.push_back('Dst2Dpi2kskk') LineTypes = std.vector('int')() LineTypes.push_back(0) LineTypes.push_back(1) LineTypes.push_back(1) LineTypes.push_back(3) LineTypes.push_back(2) LineTypes.push_back(3) LineTypes.push_back(2) LineTypes.push_back(1) LineTypes.push_back(1) LineTypes.push_back(1) LineTypes.push_back(1) LineTypes.push_back(2) LineTypes.push_back(2) LineTypes.push_back(1) LineTypes.push_back(1) MaxEfficiencies = std.vector('double')() MaxEfficiencies.push_back(0.4) MaxEfficiencies.push_back(0.9) MaxEfficiencies.push_back(0.9) MaxEfficiencies.push_back(0.6) MaxEfficiencies.push_back(0.9) MaxEfficiencies.push_back(0.6) MaxEfficiencies.push_back(0.3) MaxEfficiencies.push_back(0.3) MaxEfficiencies.push_back(0.3) MaxEfficiencies.push_back(0.2) MaxEfficiencies.push_back(0.7) MaxEfficiencies.push_back(0.6) name = TString("FitterGA") Channels = std.vector('int')() Channels.push_back(1) Channels.push_back(2) # Channels.push_back(3) # Channels.push_back(4) # Channels.push_back(5) # Channels.push_back(6) # Channels.push_back(7) # Channels.push_back(8) # Channels.push_back(9) # Channels.push_back(10) # Channels.push_back(11) # Channels.push_back(12) # Channels.push_back(13) # Channels.push_back(14) # Channels.push_back(15) lo = std.vector('double')() hi = std.vector('double')() lo.push_back(100.) hi.push_back(400.) # lo.push_back(20.) # hi.push_back(100.) # lo.push_back(600.) # hi.push_back(1300.) UsedParameters = std.vector('int')() #UsedParameters.push_back(0) UsedParameters.push_back(1) # UsedParameters.push_back(3) # UsedParameters.push_back(4) # UsedParameters.push_back(5) # UsedParameters.push_back(6) # UsedParameters.push_back(7) #fitter.CheckForUnusedOptions() pars = std.vector('double')(2) subprocess.call('rm -rf $OUTPUTDIR/*.root', shell=True) cycle = 0 firstEntry = True StartIterationFirstTime = True FinishIterationAndStartNew = False print type(cycle) while True: command = com.get_command() if command == 'configure' and state == State.NOT_READY: import pickle f = open('BWTest.pkl') channels = pickle.load(f) trigger_lines = pickle.load(f) trigger_thresholds = pickle.load(f) statistics = pickle.load(f) f.close() opt = TString( "PopSize=3:Steps=2:Cycles=2:ConvCrit=0.01:SaveBestCycle=5") fitter = TMVA.GeneticFitterMod(name, lo, hi, Channels, opt, DefParameters, UsedParameters, MaxEfficiencies, LineNames, LineTypes) fitter.RunInit(pars) state = State.READY elif command == 'start' and state == State.READY: fitter.CycleInit(cycle) fitter.ga.WriteParamsToFile(cycle) StartIterationFirstTime = True FinishIterationAndStartNew = False state = State.RUNNING print 'WANT TO CALCULATE FITNESS FIRST TIME' elif command == 'pause' and state == State.RUNNING: state = State.PAUSED print 'FITNESS CALCULATED' com.set_status(state) if cycle < fitter.GenNcycles(): if StartIterationFirstTime: print 'CYCLE %d' % cycle print 'FIRST ITERATION' # subprocess.call('rm -rf $OUTPUTDIR/bwdhistosRESULT.root',shell=True) subprocess.call( 'hadd -f $OUTPUTDIR/bwdhistosRESULT.root $OUTPUTDIR/bwdhistosRes*', shell=True) fitter.ga.CalculateFitness() fitter.ga.GetGeneticPopulation().TrimPopulation() fitter.IterationInit(cycle) fitter.ga.WriteParamsToFile(cycle) StartIterationFirstTime = False FinishIterationAndStartNew = True state = State.RUNNING print 'WANT TO CALCULATE FITNESS' elif FinishIterationAndStartNew: # subprocess.call('rm -rf $OUTPUTDIR/bwdhistosRESULT.root',shell=True) subprocess.call( 'hadd -f $OUTPUTDIR/bwdhistosRESULT.root $OUTPUTDIR/bwdhistosRes*', shell=True) fitter.ga.CalculateFitness() fitter.IterationExit(cycle) if not fitter.ga.HasConverged(fitter.GetNsteps(), fitter.GetConvCrit()): fitter.IterationInit(cycle) fitter.ga.WriteParamsToFile(cycle) print 'WANT TO CALCULATE FITNESS' else: fitter.CycleExit(cycle) print 'CYCLE ', cycle, ' FINISHED' cycle += 1 if not cycle < fitter.GenNcycles(): fitter.gstore.WriteParamsToFile(1) print 'AAAAAAAA 1111' else: fitter.CycleInit(cycle) fitter.ga.WriteParamsToFile(cycle) StartIterationFirstTime = True print 'CYCLE %d' % cycle print 'WANT TO CALCULATE FITNESS FIRST TIME' FinishIterationAndStartNew = False StartIterationFirstTime = True print 'AAAAAAAA 2222' state = State.RUNNING else: # subprocess.call('rm -rf $OUTPUTDIR/bwdhistosRESULT.root',shell=True) print 'AAAAAAAA 3333' subprocess.call( 'hadd -f $OUTPUTDIR/bwdhistosRESULT.root $OUTPUTDIR/bwdhistosRes*', shell=True) print 'AAAAAAAA 4444' fitness = fitter.RunExit() print 'AAAAAAAA 5555' print 'iterator done' state = State.READY sleep(2) elif command == 'stop' and state in (State.RUNNING, State.READY): state = State.READY elif command == 'reset': state = State.NOT_READY break else: print 'iterator: bad transition from %s to %s' % (state, command) state = State.ERROR break # Set the status com.set_status(state) # Set our status one last time com.set_status(state)
def run(): # Start the communicator: com = Communicator('ALIGNITER') # FSM loop state = State.NOT_READY com.set_status(state) n_it = 0 while True: command = com.get_command() if command == 'configure' and state == State.NOT_READY: state = State.READY elif command == 'start' and state == State.READY: state = State.RUNNING elif command == 'pause' and state == State.RUNNING: state = State.PAUSED com.set_status(state) if n_it < 4: n_it += 1 print 'iteration %d' % n_it state = State.RUNNING else: print 'iterator done' state = State.READY sleep(2) elif command == 'stop' and state in (State.RUNNING, State.READY): state = State.READY elif command == 'reset': state = State.NOT_READY break else: print 'iterator: bad transition from %s to %s' % (state, command) state = State.ERROR break # Set the status com.set_status(state) # Set our status one last time com.set_status(state)
class GUI(Tkinter.Tk): def __init__(self, parent): print '[+] Building GUI environment...' # Inherit from Tkinter's parent class Tkinter.Tk.__init__(self, parent) self.parent = parent self.settings = Settings() self.comm = Communicator() # helper class for creating widgets # send self as desired frame to store widgets into self.widgets = Widgets(frame=self) # Create the GUI application # Initialize label references self.TitleLabel = Tkinter.StringVar() self.FileLabel = Tkinter.StringVar() # Initialize text entry references self.apiEntry = None # Initialize button references self.fileBtn = None self.sfBtn = None self.rfBtn = None self.rptBtn = None self.gearBtn = None # Initialize icon references self.gear_raised = None self.gear_sunken = None self.initializeGUI(self.settings) def initializeGUI(self, settings): # Define grid layout manager self.title("VirusTotal Client") self.grid() self.widgets.createLabel(0, 0, 4, "EW", "white", "blue", "w", self.TitleLabel) self.TitleLabel.set("VirusTotal Client") self.widgets.createLabel(0, 1, 2, "EW", "black", "white", "w", self.FileLabel) self.FileLabel.set("Choose a File") # self.apiEntry = self.widgets.createEntry(0, 2, 4, "EW", self.apiLabel, "<Return>", self.setAPIKey) # if there is no value in APIKey, set a default apiLabel # confKey = settings.getAPI() # if confKey == '': # self.apiLabel.set("Enter API Key") # else: # self.apiLabel.set(confKey) self.fileBtn = self.widgets.createButton(2, 1, 2, "EW", "Upload", None, self.setFileName) self.sfBtn = self.widgets.createButton(0, 2, 1, "EW", "Send File", None, self.VTsendFile) self.rfBtn = self.widgets.createButton(1, 2, 1, "EW", "Rescan File", None, self.VTrescanFile) self.rptBtn = self.widgets.createButton(2, 2, 1, "EW", "Report", None, self.VTgetReport) self.gearBtn = self.widgets.createButton(3, 2, 1, '', "Settings", None, self.changeSettings) # self.createIcons() self.grid_columnconfigure(0, weight=1) self.resizable(True, True) self.update() self.geometry(self.geometry()) def createIcons(self): self.gear_raised = Tkinter.PhotoImage(file="images\gear_raised.gif") self.gearBtn.config(image=self.gear_raised) # self.gear_sunken = Tkinter.PhotoImage(file="images\gear_sunken.gif") def changeSettings(self): # send 'self' (main GUI frame) to create "Change Settings" as a child class dialog = CustomDialog(parent=self, title="Change Settings") settingsFrame = dialog.returnFrame() # set focus to settingsFrame when it is spawned settingsFrame.focus_set() # assign a grid layout manager to settingsFrame settingsFrame.grid() # create label to the left of the 'resource' settings entry reportLabel = Tkinter.StringVar() self.widgets.createLabel(1, 0, 1, "EW", "black", None, "w", reportLabel, settingsFrame) reportLabel.set("Last Saved Report: ") # create label to hold the settings resource value inReport = Tkinter.StringVar() self.widgets.createLabel(2, 0, 3, "EW", "black", None, "w", inReport, settingsFrame) inReport.set(self.settings.getResource()) apiLabel = Tkinter.StringVar() self.widgets.createLabel(1, 1, 1, "EW", "black", None, "w", apiLabel, settingsFrame) inAPI = Tkinter.StringVar() self.widgets.createLabel(2, 1, 3, "EW", "black", None, "w", inAPI, settingsFrame) apiLabel.set("API Key: ") inAPI.set(self.settings.getAPI()) fileLabel = Tkinter.StringVar() self.widgets.createLabel(1, 2, 1, "EW", "black", None, "w", fileLabel, settingsFrame) fileLabel.set("Last File: ") inFile = Tkinter.StringVar() self.widgets.createLabel(2, 2, 3, "EW", "black", None, "w", inFile, settingsFrame) try: inFile.set(self.settings.getFileName()) except: inFile.set("No File Specified") # create text entry box for inputting the API Key and Last Saved Report in settingsFrame # don't bind any keys or functions to it apiEntry = self.widgets.createEntry(2, 1, 3, "EW", inAPI, None, None, settingsFrame) # create Save/Cancel buttons for settingsFrame self.widgets.createButton(2, 3, 1, "EW", "Save & Exit", None, lambda: self.saveSettings(dialog, apiEntry.get()), settingsFrame) self.widgets.createButton(4, 3, 1, "EW", "Cancel", None, lambda: self.cancel(dialog), settingsFrame) settingsFrame.grid_columnconfigure(0, weight=1) # settingsFrame.resizable(True, True) settingsFrame.update() # settingsFrame.geometry(self.geometry()) def saveSettings(self, frame, newAPIKey): self.setAPIKey(newAPIKey) self.settings.updateConfig('apikey', newAPIKey) self.cancel(frame) pass def cancel(self, frame): # throw the focus on the main GUI (self) window self.focus_set() # destroy the specified frame frame.destroy() def VTsendFile(self): filename = self.FileLabel.get() if filename != "Choose a File": filename = self.settings.getFileName() # get response dict from VT response = self.comm.sendFile(filename) self.comm.prettyPrint(response) self.settings.updateConfig('resource', response['resource']) self.settings.updateConfig('lastfile', filename) else: tkMessageBox.showerror("Warning", "Filename not specified. Please upload a file.") def VTrescanFile(self): resource_id = self.settings.getResource() if resource_id != '': response = self.comm.reScan(resource_id) self.comm.prettyPrint(response) else: tkMessageBox.showerror("Warning", "You must specify a resource ID to rescan a file.") def VTgetReport(self): resource_id = self.settings.getResource() if resource_id != '': response = self.comm.getReport(resource_id) self.comm.printReport(response) else: tkMessageBox.showerror("Warning", "You must specify a resource ID to retrieve a report.") def setAPIKey(self, apikey): if apikey is " ---- none ---- ": tkMessageBox.showerror("Error", "You haven't specified an API Key") # double check the user actually wants to change his/her api key if tkMessageBox.askyesno("Warning", "Are you sure you want to change your API Key?"): self.settings.setAPI(apikey) # Uses a File Dialog helper class (tkFileDialog) to spawn a window for the user to choose a filename def setFileName(self): # Change filename settings after asking the user which filename he/she wants to upload newfile = str(self.askopenfilename(self, 'Upload a File')) self.settings.setFileName(newfile) # Change the content of FileLabel to reflect the selected filename filename = self.settings.getFileName() self.FileLabel.set(filename) # Helper function for getFileName(), because tkFileDialog requires a separate function to properly create dialog def askopenfilename(self, parent, title): return tkFileDialog.askopenfilename(parent=parent, title=title)
def run(index, data_directory): # Start the communicator: com = Communicator("AligWrk_%d" % index) # FSM loop state = State.NOT_READY com.set_status(state) n_it = 0 p_it = 2 hn = socket.gethostname() pt = Paths(index, hn) #filout = open(os.path.join(pt.gz_dir,'AnaPrint_%s.txt'%hn),'w') while True: command = com.get_command() if command == 'configure' and state == State.NOT_READY: state = State.READY elif command == 'start' and state == State.READY: state = State.RUNNING com.set_status(state) n_it += 1 ## check, if there's anything to fit histos = pt.histos_location_an() lambdas = pt.lambdas_location_an() print "HISTOS", histos print "LAMBDAS", lambdas ## Clear everything that was ## left from the previous run ## Uncomment these lines if you feel ## that it is necessary if n_it == 1 and p_it == 2: print "TIME TO REMOVE" if os.path.exists(histos): print "HISTOS", histos os.remove(histos) if os.path.exists(lambdas): print "AND LAMBDAS", lambdas os.remove(lambdas) #os.system('rm /group/calo/CalibWork/*.db') rootfiles = pt.getrootfiles() fmdstfiles = pt.getfmdstfiles() dstfiles = pt.getdstfiles() brunelhistos = pt.getbrunelhistos() for bh in brunelhistos: os.remove(bh) for ds in dstfiles: os.remove(ds) for fm in fmdstfiles: os.remove(fm) for rf in rootfiles: os.remove(rf) ## if there are any histograms - fit them if os.path.exists(histos): print 'Fitting the histograms from', histos, 'whith no coefficients' RunKali(histos, process='Fit', index=index) ## if not do whatever is necessary to obtain them else: ## first check, what files already exist fmdstfiles = pt.getfmdstfiles() dstfiles = pt.getdstfiles() histos_location = pt.histos_location_an() ## path to the calibration constants lambdas_location = pt.lambdas_location() lambdas_db_location = pt.lambdas_db_location() ## Run reconstruction if necessary list_of_files, first_run, last_run = pt.getinputfiles() if not dstfiles: print "Running event reconstruction at first" ## If there are no input files, we have to immediately go to ## paused. if not list_of_files: time.sleep(random.uniform(0.5, 1.5)) state = State.PAUSED com.set_status(state) continue RunBrunel(list_of_files, index, first_run, last_run) dstfiles = pt.getdstfiles() rootfiles = pt.getrootfiles() for bh in rootfiles: os.system('chmod a+rwx ' + bh) os.system('mv %s %shist' % (bh, bh)) ## Creat the fmdsts and root files if necessary rootfiles = pt.getrootfiles() if not fmdstfiles or not rootfiles: ## If there are no input files, we have to immediately go to ## paused. if not dstfiles: time.sleep(random.uniform(0.5, 1.5)) state = State.PAUSED com.set_status(state) continue print "And create the fmDSTs and tuples" fmDSTprod(dstfiles, index=index) rootfiles = pt.getrootfiles() fmdstfiles = pt.getfmdstfiles() for i in range(len(rootfiles)): os.system('chmod a+rwx ' + rootfiles[i]) os.system('chmod a+rwx ' + fmdstfiles[i]) ## Run re-reconstruction, if it's time already print "Should the reconstruction start?", n_it, PassIt if n_it > PassIt and os.path.exists( lambdas_db_location % p_it): print "==" * 20 print "Running the re-reconstruction" print "==" * 20 ## If there are no input files, we have to immediately go to ## paused. if not fmdstfiles: time.sleep(random.uniform(0.5, 1.5)) state = State.PAUSED com.set_status(state) continue #fmdst = fmdstfiles[0] print lambdas_db_location % p_it fmDSTprod(fmdstfiles, lambdas_db_location % p_it, index) rootfiles = pt.getrootfiles() n_it = 0 p_it += 1 # get the new root files after the re-reconstruction elif n_it > PassIt and not os.path.exists( lambdas_db_location % p_it): print "Can't make re-reconstruction without coefficients" break ## check for the calibration constants lambdas = None if os.path.exists(lambdas_location % p_it): lambdas = lambdas_location % p_it ## If there are no input files, we have to immediately go to ## paused. if not rootfiles: time.sleep(random.uniform(0.5, 1.5)) state = State.PAUSED com.set_status(state) continue ## fill the histograms from the corresponding root file print len(rootfiles) RunKali(rootfiles[0], lambdas=lambdas, index=index) #if n_it == PassIt and p_it == 2: # rootfiles = [os.path.join(pt.dst_dir,f) for f in os.listdir(pt.dst_dir) if re.match(r'.*\.(root|ROOT)$', f)] # for rf in rootfiles: os.remove(rf) # fmdstfiles = [os.path.join(pt.dst_dir,f) for f in os.listdir(pt.dst_dir) if re.match(r'.*\.(fmdst|fmDST)$', f)] # for fm in fmdstfiles: os.remove(fm) # dstfiles = [os.path.join(pt.dst_dir,f) for f in os.listdir(pt.dst_dir) if re.match(r'.*\.(dst|DST)$', f)] # for ds in dstfiles: os.remove(ds) state = State.PAUSED elif command == 'stop' and state == State.PAUSED: state = State.READY elif command == 'reset': state = State.NOT_READY break else: print 'analyzer: bad transition from %s to %s' % (state, command) state = State.ERROR break time.sleep(random.uniform(0.5, 1.5)) # Set the status com.set_status(state) time.sleep(random.uniform(0.5, 1.5)) # Set the status one last time. com.set_status(state)
def __init__(self, db_path: str): self.db_interface = DBInterface.DBInterface(db_path) self.communicator = Communicator.Communicator(self.db_interface) # @TODO Check if communicator is working, else disable API features but work otherwise self.model = Model.Model(self.db_interface)
def __init__(self): self.port = 2049 # Should be overridden by subclass. self.responseEOL = "\r" self.communicator = Communicator.SocketCommunicator( self.responseFunction)
def run(directory='.'): # Start the communicator: com = Communicator('ALIGNITER') # FSM loop state = State.NOT_READY com.set_status(state) n_it = 0 i_it = 0 p_it = 2 pt = Paths() gz_dir = pt.gz_dir dst_dir = pt.dst_dir #pt = Paths(directory) store_location = pt.store_location() while True: command = com.get_command() if command == 'configure' and state == State.NOT_READY: ## find the last one saved and ## save current as the next one if n_it == 0: StoredHistoPath = pt.histos_location_it() ih = 1 Pass = 1 while True: if ih > PassIt / 2: Pass += 1 ih = 1 if os.path.exists( StoredHistoPath % (Pass, ih)): ## look through all the existing files ih += 1 continue else: ## write to the file next to the latest existing n_it = ih - 1 p_it = Pass break state = State.READY elif command == 'start' and state == State.READY: state = State.RUNNING elif command == 'pause' and state == State.RUNNING: state = State.PAUSED com.set_status(state) if n_it < MaxIt: n_it += 1 i_it += 1 ## If enough primary iterations are done ## Set the new global iteration if i_it > PassIt: i_it = 0 p_it += 1 print 'iteration %d, pass %d' % (i_it, p_it) #RunKali(lambdas_location, 'Fit') lambdas_location = pt.lambdas_location() #lambdas_location_it= pt.lambdas_location_it() histos = pt.gethistomaps() lams = pt.getlambdamaps() for i in range(1 + MaxIt / PassIt): if lambdas_location % i in lams: lams.remove(lambdas_location % i) print 'Remove', lambdas_location % i, 'from', lams #if lambdas_location in lams: lams.remove(lambdas_location) ; print 'Remove', lambdas_location, 'from', lams if histos: #print "SPLITTING THE FILLED HISTOGRAMS" #SplitHistos (directory) SplitHistos(gz_dir) state = State.RUNNING if lams: #print "COLLECTING THE CALIBRATION COEFFICIENTS" #CollectLambdas(directory) CollectLambdas(gz_dir) #print 'Copying lambdas from ', lambdas_location, ' to ', lambdas_location_it #os.system('cp '+lambdas_location+' '+lambdas_location_it) state = State.RUNNING else: print 'iterator done' #GetOutput(directory) GetOutput(gz_dir) ## clear dbases = [ os.path.join(gz_dir, f) for f in os.listdir(gz_dir) if re.match(r'.*\.(db)$', f) ] #for db in dbases: os.remove(db) rootfiles = [ os.path.join(dst_dir, f) for f in os.listdir(dst_dir) if re.match(r'.*\.(root|ROOT)$', f) ] #for rf in rootfiles: os.remove(rf) fmdstfiles = [ os.path.join(dst_dir, f) for f in os.listdir(dst_dir) if re.match(r'.*\.(fmdst|fmDST)$', f) ] #for fm in fmdstfiles: os.remove(fm) dstfiles = [ os.path.join(dst_dir, f) for f in os.listdir(dst_dir) if re.match(r'.*\.(dst|DST)$', f) ] #for ds in dstfiles: os.remove(ds) stored = [ os.path.join(pt.store_location(), f) for f in os.listdir(pt.store_location()) if re.match(r'.*\.(gz)$', f) ] #for st in stored: os.remove(st) state = State.READY #sleep(2) elif command == 'stop' and state in (State.RUNNING, State.READY): state = State.READY elif command == 'reset': state = State.NOT_READY break else: print 'iterator: bad transition from %s to %s' % (state, command) state = State.ERROR break # Set the status com.set_status(state) # Set our status one last time com.set_status(state)
import pika import time import Communicator from proximity import * def pack_location_to_json(version, timestamp, sensor_type, uuid, major, minor, db): beacon_json = '{"uuid" : "' + uuid + '", "major" : ' + major + ', "minor" : ' + minor + ', "value" : ' + db + '}' json_msg = '{"version": ' + str(version) + ', "timestamp": ' + \ str(timestamp) + ', "sensortype": "' + sensor_type + '", "values": [' + beacon_json + "]}" return json_msg communicator = Communicator.RabbitMQCommunicator("127.0.0.1", "sg.ex.sensor_values", "sg.rk.sensor_values") #communicator = Communicator.CommunicatorDummy() communicator.setup_connection() valid_uuid = "f0018b9b75094c31a9051a27d39c003c" scanner = Scanner(loops=3) while True: version = 1 for beacon in scanner.scan(): timestamp = time.time() split_arr = beacon.split(',') if str(split_arr[1]) == valid_uuid: json = pack_location_to_json(version, timestamp, "location", str(split_arr[1]), str(split_arr[2]), str(split_arr[3]), str(split_arr[5]))
def __init__(self): self.communicator = Communicator.Communicator("WorldStateTopic", "WorldState", "WorldState", 10012) self.controller = None
def world(): import Communicator return Communicator.world()
def run(index=0): # Start the communicator: com = Communicator("ALIGNWORK_%02d" % index) # FSM loop state = State.NOT_READY com.set_status(state) n_it = 0 sleep(0.2) while True: command = com.get_command() if command == 'configure' and state == State.NOT_READY: import L0_noHlt1_histosGen_Multi state = State.READY elif command == 'start' and state == State.READY: state = State.RUNNING com.set_status(state) L0_noHlt1_histosGen_Multi.MultiRun() sleep(2) state = State.PAUSED elif command == 'stop' and state == State.PAUSED: state = State.READY elif command == 'reset': state = State.NOT_READY break else: print 'analyzer: bad transition from %s to %s' % (state, command) state = State.ERROR break sleep(2) # Set the status com.set_status(state) # Set the status one last time. com.set_status(state)
def run(index, input_directory): # Start the communicator: com = Communicator("ALIGNWORK_%02d" % index) # FSM loop state = State.NOT_READY com.set_status(state) n_it = 0 p_it = 2 hn = socket.gethostname() pt = Paths(index, hn, os.path.dirname(input_directory)) #pt = Paths(input_file,index,hn) while True: command = com.get_command() if command == 'configure' and state == State.NOT_READY: state = State.READY elif command == 'start' and state == State.READY: state = State.RUNNING com.set_status(state) n_it += 1 ## find the last one saved and ## save current as the next one if n_it == 1: StoredHistoPath = pt.histos_location_it() ih = 1 Pass = 1 while True: if ih > PassIt / 2: Pass += 1 ih = 1 if os.path.exists( StoredHistoPath % (Pass, ih)): ## look through all the existing files ih += 1 continue else: ## write to the file next to the latest existing n_it = ih p_it = Pass break ## check, if there's anything to fit histos = pt.histos_location_an() #print histos lambdas = pt.lambdas_location_an() #if pt.lambdas_location() in lambdas: lambdas.remove(pt.lambdas_location()) #print lambdas if os.path.exists(histos): print 'Fitting the histograms from', histos, 'whith no coefficients' RunKali(histos, process='Fit', index=index) state = State.PAUSED # if not, fill the histograms, or do whatever is necessary for it etc. else: ## first check, what files already exist fmdstfiles = pt.getfmdstfiles() dstfiles = pt.getdstfiles() histos_location = pt.histos_location_an() rootfiles = pt.getrootfiles() ## path to the calibration constants lambdas_location = pt.lambdas_location() lambdas_db_location = pt.lambdas_db_location() ## Run reconstruction if necessary if not dstfiles: print "Running event reconstruction at first" list_of_files = pt.getinputfiles()[index] RunBrunel(list_of_files, index) #RunBrunel(input_file, index) dstfiles = pt.getdstfiles() ## Creat the fmdsts and root files if necessary #if not fmdstfiles and not rootfiles: if not rootfiles: print "And create the fmDSTs and tuples" fmDSTprod(dstfiles[0], index=index) rootfiles = pt.getrootfiles() fmdstfiles = pt.getfmdstfiles() ## Run re-reconstruction, if it's time already print "Should the reconstruction start?", n_it, PassIt if n_it > PassIt and os.path.exists( lambdas_db_location % p_it): print "==" * 20 print "Running the re-reconstruction" print "==" * 20 fmdsts = pt.getfmdstfiles() #print lambdas_db_location%p_it fmDSTprod(fmdsts[index], lambdas_db_location % p_it, index) rootfiles = pt.getrootfiles() n_it = 0 p_it += 1 # get the new root files after the re-reconstruction elif n_it > PassIt and not os.path.exists( lambdas_db_location % p_it): print "Can't make re-reconstruction without coefficients" break ## check for the calibration constants lambdas = None if os.path.exists(lambdas_location % p_it): lambdas = lambdas_location % p_it ## fill the histograms from the corresponding root file RunKali(rootfiles[0], lambdas=lambdas, index=index) state = State.PAUSED elif command == 'stop' and state == State.PAUSED: state = State.READY elif command == 'reset': state = State.NOT_READY break else: print 'analyzer: bad transition from %s to %s' % (state, command) state = State.ERROR break time.sleep(random.uniform(0.5, 1.5)) # Set the status com.set_status(state) time.sleep(random.uniform(0.5, 1.5)) # Set the status one last time. com.set_status(state)
def train(sess, world, actor, critic, state_dim): # Set up summary Ops summary_ops, summary_vars = build_summaries() sess.run(tf.global_variables_initializer()) writer = tf.summary.FileWriter(SUMMARY_DIR, sess.graph) # Initialize target network weights actor.update_target_network() critic.update_target_network() # Initialize replay memory replay_buffer = ReplayBuffer(BUFFER_SIZE, RANDOM_SEED) for i in range(MAX_EPISODES): Communicator.init_world(world) ep_reward = 0 ep_ave_max_q = 0 s, s2 = None, None _s, _last_s = None, None for j in range(MAX_EP_STEPS): _s = np.dot( Communicator.get_screen_pixels(world, state_dim[0], state_dim[1]), [0.299, 0.587, 0.114]).astype(np.float32) / 256. if (_last_s != None): s = np.concatenate( (_last_s[..., np.newaxis], _s[..., np.newaxis]), axis=2) else: s = np.concatenate((_s[..., np.newaxis], _s[..., np.newaxis]), axis=2) _last_s = _s # Added exploration noise a = actor.predict(np.reshape(s, (1, ) + s.shape)) a[0][0] += (random.random() * 4 - 2) * (100. / (100. + i)) #a[0][1] += (random.random()*2-1) * (1. / (1. + i)) #a = actor.predict(np.reshape(s, (1,)+s.shape)) #Communicator.send_action(world, "move_forward", 1) Communicator.send_action(world, "absolute_move", a[0][0], 0, 0) _s2 = np.dot( Communicator.get_screen_pixels(world, state_dim[0], state_dim[1]), [0.299, 0.587, 0.114]).astype(np.float32) / 256. s2 = np.concatenate((_s[..., np.newaxis], _s2[..., np.newaxis]), axis=2) r = Communicator.get_reward(world) terminal = 0 if world.robot.x > world.w or world.robot.x < 0 or world.robot.y > world.h or world.robot.y < 0: terminal = 1 #Communicator.init_world(world) if RENDER_ENV: Communicator.show(world, 300, 300) replay_buffer.add(np.reshape(s, actor.s_dim), np.reshape(a, actor.a_dim), r, terminal, np.reshape(s2, actor.s_dim)) # Keep adding experience to the memory until # there are at least minibatch size samples if replay_buffer.size() > MINIBATCH_SIZE: s_batch, a_batch, r_batch, t_batch, s2_batch = \ replay_buffer.sample_batch(MINIBATCH_SIZE) # Calculate targets target_q = critic.predict_target( s2_batch, actor.predict_target(s2_batch)) y_i = [] for k in range(MINIBATCH_SIZE): if t_batch[k]: y_i.append(r_batch[k]) else: y_i.append(r_batch[k] + GAMMA * target_q[k]) # Update the critic given the targets predicted_q_value, _ = critic.train( s_batch, a_batch, np.reshape(y_i, (MINIBATCH_SIZE, 1))) ep_ave_max_q += np.amax(predicted_q_value) # Update the actor policy using the sampled gradient a_outs = actor.predict(s_batch) grads = critic.action_gradients(s_batch, a_outs) actor.train(s_batch, grads[0]) # Update target networks actor.update_target_network() critic.update_target_network() s = s2 ep_reward += r if terminal: break summary_str = sess.run(summary_ops, feed_dict={ summary_vars[0]: ep_reward, summary_vars[1]: ep_ave_max_q / float(j) }) writer.add_summary(summary_str, i) writer.flush() print('| Reward: %.2i' % int(ep_reward), " | Episode", i, \ '| Qmax: %.4f' % (ep_ave_max_q / float(j)))
import sys from Communicator import * from ImageStation import * from CameraControl import * from ISThreads import * # # Script to start the program # if __name__ == "__main__": #create the communicator (model) communicator = Communicator() #create GUI components (controllers) camera_control = CameraControl(communicator) communicator.attach(camera_control) image_station = ImageStation(communicator, camera_control) communicator.attach(image_station) #create threads for the models and controllers ct = CommunicatorThread(communicator) gt = GtkThread() #start the threads ct.start() gt.start()
def run(directory='.'): # Start the communicator: com = Communicator('AligDrv_0') # FSM loop state = State.NOT_READY com.set_status(state) n_it = 0 i_it = 0 p_it = 2 pt = Paths() gz_dir = pt.gz_dir dst_dir = pt.dst_dir store_location = pt.store_location() ##filout = open(os.path.join(gz_dir,'printouts.txt'),'w') while True: command = com.get_command() if command == 'configure' and state == State.NOT_READY: ## Remove, if something has left from the previous run if n_it == 0 and p_it == 2: #print 'Exterminate!' dbases = [ os.path.join(gz_dir, f) for f in os.listdir(gz_dir) if re.match(r'.*\.(db)$', f) or re.match(r'.*\.(gz)$', f) ] #print 'Exterminate!!!',dbases for db in dbases: os.remove(db) #stored = [os.path.join(pt.store_location(),f) for f in os.listdir(pt.store_location())] #print 'EXTERMINATE!!!',stored #for st in stored: os.remove(st) state = State.READY elif command == 'start' and state == State.READY: state = State.RUNNING elif command == 'pause' and state == State.RUNNING: state = State.PAUSED com.set_status(state) if n_it < MaxIt: n_it += 1 i_it += 1 print asctime( ), "Hi, I'm the iterator and I'm running", p_it, i_it, n_it ## If enough primary iterations are done ## Set the new global iteration if i_it > PassIt: i_it = 0 p_it += 1 print 'iteration %d, pass %d' % (i_it, p_it) lambdas_location = pt.lambdas_location() histos = pt.gethistomaps() lams = pt.getlambdamaps() for i in range(1 + MaxIt / PassIt): if lambdas_location % i in lams: lams.remove(lambdas_location % i) print 'Remove', lambdas_location % i, 'from', lams ## if there are any histograms, ## split them between the nodes ## to prepare for fitting if histos: #print "SPLITTING THE HISTOGRAMS" SplitHistos(gz_dir) ## if there are any coefficients, ## collect them together and save if lams: #print "COLLECTING THE LAMBDAS" CollectLambdas(gz_dir) state = State.RUNNING else: print 'iterator done' ## create the output plots GetOutput(gz_dir) ## clear dbases = [ os.path.join(gz_dir, f) for f in os.listdir(gz_dir) if re.match(r'.*\.(db)$', f) ] for db in dbases: os.remove(db) stored = [ os.path.join(pt.store_location(), f) for f in os.listdir(pt.store_location()) if re.match(r'.*\.(db)$', f) ] for st in stored: os.remove(st) state = State.READY #sleep(2) elif command == 'stop' and state in (State.RUNNING, State.READY): ##filout.close() state = State.READY elif command == 'reset': state = State.NOT_READY break else: print 'iterator: bad transition from %s to %s' % (state, command) state = State.ERROR break # Set the status com.set_status(state) # Set our status one last time com.set_status(state)
def run(index): # Start the communicator: com = Communicator("ALIGNWORK_%02d" % index) with open("/dev/urandom", "rb") as rand_src: bytes = rand_src.read(4) random.seed(struct.unpack("I", bytes)[0]) # FSM loop state = State.NOT_READY com.set_status(state) n_it = 0 while True: command = com.get_command() if command == 'configure' and state == State.NOT_READY: state = State.READY elif command == 'start' and state == State.READY: state = State.RUNNING com.set_status(state) sleep(random.uniform(1, 3)) state = State.PAUSED elif command == 'stop' and state == State.PAUSED: state = State.READY elif command == 'reset': state = State.NOT_READY break else: print 'analyzer: bad transition from %s to %s' % (state, command) state = State.ERROR break sleep(random.uniform(0.5, 1.5)) # Set the status com.set_status(state) # Set the status one last time. com.set_status(state)
def __init__(self): self.communicator = Communicator.Communicator("ControlTopic", "Control", "ControlPython", 10000) self.controller = None
def main(): try: sensor = Detector() mplayer = music_player() communicate = Communicator('Room1',sensor, mplayer) # communicate._startService_() communicate._ConnectMQ_() communicate._ConnectController_() communicate._consumingThread_() sensorTemp = sensor.check_led() while 1: time.sleep(1) print 'check led:', sensor.check_led(), ' led temp', sensorTemp if sensor.check_led() != sensorTemp: sensorTemp = sensor.check_led() if sensor.check_led() == True: communicate._SendLEDInfo_('ON') else: communicate._SendLEDInfo_('OFF') #threading.Thread(target=checkandsendLED) except (KeyboardInterrupt, SystemExit): print 'Central Control is offline....'