Skip to content

Add build_barchart and calc_sparseness utils #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions build_barchart/README.md
Original file line number Diff line number Diff line change
@@ -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 \<file> -m \<y_max> -x \<x_label> -y \<y_label>

where y_max - maximum value for y axis.

The script outputs
```
Barchart was saved in file <file>.png
```
Sctipt creates a barchart in the file <file>.png.

61 changes: 61 additions & 0 deletions build_barchart/build_barchart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/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 <file> -m <y_max> -x <x_label> -y <y_label>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('Using: python build_histogram.py -f <file> -m <y_max> -x <x_label> -y <y_label>')
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:])
10 changes: 10 additions & 0 deletions calc_sparseness/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## calc_sparseness
Utility for calculating sparseness of a vector of data

Usage: python calc_sparseness.py -f \<data_file>

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
```
36 changes: 36 additions & 0 deletions calc_sparseness/calc_sparseness.py
Original file line number Diff line number Diff line change
@@ -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 <data_file>')
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:])