Пример #1
0
    def test_keywordonly_arguments(self):
        def dum(x):
            return x

        with pytest.raises(TypeError):

            boot.mix_and_bootstrap([1], [2], dum, dum, 10000)  # pylint: disable=too-many-function-args
Пример #2
0
    def test_valueerror(self):

        with pytest.raises(ValueError):

            boot.mix_and_bootstrap([1], [2], num_iterations=0)

        with pytest.raises(ValueError):

            boot.mix_and_bootstrap([1], [2], num_iterations=-1)
def vDSSB_jarzynski_error_propagation(works_1,
                                      works_2,
                                      *,
                                      temperature=298.15,
                                      boltzmann_kappa=0.001985875,
                                      num_iterations=10000):
    """get STD of the Jarzynki free energy obtained by convolution of bound and unbound work vDSSB

    starting from the bound and unbound work values calculates the STD of the Jarzynski
    free energy estimate (if you use the vDSSB method)
    it uses bootstrapping, can be very time and memory consuming

    Parameters
    -----------
    works_1 : numpy.array
        the first numpy array of the work values
    works_2 : numpy.array
        the second numpy array of the work values
    temperature : float, optional
        the temperature in Kelvin at which the non equilibrium simulation
        has been done, default 298.15 K
    boltzmann_kappa : float
        the Boltzmann constant, the dafault is 0.001985875 kcal/(mol⋅K)
        and if you keep it the `works` shall be in Kcal
    num_iterations : ins, optional, default=10000
        the number of bootstrapping iterations (time and memory consuming)

    Returns
    ----------
    STD, mean : float, float
        the STD of the Jarzynski estimate and it's bootstrapped mean value

    Notes
    -----------
    it uses the `jarzynski_free_energy` present in this module, it uses bootstrapping,
    will take for granted that you mixed the work values with
    `PythonFSDAM.combine_works.combine_non_correlated_works` and uses the
    `PythonFSDAM.bootstrapping.mix_and_bootstrap` function to do the bootstrapping
    """

    helper_obj = _vDSSBJarzynskiErrorPropagationHelperClass(
        temperature=temperature, boltzmann_kappa=boltzmann_kappa)

    out_mean, out_std = boot.mix_and_bootstrap(
        works_1,
        works_2,
        mixing_function=combine.combine_non_correlated_works,
        stat_function=helper_obj.calculate_jarzynski,
        num_iterations=num_iterations)

    return out_std, out_mean
    def test_non_correlated_mixing_and_jarzynski(self):

        values_1 = np.array([-1, -2, -3, 1, 2, 3])
        values_2 = np.array([-4, -5, -6, 4, 5, 6])

        #calculated in a previous run
        expected_mean = -6.463986
        #expected_std = 1.519654

        out_mean, _ = boot.mix_and_bootstrap(
            values_1,
            values_2,
            mixing_function=combine.combine_non_correlated_works,
            stat_function=free.jarzynski_free_energy,
            num_iterations=10000)

        assert np.testing.assert_allclose(out_mean, expected_mean,
                                          rtol=0.01) is None
Пример #5
0
    def test_works(self, mocker):

        values_1 = np.array([1, 2, 3])

        values_2 = np.array([4, 5, 6])

        m_mixing = mock.MagicMock()
        m_stat = mock.MagicMock()

        m_pool = mocker.patch('multiprocessing.pool.Pool')

        m_pool_enter = mock.MagicMock()

        m_pool_enter.return_value.starmap_async.return_value.get.return_value = [
            1., 2., 3.
        ]

        m_pool.return_value.__enter__.return_value = m_pool_enter

        m_array = mocker.patch('numpy.array', return_value=1.)
        m_mean = mocker.patch('numpy.mean', return_value=1.)
        m_std = mocker.patch('numpy.std', return_value=2.)

        mocker.patch.dict('os.environ')

        out_mean, out_std = boot.mix_and_bootstrap(values_1,
                                                   values_2,
                                                   mixing_function=m_mixing,
                                                   stat_function=m_stat,
                                                   num_iterations=1,
                                                   n_threads=2)

        assert out_mean == 1.
        assert out_std == 2.

        assert m_pool.call_args[0][0] == 2

        m_stat.assert_not_called()

        m_array.assert_called_once()

        m_mean.assert_called_once()
        m_std.assert_called_once()
    def test_with_helper_class(self):

        helper_obj = Helper(temperature=100., boltzmann_kappa=0.1)

        values_1 = np.array([-1, -2, -3, 1, 2, 3])
        values_2 = np.array([-4, -5, -6, 4, 5, 6])

        #calculated in a previous run
        expected_mean = -1.20363
        #expected_std = 2.190256

        out_mean, _ = boot.mix_and_bootstrap(
            values_1,
            values_2,
            mixing_function=combine.combine_non_correlated_works,
            stat_function=helper_obj.calculate_jarzynski,
            num_iterations=10000)

        assert np.testing.assert_allclose(out_mean, expected_mean,
                                          rtol=0.07) is None
    def test_with_simple_function(self):

        values_1 = np.array([1, 2, 3])
        values_2 = np.array([4, 5, 6])

        expected_mean = np.mean(values_1 + values_2)
        expected_std = np.std(values_1 + values_2)

        out_mean, out_std = boot.mix_and_bootstrap(
            values_1,
            values_2,
            mixing_function=simple_mix,
            stat_function=dummy_calculation,
            num_iterations=10000)

        assert np.testing.assert_allclose(out_mean, expected_mean,
                                          rtol=0.003) is None

        #this assertion may fail
        assert np.testing.assert_allclose(out_std, expected_std,
                                          rtol=0.6) is None