Skip to content

Commit ea07d25

Browse files
committed
find latest wf directory
1 parent 16739ee commit ea07d25

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

examples/analyse_study_classification.ipynb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"from octopus.predict import TaskPredictorTest\n",
6363
"from octopus.predict.notebook_utils import (\n",
6464
" display_table,\n",
65+
" find_latest_study,\n",
6566
" show_aucroc_plots,\n",
6667
" show_confusionmatrix,\n",
6768
" show_overall_fi_plot,\n",
@@ -92,8 +93,10 @@
9293
"metadata": {},
9394
"outputs": [],
9495
"source": [
95-
"# INPUT: Select study\n",
96-
"study_directory = \"../studies/wf_octo_mrmr_octo/\""
96+
"# INPUT: Select study by name prefix (automatically picks the latest timestamped run)\n",
97+
"study_name_prefix = \"wf_octo_mrmr_octo\"\n",
98+
"study_directory = find_latest_study(\"../studies\", study_name_prefix)\n",
99+
"print(f\"Using study: {study_directory}\")"
97100
]
98101
},
99102
{
@@ -374,6 +377,9 @@
374377
"cell_metadata_filter": "vscode,-all",
375378
"main_language": "python",
376379
"notebook_metadata_filter": "-all"
380+
},
381+
"language_info": {
382+
"name": "python"
377383
}
378384
},
379385
"nbformat": 4,

octopus/predict/notebook_utils.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
__all__ = [
2323
"display_table",
24+
"find_latest_study",
2425
"show_aucroc_plots",
2526
"show_confusionmatrix",
2627
"show_overall_fi_plot",
@@ -31,6 +32,44 @@
3132
"show_testset_performance",
3233
]
3334

35+
def find_latest_study(studies_root: str | UPath, prefix: str) -> str:
36+
"""Find the latest study directory matching a name prefix.
37+
38+
Study directories are named ``<prefix>-YYYYMMDD_HHMMSS``. This function
39+
finds all directories matching the given *prefix* and returns the one with
40+
the most recent timestamp (lexicographic sort). Falls back to an exact
41+
match (no timestamp suffix) when no timestamped directories are found.
42+
43+
Args:
44+
studies_root: Path to the parent directory containing study directories.
45+
prefix: The study name prefix, e.g. ``"wf_octo_mrmr_octo"``.
46+
47+
Returns:
48+
Path string to the latest matching study directory.
49+
50+
Raises:
51+
FileNotFoundError: If no matching study directory is found.
52+
53+
Example:
54+
>>> from octopus.predict.notebook_utils import find_latest_study
55+
>>> study_dir = find_latest_study("./studies", "wf_octo_mrmr_octo")
56+
"""
57+
root = UPath(studies_root)
58+
# Match timestamped directories: prefix-YYYYMMDD_HHMMSS
59+
candidates = sorted(
60+
[d for d in root.glob(f"{prefix}-*") if d.is_dir()],
61+
key=lambda p: p.name,
62+
reverse=True,
63+
)
64+
if candidates:
65+
return str(candidates[0])
66+
# Fallback: exact match without timestamp
67+
exact = root / prefix
68+
if exact.is_dir():
69+
return str(exact)
70+
raise FileNotFoundError(f"No study directory found for prefix '{prefix}' in {root}")
71+
72+
3473
try:
3574
from IPython.display import display as ipython_display
3675
except ImportError:

0 commit comments

Comments
 (0)