def test_take_measurement(): with expected_protocol( ik.srs.SRS830, [ "REST", "SRAT 4", "SEND 0", "FAST 2", "STRD", "PAUS", "SPTS?", "SPTS?", "TRCA?1,0,2", "SPTS?", "TRCA?2,0,2" ], [ "2", "2", "1.234,5.678", "2", "0.456,5.321" ] ) as inst: resp = inst.take_measurement(sample_rate=1, num_samples=2) expected = ((1.234, 5.678), (0.456, 5.321)) if numpy: expected = numpy.array(expected) iterable_eq(resp, expected)
def test_all_voltage_sense(): with expected_protocol(ik.hp.HP6624a, ["VOUT? 1", "VOUT? 2", "VOUT? 3", "VOUT? 4"], ["2", "3", "4", "5"], sep="\n") as hp: expected = (2 * u.V, 3 * u.V, 4 * u.V, 5 * u.V) iterable_eq(hp.voltage_sense, expected)
def test_all_current_sense(): with expected_protocol(ik.hp.HP6624a, ["IOUT? 1", "IOUT? 2", "IOUT? 3", "IOUT? 4"], ["2", "3", "4", "5"], sep="\n") as hp: expected = (2 * u.A, 3 * u.A, 4 * u.A, 5 * u.A) iterable_eq(hp.current_sense, expected)
def test_iterable_eq_passes_basic(a): """ Test that two identical lists and tuples always pass """ b = a[:] iterable_eq(a, b) iterable_eq(tuple(a), tuple(b))
def test_iterable_eq_fails(b): """ Test failure on equal and mismatched lengths """ a = (1, 2, 3) with pytest.raises(AssertionError): iterable_eq(a, b)
def test_iterable_eq_fails_type_mismatch(): """ Test failure on type mismatch """ a = [1, 2, 3] with pytest.raises(AssertionError): iterable_eq(a, tuple(a))
def test_maui_data_source_read_waveform(init): """Return a numpy array of a waveform.""" with expected_protocol( ik.teledyne.MAUI, [ init, 'TRMD?', 'TRMD SINGLE', "C1:INSPECT? 'SIMPLE'", "C1:INSPECT? 'HORIZ_OFFSET'", "C1:INSPECT? 'HORIZ_INTERVAL'", 'TRMD AUTO' ], [ 'AUTO', '" 1. 2. 3. 4. "', "HORIZ_OFFSET : 0. ", "HORIZ_INTERVAL : 2.5 " ], sep="\n" ) as osc: if numpy: expected_wf = numpy.array( [ [0., 2.5, 5., 7.5], [1., 2., 3., 4.] ] ) else: expected_wf = ( (0., 2.5, 5., 7.5), (1., 2., 3., 4.) ) actual_wf = osc.channel[0].read_waveform() iterable_eq(actual_wf, expected_wf)
def test_tektds224_data_source_read_waveform(): with expected_protocol( ik.tektronix.TekTDS224, [ "DAT:SOU?", "DAT:SOU CH2", "DAT:ENC RIB", "DATA:WIDTH?", "CURVE?", "WFMP:CH2:YOF?", "WFMP:CH2:YMU?", "WFMP:CH2:YZE?", "WFMP:XZE?", "WFMP:XIN?", "WFMP:CH2:NR_P?", "DAT:SOU CH1" ], [ "CH1", "2", # pylint: disable=no-member "#210" + bytes.fromhex("00000001000200030004").decode("utf-8") + "0", "1", "0", "0", "1", "5" ]) as tek: data = tuple(range(5)) if numpy: data = numpy.array([0, 1, 2, 3, 4]) (x, y) = tek.channel[1].read_waveform() iterable_eq(x, data) iterable_eq(y, data)
def test_iterable_eq_fails_one_numpy_array_equal_values(): """ Test failure for one is numpy array, other is not """ a = numpy.array([1, 2, 3]) b = (1, 2, 3) with pytest.raises(AssertionError): iterable_eq(a, b)
def test_iterable_eq_fails_one_numpy_array(b): """ Test that different value and different length comparisons against numpy arrays fail """ a = numpy.array([1, 2, 3]) with pytest.raises(AssertionError): iterable_eq(a, b)
def test_iterable_eq_fails_singular_quantity(): """ Test failure on singular unitful quantity with wrong units """ a = 1 * u.V b = 1 * u.A with pytest.raises(AssertionError): iterable_eq(a, b)
def test_channel_read_waveform(): """Read waveform of channel object.""" with expected_protocol( ik.rigol.RigolDS1000Series, [":WAV:DATA? CHAN2"], [b"#210" + bytes.fromhex("00000001000200030004") + b"0"]) as osc: expected = (0, 1, 2, 3, 4) if numpy: expected = numpy.array(expected) iterable_eq(osc.channel[1].read_waveform(), expected)
def test_maui_data_source_read_waveform_different_length(init): """BF: Stacking return arrays with different length. Depending on rounding issues, time and data arrays can have different lengths. Shorten to the shorter one by cutting the longer one from the end. """ faulty_dataset_str = [] faulty_dataset_int = [] for it in range(402): # 402 datapoints will make the error faulty_dataset_str.append(str(it)) faulty_dataset_int.append(it) return_data_string = '" ' + ' '.join(faulty_dataset_str) + ' "' with expected_protocol( ik.teledyne.MAUI, [ init, 'TRMD?', 'TRMD SINGLE', "C1:INSPECT? 'SIMPLE'", "C1:INSPECT? 'HORIZ_OFFSET'", "C1:INSPECT? 'HORIZ_INTERVAL'", 'TRMD AUTO' ], [ 'AUTO', return_data_string, "HORIZ_OFFSET : 9.8895e-06 ", "HORIZ_INTERVAL : 5e-10 " ], sep="\n" ) as osc: h_offset = 9.8895e-06 h_interval = 5e-10 if numpy: # create the signal that we want to get returned signal = numpy.array(faulty_dataset_int) timebase = numpy.arange( h_offset, h_offset + h_interval * (len(signal)), h_interval ) # now cut timebase to the length of the signal timebase = timebase[0:len(signal)] # create return dataset dataset_return = numpy.stack((timebase, signal)) else: signal = tuple(faulty_dataset_int) timebase = tuple([float(val) * h_interval + h_offset for val in range(len(signal))]) timebase = timebase[0:len(signal)] dataset_return = timebase, signal actual_wf = osc.channel[0].read_waveform() iterable_eq(actual_wf, dataset_return)
def test_iterable_eq_passes_two_numpy_array_quantities(): """ Test that two unitful quantities with numpy array data will equal data will pass """ values = [1, 2, 3] a = numpy.array(values) * u.V b = numpy.array(a) * u.V iterable_eq(a, b)
def test_amplitude(): with expected_protocol(ik.srs.SRS345, ["AMPL?", "AMPL 0.1VP", "AMPL 0.1VR"], [ "1.234VP", ]) as inst: iterable_eq(inst.amplitude, (1.234 * u.V, inst.VoltageMode.peak_to_peak)) inst.amplitude = 0.1 * u.V inst.amplitude = (0.1 * u.V, inst.VoltageMode.rms)
def test_instrument_binblockread(): with expected_protocol(ik.Instrument, [], [ b"#210" + bytes.fromhex("00000001000200030004") + b"0", ], sep="\n") as inst: actual_data = inst.binblockread(2) expected = (0, 1, 2, 3, 4) if numpy: expected = numpy.array(expected) iterable_eq(actual_data, expected)
def test_agilent34410a_fetch(): with expected_protocol( ik.agilent.Agilent34410a, ["CONF?", "FETC?"], ["VOLT +1.000000E+01,+3.000000E-06", "+4.27150000E-03,5.27150000E-03" ]) as dmm: data = dmm.fetch() expected = (4.27150000E-03 * u.volt, 5.27150000E-03 * u.volt) if numpy: expected = (4.27150000E-03, 5.27150000E-03) * u.volt iterable_eq(data, expected)
def test_data_source_read_waveform_binary(values): """Read waveform from data source as binary.""" # constants - to not overkill it with hypothesis channel_no = 0 data_width = 2 yoffs = 1. ymult = 1. yzero = 0.3 xincr = 0.001 # make values to compare with ptcnt = len(values) values_arr = values if numpy: values_arr = numpy.array(values) values_packed = b"".join(struct.pack(">h", value) for value in values) values_len = str(len(values_packed)).encode() values_len_of_len = str(len(values_len)).encode() # calculations if numpy: x_calc = numpy.arange(float(ptcnt)) * xincr y_calc = ((values_arr - yoffs) * ymult) + yzero else: x_calc = tuple([float(val) * float(xincr) for val in range(ptcnt)]) y_calc = tuple(((val - yoffs) * float(ymult)) + float(yzero) for val in values) with expected_protocol( ik.tektronix.TekTDS5xx, [ "DAT:SOU?", "DAT:ENC RIB", "DATA:WIDTH?", "CURVE?", f"WFMP:CH{channel_no+1}:YOF?", f"WFMP:CH{channel_no+1}:YMU?", f"WFMP:CH{channel_no+1}:YZE?", f"WFMP:CH{channel_no+1}:XIN?", f"WFMP:CH{channel_no+1}:NR_P?" ], [ f"CH{channel_no+1}", f"{data_width}", b"#" + values_len_of_len + values_len + values_packed, f"{yoffs}", f"{ymult}", f"{yzero}", f"{xincr}", f"{ptcnt}" ] ) as inst: channel = inst.channel[channel_no] x_read, y_read = channel.read_waveform(bin_format=True) iterable_eq(x_read, x_calc) iterable_eq(y_read, y_calc)
def test_iterable_eq_fails_sequence_quantity(): """ Test failure on unitful sequences with wrong units, and wrong magnitudes """ a = (1 * u.V, 2 * u.A) b = (1 * u.A, 2 * u.A) # Different units with pytest.raises(AssertionError): iterable_eq(a, b) b = (1 * u.V, 3 * u.A) # Different magnitude with pytest.raises(AssertionError): iterable_eq(a, b)
def test_channel_wavelength(values, channel): values_packed = b"".join(struct.pack("<d", value) for value in values) values_len = str(len(values_packed)).encode() values_len_of_len = str(len(values_len)).encode() with expected_protocol(ik.yokogawa.Yokogawa6370, [ ":FORMat:DATA REAL,64", ":TRAC:X? {}".format(channel.value), ], [b"#" + values_len_of_len + values_len + values_packed]) as inst: values = tuple(values) if numpy: values = numpy.array(values, dtype="<d") iterable_eq(inst.channel[channel].wavelength(), values)
def test_all_voltage(): with expected_protocol(ik.hp.HP6624a, [ "VSET? 1", "VSET? 2", "VSET? 3", "VSET? 4", "VSET 1,{:.1f}".format(5), "VSET 2,{:.1f}".format(5), "VSET 3,{:.1f}".format(5), "VSET 4,{:.1f}".format(5), "VSET 1,{:.1f}".format(1), "VSET 2,{:.1f}".format(2), "VSET 3,{:.1f}".format(3), "VSET 4,{:.1f}".format(4) ], ["2", "3", "4", "5"], sep="\n") as hp: expected = (2 * u.V, 3 * u.V, 4 * u.V, 5 * u.V) iterable_eq(hp.voltage, expected) hp.voltage = 5 * u.V hp.voltage = (1 * u.V, 2 * u.V, 3 * u.V, 4 * u.V)
def test_all_current(): with expected_protocol(ik.hp.HP6624a, [ "ISET? 1", "ISET? 2", "ISET? 3", "ISET? 4", "ISET 1,{:.1f}".format(5), "ISET 2,{:.1f}".format(5), "ISET 3,{:.1f}".format(5), "ISET 4,{:.1f}".format(5), "ISET 1,{:.1f}".format(1), "ISET 2,{:.1f}".format(2), "ISET 3,{:.1f}".format(3), "ISET 4,{:.1f}".format(4) ], ["2", "3", "4", "5"], sep="\n") as hp: expected = (2 * u.A, 3 * u.A, 4 * u.A, 5 * u.A) iterable_eq(hp.current, expected) hp.current = 5 * u.A hp.current = (1 * u.A, 2 * u.A, 3 * u.A, 4 * u.A)
def test_data_snap(): with expected_protocol( ik.srs.SRS830, [ "SNAP? 1,2" ], [ "1.234,9.876" ] ) as inst: data = inst.data_snap(mode1=inst.Mode.x, mode2=inst.Mode.y) expected = [1.234, 9.876] iterable_eq(data, expected)
def test_data_snap_mode_as_str(): with expected_protocol( ik.srs.SRS830, [ "SNAP? 1,2" ], [ "1.234,9.876" ] ) as inst: data = inst.data_snap(mode1='x', mode2='y') expected = [1.234, 9.876] iterable_eq(data, expected)
def test_data_source_read_waveform_ascii(values, ymult, yzero, xzero, xincr): """Read waveform back in ASCII format.""" old_dat_source = 3 old_dat_stop = 100 # "previous" setting # new values channel = 0 yoffs = 0 # already tested with hypothesis # transform values to strings values_str = ",".join([str(value) for value in values]) # calculated values ptcnt = len(values) with expected_protocol( ik.tektronix.TekDPO4104, [ "DAT:SOU?", # old data source f"DAT:SOU CH{channel + 1}", "DAT:STOP?", f"DAT:STOP {10**7}", "DAT:ENC ASCI", # set encoding "CURVE?", # get the data (in bin format) "WFMP:YOF?", "WFMP:YMU?", # query y-offset "WFMP:YZE?", # query y zero "WFMP:XZE?", # query x zero "WFMP:XIN?", # retrieve x increments "WFMP:NR_P?", # retrieve number of points f"DAT:STOP {old_dat_stop}", f"DAT:SOU CH{old_dat_source + 1}" # set back old data source ], [ f"CH{old_dat_source + 1}", f"{old_dat_stop}", f"{values_str}", f"{yoffs}", f"{ymult}", f"{yzero}", f"{xzero}", f"{xincr}", f"{ptcnt}" ]) as inst: # get the values from the instrument x_read, y_read = inst.channel[channel].read_waveform(bin_format=False) # manually calculate the values if numpy: raw = numpy.array(values_str.split(","), dtype=numpy.float) x_calc = numpy.arange(ptcnt) * xincr + xzero y_calc = (raw - yoffs) * ymult + yzero else: x_calc = tuple( [float(val) * xincr + xzero for val in range(ptcnt)]) y_calc = tuple([((float(val) - yoffs) * ymult) + yzero for val in values]) # assert arrays are equal iterable_eq(x_read, x_calc) iterable_eq(y_read, y_calc)
def test_instrument_binblockread_two_reads(): inst = ik.Instrument.open_test() data = bytes.fromhex("00000001000200030004") inst._file.read_raw = mock.MagicMock( side_effect=[b"#", b"2", b"10", data[:6], data[6:]]) expected = (0, 1, 2, 3, 4) if numpy: expected = numpy.array((0, 1, 2, 3, 4)) iterable_eq(inst.binblockread(2), expected) calls_expected = [1, 1, 2, 10, 4] calls_actual = [call[0][0] for call in inst._file.read_raw.call_args_list] iterable_eq(calls_actual, calls_expected)
def test_agilent34410a_r(): with expected_protocol( ik.agilent.Agilent34410a, ["CONF?", "FORM:DATA REAL,64", "R? 1"], [ "VOLT +1.000000E+01,+3.000000E-06", # pylint: disable=no-member b"#18" + bytes.fromhex("3FF0000000000000") ]) as dmm: expected = (u.Quantity(1, u.volt), ) if numpy: expected = numpy.array([1]) * u.volt actual = dmm.r(1) iterable_eq(actual, expected)
def test_data_source_read_waveform_ascii(values): """Read waveform from data source as ASCII.""" # constants - to not overkill it with hypothesis channel_no = 0 yoffs = 1. ymult = 1. yzero = 0.3 xincr = 0.001 # make values to compare with values_str = ",".join([str(value) for value in values]) values_arr = values if numpy: values_arr = numpy.array(values) # calculations ptcnt = len(values) if numpy: x_calc = numpy.arange(float(ptcnt)) * xincr y_calc = ((values_arr - yoffs) * ymult) + yzero else: x_calc = tuple([float(val) * float(xincr) for val in range(ptcnt)]) y_calc = tuple(((val - yoffs) * float(ymult)) + float(yzero) for val in values) with expected_protocol( ik.tektronix.TekTDS5xx, [ "DAT:SOU?", "DAT:ENC ASCI", "CURVE?", f"WFMP:CH{channel_no+1}:YOF?", f"WFMP:CH{channel_no+1}:YMU?", f"WFMP:CH{channel_no+1}:YZE?", f"WFMP:CH{channel_no+1}:XIN?", f"WFMP:CH{channel_no+1}:NR_P?" ], [ f"CH{channel_no+1}", values_str, f"{yoffs}", f"{ymult}", f"{yzero}", f"{xincr}", f"{ptcnt}" ] ) as inst: channel = inst.channel[channel_no] x_read, y_read = channel.read_waveform(bin_format=False) iterable_eq(x_read, x_calc) iterable_eq(y_read, y_calc)
def test_data_source_read_waveform_bin(values, ymult, yzero, xzero, xincr): """Read the waveform of a data trace in bin format.""" old_dat_source = 3 old_dat_stop = 100 # "previous" setting # new values channel = 0 data_width = 2 # use format '>h' for decoding yoffs = 0 # already tested with hypothesis # values packing ptcnt = len(values) values_packed = b"".join(struct.pack(">h", value) for value in values) values_len = str(len(values_packed)).encode() values_len_of_len = str(len(values_len)).encode() with expected_protocol( ik.tektronix.TekDPO4104, [ "DAT:SOU?", # old data source f"DAT:SOU CH{channel+1}", "DAT:STOP?", f"DAT:STOP {10**7}", "DAT:ENC RIB", # set encoding "DATA:WIDTH?", # query data width "CURVE?", # get the data (in bin format) "WFMP:YOF?", # query yoffs "WFMP:YMU?", # query ymult "WFMP:YZE?", # query yzero "WFMP:XZE?", # query x zero "WFMP:XIN?", # retrieve x increments "WFMP:NR_P?", # retrieve number of points f"DAT:STOP {old_dat_stop}", f"DAT:SOU CH{old_dat_source + 1}" # set back old data source ], [ f"CH{old_dat_source+1}", f"{old_dat_stop}", f"{data_width}", b"#" + values_len_of_len + values_len + values_packed, f"{yoffs}", f"{ymult}", f"{yzero}", f"{xzero}", f"{xincr}", f"{ptcnt}" ], ) as inst: x_read, y_read = inst.channel[channel].read_waveform() if numpy: x_calc = numpy.arange(ptcnt) * xincr + xzero y_calc = ((numpy.array(values) - yoffs) * ymult) + yzero else: x_calc = tuple( [float(val) * xincr + xzero for val in range(ptcnt)]) y_calc = tuple([((float(val) - yoffs) * ymult) + yzero for val in values]) iterable_eq(x_read, x_calc) iterable_eq(y_read, y_calc)
def test_read_data_buffer_mode_as_str(): with expected_protocol( ik.srs.SRS830, [ "SPTS?", "TRCA?1,0,2" ], [ "2", "1.234,9.876" ] ) as inst: data = inst.read_data_buffer(channel="ch1") expected = (1.234, 9.876) if numpy: expected = numpy.array(expected) iterable_eq(data, expected)