File size: 5,481 Bytes
5f38e95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
r"""Structured tables for reports: CSV and LaTeX.

Produces booktabs-style LaTeX so the output drops cleanly into a manuscript
(``\\usepackage{booktabs}`` plus the snippet from :func:`summary_table_latex`
and no further tweaks).
"""

from __future__ import annotations

import csv
from pathlib import Path
from typing import TYPE_CHECKING, Sequence

import numpy as np

if TYPE_CHECKING:                                           # pragma: no cover
    from cdpr.recording.replay import Experiment


# ---------------------------------------------------------------------------
# Channel summaries (one row per channel)
# ---------------------------------------------------------------------------

def _channel_rows(exp: "Experiment") -> list[tuple[str, float, float, float, float]]:
    """Compute (name, mean, std, min, max) for the main scalar channels."""
    pos_norm = np.linalg.norm(exp.positions, axis=-1)
    vel_norm = np.linalg.norm(exp.linear_velocities, axis=-1)
    ang_norm = np.linalg.norm(exp.angular_velocities, axis=-1)
    rows: list[tuple[str, float, float, float, float]] = [
        (r"position magnitude [m]",
         float(pos_norm.mean()), float(pos_norm.std()),
         float(pos_norm.min()), float(pos_norm.max())),
        (r"linear speed [m/s]",
         float(vel_norm.mean()), float(vel_norm.std()),
         float(vel_norm.min()), float(vel_norm.max())),
        (r"angular speed [rad/s]",
         float(ang_norm.mean()), float(ang_norm.std()),
         float(ang_norm.min()), float(ang_norm.max())),
    ]
    if not np.isnan(exp.condition_numbers).all():
        kappa = exp.condition_numbers[np.isfinite(exp.condition_numbers)]
        if kappa.size:
            rows.append((
                r"structure-matrix $\kappa_2(\mathbf{W})$",
                float(kappa.mean()), float(kappa.std()),
                float(kappa.min()), float(kappa.max()),
            ))
    return rows


def summary_table_csv(exp: "Experiment", path: str | Path) -> Path:
    """Write a per-channel summary CSV with columns: channel, mean, std, min, max."""
    p = Path(path)
    p.parent.mkdir(parents=True, exist_ok=True)
    with p.open("w", newline="") as f:
        w = csv.writer(f)
        w.writerow(["channel", "mean", "std", "min", "max"])
        for row in _channel_rows(exp):
            w.writerow([row[0]] + [f"{x:.6e}" for x in row[1:]])
    return p


def summary_table_latex(
    exp: "Experiment", path: str | Path, *, label: str = "tab:summary", caption: str = "",
) -> Path:
    """Write a booktabs LaTeX table with the same channel summary."""
    p = Path(path)
    p.parent.mkdir(parents=True, exist_ok=True)
    rows = _channel_rows(exp)
    lines: list[str] = [
        r"\begin{table}[!htb]",
        r"  \centering",
        fr"  \caption{{{caption}}}",
        fr"  \label{{{label}}}",
        r"  \begin{tabular}{lrrrr}",
        r"    \toprule",
        r"    channel & mean & std & min & max \\",
        r"    \midrule",
    ]
    for name, mean, std, lo, hi in rows:
        lines.append(
            fr"    {name} & {mean:.4g} & {std:.4g} & {lo:.4g} & {hi:.4g} \\"
        )
    lines += [
        r"    \bottomrule",
        r"  \end{tabular}",
        r"\end{table}",
        "",
    ]
    p.write_text("\n".join(lines))
    return p


# ---------------------------------------------------------------------------
# Cable-level table (one row per cable)
# ---------------------------------------------------------------------------

def cable_summary_table(
    exp: "Experiment", path: str | Path, *, fmt: str = "csv",
    label: str = "tab:cables", caption: str = "",
) -> Path:
    """Per-cable summary (tension and length statistics) in CSV or LaTeX."""
    p = Path(path)
    p.parent.mkdir(parents=True, exist_ok=True)
    m = exp.cable_tensions.shape[1]
    cable_rows: list[tuple[int, float, float, float, float, float]] = []
    for i in range(m):
        T = exp.cable_tensions[:, i]
        L = exp.cable_lengths[:, i]
        cable_rows.append((
            i + 1,
            float(T.mean()), float(T.min()), float(T.max()),
            float(L.mean()), float(L.max() - L.min()),
        ))

    if fmt == "csv":
        with p.open("w", newline="") as f:
            w = csv.writer(f)
            w.writerow(["cable", "T_mean [N]", "T_min [N]", "T_max [N]",
                        "L_mean [m]", "L_range [m]"])
            for row in cable_rows:
                w.writerow([row[0]] + [f"{x:.6e}" for x in row[1:]])
        return p

    if fmt == "latex":
        lines: list[str] = [
            r"\begin{table}[!htb]",
            r"  \centering",
            fr"  \caption{{{caption}}}",
            fr"  \label{{{label}}}",
            r"  \begin{tabular}{rrrrrr}",
            r"    \toprule",
            r"    cable $i$ & $\bar T_i$ [N] & $T_{i,\min}$ [N] & $T_{i,\max}$ [N]"
            r" & $\bar \ell_i$ [m] & $\ell_{i,\max} - \ell_{i,\min}$ [m] \\",
            r"    \midrule",
        ]
        for i, t_mean, t_min, t_max, l_mean, l_range in cable_rows:
            lines.append(
                fr"    {i} & {t_mean:.4g} & {t_min:.4g} & {t_max:.4g} & "
                fr"{l_mean:.4g} & {l_range:.4g} \\"
            )
        lines += [
            r"    \bottomrule",
            r"  \end{tabular}",
            r"\end{table}",
            "",
        ]
        p.write_text("\n".join(lines))
        return p

    raise ValueError(f"Unsupported table format {fmt!r}; use 'csv' or 'latex'.")