def test_tis_trans_info(cv_and_states): cv, state_A, state_B = cv_and_states dct = { 'initial_state': "A", 'final_state': "B", 'interfaces': { 'cv': 'cv', 'minvals': 'float("-inf")', 'maxvals': 'np.array([0, 0.1, 0.2]) * np.pi', } } compiler = { 'cv': mock_compiler('cv', named_objs={'cv': cv}), 'volume': mock_compiler('volume', named_objs={ "A": state_A, "B": state_B }), } with mock.patch.dict(_COMPILERS_LOC, compiler): results = tis_trans_info(dct) check_unidirectional_tis(results, state_A, state_B, cv) paths.InterfaceSet._reset()
def tps_compilers_and_traj(tps_network_and_traj, flat_engine): network, traj = tps_network_and_traj compilers = { 'network': mock_compiler('network', None, {'tps_network': network}), 'engine': mock_compiler('engine', None, {'flat_engine': flat_engine}), } return compilers, traj
def test_compile_attrs_compiler_integration(self): # compile_attrs gives the existing named object (is-identity) if one # of the parameters uses that compiler to load a named object user_input = {'foo': 'named_foo'} # full_input = {'foo': {'name': 'named_foo', # 'type': 'baz', # 'bar': 'should see this'}} bar_plugin = InstanceCompilerPlugin( builder=lambda foo: 'in bar: should not see this', parameters=[Parameter('foo', compiler_for('foo'))], ) foo_plugin = InstanceCompilerPlugin( builder=lambda: 'in foo: should not see this', parameters=[], ) named_objs = {'named_foo': 'should see this'} type_dispatch = {'baz': foo_plugin} PATCH_LOC = 'paths_cli.compiling.root_compiler._COMPILERS' compiler = mock_compiler('foo', type_dispatch=type_dispatch, named_objs=named_objs) with patch.dict(PATCH_LOC, {'foo': compiler}): compiled = bar_plugin.compile_attrs(user_input) # maps attr name 'foo' to the previous existing object assert compiled == {'foo': 'should see this'}
def test_build_topology_engine(self, flat_engine): patch_loc = 'paths_cli.compiling.root_compiler._COMPILERS' compiler = mock_compiler('engine', named_objs={'flat': flat_engine}) compilers = {'engine': compiler} with patch.dict(patch_loc, compilers): topology = build_topology('flat') assert topology.n_spatial == 3 assert topology.n_atoms == 1
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)
def test_build_gaussian_selector(cv_and_states): cv, _, _ = cv_and_states dct = {'cv': 'x', 'mean': 1.0, 'stddev': 2.0} compiler = {'cv': mock_compiler('cv', named_objs={'x': cv})} with patch.dict(_COMPILERS_LOC, compiler): sel = build_gaussian_selector(dct) assert isinstance(sel, paths.GaussianBiasSelector) traj = make_1d_traj([1.0, 1.0, 1.0]) assert sel.f(traj[1], traj) == 1.0
def test_build_combo_volume(self, combo, inline): vol_A, yaml_A, desc_A = self._vol_and_yaml(0.0, 0.55, "A") vol_B, yaml_B, desc_B = self._vol_and_yaml(0.45, 1.0, "B") if inline: named_volumes_dict = {} descriptions = {} subvol_yaml = [' ' + line for line in yaml_A + yaml_B] else: named_volumes_dict = {v.name: v for v in [vol_A, vol_B]} descriptions = {"A": desc_A, "B": desc_B} subvol_yaml = [' - A', ' - B'] yml = "\n".join([f"type: {combo}", "name: bar", "subvolumes:"] + subvol_yaml) combo_class = { 'union': paths.UnionVolume, 'intersection': paths.IntersectionVolume }[combo] builder = { 'union': build_union_volume, 'intersection': build_intersection_volume }[combo] true_vol = combo_class(vol_A, vol_B) dct = yaml.load(yml, yaml.FullLoader) compiler = { 'cv': mock_compiler('cv', named_objs={'foo': self.cv}), 'volume': mock_compiler('volume', type_dispatch={'cv-volume': build_cv_volume}, named_objs=named_volumes_dict), } with mock.patch.dict('paths_cli.compiling.root_compiler._COMPILERS', compiler): vol = builder(dct) traj = make_1d_traj([0.5, 2.0, 0.2]) assert vol(traj[0]) assert not vol(traj[1]) assert vol(traj[2]) == (combo == 'union')
def test_call(self): # the `__call__` method should work in the proxy def _bar_dispatch(dct): return dct['baz'] * dct['qux'] foo_compiler = mock_compiler( category='foo', type_dispatch={'bar': _bar_dispatch}, ) proxy = _CategoryCompilerProxy('foo') user_input = {'type': 'bar', 'baz': 'baz', 'qux': 2} with patch.dict(COMPILER_LOC, {'foo': foo_compiler}): assert proxy(user_input) == "bazbaz"
def test_build_tps_network(cv_and_states): _, state_A, state_B = cv_and_states yml = "\n".join(["initial_states:", " - A", "final_states:", " - B"]) dct = yaml.load(yml, yaml.FullLoader) compiler = { 'volume': mock_compiler('volume', named_objs={ "A": state_A, "B": state_B }), } with mock.patch.dict(_COMPILERS_LOC, compiler): network = build_tps_network(dct) assert isinstance(network, paths.TPSNetwork) assert len(network.initial_states) == len(network.final_states) == 1 assert network.initial_states[0] == state_A assert network.final_states[0] == state_B
def test_movescheme_plugin(tis_compilers_and_traj): paths.InterfaceSet._reset() compilers, traj = tis_compilers_and_traj compilers['strategy'] = mock_compiler( 'strategy', type_dispatch={ 'one-way-shooting': ONE_WAY_SHOOTING_STRATEGY_PLUGIN }, named_objs={} ) dct = {'network': 'tis_network', 'strategies': [ {'type': 'one-way-shooting', 'engine': 'flat_engine'}, ]} with patch.dict(_COMPILERS_LOC, compilers): scheme = MOVESCHEME_PLUGIN(dct) assert isinstance(scheme, paths.MoveScheme) # smoke test that it can build its tree and load init conds scheme.move_decision_tree() _ = scheme.initial_conditions_from_trajectories(traj)
def test_build_topology_fail(self): patch_loc = 'paths_cli.compiling.root_compiler._COMPILERS' compilers = {'engine': mock_compiler('engine')} with patch.dict(patch_loc, compilers): with pytest.raises(InputError, match="foo"): topology = build_topology('foo')