From aea34a8a55bc4731a65773058742a2c1cab65e13 Mon Sep 17 00:00:00 2001 From: JacksonBurns Date: Thu, 3 Feb 2022 23:27:23 -0500 Subject: [PATCH 1/3] remove expensive calls to sum --- AIMSim/ops/similarity_measures.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/AIMSim/ops/similarity_measures.py b/AIMSim/ops/similarity_measures.py index c469c401..6e319418 100644 --- a/AIMSim/ops/similarity_measures.py +++ b/AIMSim/ops/similarity_measures.py @@ -252,8 +252,7 @@ def __call__(self, mol1_descriptor, mol2_descriptor): Returns: similarity_ (float): Similarity value """ - if sum(mol1_descriptor.to_numpy()) == 0 \ - or sum(mol1_descriptor.to_numpy()) == 0: + if not np.any(mol1_descriptor.to_numpy()) or not np.any(mol2_descriptor.to_numpy()): raise ValueError('Molecule descriptor has no active bits') similarity_ = None if self.metric == "l0_similarity": From 2a5271c3bc2278001e5d1034247f3d2f6d9e6c72 Mon Sep 17 00:00:00 2001 From: JacksonBurns Date: Thu, 3 Feb 2022 23:42:09 -0500 Subject: [PATCH 2/3] ravel faster than flatten --- AIMSim/ops/descriptor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AIMSim/ops/descriptor.py b/AIMSim/ops/descriptor.py index d25f4886..974cbcf4 100644 --- a/AIMSim/ops/descriptor.py +++ b/AIMSim/ops/descriptor.py @@ -64,7 +64,7 @@ def to_numpy(self): if not hasattr(self, "numpy_"): self.numpy_ = np.zeros((0,), dtype=np.int8) DataStructs.ConvertToNumpyArray(self.rdkit_, self.numpy_) - self.numpy_ = self.numpy_.flatten() + self.numpy_ = self.numpy_.ravel() return self.numpy_ def to_rdkit(self): From dd49f1e278308a61cfffeaf2480f5a2e42b72a9f Mon Sep 17 00:00:00 2001 From: JacksonBurns Date: Thu, 3 Feb 2022 23:47:19 -0500 Subject: [PATCH 3/3] `getattr` faster than `hasattr` --- AIMSim/ops/descriptor.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/AIMSim/ops/descriptor.py b/AIMSim/ops/descriptor.py index 974cbcf4..4b382c53 100644 --- a/AIMSim/ops/descriptor.py +++ b/AIMSim/ops/descriptor.py @@ -81,9 +81,7 @@ def to_rdkit(self): return self.rdkit_ def check_init(self): - if hasattr(self, "numpy_") or hasattr(self, "rdkit_"): - return True - return False + return getattr(self, "numpy_", None) is not None or getattr(self, "rdkit_", None) is not None def _set_morgan_fingerprint(self, molecule_graph, radius, n_bits, **kwargs): """Set the descriptor to a morgan fingerprint.