If this template helps then use it. If not then just delete and start from scratch.
OS (e.g. Win10): Macbook Pro - M1 Pro
PsychoPy version (e.g. 2024.2.4 Py 3.8): v2023.2.3
Standard Standalone Installation? (y/n) y
Do you want it to also run online? (y/n) n
What are you trying to achieve?
I’m looking to create some plots and data structures using matplotlib and pandas once the experiment is complete.
What did you try to make it work?
Currently, the code works. However, it requires me to specify the subject ID again, since I am placing this code in a code component at “End Experiment”. Is there a way to use the expInfo[‘subject_ID’] or something, after the experiment has ended?
Link to the most relevant existing thread you have found:
What specifically went wrong when you tried that?:
#import os
import re
import ast
import time
#import numpy as np
import pandas as pd
from scipy.stats import sem
from datetime import datetime
import matplotlib.pyplot as plt
time.sleep(10)
subID = 'S02'
data_path = f'data/{subID}'
save_path = data_path
data_files = [f for f in os.listdir(data_path) if f.startswith(subID) and f.endswith('.csv')]
pattern = re.compile(r"(S\d+)_([0-9]+)_(\d{4}-\d{2}-\d{2})_(\d{2})h(\d{2})\.(\d{2})\.(\d{3})")
def parse_file(fname):
m = pattern.match(fname)
subj, block, date, H, M, S, ms = m.groups()
dt = datetime.strptime(f"{date} {H}:{M}:{S}.{ms}", "%Y-%m-%d %H:%M:%S.%f")
return subj, block, dt
parsed = [(f, *parse_file(f)) for f in data_files]
parsed_sorted = sorted(parsed, key=lambda x: x[3])
newest_data = parsed_sorted[-1]
filename = newest_data[0]
subject = newest_data[1]
session = newest_data[2]
dt = newest_data[3]
df = pd.read_csv(f"{data_path}/{filename}")