-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble_stats_useless.py
157 lines (133 loc) · 5.79 KB
/
bubble_stats_useless.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import sys, os
import cPickle as pickle
import numpy as np
import mahotas
import getLogDistributions as gLD
import matplotlib.pyplot as plt
import matplotlib as mpl
from skimage import measure
import mokas_events as mke
def events_and_clusters(self, data=None, is_plot=True):
NNstructure = np.ones((3,3))
sw = np.unique(switch2D)[1:]
# statistics of events
events_sizes = np.array([])
for switch in sw:
q = switch2D == switch
im, n_cluster = mahotas.label(q, NNstructure)
sizes = mahotas.labeled.labeled_size(im)[1:]
events_sizes = np.concatenate((events_sizes, sizes))
# statistics of clusters
#avalanche_end gives the end of the cluster while aavalanche_start gives the beginning
print("Calculating clusters")
progress = "."
avalanches_end = np.copy(switch2D)
for sw0 in sw:
q = avalanches_end == sw0
clusters, n_cluster = mahotas.label(q, NNstructure)
for i in range(1, n_cluster+1):
cluster = clusters == i
cluster_edge = mke.pixels_at_edges(cluster)
# Get the values of the switches around the cluster
switches_at_edge = np.extract(cluster_edge, avalanches_end)
sw_next = sw0 + 1
# Check if any of the neighbours is sw0 + istep
# and set the original cluster to sw0 + istep
if sw_next in switches_at_edge:
avalanches_end[cluster] = sw_next
avalanche_switches = np.unique(avalanches_end)[1:]
avalanches_start = np.copy(switch2D)
for switch in avalanche_switches:
q = avalanches_end == switch
clusters, n_cluster = mahotas.label(q, NNstructure)
for i in range(1, n_cluster+1):
cluster = clusters == i
avalanches_start[cluster] = np.min(switch2D[cluster])
# Calculus of the cluster sizes and durations
print("Calculating cluster sizes and durations")
avalanche_sizes = np.array([])
avalanche_durations = []
for switch in avalanche_switches:
clusters, n_cluster = mahotas.label(avalanches_end == switch, NNstructure)
sizes = mahotas.labeled.labeled_size(clusters)[1:]
avalanche_sizes = np.concatenate((avalanche_sizes, sizes))
for i in range(1, n_cluster+1):
q = clusters == i
d = avalanches_end[q][0] - avalanches_start[q][0]
avalanche_durations.append(d)
avalanche_durations = np.asarray(avalanche_durations)
assert len(avalanche_sizes) == len(avalanche_durations)
average_avalanche_durations = np.unique(avalanche_durations)
average_avalanche_sizes = []
for duration in average_avalanche_durations:
size = np.mean(np.extract(avalanche_durations == duration, avalanche_sizes))
average_avalanche_sizes.append(size)
average_avalanche_sizes = np.array(average_avalanche_sizes)
if is_plot:
clrs = (np.random.rand(2*len(sw),3) + [1,1,1])/2
clrs[0] = [0,0,0]
cmap = mpl.colors.ListedColormap(clrs)
fig1, axs1 = plt.subplots(1, 3, sharex=True, sharey=True) # ColorImages of events and sizes
axs1[0].imshow(switch2D, cmap=cmap)
axs1[1].imshow(avalanches_start, cmap=cmap)
axs1[2].imshow(avalanches_end, cmap=cmap)
for i in avalanche_switches:
cluster = avalanches_end==i
cnts = measure.find_contours(cluster, 0.5, fully_connected = 'high')
for cnt in cnts:
X,Y = cnt[:,1], cnt[:,0]
for ax in axs1:
ax.plot(X, Y, c='k', antialiased=True, lw=1)
print("We have collected %i events and %i avalanches" % (len(events_sizes), len(avalanche_sizes)))
fig2, axs2 = plt.subplots(1, 2) # Distributions of events and avalanches
# Calculate and plot the distributions of clusters and avalanches
for sizes, label in zip([events_sizes, avalanche_sizes], ['events', 'avalanches_end']):
x, y, yerr = gLD.logDistribution(sizes, log_step=0.1,
first_point=1., normed=True)
# Plots of the distributions
axs2[0].loglog(x, y,'o', label=label)
axs2[0].errorbar(x, y, yerr, fmt="none")
if label == 'events':
axs2[0].loglog(x, 0.4 * x**-1.17 * np.exp(-x/50),'-', label=r'S^{-1.17} exp(-S/50)')
elif label == 'avalanches_end':
axs2[0].loglog(x, 0.4 * x**-1.17 * np.exp(-x/500),'-', label=r'S^{-1.17}')
axs2[0].legend()
axs2[0].grid(True)
axs2[1].loglog(average_avalanche_durations, average_avalanche_sizes,'o')
axs2[1].grid(True)
plt.show()
return avalanches_start, avalanches_end
if __name__ == "__main__":
qq = np.array([[13, 13, 13, 8, 8, 3],
[12, 14, 8, 6, 3, 3],
[ 9, 4, 4, 4, 3, -1],
[15, 15, 15, 2, -1, -1],
[15, 4, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1]], dtype=np.int32)
qq2 = np.array([[13, 13, 13, 8, 7, 3],
[12, 14, 8, 6, 3, 3],
[ 8, 4, 4, 4, 3, -1],
[15, 9, 15, 2, -1, -1],
[15, 15, 2, -1, -1, -1],
[-1, 2, -1, -1, -1, -1]], dtype=np.int32)
if False:
print("Loading pickle file")
print(sys.argv)
try:
choice = sys.argv[1]
except:
choice = 'bubble'
if choice == "bubble":
k = sys.argv[2]
print k
k = str(k).rjust(2,"0")
rootDir = "/data/Meas/Creep/CoFeB/Film/SuperSlowCreep/Irr_800uC/%s_Irr_800uC_0.116A" % k
filename = os.path.join(rootDir,'switchMap2D.pkl')
with open(filename, 'rb') as f:
switch2D = pickle.load(f)
else:
switch2D = qq
avalanches_start, avalanches_end = events_and_clusters(switch2D)
#print(qq2)
#print(30*"#")
#print(avalanches)