def __getitem__(cls, key): if isinstance(key, slice): # Slice on recent runs. if key.start is not None and key.start > -1: raise ValueError("Slices must be negative. The most recent " "run is referred to as -1.") if key.stop is not None and key.stop > -1: raise ValueError("Slices must be negative. The most recent " "run is referred to as -1.") if key.stop is not None: stop = -key.stop else: stop = None if key.start is None: raise ValueError("Cannot slice infinitely into the past; " "the result could become too large.") start = -key.start result = find_last(start)[stop::key.step] [_build_header(h) for h in result] elif isinstance(key, int): if key > -1: result = find_run_start(scan_id=key) if len(result) == 0: raise ValueError("No such run found.") result = result[0] # most recent match _build_header(result) else: result = find_last(-key) if len(result) < -key: raise IndexError("There are only {0} runs.".format( len(result))) result = result[-1] _build_header(result) else: raise ValueError("Must give an integer scan ID like [6], a slice " "into past scans like [-5], [-5:], or [-5:-9:2], " "or a list like [1, 7, 13].") return result
'dtype': 'number' }, 'scalar_detector': { 'source': 'PV:pv2', 'shape': None, 'dtype': 'number' }, 'Tsam': { 'source': 'PV:pv3', 'dtype': 'number', 'shape': None } } try: last_hdr = next(find_last()) scan_id = int(last_hdr.scan_id) + 1 except (IndexError, TypeError): scan_id = 1 custom = {} # Create a BeginRunEvent that serves as entry point for a run run_start = insert_run_start(scan_id=scan_id, beamline_id='csx', time=time.time(), custom=custom, uid=str(uuid.uuid4())) # Create an EventDescriptor that indicates the data # keys and serves as header for set of Event(s) descriptor = insert_descriptor(data_keys=data_keys,
def define_parser(): parser = argparse.ArgumentParser(description='Launch a data viewer') parser.add_argument('time', nargs='?', default=0, help="Sleep duration between scan steps") return parser if __name__ == '__main__': parser = define_parser() args = parser.parse_args() sleep_time = float(args.time) try: last_start_event = next(find_last()) scan_id = int(last_start_event.scan_id)+1 except IndexError: scan_id = 1 scan_id = str(scan_id) custom = {'plotx': 'linear_motor', 'ploty': ['total_img_sum'], 'moon': 'full'} # insert the run start run_start_uid = insert_run_start(scan_id=scan_id, time=ttime.time(), beamline_id='csx', custom=custom, uid=str(uuid.uuid4())) events = run(run_start_uid=run_start_uid, sleep=sleep_time, make_run_stop=False) run_stop = insert_run_stop(run_start=run_start_uid, time=ttime.time(), reason='run completed', exit_status='success', uid=str(uuid.uuid4()))
import numpy as np import uuid data_keys = {'linear_motor': {'source': 'PV:pv1', 'shape': None, 'dtype': 'number'}, 'scalar_detector': {'source': 'PV:pv2', 'shape': None, 'dtype': 'number'}, 'Tsam': {'source': 'PV:pv3', 'dtype': 'number', 'shape': None} } try: last_hdr = next(find_last()) scan_id = int(last_hdr.scan_id)+1 except (IndexError, TypeError): scan_id = 1 # Create a BeginRunEvent that serves as entry point for a run run_start = insert_run_start(scan_id=scan_id, beamline_id='csx', time=time.time(), uid=str(uuid.uuid4()), function='cos') # Create an EventDescriptor that indicates the data # keys and serves as header for set of Event(s) descriptor = insert_descriptor(data_keys=data_keys, time=time.time(), run_start=run_start, uid=str(uuid.uuid4())) func = np.cos num = 1000
def __getitem__(cls, key): if isinstance(key, slice): # Interpret key as a slice into previous scans. if key.start is not None and key.start > -1: raise ValueError("Slices must be negative. The most recent " "run is referred to as -1.") if key.stop is not None and key.stop > -1: raise ValueError("Slices must be negative. The most recent " "run is referred to as -1.") if key.stop is not None: stop = -key.stop else: stop = None if key.start is None: raise ValueError("Cannot slice infinitely into the past; " "the result could become too large.") start = -key.start result = list(find_last(start))[stop::key.step] header = Headers([Header.from_run_start(h) for h in result]) elif isinstance(key, int): if key > -1: # Interpret key as a scan_id. gen = find_run_starts(scan_id=key) try: result = next(gen) # most recent match except StopIteration: raise ValueError("No such run found.") header = Header.from_run_start(result) else: # Interpret key as the Nth last scan. gen = find_last(-key) for i in range(-key): try: result = next(gen) except StopIteration: raise IndexError( "There are only {0} runs.".format(i)) header = Header.from_run_start(result) elif isinstance(key, six.string_types): # Interpret key as a uid (or the few several characters of one). # First try searching as if we have the full uid. results = list(find_run_starts(uid=key)) if len(results) == 0: # No dice? Try searching as if we have a partial uid. gen = find_run_starts(uid={'$regex': '{0}.*'.format(key)}) results = list(gen) if len(results) < 1: raise ValueError("No such run found.") if len(results) > 1: raise ValueError("That partial uid matches multiple runs. " "Provide more characters.") result, = results header = Header.from_run_start(result) elif isinstance(key, Iterable): # Interpret key as a list of several keys. If it is a string # we will never get this far. return Headers([cls.__getitem__(k) for k in key]) else: raise ValueError("Must give an integer scan ID like [6], a slice " "into past scans like [-5], [-5:], or [-5:-9:2], " "a list like [1, 7, 13], or a (partial) uid " "like ['a23jslk'].") return header