Download sth_vector_evolve_float.py from LordAlder/Q0FDTQ: direct link, hf CLI and curl.
- Browser
- Download file 2.71 kB
-
https://huggingface.co/spaces/LordAlder/Q0FDTQ/resolve/main/sth_vector_evolve_float.py
- Command line
-
hf download hf://spaces/LordAlder/Q0FDTQ/sth_vector_evolve_float.py
-
curl -L -o sth_vector_evolve_float.py https://huggingface.co/spaces/LordAlder/Q0FDTQ/resolve/main/sth_vector_evolve_float.py
2.71 kB
| import numpy as np | |
| import mpmath as mp | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| from matplotlib.colors import LinearSegmentedColormap | |
| import copy | |
| import time | |
| import scipy.signal | |
| import gradio as gr | |
| import ST_heatmap_plot as SThp | |
| import cProfile | |
| import pstats | |
| import io | |
| from tqdm import trange | |
| def reduce_parameter_precision(params): | |
| """ | |
| Reduces the precision of floating-point parameters in the given dictionary. | |
| params: Dictionary containing the parameters. | |
| target_precision: The target precision (number of significant digits). | |
| """ | |
| for key, value in params.items(): | |
| if isinstance(value, mp.mpf): | |
| # Reducing precision using mpmath (if you still want to use mpmath) | |
| #params[key] = mp.nstr(value, target_precision) | |
| # Or, convert to standard float (if you're eliminating mpmath) | |
| params[key] = float(mp.nstr(value, 64)) | |
| return params | |
| def evolve_vector(n, m, K, G, e, d, s, q0_mask, p0_mask, Q0, P0): | |
| '''l=1''' | |
| Xavg = np.zeros((2 * n, m)) | |
| show_m_progress = m > s | |
| for _ in trange(s, desc ="Sampling: "): | |
| q0 = [(Q0 + d * (0.5 - np.random.rand()) % 1) if mask else 0 for mask in q0_mask] | |
| p0 = [(P0 + d * (0.5 - np.random.rand()) % 1) if mask else 0 for mask in p0_mask] | |
| q = copy.deepcopy(q0) | |
| p = copy.deepcopy(p0) | |
| for j in trange(m, disable=not show_m_progress,desc ="Mapping: "): | |
| for k in range(n): | |
| Xavg[k, j] += q[k] % 1 | |
| Xavg[n + k, j] += p[k] % 1 | |
| new_p = [ | |
| K * q[i] + G * (q[i - 1] + q[(i + 1) % n]) | |
| + (K ** 2 + 1 + 2 * G ** 2) * p[i] | |
| + 2 * K * G * (p[i - 1] + p[(i + 1) % n]) | |
| + G ** 2 * (p[i - 2] + p[(i + 2) % n]) | |
| for i in range(n) | |
| ] | |
| new_q = [ | |
| q[i] + K * p[i] - e * np.sin(2 * np.pi * new_p[i]) + G * (p[i - 1] + p[(i + 1) % n]) | |
| for i in range(n) | |
| ] | |
| q, p = new_q, new_p | |
| return np.array(Xavg) / s | |
| def main(params): | |
| params=reduce_parameter_precision(params) | |
| return evolve_vector(params['n'], params['m'], params['k'], params['g'], params['e'], params['d'], params['s'], params['q0_mask'], params['p0_mask'], params['Q0'], params['P0']) | |
| pr = cProfile.Profile() | |
| pr.enable() | |
| # Run the function you want to profile | |
| # Stop the profiler | |
| pr.disable() | |
| # Create a stream to capture the profiler's output | |
| s = io.StringIO() | |
| sortby = 'cumulative' # Can also use 'time' or other sorting options | |
| ps = pstats.Stats(pr, stream=s).sort_stats(sortby) | |
| ps.print_stats() | |
| print(s.getvalue()) | |