-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnarracat_ternary.py
701 lines (638 loc) · 28.1 KB
/
narracat_ternary.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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# -----------------------------------------------------------------------------------------------------------------
# NarraCat: Tools for Narrative Catalysis
# -----------------------------------------------------------------------------------------------------------------
# License: Affero GPL 1.0 http://www.affero.org/oagpl.html
# Google Code Project: http://code.google.com/p/narracat/
# Copyright 2011 Cynthia Kurtz
# -----------------------------------------------------------------------------------------------------------------
# This file:
#
# Methods that graph ternary data
# -----------------------------------------------------------------------------------------------------------------
from narracat_constants import *
from narracat_stats import *
from narracat_data import *
import colorsys
from narracat_graph import *
# -----------------------------------------------------------------------------------------------------------------
# graphing methods that write one PNG file
# -----------------------------------------------------------------------------------------------------------------
def graphPNGTernaryPlot(xValues, yValues, zValues, sizes, xAxisName, yAxisName, zAxisName, graphName, pngFileName, pngFilePath, drawStats=True):
plotXValues = []
plotYValues = []
totalX = 0
totalY = 0
totalZ = 0
#print graphName
#print "%s\t%s\t%s" % (xAxisName, yAxisName, zAxisName)
for i in range(len(xValues)):
x = xValues[i]
y = yValues[i]
z = zValues[i]
totalX += x
totalY += y
totalZ += z
#print "%s\t%s\t%s" % (x, y, z)
plotX, plotY = xyTransformForTernaryPlot(x, y, z)
plotXValues.append(plotX)
plotYValues.append(plotY)
meanX = totalX / len(xValues)
meanY = totalY / len(yValues)
meanZ = totalZ / len(zValues)
plotMeanX, plotMeanY = xyTransformForTernaryPlot(meanX, meanY, meanZ)
stdX = np.std(np.array(xValues))
stdY = np.std(np.array(yValues))
stdZ = np.std(np.array(zValues))
meanPlusStdX = min(1, meanX + stdX)
meanPlusStdY = min(1, meanY + stdY)
meanPlusStdZ = min(1, meanZ + stdZ)
meanMinusStdX = max(0, meanX - stdX)
meanMinusStdY = max(0, meanY - stdY)
meanMinusStdZ = max(0, meanZ - stdZ)
plotMeanPlusStdX, plotMeanPlusStdY = xyTransformForTernaryPlot(meanPlusStdX, meanPlusStdY, meanPlusStdZ)
plotMeanMinusStdX, plotMeanMinusStdY = xyTransformForTernaryPlot(meanMinusStdX, meanMinusStdY, meanMinusStdZ)
npArrayX = np.array(plotXValues)
npArrayY = np.array(plotYValues)
if len(xValues) > 100 or sizes:
alpha = 0.5
else:
alpha = 1.0
if sizes:
numNonZeroSizes = 0
for size in sizes:
if size > 0:
numNonZeroSizes += 1
numValues = numNonZeroSizes
else:
numValues = len(xValues)
plt.clf()
figure = plt.figure(figsize=(6,6.5))
axes = figure.add_subplot(111)
roomNeeded = 0.2
plt.subplots_adjust(left=roomNeeded)
plt.subplots_adjust(right=1-roomNeeded)
plt.subplots_adjust(top=1-roomNeeded)
plt.subplots_adjust(bottom=roomNeeded)
# hide normal axes
axes.set_frame_on(False)
axes.get_xaxis().set_visible(False)
axes.get_yaxis().set_visible(False)
# draw inner lines at 20, 24, 60, 80% for X, Y and Z
# X dimension
plt.plot([0.2,0.60], [0, 0.693], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.4,0.70], [0, 0.520], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.6,0.80], [0, 0.346], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.8,0.90], [0, 0.173], linewidth=1, c='#E5E5E5', zorder=1)
# Y dimension
plt.plot([0.1,0.2], [0.173,0], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.2,0.4], [0.346,0], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.3,0.6], [0.520,0], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.4,0.8], [0.693,0], linewidth=1, c='#E5E5E5', zorder=1)
# Z dimension
plt.plot([0.90,0.10], [0.173,0.173], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.80,0.20], [0.346,0.346], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.70,0.30], [0.520,0.520], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.60,0.40], [0.693,0.693], linewidth=1, c='#E5E5E5', zorder=1)
# draw outer boundaries
plt.plot([0, 0.5], [0, 0.866], linewidth=1, c='#000000', zorder=2)
plt.plot([1, 0.5], [0, 0.866], linewidth=1, c='#000000', zorder=2)
plt.plot([1, 0], [0, 0], linewidth=1, c='#000000', zorder=2)
# draw points
if sizes:
plt.scatter(npArrayX, npArrayY, c='#3333FF', marker='o', linewidth=0, s=sizes, zorder=10, alpha=alpha)
else:
plt.scatter(npArrayX, npArrayY, c='#3333FF', marker='o', linewidth=0, s=10, zorder=10, alpha=alpha)
if drawStats:
# draw mean, std
plt.scatter(plotMeanX, plotMeanY, marker='o', c='#FF0000', linewidth=0, s=50, zorder=40)
plt.scatter(plotMeanPlusStdX, plotMeanPlusStdY, marker='o', c='#FF0000', linewidth=0, s=20, zorder=20)
plt.scatter(plotMeanMinusStdX, plotMeanMinusStdY, marker='o', c='#FF0000',linewidth=0, s=20, zorder=20)
plt.plot([plotMeanX, plotMeanPlusStdX], [plotMeanY, plotMeanPlusStdY], linewidth=1.5, c='#FF0000', zorder=20)
plt.plot([plotMeanX, plotMeanMinusStdX], [plotMeanY, plotMeanMinusStdY], linewidth=1.5, c='#FF0000', zorder=20)
# label corners
plt.text(0.5, 1, graphName, horizontalalignment='center', fontsize=12)
plt.text(0.5, 0.9, xAxisName, horizontalalignment='center', fontsize=9)
plt.text(-0.05, 0, yAxisName, horizontalalignment='right', fontsize=9)
plt.text(1.05, 0, zAxisName, horizontalalignment='left', fontsize=9)
plt.text(0.5, 0.25, 'n=%s' % numValues, horizontalalignment='center', transform=figure.transFigure)
# standardize sizes - must do this AFTER everything is graphed
axes.set_xlim((-0.2, 1.2))
axes.set_ylim((-0.2, 1.2))
# save to file
plt.savefig(pngFilePath + cleanTextForFileName(pngFileName) + ".png", dpi=200)
plt.close(figure)
def graphPNGTernaryPlotDifferences(xValues, yValues, zValues, xAxisName, yAxisName, zAxisName, graphName, pngFileName, pngFilePath, graph=True):
plotX1Values = []
plotY1Values = []
plotX2Values = []
plotY2Values = []
#print graphName
#print "%s\t%s\t%s" % (xAxisName, yAxisName, zAxisName)
for i in range(len(xValues)):
x = xValues[i]
y = yValues[i]
z = zValues[i]
#print "%s\t%s\t%s" % (x, y, z)
plotX, plotY = xyTransformForTernaryPlot(x, y, z)
if i % 2 == 0:
plotX1Values.append(plotX)
plotY1Values.append(plotY)
else:
plotX2Values.append(plotX)
plotY2Values.append(plotY)
npArrayX1 = np.array(plotX1Values)
npArrayY1 = np.array(plotY1Values)
npArrayX2 = np.array(plotX2Values)
npArrayY2 = np.array(plotY2Values)
if len(xValues) < LOWER_LIMIT_STORY_NUMBER_FOR_COMPARISONS:
return
plt.clf()
figure = plt.figure(figsize=(8,8))
axes = figure.add_subplot(111)
roomNeeded = 0.2
plt.subplots_adjust(left=roomNeeded)
plt.subplots_adjust(right=1-roomNeeded)
plt.subplots_adjust(top=1-roomNeeded)
plt.subplots_adjust(bottom=roomNeeded)
# hide normal axes
axes.set_frame_on(False)
axes.get_xaxis().set_visible(False)
axes.get_yaxis().set_visible(False)
# draw inner lines at 20, 24, 60, 80% for X, Y and Z
# X dimension
plt.plot([0.2,0.60], [0, 0.693], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.4,0.70], [0, 0.520], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.6,0.80], [0, 0.346], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.8,0.90], [0, 0.173], linewidth=1, c='#E5E5E5', zorder=1)
# Y dimension
plt.plot([0.1,0.2], [0.173,0], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.2,0.4], [0.346,0], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.3,0.6], [0.520,0], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.4,0.8], [0.693,0], linewidth=1, c='#E5E5E5', zorder=1)
# Z dimension
plt.plot([0.90,0.10], [0.173,0.173], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.80,0.20], [0.346,0.346], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.70,0.30], [0.520,0.520], linewidth=1, c='#E5E5E5', zorder=1)
plt.plot([0.60,0.40], [0.693,0.693], linewidth=1, c='#E5E5E5', zorder=1)
# draw outer boundaries
plt.plot([0, 0.5], [0, 0.866], linewidth=1, c='#000000', zorder=2)
plt.plot([1, 0.5], [0, 0.866], linewidth=1, c='#000000', zorder=2)
plt.plot([1, 0], [0, 0], linewidth=1, c='#000000', zorder=2)
# draw lines with circles at ends
plt.plot([plotX1Values, plotX2Values], [plotY1Values, plotY2Values], linewidth=0.4, alpha=0.3, c='b', zorder=10)
plt.scatter(plotX2Values, plotY2Values, marker='o', c='b', s=5, linewidth=0, zorder=20, alpha=0.3)
# label corners
plt.text(0.5, 1.1, graphName, horizontalalignment='center', fontsize=12)
plt.text(0.5, 0.9, xAxisName, horizontalalignment='center', fontsize=9)
plt.text(-0.05, 0, yAxisName, horizontalalignment='right', fontsize=9)
plt.text(1.05, 0, zAxisName, horizontalalignment='left', fontsize=9)
note = 'n=%s' % (len(xValues) // 2)
plt.text(0.5, 0.25, note, horizontalalignment='center', fontsize=10, transform=figure.transFigure)
# standardize sizes - must do this AFTER everything is graphed
axes.set_xlim((-0.2, 1.2))
axes.set_ylim((-0.2, 1.2))
# save to file
plt.savefig(pngFilePath + cleanTextForFileName(pngFileName) + ".png", dpi=200, bbox_inches='tight', pad_inches=1.0)
plt.close(figure)
def xyTransformForTernaryPlot(x, y, z):
# from http://staff.aist.go.jp/a.noda/programs/ternary/ternary-en.html
# and http://www.agu.org/pubs/eos-news/supplements/1995-2003/000562e.shtml
plotX = 0.5 * (x + 2.0 * z) / (1.0 * x + y + z)
plotY = (np.sqrt(3.0) / 2.0) * x / (1.0 * x + y + z)
return (plotX, plotY)
# -----------------------------------------------------------------------------------------------------------------
# data integrity checks
# -----------------------------------------------------------------------------------------------------------------
def graphOneGiantTernaryPlotOfAllTernarySetValues(questions, stories):
if not DATA_HAS_TERNARY_SETS:
return
print 'writing giant all ternarySets ternary plot...'
ternaryQuestions = gather3DScaleQuestions(questions)
allArray = []
for question in ternaryQuestions:
numbersArray = question.gatherTernaryValuesFromStories(stories)
if numbersArray:
allArray.extend(numbersArray)
overallPath = OUTPUT_PATH + "overall" + os.sep
if not os.path.exists(overallPath):
os.mkdir(overallPath)
xArray = []
yArray = []
zArray = []
for x,y,z in allArray:
xArray.append(x)
yArray.append(y)
zArray.append(z)
name = "All ternarySet values"
graphPNGTernaryPlot(xArray, yArray, zArray, None, 'x', 'y', 'z', name, name, overallPath)
print ' done writing giant all ternarySets ternary plot.'
def graphTernaryPlotValuesPerParticipant(questions, stories, participants):
if not DATA_HAS_TERNARY_SETS:
return
print 'writing participant ternarySets ternary plots...'
participantPath = OUTPUT_PATH + "ternarySet values by participant" + os.sep
if not os.path.exists(participantPath):
os.mkdir(participantPath)
graphsWritten = 0
for participant in participants:
values = participant.gatherTernaryValues(questions)
xArray = []
yArray = []
zArray = []
for x,y,z in values:
xArray.append(x)
yArray.append(y)
zArray.append(z)
name = "All ternarySet values - %.0f" % float(participant.id)
graphPNGTernaryPlot(xArray, yArray, zArray, None, 'x', 'y', 'z', name, name, participantPath, drawStats=False)
graphsWritten += 1
print ' ... %s graphs written' % graphsWritten
print ' done writing participant ternarySets ternary plots.'
def calculateThirdValueStrengthForTernaryAnswers(questions, stories):
closeThreshold = 10
farThreshold = 40
numThirdIgnoresTotal = 0
numTernarySetsTotal = 0
ternaryQuestions = gather3DScaleQuestions(questions)
for ternaryQuestion in ternaryQuestions:
ternaryValues = ternaryQuestion.gatherTernaryValuesFromStories(stories)
numThirdIgnoresForThisQuestion = 0
numTernarySetsThisQuestion = 0
for x, y, z in ternaryValues:
numCloseTogether = 0
numFarApart = 0
if abs(x-y) < closeThreshold:
numCloseTogether += 1
if abs(x-z) < closeThreshold:
numCloseTogether += 1
if abs(y-z) < closeThreshold:
numCloseTogether += 1
if abs(x-y) > farThreshold:
numFarApart += 1
if abs(x-z) > farThreshold:
numFarApart += 1
if abs(y-z) > farThreshold:
numCloseTogether += 1
numFarApart += 1
if numCloseTogether == 1 and numFarApart == 2:
numThirdIgnoresForThisQuestion += 1
ignored = True
else:
ignored = False
#print ignored, numCloseTogether, x, y, z
numTernarySetsTotal += 1
numTernarySetsThisQuestion += 1
numThirdIgnoresTotal += numThirdIgnoresForThisQuestion
print ternaryQuestion.veryShortName(), numThirdIgnoresForThisQuestion, '/', numTernarySetsThisQuestion, '(', round(100.0 * numThirdIgnoresForThisQuestion / numTernarySetsThisQuestion), '%)'
if numTernarySetsTotal > 0:
print numThirdIgnoresTotal, numTernarySetsTotal, '(', round(100.0 * numThirdIgnoresTotal / numTernarySetsTotal), '%)'
else:
print 'no ternarySets'
# -----------------------------------------------------------------------------------------------------------------
# one ternary plot at a time
# -----------------------------------------------------------------------------------------------------------------
def graphTernaryPlots(questions, stories, scaleID=None, extraName=None, separateDirectories=True, overwriteFiles=False, slice=ALL_DATA_SLICE):
if not DATA_HAS_TERNARY_SETS:
return
print 'writing ternary plots ... '
if extraName:
print ' for %s' % extraName
if slice == ALL_DATA_SLICE:
extra = ""
if scaleID:
ternaryLandscapesPath = OUTPUT_PATH + "ternary plots by scale" + os.sep
else:
ternaryLandscapesPath = OUTPUT_PATH + "ternary plots" + os.sep
else:
extra = slice
if scaleID:
ternaryLandscapesPath = OUTPUT_PATH + "ternary plots by scale" + extra + os.sep
else:
ternaryLandscapesPath = OUTPUT_PATH + "ternary plots" + extra + os.sep
if not os.path.exists(ternaryLandscapesPath):
os.mkdir(ternaryLandscapesPath)
ternaryQuestions = gather3DScaleQuestions(questions)
scaleQuestion = None
if scaleID:
scaleQuestion = questionForID(questions, scaleID)
if not scaleQuestion:
print "SCALE QUESTION NOT FOUND: %s" % scaleID
return
graphsWritten = 0
for question in ternaryQuestions:
#print question.shortName, scaleQuestion.shortName
xArray = []
yArray = []
zArray = []
sizes = []
for story in stories:
if story.matchesSlice(slice):
if not scaleQuestion or story.gatherScaleValue(scaleQuestion.id):
xyzText = story.gatherAnswersForQuestionID(question.id)
if xyzText and xyzText != NO_ANSWER and xyzText != DOES_NOT_APPLY:
xyzTexts = xyzText[0].strip().split('"')
if xyzTexts[0] != DOES_NOT_APPLY:
xArray.append(int(xyzTexts[0]))
yArray.append(int(xyzTexts[1]))
zArray.append(int(xyzTexts[2]))
size = 0
if scaleQuestion:
value = story.gatherScaleValue(scaleQuestion.id)
if value != NO_ANSWER and value != DOES_NOT_APPLY:
size = int(value)
sizes.append(size)
#print story.title, xyzTexts[0], '\t', xyzTexts[1], '\t', xyzTexts[2], '\t', size
#print question.veryShortName(), scaleQuestion.veryShortName(), xArray, yArray, aArray, sizes
if len(xArray):
axisNames = question.shortName.split(" and ")
xAxisName = axisNames[0].strip()
xAxisNameAndGraphName = xAxisName.split(":")
xAxisName = xAxisNameAndGraphName[1].strip()
graphName = xAxisNameAndGraphName[0].strip()
yAxisName = axisNames[1].strip()
zAxisName = axisNames[2].strip()
if scaleQuestion:
if extra:
combinedName = "%s x %s - %s" % (graphName, scaleQuestion.veryShortName(), extra)
else:
combinedName = "%s x %s" % (graphName, scaleQuestion.veryShortName())
else:
if extra:
combinedName = "%s - %s" % (graphName, extra)
else:
combinedName = graphName
# do this before adding extra
if separateDirectories:
comboPath = ternaryLandscapesPath + cleanTextForFileName(combinedName) + os.sep
if not os.path.exists(comboPath):
os.mkdir(comboPath)
else:
comboPath = ternaryLandscapesPath
if extraName:
combinedName = "%s - %s" % (combinedName, extraName)
if overwriteFiles or not os.path.exists(comboPath + cleanTextForFileName(combinedName) + ".png"):
graphPNGTernaryPlot(xArray, yArray, zArray, sizes, xAxisName, yAxisName, zAxisName, combinedName, combinedName, comboPath)
graphsWritten += 1
print ' ... %s graphs written' % graphsWritten
print ' done writing ternary plots.'
def graphTernaryPlotsForQuestionAnswers(questions, stories, overwriteFiles=False):
if not DATA_HAS_TERNARY_SETS:
return
print 'writing ternary plots by question answer ...'
graphsWritten = 0
lowerLimitStoryNumber = LOWER_LIMIT_STORY_NUMBER_FOR_COMPARISONS
for choiceQuestion in questions:
if choiceQuestion.isChoiceQuestion():
answersToCheck = []
answersToCheck.extend(choiceQuestion.shortResponseNames)
answersToCheck.append(NO_ANSWER)
answersToCheck = removeDuplicates(answersToCheck) # because of extra answers in survey
for answer in answersToCheck:
storiesWithThisAnswer = []
for story in stories:
if story.hasAnswerForQuestionID(answer, choiceQuestion.id):
storiesWithThisAnswer.append(story)
if len(storiesWithThisAnswer) >= lowerLimitStoryNumber:
name = "%s: %s" % (choiceQuestion.shortName, answer)
graphTernaryPlots(questions, storiesWithThisAnswer, extraName=name, overwriteFiles=overwriteFiles)
print ' done writing ternary plots by question answer.'
# -----------------------------------------------------------------------------------------------------------------
# two ternary plots compared
# -----------------------------------------------------------------------------------------------------------------
def graphDifferencesBetweenTernaryPlots(questions, stories, extraName=None, separateDirectories=True, overwriteFiles=False, slice=ALL_DATA_SLICE):
if not DATA_HAS_TERNARY_SETS:
return
print 'writing ternary plot differences ... '
if extraName:
print ' for %s' % extraName
if slice == ALL_DATA_SLICE:
extra = ""
ternaryLandscapesPath = OUTPUT_PATH + "ternary plot differences" + os.sep
else:
extra = slice
ternaryLandscapesPath = OUTPUT_PATH + "ternary plot differences" + extra + os.sep
if not os.path.exists(ternaryLandscapesPath):
os.mkdir(ternaryLandscapesPath)
ternaryQuestions = gather3DScaleQuestions(questions)
graphsWritten = 0
for i in range(len(ternaryQuestions)):
for j in range(len(ternaryQuestions)):
if i < j:
firstQuestion = ternaryQuestions[i]
secondQuestion = ternaryQuestions[j]
xArray = []
yArray = []
zArray = []
for story in stories:
if story.matchesSlice(slice):
answer1 = story.gatherAnswersForQuestionID(firstQuestion.id)
answer2 = story.gatherAnswersForQuestionID(secondQuestion.id)
if answer1 and answer2 and answer1[0] != DOES_NOT_APPLY and answer2[0] != DOES_NOT_APPLY:
xyzTexts1 = answer1[0].strip().split('"')
xArray.append(int(xyzTexts1[0]))
yArray.append(int(xyzTexts1[1]))
zArray.append(int(xyzTexts1[2]))
#print xyzTexts1[0], '\t', xyzTexts1[1], '\t', xyzTexts1[2]
xyzTexts2 = answer2[0].strip().split('"')
xArray.append(int(xyzTexts2[0]))
yArray.append(int(xyzTexts2[1]))
zArray.append(int(xyzTexts2[2]))
#print xyzTexts2[0], '\t', xyzTexts2[1], '\t', xyzTexts2[2]
#print firstQuestion.id, secondQuestion.id, len(xArray), len(yArray), len(zArray)
if len(xArray) >= LOWER_LIMIT_STORY_NUMBER_FOR_COMPARISONS:
axisNames1 = firstQuestion.shortName.split(" and ")
xAxisName1 = axisNames1[0].strip()
xAxisNameAndGraphName1 = xAxisName1.split(":")
xAxisName1 = xAxisNameAndGraphName1[1].strip()
graphName1 = xAxisNameAndGraphName1[0].strip()
yAxisName1 = axisNames1[1].strip()
zAxisName1 = axisNames1[2].strip()
axisNames2 = secondQuestion.shortName.split(" and ")
xAxisName2 = axisNames2[0].strip()
xAxisNameAndGraphName2 = xAxisName2.split(":")
xAxisName2 = xAxisNameAndGraphName2[1].strip()
graphName2 = xAxisNameAndGraphName2[0].strip()
yAxisName2 = axisNames2[1].strip()
zAxisName2 = axisNames2[2].strip()
graphName = graphName1 + " x \n" + graphName2
xAxisName = xAxisName1 + " to \n" + xAxisName2
yAxisName = yAxisName1 + " to \n" + yAxisName2
zAxisName = zAxisName1 + " to \n" + zAxisName2
if extra:
combinedName = "%s %s" % (graphName, extra)
else:
combinedName = graphName
# do this before adding extra
if separateDirectories:
comboPath = ternaryLandscapesPath + cleanTextForFileName(combinedName) + os.sep
if not os.path.exists(comboPath):
os.mkdir(comboPath)
else:
comboPath = ternaryLandscapesPath
if extraName:
combinedName = "%s - %s" % (combinedName, extraName)
if overwriteFiles or not os.path.exists(comboPath + cleanTextForFileName(combinedName) + ".png"):
graphPNGTernaryPlotDifferences(xArray, yArray, zArray, xAxisName, yAxisName, zAxisName, combinedName, combinedName, comboPath)
graphsWritten += 1
print ' ... %s graphs written' % graphsWritten
print ' done writing ternary plot differnces.'
def graphTernaryPlotDifferencesForQuestionAnswers(questions, stories, overwriteFiles=False):
if not DATA_HAS_TERNARY_SETS:
return
print 'writing ternary plot differences by question answer ...'
graphsWritten = 0
lowerLimitStoryNumber = LOWER_LIMIT_STORY_NUMBER_FOR_COMPARISONS
for choiceQuestion in questions:
if choiceQuestion.isChoiceQuestion():
answersToCheck = []
answersToCheck.extend(choiceQuestion.shortResponseNames)
answersToCheck.append(NO_ANSWER)
answersToCheck = removeDuplicates(answersToCheck) # because of extra answers in survey
for answer in answersToCheck:
storiesWithThisAnswer = []
for story in stories:
if story.hasAnswerForQuestionID(answer, choiceQuestion.id):
storiesWithThisAnswer.append(story)
if len(storiesWithThisAnswer) >= lowerLimitStoryNumber:
name = "%s: %s" % (choiceQuestion.shortName, answer)
graphDifferencesBetweenTernaryPlots(questions, storiesWithThisAnswer, extraName=name, separateDirectories=True, overwriteFiles=overwriteFiles)
print ' done writing ternary plot differences by question answer.'
# -----------------------------------------------------------------------------------------------------------------
# ternary plots against choice questions
# -----------------------------------------------------------------------------------------------------------------
def compareTernaryMeansForScaleValuesWithQuestionAnswers(questions, stories, overwriteFiles=False, slice=ALL_DATA_SLICE, byQuestion=False):
if not DATA_HAS_TERNARY_SETS:
return
print 'comparing ternary means by question answer ...'
questionsConsidered = 0
lowerLimitStoryNumber = LOWER_LIMIT_STORY_NUMBER_FOR_COMPARISONS
ternaryQuestions = gather3DScaleQuestions(questions)
for ternaryQuestion in ternaryQuestions:
print ' ... ternary %s' % ternaryQuestion.shortName
for choiceQuestion in questions:
if choiceQuestion.isChoiceQuestion():
print ' ... choice question %s' % choiceQuestion.shortName
atLeastOneComparisonIsWorthShowing = False
answerValuesForThisQuestion = {}
answersToCheck = []
answersToCheck.extend(choiceQuestion.shortResponseNames)
answersToCheck.append(NO_ANSWER)
answersToCheck = removeDuplicates(answersToCheck) # because of extra answers in survey
for answer in answersToCheck:
storiesWithThisAnswer = []
for story in stories:
if story.matchesSlice(slice):
if story.hasAnswerForQuestionID(answer, choiceQuestion.id):
storiesWithThisAnswer.append(story)
if len(storiesWithThisAnswer) >= lowerLimitStoryNumber:
ternaryValues = ternaryQuestion.gatherTernaryValuesFromStories(storiesWithThisAnswer)
if len(ternaryValues) > lowerLimitStoryNumber:
answerValuesForThisQuestion[answer] = ternaryValues
results = []
i = 0
j = 0
sizes = []
values = []
colors = []
for firstAnswer in answerValuesForThisQuestion:
sizes.append([])
values.append([])
colors.append([])
for secondAnswer in answerValuesForThisQuestion:
if firstAnswer != secondAnswer:
distance = ternaryDistanceForTwoChoiceQuestions(answerValuesForThisQuestion[firstAnswer], answerValuesForThisQuestion[secondAnswer])
size, color = sizeAndColorForTernaryDistance(distance)
value = distance
else:
size = 0
color = "#000000"
value = 0
sizes[i].append(size)
values[i].append(value)
colors[i].append(color)
if size > 0:
atLeastOneComparisonIsWorthShowing = True
j += 1
i += 1
if atLeastOneComparisonIsWorthShowing:
# set labels with scale names
labels = answerValuesForThisQuestion.keys()
# set path to save file in
distanceTestsStartPath = OUTPUT_PATH + "answer ternary distance graphs" + os.sep
if not os.path.exists(distanceTestsStartPath):
os.mkdir(distanceTestsStartPath)
if byQuestion:
byQuestionPath = distanceTestsStartPath + "by question" + os.sep
if not os.path.exists(byQuestionPath):
os.mkdir(byQuestionPath)
distancesPath = byQuestionPath + cleanTextForFileName(choiceQuestion.shortName) + os.sep
else:
byScalePath = distanceTestsStartPath + "by scale" + os.sep
if not os.path.exists(byScalePath):
os.mkdir(byScalePath)
distancesPath = byScalePath + cleanTextForFileName(ternaryQuestion.veryShortName()) + os.sep
if not os.path.exists(distancesPath):
os.mkdir(distancesPath)
graphName = "Distance - %s with %s (%s)" % (ternaryQuestion.veryShortName(), choiceQuestion.shortName, slice)
note = ""#"Size of circle is degree of difference between means; bright is normal, pale is non-normal or unequal variance."
if len(labels) > 1:
graphCircleMatrix(len(labels), labels, sizes, values, None, None, colors, graphName, note, graphName, distancesPath)
print ' done comparing ternary means by question answer.'
def sizeAndColorForTernaryDistance(distance):
if distance >= 2.0: # arbitrary
# highest distance was 6
size = abs(distance) / 12.0
color = '#FF7722'
else:
size = 0
color = '#000000'
return size, color
def ternaryDistanceForTwoChoiceQuestions(firstValues, secondValues):
x1, y1, z1 = meanXYZOfTernaryValueList(firstValues)
x2, y2, z2 = meanXYZOfTernaryValueList(secondValues)
distance = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2-z1) * (z2-z1)
distance = distance / 100.0
return distance
def meanXYZOfTernaryValueList(valueList):
totalX = 0
totalY = 0
totalZ = 0
for i in range(len(valueList)):
totalX += valueList[i][0]
totalY += valueList[i][1]
totalZ += valueList[i][2]
return (totalX / len(valueList), totalY / len(valueList), totalZ / len(valueList))
# -----------------------------------------------------------------------------------------------------------------
# ternary plots against scales
# -----------------------------------------------------------------------------------------------------------------
def graphTernaryPlotsAgainstScales(questions, stories, extraName=None, separateDirectories=True, overwriteFiles=False, slice=ALL_DATA_SLICE):
if not DATA_HAS_TERNARY_SETS:
return
print 'writing ternarySets against scales ...'
scaleQuestions = gatherScaleQuestions(questions)
for question in scaleQuestions:
graphTernaryPlots(questions, stories, question.id, extraName, separateDirectories=separateDirectories,
overwriteFiles=overwriteFiles, slice=slice)
print ' done writing ternarySets against scales.'
def graphTernaryPlotsAgainstScalesForQuestionAnswers(questions, stories, overwriteFiles=False):
if not DATA_HAS_TERNARY_SETS:
return
print 'writing ternarySets against scales by question answer ...'
graphsWritten = 0
lowerLimitStoryNumber = LOWER_LIMIT_STORY_NUMBER_FOR_COMPARISONS
for choiceQuestion in questions:
if choiceQuestion.isChoiceQuestion():
answersToCheck = []
answersToCheck.extend(choiceQuestion.shortResponseNames)
answersToCheck.append(NO_ANSWER)
answersToCheck = removeDuplicates(answersToCheck) # because of extra answers in survey
for answer in answersToCheck:
storiesWithThisAnswer = []
for story in stories:
if story.hasAnswerForQuestionID(answer, choiceQuestion.id):
storiesWithThisAnswer.append(story)
if len(storiesWithThisAnswer) >= lowerLimitStoryNumber:
name = "%s: %s" % (choiceQuestion.shortName, answer)
graphTernaryPlotsAgainstScales(questions, storiesWithThisAnswer,
extraName=name, separateDirectories=True, overwriteFiles=overwriteFiles)
print ' done writing ternarySets against scales by question answer.'