Download research/frames.py from PureOne/Seoryxa: direct link, hf CLI and curl.
- Browser
- Download file 5.51 kB
-
https://huggingface.co/datasets/PureOne/Seoryxa/resolve/main/research/frames.py
- Command line
-
hf download hf://datasets/PureOne/Seoryxa/research/frames.py
-
curl -L -o frames.py https://huggingface.co/datasets/PureOne/Seoryxa/resolve/main/research/frames.py
5.51 kB
| """Pointwise Parseval completion for rational-coordinate attributed 3D graphs. | |
| Chart comparisons are exact. Harmonic quadrature and output arrays use float64. | |
| Nodes may carry fixed-length tuples of comparable scalars. An optional dense edge | |
| table must encode missing edges distinctly. Marked nodes/edges can be encoded there. | |
| The exhaustive tied-vertex fallback is intended for small coincident fixtures. | |
| """ | |
| from fractions import Fraction | |
| from functools import lru_cache | |
| from itertools import groupby, permutations, product | |
| import numpy as np | |
| from scipy.special import sph_harm_y | |
| def inner(a, b): | |
| return sum(x * y for x, y in zip(a, b)) | |
| def cross(a, b): | |
| return (a[1]*b[2] - a[2]*b[1], a[2]*b[0] - a[0]*b[2], | |
| a[0]*b[1] - a[1]*b[0]) | |
| def centered(points): | |
| if len(points) == 0 or any(len(p) != 3 for p in points): | |
| raise ValueError("Expected a nonempty list of three-coordinate rows.") | |
| pts = [tuple(Fraction(x) for x in p) for p in points] | |
| mean = tuple(sum(p[k] for p in pts) / len(pts) for k in range(3)) | |
| return [tuple(x - m for x, m in zip(p, mean)) for p in pts] | |
| def body_key(local, labels, edges, roots): | |
| count = len(local) | |
| records = [(local[i], labels[i], tuple(i == r for r in roots)) for i in range(count)] | |
| order = sorted(range(count), key=records.__getitem__) | |
| node_key = tuple(records[i] for i in order) | |
| if edges is None: | |
| return (node_key,) | |
| blocks = [list(group) for _, group in groupby(order, key=records.__getitem__)] | |
| if all(len(block) == 1 for block in blocks): | |
| edge_key = tuple(edges[i][j] for i in order for j in order) | |
| else: | |
| def tied_tables(): | |
| for choice in product(*(permutations(block) for block in blocks)): | |
| perm = [v for block in choice for v in block] | |
| yield tuple(edges[i][j] for i in perm for j in perm) | |
| edge_key = min(tied_tables()) | |
| return node_key, edge_key | |
| def charts(points, labels=None, edges=None): | |
| r = centered(points) | |
| n = len(r) | |
| labels = [()] * n if labels is None else labels | |
| if len(labels) != n or (edges is not None and | |
| (len(edges) != n or any(len(row) != n for row in edges))): | |
| raise ValueError("Labels and edge table must match the vertex count.") | |
| best = None | |
| selected = [] | |
| for i, a in enumerate(r): | |
| for j, b in enumerate(r): | |
| normal = cross(a, b) | |
| area2 = inner(normal, normal) | |
| if not area2: | |
| continue | |
| gram = (-area2, inner(a, a), inner(a, b), inner(b, b)) | |
| for sign in (-1, 1): | |
| local = [(inner(a, v), inner(b, v), sign * inner(normal, v)) for v in r] | |
| key = (gram, body_key(local, labels, edges, (i, j))) | |
| if best is None or key < best: | |
| best, selected = key, [] | |
| if key == best: | |
| selected.append((a, b, sign)) | |
| if selected: | |
| frames = [] | |
| for a, b, sign in selected: | |
| e1 = np.asarray(a, dtype=float) | |
| e1 /= np.linalg.norm(e1) | |
| e2 = np.asarray(b, dtype=float) | |
| e2 -= (e1 @ e2) * e1 | |
| e2 /= np.linalg.norm(e2) | |
| frames.append(np.column_stack([e1, e2, sign * np.cross(e1, e2)])) | |
| return "frame", frames | |
| for i, a in enumerate(r): | |
| if not inner(a, a): | |
| continue | |
| key = (-inner(a, a), body_key([(inner(a, v),) for v in r], labels, edges, (i,))) | |
| if best is None or key < best: | |
| best, selected = key, [] | |
| if key == best: | |
| selected.append(a) | |
| if selected: | |
| axes = np.asarray(selected, dtype=float) | |
| return "line", axes / np.linalg.norm(axes, axis=1)[:, None] | |
| return "point", [] | |
| def harmonics(degree, directions): | |
| directions = np.asarray(directions, dtype=float) | |
| theta = np.arctan2(np.hypot(directions[:, 0], directions[:, 1]), directions[:, 2]) | |
| phi = np.arctan2(directions[:, 1], directions[:, 0]) | |
| orders = np.arange(degree + 1)[:, None] | |
| values = sph_harm_y(degree, orders, theta[None, :], phi[None, :]) | |
| return np.vstack([values[0].real, np.sqrt(2) * values[1:].real, | |
| np.sqrt(2) * values[1:].imag]) | |
| def quadrature(degree): | |
| z, w = np.polynomial.legendre.leggauss(degree + 1) | |
| count = 2 * degree + 1 | |
| angles = 2 * np.pi * np.arange(count) / count | |
| radius = np.sqrt(1 - z*z)[:, None] | |
| grid = np.column_stack([(radius * np.cos(angles)).ravel(), | |
| (radius * np.sin(angles)).ravel(), np.repeat(z, count)]) | |
| weights = np.repeat(w * 2 * np.pi / count, count) | |
| return grid, (harmonics(degree, grid) * weights).T | |
| def action(degree, matrix, twist=0): | |
| grid, analysis = quadrature(degree) | |
| sign = -1 if np.linalg.det(matrix) < 0 else 1 | |
| return (sign ** twist) * (harmonics(degree, grid @ matrix.T) @ analysis) | |
| def parseval(chart, degree, twist=0): | |
| if degree < 0 or twist not in (0, 1): | |
| raise ValueError("Expected degree >= 0 and twist equal to 0 or 1.") | |
| kind, frames = chart | |
| count = 2 * degree + 1 | |
| if kind == "point": | |
| return np.full((count, 1), float(degree == 0 and twist == 0)) | |
| if kind == "line": | |
| if twist: | |
| return np.zeros((count, 1)) | |
| return (np.sqrt(4 * np.pi / count) | |
| * np.mean(harmonics(degree, frames), axis=1, keepdims=True)) | |
| return sum(action(degree, f, twist) for f in frames) / len(frames) | |