-
Notifications
You must be signed in to change notification settings - Fork 0
/
exclude_zeros.py
51 lines (46 loc) · 1.16 KB
/
exclude_zeros.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/python
import numpy as np
import sys, csv
''' Exclude all zero y-values from given file and write it out with new x-scale.
'''
# Check argument
if len(sys.argv) < 2:
print "Utility usage:\n\t{} <filename.csv>".format(sys.argv[0])
sys.exit(0)
# Load data from file
xl = []; yl = []; i = 0
fd = open(sys.argv[1],'rU')
c = csv.reader(fd)
for row in c:
i += 1
if len(row) != 0:
try:
xl.append(float(row[0]))
yl.append(float(row[1]))
except ValueError:
print "Inappropriate data detected in row {}: {}!".format(i,row)
else:
print "String {} is empty!".format(i)
x = np.asarray(xl)
y = np.asarray(yl)
assert(x.size==y.size)
# Exclude null y-values
y_new = []
for pointer in xrange(y.size):
if y[pointer] != 0:
y_new.append(y[pointer])
else:
continue
y_new = np.asarray(y_new)
# New x-array
x_new = np.arange(y_new.size)
assert(x_new.size==y_new.size)
# Write to file
splitter = ','
outfname = '.'.join(sys.argv[1].split('.')[:-1]) + '-eg.csv'
outfd = open(outfname,'wb')
for spointer in xrange(x_new.size):
string = str(x_new[spointer]) + splitter + str(y_new[spointer])
outfd.write(string + "\n")
outfd.close()
print "Result saved in file: ", outfname