"""InterFaceGAN's directions to the raw files the app reads.

    python directions.py <boundary.npy> ... <output-folder>

Each InterFaceGAN boundary is the normal of a plane that separates faces with
an attribute (smiling, glasses, older) from faces without it, in the
generator's W space: one unit vector of 512 numbers. The app adds a multiple
of it to every row of ws, so it reads each one as 512 little-endian float32s,
<name>.fp32.f32, with no header.
"""

import os
import sys

import numpy as np

*sources, folder = sys.argv[1:]
for source in sources:
    vector = np.load(source).astype("<f4").reshape(-1)
    if vector.size != 512:
        raise SystemExit(f"{source}: expected 512 numbers, found {vector.size}")
    name = os.path.splitext(os.path.basename(source))[0]
    path = os.path.join(folder, f"{name}.fp32.f32")
    vector.tofile(path)
    print(f"  wrote {name}.fp32.f32 (length {np.linalg.norm(vector):.3f})")
