def save_iteration(dataset_id: str, settings_id: str, cost: float, solution: List[int], precision: List[float], n_calls: int, duration: str, violations: int, historic: np.array) -> int: db = MongoDBConnection.get_iterations_db() result = db.insert_one({ "dataset_id": dataset_id, "settings_id": settings_id, "cost": cost, "solution": solution, "precision": precision, "n_calls": n_calls, "duration": duration, "violations": violations, "historic": Binary(pickle.dumps(historic, protocol=2), subtype=128) }) return result.inserted_id
def get_population_snapshot(iterations_id: int, n_snapshot: int) -> np.array: db_iter = MongoDBConnection.get_iterations_db() iter_obj = db_iter.find_one({'_id': iterations_id}) populations_documents = MongoDBConnection.get_population_gridfs() file_id = iter_obj['snapshots_population_documents'][n_snapshot] with populations_documents.get(file_id) as f: population_objs = np.load(f) return population_objs
def get_population(iterations_id: int) -> List[np.array]: db_iter = MongoDBConnection.get_iterations_db() iter_obj = db_iter.find_one({'_id': iterations_id}) populations_documents = MongoDBConnection.get_population_gridfs() population_objs = [] for file_id in iter_obj['snapshots_population_documents']: with populations_documents.get(file_id) as f: population_objs.append(np.load(f)) return population_objs
def get_pareto(iteration_id: int): db_iter = MongoDBConnection.get_iterations_db() iter_obj = db_iter.find_one({'_id': iteration_id}) paretos_gridfs = MongoDBConnection.get_paretos_gridfs() paretos = [] for pareto_file in iter_obj['paretos']: with paretos_gridfs.get(pareto_file) as f: paretos.append(pickle.loads(f.read())) return paretos
def save_population(iterations_id: int, whole_population: List[np.array]): db = MongoDBConnection.get_population_gridfs() gridfs_ids = [-1] * len(whole_population) for n_snapshot, population in enumerate(whole_population): with db.new_file() as f: gridfs_ids[n_snapshot] = f._id np.save(f, population) db_iter = MongoDBConnection.get_iterations_db() db_iter.update_one( {'_id': iterations_id}, {'$set': { 'snapshots_population_documents': gridfs_ids }})
def clear_iterations(dataset_id=None, settings_id=None): find_d = {} if dataset_id is not None: find_d['dataset_id'] = dataset_id if settings_id is not None: find_d['settings_id'] = settings_id db = MongoDBConnection.get_iterations_db() iteration = db.find(find_d) paretos_gridfs = MongoDBConnection.get_paretos_gridfs() for it in iteration: for pareto_file in it['paretos']: paretos_gridfs.delete(pareto_file) db.remove(find_d)
def save_iteration(dataset_id: str, settings_id: str, sp_generations: List[List[float]], paretos, execution_info: Dict): db = MongoDBConnection.get_paretos_gridfs() paretos_pickles = [0] * len(paretos) for n_snp, actual_pareto in enumerate(paretos): with db.new_file() as f: paretos_pickles[n_snp] = f._id binary_data = Binary(pickle.dumps(actual_pareto, protocol=2), subtype=128) f.write(binary_data) db = MongoDBConnection.get_iterations_db() result = db.insert_one({ "dataset_id": dataset_id, "settings_id": settings_id, "execution_info": execution_info, "snapshots": sp_generations, "paretos": paretos_pickles, "version": "v2" }) return result.inserted_id
def max_generations(dataset_id: str, settings_id: str) -> int: db = MongoDBConnection.get_iterations_db() max_dict = db.find({'dataset_id': dataset_id, 'settings_id': settings_id}, {'max_generations_taken'})\ .sort([('max_generations_taken', -1)]).limit(1) return max_dict[0]['max_generations_taken']
def get_distinct_id_iterations(dataset_id: str, settings_id: str) -> List[str]: db = MongoDBConnection.get_iterations_db() return db.distinct('_id', { 'dataset_id': dataset_id, 'settings_id': settings_id })
def count_iterations(dataset_id: str, settings_id: str) -> int: db = MongoDBConnection.get_iterations_db() return db.count_documents({ 'dataset_id': dataset_id, 'settings_id': settings_id })
def find_iteration(dataset_id: str, settings_id: str) -> pymongo.cursor.Cursor: db = MongoDBConnection.get_iterations_db() return db.find({'dataset_id': dataset_id, 'settings_id': settings_id})
def get_iteration(iteration_id: int) -> Dict: db = MongoDBConnection.get_iterations_db() iteration = db.find_one({"_id": iteration_id}) return iteration
def remove_iterations(dataset_id: str, settings_id: str): db = MongoDBConnection.get_iterations_db() db.remove({'dataset_id': dataset_id, 'settings_id': settings_id})
def remove_all_iterations(): db = MongoDBConnection.get_iterations_db() db.remove()