Skip to content

Commit f92a79b

Browse files
authored
Merge pull request #9 from quantopian/python3-update
Updated PyMC chapters to use Python 3.
2 parents f8e5d83 + d07d42b commit f92a79b

File tree

11 files changed

+816
-1030
lines changed

11 files changed

+816
-1030
lines changed

Chapter1_Introduction/Chapter1.ipynb

Lines changed: 43 additions & 42 deletions
Large diffs are not rendered by default.

Chapter2_MorePyMC/Chapter2.ipynb

Lines changed: 241 additions & 369 deletions
Large diffs are not rendered by default.

Chapter3_MCMC/Chapter3.ipynb

Lines changed: 93 additions & 141 deletions
Large diffs are not rendered by default.

Chapter4_TheGreatestTheoremNeverTold/Chapter4.ipynb

Lines changed: 152 additions & 175 deletions
Large diffs are not rendered by default.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import sys
2+
3+
import numpy as np
4+
from IPython.core.display import Image
5+
6+
import praw
7+
8+
9+
reddit = praw.Reddit("BayesianMethodsForHackers")
10+
subreddit = reddit.get_subreddit( "crazyideas" )
11+
12+
top_submissions = subreddit.get_top(limit=100)
13+
14+
n_sub = int( sys.argv[1] ) if sys.argv[1] else 1
15+
16+
i = 0
17+
while i < n_sub:
18+
top_submission = next(top_submissions)
19+
#while "i.imgur.com" not in top_submission.url:
20+
# #make sure it is linking to an image, not a webpage.
21+
# top_submission = next(top_submissions)
22+
i+=1
23+
24+
#print("Post contents: \n", top_submission.title)
25+
top_post = top_submission.title
26+
#top_submission.replace_more_comments(limit=5, threshold=0)
27+
#print(top_post_url)
28+
29+
upvotes = []
30+
downvotes = []
31+
contents = []
32+
"""_all_comments = top_submission.comments
33+
all_comments=[]
34+
for comment in _all_comments:
35+
try:
36+
#ups = int(round((ratio*comment.score)/(2*ratio - 1)) if ratio != 0.5 else round(comment.score/2))
37+
#upvotes.append(ups)
38+
#downvotes.append(ups - comment.score)
39+
scores.append( comment.score )
40+
contents.append( comment.body )
41+
except Exception as e:
42+
continue
43+
"""
44+
for sub in top_submissions:
45+
try:
46+
ratio = reddit.get_submission(sub.permalink).upvote_ratio
47+
ups = int(round((ratio*sub.score)/(2*ratio - 1)) if ratio != 0.5 else round(sub.score/2))
48+
upvotes.append(ups)
49+
downvotes.append(ups - sub.score)
50+
contents.append(sub.title)
51+
except Exception as e:
52+
continue
53+
votes = np.array( [ upvotes, downvotes] ).T

Chapter4_TheGreatestTheoremNeverTold/top_pic_comments.py

Lines changed: 0 additions & 63 deletions
This file was deleted.

Chapter5_LossFunctions/Chapter5.ipynb

Lines changed: 119 additions & 121 deletions
Large diffs are not rendered by default.

Chapter5_LossFunctions/DarkWorldsMetric.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def calc_delta_r(x_predicted,y_predicted,x_true,y_true):
5858
for perm in it.permutations(a[num_halos-2],num_halos):
5959
which_true_halos=[]
6060
which_predicted_halos=[]
61-
for j in xrange(num_halos): #loop through all the true halos with the
61+
for j in range(num_halos): #loop through all the true halos with the
6262

6363
distances_perm[count,j]=np.sqrt((x_true[j]-x_predicted[int(perm[j])])**2\
6464
+(y_true[j]-y_predicted[int(perm[j])])**2)
@@ -141,7 +141,7 @@ def convert_to_360(angle, x_in, y_in):
141141
theta: the angle in the range 0:2pi
142142
"""
143143
n = len(x_in)
144-
for i in xrange(n):
144+
for i in range(n):
145145
if x_in[i] < 0 and y_in[i] > 0:
146146
angle[i] = angle[i]+mt.pi
147147
elif x_in[i] < 0 and y_in[i] < 0:
@@ -204,7 +204,7 @@ def main_score( nhalo_all, x_true_all, y_true_all, x_ref_all, y_ref_all, sky_pre
204204

205205
x_predicted=np.array([],dtype=float)
206206
y_predicted=np.array([],dtype=float)
207-
for i in xrange(nhalo):
207+
for i in range(nhalo):
208208
x_predicted=np.append(x_predicted,float(sky[0])) #get the predicted values
209209
y_predicted=np.append(y_predicted,float(sky[1]))
210210
#The solution file for the test data provides masses
@@ -271,9 +271,9 @@ def main_score( nhalo_all, x_true_all, y_true_all, x_ref_all, y_ref_all, sky_pre
271271
W1=1./1000. #Weight the av_r such that < 1 is a good score > 1 is not so good.
272272
W2=1.
273273
metric = W1*av_r + W2*angle_vec #Weighted metric, weights TBD
274-
print 'Your average distance in pixels you are away from the true halo is', av_r
275-
print 'Your average angular vector is', angle_vec
276-
print 'Your score for the training data is', metric
274+
print('Your average distance in pixels you are away from the true halo is', av_r)
275+
print('Your average angular vector is', angle_vec)
276+
print('Your score for the training data is', metric)
277277
return metric
278278

279279

@@ -316,10 +316,9 @@ def main(user_fname, fname):
316316
#first input would be
317317
#a float, if succeed it
318318
#is not a header
319-
print 'THE INPUT FILE DOES NOT APPEAR TO HAVE A HEADER'
319+
print('THE INPUT FILE DOES NOT APPEAR TO HAVE A HEADER')
320320
except :
321-
print 'THE INPUT FILE APPEARS TO HAVE A HEADER, SKIPPING THE FIRST LINE'
322-
321+
print('THE INPUT FILE APPEARS TO HAVE A HEADER, SKIPPING THE FIRST LINE')
323322
skip_header = sky_prediction.next()
324323

325324

@@ -331,7 +330,7 @@ def main(user_fname, fname):
331330
if does_it_exist > 0: #If it does then find the matching solutions to the sky_id
332331
selectskyinsolutions=true_sky_id.index(sky_id)-1
333332
else: #Otherwise exit
334-
print 'Sky_id does not exist, formatting problem: ',sky_id
333+
print('Sky_id does not exist, formatting problem: ',sky_id)
335334
sys.exit(2)
336335

337336

@@ -342,7 +341,7 @@ def main(user_fname, fname):
342341

343342
x_predicted=np.array([],dtype=float)
344343
y_predicted=np.array([],dtype=float)
345-
for i in xrange(nhalo):
344+
for i in range(nhalo):
346345
x_predicted=np.append(x_predicted,float(sky[2*i+1])) #get the predicted values
347346
y_predicted=np.append(y_predicted,float(sky[2*i+2]))
348347
#The solution file for the test data provides masses
@@ -409,9 +408,9 @@ def main(user_fname, fname):
409408
W1=1./1000. #Weight the av_r such that < 1 is a good score > 1 is not so good.
410409
W2=1.
411410
metric = W1*av_r + W2*angle_vec #Weighted metric, weights TBD
412-
print 'Your average distance in pixels you are away from the true halo is', av_r
413-
print 'Your average angular vector is', angle_vec
414-
print 'Your score for the training data is', metric
411+
print('Your average distance in pixels you are away from the true halo is', av_r)
412+
print('Your average angular vector is', angle_vec)
413+
print('Your score for the training data is', metric)
415414

416415

417416
if __name__ == "__main__":

Chapter5_LossFunctions/draw_sky2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
from matplotlib.patches import Ellipse
33
import numpy as np
44

5-
def draw_sky( galaxies ):
5+
def draw_sky(galaxies):
66
"""adapted from Vishal Goklani"""
77
size_multiplier = 45
88
fig = plt.figure(figsize=(10,10))
99
#fig.patch.set_facecolor("blue")
1010
ax = fig.add_subplot(111, aspect='equal')
1111
n = galaxies.shape[0]
12-
for i in xrange(n):
12+
for i in range(n):
1313
_g = galaxies[i,:]
1414
x,y = _g[0], _g[1]
1515
d = np.sqrt( _g[2]**2 + _g[3]**2 )

0 commit comments

Comments
 (0)