Coverage for nilearn/reporting/_utils.py: 31%
13 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-16 12:39 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-16 12:39 +0200
1"""Utility functions that do not require matplotlib."""
3from collections import OrderedDict
5import pandas as pd
8def dataframe_to_html(df, precision, **kwargs):
9 """Make HTML table from provided dataframe.
11 Removes HTML5 non-compliant attributes (ex: `border`).
13 Parameters
14 ----------
15 df : pandas.Dataframe
16 Dataframe to be converted into HTML table.
18 precision : int
19 The display precision for float values in the table.
21 **kwargs : keyworded arguments
22 Supplies keyworded arguments for func: pandas.Dataframe.to_html()
24 Returns
25 -------
26 html_table : String
27 Code for HTML table.
29 """
30 with pd.option_context("display.precision", precision):
31 html_table = df.to_html(**kwargs)
32 html_table = html_table.replace('border="1" ', "")
33 return html_table.replace('class="dataframe"', 'class="pure-table"')
36def model_attributes_to_dataframe(model):
37 """Return dataframe with pertinent model attributes & information.
39 Parameters
40 ----------
41 model : Any masker object.
43 Returns
44 -------
45 attributes_df: pandas.DataFrame
46 DataFrame with the pertinent attributes of the model.
47 """
48 attributes_df = OrderedDict(
49 (
50 attr_name,
51 (
52 str(getattr(model, attr_name))
53 if isinstance(getattr(model, attr_name), dict)
54 else getattr(model, attr_name)
55 ),
56 )
57 for attr_name in model.get_params()
58 )
59 attributes_df = pd.DataFrame.from_dict(attributes_df, orient="index")
60 attributes_df.index.names = ["Parameter"]
61 attributes_df.columns = ["Value"]
62 return attributes_df