From f95f06998882357f129c2ee0459a60dd9a557da0 Mon Sep 17 00:00:00 2001 From: Enne Hekma Date: Tue, 22 Mar 2016 18:03:42 +0100 Subject: [PATCH 1/6] Added histogram plot framework --- python/plot_histogram_dV.py | 126 ++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 python/plot_histogram_dV.py diff --git a/python/plot_histogram_dV.py b/python/plot_histogram_dV.py new file mode 100644 index 0000000..90b9032 --- /dev/null +++ b/python/plot_histogram_dV.py @@ -0,0 +1,126 @@ +''' +Copyright (c) 2014-2016 Kartik Kumar, Dinamica Srl (me@kartikkumar.com) +Copyright (c) 2014-2016 Abhishek Agrawal, Delft University of Technology (abhishek.agrawal@protonmail.com) +Copyright (c) 2014-2016 Enne Hekma, Delft University of Technology (ennehekma@gmail.com) +Distributed under the MIT License. +See accompanying file LICENSE.md or copy at http://opensource.org/licenses/MIT +''' + +# Set up modules and packages +# Plotting +import matplotlib +matplotlib.use('Agg') +import matplotlib.pyplot as plt +from matplotlib import rcParams +from matplotlib import cm +from mpl_toolkits.mplot3d import Axes3D +from mpl_toolkits.mplot3d import axes3d + +# I/O +import commentjson +import json +from pprint import pprint + +# SQL database +import sqlite3 + +# Numerical +import numpy as np +import pandas as pd + +# System +import sys +import time + +print "" +print "---------------------------------------------------------------------------------" +print " D2D " +print " 0.0.2 " +print " Copyright (c) 2016, K. Kumar (me@kartikkumar.com) " +print " Copyright (c) 2016, A. Agrawal (abhishek.agrawal@protonmail.com) " +print " Copyright (c) 2016, E.J. Hekma (ennehekma@gmail.com) " +print "---------------------------------------------------------------------------------" +print "" + +# Start timer. +start_time = time.time( ) + +print "" +print "******************************************************************" +print " Input parameters " +print "******************************************************************" +print "" + +# Parse JSON configuration file +# Raise exception if wrong number of inputs are provided to script +if len(sys.argv) != 2: + raise Exception("Only provide a JSON config file as input!") + +json_data = open(sys.argv[1]) +config = commentjson.load(json_data) +json_data.close() +pprint(config) + +print "" +print "******************************************************************" +print " Operations " +print "******************************************************************" +print "" + +print "Input data files being read ..." + +output_path_prefix = config["output_directory"] + '/' + +print "Fetching scan data from database ..." + +# Connect to SQLite database. +try: + database = sqlite3.connect(config['database']) + +except sqlite3.Error, e: + print "Error %s:" % e.args[0] + sys.exit(1) + +# Fetch scan data. +scan_data = pd.read_sql( "SELECT \ + " + config['error'] + " \ + FROM sgp4_scanner_results \ + WHERE success = 1;", \ + database ) +scan_data.columns = [ config['error'] ] + +# The histogram of the data +x = scan_data[ 'transfer_delta_v'] +# print x +n, bins, patches = plt.hist( x, bins=50, normed=1, facecolor='green', alpha=0.75 ) + +# Figure properties +plt.xlabel('arrival position error magnitude [km]') +# plt.ylabel('Probability') +# plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$') +# plt.axis([40, 160, 0, 0.03]) +plt.grid(True) + +# Save figure to file. +plt.savefig(config["output_directory"] + "/" + config["histogram_figure"] + "_" + \ + ".png", dpi=config["figure_dpi"]) +plt.close() + +print "Figure generated successfully!" +print "" + +# Close SQLite database if it's still open. +if database: + database.close() + +# Stop timer +end_time = time.time( ) + +# Print elapsed time +print "Script time: " + str("{:,g}".format(end_time - start_time)) + "s" + +print "" +print "------------------------------------------------------------------" +print " Exited successfully! " +print "------------------------------------------------------------------" +print "" From 2ba57c06e73f17733308e102bbd2fc69972003ec Mon Sep 17 00:00:00 2001 From: Enne Hekma Date: Thu, 24 Mar 2016 11:36:57 +0100 Subject: [PATCH 2/6] Save --- python/plot_histogram_dV.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/python/plot_histogram_dV.py b/python/plot_histogram_dV.py index 90b9032..8d57e98 100644 --- a/python/plot_histogram_dV.py +++ b/python/plot_histogram_dV.py @@ -82,27 +82,25 @@ sys.exit(1) # Fetch scan data. -scan_data = pd.read_sql( "SELECT \ - " + config['error'] + " \ - FROM sgp4_scanner_results \ - WHERE success = 1;", \ +scan_data = pd.read_sql( "SELECT transfer_delta_v FROM lambert_scanner_results WHERE transfer_delta_v < 30;", database ) -scan_data.columns = [ config['error'] ] +scan_data.columns = [ 'transfer_delta_v' ] # The histogram of the data x = scan_data[ 'transfer_delta_v'] # print x -n, bins, patches = plt.hist( x, bins=50, normed=1, facecolor='green', alpha=0.75 ) +plt.hist( x, bins=50, facecolor='green', alpha=0.75 ) # Figure properties -plt.xlabel('arrival position error magnitude [km]') -# plt.ylabel('Probability') +plt.xlabel('Total dV magnitude [km/s]') +plt.ylabel('Frequency') +plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0)) # plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$') # plt.axis([40, 160, 0, 0.03]) plt.grid(True) # Save figure to file. -plt.savefig(config["output_directory"] + "/" + config["histogram_figure"] + "_" + \ +plt.savefig(config["output_directory"] + "/" + config["scan_figure"] + \ ".png", dpi=config["figure_dpi"]) plt.close() From f850c93cf79e1e2a673f3f2ef0cf68d73d67b7f0 Mon Sep 17 00:00:00 2001 From: Enne Hekma Date: Fri, 15 Apr 2016 15:58:03 +0200 Subject: [PATCH 3/6] Change color to greyscale plot --- python/plot_histogram_dV.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/plot_histogram_dV.py b/python/plot_histogram_dV.py index 8d57e98..a09a45b 100644 --- a/python/plot_histogram_dV.py +++ b/python/plot_histogram_dV.py @@ -80,7 +80,7 @@ except sqlite3.Error, e: print "Error %s:" % e.args[0] sys.exit(1) - + # Fetch scan data. scan_data = pd.read_sql( "SELECT transfer_delta_v FROM lambert_scanner_results WHERE transfer_delta_v < 30;", database ) @@ -88,8 +88,8 @@ # The histogram of the data x = scan_data[ 'transfer_delta_v'] -# print x -plt.hist( x, bins=50, facecolor='green', alpha=0.75 ) +# print x +plt.hist( x, bins=50, facecolor='grey', alpha=0.75 ) # Figure properties plt.xlabel('Total dV magnitude [km/s]') @@ -98,7 +98,7 @@ # plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$') # plt.axis([40, 160, 0, 0.03]) plt.grid(True) - + # Save figure to file. plt.savefig(config["output_directory"] + "/" + config["scan_figure"] + \ ".png", dpi=config["figure_dpi"]) From 3e3da7da3b1e731cda9baae763b8e6f73bc04ceb Mon Sep 17 00:00:00 2001 From: Enne Hekma Date: Fri, 15 Apr 2016 17:16:30 +0200 Subject: [PATCH 4/6] Add possibility to plot fit --- python/plot_histogram_dV.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/python/plot_histogram_dV.py b/python/plot_histogram_dV.py index a09a45b..af40a20 100644 --- a/python/plot_histogram_dV.py +++ b/python/plot_histogram_dV.py @@ -15,6 +15,7 @@ from matplotlib import cm from mpl_toolkits.mplot3d import Axes3D from mpl_toolkits.mplot3d import axes3d +import matplotlib.mlab as mlab # I/O import commentjson @@ -27,6 +28,7 @@ # Numerical import numpy as np import pandas as pd +from scipy.stats import norm # System import sys @@ -89,7 +91,12 @@ # The histogram of the data x = scan_data[ 'transfer_delta_v'] # print x -plt.hist( x, bins=50, facecolor='grey', alpha=0.75 ) +# (mu, sigma) = norm.fit(x) +# n, bins, patches = plt.hist( x, bins=50, facecolor='grey', alpha=0.75, normed=1) + +# y = mlab.normpdf( bins, mu, sigma) +# l = plt.plot(bins, y, 'r--', linewidth=2) + # Figure properties plt.xlabel('Total dV magnitude [km/s]') @@ -97,6 +104,7 @@ plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0)) # plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$') # plt.axis([40, 160, 0, 0.03]) +plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=%.3f,\ \sigma=%.3f$' %(mu, sigma)) plt.grid(True) # Save figure to file. From 41dc712f6c2aefa06924bc233b5f919e520c10af Mon Sep 17 00:00:00 2001 From: Enne Hekma Date: Thu, 12 May 2016 15:17:31 +0200 Subject: [PATCH 5/6] small change --- python/plot_histogram_dV.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/plot_histogram_dV.py b/python/plot_histogram_dV.py index af40a20..e74794c 100644 --- a/python/plot_histogram_dV.py +++ b/python/plot_histogram_dV.py @@ -92,7 +92,7 @@ x = scan_data[ 'transfer_delta_v'] # print x # (mu, sigma) = norm.fit(x) -# n, bins, patches = plt.hist( x, bins=50, facecolor='grey', alpha=0.75, normed=1) +n, bins, patches = plt.hist( x, bins=50, facecolor='grey', alpha=0.75) # y = mlab.normpdf( bins, mu, sigma) # l = plt.plot(bins, y, 'r--', linewidth=2) @@ -104,7 +104,7 @@ plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0)) # plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$') # plt.axis([40, 160, 0, 0.03]) -plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=%.3f,\ \sigma=%.3f$' %(mu, sigma)) +# plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=%.3f,\ \sigma=%.3f$' %(mu, sigma)) plt.grid(True) # Save figure to file. From 6cef4bd81ae092cc81b05ddae185ae4d561e6512 Mon Sep 17 00:00:00 2001 From: Enne Hekma Date: Tue, 31 Jan 2017 10:27:25 +0100 Subject: [PATCH 6/6] Add dV histogram plotting script. --- config/plot_histogram_dV.json.empty | 25 +++++++++++++++++++++++++ python/plot_histogram_dV.py | 20 +++++++------------- 2 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 config/plot_histogram_dV.json.empty diff --git a/config/plot_histogram_dV.json.empty b/config/plot_histogram_dV.json.empty new file mode 100644 index 0000000..9d58053 --- /dev/null +++ b/config/plot_histogram_dV.json.empty @@ -0,0 +1,25 @@ +// Copyright (c) 2014-2016 Kartik Kumar, Dinamica Srl (me@kartikkumar.com) +// Copyright (c) 2016 Enne Hekma, Delft University of Technology (ennehekma@gmail.com) +// Distributed under the MIT License. +// See accompanying file LICENSE.md or copy at http://opensource.org/licenses/MIT +{ + // Path to SQLite database containing scan data. + "database" : "", + + // Directory where scan figure is stored. + "output_directory" : ".", + + // The cutoff delta V. + "dV_cutoff" : 3, + + // Number of bins used for histogram. + "bins" : 100, + + // Filename for scan figure. + // Files are saved as png. + "scan_figure" : "dv_histo", + + // Set parameters for figures. + "figure_dpi" : 300, + "colormap" : "jet" +} diff --git a/python/plot_histogram_dV.py b/python/plot_histogram_dV.py index e74794c..bc1f280 100644 --- a/python/plot_histogram_dV.py +++ b/python/plot_histogram_dV.py @@ -84,32 +84,26 @@ sys.exit(1) # Fetch scan data. -scan_data = pd.read_sql( "SELECT transfer_delta_v FROM lambert_scanner_results WHERE transfer_delta_v < 30;", +scan_data = pd.read_sql( " SELECT transfer_delta_v \ + FROM lambert_scanner_results \ + WHERE transfer_delta_v < "+ str(config['dV_cutoff']) + ";", database ) scan_data.columns = [ 'transfer_delta_v' ] # The histogram of the data x = scan_data[ 'transfer_delta_v'] -# print x -# (mu, sigma) = norm.fit(x) -n, bins, patches = plt.hist( x, bins=50, facecolor='grey', alpha=0.75) - -# y = mlab.normpdf( bins, mu, sigma) -# l = plt.plot(bins, y, 'r--', linewidth=2) - +n, bins, patches = plt.hist( x, bins=config['bins'], facecolor='grey', alpha=0.75) # Figure properties +cmap = config['colormap'] plt.xlabel('Total dV magnitude [km/s]') plt.ylabel('Frequency') plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0)) -# plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$') -# plt.axis([40, 160, 0, 0.03]) -# plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=%.3f,\ \sigma=%.3f$' %(mu, sigma)) plt.grid(True) # Save figure to file. -plt.savefig(config["output_directory"] + "/" + config["scan_figure"] + \ - ".png", dpi=config["figure_dpi"]) +plt.savefig(config["output_directory"] + "/" + config["scan_figure"] + ".png", + dpi=config["figure_dpi"]) plt.close() print "Figure generated successfully!"