def find_similar_reactions(cls, structure, is_product: Optional[bool] = None): """ search reactions including similar molecules :param structure: CGRtools MoleculeContainer :param is_product: role of molecule: Reactant = False, Product = True, Any = None :return:ReactionSearchCache object with all found reactions or None """ if not isinstance(structure, MoleculeContainer): raise TypeError('Molecule expected') elif not len(structure): raise ValueError('empty query') if is_product is None: role = 0 elif isinstance(is_product, bool): role = 2 if is_product else 1 else: raise ValueError('invalid role') structure = dumps(structure, compression='gzip').hex() schema = cls._table_[0] # define DB schema ci, fnd = cls._database_.select( f'''SELECT * FROM "{schema}".cgrdb_search_reactions_by_molecule('\\x{structure}'::bytea, {role}, 2)''' )[0] if fnd: c = cls._database_.ReactionSearchCache[ci] c.__dict__['_size'] = fnd return c
def _run_job(args): # Reset random seed np.random.seed() synthesizer, metadata, metrics, iteration, cache_dir, \ timeout, run_id, aws_key, aws_secret, max_rows = args name = synthesizer['name'] dataset_name = metadata._metadata['name'] LOGGER.info( 'Evaluating %s on %s dataset %s with timeout %ss; iteration %s; %s', name, metadata.modality, dataset_name, timeout, iteration, used_memory()) if timeout: output = _score_with_timeout(timeout, synthesizer, metadata, metrics, iteration) else: output = _score(synthesizer, metadata, metrics, iteration, max_rows=max_rows) scores = output.get('scores') if not scores: scores = pd.DataFrame({'score': [None]}) else: scores = pd.DataFrame(scores) scores.insert(0, 'synthesizer', name) scores.insert(1, 'dataset', metadata._metadata['name']) scores.insert(2, 'modality', metadata.modality) scores.insert(3, 'iteration', iteration) scores['model_time'] = output.get('model_time') scores['run_id'] = run_id if 'error' in output: scores['error'] = output['error'] if cache_dir: cache_dir_name = str(cache_dir) base_path = f'{cache_dir_name}/{name}_{dataset_name}_{iteration}_{run_id}' if scores is not None: write_csv(scores, f'{base_path}_scores.csv', aws_key, aws_secret) if 'synthetic_data' in output: synthetic_data = compress_pickle.dumps(output['synthetic_data'], compression='gzip') write_file(synthetic_data, f'{base_path}.data.gz', aws_key, aws_secret) if 'exception' in output: exception = output['exception'].encode('utf-8') write_file(exception, f'{base_path}_error.txt', aws_key, aws_secret) return scores
def test_dump_vs_dumps(simple_dump_and_remove): path, compression, message, optimize = simple_dump_and_remove dump( message, path, compression=compression, set_default_extension=False, optimize=optimize, ) cmp1 = dumps(message, compression=compression, arcname=path, optimize=optimize) with open(path, "rb") as f: cmp2 = f.read() if compression != "gzip": assert cmp1 == cmp2 else: assert loads(cmp1, compression) == loads(cmp2, compression)
def find_similar(cls, structure): """ similarity search :param structure: CGRtools MoleculeContainer :return: MoleculeSearchCache object with all found molecules or None """ if not isinstance(structure, MoleculeContainer): raise TypeError('Molecule expected') elif not len(structure): raise ValueError('empty query') structure = dumps(structure, compression='gzip').hex() schema = cls._table_[0] # define DB schema ci, fnd = cls._database_.select( f'''SELECT * FROM "{schema}".cgrdb_search_similar_molecules('\\x{structure}'::bytea)''' )[0] if fnd: c = cls._database_.MoleculeSearchCache[ci] c.__dict__['_size'] = fnd return c
def find_mappingless_substructures(cls, structure): """ search reactions by substructures of molecules :param structure: CGRtools ReactionContainer :return: ReactionSearchCache object with all found reactions or None """ if not isinstance(structure, ReactionContainer): raise TypeError('Reaction expected') elif not structure.reactants and not structure.products: raise ValueError('empty query') structure = dumps(structure, compression='gzip').hex() schema = cls._table_[0] # define DB schema ci, fnd = cls._database_.select( f'''SELECT * FROM "{schema}".cgrdb_search_mappingless_substructure_reactions('\\x{structure}'::bytea)''' )[0] if fnd: c = cls._database_.ReactionSearchCache[ci] c.__dict__['_size'] = fnd return c
def find_substructures(cls, structure): """ substructure search substructure search is 2-step process. first step is screening procedure. next step is isomorphism testing. :param structure: CGRtools MoleculeContainer or QueryContainer :return: MoleculeSearchCache object with all found molecules or None """ if not isinstance(structure, (MoleculeContainer, QueryContainer)): raise TypeError('Molecule or Query expected') elif not len(structure): raise ValueError('empty query') structure = dumps(structure, compression='gzip').hex() schema = cls._table_[0] # define DB schema ci, fnd = cls._database_.select( f'''SELECT * FROM "{schema}".cgrdb_search_substructure_molecules('\\x{structure}'::bytea)''' )[0] if fnd: c = cls._database_.MoleculeSearchCache[ci] c.__dict__['_size'] = fnd return c
def __init__(self, **kwargs): structure = kwargs.pop('structure') if not isinstance(structure, MoleculeContainer): raise TypeError('molecule expected') super().__init__(_structure=dumps(structure, compression='gzip'), **kwargs)
def __setitem__(self, key, value): if self.writeback: self.cache[key] = value self.dict[key.encode(self.keyencoding)] = dumps( value, self._compression, self._protocol, self._fix_imports, self._buffer_callback, self._optimize, **self._kwargs)
def __init__(self, structure): """ storing reaction in DB. :param structure: CGRtools ReactionContainer """ super().__init__(_structure=dumps(structure, compression='gzip'))
def upload_obj(obj, bucket, object_name, compression="gzip"): """ Upload a Python Object to an S3 bucket. """ buffer = cp.dumps(obj, compression=compression) return upload_buffer(buffer, bucket, object_name)
#3.Cliente Socket com objeto serializado com pickle sem o protobuff. import ast, json, socket, sys, struct from ast2json import ast2json import compress_pickle as pickle address = ('localhost', 6005) #endereco do servidor client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #parametros do socket client_socket.connect(address) #conectar ao servidor list_expr = [ "(25+8)+(9*7)/(32/8)**2", "(15-8)*3", "(5+9)*(3**2)", "15+15+15+15*3", "2*2*2*2*2+32", "9*9*9", "(1+2)*(3/4)" ] for expressao in list_expr: expre_str_m = expressao #Recebe a exrpessao ast_parse_x = ast.parse( expre_str_m, "", "eval") #Cria o objeto ast – arvore binaria com a expressao sout = pickle.dumps( ast_parse_x, compression="gzip") #Serializa com compressao dos dados client_socket.sendall(sout) #Envia os dados serializados print("Expressao", expressao) #imprime a expressao
def test_dumps_loads(random_message, compressions): message = random_message assert loads(dumps(message, compressions), compressions) == message