Skip to content

Commit 87db174

Browse files
committed
added asserts, model descriptions, MEM.R done
1 parent c065592 commit 87db174

23 files changed

+154
-145
lines changed

measures/Kolmogorov complexity/calculate_Kolmogorov_complexity.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import sys
44
from pybdm import BDM
55

6+
"""
7+
This module implements the Kolmogorov complexity measure. Refer to Section 2.2 (4) for details.
8+
"""
9+
610
def calculate_Kolmogorov_complexity(pattern, grid_size):
711
"""
812
This function calculates the approximate Kolmogorov Complexity of a pattern using Block Decomposition Method.
@@ -24,19 +28,22 @@ def calculate_Kolmogorov_complexity(pattern, grid_size):
2428
# calculate density for some example patterns:
2529
# 1) Full black pattern
2630
# 2) White grid with one central black CellType
27-
# 3) Checkarboard
31+
# 3) checkerboard
2832
# 4) Random pattern
2933

3034
all_black = np.ones(grid_size * grid_size, dtype=int)
35+
assert calculate_Kolmogorov_complexity(all_black, grid_size) == 25.176631293734488
3136
print(calculate_Kolmogorov_complexity(all_black, grid_size))
3237

3338
grid_with_one_centre = np.zeros(grid_size * grid_size, dtype=int)
3439
grid_with_one_centre[(grid_size * grid_size) //2] = 1
40+
assert calculate_Kolmogorov_complexity(grid_with_one_centre, grid_size) == 48.354642249885316
3541
print(calculate_Kolmogorov_complexity(grid_with_one_centre, grid_size))
3642

37-
checkarboard = np.zeros(grid_size * grid_size, dtype=int)
38-
checkarboard[1::2] = 1
39-
print(calculate_Kolmogorov_complexity(checkarboard, grid_size))
43+
checkerboard = np.zeros(grid_size * grid_size, dtype=int)
44+
checkerboard[1::2] = 1
45+
assert calculate_Kolmogorov_complexity(checkerboard, grid_size) == 33.43569202047461
46+
print(calculate_Kolmogorov_complexity(checkerboard, grid_size))
4047

4148
random = np.random.choice([0, 1], size=(grid_size, grid_size))
4249
print(calculate_Kolmogorov_complexity(random, grid_size))

measures/asymmetry/calculate_asymmetry.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
author = 'Surabhi S Nath'
22
import numpy as np
33

4+
'''
5+
This module implememts the asymmetry measure. Refer to Section 2.2 (6) and AII for details.
6+
'''
7+
48
def get_coords(grid):
59
"""
610
This function returns the indices and count of black pixels in the pattern
@@ -67,19 +71,22 @@ def calculate_asymmetry(grid, grid_size):
6771
# calculate density for some example patterns:
6872
# 1) Full black pattern
6973
# 2) White grid with one central black CellType
70-
# 3) Checkarboard
74+
# 3) checkerboard
7175
# 4) Random pattern
72-
76+
7377
all_black = np.ones(grid_size * grid_size)
78+
assert calculate_asymmetry(all_black, grid_size) == (0.0, 0.0)
7479
print(calculate_asymmetry(all_black, grid_size))
7580

7681
grid_with_one_centre = np.zeros(grid_size * grid_size)
7782
grid_with_one_centre[(grid_size * grid_size) //2] = 1
83+
assert calculate_asymmetry(grid_with_one_centre, grid_size) == (0.0, 0.0)
7884
print(calculate_asymmetry(grid_with_one_centre, grid_size))
7985

80-
checkarboard = np.zeros(grid_size * grid_size)
81-
checkarboard[1::2] = 1
82-
print(calculate_asymmetry(checkarboard, grid_size))
86+
checkerboard = np.zeros(grid_size * grid_size)
87+
checkerboard[1::2] = 1
88+
assert calculate_asymmetry(checkerboard, grid_size) == (0.0, 0.0)
89+
print(calculate_asymmetry(checkerboard, grid_size))
8390

8491
random = np.random.choice([0, 1], size=(grid_size, grid_size))
8592
print(calculate_asymmetry(random, grid_size))

measures/density/calculate_density.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
author = 'Surabhi S Nath'
22
import numpy as np
33

4+
"""
5+
This module implements the density measure. Refer to Section 2.2 (1) and AII for details.
6+
"""
7+
48
def calculate_density(grid, grid_size):
59
"""
610
This function caclulates the density of black pixels in a pattern
@@ -19,19 +23,22 @@ def calculate_density(grid, grid_size):
1923
# calculate density for some example patterns:
2024
# 1) Full black pattern
2125
# 2) White grid with one central black CellType
22-
# 3) Checkarboard
26+
# 3) checkerboard
2327
# 4) Random pattern
2428

2529
all_black = np.ones(grid_size * grid_size)
30+
assert calculate_density(all_black, grid_size) == 1.0
2631
print(calculate_density(all_black, grid_size))
2732

2833
grid_with_one_centre = np.zeros(grid_size * grid_size)
2934
grid_with_one_centre[(grid_size * grid_size) //2] = 1
35+
assert calculate_density(grid_with_one_centre, grid_size) == 0.0044444444444444444
3036
print(calculate_density(grid_with_one_centre, grid_size))
3137

32-
checkarboard = np.zeros(grid_size * grid_size)
33-
checkarboard[1::2] = 1
34-
print(calculate_density(checkarboard, grid_size))
38+
checkerboard = np.zeros(grid_size * grid_size)
39+
checkerboard[1::2] = 1
40+
assert calculate_density(checkerboard, grid_size) == 0.49777777777777776
41+
print(calculate_density(checkerboard, grid_size))
3542

3643
random = np.random.choice([0, 1], size=(grid_size, grid_size))
3744
print(calculate_density(random, grid_size))

measures/entropy/calculate_entropy.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
import numpy as np
33
import math
44

5+
"""
6+
This module implements the entropy measure. Refer to Section 2.2 (2i) and AII for details.
7+
"""
8+
59
def calculate_entropy(gridorflatgrid, grid_size):
610
"""
711
This function calculates the singlescale entropy of the pattern
@@ -33,19 +37,22 @@ def calculate_entropy(gridorflatgrid, grid_size):
3337
# calculate density for some example patterns:
3438
# 1) Full black pattern
3539
# 2) White grid with one central black CellType
36-
# 3) Checkarboard
40+
# 3) checkerboard
3741
# 4) Random pattern
3842

3943
all_black = np.ones(grid_size * grid_size, dtype=int)
44+
assert calculate_entropy(all_black, grid_size) == 0
4045
print(calculate_entropy(all_black, grid_size))
4146

4247
grid_with_one_centre = np.zeros(grid_size * grid_size, dtype=int)
4348
grid_with_one_centre[(grid_size * grid_size) //2] = 1
49+
assert calculate_entropy(grid_with_one_centre, grid_size) == 0.041125624368577904
4450
print(calculate_entropy(grid_with_one_centre, grid_size))
4551

46-
checkarboard = np.zeros(grid_size * grid_size, dtype=int)
47-
checkarboard[1::2] = 1
48-
print(calculate_entropy(checkarboard, grid_size))
52+
checkerboard = np.zeros(grid_size * grid_size, dtype=int)
53+
checkerboard[1::2] = 1
54+
assert calculate_entropy(checkerboard, grid_size) == 0.99998575111318
55+
print(calculate_entropy(checkerboard, grid_size))
4956

5057
random = np.random.choice([0, 1], size=(grid_size, grid_size))
5158
print(calculate_entropy(random, grid_size))

measures/entropy_of_means/calculate_entropy_of_means.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""
2+
This module implements the entropy of means measure based on:
3+
https://www.geeksforgeeks.org/program-to-count-number-of-connected-components-in-an-undirected-graph/
24
Python implementation of the matrix information measurement examples from the
35
StackExchange answer written by WilliamAHuber for
46
"Measuring entropy/ information/ patterns of a 2d binary matrix"
57
http://stats.stackexchange.com/a/17556/43909
6-
78
Copyright 2014 Cosmo Harrigan
89
This program is free software, distributed under the terms of the GNU LGPL v3.0
10+
11+
Refer to Section 2.2 (2iii) and AII for details.
912
"""
1013

1114
author = 'Cosmo Harrigan'
@@ -35,7 +38,6 @@ def calculate_entropy_of_means(flatgrid, grid_size):
3538
matrices.append(output_matrix)
3639

3740
prof = profile(matrices)
38-
print(prof)
3941
return np.mean(prof)
4042

4143
if __name__ == "__main__":
@@ -44,19 +46,22 @@ def calculate_entropy_of_means(flatgrid, grid_size):
4446
# calculate density for some example patterns:
4547
# 1) Full black pattern
4648
# 2) White grid with one central black CellType
47-
# 3) Checkarboard
49+
# 3) checkerboard
4850
# 4) Random pattern
4951

5052
all_black = np.ones(grid_size * grid_size, dtype=int)
53+
assert calculate_entropy_of_means(all_black, grid_size) == 0.0
5154
print(calculate_entropy_of_means(all_black, grid_size))
5255

5356
grid_with_one_centre = np.zeros(grid_size * grid_size, dtype=int)
5457
grid_with_one_centre[(grid_size * grid_size) //2] = 1
58+
assert calculate_entropy_of_means(grid_with_one_centre, grid_size) == 0.25
5559
print(calculate_entropy_of_means(grid_with_one_centre, grid_size))
5660

57-
checkarboard = np.zeros(grid_size * grid_size, dtype=int)
58-
checkarboard[1::2] = 1
59-
print(calculate_entropy_of_means(checkarboard, grid_size))
61+
checkerboard = np.zeros(grid_size * grid_size, dtype=int)
62+
checkerboard[1::2] = 1
63+
assert calculate_entropy_of_means(checkerboard, grid_size) == 0.5
64+
print(calculate_entropy_of_means(checkerboard, grid_size))
6065

6166
random = np.random.choice([0, 1], size=(grid_size, grid_size))
6267
print(calculate_entropy_of_means(random, grid_size))

measures/entropy_of_means/calculate_profile.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,4 @@ def profile(matrices):
5353
5454
@input matrices The set of scaled filtered matrices
5555
"""
56-
print("hi")
57-
print(matrices[1])
5856
return [matrix_entropy(scale) for scale in matrices]

measures/intricacy/calculate_intricacy.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
import numpy as np
33
import sys
44

5-
# Code based on GeeksforGeeks: https://www.geeksforgeeks.org/program-to-count-number-of-connected-components-in-an-undirected-graph/
5+
"""
6+
This module implements the intricacy measure using https://www.geeksforgeeks.org/program-to-count-number-of-connected-components-in-an-undirected-graph/.
7+
Refer to Section 2.2 (5) and AII for details.
8+
"""
69

710
class Graph:
811
"""
@@ -126,19 +129,22 @@ def calculate_intricacy(grid, grid_size):
126129
# calculate density for some example patterns:
127130
# 1) Full black pattern
128131
# 2) White grid with one central black CellType
129-
# 3) Checkarboard
132+
# 3) checkerboard
130133
# 4) Random pattern
131134

132135
all_black = np.ones(grid_size * grid_size)
136+
assert calculate_intricacy(all_black, grid_size) == (1, 1)
133137
print(calculate_intricacy(all_black, grid_size))
134138

135139
grid_with_one_centre = np.zeros(grid_size * grid_size)
136140
grid_with_one_centre[(grid_size * grid_size) //2] = 1
141+
assert calculate_intricacy(grid_with_one_centre, grid_size) == (2, 2)
137142
print(calculate_intricacy(grid_with_one_centre, grid_size))
138143

139-
checkarboard = np.zeros(grid_size * grid_size)
140-
checkarboard[1::2] = 1
141-
print(calculate_intricacy(checkarboard, grid_size))
144+
checkerboard = np.zeros(grid_size * grid_size)
145+
checkerboard[1::2] = 1
146+
assert calculate_intricacy(checkerboard, grid_size) == (225, 2)
147+
print(calculate_intricacy(checkerboard, grid_size))
142148

143149
random = np.random.choice([0, 1], size=(grid_size, grid_size))
144150
print(calculate_intricacy(random, grid_size))

measures/local spatial complexity/calculate_local_spatial_complexity.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
author = 'Surabhi S Nath'
22
import numpy as np
33

4+
"""
5+
This module implements the local spatial complexity measure based on Javaheri Javid, M. A. (2019). Aesthetic Automata: Synthesis and Simulation of Aesthetic Behaviour in Cellular Automata (Doctoral dissertation, Goldsmiths, University of London).
6+
Refer to Section 2.2 (3) and AII for details.
7+
"""
8+
49
def get_tuples(col1, col2, dirn, grid, grid_size):
510
"""
611
This function calculates the numerator of the conditional and joint distributions and the denominator of the conditional distribution
@@ -132,22 +137,22 @@ def calculate_local_spatial_complexity(pattern, grid_size):
132137
# calculate density for some example patterns:
133138
# 1) Full black pattern
134139
# 2) White grid with one central black CellType
135-
# 3) Checkarboard
140+
# 3) checkerboard
136141
# 4) Random pattern
137142

138143
all_black = np.ones(grid_size * grid_size, dtype=int)
144+
assert calculate_local_spatial_complexity(all_black, grid_size) == (0.0, 0.0)
139145
print(calculate_local_spatial_complexity(all_black, grid_size))
140146

141147
grid_with_one_centre = np.zeros(grid_size * grid_size, dtype=int)
142148
grid_with_one_centre[(grid_size * grid_size) //2] = 1
149+
assert calculate_local_spatial_complexity(grid_with_one_centre, grid_size) == (0.04331646911530629, 0.0)
143150
print(calculate_local_spatial_complexity(grid_with_one_centre, grid_size))
144151

145-
checkarboard = np.zeros(grid_size * grid_size, dtype=int)
146-
checkarboard[1::2] = 1
147-
print(calculate_local_spatial_complexity(checkarboard, grid_size))
152+
checkerboard = np.zeros(grid_size * grid_size, dtype=int)
153+
checkerboard[1::2] = 1
154+
assert calculate_local_spatial_complexity(checkerboard, grid_size) == (0.0, 0.0)
155+
print(calculate_local_spatial_complexity(checkerboard, grid_size))
148156

149157
random = np.random.choice([0, 1], size=(grid_size, grid_size))
150-
print(calculate_local_spatial_complexity(random, grid_size))
151-
152-
# Ref:
153-
# Javaheri Javid, M. A. (2019). Aesthetic Automata: Synthesis and Simulation of Aesthetic Behaviour in Cellular Automata (Doctoral dissertation, Goldsmiths, University of London).
158+
print(calculate_local_spatial_complexity(random, grid_size))

measures/mean_entropy/calculate_mean_entropy.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
sys.path.insert(1, '../entropy/')
66
from calculate_entropy import *
77

8+
"""
9+
This module implements the mean entropy measure. Refer to Section 2.2 (2ii) and AII for details.
10+
"""
11+
812
def calculate_mean_entropy(gridorflatgrid, grid_size):
913
"""
1014
This function calculates the mean entropy of a pattern across all scales
@@ -36,19 +40,22 @@ def calculate_mean_entropy(gridorflatgrid, grid_size):
3640
# calculate density for some example patterns:
3741
# 1) Full black pattern
3842
# 2) White grid with one central black CellType
39-
# 3) Checkarboard
43+
# 3) checkerboard
4044
# 4) Random pattern
4145

4246
all_black = np.ones(grid_size * grid_size, dtype=int)
47+
assert calculate_mean_entropy(all_black, grid_size) == 0.0
4348
print(calculate_mean_entropy(all_black, grid_size))
4449

4550
grid_with_one_centre = np.zeros(grid_size * grid_size, dtype=int)
4651
grid_with_one_centre[(grid_size * grid_size) //2] = 1
52+
assert calculate_mean_entropy(grid_with_one_centre, grid_size) == 0.0563395650797406
4753
print(calculate_mean_entropy(grid_with_one_centre, grid_size))
4854

49-
checkarboard = np.zeros(grid_size * grid_size, dtype=int)
50-
checkarboard[1::2] = 1
51-
print(calculate_mean_entropy(checkarboard, grid_size))
55+
checkerboard = np.zeros(grid_size * grid_size, dtype=int)
56+
checkerboard[1::2] = 1
57+
assert calculate_mean_entropy(checkerboard, grid_size) == 0.9326281610774253
58+
print(calculate_mean_entropy(checkerboard, grid_size))
5259

5360
random = np.random.choice([0, 1], size=(grid_size, grid_size))
5461
print(calculate_mean_entropy(random, grid_size))

measures/quadtree/calculate_quadtree.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
author = 'Surabhi S Nath'
22
import numpy as np
33

4+
"""
5+
This module implements the quadtree measure. Refer to AII for details.
6+
"""
7+
48
def all_same(grid):
59
"""
610
Checks if all pixels of the subgrid are the same state
@@ -60,19 +64,22 @@ def calculate_quadtree(grid, grid_size):
6064
# calculate density for some example patterns:
6165
# 1) Full black pattern
6266
# 2) White grid with one central black CellType
63-
# 3) Checkarboard
67+
# 3) checkerboard
6468
# 4) Random pattern
6569

6670
all_black = np.ones(grid_size * grid_size, dtype=int)
71+
assert calculate_quadtree(all_black, grid_size) == 0
6772
print(calculate_quadtree(all_black, grid_size))
6873

6974
grid_with_one_centre = np.zeros(grid_size * grid_size, dtype=int)
7075
grid_with_one_centre[(grid_size * grid_size) //2] = 1
76+
assert calculate_quadtree(grid_with_one_centre, grid_size) == 4
7177
print(calculate_quadtree(grid_with_one_centre, grid_size))
7278

73-
checkarboard = np.zeros(grid_size * grid_size, dtype=int)
74-
checkarboard[1::2] = 1
75-
print(calculate_quadtree(checkarboard, grid_size))
79+
checkerboard = np.zeros(grid_size * grid_size, dtype=int)
80+
checkerboard[1::2] = 1
81+
assert calculate_quadtree(checkerboard, grid_size) == 84
82+
print(calculate_quadtree(checkerboard, grid_size))
7683

7784
random = np.random.choice([0, 1], size=(grid_size, grid_size))
7885
print(calculate_quadtree(random, grid_size))

0 commit comments

Comments
 (0)