188 lines
11 KiB
Python
188 lines
11 KiB
Python
import math
|
||
import sys
|
||
import os
|
||
|
||
# defining some constants
|
||
|
||
r = 32 # round to n digits
|
||
column_width = 11 # set column width of final table
|
||
|
||
# checking if the file is provided correctly
|
||
|
||
if len(sys.argv) != 2:
|
||
print(f"Please, provide data file\nUsage example: python3 {sys.argv[0]} sample_file.txt")
|
||
sys.exit(1)
|
||
|
||
if not os.path.exists(sys.argv[1]):
|
||
print("File does not exist")
|
||
sys.exit(1)
|
||
|
||
# defining functions
|
||
|
||
def avg(a):
|
||
#print(a)
|
||
return sum(a) / len(a)
|
||
|
||
def get_max_len(a):
|
||
return max(list(map(lambda x: len(str(x)), a)))
|
||
|
||
# reading input data from the file
|
||
|
||
possible_units = {"mass": {"kilogram": 1, "gram": 0.001}}
|
||
|
||
selected_table = 0
|
||
|
||
tables = {"1": {"r2": 0, "r1": 0, "norms": {"mass": 1}, "stats": []},
|
||
"2": {"r2": 0, "r1": 0, "norms": {"mass": 1}, "stats": []}}
|
||
|
||
with open(sys.argv[1]) as source_file:
|
||
for raw_i in source_file:
|
||
i = raw_i.rstrip("\n\r")
|
||
table_line = i.split()
|
||
if len(table_line) > 0:
|
||
if table_line[0] == "#":
|
||
if table_line[1] == "table":
|
||
selected_table = table_line[2]
|
||
elif table_line[1] == "r1":
|
||
tables[selected_table]["r1"] = float(table_line[2])
|
||
elif table_line[1] == "r2":
|
||
tables[selected_table]["r2"] = float(table_line[2])
|
||
elif table_line[1] == "set_unit":
|
||
if table_line[2] in possible_units and table_line[3] in possible_units[table_line[2]]:
|
||
tables[selected_table]["norms"][table_line[2]] = possible_units[table_line[2]][table_line[3]]
|
||
else:
|
||
print(f"Unsupported unit defined in line: '{i}'")
|
||
else:
|
||
if len(table_line) == 7:
|
||
new_data = list(map(float, table_line))
|
||
new_data[0] = round(new_data[0] * tables[selected_table]["norms"]["mass"], r)
|
||
tables[selected_table]["stats"].append(new_data)
|
||
|
||
# processing inputs
|
||
|
||
required_data = {"1": {"stats": [], "Mt": 0, "Imin": 0},
|
||
"2": {"stats": [], "Mt": 0, "Imin": 0}}
|
||
|
||
for i in tables:
|
||
for j in tables[i]['stats']:
|
||
new_line = []
|
||
new_line.append(round(j[0]*tables[i]['r1']*9.81, r))
|
||
new_line.append(round(avg(j[1:4]), r))
|
||
new_line.append(round(2/(tables[i]['r1']*(new_line[1]**2)), r))
|
||
|
||
new_line.append(round(j[0]*tables[i]['r2']*9.81, r))
|
||
new_line.append(round(avg(j[4:7]), r))
|
||
new_line.append(round(2/(tables[i]['r2']*(new_line[4]**2)), r))
|
||
|
||
required_data[i]["stats"].append(new_line)
|
||
|
||
# printing out the results
|
||
|
||
|
||
'''
|
||
for i in tables:
|
||
table_widths = [get_max_len(list(zip(tables[i]['stats']))[0]),
|
||
get_max_len(list(zip(required_data[i]['stats']))[0]),
|
||
get_max_len(list(zip(tables[i]['stats']))[1]),
|
||
get_max_len(list(zip(tables[i]['stats']))[2]),
|
||
get_max_len(list(zip(tables[i]['stats']))[3]),
|
||
get_max_len(list(zip(required_data[i]['stats']))[1]),
|
||
get_max_len(list(zip(required_data[i]['stats']))[2]),
|
||
|
||
get_max_len(list(zip(tables[i]['stats']))[0]),
|
||
get_max_len(list(zip(required_data[i]['stats']))[3]),
|
||
get_max_len(list(zip(tables[i]['stats']))[4]),
|
||
get_max_len(list(zip(tables[i]['stats']))[5]),
|
||
get_max_len(list(zip(tables[i]['stats']))[6]),
|
||
get_max_len(list(zip(required_data[i]['stats']))[4]),
|
||
get_max_len(list(zip(required_data[i]['stats']))[5]),
|
||
]
|
||
print("|".join( list(map(lambda x: str(x[0].center(x[1])), zip(["m", "Mi", "t1", "t2", "t3", "❬t❭", "βi"]*2, table_widths)))))
|
||
for j in range(len(required_data[i]['stats'])):
|
||
print("|".join( list(map(lambda x: str(x[0].center(x[1])),
|
||
zip([tables[i]['stats'][j][0],
|
||
required_data[i]['stats'][j][0],
|
||
tables[i]['stats'][j][1],
|
||
tables[i]['stats'][j][2],
|
||
tables[i]['stats'][j][3],
|
||
required_data[i]['stats'][j][1],
|
||
required_data[i]['stats'][j][2],
|
||
|
||
tables[i]['stats'][j][0],
|
||
required_data[i]['stats'][j][3],
|
||
tables[i]['stats'][j][4],
|
||
tables[i]['stats'][j][5],
|
||
tables[i]['stats'][j][6],
|
||
required_data[i]['stats'][j][4],
|
||
required_data[i]['stats'][j][5]],
|
||
table_widths))) ))
|
||
'''
|
||
|
||
for i in tables:
|
||
print(f"Table #{i}")
|
||
print("|".join( list(map(lambda x: str(x).center(11), ["m", "Mi", "t1", "t2", "t3", "❬t❭", "βi"]*2))))
|
||
for j in range(len(required_data[i]['stats'])):
|
||
print("|".join( list(map(lambda x: str(x).center(11),
|
||
[tables[i]['stats'][j][0],
|
||
required_data[i]['stats'][j][0],
|
||
tables[i]['stats'][j][1],
|
||
tables[i]['stats'][j][2],
|
||
tables[i]['stats'][j][3],
|
||
required_data[i]['stats'][j][1],
|
||
required_data[i]['stats'][j][2],
|
||
|
||
tables[i]['stats'][j][0],
|
||
required_data[i]['stats'][j][3],
|
||
tables[i]['stats'][j][4],
|
||
tables[i]['stats'][j][5],
|
||
tables[i]['stats'][j][6],
|
||
required_data[i]['stats'][j][4],
|
||
required_data[i]['stats'][j][5]])) ))
|
||
|
||
sigma_beta1 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["1"]['stats']))[2]))) + avg(list(zip(*required_data["1"]['stats']))[2])**2), r)
|
||
sigma_beta2 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["1"]['stats']))[5]))) + avg(list(zip(*required_data["1"]['stats']))[5])**2), r)
|
||
sigma_beta3 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["2"]['stats']))[2]))) + avg(list(zip(*required_data["2"]['stats']))[2])**2), r)
|
||
sigma_beta4 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["2"]['stats']))[5]))) + avg(list(zip(*required_data["2"]['stats']))[5])**2), r)
|
||
|
||
sigma_M1 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["1"]['stats']))[0]))) + avg(list(zip(*required_data["1"]['stats']))[0])**2), r)
|
||
sigma_M2 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["1"]['stats']))[3]))) + avg(list(zip(*required_data["1"]['stats']))[3])**2), r)
|
||
sigma_M3 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["2"]['stats']))[0]))) + avg(list(zip(*required_data["2"]['stats']))[0])**2), r)
|
||
sigma_M4 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["2"]['stats']))[3]))) + avg(list(zip(*required_data["2"]['stats']))[3])**2), r)
|
||
|
||
#print("σβ (1-4) (DO NOT USE):", sigma_beta1/avg(list(zip(*required_data["1"]['stats']))[2]), sigma_beta2/avg(list(zip(*required_data["1"]['stats']))[5]), sigma_beta3/avg(list(zip(*required_data["2"]['stats']))[2]), sigma_beta4/avg(list(zip(*required_data["2"]['stats']))[5]))
|
||
#print("σM (1-4) (DO NOT USE):", sigma_t1/avg(list(zip(*required_data["1"]['stats']))[0]), sigma_t2/avg(list(zip(*required_data["1"]['stats']))[3]), sigma_t3/avg(list(zip(*required_data["2"]['stats']))[0]), sigma_t4/avg(list(zip(*required_data["2"]['stats']))[3]))
|
||
|
||
print("σβ:", avg([math.sqrt(sigma_beta1), math.sqrt(sigma_beta2), math.sqrt(sigma_beta3), math.sqrt(sigma_beta4)]))
|
||
print("σM:", avg([math.sqrt(sigma_M1), math.sqrt(sigma_M2), math.sqrt(sigma_M3), math.sqrt(sigma_M4)]))
|
||
|
||
print(required_data)
|
||
'''
|
||
for t in required_data:
|
||
I_values_r1 = [(i[0] / i[1]) for i in zip(list(zip(*required_data[t]['stats']))[0], list(zip(*required_data[t]['stats']))[2])]
|
||
Mt1 = required_data[t]['stats'][I_values_r1.index(min(I_values_r1))][0] - (min(I_values_r1) * required_data[t]['stats'][I_values_r1.index(min(I_values_r1))][2])
|
||
print(f"Таблиця {t}, стовпець 1: Imin = {required_data[t]['stats'][I_values_r1.index(min(I_values_r1))][0]} / {required_data[t]['stats'][I_values_r1.index(min(I_values_r1))][2]} = {round(min(I_values_r1), r)}; Mт = {required_data[t]['stats'][I_values_r1.index(min(I_values_r1))][0]} - {min(I_values_r1)}*{required_data[t]['stats'][I_values_r1.index(min(I_values_r1))][2]} = {round(Mt1, r)} (за мінімальний узято рядок {I_values_r1.index(min(I_values_r1))})")
|
||
|
||
I_values_r2 = [(i[0] / i[1]) for i in zip(list(zip(*required_data[t]['stats']))[3], list(zip(*required_data[t]['stats']))[5])]
|
||
Mt2 = required_data[t]['stats'][I_values_r2.index(min(I_values_r2))][3] - (min(I_values_r2) * required_data[t]['stats'][I_values_r2.index(min(I_values_r2))][5])
|
||
print(f"Таблиця {t}, стовпець 2: Imin = {round(min(I_values_r2), r)}; Mт = {round(Mt2, r)}")
|
||
#i_min_2 = min([(i[0] / i[1]) for i in zip(list(zip(*required_data[t]['stats']))[3], list(zip(*required_data[t]['stats']))[5])])
|
||
#print(f"Imin for {t}-2: {round(i_min_2, r)}")
|
||
'''
|
||
for t in required_data:
|
||
I_values_r1 = []
|
||
M_and_beta = list(zip(list(zip(*required_data[t]['stats']))[0], list(zip(*required_data[t]['stats']))[2]))
|
||
print(M_and_beta)
|
||
for i in range(5):
|
||
print((M_and_beta[i+1][0] - M_and_beta[i][0]) / (M_and_beta[i+1][1] - M_and_beta[i][1]))
|
||
I_values_r1.append((M_and_beta[i+1][0] - M_and_beta[i][0]) / (M_and_beta[i+1][1] - M_and_beta[i][1]))
|
||
|
||
print(f"Таблиця {t}, стовпець 1: Imin = {min(I_values_r1)}; Mт = {M_and_beta[I_values_r1.index(min(I_values_r1))][0] - min(I_values_r1)*M_and_beta[I_values_r1.index(min(I_values_r1))][1]}")
|
||
|
||
I_values_r2 = []
|
||
M_and_beta = list(zip(list(zip(*required_data[t]['stats']))[3], list(zip(*required_data[t]['stats']))[5]))
|
||
for i in range(5):
|
||
print((M_and_beta[i+1][0] - M_and_beta[i][0]) / (M_and_beta[i+1][1] - M_and_beta[i][1]))
|
||
I_values_r2.append((M_and_beta[i+1][0] - M_and_beta[i][0]) / (M_and_beta[i+1][1] - M_and_beta[i][1]))
|
||
|
||
print(f"Таблиця {t}, стовпець 2: Imin = {min(I_values_r2)}; Mт = {M_and_beta[I_values_r2.index(min(I_values_r2))][0] - min(I_values_r2)*M_and_beta[I_values_r2.index(min(I_values_r2))][1]}")
|