-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_funcs.py
More file actions
69 lines (54 loc) · 2.03 KB
/
run_funcs.py
File metadata and controls
69 lines (54 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from scipy.stats import norm
import os
import random
def compute_dprime(hits, false_alarms, misses, correct_rejections):
"""
Compute d-prime based on hits/misses/false alarms/correct rejections.
D-prime = Z(HR) - Z(FAR)
Where:
HR = hits / (hits + misses)
FAR = false_alarms / (false_alarms + correct_rejections)
"""
total_signal = hits + misses
total_noise = false_alarms + correct_rejections
if total_signal == 0 or total_noise == 0:
return 0.0
pHit = (hits + 0.5) / (total_signal + 1.0)
pFA = (false_alarms + 0.5) / (total_noise + 1.0)
# pHit = hits / total_signal
# pFA = false_alarms / total_noise
# Convert to Z scores, no error checking
zHit = norm.ppf(pHit)
zFA = norm.ppf(pFA)
dprime = zHit - zFA
# crit = (zHit + zFA) / -2
# crit_prime = crit / dprime
return dprime
def collect_files(data_path, channel_types, file_extension, mode):
all_files = []
for ch_type in channel_types:
ch_dir = os.path.join(data_path, ch_type)
if os.path.isdir(ch_dir):
files_in_ch_dir = [
f for f in os.listdir(ch_dir) if f.endswith(file_extension)
]
all_files.extend([(ch_type, file) for file in files_in_ch_dir])
else:
print(f"Warning: {ch_dir} not found or is not a directory.")
if len(all_files) == 0:
print(f"No {mode} files found for the specified channel types.")
return None
return all_files
def process_trial_files(all_files, n_trials, mode, data_path):
trials_list = []
chosen_files = random.sample(all_files, min(n_trials, len(all_files)))
for trial_idx, (ch_type, trial_file) in enumerate(chosen_files, start=1):
full_path = os.path.join(data_path, ch_type, trial_file)
trials_list.append({
"Trial": trial_idx,
"trial_file": trial_file,
"ch_type": ch_type,
"trial_path": full_path,
"mode": mode
})
return trials_list