Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. from os import listdir
  2.  
  3. paths = listdir('results')
  4. battleshipFile = open('BattleshipData.csv', 'w')
  5. resultsPerDate = open('BattleshipPerDate.csv', 'w')
  6. ipAddresses = {}
  7. participantsPerDate = {}
  8.  
  9. for path in paths:
  10. file = open('results/' + path, 'r')
  11.  
  12. contentLines = file.readlines()
  13. # format condition
  14. condition = contentLines[0].strip(' ').strip('\n').lower()
  15.  
  16. print(condition)
  17. # format date
  18. dateString = contentLines[1]
  19. date = dateString.partition(':')[2].strip(' ')
  20. formattedDate = date[0:2] + '-' + date[2:4] + '-' + date[4:8]
  21.  
  22. print(formattedDate)
  23. # format ip address
  24. ipString = contentLines[2]
  25. ip = ipString.partition(':')[2].strip(' ').strip('\n')
  26.  
  27. print(ip)
  28. # name-age-gender
  29. participantList = contentLines[3].split(',')
  30. name = participantList[0]
  31. age = participantList[1]
  32. genderString = participantList[2].lower().strip('\n')
  33.  
  34. if ip in ipAddresses:
  35. print(f'{name} with ip {ip} has already been used by {ipAddresses[ip]}')
  36. continue
  37. else:
  38. ipAddresses[ip] = name
  39.  
  40. # gender to integer
  41. gender = 0
  42. if genderString == 'male':
  43. gender = 1
  44. else:
  45. gender = 2
  46.  
  47. print('Not protected by GDPR')
  48. print(f'{name},{age},{genderString}')
  49.  
  50. totalTrials = 0
  51. totalNearMiss = 0
  52. totalFullMiss = 0
  53. totalHit = 0
  54. happinessNearMiss = 0
  55. happinessFullMiss = 0
  56. happinessHit = 0
  57. willingnessNearMiss = 0
  58. willingnessFullMiss = 0
  59. willingnessHit = 0
  60.  
  61. minHappiness = 99
  62. minHappinessTrial = 0
  63. maxHappiness = 0
  64. maxHappinessTrial = 0
  65.  
  66. for trialLine in contentLines:
  67. trialList = trialLine.split(',')
  68. trialIndication = trialList[0].strip(' ')
  69. # if the lion is a trial
  70. if trialIndication == 'trial':
  71. totalTrials += 1
  72. result = trialList[4].strip(' ')
  73. happiness = int(trialList[7].strip(' '))
  74. willingness = int(trialList[8].strip(' '))
  75. # stats hits / full misses / near misses
  76. if result == 'nearMiss':
  77. totalNearMiss += 1
  78. happinessNearMiss += happiness
  79. willingnessNearMiss += willingness
  80. elif result == 'fullMiss':
  81. totalFullMiss += 1
  82. happinessFullMiss += happiness
  83. willingnessFullMiss += willingness
  84. elif result == 'hit':
  85. totalHit += 1
  86. happinessHit += happiness
  87. willingnessHit += willingness
  88.  
  89. # min happiness
  90. if minHappiness > happiness:
  91. minHappiness = happiness
  92. minHappinessTrial = totalTrials
  93. # max happiness
  94. if maxHappiness < happiness:
  95. maxHappiness = happiness
  96. maxHappinessTrial = totalTrials
  97.  
  98. print(f'Proportion of hits/near misses/full misses over to trials: {totalHit / totalTrials:.2f}/{totalNearMiss / totalTrials:.2f}/{totalFullMiss / totalTrials:.2f}')
  99. print(f'Mean happiness per hit/near miss/full miss: {happinessHit / totalHit:.2f}/{happinessNearMiss / totalNearMiss:.2f}/{happinessFullMiss / totalFullMiss:.2f}')
  100. print(f'Mean willingness per hit/near miss/full miss: {willingnessHit / totalHit:.2f}/{willingnessNearMiss / totalNearMiss:.2f}/{willingnessFullMiss / totalFullMiss:.2f}')
  101. print(f'Maximum happiness levels is: {maxHappiness} on the trial {maxHappinessTrial}')
  102. print(f'Minimum happiness levels is: {minHappiness} on the trial {minHappinessTrial}')
  103.  
  104. battleshipFile.write(f'{condition},{name},{age},{gender},{totalHit / totalTrials:.2f},{totalNearMiss / totalTrials:.2f},{totalFullMiss / totalTrials:.2f},')
  105. battleshipFile.write(f'{happinessHit / totalHit:.2f},{happinessNearMiss / totalNearMiss:.2f},{happinessFullMiss / totalFullMiss:.2f},')
  106. battleshipFile.write(f'{willingnessHit / totalHit:.2f},{willingnessNearMiss / totalNearMiss:.2f},{willingnessFullMiss / totalFullMiss:.2f},')
  107. battleshipFile.write(f'{maxHappiness},{maxHappinessTrial},{minHappiness},{minHappinessTrial}\n')
  108.  
  109. # fill in the information per date
  110. # number of participants, participants skill, participants luck, participants mixed, number of trials
  111. if formattedDate in participantsPerDate:
  112. participants = participantsPerDate[formattedDate]
  113. participants[0] += 1
  114. participants[4] += totalTrials
  115.  
  116. if condition == 'skill':
  117. participants[1] += 1
  118. elif condition == 'luck':
  119. participants[2] += 1
  120. elif condition == 'mixed':
  121. participants[3] += 1
  122.  
  123. else: # the case for the first participant on that date
  124. participants = [1, 0, 0, 0, totalTrials]
  125.  
  126. if condition == 'skill':
  127. participants[1] = 1
  128. elif condition == 'luck':
  129. participants[2] = 1
  130. elif condition == 'mixed':
  131. participants[3] = 1
  132. participantsPerDate[formattedDate] = participants
  133.  
  134. battleshipFile.close()
  135.  
  136. for date in participantsPerDate:
  137. participants = participantsPerDate[date]
  138. resultsPerDate.write(f'{date},{participants[0]},{participants[1]},{participants[2]},{participants[3]},{participants[4]/participants[0]:.2f}\n')
  139.  
  140. resultsPerDate.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement