add more details, add visualize pickle module

This commit is contained in:
HappyZ 2019-05-22 11:47:54 -05:00
parent 1679eecce5
commit 0ae6bdd382
4 changed files with 112 additions and 11 deletions

View File

@ -49,3 +49,18 @@ def convert_vector_to_mat(
x, y = (rx_locs[i, :] + offset) / block_size
result[int(x), int(y)] = rx_rsses[i]
return result
def stats(data):
'''
'''
data = data.reshape(-1,)
return {
"5perc": np.percentile(data, 5),
"25perc": np.percentile(data, 25),
"median": np.median(data),
"75perc": np.percentile(data, 75),
"95perc": np.percentile(data, 95),
"mean": np.mean(data),
"std": np.std(data),
}

View File

@ -66,7 +66,7 @@ def main(args):
filepaths.extend(["{}/{}".format(args.folderpath, file) for file in files if '.pickle' in file])
# prepare
if args.outputfile:
if args.outputfile and not os.path.isfile(args.outputfile):
with open(args.outputfile, 'w') as f:
f.write("filename,tx_x,tx_y,tx_pwr,env_gamma\n")

View File

@ -6,6 +6,7 @@ import pickle
import argparse
from libs.util import touch
from libs.util import stats
from libs.util import convert_mat_to_vector
from libs.util import convert_vector_to_mat
from libs.fitting import modelfit_log_gamma
@ -34,7 +35,7 @@ def fittingSingle(filepath, args):
rx_locs, rx_rsses = convert_mat_to_vector(data, block_size=0.1)
# start fitting
min_pmse = float('inf')
min_fit_mse = float('inf')
best_tx_loc = None
best_tx_pwr = None
best_env_gamma = None
@ -51,20 +52,27 @@ def fittingSingle(filepath, args):
monte_carlo_sampling_rate=0.8
)
# unpack
pmse, est_tx_loc, est_tx_pwr, est_env_gamma, est_rsses = result
if pmse < min_pmse:
print("|----- found better:", pmse, est_tx_loc, est_tx_pwr, est_env_gamma)
min_pmse = pmse
fit_mse, est_tx_loc, est_tx_pwr, est_env_gamma, est_rsses = result
if fit_mse < min_fit_mse:
print("|----- found better:", fit_mse, est_tx_loc, est_tx_pwr, est_env_gamma)
min_fit_mse = fit_mse
best_tx_loc = est_tx_loc
best_tx_pwr = est_tx_pwr
best_env_gamma = est_env_gamma
best_rsses = est_rsses
# calculate diff
data_best_est = convert_vector_to_mat(rx_locs, best_rsses, data.shape, block_size=0.1)
data_diff = data - data_best_est
stat = stats(data_diff)
print("|- final:")
print("|--- tx loc: {:.3f}, {:.3f}".format(best_tx_loc[0], best_tx_loc[1]))
print("|--- tx pwr: {:.2f}".format(best_tx_pwr))
print("|--- env gamma: {:.2f}".format(best_env_gamma))
print("|--- fitting mse: {:.6f}".format(min_pmse))
data_best_est = convert_vector_to_mat(rx_locs, best_rsses, data.shape, block_size=0.1)
print("|--- fitting mse: {:.6f}".format(min_fit_mse))
print("|--- rss diff: 5perc/95perc/median/mean: {:.4f}/{:.4f}/{:.4f}/{:.4f} m"
.format(stat['5perc'], stat['95perc'], stat['median'], stat['mean']))
if args.outputfile:
filename = os.path.basename(args.outputfile)
@ -73,7 +81,8 @@ def fittingSingle(filepath, args):
"{},".format(filename) +
"{:.3f},{:.3f},".format(best_tx_loc[0], best_tx_loc[1]) +
"{:.2f},{:.2f}".format(best_tx_pwr, best_env_gamma) +
"{:.6f}".format(min_pmse) +
"{:.6f},".format(min_fit_mse) +
"{:.4f},{:.4f},{:.4f},{:.4f}".format(stat['5perc'], stat['95perc'], stat['median'], stat['mean']) +
"\n"
)
@ -96,9 +105,9 @@ def main(args):
filepaths.extend(["{}/{}".format(args.folderpath, file) for file in files if '.pickle' in file])
# prepare
if args.outputfile:
if args.outputfile and not os.path.isfile(args.outputfile):
with open(args.outputfile, 'w') as f:
f.write("filename,tx_x,tx_y,tx_pwr,env_gamma,fit_mse\n")
f.write("filename,tx_x,tx_y,tx_pwr,env_gamma,fit_mse,rss_diff_5perc,rss_diff_95perc,rss_diff_med,rss_diff_avg\n")
# loop through and fit

77
visualize.py Normal file
View File

@ -0,0 +1,77 @@
#!/usr/bin/python
import os
import pickle
import argparse
from libs.plotting import plotRSS
def loadData(filepath):
try:
return pickle.load(open(filepath, "rb"))
except BaseException as e:
print("err: {}".format(e))
pass
return None
def plotSingle(filepath, args):
'''
'''
# load data from file
data = loadData(filepath)
if data is None:
print("err: failed to load file {}".format(filepath))
return
plotRSS(data)
def main(args):
# finding files
filepaths = []
if args.filepath:
filepaths.append(args.filepath)
if args.folderpath:
files = os.listdir(args.folderpath)
filepaths.extend(["{}/{}".format(args.folderpath, file) for file in files if '.pickle' in file])
# loop through and plot
for i in range(len(filepaths)):
print("- plotting file {}".format(filepaths[i]))
plotSingle(filepaths[i], args)
if __name__ == '__main__':
p = argparse.ArgumentParser(description='Visualization')
p.add_argument(
'--filepath', '-f',
dest='filepath',
default=None,
help='input filepath for a pickle'
)
p.add_argument(
'--folderpath', '-fd',
dest='folderpath',
default=None,
help='input folderpath for many pickles'
)
try:
args = p.parse_args()
except BaseException as e:
print(e)
sys.exit()
if args.filepath is None and args.folderpath is None:
print("at least specify file `-f` or folder `-fd`")
sys.exit()
elif args.filepath is None and not os.path.isdir(args.folderpath):
print("folder {} not exit".format(args.folderpath))
sys.exit()
elif args.folderpath is None and not os.path.isfile(args.filepath):
print("file {} not exit".format(args.filepath))
sys.exit()
main(args)