Skip to content

Commit a810cf0

Browse files
author
ugurthemaster
committed
Update DarkWorldsMetric.py
Minor corrections
1 parent 596dcd5 commit a810cf0

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

Chapter5_LossFunctions/DarkWorldsMetric.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def calc_delta_r(x_predicted,y_predicted,x_true,y_true):
2323
""" Compute the scalar distance between predicted halo centers
2424
and the true halo centers. Predictions are matched to the closest
2525
halo center.
26-
Notes: It takes in the predicted and true positions, and then loops over each possile configuration and finds the most optimal one.
26+
Notes: It takes in the predicted and true positions, and then loops over each possible configuration and finds the most optimal one.
2727
Arguments:
2828
x_predicted, y_predicted: vector for predicted x- and y-positions (1 to 3 elements)
2929
x_true, y_true: vector for known x- and y-positions (1 to 3 elements)
@@ -34,11 +34,11 @@ def calc_delta_r(x_predicted,y_predicted,x_true,y_true):
3434
e.g if true_halo_indexes=[0,1] and measured_halo_indexes=[1,0] then the first x,y coordinates of the true halo position matches the second input of the predicted x,y coordinates.
3535
"""
3636

37-
num_halos=len(x_true) #Only works for number of halso > 1
37+
num_halos=len(x_true) #Only works for number of halos > 1
3838
num_configurations=mt.factorial(num_halos) #The number of possible different comb
3939
configurations=np.zeros([num_halos,num_configurations],int) #The array of combinations
4040
#I will pass back
41-
distances = np.zeros([num_configurations],float) #THe array of the distances
41+
distances = np.zeros([num_configurations],float) #The array of the distances
4242
#for all possible combinations
4343

4444
radial_distance=[] #The vector of distances
@@ -49,7 +49,7 @@ def calc_delta_r(x_predicted,y_predicted,x_true,y_true):
4949
count=0 #For the index of the distances array
5050
true_halo_indexes=[] #The tuples which will show the order of halos picked
5151
predicted_halo_indexes=[]
52-
distances_perm=np.zeros([num_configurations,num_halos],float) #The distance between eac
52+
distances_perm=np.zeros([num_configurations,num_halos],float) #The distance between each
5353
#true and predicted
5454
#halo for every comb
5555
true_halo_indexes_perm=[] #log of all the permutations of true halos used
@@ -63,9 +63,9 @@ def calc_delta_r(x_predicted,y_predicted,x_true,y_true):
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)
6565
#This array logs the distance between true and
66-
#predicted halo for ALL configruations
66+
#predicted halo for ALL configurations
6767

68-
which_true_halos.append(j) #logthe order in which I try each true halo
68+
which_true_halos.append(j) #log the order in which I try each true halo
6969
which_predicted_halos.append(int(perm[j])) #log the order in which I true
7070
#each predicted halo
7171
true_halo_indexes_perm.append(which_true_halos) #this is a tuple of tuples of
@@ -109,7 +109,7 @@ def calc_theta(x_predicted, y_predicted, x_true, y_true, x_ref, y_ref):
109109

110110

111111
# Angle at which the halo is at
112-
#with respect to the reference poitn
112+
#with respect to the reference point
113113
phi[x_true != x_ref] = np.arctan((y_predicted[x_true != x_predicted]-\
114114
y_true[x_true != x_predicted])\
115115
/(x_predicted[x_true != x_predicted]-\
@@ -165,7 +165,7 @@ def get_ref(x_halo,y_halo,weight):
165165
""" Gets the reference point of the system of halos by weighted averaging the x and y
166166
coordinates.
167167
Arguments:
168-
x_halo, y_halo: Vector num_halos referrin to the coordinates of the halos
168+
x_halo, y_halo: Vector num_halos referring to the coordinates of the halos
169169
weight: the weight which will be assigned to the position of the halo
170170
num_halos: number of halos in the system
171171
Returns:
@@ -190,7 +190,7 @@ def main_score( nhalo_all, x_true_all, y_true_all, x_ref_all, y_ref_all, sky_pre
190190
r=np.array([],dtype=float) # The array which I will log all the calculated radial distances
191191
angle=np.array([],dtype=float) #The array which I will log all the calculated angles
192192
#Load in the sky_ids from the true
193-
num_halos_total=0 #Keep track of how many halos are iput into the metric
193+
num_halos_total=0 #Keep track of how many halos are input into the metric
194194

195195

196196

@@ -205,12 +205,12 @@ def main_score( nhalo_all, x_true_all, y_true_all, x_ref_all, y_ref_all, sky_pre
205205
x_predicted=np.array([],dtype=float)
206206
y_predicted=np.array([],dtype=float)
207207
for i in xrange(nhalo):
208-
x_predicted=np.append(x_predicted,float(sky[0])) #get the predictd values
208+
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
211211
#to calculate the centre of mass where as the Training_halo.csv
212212
#direct provides x_ref y_ref. So in the case of test data
213-
#we need to calculae the ref point from the masses using
213+
#we need to calculate the ref point from the masses using
214214
#Get_ref()
215215

216216
x_ref=x_ref_all[selectskyinsolutions]
@@ -219,7 +219,7 @@ def main_score( nhalo_all, x_true_all, y_true_all, x_ref_all, y_ref_all, sky_pre
219219
num_halos_total=num_halos_total+nhalo
220220

221221

222-
#Single halo case, this needs to be separately caluclated since
222+
#Single halo case, this needs to be separately calculated since
223223
#x_ref = x_true
224224
if nhalo == 1:
225225
#What is the radial distance between the true and predicted position
@@ -260,15 +260,15 @@ def main_score( nhalo_all, x_true_all, y_true_all, x_ref_all, y_ref_all, sky_pre
260260
# Find what the average distance the estimate is from the halo position
261261
av_r=sum(r)/len(r)
262262

263-
#In order to quanitfy the orientation invariance we will express each angle
264-
# as a vector and find the average vecor
263+
#In order to quantify the orientation invariance we will express each angle
264+
# as a vector and find the average vector
265265
#R_bar^2=(1/N Sum^Ncos(theta))^2+(1/N Sum^Nsin(theta))**2
266266

267267
N = float(num_halos_total)
268268
angle_vec = np.sqrt(( 1.0/N * sum(np.cos(angle)) )**2 + \
269269
( 1.0/N * sum(np.sin(angle)) )**2)
270270

271-
W1=1./1000. #Weight the av_r such that < 1 i a good score > 1 isnt so good.
271+
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
274274
print 'Your average distance in pixels you are away from the true halo is', av_r
@@ -304,7 +304,7 @@ def main(user_fname, fname):
304304

305305

306306

307-
num_halos_total=0 #Keep track of how many halos are iput into the metric
307+
num_halos_total=0 #Keep track of how many halos are input into the metric
308308

309309

310310
sky_prediction = c.reader(open(user_fname, 'rb')) #Open the result.csv
@@ -314,9 +314,9 @@ def main(user_fname, fname):
314314
with open(user_fname, 'r') as f:
315315
header = float((f.readline()).split(',')[1]) #try and make where the
316316
#first input would be
317-
#a float, if succed its
318-
#not a header
319-
print 'THE INPUT FILE DOESNT APPEAR TO HAVE A HEADER'
317+
#a float, if succeed it
318+
#is not a header
319+
print 'THE INPUT FILE DOES NOT APPEAR TO HAVE A HEADER'
320320
except :
321321
print 'THE INPUT FILE APPEARS TO HAVE A HEADER, SKIPPING THE FIRST LINE'
322322

@@ -343,7 +343,7 @@ def main(user_fname, fname):
343343
x_predicted=np.array([],dtype=float)
344344
y_predicted=np.array([],dtype=float)
345345
for i in xrange(nhalo):
346-
x_predicted=np.append(x_predicted,float(sky[2*i+1])) #get the predictd values
346+
x_predicted=np.append(x_predicted,float(sky[2*i+1])) #get the predicted values
347347
y_predicted=np.append(y_predicted,float(sky[2*i+2]))
348348
#The solution file for the test data provides masses
349349
#to calculate the centre of mass where as the Training_halo.csv
@@ -357,7 +357,7 @@ def main(user_fname, fname):
357357
num_halos_total=num_halos_total+nhalo
358358

359359

360-
#Single halo case, this needs to be separately caluclated since
360+
#Single halo case, this needs to be separately calculated since
361361
#x_ref = x_true
362362
if nhalo == 1:
363363
#What is the radial distance between the true and predicted position
@@ -398,15 +398,15 @@ def main(user_fname, fname):
398398
# Find what the average distance the estimate is from the halo position
399399
av_r=sum(r)/len(r)
400400

401-
#In order to quanitfy the orientation invariance we will express each angle
402-
# as a vector and find the average vecor
401+
#In order to quantify the orientation invariance we will express each angle
402+
# as a vector and find the average vector
403403
#R_bar^2=(1/N Sum^Ncos(theta))^2+(1/N Sum^Nsin(theta))**2
404404

405405
N = float(num_halos_total)
406406
angle_vec = np.sqrt(( 1.0/N * sum(np.cos(angle)) )**2 + \
407407
( 1.0/N * sum(np.sin(angle)) )**2)
408408

409-
W1=1./1000. #Weight the av_r such that < 1 i a good score > 1 isnt so good.
409+
W1=1./1000. #Weight the av_r such that < 1 is a good score > 1 is not so good.
410410
W2=1.
411411
metric = W1*av_r + W2*angle_vec #Weighted metric, weights TBD
412412
print 'Your average distance in pixels you are away from the true halo is', av_r

0 commit comments

Comments
 (0)