-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_05a_stats.py
executable file
·51 lines (34 loc) · 2.02 KB
/
example_05a_stats.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/python3
import numpy
#=======================================================================================================================
def main():
in_file = "data_file.csv"
fp_in = open(in_file, "r")
ndata_list = []
for in_data in fp_in:
in_data = in_data.strip() # remove leading and trailing whitespace
data_list = in_data.split(",") # make a list (data_list) from the comma-separated data items in in_data
ndata = float(data_list[2]) # ndata is the third item in the list, in the form of a floating point number
ndata = ndata * 10
ndata_list.append(ndata)
fp_in.close()
# we have our list of data ready, so do some binning
# see https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.histogram.html
number_of_bins = 12
(bins, bin_edges) = numpy.histogram(ndata_list, number_of_bins)
out_file = "binned_data.txt" # setup and open the output file
fp_out = open(out_file, "w")
print_text = [] # create an empty list
for i in range(len(bins)): # go through the bins array
thisLine = str(bin_edges[i]) # and create a string containing this value of bin_edges
thisLine += ", " # append a comma and some spaces to the string
thisLine += str(bins[i]) # append this value of bins to the string
print_text.append(thisLine) # append the string to the print_text list
print_text.append(str(bin_edges[-1])) # after the loop, append the last value of bin_edges to the print_text list
for line in print_text: # go through the print_text list, item by item
fp_out.write(line) # and write this item to the output file
fp_out.write("\n") # write an end-of-line character to the output file
fp_out.close()
print("Data binned, end of run")
#=======================================================================================================================
main()