"""Sphinx extension: enhance dataset documentation pages. 1. Injects an enhanced dataset card (paradigm chips, stats, action buttons) 2. Injects an adaptive visual summary grid with panels for timeline, class balance, sessions, channels, and HED tags (when available) 3. Restructures the docstring into a tabbed layout (Overview, Code Examples, Metadata, Notes) 4. Shows inherited methods below tabs Pre-generated SVG images live in ``_static/timelines/.svg`` and ``_static/viz/_classes.svg`` / ``_sessions.svg``. To regenerate *all* SVGs (timelines + viz), run (from the repo root):: PYTHONPATH=. python scripts/generate_dataset_viz.py """ import csv import functools import inspect import json import math import os import re import statistics from datetime import datetime, timezone from html import escape from urllib.parse import quote from urllib.request import Request, urlopen try: from dataset_constants import ( PARADIGM_COLORS, PARADIGM_LABELS, ) from dataset_constants import country_flag as _country_flag_iso from dataset_constants import ( normalize_country, ) except ImportError: from docs.source.sphinxext.dataset_constants import ( PARADIGM_COLORS, PARADIGM_LABELS, ) from docs.source.sphinxext.dataset_constants import country_flag as _country_flag_iso from docs.source.sphinxext.dataset_constants import ( normalize_country, ) _PARADIGM_LABELS = PARADIGM_LABELS _PARADIGM_COLORS = PARADIGM_COLORS _BENCHMARK_FILES = [ ("within_session_mi_left_vs_right_hand.csv", "MI left vs right"), ("within_session_mi_all_classes.csv", "MI all classes"), ("within_session_mi_right_hand_vs_feet.csv", "MI right hand vs feet"), ("within_session_ssvep_all_classes.csv", "SSVEP all classes"), ("within_session_erp_p300_all_classes.csv", "ERP/P300 all classes"), ] _BENCHMARK_CONTEXT_CACHE = {} _DOI_METADATA_CACHE = {} _DOI_CACHE_LOADED = False _DOI_RE = re.compile(r"^10\.\d{4,}/", re.IGNORECASE) _RST_INLINE_RE = re.compile(r"\*\*(.+?)\*\*|``(.+?)``|\*(.+?)\*") _RST_FOOTNOTE_RE = re.compile(r"\s*\[\d+\]_\.?") _RST_LIST_SPLIT_RE = re.compile(r"\s+- ") _DATASET_PAGEVIEWS_CACHE = None _DATASET_PAGEVIEWS_CACHE_SRC = None # --------------------------------------------------------------------------- # Official Creative Commons SVG icons (from creativecommons/cc-assets) # Stored as individual .svg files in _static/icons/cc/. # --------------------------------------------------------------------------- _CC_ICONS_DIR = os.path.join(os.path.dirname(__file__), "..", "_static", "icons", "cc") @functools.lru_cache(maxsize=None) def _cc_icon_svg(icon_key, size=16): """Return an inline ```` element for a Creative Commons icon. Reads the SVG file from ``_static/icons/cc/.svg`` (cached via ``lru_cache``) and injects ``width``/``height``/``aria-hidden`` attributes so it can be embedded inline. """ svg_path = os.path.join(_CC_ICONS_DIR, f"{icon_key}.svg") try: with open(svg_path, "r") as fh: svg = fh.read().strip() except FileNotFoundError: return "" if not svg: return "" # Inject width/height/aria-hidden into the opening tag. return svg.replace( "
  • " f'{escape(entry["label"])} ' f'{entry["n_pipelines"]} pipelines' f'{stats_str}' "
  • " ) rows_html = "\n ".join(rows) return ( '
    ' '
    ' '

    Benchmark Context

    ' 'WithinSession' "
    " f'

    Included in {ctx["n_tables"]} MOABB benchmark table(s). ' "Scores are across available pipelines (WithinSession accuracy).

    " f'

    Sample frame: {escape(sample_frame or "N/A")}

    ' f"
      {rows_html}
    " "
    " ) def _make_citation_impact_html( info, benchmark_ctx, *, live_citations=True, pageview_counts=None, pageview_rank=None, pageview_meta=None, ): """Build a compact citation, impact, and visibility block.""" code = str(info.get("code") or "") paper_doi = _normalize_doi(info.get("paper_doi") or info.get("doi")) dataset_doi = _normalize_doi(info.get("dataset_doi") or info.get("doi")) if not code and not paper_doi and not dataset_doi: return "" items = [] script_html = "" if paper_doi: doi_link_href = escape(f"https://doi.org/{quote(paper_doi, safe='')}", quote=True) items.append( f'
  • Paper DOI{escape(paper_doi)}
  • ' ) if _is_likely_doi(paper_doi): if live_citations: items.append( f'
  • CitationsLoading…
  • ' ) openalex_id = quote(f"https://doi.org/{paper_doi}", safe="") openalex_url = f"https://api.openalex.org/works/{openalex_id}" crossref_url = f"https://api.crossref.org/works/{quote(paper_doi)}" items.append( "
  • Public API" f'Crossref' " | " f'OpenAlex' "
  • " ) script_html = """ """ else: doi_static_href = escape( f"https://doi.org/{quote(paper_doi, safe='')}", quote=True ) items.append( f'
  • CitationsSee DOI
  • ' ) if dataset_doi and dataset_doi != paper_doi: data_doi_href = escape( f"https://doi.org/{quote(dataset_doi, safe='')}", quote=True ) items.append( f'
  • Data DOI{escape(dataset_doi)}
  • ' ) if benchmark_ctx and benchmark_ctx.get("n_tables"): items.append( f'
  • MOABB tables{benchmark_ctx["n_tables"]} (WithinSession)
  • ' ) # --- Page Views row (single rich entry in the same list) --- if isinstance(pageview_counts, dict) and any( key in pageview_counts for key in ("last30", "all_time", "weekly_12") ): last30 = pageview_counts.get("last30") all_time = pageview_counts.get("all_time") updated_str = _format_updated_utc((pageview_meta or {}).get("generated_at_utc")) # Rank line if ( isinstance(pageview_rank, dict) and pageview_rank.get("rank") and pageview_rank.get("total") ): rank_line = ( f'
    #{int(pageview_rank["rank"])} of ' f'{int(pageview_rank["total"])} · Top {int(pageview_rank.get("top_percent", 0))}% most viewed
    ' ) else: rank_line = '
    Ranking: n/a
    ' # Sparkline sparkline_cell = "" weekly = pageview_counts.get("weekly_12") if weekly: sparkline_cell = ( f'
    ' f"{_sparkline_svg(weekly)}
    " ) # Compose the rich right-hand value pv_value = ( f'
    ' f'
    ' f'
    ' f"30d: {_format_count(last30)}" f' · ' f"all-time: {_format_count(all_time)}" f"
    " f"{rank_line}" f'
    Updated: {updated_str}
    ' f"
    " f"{sparkline_cell}" f"
    " ) items.append(f'
  • Page Views{pv_value}
  • ') if not items: return "" list_html = "\n ".join(items) return ( '
    ' '

    Citation & Impact

    ' f"
      {list_html}
    " f"{script_html}" "
    " ) def _extract_description_text(lines): """Extract plain description lines from docstring, skipping admonitions/directives.""" def _skip_directive_block(start_idx): directive_indent = len(lines[start_idx]) - len(lines[start_idx].lstrip()) i = start_idx + 1 while i < len(lines): if lines[i].strip() == "": i += 1 continue line_indent = len(lines[i]) - len(lines[i].lstrip()) if line_indent > directive_indent: i += 1 continue break return i desc = [] i = 0 while i < len(lines): stripped = lines[i].strip() # Skip admonition blocks (directive + indented body) if stripped.startswith(".. admonition::"): i = _skip_directive_block(i) continue # Stop at rubrics and version directives if stripped.startswith(".. rubric::"): break if stripped.startswith( (".. versionadded::", ".. versionchanged::", ".. deprecated::") ): break if stripped.startswith(".. "): i = _skip_directive_block(i) continue # Stop at underline-style "references" or "References" header if ( stripped.lower() in ("references", "references:") and i + 1 < len(lines) and set(lines[i + 1].strip()) <= {"-", "=", "~"} and lines[i + 1].strip() ): break desc.append(stripped) i += 1 # Strip leading/trailing blanks while desc and not desc[0]: desc.pop(0) while desc and not desc[-1]: desc.pop() return desc def _rst_paragraph_to_html(text): """Convert a paragraph of reST-like text to simple HTML. Handles: **bold**, *italic*, ``code``, list items (- prefix), and strips footnote references like [1]_. """ # Strip reST footnote references text = _RST_FOOTNOTE_RE.sub("", text) # Check if this is a list block (lines starting with "- ") if " - " in text and text.lstrip().startswith("- "): # Split on " - " pattern that indicates list items items = _RST_LIST_SPLIT_RE.split(text) items = [it.strip() for it in items if it.strip()] formatted = [] for item in items: item = _rst_inline_to_html(item) formatted.append(f"
  • {item}
  • ") return f'
      {"".join(formatted)}
    ' return f"

    {_rst_inline_to_html(text)}

    " def _rst_inline_to_html(text): """Convert reST inline markup to HTML, escaping the rest.""" parts = [] pos = 0 # Match **bold**, *italic*, ``code`` — process in order of appearance for m in _RST_INLINE_RE.finditer(text): # Escape text before this match parts.append(escape(text[pos : m.start()])) if m.group(1) is not None: parts.append(f"{escape(m.group(1))}") elif m.group(2) is not None: parts.append(f"{escape(m.group(2))}") elif m.group(3) is not None: parts.append(f"{escape(m.group(3))}") pos = m.end() parts.append(escape(text[pos:])) return "".join(parts) def _make_overview_teaser_html(description_lines, cls_name): """Build a collapsible overview teaser panel with key facts.""" if not description_lines: return "" # --- Parse all paragraphs and references --- all_paragraphs = [] current = [] ref_lines = [] in_refs = False for line in description_lines: if re.match(r"^\.\.\s+rubric::\s*References", line): in_refs = True continue if in_refs: ref_lines.append(line) continue if not line: if current: all_paragraphs.append(" ".join(current)) current = [] else: # If this line starts a list item and we have non-list content # buffered, flush the buffer first if line.startswith("- ") and current and not current[0].startswith("- "): all_paragraphs.append(" ".join(current)) current = [] current.append(line) if current: all_paragraphs.append(" ".join(current)) all_paragraphs = [p.strip() for p in all_paragraphs if p.strip()] if not all_paragraphs: return "" # --- Split into teaser (visible) vs overflow (hidden) --- # Show enough paragraphs to fill ~10-15 lines (~800 chars) TEASER_CHAR_LIMIT = 800 teaser_paragraphs = [] overflow_paragraphs = [] char_count = 0 for i, p in enumerate(all_paragraphs): if char_count < TEASER_CHAR_LIMIT or i == 0: teaser_paragraphs.append(p) char_count += len(p) else: overflow_paragraphs.append(p) # --- Build overflow HTML (hidden by default) --- full_html_parts = [] for p in overflow_paragraphs: full_html_parts.append(_rst_paragraph_to_html(p)) # References (collapsed inside expanded) ref_html = "" ref_text = [r for r in ref_lines if r.strip()] if ref_text: ref_content = "".join(f"

    {_rst_inline_to_html(r)}

    " for r in ref_text) ref_html = ( '
    ' "References" f"{ref_content}" "
    " ) full_section = "" if full_html_parts or ref_html: full_content = "\n".join(full_html_parts) full_section = ( f'
    ' f"{full_content}" f"{ref_html}" f"
    " ) # --- Compose component --- overview_id = _dataset_dom_id("ds-overview", cls_name) teaser_parts = [] for p in teaser_paragraphs: html = _rst_paragraph_to_html(p) # Add class to

    and

      elements for consistent styling html = html.replace("

      ", '

      ', 1) html = html.replace( '

        ', '
          ', 1, ) teaser_parts.append(html) teaser_html = "".join(teaser_parts) # Only show expand controls if there's overflow content has_overflow = bool(full_section) toggle_btn = "" if has_overflow: toggle_btn = ( f'" ) return ( f'
          ' f'

          Overview

          ' f"{teaser_html}" f"{full_section}" f'
          ' f"{toggle_btn}" f'Open in Overview tab →' f"
          " f"
          " ) def _make_hed_summary_html(info): """Build HTML summary for embedded HED tags.""" hed_map = info.get("hed_tags") if info else None event_id = info.get("event_id") if info else None event_total = len(event_id) if isinstance(event_id, dict) and event_id else None if not hed_map: return "" items = list(hed_map.items()) tagged = len(items) denom = event_total if event_total else tagged coverage = f"{tagged}/{denom}" family_counts = {} event_rows = [] tree_items = [] for event_name, hed_str in items: elements = _split_hed_top_level(hed_str) tokens = [] for elem in elements: t = _hed_token_label(elem) if t and t not in tokens: tokens.append(t) for t in tokens: family_counts[t] = family_counts.get(t, 0) + 1 chip_html = "".join( f'{escape(tok)}' for tok in tokens[:5] ) event_rows.append( '
          ' f'{escape(str(event_name))}' f'
          {chip_html}
          ' "
          " ) tree_nodes = [_hed_element_to_tree(e) for e in elements] tree_lines = _render_hed_tree_lines(tree_nodes) tree_text = "\n".join(tree_lines) if tree_lines else "(no tree)" tree_items.append( '
          ' f'Tree · {escape(str(event_name))}' f'
          {escape(tree_text)}
          ' "
          " ) top_families = sorted(family_counts.items(), key=lambda x: (-x[1], x[0]))[:6] max_count = max([c for _, c in top_families], default=1) bar_rows = [] for fam, count in top_families: width = int((count / max_count) * 100) bar_rows.append( '
          ' f'{escape(fam)}' f'
          ' f"{count}" "
          " ) return ( '
          ' '
          ' 'HED tags' f'{coverage} events annotated' "
          " '

          Source: MOABB BIDS HED annotation mapping.

          ' f'
          {"".join(bar_rows)}
          ' f'
          {"".join(event_rows)}
          ' '
          ' '

          HED tree view

          ' f'{"".join(tree_items)}' "
          " "
          " ) # --------------------------------------------------------------------------- # Enhanced header card (Layer 1) # --------------------------------------------------------------------------- def _make_github_issue_url(cls_name): """Build a pre-filled GitHub issue URL for this dataset.""" issue_title = quote(f"[Dataset] Issue with {cls_name}") issue_body = quote( f"## Dataset\n\n" f"- **Dataset ID:** {cls_name}\n\n" f"## Issue Description\n\n" f"Please describe the issue you encountered with this dataset:\n\n" f"## Steps to Reproduce\n\n" f"1. \n2. \n3. \n\n" f"## Expected Behavior\n\n\n" f"## Additional Context\n\n" ) url = ( f"https://github.com/NeuroTechX/moabb/issues/new" f"?title={issue_title}&body={issue_body}&labels=dataset" ) return escape(url, quote=True) def _country_flag(country_str): """Return a flag emoji for a country name or ISO 3166-1 alpha-2 code.""" iso2 = normalize_country(country_str) return _country_flag_iso(iso2) def _highlight_python(code): """Highlight a Python code string using Pygments, returning HTML.""" from pygments import highlight as _pygments_highlight from pygments.formatters import HtmlFormatter from pygments.lexers import PythonLexer formatter = HtmlFormatter(nowrap=False, cssclass="highlight") return _pygments_highlight(code, PythonLexer(), formatter) def _load_dataset_pageviews(srcdir): """Load GA4 dataset page views snapshot from docs static assets.""" global _DATASET_PAGEVIEWS_CACHE, _DATASET_PAGEVIEWS_CACHE_SRC if _DATASET_PAGEVIEWS_CACHE is not None and _DATASET_PAGEVIEWS_CACHE_SRC == srcdir: return _DATASET_PAGEVIEWS_CACHE snapshot_path = os.path.join(srcdir, "_static", "analytics", "pageviews.json") payload = { "generated_at_utc": "", "status": "disabled", "reason": "", "counts": {}, "ranks": {}, } def _norm_name(name): return re.sub(r"[^a-z0-9]+", "", str(name).strip().lower()) canonical_name_map = {} try: from moabb.datasets.utils import dataset_list for ds_cls in dataset_list: canonical_name_map[_norm_name(ds_cls.__name__)] = ds_cls.__name__ except Exception: canonical_name_map = {} try: with open(snapshot_path, encoding="utf-8") as f: raw_payload = json.load(f) payload["generated_at_utc"] = str(raw_payload.get("generated_at_utc", "") or "") payload["status"] = str(raw_payload.get("status", "") or "disabled") payload["reason"] = str(raw_payload.get("reason", "") or "") raw_counts = raw_payload.get("counts", {}) merged_counts = {} if isinstance(raw_counts, dict): for cls_name, values in raw_counts.items(): if not isinstance(values, dict): continue canonical = canonical_name_map.get(_norm_name(cls_name), str(cls_name)) entry = merged_counts.setdefault( canonical, {"last30": 0, "all_time": 0, "weekly_12": [0] * 12} ) if "last30" in values: try: entry["last30"] += int(values["last30"]) except (TypeError, ValueError): pass if "all_time" in values: try: entry["all_time"] += int(values["all_time"]) except (TypeError, ValueError): pass weekly = values.get("weekly_12") if isinstance(weekly, list): for i, val in enumerate(weekly[:12]): try: entry["weekly_12"][i] += int(val) except (TypeError, ValueError): pass payload["counts"] = merged_counts ranked = sorted( payload["counts"].items(), key=lambda kv: (-int(kv[1].get("all_time", 0)), kv[0]), ) total = len(ranked) ranks = {} if total > 0: for idx, (name, _) in enumerate(ranked, start=1): ranks[name] = { "rank": idx, "total": total, "top_percent": max(1, math.ceil((idx / total) * 100)), } payload["ranks"] = ranks except Exception: pass _DATASET_PAGEVIEWS_CACHE = payload _DATASET_PAGEVIEWS_CACHE_SRC = srcdir return payload def _get_dataset_pageview_counts(srcdir, cls_name): """Return page view counts for a dataset class name (if available).""" return _load_dataset_pageviews(srcdir).get("counts", {}).get(cls_name, {}) def _get_dataset_pageview_rank(srcdir, cls_name): """Return pageview rank metadata for a dataset class name (if available).""" return _load_dataset_pageviews(srcdir).get("ranks", {}).get(cls_name, {}) def _get_dataset_pageview_meta(srcdir): """Return GA pageview snapshot metadata.""" payload = _load_dataset_pageviews(srcdir) return { "generated_at_utc": payload.get("generated_at_utc", ""), "status": payload.get("status", ""), "reason": payload.get("reason", ""), } def _format_count(value): """Return a thousands-separated integer string, or 'n/a'.""" try: return f"{int(value):,}" except (TypeError, ValueError): return "n/a" def _format_updated_utc(iso_text): """Format ISO timestamp into YYYY-MM-DD UTC.""" if not iso_text: return "n/a" try: parsed = datetime.fromisoformat(str(iso_text).replace("Z", "+00:00")) parsed = parsed.astimezone(timezone.utc) return parsed.strftime("%Y-%m-%d UTC") except Exception: return "n/a" def _sparkline_svg(values): """Return an inline SVG sparkline for a sequence of numeric values.""" if not isinstance(values, list) or len(values) < 2: return "" nums = [] for val in values[:12]: try: nums.append(max(0, int(val))) except (TypeError, ValueError): nums.append(0) if len(nums) < 2: return "" width, height = 110, 28 pad = 2 min_y = pad max_y = height - pad max_val = max(nums) if nums else 0 denom = max_val if max_val > 0 else 1 step = (width - 2 * pad) / (len(nums) - 1) points = [] for i, val in enumerate(nums): x = pad + i * step y = max_y - ((val / denom) * (max_y - min_y)) points.append((x, y)) line_points = " ".join(f"{x:.2f},{y:.2f}" for x, y in points) area_path = ( f"M {points[0][0]:.2f} {max_y:.2f} " + " ".join(f"L {x:.2f} {y:.2f}" for x, y in points) + f" L {points[-1][0]:.2f} {max_y:.2f} Z" ) return ( '' f'' f'' "" ) def _make_provenance_html(info): """Build the author provenance byline block for the card header.""" investigators = info.get("investigators") or [] senior_author = info.get("senior_author") contact_info = info.get("contact_info") or [] institution = info.get("institution") country = info.get("country") publication_year = info.get("publication_year") if not investigators and not senior_author: return "" # Build author name spans — all authors listed, senior author highlighted senior_lower = (senior_author or "").strip().lower() author_spans = [] for name in investigators: safe = escape(name) if name.strip().lower() == senior_lower: author_spans.append( f'{safe}' ) else: author_spans.append(f'{safe}') authors_line = "" if author_spans: authors_line = ( '

          ' 'Authors' f'{", ".join(author_spans)}' "

          " ) # Build provenance meta line: flag institution, country · year · email meta_parts = [] if institution: flag = _country_flag(country) if country else "" flag_prefix = f"{flag}\u2002" if flag else "" inst_str = f"{flag_prefix}{escape(institution)}" if country: inst_str += f", {escape(country)}" meta_parts.append(f"{inst_str}") if publication_year: meta_parts.append(f"{int(publication_year)}") if contact_info: email = contact_info[0] safe_email = escape(email) meta_parts.append(f'{safe_email}') meta_line = "" if meta_parts: sep = '\u00b7' meta_line = f'
          {sep.join(meta_parts)}
          ' return f'
          {authors_line}{meta_line}
          ' def _make_header_html( cls_name, info, source_url=None, *, live_citations=True, pageview_counts=None, pageview_rank=None, pageview_meta=None, description_lines=None, ): """Build the enhanced dataset card HTML (Layer 1).""" paradigm = info.get("paradigm") or "unknown" label = _PARADIGM_LABELS.get(paradigm, paradigm.title()) color = _PARADIGM_COLORS.get(paradigm, "#546E7A") n_subj = info.get("n_subjects") n_sess = info.get("n_sessions") paper_doi = _normalize_doi(info.get("paper_doi") or info.get("doi")) sampling_rate = info.get("sampling_rate") n_channels = info.get("n_channels") channel_types = info.get("channel_types") n_classes = info.get("n_classes") class_labels = info.get("class_labels") trial_duration = info.get("trial_duration") default_subject = info.get("default_subject", 1) subject_literal = repr(default_subject) code = info.get("code") quickstart_id = _dataset_dom_id("ds-quickstart", cls_name) quickstart_btn_id = _dataset_dom_id("ds-quickstart-btn", cls_name) source_html = "" if source_url: source_html = ( f'[source]' ) # --- Subtitle: auto-generated from paradigm + classes --- # Use the actual count of class labels when available display_n_classes = n_classes if class_labels: display_n_classes = len(class_labels) subtitle_parts = [label] if display_n_classes is not None: subtitle_parts.append(f"{display_n_classes} classes") if class_labels and len(class_labels) <= 6: safe_labels = [escape(str(lbl)) for lbl in class_labels[:6]] subtitle_parts.append("(" + " vs ".join(safe_labels) + ")") subtitle = ", ".join(subtitle_parts[:2]) if len(subtitle_parts) > 2: subtitle += " " + subtitle_parts[2] # --- Stat chips --- chips = [] chips.append(f'{label}') if code: chips.append(f'Code: {code}') if n_subj is not None: chips.append(f'{n_subj} subjects') if n_sess is not None: sess_label = "session" if n_sess == 1 else "sessions" chips.append(f'{n_sess} {sess_label}') # Channel chip if n_channels is not None: ch_detail = "" if channel_types and isinstance(channel_types, dict): eeg_count = channel_types.get("eeg", channel_types.get("EEG", 0)) if eeg_count and eeg_count != n_channels: ch_detail = f" ({eeg_count} EEG)" chips.append( f'{n_channels} ch{ch_detail}' ) # Sampling rate chip if sampling_rate is not None: sr_display = ( f"{int(sampling_rate)}" if sampling_rate == int(sampling_rate) else f"{sampling_rate:g}" ) chips.append(f'{sr_display} Hz') # Classes chip if display_n_classes is not None: chips.append( f'{display_n_classes} classes' ) # Trial duration chip if trial_duration is not None: dur_display = ( f"{trial_duration:g}" if trial_duration != int(trial_duration) else f"{int(trial_duration)}.0" ) chips.append(f'{dur_display} s trials') # License chip license_raw = info.get("license") license_key = _normalize_license(license_raw) if license_key: display_name, license_url, icon_keys = _LICENSE_INFO[license_key] icons_html = "".join(_cc_icon_svg(k) for k in icon_keys) if license_url: chips.append( f'' f"{icons_html}{escape(display_name)}" ) else: chips.append( f'' f"{icons_html}{escape(display_name)}" ) chips_html = "\n ".join(chips) benchmark_html = _make_benchmark_context_html(cls_name, info) benchmark_ctx = _get_benchmark_context(cls_name) citation_html = _make_citation_impact_html( info, benchmark_ctx, live_citations=live_citations, pageview_counts=pageview_counts, pageview_rank=pageview_rank, pageview_meta=pageview_meta, ) compare_anchor_map = { "imagery": "motor-imagery", "p300": "p300-erp", "erp": "p300-erp", "ssvep": "ssvep", "cvep": "c-vep", "rstate": "resting-states", } compare_anchor = compare_anchor_map.get(paradigm) compare_href = "../dataset_summary.html" if compare_anchor: compare_href += f"#{compare_anchor}" # --- Optional class-label line --- class_line = "" if class_labels: preview = ", ".join(escape(str(lbl)) for lbl in class_labels[:8]) if len(class_labels) > 8: preview += ", ..." class_line = ( f'

          ' f"Class Labels: {preview}

          " ) # --- Action buttons --- actions = [] # Quickstart button toggles the code panel actions.append( ( f'" ) ) if paper_doi: doi_href = escape(f"https://doi.org/{quote(paper_doi, safe='')}", quote=True) actions.append( f'Read Paper' ) actions.append(f'Compare Similar') github_url = _make_github_issue_url(cls_name) actions.append( f'Report Issue' ) actions_html = "\n ".join(actions) # --- Quickstart code block (Pygments-highlighted) --- quickstart_code = ( f"from moabb.datasets import {cls_name}\n\n" f"dataset = {cls_name}()\n" f"data = dataset.get_data(subjects=[{subject_literal}])\n" f"print(data[{subject_literal}])" ) hl_code = _highlight_python(quickstart_code) quickstart = ( f'" ) # --- Alt name (paper description) --- alt_name_html = "" paper_desc = info.get("paper_description") if paper_desc: alt_name_html = f'

          {escape(paper_desc)}

          ' # --- Author provenance --- provenance_html = _make_provenance_html(info) # --- Overview teaser --- overview_teaser = _make_overview_teaser_html(description_lines or [], cls_name) return f"""\
          {source_html}

          Dataset Snapshot

          {cls_name}

          {alt_name_html}

          {subtitle}

          {provenance_html}
          {chips_html}
          {class_line}
          {actions_html}
          {overview_teaser} {quickstart} {benchmark_html} {citation_html}
          """ # --------------------------------------------------------------------------- # Visual summary grid (Layer 2) # --------------------------------------------------------------------------- def _make_visual_grid_lines(cls_name, info, srcdir, docstring_lines=None): """Build RST lines for the adaptive visual summary grid.""" lines = [] paradigm = info.get("paradigm") or "unknown" paradigm_label = _PARADIGM_LABELS.get(paradigm, paradigm.title()) n_classes = info.get("n_classes") class_labels = info.get("class_labels") or [] display_n_classes = len(class_labels) if class_labels else n_classes runs_per_session = info.get("runs_per_session") n_sessions = info.get("n_sessions") trial_duration = info.get("trial_duration") has_hed = bool(info.get("hed_tags")) if info else False hed_html = _make_hed_summary_html(info) if has_hed else "" # Check which SVGs exist timeline_svg = os.path.join(srcdir, "_static", "timelines", f"{cls_name}.svg") has_timeline = os.path.exists(timeline_svg) # If the class docstring already embeds a protocol figure via a # ``.. figure::`` directive (local or external URL), skip the # auto-injected timeline grid card to avoid duplication. if has_timeline and docstring_lines is not None: if any(".. figure::" in line for line in docstring_lines): has_timeline = False # Build channel summary HTML channel_html = _make_channel_summary_html(info) # Count how many grid items we have (timeline gets full width, others share row) n_items = sum([has_timeline, has_hed, bool(channel_html)]) if n_items == 0: if not has_timeline: return [] # Timeline gets its own full-width row; remaining items share a 2-col row n_cols = 2 if (n_items - int(has_timeline)) >= 2 else 1 lines.extend( [ "", f".. grid:: {n_cols}", " :gutter: 3", "", ] ) # Panel footnotes protocol_bits = [] if trial_duration is not None: protocol_bits.append(f"{trial_duration:g}s task window per trial") if display_n_classes is not None: protocol_bits.append( f"{display_n_classes}-class {paradigm_label.lower()} paradigm" ) if runs_per_session is not None and n_sessions is not None: protocol_bits.append( f"{runs_per_session} runs/session across {n_sessions} sessions" ) protocol_note = " \u00b7 ".join(protocol_bits) if has_timeline: lines.extend( [ " .. grid-item-card:: Stimulus Protocol", " :columns: 12", " :class-card: ds-viz-card", "", f" .. image:: /_static/timelines/{cls_name}.svg", " :width: 100%", " :class: timeline-diagram", "", ] ) if protocol_note: lines.extend( [ " .. raw:: html", "", f'

          {escape(protocol_note)}

          ', "", ] ) if has_hed: lines.extend( [ " .. grid-item-card:: HED Event Tags", " :class-card: ds-viz-card", "", " .. raw:: html", "", ] ) for hed_line in hed_html.split("\n"): lines.append(f" {hed_line}") lines.append("") if channel_html: lines.extend( [ " .. grid-item-card:: Channel Summary", " :class-card: ds-viz-card", "", " .. raw:: html", "", ] ) for ch_line in channel_html.split("\n"): lines.append(f" {ch_line}") lines.append("") # Timeline disclaimer if has_timeline: lines.extend( [ ".. raw:: html", "", '

          ' "This diagram is automatically generated from MOABB metadata. " "Please consult the original publication to confirm " "the experimental protocol details.

          ", "", ] ) return lines def _make_channel_summary_html(info): """Build a small HTML card summarising channel configuration.""" n_channels = info.get("n_channels") if info else None channel_types = info.get("channel_types") if info else None montage = info.get("montage") if info else None sampling_rate = info.get("sampling_rate") if info else None reference = info.get("reference") if info else None filter_range = info.get("filter_range") if info else None line_freq = info.get("line_freq") if info else None sensor_type = info.get("sensor_type") if info else None if ( n_channels is None and montage is None and sampling_rate is None and reference is None and filter_range is None and line_freq is None and sensor_type is None ): return "" rows = [] if n_channels is not None: rows.append(("Total channels", f"{float(n_channels):g}")) if channel_types and isinstance(channel_types, dict): sorted_types = sorted(channel_types.items(), key=lambda x: (-x[1], x[0])) for ctype, count in sorted_types[:4]: if str(ctype).lower() == "eeg" and sensor_type: rows.append((ctype.upper(), f"{float(count):g} ({sensor_type})")) else: rows.append((ctype.upper(), f"{float(count):g}")) if montage is not None: rows.append(("Montage", "10-05" if montage == "standard_1005" else str(montage))) if sampling_rate is not None: sr_display = ( f"{int(sampling_rate)} Hz" if sampling_rate == int(sampling_rate) else f"{sampling_rate:g} Hz" ) rows.append(("Sampling", sr_display)) if reference: rows.append(("Reference", str(reference))) if filter_range: rows.append(("Filter", str(filter_range))) if line_freq is not None: line_display = ( f"{float(line_freq):g} Hz" if isinstance(line_freq, (int, float)) else str(line_freq) ) rows.append(("Notch / line", line_display)) if not rows: return "" row_html = "\n".join( f'
          {escape(str(key))}{escape(str(val))}
          ' for key, val in rows ) return f'
          {row_html}
          ' # --------------------------------------------------------------------------- # Tabbed docstring restructuring (Layer 3) # --------------------------------------------------------------------------- def _restructure_docstring_lines(lines, cls_name, default_subject=1): """Reorganize docstring lines into a tabbed layout. Scans lines for section markers and groups content into: - Overview (description + references) - Code Examples (code snippet) - Metadata (admonition cards) - Notes (notes, version directives) Returns modified lines wrapped in sphinx-design tab-set. """ # Classify lines into buckets metadata_lines = [] description_lines = [] reference_lines = [] notes_lines = [] current_bucket = "description" in_admonition = False admonition_indent = 0 i = 0 while i < len(lines): line = lines[i] stripped = line.strip() # Detect admonition starts (metadata cards + feedback) if stripped.startswith(".. admonition::"): title = stripped[len(".. admonition::") :].strip() metadata_titles = { "Dataset summary", "Participants", "Equipment", "Preprocessing", "Data Access", "Experimental Protocol", } if title in metadata_titles: current_bucket = "metadata" in_admonition = True admonition_indent = len(line) - len(line.lstrip()) metadata_lines.append(line) i += 1 continue elif "Found an issue" in title: # Discard feedback section — "Report Issue" is now in # the card header action bar. current_bucket = "discard_feedback" in_admonition = True admonition_indent = len(line) - len(line.lstrip()) i += 1 continue # Detect rubric sections if stripped.startswith(".. rubric::"): rubric_title = stripped[len(".. rubric::") :].strip() if rubric_title == "References": current_bucket = "references" in_admonition = False reference_lines.append(line) i += 1 continue elif rubric_title in ("Notes", "Notes:"): current_bucket = "notes" in_admonition = False notes_lines.append(line) i += 1 continue # Detect version directives → notes if ( stripped.startswith(".. versionadded::") or stripped.startswith(".. versionchanged::") or stripped.startswith(".. deprecated::") ): current_bucket = "notes" notes_lines.append(line) i += 1 continue # If in an admonition, check if we've left it (by indentation) if in_admonition: if stripped == "": # Blank lines can be part of admonition if current_bucket == "metadata": metadata_lines.append(line) # discard_feedback: silently skip i += 1 continue line_indent = len(line) - len(line.lstrip()) if line_indent > admonition_indent: # Still inside admonition if current_bucket == "metadata": metadata_lines.append(line) # discard_feedback: silently skip i += 1 continue else: # Exited admonition in_admonition = False current_bucket = "description" # Route to current bucket if current_bucket == "references": # Stay in references until a new section starts or double blank reference_lines.append(line) elif current_bucket == "notes": notes_lines.append(line) elif current_bucket == "metadata": metadata_lines.append(line) else: description_lines.append(line) i += 1 # Clean up: strip trailing blanks from each bucket def _strip_trailing_blanks(lst): while lst and lst[-1].strip() == "": lst.pop() return lst description_lines = _strip_trailing_blanks(description_lines) metadata_lines = _strip_trailing_blanks(metadata_lines) reference_lines = _strip_trailing_blanks(reference_lines) notes_lines = _strip_trailing_blanks(notes_lines) # If we have very little content, don't restructure has_metadata = bool(metadata_lines) has_description = any(line.strip() for line in description_lines) if not has_metadata and not has_description: return None # Don't restructure def _reindent(block, base_indent): """Re-indent a block of lines to a new base indentation. Finds the minimum indentation in the block and shifts all lines so that minimum becomes ``base_indent``. Blank lines stay blank. """ # Determine minimum indentation of non-blank lines min_indent = None for bline in block: if bline.strip(): indent = len(bline) - len(bline.lstrip()) if min_indent is None or indent < min_indent: min_indent = indent if min_indent is None: min_indent = 0 out = [] for bline in block: if not bline.strip(): out.append("") else: # Strip the common prefix, add the new base indent current_indent = len(bline) - len(bline.lstrip()) extra = current_indent - min_indent out.append(" " * (base_indent + extra) + bline.lstrip()) return out # The tab-item content needs 6 spaces of indentation (3 for tab-set + 3 for tab-item) TAB_INDENT = 6 # Build the tabbed layout new_lines = [] # Tab-set directive new_lines.append("") new_lines.append(".. tab-set::") new_lines.append(" :class: ds-doc-tabs") new_lines.append("") # --- Tab: Overview --- new_lines.append(" .. tab-item:: Overview") new_lines.append("") new_lines.append( " " * TAB_INDENT + f".. _{_dataset_dom_id('ds-overview', cls_name)}:" ) new_lines.append("") if description_lines: new_lines.extend(_reindent(description_lines, TAB_INDENT)) new_lines.append("") if reference_lines: new_lines.extend(_reindent(reference_lines, TAB_INDENT)) new_lines.append("") # If overview is empty, add a placeholder if not description_lines and not reference_lines: new_lines.append(" " * TAB_INDENT + "*No description available.*") new_lines.append("") # --- Tab: Code Examples --- new_lines.append(" .. tab-item:: Code Examples") new_lines.append("") new_lines.append(" " * TAB_INDENT + ".. code-block:: python") new_lines.append("") new_lines.append(" " * (TAB_INDENT + 3) + f"from moabb.datasets import {cls_name}") new_lines.append(" " * (TAB_INDENT + 3) + f"dataset = {cls_name}()") subject_literal = repr(default_subject) new_lines.append( " " * (TAB_INDENT + 3) + f"data = dataset.get_data(subjects=[{subject_literal}])" ) new_lines.append(" " * (TAB_INDENT + 3) + f"print(data[{subject_literal}])") new_lines.append("") # --- Tab: Metadata --- if has_metadata: new_lines.append(" .. tab-item:: Metadata") new_lines.append("") new_lines.extend(_reindent(metadata_lines, TAB_INDENT)) new_lines.append("") # --- Tab: Notes --- if notes_lines: new_lines.append(" .. tab-item:: Notes") new_lines.append("") new_lines.extend(_reindent(notes_lines, TAB_INDENT)) new_lines.append("") return new_lines # --------------------------------------------------------------------------- # Legacy timeline lines (kept for when grid is not used) # --------------------------------------------------------------------------- def _make_timeline_lines(cls_name, srcdir): """Build RST lines for the timeline image + disclaimer.""" svg_path = os.path.join(srcdir, "_static", "timelines", f"{cls_name}.svg") if not os.path.exists(svg_path): return [] return [ "", ".. rubric:: Stimulus Protocol Timeline", "", f".. image:: /_static/timelines/{cls_name}.svg", " :width: 100%", " :class: timeline-diagram", "", ".. raw:: html", "", '

          ' "This diagram is automatically generated from MOABB metadata. " "Please consult the original publication to confirm " "the experimental protocol details.

          ", "", ] # --------------------------------------------------------------------------- # Main docstring processor # --------------------------------------------------------------------------- def _is_autosummary_context(): """Return True if we are called from autosummary's summary extraction.""" import traceback for frame_info in traceback.extract_stack(): if "autosummary" in frame_info.filename and frame_info.name == "get_items": return True return False def autodoc_process_docstring(app, what, name, obj, options, lines): """Enhance dataset class docstrings with card, grid, and tabs.""" if what != "class": return if not _is_concrete_dataset(obj): return # Skip heavy restructuring when autosummary is extracting one-line # summaries for the API table — it only needs the first paragraph. if _is_autosummary_context(): return cls_name = obj.__name__ info = _get_dataset_info(obj) source_url = _get_dataset_source_url(obj) # --- Extract description lines for teaser (before restructuring) --- desc_lines = _extract_description_text(lines) # --- Layer 1: Enhanced card (inserted at top) --- top_block = [] if info: live_citations = getattr(app.config, "dataset_card_live_citations", True) pageview_counts = _get_dataset_pageview_counts(app.srcdir, cls_name) pageview_rank = _get_dataset_pageview_rank(app.srcdir, cls_name) pageview_meta = _get_dataset_pageview_meta(app.srcdir) header_html = _make_header_html( cls_name, info, source_url=source_url, live_citations=live_citations, pageview_counts=pageview_counts, pageview_rank=pageview_rank, pageview_meta=pageview_meta, description_lines=desc_lines, ) top_block.append(".. raw:: html") top_block.append("") for h_line in header_html.split("\n"): top_block.append(f" {h_line}") top_block.append("") # --- Layer 2: Visual summary grid --- if info: grid_lines = _make_visual_grid_lines( cls_name, info, app.srcdir, docstring_lines=lines ) top_block.extend(grid_lines) # --- Layer 3: Restructure remaining docstring into tabs --- default_subject = info.get("default_subject", 1) if info else 1 restructured = _restructure_docstring_lines( lines, cls_name, default_subject=default_subject ) if restructured is not None: # Replace all existing lines with restructured content lines.clear() lines.extend(restructured) # Insert the card + grid at position 0 for i, line in enumerate(top_block): lines.insert(i, line) def source_read_add_inherited(app, docname, source): """Inject :inherited-members: and __init__ into dataset page RST sources. Auto-generated RST files from autosummary only have :members:. For dataset classes we also need inherited methods (get_data, download, etc.) and __init__ shown explicitly. """ if not docname.startswith("generated/moabb.datasets."): return # Skip non-class pages (e.g. function pages, module pages) if not re.search(r"\.\. autoclass::", source[0]): return # Remove sidebars on dataset pages for a focused, wide layout. # PyData theme reads this from document metadata field lists. source[0] = ( ":html_theme.sidebar_primary.remove:\n" ":html_theme.sidebar_secondary.remove:\n\n" + source[0] ) # Add :inherited-members: and :show-inheritance: after :members: source[0] = source[0].replace( " :members:\n", " :members:\n :inherited-members:\n :show-inheritance:\n", ) # Add __init__ to :special-members: so the constructor is documented source[0] = re.sub( r"(:special-members:.*)", r"\1,__init__", source[0], ) def _generate_all_svgs(app): """Generate stimulus timeline SVGs. Runs once at the start of the Sphinx build (builder-inited event). SVGs are written to ``_static/timelines/``. Controlled by the ``dataset_card_generate_svgs`` config value (default ``True``). When ``False``, SVG generation is skipped entirely. Existing SVG files are never overwritten. """ if not getattr(app.config, "dataset_card_generate_svgs", True): return import traceback srcdir = app.srcdir timeline_dir = os.path.join(srcdir, "_static", "timelines") os.makedirs(timeline_dir, exist_ok=True) try: from moabb.analysis.timeline import stimulus_timeline_svg from moabb.datasets.utils import dataset_list except ImportError: traceback.print_exc() print( "[dataset_timeline_ext] Could not import timeline functions. " "Make sure moabb is installed from the current repo." ) return for ds_cls in dataset_list: name = ds_cls.__name__ try: ds = ds_cls() except Exception: continue # Timeline timeline_path = os.path.join(timeline_dir, f"{name}.svg") if not os.path.exists(timeline_path): try: svg = stimulus_timeline_svg(ds) with open(timeline_path, "w", encoding="utf-8") as f: f.write(svg) except Exception: pass def setup(app): app.add_config_value("dataset_card_live_citations", True, "html") app.add_config_value("dataset_card_generate_svgs", True, "html") app.connect("builder-inited", _generate_all_svgs) app.connect("autodoc-process-docstring", autodoc_process_docstring) app.connect("source-read", source_read_add_inherited) return {"version": "1.0", "parallel_read_safe": True}