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 setUp(self): self.base_dir = glbl.base self.home_dir = glbl.home self.config_dir = glbl.xpdconfig os.chdir(self.base_dir) if os.path.isdir(self.home_dir): shutil.rmtree(self.home_dir) if os.path.isdir(self.config_dir): shutil.rmtree(self.config_dir) os.makedirs(self.config_dir, exist_ok=True) self.PI_name = 'Billinge ' self.saf_num = 234 self.wavelength = 0.1812 self.experimenters = [('van der Banerjee', 'S0ham', 1), ('Terban ', ' Max', 2)] self.saffile = os.path.join(self.config_dir, 'saf{}.yml'.format(self.saf_num)) #_make_clean_env() loadinfo = { 'saf number': self.saf_num, 'PI last name': self.PI_name, 'experimenter list': self.experimenters } with open(self.saffile, 'w') as fo: yaml.dump(loadinfo, fo) self.bt = _start_beamtime(self.saf_num, home_dir=self.home_dir) self.stbt_list = [ 'bt_bt.yml', 'ex_l-user.yml', 'sa_l-user.yml', 'sp_ct_0.1.yml', 'sp_ct_0.5.yml', 'sp_ct_1.yml', 'sp_ct_5.yml', 'sp_ct_10.yml', 'sp_ct_30.yml' ]
def test_import_sample_info_core_function(self): # no bt, default argument will fail self.assertRaises(TypeError, lambda: _import_sample_info(bt=None)) # make bt but no spreadsheet self.bt = _start_beamtime(self.PI_name, self.saf_num, self.experimenters, wavelength=self.wavelength) # expect FileNotFoundError as no spreadsheet self.assertRaises(FileNotFoundError, lambda: _import_sample_info(bt=self.bt)) # copy spreadsheet xlf = '300000_sample.xlsx' src = os.path.join(os.path.dirname(__file__), xlf) shutil.copyfile(src, os.path.join(glbl.import_dir, xlf)) # expect to pass with explicit argument _import_sample_info(300000, self.bt) # check imported sample metadata for sample in self.bt.samples: # Sample is a ChainMap with self.maps[1] == bt self.assertEqual(sample.maps[1], self.bt) # expect ValueError with inconsistent SAF_num between bt and input self.bt['bt_safN'] = 300179 self.assertTrue(os.path.isfile(os.path.join(glbl.import_dir, xlf))) self.assertRaises(ValueError, lambda: _import_sample_info(300000, self.bt)) # expct TypeError with incorrect beamtime self.assertRaises(TypeError, lambda: _import_sample_info(bt=set()))
def test_start_beamtime(self): os.chdir(self.base_dir) # clean environment checked above, so only check a case that works os.mkdir(self.home_dir) os.mkdir(self.config_dir) loadinfo = { 'saf number': self.saf_num, 'PI last name': self.PI_name, 'experimenter list': self.experimenters } with open(self.saffile, 'w') as fo: yaml.dump(loadinfo, fo) self.assertTrue(os.path.isfile(self.saffile)) bt = _start_beamtime(self.saf_num, home_dir=self.home_dir) self.assertEqual(os.getcwd(), self.home_dir) # we should be in home, are we? self.assertIsInstance( bt, bts.Beamtime) # there should be a bt object, is there? self.assertEqual(bt.md['bt_experimenters'], [('van der Banerjee', 'S0ham', 1), ('Terban', 'Max', 2)]) self.assertEqual(bt.md['bt_piLast'], 'Billinge') self.assertEqual(bt.md['bt_safN'], 123) self.assertEqual(bt.md['bt_wavelength'], None) os.chdir(self.base_dir) newobjlist = _get_yaml_list() strtScnLst = [ 'bt_bt.yml', 'ex_l-user.yml', 'sa_l-user.yml', 'sp_ct_0.1.yml', 'sp_ct_0.5.yml', 'sp_ct_1.yml', 'sp_ct_5.yml', 'sp_ct_10.yml', 'sp_ct_30.yml' ] self.assertEqual(newobjlist, strtScnLst)
def setUp(self): self.base_dir = glbl.base self.home_dir = os.path.join(self.base_dir, 'xpdUser') self.config_dir = os.path.join(self.base_dir, 'xpdConfig') self.PI_name = 'Billinge ' self.saf_num = 300000 # must be 30000 for proper load of config yaml => don't change self.wavelength = 0.1812 self.experimenters = [('van der Banerjee', 'S0ham', 1), ('Terban ', ' Max', 2)] # make xpdUser dir. That is required for simulation os.makedirs(self.home_dir, exist_ok=True) self.bt = _start_beamtime(self.PI_name, self.saf_num, self.experimenters, wavelength=self.wavelength) xlf = '300000_sample.xlsx' src = os.path.join(os.path.dirname(__file__), xlf) shutil.copyfile(src, os.path.join(glbl.import_dir, xlf)) import_sample_info(self.saf_num, self.bt) self.sp = ScanPlan(self.bt, ct, 5) glbl.shutter_control = True self.xrun = CustomizedRunEngine(self.bt) open_collection('unittest') # simulation objects glbl.area_det = SimulatedPE1C('pe1c', {'pe1_image': lambda: 5}) glbl.temp_controller = be.motor glbl.shutter = be.Mover('motor', {'motor': lambda x: x}, {'x': 0})
def setUp(self): self.base_dir = glbl.base self.home_dir = os.path.join(self.base_dir, 'xpdUser') self.config_dir = os.path.join(self.base_dir, 'xpdConfig') self.PI_name = 'Billinge ' self.saf_num = 300000 # must be 30000 for proper load of config yaml => don't change self.wavelength = 0.1812 self.experimenters = [('van der Banerjee', 'S0ham', 1), ('Terban ', ' Max', 2)] # make xpdUser dir. That is required for simulation os.makedirs(self.home_dir, exist_ok=True) # set simulation objects glbl.area_det = SimulatedPE1C('pe1c', {'pe1_image': lambda: 5}) print("AT SETUP: numbe_of_sets = {}, images_per_set = {}, " "acquire_time ={}" .format(glbl.area_det.number_of_sets.get(), glbl.area_det.images_per_set.get(), glbl.area_det.cam.acquire_time.get())) glbl.temp_controller = cs700 glbl.shutter = shctl1 self.bt = _start_beamtime(self.PI_name, self.saf_num, self.experimenters, wavelength=self.wavelength) xlf = '300000_sample.xlsx' src = os.path.join(os.path.dirname(__file__), xlf) shutil.copyfile(src, os.path.join(glbl.import_dir, xlf)) import_sample_info(self.saf_num, self.bt) self.xrun = CustomizedRunEngine(self.bt) open_collection('unittest')
def setUp(self): self.base_dir = glbl.base self.home_dir = glbl.home self.config_dir = glbl.xpdconfig os.makedirs(self.config_dir, exist_ok=True) self.PI_name = 'Billinge ' self.saf_num = 123 self.wavelength = 0.1812 self.experimenters = [('van der Banerjee', 'S0ham', 1), ('Terban ', ' Max', 2)] self.saffile = os.path.join(self.config_dir, 'saf{}.yml'.format(self.saf_num)) loadinfo = { 'saf number': self.saf_num, 'PI last name': self.PI_name, 'experimenter list': self.experimenters } with open(self.saffile, 'w') as fo: yaml.dump(loadinfo, fo) self.bt = _start_beamtime(self.saf_num, home_dir=self.home_dir) self.stbt_list = [ 'bt_bt.yml', 'ex_l-user.yml', 'sa_l-user.yml', 'sc_ct.1s.yml', 'sc_ct.5s.yml', 'sc_ct1s.yml', 'sc_ct5s.yml', 'sc_ct10s.yml', 'sc_ct30s.yml' ] self.ex = Experiment('validateDark_unittest', self.bt) self.sa = Sample('unitttestSample', self.ex)
def bt(home_dir): # start a beamtime pi = "Billinge " saf_num = 300000 wavelength = xpd_wavelength experimenters = [["van der Banerjee", "S0ham", 1], ["Terban ", " Max", 2]] # copying example longterm config file os.makedirs(glbl_dict["xpdconfig"], exist_ok=True) pytest_dir = rs_fn("xpdacq", "tests/") config = "XPD_beamline_config.yml" configsrc = os.path.join(pytest_dir, config) shutil.copyfile(configsrc, glbl_dict["blconfig_path"]) assert os.path.isfile(glbl_dict["blconfig_path"]) bt = _start_beamtime(pi, saf_num, experimenters, wavelength=wavelength, test=True) # spreadsheet xlf = "300000_sample.xlsx" src = os.path.join(pytest_dir, xlf) shutil.copyfile(src, os.path.join(glbl_dict["import_dir"], xlf)) import_sample_info(saf_num, bt) yield bt # when we are done with the glbl delete the folders. shutil.rmtree(glbl_dict["home"])
def test_end_beamtime(self): # end_beamtime has been run self.assertRaises(SystemExit, lambda: _end_beamtime()) self.PI_name = 'Billinge ' self.saf_num = 234 self.wavelength = 0.1812 self.experimenters = [('van der Banerjee', 'S0ham', 1), ('Terban ', ' Max', 2)] self.saffile = os.path.join(self.config_dir, 'saf{}.yml'.format(self.saf_num)) loadinfo = { 'saf number': self.saf_num, 'PI last name': self.PI_name, 'experimenter list': self.experimenters } os.makedirs(self.config_dir, exist_ok=True) with open(self.saffile, 'w') as fo: yaml.dump(loadinfo, fo) self.bt = _start_beamtime(self.saf_num, home_dir=self.home_dir) bt_path_src = os.path.join(glbl.yaml_dir, 'bt_bt.yml') bt_path_dst = os.path.join(glbl.import_dir, 'bt_bt.yml') # move out for now, no bt shutil.move(bt_path_src, bt_path_dst) self.assertTrue(os.path.isfile(bt_path_dst)) self.assertFalse(os.path.isfile(bt_path_src)) self.assertRaises(SystemExit, lambda: _load_bt(glbl.yaml_dir)) # move back and test archieving funtionality shutil.move(bt_path_dst, bt_path_src) self.assertTrue(os.path.isfile(bt_path_src)) self.assertFalse(os.path.isfile(bt_path_dst)) bt_uid = self.bt.md['bt_uid'] archive_full_name = _execute_end_beamtime(self.PI_name, self.saf_num, bt_uid, glbl.base) test_tar_name = '_'.join([ self.PI_name.strip().replace(' ', ''), str(self.saf_num).strip(), strftime('%Y-%m-%d-%H%M'), bt_uid ]) # is tar file name correct? self.assertEqual(archive_full_name, os.path.join(glbl.archive_dir, test_tar_name)) # are contents tared correctly? archive_test_dir = os.path.join(glbl.home, 'tar_test') os.makedirs(archive_test_dir, exist_ok=True) shutil.unpack_archive(archive_full_name + '.tar', archive_test_dir) content_list = os.listdir(archive_test_dir) # is tarball starting from xpdUser? self.assertTrue('xpdUser' in content_list) # is every directory included basename_list = list(map(os.path.basename, glbl.allfolders)) _exclude_list = ['xpdUser', 'xpdConfig', 'yml'] for el in _exclude_list: basename_list.remove(el) for el in basename_list: self.assertTrue( el in os.listdir(os.path.join(archive_test_dir, 'xpdUser'))) # now test deleting directories _delete_home_dir_tree() self.assertTrue(len(os.listdir(glbl.home)) == 0)
def test_blocking_beamtime(self): os.mkdir(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)) # test if start_beamtime properly modify the state _start_beamtime(self.PI_name, self.saf_num, test=True) assert glbl['_active_beamtime'] # test if it blocks after beamtime glbl['_active_beamtime'] = False self.assertRaises( xpdAcqError, lambda: _start_beamtime(self.PI_name, self.saf_num, test=True)) # restore glbl['_active_beamtime'] = True
def test_import_sample_info_core_function(self): # no bt, default argument will fail self.assertRaises(TypeError, lambda: _import_sample_info(bt=None)) # make bt but no spreadsheet 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, ) # expect FileNotFoundError as no spreadsheet xlf = "300000_sample.xlsx" self.assertFalse(os.path.isfile(os.path.join(glbl["import_dir"], xlf))) self.assertRaises(FileNotFoundError, lambda: _import_sample_info(bt=self.bt)) # copy spreadsheet xlf = "300000_sample.xlsx" src = os.path.join(self.pkg_rs, xlf) shutil.copyfile(src, os.path.join(glbl["import_dir"], xlf)) # problematic ones xlf2 = "999999_sample.xlsx" src = os.path.join(os.path.dirname(__file__), xlf2) shutil.copyfile(src, os.path.join(glbl["import_dir"], xlf2)) # test with ordinary import ## # expect to pass with explicit argument _import_sample_info(300000, self.bt) # check imported sample metadata for sample in self.bt.samples.values(): # Sample is a ChainMap with self.maps[1] == bt self.assertEqual(sample.maps[1], self.bt) # expect ValueError with inconsistent SAF_num between bt and input self.bt["bt_safN"] = str(300179) self.assertTrue(os.path.isfile(os.path.join(glbl["import_dir"], xlf))) self.assertRaises(ValueError, lambda: _import_sample_info(300000, self.bt)) # expct TypeError with incorrect beamtime self.assertRaises(TypeError, lambda: _import_sample_info(bt=set())) # error when validate the md self.bt["bt_safN"] = str(999999) self.assertRaises( RuntimeError, lambda: _import_sample_info(999999, self.bt, validate_only=True), ) # test get_md_method sample_obj_list = [el for el in self.bt.samples.values()] for i, el in enumerate(sample_obj_list): self.assertEqual(dict(el), self.bt.samples.get_md(i))
def test_end_beamtime(self): _required_info = ['bt_piLast', 'bt_safN', 'bt_uid'] # end_beamtime has been run self.assertRaises(FileNotFoundError, lambda: _end_beamtime()) # entire trip. _start_beamtime to _end_beamtime self.bt = _start_beamtime(self.PI_name, self.saf_num, self.experimenters, wavelength=self.wavelength) bt_path_src = os.path.join(glbl.yaml_dir, 'bt_bt.yml') bt_path_dst = os.path.join(glbl.import_dir, 'bt_bt.yml') # move out for now, no bt shutil.move(bt_path_src, bt_path_dst) self.assertTrue(os.path.isfile(bt_path_dst)) self.assertFalse(os.path.isfile(bt_path_src)) self.assertRaises(SystemExit, lambda: _load_bt(glbl.yaml_dir)) # move back and test archieving funtionality shutil.move(bt_path_dst, bt_path_src) self.assertTrue(os.path.isfile(bt_path_src)) self.assertFalse(os.path.isfile(bt_path_dst)) pi_name = self.bt.get('bt_piLast') saf_num = self.bt.get('bt_safN') bt_uid = self.bt.get('bt_uid') archive_name = _load_bt_info(self.bt, _required_info) archive_full_name = _tar_user_data(archive_name) test_tar_name = '_'.join([pi_name, saf_num, bt_uid, strftime('%Y-%m-%d-%H%M')]) # is tar file name correct? self.assertEqual(archive_full_name, os.path.join(glbl.archive_dir, test_tar_name)) # are contents tared correctly? archive_test_dir = os.path.join(glbl.home, 'tar_test') os.makedirs(archive_test_dir, exist_ok=True) shutil.unpack_archive(archive_full_name + '.tar', archive_test_dir) content_list = os.listdir(archive_test_dir) # is tarball starting from xpdUser? self.assertTrue('xpdUser' in content_list) # is every directory included basename_list = list(map(os.path.basename, glbl.allfolders)) _exclude_list = ['xpdUser', 'xpdConfig', 'yml', 'samples', 'scanplans', 'experiments'] # _exclude_list means sub directories and top directories # that will not be in tar structure for el in _exclude_list: basename_list.remove(el) for el in basename_list: self.assertTrue(el in os.listdir(os.path.join(archive_test_dir, 'xpdUser'))) # now test deleting directories _delete_home_dir_tree() self.assertTrue(len(os.listdir(glbl.home)) == 0)
def setUp(self): self.base_dir = Path(glbl["base"]) self.home_dir = Path(glbl["home_dir"]) self.config_dir = Path(glbl["xpdconfig"]) self.PI_name = "Billinge " # must be 30000 for proper load of config yaml => don't change self.saf_num = 300000 self.wavelength = 0.1812 self.experimenters = [ ("van der Banerjee", "S0ham", 1), ("Terban ", " Max", 2), ] # make xpdUser dir. That is required for simulation if self.home_dir.is_dir(): shutil.rmtree(self.home_dir) self.home_dir.mkdir() # set simulation objects db = databroker.v1.temp() configure_device( area_det=pe1c, temp_controller=cs700, shutter=shctl1, db=db, filter_bank=fb, ) if self.config_dir.is_dir(): shutil.rmtree(self.config_dir) self.config_dir.mkdir() pytest_dir = Path(rs_fn("xpdacq", "tests/")) config = "XPD_beamline_config.yml" configsrc = pytest_dir.joinpath(config) shutil.copyfile(configsrc, glbl["blconfig_path"]) self.bt = _start_beamtime( self.PI_name, self.saf_num, self.experimenters, wavelength=self.wavelength, test=True, ) xlf = "300000_sample.xlsx" src = os.path.join(os.path.dirname(__file__), xlf) shutil.copyfile(src, os.path.join(glbl["import_dir"], xlf)) import_sample_info(self.saf_num, self.bt) self.xrun = CustomizedRunEngine({}) self.xrun.beamtime = self.bt # link mds self.xrun.subscribe(db.v1.insert, "all") # grad init_exp_hash_uid self.init_exp_hash_uid = glbl["exp_hash_uid"]
def test_end_beamtime(self): # end_beamtime has been run self.assertRaises(SystemExit, lambda:_end_beamtime()) self.PI_name = 'Billinge ' self.saf_num = 234 self.wavelength = 0.1812 self.experimenters = [('van der Banerjee','S0ham',1),('Terban ',' Max',2)] self.saffile = os.path.join(self.config_dir,'saf{}.yml'.format(self.saf_num)) loadinfo = {'saf number':self.saf_num,'PI last name':self.PI_name,'experimenter list':self.experimenters} os.makedirs(self.config_dir, exist_ok = True) with open(self.saffile, 'w') as fo: yaml.dump(loadinfo,fo) self.bt = _start_beamtime(self.saf_num,home_dir=self.home_dir) bt_path_src = os.path.join(glbl.yaml_dir,'bt_bt.yml') bt_path_dst = os.path.join(glbl.import_dir, 'bt_bt.yml') # move out for now, no bt shutil.move(bt_path_src, bt_path_dst) self.assertTrue(os.path.isfile(bt_path_dst)) self.assertFalse(os.path.isfile(bt_path_src)) self.assertRaises(SystemExit, lambda:_load_bt(glbl.yaml_dir)) # move back and test archieving funtionality shutil.move(bt_path_dst, bt_path_src) self.assertTrue(os.path.isfile(bt_path_src)) self.assertFalse(os.path.isfile(bt_path_dst)) bt_uid = self.bt.md['bt_uid'] archive_full_name = _execute_end_beamtime(self.PI_name, self.saf_num, bt_uid, glbl.base) test_tar_name = '_'.join([self.PI_name.strip().replace(' ', ''), str(self.saf_num).strip(), strftime('%Y-%m-%d-%H%M'), bt_uid]) # is tar file name correct? self.assertEqual(archive_full_name, os.path.join(glbl.archive_dir, test_tar_name)) # are contents tared correctly? archive_test_dir = os.path.join(glbl.home,'tar_test') os.makedirs(archive_test_dir, exist_ok = True) shutil.unpack_archive(archive_full_name+'.tar', archive_test_dir) content_list = os.listdir(archive_test_dir) # is tarball starting from xpdUser? self.assertTrue('xpdUser' in content_list) # is every directory included basename_list = list(map(os.path.basename, glbl.allfolders)) _exclude_list = ['xpdUser','xpdConfig','yml'] for el in _exclude_list: basename_list.remove(el) for el in basename_list: self.assertTrue(el in os.listdir(os.path.join(archive_test_dir,'xpdUser'))) # now test deleting directories _delete_home_dir_tree() self.assertTrue(len(os.listdir(glbl.home)) == 0)
def setUp(self): self.base_dir = glbl.base self.home_dir = glbl.home self.config_dir = glbl.xpdconfig os.makedirs(self.config_dir, exist_ok=True) self.PI_name = 'Billinge ' self.saf_num = 123 self.wavelength = 0.1812 self.experimenters = [('van der Banerjee','S0ham',1),('Terban ',' Max',2)] self.saffile = os.path.join(self.config_dir,'saf{}.yml'.format(self.saf_num)) loadinfo = {'saf number':self.saf_num,'PI last name':self.PI_name,'experimenter list':self.experimenters} with open(self.saffile, 'w') as fo: yaml.dump(loadinfo,fo) self.bt = _start_beamtime(self.saf_num,home_dir=self.home_dir) self.stbt_list = ['bt_bt.yml','ex_l-user.yml','sa_l-user.yml','sc_ct.1s.yml','sc_ct.5s.yml','sc_ct1s.yml','sc_ct5s.yml','sc_ct10s.yml','sc_ct30s.yml'] self.ex = Experiment('validateDark_unittest', self.bt) self.sa = Sample('unitttestSample', self.ex)
def setUp(self): self.base_dir = glbl.base self.home_dir = os.path.join(self.base_dir, 'xpdUser') self.config_dir = os.path.join(self.base_dir, 'xpdConfig') self.PI_name = 'Billinge ' self.saf_num = 30079 # must be 30079 for proper load of config yaml => don't change self.wavelength = 0.1812 self.experimenters = [('van der Banerjee', 'S0ham', 1), ('Terban ', ' Max', 2)] # make xpdUser dir. That is required for simulation os.makedirs(self.home_dir, exist_ok=True) self.bt = _start_beamtime(self.PI_name, self.saf_num, self.experimenters, wavelength=self.wavelength) xlf = '30079_sample.xlsx' src = os.path.join(os.path.dirname(__file__), xlf) shutil.copyfile(src, os.path.join(glbl.xpdconfig, xlf)) import_sample(self.saf_num, self.bt)
def test_start_beamtime(self): os.chdir(self.base_dir) # clean environment checked above, so only check a case that works os.mkdir(self.home_dir) os.mkdir(self.config_dir) loadinfo = {'saf number':self.saf_num,'PI last name':self.PI_name,'experimenter list':self.experimenters} with open(self.saffile, 'w') as fo: yaml.dump(loadinfo,fo) self.assertTrue(os.path.isfile(self.saffile)) bt = _start_beamtime(self.saf_num,home_dir=self.home_dir) self.assertEqual(os.getcwd(),self.home_dir) # we should be in home, are we? self.assertIsInstance(bt,bts.Beamtime) # there should be a bt object, is there? self.assertEqual(bt.md['bt_experimenters'],[('van der Banerjee','S0ham',1),('Terban','Max',2)]) self.assertEqual(bt.md['bt_piLast'],'Billinge') self.assertEqual(bt.md['bt_safN'],123) self.assertEqual(bt.md['bt_wavelength'],None) os.chdir(self.base_dir) newobjlist = _get_yaml_list() strtScnLst = ['bt_bt.yml','ex_l-user.yml','sa_l-user.yml','sp_ct_0.1.yml','sp_ct_0.5.yml','sp_ct_1.yml','sp_ct_5.yml','sp_ct_10.yml','sp_ct_30.yml'] self.assertEqual(newobjlist,strtScnLst)
def setUp(self): self.base_dir = glbl.base self.home_dir = os.path.join(self.base_dir, 'xpdUser') self.config_dir = os.path.join(self.base_dir, 'xpdConfig') self.PI_name = 'Billinge ' self.saf_num = 30079 # must be 30079 for proper load of config yaml => don't change self.wavelength = 0.1812 self.experimenters = [('van der Banerjee', 'S0ham', 1), ('Terban ', ' Max', 2)] # make xpdUser dir. That is required for simulation os.makedirs(self.home_dir, exist_ok=True) self.bt = _start_beamtime(self.PI_name, self.saf_num, self.experimenters, wavelength=self.wavelength) xlf = '300000_sample.xlsx' src = os.path.join(os.path.dirname(__file__), xlf) shutil.copyfile(src, os.path.join(glbl.import_dir, xlf)) # simulation objects glbl.area_det = SimulatedPE1C('pe1c', {'pe1_image': lambda: 5}) glbl.temp_controller = be.motor glbl.shutter = MagicMock()
def setUp(self): self.base_dir = glbl.base self.home_dir = glbl.home self.config_dir = glbl.xpdconfig os.chdir(self.base_dir) if os.path.isdir(self.home_dir): shutil.rmtree(self.home_dir) if os.path.isdir(self.config_dir): shutil.rmtree(self.config_dir) os.makedirs(self.config_dir, exist_ok=True) self.PI_name = 'Billinge ' self.saf_num = 234 self.wavelength = 0.1812 self.experimenters = [('van der Banerjee','S0ham',1),('Terban ',' Max',2)] self.saffile = os.path.join(self.config_dir,'saf{}.yml'.format(self.saf_num)) #_make_clean_env() loadinfo = {'saf number':self.saf_num,'PI last name':self.PI_name,'experimenter list':self.experimenters} with open(self.saffile, 'w') as fo: yaml.dump(loadinfo,fo) self.bt = _start_beamtime(self.saf_num,home_dir=self.home_dir) self.stbt_list = ['bt_bt.yml','ex_l-user.yml','sa_l-user.yml','sp_ct.1s.yml','sp_ct.5s.yml','sp_ct1s.yml','sp_ct5s.yml','sp_ct10s.yml','sp_ct30s.yml']
def setUp(self): self.base_dir = glbl["base"] self.home_dir = os.path.join(self.base_dir, "xpdUser") self.config_dir = os.path.join(self.base_dir, "xpdConfig") self.PI_name = "Billinge " # must be 30079 for proper load of config yaml => don't change self.saf_num = 30079 self.wavelength = 0.1812 self.experimenters = [ ("van der Banerjee", "S0ham", 1), ("Terban ", " Max", 2), ] # make xpdUser dir. That is required for simulation os.makedirs(self.home_dir, exist_ok=True) os.makedirs(self.config_dir, exist_ok=True) # set simulation objects configure_device( db=db, shutter=shctl1, area_det=pe1c, temp_controller=cs700, filter_bank=fb, ) pytest_dir = rs_fn("xpdacq", "tests/") config = "XPD_beamline_config.yml" configsrc = os.path.join(pytest_dir, config) shutil.copyfile(configsrc, os.path.join(glbl["xpdconfig"], config)) assert os.path.isfile(os.path.join(glbl["xpdconfig"], config)) self.bt = _start_beamtime( self.PI_name, self.saf_num, self.experimenters, wavelength=self.wavelength, test=True, ) xlf = "300000_sample.xlsx" src = os.path.join(pytest_dir, xlf) shutil.copyfile(src, os.path.join(glbl["import_dir"], xlf))
def test_import_sample_info(self): # no bt, default argument will fail self.assertRaises(FileNotFoundError, lambda: import_sample_info()) # make bt but no spreadsheet self.bt = _start_beamtime(self.PI_name, self.saf_num, self.experimenters, wavelength=self.wavelength) # expect FileNotFoundError as no spreadsheet self.assertRaises(FileNotFoundError, lambda: import_sample_info()) # copy spreadsheet xlf = '300000_sample.xlsx' src = os.path.join(os.path.dirname(__file__), xlf) shutil.copyfile(src, os.path.join(glbl.import_dir, xlf)) # expect to pass with default argument import_sample_info() # check imported sample metadata for sample in self.bt.samples: # Sample is a ChainMap with self.maps[1] == bt self.assertEqual(sample.maps[1], self.bt) # expect to pass with explicit argument import_sample_info(300000, self.bt) # check imported sample metadata for sample in self.bt.samples: # Sample is a ChainMap with self.maps[1] == bt self.assertEqual(sample.maps[1], self.bt) # expect ValueError with inconsistent SAF_num between bt and input self.bt['bt_safN'] = 300179 self.assertTrue(os.path.isfile(os.path.join(glbl.import_dir, xlf))) self.assertRaises(ValueError, lambda: import_sample_info(300000)) # expct TypeError with incorrect beamtime self.assertRaises(TypeError, lambda: import_sample_info(bt=set()))
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_end_beamtime(self): _required_info = ["bt_piLast", "bt_safN", "bt_uid"] # end_beamtime has been run os.makedirs(self.home_dir, exist_ok=True) self.assertRaises(FileNotFoundError, lambda: _end_beamtime()) # entire trip. _start_beamtime to _end_beamtime # 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, ) bt_path_src = os.path.join(glbl_dict["yaml_dir"], "bt_bt.yml") bt_path_dst = os.path.join(glbl_dict["import_dir"], "bt_bt.yml") # move out for now, no bt shutil.move(bt_path_src, bt_path_dst) self.assertTrue(os.path.isfile(bt_path_dst)) self.assertFalse(os.path.isfile(bt_path_src)) self.assertRaises(SystemExit, lambda: _load_bt(glbl_dict["yaml_dir"])) # move back and test archieving funtionality shutil.move(bt_path_dst, bt_path_src) self.assertTrue(os.path.isfile(bt_path_src)) self.assertFalse(os.path.isfile(bt_path_dst)) pi_name = self.bt.get("bt_piLast") saf_num = self.bt.get("bt_safN") bt_uid = self.bt.get("bt_uid") archive_name = _load_bt_info(self.bt, _required_info) os.makedirs(glbl_dict['archive_dir']) assert os.path.isdir(glbl_dict['archive_dir']) archive_full_name, local_archive_name = _tar_user_data(archive_name) test_tar_name = '_'.join([pi_name, saf_num, bt_uid, strftime('%Y-%m-%d-%H%M')]) # is tar file name correct? self.assertEqual(archive_full_name, os.path.join(glbl_dict['archive_dir'], test_tar_name)) # are contents tared correctly? # archive_test_dir = os.path.join(glbl_dict['home'], 'tar_test') content_list = os.listdir(archive_full_name) # is remote copy the same name as local archive? assert os.path.basename(local_archive_name) in content_list assert len(content_list) == 1 # is every directory included full_fp_list = list(map(os.path.basename, glbl_dict['allfolders'])) exclude_fp_list = ['xpdUser', 'xpdConfig', 'yml', 'samples', 'scanplans'] bkg_fp_list = [el for el in full_fp_list if el not in exclude_fp_list] # exclude top dirs remote_fp_list = os.listdir(os.path.join(archive_full_name, local_archive_name)) # difference should be empty set assert not set(bkg_fp_list).difference(remote_fp_list) # hidden files should be excluded from the archive assert not list(glob.glob(archive_full_name + "**/.*")) # now test deleting directories _delete_local_archive(local_archive_name) self.assertFalse(os.path.isdir(local_archive_name))
Sample = Sample ct = ct Tramp = Tramp Tlist = Tlist tseries = tseries run_calibration = run_calibration xpd_configuration = xpd_configuration print("INFO: Initializing the XPD data acquisition environment ...") glbl, bt, xrun = ipysetup(area_det=pe1c, shutter=shctl1, temp_controller=cs700, filter_bank=fb, ring_current=ring_current, db=db) if not bt: bt = _start_beamtime("Billinge", 300000, wavelength=0.1675) xrun.beamtime = bt print("INFO: Initialized glbl, bt, xrun.") xrun.subscribe(Publisher("localhost:5577", prefix=b'raw')) print("INFO: Publish data to localhost port 5567 with prefix 'raw'.") if Path(glbl["home"]).is_dir(): os.chdir(glbl["home"]) print("INFO: Changed home to {}".format(glbl["home"])) elif Path(glbl["base"]).is_dir(): os.chdir(glbl["base"]) print("INFO: Changed home to {}".format(glbl["base"])) print("OK, ready to go. To continue, follow the steps in the xpdAcq" "documentation at http://xpdacq.github.io/xpdacq") # delete useless names del os, Path, ipysetup, Publisher, temp, xpd_pe1c