File size: 7,627 Bytes
6977b53 0a3f2cf 6977b53 0a3f2cf 6977b53 0a3f2cf 6977b53 0a3f2cf 6977b53 | 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 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 | // Cross-episode statistics for per-episode-folder datasets. Each folder is a
// self-contained 1-episode v3 dataset, so whole-dataset numbers must be
// aggregated client-side:
// * episode lengths — one tiny meta/info.json fetch per folder (cheap,
// done automatically when the Statistics tab opens in folder mode);
// * tactile aggregates — needs every folder's data parquet (MBs each), so
// it is opt-in behind a button (see TactileAggregate in tactile-panel).
import { authHeaders } from "./auth";
import { hubResolveUrl } from "./repoRef";
import { fetchParquetFile, readParquetAsObjects } from "./parquetUtils";
import type {
EpisodeLengthStats,
EpisodeLengthInfo,
} from "@/app/[org]/[dataset]/[episode]/fetch-data";
const DATASET_URL =
process.env.DATASET_URL || "https://huggingface.co/datasets";
function folderUrl(repoId: string, folder: string, rel: string): string {
return hubResolveUrl(DATASET_URL, repoId, `${folder}/${rel}`);
}
type FolderInfo = {
folder: string;
fps: number;
frames: number;
features: Record<string, { dtype: string; shape: number[] }>;
};
const infoCache = new Map<string, Promise<FolderInfo | null>>();
async function loadFolderInfo(
repoId: string,
folder: string,
): Promise<FolderInfo | null> {
const key = `${repoId}/${folder}`;
const hit = infoCache.get(key);
if (hit) return hit;
const p = (async () => {
try {
const res = await fetch(folderUrl(repoId, folder, "meta/info.json"), {
headers: authHeaders(),
});
if (!res.ok) return null;
const j = await res.json();
return {
folder,
fps: Number(j.fps) || 30,
frames: Number(j.total_frames) || 0,
features: j.features ?? {},
};
} catch {
return null;
}
})();
infoCache.set(key, p);
return p;
}
function buildHistogram(
lengths: number[],
): { binLabel: string; count: number }[] {
if (!lengths.length) return [];
const min = Math.min(...lengths);
const max = Math.max(...lengths);
if (max - min < 1e-9) {
return [{ binLabel: `${min.toFixed(1)}s`, count: lengths.length }];
}
const nBins = Math.min(6, lengths.length);
const w = (max - min) / nBins;
return Array.from({ length: nBins }, (_, b) => {
const lo = min + b * w;
const hi = lo + w;
const count = lengths.filter(
(v) => v >= lo && (b === nBins - 1 ? v <= hi : v < hi),
).length;
return { binLabel: `${lo.toFixed(1)}–${hi.toFixed(1)}s`, count };
});
}
export async function loadFolderEpisodeLengths(
repoId: string,
folders: string[],
): Promise<{
stats: EpisodeLengthStats;
totalFrames: number;
fps: number;
} | null> {
const infos = (
await Promise.all(folders.map((f) => loadFolderInfo(repoId, f)))
).filter((x): x is FolderInfo => x !== null);
if (!infos.length) return null;
const fps = infos[0].fps || 30;
const round1 = (v: number) => Math.round(v * 10) / 10;
const all: EpisodeLengthInfo[] = infos.map((inf, i) => ({
episodeIndex: i,
frames: inf.frames,
lengthSeconds: round1(inf.frames / (inf.fps || fps)),
}));
const lengths = all.map((e) => e.lengthSeconds);
const mean = lengths.reduce((a, b) => a + b, 0) / lengths.length;
const sorted = [...lengths].sort((a, b) => a - b);
const median =
sorted.length % 2
? sorted[(sorted.length - 1) / 2]
: (sorted[sorted.length / 2 - 1] + sorted[sorted.length / 2]) / 2;
const std = Math.sqrt(
lengths.reduce((a, b) => a + (b - mean) ** 2, 0) / lengths.length,
);
const byLen = [...all].sort((a, b) => a.lengthSeconds - b.lengthSeconds);
return {
stats: {
shortestEpisodes: byLen.slice(0, 5),
longestEpisodes: byLen.slice(-5).reverse(),
allEpisodeLengths: all,
meanEpisodeLength: round1(mean),
medianEpisodeLength: round1(median),
stdEpisodeLength: round1(std),
episodeLengthHistogram: buildHistogram(lengths),
},
totalFrames: infos.reduce((a, b) => a + b.frames, 0),
fps,
};
}
// ---- tactile aggregation (opt-in, heavy) -----------------------------------
const CONTACT_THR_N = 0.05;
export type FolderTactileRow = {
channel: string;
peak: number;
peakFolder: string;
peakT: number;
meanContactPct: number;
maxSumFz: number;
episodes: number;
};
type PerChannelAcc = {
peak: number;
peakFolder: string;
peakT: number;
contactPcts: number[];
maxSumFz: number;
};
export async function computeFolderTactileAggregate(
repoId: string,
folders: string[],
onProgress: (done: number, total: number, folder: string) => void,
): Promise<FolderTactileRow[]> {
const acc = new Map<string, PerChannelAcc>();
for (let i = 0; i < folders.length; i++) {
const folder = folders[i];
onProgress(i, folders.length, folder);
const info = await loadFolderInfo(repoId, folder);
if (!info) continue;
const sensorCols = Object.entries(info.features)
.filter(
([k, f]) =>
k.startsWith("observation.sensors.") && (f.shape?.length ?? 0) >= 2,
)
.map(([k]) => k);
if (!sensorCols.length) continue;
let rows: Record<string, unknown>[] = [];
try {
const buf = await fetchParquetFile(
folderUrl(repoId, folder, "data/chunk-000/file-000.parquet"),
);
rows = await readParquetAsObjects(buf, ["timestamp", ...sensorCols]);
} catch {
continue;
}
for (const col of sensorCols) {
// channels: (F,P,3) with small F -> per-finger; else single
const first = rows.find((r) => r[col] != null)?.[col] as
| number[][][]
| number[][]
| undefined;
if (!first) continue;
const isMulti =
Array.isArray(first) &&
Array.isArray(first[0]) &&
Array.isArray((first[0] as number[][])[0]) &&
first.length <= 4;
const nCh = isMulti ? first.length : 1;
for (let k = 0; k < nCh; k++) {
const chName = (isMulti ? `${col}[${k}]` : col).replace(
"observation.sensors.",
"",
);
let a = acc.get(chName);
if (!a) {
a = {
peak: 0,
peakFolder: "",
peakT: 0,
contactPcts: [],
maxSumFz: 0,
};
acc.set(chName, a);
}
let contact = 0;
let n = 0;
for (const r of rows) {
const cell = r[col] as number[][][] | number[][] | undefined;
if (!cell) continue;
const taxels = (
isMulti ? (cell as number[][][])[k] : (cell as number[][])
) as number[][];
if (!taxels) continue;
n++;
let framePeak = 0;
let sumFz = 0;
for (const p of taxels) {
const m = Math.hypot(Number(p[0]), Number(p[1]), Number(p[2]));
sumFz += Number(p[2]);
if (m > framePeak) framePeak = m;
}
if (framePeak >= CONTACT_THR_N) contact++;
if (sumFz > a.maxSumFz) a.maxSumFz = sumFz;
if (framePeak > a.peak) {
a.peak = framePeak;
a.peakFolder = folder;
a.peakT = Number(r.timestamp) || 0;
}
}
if (n) a.contactPcts.push((100 * contact) / n);
}
}
}
onProgress(folders.length, folders.length, "");
return [...acc.entries()].map(([channel, a]) => ({
channel,
peak: a.peak,
peakFolder: a.peakFolder,
peakT: a.peakT,
meanContactPct: a.contactPcts.length
? a.contactPcts.reduce((x, y) => x + y, 0) / a.contactPcts.length
: 0,
maxSumFz: a.maxSumFz,
episodes: a.contactPcts.length,
}));
}
|