コード例 #1
0
 def test_register(self, named):
     cv = CoordinateFunctionCV(lambda s: s.xyz[0][0])
     if named:
         cv = cv.named('foo')
     do_naming = lambda obj, obj_type, store_name: cv.named('foo')
     assert cv.is_named == named
     self.wizard.name = mock.Mock(side_effect=do_naming)
     assert len(self.wizard.cvs) == 0
     self.wizard.register(cv, 'CV', 'cvs')
     assert len(self.wizard.cvs) == 1
     assert cv.name == 'foo'
コード例 #2
0
 def test_name(self):
     wizard = make_mock_wizard('foo')
     cv = CoordinateFunctionCV(lambda s: s.xyz[0][0])
     assert not cv.is_named
     result = wizard.name(cv, obj_type="CV", store_name="cvs")
     assert result is cv
     assert result.is_named
     assert result.name == 'foo'
コード例 #3
0
def test_gaussian_selector():
    wizard = mock_wizard(['x', '1.0', '0.5'])
    cv = CoordinateFunctionCV(lambda s: s.xyz[0][0]).named('x')
    wizard.cvs[cv.name] = cv
    sel = gaussian_selector(wizard)
    assert isinstance(sel, paths.GaussianBiasSelector)
    assert sel.alpha == 2.0
    assert sel.l_0 == 1.0
コード例 #4
0
 def test_name_exists(self):
     wizard = make_mock_retry_wizard(['foo', 'bar'])
     wizard.cvs['foo'] = 'placeholder'
     cv = CoordinateFunctionCV(lambda s: s.xyz[0][0])
     assert not cv.is_named
     result = wizard.name(cv, obj_type="CV", store_name="cvs")
     assert result is cv
     assert result.is_named
     assert result.name == 'bar'
     assert wizard.console.input.call_count == 2
コード例 #5
0
def test_cv_defined_volume(periodic):
    if periodic:
        min_ = 0.0
        max_ = 1.0
        cv = CoordinateFunctionCV(
            lambda snap: snap.xyz[0][0],
            period_min=min_, period_max=max_
        ).named('x')
        inputs = ['x', '0.75', '1.25']
        in_state = make_1d_traj([0.2, 0.8])
        out_state = make_1d_traj([0.5])
    else:
        cv = CoordinateFunctionCV(lambda snap: snap.xyz[0][0]).named('x')
        inputs = ['x', '0.0', '1.0']
        in_state = make_1d_traj([0.5])
        out_state = make_1d_traj([-0.1, 1.1])
    wizard = mock_wizard(inputs)
    wizard.cvs[cv.name] = cv
    vol = CV_DEFINED_VOLUME_PLUGIN(wizard)
    assert "interval" in wizard.console.log_text
    for snap in in_state:
        assert vol(snap)
    for snap in out_state:
        assert not vol(snap)
コード例 #6
0
    def test_storage_description_line(self, count):
        from openpathsampling.experimental.storage.collective_variables \
                import CoordinateFunctionCV
        expected = {
            0: "",
            1: "* 1 cv: ['foo']",
            2: "* 2 cvs: ['foo', 'bar']"
        }[count]

        self.wizard.cvs = {
            name: CoordinateFunctionCV(lambda s: s.xyz[0][0]).named(name)
            for name in ['foo', 'bar'][:count]
        }
        line = self.wizard._storage_description_line('cvs')
        assert line == expected
コード例 #7
0
    def test_build_cv_volume(self, inline, periodic):
        self.set_periodic(periodic)
        yml = self.yml.format(func=self.func[inline])
        dct = yaml.load(yml, Loader=yaml.FullLoader)
        period_min, period_max = {
            'periodic': (-np.pi, np.pi),
            'nonperiodic': (None, None)
        }[periodic]
        mock_cv = CoordinateFunctionCV(lambda s: s.xyz[0][0],
                                       period_min=period_min,
                                       period_max=period_max).named('foo')

        patch_loc = 'paths_cli.compiling.root_compiler._COMPILERS'

        if inline == 'external':
            compilers = {
                'cv': mock_compiler('cv', named_objs={'foo': mock_cv})
            }
        elif inline == 'inline':
            fake_plugin = CVCompilerPlugin(
                name="fake_type",
                parameters=[Parameter('input_data', str)],
                builder=lambda input_data: mock_cv)
            compilers = {
                'cv': mock_compiler('cv',
                                    type_dispatch={'fake_type': fake_plugin})
            }
        else:  # -no-cov-
            raise RuntimeError("Should never get here")

        with mock.patch.dict(patch_loc, compilers):
            vol = build_cv_volume(dct)

        in_state = make_1d_traj([0.5])[0]
        assert vol.collectivevariable(in_state) == 0.5
        assert vol(in_state)
        out_state = make_1d_traj([2.0])[0]
        assert vol.collectivevariable(out_state) == 2.0
        assert not vol(out_state)
        if_periodic = make_1d_traj([7.0])[0]
        assert (vol(if_periodic) == (periodic == 'periodic'))
        expected_class = {
            'nonperiodic': paths.CVDefinedVolume,
            'periodic': paths.PeriodicCVDefinedVolume
        }[periodic]
        assert isinstance(vol, expected_class)
コード例 #8
0
def coordinate(wizard, prereqs=None):
    """Builder for coordinate CV.

    Parameters
    ----------
    wizard : :class:`.Wizard`
        wizard for user interaction
    prereqs :
        prerequisites (unused in this method)

    Return
    ------
    CoordinateFunctionCV :
        the OpenPathSampling CV for this selecting this coordinate
    """
    # TODO: atom_index should be from wizard.ask_custom_eval
    from openpathsampling.experimental.storage.collective_variables import \
            CoordinateFunctionCV
    atom_index = coord = None
    while atom_index is None:
        idx = wizard.ask("For which atom do you want to get the "
                         "coordinate? (counting from zero)")
        try:
            atom_index = int(idx)
        except Exception as e:
            wizard.exception(
                "Sorry, I can't make an atom index from "
                f"'{idx}'", e)

    while coord is None:
        xyz = wizard.ask("Which coordinate (x, y, or z) do you want for "
                         f"atom {atom_index}?")
        try:
            coord = {'x': 0, 'y': 1, 'z': 2}[xyz]
        except KeyError:
            wizard.bad_input("Please select one of 'x', 'y', or 'z'")

    cv = CoordinateFunctionCV(lambda snap: snap.xyz[atom_index][coord])
    return cv
コード例 #9
0
def volume_setup():
    cv = CoordinateFunctionCV(lambda snap: snap.xyz[0][0]).named('x')
    vol1 = paths.CVDefinedVolume(cv, 0.0, 1.0)
    vol2 = paths.CVDefinedVolume(cv, 0.5, 1.5)
    return vol1, vol2