Skip to content

Commit dbd6a5f

Browse files
authored
converted experiment configuration files from .pcx to .json (#59)
1 parent 5d046d8 commit dbd6a5f

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

gui/GUI_main.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import traceback
55
import logging
66

7+
from pathlib import Path
78
from serial.tools import list_ports
89
from pyqtgraph.Qt import QtGui, QtCore, QtWidgets
910

@@ -116,6 +117,7 @@ def __init__(self,app):
116117
shortcuts_action.setIcon(QtGui.QIcon("gui/icons/keyboard.svg"))
117118
help_menu.addAction(shortcuts_action)
118119

120+
self.pcx2json()
119121
self.show()
120122

121123
def go_to_data(self):
@@ -138,13 +140,19 @@ def get_task_file_list(self):
138140
subdir_1/subdir_2/task_file_name.py'''
139141
task_files = []
140142
# this function gets called every second. Normally we would use get_setting("folder","tasks")
141-
# but there no need to constantly be rereading the user_settings.json file that isn't changing
143+
# but there is no need to constantly be rereading the user_settings.json file that isn't changing
142144
# so we use this self.task_directory variable that is only updated when a new user settting is saved
143145
for (dirpath, dirnames, filenames) in os.walk(self.task_directory):
144146
task_files += [os.path.join(dirpath, file).split(self.task_directory)[1][1:-3]
145147
for file in filenames if file.endswith('.py')]
146148
return task_files
147149

150+
def pcx2json(self):
151+
"""Converts legacy .pcx files to .json files"""
152+
exp_dir = Path(dirs['experiments'])
153+
for f in exp_dir.glob('*.pcx'):
154+
f.rename(f.with_suffix('.json'))
155+
148156
def refresh(self):
149157
'''Called regularly when framework not running.'''
150158
# Scan task folder.
@@ -153,7 +161,7 @@ def refresh(self):
153161
if self.available_tasks_changed:
154162
self.available_tasks = tasks
155163
# Scan experiments folder.
156-
experiments = [t.split('.')[0] for t in os.listdir(dirs['experiments']) if t[-4:] == '.pcx']
164+
experiments = [exp_file.stem for exp_file in Path(dirs['experiments']).glob('*.json')]
157165
self.available_experiments_changed = experiments != self.available_experiments
158166
if self.available_experiments_changed:
159167
self.available_experiments = experiments

gui/configure_experiment_tab.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def new_experiment(self, dialog=True):
185185

186186
def delete_experiment(self):
187187
'''Delete an experiment file after dialog to confirm deletion.'''
188-
exp_path = os.path.join(dirs['experiments'], self.name_text.text()+'.pcx')
188+
exp_path = os.path.join(dirs['experiments'], self.name_text.text()+'.json')
189189
if os.path.exists(exp_path):
190190
reply = QtWidgets.QMessageBox.question(
191191
self,
@@ -215,10 +215,10 @@ def save_experiment(self, from_dialog=False):
215215
setup = str(self.subjects_table.cellWidget(s,1).currentText())
216216
run = self.subjects_table.cellWidget(s,0).isChecked()
217217
d[subject] = {'setup':setup,'run':run} # add dict subject entry
218-
'''Store the current state of the experiment tab as a JSON object
219-
saved in the experiments folder as .pcx file.'''
218+
# Store the current state of the experiment tab as a JSON object
219+
# saved in the experiments folder as .json file.
220220
experiment = self.experiment_dict()
221-
file_name = self.name_text.text()+'.pcx'
221+
file_name = self.name_text.text()+'.json'
222222
exp_path = os.path.join(dirs['experiments'], file_name)
223223
if os.path.exists(exp_path) and (exp_path != self.saved_exp_path):
224224
reply = QtWidgets.QMessageBox.question(
@@ -229,7 +229,7 @@ def save_experiment(self, from_dialog=False):
229229
)
230230
if reply == QtWidgets.QMessageBox.StandardButton.No:
231231
return False
232-
with open(exp_path,'w') as exp_file:
232+
with open(exp_path,'w', encoding='utf-8') as exp_file:
233233
exp_file.write(json.dumps(experiment, sort_keys=True, indent=4))
234234
if not from_dialog:
235235
cbox_set_item(self.experiment_select, experiment['name'], insert=True)
@@ -239,9 +239,9 @@ def save_experiment(self, from_dialog=False):
239239
return True
240240

241241
def load_experiment(self, experiment_name):
242-
'''Load experiment .pcx file and set fields of experiment tab.'''
243-
exp_path = os.path.join(dirs['experiments'], experiment_name +'.pcx')
244-
with open(exp_path,'r') as exp_file:
242+
'''Load experiment .json file and set fields of experiment tab.'''
243+
exp_path = os.path.join(dirs['experiments'], experiment_name +'.json')
244+
with open(exp_path,'r', encoding='utf-8') as exp_file:
245245
experiment = json.loads(exp_file.read())
246246
self.name_text.setText(experiment['name'])
247247
if experiment['task'] in self.GUI_main.available_tasks:
@@ -335,7 +335,7 @@ def save_dialog(self):
335335
cancel is selected, True otherwise.'''
336336
if self.saved_exp_dict == self.experiment_dict():
337337
return True # Experiment has not been edited.
338-
exp_path = os.path.join(dirs['experiments'], self.name_text.text()+'.pcx')
338+
exp_path = os.path.join(dirs['experiments'], self.name_text.text()+'.json')
339339
dialog_text = None
340340
if not os.path.exists(exp_path):
341341
dialog_text = 'Experiment not saved, save experiment?'

0 commit comments

Comments
 (0)