Created
April 26, 2024 12:23
-
-
Save zeyus/d54f91344a4a0ed974937cf3c2dda43d to your computer and use it in GitHub Desktop.
Read in the collected data from the drawing experiment, display information about completed trials and save SVGs and PNGs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| import sys | |
| # pip install cairosvg | |
| from cairosvg import svg2png | |
| maxInt = sys.maxsize | |
| while True: | |
| # decrease the maxInt value by factor 10 | |
| # as long as the OverflowError occurs. | |
| try: | |
| csv.field_size_limit(maxInt) | |
| break | |
| except OverflowError: | |
| maxInt = int(maxInt/10) | |
| csv_file = "data/animalfeatures_2024-04-26.csv" | |
| svg_dir = "data/svgs_2024-04-26" | |
| png_dir = "data/pngs_2024-04-26" | |
| # get number of rows that the "svg" column != "N/A" per "prolific_id" | |
| drawings_per_participant = {} | |
| with open(csv_file, mode='r') as file: | |
| csv_reader = csv.DictReader(file) | |
| for row in csv_reader: | |
| if row["svg"] != "N/A" and row["svg"] != "": | |
| if row["prolific_id"] in drawings_per_participant: | |
| drawings_per_participant[row["prolific_id"]] += 1 | |
| else: | |
| drawings_per_participant[row["prolific_id"]] = 1 | |
| base_filename = f"{row['prolific_id']}_{row['condition']}_{row['trial']}_{row['animal']}_{row['action']}" | |
| # save contents of the svg column to a svg file | |
| with open(f"{svg_dir}/{base_filename}.svg", "w") as svg_file: | |
| svg_file.write(row["svg"]) | |
| # save contents of the svg column to a png file | |
| svg2png(bytestring=row["svg"], write_to=f"{png_dir}/{base_filename}.png") | |
| print("Number of participants with a drawing: ", len(drawings_per_participant)) | |
| # print how many have 16 completed drawings | |
| count = 0 | |
| print("Number of drawings with content by participant:") | |
| for key, value in drawings_per_participant.items(): | |
| print(key, ":", value) | |
| if value == 16: | |
| count += 1 | |
| # number of participants with complete drawings | |
| print("Number of participants with 16 drawings: ", count) |
zeyus
commented
Apr 26, 2024
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment