Skip to content

Coords¤

unpack_size

abcdlux_patch.unpack_size(N) ¤

Source code in abcdlux_patch.py
56
57
58
59
60
def unpack_size(N: int | tuple[int, int]) -> tuple[int, int]:
    if isinstance(N, tuple):
        Nx, Ny = N
        return int(Nx), int(Ny)
    return int(N), int(N)
unpack_scale

abcdlux_patch.unpack_scale(d) ¤

Source code in abcdlux_patch.py
63
64
65
66
67
68
def unpack_scale(d: float | tuple) -> tuple[float, float]:
    if isinstance(d, tuple):
        dx, dy = d
        return float(dx), float(dy)
    val = float(d)
    return val, val
unpack_coords

abcdlux_patch.unpack_coords(coords) ¤

Source code in abcdlux_patch.py
111
112
113
114
115
116
117
118
119
120
def unpack_coords(coords: Array | tuple) -> tuple[Array, Array]:
    # Avoid isinstance(coords, Array) fragility: just check "ndim"
    if hasattr(coords, "ndim"):
        if coords.ndim != 1:
            raise ValueError(
                f"unpack_coords: Array coords must be 1D, got shape={coords.shape}."
            )
        return coords, coords
    x, y = coords
    return x, y
unpack_coord_spec

abcdlux_patch.unpack_coord_spec(spec) ¤

Source code in abcdlux_patch.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
def unpack_coord_spec(spec: Array | tuple) -> tuple[Array, Array]:
    # Case: explicit array -> symmetric x=y
    if hasattr(spec, "ndim"):
        return unpack_coords(spec)

    if isinstance(spec, tuple) and len(spec) == 2:
        a, b = spec

        # Uniform spec: (N, d) or ((Nx, Ny), (dx, dy))
        if _is_size_like(a) and _is_scale_like(b):
            Nx, Ny = unpack_size(a)
            dx, dy = unpack_scale(b)
            x = dlu.nd_coords(Nx, dx)
            y = dlu.nd_coords(Ny, dy)
            return x, y

        # Explicit (x, y)
        if not _is_size_like(a) and not _is_scale_like(b):
            return unpack_coords(spec)

    raise TypeError(f"unpack_coord_spec: unsupported spec {type(spec)} value={spec}")