### Initialize Clustering and Validation Pipeline Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Intermediate_RegressorClassifier.ipynb Configures model parameters based on the selected clustering algorithm and initializes the clustering object. This setup is a prerequisite for executing the data splitting functions. ```python from automol.validation import stratified_validation, leave_grp_out_validation, mixed_validation from automol.stacking_util import get_clustering_algorithm model_param['Clustering algorithm used for validation'] = val_clustering c_algo = get_clustering_algorithm(clustering=val_clustering, n_clusters=val_km_groups, cutoff=val_butina_cutoff, include_chirality=val_include_chirality, verbose=verbose, random_state=random_state, feature_generators=stacked_model.feature_generators, used_features=val_used_features_km) ``` -------------------------------- ### RDKit and Bokeh Setup for Molecule Visualization (Python) Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Data_cleaning.ipynb This Python code imports necessary libraries from RDKit and Bokeh for cheminformatics tasks and interactive plotting. It includes functions for preparing molecules, converting them to SVG format for display, and setting up Bokeh for interactive visualizations, specifically for plotting outlier data points with associated chemical structures and information. ```python from bokeh.io import output_notebook output_notebook() from rdkit import Chem from rdkit.Chem import MolFromSmiles, AllChem from rdkit import DataStructs from rdkit.Chem import Draw from rdkit.Chem import PandasTools from rdkit.Chem.Draw import IPythonConsole from rdkit.Chem import rdDepictor from rdkit.Chem.Draw import rdMolDraw2D from rdkit.Chem.PandasTools import ChangeMoleculeRendering from IPython.display import SVG from bokeh.plotting import figure, show, output_notebook, ColumnDataSource from bokeh.models import HoverTool from bokeh.transform import factor_cmap from bokeh.plotting import figure, output_file, save from bokeh.models import Title from bokeh.models import Span from bokeh.models import Legend, LegendItem def _prepareMol(mol, kekulize): mc = Chem.Mol(mol.ToBinary()) if kekulize: try: Chem.Kekulize(mc) except: mc = Chem.Mol(mol.ToBinary()) if not mc.GetNumConformers(): rdDepictor.Compute2DCoords(mc) return mc def moltosvg(mol, molSize=(200, 100), kekulize=True, drawer=None, **kwargs): mc = _prepareMol(mol, kekulize) if drawer is None: drawer = rdMolDraw2D.MolDraw2DSVG(molSize[0], molSize[1]) drawer.DrawMolecule(mc, **kwargs) drawer.FinishDrawing() svg = drawer.GetDrawingText() return SVG(svg.replace('svg:', '')) ``` -------------------------------- ### Initialize Model and Parameter Factory with ModelAndParams Source: https://context7.com/openanalytics/automol/llms.txt The ModelAndParams class initializes pre-configured models and parameter grids for regression or classification tasks. It simplifies model setup by automatically configuring estimators, search strategies, and cross-validation based on task type and computational budget. Dependencies include the 'automol.stacking_util' module. ```python from automol.stacking_util import ModelAndParams # Initialize the model factory for regression param_factory = ModelAndParams( task='Regression', # 'Regression', 'Classification', or 'RegressionClassifier' computional_load='moderate', # 'cheap', 'moderate', or 'expensive' distribution_defaults=False, # Use distributions for randomized search use_gpu=False, normalizer=True, # Normalize input to base estimators top_normalizer=True, # Normalize input to top estimator random_state=[1, 7, 42, 55, 3], # Random states for reproducibility use_sample_weight=False, verbose=1, n_jobs=4 ) # Get the configured model and parameter grids stacked_model, prefixes, params_grid, blender_params, paramsearch = param_factory.get_model_and_params() # stacked_model: FeatureGenerationRegressor or FeatureGenerationClassifier # prefixes: {'method_prefix': 'reg', 'dim_prefix': 'reduce_dim', 'estimator_prefix': 'est_pipe'} # params_grid: List of parameter dictionaries for base estimators # blender_params: Parameters for the top-level estimator # paramsearch: GridSearch, RandomizedSearch, or HyperoptSearch object ``` -------------------------------- ### Instantiate Model Configuration Utility (Python) Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Intermediate_Classifier.ipynb Imports the 'ModelAndParams' class from 'automol.stacking_util' to manage model and parameter configurations. This class is used to set up the model, its parameters, and the parameter search object. ```python from automol.stacking_util import ModelAndParams if model_config=='single_method' and len(clf_list)>1: ``` -------------------------------- ### SklearnClusteringForSmiles Example Implementation (Python) Source: https://github.com/openanalytics/automol/blob/main/README.md An example implementation of a clustering algorithm using scikit-learn estimators. It inherits from `ClusteringAlgorithm` and initializes with feature generators, used features, and an optional scikit-learn estimator. The `cluster` method processes SMILES strings to generate cluster assignments. ```python class SklearnClusteringForSmiles(ClusteringAlgorithm): def __init__(self,*, feature_generators: dict= {},used_features:List[str]=None, random_state:int=42,sklearn_estimator=None): """initialisation of KmeansForSmiles with provided dictionary of feature generators and list of used features. If feature generation dictionary is not provided, default public generators are used If used features is not provided or provided features are not available in the generator dictionary, Bottleneck is used if present in generation dictionary, otherwise al generators in the dictionary are used. Args: feature_generators(dict): dictionary containing different feature generators used_features(list[str]): list of keys indicating the used features """ super(SklearnClusteringForSmiles, self).__init__() if not feature_generators or feature_generators is None: self._feature_generators= retrieve_default_offline_generators(model='ChEMBL', radius=2, nbits=2048) else: assert isinstance(feature_generators,dict), 'provided feature generators must be dictionary' self._feature_generators=feature_generators if used_features is None or len(used_features)<1: used_features=['Bottleneck'] self._used_features=[] for feat in used_features: if feat in self._feature_generators: self._used_features.append(feat) self._random_state=random_state self._estimator=sklearn_estimator def cluster(self,smiles): ằm""generate the groups from a given list of smiles Args: smiles (list[str]): list of smiles (list of strings) Attributes: sz (int): length of smiles list groups (array): an array filled with cluster/group indices for the smiles list generated_features(dict): dictionary with all the generated feature matrices """ smiles=self._check_input_smiles(smiles) self._groups=np.repeat(-1,len(smiles)) X_list=[] for key in self._used_features: ``` -------------------------------- ### Initialize ModelAndParams and Check Configuration in Python Source: https://github.com/openanalytics/automol/blob/main/Tutorials/BlenderFeatures.ipynb This snippet shows the initialization of the ModelAndParams class, which is used to set up the model, parameters, and search objects. It also includes a conditional check to warn the user if multiple base estimators are provided while the model configuration is set to 'single_method'. ```python from automol.stacking_util import ModelAndParams if model_config=='single_method' and len(reg_list)>1: print(f'Warning: more methods are given in reg_list and hierarchy is set to {model_config}, only {reg_list[0]} will be used') if red_dim_list_per_prop is not None: # Further logic related to dimensionality reduction configuration would follow here ``` -------------------------------- ### Integrate MolfeatFPVecTransformer Feature Generator (Python) Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Expert_Classifier.ipynb This snippet shows how to integrate the MolfeatFPVecTransformer from AutoMoL for generating molecular fingerprints. It supports various fingerprint types like 'maccs' and requires the 'molfeat' library to be installed. Error handling is included for missing installations. ```python try: from automol.feature_generators import MolfeatFPVecTransformer #works: desc2D, atompair-count, topological-count, fcfp-count, ecfp-count, estate, erg, secfp, pattern, rdkit, fcfp, ecfp, avalon, maccs feature_generators['maccs']= MolfeatFPVecTransformer(kind='maccs', dtype=float) except: print('molfeat likely not installed') ``` -------------------------------- ### Prepare Plotting Figure Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Data_cleaning.ipynb Configures the y-axis label and legend location for a plotting figure. This is a common setup step before rendering plots. ```python fig.yaxis.axis_label ="Y-coordinate" fig.legend.location = legend_pos return fig ``` -------------------------------- ### Initialize and Configure Method Archives Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Expert_Regressor.ipynb Creates instances of `RegressorArchive` and `ReducedimArchive` using defined prefixes and configuration parameters. These archives manage available methods and their parameters for regression and dimensionality reduction tasks. ```python #create own method archives method_archive=RegressorArchive(method_prefix=prefixes['method_prefix'],distribution_defaults=distribution_defaults,hyperopt_defaults=hyperopt_defaults, random_state=random_state,xgb_threads=xgb_threads,rfr_threads=rfr_threads, method_jobs=n_jobs_dict['method_jobs']) dim_archive=ReducedimArchive(method_prefix=prefixes['dim_prefix'],distribution_defaults=distribution_defaults,hyperopt_defaults=hyperopt_defaults, random_state=random_state) ``` -------------------------------- ### Initialize and Configure Method Archives Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Expert_Classifier.ipynb Creates instances of ClassifierArchive and ReducedimArchive to manage machine learning methods and dimensionality reduction techniques. These archives are configured with naming prefixes, distribution and hyperopt defaults, random states, and thread settings. Methods can be added or customized within these archives. ```python from automol.stacking_methodarchive import ClassifierArchive,ReducedimArchive prefixes={'method_prefix':'clf', 'dim_prefix':'reduce_dim', 'estimator_prefix':'est_pipe'} method_archive=ClassifierArchive(method_prefix=prefixes['method_prefix'],distribution_defaults=distribution_defaults,hyperopt_defaults=hyperopt_defaults, random_state=random_state,xgb_threads=xgb_threads,rfr_threads=rfr_threads, method_jobs=n_jobs_dict['method_jobs']) dim_archive=ReducedimArchive(method_prefix=prefixes['dim_prefix'],distribution_defaults=distribution_defaults,hyperopt_defaults=hyperopt_defaults, random_state=random_state,clf=True) ``` -------------------------------- ### Export Jupyter Notebook to HTML Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Expert_Regressor.ipynb This script defines the notebook filename and target output path, then executes a shell command to convert the notebook to HTML. It relies on the jupyter-nbconvert utility being installed and accessible in the environment. ```python ##################################################### #the name of this notebook, is important for saving the output (without the .ipynb) notebook_name='Expert_Regressor' #html file where the output is save_output_to_file='Cypro_stackingregmodel.html' ##################################################### notebook_file=notebook_name+'.ipynb' notebook_html=notebook_name+'.html' !jupyter-nbconvert --to html $notebook_file !mv $notebook_html $save_output_to_file ``` -------------------------------- ### Configure and Initialize Clustering Algorithms Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Regressor.ipynb Sets up model parameters based on the selected clustering algorithm and initializes the algorithm instance using the automol library. It supports various strategies like Scaffold, Butina, and Bottleneck clustering. ```python model_param['Clustering algorithm used for validation']=val_clustering if val_clustering=="Scaffold" : model_param['Include chiralty?']=val_include_chirality if val_clustering=="Butina" or val_clustering=="HierarchicalButina": model_param['Threshold(s) used for butina when creating Validation set']=val_butina_cutoff if val_clustering=="Bottleneck" : model_param['Number of clusters for Kmeans++']=val_km_groups model_param['Used features for Kmeans++']=val_used_features_km from automol.stacking_util import get_clustering_algorithm c_algo=get_clustering_algorithm(clustering=val_clustering, n_clusters=val_km_groups, cutoff=val_butina_cutoff, include_chirality=val_include_chirality, verbose=verbose, random_state=random_state, feature_generators=stacked_model.feature_generators, used_features=val_used_features_km) ``` -------------------------------- ### Initialize ModelAndParams and Handle Warnings in Python Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Expert_Classifier.ipynb Initializes the `ModelAndParams` class and includes a warning mechanism for cases where multiple base estimators are provided but a single method configuration is selected. ```python ##################################################### from automol.stacking_util import ModelAndParams if model_config=='single_method' and len(clf_list)>1: print(f'Warning: more methods are given in clf_list and hierarchy is set to {model_config}, only {clf_list[0]} will be used') if red_dim_list_per_prop is not None: ``` -------------------------------- ### Get Clustering Algorithm Instance Source: https://github.com/openanalytics/automol/blob/main/Tutorials/BlenderFeatures.ipynb Retrieves a clustering algorithm instance from automol.stacking_util. This function takes various parameters such as clustering method, number of clusters, cutoff, and feature generators to create a configurable clustering object. ```python from automol.validation import stratified_validation, leave_grp_out_validation, mixed_validation from automol.stacking_util import get_clustering_algorithm c_algo=get_clustering_algorithm(clustering=val_clustering, n_clusters=val_km_groups, cutoff=val_butina_cutoff, include_chirality=val_include_chirality, verbose=verbose, random_state=random_state, feature_generators=stacked_model.feature_generators, used_features=val_used_features_km) ``` -------------------------------- ### Fit Statistical Distributions using Fitter Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Data_cleaning.ipynb Installs and utilizes the fitter library to identify the best-fitting probability distributions for a given dataset. It supports common scipy distributions and provides a summary of the best fits. ```python !pip install --user fitter from fitter import Fitter, get_common_distributions, get_distributions def fit_distributions(df,property_to_be_plotted,distributions=None): mask=np.isnan(df[property_to_be_plotted]) fitting_values = df[property_to_be_plotted][~mask].values if distributions is None: distributions=['cauchy', 'chi2', 'expon', 'exponpow', 'gamma', 'lognorm', 'norm', 'powerlaw', 'rayleigh', 'uniform'] f = Fitter(fitting_values,distributions=distributions) f.fit() return f f=fit_distributions(df,property_to_be_plotted) f.summary(Nbest=3) ``` -------------------------------- ### Configure Property Classes with ClassBuilder Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Expert_Classifier.ipynb Demonstrates how to initialize and use the ClassBuilder class to bin continuous or categorical data into classes. It handles data cleaning, quantile generation, and the creation of training properties for downstream model consumption. ```python categorical=False properties=['Y'] nb_classes=[2] class_values= [[75]] class_quantiles=None min_allowed_class_samples=30 df.dropna(inplace=True,how='all', subset = properties) df.reset_index(drop=True,inplace=True) from automol.property_prep import ClassBuilder prop_builder=ClassBuilder(properties=properties,nb_classes=nb_classes,class_values=class_values, categorical=categorical,use_quantiles=class_quantiles is not None, prefix='Class',min_allowed_class_samples=30,verbose=verbose) prop_builder.check_properties(df) df,train_properties=prop_builder.generate_train_properties(df) ``` -------------------------------- ### Save Notebook and Output as HTML (Python) Source: https://github.com/openanalytics/automol/blob/main/Tutorials/BlenderFeatures.ipynb This snippet configures variables for notebook and output file names and then uses shell commands to convert the notebook to HTML and move it to the specified output file. It requires the 'jupyter' command-line tool to be installed and accessible. ```python ##################################################### #the name of this notebook, is important for saving the output (without the .ipynb) notebook_name='Intermediate_Regressor' #html file where the output is save_output_to_file=f'{name}_stackingregmodel.html' ##################################################### notebook_file=notebook_name+'.ipynb' notebook_html=notebook_name+'.html' !jupyter-nbconvert --to html $notebook_file !mv $notebook_html $save_output_to_file ``` -------------------------------- ### Configure Model Parameters and Initialize Model Factory Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Expert_RegressorClassifier.ipynb This snippet demonstrates how to configure various model parameters, including dimensionality reduction, regularization lists, and search strategies. It then initializes a ModelAndParams factory object with these parameters to generate a stacked model and associated configurations. ```python if model_config=='single_method' and len(reg_list)>1: print(f'Warning: more methods are given in reg_list and hierarchy is set to {model_config}, only {reg_list[0]} will be used') if red_dim_list_per_prop is not None: assert len(red_dim_list_per_prop)==len(properties), 'provided list of dim reduction methods per peroprty must equal length of list of properties' model_param['red_dim_list']=red_dim_list_per_prop else: model_param['red_dim_list']=red_dim_list model_param['local_dim_red']=local_dim_red model_param['reg_list']=reg_list model_param['blender_list']=blender_list model_param['force_gridsearch']=force_gridsearch model_param['randomized_iterations']=randomized_iterations model_param['distribution_defaults']=distribution_defaults model_param['hyperopt_defaults']=hyperopt_defaults model_param['used_features']=used_features param_Factory=ModelAndParams(task=task, distribution_defaults=distribution_defaults, #use distributions for parameter selection in randomized search hyperopt_defaults=hyperopt_defaults, #use hyperopt use_gpu=False, normalizer=True, #normalize input base estimators top_normalizer=False,#normalize output base estimators and thus input top estimator random_state=random_state_list,#random_state red_dim_list=red_dim_list,#list of dimensionality reducing methods keys from dim_archive method_list=reg_list,#list of classifier keys from method_archive blender_list=blender_list,#list of classifier keys from blender_archive model_config=model_config, #string with the model layout/config randomized_iterations=randomized_iterations, #number of randomized iterations n_jobs_dict=n_jobs_dict, use_sample_weight=use_sample_weight, feature_generators=feature_generators, local_dim_red=local_dim_red, dim_red_n_components=dim_red_n_components, method_archive=method_archive, #archive class holding the methods dim_archive=dim_archive, #archive class holding the dimensionality reducing methods blender_archive=blender_archive, #archive class holding the blender methods prefixes=prefixes #naming convention in parameters for grid/randomized search ,hyperopt_threads=hyperopt_threads, #overwrites n_jobs_dict['inner_jobs'] in the case of hyperopt_defaults labelnames=labelnames, verbose=verbose) stacked_model, prefixes,params_grid,blender_params,paramsearch = param_Factory.get_model_and_params() ``` -------------------------------- ### Setup Cross-Validation Parameters Source: https://github.com/openanalytics/automol/blob/main/Tutorials/MultiTarget_Classifier.ipynb Defines the cross-validation strategy, including the number of folds and the splitting method (LGO, GKF, or SKF). It includes logic to validate fold counts against cluster groups to ensure model stability. ```python cross_val_split='GKF' outer_folds=4 if outer_folds > km_groups: if km_groups > 2: outer_folds = km_groups - 1 else: km_groups = outer_folds + 1 results_dictionary = {} ``` -------------------------------- ### Configure and Extend Method Archives in AutoMol Source: https://context7.com/openanalytics/automol/llms.txt Demonstrates how to initialize Regressor, Classifier, and Reducedim archives. It shows how to add custom parameter ranges to existing methods and register new scikit-learn estimators with custom hyperparameter grids. ```python from automol.stacking_methodarchive import RegressorArchive, ClassifierArchive, ReducedimArchive from sklearn.ensemble import RandomForestRegressor from sklearn.neural_network import MLPRegressor # Create method archive with defaults random_state_list = [1, 7, 42, 55, 3] method_archive = RegressorArchive( method_prefix='reg', distribution_defaults=False, hyperopt_defaults=False, random_state=42 ) # Add or modify parameter ranges for existing methods method_archive.add_param('rfr', 'n_estimators', [50, 100, 200, 500]) # Add a completely new method method_archive.add_method( key='my_rf', method=RandomForestRegressor(n_jobs=2), param_dict={ 'n_estimators': [100, 200, 500], 'max_depth': [5, 10, 15, None], 'min_samples_split': [2, 5, 10], 'random_state': random_state_list } ) # Get parameter grid for selected methods method_list = ['lasso', 'svr', 'my_rf'] params_grid = method_archive.get_params(method_list) # For classification clf_archive = ClassifierArchive( method_prefix='clf', distribution_defaults=False, hyperopt_defaults=False, random_state=42 ) # For dimensionality reduction dim_archive = ReducedimArchive( dim_prefix='reduce_dim', distribution_defaults=False, hyperopt_defaults=False, random_state=42 ) ``` -------------------------------- ### Ignore Python Warnings and Print AutoMoL Version Source: https://github.com/openanalytics/automol/blob/main/Tutorials/3DRegressor.ipynb This snippet demonstrates how to ignore convergence warnings and other Python warnings, and then prints the installed version of the AutoMoL library. It's useful for cleaning up console output during development or deployment. ```python import warnings import sys, os if not sys.warnoptions: warnings.simplefilter("ignore") os.environ["PYTHONWARNINGS"] = "ignore" # Also affect subprocesses from automol.version import __version__ as AutoMLv print(AutoMLv) ``` -------------------------------- ### Configure and Execute Cross-Validation Model Search in Automol Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Lipophilicity_AstraZeneca.ipynb Sets up cross-validation parameters, including the split strategy (LGO or GKF), number of outer folds, and features to be used. It then iterates through training properties to search for optimal models using the specified cross-validation setup. ```python %%time ##################################################### #cross-validation parameters #select cross-validation split: LGO or GKF cross_val_split='GKF' outer_folds=4 #select the features used when training the model used_features=['Bottleneck'] #used_features=['rdkit','fps_1024_2'] #used_features=['rdkit','fps_1024_2', 'Bottleneck'] ##################################################### model_param['Used cross-validation technique']=cross_val_split model_param['used features']=used_features if cross_val_split != 'LGO': model_param['Number of folds']=outer_folds #use GKF for regression instead of stratified group kfold if cross_val_split=='SKF': cross_val_split='GKF' if outer_folds>km_groups: if km_groups>2: outer_folds=km_groups-1 else: km_groups=outer_folds+1 results_dictionary={} #empty to put this one first (keep in mind order in dictionary is not reliable) for p in train_properties: results_dictionary[p]={} sample_weight=None if use_sample_weight: sample_weight=stacked_model.Train[f'sw_{p}'].values stacked_model.search_model(df= None, prop=p, smiles=standard_smiles_column, params_grid=params_grid, paramsearch=paramsearch, scoring=scorer, cv=outer_folds-1, outer_cv_fold=outer_folds, split=cross_val_split, use_memory=True, plot_validation=True, blender_params=blender_params ,prefix_dict=prefixes,random_state=random_state ,sample_weight=sample_weight ,results_dict=results_dictionary, features=used_features) model_str=stacked_model.print_metrics() ``` -------------------------------- ### Configure Model Parameters and Options Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Expert_Classifier.ipynb Sets up various parameters for model training and optimization, including task, scoring function, random states, and configuration for model complexity (e.g., single_method, inner_stacking). It also defines options for grid search, randomized iterations, and the use of distribution defaults or HyperOpt for parameter selection. ```python random_state_list=[1,7,42,55,3] n_jobs=10 model_param['Task']=task model_param['Score function']=scorer model_param['Random state']=random_state model_param['Random state list']=random_state_list model_config='single_method' if model_config=='inner_stacking' or model_config=='single_stack' or model_config=='top_stacking': use_sample_weight=False force_gridsearch=False randomized_iterations=30 distribution_defaults=False hyperopt_defaults=False ``` -------------------------------- ### Save Notebook and Output to HTML (Python) Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Intermediate_RegressorClassifier.ipynb This Python code snippet configures the notebook name and the output HTML file path. It then uses shell commands to convert the current notebook to HTML and move it to the specified output file. Ensure that `jupyter` is installed and accessible in your environment. ```python ##################################################### #the name of this notebook, is important for saving the output (without the .ipynb) notebook_name='Intermediate_RegressorClassifier' #html file where the output is save_output_to_file=f'{name}_intermregclfmodel.html' ##################################################### notebook_file=notebook_name+'.ipynb' notebook_html=notebook_name+'.html' !jupyter-nbconvert --to html $notebook_file !mv $notebook_html $save_output_to_file ``` -------------------------------- ### Load and Standardize Drug Discovery Data Source: https://github.com/openanalytics/automol/blob/main/Tutorials/RegressionClassifier.ipynb Demonstrates how to load datasets using PyTDC, standardize SMILES strings using RDKit, and prepare dataframes for machine learning tasks. ```python ##################################################### #name of the dataset name='Bioavailability_Ma' #set the column in the data file where the smiles can be found smiles_column='Drug' #verbosity verbose=1 # set to true if you want to add rdkit descriptors to the feature set check_rdkit_desc=False #name of the new column to add the standardized smiles in the dataframe (choice is free) standard_smiles_column='stand_SMILES' ##################################################### from tdc.single_pred import ADME import numpy as np, pandas as pd from matplotlib import pyplot as plt from automol.property_prep import add_stereo_smiles,validate_rdkit_smiles, add_rdkit_standardized_smiles data = ADME(name = name) split = data.get_split() df=split['train'] test_df=split['test'] model_param={} model_param['Data file']=name add_rdkit_standardized_smiles(df, smiles_column,verbose=verbose,outname=standard_smiles_column) add_rdkit_standardized_smiles(test_df, smiles_column,verbose=verbose,outname=standard_smiles_column) model_param['Standardization']='rdkit' if check_rdkit_desc: validate_rdkit_smiles(df, standard_smiles_column,verbose=verbose) df.dropna(inplace=True, subset = [standard_smiles_column]) ``` -------------------------------- ### Generate Molecular Features with MolfeatMoleculeTransformer Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Molfeat_testing.ipynb This code example shows how to generate molecular features using the MolfeatMoleculeTransformer, which supports various featurizers like Mordred descriptors and pharmacophores. The `featurizer` argument can be a string name or a pharmacophore object. The output is a NumPy array of molecular features. ```python from automol.feature_generators import MolfeatMoleculeTransformer from molfeat.calc.pharmacophore import Pharmacophore2D feature_generator = MolfeatMoleculeTransformer(featurizer='cats2d', dtype=float) X = feature_generator.generate(['[Cu]', 'C[C@H](N)C(=O)O', 'CC(O)C(=O)O', 'OCC(O)CO', 'Oc1ccccc1', 'Nc1ccncc1', None, 'C#CC(C)(O)CC', '', 'ClCCCl', 'O=C1CCC(=O)N1', 'O=CCCCC=O', 'N[C@@H]1CONC1=O', 'S1C=CSC1=C']) X.shape ``` -------------------------------- ### Configure Model Training Parameters (Python) Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Intermediate_Classifier.ipynb Sets up essential parameters for training machine learning models, including the task type (classification or regression), computational load, optimization scorer, random states, number of jobs for parallel processing, search type for parameter optimization, number of iterations for randomized search, and the model configuration. ```python ##################################################### #Classification or Regression task='Classification' #Estimate of the computational workload, more expensive will take more time, but is not gauranteed to provide better results, although more likely #choose between cheap, moderate or expensive computional_load='cheap' #The metric that is optimized when searching the method parameters, examples: #set scorer : accuracy, balanced_accuracy, f1_weighted, precision, neg_log_loss, roc_auc_ovr_weighted, recall scorer='balanced_accuracy' #random state for internal randomness, e.g. randomizedsearch, hyperopt, ... random_state=5 #random state values for methods, available as a parameter random_state_list=[1,7,42,55,3] #number of jobs used when searching the optimal method parameters (-1 for all) n_jobs_dict={'outer_jobs':1, #number of jobs/processes for outer cross-validation 'inner_jobs':-1, #number of jobs/processes for inner cross-validation. -1 means all 'method_jobs':2} #number of jobs/processes available for the methods. Note that sgd and mlp use blas/mkl and are not affected by this value. (try setting environment variables) #search type of parameter search: hyperopt, randomized or grid search_type='randomized' #number of iterations used when search_type is not grid randomized_iterations=60 #inner_methods, inner_stacking, single_stack, top_method, top_stacking, single_method model_config='top_method' ``` -------------------------------- ### Load and Standardize Chemical Datasets Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Lipophilicity_AstraZeneca.ipynb Downloads datasets using PyTDC, standardizes SMILES strings using RDKit, and prepares training/test dataframes for machine learning tasks. ```python ##################################################### #name of the dataset name='Lipophilicity_AstraZeneca' #set the column in the data file where the smiles can be found smiles_column='Drug' #verbosity verbose=1 # set to true if you want to add rdkit descriptors to the feature set check_rdkit_desc=False #name of the new column to add the standardized smiles in the dataframe (choice is free) standard_smiles_column='stand_SMILES' ##################################################### from tdc.single_pred import ADME import numpy as np, pandas as pd from automol.property_prep import add_stereo_smiles,validate_rdkit_smiles, add_rdkit_standardized_smiles from tdc.single_pred import Tox if name=='LD50_Zhu': data = Tox(name = 'LD50_Zhu') else: data = ADME(name = name) split = data.get_split() df=split['train'] test_df=split['test'] add_rdkit_standardized_smiles(df, smiles_column,verbose=verbose,outname=standard_smiles_column) add_rdkit_standardized_smiles(test_df, smiles_column,verbose=verbose,outname=standard_smiles_column) if check_rdkit_desc: validate_rdkit_smiles(df, standard_smiles_column,verbose=verbose) df.dropna(inplace=True, subset = [standard_smiles_column]) ``` -------------------------------- ### Save Notebook and Output to HTML (Python) Source: https://github.com/openanalytics/automol/blob/main/Tutorials/MultiTarget_Regressor.ipynb This Python code snippet configures the notebook name and the desired HTML output file path. It then uses shell commands to convert the current notebook to HTML and move it to the specified location. Ensure `jupyter` is installed and accessible in your environment. ```python ##################################################### #the name of this notebook, is important for saving the output (without the .ipynb) notebook_name='MultiTarget_Regressor' #html file where the output is save_output_to_file=F'{name}_stackingregmodel.html' ##################################################### notebook_file=notebook_name+'.ipynb' notebook_html=notebook_name+'.html' !jupyter-nbconvert --to html $notebook_file !mv $notebook_html $save_output_to_file ``` -------------------------------- ### Cluster Data for Nested Cross-Validation with Scikit-learn Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Expert_Classifier.ipynb Clusters the data for nested cross-validation using a specified clustering algorithm. This example demonstrates using Scikit-learn's BisectingKMeans for clustering, with parameters like the number of clusters and random state. The custom `my_own_clustering` function is then used to integrate this Scikit-learn estimator into the automol framework for data clustering. ```python #property check cv_clustering='Bottleneck' km_groups=20 for p in properties: prop_count=df[p].count() if cv_clustering=='Bottleneck' and prop_count/km_groups<10: print('Warning: on average less than 10 samples per cluster') #clustering the data #https://scikit-learn.org/stable/modules/generated/sklearn.cluster.BisectingKMeans.html#sklearn.cluster.BisectingKMeans from sklearn.cluster import BisectingKMeans sklearn_clustering = BisectingKMeans(n_clusters=20, random_state=0) train_clustering_algo=my_own_clustering(feature_generators=feature_generators, used_features=['Bottleneck'], random_state=42, sklearn_estimator=sklearn_clustering) stacked_model.Data_clustering(random_state=random_state,clustering_algorithm=train_clustering_algo) ``` -------------------------------- ### Configure Model Training Parameters in Python Source: https://github.com/openanalytics/automol/blob/main/Tutorials/RegressionClassifier.ipynb Sets up configuration variables for machine learning model training, including task type, computational load, and scoring metrics. These variables are then used to initialize the ModelAndParams class. ```python ##################################################### #Classification, RegressionClassification or Regression task='RegressionClassification' #Estimate of the computational workload, more expensive will take more time, but is not gauranteed to provide better results, although more likely #choose between cheap, moderate or expensive computional_load='cheap' #The metric that is optimized when searching the method parameters, examples: #r2,explained_variance,neg_mean_gamma_deviance, neg_mean_poisson_deviance, 'neg_mean_squared_error', #'neg_mean_squared_log_error','neg_median_absolute_error','neg_root_mean_squared_error', scorer='r2' #random state for internal randomness, e.g. randomizedsearch, hyperopt, ... random_state=5 #random state values for methods, available as a parameter random_state_list=[1,7,42,55,3] #number of jobs used when searching the optimal method parameters (-1 for all) n_jobs=20 ##################################################### from automol.stacking_util import ModelAndParams model_param['Computational power']=computional_load model_param['Task']=task model_param['Score function']=scorer model_param['Random state']=random_state model_param['Random state list']=random_state_list #sample_weight not available for expensive option if computional_load=='expensive': use_sample_weight=False param_Factory=ModelAndParams(task=task, computional_load=computional_load, distribution_defaults=False, #use distributions for parameter selection in randomized search use_gpu=False, normalizer=True, #normalize input base estimators top_normalizer=True,#normalize output base estimators and thus input top estimator random_state=random_state_list,#random_state use_sample_weight=use_sample_weight, verbose=verbose, n_jobs=n_jobs, labelnames=labelnames) stacked_model, prefixes,params_grid,blender_params,paramsearch = param_Factory.get_model_and_params() ``` -------------------------------- ### Load and Standardize Chemical Datasets Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Intermediate_Classifier.ipynb Demonstrates how to load a dataset from PyTDC, standardize SMILES strings using RDKit, and prepare training/test dataframes for the AutoMoL pipeline. ```python ##################################################### #name of the dataset name='HydrationFreeEnergy_FreeSolv' #set the column in the data file where the smiles can be found smiles_column='Drug' #verbosity verbose=1 # set to true if you want to add rdkit descriptors to the feature set check_rdkit_desc=False #name of the new column to add the standardized smiles in the dataframe (choice is free) standard_smiles_column='stand_SMILES' ##################################################### from tdc.single_pred import ADME import numpy as np, pandas as pd from matplotlib import pyplot as plt from automol.property_prep import add_stereo_smiles,validate_rdkit_smiles, add_rdkit_standardized_smiles data = ADME(name = name) split = data.get_split() df=split['train'] test_df=split['test'] model_param={} model_param['Data file']=name add_rdkit_standardized_smiles(df, smiles_column,verbose=verbose,outname=standard_smiles_column) add_rdkit_standardized_smiles(test_df, smiles_column,verbose=verbose,outname=standard_smiles_column) model_param['Standardization']='rdkit' if check_rdkit_desc: validate_rdkit_smiles(df, standard_smiles_column,verbose=verbose) df.dropna(inplace=True, subset = [standard_smiles_column]) df.head(5) ``` -------------------------------- ### Validate Model Predictions and Show Regression Report (Python) Source: https://github.com/openanalytics/automol/blob/main/Tutorials/RelativeRegressor.ipynb This snippet validates model predictions against true values and displays a regression report. It utilizes the ResultDesigner class from automol.stacking_util. The function get_y_true is defined to prepare the true values, and the predict method is called to get model outputs. The show_regression_report method then visualizes the results. ```python %matplotlib inline ##################################################### #revert to original properties? revert_to_original=False positive_cls="<" ##################################################### from automol.stacking_util import ResultDesigner smiles_list=stacked_model.Validation[standard_smiles_column] original_indices,indices=stacked_model.data_form.get_pairs(stacked_model.Validation,'pairs') def get_y_true(df,properties,indices): if indices is not None: y_true_l=[] for p in properties: y=df[p].values y_p=np.zeros(len(indices)) for idx,(i,j) in enumerate(indices): if len(y_p.shape)>1: y_p[idx,:]=stacked_model.data_form.apply_property_operation(y[i,:],y[j,:]) else: y_p[idx]=stacked_model.data_form.apply_property_operation(y[i],y[j]) y_true_l.append(y_p) else: y_true_l=[df[f'{p}'].values for p in properties] return y_true_l out=stacked_model.predict( props =None, smiles=smiles_list,compute_SD=True,convert_log10=transformed_data and revert_to_original,original_indices=original_indices, indices=indices) out={ key:item.astype(dtype=float) for key,item in out.items()} if transformed_data and revert_to_original: if prop_cliff_dict is not None: original_prop_cliff_dict=prop_cliff_dict.copy() prop_cliff_dict={'_'.join(key.split('_')[1:]):val for key,val in prop_cliff_dict.items()} properties=original_props else: properties=train_properties y_true_l=get_y_true(stacked_model.Validation, properties,indices) ResultDesigner().show_regression_report(properties,out,y_true=y_true_l,prop_cliffs=None, leave_grp_out=None,positive_cls=positive_cls) ``` ```python %matplotlib inline from automol.stacking_util import ResultDesigner smiles_list=test_df[standard_smiles_column] original_indices,indices=stacked_model.data_form.get_pairs(test_df,'pairs') out=stacked_model.predict( props =None, smiles=smiles_list,compute_SD=True,convert_log10=transformed_data and revert_to_original,original_indices=original_indices, indices=indices) out={ key:item.astype(dtype=float) for key,item in out.items()} if transformed_data and revert_to_original: if prop_cliff_dict is not None: original_prop_cliff_dict=prop_cliff_dict.copy() prop_cliff_dict={'_'.join(key.split('_')[1:]):val for key,val in prop_cliff_dict.items()} properties=original_props else: properties=train_properties y_true_l=get_y_true(test_df, properties,indices) ResultDesigner().show_regression_report(properties,out,y_true=y_true_l,prop_cliffs=None, leave_grp_out=None,positive_cls=positive_cls) ``` -------------------------------- ### Print Available Method Keys Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Expert_Regressor.ipynb Iterates through the `method_archive` and `dim_archive` to print the keys and corresponding estimator names for all available methods and dimensionality reduction techniques. This helps in understanding the registered options. ```python # print available method keys print('***************************************** key: Estimator *****************************************') for key,method in method_archive.get_all_method_keys_plus_estimators(): print(f'\33[1m{key}\33[0m: {method}') print('***************************************** Key: Dimensionality reduction method *****************************************') for key,method in dim_archive.get_all_method_keys_plus_estimators(): print(f'\33[1m{key}\33[0m: {method}') ``` -------------------------------- ### Configure and Initialize Model Archives in Automol Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Expert_RegressorClassifier.ipynb This snippet demonstrates how to define configuration parameters for stacking, initialize Regressor and Reducedim archives, and register custom models into the archive. It also shows how to manage parallel execution threads and handle method duplication for ensemble diversity. ```python from automol.stacking_util import add_lgbm_xtimes_hyperopt, add_xgb_xtimes_hyperopt from automol.stacking_methodarchive import RegressorArchive, ReducedimArchive # Configure job parallelization n_jobs_dict = {'outer_jobs': None, 'inner_jobs': -1, 'method_jobs': 2} # Initialize archives method_archive = RegressorArchive(method_prefix='reg', distribution_defaults=False, hyperopt_defaults=False, method_jobs=n_jobs_dict['method_jobs']) dim_archive = ReducedimArchive(method_prefix='reduce_dim', distribution_defaults=False, hyperopt_defaults=False) # Register a custom MLP regressor mlp_param_dict = {'random_state': [1, 7, 42, 55, 3]} method_archive.add_method('my_mlp', myown_mlpregressor(), mlp_param_dict) # Duplicate a method for ensemble diversity reg_list = method_archive.duplicate_method_xtimes(method_name='lgbm', x=5, random_state=42) # Display registered methods for key, method in method_archive.get_all_method_keys_plus_estimators(): print(f'{key}: {method}') ``` -------------------------------- ### Initialize ModelAndParams for Automated Machine Learning Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Classifier.ipynb This snippet demonstrates how to configure the ModelAndParams factory to generate a stacked machine learning model. It sets the task type, computational intensity, and optimization parameters before retrieving the model and parameter grids. ```python from automol.stacking_util import ModelAndParams task = 'Classification' computional_load = 'cheap' scorer = 'balanced_accuracy' random_state = 5 random_state_list = [1, 7, 42, 55, 3] n_jobs = 20 model_param = { 'Computational power': computional_load, 'Task': task, 'Score function': scorer, 'Random state': random_state, 'Random state list': random_state_list } use_sample_weight = True if computional_load == 'expensive': use_sample_weight = False param_Factory = ModelAndParams( task=task, computional_load=computional_load, distribution_defaults=False, use_gpu=False, normalizer=True, top_normalizer=True, random_state=random_state_list, use_sample_weight=use_sample_weight, verbose=True, n_jobs_dict={'outer_jobs': 4, 'inner_jobs': 4, 'method_jobs': 4}, labelnames=['target'] ) stacked_model, prefixes, params_grid, blender_params, paramsearch = param_Factory.get_model_and_params() ``` -------------------------------- ### Load and Standardize Drug Design Datasets Source: https://github.com/openanalytics/automol/blob/main/Tutorials/Expert_Classifier.ipynb Demonstrates how to ingest datasets from PyTDC, standardize SMILES strings using RDKit, and prepare training and testing dataframes for the AutoMoL pipeline. ```python ##################################################### #name of the dataset name='PPBR_AZ' #set the column in the data file where the smiles can be found smiles_column='Drug' #verbosity verbose=1 # set to true if you want to add rdkit descriptors to the feature set check_rdkit_desc=False #name of the new column to add the standardized smiles in the dataframe (choice is free) standard_smiles_column='stand_SMILES' ##################################################### from tdc.single_pred import ADME import numpy as np, pandas as pd from matplotlib import pyplot as plt from automol.property_prep import add_stereo_smiles,validate_rdkit_smiles, add_rdkit_standardized_smiles data = ADME(name = name) split = data.get_split() df=split['train'] test_df=split['test'] model_param={} model_param['Data file']=name add_rdkit_standardized_smiles(df, smiles_column,verbose=verbose,outname=standard_smiles_column) add_rdkit_standardized_smiles(test_df, smiles_column,verbose=verbose,outname=standard_smiles_column) model_param['Standardization']='rdkit' if check_rdkit_desc: validate_rdkit_smiles(df, standard_smiles_column,verbose=verbose) df.dropna(inplace=True, subset = [standard_smiles_column]) df.head(5) ``` -------------------------------- ### Configure Clustering Algorithms for Nested Cross-Validation Source: https://github.com/openanalytics/automol/blob/main/Tutorials/RegressionClassifier.ipynb Initializes clustering parameters such as chirality, K-means groups, and Butina cutoffs. It dynamically builds a model parameter dictionary and instantiates the clustering algorithm object used for data partitioning. ```python include_chirality=False km_groups=20 used_features_km=['Bottleneck'] if cv_clustering=="Butina": butina_cutoff=0.6 else: butina_cutoff=[0.2,0.4,0.6] cv_c_algo=get_clustering_algorithm(clustering=cv_clustering, n_clusters=km_groups, cutoff=butina_cutoff, include_chirality=include_chirality, verbose=verbose, random_state=random_state, feature_generators=stacked_model.feature_generators, used_features=used_features_km) stacked_model.Data_clustering(random_state=random_state, clustering_algorithm=cv_c_algo) ```