Skip to content

Commit 5e45764

Browse files
author
Syed Nasim
committed
formatted code using black
1 parent db63746 commit 5e45764

File tree

6 files changed

+190
-181
lines changed

6 files changed

+190
-181
lines changed

clusters.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,50 @@
11
import points as pointsclass
22
import math
33

4+
45
def sdown(x):
5-
y = x*10000
6+
y = x * 10000
67
z = int(y)
7-
return (z/10000)
8+
return z / 10000
9+
10+
11+
def dist(epi_x, epi_y, pt_x, pt_y):
12+
# return (((epi_x-pt_x)**2) + ((epi_y-pt_y)**2))**0.5
13+
return math.sqrt(math.pow(epi_x - pt_x, 2.0) + math.pow(epi_y - pt_y, 2.0))
814

9-
def dist(epi_x,epi_y, pt_x,pt_y):
10-
#return (((epi_x-pt_x)**2) + ((epi_y-pt_y)**2))**0.5
11-
return math.sqrt(math.pow(epi_x - pt_x,2.0) + math.pow(epi_y - pt_y,2.0))
1215

1316
def finalize(clusters, points):
14-
nos = len(clusters)
15-
for i in range(1, nos+1):
16-
pts = pointsclass.getpoints_cl(points, i-1)
17-
clusters[i-1] ['points'].append(pts)
18-
return clusters
17+
nos = len(clusters)
18+
for i in range(1, nos + 1):
19+
pts = pointsclass.getpoints_cl(points, i - 1)
20+
clusters[i - 1]["points"].append(pts)
21+
return clusters
22+
1923

2024
def update_epi(clusters, points):
2125

22-
for i in range(1, len(clusters)+1):
23-
xsum = 0
24-
ysum = 0
26+
for i in range(1, len(clusters) + 1):
27+
xsum = 0
28+
ysum = 0
2529

26-
pts = pointsclass.getpoints_cl(points, i-1)
27-
net = len(pts)
30+
pts = pointsclass.getpoints_cl(points, i - 1)
31+
net = len(pts)
2832

29-
#Ignore clusters with no points
30-
if net == 0:
31-
continue
33+
# Ignore clusters with no points
34+
if net == 0:
35+
continue
3236

33-
for x in pts:
34-
xsum += int(x['x'])
35-
for y in pts:
36-
ysum += int(y['y'])
37+
for x in pts:
38+
xsum += int(x["x"])
39+
for y in pts:
40+
ysum += int(y["y"])
3741

38-
xav = (xsum/net)
39-
yav = (ysum/net)
42+
xav = xsum / net
43+
yav = ysum / net
4044

41-
clusters[i-1]['epi_x'] = xav
42-
clusters[i-1]['epi_y'] = yav
45+
clusters[i - 1]["epi_x"] = xav
46+
clusters[i - 1]["epi_y"] = yav
4347

44-
#print(i, clusters[i-1]['epi_x'], clusters[i-1]['epi_y'])
48+
# print(i, clusters[i-1]['epi_x'], clusters[i-1]['epi_y'])
4549

46-
return clusters
50+
return clusters

ident.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
def sdown(x):
2-
y = x*100
2+
y = x * 100
33
z = int(y)
4-
return (z/100)
4+
return z / 100
5+
56

67
def nameclusters(clusters, setup):
78

89
if setup == True:
910
rep = input("Names have already been set. Do you want to repeat? (y/n)")
10-
if rep == 'n':
11+
if rep == "n":
1112
return [clusters, setup]
1213
else:
13-
pass
14+
pass
1415

1516
i = 1
1617
print()
1718

1819
print(len(clusters), "clusters have been identified.")
1920
for c0 in clusters:
20-
pts = len(c0['points'][0])
21+
pts = len(c0["points"][0])
2122
print(" - Cluster", i, "has ", pts, "elements.")
2223
i += 1
2324
print()
2425

2526
i = 1
2627
for c1 in clusters:
27-
print(" - Cluster", i, ": ", "(", sdown(c1['epi_x']), "," , sdown(c1['epi_y']), ")")
28+
print(
29+
" - Cluster", i, ": ", "(", sdown(c1["epi_x"]), ",", sdown(c1["epi_y"]), ")"
30+
)
2831
i += 1
2932
print()
30-
33+
3134
i = 1
3235
for c2 in clusters:
3336
print(" - Enter title for cluster ", i, end="")
34-
c2['title'] = input(": ")
37+
c2["title"] = input(": ")
3538
i += 1
3639

37-
38-
return [clusters, True]
40+
return [clusters, True]

main.py

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
# STEP 5: Update epicenters
2727
# STEP 6: Repeat 3 until epicenters don't change
2828

29+
2930
def train(csvfile):
3031

3132
# STEP 1
@@ -42,24 +43,26 @@ def train(csvfile):
4243
glob_cluster_list = []
4344
glob_nos_clusters = 0
4445

45-
glob_nos_clusters = int(input("\nEnter the number of clusters clearly visible in the plot: "))
46-
for i in range (1, glob_nos_clusters+1):
46+
glob_nos_clusters = int(
47+
input("\nEnter the number of clusters clearly visible in the plot: ")
48+
)
49+
for i in range(1, glob_nos_clusters + 1):
4750
temp_dict = {}
48-
temp_dict["epi_x"] = random.randint(1,10)
49-
temp_dict["epi_y"] = random.randint(1,10)
50-
temp_dict['points'] = []
51-
temp_dict['title'] = i
51+
temp_dict["epi_x"] = random.randint(1, 10)
52+
temp_dict["epi_y"] = random.randint(1, 10)
53+
temp_dict["points"] = []
54+
temp_dict["title"] = i
5255

5356
glob_cluster_list.append(temp_dict)
54-
#print("- Initialized ", glob_nos_clusters, " clusters")
57+
# print("- Initialized ", glob_nos_clusters, " clusters")
5558

56-
#print ("INIT DEBUG \n")
57-
#for k in glob_cluster_list:
59+
# print ("INIT DEBUG \n")
60+
# for k in glob_cluster_list:
5861
# print (k['epi_x'], k['epi_y'], end=" ")
5962
# print()
6063

61-
for l in range (glob_nos_points*5):
62-
#Run assignment
64+
for l in range(glob_nos_points * 5):
65+
# Run assignment
6366
glob_points_list = points.nextassign(glob_cluster_list, glob_points_list)
6467
glob_cluster_list = clusters.update_epi(glob_cluster_list, glob_points_list)
6568

@@ -69,33 +72,33 @@ def train(csvfile):
6972
new_x = new_y = 0
7073
change = 0
7174

72-
#record previous epicenters:
75+
# record previous epicenters:
7376
for i in glob_cluster_list:
74-
prev_x += i['epi_x']
75-
prev_y += i['epi_y']
77+
prev_x += i["epi_x"]
78+
prev_y += i["epi_y"]
7679

77-
#Run assignment
80+
# Run assignment
7881
glob_points_list = points.nextassign(glob_cluster_list, glob_points_list)
7982
glob_cluster_list = clusters.update_epi(glob_cluster_list, glob_points_list)
8083

81-
#record new epicenters:
84+
# record new epicenters:
8285
for j in glob_cluster_list:
83-
new_x += j['epi_x']
84-
new_y += j['epi_y']
86+
new_x += j["epi_x"]
87+
new_y += j["epi_y"]
8588

8689
change = abs(prev_x - new_x) + abs(prev_y - new_y)
8790
if change == 0:
8891
break
8992

90-
print ("FIN DEBUG \n")
93+
print("FIN DEBUG \n")
9194
for k in glob_cluster_list:
92-
print (k['epi_x'], k['epi_y'], end=" ")
93-
print()
94-
#print ("\n\nPOINTS:: ")
95-
#for p in glob_points_list:
95+
print(k["epi_x"], k["epi_y"], end=" ")
96+
print()
97+
# print ("\n\nPOINTS:: ")
98+
# for p in glob_points_list:
9699
# print(p)
97100

98-
glob_cluster_list = clusters.finalize(glob_cluster_list,glob_points_list)
101+
glob_cluster_list = clusters.finalize(glob_cluster_list, glob_points_list)
99102

100103
print("Hopefully, now the epicenters are correctly arranged")
101104
global glob_trained
@@ -104,36 +107,36 @@ def train(csvfile):
104107

105108
print(" ** \n Welcome to KMeansExample.\n **\n")
106109
csvfile = input("Please enter the csv file containing the student records: ")
107-
#csvfile = 'data/simple.csv'
110+
# csvfile = 'data/simple.csv'
108111
print("Working on student records at ", csvfile, " ..")
109112

110113
while True:
111-
print ("\n\n")
112-
print (" * (1) for previewing the records")
113-
print (" * (2) for proceeding with training")
114-
print (" * (3) for getting a prediction")
115-
print (" * (4) for Exiting the predictor")
114+
print("\n\n")
115+
print(" * (1) for previewing the records")
116+
print(" * (2) for proceeding with training")
117+
print(" * (3) for getting a prediction")
118+
print(" * (4) for Exiting the predictor")
116119

117120
if glob_trained == True:
118-
print (" * (5) for Rich preview")
119-
print (" * (6) for Naming clusters")
120-
print (" * (7) for Viewing points")
121+
print(" * (5) for Rich preview")
122+
print(" * (6) for Naming clusters")
123+
print(" * (7) for Viewing points")
121124

122125
i = int(input("Enter action: "))
123126

124127
if i == 1:
125128
print("Previewing the records: ")
126129
preview.preview(csvfile)
127-
130+
128131
elif i == 2:
129132
train(csvfile)
130133

131134
elif i == 3:
132135
predict.predict(glob_cluster_list)
133-
136+
134137
elif i == 4:
135138
break
136-
139+
137140
elif i == 5:
138141
pass
139142
preview.richprev(glob_cluster_list)
@@ -143,7 +146,7 @@ def train(csvfile):
143146
glob_cluster_list = ret[0]
144147
glob_named = ret[1]
145148
print("Names set.")
146-
149+
147150
elif i == 7:
148151
points.view(glob_cluster_list)
149152

0 commit comments

Comments
 (0)