Exemplo n.º 1
0
    def test_rmul(self):
        # Test with another Quantity object of similar units
        vel3 = self.vel2 * self.vel1
        self.assertEqual(vel3, Quantity(mag=self.mag2*self.mag1, m=2., s=-2.))

        vel4 = 2.*self.vel1
        self.assertEqual(vel4, Quantity(mag=self.mag1*2., m=1., s=-1.))
Exemplo n.º 2
0
    def test_rtruediv(self):
        # Test with another Quantity object of similar units
        time1 = self.vel1 / self.accel1
        self.assertEqual(time1,
                         Quantity(mag=self.mag1 / self.accel1.mag, s=1.))

        inv_vel1 = 2 / self.vel1
        self.assertEqual(inv_vel1, Quantity(mag=2 / self.mag1, m=-1., s=1.))
Exemplo n.º 3
0
    def test_truediv(self):
        # Test with another Quantity object of similar units
        time1 = self.vel1 / self.accel1
        self.assertEqual(time1,
                         Quantity(mag=self.mag1 / self.accel1.mag, s=1.))

        vel3 = self.vel1 / 2.
        self.assertEqual(vel3, Quantity(mag=self.mag1 / 2., m=1., s=-1.))
Exemplo n.º 4
0
    def test_rfloordiv(self):
        # Test with another Quantity object of similar units
        time1 = self.vel1 // self.accel1
        self.assertEqual(time1,
                         Quantity(mag=self.mag1 // self.accel1.mag, s=1.))

        vel3 = 2 // self.vel1
        self.assertEqual(vel3, Quantity(mag=2 // self.mag1, m=-1., s=1.))
Exemplo n.º 5
0
 def test_from_units(self):
     self.assertEqual(Quantity.from_units(mag=self.mag1, units='m/s'),
                      self.vel1)
     self.assertEqual(Quantity.from_units(mag=self.mag1, units='m s-1'),
                      self.vel1)
     self.assertEqual(Quantity.from_units(mag=self.mag1, units='m s^-1'),
                      self.vel1)
     self.assertEqual(Quantity.from_units(mag=self.accel1.mag, units='m/s2'),
                      self.accel1)
Exemplo n.º 6
0
 def test_debye_to_einstein(self):
     debye_temp = Quantity.from_units(200., 'K')
     einstein_temp = Quantity.from_units(
             self.ans.at['test_debye_to_einstein', 0], 'K')
     # Check that func works with floats
     self.assertAlmostEqual(c.debye_to_einstein(debye_temp('K')),
                            einstein_temp('K'))
     # Check that func works with Quantity objects
     self.assertAlmostEqual(c.debye_to_einstein(debye_temp),
                            einstein_temp)
Exemplo n.º 7
0
 def test_einstein_to_debye(self):
     einstein_temp = Quantity.from_units(161., 'K')
     debye_temp = Quantity.from_units(
             self.ans.at['test_einstein_to_debye', 0], 'K')
     # Check that func works with floats
     self.assertAlmostEqual(c.einstein_to_debye(einstein_temp('K')),
                            debye_temp('K'))
     # Check that func works with Quantity objects
     self.assertAlmostEqual(c.einstein_to_debye(einstein_temp),
                            debye_temp)
Exemplo n.º 8
0
    def test_pow(self):
        # Test with another Quantity object of similar units
        vel3 = self.vel1 ** 2
        self.assertEqual(vel3, Quantity(mag=self.mag1**2, m=2., s=-2.))

        vel4 = self.vel1 ** Quantity(2.)
        self.assertEqual(vel4, Quantity(mag=self.mag1**2., m=2., s=-2.))

        with self.assertRaises(TypeError):
            self.vel1 ** self.vel2
Exemplo n.º 9
0
    def test_rsub(self):
        # Test with another Quantity object of similar units
        vel3 = self.vel2 - self.vel1
        self.assertEqual(vel3, Quantity(mag=self.mag2-self.mag1, m=1., s=-1))

        # Test if error raised when using incompatible units
        with self.assertRaises(TypeError):
            vel3 = 1. - self.vel1
        with self.assertRaises(TypeError):
            vel3 = Quantity(mag=1., kg=1.) - self.vel1
Exemplo n.º 10
0
    def test_add(self):
        # Test with another Quantity object of similar units
        vel3 = self.vel1 + self.vel2
        self.assertEqual(vel3, Quantity(mag=self.mag1+self.mag2, m=1., s=-1))

        # Test if error raised when using incompatible units
        with self.assertRaises(TypeError):
            vel3 = self.vel1 + 1.
        with self.assertRaises(TypeError):
            vel3 = self.vel1 + Quantity(mag=1., kg=1.)
Exemplo n.º 11
0
 def test_energy_to_temp(self):
     E = Quantity.from_units(0.1, 'eV')
     temp = Quantity.from_units(self.ans.at['test_energy_to_temp', 0], 'K')
     # Check that Quantity objects work
     self.assertAlmostEqual(c.energy_to_temp(E), temp('K'))
     # Check that inputted floats work
     self.assertAlmostEqual(c.energy_to_temp(E('J')), temp('K'))
     self.assertAlmostEqual(c.energy_to_temp(E('eV'), units_in='eV'),
                            temp('K'))
     self.assertAlmostEqual(c.energy_to_temp(E, units_out='oC'),
                            temp('oC'))
     # Check if successfully outputs Quantity objects
     self.assertEqual(c.energy_to_temp(E, return_quantity=True), temp)
Exemplo n.º 12
0
 def test_freq_to_energy(self):
     freq = Quantity.from_units(2.4e13, 'Hz')
     E = Quantity.from_units(self.ans.at['test_freq_to_energy', 0], 'J')
     # Check that Quantity objects work
     self.assertAlmostEqual(c.freq_to_energy(freq), E('J'))
     # Check that inputted floats work
     self.assertAlmostEqual(c.freq_to_energy(freq('Hz')), E('J'))
     self.assertAlmostEqual(
             c.freq_to_energy(freq('min-1'), units_in='min-1'), E('J'))
     self.assertAlmostEqual(
             c.freq_to_energy(freq, units_out='eV'), E('eV'))
     # Check if successfully outputs Quantity objects
     self.assertEqual(c.freq_to_energy(freq, return_quantity=True), E)
Exemplo n.º 13
0
 def test_inertia_to_temp(self):
     inertia = Quantity.from_units(3.5E-49, 'kg m2')
     temp = Quantity.from_units(self.ans.at['test_inertia_to_temp', 0], 'K')
     # Check that Quantity objects work
     self.assertAlmostEqual(c.inertia_to_temp(inertia), temp('K'), places=6)
     # Check that inputted floats work
     self.assertAlmostEqual(c.inertia_to_temp(inertia('kg m2')), temp('K'),
                            places=6)
     self.assertAlmostEqual(
             c.inertia_to_temp(inertia('g cm2'), units_in='g cm2'),
             temp('K'), places=6)
     self.assertAlmostEqual(c.inertia_to_temp(inertia, units_out='oC'),
                            temp('oC'), places=6)
Exemplo n.º 14
0
    def test_imul(self):
        # Set up the class to use
        mag3 = 5.
        vel3 = Quantity(mag=mag3, m=1., s=-1.)

        # Test with another Quantity object of similar units
        vel3 *= self.vel1
        self.assertEqual(vel3, Quantity(mag=mag3*self.mag1, m=2., s=-2.))

        mag4 = 10.
        vel4 = Quantity(mag=mag4, m=1., s=-1.)
        vel4 *= 2.
        self.assertEqual(vel4, Quantity(mag=mag4*2., m=1., s=-1.))
Exemplo n.º 15
0
 def test_energy_to_freq(self):
     E = Quantity.from_units(0.1, 'eV')
     freq = Quantity.from_units(self.ans.at['test_energy_to_freq', 0], 'Hz')
     # Check that Quantity objects work
     self.assertAlmostEqual(c.energy_to_freq(E), freq('Hz'))
     # Check that inputted floats work
     self.assertAlmostEqual(c.energy_to_freq(E('J')), freq('Hz'))
     self.assertAlmostEqual(c.energy_to_freq(E('eV'), units_in='eV'),
                            freq('Hz'))
     self.assertAlmostEqual(c.energy_to_freq(E, units_out='min-1'),
                            freq('min-1'))
     # Check if successfully outputs Quantity objects
     self.assertEqual(c.energy_to_freq(E, return_quantity=True), freq)
Exemplo n.º 16
0
 def test_freq_to_temp(self):
     freq = Quantity.from_units(2.4e13, 'Hz')
     temp = Quantity.from_units(self.ans.at['test_freq_to_temp', 0], 'K')
     # Check that Quantity objects work
     self.assertAlmostEqual(c.freq_to_temp(freq), temp('K'))
     # Check that inputted floats work
     self.assertAlmostEqual(c.freq_to_temp(freq('Hz')), temp('K'))
     self.assertAlmostEqual(
             c.freq_to_temp(freq('min-1'), units_in='min-1'), temp('K'))
     self.assertAlmostEqual(
             c.freq_to_temp(freq, units_out='oC'), temp('oC'))
     # Check if successfully outputs Quantity objects
     self.assertEqual(c.freq_to_temp(freq, return_quantity=True), temp)
Exemplo n.º 17
0
 def test_temp_to_energy(self):
     temp = Quantity.from_units(1150., 'K')
     E = Quantity.from_units(self.ans.at['test_temp_to_energy', 0], 'J')
     # Check that Quantity objects work
     self.assertAlmostEqual(c.temp_to_energy(temp), E('J'))
     # Check that inputted floats work
     self.assertAlmostEqual(c.temp_to_energy(temp('K')), E('J'))
     self.assertAlmostEqual(c.temp_to_energy(temp('oC'), units_in='oC'),
                            E('J'))
     self.assertAlmostEqual(c.temp_to_energy(temp, units_out='eV'),
                            E('eV'))
     # Check if successfully outputs Quantity objects
     self.assertEqual(c.temp_to_energy(temp, return_quantity=True),
                      E)
Exemplo n.º 18
0
    def test_isub(self):
        # Set up the class to use
        mag3 = 5.
        vel3 = Quantity(mag=mag3, m=1., s=-1.)

        # Test with another Quantity object of similar units
        vel3 -= self.vel1
        self.assertEqual(vel3, Quantity(mag=mag3-self.mag1, m=1., s=-1))

        # Test if error raised when using incompatible units
        with self.assertRaises(TypeError):
            vel3 -= 1.
        with self.assertRaises(TypeError):
            vel3 -= Quantity(mag=1., kg=1.)
Exemplo n.º 19
0
def fmin(x1, x2, **kwargs):
    x2_units = x1._get_other_units(x2)
    if not x1.equals(x2):
        err_msg = ('Incompatible units between x1 ({}) and x2 ({}) for '
                   'numpy.fmin.'.format(x1, x2))
        raise TypeError(err_msg)
    return Qty._from_qty(units=x.units, mag=np.fmin(x1.mag, x2.mag, **kwargs))
Exemplo n.º 20
0
def convert_unit(num=None, initial=None, final=None):
    """Converts units between two unit sets

    Parameters
    ----------
        num : float, optional
            Number to convert. If not specified, will return the appropriate
            conversion factor. Default is 0 for temperature-based units, 1
            otherwise.
        initial : str
            Units that num is currently in. Different units must be sparated by
            a ' ' or '/'. Supports powers as numbers after units. e.g. 'cm/s2',
            'cm s-2', or 'cm s^-2'.
        final : str
            Units you would like num to be in. Different units must be sparated
            by a ' ' or '/'. Supports powers as numbers after units. e.g.
            'cm/s2', 'cm s-2', or 'cm s^-2'.
    Returns
    -------
        conversion_num : float
            num in the appropriate units
    Raises
    ------
        ValueError
            If unit types are not consistent or not supported
    """
    if initial in _temp_units and final in _temp_units:
        if num is None:
            num = 0.
        return convert_temp(num=num, initial=initial, final=final)
    else:
        if num is None:
            num = 1.
        in_qty = Quantity.from_units(mag=num, units=initial)
        return in_qty(final)
Exemplo n.º 21
0
 def test_energy_to_wavenumber(self):
     E = Quantity.from_units(0.1, 'eV')
     wavenumber = Quantity.from_units(
             self.ans.at['test_energy_to_wavenumber', 0], 'cm-1')
     # Check that Quantity objects work
     self.assertAlmostEqual(c.energy_to_wavenumber(E),
                            wavenumber('cm-1'))
     # Check that inputted floats work
     self.assertAlmostEqual(c.energy_to_wavenumber(E('J')),
                            wavenumber('cm-1'))
     self.assertAlmostEqual(c.energy_to_wavenumber(E('eV'), units_in='eV'),
                            wavenumber('cm-1'))
     self.assertAlmostEqual(c.energy_to_wavenumber(E, units_out='m-1'),
                            wavenumber('m-1'))
     # Check if successfully outputs Quantity objects
     self.assertEqual(c.energy_to_wavenumber(E, return_quantity=True),
                      wavenumber)
Exemplo n.º 22
0
 def test_temp_to_freq(self):
     temp = Quantity.from_units(1150., 'K')
     freq = Quantity.from_units(
             self.ans.at['test_temp_to_freq', 0], 'Hz')
     # Check that Quantity objects work
     self.assertAlmostEqual(c.temp_to_freq(temp), freq('Hz'))
     # Check that inputted floats work
     self.assertAlmostEqual(c.temp_to_freq(temp('K')),
                            freq('Hz'))
     self.assertAlmostEqual(
             c.temp_to_freq(temp('oC'), units_in='oC'),
             freq('Hz'))
     self.assertAlmostEqual(c.temp_to_freq(temp, units_out='min-1'),
                            freq('min-1'))
     # Check if successfully outputs Quantity objects
     self.assertEqual(c.temp_to_freq(temp, return_quantity=True),
                            freq)
Exemplo n.º 23
0
 def test_freq_to_wavenumber(self):
     freq = Quantity.from_units(2.4e13, 'Hz')
     wavenumber = Quantity.from_units(
             self.ans.at['test_freq_to_wavenumber', 0], 'cm-1')
     # Check that Quantity objects work
     self.assertAlmostEqual(c.freq_to_wavenumber(freq), wavenumber('cm-1'))
     # Check that inputted floats work
     self.assertAlmostEqual(c.freq_to_wavenumber(freq('Hz')),
                            wavenumber('cm-1'))
     self.assertAlmostEqual(
             c.freq_to_wavenumber(freq('min-1'), units_in='min-1'),
             wavenumber('cm-1'))
     self.assertAlmostEqual(c.freq_to_wavenumber(freq, units_out='m-1'),
                            wavenumber('m-1'))
     # Check if successfully outputs Quantity objects
     self.assertEqual(c.freq_to_wavenumber(freq, return_quantity=True),
                            wavenumber)
Exemplo n.º 24
0
 def test_call(self):
     self.assertAlmostEqual(self.vel1(), self.mag1)
     self.assertAlmostEqual(self.vel1('cm/s'), self.mag1*100.)
     self.assertAlmostEqual(self.vel1('cm/min'), self.mag1*100.*60.)
     with self.assertRaises(ValueError):
         self.vel1('cm3')
     
     temp = Quantity(mag=273.15, K=1.)
     self.assertEqual(temp('oC'), temp.mag-273.15)
Exemplo n.º 25
0
 def test_wavenumber_to_freq(self):
     wavenumber = Quantity.from_units(800., 'cm-1')
     freq = Quantity.from_units(
             self.ans.at['test_wavenumber_to_freq', 0], 'Hz')
     # Check that Quantity objects work
     self.assertAlmostEqual(c.wavenumber_to_freq(wavenumber), freq('Hz'))
     # Check that inputted floats work
     self.assertAlmostEqual(c.wavenumber_to_freq(wavenumber('cm-1')),
                            freq('Hz'))
     self.assertAlmostEqual(
             c.wavenumber_to_freq(wavenumber('m-1'), units_in='m-1'),
             freq('Hz'))
     self.assertAlmostEqual(
             c.wavenumber_to_freq(wavenumber, units_out='min-1'),
             freq('min-1'))
     # Check if successfully outputs Quantity objects
     self.assertEqual(c.wavenumber_to_freq(wavenumber, return_quantity=True),
                      freq)
Exemplo n.º 26
0
 def test_wavenumber_to_energy(self):
     wavenumber = Quantity.from_units(800., 'cm-1')
     energy = Quantity.from_units(
             self.ans.at['test_wavenumber_to_energy', 0], 'J')
     # Check that Quantity objects work
     self.assertAlmostEqual(c.wavenumber_to_energy(wavenumber), energy('J'))
     # Check that inputted floats work
     self.assertAlmostEqual(c.wavenumber_to_energy(wavenumber('cm-1')),
                            energy('J'))
     self.assertAlmostEqual(
             c.wavenumber_to_energy(wavenumber('m-1'), units_in='m-1'),
             energy('J'))
     self.assertAlmostEqual(
                     c.wavenumber_to_energy(wavenumber, units_out='eV'),
                     energy('eV'))
     # Check if successfully outputs Quantity objects
     self.assertEqual(c.wavenumber_to_energy(wavenumber, return_quantity=True),
                            energy)
Exemplo n.º 27
0
 def test_wavenumber_to_inertia(self):
     wavenumber = Quantity.from_units(800., 'cm-1')
     inertia = Quantity.from_units(
             self.ans.at['test_wavenumber_to_inertia', 0], 'kg m2')
     # Check that Quantity objects work
     self.assertAlmostEqual(c.wavenumber_to_inertia(wavenumber),
                            inertia('kg m2'))
     # Check that inputted floats work
     self.assertAlmostEqual(c.wavenumber_to_inertia(wavenumber('cm-1')),
                            inertia('kg m2'))
     self.assertAlmostEqual(
             c.wavenumber_to_inertia(wavenumber('m-1'), units_in='m-1'),
             inertia('kg m2'))
     self.assertAlmostEqual(c.wavenumber_to_inertia(wavenumber, units_out='g cm2'),
                            inertia('g cm2'))
     # Check if successfully outputs Quantity objects
     self.assertAlmostEqual(
             c.wavenumber_to_inertia(wavenumber, return_quantity=True),
             inertia)
Exemplo n.º 28
0
 def test_temp_to_wavenumber(self):
     temp = Quantity.from_units(1150., 'K')
     wavenumber = Quantity.from_units(
             self.ans.at['test_temp_to_wavenumber', 0], 'cm-1')
     # Check that Quantity objects work
     self.assertAlmostEqual(c.temp_to_wavenumber(temp), wavenumber('cm-1'))
     # Check that inputted floats work
     self.assertAlmostEqual(c.temp_to_wavenumber(temp('K')),
                            wavenumber('cm-1'))
     self.assertAlmostEqual(
             c.temp_to_wavenumber(temp('oC'), units_in='oC'),
             wavenumber('cm-1'))
     self.assertAlmostEqual(c.temp_to_wavenumber(temp, units_out='m-1'),
                            wavenumber('m-1'))
     # Check if successfully outputs Quantity objects
     # Unsure why but Quantity objects were not compatible despite magnitude
     # and units agreeing. Separating tests
     wavenumber_out = c.temp_to_wavenumber(temp, return_quantity=True)
     self.assertAlmostEqual(wavenumber_out.mag, wavenumber.mag)
     self.assertEqual(wavenumber_out.units, wavenumber.units)
Exemplo n.º 29
0
    def test_nanprod(self):
        # Testing a 1D array
        mag_1d = np.array([5., 6., np.nan])
        speeds_1d = Quantity.from_units(mag=mag_1d, units='m/s')
        units_1d = speeds_1d.units
        expected_nanprod_1d = Quantity._from_qty(mag=np.nanprod(mag_1d),
                                              units=units_1d*len(mag_1d))
        self.assertEqual(expected_nanprod_1d, np.nanprod(speeds_1d))

        # Testing a 2D array
        mag_2d = np.array([[5., 6.], [7., np.nan]])
        speeds_2d = Quantity.from_units(mag=mag_2d, units='m/s')
        units_2d = speeds_2d.units

        # Axis not specified
        expected_nanprod_2d = Quantity._from_qty(mag=np.nanprod(mag_2d),
                                              units=units_2d*mag_2d.size)
        self.assertEqual(expected_nanprod_2d, np.nanprod(speeds_2d))

        # Axis = 0
        axis = 0
        expected_nanprod_2d = Quantity._from_qty(mag=np.nanprod(mag_2d, axis=axis),
                                              units=units_2d*mag_2d.shape[1])
        np.testing.assert_array_equal(expected_nanprod_2d,
                                      np.nanprod(speeds_2d, axis=axis))

        # Axis = 1
        axis = 1
        expected_nanprod_2d = Quantity._from_qty(mag=np.nanprod(mag_2d, axis=axis),
                                              units=units_2d*mag_2d.shape[0])
        np.testing.assert_array_equal(expected_nanprod_2d,
                                      np.nanprod(speeds_2d, axis=axis))
Exemplo n.º 30
0
    def test_nancumsum(self):
        # Testing a 1D array
        mag_1d = np.array([5., np.nan])
        speeds_1d = Quantity.from_units(mag=mag_1d, units='m/s')
        units_1d = speeds_1d.units
        expected_nancumsum_1d = Quantity._from_qty(mag=np.nancumsum(mag_1d),
                                              units=units_1d)
        np.testing.assert_array_equal(expected_nancumsum_1d,
                                      np.nancumsum(speeds_1d))

        # Testing a 2D array
        mag_2d = np.array([[5., 6.], [7., np.nan]])
        speeds_2d = Quantity.from_units(mag=mag_2d, units='m/s')
        units_2d = speeds_2d.units

        # Axis not specified
        expected_nancumsum_2d = Quantity._from_qty(mag=np.nancumsum(mag_2d),
                                              units=units_2d)
        np.testing.assert_array_equal(expected_nancumsum_2d,
                                      np.nancumsum(speeds_2d))

        # Axis = 0
        axis = 0
        expected_nancumsum_2d = Quantity._from_qty(mag=np.nancumsum(mag_2d, axis=axis),
                                              units=units_2d)
        np.testing.assert_array_equal(expected_nancumsum_2d,
                                      np.nancumsum(speeds_2d, axis=axis))

        # Axis = 1
        axis = 1
        expected_nancumsum_2d = Quantity._from_qty(mag=np.nancumsum(mag_2d, axis=axis),
                                              units=units_2d)
        np.testing.assert_array_equal(expected_nancumsum_2d,
                                      np.nancumsum(speeds_2d, axis=axis))