Skip to content

Commit

Permalink
add ms2rescore profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurDeclercq committed Apr 12, 2024
1 parent 21195c4 commit eea9500
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
35 changes: 34 additions & 1 deletion ms2rescore/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import sys
from pathlib import Path
from typing import Union
import cProfile
import pstats
import io

Check failure on line 12 in ms2rescore/__main__.py

View workflow job for this annotation

GitHub Actions / test-python-package (3.8)

Ruff (F401)

ms2rescore/__main__.py:12:8: F401 `io` imported but unused

Check failure on line 12 in ms2rescore/__main__.py

View workflow job for this annotation

GitHub Actions / test-python-package (3.9)

Ruff (F401)

ms2rescore/__main__.py:12:8: F401 `io` imported but unused

Check failure on line 12 in ms2rescore/__main__.py

View workflow job for this annotation

GitHub Actions / test-python-package (3.10)

Ruff (F401)

ms2rescore/__main__.py:12:8: F401 `io` imported but unused

Check failure on line 12 in ms2rescore/__main__.py

View workflow job for this annotation

GitHub Actions / test-python-package (3.11)

Ruff (F401)

ms2rescore/__main__.py:12:8: F401 `io` imported but unused

from rich.console import Console
from rich.logging import RichHandler
Expand Down Expand Up @@ -139,6 +142,14 @@ def _argument_parser() -> argparse.ArgumentParser:
dest="fasta_file",
help="path to FASTA file",
)
parser.add_argument(
"--profile",
# metavar="BOOL",
action="store_true",
# type=bool,
# dest="profile",
help="boolean to enable profiling with cProfile",
)

return parser

Expand All @@ -161,6 +172,22 @@ def _setup_logging(passed_level: str, log_file: Union[str, Path]):
)


def profile(fnc, filepath):
"""A decorator that uses cProfile to profile a function"""

def inner(*args, **kwargs):
pr = cProfile.Profile()
pr.enable()
retval = fnc(*args, **kwargs)
pr.disable()
with open(filepath, "w") as f:
ps = pstats.Stats(pr, stream=f).sort_stats("cumulative")
ps.print_stats(r"(?<![\w-])ms2rescore(?![\w-])") # Only print stats for ms2rescore
return retval

return inner


def main_tims():
"""Run MS²Rescore command-line interface in TIMS²Rescore mode."""
main(tims=True)
Expand Down Expand Up @@ -196,7 +223,13 @@ def main(tims=False):

# Run MS²Rescore
try:
rescore(configuration=config)
if cli_args.profile:
profiled_rescore = profile(
rescore, config["ms2rescore"]["output_path"] + ".profile.txt"
)
profiled_rescore(configuration=config)
else:
rescore(configuration=config)
except Exception as e:
LOGGER.exception(e)
sys.exit(1)
Expand Down
5 changes: 5 additions & 0 deletions ms2rescore/package_data/config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@
"description": "Write an HTML report with various QC metrics and charts",
"type": "boolean",
"default": false
},
"profile": {
"description": "Write an txt report using cProfile for profiling",
"type": "boolean",
"default": false
}
}
}
Expand Down

0 comments on commit eea9500

Please sign in to comment.