Skip to content

Commit 1fa5f9b

Browse files
committed
first version of the tutorial adding something on the 2D Ising model
1 parent 0875064 commit 1fa5f9b

File tree

1,821 files changed

+512016
-60
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,821 files changed

+512016
-60
lines changed

dadapy/hamming.py

+23-28
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def __init__(
2626
):
2727
self.q = q
2828
self.coordinates = coordinates
29-
self.metric = "hamming"
3029
self.distances = distances
3130
self.crossed_distances = crossed_distances
3231
self.verbose = verbose
@@ -45,7 +44,8 @@ def compute_distances(
4544
check_format=True,
4645
):
4746
"""
48-
Computes all to all distances in dataset and stores them in the matrix self.distances
47+
Computes all to all distances in dataset and stores them
48+
in the matrix "self.distances" of shape (Ns,Ns), where Ns is the number of samples
4949
"""
5050
if self.q == 2:
5151
self.distances = jcompute_distances(
@@ -63,15 +63,20 @@ def D_histogram(
6363
compute_flag=0, # 1 to compute histogram (else it is loaded)
6464
save=False, # 1 to save computed histogram
6565
resultsfolder="results/hist/",
66+
filename="counts.txt",
6667
):
6768
"""
68-
Given the computed distances this routine computes the histogram (Pemp) and saves is
69+
Given the computed distances, this routine computes the histogram (Pemp).
70+
It defines
71+
- self.D_values, a vector containing the sampled distances
72+
- self.D_counts, a vector containing how many times each distance was sampled
73+
- self.D_probs, self.counts normalized by the total number of counts observed.
6974
"""
7075
assert self.crossed_distances == 0
7176

7277
if save:
7378
os.makedirs(resultsfolder, exist_ok=True)
74-
c_fname = resultsfolder + "counts"
79+
_filename = resultsfolder + filename
7580

7681
if compute_flag:
7782
self.D_values, self.D_counts = np.unique(self.distances, return_counts=True)
@@ -90,17 +95,25 @@ def D_histogram(
9095

9196
if save:
9297
np.savetxt(
93-
fname=c_fname + ".txt",
98+
fname=_filename,
9499
X=np.transpose([self.D_values, self.D_counts]),
95100
fmt="%d,%d",
96101
)
97102
else:
98-
f = np.loadtxt(c_fname + ".txt", delimiter=",", dtype=int)
103+
f = np.loadtxt(_filename, delimiter=",", dtype=int)
99104
self.D_values = f[:, 0]
100105
self.D_counts = f[:, 1]
101106
self.D_probs = self.D_counts / np.sum(self.D_counts)
102107

103108
def set_r_quantile(self, alpha, round=True, precision=10):
109+
"""
110+
Defines
111+
112+
- self.r as the quantile of order alpha of self.D_probs,
113+
which can be used for rmax or rmin,
114+
to discard distances larger than rmax or smaller than rmin, respectively.
115+
- self.r_idx as the index of self.r in self.D_values
116+
"""
104117
if round:
105118
alpha = np.round(alpha, precision)
106119
self.D_probs = np.round(self.D_probs, precision)
@@ -114,28 +127,10 @@ def set_r_quantile(self, alpha, round=True, precision=10):
114127
self.r = int(self.D_values[self.r_idx])
115128
return
116129

117-
def set_r(self, r=None, n_sigma=3):
118-
if r is not None:
119-
self.r = r
120-
else:
121-
self.compute_moments()
122-
r = int(np.round(self.D_mu_emp - n_sigma * np.sqrt(self.D_var_emp), 0))
123-
if r >= self.D_values[0]:
124-
self.r = r
125-
else:
126-
self.r = self.D_values[0]
127-
128-
def set_r_idx(self, r_idx=None):
129-
if r_idx is not None:
130-
self.r_idx = r_idx
131-
else:
132-
aux = np.asarray(self.r == self.D_values).nonzero()[0]
133-
if len(aux) > 0:
134-
self.r_idx = aux[0]
135-
else:
136-
self.r_idx = 0
137-
138130
def compute_moments(self):
131+
"""
132+
computes the empirical mean and variance of H.D_probs
133+
"""
139134
self.D_mu_emp = np.dot(self.D_probs, self.D_values)
140135
self.D_var_emp = np.dot(self.D_probs, self.D_values**2) - self.D_mu_emp**2
141136

@@ -420,7 +415,7 @@ def minimize_KL(Op):
420415
class BID:
421416
def __init__(
422417
self,
423-
H=None, # instance of Hamming class defined above
418+
H=Hamming(), # instance of Hamming class defined above
424419
Op=None, # instance of Optimizer class defined above
425420
alphamin=0.0,
426421
alphamax=0.2,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
0,6666
2+
1,41186
3+
2,137369
4+
3,313850
5+
4,540111
6+
5,750004
7+
6,874420
8+
7,889678
9+
8,802703
10+
9,651278
11+
10,479772
12+
11,323353
13+
12,200816
14+
13,115544
15+
14,61751
16+
15,31203
17+
16,15125
18+
17,7053
19+
18,3306
20+
19,1465
21+
20,678
22+
21,252
23+
22,113
24+
23,38
25+
24,15
26+
25,4
27+
26,1
28+
27,2
29+
9974,1
30+
9975,2
31+
9976,13
32+
9977,48
33+
9978,100
34+
9979,309
35+
9980,659
36+
9981,1533
37+
9982,3227
38+
9983,7158
39+
9984,15026
40+
9985,31304
41+
9986,61676
42+
9987,115611
43+
9988,201143
44+
9989,323535
45+
9990,480037
46+
9991,651106
47+
9992,802960
48+
9993,889424
49+
9994,875168
50+
9995,750171
51+
9996,540532
52+
9997,313787
53+
9998,137434
54+
9999,41066
55+
10000,6714
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
0,6
2+
1,33
3+
2,270
4+
3,1236
5+
4,4595
6+
5,13063
7+
6,31216
8+
7,63822
9+
8,114823
10+
9,184118
11+
10,269691
12+
11,363323
13+
12,454201
14+
13,531295
15+
14,579959
16+
15,596097
17+
16,578923
18+
17,535015
19+
18,468375
20+
19,389550
21+
20,309200
22+
21,237401
23+
22,174358
24+
23,122598
25+
24,82780
26+
25,54954
27+
26,35340
28+
27,21848
29+
28,13026
30+
29,7502
31+
30,4355
32+
31,2361
33+
32,1256
34+
33,613
35+
34,298
36+
35,140
37+
36,68
38+
37,34
39+
38,9
40+
39,3
41+
40,1
42+
9961,2
43+
9962,8
44+
9963,28
45+
9964,54
46+
9965,121
47+
9966,288
48+
9967,599
49+
9968,1237
50+
9969,2296
51+
9970,4323
52+
9971,7625
53+
9972,13049
54+
9973,21842
55+
9974,35311
56+
9975,55161
57+
9976,83068
58+
9977,122785
59+
9978,174037
60+
9979,237456
61+
9980,310060
62+
9981,388835
63+
9982,467920
64+
9983,534727
65+
9984,580120
66+
9985,596245
67+
9986,580723
68+
9987,530779
69+
9988,454515
70+
9989,363580
71+
9990,269742
72+
9991,184248
73+
9992,114511
74+
9993,64065
75+
9994,31117
76+
9995,13145
77+
9996,4543
78+
9997,1266
79+
9998,264
80+
9999,45
81+
10000,4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
1,1
2+
7,5
3+
8,18
4+
9,81
5+
10,274
6+
11,798
7+
12,1775
8+
13,3685
9+
14,7443
10+
15,13464
11+
16,22482
12+
17,36597
13+
18,56221
14+
19,80779
15+
20,112896
16+
21,151290
17+
22,193759
18+
23,238575
19+
24,285791
20+
25,328497
21+
26,365695
22+
27,395586
23+
28,414940
24+
29,421795
25+
30,416480
26+
31,401741
27+
32,376444
28+
33,344360
29+
34,305943
30+
35,265864
31+
36,225529
32+
37,186434
33+
38,150782
34+
39,118859
35+
40,91679
36+
41,68849
37+
42,50612
38+
43,36474
39+
44,25792
40+
45,17612
41+
46,11700
42+
47,7736
43+
48,4938
44+
49,3062
45+
50,1917
46+
51,1110
47+
52,636
48+
53,363
49+
54,194
50+
55,108
51+
56,43
52+
57,29
53+
58,13
54+
59,3
55+
60,2
56+
61,1
57+
9939,1
58+
9940,1
59+
9941,5
60+
9942,10
61+
9943,22
62+
9944,47
63+
9945,91
64+
9946,192
65+
9947,332
66+
9948,610
67+
9949,1111
68+
9950,1835
69+
9951,3031
70+
9952,4968
71+
9953,7691
72+
9954,11862
73+
9955,17512
74+
9956,25627
75+
9957,36367
76+
9958,50879
77+
9959,68924
78+
9960,91656
79+
9961,119019
80+
9962,150657
81+
9963,186323
82+
9964,225640
83+
9965,266036
84+
9966,306072
85+
9967,344005
86+
9968,377027
87+
9969,401237
88+
9970,417891
89+
9971,421702
90+
9972,413982
91+
9973,395937
92+
9974,366806
93+
9975,328491
94+
9976,284850
95+
9977,239795
96+
9978,193465
97+
9979,151035
98+
9980,112872
99+
9981,81498
100+
9982,55695
101+
9983,36525
102+
9984,22859
103+
9985,13419
104+
9986,7324
105+
9987,3844
106+
9988,1807
107+
9989,745
108+
9990,294
109+
9991,99
110+
9992,18
111+
9993,1

0 commit comments

Comments
 (0)