Exemplo n.º 1
0
def _get_fitness(anion, genes, target, models, deslists):
    """
    the fitness function passed to the engine.

    Parameters
    ----------
    anion : RDKit Mol Object
        the anion comprising the IL
    genes : str
        the smiles string representing the cation of the IL
    target : list, int, or array
        the target property values of the IL
    models : array of, or single Keras model
        array or single keras model to use in the prediction
        of the targets
    deslists : array of, or single pandas dataFrame
        contains the mean and stds of the model inputs

    """
    predictions = []
    for i, name in enumerate(models):
        cation = Chem.MolFromSmiles(genes)
        model = name
        deslist = deslists[i]
        if isinstance(deslist, list):
            deslist = deslist[0]
        feature_vector = []

        for item in deslist:

            if "anion" in item:
                with genetic.suppress_rdkit_sanity():
                    feature_vector.append(
                        calculator([item.partition('-')[0]
                                    ]).CalcDescriptors(anion)[0])
            elif "cation" in item:
                with genetic.suppress_rdkit_sanity():
                    feature_vector.append(
                        calculator([item.partition('-')[0]
                                    ]).CalcDescriptors(cation)[0])
            elif "Temperature, K" in item:
                feature_vector.append(298.15)
            elif "Pressure, kPa" in item:
                feature_vector.append(101.325)
            else:
                print("unknown descriptor in list: %s" % item)
        features_normalized = (feature_vector - deslist.iloc[0].values) /\
            deslist.iloc[1].values
        prediction = np.round(np.exp(
            model.predict(np.array(features_normalized).reshape(1, -1))[0]),
                              decimals=2)
        predictions.append(prediction[0])
    predictions = np.array(predictions)
    error = abs((predictions - target) / target)
    error = np.average(error)

    return 1 - error, predictions
Exemplo n.º 2
0
def get_fitness(anion, genes, target):
    model_ID = "density"
    cation = Chem.MolFromSmiles(genes)
    model = genetic.load_data("{}_qspr.h5".format(model_ID), h5File=True)
    deslist = genetic.load_data("{}_desc.csv".format(model_ID))
    feature_vector = []
    with genetic.suppress_rdkit_sanity():
        for item in deslist:
            if "anion" in item:
                feature_vector.append(
                    calculator([item.partition('-')[0]
                                ]).CalcDescriptors(anion)[0])
            elif "cation" in item:
                feature_vector.append(
                    calculator([item.partition('-')[0]
                                ]).CalcDescriptors(cation)[0])
            elif "Temperature, K" in item:
                feature_vector.append(298.15)
            elif "Pressure, kPa" in item:
                feature_vector.append(101.325)
            else:
                print("unknown descriptor in list: %s" % item)
    features_normalized = (feature_vector - deslist.iloc[0].values) /\
        deslist.iloc[1].values
    prediction = exp(
        model.predict(np.array(features_normalized).reshape(1, -1))[0])
    error = abs((prediction - target) / target)
    return 1 - error, prediction
Exemplo n.º 3
0
def _get_fitness(anion, genes, target, models, deslists):
    """
    the fitness function passed to the engine. In this case fitness
    is determined by a model developed by the salty module.
    The fitness function can handle multi-output models
    """
    predictions = []
    for i, name in enumerate(models):
        cation = Chem.MolFromSmiles(genes)
        model = name
        deslist = deslists[i]
        if isinstance(deslist, list):
            deslist = deslist[0]
        feature_vector = []

        for item in deslist:

            if "anion" in item:
                with genetic.suppress_rdkit_sanity():
                    feature_vector.append(
                        calculator([item.partition('-')[0]
                                    ]).CalcDescriptors(anion)[0])
            elif "cation" in item:
                with genetic.suppress_rdkit_sanity():
                    feature_vector.append(
                        calculator([item.partition('-')[0]
                                    ]).CalcDescriptors(cation)[0])
            elif "Temperature, K" in item:
                feature_vector.append(298.15)
            elif "Pressure, kPa" in item:
                feature_vector.append(101.325)
            else:
                print("unknown descriptor in list: %s" % item)
        features_normalized = (feature_vector - deslist.iloc[0].values) /\
            deslist.iloc[1].values
        prediction = np.round(np.exp(
            model.predict(np.array(features_normalized).reshape(1, -1))[0]),
                              decimals=2)
        predictions.append(prediction[0])
    predictions = np.array(predictions)
    error = abs((predictions - target) / target)
    error = np.average(error)

    return 1 - error, predictions
Exemplo n.º 4
0
def _get_fitness(anion, genes, target, model_ID):
    """
    the fitness function passed to the engine. In this case fitness
    is determined by a model developed by the salty module. It is
    important to note that the fitness function can handle multi-
    output models
    """
    cation = Chem.MolFromSmiles(genes)
    model = genetic.load_data("{}.sav".format(model_ID), pickleFile=True)
    deslist = genetic.load_data("{}_descriptors.csv".format(model_ID))
    feature_vector = []

    for item in deslist:

        if "anion" in item:
            with genetic.suppress_rdkit_sanity():
                feature_vector.append(
                    calculator([item.partition('-')[0]
                                ]).CalcDescriptors(anion)[0])
        elif "cation" in item:
            with genetic.suppress_rdkit_sanity():
                feature_vector.append(
                    calculator([item.partition('-')[0]
                                ]).CalcDescriptors(cation)[0])
        elif "Temperature, K" in item:
            feature_vector.append(298.15)
        elif "Pressure, kPa" in item:
            feature_vector.append(101.325)
        else:
            print("unknown descriptor in list: %s" % item)
    features_normalized = (feature_vector - deslist.iloc[0].values) /\
        deslist.iloc[1].values
    prediction = np.round(np.exp(
        model.predict(np.array(features_normalized).reshape(1, -1))[0]),
                          decimals=2)
    error = abs((prediction - target) / target)
    error = np.average(error)

    return 1 - error, prediction