-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_manager.py
211 lines (171 loc) · 7.17 KB
/
data_manager.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import json
import csv
from datetime import datetime
import shutil
import logging
from pathlib import Path
class DataManager:
"""Handles all data saving and organization for the gaze estimation experiment."""
def __init__(self, base_directory=None, app_version='1.0.0'):
"""
Initialize the data manager.
Args:
base_directory: Optional path to base directory
app_version: Version string for the application
"""
# Store app version
self.app_version = app_version
# Set up base directory
if base_directory is None:
self.base_directory = self._create_base_directory()
else:
self.base_directory = Path(base_directory)
# Set up logging
self._setup_logging()
def _setup_logging(self):
"""Configure logging for the data manager."""
log_file = self.base_directory / 'experiment.log'
logging.basicConfig(
filename=str(log_file),
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
def _create_base_directory(self):
"""Create the base directory with current date."""
current_date = datetime.now().strftime("%Y%m%d")
base_dir = Path(f"{current_date}_GazeEstimationExperiment")
base_dir.mkdir(parents=True, exist_ok=True)
return base_dir
def create_subject_directory(self, subject_id):
"""Create directory structure for a new subject."""
try:
# Create subject directory with padded ID
subject_dir = self.base_directory / f"S{str(subject_id).zfill(3)}"
subject_dir.mkdir(exist_ok=True)
logging.info(f"Created directory structure for subject {subject_id}")
return subject_dir
except Exception as e:
logging.error(f"Error creating subject directory: {str(e)}")
raise
def create_trial_directory(self, subject_dir):
"""Create a new trial directory with incrementing trial number."""
try:
# Find existing trial directories
existing_trials = list(subject_dir.glob("Trial_*"))
trial_num = len(existing_trials) + 1
# Create new trial directory
trial_dir = subject_dir / f"Trial_{str(trial_num).zfill(3)}"
trial_dir.mkdir(exist_ok=True)
logging.info(f"Created trial directory: {trial_dir}")
return trial_dir
except Exception as e:
logging.error(f"Error creating trial directory: {str(e)}")
raise
def save_metadata(self, subject_dir, metadata):
"""Save subject metadata to JSON file."""
try:
metadata_file = subject_dir / "Metadata.json"
with open(metadata_file, 'w') as f:
json.dump(metadata, f, indent=2)
logging.info(f"Saved metadata to {metadata_file}")
except Exception as e:
logging.error(f"Error saving metadata: {str(e)}")
raise
def save_trial_config(self, trial_dir, config):
"""Save trial configuration including setup parameters."""
try:
config_file = trial_dir / "setup_config.json"
with open(config_file, 'w') as f:
json.dump(config, f, indent=2)
logging.info(f"Saved trial configuration to {config_file}")
except Exception as e:
logging.error(f"Error saving trial configuration: {str(e)}")
raise
def save_landmark_data(self, trial_dir, landmarks_data, column_names):
"""Save landmarks data to CSV file."""
try:
landmarks_file = trial_dir / "landmark_data.csv"
with open(landmarks_file, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(column_names)
writer.writerows(landmarks_data)
logging.info(f"Saved landmarks data to {landmarks_file}")
except Exception as e:
logging.error(f"Error saving landmarks data: {str(e)}")
raise
def save_experiment_data(self, trial_dir, data):
"""Save experiment-specific data to CSV file."""
try:
experiment_file = trial_dir / "experiment_data.csv"
with open(experiment_file, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(data['headers'])
writer.writerows(data['rows'])
logging.info(f"Saved experiment data to {experiment_file}")
except Exception as e:
logging.error(f"Error saving experiment data: {str(e)}")
raise
def get_trial_count(self, subject_dir):
"""Get the number of existing trials for a subject."""
try:
return len(list(subject_dir.glob("Trial_*")))
except Exception as e:
logging.error(f"Error counting trials: {str(e)}")
return 0
def backup_data(self, backup_path):
"""Create a backup of all experiment data."""
try:
backup_dir = Path(backup_path)
backup_dir.mkdir(parents=True, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
backup_name = f"GazeEstimation_Backup_{timestamp}"
backup_path = backup_dir / backup_name
shutil.copytree(self.base_directory, backup_path)
logging.info(f"Created backup at {backup_path}")
return backup_path
except Exception as e:
logging.error(f"Error creating backup: {str(e)}")
raise
def main():
"""Test the DataManager functionality."""
dm = DataManager()
try:
# Test subject creation
subject_dir = dm.create_subject_directory("001")
# Test metadata saving
test_metadata = {
"subject": {
"id": "001",
"age": 25,
"gender": "Female"
},
"session": {
"date": datetime.now().strftime("%Y-%m-%d"),
"experimenter": "Test"
}
}
dm.save_metadata(subject_dir, test_metadata)
# Test trial creation and data saving
trial_dir = dm.create_trial_directory(subject_dir)
# Test trial config saving
test_config = {
"trial_id": "001",
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"setup": {
"yaw": 15,
"pitch": -15,
"distance": 60
},
"conditions": {
"dot_display_time": 2000,
"rest_time": 1000,
"grid_size": 3,
"dot_radius": 15
}
}
dm.save_trial_config(trial_dir, test_config)
print("Test completed successfully!")
except Exception as e:
print(f"Test failed: {str(e)}")
if __name__ == "__main__":
main()