Skip to content

Table1

Bases: Reporter

Table1 is a common term used in epidemiology to describe a table that shows an overview of the baseline characteristics of a cohort. It contains the counts and percentages of the cohort that have each characteristic, for both boolean and value characteristics. In addition, summary statistics are provided for value characteristics (mean, std, median, min, max).

Table1 by default reports on all phenotypes in the cohort's characteristics, but a custom list of phenotypes can be provided to the execute() method. When using the default cohort.characteristics, the section structure defined on the cohort is preserved in the Table1 output for better organization and display.

Parameters:

Name Type Description Default
decimal_places

Number of decimal places to round to. Default: 1

required
include_component_phenotypes_level

When set to an integer, component (child) phenotypes are expanded inline beneath each parent phenotype, indented according to their nesting depth. None (default) disables expansion. Set to a large number (e.g. 100) to include all levels.

None
Source code in phenex/reporting/table1.py
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
class Table1(Reporter):
    """
    Table1 is a common term used in epidemiology to describe a table that shows an overview of the baseline characteristics of a cohort. It contains the counts and percentages of the cohort that have each characteristic, for both boolean and value characteristics. In addition, summary statistics are provided for value characteristics (mean, std, median, min, max).

    Table1 by default reports on all phenotypes in the cohort's characteristics, but a custom list of phenotypes can be provided to the execute() method. When using the default cohort.characteristics, the section structure defined on the cohort is preserved in the Table1 output for better organization and display.

    Parameters:
        decimal_places: Number of decimal places to round to. Default: 1
        include_component_phenotypes_level: When set to an integer, component
            (child) phenotypes are expanded inline beneath each parent phenotype,
            indented according to their nesting depth.  ``None`` (default) disables
            expansion.  Set to a large number (e.g. 100) to include all levels.
    """

    def __init__(self, include_component_phenotypes_level=None, **kwargs):
        super().__init__(**kwargs)
        self.include_component_phenotypes_level = include_component_phenotypes_level
        self.characteristic_sections = None

    # ------------------------------------------------------------------
    # Component-phenotype expansion helpers
    # ------------------------------------------------------------------

    def _expand_with_components(self, phenotypes):
        """Return a flat, ordered list that interleaves component phenotypes.

        Each top-level phenotype is followed (depth-first) by its children up
        to *include_component_phenotypes_level* levels deep.  Component phenotypes
        are wrapped in a :class:`_ComponentPhenotypeView` whose ``display_name``
        is prefixed with two spaces per nesting level so the hierarchy is visible
        in the final table.
        """
        result = []
        for p in phenotypes:
            result.append(p)
            self._collect_components(p, result, level=1)
        return result

    def _collect_components(self, phenotype, result, level):
        if level > self.include_component_phenotypes_level:
            return
        children = getattr(phenotype, "children", None) or []
        for child in children:
            indent = "\u00a0\u00a0" * level  # non-breaking spaces for visual indent
            view = _ComponentPhenotypeView(
                child, f"{indent}{child.display_name}", level=level
            )
            result.append(view)
            self._collect_components(child, result, level + 1)

    def execute(
        self, cohort: "Cohort", phenotypes: "Optional[Union[List, Dict]]" = None
    ) -> pd.DataFrame:
        self.cohort = cohort

        if phenotypes is None:
            self._phenotypes = cohort.characteristics
            self.characteristic_sections = getattr(
                cohort, "characteristic_sections", None
            )
        elif isinstance(phenotypes, dict):
            self.characteristic_sections = {
                section: [p.display_name for p in phenos]
                for section, phenos in phenotypes.items()
            }
            self._phenotypes = [p for phenos in phenotypes.values() for p in phenos]
        else:
            self._phenotypes = phenotypes
            self.characteristic_sections = None

        if len(self._phenotypes) == 0:
            logger.info("No phenotypes. table1 is empty")
            return pd.DataFrame()

        # Optionally expand each phenotype with its component children
        if self.include_component_phenotypes_level is not None:
            self._phenotypes = self._expand_with_components(self._phenotypes)

        self.cohort_names_in_order = [x.name for x in self._phenotypes]
        self.N = (
            cohort.index_table.filter(cohort.index_table.BOOLEAN == True)
            .select("PERSON_ID")
            .distinct()
            .count()
            .execute()
        )
        logger.debug("Starting with categorical columns for table1")
        self.df_categoricals = self._report_categorical_columns()
        logger.debug("Starting with boolean columns for table1")
        self.df_booleans = self._report_boolean_columns()
        logger.debug("Starting with value columns for table1")
        self.df_values = self._report_value_columns()
        logger.debug("Collecting value distributions for histogram visualization")
        self._value_distributions = self._collect_value_distributions()

        # add the full cohort size as the first row
        df_n = pd.DataFrame(
            {"N": [self.N], "inex_order": [-1], "_level": [0]}, index=["Cohort"]
        )
        # add percentage column
        dfs = [
            df
            for df in [df_n, self.df_booleans, self.df_values, self.df_categoricals]
            if df is not None
        ]
        if len(dfs) > 1:
            self.df = pd.concat(dfs)
        elif len(dfs) == 1:
            self.df = dfs[0]
        else:
            self.df = None
        if self.df is not None:
            self.df["Pct"] = 100 * self.df["N"] / self.N
            # reorder columns so N and Pct are first
            first_cols = ["N", "Pct"]
            column_order = first_cols + [
                x for x in self.df.columns if x not in first_cols
            ]
            self.df = self.df[column_order]
        logger.debug("Finished creating table1")

        self.df = self.df.reset_index()
        self.df.columns = ["Name"] + list(self.df.columns[1:])

        self.df = self.df.sort_values(by=["inex_order", "Name"])
        self.df = self.df.reset_index()[
            [x for x in self.df.columns if x not in ["index", "inex_order"]]
        ]
        # Strip the "NNNN_" sort-order prefix that BinPhenotype embeds in bin
        # labels (e.g. "Age group=0003_[30-40)" → "Age group=[30-40)").
        self.df["Name"] = self.df["Name"].str.replace(r"=\d{4}_", "=", regex=True)
        return self.df

    def _get_boolean_characteristics(self):
        return [x for x in self._phenotypes if x.output_display_type == "boolean"]

    def _get_value_characteristics(self):
        return [x for x in self._phenotypes if x.output_display_type == "value"]

    def _get_categorical_characteristics(self):
        return [x for x in self._phenotypes if x.output_display_type == "categorical"]

    def _get_boolean_count_for_phenotype(self, phenotype):
        result = (
            phenotype.table.select(["PERSON_ID", "BOOLEAN"])
            .distinct()["BOOLEAN"]
            .sum()
            .execute()
        )
        # Return 0 if result is None or NaN (no rows with BOOLEAN=True)
        return (
            0
            if result is None or (isinstance(result, float) and pd.isna(result))
            else int(result)
        )

    def _report_boolean_columns(self):
        # get list of all boolean columns
        boolean_phenotypes = self._get_boolean_characteristics()
        logger.debug(
            f"Found {len(boolean_phenotypes)} : {[x.name for x in boolean_phenotypes]}"
        )
        if len(boolean_phenotypes) == 0:
            return None
        # get count of 'Trues' in the boolean columns i.e. the phenotype counts
        df_t1 = pd.DataFrame()
        df_t1["N"] = [
            self._get_boolean_count_for_phenotype(phenotype)
            for phenotype in boolean_phenotypes
        ]
        df_t1.index = [x.display_name for x in boolean_phenotypes]
        df_t1["inex_order"] = [
            self.cohort_names_in_order.index(x.name) for x in boolean_phenotypes
        ]
        df_t1["_level"] = [getattr(x, "_level", 0) for x in boolean_phenotypes]
        return df_t1

    def _report_value_columns(self):
        value_phenotypes = self._get_value_characteristics()
        logger.debug(
            f"Found {len(value_phenotypes)} : {[x.name for x in value_phenotypes]}"
        )

        if len(value_phenotypes) == 0:
            return None

        names = []
        dfs = []
        for phenotype in value_phenotypes:
            _table = phenotype.table.select(["PERSON_ID", "VALUE"]).distinct()
            d = {
                "N": self._get_boolean_count_for_phenotype(phenotype),
                "Mean": _table["VALUE"].mean().execute(),
                "STD": _table["VALUE"].std().execute(),
                "Min": _table["VALUE"].min().execute(),
                "P10": _table["VALUE"].quantile(0.10).execute(),
                "P25": _table["VALUE"].quantile(0.25).execute(),
                "Median": _table["VALUE"].median().execute(),
                "P75": _table["VALUE"].quantile(0.75).execute(),
                "P90": _table["VALUE"].quantile(0.90).execute(),
                "Max": _table["VALUE"].max().execute(),
                "inex_order": self.cohort_names_in_order.index(phenotype.name),
                "_level": getattr(phenotype, "_level", 0),
            }
            dfs.append(pd.DataFrame.from_dict([d]))
            names.append(phenotype.display_name)
        if len(dfs) == 1:
            df = dfs[0]
        else:
            df = pd.concat(dfs)
        df.index = names
        return df

    def _report_categorical_columns(self):
        categorical_phenotypes = self._get_categorical_characteristics()
        logger.debug(
            f"Found {len(categorical_phenotypes)} : {[x.name for x in categorical_phenotypes]}"
        )
        if len(categorical_phenotypes) == 0:
            return None
        dfs = []
        names = []
        for phenotype in categorical_phenotypes:
            name = phenotype.display_name
            _table = phenotype.table.select(["PERSON_ID", "VALUE"])
            # Get counts for each category.
            # Keep the raw VALUE (which may carry a "NNNN_" sort prefix from
            # BinPhenotype) in the index so that the final sort_values("Name")
            # in execute() orders bins correctly. The prefix is stripped there.
            cat_counts = (
                _table.distinct().group_by("VALUE").aggregate(N=_.count()).execute()
            )
            cat_counts.index = [
                f"{name}={v if v is not None else 'None'}" for v in cat_counts["VALUE"]
            ]
            _df = pd.DataFrame(cat_counts["N"])
            _df["inex_order"] = self.cohort_names_in_order.index(phenotype.name)
            _df["_level"] = getattr(phenotype, "_level", 0)
            dfs.append(_df)
            names.extend(cat_counts.index)
        if len(dfs) == 1:
            df = dfs[0]
        else:
            df = pd.concat(dfs)
        df.index = names
        return df

    def _collect_value_distributions(self):
        """Compute KDE curves for numeric phenotypes.

        Stores ``{"x": [...], "y": [...]}`` per phenotype where *y* is
        normalised so the peak equals 100.  This is far more compact than
        raw patient-level values and avoids binning decisions at display time.
        """
        import numpy as np
        from scipy.stats import gaussian_kde

        N_POINTS = 200
        PADDING = 0.10  # 10% range padding on each side

        value_phenotypes = self._get_value_characteristics()
        distributions = {}
        for phenotype in value_phenotypes:
            try:
                values = np.array(
                    phenotype.table.select(["PERSON_ID", "VALUE"])
                    .distinct()["VALUE"]
                    .execute()
                    .dropna()
                    .tolist(),
                    dtype=float,
                )
                if len(values) < 2:
                    continue
                # For integer-valued data, widen the bandwidth to avoid
                # spiky peaks at each integer and produce smooth plateaus.
                is_integer = np.allclose(values, np.round(values))
                bw = 1.5 if is_integer else None
                kde = gaussian_kde(values, bw_method=bw)
                lo, hi = float(values.min()), float(values.max())
                pad = (hi - lo) * PADDING if hi > lo else 1.0
                x = np.linspace(lo - pad, hi + pad, N_POINTS)
                y = kde(x)
                y = y / y.max() * 100  # normalise peak to 100
                distributions[phenotype.display_name] = {
                    "x": np.round(x, 4).tolist(),
                    "y": np.round(y, 2).tolist(),
                }
            except Exception:
                pass
        return distributions

    def get_pretty_display(self) -> pd.DataFrame:
        """
        Return a formatted version of the Table1 results for display.

        Formats numeric columns and converts counts to strings to avoid NaN display.

        Returns:
            pd.DataFrame: Formatted copy of the results
        """
        # Create a copy to avoid modifying the original
        pretty_df = self.df.copy()

        # Drop the internal _level column from display output
        pretty_df = pretty_df.drop(columns=["_level"], errors="ignore")

        # cast counts to integer and to str, so that we can display without 'NaNs'
        pretty_df["N"] = pretty_df["N"].astype("Int64").astype(str)

        pretty_df = pretty_df.round(self.decimal_places)

        to_prettify = [
            "Pct",
            "Mean",
            "STD",
            "Min",
            "P10",
            "P25",
            "Median",
            "P75",
            "P90",
            "Max",
        ]
        for column in to_prettify:
            if column in pretty_df.columns:
                pretty_df[column] = pretty_df[column].astype(str)

        pretty_df = pretty_df.replace("<NA>", "").replace("nan", "")

        return pretty_df

    def to_json(self, filename: str) -> str:
        """Export Table1 to JSON, including section metadata when available."""
        import json
        from pathlib import Path

        if not hasattr(self, "df"):
            raise AttributeError("Call execute() first before calling to_json().")

        filepath = Path(filename)
        if filepath.suffix != ".json":
            filepath = filepath.with_suffix(".json")
        filepath.parent.mkdir(parents=True, exist_ok=True)

        payload = {
            "reporter_type": self.__class__.__name__,
            "rows": self.df.to_dict(orient="records"),
        }
        if self.characteristic_sections:
            payload["sections"] = self.characteristic_sections

        if hasattr(self, "_value_distributions") and self._value_distributions:
            payload["kdes"] = self._value_distributions

        with filepath.open("w") as f:
            json.dump(payload, f, indent=2, default=str)

        return str(filepath.absolute())

    def to_excel(self, filename: str) -> str:
        """Export Table1 to Excel, applying progressive gray fills for component rows."""
        import openpyxl
        from openpyxl.styles import PatternFill
        from pathlib import Path

        if not hasattr(self, "df"):
            raise AttributeError("Call execute() first before calling to_excel().")

        filepath = Path(filename)
        if filepath.suffix != ".xlsx":
            filepath = filepath.with_suffix(".xlsx")
        filepath.parent.mkdir(parents=True, exist_ok=True)

        # Write pretty display (strips _level)
        pretty_df = self.get_pretty_display()
        pretty_df.to_excel(filepath, index=False)

        # Apply gray fills based on _level if present
        if "_level" in self.df.columns:
            wb = openpyxl.load_workbook(filepath)
            ws = wb.active
            # header row is row 1; data starts at row 2
            for row_idx, level in enumerate(
                self.df["_level"].fillna(0).astype(int).values, start=2
            ):
                hex_color = self._level_to_gray_hex(level)
                if hex_color:
                    fill = PatternFill(
                        start_color=hex_color, end_color=hex_color, fill_type="solid"
                    )
                    for cell in ws[row_idx]:
                        cell.fill = fill
            wb.save(filepath)

        return str(filepath.absolute())

    @staticmethod
    def _level_to_gray_hex(level: int) -> str:
        """Return a 6-char hex fill color for a component nesting level (empty = no fill)."""
        if level <= 0:
            return ""
        value = max(235 - 20 * (level - 1), 100)
        return f"{value:02X}{value:02X}{value:02X}"

name property

Name of the reporter, used for identification and output file naming.

get_pretty_display()

Return a formatted version of the Table1 results for display.

Formats numeric columns and converts counts to strings to avoid NaN display.

Returns:

Type Description
DataFrame

pd.DataFrame: Formatted copy of the results

Source code in phenex/reporting/table1.py
def get_pretty_display(self) -> pd.DataFrame:
    """
    Return a formatted version of the Table1 results for display.

    Formats numeric columns and converts counts to strings to avoid NaN display.

    Returns:
        pd.DataFrame: Formatted copy of the results
    """
    # Create a copy to avoid modifying the original
    pretty_df = self.df.copy()

    # Drop the internal _level column from display output
    pretty_df = pretty_df.drop(columns=["_level"], errors="ignore")

    # cast counts to integer and to str, so that we can display without 'NaNs'
    pretty_df["N"] = pretty_df["N"].astype("Int64").astype(str)

    pretty_df = pretty_df.round(self.decimal_places)

    to_prettify = [
        "Pct",
        "Mean",
        "STD",
        "Min",
        "P10",
        "P25",
        "Median",
        "P75",
        "P90",
        "Max",
    ]
    for column in to_prettify:
        if column in pretty_df.columns:
            pretty_df[column] = pretty_df[column].astype(str)

    pretty_df = pretty_df.replace("<NA>", "").replace("nan", "")

    return pretty_df

to_csv(filename)

Export reporter results to CSV format.

Default implementation exports self.df if it exists. Subclasses can override for custom behavior. If pretty_display=True, formats the DataFrame before export.

Parameters:

Name Type Description Default
filename str

Path to the output file (relative or absolute, with or without .csv extension)

required

Returns:

Name Type Description
str str

Full path to the created file

Raises:

Type Description
AttributeError

If self.df is not defined (call execute() first)

Source code in phenex/reporting/reporter.py
def to_csv(self, filename: str) -> str:
    """
    Export reporter results to CSV format.

    Default implementation exports self.df if it exists. Subclasses can override for custom behavior. If pretty_display=True, formats the DataFrame before export.

    Args:
        filename: Path to the output file (relative or absolute, with or without .csv extension)

    Returns:
        str: Full path to the created file

    Raises:
        AttributeError: If self.df is not defined (call execute() first)
    """
    if not hasattr(self, "df"):
        raise AttributeError(
            f"{self.__class__.__name__} does not have a 'df' attribute. "
            "Call execute() first or implement a custom to_csv() method."
        )

    # Convert to Path object and ensure .csv extension
    filepath = Path(filename)
    if filepath.suffix != ".csv":
        filepath = filepath.with_suffix(".csv")

    # Create parent directories if needed
    filepath.parent.mkdir(parents=True, exist_ok=True)

    # Apply pretty display formatting
    df_to_export = self.get_pretty_display()

    # Export to CSV
    df_to_export.to_csv(filepath, index=False)

    return str(filepath.absolute())

to_excel(filename)

Export Table1 to Excel, applying progressive gray fills for component rows.

Source code in phenex/reporting/table1.py
def to_excel(self, filename: str) -> str:
    """Export Table1 to Excel, applying progressive gray fills for component rows."""
    import openpyxl
    from openpyxl.styles import PatternFill
    from pathlib import Path

    if not hasattr(self, "df"):
        raise AttributeError("Call execute() first before calling to_excel().")

    filepath = Path(filename)
    if filepath.suffix != ".xlsx":
        filepath = filepath.with_suffix(".xlsx")
    filepath.parent.mkdir(parents=True, exist_ok=True)

    # Write pretty display (strips _level)
    pretty_df = self.get_pretty_display()
    pretty_df.to_excel(filepath, index=False)

    # Apply gray fills based on _level if present
    if "_level" in self.df.columns:
        wb = openpyxl.load_workbook(filepath)
        ws = wb.active
        # header row is row 1; data starts at row 2
        for row_idx, level in enumerate(
            self.df["_level"].fillna(0).astype(int).values, start=2
        ):
            hex_color = self._level_to_gray_hex(level)
            if hex_color:
                fill = PatternFill(
                    start_color=hex_color, end_color=hex_color, fill_type="solid"
                )
                for cell in ws[row_idx]:
                    cell.fill = fill
        wb.save(filepath)

    return str(filepath.absolute())

to_html(filename)

Export reporter results to HTML format.

Default implementation exports self.df if it exists. Subclasses can override for custom behavior. If pretty_display=True, formats the DataFrame before export.

Parameters:

Name Type Description Default
filename str

Path to the output file (relative or absolute, with or without .html extension)

required

Returns:

Name Type Description
str str

Full path to the created file

Raises:

Type Description
AttributeError

If self.df is not defined (call execute() first)

Source code in phenex/reporting/reporter.py
def to_html(self, filename: str) -> str:
    """
    Export reporter results to HTML format.

    Default implementation exports self.df if it exists. Subclasses can override for custom behavior. If pretty_display=True, formats the DataFrame before export.

    Args:
        filename: Path to the output file (relative or absolute, with or without .html extension)

    Returns:
        str: Full path to the created file

    Raises:
        AttributeError: If self.df is not defined (call execute() first)
    """
    if not hasattr(self, "df"):
        raise AttributeError(
            f"{self.__class__.__name__} does not have a 'df' attribute. "
            "Call execute() first or implement a custom to_html() method."
        )

    # Convert to Path object and ensure .html extension
    filepath = Path(filename)
    if filepath.suffix != ".html":
        filepath = filepath.with_suffix(".html")

    # Create parent directories if needed
    filepath.parent.mkdir(parents=True, exist_ok=True)

    # Apply pretty display formatting
    df_to_export = self.get_pretty_display()

    # Export to HTML
    df_to_export.to_html(filepath, index=False)

    return str(filepath.absolute())

to_json(filename)

Export Table1 to JSON, including section metadata when available.

Source code in phenex/reporting/table1.py
def to_json(self, filename: str) -> str:
    """Export Table1 to JSON, including section metadata when available."""
    import json
    from pathlib import Path

    if not hasattr(self, "df"):
        raise AttributeError("Call execute() first before calling to_json().")

    filepath = Path(filename)
    if filepath.suffix != ".json":
        filepath = filepath.with_suffix(".json")
    filepath.parent.mkdir(parents=True, exist_ok=True)

    payload = {
        "reporter_type": self.__class__.__name__,
        "rows": self.df.to_dict(orient="records"),
    }
    if self.characteristic_sections:
        payload["sections"] = self.characteristic_sections

    if hasattr(self, "_value_distributions") and self._value_distributions:
        payload["kdes"] = self._value_distributions

    with filepath.open("w") as f:
        json.dump(payload, f, indent=2, default=str)

    return str(filepath.absolute())

to_markdown(filename)

Export reporter results to Markdown format.

Default implementation exports self.df if it exists. Subclasses can override for custom behavior. If pretty_display=True, formats the DataFrame before export.

Parameters:

Name Type Description Default
filename str

Path to the output file (relative or absolute, with or without .md extension)

required

Returns:

Name Type Description
str str

Full path to the created file

Raises:

Type Description
AttributeError

If self.df is not defined (call execute() first)

ImportError

If tabulate is not installed (required for df.to_markdown())

Source code in phenex/reporting/reporter.py
def to_markdown(self, filename: str) -> str:
    """
    Export reporter results to Markdown format.

    Default implementation exports self.df if it exists. Subclasses can override for custom behavior. If pretty_display=True, formats the DataFrame before export.

    Args:
        filename: Path to the output file (relative or absolute, with or without .md extension)

    Returns:
        str: Full path to the created file

    Raises:
        AttributeError: If self.df is not defined (call execute() first)
        ImportError: If tabulate is not installed (required for df.to_markdown())
    """
    if not hasattr(self, "df"):
        raise AttributeError(
            f"{self.__class__.__name__} does not have a 'df' attribute. "
            "Call execute() first or implement a custom to_markdown() method."
        )

    # Convert to Path object and ensure .md extension
    filepath = Path(filename)
    if filepath.suffix != ".md":
        filepath = filepath.with_suffix(".md")

    # Create parent directories if needed
    filepath.parent.mkdir(parents=True, exist_ok=True)

    # Apply pretty display formatting
    df_to_export = self.get_pretty_display()

    # Export to Markdown (requires tabulate package)
    try:
        markdown_content = df_to_export.to_markdown(index=False)
        filepath.write_text(markdown_content)
    except ImportError:
        raise ImportError(
            "tabulate is required for Markdown export. Install with: pip install tabulate"
        )

    return str(filepath.absolute())

to_word(filename)

Export reporter results to Microsoft Word format.

Default implementation exports self.df as a simple table if it exists. Subclasses can override for custom formatting (headers, styling, etc). If pretty_display=True, formats the DataFrame before export using get_pretty_display().

Parameters:

Name Type Description Default
filename str

Path to the output file (relative or absolute, with or without .docx extension)

required

Returns:

Name Type Description
str str

Full path to the created file

Raises:

Type Description
AttributeError

If self.df is not defined (call execute() first)

ImportError

If python-docx is not installed

Source code in phenex/reporting/reporter.py
def to_word(self, filename: str) -> str:
    """
    Export reporter results to Microsoft Word format.

    Default implementation exports self.df as a simple table if it exists.
    Subclasses can override for custom formatting (headers, styling, etc).
    If pretty_display=True, formats the DataFrame before export using get_pretty_display().

    Args:
        filename: Path to the output file (relative or absolute, with or without .docx extension)

    Returns:
        str: Full path to the created file

    Raises:
        AttributeError: If self.df is not defined (call execute() first)
        ImportError: If python-docx is not installed
    """
    if not hasattr(self, "df"):
        raise AttributeError(
            f"{self.__class__.__name__} does not have a 'df' attribute. "
            "Call execute() first or implement a custom to_word() method."
        )

    try:
        from docx import Document
    except ImportError:
        raise ImportError(
            "python-docx is required for Word export. Install with: pip install python-docx"
        )

    # Convert to Path object and ensure .docx extension
    filepath = Path(filename)
    if filepath.suffix != ".docx":
        filepath = filepath.with_suffix(".docx")

    # Create parent directories if needed
    filepath.parent.mkdir(parents=True, exist_ok=True)

    # Apply pretty display formatting
    df_to_export = self.get_pretty_display()

    # Create Word document with table
    doc = Document()

    # Add table (rows + 1 for header)
    table = doc.add_table(
        rows=len(df_to_export) + 1, cols=len(df_to_export.columns)
    )
    table.style = "Light Grid Accent 1"

    # Add header row
    for col_idx, column_name in enumerate(df_to_export.columns):
        table.rows[0].cells[col_idx].text = str(column_name)

    # Add data rows
    for row_idx, (_, row_data) in enumerate(df_to_export.iterrows(), start=1):
        for col_idx, value in enumerate(row_data):
            table.rows[row_idx].cells[col_idx].text = str(value)

    # Save document
    doc.save(str(filepath))

    return str(filepath.absolute())