-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWCST_Scoring.py
643 lines (483 loc) · 24.9 KB
/
WCST_Scoring.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
from __future__ import with_statement
import re, csv, os
from prettytable import PrettyTable
# The top level folder which contains the tests
PATH = os.getcwd() # r"C:\Users\wyko.terhaar\Downloads\Map voor Wyko\raw"
# Variables which contain the column numbers, used for readability
SUBJECT = 3
MATCH = 5
SET = 6
STIMULUS = 9
RESPONSE = 10
def saveResults(test):
with open('ANALYSIS_' + test[0]['file'] + '.csv', 'w', newline='') as csvfile:
cw = csv.writer(csvfile)
cw.writerow(['#', 'Match', "Response", 'Correct', 'Pres-To', 'Perserverative', 'Ambiguous', "2b (1st Sandwich)", '2c (Chained Sandwich)', '3.1 (Self-Perserveration)', '3.2', '3.3', 'Reasoning'])
prevMatch = ''
prevPrin = ''
for a in test:
response = ''
if a['stimulus']['color'] == a['response']['color']: response += 'C* '
else: response += 'C '
if a['stimulus']['form'] == a['response']['form']: response += 'F* '
else: response += 'F '
if a['stimulus']['number'] == a['response']['number']: response += 'N* '
else: response += 'N '
if a['perservative'] and a['ambiguous']: pers = 'A-Pers'
elif a['perservative']: pers = 'U-Pers'
else: pers = ''
if a['match'] != prevMatch: n = a['match']
else: n = ''
if a['currentPerservativeTo'] != prevPrin:
prin = a['currentPerservativeTo']
prevPrin = prin
else: prin = ''
if a['correct']: corr = '+ '
else: corr = ' '
if a['firstSandwich']: sw= '+'
else: sw = ''
if a['2c']: chain = '+'
else: chain = ''
cw.writerow([a['attemptNum'],
a['match'], #n,
response,
'+' if a['correct'] else '',
a['currentPerservativeTo'], #prin,
'+' if a['perservative'] else '',
'+' if a['ambiguous'] else '',
sw,
chain,
'X' if a['rule_3_1'] else '',
'X' if a['rule_3_2'] else '',
'X' if a['rule_3_3'] else '',
a['reasoning']
])
prevMatch = a['match']
def printTest(test):
x = PrettyTable()
# x.padding_width = 10
x.left_padding_width = 1
x.right_padding_width = 1
x.field_names = ['#', "Match", "Matched", 'Pres-To', 'Pers', "2b", '2c', '3.1', '3.2', '3.3']
prevMatch = ''
prevPrin = ''
for a in test:
response = ''
if a['stimulus']['color'] == a['response']['color']: response += 'C* '
else: response += 'C '
if a['stimulus']['form'] == a['response']['form']: response += 'F* '
else: response += 'F '
if a['stimulus']['number'] == a['response']['number']: response += 'N* '
else: response += 'N '
if a['perservative'] and a['ambiguous']: pers = 'A-Pers'
elif a['perservative']: pers = 'U-Pers'
else: pers = ''
if a['match'] != prevMatch: n = a['match']
else: n = ''
if a['currentPerservativeTo'] != prevPrin:
prin = a['currentPerservativeTo']
prevPrin = prin
else: prin = ''
if a['correct']: corr = '+ '
else: corr = ' '
if a['firstSandwich']: sw= '+'
else: sw = ''
if a['2c']: chain = '+'
else: chain = ''
x.add_row([a['attemptNum'],
a['match'], #n,
corr + response,
a['currentPerservativeTo'], #prin,
pers,
sw,
chain,
'X' if a['rule_3_1'] else '',
'X' if a['rule_3_2'] else '',
'X' if a['rule_3_3'] else '',
])
prevMatch = a['match']
print(x.get_string(title= str(test[0]['classNum']) + ', ' + str(test[0]['testNum'])+ ', ' + str(test[0]['subject']) + ', ' + test[0]['file']))
def splitStim(stim):
x = re.match(r'(^[A-Z][a-z]+)([A-Z][a-z]+)(\d+)', stim)
return {
'color' : x.group(1),
'form' : x.group(2),
'number' : x.group(3)
}
def continuePreviousPresTo(test, attemptNum):
if attemptNum > 0:
test[attemptNum]['currentPerservativeTo'] = test[attemptNum-1]['currentPerservativeTo']
test[attemptNum]['reasoning'] += 'Principle was set to ' + str(test[attemptNum]['currentPerservativeTo']) + ' to continue previous pattern.'
return True
else:
test[attemptNum]['reasoning'] += 'Principle is none due to start of test.'
return None
def checkNewSet(test, attemptNum):
# The very first attempt will never have a Principle
if attemptNum == 0: return None
if test[attemptNum]['set'] != test[attemptNum-1]['set']:
test[attemptNum]['currentPerservativeTo'] = test[attemptNum-1]['match']
test[attemptNum]['reasoning'] += ' - Principle was set to ' + str(test[attemptNum]['currentPerservativeTo']) + ' (last set match clause) because set was changed.'
return True
def checkAnswer(test, attemptNum):
# Determine how ambiguous the answer is
matchQuotient = 0 # This is the number of ways the response matches the answer
for k, v in test[attemptNum]['stimulus'].items():
if test[attemptNum]['response'][k] == v: matchQuotient += 1
# Mark whether the attempt is ambiguous
if matchQuotient > 1:
test[attemptNum]['ambiguous'] = True
else: test[attemptNum]['ambiguous'] = False
# Determine if the answer is correct
if isCorrect(test, attemptNum): test[attemptNum]['correct'] = True
else: test[attemptNum]['correct'] = False
def isCorrect(test, a):
# Determine if a response matches the stimulus on the match criteria
match = test[a]['match']
if test[a]['stimulus'][match] == test[a]['response'][match]: return True
else: return False
def checkFirstSetPers(test, attemptNum):
# Break out if this is not the first set
if test[attemptNum]['set'] != 1: return None
# Break out if this was not an incorrect answer
if test[attemptNum]['correct'] == True: return None
if test[attemptNum]['currentPerservativeTo'] is not None:
test[attemptNum]['reasoning'] += ' - Principle already set. No change for unambiguous error.'
return None
# Check if the attempt had an unambiguous incorrect answer.
# If so, set the Principle whichever principle the client matched
if (test[attemptNum]['correct'] == False and
test[attemptNum]['ambiguous'] == False):
for k, v in test[attemptNum]['stimulus'].items():
if test[attemptNum]['response'][k] == v:
test[attemptNum]['currentPerservativeTo'] = k
test[attemptNum]['set1PrincipleEstablished'] = True
test[attemptNum]['reasoning'] += ' - Principle was established as ' + k + ' from first unambiguous error.'
return True
# If the client perserverated to the Other category, Principle isn't set.
test[attemptNum]['reasoning'] += ' - Client perserverated to Other category. No Principle set.'
return None
def containsPrincipleMatch(test, attemptNum):
# Helper function which satisfies Heaton rule 2a:
# > The ambiguous response must match the
# > perseverated-to principle that is currently in
# > effect (in our example, Color as defined by
# > the previous sorting category)
# False if not principle has been set yet.
if test[attemptNum]['currentPerservativeTo'] is None: return False
pers = test[attemptNum]['currentPerservativeTo']
# Check to see if the response matches the stimulus on the current Perserveration principle.
# This would suggest that this response is perserverating
if test[attemptNum]['stimulus'][pers] == test[attemptNum]['response'][pers]:
# test[attemptNum]['reasoning'] += ' - Attempt has a response (' + test[attemptNum]['response'][pers] + ') which matches the principle (' + pers + ')'
return True
else: return False
def getMatches(test, a):
matches = []
for k, v in test[a]['stimulus'].items():
if test[a]['response'][k] == v: matches.append(k)
return matches
def checkUnambiguousPerserveration(test, attemptNum):
# Check if the attempt had an unambiguous incorrect answer. Skip the answer
# in which the Principle principle was established in the first set.
if (test[attemptNum]['correct'] == False and
test[attemptNum]['ambiguous'] == False and
test[attemptNum]['currentPerservativeTo'] is not None and
test[attemptNum]['set1PrincipleEstablished'] == False and
containsPrincipleMatch(test, attemptNum)
):
test[attemptNum]['reasoning'] += ' - Attempt is unambiguously perservative due to matching the current principle and nothing else.'
test[attemptNum]['perservative'] = True
return True
else: return False
def isSandwiched(test, attemptNum):
# It has to have a principle match to be considered perservative at all
if not containsPrincipleMatch(test, attemptNum): return False
# It has to be ambiguous to be considered for sandwich perseveration
if not test[attemptNum]['ambiguous']: return False
# First we look backwards to find if an ambiguous, potentially perservative
# response was sandwiched by an unambiguous response for the same principle
x = attemptNum - 1
sandwichBefore = False
while x > 0:
if test[x]['set'] != test[attemptNum]['set']: break
if (test[x]['ambiguous'] == False and
test[x]['perservative'] == True and
test[x]['currentPerservativeTo'] == test[attemptNum]['currentPerservativeTo']
):
sandwichBefore = True
# print (str(attemptNum) + ' Sandwiched Before by attempt ' + str(x))
break
x -= 1
if sandwichBefore == False: return False
# Next we check forwards.
y = attemptNum + 1
sandwichAfter = False
while y < len(test):
if test[y]['set'] != test[attemptNum]['set']: break
if (test[y]['ambiguous'] == False and
test[y]['perservative'] == True and
test[y]['currentPerservativeTo'] == test[attemptNum]['currentPerservativeTo']
):
sandwichAfter = True
# print (str(attemptNum) + ' Sandwiched After by attempt ' + str(y))
break
y += 1
if sandwichAfter and sandwichBefore:
#Mark the sandwich if it hasn't already been done
if not test[attemptNum]['sandwiched']: test[attemptNum]['reasoning'] += ' - Attempt ' + str(attemptNum) + ' is "sandwiched" between ' + str(x) + ' and ' + str(y)
test[attemptNum]['sandwiched'] = True
# print (str(attemptNum) + ' Sandwiched Before by attempt ' + str(x))
# print (str(attemptNum) + ' Sandwiched After by attempt ' + str(y))
# print (test[x])
# print (test[attemptNum])
# print (test[y])
# wait = input('')
return True
else:
if not 'Attempt is not sandwiched' in test[attemptNum]['reasoning']:
test[attemptNum]['reasoning'] += ' - Attempt is not sandwiched.'
return False
def isFirstSandwich(test, attemptNum):
if not isSandwiched(test, attemptNum): return False
x = attemptNum - 1
while x > 0:
if test[x]['set'] != test[attemptNum]['set']: return False
if isSandwiched(test, x): return False
# if test[x]['sandwiched']: return False
if (test[x]['ambiguous'] == False and
test[x]['perservative'] == True and
test[x]['currentPerservativeTo'] == test[attemptNum]['currentPerservativeTo']
):
test[attemptNum]['firstSandwich'] = True
test[attemptNum]['perservative'] = True
test[attemptNum]['reasoning'] += ' - Attempt is a first sandwich, matching 2a and 2b. Marking perservative.'
return True
x-=1
def isChainedSandwich(test, attemptNum):
if not isSandwiched(test, attemptNum): return False
if isFirstSandwich(test, attemptNum): return False
x = attemptNum - 1
while x > 0:
if test[x]['set'] != test[attemptNum]['set']: return False
# Check to see if we found the bread
if (test[x]['ambiguous'] == False and
test[x]['perservative'] == True and
test[x]['currentPerservativeTo'] == test[attemptNum]['currentPerservativeTo']
):
break
# If any of the preceeding attempts before the "bread" aren't also
# sandwiches, then 2c doesn't apply
if not isSandwiched(test, x): return False
x -= 1
# Next we check forwards.
y = attemptNum + 1
while y < len(test):
if test[y]['set'] != test[attemptNum]['set']: return False
# Check to see if we found the bread
if (test[y]['ambiguous'] == False and
test[y]['perservative'] == True and
test[y]['currentPerservativeTo'] == test[attemptNum]['currentPerservativeTo']
):
break
# If any of the preceeding attempts before the "bread" aren't also
# sandwiches, then 2c doesn't apply
if not isSandwiched(test, y): return False
y += 1
# print('Holy shit, we found one on attempt ', attemptNum)
# print (test[attemptNum])
return True
def checkChainedSandwich(test, a):
if isChainedSandwich(test, a):
test[a]['2c'] = True
test[a]['reasoning'] += ' - Attempt is chain sandwich perservative per 2c'
test[a]['perservative'] = True
return True
else:
test[a]['2c'] = False
if (
test[a]['sandwiched'] and
'NOT perservative per 2c' not in test[a]['reasoning']
): test[a]['reasoning'] += ' - Sandwiched attempt is NOT perservative per 2c'
return False
def checkSelfPerserveration(test, a):
# 1. The client must make 3 unambiguous errors to a sorting principle
# which is neither correct nor currently perserverative.
#
# 2. All responses between the first and third unambiguous response must
# match this sorting principle.
#
# 3. The new principle becomes active to register perserverations only
# after the second unambiguous error.
# First, we check to see if this is an unambiguous error
# to something other than the current principle
matches = getMatches(test, a)
if len(matches) != 1: return False # One match for an unambiguous result
if test[a]['currentPerservativeTo'] in matches: return False
if isCorrect(test, a): return False # Make sure it's an error
match = matches[0]
# If we get here, then we know the attempt is a candidate for the first indicator.
# Let's look ahead for more indicators!
x = a
unambiguousMatches = [x,] # We need 3 to confirm self-perserveration
# print{'Added first', x)
while x < len(test)-1:
x+=1
# Make sure the potential principle is matched in all subsequent attempts
# This covers the intermediate results
tempMatches = getMatches(test, x)
if not match in tempMatches: return False
# Now we look for the last two unambiguous errors to the new, not currently
# perserverative principle
if len(tempMatches) != 1: continue # Ensure it is an unambiguous result
if isCorrect(test, x): continue # Make sure it's an error
if test[x]['currentPerservativeTo'] in tempMatches: continue # Not currently pers
# It's a match!
unambiguousMatches.append(test[x]['attemptNum'])
if len(unambiguousMatches) == 3: break
if len(unambiguousMatches) != 3: return False
# print(str(test[0]['classNum']) + ', ' + str(test[0]['testNum'])+ ', ' + str(test[0]['subject']), '\n', unambiguousMatches, '\n')
test[unambiguousMatches[0]]['rule_3_1'] = True
test[unambiguousMatches[0]]['reasoning'] += ' - Rule 3: First unambiguous self-perserveration'
test[unambiguousMatches[1]]['rule_3_2'] = True
test[unambiguousMatches[1]]['reasoning'] += ' - Rule 3: Second unambiguous self-perserveration after attempt ' + str(test[unambiguousMatches[0]]['attemptNum'])
test[unambiguousMatches[2]]['rule_3_3'] = True
test[unambiguousMatches[2]]['reasoning'] += ' - Rule 3: Final unambiguous self-perserveration'
# Set all responses from the first untill the second response (not
# including the second response) as unscorable
x = unambiguousMatches[0]
while x < unambiguousMatches[1]:
test[x]['currentPerservativeTo'] = None
test[x]['reasoning'] += ' - Rule 3: Set to unscorable for first to second responses'
# print (test[x])
x+=1
#################################
# Principle is not set for future attempts. Maybe we also need to have the category changer run after this?
#####################################
# Set all the rest, up to the next set change, to the new principle
x = unambiguousMatches[1]
while (x < len(test) and test[x]['set'] == test[unambiguousMatches[1]]['set']):
test[x]['currentPerservativeTo'] = match
test[x]['reasoning'] += ' - Rule 3: Principle set to ' + match + ' due to self-perserveration'
# print("Test ", x, " principle set to ", match)
x+=1
def analyzeTest(fullpath, testNum, classNum):
test = []
# Open the file and read it into memory
with open (fullpath) as f: lines = f.readlines()
# Iterate through the test and format it for analysis. Skip the headers.
lines.pop(0)
lineCount = 0
for line in lines:
# Split the test report into an array of Dicts
# Added some error handling because the text files aren't always clean.
try:
attempt = line.split()
test.append({
'file' : os.path.basename(fullpath),
'attemptNum' : lineCount,
'subject' : int(attempt[SUBJECT]),
'set' : int(attempt[SET]),
'match' : attempt[MATCH],
'stimulus' : splitStim(attempt[STIMULUS]),
'response' : splitStim(attempt[RESPONSE]),
'testNum' : testNum,
'2c' : '',
'classNum' : classNum,
'currentPerservativeTo' : None, # Stores the currently active Pres-To Principle
'reasoning' : '', # Contains plaintext reasoning to help verify results
# The following are all boolean
'correct' : False,
'perservative' : False,
'ambiguous' : False,
'sandwiched' : False,
'firstSandwich' : False,
'set1PrincipleEstablished' : False, # Will change to true if the principle changed this attempt in the first set
'rule_3_1' : False, # These are to show matches for Heaton's rule 3
'rule_3_2' : False, # for self perserverations
'rule_3_3' : False,
})
except: print ('There was an error reading line ' + str(lineCount) + ' in file: \n' + fullpath)
lineCount += 1
# First pass: Analyze the data with a set of rules
for attempt in test:
# 1. Set the principle the same as last attempt.The current
# principle will be the same as the last attempt, unless
# it changes in a subsequent rule.
continuePreviousPresTo(test, attempt['attemptNum'])
# 2. Check if we just moved into a new set, and adjust the principle accordingly
checkNewSet(test, attempt['attemptNum'])
# 3. Check if the attempt was an error
checkAnswer(test, attempt['attemptNum'])
# 4. If Principle has not been determined (first set) then the first unambiguous
# incorrect answer determines the first-set's Principle
checkFirstSetPers(test, attempt['attemptNum'])
for attempt in test:
# 5. Heaton's rule 3: Find self-made perserverations
checkSelfPerserveration(test, attempt['attemptNum'])
for attempt in test:
# 6. If this was an unambiguous error matching the perservative-to rule, AND
# the rule was not established this attempt (on the first set).
# We have to know this for every rule before we can go on to the more complicated
# tests, so we finish the loop this way and then loop again
checkUnambiguousPerserveration(test, attempt['attemptNum'])
for attempt in test:
# 7. Now we start looking for ambiguous perserverations. Here we check the
# "sandwich rule."
isSandwiched(test, attempt['attemptNum'])
for attempt in test:
# 8. Check if the sandwiched ambiguous answers are the first ones
isFirstSandwich(test, attempt['attemptNum'])
for attempt in test:
# 9. Check rule 2c for chained perserverations
checkChainedSandwich(test, attempt['attemptNum'])
# Return the fully populated and analyzed test object
# printTest(test)
saveResults(test)
return test
# Iterate through each file in each folder in the PATH variable
allTests = []
for path, dirs, files in os.walk(PATH):
if path == PATH: continue # Skip the root
for filename in files:
if '.py' in filename: continue # Skip any python files
if 'ANALYSIS' in filename: continue # Skip any generated analysis files
# if not 'iqdat' in filename: continue
fullpath = os.path.join(path, filename) # Get the filename for each file
# Get the test number and class number from the directory names
p = path.split('\\')
classNum = p[len(p)-1]
testNum = p[len(p)-2]
allTests.append(analyzeTest(fullpath, testNum, classNum))
for t in allTests:
totalCorrect = 0
totalError = 0
totalSetsAttempted = 0
totalNonPerserverative = 0
totalNonPerserverativeErrors = 0
totalPerserverative = 0
totalTrials = 0
for a in t:
if a['correct']: totalCorrect +=1
if not a['correct']: totalError +=1
totalSetsAttempted = a['set'] # Will end up being the final set
if a['perservative']: totalPerserverative +=1
if not a['perservative']: totalNonPerserverative +=1
if (not a['perservative'] and not a['correct']): totalNonPerserverativeErrors +=1
totalTrials+=1
with open('SUMMARY_' + t[0]['testNum'] + '_' + t[0]['classNum'] + '.csv', 'a', newline='') as csvfile:
cw = csv.writer(csvfile)
if os.stat('SUMMARY_' + t[0]['testNum'] + '_' + t[0]['classNum'] + '.csv').st_size == 0:
cw.writerow(['Class', 'Subject', 'Sets Attempted', 'Correct', 'Errors', 'Non-Perserverative', 'Non-Perserverative Errors', 'Perserverative', 'Trials', '% Perserverative'])
cw.writerow([
t[0]['classNum'],
t[0]['subject'],
totalSetsAttempted,
totalCorrect,
totalError,
totalNonPerserverative,
totalNonPerserverativeErrors,
totalPerserverative,
totalTrials,
str(round((totalPerserverative / totalTrials)*100, 2)) + '%',
])