new_simulation.experiment_id = to_experiment.id tags = copy.copy(simulation.tags) tags[CopiedFromSimulation] = simulation.id new_simulation.set_tags(tags) job = simulation.hpc_jobs[-1] # override any fields here as necessary... if job and job.configuration new_simulation.configuration = Configuration( environment_name=job.configuration.environment_name, simulation_input_args=job.configuration.simulation_input_args, working_directory_root=job.configuration.working_directory_root, executable_path=job.configuration.executable_path, maximum_number_of_retries=SetupParser.get(parameter='num_retries'), priority=SetupParser.get(parameter='priority'), min_cores=job.configuration.min_cores, max_cores=job.configuration.max_cores, exclusive=job.configuration.exclusive, node_group_name=SetupParser.get(parameter='node_group'), asset_collection_id=job.configuration.asset_collection_id) with tempfile.TemporaryDirectory() as dir files_to_add_last = {} for f in simulation.files if f.file_name == 'config.json' dest_file = os.path.join(dir, 'config.json') with open(dest_file, 'wb') as fp fp.write(f.retrieve()) modify_config_json(dest_file) # with open(dest_file, 'rb') as fp
def copy_simulation(simulation, to_experiment): simulation.refresh(query_criteria=QueryCriteria().select_children( ['files', 'hpc_jobs', 'tags'])) new_simulation = Simulation(simulation.name, description=simulation.description) new_simulation.experiment_id = to_experiment.id tags = copy.copy(simulation.tags) tags["CopiedFromSimulation"] = simulation.id new_simulation.set_tags(tags) job = simulation.hpc_jobs[-1] # override any fields here as necessary... if job and job.configuration: new_simulation.configuration = Configuration( environment_name=job.configuration.environment_name, simulation_input_args=job.configuration.simulation_input_args, working_directory_root=job.configuration.working_directory_root, executable_path=job.configuration.executable_path, maximum_number_of_retries=SetupParser.get(parameter='num_retries'), priority=SetupParser.get(parameter='priority'), min_cores=job.configuration.min_cores, max_cores=job.configuration.max_cores, exclusive=job.configuration.exclusive, node_group_name=SetupParser.get(parameter='node_group'), asset_collection_id=job.configuration.asset_collection_id) with tempfile.TemporaryDirectory() as dir: files_to_add_last = {} for f in simulation.files: if f.file_name == 'config.json': dest_file = os.path.join(dir, 'config.json') with open(dest_file, 'wb') as fp: fp.write(f.retrieve()) modify_config_json(dest_file) # with open(dest_file, 'rb') as fp: # data = fp.read() filename = dest_file # checksum = hashlib.md5(data).hexdigest() sf = SimulationFile(file_name=filename, file_type=f.file_type, description=f.description) files_to_add_last[filename] = sf else: filename = f.file_name checksum = f.md5_checksum sf = SimulationFile(file_name=filename, file_type=f.file_type, description=f.description, md5_checksum=checksum) new_simulation.add_file(sf) new_simulation.save(return_missing_files=False) if len(files_to_add_last) > 0: for file_path, sf in files_to_add_last.items(): new_simulation.add_file(sf, file_path=file_path) new_simulation.save(return_missing_files=False) print('new sim = ' + str(new_simulation.id)) return new_simulation