From 5e17688870d04f4a0ef259431f73287ea21b793d Mon Sep 17 00:00:00 2001 From: hanemile Date: Sat, 9 Dec 2017 14:19:59 +0100 Subject: minor ajustments --- src/coord.py | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100755 src/coord.py (limited to 'src/coord.py') diff --git a/src/coord.py b/src/coord.py new file mode 100755 index 0000000..f801aee --- /dev/null +++ b/src/coord.py @@ -0,0 +1,146 @@ +#!/usr/bin/env python + +# import libraries +import time +import numpy as np +import sys + +# define the number of stars that should be generated +# (this is not the final value -> the final value is about 1000 times smaller) +nos = int(sys.argv[1]) + +# define some arrays for storing the values +arr_stars = np.zeros((int(nos), 3)) +arr_r = np.zeros((int(nos), 1)) +arr_saved_stars = np.zeros((int(nos), 3)) + +# define various paths +path = "data/rho6.csv" +save_path = "stars/star18.csv" +# star13 -> 586 stars + +# define the random-value range [rho_min; rho_max] +rand_min = 0 +rand_max = 1477.1586582000994 + +# define the range (size) of the galaxy +range_min = -1e7 +range_max = 1e7 + +# main function +def main(): + + time_start_gen_array = time.time() + + # generate n=nos stars + for i in range(0, nos): + + # generate the random values + arr_stars[i][0] = np.random.uniform(range_min, range_max, size=1) + arr_stars[i][1] = np.random.uniform(range_min, range_max, size=1) + arr_stars[i][2] = np.random.uniform(range_min, range_max, size=1) + + # calculate the distance of the star to the center of the galaxy + arr_r[i][0] = np.sqrt(pow(arr_stars[i][0], 2) + pow(arr_stars[i][1], 2) + pow(arr_stars[i][2], 2)) + + # print the randomly generated arrays + print(arr_stars) + print(arr_r) + + time_end_gen_array = time.time() + time_all_gen_array = time_end_gen_array - time_start_gen_array + # define the variables for storing the amount of stars kept or kicked away + stars_kept = 0 + stars_kicked = 0 + + # start the timer + start = time.time() + + # open the rho file + with open(path) as data: + + # read out the lines from the rho file + rho_file = data.readlines() + + # for every star... + for i in range(0, nos): + # print(i) + + # random value + a = np.random.uniform(rand_min, rand_max, size=1) + + # corresponding rho value + b = float(rho_file[int(round(arr_r[i][0], 0))].split(", ")[1].strip("\n")) + + # if the random value is smaller than the corresponding rho value + if( a < b): + # add the coordinate to arr_saved_stars + arr_saved_stars[stars_kept][0] = arr_stars[i][0] + arr_saved_stars[stars_kept][1] = arr_stars[i][1] + arr_saved_stars[stars_kept][2] = arr_stars[i][2] + + # increment the star_kept counter + stars_kept += 1 + + else: + # increment the star_kicked counter + stars_kicked += 1 + + if(i % (nos/(nos/10)) == 0): + if(i != 0): + a = str(round(i / nos * 100, 1)) + "%" + time_temp = time.time() + time_past = round(time_temp - start, 2) + print("{:<10}{:<20}".format(a, time_past)) + + print("") + end = time.time() + whole_time = end - start + print(">> Finished generating stars in " + str(whole_time) + " seconds\n") + + start_delete_rows = time.time() + + # delete all unused rows + print(">> Deleting unused rows in the arr_saved_stars array") + # for i in range(nos - stars_kicked, nos): + # np.delete(arr_saved_stars, (i), axis=0) + end_delete_rows = time.time() + time_delete_rows = end_delete_rows - start_delete_rows + print(">> Finished deleting stars in " + str(round(time_delete_rows, 4)) + " seconds \n") + + start_write_file = time.time() + + # write the star coordinates to a file + print(">> Writing the star-data to " + save_path) + with open(save_path, "a") as stars_data: + for i in range(0, nos): + x = arr_saved_stars[i][0] + y = arr_saved_stars[i][1] + z = arr_saved_stars[i][2] + + stars_data.write(str(x) + ", " + str(y) + ", " + str(z) + "\n") + + end_write_file = time.time() + time_write_file = end_write_file - start_write_file + print(">> Finished writing star-data to " + save_path + " in " + str(round(time_write_file, 4)) + " seconds\n") + + + stars_percent = stars_kept / nos * 100 + + time_all = whole_time + time_write_file + time_delete_rows + + # print some stats + print("") + print("{:<30}{:<30}".format("Time (complete)", str(round(time_all, 4)) + " seconds")) + print("{:<30}{:<30}".format("Time (gen arrays)", str(round(time_all_gen_array, 4)) + " seconds")) + print("{:<30}{:<30}".format("Time (calculate stars)", str(round(whole_time, 4)) + " seconds")) + print("{:<30}{:<30}".format("Time (delete rows)", str(round(time_delete_rows, 4)) + " seconds")) + print("{:<30}{:<30}".format("Time (write to file)", str(round(time_write_file, 4)) + " seconds")) + print("{:-<40}".format("")) + print("{:<20}{:<20}".format("Number of Stars", str(nos))) + print("{:<20}{:<20}".format("Stars Kept:", str(stars_kept))) + print("{:<20}{:<20}".format("Stars Kicked:", str(stars_kicked))) + print("{:<20}{:<20}".format("Stars Percent", str(round(stars_percent, 4)) + "%")) + +if __name__ == "__main__": + main() -- cgit 1.4.1 From 95772c59a241cd224310aa209b3ff976080ccdb5 Mon Sep 17 00:00:00 2001 From: hanemile Date: Sat, 9 Dec 2017 15:49:09 +0100 Subject: added feature for exactly generating n files --- src/coord.py | 84 +++++++++++++++++++----------------------------------------- 1 file changed, 27 insertions(+), 57 deletions(-) (limited to 'src/coord.py') diff --git a/src/coord.py b/src/coord.py index f801aee..b5d9f49 100755 --- a/src/coord.py +++ b/src/coord.py @@ -6,17 +6,15 @@ import numpy as np import sys # define the number of stars that should be generated -# (this is not the final value -> the final value is about 1000 times smaller) nos = int(sys.argv[1]) # define some arrays for storing the values arr_stars = np.zeros((int(nos), 3)) -arr_r = np.zeros((int(nos), 1)) arr_saved_stars = np.zeros((int(nos), 3)) # define various paths path = "data/rho6.csv" -save_path = "stars/star18.csv" +save_path = "stars/star19.csv" # star13 -> 586 stars # define the random-value range [rho_min; rho_max] @@ -30,28 +28,10 @@ range_max = 1e7 # main function def main(): - time_start_gen_array = time.time() - - # generate n=nos stars - for i in range(0, nos): - - # generate the random values - arr_stars[i][0] = np.random.uniform(range_min, range_max, size=1) - arr_stars[i][1] = np.random.uniform(range_min, range_max, size=1) - arr_stars[i][2] = np.random.uniform(range_min, range_max, size=1) - - # calculate the distance of the star to the center of the galaxy - arr_r[i][0] = np.sqrt(pow(arr_stars[i][0], 2) + pow(arr_stars[i][1], 2) + pow(arr_stars[i][2], 2)) - - # print the randomly generated arrays - print(arr_stars) - print(arr_r) - - time_end_gen_array = time.time() - time_all_gen_array = time_end_gen_array - time_start_gen_array # define the variables for storing the amount of stars kept or kicked away stars_kept = 0 stars_kicked = 0 + i = 0 # start the timer start = time.time() @@ -63,54 +43,50 @@ def main(): rho_file = data.readlines() # for every star... - for i in range(0, nos): - # print(i) + # for i in range(0, nos): + while(stars_kept < nos): + + # generate the random star-coordinates + x = np.random.uniform(range_min, range_max, size=1) + y = np.random.uniform(range_min, range_max, size=1) + z = np.random.uniform(range_min, range_max, size=1) + + # calculate the distance of the star to the center of the galaxy + r = np.sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2)) - # random value + # generate a random value in the range [rand_min; rand_max] a = np.random.uniform(rand_min, rand_max, size=1) - # corresponding rho value - b = float(rho_file[int(round(arr_r[i][0], 0))].split(", ")[1].strip("\n")) + # read out the corresponding rho value from the lookuptable (rho-file) + b = float(rho_file[int(round(r[0], 0))].split(", ")[1].strip("\n")) + + # print("{:<5}{:<20}{:<20}{:<20}{:<20}{:<20}{:<20}".format(str(stars_kept), str(x), str(y), str(z), str(a), str(b), str(r))) # if the random value is smaller than the corresponding rho value - if( a < b): + if(a < b): # add the coordinate to arr_saved_stars - arr_saved_stars[stars_kept][0] = arr_stars[i][0] - arr_saved_stars[stars_kept][1] = arr_stars[i][1] - arr_saved_stars[stars_kept][2] = arr_stars[i][2] + arr_saved_stars[stars_kept][0] = x + arr_saved_stars[stars_kept][1] = y + arr_saved_stars[stars_kept][2] = z # increment the star_kept counter stars_kept += 1 + print(stars_kept) else: # increment the star_kicked counter stars_kicked += 1 - if(i % (nos/(nos/10)) == 0): - if(i != 0): - a = str(round(i / nos * 100, 1)) + "%" - time_temp = time.time() - time_past = round(time_temp - start, 2) - print("{:<10}{:<20}".format(a, time_past)) + # increment i + i = i + 1 print("") end = time.time() whole_time = end - start print(">> Finished generating stars in " + str(whole_time) + " seconds\n") - start_delete_rows = time.time() - - # delete all unused rows - print(">> Deleting unused rows in the arr_saved_stars array") - # for i in range(nos - stars_kicked, nos): - # np.delete(arr_saved_stars, (i), axis=0) - end_delete_rows = time.time() - time_delete_rows = end_delete_rows - start_delete_rows - print(">> Finished deleting stars in " + str(round(time_delete_rows, 4)) + " seconds \n") - - start_write_file = time.time() - # write the star coordinates to a file + start_write_file = time.time() print(">> Writing the star-data to " + save_path) with open(save_path, "a") as stars_data: for i in range(0, nos): @@ -124,23 +100,17 @@ def main(): time_write_file = end_write_file - start_write_file print(">> Finished writing star-data to " + save_path + " in " + str(round(time_write_file, 4)) + " seconds\n") - - stars_percent = stars_kept / nos * 100 - - time_all = whole_time + time_write_file + time_delete_rows + time_all = whole_time + time_write_file # print some stats print("") print("{:<30}{:<30}".format("Time (complete)", str(round(time_all, 4)) + " seconds")) - print("{:<30}{:<30}".format("Time (gen arrays)", str(round(time_all_gen_array, 4)) + " seconds")) print("{:<30}{:<30}".format("Time (calculate stars)", str(round(whole_time, 4)) + " seconds")) - print("{:<30}{:<30}".format("Time (delete rows)", str(round(time_delete_rows, 4)) + " seconds")) print("{:<30}{:<30}".format("Time (write to file)", str(round(time_write_file, 4)) + " seconds")) print("{:-<40}".format("")) print("{:<20}{:<20}".format("Number of Stars", str(nos))) - print("{:<20}{:<20}".format("Stars Kept:", str(stars_kept))) print("{:<20}{:<20}".format("Stars Kicked:", str(stars_kicked))) - print("{:<20}{:<20}".format("Stars Percent", str(round(stars_percent, 4)) + "%")) + print("{:<20}{:<20}".format("Percent: ", str( nos / stars_kicked * 100 ) + "%")) if __name__ == "__main__": main() -- cgit 1.4.1 From 23aa8723bc8c6ba0e96bc7cbac88075591116110 Mon Sep 17 00:00:00 2001 From: hanemile Date: Sat, 9 Dec 2017 16:01:33 +0100 Subject: added command line argument for specifying the location where the star coordinates should be saved --- src/coord.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/coord.py') diff --git a/src/coord.py b/src/coord.py index b5d9f49..4094b25 100755 --- a/src/coord.py +++ b/src/coord.py @@ -14,7 +14,7 @@ arr_saved_stars = np.zeros((int(nos), 3)) # define various paths path = "data/rho6.csv" -save_path = "stars/star19.csv" +save_path = "stars/" + sys.argv[2] # star13 -> 586 stars # define the random-value range [rho_min; rho_max] -- cgit 1.4.1 From 073e28e0e052941cf0913182944df6cf6363764d Mon Sep 17 00:00:00 2001 From: hanemile Date: Sat, 9 Dec 2017 17:53:54 +0100 Subject: fixed some bugs --- src/coord.py | 5 +++-- src/gen.py | 66 ------------------------------------------------------------ 2 files changed, 3 insertions(+), 68 deletions(-) delete mode 100755 src/gen.py (limited to 'src/coord.py') diff --git a/src/coord.py b/src/coord.py index 4094b25..932cf80 100755 --- a/src/coord.py +++ b/src/coord.py @@ -102,11 +102,12 @@ def main(): time_all = whole_time + time_write_file + time_min = round(time_all / 60, 1) + # print some stats print("") print("{:<30}{:<30}".format("Time (complete)", str(round(time_all, 4)) + " seconds")) - print("{:<30}{:<30}".format("Time (calculate stars)", str(round(whole_time, 4)) + " seconds")) - print("{:<30}{:<30}".format("Time (write to file)", str(round(time_write_file, 4)) + " seconds")) + print("{:<30}{:<30}".format("Time (complete)", str(round(time_min, 4)) + " minutes")) print("{:-<40}".format("")) print("{:<20}{:<20}".format("Number of Stars", str(nos))) print("{:<20}{:<20}".format("Stars Kicked:", str(stars_kicked))) diff --git a/src/gen.py b/src/gen.py deleted file mode 100755 index b4b7933..0000000 --- a/src/gen.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python - -from numpy import genfromtxt -import numpy as np -import math -import os -import socket -import time -import matplotlib.pyplot as plt -host = socket.gethostname() - -listx = [] -listy = [] - -def gen_stars(stars): - stars = int(stars) - - # lists - listrho = [] - - # create new file for every calculation - path = "data/" + str(host) + "_" + str(os.getpid()) + ".csv" - print("path: " + str(path)) - - # define the size of the galaxy - length = 1.5e6 - - # define the borders - range_min = -int(length) - range_max = int(length) - - # define the rho range - rand_min = rho(0) - rand_max = rho(math.sqrt(length**2 + length**2)) - - # create random stars - for r in range(0, stars): - x = np.random.uniform(range_min, range_max, size=1) - y = np.random.uniform(range_min, range_max, size=1) - rand_val = np.random.uniform(rand_min, rand_max, size=1) - rho_xy = rho(math.sqrt(x**2 + y**2)) - - # if the random value is smaller than the rho value, generate a star - if rand_val < rho_xy: - - # open a file to write to - with open(path, "a") as data: - - # write the data to the file - data.write(str(x).strip("[]") + "," + str(y).strip("[]") + "\n") - listx.append(x) - listy.append(y) - print(str(x) + ", " + str(y)) - - print("range_min: " + str(range_min)) - print("range_max: " + str(range_max)) - - print("rand_min: " + str(rand_min)) - print("rand_max: " + str(rand_max)) - -# generate n stars -gen_stars(1e6) - -# plot the stars coordinates in 2D space -plt.scatter(listx, listy) -plt.show() -- cgit 1.4.1 From 8d52500a85acaccb19bc0892165c345d2b06a837 Mon Sep 17 00:00:00 2001 From: hanemile Date: Mon, 11 Dec 2017 20:40:58 +0100 Subject: telegram bot now prints where the stars are being saved (path) --- src/coord.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'src/coord.py') diff --git a/src/coord.py b/src/coord.py index 932cf80..ecd978c 100755 --- a/src/coord.py +++ b/src/coord.py @@ -4,6 +4,7 @@ import time import numpy as np import sys +from subprocess import call # define the number of stars that should be generated nos = int(sys.argv[1]) @@ -13,8 +14,8 @@ arr_stars = np.zeros((int(nos), 3)) arr_saved_stars = np.zeros((int(nos), 3)) # define various paths -path = "data/rho6.csv" -save_path = "stars/" + sys.argv[2] +path = "data/2e7.csv" +save_path = "stars/" + sys.argv[2] + ".csv" # star13 -> 586 stars # define the random-value range [rho_min; rho_max] @@ -53,12 +54,13 @@ def main(): # calculate the distance of the star to the center of the galaxy r = np.sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2)) + # print(round(int(r), 0)) # generate a random value in the range [rand_min; rand_max] a = np.random.uniform(rand_min, rand_max, size=1) # read out the corresponding rho value from the lookuptable (rho-file) - b = float(rho_file[int(round(r[0], 0))].split(", ")[1].strip("\n")) + b = float(rho_file[round(int(r), 0)].split(", ")[1].strip("\n")) # print("{:<5}{:<20}{:<20}{:<20}{:<20}{:<20}{:<20}".format(str(stars_kept), str(x), str(y), str(z), str(a), str(b), str(r))) @@ -83,7 +85,8 @@ def main(): print("") end = time.time() whole_time = end - start - print(">> Finished generating stars in " + str(whole_time) + " seconds\n") + out = ">> Finished generating stars in " + str(whole_time) + " seconds\n" + print(out) # write the star coordinates to a file start_write_file = time.time() @@ -98,7 +101,8 @@ def main(): end_write_file = time.time() time_write_file = end_write_file - start_write_file - print(">> Finished writing star-data to " + save_path + " in " + str(round(time_write_file, 4)) + " seconds\n") + out = ">> Finished writing star-data to " + save_path + " in " + str(round(time_write_file, 4)) + " seconds\n" + print(out) time_all = whole_time + time_write_file @@ -106,12 +110,29 @@ def main(): # print some stats print("") - print("{:<30}{:<30}".format("Time (complete)", str(round(time_all, 4)) + " seconds")) - print("{:<30}{:<30}".format("Time (complete)", str(round(time_min, 4)) + " minutes")) + print("{:<20}{:<20}".format("Time (complete)", str(round(time_all, 4)) + " seconds")) print("{:-<40}".format("")) print("{:<20}{:<20}".format("Number of Stars", str(nos))) print("{:<20}{:<20}".format("Stars Kicked:", str(stars_kicked))) print("{:<20}{:<20}".format("Percent: ", str( nos / stars_kicked * 100 ) + "%")) + hour = int( time_all // 3600 ) + time_all = time_all % 3600 + minutes = int( time_all // 60 ) + time_all = time_all % 60 + seconds = int( time_all ) + + a = path + + time_a = str(hour) + ":" + str(minutes) + ":" + str(seconds) + b = "{:<20}{:<20}".format("Time (h:m:s)", time_a ) + c = "{:<20}{:<20}".format("Number of Stars", str(nos)) + d = "{:<20}{:<20}".format("Stars Kicked:", str(stars_kicked)) + e = "{:<20}{:<20}".format("Percent: ", str( nos / stars_kicked * 100 ) + "%") + + f = a + "\n" + b "\n" + c + "\n" + d + "\n" + e + + call(["telegram-send", "--pre", str(f) ]) + if __name__ == "__main__": main() -- cgit 1.4.1