Skip to content

Commit 0c7ad4f

Browse files
geemi725claude
andcommitted
Clean up code quality issues across core modules
- Replace wildcard `from typing import *` with explicit imports - Remove duplicate, unused, and unnecessary imports - Fix `== None` to `is None`, bare `except:` to `except Exception:` - Replace `type(x) == T` with `isinstance`, fix bool comparisons - Simplify redundant ternary; fix return type annotation on get_functional_groups Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent d4d0211 commit 0c7ad4f

3 files changed

Lines changed: 22 additions & 24 deletions

File tree

exmol/data.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass, asdict, field
2-
from typing import Optional
2+
from typing import Optional, Tuple
33
import numpy as np # type: ignore
44

55

@@ -10,13 +10,13 @@ class Descriptors:
1010
#: Descriptor type
1111
descriptor_type: str
1212
#: Descriptor values
13-
descriptors: tuple
13+
descriptors: Tuple
1414
# Descriptor name
15-
descriptor_names: tuple
15+
descriptor_names: Tuple
1616
# plotting name
17-
plotting_names: tuple = ()
17+
plotting_names: Tuple = ()
1818
# t_stats for each molecule
19-
tstats: tuple = ()
19+
tstats: Tuple = ()
2020

2121

2222
@dataclass

exmol/exmol.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
from functools import reduce, lru_cache
1+
from functools import reduce
22
import inspect
3-
from typing import *
3+
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union, cast
44
import io
55
import math
66
import requests # type: ignore
77
import numpy as np
88
import matplotlib.pyplot as plt # type: ignore
9-
from matplotlib.patches import Rectangle, FancyBboxPatch # type: ignore
9+
from matplotlib.patches import FancyBboxPatch # type: ignore
1010
from matplotlib.offsetbox import AnnotationBbox # type: ignore
1111
import matplotlib as mpl # type: ignore
1212
import re
1313
import selfies as sf # type: ignore
1414
import tqdm # type: ignore
15-
import textwrap # type: ignore
15+
import textwrap
1616
import skunk # type: ignore
1717
import synspace # type: ignore
1818

@@ -25,7 +25,6 @@
2525
from rdkit.Chem import MolToSmiles as mol2smi # type: ignore
2626
from rdkit.Chem import rdchem, MACCSkeys, AllChem # type: ignore
2727
from rdkit.Chem.Draw import MolToImage as mol2img, DrawMorganBit # type: ignore
28-
from rdkit.Chem import rdchem # type: ignore
2928
from rdkit.DataStructs.cDataStructs import BulkTanimotoSimilarity, TanimotoSimilarity # type: ignore
3029

3130
import openai
@@ -52,7 +51,7 @@ def _ecfp_names(examples, joint_bits):
5251
multiple_bases = _check_multiple_bases(examples)
5352
# need to get base molecule(s) for naming
5453
bitInfo = {} # Type Dict[Any, Any]
55-
base_mol = [smi2mol(e.smiles) for e in examples if e.is_origin == True]
54+
base_mol = [smi2mol(e.smiles) for e in examples if e.is_origin]
5655
if multiple_bases:
5756
multiBitInfo = {} # type: Dict[int, Tuple[Any, int, int]]
5857
for b in base_mol:
@@ -256,7 +255,7 @@ def name_morgan_bit(m: Any, bitInfo: Dict[Any, Any], key: int) -> Optional[str]:
256255

257256
def get_functional_groups(
258257
mol: Any, return_all: bool = False, cutoff: int = 300
259-
) -> set[str]:
258+
) -> Set[str]:
260259
"""Get a set of functional groups present in a molecule, sorted by priority, avoiding overlaps.
261260
262261
:param mol: RDKit molecule
@@ -467,16 +466,16 @@ def run_stoned(
467466
"""
468467
if alphabet is None:
469468
alphabet = get_basic_alphabet()
470-
if type(alphabet) == set:
469+
if isinstance(alphabet, set):
471470
alphabet = list(alphabet)
472471
alphabet_symbols = _alphabet_to_elements(alphabet)
473472
# make sure starting smiles is consistent with alphabet
474473
_ = _check_alphabet_consistency(start_smiles, alphabet_symbols, check=True)
475474
num_mutation_ls = list(range(min_mutations, max_mutations + 1))
476475

477476
start_mol = smi2mol(start_smiles)
478-
if start_mol == None:
479-
raise Exception("Invalid starting structure encountered")
477+
if start_mol is None:
478+
raise ValueError("Invalid starting structure encountered")
480479

481480
# want it so after sampling have num_samples
482481
randomized_smile_orderings = [
@@ -582,7 +581,7 @@ def run_chemed(
582581
return [], []
583582
try:
584583
data = reply.json()
585-
except:
584+
except Exception:
586585
return [], []
587586
smiles = [d["CanonicalSMILES"] for d in data["PropertyTable"]["Properties"]]
588587
smiles = list(set(smiles))
@@ -990,7 +989,7 @@ def rcf_explain(
990989
:param nmols: Desired number of molecules
991990
:param filter_nondrug: Whether or not to filter out non-drug molecules. Default is True if input passes filter
992991
"""
993-
if type(delta) is float:
992+
if isinstance(delta, (int, float)):
994993
delta = (-delta, delta)
995994

996995
def is_high(e):
@@ -1144,7 +1143,7 @@ def plot_cf(
11441143
fig, axs = plt.subplots(R, C, **figure_kwargs)
11451144
else:
11461145
axs = fig.subplots(R, C)
1147-
if type(axs) != np.ndarray: # Happens if nrows=ncols=1
1146+
if not isinstance(axs, np.ndarray): # Happens if nrows=ncols=1
11481147
axs = np.array([[axs]])
11491148
axs = axs.flatten()
11501149
for i, (img, e) in enumerate(zip(imgs, exps)):
@@ -1256,7 +1255,7 @@ def plot_descriptors(
12561255
if descriptor_type == "ecfp":
12571256
# get reference for ECFP
12581257
if multiple_bases:
1259-
bases = [smi2mol(e.smiles) for e in examples if e.is_origin == True]
1258+
bases = [smi2mol(e.smiles) for e in examples if e.is_origin]
12601259
bi = {} # type: Dict[Any, Any]
12611260
for b in bases:
12621261
bit_info = {} # type: Dict[Any, Any]
@@ -1403,7 +1402,7 @@ def check_multiple_aromatic_rings(mol):
14031402
continue
14041403
if flag:
14051404
count += 1
1406-
return True if count > 1 else False
1405+
return count > 1
14071406

14081407

14091408
def merge_text_explains(

exmol/plot_utils.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from matplotlib.offsetbox import OffsetImage, AnnotationBbox, TextArea, VPacker # type: ignore
2-
from typing import *
3-
import xml.etree.ElementTree as ET # type: ignore
4-
import io # type: ignore
1+
from matplotlib.offsetbox import AnnotationBbox, TextArea, VPacker # type: ignore
2+
from typing import Any, Dict, List, Optional, Tuple
3+
import io
54
import matplotlib.pyplot as plt # type: ignore
65
import numpy as np # type: ignore
76
from rdkit.Chem import rdFMCS as MCS # type: ignore

0 commit comments

Comments
 (0)