Download sth_matrix_evolve_float.py from LordAlder/Q0FDTQ: direct link, hf CLI and curl.
- Browser
- Download file 2.63 kB
-
https://huggingface.co/spaces/LordAlder/Q0FDTQ/resolve/main/sth_matrix_evolve_float.py
- Command line
-
hf download hf://spaces/LordAlder/Q0FDTQ/sth_matrix_evolve_float.py
-
curl -L -o sth_matrix_evolve_float.py https://huggingface.co/spaces/LordAlder/Q0FDTQ/resolve/main/sth_matrix_evolve_float.py
2.63 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 create_matrix_M(n, k, g, l): | |
| K = k * np.ones((n, 1)) | |
| G = g * np.ones((n, 1)) | |
| K = np.diag(K.flatten()) | |
| G = np.diag(G.flatten()) | |
| C = K | |
| for i in range(1, l + 1): | |
| P = np.roll(np.eye(n), l, axis=1) | |
| C += P @ G + G @ P.T | |
| M = np.block([[np.eye(n), C], [C, C @ C + np.eye(n)]]) | |
| return M | |
| def evolve_matrix(n, m, d, s, q0_mask, p0_mask, Q0, P0, M): | |
| Xavg = np.zeros((2 * n, m)) | |
| show_m_progress = m > s | |
| for _ in trange(s, desc ="Sampling: "): | |
| # Initialize q0 and p0 based on masks | |
| 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] | |
| x = np.concatenate((q0, p0)) | |
| for k in range(2 * n): | |
| Xavg[k, 0] += x[k] % 1 | |
| for j in trange(1,m, disable=not show_m_progress,desc ="Mapping: "): | |
| x = M @ x | |
| for k in range(2 * n): | |
| Xavg[k, j] += x[k] % 1 | |
| return (Xavg / s) | |
| def main(params): | |
| params=reduce_parameter_precision(params) | |
| M = create_matrix_M(params['n'], params['k'], params['g'], params['l']) | |
| return evolve_matrix(params['n'], params['m'], params['d'], params['s'], params['q0_mask'], params['p0_mask'], params['Q0'], params['P0'], M) | |
| 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()) | |