<jupyter_start><jupyter_text>Timing of (truncated) contagion maps<jupyter_code>%load_ext autoreload
%autoreload 2

import cmap as conmap
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns; 
sns.set_theme()
import pandas as pd

# For pretty colourmaps
import palettable
from matplotlib.colors import ListedColormap

sns.set_style("white")

import time

# import matplotlib
# import matplotlib.pyplot as plt

# matplotlib.rcParams['pdf.fonttype'] = 42
# matplotlib.rcParams['ps.fonttype'] = 42<jupyter_output>The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload<jupyter_text>In dependence of the network size $N$  Timing the calculations<jupyter_code>N_vec = 10**np.arange(1.5,4,0.25)
N_vec = N_vec.astype(int)

t=0.3

truncatedMapTime = []
fullMapTime = []

# time the execution of the maps
for N in N_vec:
    print(N)
    # network construction
    noisyRL = conmap.constructNoisyRingLattice(numberNodes=N,geometricDegree=6,nongeometricDegree=2)
    print('network constructed')
    # time the trunctated map
    start = time.time()
    contagionMap = conmap.runTruncatedContagionMap(noisyRL,threshold=t,numberSteps=20,symmetric=True)
    end = time.time()
    truncatedMapTime.append(end-start)

    # time the fill map
    start = time.time()
    contagionMap = conmap.runTruncatedContagionMap(noisyRL,threshold=t,symmetric=True)
    end = time.time()
    fullMapTime.append(end-start)
# save the times
out=pd.DataFrame()
out['N'] = N_vec
out['time full'] = fullMapTime
out['truncated full'] = truncatedMapTime
out.to_csv('./out/computationTimeSize.csv')
# fit powerlaw to data
from scipy.optimize import curve_fit

def func_powerlaw(_offsetx, m, c, c0):
    return c0 + x**m * c

def func_powerlaw(x, m, c):
    return (x**m) * c

fittedValues_fullmap, pcov = curve_fit(func_powerlaw, N_vec, fullMapTime, maxfev=2000 )
fittedValues_truncatedmap, pcov = curve_fit(func_powerlaw, N_vec, truncatedMapTime, maxfev=2000 )<jupyter_output><empty_output><jupyter_text>Plotting<jupyter_code>fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
plt.scatter(N_vec,fullMapTime,color='#3eafd1',s=100,marker='s')
plt.scatter(N_vec,truncatedMapTime,color='#c1502e',s=50,marker='o')

ax.set_yscale('log')
ax.set_xscale('log')

# add fit lines
plt.plot(N_vec, func_powerlaw(N_vec, fittedValues_truncatedmap[0], fittedValues_truncatedmap[1]), '-',color='#c1502e')
plt.plot(N_vec, func_powerlaw(N_vec, fittedValues_fullmap[0], fittedValues_fullmap[1]), '-',color='#3eafd1')
plt.xlabel('network size, $N$')
plt.ylabel('computation time, $\delta t$ in s')

# add labels
plt.text(10**2.2,10**2,'full contagion map\n $\delta t \propto N^{2.6}$',color='#3eafd1')
plt.text(10**2.5,0.05,'truncated contagion map\n $\delta t \propto N^{1.97}$',color='#c1502e')
plt.tight_layout()

plt.savefig('./figures/Fig7-computationalComplexityTruncatedContagion.pdf')<jupyter_output><empty_output><jupyter_text>In dependence of the scaling parameter $s$<jupyter_code>N=10000

s_vec = 10**np.arange(1,3,0.25)
s_vec = s_vec.astype(int)



t=0.3

truncatedMapTime = []
fullMapTime = []

# time the execution of the maps
for s in s_vec:
    print(s)
    # network construction
    noisyRL = conmap.constructNoisyRingLattice(numberNodes=N,geometricDegree=6,nongeometricDegree=2)
    print('network constructed')
    # time the trunctated map
    start = time.time()
    contagionMap = conmap.runTruncatedContagionMap(noisyRL,threshold=t,numberSteps=s,symmetric=True)
    end = time.time()
    truncatedMapTime.append(end-start)
print('full map')
# time the full map only once
start = time.time()
contagionMap = conmap.runTruncatedContagionMap(noisyRL,threshold=t,symmetric=True)
end = time.time()
fullMapTime.append(end-start)
# save the times
out=pd.DataFrame()
out['s'] = s_vec
out['truncated full'] = truncatedMapTime
#out['time full'] = fullMapTime
out.to_csv('./out/computationTimeSteps.csv')
# fit powerlaw to data
from scipy.optimize import curve_fit

def func_powerlaw(_offsetx, m, c, c0):
    return c0 + x**m * c

def func_powerlaw(x, m, c):
    return (x**m) * c

fittedValues_truncatedmap, pcov = curve_fit(func_powerlaw, s_vec, truncatedMapTime, maxfev=2000 )




#sns.set_context("notebook", font_scale=1.2)


fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
plt.scatter(s_vec,truncatedMapTime,color='#c1502e',s=50,marker='o')

plt.hlines(fullMapTime[0],s_vec[0],10**3,color='#3eafd1',linestyle=':')


ax.set_yscale('log')
ax.set_xscale('log')

# # add fit lines
plt.plot(s_vec, func_powerlaw(s_vec, fittedValues_truncatedmap[0], fittedValues_truncatedmap[1]), '-',color='#c1502e')
plt.xlabel('number of steps, $s$')
plt.ylabel('computation time, $\delta t$ in s')

plt.xticks([10**1,10**2,10**3])

plt.yticks([10**2,10**3,10**4])


# # add labels
plt.text(10**1,10**3.8,'full contagion map $\delta t \propto s^{0}$',color='#3eafd1')
plt.text(10**1.7,10**2.5,'truncated contagion map $\delta t \propto s^{0.58}$',color='#c1502e')
plt.tight_layout()

plt.savefig('./figures/Fig7b-computationalComplexityTruncatedContagion_stepsize.pdf')


fittedValues_truncatedmap
s_vec<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14468828898815303, 'fraction_numerical': 0.03282190716644009, 'mean_word_length': 6.182705718270572, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '140509', 'license': 'MIT', 'n_tokens_mistral': 2124, 'n_tokens_neox': 1982, 'n_words': 370, 'path': 'python/Fig7-computationalTime.ipynb', 'repo_name': 'floklimm/contagionMap', 'size': '5149'}