#!/usr/bin/env python3 """Average hq_record recording directories using the existing player's decoder.""" from __future__ import annotations import argparse import json import os from pathlib import Path import sys import time import uuid import numpy as np try: # shared core lives beside this script import spectro_core as core except ImportError as exc: # pragma: no cover - placement error raise SystemExit('hq_average.py needs spectro_core.py in the same directory ' '(or on sys.path)') from exc def load_player(source, module_dir=None): """Decoder module: --module-dir, then this folder, then the project above.""" for directory in ([Path(module_dir)] if module_dir else [Path(__file__).resolve().parent, Path(source).parent.parent]): if (directory / 'hq_spectrum.py').is_file(): return core.load_hq(directory) return core.load_hq() def write_json(path, value): core.write_json(path, value) def average(source, output, group=0, drop_last=False, module_dir=None): source, output = Path(source).resolve(), Path(output).resolve() if group < 0: raise ValueError('group must be >= 0') if source == output or source in output.parents or output in source.parents: raise ValueError('Input and output must be separate, non-nested directories') if output.exists(): raise ValueError(f'Output already exists: {output}') meta = json.loads((source / 'meta.json').read_text(encoding='utf-8')) if meta.get('kind') != 'hq_raw_recording' or meta.get('format_version') != 1: raise ValueError('Expected hq_raw_recording format_version 1') w, h, stride = (int(meta[k]) for k in ('width', 'height', 'stride')) fb = int(meta['frame_bytes']) if min(w, h, stride) <= 0 or fb != h * stride: raise ValueError('Invalid dimensions/stride/frame_bytes') size = (source / 'frames.bin').stat().st_size count, remainder = divmod(size, fb) if remainder or not count or int(meta.get('frames', count)) != count: raise ValueError('Truncated recording or frame count mismatch; repair source separately') if not meta.get('completed'): raise ValueError('Source recording is not marked completed') records = [] metadata_path = source / 'frames.jsonl' if metadata_path.exists(): for i, line in enumerate(metadata_path.read_text(encoding='utf-8').splitlines()): row = json.loads(line) if not isinstance(row, dict): raise ValueError(f'Invalid metadata row {i}') nested = row.pop('metadata', None) if isinstance(nested, dict): row = {**nested, **row} for key, expected in [('i', i), ('offset', i * fb), ('bytes', fb)]: if key in row and row[key] != expected: raise ValueError(f'Incorrect metadata {key} in row {i}') records.append(row) if len(records) != count: raise ValueError('frames.jsonl count mismatch') else: records = [{} for _ in range(count)] # Exposure/gain changes are retained as ranges in each averaged frame's provenance. player = load_player(source, module_dir) pattern = str(meta.get('pattern') or player.pattern_from_format(meta['raw_format'])) if pattern not in ('BGGR', 'RGGB', 'GBRG', 'GRBG'): raise ValueError('Unsupported Bayer pattern') config = {'size': (w, h), 'stride': stride} n = group or count groups = [(a, min(a + n, count)) for a in range(0, count, n) if not (drop_last and min(a + n, count) - a < n)] if not groups: raise ValueError('No output frames; reduce group size or omit --drop-last') output.parent.mkdir(parents=True, exist_ok=True) temp = output.parent / (output.name + '.partial-' + uuid.uuid4().hex[:8]) temp.mkdir() started = time.monotonic() try: depth = None out_fb = w * h * 2 with (source / 'frames.bin').open('rb') as inp, (temp / 'frames.bin').open('wb') as raw_out, (temp / 'frames.jsonl').open('w', encoding='utf-8') as mout: for oi, (a, b) in enumerate(groups): acc = np.zeros((h, w), dtype=np.float64) inp.seek(a * fb) for index in range(a, b): data = inp.read(fb) if len(data) != fb: raise ValueError('Input changed or was truncated while reading') image, bits = player.decode_raw(np.frombuffer(data, dtype=np.uint8).reshape(h, stride), config, meta['raw_format']) if depth is not None and depth != bits: raise ValueError('Decoded bit depth changed') depth = bits acc += image image = np.clip(np.rint(acc / (b - a)), 0, (1 << depth) - 1).astype('