Advertisement
Guest User

Untitled

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