Skip to content

Commit

Permalink
Merge branch 'main' into doc/add-usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies authored Aug 19, 2024
2 parents 56d1e84 + e0a30e6 commit ef788d2
Show file tree
Hide file tree
Showing 15 changed files with 959 additions and 23 deletions.
12 changes: 12 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
23.2.2 (August 19, 2024)
========================
Bug-fix release in the 23.2.x series.

CHANGES
-------

**Full Changelog**: https://github.com/nipreps/nireports/compare/23.2.1...23.2.2

* ENH: Support PNGs and JPGs in reportlets (#126)


23.2.1 (May 07, 2024)
=====================
Bug-fix release in the 23.2.x series.
Expand Down
2 changes: 1 addition & 1 deletion nireports/assembler/data/rating-widget/bootstrap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defaults:
endpoint: "https://localhost:8000/"
access_token: <secret_token>
settings:
style: 'width: 30%; top: 100px; left: 65%;'
style: 'width: 30%; top: 100px; left: 65%; max-height: 85%; overflow-y: auto;'
navbar_label: Rating widget
mintime: 10
components:
Expand Down
2 changes: 1 addition & 1 deletion nireports/assembler/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class Report:
... dirs_exist_ok=True,
... )
>>> REPORT_BASELINE_LENGTH = 41770
>>> RATING_WIDGET_LENGTH = 83308
>>> RATING_WIDGET_LENGTH = 83343
Examples
Expand Down
47 changes: 36 additions & 11 deletions nireports/assembler/reportlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,21 @@
from nireports.assembler import data
from nireports.assembler.misc import dict2html, read_crashfile

SVG_SNIPPET = [
"""\
IMG_SNIPPET = """\
<div class="reportlet">
<object class="svg-reportlet" type="image/svg+xml" data="./{name}" style="{style}">
Problem loading figure {name}. If the link below works, please try \
reloading the report in your browser.</object>
<img class="{ext}-reportlet" src="./{name}" style="{style}" />
</div>
<small>Get figure file: <a href="./{name}" target="_blank">{name}</a></small>
""",
"""\
"""

SVG_SNIPPET = """\
<div class="reportlet">
<img class="svg-reportlet" src="./{name}" style="{style}" />
<object class="{ext}-reportlet" type="image/{ext}+xml" data="./{name}" style="{style}">
Problem loading figure {name}. If the link below works, please try \
reloading the report in your browser.</object>
</div>
<small>Get figure file: <a href="./{name}" target="_blank">{name}</a></small>
""",
]
"""

METADATA_ACCORDION_BLOCK = """\
<div class="accordion accordion-flush" id="{metadata_id}">
Expand Down Expand Up @@ -265,10 +264,36 @@ def __init__(self, layout, config=None, out_dir=None, bids_filters=None, metadat
style = {"width": "100%"} if is_static else {}
style.update(config.get("style", {}))

contents = SVG_SNIPPET[is_static].format(
snippet = IMG_SNIPPET if is_static else SVG_SNIPPET
contents = snippet.format(
ext=ext[1:],
name=html_anchor,
style="; ".join(f"{k}: {v}" for k, v in style.items()),
)
elif ext in (".png", ".jpg", ".jpeg"):
entities = dict(bidsfile.entities)
if desc_text:
desc_text = desc_text.format(**entities)

try:
html_anchor = src.relative_to(out_dir)
except ValueError:
html_anchor = src.relative_to(Path(layout.root))
dst = out_dir / html_anchor
dst.parent.mkdir(parents=True, exist_ok=True)
copyfile(src, dst, copy=True, use_hardlink=True)

style = {"width": "100%"}
style.update(config.get("style", {}))

snippet = IMG_SNIPPET
contents = snippet.format(
ext=ext[1:],
name=html_anchor,
style="; ".join(f"{k}: {v}" for k, v in style.items()),
)
else:
raise RuntimeError(f"Unsupported file extension: {ext}")

if contents:
self.components.append((contents, desc_text))
Expand Down
8 changes: 8 additions & 0 deletions nireports/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
test_workdir = os.getenv("TEST_WORK_DIR")


@pytest.fixture(scope="session", autouse=True)
def legacy_printoptions():
from packaging.version import Version

if Version(np.__version__) >= Version("1.22"):
np.set_printoptions(legacy="1.21")


@pytest.fixture(autouse=True)
def expand_namespace(doctest_namespace):
doctest_namespace["PY_VERSION"] = version_info
Expand Down
7 changes: 6 additions & 1 deletion nireports/interfaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@

from nireports.interfaces.fmri import FMRISummary
from nireports.interfaces.mosaic import PlotContours, PlotMosaic, PlotSpikes
from nireports.interfaces.nuisance import CompCorVariancePlot, ConfoundsCorrelationPlot
from nireports.interfaces.nuisance import (
CompCorVariancePlot,
ConfoundsCorrelationPlot,
RaincloudPlot,
)

__all__ = (
"CompCorVariancePlot",
"ConfoundsCorrelationPlot",
"RaincloudPlot",
"FMRISummary",
"PlotContours",
"PlotMosaic",
Expand Down
108 changes: 107 additions & 1 deletion nireports/interfaces/nuisance.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
)
from nipype.utils.filemanip import fname_presuffix

from nireports.reportlets.nuisance import confounds_correlation_plot
from nireports.reportlets.nuisance import confounds_correlation_plot, plot_raincloud
from nireports.reportlets.xca import compcor_variance_plot


Expand Down Expand Up @@ -142,3 +142,109 @@ def _run_interface(self, runtime):
ignore_initial_volumes=self.inputs.ignore_initial_volumes,
)
return runtime


class _RaincloudPlotInputSpec(BaseInterfaceInputSpec):
data_file = File(exists=True, mandatory=True, desc="File containing the data")
out_file = traits.Either(None, File, value=None, usedefault=True, desc="Path to save plot")
group_name = traits.Str(
"group_name",
mandatory=True,
desc="Group name of interest",
)
feature = traits.Str(
"feature",
mandatory=True,
desc="Feature of interest",
)
palette = traits.Str(
"Set2",
usedefault=True,
desc="Color palette name",
)
orient = traits.Str(
"v",
usedefault=True,
desc="Orientation",
)
density = traits.Bool(
True,
usedefault=True,
desc="``True`` to plot the density",
)
upper_limit_value = traits.Float(
None,
usedefault=True,
desc="Upper limit value over which any value in the data will be styled "
"with a different style",
)
upper_limit_color = traits.Str(
"gray",
usedefault=True,
desc="Lower limit value under which any value in the data will be styled "
"with a different style",
)
lower_limit_value = traits.Float(
None,
usedefault=True,
desc="",
)
lower_limit_color = traits.Str(
"gray",
usedefault=True,
desc="Color name to represent values under ``lower_limit_value``",
)
limit_offset = traits.Float(
None,
usedefault=True,
desc="Offset to plot the values over/under the upper/lower limit values",
)
mark_nans = traits.Bool(
True,
usedefault=True,
desc="``True`` to plot NaNs as dots. ``nans_values`` must be provided if True",
)
nans_value = traits.Float(
None,
usedefault=True,
desc="Value to use for NaN values`",
)
nans_color = traits.Str(
"black",
usedefault=True,
desc="Color name to represent NaN values",
)


class _RaincloudPlotOutputSpec(TraitedSpec):
out_file = File(exists=True, desc="Path to saved plot")


class RaincloudPlot(SimpleInterface):
"""Plot a raincloud of values."""

input_spec = _RaincloudPlotInputSpec
output_spec = _RaincloudPlotOutputSpec

def _run_interface(self, runtime, **kwargs):
if self.inputs.out_file is None:
self._results["out_file"] = fname_presuffix(
self.inputs.data_file,
suffix="_raincloud.svg",
use_ext=False,
newpath=runtime.cwd,
)
else:
self._results["out_file"] = self.inputs.out_file
plot_raincloud(
data_file=self.inputs.data_file,
group_name=self.inputs.group_name,
feature=self.inputs.feature,
palette=self.inputs.palette,
orient=self.inputs.orient,
density=self.inputs.density,
mark_nans=self.inputs.mark_nans,
output_file=self._results["out_file"],
**kwargs,
)
return runtime
Loading

0 comments on commit ef788d2

Please sign in to comment.