Esempio n. 1
0
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]
Esempio n. 2
0
def frechet_distance(ref, gen, func=None, n_jobs=1):
    if func is not None:
        ref_values = mapper(n_jobs)(func, ref)
        gen_values = mapper(n_jobs)(func, gen)
    else:
        ref_values = ref
        gen_values = gen
    ref_mean = np.mean(ref_values)
    ref_var = np.var(ref_values)
    gen_mean = np.mean(gen_values)
    gen_var = np.var(gen_values)
    return calculate_frechet_distance(ref_mean, ref_var, gen_mean, gen_var)
Esempio n. 3
0
def get_all_metrics(ref,
                    gen,
                    k=[1000, 10000],
                    n_jobs=1,
                    gpu=-1,
                    batch_size=512):
    '''
    Computes all available metrics between two lists of SMILES:
    * %valid
    ----- Next metrics are only computed for valid molecules -----
    * %unique@k
    * Frechet ChemNet Distance (FCD)
    * fragment similarity
    * scaffold similarity
    * morgan similarity
    '''
    metrics = {}
    if n_jobs != 1:
        pool = Pool(n_jobs)
    else:
        pool = 1
    metrics['valid'] = fraction_valid(gen, n_jobs=n_jobs)
    gen = remove_invalid(gen, canonize=True)
    ref = remove_invalid(ref, canonize=True)
    gen_mols = mapper(pool)(get_mol, gen)
    ref_mols = mapper(pool)(get_mol, ref)

    if not isinstance(k, (list, tuple)):
        k = [k]
    for k_ in k:
        metrics['unique@{}'.format(k_)] = fraction_unique(gen, k_, pool)

    metrics['FCD'] = frechet_chemnet_distance(ref,
                                              gen,
                                              gpu=gpu,
                                              batch_size=batch_size)
    metrics['SNN'] = morgan_similarity(ref_mols, gen_mols, pool, gpu=gpu)
    metrics['Frag'] = fragment_similarity(ref_mols, gen_mols, pool)
    metrics['Scaf'] = scaffold_similarity(ref_mols, gen_mols, pool)
    metrics['IntDiv'] = internal_diversity(gen_mols, pool)
    metrics['Filters'] = fraction_passes_filters(gen_mols, pool)
    metrics['logP'] = frechet_distance(ref_mols, gen_mols, logP, pool)
    metrics['SA'] = frechet_distance(ref_mols, gen_mols, SA, pool)
    metrics['QED'] = frechet_distance(ref_mols, gen_mols, QED, pool)
    metrics['NP'] = frechet_distance(ref_mols, gen_mols, NP, pool)
    metrics['weight'] = frechet_distance(ref_mols, gen_mols, weight, pool)
    if n_jobs != 1:
        pool.close()
    return metrics
Esempio n. 4
0
    def _get_metrics(self, ref, ref_mols, rollout):
        rollout_mols = mapper(self.n_jobs)(get_mol, rollout)
        result = [[0 if m is None else 1] for m in rollout_mols]

        if sum([r[0] for r in result], 0) == 0:
            return result

        rollout = remove_invalid(rollout, canonize=True, n_jobs=self.n_jobs)
        rollout_mols = mapper(self.n_jobs)(get_mol, rollout)
        if len(rollout) < 2:
            return result

        if len(self.metrics):
            for metric_name in self.metrics:
                if metric_name == 'fcd':
                    m = FCDMetric(n_jobs=self.n_jobs)(ref, rollout)
                elif metric_name == 'morgan':
                    m = SNNMetric(n_jobs=self.n_jobs)(ref_mols, rollout_mols)
                elif metric_name == 'fragments':
                    m = FragMetric(n_jobs=self.n_jobs)(ref_mols, rollout_mols)
                elif metric_name == 'scaffolds':
                    m = ScafMetric(n_jobs=self.n_jobs)(ref_mols, rollout_mols)
                elif metric_name == 'internal_diversity':
                    m = internal_diversity(rollout_mols, n_jobs=self.n_jobs)
                elif metric_name == 'filters':
                    m = fraction_passes_filters(rollout_mols,
                                                n_jobs=self.n_jobs)
                elif metric_name == 'logp':
                    m = -FrechetMetric(func=logP, n_jobs=self.n_jobs)(
                        ref_mols, rollout_mols)
                elif metric_name == 'sa':
                    m = -FrechetMetric(func=SA, n_jobs=self.n_jobs)(
                        ref_mols, rollout_mols)
                elif metric_name == 'qed':
                    m = -FrechetMetric(func=QED, n_jobs=self.n_jobs)(
                        ref_mols, rollout_mols)
                elif metric_name == 'np':
                    m = -FrechetMetric(func=NP, n_jobs=self.n_jobs)(
                        ref_mols, rollout_mols)
                elif metric_name == 'weight':
                    m = -FrechetMetric(func=weight, n_jobs=self.n_jobs)(
                        ref_mols, rollout_mols)

                m = MetricsReward._nan2zero(m)
                for i in range(len(rollout)):
                    result[i].append(m)

        return result
Esempio n. 5
0
def fraction_valid(gen, n_jobs=1):
    '''
    Computes a number of valid molecules
    :param gen: list of SMILES
    '''
    gen = mapper(n_jobs)(get_mol, gen)
    return 1 - gen.count(None) / len(gen)
Esempio n. 6
0
def compute_intermediate_statistics(smiles, n_jobs=1, gpu=-1, batch_size=512):
    '''
    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.
    '''

    if n_jobs != 1:
        pool = Pool(n_jobs)
    else:
        pool = 1
    statistics = {}
    mols = mapper(pool)(get_mol, smiles)
    kwargs = {'n_jobs': n_jobs, 'gpu': gpu, 'batch_size': batch_size}
    statistics['FCD'] = FCDMetric(**kwargs).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), ('NP', NP),
                       ('weight', weight)]:
        statistics[name] = FrechetMetric(func, **kwargs).precalc(mols)
    if n_jobs != 1:
        pool.terminate()
    return statistics
Esempio n. 7
0
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), ('NP', NP),
                       ('weight', weight)]:
        statistics[name] = FrechetMetric(func, **kwargs).precalc(mols)
    if close_pool:
        pool.terminate()
    return statistics
Esempio n. 8
0
    def fit(self, data):
        """
        Collects fragment frequencies in a training set

        Arguments:
            data: list of SMILES, training dataset

        """
        # Split molecules from dataset into BRICS fragments
        fragments = mapper(self.n_jobs)(fragmenter, data)

        # Compute fragment frequencies
        counts = Counter()
        for mol_frag in fragments:
            counts.update(mol_frag)
        counts = pd.DataFrame(pd.Series(counts).items(),
                              columns=['fragment', 'count'])
        counts['attachment_points'] = [
            fragment.count('*') for fragment in counts['fragment'].values
        ]
        counts['frequency'] = counts['count'] / counts['count'].sum()
        self.fragment_counts = counts

        # Compute number of fragments distribution
        fragments_count_distribution = Counter([len(f) for f in fragments])
        total = sum(fragments_count_distribution.values())
        for k in fragments_count_distribution:
            fragments_count_distribution[k] /= total
        self.fragments_count_distribution = fragments_count_distribution
        self.fitted = True
        return self
Esempio n. 9
0
def reproduce(seed,
              samples_path=None,
              metrics_path=None,
              n_jobs=1,
              device='cpu',
              verbose=False,
              samples=30000):
    train = moses.get_dataset('train')
    model = CombinatorialGenerator(n_jobs=n_jobs)

    if verbose:
        print("Training...")
    model.fit(train)

    if verbose:
        print(f"Sampling for seed {seed}")
    seeds = list(range((seed - 1) * samples, seed * samples))
    samples = mapper(n_jobs)(model.generate_one, seeds)
    if samples_path is not None:
        with open(samples_path, 'w') as f:
            f.write('SMILES\n')
            for sample in samples:
                f.write(sample + '\n')
    if verbose:
        print(f"Computing metrics for seed {seed}")
    metrics = moses.get_all_metrics(samples, n_jobs=n_jobs, device=device)
    if metrics_path is not None:
        with open(metrics_path, 'w') as f:
            for key, value in metrics.items():
                f.write("%s,%f\n" % (key, value))
    return samples, metrics
Esempio n. 10
0
def compute_fragments(mol_list, n_jobs=1):
    """
    fragment list of mols using BRICS and return smiles list
    """
    fragments = Counter()
    for mol_frag in mapper(n_jobs)(fragmenter, mol_list):
        fragments.update(mol_frag)
    return fragments
Esempio n. 11
0
 def generate(self, n, seed=1, mode=0, verbose=False):
     self.set_mode(mode)
     seeds = range((seed - 1) * n, seed * n)
     if verbose:
         print('generating...')
         seeds = tqdm(seeds, total=n)
     samples = mapper(self.n_jobs)(self.generate_one, seeds)
     return samples
Esempio n. 12
0
 def get_vocabulary(self, data):
     clusters = set()
     for mol in tqdm(mapper(self.config.n_jobs)(MolTree, data),
                     total=len(data),
                     postfix=['Creating vocab']):
         for c in mol.nodes:
             clusters.add(c.smiles)
     return JTreeVocab(sorted(list(clusters)))
Esempio n. 13
0
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)
Esempio n. 14
0
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)
Esempio n. 15
0
    def __init__(self, ref, n_ref_subsample, n_rollouts, n_jobs, metrics=[]):
        assert all([m in MetricsReward.supported_metrics for m in metrics])

        self.ref = remove_invalid(ref, canonize=True, n_jobs=n_jobs)
        self.ref_mols = mapper(n_jobs)(get_mol, self.ref)

        self.n_ref_subsample = n_ref_subsample
        self.n_rollouts = n_rollouts
        self.n_jobs = n_jobs
        self.metrics = metrics
Esempio n. 16
0
def compute_scaffolds(mol_list, n_jobs=1, min_rings=2):
    """
    Extracts a scafold from a molecule in a form of a canonic SMILES
    """
    scaffolds = Counter()
    map_ = mapper(n_jobs)
    scaffolds = Counter(
        map_(partial(compute_scaffold, min_rings=min_rings), mol_list))
    if None in scaffolds:
        scaffolds.pop(None)
    return scaffolds
Esempio n. 17
0
def fingerprints(smiles_mols_array,
                 n_jobs=1,
                 already_unique=False,
                 *args,
                 **kwargs):
    '''
    Computes fingerprints of smiles np.array/list/pd.Series with n_jobs workers.
    e.g.fingerprints(smiles_mols_array, type='morgan', n_jobs=10)
    Inserts np.NaN to rows corresponding to incorrect smiles.
    IMPORTANT: if there is at least one np.NaN, the dtype would be float
    :param smiles_mols_array: list/array/pd.Series of smiles or already computed RDKit molecules
    :param n_jobs: number of parralel workers to execute
    :param already_unique: flag for performance reasons, if smiles array is big and already unique.
                           Its value is set to True if smiles_mols_array contain RDKit molecules already.
    '''
    if isinstance(smiles_mols_array, pd.Series):
        smiles_mols_array = smiles_mols_array.values
    else:
        smiles_mols_array = np.asarray(smiles_mols_array)
    if not isinstance(smiles_mols_array[0], str):
        already_unique = True

    if not already_unique:
        smiles_mols_array, inv_index = np.unique(smiles_mols_array,
                                                 return_inverse=True)

    fps = mapper(n_jobs)(partial(fingerprint, *args, **kwargs),
                         smiles_mols_array)

    length = 1  # Need to know the length to convert None into np.array with nan values
    for fp in fps:
        if fp is not None:
            length = fp.shape[-1]
            first_fp = fp
            break
    fps = [
        fp if fp is not None else np.array([np.NaN]).repeat(length)[None, :]
        for fp in fps
    ]
    if scipy.sparse.issparse(first_fp):
        fps = scipy.sparse.vstack(fps).tocsr()
    else:
        fps = np.vstack(fps)
    if not already_unique:
        return fps[inv_index]
    else:
        return fps
Esempio n. 18
0
def fraction_unique(gen, k=None, n_jobs=1, check_validity=True):
    '''
    Computes a number of unique molecules
    :param gen: list of SMILES
    :param k: compute unique@k
    :param check_validity: raises ValueError if invalid molecules are present
    '''
    if k is not None:
        if len(gen) < k:
            warnings.warn(
                "Can't compute unique@{}. gen contains only {} molecules".
                format(k, 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)
Esempio n. 19
0
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)
Esempio n. 20
0
 def precalc(self, mols):
     if self.func is not None:
         values = mapper(self.n_jobs)(self.func, mols)
     else:
         values = mols
     return {'mu': np.mean(values), 'var': np.var(values)}
Esempio n. 21
0
def novelty(gen, train, n_jobs=1):
    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)
Esempio n. 22
0
def get_all_metrics(test, gen, k=None, n_jobs=1, device='cpu',
                    batch_size=512, test_scaffolds=None,
                    ptest=None, ptest_scaffolds=None,
                    pool=None, gpu=None, train=None):
    """
    Computes all available metrics between test (scaffold test)
    and generated sets of SMILES.
    Parameters:
        test: list of test SMILES
        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
        test_scaffolds: list of scaffold test SMILES
            Will compute only on the general test set if not specified
        ptest: dict with precalculated statistics of the test set
        ptest_scaffolds: dict with precalculated statistics
            of the scaffold test set
        pool: optional multiprocessing pool to use for parallelization
        gpu: deprecated, use `device`
        train: list of train SMILES
    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, NP, weight
        * Novelty (molecules not present in train)
    """
    if k is None:
        k = [1000, 10000]
    disable_rdkit_log()
    metrics = {}
    if gpu is not None:
        warnings.warn(
            "parameter `gpu` is deprecated. Use `device`",
            DeprecationWarning
        )
        if gpu == -1:
            device = 'cpu'
        else:
            device = 'cuda:{}'.format(gpu)
    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)

    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
        )
    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}
    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'])
    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)

    # Properties
    for name, func in [('logP', logP), ('SA', SA),
                       ('QED', QED), ('NP', NP),
                       ('weight', weight)]:
        metrics[name] = FrechetMetric(func, **kwargs)(gen=mols,
                                                      pref=ptest[name])

    if train is not None:
        metrics['Novelty'] = novelty(mols, train, pool)
    enable_rdkit_log()
    if close_pool:
        pool.close()
        pool.join()
    return metrics
Esempio n. 23
0
 def precalc(self, mols):
     if self.func is not None:
         values = mapper(self.n_jobs)(self.func, mols)
     else:
         values = mols
     return {'values': values}
Esempio n. 24
0
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)

    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)
    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}
    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'])
    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)

    # Properties
    for name, func in [('logP', logP), ('SA', SA), ('QED', QED),
                       ('weight', weight)]:
        metrics[name] = WassersteinMetric(func, **kwargs)(gen=mols,
                                                          pref=ptest[name])

    if train is not None:
        metrics['Novelty'] = novelty(mols, train, pool)
    enable_rdkit_log()
    if close_pool:
        pool.close()
        pool.join()
    return metrics
Esempio n. 25
0
 def get_reference_data(self, data):
     ref_smiles = remove_invalid(data, canonize=True, n_jobs=self.n_jobs)
     ref_mols = mapper(self.n_jobs)(get_mol, ref_smiles)
     return ref_smiles, ref_mols
Esempio n. 26
0
def get_all_metrics(test,
                    gen,
                    k=[1000, 10000],
                    n_jobs=1,
                    gpu=-1,
                    batch_size=512,
                    test_scaffolds=None,
                    ptest=None,
                    ptest_scaffolds=None):
    '''
    Computes all available metrics between test (scaffold test) and generated sets of SMILES.
    Parameters:
        test: list of test SMILES
        gen: list of generated SMILES
        k: list with values for unique@k.
            Will calculate number of unique molecules in the first k molecules.
        n_jobs: number of workers for parallel processing
        gpu: index of GPU for FCD metric and internal diversity, -1 means use CPU
        batch_size: batch size for FCD metric
        test_scaffolds: list of scaffold test SMILES
            Will compute only on the general test set if not specified
        ptest: dict with precalculated statistics of the test set
        ptest_scaffolds: dict with precalculated statistics of the scaffold test 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, NP, weight
    '''
    disable_rdkit_log()
    metrics = {}
    if n_jobs != 1:
        pool = Pool(n_jobs)
    else:
        pool = 1
    metrics['valid'] = fraction_valid(gen, n_jobs=n_jobs)
    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)

    if ptest is None:
        ptest = compute_intermediate_statistics(test,
                                                n_jobs=n_jobs,
                                                gpu=gpu,
                                                batch_size=batch_size)
    if test_scaffolds is not None and ptest_scaffolds is None:
        ptest_scaffolds = compute_intermediate_statistics(
            test_scaffolds, n_jobs=n_jobs, gpu=gpu, batch_size=batch_size)
    mols = mapper(pool)(get_mol, gen)
    kwargs = {'n_jobs': pool, 'gpu': gpu, 'batch_size': batch_size}
    metrics['FCD/Test'] = FCDMetric(**kwargs)(gen=gen, ptest=ptest['FCD'])
    metrics['SNN/Test'] = SNNMetric(**kwargs)(gen=mols, ptest=ptest['SNN'])
    metrics['Frag/Test'] = FragMetric(**kwargs)(gen=mols, ptest=ptest['Frag'])
    metrics['Scaf/Test'] = ScafMetric(**kwargs)(gen=mols, ptest=ptest['Scaf'])
    if ptest_scaffolds is not None:
        metrics['FCD/TestSF'] = FCDMetric(**kwargs)(
            gen=gen, ptest=ptest_scaffolds['FCD'])
        metrics['SNN/TestSF'] = SNNMetric(**kwargs)(
            gen=mols, ptest=ptest_scaffolds['SNN'])
        metrics['Frag/TestSF'] = FragMetric(**kwargs)(
            gen=mols, ptest=ptest_scaffolds['Frag'])
        metrics['Scaf/TestSF'] = ScafMetric(**kwargs)(
            gen=mols, ptest=ptest_scaffolds['Scaf'])

    metrics['IntDiv'] = internal_diversity(mols, pool, gpu=gpu)
    metrics['IntDiv2'] = internal_diversity(mols, pool, gpu=gpu, p=2)
    metrics['Filters'] = fraction_passes_filters(mols, pool)

    # Properties
    for name, func in [('logP', logP), ('SA', SA), ('QED', QED), ('NP', NP),
                       ('weight', weight)]:
        metrics[name] = FrechetMetric(func, **kwargs)(gen=mols,
                                                      ptest=ptest[name])
    enable_rdkit_log()
    if n_jobs != 1:
        pool.terminate()
    return metrics