def test_toftof_chopper(session): chRatio = session.getDevice('chRatio') chCRC = session.getDevice('chCRC') chST = session.getDevice('chST') chDS = session.getDevice('chDS') ch = session.getDevice('ch') assert chRatio.read(0) == 1 assert chCRC.read(0) == 1 assert chST.read(0) == 1 # speed = 0 speed = 6000 assert chDS.read(0) == [speed, speed, speed, speed, speed, speed, speed] for disc in ['d1', 'd2', 'd3', 'd4', 'd6', 'd7']: assert session.getDevice(disc).read(0) == speed assert session.getDevice('d5').read(0) == -speed chSpeed = session.getDevice('chSpeed') chSpeed.maw(6000) assert chSpeed.read(0) == speed assert chDS.status(0)[0] == status.OK chWL = session.getDevice('chWL') assert chWL.read(0) == 4.5 chWL.maw(5) assert chWL.read(0) == 5 chRatio.maw(2) assert chRatio.read(0) == 2 chST.maw(2) assert chST.read(0) == 2 chCRC.maw(0) assert chCRC.read(0) == 0 speeds = [6000, 3000, 4000, 4500, 4800, 5000, 5143, 5250, 4667, 4200] for r, s in zip(range(1, 11), speeds): # pylint: disable=range-builtin-not-iterating chRatio.maw(r) assert chRatio.read(0) == r assert chDS.read(0)[4] == approx(s, 1) assert ch.read(0) == approx(6000, 0.1) chRatio.maw(1) assert chRatio.read(0) == 1 chCRC.maw(1) assert chCRC.read(0) == 1 chST.maw(1) assert chST.read(0) == 1 chWL.maw(4.5) assert chWL.read(0) == 4.5
def test_resolution_analysis(self): chSpeed = 14000 chWL = 6. chRatio = 4 chST = 1 ra = ResolutionAnalysis(chSpeed, chWL, chRatio, chST) assert approx(ra.E0, abs=5e-5) == 2.2723 assert approx(ra.q_low_0, abs=5e-5) == 0.1386 assert approx(ra.q_high_0, abs=5e-5) == 1.9629 assert approx(ra.dE_min, abs=5e-5) == -1.0723 assert approx(ra.dE_el, abs=5e-5) == 32.9835
def test_motor_stop_target(self, motortype): motor = getattr(self, motortype) motor.move(0.5) # Test motor stop behavior motor.stop() assert motor.target == approx(motor.read()) # Let the targets update time.sleep(2) # Continue the movement of motor and check if it reaches target motor.maw(0.5) assert motor.read() == approx(0.5, abs=1e-3)
def test_to_k(): assert to_k(1, 'A-1') == 1 in_out = in_tok[:] in_out += [(r, u, v) for (v, u, r) in in_fromk] for args in in_out: assert to_k(args[0], args[1]) == approx(args[2], abs=1e-4)
def test_frametime_calculations(self): for speed, ratio, expected in [ (14000, 5, 0.0107143), (0, 1, 0.052), ]: assert calculateFrameTime(speed, ratio) == approx(expected, abs=1e-7)
def test_timeinterval_calculations(self): for speed, ratio, expected in [ (14000, 5, 0.0107143), (0, 1, 0.052), ]: assert calculateTimeInterval(speed, ratio) == approx(expected, abs=1e-7)
def test_movement(self, session): """Test moves to different positions.""" med = session.getDevice('med') med.maw([-2.5 * i for i in range(11)] + [0] * 11) # Code should return immediately med.move([-2.5 * i for i in range(11)] + [0] * 11) assert med.read(0) == [-2.5 * i for i in range(11)] + [0] * 11 for r, e in zip(med._read_corr()[0], [-8.92, -9.39, -9.98, -10.70, -11.54, -12.5, -13.58, -14.81, -16.15, -17.61, -19.19]): assert r == approx(e, abs=0.01) dirname = os.path.dirname(__file__) # read good targets from file with open(os.path.join(dirname, 'med_test.txt')) as f: for s in f.readlines(): # convert stringified list to list avoiding use of 'eval' v = ast.literal_eval(s) assert med.isAllowed(v)[0] # read bad targets from file with open(os.path.join(dirname, 'med_test_fail.txt')) as f: for s in f.readlines(): # convert stringified list to list avoiding use of 'eval' v = ast.literal_eval(s) assert not med.isAllowed(v)[0]
def test_magnet(session): current = session.getDevice('current') magnet = session.getDevice('magnet') # the default formula is B(I) = c0*I + c1*erf(c2*I) + c3*atan(c4*I) magnet.calibration = 1, 0, 0, 0, 0 assert magnet.abslimits == (-100, 100) magnet.maw(1) assert magnet.read() == 1 assert current.read() == 1 magnet.calibration = 1, 1, 1, 1, 1 assert magnet.abslimits[0] == approx(-102.56, abs=1e-2) magnet.maw(1) assert magnet.read() == 1 assert current.read() == approx(0.33, abs=1e-2)
def test_energy_fromk(): # include backward calculations into test in_out = in_fromk[:] in_out += [(r, u, v) for (v, u, r) in in_tok] for args in in_out: e = energy.Energy(value=args[0], unit='A-1') assert e.asUnit(args[1]) == approx(args[2], abs=1e-4)
def test_out_of_bounds_errors_out(self, motortype): motor = getattr(self, motortype) llm, hlm = motor.abslimits # Test the limits assert raises(LimitError, motor, llm - 0.1) assert motor.target == approx(motor.read()) assert raises(LimitError, motor, hlm + 0.1) assert motor.target == approx(motor.read()) # Test that even below limits, if slave motors can't move # error is produced assert raises(ErrorLogged, motor, llm + 0.1) assert motor.target == approx(motor.read()) assert raises(ErrorLogged, motor, hlm - 0.1) assert motor.target == approx(motor.read())
def test_phi1_calculations(self): for x, speed, wl, expected in [(1, 14000, 6, 0.), (2, 14000, 6, 12.74), (3, 14000, 6, 432.78), (4, 14000, 6, 1013.21), (5, 14000, 6, 1022.77), (6, 14000, 6, 1264.45), (7, 14000, 6, 1274.00), (5, 7000, 6, 511.38)]: assert phi1(x, speed, wl) == approx(expected, abs=1e-2)
def test_from_k(): assert from_k(1, 'A-1') == 1 # include backward calculations into test in_out = in_fromk[:] in_out += [(r, u, v) for (v, u, r) in in_tok] for args in in_out: assert from_k(args[0], args[1]) == approx(args[2], abs=1e-4)
def test_t2(self): for x, expected in [ (2, 0.017138), (3, 0.012138), (4, 0.005228), (5, 0.005114), (6, 0.002237), (7, 0.002123), ]: assert t2(x, ilambda=6) == approx(expected, abs=1e-6)
def test_virtualgonios(session, tas): gx, gy = session.getDevice('sgx'), session.getDevice('sgy') v1, v2 = session.getDevice('vg1'), session.getDevice('vg2') # when psi0 = 0, gx == v1 and gy == v2 tas._attached_cell.psi0 = 0 gx.maw(0) gy.maw(0) assert v1.read(0) == 0 assert v2.read(0) == 0 gx.maw(2) assert v1.read(0) == approx(2) assert v2.read(0) == approx(0) gy.maw(1) assert v1.read(0) == approx(2) assert v2.read(0) == approx(1) v1.maw(0) v2.maw(1.3) assert gx.read(0) == approx(0) assert gy.read(0) == approx(1.3) # psi0 = 45deg tas._attached_cell.psi0 = 45 gx.maw(0) gy.maw(0) assert v1.read(0) == 0 assert v2.read(0) == 0 v1.maw(4) assert gx.read(0) == approx(2 * sqrt(2), abs=5e-2) assert gy.read(0) == approx(2 * sqrt(2), abs=5e-2) # limits of sgx, sgy are +/- 5 deg v1.maw(7) assert raises(LimitError, v2.maw, 3) # make sure the calculations match intent D2R = pi / 180 tas._attached_cell.psi0 = 64 v1.maw(1.5) v2.maw(-2) # extract angles in radians psi = 64 * D2R x1 = 1.5 * D2R y1 = -2 * D2R x2 = gx.read(0) * D2R y2 = gy.read(0) * D2R # these two matrices should deliver the same rotation m1 = dot(Xrot(x1), Yrot(y1)) m2 = dot(dot(Zrot(psi), Xrot(x2)), dot(Yrot(y2), Zrot(-psi))) assert allclose(m1, m2, atol=1e-3)
def test_params(session): axis = session.getDevice('axis') # drag error should be the default: 1 assert axis.dragerror == 1 # min/max parameters got from motor device assert axis.abslimits == (-100, +100) # usermin/usermax got from motor device assert axis.userlimits == (-50, +50) # unit automatically from motor device assert axis.unit == 'mm' # abslimits from config axis2 = session.getDevice('limit_axis') assert axis2.abslimits == (-1, +1) # offset axis2.maw(1) assert axis2.read() == approx(1) axis2.offset = 1 assert axis2.read() == approx(0)
def tscan(Qh, Qk, Ql, ny, dQh, dQk, dQl, dny, numsteps, SM, SC, sense, sample): Qhkl = array([Qh, Qk, Ql], float) dQhkl = array([dQh, dQk, dQl], float) print(' ' + ('%-9s' * 14) % ('h', 'k', 'l', 'ny', 'ki', 'kf', 'phi', 'psi', 'alpha', 'hcalc', 'kcalc', 'lcalc', 'nycalc', 'dval')) for _ in range(numsteps): Qhkl += dQhkl ny += dny angles = sample.cal_angles(Qhkl, ny, SM, SC, sense, False) hklr = sample.angle2hkl(angles[:4], False) nyr = sample.cal_ny(angles[0], angles[1]) dval = sample.cal_dvalue_real(Qhkl) print( ('%7.3f ' * 14) % (tuple(Qhkl) + (ny, ) + tuple(angles) + tuple(hklr) + (nyr, dval))) for i in range(3): assert Qhkl[i] == approx(hklr[i]) assert ny == approx(nyr)
def test_mono_device(session): mono = session.getDevice('t_mono') mth = session.getDevice('t_mth') # unit switching mono.unit = 'A-1' mono.maw(1.4) assert mono.read(0) == approx(1.4, abs=1e-3) assert mono.target == 1.4 mono.unit = 'meV' assert mono.read(0) == approx(4.061, abs=1e-3) assert mono.target == approx(4.061, abs=1e-3) assert mono.status()[0] == status.OK for unit in ['THz', 'A', 'A-1']: mono.unit = unit assert mono.status()[0] == status.OK # mth/mtt mismatch mth.maw(mth() + 5) assert mono.status(0)[0] == status.NOTREACHED old = mono.read(0) mono.reset() assert raises(LimitError, mono.doStart, 0.11) assert raises(LimitError, mono.maw, 0.11) mono.maw(1.5) for mode in ['flat', 'vertical', 'double', 'manual']: mono.focmode = mode mono.maw(1.5) mono.maw(old) mono.hfocuspars = mono.hfocuspars mono.vfocuspars = mono.vfocuspars mtt = session.getDevice('t_mtt') mtt.move(mtt.read(0) + 2) mono.finish() assert mono._calcurvature(1., 1., 1) == approx(1.058, abs=1e-3)
def test_counterdelay_calculations(self): for wl, speed, ratio, delay, ch5_90deg_offset, expected in [ (6, 14000, 5, 0, False, 129070 * 5e-8), (6, 14000, 5, 1, False, 200499 * 5e-8), (6, 14000, 5, 0, True, 155856 * 5e-8), (6, 14000, 6, 0, False, 127999 * 5e-8), (6, 14000, 6, 0, True, 153713 * 5e-8), ]: assert calculateCounterDelay(wl, speed, ratio, delay, ch5_90deg_offset) == approx(expected, abs=5e-8)
def test_analyzer_off_behaviour(self, session): # Turn off the analyzer component self.distances.__dict__['analyser'] = 'NOT ACTIVE' # The slave positions at ath = 0.0 should be same as when ath = 0.1 self.m2t.maw(0.1) self.s2t.maw(0.1) self.ath.maw(0.1) for slavename, target in test_targets[(0.100, 0.100, 0.000)]: slave = session.getDevice(slavename) assert slave.read() == approx(target, abs=1e-2)
def test_rangelistlog(): l3 = RangeListLog(1., 2., 3) print(l3) assert len(l3) == 3 assert l3[0] == 1. assert l3[-1] == 2. assert numpy.allclose(l3, [1.0, 1.4142135623730949, 2.0]) l3a = RangeListLog(0.1, 200., 3) assert len(l3a) == 3 assert l3a[0] == approx(0.1, abs=1e-5) assert l3a[-1] == approx(200., abs=1e-5) assert numpy.allclose(l3a, [0.10000000000000001, 4.4721359549995787, 200.0]) l3b = RangeListLog(200, 2., 5) assert len(l3b) == 5 assert l3b[0] == approx(200., abs=1e-5) assert l3b[-1] == approx(2., abs=1e-5) assert raises(UsageError, RangeListLog, -1, 2, 10)
def test_or_calculation(self, eulerian): """Test calc_or function.""" assert eulerian.calc_or().tolist() == [[ approx(-0.862, abs=0.001), approx(0.0795, abs=0.001), approx(-0.5, abs=0.001) ], [ approx(0.362, abs=0.001), approx(0.787, abs=0.001), approx(-0.5, abs=0.001) ], [ approx(0.354, abs=0.001), approx(-0.612, abs=0.001), approx(-0.707, abs=0.001) ]]
def test_motor_operations(self, offset, target): """ Test that after a device is moved to a certain position the new position read is the same as the one used with move """ precision = self.device.precision assert self.device.read(0) == approx(0, precision) # Set the offset offset = unit_value(offset, self.device.unit) self.device.offset = offset assert self.device.offset == approx(offset, precision) # Check the change in limits (usermin, usermax) = self.device.userlimits (absmin, absmax) = self.device.abslimits assert usermin == approx(absmin + offset, precision) assert usermax == approx(absmax + offset, precision) # Move to the new target target = unit_value(target, self.device.unit) self.device.maw(target) assert is_at_target(self.device, target)
def test_sharpness(): img = scipy.misc.ascent().astype(np.uint16) sharpvals = [] sigmas = [-30, -11, -5, -3, 0, 3, 5, 11, 30] exp_sharpness = [15.34, 34.98, 63.92, 99.24, 271.84, 99.24, 63.92, 34.98, 15.34] # calculate sharpness for a series of blurred pictures for sigma, expected in zip(sigmas, exp_sharpness): blurred_img = ndimage.gaussian_filter(img, sigma=abs(sigma)) sharp = scharr_filter(gam_rem_adp_log(blurred_img, 25, 100, 400, 0.8)) assert sharp == approx(expected, abs=0.01) sharpvals.append(sharp)
def test_do_start(self): nu = self.session.getDevice('nu') nu._attached_com = Mock() nu._attached_coz = Mock() target = 45 expected_coz = 1 * nu.coz_scale_factor nu.start(target) coz_target = nu._attached_coz.start.call_args[0] com_target = nu._attached_com.start.call_args[0] assert com_target[0] == -target assert coz_target[0] == approx(expected_coz, abs=0.01)
def test_movement(session): axis = session.getDevice('axis') # moving once axis.maw(1) assert axis.read() == approx(1) assert axis.status()[0] == status.OK # moving again axis.maw(2) assert axis.read() == approx(2) assert axis.status()[0] == status.OK # moving out of limits? assert raises(LimitError, axis.move, 150) # simulate a busy motor axis._attached_motor.curstatus = (status.BUSY, 'busy') # moving while busy? # assert raises(NicosError, axis.move, 10) # forwarding of motor status by doStatus() assert axis.status(0)[0] == status.BUSY axis._attached_motor.curstatus = (status.OK, '') # now move for a while axis.maw(0) motor = session.getDevice('motor') motor.speed = 5 try: axis.move(0.5) axis.wait() assert axis.read() == approx(0.5) axis.move(0) sleep(0.1) axis.stop() axis.wait() assert 0 <= axis.read() <= 1 finally: motor.stop()
def test_init(session): """Test init of device if external devices at right positions.""" # remove the existing device session.destroyDevice('wav') # set the external devices correctly transm = session.getDevice('transm') transm.maw('Ge') omgm = session.getDevice('tthm') omgm.maw(68) # recreate device wav = session.createDevice('wav', recreate=True) # check plane assert wav.crystal == 'Ge' assert wav.plane == '311' assert wav.read(0) == approx(1.908, abs=0.001)
def test_motor_has_correct_targets(self, targets, session): # Move the motors to targets ml = self.controller._get_move_list({ 'm2t': targets[0], 's2t': targets[1], 'ath': targets[2] }) for dev, targ in ml: dev = dev.name found = False for d, t in test_targets[targets]: if d == dev: assert t == approx(targ, abs=1e-2), '%s target mismatch' % d found = True assert found, 'unexpectedly moved %s' % dev
def test_virtualdet(session, tas): tdet = session.getDevice('vtasdet') tas.scanmode = 'CKI' tas.scanconstant = 1.57 tas.maw([1, 0, 0, 0]) countres = count(tdet, 1) assert countres[0] == 1.0 cps = float(countres[2]) countres2 = count(tdet, 100) assert countres2[2] / cps == approx(100, abs=30) tas.maw([0.5, 0, 0, 0]) countres = count(tdet, 1) assert countres[2] < 10 countres = count(tdet, mon=10000) assert countres[1] == 10000
def test_error_handling(session, log, tas): # check that if one subdev errors out, we wait for the other subdevs mfh = session.getDevice('t_mfh') phi = session.getDevice('t_phi') tas.maw([1.01, 0, 0, 1]) phi.speed = 1 mfh._status_exception = PositionError(mfh, 'wrong position') with log.allow_errors(): try: tas.maw([1, 0, 0, 1]) except MoveError: pass else: assert False, 'PositionError not raised' # but we still arrived with phi assert phi() == approx(-46.6, abs=0.1)
def test_basic(session): """Test basic functions.""" # get wavelength device wav = session.getDevice('wav') # Attached devices are not at right positions device assert wav.plane == '' # pylint: disable=compare-to-empty-string assert wav.read(0) is None assert wav.status(0)[0] == status.WARN assert wav.crystal is None assert raises(ConfigurationError, setattr, wav, 'plane', '311') assert raises(ConfigurationError, wav.maw, 1) # external monochromator changer will be set to a valid pos transm = session.getDevice('transm') transm.maw('Ge') assert transm.read(0) == 'Ge' assert wav.crystal == 'Ge' assert wav.status(0)[0] == status.WARN assert raises(ConfigurationError, wav.maw, 1) # external rotation of the monochromator will be set to valid pos omgm = session.getDevice('tthm') omgm.maw(68) assert wav.status(0)[0] == status.ERROR assert wav._crystal() is not None assert raises(ConfigurationError, wav.maw, 1) # plane setting assert wav.plane == '' # pylint: disable=compare-to-empty-string assert raises(ValueError, setattr, wav, 'plane', '111') wav.plane = '311' assert wav.read(0) == approx(1.908, abs=0.001) wav.maw(1.7) assert wav.read(0) == 1.7