def remove_invalid(gen, canonize=True, n_jobs=1): """ Removes invalid molecules from the dataset """ if not canonize: mols = mapper(n_jobs)(get_mol, gen) return [gen_ for gen_, mol in zip(gen, mols) if mol is not None] return [x for x in mapper(n_jobs)(canonic_smiles, gen) if x is not None]
def compute_intermediate_statistics(smiles, n_jobs=1, device='cpu', batch_size=512, pool=None): """ The function precomputes statistics such as mean and variance for FCD, etc. It is useful to compute the statistics for test and scaffold test sets to speedup metrics calculation. """ close_pool = False if pool is None: if n_jobs != 1: pool = Pool(n_jobs) close_pool = True else: pool = 1 statistics = {} mols = mapper(pool)(get_mol, smiles) kwargs = {'n_jobs': pool, 'device': device, 'batch_size': batch_size} kwargs_fcd = {'n_jobs': n_jobs, 'device': device, 'batch_size': batch_size} # statistics['FCD'] = FCDMetric(**kwargs_fcd).precalc(smiles) statistics['SNN'] = SNNMetric(**kwargs).precalc(mols) statistics['Frag'] = FragMetric(**kwargs).precalc(mols) statistics['Scaf'] = ScafMetric(**kwargs).precalc(mols) for name, func in [('logP', logP), ('SA', SA), ('QED', QED), ('weight', weight)]: statistics[name] = WassersteinMetric(func, **kwargs).precalc(mols) if close_pool: pool.terminate() return statistics
def novelty(gen, train, n_jobs=1): """ tbd """ gen_smiles = mapper(n_jobs)(canonic_smiles, gen) gen_smiles_set = set(gen_smiles) - {None} train_set = set(train) return len(gen_smiles_set - train_set) / len(gen_smiles_set)
def fraction_valid(gen, n_jobs=1): """ Computes a number of valid molecules Parameters: gen: list of SMILES n_jobs: number of threads for calculation """ gen = mapper(n_jobs)(get_mol, gen) return 1 - gen.count(None) / len(gen)
def fraction_passes_filters(gen, n_jobs=1): """ Computes the fraction of molecules that pass filters: * MCF * PAINS * Only allowed atoms ('C','N','S','O','F','Cl','Br','H') * No charges """ passes = mapper(n_jobs)(mol_passes_filters, gen) return np.mean(passes)
def fraction_unique(gen, k=None, n_jobs=1, check_validity=True): """ Computes a number of unique molecules Parameters: gen: list of SMILES k: compute unique@k n_jobs: number of threads for calculation check_validity: raises ValueError if invalid molecules are present """ if k is not None: if len(gen) < k: warnings.warn( "Can't compute unique@{}.".format(k) + "gen contains only {} molecules".format(len(gen)) ) gen = gen[:k] canonic = set(mapper(n_jobs)(canonic_smiles, gen)) if None in canonic and check_validity: raise ValueError("Invalid molecule passed to unique@k") return len(canonic) / len(gen)
def get_all_metrics(gen, k=None, n_jobs=1, device='cpu', batch_size=512, pool=None, test=None, test_scaffolds=None, ptest=None, ptest_scaffolds=None, train=None): """ Computes all available metrics between test (scaffold test) and generated sets of SMILES. Parameters: gen: list of generated SMILES k: int or list with values for unique@k. Will calculate number of unique molecules in the first k molecules. Default [1000, 10000] n_jobs: number of workers for parallel processing device: 'cpu' or 'cuda:n', where n is GPU device number batch_size: batch size for FCD metric pool: optional multiprocessing pool to use for parallelization test (None or list): test SMILES. If None, will load a default test set test_scaffolds (None or list): scaffold test SMILES. If None, will load a default scaffold test set ptest (None or dict): precalculated statistics of the test set. If None, will load default test statistics. If you specified a custom test set, default test statistics will be ignored ptest_scaffolds (None or dict): precalculated statistics of the scaffold test set If None, will load default scaffold test statistics. If you specified a custom test set, default test statistics will be ignored train (None or list): train SMILES. If None, will load a default train set Available metrics: * %valid * %unique@k * Frechet ChemNet Distance (FCD) * Fragment similarity (Frag) * Scaffold similarity (Scaf) * Similarity to nearest neighbour (SNN) * Internal diversity (IntDiv) * Internal diversity 2: using square root of mean squared Tanimoto similarity (IntDiv2) * %passes filters (Filters) * Distribution difference for logP, SA, QED, weight * Novelty (molecules not present in train) """ # if test is None: # if ptest is not None: # raise ValueError( # "You cannot specify custom test " # "statistics for default test set") # test = get_dataset('test') # ptest = get_statistics('test') # # if test_scaffolds is None: # if ptest_scaffolds is not None: # raise ValueError( # "You cannot specify custom scaffold test " # "statistics for default scaffold test set") # test_scaffolds = get_dataset('test_scaffolds') # ptest_scaffolds = get_statistics('test_scaffolds') # # train = train or get_dataset('train') if k is None: k = [1000, 10000] disable_rdkit_log() metrics = {} close_pool = False if pool is None: if n_jobs != 1: pool = Pool(n_jobs) close_pool = True else: pool = 1 metrics['valid'] = fraction_valid(gen, n_jobs=pool) gen = remove_invalid(gen, canonize=True) if not isinstance(k, (list, tuple)): k = [k] for _k in k: metrics['unique@{}'.format(_k)] = fraction_unique(gen, _k, pool) mols = mapper(pool)(get_mol, gen) kwargs = {'n_jobs': pool, 'device': device, 'batch_size': batch_size} # kwargs_fcd = {'n_jobs': n_jobs, 'device': device, 'batch_size': batch_size} if test or ptest: if ptest is None: ptest = compute_intermediate_statistics(test, n_jobs=n_jobs, device=device, batch_size=batch_size, pool=pool) if test_scaffolds is not None and ptest_scaffolds is None: ptest_scaffolds = compute_intermediate_statistics( test_scaffolds, n_jobs=n_jobs, device=device, batch_size=batch_size, pool=pool ) # metrics['FCD/Test'] = FCDMetric(**kwargs_fcd)(gen=gen, pref=ptest['FCD']) metrics['SNN/Test'] = SNNMetric(**kwargs)(gen=mols, pref=ptest['SNN']) metrics['Frag/Test'] = FragMetric(**kwargs)(gen=mols, pref=ptest['Frag']) metrics['Scaf/Test'] = ScafMetric(**kwargs)(gen=mols, pref=ptest['Scaf']) # Properties for name, func in [('logP', logP), ('SA', SA), ('QED', QED), ('weight', weight)]: metrics[name] = WassersteinMetric(func, **kwargs)( gen=mols, pref=ptest[name]) if test_scaffolds or ptest_scaffolds: if ptest_scaffolds is not None: # metrics['FCD/TestSF'] = FCDMetric(**kwargs_fcd)( # gen=gen, pref=ptest_scaffolds['FCD'] # ) metrics['SNN/TestSF'] = SNNMetric(**kwargs)( gen=mols, pref=ptest_scaffolds['SNN'] ) metrics['Frag/TestSF'] = FragMetric(**kwargs)( gen=mols, pref=ptest_scaffolds['Frag'] ) metrics['Scaf/TestSF'] = ScafMetric(**kwargs)( gen=mols, pref=ptest_scaffolds['Scaf'] ) metrics['IntDiv'] = internal_diversity(mols, pool, device=device) metrics['IntDiv2'] = internal_diversity(mols, pool, device=device, p=2) metrics['Filters'] = fraction_passes_filters(mols, pool) if train is not None: metrics['Novelty'] = novelty(mols, train, pool) enable_rdkit_log() if close_pool: pool.close() pool.join() return metrics
def precalc(self, mols): if self.func is not None: values = mapper(self.n_jobs)(self.func, mols) else: values = mols return {'values': values}