Exemplo n.º 1
0
def test_split_unit_str():
    """
    util_fns: Tests that split_unit_str acts correctly.
    """
    # Check that unit strings go through OK.
    mag, units = split_unit_str("42 foobars")
    eq_(mag, 42)
    eq_(units, "foobars")

    # Check that default units work.
    mag, units = split_unit_str("42", default_units="foobars")
    eq_(mag, 42)
    eq_(units, "foobars")

    # Check that default units are ignored if there's actual units.
    mag, units = split_unit_str("42 snafus")
    eq_(mag, 42)
    eq_(units, "snafus")

    # Finally, check that lookups work.
    unit_dict = {
        "FOO": "foobars",
        "SNA": "snafus"
    }
    mag, units = split_unit_str("42 FOO", lookup=unit_dict.__getitem__)
    eq_(mag, 42)
    eq_(units, "foobars")
Exemplo n.º 2
0
def test_split_unit_str_magnitude_with_decimal():
    """
    split_unit_str: Given a string with magnitude containing a decimal, I
    expect the function to correctly parse the magnitude.
    """
    # Decimal and units
    mag, units = split_unit_str("123.4 foobars")
    assert mag == 123.4
    assert units == "foobars"
    # Decimal, units, and exponential
    mag, units = split_unit_str("123.4E1 foobars")
    assert mag == 1234
    assert units == "foobars"
Exemplo n.º 3
0
def test_split_unit_str_magnitude_with_decimal():
    """
    split_unit_str: Given a string with magnitude containing a decimal, I
    expect the function to correctly parse the magnitude.
    """
    # Decimal and units
    mag, units = split_unit_str("123.4 foobars")
    assert mag == 123.4
    assert units == "foobars"
    # Decimal, units, and exponential
    mag, units = split_unit_str("123.4E1 foobars")
    assert mag == 1234
    assert units == "foobars"
Exemplo n.º 4
0
def test_split_unit_str_only_units():
    """
    split_unit_str: Given a bad string containing only units (ie, no numbers),
    I expect the function to raise a ValueError.
    """
    with pytest.raises(ValueError):
        _ = split_unit_str("foobars")
Exemplo n.º 5
0
def test_split_unit_str_only_units():
    """
    split_unit_str: Given a bad string containing only units (ie, no numbers),
    I expect the function to raise a ValueError.
    """
    with pytest.raises(ValueError):
        _ = split_unit_str("foobars")
Exemplo n.º 6
0
def test_split_unit_str_only_exponential():
    """
    split_unit_str: Given a string with only an exponential, I expect the
    function to raise a ValueError.
    """
    with pytest.raises(ValueError):
        _ = split_unit_str("E3")
Exemplo n.º 7
0
def test_split_unit_str_empty_string():
    """
    split_unit_str: Given an empty string, I expect the function to raise
    a ValueError.
    """
    with pytest.raises(ValueError):
        _ = split_unit_str("")
Exemplo n.º 8
0
def test_split_unit_str_only_exponential():
    """
    split_unit_str: Given a string with only an exponential, I expect the
    function to raise a ValueError.
    """
    with pytest.raises(ValueError):
        _ = split_unit_str("E3")
Exemplo n.º 9
0
def test_split_unit_str_empty_string():
    """
    split_unit_str: Given an empty string, I expect the function to raise
    a ValueError.
    """
    with pytest.raises(ValueError):
        _ = split_unit_str("")
Exemplo n.º 10
0
def quantity_constructor(loader, node):
    """
    Constructs a `pq.Quantity` instance from a PyYAML
    node tagged as ``!Q``.
    """
    # Follows the example of http://stackoverflow.com/a/43081967/267841.
    value = loader.construct_scalar(node)
    return pq.Quantity(*split_unit_str(value))
Exemplo n.º 11
0
def quantity_constructor(loader, node):
    """
    Constructs a `pq.Quantity` instance from a PyYAML
    node tagged as ``!Q``.
    """
    # Follows the example of http://stackoverflow.com/a/43081967/267841.
    value = loader.construct_scalar(node)
    return pq.Quantity(*split_unit_str(value))
Exemplo n.º 12
0
    def window(self):
        """
        Gets/sets the length of the coincidence window between the two signals.

        :units: As specified (if a `~pint.Quantity`) or assumed to be
            of units nanoseconds.
        :type: `~pint.Quantity`
        """
        return u.Quantity(*split_unit_str(self.query("WIND?"), "ns"))
Exemplo n.º 13
0
    def voltage(self):
        """
        Gets/sets the output voltage of the source.

        :units: As specified, or assumed to be :math:`\\text{V}` otherwise.
        :type: `float` or `~pint.Quantity`
        """
        raw = self.query("SOUR:VOLT?")
        return u.Quantity(*split_unit_str(raw, u.volt)).to(u.volt)
Exemplo n.º 14
0
    def window(self):
        """
        Gets/sets the length of the coincidence window between the two signals.

        :units: As specified (if a `~quantities.Quantity`) or assumed to be
            of units nanoseconds.
        :type: `~quantities.Quantity`
        """
        return pq.Quantity(*split_unit_str(self.query("WIND?"), "ns"))
Exemplo n.º 15
0
 def dwell_time(self):
     """
     Gets/sets the length of time before a clear signal is sent to the 
     counters.
     
     :units: As specified (if a `~quantities.Quantity`) or assumed to be
         of units seconds.
     :type: `~quantities.Quantity`
     """
     return pq.Quantity(*split_unit_str(self.query("DWEL?"), "s"))
Exemplo n.º 16
0
def test_split_unit_str_magnitude_and_units():
    """
    split_unit_str: Given the input "42 foobars" I expect the output
    to be (42, "foobars").

    This checks that "[val] [units]" works where val is a non-scientific number
    """
    mag, units = split_unit_str("42 foobars")
    assert mag == 42
    assert units == "foobars"
Exemplo n.º 17
0
    def delay(self):
        """
        Get/sets the delay value (in nanoseconds) on Channel 1.

        When setting, ``N`` may be ``0, 2, 4, 6, 8, 10, 12, or 14ns``.

        :rtype: `~pint.Quantity`
        :return: the delay value
        """
        return u.Quantity(*split_unit_str(self.query("DELA?"), "ns"))
Exemplo n.º 18
0
 def dwell_time(self):
     """
     Gets/sets the length of time before a clear signal is sent to the 
     counters.
     
     :units: As specified (if a `~quantities.Quantity`) or assumed to be
         of units seconds.
     :type: `~quantities.Quantity`
     """
     return pq.Quantity(*split_unit_str(self.query("DWEL?"), "s"))
Exemplo n.º 19
0
        def temperature(self):
            """
            Gets the current temperature of the specified channel.

            :units: As specified by the instrument.
            :rtype: `~quantities.quantity.Quantity`
            """
            val, units = split_unit_str(self.query("TEMP?"))
            units = "deg{}".format(units)
            return pq.Quantity(val, units)
Exemplo n.º 20
0
    def delay(self):
        """
        Get/sets the delay value (in nanoseconds) on Channel 1.

        When setting, ``N`` may be ``0, 2, 4, 6, 8, 10, 12, or 14ns``.

        :rtype: quantities.ns
        :return: the delay value
        """
        return pq.Quantity(*split_unit_str(self.query("DELA?"), "ns"))
Exemplo n.º 21
0
def test_split_unit_str_magnitude_and_units():
    """
    split_unit_str: Given the input "42 foobars" I expect the output
    to be (42, "foobars").

    This checks that "[val] [units]" works where val is a non-scientific number
    """
    mag, units = split_unit_str("42 foobars")
    assert mag == 42
    assert units == "foobars"
Exemplo n.º 22
0
        def temperature(self):
            """
            Gets the current temperature of the specified channel.

            :units: As specified by the instrument.
            :rtype: `~quantities.quantity.Quantity`
            """
            val, units = split_unit_str(self.query("TEMP?"))
            units = "deg{}".format(units)
            return pq.Quantity(val, units)
Exemplo n.º 23
0
def test_split_unit_str_ignore_default_units():
    """
    split_unit_str: Given the input "42 snafus" and default_units="foobars"
    I expect the output to be (42, "snafus").

    This verifies that if the input has units, then any specified default_units
    are ignored.
    """
    mag, units = split_unit_str("42 snafus", default_units="foobars")
    assert mag == 42
    assert units == "snafus"
Exemplo n.º 24
0
def test_split_unit_str_magnitude_and_default_units():
    """
    split_unit_str: Given the input "42" and default_units="foobars"
    I expect output to be (42, "foobars").

    This checks that when given a string without units, the function returns
    default_units as the units.
    """
    mag, units = split_unit_str("42", default_units="foobars")
    assert mag == 42
    assert units == "foobars"
Exemplo n.º 25
0
def test_split_unit_str_magnitude_and_default_units():
    """
    split_unit_str: Given the input "42" and default_units="foobars"
    I expect output to be (42, "foobars").

    This checks that when given a string without units, the function returns
    default_units as the units.
    """
    mag, units = split_unit_str("42", default_units="foobars")
    assert mag == 42
    assert units == "foobars"
Exemplo n.º 26
0
def test_split_unit_str_ignore_default_units():
    """
    split_unit_str: Given the input "42 snafus" and default_units="foobars"
    I expect the output to be (42, "snafus").

    This verifies that if the input has units, then any specified default_units
    are ignored.
    """
    mag, units = split_unit_str("42 snafus", default_units="foobars")
    assert mag == 42
    assert units == "snafus"
Exemplo n.º 27
0
def test_split_unit_str_lookups():
    """
    split_unit_str: Given the input "42 FOO" and a dictionary for our units
    lookup, I expect the output to be (42, "foobars").

    This checks that the unit lookup parameter is correctly called, which can be
    used to map between units as string and their pyquantities equivalent.
    """
    unit_dict = {"FOO": "foobars", "SNA": "snafus"}
    mag, units = split_unit_str("42 FOO", lookup=unit_dict.__getitem__)
    assert mag == 42
    assert units == "foobars"
Exemplo n.º 28
0
def test_split_unit_str_scientific_notation():
    """
    split_unit_str: Given inputs of scientific notation, I expect the output
    to correctly represent the inputted magnitude.

    This checks that inputs with scientific notation are correctly converted
    to floats.
    """
    # No signs, no units
    mag, units = split_unit_str("123E1")
    assert mag == 1230
    assert units == pq.dimensionless
    # Negative exponential, no units
    mag, units = split_unit_str("123E-1")
    assert mag == 12.3
    assert units == pq.dimensionless
    # Negative magnitude, no units
    mag, units = split_unit_str("-123E1")
    assert mag == -1230
    assert units == pq.dimensionless
    # No signs, with units
    mag, units = split_unit_str("123E1 foobars")
    assert mag == 1230
    assert units == "foobars"
    # Signs everywhere, with units
    mag, units = split_unit_str("-123E-1 foobars")
    assert mag == -12.3
    assert units == "foobars"
    # Lower case e
    mag, units = split_unit_str("123e1")
    assert mag == 1230
    assert units == pq.dimensionless
Exemplo n.º 29
0
def test_split_unit_str_scientific_notation():
    """
    split_unit_str: Given inputs of scientific notation, I expect the output
    to correctly represent the inputted magnitude.

    This checks that inputs with scientific notation are correctly converted
    to floats.
    """
    # No signs, no units
    mag, units = split_unit_str("123E1")
    assert mag == 1230
    assert units == u.dimensionless
    # Negative exponential, no units
    mag, units = split_unit_str("123E-1")
    assert mag == 12.3
    assert units == u.dimensionless
    # Negative magnitude, no units
    mag, units = split_unit_str("-123E1")
    assert mag == -1230
    assert units == u.dimensionless
    # No signs, with units
    mag, units = split_unit_str("123E1 foobars")
    assert mag == 1230
    assert units == "foobars"
    # Signs everywhere, with units
    mag, units = split_unit_str("-123E-1 foobars")
    assert mag == -12.3
    assert units == "foobars"
    # Lower case e
    mag, units = split_unit_str("123e1")
    assert mag == 1230
    assert units == u.dimensionless
Exemplo n.º 30
0
def test_split_unit_str_lookups():
    """
    split_unit_str: Given the input "42 FOO" and a dictionary for our units
    lookup, I expect the output to be (42, "foobars").

    This checks that the unit lookup parameter is correctly called, which can be
    used to map between units as string and their pyquantities equivalent.
    """
    unit_dict = {
        "FOO": "foobars",
        "SNA": "snafus"
    }
    mag, units = split_unit_str("42 FOO", lookup=unit_dict.__getitem__)
    assert mag == 42
    assert units == "foobars"
Exemplo n.º 31
0
    def dwell_time(self):
        """
        Gets/sets the length of time before a clear signal is sent to the
        counters.

        :units: As specified (if a `~pint.Quantity`) or assumed to be
            of units seconds.
        :type: `~pint.Quantity`
        """
        # the older versions of the firmware erroneously report the units of the
        # dwell time as being seconds rather than ms
        dwell_time = u.Quantity(*split_unit_str(self.query("DWEL?"), "s"))
        if self.firmware[0] <= 2 and self.firmware[1] <= 1:
            return dwell_time/1000.0

        return dwell_time
Exemplo n.º 32
0
    def dwell_time(self):
        """
        Gets/sets the length of time before a clear signal is sent to the
        counters.

        :units: As specified (if a `~quantities.Quantity`) or assumed to be
            of units seconds.
        :type: `~quantities.Quantity`
        """
        # the older versions of the firmware erroneously report the units of the
        # dwell time as being seconds rather than ms
        dwell_time = pq.Quantity(*split_unit_str(self.query("DWEL?"), "s"))
        if self.firmware[0] <= 2 and self.firmware[1] <= 1:
            return dwell_time/1000.0
        else:
            return dwell_time
Exemplo n.º 33
0
    def voltage_range(self):
        """
        Gets the voltage range for the current channel.

        The MAX function SCPI command is designed in such a way
        on this device that it always returns the largest absolute value.
        There is no need to query MIN, as it is always 0., but one has to
        order the values as MAX can be negative.

        :units: :math:`\\text{V}`.
        :type: array of `~pint.Quantity`
        """
        value = u.Quantity(
            *split_unit_str(self.query("SOUR:VOLT? MAX"), u.volt))
        if value < 0.:
            return value, 0.
        return 0., value
Exemplo n.º 34
0
 def temperature(self):
     val, units = split_unit_str(self._query("TEMP?"))
     units = "deg{}".format(units)
     return pq.Quantity(val, units)
Exemplo n.º 35
0
 def getter(self):
     return pq.Quantity(*split_unit_str(
         self._query("{}?".format(base_name)), default_units))
Exemplo n.º 36
0
 def max_getter(self):
     return pq.Quantity(
         *split_unit_str(self._query("{}:MAX?".format(base_name))))
Exemplo n.º 37
0
 def temperature(self):
     val, units = split_unit_str(self._query("TEMP?"))
     units = "deg{}".format(units)
     return pq.Quantity(val, units)
Exemplo n.º 38
0
 def getter(self):
     return pq.Quantity(*split_unit_str(
         self._query("{}?".format(base_name)),
         default_units
     ))
Exemplo n.º 39
0
 def max_getter(self):
     return pq.Quantity(*split_unit_str(self._query("{}:MAX?".format(base_name))))