def test_context_manager_replace(): with fsr.handler_context({'syn-mod': SynHandlerMod}): assert_true(fsr._h_registry['syn-mod'] is SynHandlerMod) with fsr.handler_context({'syn-mod': SynHandlerEcho}): assert_true(fsr._h_registry['syn-mod'] is SynHandlerEcho) assert_true(fsr._h_registry['syn-mod'] is SynHandlerMod) assert_false('syn-mod' in fsr._h_registry)
def test_maps_spectrum_round_trip(self): sn = np.sin(self.th) with fsr.handler_context({'hdf5_maps': HDFM}): for eid, sc in zip(self.eids_spectrum, self.scale): print(eid) data = retrieve(eid) assert_array_equal(data, sc * sn)
def test_register_fail(): with fsr.handler_context({'syn-mod': SynHandlerMod}): # shouldn't raise, it is a no-op as it is regiristering # the same class with the same name fsr.register_handler('syn-mod', SynHandlerMod) # should raise as it is trying to change the registered class assert_raises(RuntimeError, fsr.register_handler, 'syn-mod', SynHandlerEcho)
def test_custom(): test_path = os.path.join(BASE_PATH, str(uuid.uuid4()) + '.npy') dd = np.random.rand(500, 500) with fs_write.NpyWriter(test_path, resource_kwargs={'mmap_mode': 'r'}) as f: eid = f.add_data(dd) with fsr.handler_context({'npy': fs_read.NpyHandler}): ret = fsc.retrieve(eid) assert_array_equal(dd, ret)
def test_overwrite_global(): mock_base = dict(spec='syn-mod', resource_path='', resource_kwargs={'shape': (5, 7)}) res = fsc.insert_resource(**mock_base) cache_key = (str(res.id), SynHandlerMod.__name__) with fsr.handler_context({'syn-mod': SynHandlerMod}): fsr.get_spec_handler(res.id) assert_in(cache_key, fsr._HANDLER_CACHE) fsr.register_handler('syn-mod', SynHandlerEcho, overwrite=True) assert_not_in(cache_key, fsr._HANDLER_CACHE)
def test_get_handler_global(): mock_base = dict(spec='syn-mod', resource_path='', resource_kwargs={'shape': (5, 7)}) res = fsc.insert_resource(**mock_base) cache_key = (str(res.id), SynHandlerMod.__name__) with fsr.handler_context({'syn-mod': SynHandlerMod}): handle = fsr.get_spec_handler(res.id) assert_true(isinstance(handle, SynHandlerMod)) assert_in(cache_key, fsr._HANDLER_CACHE) assert_not_in(cache_key, fsr._HANDLER_CACHE)
def run_simulation(sim): # Load info from simulation request sim_params, = find_simulation_parameter_document(_id=sim.params.id) # TODO: Throw in some statments about timeouts, acceptable U(q), etc. iterations = sim_params.iterations target_acceptance = sim_params.target_acceptance ensemble_temp = sim_params.temperature # Load Starting Atoms starting_atoms_entry, = find_atomic_config_document( _id=sim.starting_atoms.id) starting_atoms = starting_atoms_entry.file_payload try: traj_entry, = find_atomic_config_document(_id=sim.starting_atoms.id) traj = traj_entry.file_payload except: traj_entry = None traj = None # We want to continue this simulation if isinstance(traj, list): # Give back the final configuration atoms = traj[-1] # Search filestore and get the file_location with handler_context({'ase': FileLocation}): atoms_file_location = fsc.retrieve(starting_atoms_entry.file_uid) wtraj = PickleTrajectory(atoms_file_location, 'a') # This is a new sim with a new trajectory elif traj is None: # Give back the initial config atoms = starting_atoms # Generate new file location and save it to filestore new_atoms_entry = insert_atom_document( starting_atoms_entry.name + '_' + sim.name, atoms) with handler_context({'ase': FileLocation}): new_file_location = fsc.retrieve(new_atoms_entry.file_uid) wtraj = PickleTrajectory(new_file_location, 'w') sim.simulation_atoms = new_atoms_entry sim.save() # Create Calculators pes, = find_pes_document(_id=sim.pes.id) master_calc = pes.payload # Attach MulitCalc to atoms atoms.set_calculator(master_calc) sim.start_total_energy.append(atoms.get_total_energy()) sim.start_potential_energy.append(atoms.get_potential_energy()) sim.start_kinetic_energy.append(atoms.get_kinetic_energy()) sim.start_time.append(ttime.time()) sim.ran = True sim.save() # Simulate # TODO: eventually support different simulation engines out_traj, samples, l_p_i, seed = nuts(atoms, target_acceptance, iterations, ensemble_temp, wtraj) sim.end_time.append(ttime.time()) sim.total_iterations.append(sim.params.iterations) sim.total_samples.append(samples) sim.leapfrog_per_iter.append(l_p_i) sim.finished = True sim.seed.append(seed) sim.save() # Write info to DB sim.final_potential_energy.append(out_traj[-1].get_potential_energy()) sim.final_kinetic_energy.append(out_traj[-1].get_kinetic_energy()) sim.final_kinetic_energy.append(out_traj[-1].get_total_energy()) sim.save()
def test_retrival(self): with fsr.handler_context({'npy_FRAMEWISE': NpyFrameWise}): for i, datum_id in enumerate(self.datum_ids): data = retrieve(datum_id) known_data = i * np.ones((9, 8)) assert_array_equal(data, known_data)
def test_maps_plane_round_trip(self): base = self.scale.reshape(self.N, self.M) with fsr.handler_context({'hdf5_planes': HDFE}): for eid, v in zip(self.eids_planes, np.sin(self.th)): data = retrieve(eid) assert_array_equal(data, base * v)
def _npsave_helper(dd, base_path): eid = fs_write.save_ndarray(dd, base_path) with fsr.handler_context({'npy': fs_read.NpyHandler}): ret = fsc.retrieve(eid) assert_array_equal(dd, ret)
def run_simulation(sim): # Load info from simulation request sim_params, = find_simulation_parameter_document(_id=sim.params.id) # TODO: Throw in some statments about timeouts, acceptable U(q), etc. iterations = sim_params.iterations target_acceptance = sim_params.target_acceptance ensemble_temp = sim_params.temperature # Load Starting Atoms starting_atoms_entry, = find_atomic_config_document(_id=sim.starting_atoms.id) starting_atoms = starting_atoms_entry.file_payload try: traj_entry, = find_atomic_config_document(_id=sim.starting_atoms.id) traj = traj_entry.file_payload except: traj_entry = None traj = None # We want to continue this simulation if isinstance(traj, list): # Give back the final configuration atoms = traj[-1] # Search filestore and get the file_location with handler_context({'ase': FileLocation}): atoms_file_location = fsc.retrieve(starting_atoms_entry.file_uid) wtraj = PickleTrajectory(atoms_file_location, 'a') # This is a new sim with a new trajectory elif traj is None: # Give back the initial config atoms = starting_atoms # Generate new file location and save it to filestore new_atoms_entry = insert_atom_document( starting_atoms_entry.name + '_' + sim.name, atoms) with handler_context({'ase': FileLocation}): new_file_location = fsc.retrieve(new_atoms_entry.file_uid) wtraj = PickleTrajectory(new_file_location, 'w') sim.simulation_atoms = new_atoms_entry sim.save() # Create Calculators pes, = find_pes_document(_id=sim.pes.id) master_calc = pes.payload # Attach MulitCalc to atoms atoms.set_calculator(master_calc) sim.start_total_energy.append(atoms.get_total_energy()) sim.start_potential_energy.append(atoms.get_potential_energy()) sim.start_kinetic_energy.append(atoms.get_kinetic_energy()) sim.start_time.append(ttime.time()) sim.ran = True sim.save() # Simulate # TODO: eventually support different simulation engines out_traj, samples, l_p_i, seed = nuts(atoms, target_acceptance, iterations, ensemble_temp, wtraj) sim.end_time.append(ttime.time()) sim.total_iterations.append(sim.params.iterations) sim.total_samples.append(samples) sim.leapfrog_per_iter.append(l_p_i) sim.finished = True sim.seed.append(seed) sim.save() # Write info to DB sim.final_potential_energy.append(out_traj[-1].get_potential_energy()) sim.final_kinetic_energy.append(out_traj[-1].get_kinetic_energy()) sim.final_kinetic_energy.append(out_traj[-1].get_total_energy()) sim.save()