Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 43 additions & 42 deletions Chapter1_Introduction/Chapter1.ipynb

Large diffs are not rendered by default.

610 changes: 241 additions & 369 deletions Chapter2_MorePyMC/Chapter2.ipynb

Large diffs are not rendered by default.

234 changes: 93 additions & 141 deletions Chapter3_MCMC/Chapter3.ipynb

Large diffs are not rendered by default.

327 changes: 152 additions & 175 deletions Chapter4_TheGreatestTheoremNeverTold/Chapter4.ipynb

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions Chapter4_TheGreatestTheoremNeverTold/top_crazyideas_submissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import sys

import numpy as np
from IPython.core.display import Image

import praw


reddit = praw.Reddit("BayesianMethodsForHackers")
subreddit = reddit.get_subreddit( "crazyideas" )

top_submissions = subreddit.get_top(limit=100)

n_sub = int( sys.argv[1] ) if sys.argv[1] else 1

i = 0
while i < n_sub:
top_submission = next(top_submissions)
#while "i.imgur.com" not in top_submission.url:
# #make sure it is linking to an image, not a webpage.
# top_submission = next(top_submissions)
i+=1

#print("Post contents: \n", top_submission.title)
top_post = top_submission.title
#top_submission.replace_more_comments(limit=5, threshold=0)
#print(top_post_url)

upvotes = []
downvotes = []
contents = []
"""_all_comments = top_submission.comments
all_comments=[]
for comment in _all_comments:
try:
#ups = int(round((ratio*comment.score)/(2*ratio - 1)) if ratio != 0.5 else round(comment.score/2))
#upvotes.append(ups)
#downvotes.append(ups - comment.score)
scores.append( comment.score )
contents.append( comment.body )
except Exception as e:
continue
"""
for sub in top_submissions:
try:
ratio = reddit.get_submission(sub.permalink).upvote_ratio
ups = int(round((ratio*sub.score)/(2*ratio - 1)) if ratio != 0.5 else round(sub.score/2))
upvotes.append(ups)
downvotes.append(ups - sub.score)
contents.append(sub.title)
except Exception as e:
continue
votes = np.array( [ upvotes, downvotes] ).T
63 changes: 0 additions & 63 deletions Chapter4_TheGreatestTheoremNeverTold/top_pic_comments.py

This file was deleted.

240 changes: 119 additions & 121 deletions Chapter5_LossFunctions/Chapter5.ipynb

Large diffs are not rendered by default.

27 changes: 13 additions & 14 deletions Chapter5_LossFunctions/DarkWorldsMetric.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def calc_delta_r(x_predicted,y_predicted,x_true,y_true):
for perm in it.permutations(a[num_halos-2],num_halos):
which_true_halos=[]
which_predicted_halos=[]
for j in xrange(num_halos): #loop through all the true halos with the
for j in range(num_halos): #loop through all the true halos with the

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

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


Expand Down Expand Up @@ -316,10 +316,9 @@ def main(user_fname, fname):
#first input would be
#a float, if succeed it
#is not a header
print 'THE INPUT FILE DOES NOT APPEAR TO HAVE A HEADER'
print('THE INPUT FILE DOES NOT APPEAR TO HAVE A HEADER')
except :
print 'THE INPUT FILE APPEARS TO HAVE A HEADER, SKIPPING THE FIRST LINE'

print('THE INPUT FILE APPEARS TO HAVE A HEADER, SKIPPING THE FIRST LINE')
skip_header = sky_prediction.next()


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


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

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


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions Chapter5_LossFunctions/draw_sky2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
from matplotlib.patches import Ellipse
import numpy as np

def draw_sky( galaxies ):
def draw_sky(galaxies):
"""adapted from Vishal Goklani"""
size_multiplier = 45
fig = plt.figure(figsize=(10,10))
#fig.patch.set_facecolor("blue")
ax = fig.add_subplot(111, aspect='equal')
n = galaxies.shape[0]
for i in xrange(n):
for i in range(n):
_g = galaxies[i,:]
x,y = _g[0], _g[1]
d = np.sqrt( _g[2]**2 + _g[3]**2 )
Expand Down
Loading