From 5deacf9a89796af5b0e17c94d85be68dfa07968a Mon Sep 17 00:00:00 2001 From: Vladimir Privalov Date: Sat, 19 Oct 2019 17:27:14 +0200 Subject: [PATCH 1/2] Add build_barchart and calc_sparseness utils --- build_barchart/README.md | 31 +++++++++++++++ build_barchart/build_barchart.py | 62 ++++++++++++++++++++++++++++++ calc_sparseness/README.md | 10 +++++ calc_sparseness/calc_sparseness.py | 36 +++++++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 build_barchart/README.md create mode 100644 build_barchart/build_barchart.py create mode 100644 calc_sparseness/README.md create mode 100644 calc_sparseness/calc_sparseness.py diff --git a/build_barchart/README.md b/build_barchart/README.md new file mode 100644 index 0000000..4c9222b --- /dev/null +++ b/build_barchart/README.md @@ -0,0 +1,31 @@ +## build_barchart +Utility for building bar chart from data stored in file. This utility can be useful in scientific work for experimental data analysis. + +### Prerequisities +The script requires matplotlib installed. To install it use pip: + +``` +pip install matplotlib +``` + +### Usage +Create a file with data in the following format +``` +1: 0 +2: 1 +3: 0 +4: 2 +... +``` +First number specifies the values along the x axis i.e. bins in the histogram. Second number if the value for each bin i.e. the height of the bin. + +Using: python build_histogram.py -f \ -m \ -x \ -y \ + +where y_max - maximum value for y axis. + +The script outputs +``` +Barchart was saved in file .png +``` +Sctipt creates a barchart in the file .png. + diff --git a/build_barchart/build_barchart.py b/build_barchart/build_barchart.py new file mode 100644 index 0000000..827110d --- /dev/null +++ b/build_barchart/build_barchart.py @@ -0,0 +1,62 @@ + +#!/usr/bin/python +# Filename: build_barchart.py + +import os +import sys, getopt +import numpy as np +import matplotlib.pyplot as plt + +y_lim = 20 # the number of data items +x_label = "" +y_label = "" + +def read_file(file_name): + global y_lim + global x_label + global y_label + values = [] + for line in open(file_name, 'r'): + if line != '': + value_strs = line.split(':') + value = int(value_strs[1]) + values.append(value) + + N = len(values) + x = range(N) + width = 1 + + plt.bar(x, values, width, color="blue") + plt.xlabel(x_label) + plt.ylabel(y_label) + plt.ylim(0,y_lim) + barchart_file = file_name+'.png' + plt.savefig(barchart_file) + print('Barchart was saved in file ',barchart_file) + +def main(argv): + file_name = "" + global y_lim, x_label, y_label + try: + opts, args = getopt.getopt(argv,"h:f:m:x:y:") + except getopt.GetoptError: + print('Unknown arguments!\nUsing: python build_histogram.py -f -m -x -y ') + sys.exit(2) + for opt, arg in opts: + if opt == '-h': + print('Using: python build_histogram.py -f -m -x -y ') + sys.exit() + elif opt == '-f': + file_name = arg + elif opt == '-m': + y_lim = int(arg) + elif opt == '-x': + x_label = arg + elif opt == '-y': + y_label = arg + + read_file(file_name) + + +if __name__ == '__main__': + main(sys.argv[1:]) diff --git a/calc_sparseness/README.md b/calc_sparseness/README.md new file mode 100644 index 0000000..f8c0189 --- /dev/null +++ b/calc_sparseness/README.md @@ -0,0 +1,10 @@ +## calc_sparseness +Utility for calculating sparseness of a vector of data + +Usage: python calc_sparseness.py -f \ + +Data in file: + +``` +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.11 6163 0 0 0 0 0.109525 0 0 0 +``` diff --git a/calc_sparseness/calc_sparseness.py b/calc_sparseness/calc_sparseness.py new file mode 100644 index 0000000..57186d3 --- /dev/null +++ b/calc_sparseness/calc_sparseness.py @@ -0,0 +1,36 @@ +#!/usr/bin/python +# Filename: calc_sparseness.py + +import sys, getopt + +def read_data(file): + data_len = 0 + zeros = 0 + + infile = open(file, 'r') + line = infile.readline() + #print line,'\n' + values = [float(v) for v in line.split(' ') if len(v)] + data_len = len(values) + + zero_vals = [v for v in values if v == 0] + zeros = len(zero_vals) + + print('zeros ',zeros,' in total ',data_len,'\n') + +def main(argv): + data_file = '' + try: + opts, args = getopt.getopt(argv,"f:") + except getopt.GetoptError: + print('python calc_sparseness.py -f ') + sys.exit(2) + for opt, arg in opts: + if opt == '-f': + data_file = arg + + print('data file:', data_file,'\n') + read_data(data_file) + +if __name__ == "__main__": + main(sys.argv[1:]) From e6c9a973edb3307c385de9d23e3aff76aa50fd6f Mon Sep 17 00:00:00 2001 From: Vladimir Privalov Date: Sat, 19 Oct 2019 17:53:16 +0200 Subject: [PATCH 2/2] Remove first blank line in build_barchart.py --- build_barchart/build_barchart.py | 1 - 1 file changed, 1 deletion(-) diff --git a/build_barchart/build_barchart.py b/build_barchart/build_barchart.py index 827110d..da987ab 100644 --- a/build_barchart/build_barchart.py +++ b/build_barchart/build_barchart.py @@ -1,4 +1,3 @@ - #!/usr/bin/python # Filename: build_barchart.py