Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from os import listdir
- dataFile = open('all_results.csv', 'w')
- # Here, we're creating a program to combine all participant data into a .csv file from the Battleship experiment
- # Note, data files must be stored in the current directory in order for their being in the created .csv file
- allData = sorted(listdir('results')) # Such that the same order of data is kept throughout .csv regardless of additions
- for file in allData:
- condition = '' # These (empty) variables will (1) store data for each participant in respect of their name;
- name = '' # (2) appear in this order as columns in the .csv file. Each row will be one participant
- age = ''
- gender = ''
- targetHit = 0
- targetNearMiss = 0
- targetFullMiss = 0
- happiness = []
- happinessHit = 0
- happinessNearMiss = 0
- happinessFullMiss = 0
- willingnessHit = 0
- willingnessNearMiss = 0
- willingnessFullMiss = 0
- datum = open('results/' + file, 'r')
- allLines = datum.readlines()
- condition = allLines[0].strip(' ').strip('\n') # Collating condition
- demographics = allLines[3].title().split(',')
- name = demographics[0] # Collating name
- age = demographics[1] # Collating age
- if demographics[2] == 'Male\n': # Coding gender such that later statistical analysis is easier (i.e. ratio)
- gender = '1' # Males coded as '1' and females coded as '2' (collated in gender)
- else:
- gender = '2'
- trialOutcome = allLines[5:] # Collating the proportion of hits, near misses and full misses
- for trial in trialOutcome: # Analysing each trial per participant (varying no. of trials per participant)
- ratings = trial.replace(',', ' ')
- ratings = ratings[-7:] # Max. characters possible at the end for max. happiness and willingness (100)
- happinessRatings = ratings.split(' ')
- del (happinessRatings[0])
- if 'hit' in trial:
- targetHit += 1 # Collecting the number of times the target is hit etc.
- willingnessHit += int(ratings[-3:]) # Summing willingness ratings per participant to calculate average
- if len(happinessRatings) == 3: # Length here indicative of no. of digits in happiness rating
- happinessHit += int(happinessRatings[1])
- happiness.append(happinessRatings[1])
- else:
- happinessHit += int(happinessRatings[0])
- happiness.append(happinessRatings[0])
- elif 'nearMiss' in trial:
- targetNearMiss += 1
- willingnessNearMiss += int(ratings[-3:])
- if len(happinessRatings) == 3:
- happinessNearMiss += int(happinessRatings[1])
- happiness.append(happinessRatings[1])
- else:
- happinessNearMiss += int(happinessRatings[0])
- happiness.append(happinessRatings[0])
- elif 'fullMiss' in trial:
- targetFullMiss += 1
- willingnessFullMiss += int(ratings[-3:])
- if len(happinessRatings) == 3:
- happinessFullMiss += int(happinessRatings[1])
- happiness.append(happinessRatings[1])
- else:
- happinessFullMiss += int(happinessRatings[0])
- happiness.append(happinessRatings[0])
- maximumHappinessTrial = happiness.index(max(happiness)) + 1 # +1 so that the first trial will appear as '1'
- maximumHappiness = max(happiness).strip('\n') # Strip so items appear in the same row as associated items
- minimumHappinessTrial = happiness.index(min(happiness)) + 1
- minimumHappiness = min(happiness).strip('\n')
- total = targetHit + targetNearMiss + targetFullMiss # Each to be divided by total; expressed as decimal
- dataFile.write(f'{condition}, {name}, {age}, {gender}, {targetHit / total:.2f}, {targetNearMiss / total:.2f}, '
- f'{targetFullMiss / total:.2f}, {happinessHit / targetHit:.2f}, {willingnessHit / targetHit:.2f}, '
- f'{happinessNearMiss / targetNearMiss:.2f}, {willingnessNearMiss / targetNearMiss:.2f}, '
- f'{happinessFullMiss / targetFullMiss:.2f}, {willingnessFullMiss / targetFullMiss:.2f}, '
- f'{maximumHappiness}, {maximumHappinessTrial}, {minimumHappiness}, {minimumHappinessTrial}\n')
- dataFile.close()
- # Here, we're creating a .csv file that organises the data files in respect of the date participation occurred
- newDataFile = open('by_date_results.csv', 'w')
- allData = sorted(listdir())
- del (allData[0:3])
- participationDate = {}
- for file in allData:
- date = '' # Collating individual date each participant performed experiment on
- luck = 0
- mixed = 0
- skill = 0
- if "results_" in file:
- datum = open(file, 'r')
- allLines = datum.readlines()
- day = allLines[1]
- day = day[6:14]
- date = day[:2] + '-' + day[2:4] + '-' + day[4:] # Formatting such that day/month/year are easily distinguished
- participationDate[day] = 0
- if '04062013' in day and 'luck' in allLines[0]:
- luck += 1
- elif '04062013' in day and 'mixed' in allLines[0]:
- mixed += 1
- elif '04062013' in day and 'skill' in allLines[0]:
- skill += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement