Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Add sphinx event to add capability table to estimators' docs individually #2468

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,108 @@ def _does_not_start_with_underscore(input_string):
file.write(df_str)


def _add_estimator_capabilities_table(app, pagename, templatename, context, doctree):
"""Add estimator capabilities table to HTML page."""
from aeon.utils.discovery import all_estimators

estimators = all_estimators(include_sklearn=False)

if "title" in context and "body" in context:
for estimator_name, estimator_class in estimators:
if estimator_name == context["title"]:
if '<span class="caption-text">Capabilities</span>' in context["body"]:
return

tags = estimator_class.get_class_tags()

capabilities = {
key.split(":")[1]: value
for key, value in tags.items()
if key.startswith("capability:")
}

html_output = """
<div class="table-wrapper docutils container" id="id3">
<table class="docutils align-default" id="id3">
<caption>
<span class="caption-text">Capabilities</span>
<a class="headerlink" href="#id3" title="Link to this table">¶</a>
</caption>
<tbody>
"""

for idx, (key, value) in enumerate(capabilities.items()):
row_class = "row-odd" if idx % 2 == 0 else "row-even"
formatted_key = key.replace("_", " ").title()
if value is True:
formatted_value = "Yes"
elif value is False:
formatted_value = "No"
elif value is None:
formatted_value = "Not Set"
else:
formatted_value = str(value)

html_output += f"""
<tr class="{row_class}">
<th class="stub"><p>{formatted_key}</p></th>
<td><p>{formatted_value}</p></td>
</tr>
"""

html_output += """
</tbody>
</table>
</div>
"""

body = context["body"]

def _insert_table(html_content, table_markup):
"""Insert table into HTML content."""
# Look for existing NOTES section outside methods
start_methods = html_content.find('<dl class="py method">')
section_before_methods = html_content[:start_methods]

# Look for Notes section
notes_heading = '<p class="rubric">Notes</p>'
notes_pos = section_before_methods.find(notes_heading)

if notes_pos != -1:
# Notes exists, insert table after it
insert_pos = notes_pos + len(notes_heading)
return (
html_content[:insert_pos]
+ "\n"
+ table_markup
+ html_content[insert_pos:]
)
else:
# Need to create Notes section
# Find position before References or Examples or Methods
# whichever comes first
ref_pos = section_before_methods.find(
'<p class="rubric">References</p>'
)
ex_pos = section_before_methods.find(
'<p class="rubric">Examples</p>'
)

positions = [
pos for pos in [ref_pos, ex_pos, start_methods] if pos != -1
]
insert_pos = min(positions) if positions else start_methods

new_section = f'\n<p class="rubric">Notes</p>\n{table_markup}\n'
return (
html_content[:insert_pos]
+ new_section
+ html_content[insert_pos:]
)

context["body"] = _insert_table(body, html_output)


def setup(app):
"""Set up sphinx builder.

Expand All @@ -409,6 +511,7 @@ def setup(app):
app : Sphinx application object
"""
app.connect("builder-inited", _make_estimator_overview)
app.connect("html-page-context", _add_estimator_capabilities_table)


# -- Extension configuration -------------------------------------------------
Expand Down