-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkgraph.py
218 lines (191 loc) · 7.33 KB
/
mkgraph.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/local/bin/python3
# A script to plot a graph on values recorded in an csv-input file. The file
# has the following format:
#
# Header: posx,posy,posz,time,prop-name-1,prop-name-2,...
# The rows follow accordingly.
# Note: (posx,posy,posx) represents a 3D coordinate of a position/location.
# posy is for now ignored.
#
# Two type of graphs can be produced:
#
# (1) time-graph plots the values of chosen properties vs time
# (2) heatmap shows the values of chosen properties vs position (x,z)
#
# General syntax to use this script:
#
# > mkgraph [options] arg1 arg2 ...
#
# You can do "mkgraph --help" to get some help-info.
# Example usages:
#
# > mkgraph -i input.csv prop-name-1
# > mkgraph -i input.csv health (if health is a property-name in the csv-file)
#
# You can also do:
#
# > mkgraph --help
#
#
# Options:
# -i filename : specify the input csv-file
# -o output : specify the name of the file to save the resulting graph (.png format)
# -g graphtype : either "timegraph" or "heatmap"
#
# For heatmaps, we have further options:
# --hmWidth=w and --hmHeight=h : the width and height of the produced map (1 unit length
# corespond to one 1-unit length of the coordinate-system
# used to express positions in the input csv-file).
# --hmScale=s : you can imagine the heatmap to be constructed from tiles of sxs in size.
# Positions in the same tile are then considered as representing the same
# position.
# --hmMinval=a, --hmMaxval=b : the assumed minimum and maximum values of attributes whose values
# are to be plotted into the heatmap./
#
saveToFile = True
import sys
from optparse import OptionParser
import matplotlib
if saveToFile:
matplotlib.use('Agg') # to generate png output, must be before importing matplotlib.pyplot
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np
import math
import csv
import os
import pprint
import statistics
import scipy.stats as scistats
# Parsed command-line options and arguments to this script will be set here.
#
# Available options:
# scriptOptions.inputFile
# scriptOptions.graphType
# scriptOptions.heatmapWidth
# scriptOptions.heatmapHeight
# scriptOptions.heatmapValMin
# scriptOptions.heatmapValMax
# scriptOptions.heatmapSizeScale
#
# The script-args specify the properties from the input data that are
# to be plotted.
scriptOptions = None
selectedProperties = None
def loadCSV(csvfile):
""" A help function to read a csv-file. """
# need to use a correct character encoding.... latin-1 does it
with open(csvfile, encoding='latin-1') as file:
content = csv.DictReader(file, delimiter=',')
rows = []
for row in content: rows.append(row)
return rows
def parseCommandLineArgs():
""" A help function to parse the command-line arguments of this script. """
parser = OptionParser("usage: %prog [options] arg1 arg2 (each arg is a property-name/column-name in the inputfile)")
parser.add_option("-i", dest="inputFile", help="input csv-file")
parser.add_option("-o", dest="outputFile", default="plot.png", help="output file")
parser.add_option("-g", dest="graphType", default="timegraph",
choices=["timegraph","heatmap","coldmap"],
help="the type of graph")
parser.add_option("--hmWidth", dest="heatmapWidth" , help="the width of heatmap")
parser.add_option("--hmHeight", dest="heatmapHeight", help="the height of heatmap")
parser.add_option("--hmScale", dest="heatmapSizeScale", default="1", help="the size-scale of heatmap")
parser.add_option("--hmMinval", dest="heatmapValMin", default="0", help="minimum heat value in heatmap")
parser.add_option("--hmMaxval", dest="heatmapValMax", default="100", help="maximum heat value in heatmap")
# parse the options and arguments:
(options,args) = parser.parse_args()
#print("xxx", options, "yyy", args)
#print("a ", options.fileIn)
if(options.inputFile == None):
print(" Specify an input-file.")
exit(2)
if(args == []):
print(" Specify at least one property-name whose values are to be plotted.")
exit(2
)
global scriptOptions
global selectedProperties
scriptOptions = options
selectedProperties = args
def mkTimeProgressionGraph(filename):
"""
A function to draw a time-graph. This plots the values of the properties
specified in propertiesToDisplay over time.
"""
# read the data from the file:
dataset = loadCSV(filename)
plt.ylabel('values', fontsize=12)
plt.xlabel('time', fontsize=12)
plt.grid(b=True, axis='y')
# plot the values of
for propName in selectedProperties:
plt.plot([ int(r['time']) for r in dataset ],
[ float(r[propName]) for r in dataset ],
label = propName , )
plt.rcParams.update({'font.size': 12})
#fig.suptitle("Emotion time progression")
#plt.title(f"{propertiesToDisplay} overtime", fontsize=10)
plt.title("values overtime", fontsize=10)
plt.legend()
if saveToFile : plt.savefig(scriptOptions.outputFile)
else : plt.show()
def mkHeatMap(filename,
width,
height,
scale,
minvalue,
maxvalue):
""" A function to draw heatmap. """
dataset = loadCSV(filename)
white = maxvalue
H = math.ceil(scale*height)
W = math.ceil(scale*width)
map = np.zeros((H,W))
for y in range(0,H):
for x in range(0,W):
map[y][x] = white
for r in dataset:
x = round(scale*float(r['posx']))
y = round(scale*float(r['posz']))
# combine the value of the properties by summing them:
value = -minvalue
for propName in selectedProperties:
if r[propName] != '':
value = value + float(r[propName])
#gold = float(r['gold'])
#satisfaction = float(r['satisfaction'])
#combined = 10*(hope + 1.1*joy + 1.5*satisfaction)
if map[(y,x)]==white:
map[(y,x)] = value
else:
#map[(y,x)] = max(map[(y,x)],value)
map[(y,x)] = map[(y,x)] + 1
ax = plt.gca()
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
plt.imshow(map, cmap='hot', origin='lower', interpolation='nearest')
#plt.imshow(map, cmap='hot', origin='lower', interpolation='bilinear')
plt.title("heat map")
#plt.legend()
if saveToFile : plt.savefig(scriptOptions.outputFile)
else : plt.show()
def main():
## Parsing the command-line argumentrs
parseCommandLineArgs()
print('** Input file: ', scriptOptions.inputFile)
print('** Graph type: ', scriptOptions.graphType)
print('** Properties to show: ', selectedProperties)
#print(content)
if(scriptOptions.graphType=="timegraph") :
mkTimeProgressionGraph(scriptOptions.inputFile)
elif(scriptOptions.graphType=="heatmap") :
mkHeatMap(scriptOptions.inputFile,
int(scriptOptions.heatmapWidth),
int(scriptOptions.heatmapHeight),
float(scriptOptions.heatmapSizeScale),
float(scriptOptions.heatmapValMin),
float(scriptOptions.heatmapValMax)
)
if __name__ == "__main__":
main()