-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc_Output.cpp
More file actions
396 lines (332 loc) · 13.1 KB
/
c_Output.cpp
File metadata and controls
396 lines (332 loc) · 13.1 KB
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
396
#include "c_Output.h"
// __ ___ _ _ _ _
// \ \ / (_) | (_) | | (_)
// \ \ / / _ ___ _ _ __ _| |_ ___ __ _| |_ _ ___ _ __
// \ \/ / | / __| | | |/ _` | | / __|/ _` | __| |/ _ \| '_ \
// \ / | \__ \ |_| | (_| | | \__ \ (_| | |_| | (_) | | | |
// \/ |_|___/\__,_|\__,_|_|_|___/\__,_|\__|_|\___/|_| |_|
//
//
std::string c_Output::colouriser(int g, int columnlist, int articlelist, std::string colourFlag)
{
std::string pagedata = ""; // create page data variable
int currart = articlelist; // first article to process (last one entered)
int flagInt = 0;
int currcol = 0;
while (currart >= 0)
{
while (currcol <= columnlist) // for each column on the page
{
if (currcol == 0)
{
pagedata = pagedata + "[";
}
if (currart > int(transformationInstance.currentCopy.pages[g].columns[currcol].articles.size()) - 1)
// if it doesn't have a word count
{
pagedata = pagedata + "0"; // use 0
}
else {
// if it does have a word count
std::string key_value = transformationInstance.currentCopy.pages[g].columns[currcol].articles[currart].key_value;
for (size_t i = 0; i < legendInstance.legendEntries.size(); i++)
{
if (key_value == legendInstance.legendEntries[i].keyCharacter)
{
pagedata = pagedata + std::to_string(legendInstance.selectedColourScheme[i]); // use legend colour
}
}
}
if (currcol == columnlist) // if it is the last column on the page
{
pagedata = pagedata + "]"; // end the article list
}
else // if its not
{
pagedata = pagedata + ","; // add comma
}
++currcol; // advance to next column
}
currcol = 0; // reset column counter
if (currart == 0)
// if it is the last article (of the most populated column)
{
pagedata = pagedata + "]"; // end the list of lists
}
else // if it isn't the last article
{
pagedata = pagedata + ","; // add a comma
}
--currart; // advanced to next article counter (down one number)
}
return pagedata;
}
std::string c_Output::patterniser(int g, int columnlist, int articlelist, std::string colourFlag)
{
std::string pagedata = ""; // create page data variable
int currart = articlelist; // first article to process (last one entered)
int flagInt = 0;
int currcol = 0;
while (currart >= 0)
{
while (currcol <= columnlist) // for each column on the page
{
if (currcol == 0)
{
pagedata = pagedata + "[";
}
if (currart > int(transformationInstance.currentCopy.pages[g].columns[currcol].articles.size()) - 1)
// if it doesn't have a word count
{
pagedata = pagedata + "0"; // use 0
}
else {
// if it does have a word count
std::string key_value = transformationInstance.currentCopy.pages[g].columns[currcol].articles[currart].key_value;
for (size_t i = 0; i < legendInstance.legendEntries.size(); i++)
{
if (key_value == legendInstance.legendEntries[i].keyCharacter)
{
pagedata = pagedata + "\'" + legendInstance.patternScheme[i] + "\'" ; // use legend colour
}
}
}
if (currcol == columnlist) // if it is the last column on the page
{
pagedata = pagedata + "]"; // end the article list
}
else // if its not
{
pagedata = pagedata + ","; // add comma
}
++currcol; // advance to next column
}
currcol = 0; // reset column counter
if (currart == 0)
// if it is the last article (of the most populated column)
{
pagedata = pagedata + "]"; // end the list of lists
}
else // if it isn't the last article
{
pagedata = pagedata + ","; // add a comma
}
--currart; // advanced to next article counter (down one number)
}
return pagedata;
}
std::string c_Output::wordCountiser(int g, int columnlist, int articlelist)
{
std::string pagedata = "["; // create page data variable
int currart = articlelist; // first article to process (last one entered)
int flagInt = 0;
int currcol = 0;
while (currart >= 0)
{
pagedata = pagedata + "["; // create list of article word counts
while (currcol <= columnlist) // for each column on the page
{
if (currart > int(transformationInstance.currentCopy.pages[g].columns[currcol].articles.size()) - 1)
// if it doesn't have a word count
{
pagedata = pagedata + "0"; // use 0
}
else {
// if it does have a word count
pagedata = pagedata
+ std::to_string(transformationInstance.currentCopy.pages[g].columns[currcol].articles[currart].wc);
// add the word count to the list
}
if (currcol == columnlist) // if it is the last column on the page
{
pagedata = pagedata + "]"; // end the article list
}
else // if its not
{
pagedata = pagedata + ","; // add comma
}
++currcol; // advance to next column
}
currcol = 0; // reset column counter
if (currart == 0)
// if it is the last article (of the most populated column)
{
pagedata = pagedata + "]"; // end the list of lists
}
else // if it isn't the last article
{
pagedata = pagedata + ","; // add a comma
}
--currart; // advanced to next article counter (down one number)
}
return pagedata;
}
std::string c_Output::labeliser(int g, int columnlist, int articlelist)
{
std::string pagedata = "["; // create page data variable
int currart = articlelist; // first article to process (last one entered)
int flagInt = 0;
int currcol = 0;
while (currart >= 0)
{
pagedata = pagedata + "["; // create list of article word counts
while (currcol <= columnlist) // for each column on the page
{
if (currart > int(transformationInstance.currentCopy.pages[g].columns[currcol].articles.size()) - 1)
// if it doesn't have a word count
{
pagedata = pagedata + "'No Content'"; // use 0
}
else {
// if it does have a word count
pagedata = pagedata
+ "'" + (transformationInstance.currentCopy.pages[g].columns[currcol].articles[currart].type) + "'";
// add the article type to the list
}
if (currcol == columnlist) // if it is the last column on the page
{
pagedata = pagedata + "]"; // end the article list
}
else // if its not
{
pagedata = pagedata + ","; // add comma
}
++currcol; // advance to next column
}
currcol = 0; // reset column counter
if (currart == 0)
// if it is the last article (of the most populated column)
{
pagedata = pagedata + "]"; // end the list of lists
}
else // if it isn't the last article
{
pagedata = pagedata + ","; // add a comma
}
--currart; // advanced to next article counter (down one number)
}
return pagedata;
}
int c_Output::findmaxarticleno(int g)
{
size_t maxno = 0; // start with assumption of 0 articles as test condition
for (size_t j = 0; j < transformationInstance.currentCopy.pages[g].columns.size(); j++) // for every column
{
if (maxno < transformationInstance.currentCopy.pages[g].columns[j].articles.size())
// check to see if its number of articles is the most you've seen so far
{
maxno = int(transformationInstance.currentCopy.pages[g].columns[j].articles.size());
// if it is, change the test condition
}
}
return maxno; // send maximum number of articles / column for this page onward
}
void c_Output::graphicMaker(std::string colourFlag)
{
// declare filename variable
std::string filename;
// remove spaces from title
transformationInstance.currentCopy.documentID.erase(std::remove(transformationInstance.currentCopy.documentID.begin(), transformationInstance.currentCopy.documentID.end(), ' '), transformationInstance.currentCopy.documentID.end());
// create filename stub
std::string filenameStub = transformationInstance.currentCopy.documentID;
// declare and start python script variable
std::string pythonScript = "import matplotlib.pyplot as plt" + lineBreak
+ "import numpy as np" + lineBreak;
// declare page data matrix variable
std::string pageData = "";
// for each page
for (size_t g = 0; g < transformationInstance.currentCopy.pages.size(); g++)
{
// set max columns and articles
int columnlist = int(transformationInstance.currentCopy.pages[g].columns.size() - 1); // last column
int articlelist = findmaxarticleno(g) - 1; // last article
// create colour matrix
pageData = colouriser(g, columnlist, articlelist, colourFlag);
// add colour matrix to python file
pythonScript = pythonScript + "values_colour = [" + pageData + lineBreak;
// create wordcount matrix
pageData = wordCountiser(g, columnlist, articlelist);
// add wordcount matrix data to python file
pythonScript = pythonScript + "values = " + pageData + lineBreak;
// create label matrix
pageData = labeliser(g, columnlist, articlelist);
// add label matrix to python file
pythonScript = pythonScript + "values_text = " + pageData + lineBreak;
if (colourFlag == "P")
{
// create pattern matrix
pageData = patterniser(g, columnlist, articlelist, colourFlag);
// add pattern matrix to python file
pythonScript = pythonScript + "values_pattern = [" + pageData + lineBreak;
}
// add general python scripting
pythonScript = pythonScript
+ "max_cols = len(values)" + lineBreak
+ "max_rows = len(values[0])" + lineBreak
+ "values_sums = [sum([r[i] for r in values]) for i in range(max_rows)]" + lineBreak
+ "values_norm = [[v / values_sums[i] for i, v in enumerate(row)] for row in values]" + lineBreak
+ "fig, ax = plt.subplots(1, figsize = (12, 20))" + lineBreak
+ "for row_num in range(max_cols) :" + lineBreak;
if (colourFlag != "P")
{
pythonScript = pythonScript + tab + "ax.bar(range(max_rows), values_norm[row_num], bottom = [sum([values_norm[i][j] for i in range(row_num)]) for j in range(max_rows)], width = 1, edgecolor = '#000000', color = [plt.get_cmap('";
// add colour spectrum type
if (colourFlag == "F") { pythonScript = pythonScript + "hsv"; }
else if (colourFlag == "C") { pythonScript = pythonScript + "nipy_spectral"; }
else if (colourFlag == "G") { pythonScript = pythonScript + "Greys"; }
else if (colourFlag == "P") { pythonScript = pythonScript + "Greys"; }
if (colourFlag == "F")
{
// add general python scripting
pythonScript = pythonScript + "')(i) for i in values_colour[row_num]], alpha=0.5)" + lineBreak;
}
else
{
// add general python scripting
pythonScript = pythonScript + "')(i) for i in values_colour[row_num]])" + lineBreak;
}
}
if (colourFlag == "P")
{
pythonScript = pythonScript + tab + "bars = ax.bar(range(max_rows), values_norm[row_num], bottom = [sum([values_norm[i][j] for i in range(row_num)]) for j in range(max_rows)], width = 1, edgecolor = '#000000', color = [plt.get_cmap('";
// add colour spectrum type
if (colourFlag == "F") { pythonScript = pythonScript + "hsv"; }
else if (colourFlag == "C") { pythonScript = pythonScript + "nipy_spectral"; }
else if (colourFlag == "G") { pythonScript = pythonScript + "Greys"; }
else if (colourFlag == "P") { pythonScript = pythonScript + "Greys"; }
// add general python scripting
pythonScript = pythonScript + "')(i) for i in values_colour[row_num]])" + lineBreak
+ tab + "for bar, pattern in zip(bars, values_pattern[row_num]):" + lineBreak
+ tab + tab + "bar.set_hatch(pattern)" + lineBreak;
}
pythonScript = pythonScript + "ax.set_xlim(-0.5, max_rows - 0.5)\nax.set_xticks([])\nax.set_yticks([])" + lineBreak
+ "ax.set_ylim(0, 1)\nfor x in range(max_rows) :\n\tfor y in range(max_cols) :" + lineBreak
+ tab + tab + "if values[y][x] != 0.0:\n\t\t\t\tax.text(x, values_norm[y][x] / 2 + sum([values_norm[newy][x] for newy in range(y)]), values_text[y][x], fontsize = 10, ha='center', va='center', bbox=dict(facecolor='white', edgecolor='white', boxstyle='round,pad=.1'))" + lineBreak
+ "plt.savefig(\"";
// Set colour spectrum prefix to filename of visualisation
if (colourFlag == "F") { pythonScript = pythonScript + "Colour_"; }
else if (colourFlag == "C") { pythonScript = pythonScript + "Colourblind_"; }
else if (colourFlag == "G") { pythonScript = pythonScript + "Greyscale_"; }
else if (colourFlag == "P") { pythonScript = pythonScript + "Patterned_"; }
// complete python script
pythonScript = pythonScript + filenameStub + "_" + std::to_string(transformationInstance.currentCopy.pages[g].position) + ".png\", bbox_inches='tight')" + lineBreak;
}
// set colour spectrum prefix to filename of visualisation script
if (colourFlag == "F") { filename = "Colour_"; }
else if (colourFlag == "C") { filename = "Colourblind_"; }
else if (colourFlag == "G") { filename = "Greyscale_"; }
else if (colourFlag == "P") { filename = "Patterned_"; }
// complete filename
filename = filename + filenameStub + ".py";
// open python file
std::ofstream matrixfile(filename);
// save data to file
matrixfile << pythonScript;
matrixfile.close();
// create windows batch file to run python script, with "python" command
std::fstream batchFilename;
batchFilename.open("createGraphics.bat", std::fstream::app);
filename = pythonFlag + " legends.py" + lineBreak
+ pythonFlag + " " + filename + lineBreak;
batchFilename << filename;
}