annotate SMART/Java/Python/plotCsv.py @ 13:03045debed6e

Uploaded
author m-zytnicki
date Wed, 17 Apr 2013 10:39:35 -0400
parents 769e306b7933
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 #! /usr/bin/env python
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 # Copyright INRA-URGI 2009-2010
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 Plot the data from the data files
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 import os
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 import re
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from optparse import OptionParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from SMART.Java.Python.misc.RPlotter import *
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 from SMART.Java.Python.misc.Progress import *
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 def mergeData(line1, line2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 if line1.keys() != line2.keys():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 sys.exit("Error! Input files do not correspond to each other! Aborting...")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 mergedData = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 for key in line1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 mergedData[key] = (line1[key], line2[key])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 return mergedData
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 # parse command line
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 description = "Plot CSV v1.0.1: Plot the content of a CSV file. [Category: Personnal]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 parser.add_option("-i", "--input", dest="inputFileNames", action="store", type="string", help="input file [compulsory] [format: file in CSV format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [compulsory] [format: output file in PNG format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 parser.add_option("-s", "--shape", dest="shape", action="store", type="string", help="shape of the plot [format: choice (line, bar, points, heatPoints)]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 parser.add_option("-l", "--log", dest="log", action="store", default="", type="string", help="use log on x- or y-axis (write 'x', 'y' or 'xy') [format: string] [default: ]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 plotter = RPlotter(options.outputFileName, options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 if options.shape == "bar":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 plotter.setBarplot(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 elif options.shape == "points":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 plotter.setPoints(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 elif options.shape == "heatPoints":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 plotter.setHeatPoints(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 plotter.setLog(options.log)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 lines = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 nbsColumns = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 for inputFileName in options.inputFileNames.split(","):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 inputFile = open(inputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 line = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 nbColumns = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 for point in inputFile:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 point = point.strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 m = re.search(r"^\s*(\S+)\s+(\d+\.?\d*)\s+(\d+\.?\d*)\s*$", point)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 if m != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 line[m.group(1)] = (float(m.group(2)), float(m.group(3)))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 if nbColumns == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 nbColumns = 3
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 nbsColumns.append(nbColumns)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 elif nbColumns != 3:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 sys.exit("Number of columns changed around line '%s' of file '%s'! Aborting..." % (point, inputFileName))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 m = re.search(r"^\s*(\d+\.?\d*)\s+(\d+\.?\d*)\s*$", point)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 if m != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 line[float(m.group(1))] = float(m.group(2))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 if nbColumns == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 nbColumns = 2
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 nbsColumns.append(nbColumns)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 if nbColumns != 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 sys.exit("Number of columns changed around line '%s' of file '%s'! Aborting..." % (point, inputFileName))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 m = re.search(r"^\s*(\S+)\s+(\d+\.?\d*)\s*$", point)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 if m != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 line[m.group(1)] = float(m.group(2))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 if nbColumns == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 nbColumns = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 nbsColumns.append(nbColumns)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 if nbColumns != 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 sys.exit("Number of columns changed around line '%s' of file '%s'! Aborting..." % (point, inputFileName))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 sys.exit("Do not understand line '%s' of file '%s'! Aborting..." % (point, inputFileName))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 lines.append(line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 if len(lines) != len(nbsColumns):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 sys.exit("Something is wrong in the input files! Aborting...")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 if options.shape == "bar":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 if len(lines) != 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 sys.exit("Error! Bar plot should have exactly one input file! Aborting...")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 if nbsColumns[0] != 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 sys.exit("Error! Bar plot input file should have exactly two columns! Aborting...")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 plotter.addLine(lines[0])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 elif options.shape == "points":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 if len(lines) != 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 sys.exit("Error! Points cloud should have exactly two input file! Aborting...")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 if nbsColumns[0] != 2 or nbsColumns[1] != 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 sys.exit("Error! Points cloud plot input file should have exactly two columns! Aborting...")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 plotter.addLine(mergedData(lines[0], lines[1]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 elif options.shape == "heatPoints":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 if len(lines) != 3:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 sys.exit("Error! Heat points cloud should have exactly three input file! Aborting...")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 plotter.addLine(mergeData(lines[0], lines[1]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 plotter.addHeatLine(lines[2])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 elif options.shape == "line":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 for i in range(0, len(lines)):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 if (nbsColumns[i] != 2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 sys.exit("Error! Curve plot input file should have exactly two columns! Aborting...")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 plotter.addLine(lines[i])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 sys.exit("Do not understand shape '%s'" % (options.shape))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 plotter.plot()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146