-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_legendCreation.cpp
395 lines (324 loc) · 13.6 KB
/
c_legendCreation.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include "c_LegendCreation.h"
// _ _ _____ _ _
// | | | | / ____| | | (_)
// | | ___ __ _ ___ _ __ __| | | | _ __ ___ __ _| |_ _ ___ _ __
// | | / _ \/ _` |/ _ \ '_ \ / _` | | | | '__/ _ \/ _` | __| |/ _ \| '_ \
// | |___| __/ (_| | __/ | | | (_| | | |____| | | __/ (_| | |_| | (_) | | | |
// |______\___|\__, |\___|_| |_|\__,_| \_____|_| \___|\__,_|\__|_|\___/|_| |_|
// __/ |
// |___/
void c_LegendCreation::legendCreator(std::vector<entry> legendEntries, std::string inputDirectory)
{
// Size legend boxes for horizontal legend
int block = 250 / legendItemCount;
// Begin python script
std::string pythonScript = "import matplotlib.pyplot as plt" + lineBreak
+ "import numpy as np" + lineBreak
+ "lvalues = [[";
// Create legend boxes
for (int i = 0; i < legendItemCount; i++)
{
pythonScript = pythonScript + std::to_string(block);
if (i+1 != legendItemCount) { pythonScript = pythonScript + ","; }
}
pythonScript = pythonScript + "]]" + lineBreak;
// Create legend colours
pythonScript = pythonScript + "lvalues_colour =[[";
for (int i = 0; i < legendItemCount; i++)
{
pythonScript = pythonScript + std::to_string(selectedColourScheme[i]);
if (i+1 != legendItemCount){pythonScript = pythonScript + ","; }
}
pythonScript = pythonScript + "]]" + lineBreak;
// Create legend labels
pythonScript = pythonScript + "lvalues_text = [[";
for (size_t i = 0; i < legendEntries.size(); i++)
{
pythonScript = pythonScript + "'" + legendEntries[i].keyWord;
if (i+1 < size_t(legendItemCount)) { pythonScript = pythonScript + "', "; }
}
pythonScript = pythonScript + "']]" + lineBreak;
// Add Patterns Data, if required
if (legendColourScheme == "P")
{
pythonScript = pythonScript + "lvalues_pattern = [[";
for (size_t i = 0; i < legendEntries.size(); i++)
{
pythonScript = pythonScript + "'" + patternScheme[i];
if (i + 1 < size_t(legendItemCount)) { pythonScript = pythonScript + "', "; }
}
pythonScript = pythonScript + "']]" + lineBreak;
}
// Add in standard python scripting
pythonScript = pythonScript + "lmax_cols = len(lvalues)" + lineBreak
+ "lmax_rows = len(lvalues[0])" + lineBreak
+ "lvalues_sums = [sum([r[i] for r in lvalues]) for i in range(lmax_rows)]" + lineBreak
+ "lvalues_norm = [[v/lvalues_sums[i] for i, v in enumerate(row)] for row in lvalues]" + lineBreak
+ "fig, ax = plt.subplots(1, figsize=(50, 2))" + lineBreak
+ "for row_num in range(lmax_cols):" + lineBreak;
// Colourise without patterns
if (legendColourScheme != "P")
{
pythonScript = pythonScript + tab + "ax.bar(range(lmax_rows), lvalues_norm[row_num], bottom = [sum([lvalues_norm[i][j] for i in range(row_num)]) for j in range(lmax_rows)], width = 1, edgecolor = '#000000', color = [plt.get_cmap('";
// add colour spectrum type
if (legendColourScheme == "F") { pythonScript = pythonScript + "hsv"; }
else if (legendColourScheme == "C") { pythonScript = pythonScript + "nipy_spectral"; }
else if (legendColourScheme == "G") { pythonScript = pythonScript + "Greys"; }
else if (legendColourScheme == "P") { pythonScript = pythonScript + "Greys"; }
if (legendColourScheme == "F")
{
// add general python scripting
pythonScript = pythonScript + "')(i) for i in lvalues_colour[row_num]], alpha=0.5)" + lineBreak;
}
else
{
// add general python scripting
pythonScript = pythonScript + "')(i) for i in lvalues_colour[row_num]])" + lineBreak;
}
}
// OR Colourise and add pattern overlay, if required
else if (legendColourScheme == "P")
{
pythonScript = pythonScript + tab + "bars = ax.bar(range(lmax_rows), lvalues_norm[row_num], bottom = [sum([lvalues_norm[i][j] for i in range(row_num)]) for j in range(lmax_rows)], width = 1, edgecolor = '#000000', color = [plt.get_cmap('";
// add colour spectrum type
if (legendColourScheme == "F") { pythonScript = pythonScript + "hsv"; }
else if (legendColourScheme == "C") { pythonScript = pythonScript + "nipy_spectral"; }
else if (legendColourScheme == "G") { pythonScript = pythonScript + "Greys"; }
else if (legendColourScheme == "P") { pythonScript = pythonScript + "Greys"; }
// add general python scripting
pythonScript = pythonScript + "')(i) for i in lvalues_colour[row_num]])" + lineBreak
+ tab + "for bar, pattern in zip(bars, lvalues_pattern[row_num]):" + lineBreak
+ tab + tab + "bar.set_hatch(pattern)" + lineBreak;
}
// Complete python script
pythonScript = pythonScript + "ax.set_xlim(-0.5, lmax_rows-.5)" + lineBreak
+ "ax.set_xticks([])" + lineBreak
+ "ax.set_yticks([])" + lineBreak
+ "ax.set_ylim(0, 1)" + lineBreak
+ "for x in range(lmax_rows):" + lineBreak
+ tab + "for y in range(lmax_cols):" + lineBreak
+ tab + tab + "if lvalues[y][x] != 0.0:" + lineBreak
+ tab + tab + tab + "ax.text(x, y + .5, lvalues_text[y][x], fontsize = 16, ha='center', va='center', bbox=dict(facecolor='white', edgecolor='white', boxstyle='round,pad=.1'))" + lineBreak
+ "plt.savefig('Legend_Horizontal.png', bbox_inches='tight')" + lineBreak;
// Begin vertical legend with box sizes
pythonScript = pythonScript + "lvalues = [";
for (int i = 0; i < legendItemCount; i++)
{
pythonScript = pythonScript + "[" + std::to_string(block);
if (i + 1 != legendItemCount) { pythonScript = pythonScript + "],"; }
}
pythonScript = pythonScript + "]]" + lineBreak;
// Add colour values
pythonScript = pythonScript + "lvalues_colour =[";
for (int i = 0; i < legendItemCount; i++)
{
pythonScript = pythonScript + "[" + std::to_string(selectedColourScheme[i]);
if (i+1 != legendItemCount) { pythonScript = pythonScript + "],"; }
}
pythonScript = pythonScript + "]]" + lineBreak;
// Add labels
pythonScript = pythonScript + "lvalues_text = [";
for (size_t i = 0; i < legendEntries.size(); i++)
{
pythonScript = pythonScript + "['" + legendEntries[i].keyWord;
if (i+1 != legendItemCount) { pythonScript = pythonScript + "'],"; }
}
pythonScript = pythonScript + "']]" + lineBreak;
// Add Patterns Data, if required
if (legendColourScheme == "P")
{
pythonScript = pythonScript + "lvalues_pattern = [";
for (size_t i = 0; i < legendEntries.size(); i++)
{
pythonScript = pythonScript + "['" + patternScheme[i];
if (i + 1 < size_t(legendItemCount)) { pythonScript = pythonScript + "'],"; }
}
pythonScript = pythonScript + "']]" + lineBreak;
}
// Add standard python scripting
pythonScript = pythonScript + "lmax_cols = len(lvalues)" + lineBreak
+ "lmax_rows = len(lvalues[0])" + lineBreak
+ "lvalues_sums = [sum([r[i] for r in lvalues]) for i in range(lmax_rows)]" + lineBreak
+ "lvalues_norm = [[v/lvalues_sums[i] for i, v in enumerate(row)] for row in lvalues]" + lineBreak
+ "fig, ax = plt.subplots(1, figsize=(4, 20))" + lineBreak
+ "for row_num in range(lmax_cols):" + lineBreak;
// Colourise legend
if (legendColourScheme != "P")
{
pythonScript = pythonScript + tab + "ax.bar(range(lmax_rows), lvalues_norm[row_num], bottom = [sum([lvalues_norm[i][j] for i in range(row_num)]) for j in range(lmax_rows)], width = 1, edgecolor = '#000000', color = [plt.get_cmap('";
// add colour spectrum type
if (legendColourScheme == "F") { pythonScript = pythonScript + "hsv"; }
else if (legendColourScheme == "C") { pythonScript = pythonScript + "nipy_spectral"; }
else if (legendColourScheme == "G") { pythonScript = pythonScript + "Greys"; }
else if (legendColourScheme == "P") { pythonScript = pythonScript + "Greys"; }
if (legendColourScheme == "F")
{
// add general python scripting
pythonScript = pythonScript + "')(i) for i in lvalues_colour[row_num]], alpha=0.5)" + lineBreak;
}
else
{
// add general python scripting
pythonScript = pythonScript + "')(i) for i in lvalues_colour[row_num]])" + lineBreak;
}
}
// Colourise legend with pattern, if required
else if (legendColourScheme == "P")
{
pythonScript = pythonScript + tab + "bars = ax.bar(range(lmax_rows), lvalues_norm[row_num], bottom = [sum([lvalues_norm[i][j] for i in range(row_num)]) for j in range(lmax_rows)], width = 1, edgecolor = '#000000', color = [plt.get_cmap('";
// add colour spectrum type
if (legendColourScheme == "F") { pythonScript = pythonScript + "hsv"; }
else if (legendColourScheme == "C") { pythonScript = pythonScript + "nipy_spectral"; }
else if (legendColourScheme == "G") { pythonScript = pythonScript + "Greys"; }
else if (legendColourScheme == "P") { pythonScript = pythonScript + "Greys"; }
// add general python scripting
pythonScript = pythonScript + "')(i) for i in lvalues_colour[row_num]])" + lineBreak
+ tab + "for bar, pattern in zip(bars, lvalues_pattern[row_num]):" + lineBreak
+ tab + tab + "bar.set_hatch(pattern)" + lineBreak;
}
// Complete legend code
pythonScript = pythonScript + "ax.set_xlim(-0.5, lmax_rows-.5)" + lineBreak
+ "ax.set_xticks([])" + lineBreak
+ "ax.set_yticks([])" + lineBreak
+ "ax.set_ylim(0, 1)" + lineBreak
+ "for x in range(lmax_rows):" + lineBreak
+ tab + "for y in range(lmax_cols):" + lineBreak
+ tab + tab + "if lvalues[y][x] != 0.0:" + lineBreak
+ tab + tab + tab + "ax.text(x, lvalues_norm[y][x] / 2 + sum([lvalues_norm[newy][x] for newy in range(y)]), lvalues_text[y][x], fontsize = 16, ha='center', va='center', bbox=dict(facecolor='white', edgecolor='white', boxstyle='round,pad=.1'))" + lineBreak
+ "plt.savefig('Legend_Vertical.png', bbox_inches='tight')";
// open python file
std::string filename = "legends.py";
std::ofstream legendsFile(filename);
legendsFile << pythonScript; // save data to file
legendsFile.close();
}
std::string c_LegendCreation::colourSchemeChooser()
{
// Ask for colour or pattern scheme
do
{
std::cout << "\nWhich colour scheme would you like: \n"
<< "\t (F)ull Colour \n"
<< "\t (C)olourblind Friendly \n"
<< "\t (G)rey Scale \n"
<< "\t (P)atterns \n";
std::cin >> legendColourScheme;
} while (legendColourScheme != "F"
&& legendColourScheme != "C"
&& legendColourScheme != "G"
&& legendColourScheme != "P");
return legendColourScheme;
}
void c_LegendCreation::colourSchemeLoader(int legendItemCount)
{
// If not set, ask for colour scheme
if (legendColourScheme == "Q")
{
legendColourScheme = colourSchemeChooser();
}
// Set Colour Scheme
if (legendColourScheme == "F")
{
selectedColourScheme[0] = 256 / legendItemCount;
for (int i = 2; i < legendItemCount + 1; i++) { selectedColourScheme[i - 1] = 256 / legendItemCount * i; }
}
else if (legendColourScheme == "C")
{
for (size_t i = 0; i < 5; i++) { selectedColourScheme[i] = colourblind[i]; }
}
else if (legendColourScheme == "G")
{
selectedColourScheme[0] = 256/legendItemCount;
for (int i = 2; i < legendItemCount + 1; i++) { selectedColourScheme[i-1] = 256/legendItemCount * i; }
}
else if (legendColourScheme == "P")
{
selectedColourScheme[0] = 200/legendItemCount;
for (int i = 2; i < legendItemCount + 1; i++) { selectedColourScheme[i-1] = 200 / legendItemCount * i; }
}
}
std::string c_LegendCreation::legendQuery(std::string inputDirectory)
{
do
{
// Ask for number of legend items (maximum 9, except colour blind, maximum 6)
if (legendColourScheme == "C")
{
do
{
std::cout << "How many legend items do you have (maximum 5)?" << std::endl;
std::cin >> legendItemCount;
} while (legendItemCount > 5 || legendItemCount < 0);
}
else
{
do
{
std::cout << "How many legend items do you have (maximum 9)?" << std::endl;
std::cin >> legendItemCount;
} while (legendItemCount > 9 || legendItemCount < 0);
}
legendEntries.clear();
// Ask for item 1 (repeat through number)
for (int i = 0; i <= legendItemCount - 1; i++)
{
std::string tempVariable;
legendEntries.push_back(blankEntry);
if (i == 0) { std::cin.ignore(); }
std::cout << "What is item #" << i + 1 << "? ";
std::getline(std::cin,legendEntries[i].keyWord = tempVariable);
std::cout << "What is the key character(s) used for this type in your data file (no spaces)? ";
std::getline(std::cin, legendEntries[i].keyCharacter = tempVariable);
}
std::cout << std::endl;
for (size_t i = 0; i < legendEntries.size(); i++)
{
// Display Legend
std::cout << i + 1 << ". " << legendEntries[i].keyWord << "\t\t(" << legendEntries[i].keyCharacter << ")" << std::endl;
}
// Confirm Legend
std::cout << std::endl << "Is this correct (y/n) ? " << std::endl;
std::cin >> responseString;
// If yes, return
if (responseString == "y")
{
colourSchemeLoader(legendItemCount);
legendCreator(legendEntries, inputDirectory);
legendLoaded = "Y";
}
// Otherwise, start over
} while (responseString != "y");
return legendColourScheme;
}
void c_LegendCreation::legendLoader(std::string inputDirectory, std::string legendFile)
{
// Clear Vector from previous run
legendEntries.clear();
// Load file
std::ifstream input(inputDirectory + legendFile);
while (input)
{
// Separate by line
std::string line;
for (std::string line; std::getline(input, line, '\n');)
{
// Separate by field
std::istringstream iline(line);
while (iline)
{
std::string field;
std::getline(iline, field, '\t');
buffer.push_back(field);
}
legendEntries.push_back(blankEntry);
size_t current = legendEntries.size() - 1;
legendEntries[current].keyWord = buffer[0];
legendEntries[current].keyCharacter = buffer[1];
buffer.clear();
}
// Create Legend
legendItemCount = legendEntries.size();
colourSchemeLoader(legendItemCount);
legendCreator(legendEntries, inputDirectory);
}
}