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

fix HTML display of Spectrum objects #13041

Merged
merged 9 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test-doc: sample_data testing_data
$(PYTESTS) --doctest-modules --doctest-ignore-import-errors --doctest-glob='*.rst' ./doc/ --ignore=./doc/auto_examples --ignore=./doc/auto_tutorials --ignore=./doc/_build --ignore=./doc/conf.py --ignore=doc/sphinxext --fulltrace

pre-commit:
@pre-commit run -a
@pre-commit run -a --show-diff-on-failure

# Aliases for stuff we used to support or users might think of
ruff: pre-commit
Expand Down
2 changes: 1 addition & 1 deletion mne/html_templates/repr/_acquisition.html.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<td>{{ "%0.2f" | format(info["sfreq"]) }} Hz</td>
</tr>
{% endif %}
{% if inst is defined and inst.times is defined %}
{% if inst is defined and inst | has_attr("times") and inst.times is defined %}
<tr class="repr-element {{ section_class_name }} {{ collapsed_row_class }}">
<td class="mne-repr-section-toggle"></td>
<td>Time points</td>
Expand Down
62 changes: 62 additions & 0 deletions mne/html_templates/repr/_frequencies.html.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{% set section = "Frequencies" %}
{% set section_class_name = section | lower | append_uuid %}

{# Collapse content during documentation build. #}
{% if collapsed %}
{% set collapsed_row_class = "mne-repr-collapsed" %}
{% else %}
{% set collapsed_row_class = "" %}
{% endif %}

{%include 'static/_section_header_row.html.jinja' %}

<tr class="repr-element {{ section_class_name }} {{ collapsed_row_class }}">
<td class="mne-repr-section-toggle"></td>
<td>Data type</td>
<td>{{ inst._data_type }}</td>
</tr>
<tr class="repr-element {{ section_class_name }} {{ collapsed_row_class }}">
<td class="mne-repr-section-toggle"></td>
<td>Computed from</td>
<td>{{ computed_from }}</td>
</tr>
<tr class="repr-element {{ section_class_name }} {{ collapsed_row_class }}">
<td class="mne-repr-section-toggle"></td>
<td>Estimation method</td>
<td>{{ inst.method }}</td>
</tr>
<!--
<tr class="repr-element {{ section_class_name }} {{ collapsed_row_class }}">
<td class="mne-repr-section-toggle"></td>
<td>Dims</td>
<td>{{ inst._dims | join(", ") }}</td>
</tr>
-->
{% if "taper" in inst._dims %}
<tr class="repr-element {{ section_class_name }} {{ collapsed_row_class }}">
<td class="mne-repr-section-toggle"></td>
<td>Number of tapers</td>
<td>{{ inst._mt_weights.size }}</td>
</tr>
{% endif %}
{% if inst.freqs is defined %}
<tr class="repr-element {{ section_class_name }} {{ collapsed_row_class }}">
<td class="mne-repr-section-toggle"></td>
<td>Frequency range</td>
<td>{{ '%.2f'|format(inst.freqs[0]) }} – {{ '%.2f'|format(inst.freqs[-1]) }} Hz</td>
</tr>
<tr class="repr-element {{ section_class_name }} {{ collapsed_row_class }}">
<td class="mne-repr-section-toggle"></td>
<td>Number of frequency bins</td>
<td>{{ inst.freqs|length }}</td>
</tr>
{%- for unit in units %}
<tr class="repr-element {{ section_class_name }} {{ collapsed_row_class }}">
<td class="mne-repr-section-toggle"></td>
{%- if loop.index == 1 %}
<td rowspan={{ units | length }}>Units</td>
{%- endif %}
<td class="justify">{{ unit }}</td>
</tr>
{%- endfor %}
{% endif %}
57 changes: 9 additions & 48 deletions mne/html_templates/repr/spectrum.html.jinja
Original file line number Diff line number Diff line change
@@ -1,50 +1,11 @@
{%include '_js_and_css.html.jinja' %}

{% set info = inst.info %}

<table class="table mne-repr-table">
<tr>
<th>Data type</th>
<td>{{ spectrum._data_type }}</td>
</tr>
{%- for unit in units %}
<tr>
{%- if loop.index == 1 %}
<th rowspan={{ units | length }}>Units</th>
{%- endif %}
<td class="justify">{{ unit }}</td>
</tr>
{%- endfor %}
<tr>
<th>Data source</th>
<td>{{ inst_type }}</td>
</tr>
{%- if inst_type == "Epochs" %}
<tr>
<th>Number of epochs</th>
<td>{{ spectrum.shape[0] }}</td>
</tr>
{% endif -%}
<tr>
<th>Dims</th>
<td>{{ spectrum._dims | join(", ") }}</td>
</tr>
<tr>
<th>Estimation method</th>
<td>{{ spectrum.method }}</td>
</tr>
{% if "taper" in spectrum._dims %}
<tr>
<th>Number of tapers</th>
<td>{{ spectrum._mt_weights.size }}</td>
</tr>
{% endif %}
<tr>
<th>Number of channels</th>
<td>{{ spectrum.ch_names|length }}</td>
</tr>
<tr>
<th>Number of frequency bins</th>
<td>{{ spectrum.freqs|length }}</td>
</tr>
<tr>
<th>Frequency range</th>
<td>{{ '%.2f'|format(spectrum.freqs[0]) }} – {{ '%.2f'|format(spectrum.freqs[-1]) }} Hz</td>
</tr>
{%include '_general.html.jinja' %}
{%include '_acquisition.html.jinja' %}
{%include '_channels.html.jinja' %}
{%include '_frequencies.html.jinja' %}
{%include '_filters.html.jinja' %}
</table>
4 changes: 3 additions & 1 deletion mne/time_frequency/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,9 @@ def _repr_html_(self, caption=None):
inst_type_str = _get_instance_type_string(self)
units = [f"{ch_type}: {unit}" for ch_type, unit in self.units().items()]
t = _get_html_template("repr", "spectrum.html.jinja")
t = t.render(spectrum=self, inst_type=inst_type_str, units=units)
t = t.render(
inst=self, computed_from=inst_type_str, units=units, filenames=None
)
return t

def _check_values(self):
Expand Down
3 changes: 1 addition & 2 deletions tools/hooks/sync_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def _prettify_pin(pin):
f"- `{key} <{url}>`__{core_deps_pins[key.lower()]}"
for key, url in CORE_DEPS_URLS.items()
]
core_deps_rst = "\n" + "\n".join(core_deps_bullets) + "\n"

# rewrite the README file
lines = README_PATH.read_text("utf-8").splitlines()
Expand All @@ -92,7 +91,7 @@ def _prettify_pin(pin):
if line.strip() == BEGIN:
skip = True
out_lines.append(line)
out_lines.append(core_deps_rst)
out_lines.extend(["", *core_deps_bullets, ""])
if line.strip() == END:
skip = False
if not skip:
Expand Down
9 changes: 4 additions & 5 deletions tutorials/time-freq/10_spectrum_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@
raw.compute_psd()

# %%
# By default, the spectral estimation method will be the
# :footcite:t:`Welch1967` method for continuous data, and the multitaper
# method :footcite:`Slepian1978` for epoched or averaged data. This default can
# be overridden by passing ``method='welch'`` or ``method='multitaper'`` to the
# :meth:`~mne.io.Raw.compute_psd` method.
# By default, the spectral estimation method will be the :footcite:t:`Welch1967` method
# for continuous data, and the multitaper method :footcite:`Slepian1978` for epoched or
# averaged data. This default can be overridden by passing ``method='welch'`` or
# ``method='multitaper'`` to the :meth:`~mne.io.Raw.compute_psd` method.
#
# There are many other options available as well; for example we can compute a
# spectrum from a given span of times, for a chosen frequency range, and for a
Expand Down
Loading