How to use quantities - 10 common examples

To help you get started, we’ve selected a few quantities examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kanzure / skdb / tests / test_units.py View on Github external
def test_compound_quantities(self):
            m1 = skdb.Unit("m**2/m**3")
            m2 = quantities.CompoundUnit("m**2/m**3")
            self.assertTrue(m1.compatible(m2))
        def test_uncertain_quantities(self):
github CINPLA / exdir / tests / test_quantities.py View on Github external
assert isinstance(result, pq.UncertainQuantity)
    assert result.magnitude.tolist() == pq_value.magnitude.tolist()
    assert result.dimensionality.string == pq_value.dimensionality.string
    assert result.uncertainty.magnitude.tolist() == pq_value.uncertainty.magnitude.tolist()

    pq_dict = {"quantity": {"unit": "m", "value": 1},
               "uq_quantity": {"unit": "m", "uncertainty": [3, 4], "value": [1.0, 2.0]}}
    pq_values = {"quantity": pq.Quantity(1, "m"),
                 "uq_quantity": pq.UncertainQuantity([1, 2], "m", [3, 4])}
    result = convert_back_quantities(pq_values)
    assert result == pq_values

    pq_values = {"list": [1, 2, 3], "quantity": {"unit": "m", "value": 1}}
    result = convert_back_quantities(pq_values)
    assert result == {"list": [1, 2, 3], "quantity": pq.Quantity(1, "m")}
github scidash / sciunit / sciunit / unit_test / validator_tests.py View on Github external
paraVal = ParametersValidator()

        # test validate_quantity
        q = pq.Quantity([1, 2, 3], 'A')
        paraVal.validate_quantity(q)
        self.assertRaises(
            BaseException, paraVal.validate_quantity, "I am not a quantity")

        q = pq.Quantity([1,2,3], pq.s)
        self.assertTrue(paraVal._validate_type_time(q))
        self.assertRaises(
            BaseException, paraVal._validate_type_voltage, q)
        self.assertRaises(
            BaseException, paraVal._validate_type_current, q)

        q = pq.Quantity([1,2,3], pq.V)
        self.assertTrue(paraVal._validate_type_voltage(q))
        self.assertRaises(
            BaseException, paraVal._validate_type_time, q)
        self.assertRaises(
            BaseException, paraVal._validate_type_current, q)

        q = pq.Quantity([1,2,3], pq.A)
        self.assertTrue(paraVal._validate_type_current(q))
        self.assertRaises(
            BaseException, paraVal._validate_type_voltage, q)
        self.assertRaises(
            BaseException, paraVal._validate_type_time, q)

        self.assertRaises(
            BaseException, paraVal._validate_type_current, "I am not a quantity")
        self.assertRaises(
github NeuralEnsemble / PyNN / test / unittests / test_populationview.py View on Github external
# but then need to make sure we get the right initial value
        sim.run(t2)
        sim.reset()
        pv.record('spikes')
        pv.record('w')
        sim.run(t3)
        data = p.get_data(gather=True)
        self.assertEqual(len(data.segments), 2)

        seg0 = data.segments[0]
        self.assertEqual(len(seg0.analogsignals), 1)
        v = seg0.analogsignals[0]
        self.assertEqual(v.name, 'v')
        num_points = int(round((t1 + t2) / sim.get_time_step())) + 1
        self.assertEqual(v.shape, (num_points, pv.size))
        self.assertEqual(v.t_start, 0.0 * pq.ms)
        self.assertEqual(v.units, pq.mV)
        self.assertEqual(v.sampling_period, 0.1 * pq.ms)
        self.assertEqual(len(seg0.spiketrains), 0)

        seg1 = data.segments[1]
        self.assertEqual(len(seg1.analogsignals), 2)
        w = seg1.filter(name='w')[0]
        self.assertEqual(w.name, 'w')
        num_points = int(round(t3 / sim.get_time_step())) + 1
        self.assertEqual(w.shape, (num_points, pv.size))
        self.assertEqual(v.t_start, 0.0)
        self.assertEqual(len(seg1.spiketrains), pv.size)
github NeuralEnsemble / PyNN / test / system / test_neuron.py View on Github external
connector = nrn.AllToAllConnector()
    syn = nrn.StaticSynapse(weight=0.1)
    prj_alpha = nrn.Projection(p2, p1, connector, syn, receptor_type='apical.ampa')

    nrn.run(250.0)

    data = p1.get_data().segments[0].analogsignals
    assert_equal(len(data), 2)  # one array per variable
    assert_equal(data[0].name, 'apical(1.0).v')
    assert_equal(data[1].name, 'soma(0.5).ina')
    assert_equal(data[0].sampling_rate, 10.0 * pq.kHz)
    assert_equal(data[0].units, pq.mV)
    assert_equal(data[1].units, pq.mA / pq.cm**2)
    assert_equal(data[0].t_start, 0.0 * pq.ms)
    assert_equal(data[0].t_stop, 250.1 * pq.ms)  # would prefer if it were 250.0, but this is a fundamental Neo issue
    assert_equal(data[0].shape, (2501, 10))
    return data
github SpikeInterface / spikeextractors / tests / test_mearec_extractors.py View on Github external
def _create_dataset(self):
        M=32
        N=10000
        K=10
        L=150
        T=224
        fs = 30000 * pq.Hz
        duration = N/fs

        self.dataset=dict(
            num_channels=M,
            num_timepoints=N,
            num_events=L,
            num_units=K,
        )
        # create neo spike trains
        times = np.arange(N)
        recordings = np.random.randn(M, N)
        positions = np.random.randn(M, 3)
        templates = np.random.randn(K, T)
        peaks = np.random.randn(K, M)
        sources = np.random.randn(K, N)
        spiketrains = []
github ryanvarley / ExoData / tests / test_equations.py View on Github external
def test_works_earth(self):

        H_p = 8500 * pq.m
        R_p = 1 * aq.R_e
        R_s = 1 * aq.R_s

        answer = 1.12264e-06 * pq.dimensionless
        result = ratioTerminatorToStar(H_p, R_p, R_s)

        self.assertTrue(answer - result < 0.001)
github CINPLA / exdir / tests / test_quantities.py View on Github external
result = convert_quantities(np.array([1, 2, 3]))
    assert result == [1, 2, 3]

    result = convert_quantities(1)
    assert result == 1

    result = convert_quantities(2.3)
    assert result == 2.3

    pq_value = pq.UncertainQuantity([1, 2], "m", [3, 4])
    result = convert_quantities(pq_value)
    assert result == {"unit": "m", "uncertainty": [3, 4], "value": [1.0, 2.0]}

    pq_values = {"quantity": pq.Quantity(1, "m"),
                 "uq_quantity": pq.UncertainQuantity([1, 2], "m", [3, 4])}
    result = convert_quantities(pq_values)
    assert(result == {"quantity": {"unit": "m", "value": 1},
                      "uq_quantity": {"unit": "m", "uncertainty": [3, 4], "value": [1.0, 2.0]}})

    pq_values = {"list": [1, 2, 3], "quantity": pq.Quantity(1, "m")}
    pq_dict = {"list": [1, 2, 3], "quantity": {"unit": "m", "value": 1}}
    result = convert_quantities(pq_values)
    assert result == pq_dict
github JelteF / PyLaTeX / tests / test_quantities.py View on Github external
def test_quantity_uncertain():
    t = pq.UncertainQuantity(7., pq.second, 1.)
    q1 = Quantity(t)
    assert q1.dumps() == r'\SI{7.0 +- 1.0}{\second}'
github CINPLA / exdir / tests / test_quantities.py View on Github external
assert result == {"value": 1, "unit": "m"}

    pq_value = pq.Quantity([1, 2, 3], "m")
    result = convert_quantities(pq_value)
    assert result == {"value": [1, 2, 3], "unit": "m"}

    result = convert_quantities(np.array([1, 2, 3]))
    assert result == [1, 2, 3]

    result = convert_quantities(1)
    assert result == 1

    result = convert_quantities(2.3)
    assert result == 2.3

    pq_value = pq.UncertainQuantity([1, 2], "m", [3, 4])
    result = convert_quantities(pq_value)
    assert result == {"unit": "m", "uncertainty": [3, 4], "value": [1.0, 2.0]}

    pq_values = {"quantity": pq.Quantity(1, "m"),
                 "uq_quantity": pq.UncertainQuantity([1, 2], "m", [3, 4])}
    result = convert_quantities(pq_values)
    assert(result == {"quantity": {"unit": "m", "value": 1},
                      "uq_quantity": {"unit": "m", "uncertainty": [3, 4], "value": [1.0, 2.0]}})

    pq_values = {"list": [1, 2, 3], "quantity": pq.Quantity(1, "m")}
    pq_dict = {"list": [1, 2, 3], "quantity": {"unit": "m", "value": 1}}
    result = convert_quantities(pq_values)
    assert result == pq_dict