1
+ #!/usr/bin/python3
2
+
3
+ ##################################################
4
+ ## Python script to generate heatmaps
5
+ #
6
+ ## $ python3 generateJavaConfFile.py -h
7
+ #
8
+ ##################################################
9
+ ## Author: RoiArthurB
10
+ ## Copyright: Copyright 2020, COMOKIT, COMOKIT-HPC
11
+ ## Licence: LGPL 3.0
12
+ ## Version: 1.0.0
13
+ ## Maintainer: RoiArthurB
14
+ ##################################################
15
+
16
+ import os
17
+ import argparse
18
+ import matplotlib .pyplot as plt
19
+ import numpy as np
20
+ import pandas as pd
21
+ import csv
22
+
23
+ # 0 _ Get/Set parameters
24
+ #
25
+ parser = argparse .ArgumentParser (usage = '$ python3 %(prog)s [options]' )
26
+ # CSV
27
+ parser .add_argument ('-f' , '--folder' , metavar = '' , help = "Folder from where scrapping CSV files" , default = "./" , type = str )
28
+ parser .add_argument ('-e' , '--exclude' , metavar = '' , help = "List of variable to exclude" , nargs = '+' , type = str )
29
+ # PNG
30
+ parser .add_argument ('-t' , '--title' , metavar = '' , help = "Graph title" , default = "" , type = str )
31
+ parser .add_argument ('-o' , '--output' , metavar = '' , help = "Path and name of the output file [Default = './out']" , default = "./out" , type = str )
32
+ parser .add_argument ('--csv' , action = 'store_true' , help = 'Save output as CSV file' )
33
+ # Script
34
+ parser .add_argument ('-m' , '--mode' , metavar = '' , help = "[1: Final death number, 2: Last day death]" , type = int , required = True )
35
+ parser .add_argument ('-q' , '--quiet' , action = 'store_true' , help = 'Disable verbose' )
36
+ args = parser .parse_args ()
37
+
38
+ ## Jupyter notebook CLI parameters
39
+ #
40
+ #args.folder = "/tmp/HeadlessComparison"
41
+ #args.exclude = ["allow_transmission_building"]
42
+ #
43
+
44
+
45
+ # 1 _ Scrap CSV
46
+ #
47
+ onlyfiles = [f for f in os .listdir (args .folder ) if os .path .isfile (os .path .join (args .folder , f )) and ("csv" in f )]
48
+
49
+ if not args .quiet :
50
+ print ("Scrapped" , len (onlyfiles ), "csv files" )
51
+
52
+
53
+ # 2 _ Make DataFrame
54
+ #
55
+
56
+ heatmap = pd .DataFrame ()
57
+ # Used in png
58
+ label = {"x" : "-1" , "y" : "-1" }
59
+
60
+ for CSV in onlyfiles :
61
+ col = index = - 1
62
+ variableExcluded = 0
63
+ # Check and create col/row in dataframe
64
+ for i in range (len (CSV .split ("-" ))):
65
+ val = CSV .split ("-" )[i ].rsplit ("_" ,1 )
66
+
67
+ # Eclude parameters from heatmap
68
+ if val [0 ] in args .exclude :
69
+ variableExcluded = variableExcluded + 1
70
+ continue
71
+ else :
72
+ val = val [1 ].split (".csv" )[0 ]
73
+
74
+ # Add col
75
+ if (i == variableExcluded ):
76
+ col = val
77
+ if not (val in heatmap .columns ):
78
+ heatmap [val ]= np .nan
79
+ # Set col variable name
80
+ if label ["x" ] == "-1" :
81
+ label ["x" ] = CSV .split ("-" )[i ].rsplit ("_" ,1 )[0 ]
82
+
83
+ # Add row
84
+ if (i == variableExcluded + 1 ):
85
+ index = val
86
+ if not (val in heatmap .index ):
87
+ heatmap = heatmap .append (pd .Series (name = val , dtype = "float64" ))
88
+ # Set row variable name
89
+ if label ["y" ] == "-1" :
90
+ label ["y" ] = CSV .split ("-" )[i ].rsplit ("_" ,1 )[0 ]
91
+
92
+ # Gather value
93
+ with open (os .path .join (args .folder ,CSV ), 'r' ) as f :
94
+ death = list (csv .reader (f ))[- 1 ]
95
+ if args .mode == 1 :
96
+ heatmap [col ][index ] = np .float64 (death [- 1 ].split (", " )[- 2 ])
97
+
98
+ elif args .mode == 2 :
99
+ previousDeath = np .float64 (death [- 1 ].split (", " )[- 2 ])
100
+ day = len (death )
101
+ for step in reversed (death ):
102
+ nbrDeath = np .float64 (step .split (", " )[- 2 ])
103
+ if nbrDeath < float (previousDeath ):
104
+ heatmap [col ][index ] = int (day / 24 )
105
+ break
106
+ else :
107
+ previousDeath = nbrDeath
108
+ day = day - 1
109
+
110
+ # Reorder row and col
111
+ heatmap = heatmap .sort_index ().sort_index (axis = 1 )
112
+ if not args .quiet :
113
+ print ("Heatmap generation done" )
114
+
115
+ # 3 _ Make heatmap
116
+ #
117
+
118
+ # Saved CSV
119
+ if args .csv :
120
+ pd .DataFrame (heatmap ).to_csv (args .output + '.csv' )
121
+ if not args .quiet :
122
+ print ("CSV exported here: " + args .output + '.csv' )
123
+
124
+ # Displaying dataframe as an heatmap
125
+ plt .imshow (heatmap )
126
+
127
+ colorLabel = ""
128
+ if args .mode == 1 :
129
+ colorLabel = "Number of death"
130
+ if args .mode == 2 :
131
+ colorLabel = "Day of last death"
132
+ plt .colorbar (label = colorLabel )
133
+
134
+ # Assigning labels of x/y-axis
135
+ plt .title (args .title )
136
+ plt .xlabel (label ["x" ])
137
+ plt .ylabel (label ["y" ])
138
+ plt .xticks (range (len (heatmap )), heatmap .columns )
139
+ plt .yticks (range (len (heatmap )), heatmap .index )
140
+
141
+ # Fit output graph
142
+ plt .tight_layout ()
143
+
144
+ # Saved PNG
145
+ plt .savefig (args .output + '.png' )
146
+ if not args .quiet :
147
+ print ("PNG saved here: " + args .output + '.png' )
0 commit comments