def test_appendscan(session): m1 = session.getDevice('motor') m2 = session.getDevice('motor2') det = session.getDevice('det') scan(m1, 0, 1, 3, det, t=0.) dataman = session.experiment.data dataset1 = dataman.getLastScans()[-1] assert dataset1.startpositions == [[0], [1], [2]] appendscan(3, 2) dataset2 = dataman.getLastScans()[-1] assert dataset2.continuation == [dataset1.uid] assert dataset2.startpositions == [[4], [6], [8]] appendscan(-3) dataset3 = dataman.getLastScans()[-1] assert dataset3.continuation == [dataset2.uid, dataset1.uid] assert dataset3.startpositions == [[-1], [-2], [-3]] appendscan(-3) dataset4 = dataman.getLastScans()[-1] assert dataset4.continuation == [dataset3.uid, dataset2.uid, dataset1.uid] assert dataset4.startpositions == [[-4], [-5], [-6]] scan([m2, m1], [0, 10], [1, 2], 3, det, t=0.) appendscan(3) dataset5 = dataman.getLastScans()[-1] assert dataset5.startpositions == [[3, 16], [4, 18], [5, 20]] appendscan(3, [0, 1]) dataset5 = dataman.getLastScans()[-1] assert dataset5.startpositions == [[5, 21], [5, 22], [5, 23]]
def test_scan_invalidpreset(session, log): m = session.getDevice('motor') # invalid preset session.experiment.setDetectors([session.getDevice('det')]) with log.assert_warns('these preset keys were not recognized by any of ' 'the detectors: .*'): scan(m, 0, 1, 1, preset=5, t=0.)
def test_scan2(session): m = session.getDevice('motor') det = session.getDevice('det') mm = session.getDevice('manual') mm.move(0) session.experiment.setDetectors([session.getDevice('det')]) dataman = session.experiment.data try: session.experiment.setEnvironment([]) # scan with different detectors scan(m, [0, 1], det, m=1, t=0.) dataset = dataman.getLastScans()[-1] assert dataset.devvaluelists == [[0.], [1.]] # 2 points, 5 detector channels assert len(dataset.detvaluelists) == 2 assert len(dataset.detvaluelists[0]) == 5 assert [v.name for v in dataset.detvalueinfo] == \ ['timer', 'mon1', 'ctr1', 'ctr2', 'img.sum'] # scan with multistep scan(m, [0, 1], det, manual=[3, 4], t=0.) dataset = dataman.getLastScans()[-1] assert dataset.devvaluelists == [[0., 3.], [0., 4.], [1., 3.], [1., 4.]] finally: session.experiment.envlist = [] session.experiment.detlist = []
def prepare(session, dataroot): """Prepare a dataset for StressSpec""" session.experiment.setDetectors(['adet']) # Create devices needed in data sinks for dev in ['xt', 'yt', 'zt', 'slits', 'slitm', 'slite', 'slitp', 'omgm']: session.getDevice(dev) # Adjust the monochromator to reasonable position and check it tthm = session.getDevice('tthm') tthm.maw(69) transm = session.getDevice('transm') wav = session.getDevice('wav') assert wav.plane == '' # pylint: disable=compare-to-empty-string transm.maw('Ge') wav.plane = '311' wav.maw(1.7) # Perform different scans phis = session.getDevice('phis') timescan(1, t=0.05) scan(phis, 0, 0.1, 1, t=0.05, info='phi scan on time') scan(phis, 0, 0.1, 1, mon1=50, info='phi scan on monitor') contscan(phis, 0, 1, 1000, 0.001) yield
def test_simmode(session): m = session.getDevice('motor') det = session.getDevice('det') scan(m, 0, 1, 5, 0., det, 'test scan') assert m._sim_min == 0 assert m._sim_max == 4 assert m._sim_value == 4
def setup_module(session): """Setup dataroot and generate a dataset by scanning""" exp = session.experiment dataroot = path.join(config.nicos_root, 'testdata') os.makedirs(dataroot) counter = path.join(dataroot, exp.counterfile) open(counter, 'w').close() updateFileCounter(counter, 'scan', 42) updateFileCounter(counter, 'point', 167) exp._setROParam('dataroot', dataroot) exp.new(1234, user='******', localcontact=exp.localcontact) exp.sample.new({'name': 'mysample'}) assert path.abspath(exp.datapath) == \ path.abspath(path.join(config.nicos_root, 'testdata', year, 'p1234', 'data')) m = session.getDevice('motor2') det = session.getDevice('det') tdev = session.getDevice('tdev') session.experiment.setEnvironment([]) scan(m, 0, 1, 5, det, tdev, t=0.005) yield
def test_live_count(session): det = session.getDevice('det') m = session.getDevice('motor') def _go_live(det): live(det) waitForState(det, BUSY) _go_live(det) det.stop() waitForState(det, OK) session._thd_acquire.join() _go_live(det) waitForState(det, BUSY) count(det, t=0) _go_live(det) waitForState(det, BUSY) scan(m, [0, 4, 5], 0., det) count(t=0) _go_live(det) count() # t=0 count() # t=0 scan(m, [0, 4, 5], 0., det, live=1) # live will be ignored
def NutatorsZeros_In(start=45, stop=135, step=5, t=2): adet = session.getDevice('adet') lam = session.getDevice('wavelength')() Cryopad = session.getDevice('Cryopad') maw('pc2', 0, 'pc1', 180.0 / (Cryopad.coefficients[0] * lam), 'Fout', 'off') nsteps = int((stop - start) / step) scan(['nutator1', 'nutator2'], [start, start - 90], [step, step], nsteps, adet, t=t) fit(ZerosFit, 'Asym')
def test_scan_gen_elog(session): m = session.getDevice('motor') mm = session.getDevice('manual') mm.move(0) session.experiment.setDetectors([session.getDevice('det')]) try: # plain scan, with some extras: infostring, firstmove scan(m, 0, 1, 5, 0., 'test scan', manual=1) dataset = ScanData(session.experiment.data.getLastScans()[-1]) genplot.plotDataset(dataset, path.join(runtime_root, 'testplt'), 'svg') finally: session.experiment.detlist = []
def PeakSearch(reflist=None, threshold=100, **preset): sample, inst = getSampleInst() if not hasattr(inst, 'searchpars'): session.log.error('%s does not support peak search', inst.name) return rfl = sample.getRefList(reflist) if rfl is None: session.log.error('Reflection list %s not found', reflist) return mots = inst.get_motors() dstt = session.getDevice(mots[0]) dom = session.getDevice(mots[1]) dchi = session.getDevice(mots[2]) dphi = session.getDevice(mots[3]) mots = [dstt, dom, dchi, dphi] sttpars = inst.searchpars['stt'] chipars = inst.searchpars['chi'] phipars = inst.searchpars['phi'] nphi = (phipars[1] - phipars[0]) / phipars[2] for stt in np.arange(sttpars[0], sttpars[1], sttpars[2]): for chi in np.arange(chipars[0], chipars[1], chipars[2]): dstt.start(stt) dstt.start(stt / 2.) dchi.start(chi) multiWait([dstt, dom, dchi]) session.log.info('Searching at stt = %f, chi = %f', stt, chi) scan(dphi, phipars[0], phipars[2], int(nphi), **preset) peaks = findpeaks(mots[3].name, inst.center_counter, npoints=4) if peaks: session.log.info('%d candidate peaks found in phi scan', len(peaks)) for p in peaks: dphi.maw(p) ang = [m.read(0) for m in mots] r = ((0, 0, 0), tuple(ang), ()) if not test_threshold(dphi.name, inst.center_counter, p, threshold): session.log.info('Peak at %f below threshold %f', p, threshold) continue if test_proximity(rfl, ang): session.log.info('Peak at %f failed proximity test', p) continue session.log.info('Centering reflection at phi = %f', p) ok, maxang = inner_center(r, **preset) if ok: rfl.append(None, tuple(maxang), None) else: session.log.warning('Failed centering at phi = %f', p)
def test_Scan(self, session): template = { 'entry:NXentry': { 'time': DetectorDataset('timer', 'float32'), 'mon': DetectorDataset('mon1', 'uint32'), 'counts': ImageDataset(0, 0, signal=NXAttribute(1, 'int32')), 'sry': DeviceDataset('sry'), }, 'data:NXdata': { 'None': NXScanLink(), } } setTemplate(template) self.setScanCounter(session, 48) session.experiment.setDetectors([ 'det', ]) sry = session.getDevice('sry') scan(sry, 0, 1, 5, t=0.001) fin = h5py.File( path.join(session.experiment.datapath, 'test%sn000049.hdf' % year), 'r') ds = fin['entry/sry'] assert (len(ds) == 5) ds = fin['entry/time'] assert (len(ds) == 5) ds = fin['entry/mon'] assert (len(ds) == 5) ds = fin['entry/counts'] assert (len(ds) == 5) ds = fin['data/sry'] assert (len(ds) == 5) assert (ds[0] == 0) assert (ds[1] == 1) assert (ds[2] == 2) assert (ds[3] == 3) assert (ds.attrs['target'] == b'/entry/sry') fin.close()
def nGI_stepping(n_images, p=1, angle=0, *detlist, **preset): """Performs a nGI stepping scan of G0 over p periods in n_images-1 steps. Calculates the stepping period from the angle of the grating lines to the vertical axis *angle*. Example: >>> nGI_stepping(11,1,0,t=30) # steps G0 over one period from 0 to 1.6 mm >>> # in 0.16 mm steps and count for 30 s """ import numpy as np stepwidth = 1.6 / np.cos(angle * 2 * np.pi / 360) * p / (n_images - 1) session.log.info('Starting nGI scan.') scan('G0tx', 0, stepwidth, n_images, *detlist, **preset) maw('fastshutter', 'closed') session.log.info('fastshutter closed')
def test_scan_plotindex(session): m = session.getDevice('motor') m2 = session.getDevice('motor2') mm = session.getDevice('manual') mm.move(0) dataman = session.experiment.data session.experiment.setDetectors([session.getDevice('det')]) # first device is moving scan([m, m2], [0, 0], [2, 0], 2, t=0) assert dataman.getLastScans()[-1].xindex == 0 # second device is moving scan([m, m2], [0, 0], [0, 2], 2, t=0) assert dataman.getLastScans()[-1].xindex == 1 # second device is moving, with multistep (issue #4030, #4031) scan([m, m2], [0, 0], [0, 2], 2, t=0., manual=[1, 2]) assert dataman.getLastScans()[-1].xindex == 1 # degenerate case only multistep moving, with multistep (issue #4030, #4031) scan([m, m2], [0, 0], [0, 0], 2, t=0., manual=[1, 2]) assert dataman.getLastScans()[-1].xindex == 0
def test_scan_errorhandling(session, log): t = session.getDevice('tdev') m = session.getDevice('motor') dataman = session.experiment.data # no errors, works fine scan(t, [0, 1, 2]) dataset = dataman.getLastScans()[-1] assert dataset.devvaluelists == [[0], [1], [2]] with log.allow_errors(): # limit error: skips the data points scan(t, [-10, 0, 10, 3]) dataset = dataman.getLastScans()[-1] assert dataset.devvaluelists == [[0], [3]] # communication error during move: also skips the data points t._value = 0 t._start_exception = CommunicationError() scan(t, [0, 1, 2, 3]) dataset = dataman.getLastScans()[-1] assert dataset.devvaluelists == [[ 0 ]] # tdev.move only raises when target != 0 # position error during move: ignored t._value = 0 t._read_exception = None t._start_exception = PositionError() scan(t, [0, 1, 2, 3]) dataset = dataman.getLastScans()[-1] assert dataset.devvaluelists == [[0], [None], [None], [None]] # position error during readout: ignored t._read_exception = PositionError() t._start_exception = None scan(t, [0, 1, 2, 3]) dataset = dataman.getLastScans()[-1] assert dataset.devvaluelists == [[None], [None], [None], [None]] # also as an environment device scan(m, [0, 1], t) dataset = dataman.getLastScans()[-1] assert dataset.devvaluelists == [[0.], [1.]] assert dataset.envvaluelists == [[None], [None]] # other errors: reraised t._start_exception = RuntimeError() assert raises(RuntimeError, scan, t, [0, 1, 2, 3])
def NutatorPerpendicularity(start=45, stop=135, step=5, t=2): adet = session.getDevice('adet') maw('nutator2', 0, 'nutator1', start, 'Fout', 'off', 'pc1', 0, 'pc2', 0) nsteps = int((stop - start) / step) scan('nutator1', start, step, nsteps, adet, t=t) fit(PerpFit, 'Asym')
def PC2Current(start=-4, stop=4, step=0.25, t=2): adet = session.getDevice('adet') maw('nutator2', 0, 'nutator1', 0, 'Fout', 'off', 'pc1', 0) nsteps = int((stop - start) / step) scan('pc2', start, step, nsteps, adet, t=t) fit(PCurrentFit, 'Asym')
def test_scan(session, log): m = session.getDevice('motor') m2 = session.getDevice('motor2') c = session.getDevice('coder') mm = session.getDevice('manual') mm.move(0) # check that scans are impossible in slave mode session.setMode(SLAVE) assert raises(ModeError, scan, m, [0, 1, 2, 10]) session.setMode(MASTER) session.experiment.setDetectors([session.getDevice('det')]) session.experiment.setEnvironment([avg(mm), minmax(mm)]) dataman = session.experiment.data try: # plain scan, with some extras: infostring, firstmove scan(m, 0, 1, 5, 0.005, 'test scan', manual=1) dataset = dataman.getLastScans()[-1] assert [v.name for v in dataset.devvalueinfo] == ['motor'] assert [v.unit for v in dataset.devvalueinfo] == ['mm'] assert dataset.devvaluelists == [[float(i)] for i in range(5)] assert [v.name for v in dataset.detvalueinfo] == \ ['timer', 'mon1', 'ctr1', 'ctr2', 'img.sum'] assert [v.unit for v in dataset.detvalueinfo] == \ ['s', 'cts', 'cts', 'cts', 'cts'] assert dataset.info.startswith('test scan') assert len(dataset.detvaluelists) == 5 assert len(dataset.envvaluelists[0]) == 3 assert dataset.envvaluelists[0] == [1., 1., 1.] # avg, min, max assert mm.read() == 1 session.experiment.setEnvironment([]) # scan with second basic syntax scan(m, [0, 4, 5], 0.) dataset = dataman.getLastScans()[-1] assert dataset.devvaluelists == [[float(i)] for i in [0, 4, 5]] # scan with multiple devices scan([m, m2], [0, 0], [1, 2], 3, t=0.) dataset = dataman.getLastScans()[-1] assert dataset.devvaluelists == [[float(i), float(i * 2)] for i in [0, 1, 2]] # same with tuple arguments scan((m, m2), (0, 0), (1, 2), 2, t=0.) # scan with multiple devices and second basic syntax scan([m, m2], [[0, 0, 1], [4, 2, 1]], t=0.) dataset = dataman.getLastScans()[-1] assert dataset.devvaluelists == [[0., 4.], [0., 2.], [1., 1.]] # scan with different environment scan(m, [0, 1], c) dataset = dataman.getLastScans()[-1] assert dataset.devvaluelists == [[0.], [1.]] assert dataset.envvaluelists == [[0.], [1.]] assert dataset.devvalueinfo[0].name == 'motor' assert dataset.devvalueinfo[0].unit == 'mm' assert dataset.envvalueinfo[0].name == 'coder' assert dataset.envvalueinfo[0].unit == 'mm' finally: session.experiment.envlist = [] session.experiment.detlist = []
def test_experiment(session, cleanup): exp = session.experiment # setup test scenario exp._setROParam('dataroot', path.join(runtime_root, 'data')) exp.proposal = 'service' exp._setROParam('proptype', 'service') # if there is no exp.new, we need to adjust proposalpath ourselfs! exp.proposalpath = exp.proposalpath_of(exp.proposal) # create the needed script file spath = path.join(runtime_root, 'data', year, 'service', 'scripts') assert exp.scriptpath == spath ensureDirectory(spath) with open(path.join(spath, 'servicestart.py'), 'w') as fp: fp.write('Remark("service time")\n') # first, go in service mode exp.servicescript = 'servicestart.py' try: exp.new('service', localcontact=exp.localcontact) finally: exp.servicescript = '' assert exp.proposal == 'service' assert exp.proptype == 'service' assert exp.remark == 'service time' assert exp.scriptpath == spath # check correct operation of sampledir exp.sampledir = 'sample' assert exp.datapath == path.join(exp.dataroot, year, 'service', 'sample', 'data') exp.sampledir = '' # for this proposal, remove access rights after switching back exp._setROParam('managerights', dict(disableFileMode=0, disableDirMode=0)) # then, go in proposal mode exp.new(999, 'etitle', 'me <*****@*****.**>', 'you') # check that all properties have been set accordingly assert exp.proposal == 'p999' assert exp.proptype == 'user' assert exp.title == 'etitle' assert exp.localcontact == 'me <*****@*****.**>' assert exp.users == 'you' assert exp.remark == '' #pylint: disable=compare-to-empty-string # check that directories have been created assert path.isdir(datapath('p999')) assert path.isdir(datapath('p999', 'scripts')) assert path.isdir(datapath('p999', 'data')) # check that templating works assert path.isfile(datapath('p999', 'scripts', 'start_p999.py')) run('start_p999.py') assert exp.remark == 'proposal p999 started by you; sample is unknown' # try a small scan; check for data file written scan(session.getDevice('axis'), 0, 1, 5, 0.01, u'Meßzeit') assert path.isfile(datapath('..', 'counters')) nr = readFileCounter(datapath('..', 'counters'), 'scan') fn = datapath('p999', 'data', 'p999_%08d.dat' % nr) assert path.isfile(fn) with open(fn, 'rb') as fp: assert u'Meßzeit'.encode('utf-8') in fp.read() # now, finish the experiment thd = exp.finish() if thd: thd.join() # have the access rights been revoked? if os.name != 'nt': assert not os.access(datapath('p999'), os.X_OK) # did we switch back to service proposal? assert exp.proposal == 'service' # switch back to proposal (should re-enable directory) exp.new('p999', localcontact=exp.localcontact) assert os.access(datapath('p999'), os.X_OK) assert exp.users == '' #pylint: disable=compare-to-empty-string # has the zip file been created? assert path.isfile(datapath('p999', 'p999.zip')) exp.addUser('A User') assert exp.users == 'A User' exp.addUser('Another User', '*****@*****.**') assert exp.users == 'A User, Another User <*****@*****.**>' exp.addUser('Athird User', '*****@*****.**', 'An Institute, Anywhere street, 12345 Anywhere') assert exp.users == 'A User, Another User <*****@*****.**>, '\ 'Athird User <*****@*****.**> ' \ '(An Institute, Anywhere street, 12345 Anywhere)' exp.users = 'Jülich' exp.users = u'Jülich' exp.scripts = ['Test ümlauts'] exp.scripts = [u'Test ümlauts'] assert exp.scripts == ['Test ümlauts'] # and back to service exp.new('service', localcontact=exp.localcontact)