def data(filepath=None, host='localhost', port=_lyse_port, timeout=5): if filepath is not None: return _get_singleshot(filepath) else: df = zmq_get(port, host, 'get dataframe', timeout) try: padding = ('', ) * (df.columns.nlevels - 1) try: integer_indexing = _labconfig.getboolean( 'lyse', 'integer_indexing') except (LabConfig.NoOptionError, LabConfig.NoSectionError): integer_indexing = False if integer_indexing: df.set_index(['sequence_index', 'run number', 'run repeat'], inplace=True, drop=False) else: df.set_index([('sequence', ) + padding, ('run time', ) + padding], inplace=True, drop=False) df.index.names = ['sequence', 'run time'] except KeyError: # Empty DataFrame or index column not found, so fall back to RangeIndex instead pass df.sort_index(inplace=True) return df
def check_connectivity_loop(self): time_to_sleep = 1 #self._check_connectivity_logger = logging.getLogger('BLACS.AnalysisSubmission.check_connectivity_loop') while True: # get the current host: host = self.server send_to_server = self.send_to_server if host and send_to_server: try: self.server_online = 'checking' response = zmq_get(self.port, host, 'hello', timeout=2) if response == 'hello': success = True else: success = False except Exception: success = False # update GUI self.server_online = 'online' if success else 'offline' # update how long we should sleep if success: time_to_sleep = 10 else: time_to_sleep = 1 else: self.server_online = '' # stop sleeping if the host changes for i in range(time_to_sleep * 5): if host == self.server and send_to_server == self.send_to_server: time.sleep(0.2)
def submit_waiting_files(self): success = True while self._waiting_for_submission and success: path = self._waiting_for_submission[0] self._mainloop_logger.info('Submitting run file %s.\n' % os.path.basename(path)) data = { 'filepath': labscript_utils.shared_drive.path_to_agnostic(path) } self.server_online = 'checking' try: response = zmq_get(self.port, self.server, data, timeout=1) except (TimeoutError, gaierror): success = False else: success = (response == 'added successfully') try: self._waiting_for_submission.pop(0) except IndexError: # Queue has been cleared pass if not success: break # update GUI self.server_online = 'online' if success else 'offline' self.time_of_last_connectivity_check = time.time()
def report_fitness(individual_id, fitness, host='localhost'): port = int(_exp_config.get('ports', 'mise')) fitness = float(fitness) individual_id = int(individual_id) data = ('from lyse', individual_id, fitness) success, message = zmq_get(port, host, data, timeout=2) if not success: raise RuntimeError(message)
def test_rep_server(self): class MyServer(ZMQServer): def handler(self, data): if data == 'error': raise TestError return data server = MyServer(port=None, bind_address='tcp://127.0.0.1') try: self.assertIsInstance(server.context, SecureContext) response = zmq_get(server.port, data='hello!') self.assertEqual(response, 'hello!') # Ignore the exception in the other thread: clientserver.raise_exception_in_thread = lambda *args: None try: with self.assertRaises(TestError): zmq_get(server.port, data='error') finally: clientserver.raise_exception_in_thread = raise_exception_in_thread finally: server.shutdown()
def submit_to_blacs(shot_file, host='localhost', timeout=5): """Submit a shot file to BLACS running on the given hostname. Uses port number as saved in labconfig. Returns BLACS's reponse or raises an exception if shot was not added successfully.""" from labscript_utils.labconfig import LabConfig import zprocess from labscript_utils.shared_drive import path_to_agnostic config = LabConfig() port = int(config.get('ports', 'blacs')) data = path_to_agnostic(shot_file) response = zprocess.zmq_get(port, host, data, timeout=timeout) if 'added successfully' not in response: raise Exception(response) return response
def submit_waiting_files(self): if not self._waiting_for_submission: return while self._waiting_for_submission: path = self._waiting_for_submission[0] try: self._mainloop_logger.info('Submitting run file %s.\n'%os.path.basename(path)) data = {'filepath': labscript_utils.shared_drive.path_to_agnostic(path)} response = zmq_get(self.port, self.server, data, timeout = 2) if response != 'added successfully': raise Exception except: return else: self._waiting_for_submission.pop(0)
def data(filepath=None, host='localhost', timeout=5): if filepath is not None: return _get_singleshot(filepath) else: port = 42519 df = zmq_get(port, host, 'get dataframe', timeout) try: padding = ('',)*(df.columns.nlevels - 1) df.set_index([('sequence',) + padding,('run time',) + padding], inplace=True, drop=False) df.index.names = ['sequence', 'run time'] # df.set_index(['sequence', 'run time'], inplace=True, drop=False) except KeyError: # Empty dataframe? pass df.sort_index(inplace=True) return df
def data(filepath=None, host='localhost', timeout=5): if filepath is not None: return _get_singleshot(filepath) else: port = 42519 df = zmq_get(port, host, 'get dataframe', timeout) try: padding = ('', ) * (df.columns.nlevels - 1) df.set_index([('sequence', ) + padding, ('run time', ) + padding], inplace=True, drop=False) df.index.names = ['sequence', 'run time'] # df.set_index(['sequence', 'run time'], inplace=True, drop=False) except KeyError: # Empty dataframe? pass df.sort_index(inplace=True) return df
def check_connectivity(self): host = self.server send_to_server = self.send_to_server if host and send_to_server: self.server_online = 'checking' try: response = zmq_get(self.port, host, 'hello', timeout=1) except (TimeoutError, gaierror): success = False else: success = (response == 'hello') # update GUI self.server_online = 'online' if success else 'offline' else: self.server_online = '' self.time_of_last_connectivity_check = time.time()
def ShotReady(self): log.info("hey dude, shot is ready!") # log.info('received shot') now = datetime.now() # ------------------------------------------------------- # load settings with open(json_path) as jsonfile: last_program = json.load(jsonfile) new_sequence_index = last_program["sequence_index"] with open('settings.py') as f: save_dict = literal_eval(f.read()) # check if the previous shot was a temp one # if self.to_be_kept == False: # self.remove_h5file(self.h5path) self.to_be_kept = save_dict['save'] # ------------------------------------------------------- # prepare path for h5 file if new_sequence_index != self.sequence_index: # move to a new folder self.sequence_index = new_sequence_index self.run_number = 0 if self.to_be_kept == False: self.h5dirpath = h5path_temp / now.strftime( '%Y/%m/%d/') / f"{self.sequence_index:04d}" else: self.h5dirpath = self.h5path_0 / now.strftime( '%Y/%m/%d/') / f"{self.sequence_index:04d}" self.h5dirpath.mkdir(parents=True) self.sequence_id = now.strftime( '%Y_%m_%d') + '_' + save_dict['sequence_name'] else: self.run_number = self.run_number + 1 #self.pprint(f"{self.sequence_index}, {self.run_number}") self.h5path = self.h5dirpath / f"{self.sequence_id}_{self.run_number}.h5" attrs = defaultdict() attrs['sequence_id'] = self.sequence_id attrs['run time'] = now.isoformat() attrs['sequence_index'] = self.sequence_index attrs['run time'] = now.isoformat() self.make_new_h5file(self.h5path, attrs) # ------------------------------------------------------- # here put whatever data you want in the h5 file with h5py.File(self.h5path) as h5file: # add raw images for c in cameras_list: images = c.get_img() for k, v in images.items(): h5file[f"data/{c.name}/{k}"] = v # save program variables try: with open(json_path) as jsonfile: last_program = json.load(jsonfile) for k, v in last_program.items(): h5file[f"experiment/{k}"] = json.dumps(v) for k, v in last_program['variables'].items(): h5file["globals"].attrs[k] = v except FileNotFoundError: print("Json file not found") # add user globals for k, v in save_dict['user globals'].items(): h5file["globals"].attrs[k] = v # ------------------------------------------------------- # submit to lyse timeout = 2 if save_dict['submit_to_lyse']: try: self.submit_to_lyse(self.h5path, timeout) except zprocess.utils.TimeoutError: print('lyse is not ON') if save_dict['submit_to_runviewer']: try: zprocess.zmq_get(42521, data=str(self.h5path), timeout=timeout) except zprocess.utils.TimeoutError: print('runviewer is not ON') print('END')
def submit_to_lyse(self, filepath, timeout=2): zprocess.zmq_get(42519, data={'filepath': str(filepath)}, timeout=timeout)
def submit_to_lyse(filepath): zprocess.zmq_get(42519, data={'filepath': str(filepath)})
def makeh5file(self): logger.info("shot started") now = datetime.now() # ------------------------------------------------------- # load settings with open(self.json_path) as jsonfile: last_program = json.load(jsonfile) new_sequence_index = last_program["sequence_index"] # read current settings with open('settings.yaml') as f: save_dict = yaml.load(f) self.to_be_kept = save_dict['save'] self.get_bristol = save_dict['get_bristol'] # ------------------------------------------------------- # prepare path for h5 file if new_sequence_index != self.sequence_index: # move to a new folder self.sequence_index = new_sequence_index self.run_number = 0 if self.to_be_kept is False: self.h5dirpath = self.h5path_temp / now.strftime( '%Y/%m/%d/') / f"{self.sequence_index:04d}" else: self.h5dirpath = self.h5path_0 / now.strftime( '%Y/%m/%d/') / f"{self.sequence_index:04d}" self.h5dirpath.mkdir(parents=True) self.sequence_id = now.strftime( '%Y_%m_%d') + '_' + save_dict['sequence_name'] else: self.run_number = self.run_number + 1 self.h5path = self.h5dirpath / f"{self.sequence_id}_{self.run_number}.h5" attrs = defaultdict() attrs['sequence_id'] = self.sequence_id attrs['run time'] = now.isoformat() attrs['sequence_index'] = self.sequence_index attrs['run time'] = now.isoformat() self.make_new_h5file(self.h5path, attrs) with h5py.File(self.h5path) as h5file: # save program variables try: with open(self.json_path) as jsonfile: last_program = json.load(jsonfile) for k, v in last_program.items(): h5file[f"experiment/{k}"] = json.dumps(v) for k, v in last_program['variables'].items(): h5file["globals"].attrs[k] = v except FileNotFoundError: logger.error("Json file not found") # add user globals for k, v in save_dict['user_globals'].items(): h5file["globals"].attrs[k] = v # append file to the queue shot = {'timestamp': time.time(), 'filepath': self.h5path} self.filequeue.append(shot) if save_dict['submit_to_runviewer']: try: zprocess.zmq_get(42521, data=str(self.h5path), timeout=2) except zprocess.utils.TimeoutError: logger.info('runviewer is not ON') # add new scopes and create clients scopes = save_dict['scopes']