From a12c8b9dcd48291fdc7071daed638e3282b52e8c Mon Sep 17 00:00:00 2001 From: ale94mleon Date: Mon, 12 Dec 2022 10:25:39 +0100 Subject: [PATCH] delete redundant code in GAmol --- Contrib/adme_models/test.ipynb | 2 +- docs/source/CHANGELOG.md | 15 +++++++++++---- moldrug/_version.py | 2 +- moldrug/utils.py | 35 +++++++++++++++++++--------------- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/Contrib/adme_models/test.ipynb b/Contrib/adme_models/test.ipynb index 66b4f7e..e349968 100644 --- a/Contrib/adme_models/test.ipynb +++ b/Contrib/adme_models/test.ipynb @@ -115,7 +115,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.13 | packaged by conda-forge | (main, May 27 2022, 16:58:50) \n[GCC 10.3.0]" }, "orig_nbformat": 4, "vscode": { diff --git a/docs/source/CHANGELOG.md b/docs/source/CHANGELOG.md index 8068181..3d2a59c 100644 --- a/docs/source/CHANGELOG.md +++ b/docs/source/CHANGELOG.md @@ -7,13 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [unreleased] -### Fix +## [3.2.2] - 2020.12.12 + +### Fixed - Bug: The initial individual was not printed properly. +### Removed + +- Redundant code in `moldrug.utils.GA` + ## [3.2.0] - 2022.11.28 -### Fix +### Fixed - Bug: The output error name when constraint fails has a idx prefix. E.g. `33_conf_27_error.pbz2` now is: `idx_33_conf_27_error.pbz2`. Now it is easy to delete all of this files at the end of the simulation if they are not needed. (on the last version the naming was not changing) @@ -35,7 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - The output error name when constraint fails has a idx prefix. E.g. 33_conf_27_error.pbz2 now is: idx_33_conf_27_error.pbz2. Now it is easy to delete all of this files at the end of the simulation if they are not needed. -### Fix +### Fixed - Clean code. - Improve docs. @@ -265,7 +271,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Minor code cleaning. - Better code covered during testing -[unreleased]: https://github.com/ale94mleon/MolDrug/compare/3.2.0...HEAD +[unreleased]: https://github.com/ale94mleon/MolDrug/compare/3.2.2...HEAD +[3.2.2]: https://github.com/ale94mleon/MolDrug/compare/3.2.0...3.2.2 [3.2.0]: https://github.com/ale94mleon/MolDrug/compare/3.1.0...3.2.0 [3.1.0]: https://github.com/ale94mleon/MolDrug/compare/3.0.3...3.1.0 [3.0.3]: https://github.com/ale94mleon/MolDrug/compare/3.0.1...3.0.3 diff --git a/moldrug/_version.py b/moldrug/_version.py index 9c522ce..1815fec 100644 --- a/moldrug/_version.py +++ b/moldrug/_version.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # We will use semantic version (major, minor, patch) -__version_tuple__ = version_tuple = (3, 2, 1) +__version_tuple__ = version_tuple = (3, 2, 2) __version__ = version = '.'.join([str(i) for i in __version_tuple__]) \ No newline at end of file diff --git a/moldrug/utils.py b/moldrug/utils.py index 7beba32..82174b0 100644 --- a/moldrug/utils.py +++ b/moldrug/utils.py @@ -997,7 +997,6 @@ def roulette_wheel_selection(p:List[float]): ind = np.argwhere(r <= c) return ind[0][0] - def to_dataframe(individuals:List[Individual]): """Create a DataFrame from individuals. @@ -1063,7 +1062,7 @@ def __init__(self, seed_mol:Union[Chem.rdchem.Mol, Iterable[Chem.rdchem.Mol]], ValueError In case of incorrect definition of mutate_crem_kwargs. It must be None or a dict instance. """ - self.__moldrug_version = __version__ + self.__moldrug_version__ = __version__ if mutate_crem_kwargs is None: mutate_crem_kwargs = dict() elif not isinstance(mutate_crem_kwargs, dict): @@ -1141,8 +1140,8 @@ def __call__(self, njobs:int = 1): self.NumCalls += 1 # Check version of MolDrug - if self.__moldrug_version != __version__: - warn.warning(f"{self.__class__.__name__} was initialized with moldrug-{self.__moldrug_version} but was called with moldrug-{__version__}") + if self.__moldrug_version__ != __version__: + warn.warning(f"{self.__class__.__name__} was initialized with moldrug-{self.__moldrug_version__} but was called with moldrug-{__version__}") # Here we will update if needed some parameters for the crem operations that could change between different calls. # We need to return the molecule, so we override the possible user definition respect to this keyword @@ -1202,10 +1201,7 @@ def __call__(self, njobs:int = 1): # Creating the arguments args_list = [] # Make a copy of the self.costfunc_kwargs - kwargs_copy = self.costfunc_kwargs.copy() - if 'wd' in getfullargspec(self.costfunc).args: - costfunc_jobs = tempfile.TemporaryDirectory(prefix='costfunc') - kwargs_copy['wd'] = costfunc_jobs.name + kwargs_copy = self.__make_kwargs_copy__() for individual in self.pop: args_list.append((individual, kwargs_copy)) @@ -1236,8 +1232,6 @@ def __call__(self, njobs:int = 1): 'generated':len(self.pop[:]) } - if 'wd' in getfullargspec(self.costfunc).args: shutil.rmtree(costfunc_jobs.name) - # Print some information of the initial population print(f"Initial Population: Best Individual: {min(self.pop)}") print(f"Accepted rate: {self.acceptance[self.NumGens]['accepted']} / {self.acceptance[self.NumGens]['generated']}\n") @@ -1296,10 +1290,7 @@ def __call__(self, njobs:int = 1): # Creating the arguments args_list = [] # Make a copy of the self.costfunc_kwargs - kwargs_copy = self.costfunc_kwargs.copy() - if 'wd' in getfullargspec(self.costfunc).args: - costfunc_jobs = tempfile.TemporaryDirectory(prefix='costfunc') - kwargs_copy['wd'] = costfunc_jobs.name + kwargs_copy = self.__make_kwargs_copy__() NumbOfSawIndividuals = len(self.SawIndividuals) for (i, individual) in enumerate(popc): @@ -1324,7 +1315,6 @@ def __call__(self, njobs:int = 1): f"=========Parellel=========:\n {e1}\n"\ f"==========Serial==========:\n {e2}" ) - if 'wd' in getfullargspec(self.costfunc).args: shutil.rmtree(costfunc_jobs.name) # Merge, Sort and Select self.pop += popc @@ -1421,6 +1411,21 @@ def mutate(self, individual:Individual): mol = Chem.AddHs(mol) return Individual(mol) + def __make_kwargs_copy__(self): + """Make a copy of the self.costfunc_kwargs. + It creates a temporal directory. + + Returns + ------- + dict + A copy of self.costfunc_kwargs with wd changed if needed + """ + kwargs_copy = self.costfunc_kwargs.copy() + if 'wd' in getfullargspec(self.costfunc).args: + costfunc_jobs = tempfile.TemporaryDirectory(prefix='costfunc') + kwargs_copy['wd'] = costfunc_jobs.name + return kwargs_copy + def pickle(self, title:str, compress = False): """Method to pickle the whole GA class