def dump_to_file(self, file_path): ''' Write the data from the online datbases to a local file ''' with open(file_path, 'wb') as file: pickle.dump(self.prices, file) pickle.dump(self.prices_flat, file)
def dump_to_file(self, file_path=gc.historian_data, refs=False, compressed=False): """Writes the data from the online datbases to a local file. Args: file_path (str, optional): Path to the output file. (default: {gc.historian_data}) refs (bool, optional): Whether to include the references or just the counts. (default: {False}) compressed (bool, optional): Whether the data is compressed. (default: {False}) """ if not refs: file_path += '_no_refs' for k in self.occurrences.keys(): self.occurrences[k] = tuple(self.occurrences[k][0:2]) if compressed: file_path += '_compressed' with open(file_path, 'wb') as file: pickle.dump(dict(self.occurrences), file) MyLogger.print_and_log("Saved to {}".format(file_path), historian_loc, level=1)
def dump_to_file(self, retro, file_path, chiral=False): """Write the template database to a file. Args: retro (bool): Whether in the retrosynthetic direction. file_path (str): Specifies where to save the database. chiral (bool, optional): Whether to care about chirality. (default: {False}) """ if not self.templates: raise ValueError( 'Cannot dump to file if templates have not been loaded') if retro and chiral: pickle_templates = [] # reconstruct template list, but without chiral rxn object (can't be pickled) for template in self.templates: pickle_templates.append({ 'name': template['name'], 'reaction_smarts': template['reaction_smarts'], 'incompatible_groups': template['incompatible_groups'], 'references': template['references'], 'rxn_example': template['rxn_example'], 'explicit_H': template['explicit_H'], '_id': template['_id'], 'product_smiles': template['product_smiles'], 'necessary_reagent': template['necessary_reagent'], 'efgs': template['efgs'], 'intra_only': template['intra_only'], 'dimer_only': template['dimer_only'], 'chiral': template['chiral'], 'count': template['count'], }) else: pickle_templates = self.templates with open(file_path, 'w+') as file: pickle.dump(pickle_templates, file) MyLogger.print_and_log('Wrote templates to {}'.format(file_path), transformer_loc)
def dump_to_file(self, retro, file_path, chiral=False): ''' Write the template database to a file, of which the path in specified in the general configuration ''' if not self.templates: raise ValueError( 'Cannot dump to file if templates have not been loaded') if retro and chiral: pickle_templates = [] # reconstruct template list, but without chiral rxn object (can't be pickled) for template in self.templates: pickle_templates.append({ 'name': template['name'], 'reaction_smarts': template['reaction_smarts'], 'incompatible_groups': template['incompatible_groups'], 'references': template['references'], 'rxn_example': template['rxn_example'], 'explicit_H': template['explicit_H'], '_id': template['_id'], 'product_smiles': template['product_smiles'], 'necessary_reagent': template['necessary_reagent'], 'efgs': template['efgs'], 'intra_only': template['intra_only'], 'dimer_only': template['dimer_only'], 'chiral': template['chiral'], 'count': template['count'], }) else: pickle_templates = self.templates with open(file_path, 'w+') as file: pickle.dump(pickle_templates, file) MyLogger.print_and_log('Wrote templates to {}'.format(file_path), transformer_loc)
def dump_to_file(self, file_path=gc.historian_data, refs=False, compressed=False): ''' Write the data from the online datbases to a local file ''' if not refs: file_path += '_no_refs' for k in self.occurrences.keys(): self.occurrences[k] = tuple(self.occurrences[k][0:2]) if compressed: file_path += '_compressed' with open(file_path, 'wb') as file: pickle.dump(dict(self.occurrences), file) MyLogger.print_and_log( "Saved to {}".format(file_path), historian_loc, level=1)
def upload_to_db(self): """Uploads the data to the online database.""" db_client = MongoClient(gc.MONGO['path'], gc.MONGO['id'], connect=gc.MONGO['connect']) self.CHEMICAL_HISTORY_DB = db_client[gc.CHEMICAL_HISTORY['database']][ gc.CHEMICAL_HISTORY['collection']] docs = [] failed_docs = [] ctr = 0 for (i, (smi, info)) in tqdm(enumerate(self.occurrences.items())): doc = tup_to_dict(info, refs=True) doc['smiles'] = smi docs.append(doc) ctr += 1 if ctr >= 500: try: self.CHEMICAL_HISTORY_DB.insert_many(docs, ordered=False) except KeyboardInterrupt: raise KeyboardInterrupt except Exception as e: print(e) time.sleep(5) for doc in docs: try: self.CHEMICAL_HISTORY_DB.insert_one(doc) except Exception as e: print('#### {}'.format(e)) print(doc) failed_docs.append(doc) docs = [] ctr = 0 try: self.CHEMICAL_HISTORY_DB.insert_many(docs) except Exception as e: print(e) for doc in docs: try: self.CHEMICAL_HISTORY_DB.insert_one(doc) except Exception as e: print('## {}'.format(e)) failed_docs.append(doc) with open('failed_docs.pickle', 'wb') as fid: pickle.dump(failed_docs, fid, -1)
def dump_to_file(self, file_path=gc.reactionhistorian_data): ''' Write the data from the online datbases to a local file ''' if not self.REACTION_DB: MyLogger.print_and_log( "No database information to output to file.", historian_loc, level=3) return with open(file_path, 'wb') as file: pickle.dump(dict(self.occurrences), file) pickle.dump(dict(self.occurrences_flat), file) MyLogger.print_and_log("Saved to {}".format(file_path), historian_loc, level=1)
def load(self, folder="", worker_no = 0): '''Load a neural network scoring model''' if worker_no==0: MyLogger.print_and_log('Starting to load scorer...', template_nn_scorer_loc) # First load neural network if not folder: MyLogger.print_and_log( 'Cannot load neural network without the directory in which the parameters are saved. Exiting...', template_nn_scorer_loc, level=3) # Get model args ARGS_FPATH = os.path.join(folder, 'args.json') with open(ARGS_FPATH, 'r') as fid: args = json.load(fid) N_h2 = int(args['Nh2']) N_h1 = int(args['Nh1']) N_h3 = int(args['Nh3']) N_hf = int(args['Nhf']) l2v = float(args['l2']) lr = float(args['lr']) context_weight = float(args['context_weight']) enhancement_weight = float(args['enhancement_weight']) optimizer = args['optimizer'] inner_act = args['inner_act'] TARGET_YIELD = False self.model = build(F_atom=self.F_atom, F_bond=self.F_bond, N_h1=N_h1, N_h2=N_h2, N_h3=N_h3, N_hf=N_hf, l2v=l2v, inner_act=inner_act, context_weight=context_weight, enhancement_weight=enhancement_weight, TARGET_YIELD=TARGET_YIELD, absolute_score=True) WEIGHTS_FPATH = os.path.join(folder, 'weights.h5') self.model.load_weights(WEIGHTS_FPATH, by_name=True) # Now load solvent information # Try to load from file first from makeit.utilities.io.files import get_abraham_solvents_path file_path = get_abraham_solvents_path() if os.path.isfile(file_path): with open(file_path, 'rb') as fid: self.solvent_name_to_smiles = pickle.load(fid) self.solvent_smiles_to_params = pickle.load(fid) else: db_client = MongoClient(gc.MONGO['path'], gc.MONGO[ 'id'], connect=gc.MONGO['connect']) db = db_client[gc.SOLVENTS['database']] SOLVENT_DB = db[gc.SOLVENTS['collection']] for doc in SOLVENT_DB.find(): try: if doc['_id'] == 'default': self.solvent_name_to_smiles['default'] = doc['_id'] else: self.solvent_name_to_smiles[doc['name']] = doc['_id'] self.solvent_smiles_to_params[doc['_id']] = doc except KeyError: MyLogger.print_and_log('Solvent doc {} missing a name'.format( doc), template_nn_scorer_loc, level=1) with open(file_path, 'wb') as fid: pickle.dump(self.solvent_name_to_smiles, fid) pickle.dump(self.solvent_smiles_to_params, fid) if worker_no == 0: MyLogger.print_and_log('Scorer has been loaded.', template_nn_scorer_loc)