Exemplo n.º 1
0
 def test_add_compatible(self):
     a = UnitArray([1, 2, 3], units=meters)
     b = UnitArray([1, 2, 3], units=feet)
     result = a + b
     mplusf = (meters + feet).value
     self.assertEqual(result[0], mplusf)
     self.assertEqual(result.units, meters)
Exemplo n.º 2
0
    def test_gram_per_mol(self):
        x = UnitArray([1000, 2000, 3000], units=chr_units.gram_per_mol)
        expected = UnitArray([1000, 2000, 3000], units=mass.g / substance.mole)
        assert_unit_array_almost_equal(x, expected)

        y = UnitArray([1000, 2000, 3000], units="g/mol")
        assert_unit_array_almost_equal(y, expected)
Exemplo n.º 3
0
 def test_subtract_compatible(self):
     a = UnitArray([1, 2, 3], units=meters)
     b = UnitArray([1, 2, 3], units=feet)
     result = a - b
     mminusf = (meters - feet).value
     self.assertEqual(result[0], mminusf)
     self.assertEqual(result.units, meters)
    def test_unit_array_with_decorated_docstring_function(self):
        """Does has_units wrap with docstring work ?
        """
        def addfunc(a, b):
            return a + b

        @has_units
        def add(a, b):
            ''' Add two arrays in ft/s and convert them to m/s.

            Parameters
            ----------
            a : array : units=ft/s
                An array
            b : array : units=ft/s
                Another array

            Returns
            -------
            c : array : units=m/s
                c = a + b
            '''
            return (a + b) * 0.3048

        a = UnitArray(arange(100), units=feet / second)
        b = UnitArray(arange(100)**2, units=feet / second)
        self.assertTrue(
            allclose(addfunc(a, b).as_units(meters / second), add(a, b)))

        a = UnitArray(arange(100), units=meters / second)
        b = UnitArray(arange(100)**2, units=meters / second)
        self.assertTrue(
            allclose(addfunc(a, b).as_units(meters / second), add(a, b)))
    def test_unit_array_with_decorated_docstring_and_inputted_parameters(self):
        """Does has_units wrap with expanded docstring and inputting
        parameters work the same ?
        """
        @has_units(inputs="a:an array:units=ft/s;b:array:units=ft/s",
                   outputs="c:an array:units=m/s")
        def add(a, b):
            " Add two arrays in ft/s and convert them to m/s. "
            return (a + b) * 0.3048

        @has_units
        def add_doc(a, b):
            ''' Add two arrays in ft/s and convert them to m/s.

            Parameters
            ----------
            a : array : units=ft/s
                An array
            b : array : units=ft/s
                Another array

            Returns
            -------
            c : array : units=m/s
                c = a + b
            '''
            return (a + b) * 0.3048

        a = UnitArray(arange(100), units=feet / second)
        b = UnitArray(arange(100)**2, units=feet / second)
        self.assertTrue(allclose(add_doc(a, b), add(a, b)))

        a = UnitArray(arange(100), units=meters / second)
        b = UnitArray(arange(100)**2, units=meters / second)
        self.assertTrue(allclose(add_doc(a, b), add(a, b)))
Exemplo n.º 6
0
    def test_concatenate(self):
        unit_ary_1 = UnitArray(numpy.array((1, 2, 3)), units=meters)
        unit_ary_2 = UnitArray(numpy.array((4, 5, 6)), units=meters)

        new_unit_ary = UnitArray.concatenate([unit_ary_1, unit_ary_2])
        expected = UnitArray((1, 2, 3, 4, 5, 6), units=meters)
        self.assertTrue(numpy.all(new_unit_ary == expected))
Exemplo n.º 7
0
    def test_kilo_dalton(self):
        x = UnitArray([1, 2, 3], units=chr_units.kilo_dalton)
        expected = UnitArray([1, 2, 3], units=chr_units.kilogram_per_mol)
        assert_unit_array_almost_equal(x, expected)
        # This unit is equal but distinct:
        self.assertIsNot(chr_units.kilo_dalton, chr_units.kilogram_per_mol)

        y = UnitArray([1, 2, 3], units="kDa")
        assert_unit_array_almost_equal(y, expected)
Exemplo n.º 8
0
    def test_dalton(self):
        x = UnitArray([1000, 2000, 3000], units=chr_units.gram_per_mol)
        expected = UnitArray([1000, 2000, 3000], units=chr_units.dalton)
        assert_unit_array_almost_equal(x, expected)
        # This unit is equal but distinct:
        self.assertIsNot(chr_units.dalton, chr_units.gram_per_mol)

        y = UnitArray([1000, 2000, 3000], units="Da")
        assert_unit_array_almost_equal(y, expected)
Exemplo n.º 9
0
 def test_divide_pass(self):
     a = UnitArray([1, 2, 3], units=meters / second)
     result = a / 3.0
     self.assertEqual(result.units, meters / second)
     result = 3.0 / a
     self.assertEqual(result.units, second / meters)
     b = UnitArray([3, 2, 1], units=second)
     result = a / b
     self.assertEqual(result.units, meters / second**2)
 def setUp(self):
     # Make some data to play with.
     self.meter_array = UnitArray([1., 2, 3], units=meters)
     self.second_array = UnitArray([3., 2, 1], units=second)
     self.feet_array = UnitArray([4., 5, 6], units=feet)
     self.meter_scalar = UnitScalar(1., units=meters)
     self.second_scalar = UnitScalar(3., units=second)
     self.feet_scalar = UnitScalar(4., units=feet)
     unittest.TestCase.setUp(self)
Exemplo n.º 11
0
    def test_concatenate_keeps_attribute(self):
        """ concatenating does not call __new__ on the result
            so the attribute must be set correctly in __array_finalize__
        """
        unit_ary_1 = UnitArray(numpy.array((1, 2, 3)), units=meters)
        unit_ary_2 = UnitArray(numpy.array((1, 2, 3)), units=meters)

        new_unit_ary = UnitArray.concatenate([unit_ary_1, unit_ary_2])

        self.assertEqual(new_unit_ary.units, meters)
 def test_product_component_purity_calc(self):
     load = self.load
     load_conc_units = load.product_concentration.units
     expt_concentrations = UnitArray([0.07896, 0.82908, 0.03195718],
                                     units=load_conc_units)
     expt_purities = expt_concentrations / load.product_concentration * 100
     expt_purities = UnitArray(expt_purities, units=percent)
     assert_unit_array_almost_equal(load.product_component_purities,
                                    expt_purities,
                                    atol=1e-5)
Exemplo n.º 13
0
 def test_pow_pass(self):
     a = UnitArray([1, 2, 3], units=meters)
     b = 0.5
     result = a**b
     self.assertEqual(result.units, meters**0.5)
     c = array(0.5)
     result = a**c
     self.assertEqual(result.units, meters**0.5)
     d = UnitArray(0.5, units=dimensionless)
     result = a**d
     self.assertEqual(result.units, meters**0.5)
 def test_product_component_purity_calc(self):
     pool = self.pool
     pool_conc_units = pool.product_concentration.units
     sim_concentrations = UnitArray([0.1, 0.25, 0.05],
                                    units=pool_conc_units)
     pool.product_component_concentrations = sim_concentrations
     sim_purities = sim_concentrations / pool.product_concentration * 100
     sim_purities = UnitArray(sim_purities, units=percent)
     assert_unit_array_almost_equal(pool.product_component_purities,
                                    sim_purities,
                                    atol=1e-5)
Exemplo n.º 15
0
 def test_pickle(self):
     'Pickling'
     for a in (
             UnitArray(0),
             UnitArray([0, 1]),
             UnitArray([0, 1], units=feet / seconds),
             UnitArray([[0.5, 1.0], [10., 20.]], units=meters),
     ):
         state = dumps(a)
         b = loads(state)
         self.assertTrue(all(b == a))
         self.assertTrue(hasattr(b, 'units'))
         self.assertEqual(b.units, a.units)
Exemplo n.º 16
0
    def test_concatenate_keeps_attribute_using_mixed_units(self):
        """ concatenating does not call __new__ on the result
            so the attribute must be set correctly in __array_finalize__.
            This test creates another unit array with different units
            before the concatenate to make sure the finalize method does not
            assign the wrong units
        """
        unit_ary_1 = UnitArray(numpy.array((1, 2, 3)), units=meters)
        unit_ary_2 = UnitArray(numpy.array((1, 2, 3)), units=meters)

        unit_ary_3 = UnitArray(numpy.array((1, 2, 3)), units=feet)
        new_unit_ary = UnitArray.concatenate([unit_ary_1, unit_ary_2])

        self.assertEqual(new_unit_ary.units, meters)
Exemplo n.º 17
0
    def test_repr(self):
        """ Test output of repr()"""
        a = UnitArray([1, 2, 3], units="cm")
        self.assertEqual(repr(a), "UnitArray([1, 2, 3], units='0.01*m')")

        # unit with no label
        labelless_unit = cm * gram
        a = UnitArray([1, 2, 3], units=labelless_unit)
        self.assertEqual(repr(a), "UnitArray([1, 2, 3], units='1e-05*m*kg')")

        # dimensionless quantity
        dimensionless_unit = copy(dimensionless)
        dimensionless_unit.label = "Cool unit"
        a = UnitArray([1, 2, 3], units=dimensionless_unit)
        self.assertEqual(repr(a), "UnitArray([1, 2, 3], units='1')")
    def test_unit_array_with_units_decorated(self):
        def func(value):
            return value

        @has_units(inputs="value: a value: units=m/s;")
        def func_wrapped(value):
            return value

        a = UnitArray(arange(100), units=meters / second)
        self.assertTrue(
            allclose(func(a).as_units(meters / second), func_wrapped(a)))

        a = UnitArray(arange(100), units=feet / second)
        self.assertTrue(
            allclose(func(a).as_units(meters / second), func_wrapped(a)))
Exemplo n.º 19
0
 def test_array_equality(self):
     """ Rich comparison of array == UnitArray should compare the data
     """
     ary = numpy.array((1, 2, 3))
     unit_ary = UnitArray(ary)
     res = unit_ary + ary
     assert numpy.all(res == (ary + ary))
Exemplo n.º 20
0
 def test_add(self):
     """ Binary operation of array + UnitArray should return a UnitArray.
     """
     ary = numpy.array((1, 2, 3))
     unit_ary = UnitArray(ary)
     res = unit_ary + ary
     assert type(res) is UnitArray
Exemplo n.º 21
0
    def test_le(self):
        a = UnitArray([1.0, 2.0, 3], units=dimensionless)
        b = UnitArray([3.0, 1.0, 1], units=2.0 * dimensionless)
        result = a <= b

        self.assertEqual(result[0], True)
        self.assertEqual(result[1], True)
        self.assertEqual(result[2], False)
        c = 2.0
        result = b <= c
        self.assertEqual(result[0], False)
        self.assertEqual(result[1], True)
        self.assertEqual(result[2], True)
        self.assertEqual(result[0], False)
        self.assertEqual(result[1], True)
        self.assertEqual(result[2], True)
Exemplo n.º 22
0
 def test_subtract_dimensionless(self):
     a = UnitArray([1, 2, 3], units=dimensionless)
     b = 1
     result = a - b
     self.assertEqual(result.units, dimensionless)
     assert_array_equal(result, UnitArray([0, 1, 2], units=dimensionless))
     result = b - a
     self.assertEqual(result.units, dimensionless)
     assert_array_equal(result, UnitArray([0, -1, -2], units=dimensionless))
     c = array([3, 2, 1])
     result = a - c
     self.assertEqual(result.units, dimensionless)
     assert_array_equal(result, UnitArray([-2, 0, 2], units=dimensionless))
     result = c - a
     self.assertEqual(result.units, dimensionless)
     assert_array_equal(result, UnitArray([2, 0, -2], units=dimensionless))
Exemplo n.º 23
0
    def test_bed_height_proxy(self):
        # Make a view. Equivalent to view = ColumnView(model=self.model)
        model_view = self._get_model_view()
        range_units = self.model.bed_height_range.units

        # Check that the view got the initial values from the model:
        self.assertEqual(model_view.column_type_bed_height_min,
                         UnitScalar(10, units=range_units))
        self.assertEqual(model_view.column_type_bed_height_max,
                         UnitScalar(30, units=range_units))

        # Set model min/max values
        # FIXME: This min_val of 20.0 is required to avoid breaking tests in
        # test_simulation_from_experiment_builder. Somehow, this test is
        # setting CP_001's bed_height_actual to min_val, and other code then
        # try to add CP_001 with a bed_height_actual of 20 in the datasource,
        # provoking a DS collision. The connection is unclear at this point.
        min_val = 20.0
        max_val = 21.22
        min_val_unitted = UnitScalar(min_val, units=range_units)
        max_val_unitted = UnitScalar(max_val, units=range_units)

        # Set the view and make sure the model is updated
        model_view.column_type_bed_height_min = min_val_unitted
        model_view.column_type_bed_height_max = max_val_unitted

        # Check that model's range is updated
        self.assertEqual(self.model.bed_height_range,
                         UnitArray([min_val, max_val], units=range_units))
Exemplo n.º 24
0
 def test_index_name(self):
     """ Can set and retreive the index_name?
     """
     ary = numpy.array((1, 2, 3))
     unit_ary = UnitArray(ary)
     unit_ary.index_name = "depth"
     self.assertEqual(unit_ary.index_name, "depth")
Exemplo n.º 25
0
 def setUp(self):
     # Make some useful data.
     self.unit_array = UnitArray((1, 2, 3), units=meters)
     self.unit_scalar = UnitScalar(1, units=meters)
     self.plain_array = array([1, 2, 3])
     self.plain_scalar = 1
     unittest.TestCase.setUp(self)
Exemplo n.º 26
0
    def test_str(self):
        """ Test output of str() """
        a = UnitArray([1, 2, 3], units="cm")
        self.assertEqual(str(a), "UnitArray (cm): [1, 2, 3]")

        # unit with no label
        labelless_unit = cm * gram
        a = UnitArray([1, 2, 3], units=labelless_unit)
        # For units with no label, the repr of the unit is used.
        self.assertEqual(str(a), "UnitArray (1e-05*m*kg): [1, 2, 3]")

        # dimensionless quantity
        dimensionless_unit = copy(dimensionless)
        dimensionless_unit.label = "Cool unit"
        a = UnitArray([1, 2, 3], units=dimensionless_unit)
        self.assertEqual(str(a), "UnitArray (Cool unit): [1, 2, 3]")
Exemplo n.º 27
0
    def update_product_assays(self):
        """ Returns a list of ProductAssays for all comps but strip

        It is constructed from the model's assay data.
        """
        solution = self.model
        assay_values = solution.product_component_assay_values
        if assay_values is None:
            vals = [0.] * len(solution.product.product_component_assays)
            assay_values = UnitArray(vals, units=percent)

        num_comps = len(assay_values)
        product_assays = []
        for i in range(num_comps):
            assay_name = solution.product.product_component_assays[i]
            # Exclude the strip, since it is handled separately:
            if assay_name == STRIP_COMP_NAME:
                continue

            prod_assay_data = {
                'name': assay_name,
                'proportion': UnitScalar(assay_values[i],
                                         units=assay_values.units)
            }
            product_assays.append(ProductAssay(**prod_assay_data))

        self.product_assays = product_assays
Exemplo n.º 28
0
def request_new_system_type(datasource, kind="livemodal", **traits):
    """ Build a new instance of a SystemType invoking its UI editor.

    Parameters
    -----------
    datasource : Instance(SimpleDataSource)
        User datasource targeted to be contributed to.

    kind : str
        Should be set to None to make it non-blocking (only useful for testing
        purposes).

    traits : dict
        Attributes of the created object. Used to override the defaults.

    Returns
    -------
    Tuple with the object created and the data to create it since it is to be
    contributed to the SimpleDataSource.
    """
    defaults = dict(
        name='New System Type', manufacturer='manufacturer',
        manufacturer_name='New System Type',
        flow_range=UnitArray([0., 0.], units=chr_units.ml_per_min)
    )
    defaults.update(traits)
    model = SystemType(**defaults)
    view = SystemTypeView(model=model)
    return _show_view_and_return_model(view, kind)
Exemplo n.º 29
0
def request_new_column_type(datasource, kind="livemodal", **traits):
    """ Build a new instance of a ColumnType invoking its UI editor.

    Parameters
    -----------
    datasource : Instance(SimpleDataSource)
        User datasource targeted to be contributed to.

    kind : str
        Should be set to None to make it non-blocking (only useful for testing
        purposes).

    traits : dict
        Attributes of the created object. Used to override the defaults.

    Returns
    -------
    Tuple with the object created and the data to create it since it is to be
    contributed to the SimpleDataSource.
    """
    defaults = dict(name='New Column Type', manufacturer='manufacturer',
                    manufacturer_name='New Column Type',
                    diameter=UnitScalar(0.0, units='cm'),
                    bed_height_range=UnitArray([0., 0.], units='cm'),
                    bed_height_adjust_method='None')
    defaults.update(traits)
    model = ColumnType(**defaults)
    view = ColumnTypeView(model=model)
    return _show_view_and_return_model(view, kind)
Exemplo n.º 30
0
def calculate_step_start_times(sim):
    """ Calculate all step start times and stop time of the last step in min
    from the beginning of the method.

    Parameters
    ----------
    sim : _BaseExperiment (Experiment or Simulation)
        Experiment-like object containing the method to analyze.

    Returns
    -------
    UnitArray
        Start times for all steps and stop time of last step in minutes.
    """
    start_times = [0.]

    for step in sim.method.method_steps:
        step_time = vol_to_time(step.volume,
                                step.flow_rate,
                                column=sim.column,
                                to_unit="minute")
        start_times.append(step_time)

    start_times = np.cumsum(start_times)
    return UnitArray(start_times, units="minute")