|
| 1 | +import inspect |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +from numpydoc_decorator import doc # type: ignore |
| 6 | + |
| 7 | +from .base import AnophelesBase |
| 8 | + |
| 9 | + |
| 10 | +class AnophelesDescribe(AnophelesBase): |
| 11 | + """Mixin class providing API introspection and discovery functionality.""" |
| 12 | + |
| 13 | + @doc( |
| 14 | + summary=""" |
| 15 | + List all available public API methods with their descriptions. |
| 16 | + """, |
| 17 | + returns=""" |
| 18 | + A dataframe with one row per public method, containing the method |
| 19 | + name, a short summary description, and its category (data access, |
| 20 | + analysis, or plotting). |
| 21 | + """, |
| 22 | + parameters=dict( |
| 23 | + category=""" |
| 24 | + Optional filter to show only methods of a given category. |
| 25 | + Supported values are "data", "analysis", "plot", or None to |
| 26 | + show all methods. |
| 27 | + """, |
| 28 | + ), |
| 29 | + ) |
| 30 | + def describe_api( |
| 31 | + self, |
| 32 | + category: Optional[str] = None, |
| 33 | + ) -> pd.DataFrame: |
| 34 | + methods_info = [] |
| 35 | + |
| 36 | + # Walk through all public methods on this instance. |
| 37 | + for name in sorted(dir(self)): |
| 38 | + # Skip private/dunder methods. |
| 39 | + if name.startswith("_"): |
| 40 | + continue |
| 41 | + |
| 42 | + attr = getattr(type(self), name, None) |
| 43 | + if attr is None: |
| 44 | + continue |
| 45 | + |
| 46 | + # Only include callable methods and non-property attributes. |
| 47 | + if isinstance(attr, property): |
| 48 | + continue |
| 49 | + if not callable(attr): |
| 50 | + continue |
| 51 | + |
| 52 | + # Extract the docstring summary. |
| 53 | + summary = self._extract_summary(attr) |
| 54 | + |
| 55 | + # Determine category. |
| 56 | + method_category = self._categorize_method(name) |
| 57 | + |
| 58 | + methods_info.append( |
| 59 | + { |
| 60 | + "method": name, |
| 61 | + "summary": summary, |
| 62 | + "category": method_category, |
| 63 | + } |
| 64 | + ) |
| 65 | + |
| 66 | + df = pd.DataFrame(methods_info) |
| 67 | + |
| 68 | + # Apply category filter if specified. |
| 69 | + if category is not None: |
| 70 | + valid_categories = {"data", "analysis", "plot"} |
| 71 | + if category not in valid_categories: |
| 72 | + raise ValueError( |
| 73 | + f"Invalid category: {category!r}. " |
| 74 | + f"Must be one of {valid_categories}." |
| 75 | + ) |
| 76 | + df = df[df["category"] == category].reset_index(drop=True) |
| 77 | + |
| 78 | + return df |
| 79 | + |
| 80 | + @staticmethod |
| 81 | + def _extract_summary(method) -> str: |
| 82 | + """Extract the first line of the docstring as a summary.""" |
| 83 | + docstring = inspect.getdoc(method) |
| 84 | + if not docstring: |
| 85 | + return "" |
| 86 | + # Take the first non-empty line as the summary. |
| 87 | + for line in docstring.strip().splitlines(): |
| 88 | + line = line.strip() |
| 89 | + if line: |
| 90 | + return line |
| 91 | + return "" |
| 92 | + |
| 93 | + @staticmethod |
| 94 | + def _categorize_method(name: str) -> str: |
| 95 | + """Categorize a method based on its name.""" |
| 96 | + if name.startswith("plot_"): |
| 97 | + return "plot" |
| 98 | + data_prefixes = ( |
| 99 | + "sample_", |
| 100 | + "snp_", |
| 101 | + "hap_", |
| 102 | + "cnv_", |
| 103 | + "genome_", |
| 104 | + "open_", |
| 105 | + "lookup_", |
| 106 | + "read_", |
| 107 | + "general_", |
| 108 | + "sequence_", |
| 109 | + "cohorts_", |
| 110 | + "aim_", |
| 111 | + "gene_", |
| 112 | + ) |
| 113 | + if name.startswith(data_prefixes): |
| 114 | + return "data" |
| 115 | + return "analysis" |
0 commit comments