Spaces:
Sleeping
Sleeping
Download src/modules/auto_params.py from Sierpinksi-Project/NGF_on_Fractale: direct link, hf CLI and curl.
- Browser
- Download file 2.8 kB
-
https://huggingface.co/spaces/Sierpinksi-Project/NGF_on_Fractale/resolve/main/src/modules/auto_params.py
- Command line
-
hf download hf://spaces/Sierpinksi-Project/NGF_on_Fractale/src/modules/auto_params.py
-
curl -L -o auto_params.py https://huggingface.co/spaces/Sierpinksi-Project/NGF_on_Fractale/resolve/main/src/modules/auto_params.py
2.8 kB
| from __future__ import annotations | |
| from typing import Dict, Sequence, Tuple | |
| import networkx as nx | |
| import numpy as np | |
| def estimate_bath_correlation_length( | |
| G: nx.Graph, | |
| boundary_nodes: Sequence[int], | |
| max_pairs: int = 200, | |
| min_xi: float = 0.5, | |
| max_xi: float = 100.0, | |
| factor: float = 4.0, | |
| ) -> float: | |
| """ | |
| Schätze eine sinnvolle Korrelationlänge ξ_bad aus den Graph-Distanzen der Randknoten. | |
| Idee: | |
| - Bestimme typische kürzeste Pfadlängen d(i,j) zwischen Randknoten i,j. | |
| - Verwende den Mittelwert dieser Distanzen, geteilt durch einen Faktor ``factor``, | |
| als Schätzer für ξ_bad. | |
| - Beschneide den Wert auf das Intervall [min_xi, max_xi]. | |
| Die Berechnung verwendet aus Effizienzgründen maximal ``max_pairs`` Randknoten-Paare. | |
| """ | |
| bnodes = list(boundary_nodes) | |
| if len(bnodes) < 2: | |
| # Fallback: neutraler Wert im Sliderbereich | |
| return 1.0 | |
| pairs: list[Tuple[int, int]] = [] | |
| for i, u in enumerate(bnodes): | |
| for v in bnodes[i + 1 :]: | |
| pairs.append((u, v)) | |
| if not pairs: | |
| return 1.0 | |
| if len(pairs) > max_pairs: | |
| rng = np.random.default_rng(0) | |
| idx = rng.choice(len(pairs), size=max_pairs, replace=False) | |
| pairs = [pairs[int(k)] for k in idx] | |
| dists = [nx.shortest_path_length(G, u, v) for (u, v) in pairs] | |
| if not dists: | |
| return 1.0 | |
| d_mean = float(np.mean(dists)) | |
| if d_mean <= 0.0 or not np.isfinite(d_mean): | |
| return 1.0 | |
| xi = d_mean / float(factor) | |
| xi = float(np.clip(xi, min_xi, max_xi)) | |
| return xi | |
| def estimate_alpha_beta( | |
| edge_energies: Dict[Tuple[int, int], float], | |
| min_alpha: float = 0.05, | |
| max_alpha: float = 1.5, | |
| min_beta: float = 0.1, | |
| max_beta: float = 5.0, | |
| ) -> tuple[float, float]: | |
| """ | |
| Schätze (α, β) aus der Verteilung der Kantenergien. | |
| Heuristik: | |
| - β wächst mit dem Logarithmus des Dynamikbereichs R = E_max / E_mean. | |
| - α wächst mit der relativen Streuung (Coefficient of Variation) CV = E_std / E_mean. | |
| Die Rückgabewerte werden auf die Intervalle [min_alpha, max_alpha] bzw. | |
| [min_beta, max_beta] beschränkt. | |
| """ | |
| if not edge_energies: | |
| return 0.5, 1.5 | |
| E = np.fromiter(edge_energies.values(), dtype=float) | |
| if E.size == 0: | |
| return 0.5, 1.5 | |
| E_mean = float(E.mean()) | |
| E_std = float(E.std()) | |
| E_max = float(E.max()) | |
| ref = E_mean | |
| if abs(ref) < 1e-12: | |
| ref = E_std if abs(E_std) > 1e-12 else 1.0 | |
| R = abs(E_max / ref) if ref != 0.0 else 1.0 | |
| beta = 0.5 * float(np.log1p(R)) | |
| beta = float(np.clip(beta, min_beta, max_beta)) | |
| CV = abs(E_std / ref) if ref != 0.0 else 0.0 | |
| alpha = 0.3 * CV | |
| alpha = float(np.clip(alpha, min_alpha, max_alpha)) | |
| return alpha, beta | |