Skip to content

Commit 07d4c7b

Browse files
committed
final code push
1 parent 5f1f101 commit 07d4c7b

9 files changed

+81
-92
lines changed

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ scipy
88
importlib
99
powerlaw
1010
mpl_toolkits
11-
pickle
11+
pickle
12+
seaborn
176 KB
Loading
171 KB
Loading
170 KB
Loading
158 KB
Loading
170 KB
Loading
79.4 KB
Loading

src/lattice.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
# Automatically setting the local path to this repo for easy file writing and saving
3333
dir_path = path.dirname(path.realpath(__file__))
3434

35+
3536
class Lattice():
3637
def __init__(self, size=(10, 10),
3738
rand_dist=('uniform',),
@@ -543,7 +544,7 @@ def get_clusters(self):
543544
plt.colorbar(fitness)
544545
plt.tight_layout()
545546
plt.savefig(path.join(dir_path, 'figures/lattice_itr=1.png'), dpi=300)
546-
547+
547548
plt.figure()
548549
plt.title('Age Distribution after {} iterations'.format(iterations))
549550
age = lattice.plot(label='age')
@@ -552,9 +553,6 @@ def get_clusters(self):
552553
plt.savefig(path.join(dir_path, 'figures/lattice-age_itr={}.png'.format(iterations)), dpi=300)
553554
#
554555

555-
556-
557-
558556
"""
559557
number_of_frames = 2000
560558
def update_hist(num):
@@ -567,4 +565,4 @@ def update_hist(num):
567565
568566
animation = animation.FuncAnimation(fig, update_hist, number_of_frames,interval=20)
569567
plt.show()
570-
"""
568+
"""

src/main.py

Lines changed: 76 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
warnings.filterwarnings("ignore")
3232

3333

34-
def print_statement(alpha,r,p,name):
34+
def print_statement(alpha, r, p, name):
3535

36-
print("The slope of {} disrtibution is {}".format(name, round(alpha,4)))
36+
print("The slope of {} disrtibution is {}".format(name, round(alpha, 4)))
3737
if r < 0:
38-
print("{} follows a lognormal distribution with a p = {} ".format(name,round(p,4)))
38+
print("{} follows a lognormal distribution with a p = {} ".format(name, round(p, 4)))
3939
if r > 0:
40-
print("{} follows a powerlaw distribution with a p = {} ".format(name, round(p,4)))
40+
print("{} follows a powerlaw distribution with a p = {} ".format(name, round(p, 4)))
4141

4242

4343
def comp_average_fitness(size=(20, 20), iteration=2000, repetition=10, std=0.3):
@@ -102,7 +102,6 @@ def comp_average_fitness(size=(20, 20), iteration=2000, repetition=10, std=0.3):
102102
plt.show()
103103

104104

105-
106105
def comp_avalanche_time(size=(20, 20), iteration=2000, repetition=10, std=0.2):
107106
"""
108107
Plots the avalanche distribution in a log-log plot
@@ -153,11 +152,10 @@ def comp_avalanche_time(size=(20, 20), iteration=2000, repetition=10, std=0.2):
153152
dpi=300)
154153
plt.show()
155154

156-
print_statement(result_uniform.power_law.alpha, R_unifrom,p_uniform, "uniform")
155+
print_statement(result_uniform.power_law.alpha, R_unifrom, p_uniform, "uniform")
157156
print_statement(result_gaussian.power_law.alpha, R_gaussian, p_gaussian, "gaussian")
158157

159158

160-
161159
def comp_mutation_dist(size=(20, 20), iteration=2000, repetition=10, std=0.2):
162160
"""
163161
Plots the distribution between distances between mutations
@@ -211,11 +209,10 @@ def comp_mutation_dist(size=(20, 20), iteration=2000, repetition=10, std=0.2):
211209
dpi=300)
212210
plt.show()
213211

214-
print_statement(result_uniform.power_law.alpha, R_unifrom,p_uniform, "uniform")
212+
print_statement(result_uniform.power_law.alpha, R_unifrom, p_uniform, "uniform")
215213
print_statement(result_gaussian.power_law.alpha, R_gaussian, p_gaussian, "gaussian")
216214

217215

218-
219216
def comp_diff_neighbours(size=(20, 20), iteration=2000, repetition=10):
220217
"""
221218
Plots the avalanche distribution in a log-log plot
@@ -253,10 +250,10 @@ def comp_diff_neighbours(size=(20, 20), iteration=2000, repetition=10):
253250
free_percent=0,
254251
iterations=iterations,
255252
)
256-
moore = Lattice(size=size, torus_mode=True,neighbourhood='Moore', rand_dist=('uniform',), free_percent=0, iterations=iterations,
253+
moore = Lattice(size=size, torus_mode=True, neighbourhood='Moore', rand_dist=('uniform',), free_percent=0, iterations=iterations,
257254
)
258-
vonNeumann = Lattice(size=(50,50), torus_mode=True,neighbourhood='von Neumann', rand_dist=('uniform',), free_percent=0,
259-
iterations=iterations,)
255+
vonNeumann = Lattice(size=(50, 50), torus_mode=True, neighbourhood='von Neumann', rand_dist=('uniform',), free_percent=0,
256+
iterations=iterations,)
260257

261258
moore.run(["mutation", "avalanche_time", "get_dist_btw_mutation"])
262259
vonNeumann.run(["mutation", "avalanche_time", "get_dist_btw_mutation"])
@@ -296,10 +293,8 @@ def comp_diff_neighbours(size=(20, 20), iteration=2000, repetition=10):
296293
R_vonNeumann, p_vonNeumann = result_vonNeumann.distribution_compare(
297294
'power_law', 'exponential', normalized_ratio=True)
298295

299-
300-
print_statement(result_moore.power_law.alpha, R_moore,p_moore, "More Neighbour")
301-
print_statement(result_vonNeumann.power_law.alpha, R_vonNeumann,p_vonNeumann, "von Neumann Neighbourhood")
302-
296+
print_statement(result_moore.power_law.alpha, R_moore, p_moore, "More Neighbour")
297+
print_statement(result_vonNeumann.power_law.alpha, R_vonNeumann, p_vonNeumann, "von Neumann Neighbourhood")
303298

304299
n_moore, bins_moore = np.histogram(mutation_dist_moore_list, density=True)
305300
n_vonNeumann, bins_vonNeumann = np.histogram(mutation_dist_vonNeumann_list, density=True)
@@ -320,8 +315,9 @@ def comp_diff_neighbours(size=(20, 20), iteration=2000, repetition=10):
320315
plt.savefig(path.join(dir_path, 'figures/diff_neighbours_s={}_itr={}_rep={}.png'.format(size, iteration, repetition)), dpi=300)
321316
plt.show()
322317

323-
print_statement(result_moore.power_law.alpha, R_moore,p_moore, "More Neighbour")
324-
print_statement(result_vonNeumann.power_law.alpha, R_vonNeumann,p_vonNeumann, 'von Neumann')
318+
print_statement(result_moore.power_law.alpha, R_moore, p_moore, "More Neighbour")
319+
print_statement(result_vonNeumann.power_law.alpha, R_vonNeumann, p_vonNeumann, 'von Neumann')
320+
325321

326322
def is_free_variation(i_min=0, i_max=1, i_iter=6, iterations=2000):
327323
'''
@@ -394,58 +390,52 @@ def is_free_variation(i_min=0, i_max=1, i_iter=6, iterations=2000):
394390
def comp_cluster_sizes(iterations=2000):
395391
# Compares the cluster sizes of different sizes of grid
396392

393+
small = Lattice(size=(20, 20), torus_mode=True, rand_dist=('uniform',), free_percent=0, iterations=iterations,
394+
age_fraction=1 / 10)
395+
medium = Lattice(size=(50, 50), torus_mode=True, rand_dist=('uniform',), free_percent=0, iterations=iterations,
396+
age_fraction=1 / 10)
397397

398+
large = Lattice(size=(70, 70), torus_mode=True, rand_dist=('uniform',), free_percent=0, iterations=iterations,
399+
age_fraction=1 / 10)
398400

401+
small.run(["mutation", "update_age", "get_cluster"])
402+
medium.run(["mutation", "update_age", "get_cluster"])
403+
large.run(["mutation", "update_age", "get_cluster"])
399404

400-
small= Lattice(size=(20,20), torus_mode=True, rand_dist=('uniform',), free_percent=0, iterations=iterations,
401-
age_fraction=1 / 10)
402-
medium = Lattice(size=(50,50), torus_mode=True, rand_dist=('uniform',), free_percent=0, iterations=iterations,
403-
age_fraction=1 / 10)
404-
405-
large = Lattice(size=(70,70), torus_mode=True, rand_dist=('uniform',), free_percent=0, iterations=iterations,
406-
age_fraction=1 / 10)
407-
408-
small.run(["mutation", "update_age","get_cluster"])
409-
medium.run(["mutation","update_age","get_cluster"])
410-
large.run(["mutation","update_age","get_cluster"])
411-
412-
413-
small_hist = np.concatenate([small.cluster_size[x] for x in small.cluster_size])
414-
medium_hist = np.concatenate([medium.cluster_size[x] for x in medium.cluster_size])
415-
large_hist = np.concatenate([large.cluster_size[x] for x in large.cluster_size])
416-
417-
418-
#get the power law
419-
small_results = powerlaw.Fit(small_hist, discrete = True, verbose=False)
420-
medium_results = powerlaw.Fit(medium_hist,dicsrete = True, verbose=False)
421-
large_resutls = powerlaw.Fit(large_hist,discrete = True, verbose=False)
422-
423-
r_small,p_small = small_results.distribution_compare('power_law', 'exponential', normalized_ratio = True)
424-
r_medium,p_medium = medium_results.distribution_compare('power_law', 'exponential', normalized_ratio = True)
425-
r_large,p_large = large_resutls.distribution_compare('power_law', 'exponential', normalized_ratio = True)
426-
427-
405+
small_hist = np.concatenate([small.cluster_size[x] for x in small.cluster_size])
406+
medium_hist = np.concatenate([medium.cluster_size[x] for x in medium.cluster_size])
407+
large_hist = np.concatenate([large.cluster_size[x] for x in large.cluster_size])
428408

429-
#plot the power law
430-
plt.figure()
431-
powerlaw.plot_pdf(small_hist,label='20X20 Grid')
432-
powerlaw.plot_pdf(medium_hist,label = '50X50 Grid')
433-
powerlaw.plot_pdf(large_hist,label = '70X70 Grid')
409+
# get the power law
410+
small_results = powerlaw.Fit(small_hist, discrete=True, verbose=False)
411+
medium_results = powerlaw.Fit(medium_hist, dicsrete=True, verbose=False)
412+
large_resutls = powerlaw.Fit(large_hist, discrete=True, verbose=False)
434413

414+
r_small, p_small = small_results.distribution_compare('power_law', 'exponential', normalized_ratio=True)
415+
r_medium, p_medium = medium_results.distribution_compare('power_law', 'exponential', normalized_ratio=True)
416+
r_large, p_large = large_resutls.distribution_compare('power_law', 'exponential', normalized_ratio=True)
435417

436-
plt.title("Compare cluster size for different grid sizes")
437-
plt.xlabel("Cluster size ")
438-
plt.ylabel("Probability")
439-
plt.legend()
440-
plt.show()
418+
# plot the power law
419+
plt.figure()
420+
powerlaw.plot_pdf(small_hist, label='20X20 Grid')
421+
powerlaw.plot_pdf(medium_hist, label='50X50 Grid')
422+
powerlaw.plot_pdf(large_hist, label='70X70 Grid')
441423

424+
plt.title("Compare cluster size for different grid sizes")
425+
plt.xlabel("Cluster size ")
426+
plt.ylabel("Probability")
427+
plt.legend()
428+
plt.grid()
429+
plt.tight_layout()
430+
plt.savefig(path.join(dir_path, 'figures/cluster-sizes_rep={}.png'.format(iterations)), dpi=300)
431+
plt.show()
442432

443-
print_statement(small_results.power_law.alpha, r_small, p_small, "the 20X20 grid's")
444-
print_statement(medium_results.power_law.alpha, r_medium, p_medium, "the 50X50 grid's")
445-
print_statement(large_resutls.power_law.alpha, r_large, p_large, "the 70X70 grid's")
433+
print_statement(small_results.power_law.alpha, r_small, p_small, "the 20X20 grid's")
434+
print_statement(medium_results.power_law.alpha, r_medium, p_medium, "the 50X50 grid's")
435+
print_statement(large_resutls.power_law.alpha, r_large, p_large, "the 70X70 grid's")
446436

447437

448-
def comp_moving_vs_stationary(size=(20, 20),iteration = 2000,repetition = 10):
438+
def comp_moving_vs_stationary(size=(20, 20), iteration=2000, repetition=10):
449439
"""
450440
Compares the cluster sizes and avalanche time
451441
@@ -458,13 +448,21 @@ def comp_moving_vs_stationary(size=(20, 20),iteration = 2000,repetition = 10):
458448
avalanche_stationary_list = []
459449

460450
for i in range(repetition):
461-
stationary = Lattice(size=size, torus_mode=True,neighbourhood='Moore', rand_dist=('uniform',), free_percent=0, iterations=iterations,
462-
)
463-
move = Lattice(size=(50,50), torus_mode=True,neighbourhood='Moore', rand_dist=('uniform',), free_percent=0.3,
464-
iterations=iterations,)
451+
stationary = Lattice(
452+
size=size,
453+
torus_mode=True,
454+
neighbourhood='Moore',
455+
rand_dist=(
456+
'uniform',
457+
),
458+
free_percent=0,
459+
iterations=iterations,
460+
)
461+
move = Lattice(size=(50, 50), torus_mode=True, neighbourhood='Moore', rand_dist=('uniform',), free_percent=0.3,
462+
iterations=iterations,)
465463

466-
stationary.run(["mutation","avalanche_time"])
467-
move.run(["moving","avalanche_time"])
464+
stationary.run(["mutation", "avalanche_time"])
465+
move.run(["moving", "avalanche_time"])
468466

469467
avalanche_move_list = avalanche_move_list + move.avalanche_time_list['avalanche_time']
470468
avalanche_stationary_list = avalanche_stationary_list + stationary.avalanche_time_list['avalanche_time']
@@ -486,26 +484,23 @@ def comp_moving_vs_stationary(size=(20, 20),iteration = 2000,repetition = 10):
486484
plt.xlabel("Avalanche sizes ")
487485
plt.grid()
488486
plt.tight_layout()
487+
plt.savefig(path.join(dir_path, 'figures/moving-vs-stationary_size={}_itr{}_rep={}.png'.format(size, iteration, repetition)), dpi=300)
489488
plt.show()
490489

491-
492-
493490
print_statement(result_move.power_law.alpha, R_move, p_move, "migration")
494491
print_statement(result_stationary.power_law.alpha, R_stationary, p_stationary, "no migration")
495492

496493

497-
498-
499-
def comp_diff_dim(iterations = 2000):
494+
def comp_diff_dim(iterations=2000):
500495
"""
501496
Compares
502497
"""
503498
# Compares the cluster sizes of different sizes of grid
504499

505500
grid = Lattice(size=(20, 20), torus_mode=True, rand_dist=('uniform',), free_percent=0, iterations=iterations,
506-
age_fraction=1 / 10)
501+
age_fraction=1 / 10)
507502
cube = Lattice(size=(20, 20, 3), torus_mode=True, rand_dist=('uniform',), free_percent=0, iterations=iterations,
508-
age_fraction=1 / 10)
503+
age_fraction=1 / 10)
509504

510505
grid.run(["mutation", "update_age", "get_cluster"])
511506
cube.run(["mutation", "update_age", "get_cluster"])
@@ -514,30 +509,29 @@ def comp_diff_dim(iterations = 2000):
514509
cube_hist = np.concatenate([cube.cluster_size[x] for x in cube.cluster_size])
515510

516511
# get the power law
517-
grid_results = powerlaw.Fit(grid_hist, discrete=True,verbose=False)
518-
cube_results = powerlaw.Fit(grid_hist, dicsrete=True,verbose=False)
512+
grid_results = powerlaw.Fit(grid_hist, discrete=True, verbose=False)
513+
cube_results = powerlaw.Fit(grid_hist, dicsrete=True, verbose=False)
519514

520515
r_grid, p_grid = grid_results.distribution_compare('power_law', 'exponential', normalized_ratio=True)
521516
r_cube, p_cube = cube_results.distribution_compare('power_law', 'exponential', normalized_ratio=True)
522517

523518
# plot the power law
524519
plot_setting()
525-
powerlaw.plot_pdf(grid_hist,label='2 Dimensions')
526-
powerlaw.plot_pdf(cube_hist,label = '3 Dimensions')
520+
powerlaw.plot_pdf(grid_hist, label='2 Dimensions')
521+
powerlaw.plot_pdf(cube_hist, label='3 Dimensions')
527522
plt.title("Cluster Distribution for 2D and 3D")
528523
plt.xlabel("Cluster size ")
529524
plt.ylabel("Probability ")
530525
plt.grid()
531526
plt.legend()
532527
plt.tight_layout()
528+
plt.savefig(path.join(dir_path, 'figures/different_dimentionsitr{}_.png'.format(iterations)), dpi=300)
533529
plt.show()
534530

535531
print_statement(grid_results.power_law.alpha, r_grid, p_grid, "2D")
536532
print_statement(cube_results.power_law.alpha, r_cube, p_cube, "3D")
537533

538534

539-
540-
541535
def get_fitness_dist(iterations=20000):
542536
"""
543537
Get the distribution of fitness and situate it with respect to the threshold
@@ -549,16 +543,12 @@ def get_fitness_dist(iterations=20000):
549543

550544
plot_setting()
551545
plt.figure()
552-
plt.hist(list(lattice.fitness_dict.values()),label='Fitness Distribution')
553-
plt.axvline(x=max(lattice.threshold_list['threshold']), color='red',label='Threshold')
546+
plt.hist(list(lattice.fitness_dict.values()), label='Fitness Distribution')
547+
plt.axvline(x=max(lattice.threshold_list['threshold']), color='red', label='Threshold')
554548
plt.xlim((0, 1))
555549
plt.xlabel('Fitness')
556550
plt.ylabel('Probability')
557551
plt.tight_layout()
558552
plt.legend()
559-
553+
plt.savefig(path.join(dir_path, 'figures/fitness_distance_itr{}_.png'.format(iterations)), dpi=300)
560554
plt.show()
561-
562-
563-
564-

0 commit comments

Comments
 (0)