|
| 1 | +""" |
| 2 | +Read-only results-inspector tools over PyAutoFit output directories. |
| 3 | +
|
| 4 | +Each function is a thin wrapper over an existing public PyAutoFit aggregator |
| 5 | +API (`autofit.aggregator.Aggregator`, `af.SearchOutput`): argument parsing, one call, and |
| 6 | +JSON-friendly serialization — nothing more. Any behaviour beyond that belongs |
| 7 | +in PyAutoFit itself, not here ("glue, not code"). |
| 8 | +
|
| 9 | +This module deliberately has no MCP dependency: `server.py` registers these |
| 10 | +functions as MCP tools, and the test suite exercises them without the `mcp` |
| 11 | +package installed. |
| 12 | +""" |
| 13 | + |
| 14 | +import contextlib |
| 15 | +import json |
| 16 | +import sys |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +from autonerves.dictable import to_dict |
| 20 | + |
| 21 | +import autofit as af |
| 22 | + |
| 23 | +# The directory-backed aggregator — af.Aggregator is the database-backed one, |
| 24 | +# and the alias also keeps the API audit from resolving it there. |
| 25 | +from autofit.aggregator import Aggregator as DirectoryAggregator |
| 26 | + |
| 27 | + |
| 28 | +@contextlib.contextmanager |
| 29 | +def _stdout_to_stderr(): |
| 30 | + """ |
| 31 | + An MCP stdio server must keep stdout clean — it carries the JSON-RPC |
| 32 | + channel — but the directory aggregator prints progress to stdout, |
| 33 | + so every autofit call runs with stdout redirected to stderr. |
| 34 | + """ |
| 35 | + with contextlib.redirect_stdout(sys.stderr): |
| 36 | + yield |
| 37 | + |
| 38 | + |
| 39 | +def _float_or_none(value): |
| 40 | + try: |
| 41 | + return None if value is None else float(value) |
| 42 | + except (TypeError, ValueError): |
| 43 | + return None |
| 44 | + |
| 45 | + |
| 46 | +def _search_row(search) -> dict: |
| 47 | + summary = search.samples_summary |
| 48 | + max_lh_sample = getattr(summary, "max_log_likelihood_sample", None) |
| 49 | + return dict( |
| 50 | + name=search.name, |
| 51 | + unique_tag=search.unique_tag, |
| 52 | + directory=str(search.directory), |
| 53 | + is_complete=search.is_complete, |
| 54 | + log_evidence=_float_or_none(getattr(summary, "log_evidence", None)), |
| 55 | + max_log_likelihood=_float_or_none( |
| 56 | + getattr(max_lh_sample, "log_likelihood", None) |
| 57 | + ), |
| 58 | + model_free_parameters=getattr(search.model, "prior_count", None), |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +def list_searches( |
| 63 | + directory: str, |
| 64 | + sort_by: str = "log_evidence", |
| 65 | + limit: int = 20, |
| 66 | + completed_only: bool = False, |
| 67 | +) -> list: |
| 68 | + with _stdout_to_stderr(): |
| 69 | + aggregator = DirectoryAggregator.from_directory( |
| 70 | + directory, completed_only=completed_only |
| 71 | + ) |
| 72 | + rows = [_search_row(search) for search in aggregator] |
| 73 | + rows.sort( |
| 74 | + key=lambda row: (row.get(sort_by) is not None, row.get(sort_by)), |
| 75 | + reverse=True, |
| 76 | + ) |
| 77 | + return rows[:limit] if limit else rows |
| 78 | + |
| 79 | + |
| 80 | +def get_model(directory: str) -> dict: |
| 81 | + with _stdout_to_stderr(): |
| 82 | + model = af.SearchOutput(Path(directory)).model |
| 83 | + return dict(info=model.info, model=to_dict(model)) |
| 84 | + |
| 85 | + |
| 86 | +def get_result_summary(directory: str) -> str: |
| 87 | + with _stdout_to_stderr(): |
| 88 | + return af.SearchOutput(Path(directory)).model_results |
| 89 | + |
| 90 | + |
| 91 | +def get_samples_summary(directory: str) -> dict: |
| 92 | + with _stdout_to_stderr(): |
| 93 | + search_output = af.SearchOutput(Path(directory)) |
| 94 | + summary = search_output.samples_summary |
| 95 | + if summary is None: |
| 96 | + raise FileNotFoundError( |
| 97 | + f"No samples summary found under {directory}/files/ — " |
| 98 | + "is this a completed search output directory?" |
| 99 | + ) |
| 100 | + model = search_output.model |
| 101 | + return dict( |
| 102 | + log_evidence=_float_or_none(summary.log_evidence), |
| 103 | + max_log_likelihood=_float_or_none( |
| 104 | + summary.max_log_likelihood_sample.log_likelihood |
| 105 | + ), |
| 106 | + parameter_paths=[".".join(path) for path in model.paths], |
| 107 | + max_log_likelihood_parameters=[ |
| 108 | + float(value) |
| 109 | + for value in summary.max_log_likelihood_sample.parameter_lists_for_model( |
| 110 | + model |
| 111 | + ) |
| 112 | + ], |
| 113 | + # MLE searches (e.g. LBFGS) have no PDF, so no median-PDF sample. |
| 114 | + median_pdf_parameters=None |
| 115 | + if summary.median_pdf_sample is None |
| 116 | + else [ |
| 117 | + float(value) |
| 118 | + for value in summary.median_pdf_sample.parameter_lists_for_model( |
| 119 | + model |
| 120 | + ) |
| 121 | + ], |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +def get_search_info(directory: str) -> dict: |
| 126 | + search_json = Path(directory) / "files" / "search.json" |
| 127 | + with _stdout_to_stderr(): |
| 128 | + search_output = af.SearchOutput(Path(directory)) |
| 129 | + return dict( |
| 130 | + name=search_output.name, |
| 131 | + unique_tag=search_output.unique_tag, |
| 132 | + is_complete=search_output.is_complete, |
| 133 | + search=json.loads(search_json.read_text()) |
| 134 | + if search_json.exists() |
| 135 | + else None, |
| 136 | + ) |
| 137 | + |
| 138 | + |
| 139 | +def list_images(directory: str) -> list: |
| 140 | + # Visualization outputs live under <directory>/image/ (SearchOutput.image |
| 141 | + # reads there, despite its docstring saying `files/`). |
| 142 | + return sorted(path.name for path in (Path(directory) / "image").glob("*.png")) |
| 143 | + |
| 144 | + |
| 145 | +def fetch_image(directory: str, name: str = "subplot_fit"): |
| 146 | + with _stdout_to_stderr(): |
| 147 | + return af.SearchOutput(Path(directory)).image(name) |
0 commit comments