Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. from os import listdir
  2.  
  3. dataFile = open('all_results.csv', 'w')
  4. # Here, we're creating a program to combine all participant data into a .csv file from the Battleship experiment
  5. # Note, data files must be stored in the current directory in order for their being in the created .csv file
  6.  
  7. allData = sorted(listdir('results')) # Such that the same order of data is kept throughout .csv regardless of additions
  8. for file in allData:
  9. targetHit = 0
  10. targetNearMiss = 0
  11. targetFullMiss = 0
  12. happiness = []
  13. happinessHit = 0
  14. happinessNearMiss = 0
  15. happinessFullMiss = 0
  16. willingnessHit = 0
  17. willingnessNearMiss = 0
  18. willingnessFullMiss = 0
  19.  
  20. datum = open('results/' + file, 'r')
  21. allLines = datum.readlines()
  22.  
  23. condition = allLines[0].strip(' ').strip('\n') # Collating condition
  24. demographics = allLines[3].title().split(',') # Dividing name, age and gender (initially coded together)
  25. name = demographics[0] # Collating name
  26. age = demographics[1] # Collating age
  27. if demographics[2] == 'Male\n': # Coding gender such that later statistical analysis is easier (i.e. ratio)
  28. gender = '1' # Males coded as '1' and females coded as '2' (collated in gender)
  29. else:
  30. gender = '2'
  31.  
  32. trialOutcome = allLines[5:] # Collating the proportion of hits, near misses and full misses
  33. for trial in trialOutcome: # Analysing each trial per participant (varying no. of trials per participant)
  34. ratings = trial.replace(',', ' ')
  35. ratings = ratings[-7:] # Max. characters possible at the end for max. happiness and willingness (100)
  36. happinessRatings = ratings.split(' ')
  37. del (happinessRatings[0])
  38. if 'hit' in trial:
  39. targetHit += 1 # Collecting the number of times the target is hit etc.
  40. willingnessHit += int(ratings[-3:]) # Summing willingness ratings per participant to calculate average
  41. if len(happinessRatings) == 3: # Length here indicative of no. of digits in happiness rating
  42. happinessHit += int(happinessRatings[1])
  43. happiness.append(happinessRatings[1])
  44. else:
  45. happinessHit += int(happinessRatings[0])
  46. happiness.append(happinessRatings[0])
  47. elif 'nearMiss' in trial:
  48. targetNearMiss += 1
  49. willingnessNearMiss += int(ratings[-3:])
  50. if len(happinessRatings) == 3:
  51. happinessNearMiss += int(happinessRatings[1])
  52. happiness.append(happinessRatings[1])
  53. else:
  54. happinessNearMiss += int(happinessRatings[0])
  55. happiness.append(happinessRatings[0])
  56. elif 'fullMiss' in trial:
  57. targetFullMiss += 1
  58. willingnessFullMiss += int(ratings[-3:])
  59. if len(happinessRatings) == 3:
  60. happinessFullMiss += int(happinessRatings[1])
  61. happiness.append(happinessRatings[1])
  62. else:
  63. happinessFullMiss += int(happinessRatings[0])
  64. happiness.append(happinessRatings[0])
  65. maximumHappinessTrial = happiness.index(max(happiness)) + 1 # +1 so that the first trial will appear as '1'
  66. maximumHappiness = max(happiness).strip('\n') # Strip so items appear in the same row as associated items
  67. minimumHappinessTrial = happiness.index(min(happiness)) + 1
  68. minimumHappiness = min(happiness).strip('\n')
  69. total = targetHit + targetNearMiss + targetFullMiss # Each to be divided by total; expressed as decimal
  70.  
  71. dataFile.write(f'{condition}, {name}, {age}, {gender}, {targetHit / total:.2f}, {targetNearMiss / total:.2f}, '
  72. f'{targetFullMiss / total:.2f}, {happinessHit / targetHit:.2f}, {willingnessHit / targetHit:.2f}, '
  73. f'{happinessNearMiss / targetNearMiss:.2f}, {willingnessNearMiss / targetNearMiss:.2f}, '
  74. f'{happinessFullMiss / targetFullMiss:.2f}, {willingnessFullMiss / targetFullMiss:.2f}, '
  75. f'{maximumHappiness}, {maximumHappinessTrial}, {minimumHappiness}, {minimumHappinessTrial}\n')
  76.  
  77. dataFile.close()
  78.  
  79. # Here, we're creating a .csv file that organises the data files in respect of the date participation occurred
  80. newDataFile = open('by_date_results.csv', 'w')
  81. allData = sorted(listdir('results'))
  82. participationDate = {}
  83. for file in allData:
  84. luck = 0
  85. mixed = 0
  86. skill = 0
  87.  
  88. datum = open('results/' + file, 'r')
  89. allLines = datum.readlines()
  90.  
  91. day = allLines[1]
  92. day = day[6:14]
  93. date = day[:2] + '-' + day[2:4] + '-' + day[4:] # Formatting such that day/month/year are easily distinguished
  94. participationDate[day] = 0
  95.  
  96. if '04062013' in day and 'luck' in allLines[0]:
  97. luck += 1
  98. elif '04062013' in day and 'mixed' in allLines[0]:
  99. mixed += 1
  100. elif '04062013' in day and 'skill' in allLines[0]:
  101. skill += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement