File size: 2,706 Bytes
9c046e9 5374cb2 9c046e9 5374cb2 9c046e9 5374cb2 9c046e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 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())
|