Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lordmikerahl committed Mar 27, 2024
1 parent eb0306c commit 5aed9b0
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 630 deletions.
110 changes: 82 additions & 28 deletions baybe_hack.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -22,7 +22,7 @@
"from baybe.parameters import NumericalDiscreteParameter, NumericalContinuousParameter\n",
"from baybe.searchspace import SearchSpace\n",
"\n",
"from baybe.recommenders import RandomRecommender, SequentialGreedyRecommender, NaiveHybridRecommender\n",
"from baybe.recommenders import RandomRecommender, SequentialGreedyRecommender\n",
"from baybe.surrogates import GaussianProcessSurrogate\n",
"\n",
"from baybe.strategies import TwoPhaseStrategy\n",
Expand Down Expand Up @@ -55,9 +55,20 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'\\noverpotential = NumericalTarget(\\n name=\"overpotential\", \\n mode=\"MAX\", \\n bounds=(-400, 0),\\n transformation=\"LINEAR\" # optional, will be applied if bounds are not None, LINEAR only one available for MAX/MIN\\n ) \\n\\noverpotential_slope = NumericalTarget(\\n name=\"overpotential_slope\", \\n mode=\"MAX\", \\n bounds=(-0.05, 0.05),\\n transformation=\"LINEAR\" # optional, will be applied if bounds are not None, LINEAR only one available for MAX/MIN\\n )\\n\\nobjective = Objective(\\n mode=\"DESIRABILITY\",\\n targets=[overpotential, overpotential_slope],\\n weights=[1.0, 1.0], # optional, by default all weights are equal\\n combine_func=\"GEOM_MEAN\", # optional, geometric mean is the default\\n)\\n'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\"\"\n",
"overpotential = NumericalTarget(\n",
Expand Down Expand Up @@ -92,7 +103,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -129,9 +140,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "OptionalImportError",
"evalue": "The requested functionality requires the installation of optional chemistry dependencies. Please run \"pip install 'baybe[chem]'\".",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"File \u001b[0;32m~/.local/lib/python3.10/site-packages/baybe/utils/chemistry.py:17\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 17\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmordred\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Calculator, descriptors\n\u001b[1;32m 18\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mrdkit\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Chem, RDLogger\n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'mordred'",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[0;31mOptionalImportError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[5], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mbaybe\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mparameters\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m SubstanceParameter\n\u001b[0;32m----> 3\u001b[0m \u001b[43mSubstanceParameter\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 4\u001b[0m \u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mSolvent\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 5\u001b[0m \u001b[43m \u001b[49m\u001b[43mdata\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m{\u001b[49m\n\u001b[1;32m 6\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mWater\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mO\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 7\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m1-Octanol\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mCCCCCCCCO\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 8\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mToluene\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mCC1=CC=CC=C1\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 9\u001b[0m \u001b[43m \u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 10\u001b[0m \u001b[43m \u001b[49m\u001b[43mencoding\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mMORDRED\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;66;43;03m# optional\u001b[39;49;00m\n\u001b[1;32m 11\u001b[0m \u001b[43m \u001b[49m\u001b[43mdecorrelate\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m0.7\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;66;43;03m# optional\u001b[39;49;00m\n\u001b[1;32m 12\u001b[0m \u001b[43m)\u001b[49m\n",
"File \u001b[0;32m<attrs generated init baybe.parameters.substance.SubstanceParameter>:10\u001b[0m, in \u001b[0;36m__init__\u001b[0;34m(self, name, data, decorrelate, encoding)\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m _config\u001b[38;5;241m.\u001b[39m_run_validators \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m 9\u001b[0m __attr_validator_name(\u001b[38;5;28mself\u001b[39m, __attr_name, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mname)\n\u001b[0;32m---> 10\u001b[0m \u001b[43m__attr_validator_data\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m__attr_data\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdata\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 11\u001b[0m __attr_validator_decorrelate(\u001b[38;5;28mself\u001b[39m, __attr_decorrelate, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdecorrelate)\n",
"File \u001b[0;32m~/.local/lib/python3.10/site-packages/attr/_make.py:3059\u001b[0m, in \u001b[0;36m_AndValidator.__call__\u001b[0;34m(self, inst, attr, value)\u001b[0m\n\u001b[1;32m 3057\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, inst, attr, value):\n\u001b[1;32m 3058\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m v \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validators:\n\u001b[0;32m-> 3059\u001b[0m \u001b[43mv\u001b[49m\u001b[43m(\u001b[49m\u001b[43minst\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mattr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/.local/lib/python3.10/site-packages/baybe/parameters/substance.py:77\u001b[0m, in \u001b[0;36mSubstanceParameter._validate_substance_data\u001b[0;34m(self, _, data)\u001b[0m\n\u001b[1;32m 67\u001b[0m \u001b[38;5;129m@data\u001b[39m\u001b[38;5;241m.\u001b[39mvalidator\n\u001b[1;32m 68\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_validate_substance_data\u001b[39m( \u001b[38;5;66;03m# noqa: DOC101, DOC103\u001b[39;00m\n\u001b[1;32m 69\u001b[0m \u001b[38;5;28mself\u001b[39m, _: Any, data: \u001b[38;5;28mdict\u001b[39m[\u001b[38;5;28mstr\u001b[39m, Smiles]\n\u001b[1;32m 70\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 71\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Validate that the substance data, provided as SMILES, is valid.\u001b[39;00m\n\u001b[1;32m 72\u001b[0m \n\u001b[1;32m 73\u001b[0m \u001b[38;5;124;03m Raises:\u001b[39;00m\n\u001b[1;32m 74\u001b[0m \u001b[38;5;124;03m ValueError: If one or more of the SMILES are invalid.\u001b[39;00m\n\u001b[1;32m 75\u001b[0m \u001b[38;5;124;03m ValueError: If the several entries represent the same substance.\u001b[39;00m\n\u001b[1;32m 76\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m---> 77\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mbaybe\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m chemistry\n\u001b[1;32m 79\u001b[0m \u001b[38;5;66;03m# Check for invalid SMILES\u001b[39;00m\n\u001b[1;32m 80\u001b[0m canonical_smiles \u001b[38;5;241m=\u001b[39m {}\n",
"File \u001b[0;32m~/.local/lib/python3.10/site-packages/baybe/utils/chemistry.py:21\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mrdkit\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mChem\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mrdMolDescriptors\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m GetMorganFingerprintAsBitVect\n\u001b[1;32m 20\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m:\n\u001b[0;32m---> 21\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m OptionalImportError(\n\u001b[1;32m 22\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"The requested functionality requires the installation of optional \"\"\"\u001b[39;00m\n\u001b[1;32m 23\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"chemistry dependencies. Please run \"pip install 'baybe[chem]'\".\"\"\"\u001b[39;00m\n\u001b[1;32m 24\u001b[0m )\n\u001b[1;32m 26\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mbaybe\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutils\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mnumerical\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m DTypeFloatNumpy\n\u001b[1;32m 28\u001b[0m _mordred_calculator \u001b[38;5;241m=\u001b[39m Calculator(descriptors)\n",
"\u001b[0;31mOptionalImportError\u001b[0m: The requested functionality requires the installation of optional chemistry dependencies. Please run \"pip install 'baybe[chem]'\"."
]
}
],
"source": [
"from baybe.parameters import SubstanceParameter\n",
"\n",
Expand Down Expand Up @@ -177,9 +208,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"CustomDiscreteParameter(name='Polymer', data= Glass_Transition_TempC Weight_kDalton\n",
"Polymer A 20 120\n",
"Polymer B -71 32\n",
"Polymer C -39 241, decorrelate=True, encoding=<CustomEncoding.CUSTOM: 'CUSTOM'>)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"from baybe.parameters import CustomDiscreteParameter\n",
Expand All @@ -205,7 +250,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -226,9 +271,7 @@
"The **SequentialGreedyRecommender** is a powerful recommender that leverages BoTorch optimization functions to perform sequential Greedy optimization. It can be applied for discrete, continuous and hybrid sarch spaces. It is an implementation of the BoTorch optimization functions for discrete, continuous and mixed spaces. **It is important to note that this recommender performs a brute-force search when applied in hybrid search spaces, as it optimizes the continuous part of the space while exhaustively searching choices in the discrete subspace.** You can customize this behavior to only sample a certain percentage of the discrete subspace via the sample_percentage attribute and to choose different sampling strategies via the hybrid_sampler attribute. \n",
"\n",
"e.g.\n",
"strategy = TwoPhaseStrategy(recommender=SequentialGreedyRecommender(hybrid_sampler=\"Farthest\", sampling_percentage=0.3))\n",
"\n",
"The **NaiveHybridRecommender** can be applied to all search spaces, but is intended to be used in hybrid spaces. This recommender **combines individual recommenders for the continuous and the discrete subspaces. It independently optimizes each subspace and consolidates the best results to generate a candidate for the original hybrid space.** "
"strategy = TwoPhaseStrategy(recommender=SequentialGreedyRecommender(hybrid_sampler=\"Farthest\", sampling_percentage=0.3))"
]
},
{
Expand All @@ -241,18 +284,9 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\lordm\\Desktop\\Projects\\baybe\\.venv\\lib\\site-packages\\baybe\\recommenders\\bayesian.py:492: UserWarning: The value of 'allow_recommending_already_measured' differs from what is specified in the discrete recommender. The value of the discrete recommender will be ignored.\n",
" warnings.warn(\n"
]
}
],
"outputs": [],
"source": [
"available_surr_models = [\n",
" \"GaussianProcessSurrogate\", \n",
Expand Down Expand Up @@ -281,7 +315,7 @@
" allow_recommending_already_measured=False,\n",
" )\n",
"\n",
"hybrid_recommender = NaiveHybridRecommender(\n",
"hybrid_recommender = SequentialGreedyRecommender(\n",
" allow_repeated_recommendations=False,\n",
" allow_recommending_already_measured=False\n",
")"
Expand All @@ -296,9 +330,29 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/vscode/.local/lib/python3.10/site-packages/baybe/strategies/deprecation.py:26: DeprecationWarning: 'TwoPhaseStrategy' is deprecated and will be removed in a future version. Please use 'recommenders.TwoPhaseMetaRecommender' class instead.\n",
" warnings.warn(\n"
]
},
{
"ename": "NameError",
"evalue": "name 'objective' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[10], line 10\u001b[0m\n\u001b[1;32m 1\u001b[0m strategy \u001b[38;5;241m=\u001b[39m TwoPhaseStrategy(\n\u001b[1;32m 2\u001b[0m initial_recommender \u001b[38;5;241m=\u001b[39m RandomRecommender(), \u001b[38;5;66;03m# Initial recommender, if no training data is available\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;66;03m# Other initial recommenders don't seem to work for my hybrid search space/set of parameters\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 7\u001b[0m switch_after\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m \u001b[38;5;66;03m# Switch to the model-based recommender after 1 batch or iteration (so the initial training data)\u001b[39;00m\n\u001b[1;32m 8\u001b[0m )\n\u001b[0;32m---> 10\u001b[0m campaign \u001b[38;5;241m=\u001b[39m Campaign(searchspace, \u001b[43mobjective\u001b[49m, strategy)\n",
"\u001b[0;31mNameError\u001b[0m: name 'objective' is not defined"
]
}
],
"source": [
"strategy = TwoPhaseStrategy(\n",
" initial_recommender = RandomRecommender(), # Initial recommender, if no training data is available\n",
Expand Down Expand Up @@ -354,7 +408,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 5aed9b0

Please sign in to comment.