Skip to content

Commit 3995b24

Browse files
committed
Improved logging
* Change many print statements to logging statements, for better control * Save log file to web app zip file for easier debugging
1 parent e259429 commit 3995b24

File tree

4 files changed

+416
-250
lines changed

4 files changed

+416
-250
lines changed

app/run_utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def configure_logging(level=None):
4444
logging.basicConfig(
4545
level=level,
4646
format="[%(asctime)s] [%(filename)s:%(lineno)d] %(levelname)s - %(message)s",
47-
datefmt="%Y-%m-%d %H:%M:%S",
47+
datefmt="%Y-%m-%d %H:%M:%S %Z",
4848
handlers=[
4949
logging.StreamHandler(), # Outputs logs to stderr by default
5050
# If you also want to log to a file, uncomment the following line:
@@ -93,8 +93,10 @@ def run_cli_command(
9393

9494
assert len(args) == len(ARG_ORDER), f'Expected {len(ARG_ORDER)} arguments, got {len(args)}'
9595

96+
inference_log_level = os.environ.get("INFERENCE_LOG_LEVEL", os.environ.get("LOG_LEVEL", "WARNING"))
97+
9698
all_arg_dict = {"protein_path": protein_path, "ligand": ligand, "config": config_path,
97-
"no_final_step_noise": True}
99+
"no_final_step_noise": True, "loglevel": inference_log_level}
98100
for arg_name, arg_val in zip(ARG_ORDER, args):
99101
all_arg_dict[arg_name] = arg_val
100102

@@ -136,12 +138,18 @@ def run_cli_command(
136138
capture_output=True,
137139
)
138140
logging.debug(f"Command output:\n{result.stdout}")
141+
full_output = f"Standard out:\n{result.stdout}"
139142
if result.stderr:
140143
# Skip progress bar lines
141144
stderr_lines = result.stderr.split("\n")
142145
stderr_lines = filter(lambda x: "%|" not in x, stderr_lines)
143146
stderr_text = "\n".join(stderr_lines)
144147
logging.error(f"Command error:\n{stderr_text}")
148+
full_output += f"\nStandard error:\n{stderr_text}"
149+
150+
with open(f"{temp_dir_path}/output.log", "w") as log_file:
151+
log_file.write(full_output)
152+
145153
else:
146154
logging.debug("Skipping command execution")
147155
artificial_output_dir = os.path.join(TEMP_DIR, "artificial_output")

datasets/process_mols.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from datasets.constants import aa_short2long, atom_order, three_to_one
1818
from datasets.parse_chi import get_chi_angles, get_coords, aa_idx2aa_short, get_onehot_sequence
1919
from utils.torsion import get_transformation_mask
20+
from utils.logging_utils import get_logger
2021

2122

2223
periodic_table = GetPeriodicTable()
@@ -305,11 +306,11 @@ def generate_conformer(mol):
305306
failures, id = 0, -1
306307
while failures < 3 and id == -1:
307308
if failures > 0:
308-
print(f'rdkit coords could not be generated. trying again {failures}.')
309+
get_logger().debug(f'rdkit coords could not be generated. trying again {failures}.')
309310
id = AllChem.EmbedMolecule(mol, ps)
310311
failures += 1
311312
if id == -1:
312-
print('rdkit coords could not be generated without using random coords. using random coords now.')
313+
get_logger().info('rdkit coords could not be generated without using random coords. using random coords now.')
313314
ps.useRandomCoords = True
314315
AllChem.EmbedMolecule(mol, ps)
315316
AllChem.MMFFOptimizeMolecule(mol, confId=0)
@@ -417,6 +418,7 @@ def write_mol_with_coords(mol, new_coords, path):
417418
w.write(mol)
418419
w.close()
419420

421+
420422
def read_molecule(molecule_file, sanitize=False, calc_charges=False, remove_hs=False):
421423
if molecule_file.endswith('.mol2'):
422424
mol = Chem.MolFromMol2File(molecule_file, sanitize=False, removeHs=False)
@@ -433,8 +435,8 @@ def read_molecule(molecule_file, sanitize=False, calc_charges=False, remove_hs=F
433435
elif molecule_file.endswith('.pdb'):
434436
mol = Chem.MolFromPDBFile(molecule_file, sanitize=False, removeHs=False)
435437
else:
436-
return ValueError('Expect the format of the molecule_file to be '
437-
'one of .mol2, .sdf, .pdbqt and .pdb, got {}'.format(molecule_file))
438+
raise ValueError('Expect the format of the molecule_file to be '
439+
'one of .mol2, .sdf, .pdbqt and .pdb, got {}'.format(molecule_file))
438440

439441
try:
440442
if sanitize or calc_charges:
@@ -449,7 +451,12 @@ def read_molecule(molecule_file, sanitize=False, calc_charges=False, remove_hs=F
449451

450452
if remove_hs:
451453
mol = Chem.RemoveHs(mol, sanitize=sanitize)
452-
except:
454+
455+
except Exception as e:
456+
# Print stacktrace
457+
import traceback
458+
msg = traceback.format_exc()
459+
get_logger().warning(f"Failed to process molecule: {molecule_file}\n{msg}")
453460
return None
454461

455462
return mol

0 commit comments

Comments
 (0)