def _auto_dark_collection(scan, subs={}): ''' function to cover automated dark collection logic ''' light_cnt_time = scan.md['sp_params']['exposure'] try: expire_time = scan.md['sp_dk_window'] except KeyError: # protection, shouldn't happen warnings.warn( '''It seems your ScanPlan object wasn't instantiated properly. This may indicate a problem with the current version of the code." Current scan will keep going but please notify the instrument scientist who can post a bug report''' ) expire_time = 0 dark_field_uid = _validate_dark(light_cnt_time, expire_time) if not dark_field_uid: print( '''INFO: auto_dark didn't detect a valid dark, so is collecting a new dark frame. See documentation at http://xpdacq.github.io for more information about controlling this behavior''' ) # create a count plan with the same light_cnt_time if scan.sp.shutter: auto_dark_scanplan = ScanPlan('ct', {'exposure': light_cnt_time}, auto_dark_plan=True) else: auto_dark_scanplan = ScanPlan('ct', {'exposure': light_cnt_time}, shutter=False, auto_dark_plan=True) dark_field_uid = dark(scan.sa, auto_dark_scanplan, subs) auto_dark_md_dict = {'sc_dk_field_uid': dark_field_uid} return auto_dark_md_dict
def test_dark_in_prun_cannot_find_a_valid_dark(self): # case 2: can't find a qualified dark and test if md got updated time_now = time.time() self.bt.set_wavelength(0.18448) # build the dark yml dark_scan_list = [] dark_uid = str(uuid.uuid4()) dark_uid2 = str(uuid.uuid4()) dark_scan_list.append((dark_uid, 0.3, time_now - 2000)) dark_scan_list.append((dark_uid2, 0.1, time_now - 200)) with open(glbl.dk_yaml, 'w') as f: yaml.dump(dark_scan_list, f) test_list = _read_dark_yaml() self.assertEqual(test_list, dark_scan_list) # none with the right exposure scanplan = ScanPlan('ct', {'exposure': 0.01}, shutter=False) prun(self.sa, scanplan) self.assertTrue('sc_dk_field_uid' in glbl.xpdRE.call_args_list[-1][1]) self.assertFalse( glbl.xpdRE.call_args_list[-1][1]['sc_dk_field_uid'] == dark_uid2) # Second one has the right exposure time but expires glbl.dk_window = 1. scanplan = ScanPlan('ct', {'exposure': 0.1}, shutter=False) prun(self.sa, scanplan) self.assertTrue('sc_dk_field_uid' in glbl.xpdRE.call_args_list[-1][1]) self.assertFalse( glbl.xpdRE.call_args_list[-1][1]['sc_dk_field_uid'] == dark_uid2)
def test_double_scan(self): xpd_configuration["db"].prepare_hook = lambda name, doc: copy.deepcopy( doc) key_list = ["owner", "facility", "group"] for k in key_list: self.xrun.md[k] = glbl[k] uids = self.xrun( {"sample_name": "double_scan"}, [ScanPlan(self.bt, ct, 1.0), ScanPlan(self.bt, ct, 1.0)], ) hdrs = [xpd_configuration["db"][-1 * i] for i in [1, 2]] starts = [h["start"] for h in hdrs] for h in hdrs: assert "dark_frame" not in h["start"] assert h["start"]["uid"] in uids assert h["start"]["sample_name"] == "double_scan" pops = [ "uid", "time", "sp_uid", "scan_id", # Due to [] () issues or timestamps "plan_args", "hints", "bt_experimenters", ] for p in ["uid", "time", "sp_uid", "scan_id"]: for s in starts[1:]: assert s[p] != starts[0][p] [s.pop(p) for s in starts for p in pops] for s in starts: assert s == starts[0]
def test_print_scanplan(self): # using positional args sp1 = ScanPlan(self.bt, ct, 1) # using kwargs is equivalent sp2 = ScanPlan(self.bt, ct, exposure=1) # test Msg processed self.assertEqual(str(sp1), str(sp2))
def test_shutter_step(self): # test with Tramp shutter = xpd_configuration["shutter"] temp_controller = xpd_configuration["temp_controller"] exp, Tstart, Tstop, Tstep = 5, 300, 200, 10 msg_list = [] def msg_rv(msg): msg_list.append(msg) self.xrun.msg_hook = msg_rv self.xrun({}, ScanPlan(self.bt, Tramp, exp, Tstart, Tstop, Tstep)) set_msg_list = [msg for msg in msg_list if msg.command == "set"] set_msgs = iter(set_msg_list) while True: try: set_msg = next(set_msgs) if set_msg.obj.name == temp_controller.name: # after set the temp_controller, must be: # open shutter -> read -> close open_msg = next(set_msgs) assert open_msg.obj.name == shutter.name assert len(open_msg.args) == 1 assert open_msg.args[0] == 60 # open shutter first close_msg = next(set_msgs) assert close_msg.obj.name == shutter.name assert len(close_msg.args) == 1 assert close_msg.args[0] == 0 # close then move except StopIteration: print("stop") break
def test_prun_with_bleusky_plan(self): cc = Count([det], 2) self.sp = ScanPlan('bluesky', {'bluesky_plan': cc}, shutter=False) self.sc = Scan(self.sa, self.sp) self.assertEqual(self.sc.sp, self.sp) cfg_f_name = 'srxconfig.cfg' cfg_src = os.path.join(os.path.dirname(__file__), cfg_f_name) # __file__ gives relative path cfg_dst = os.path.join(glbl.config_base, cfg_f_name) shutil.copy(cfg_src, cfg_dst) # sp_params should be id to object self.assertEqual(id(cc), self.sp.md['sp_params']['bluesky_plan']) # case 1: bluesky plan object exist in current name space prun(self.sa, self.sp) # is xpdRE used? self.assertTrue(glbl.xpdRE.called) # is md updated? self.assertFalse(glbl.xpdRE.call_args_list[-1][1] == self.sc.md) # is prun passed eventually? self.assertTrue('sc_isprun' in glbl.xpdRE.call_args_list[-1][1]) # is auto_dark executed? -> No as we don't support self.assertFalse('sc_dk_field_uid' in glbl.xpdRE.call_args_list[-1][1]) # is calibration loaded? self.assertTrue(cfg_f_name in glbl.xpdRE.call_args_list[-1][1] ['sc_calibration_file_name']) # is 'blusky_plan' appears in sp_params ? self.assertTrue( 'bluesky_plan' in glbl.xpdRE.call_args_list[-1][1]['sp_params']) # is ScanPlan.md remain unchanged after scan? self.assertFalse('sc_isprun' in self.sp.md) # case 2: bluesky plan object doesn't exist in current name spaece del cc self.assertRaises(NameError, lambda: prun(self.sa, self, sp))
def test_auto_dark_collection(self): self.sp_set_dk_window = ScanPlan('ct', {'exposure': 0.1}, dk_window=25575, shutter=False) self.sp = ScanPlan('ct', {'exposure': 0.1}, shutter=False) self.sc_set_dk_window = Scan(self.sa, self.sp_set_dk_window) self.sc = Scan(self.sa, self.sp) auto_dark_md_dict_set_dk_window = _auto_dark_collection( self.sc_set_dk_window) auto_dark_md_dict = _auto_dark_collection(self.sc) # test if md is updated self.assertTrue('sc_dk_field_uid' in auto_dark_md_dict_set_dk_window and 'sc_dk_field_uid' in auto_dark_md_dict) # test if dk_window is overwrittten self.assertEqual(glbl.dk_window, self.sp.md['sp_dk_window']) self.assertEqual(25575, self.sp_set_dk_window.md['sp_dk_window'])
def test_new_prun_no_auto_dark_but_auto_calibration(self): self.sp = ScanPlan('ct', { 'exposure': 0.1, 'dk_window': 32767 }, shutter=False) self.sc = Scan(self.sa, self.sp) self.assertEqual(self.sc.sp, self.sp) cfg_f_name = 'srxconfig.cfg' cfg_src = os.path.join(os.path.dirname(__file__), cfg_f_name) # __file__ gives relative path cfg_dst = os.path.join(glbl.config_base, cfg_f_name) shutil.copy(cfg_src, cfg_dst) prun(self.sa, self.sp, auto_dark=False) # is xpdRE used? self.assertTrue(glbl.xpdRE.called) # is md updated? self.assertFalse(glbl.xpdRE.call_args_list[-1][1] == self.sc.md) # is prun passed eventually? self.assertTrue('sc_isprun' in glbl.xpdRE.call_args_list[-1][1]) # is auto_dark executed? -> No self.assertFalse('sc_dk_field_uid' in glbl.xpdRE.call_args_list[-1][1]) # is calibration loaded? self.assertTrue(cfg_f_name in glbl.xpdRE.call_args_list[-1][1] ['sc_calibration_file_name']) # is ScanPlan.md remain unchanged after scan? self.assertFalse('sc_isprun' in self.sp.md)
def test_facility_md(self): key_list = ["owner", "facility", "group"] for k in key_list: self.xrun.md[k] = glbl[k] self.xrun({}, ScanPlan(self.bt, ct, 1.0)) h = xpd_configuration["db"][-1] assert all(k in h.start for k in key_list) assert all(glbl[k] == h.start[k] for k in key_list)
def test_dryrun(self): self.sp = ScanPlan('ct', {'exposure': 0.1}, shutter=False) self.sc = Scan(self.sa, self.sp) self.assertEqual(self.sc.sp, self.sp) md_copy = dict(self.sc.md) dryrun(self.sa, self.sp) # is scan_md remain the same? self.assertTrue(md_copy == self.sc.md)
def test_min_exposure_time(self): bt = Beamtime("Simon", 123, [], wavelength=0.1828, custom1="A") # shorter than acq time -> ValueError glbl["frame_acq_time"] = 0.5 print("frame acq time = {}".format(glbl["frame_acq_time"])) # exposure as arg self.assertRaises(ValueError, lambda: ScanPlan(bt, ct, 0.2)) # exposure as kwarg self.assertRaises(ValueError, lambda: ScanPlan(bt, ct, exposure=0.2)) # proper frame acq time -> pass glbl["frame_acq_time"] = 0.1 ScanPlan(bt, ct, 0.2) # test with xrun xrun = CustomizedRunEngine(bt) xrun({}, ScanPlan(bt, ct, 0.2)) # safe, should pass glbl["frame_acq_time"] = 0.5 self.assertRaises(ValueError, lambda: xrun({}, ScanPlan(bt, ct, 0.2))) glbl["frame_acq_time"] = 0.1 # reset after test
def test_ct_scanplan_md(self): sp = ScanPlan(self.bt, ct, 1) sp_md = dict(sp) # md to scanplan itself self.assertEqual(sp_md["sp_plan_name"], "ct") self.assertTrue("sp_uid" in sp_md) self.assertTrue(1 in sp_md["sp_args"]) # scanplan knows bt for k, v in dict(self.bt).items(): self.assertEqual(sp_md[k], v)
def test_ct_scanplan_md(self): sp = ScanPlan(self.bt, ct, 1) sp_md = dict(sp) # md to scanplan itself self.assertEqual(sp_md['sp_plan_name'], 'ct') self.assertTrue('sp_uid' in sp_md) self.assertTrue(1 in sp_md['sp_args']) # scanplan knows bt for k, v in dict(self.bt).items(): self.assertEqual(sp_md[k], v)
def test_scanplan_yamlize(self): sp = ScanPlan(self.bt, ct, 1) # bound arguments # expected_bound_args = {'exposure': 1, 'md': None} expected_bound_args = {'exposure': 1} # py3.4 only get args self.assertEqual(dict(sp.bound_arguments), expected_bound_args) # reload reload_dict = yaml.load(sp.to_yaml()) self.assertEqual(len(reload_dict), 2) # bt and sp ## contents of chainmap self.assertEqual(reload_dict[0], sp.maps[0]) self.assertEqual(reload_dict[1], sp.maps[1]) # equality reload_scanplan = ScanPlan.from_yaml(sp.to_yaml()) other_sp = ScanPlan(self.bt, ct, 5) self.assertFalse(sp == other_sp) self.assertTrue(sp == reload_scanplan)
def test_xpdmd_insert(self): key = "xpdacq_md_version" val = XPDACQ_MD_VERSION msg_list = [] def msg_rv(msg): msg_list.append(msg) self.xrun.msg_hook = msg_rv self.xrun({}, ScanPlan(self.bt, ct, 1.0)) open_run = [el.kwargs for el in msg_list if el.command == "open_run"].pop() assert key in open_run assert open_run[key] == val
def test_auto_load_calibration(self): self.sp = ScanPlan('ct', {'exposure': 0.1}, shutter=False) # no config file in xpdUser/config_base auto_calibration_md_dict = _auto_load_calibration_file() self.assertIsNone(auto_calibration_md_dict) # one config file in xpdUser/config_base: cfg_f_name = 'srxconfig.cfg' cfg_src = os.path.join(os.path.dirname(__file__), cfg_f_name) # __file__ gives relative path cfg_dst = os.path.join(glbl.config_base, cfg_f_name) config = ConfigParser() config.read(cfg_src) with open(cfg_dst, 'w') as f_original: config.write(f_original) #shutil.copy(cfg_src, cfg_dst) auto_calibration_md_dict = _auto_load_calibration_file() # is file loaded?? self.assertTrue( 'sc_calibration_parameters' in auto_calibration_md_dict) # is information loaded in correctly? self.assertEqual( auto_calibration_md_dict['sc_calibration_parameters']['Experiment'] ['integrationspace'], 'qspace') self.assertEqual( auto_calibration_md_dict['sc_calibration_parameters']['Others'] ['uncertaintyenable'], 'True') self.assertEqual(auto_calibration_md_dict['sc_calibration_file_name'], cfg_f_name) # multiple config files in xpdUser/config_base: self.assertTrue(os.path.isfile(cfg_dst)) modified_cfg_f_name = 'srxconfig_1.cfg' modified_cfg_dst = os.path.join(glbl.config_base, modified_cfg_f_name) config = ConfigParser() config.read(cfg_src) config['Others']['avgmask'] = 'False' with open(modified_cfg_dst, 'w') as f_modified: config.write(f_modified) self.assertTrue(os.path.isfile(modified_cfg_dst)) #modified_cfg_src = os.path.join(os.path.dirname(__file__), modified_cfg_f_name) #shutil.copy(modified_cfg_src, modified_cfg_dst) modified_auto_calibration_md_dict = _auto_load_calibration_file() # is information loaded in correctly? #debug = list(map(lambda x: os.path.getmtime(x), os.listdir(glbl.config_base))) #print(debug) self.assertEqual( modified_auto_calibration_md_dict['sc_calibration_file_name'], modified_cfg_f_name) self.assertEqual( modified_auto_calibration_md_dict['sc_calibration_parameters'] ['Others']['avgmask'], 'False')
def test_start_beamtime(self): # sanity check. xpdUser directory exists. # First make sure the code works right when it doesn't exist. self.assertFalse(os.path.isdir(self.home_dir)) self.assertRaises(RuntimeError, lambda: _start_beamtime(self.PI_name, self.saf_num)) # now make it the proper thing...xpdUser directory os.mkdir(self.home_dir) self.assertTrue(os.path.isdir(self.home_dir)) # but put a file in it self.newfile = os.path.join(self.home_dir, 'touched.txt') open(self.newfile, 'a').close() self.assertTrue(os.path.isfile(self.newfile)) self.assertRaises(FileExistsError, lambda: _start_beamtime(self.PI_name, self.saf_num)) os.remove(self.newfile) # do the same but with directories self.newdir = os.path.join(self.home_dir, 'userJunk') os.mkdir(self.newdir) self.assertTrue(os.path.isdir(self.newdir)) self.assertRaises(FileExistsError, lambda: _start_beamtime(self.PI_name, self.saf_num)) os.removedirs(self.newdir) # real doing: os.mkdir(self.home_dir) self.assertTrue(os.path.isdir(self.home_dir)) self.bt = _start_beamtime(self.PI_name, self.saf_num, self.experimenters, wavelength=self.wavelength) self.assertIsInstance(self.bt, Beamtime) # test normalized md self.assertEqual('Billinge', self.bt.get('bt_piLast')) self.assertEqual('123', self.bt.get('bt_safN')) self.assertEqual(self.experimenters, self.bt.get('bt_experimenters')) self.assertEqual(self.wavelength, self.bt.get('bt_wavelength')) self.assertEqual(os.getcwd(), self.home_dir) # test prepoluate ScanPlan self.assertEqual(len(self.bt.scanplans), len(EXPO_LIST)) for sp, expect_arg in zip(self.bt.scanplans, EXPO_LIST): self.assertEqual(sp['sp_args'], (expect_arg,)) # test if yml files are saved properly for expo in EXPO_LIST: f_path = os.path.join(glbl.scanplan_dir, 'ct_{}.yml'.format(expo)) self.assertTrue(os.path.isfile(f_path)) # test if it can be reloaded for current_sp in self.bt.scanplans: reload_sp = ScanPlan.from_yaml(current_sp.to_yaml()) self.assertEqual(reload_sp, current_sp) self.assertFalse(id(reload_sp) == id(current_sp))
def test_analysis_stage_insert(self): key = "analysis_stage" val = "raw" msg_list = [] def msg_rv(msg): msg_list.append(msg) self.xrun.msg_hook = msg_rv self.xrun({}, ScanPlan(self.bt, ct, 1.0)) open_run = [el.kwargs for el in msg_list if el.command == "open_run"].pop() assert key in open_run assert open_run[key] == val
def test_calibration_client_server_md_insert(self): server_val = self.init_exp_hash_uid client_key = "detector_calibration_client_uid" msg_list = [] def msg_rv(msg): msg_list.append(msg) self.xrun.msg_hook = msg_rv glbl["auto_load_calib"] = True assert glbl["auto_load_calib"] # calibration hasn't been run -> still receive client uid self.xrun({}, ScanPlan(self.bt, ct, 1.0)) open_run = [el.kwargs for el in msg_list if el.command == "open_run"].pop() assert client_key in open_run assert open_run[client_key] == server_val # attach calib md to glbl and verify injection cfg_f_name = glbl["calib_config_name"] cfg_src = os.path.join(pytest_dir, cfg_f_name) cfg_dst = os.path.join(glbl["config_base"], cfg_f_name) shutil.copy(cfg_src, cfg_dst) with open(cfg_dst) as f: config_from_file = yaml.unsafe_load(f) glbl["calib_config_dict"] = config_from_file msg_list = [] def msg_rv(msg): msg_list.append(msg) self.xrun.msg_hook = msg_rv self.xrun({}, ScanPlan(self.bt, ct, 1.0)) open_run = [el.kwargs for el in msg_list if el.command == "open_run"].pop() assert client_key in open_run assert open_run[client_key] == server_val
def test_set_beamdump_suspender(self): # operate at full current sig = ophyd.Signal(name="ring_current") xpd_configuration["ring_current"] = sig set_beamdump_suspender(self.xrun, wait_time=0.1) sig.put(200) self.xrun({}, ScanPlan(self.bt, ct, .1)) # operate at low current, test user warnning with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") # trigger warning sig.put(30) # low current set_beamdump_suspender(self.xrun, wait_time=0.1) # check warning assert len(w) == 1 assert issubclass(w[-1].category, UserWarning)
def test_background(self): self.sp = ScanPlan('ct', {'exposure': 0.1}, shutter=False) self.sc = Scan(self.sa, self.sp) self.assertEqual(self.sc.sp, self.sp) background(self.sa, self.sp) # is xpdRE used? self.assertTrue(glbl.xpdRE.called) # is md updated? self.assertFalse(glbl.xpdRE.call_args_list[-1][1] == self.sc.md) # is md labeled correctly? self.assertTrue('sc_isbackground' in glbl.xpdRE.call_args_list[-1][1]) # is auto_dark executed? self.assertTrue('sc_dk_field_uid' in glbl.xpdRE.call_args_list[-1][1]) # is calibration loaded? -> No self.assertFalse( 'sc_calibration_file_name' in glbl.xpdRE.call_args_list[-1][1]) # is ScanPlan.md remain unchanged after scan? self.assertFalse('sc_iscalibration' in self.sp.md)
def test_dark_in_prun_can_find_a_valid_dark(self): # case 1: find a qualified dark and test if md got updated time_now = time.time() self.bt.set_wavelength(0.18448) light_cnt_time = 0.01 dark_scan_list = [] dark_uid = str(uuid.uuid4()) dark_scan_list.append((dark_uid, light_cnt_time, time_now - 600)) with open(glbl.dk_yaml, 'w') as f: yaml.dump(dark_scan_list, f) test_list = _read_dark_yaml() self.assertEqual(test_list, dark_scan_list) scanplan = ScanPlan('ct', {'exposure': light_cnt_time}, shutter=False) prun(self.sa, scanplan) self.assertTrue('sc_dk_field_uid' in glbl.xpdRE.call_args_list[-1][1]) self.assertTrue( glbl.xpdRE.call_args_list[-1][1]['sc_dk_field_uid'] == dark_uid)
def test_load_beamline_config(self): # no beamline config -> raise if os.path.exists(glbl["blconfig_path"]): os.remove(glbl["blconfig_path"]) with self.assertRaises(xpdAcqException): _load_beamline_config(glbl["blconfig_path"], test=True) # move files stem, fn = os.path.split(glbl["blconfig_path"]) src = os.path.join(pytest_dir, fn) shutil.copyfile(src, glbl["blconfig_path"]) beamline_config_dict = _load_beamline_config(glbl["blconfig_path"], test=True) assert "is_pytest" in beamline_config_dict # check md -> only is_pytest in template now self.xrun.md["beamline_config"] = beamline_config_dict self.xrun({}, ScanPlan(self.bt, ct, 1.0)) hdr = xpd_configuration["db"][-1] print(beamline_config_dict) assert hdr.start["beamline_config"] == beamline_config_dict
def test_scanplan_yamlize(self): sp = ScanPlan(self.bt, ct, 1) # bound arguments # expected_bound_args = {'exposure': 1, 'md': None} expected_bound_args = {"exposure": 1} # py3.4 only get args self.assertEqual(dict(sp.bound_arguments), expected_bound_args) # reload reload_dict = yaml.unsafe_load(sp.to_yaml()) self.assertEqual(len(reload_dict), 2) # bt and sp ## contents of chainmap self.assertEqual(reload_dict[0], sp.maps[0]) self.assertEqual(reload_dict[1], sp.maps[1]) # equality reload_scanplan = ScanPlan.from_yaml(sp.to_yaml()) other_sp = ScanPlan(self.bt, ct, 5) self.assertFalse(sp == other_sp) self.assertTrue(sp == reload_scanplan)
def _execute_start_beamtime(piname, safn, explist, wavelength=None, home_dir=None): PI_name = piname saf_num = safn _make_clean_env() os.chdir(home_dir) bt = Beamtime(PI_name, saf_num, experimenters=explist) # now populate the database with some lazy-user objects ex = Experiment('l-user', bt) sa = Sample('l-user', ex) sc01 = ScanPlan('ct', {'exposure': 0.1}) sc05 = ScanPlan('ct', {'exposure': 0.5}) sc1 = ScanPlan('ct', {'exposure': 1.0}) sc5 = ScanPlan('ct', {'exposure': 5.0}) sc10 = ScanPlan('ct', {'exposure': 10.0}) sc30 = ScanPlan('ct', {'exposure': 30.0}) return bt
def test_start_beamtime(self): # sanity check. xpdUser directory exists. # First make sure the code works right when it doesn't exist. self.assertFalse(os.path.isdir(self.home_dir)) self.assertRaises( RuntimeError, lambda: _start_beamtime(self.PI_name, self.saf_num) ) # now make it the proper thing...xpdUser directory os.mkdir(self.home_dir) self.assertTrue(os.path.isdir(self.home_dir)) # but put a file in it self.newfile = os.path.join(self.home_dir, "touched.txt") open(self.newfile, "a").close() self.assertTrue(os.path.isfile(self.newfile)) self.assertRaises( FileExistsError, lambda: _start_beamtime(self.PI_name, self.saf_num), ) os.remove(self.newfile) # do the same but with directories self.newdir = os.path.join(self.home_dir, "userJunk") os.mkdir(self.newdir) self.assertTrue(os.path.isdir(self.newdir)) self.assertRaises( FileExistsError, lambda: _start_beamtime(self.PI_name, self.saf_num), ) os.removedirs(self.newdir) # real doing: os.mkdir(self.home_dir) self.assertTrue(os.path.isdir(self.home_dir)) # copying example longterm config file pytest_dir = rs_fn("xpdacq", "tests/") config = "XPD_beamline_config.yml" configsrc = os.path.join(pytest_dir, config) shutil.copyfile(configsrc, os.path.join(self.config_dir, config)) self.bt = _start_beamtime( self.PI_name, self.saf_num, self.experimenters, wavelength=self.wavelength, test=True, ) self.assertIsInstance(self.bt, Beamtime) # test normalized md self.assertEqual("Billinge", self.bt.get("bt_piLast")) self.assertEqual("123", self.bt.get("bt_safN")) self.assertEqual(self.experimenters, self.bt.get("bt_experimenters")) self.assertEqual(self.wavelength, self.bt.get("bt_wavelength")) self.assertEqual(os.getcwd(), self.home_dir) # test prepoluate ScanPlan self.assertEqual(len(self.bt.scanplans), len(EXPO_LIST)) for sp, expect_arg in zip(list(self.bt.scanplans.values()), EXPO_LIST): self.assertEqual(sp["sp_args"], (expect_arg,)) # test if yml files are saved properly for expo in EXPO_LIST: f_path = os.path.join( glbl_dict["scanplan_dir"], "ct_{}.yml".format(expo) ) self.assertTrue(os.path.isfile(f_path)) # test if it can be reloaded for current_sp in self.bt.scanplans.values(): reload_sp = ScanPlan.from_yaml(current_sp.to_yaml()) self.assertEqual(reload_sp, current_sp) self.assertFalse(id(reload_sp) == id(current_sp))
def test_xrun_with_xpdAcqPlans(self): exp = 5 # test with ct msg_list = [] def msg_rv(msg): msg_list.append(msg) self.xrun.msg_hook = msg_rv self.xrun({}, ScanPlan(self.bt, ct, exp)) open_run = [el.kwargs for el in msg_list if el.command == "open_run"].pop() self.assertEqual(open_run["sp_type"], "ct") self.assertEqual(open_run["sp_requested_exposure"], exp) # test with Tramp Tstart, Tstop, Tstep = 300, 200, 10 msg_list = [] def msg_rv(msg): msg_list.append(msg) self.xrun.msg_hook = msg_rv traj_list = [] # courtesy of bluesky test temp_controller = xpd_configuration["temp_controller"] callback = collector(temp_controller.readback.name, traj_list) self.xrun( {}, ScanPlan(self.bt, Tramp, exp, Tstart, Tstop, Tstep), subs={"event": callback}, ) # verify trajectory Num, diff = _nstep(Tstart, Tstop, Tstep) expected_traj = np.linspace(Tstart, Tstop, Num) assert np.all(traj_list == expected_traj) # verify md open_run = [el.kwargs for el in msg_list if el.command == "open_run"].pop() self.assertEqual(open_run["sp_type"], "Tramp") self.assertEqual(open_run["sp_requested_exposure"], exp) self.assertEqual(open_run["sp_startingT"], Tstart) self.assertEqual(open_run["sp_endingT"], Tstop) self.assertEqual(open_run["sp_requested_Tstep"], Tstep) # test with tseries delay, num = 0.1, 5 msg_list = [] def msg_rv(msg): msg_list.append(msg) self.xrun.msg_hook = msg_rv self.xrun({}, ScanPlan(self.bt, tseries, exp, delay, num)) open_run = [el.kwargs for el in msg_list if el.command == "open_run"].pop() self.assertEqual(open_run["sp_type"], "tseries") self.assertEqual(open_run["sp_requested_exposure"], exp) self.assertEqual(open_run["sp_requested_delay"], delay) self.assertEqual(open_run["sp_requested_num"], num) # test with Tlist T_list = [300, 256, 128] msg_list = [] def msg_rv(msg): msg_list.append(msg) traj_list = [] # courtesy of bluesky test temp_controller = xpd_configuration["temp_controller"] callback = collector(temp_controller.readback.name, traj_list) self.xrun.msg_hook = msg_rv self.xrun({}, ScanPlan(self.bt, Tlist, exp, T_list), subs={"event": callback}) # verify trajectory assert T_list == traj_list # verify md open_run = [el.kwargs for el in msg_list if el.command == "open_run"].pop() self.assertEqual(open_run["sp_type"], "Tlist") self.assertEqual(open_run["sp_requested_exposure"], exp) self.assertEqual(open_run["sp_T_list"], T_list)
def test_scanplan_roundtrip(self): sp = ScanPlan(self.bt, ct, 1) reload_sp = ScanPlan.from_yaml(sp.to_yaml()) self.assertEqual(reload_sp, sp)
def test_ct_scanplan_autoname(self): sp = ScanPlan(self.bt, ct, 1) # std_f_name = 'ct_1_None.yml' #py3.4 only gets args std_f_name = 'ct_1.yml' # py3.4 only gets args yaml_name = os.path.basename(sp.default_yaml_path()) self.assertEqual(yaml_name, std_f_name)
def test_scanplan_roundtrip(self): bt = Beamtime('Simon', '123', [], wavelength=0.1828) sp = ScanPlan(self.bt, ct, 1) reload_sp = ScanPlan.from_yaml(sp.to_yaml()) self.assertEqual(reload_sp, sp)
def test_scanplan_roundtrip(self): bt = Beamtime("Simon", "123", [], wavelength=0.1828) sp = ScanPlan(self.bt, ct, 1) reload_sp = ScanPlan.from_yaml(sp.to_yaml()) self.assertEqual(reload_sp, sp)
def test_ct_scanplan_autoname(self): sp = ScanPlan(self.bt, ct, 1) # std_f_name = 'ct_1_None.yml' #py3.4 only gets args std_f_name = "ct_1.yml" # py3.4 only gets args yaml_name = os.path.basename(sp.default_yaml_path()) self.assertEqual(yaml_name, std_f_name)