-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.py
1492 lines (1230 loc) · 56.3 KB
/
GUI.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
from PyQt6.QtCore import *
import sys
import threading
import subprocess
import os
from pathlib import Path
import json
import platform
import socket
import time
import numpy as np
class CustomTableWidget(QTableWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def contextMenuEvent(self, event):
# Get the row and column that was clicked
row = self.rowAt(event.y())
col = self.columnAt(event.x())
# Create a context menu
menu = QMenu(self)
# Add an action to fill all arenas
fill_all_action = QAction("Fill Experiment", self)
fill_all_action.triggered.connect(
lambda checked, row=row: self.fill_experiment(row)
)
menu.addAction(fill_all_action)
# Add a separator
menu.addSeparator()
# Add an action to fill the selected arena
fill_arena_action = QAction("Fill Arena", self)
fill_arena_action.triggered.connect(
lambda checked, col=col, row=row: self.fill_arena(col, row)
)
menu.addAction(fill_arena_action)
# Show the context menu at the current mouse position
menu.exec(event.globalPos())
def fill_arena(self, col, row):
# Get the arena number from the column label
column_label = self.horizontalHeaderItem(col).text()
arena_number = int(column_label.split("_")[0][5:])
# Prompt the user to enter a value
value, ok = QInputDialog.getText(self, f"Fill Arena {arena_number}", "Value:")
if not ok:
return
# Find the columns for the given arena
for col in range(1, self.columnCount()):
column_label = self.horizontalHeaderItem(col).text()
if column_label.startswith(f"Arena{arena_number}_"):
# Set the value for the given row in the column
item = self.item(row, col)
if item:
item.setText(value)
def fill_experiment(self, row):
# Prompt the user to enter a value
value, ok = QInputDialog.getText(self, "Fill Experiment", "Value:")
if not ok:
return
# Set the value for all cells in the given row
for col in range(1, self.columnCount()):
item = self.item(row, col)
if item:
item.setText(value)
def add_empty_rows(self, row_count):
for _ in range(row_count):
row = self.rowCount()
self.insertRow(row)
# Add empty items to each cell of the new row
for col in range(self.columnCount()):
item = QTableWidgetItem("")
self.setItem(row, col, item)
def set_cell_colors(self):
# Define a list of colors to use for each arena
colors = [
"#F7DC6F",
"#82E0AA",
"#85C1E9",
"#BB8FCE",
"#F1948A",
"#6C88C4",
"#D35400",
"#FFBD00",
"#1ABC9C",
]
# Iterate over the cells of the table
for row in range(self.rowCount()):
for col in range(1, self.columnCount()):
# Get the arena number from the column label
column_label = self.horizontalHeaderItem(col).text()
arena_number = int(column_label.split("_")[0][5:])
# Get the color for this arena
color = colors[arena_number - 1]
# Set the background color of the cell
item = self.item(row, col)
if item:
item.setBackground(QColor(color))
class ExperimentWindow(QWidget):
def __init__(self, tab_widget, *args, **kwargs):
super(ExperimentWindow, self).__init__(*args, **kwargs)
"""
Experiment window
This is the main window of the application.
Attributes
----------
tab_widget : QTabWidget
The tab widget that contains the experiment window
duration_spinbox : QSpinBox
A spinbox widget for setting the duration of the recording
fps_spinbox : QSpinBox
A spinbox widget for setting the frame rate of the recording
folder_lineedit : QLineEdit
A line edit widget for entering the name of the data folder
record_button : QPushButton
A button widget for starting the recording process
HardwareTrigger_checkbox : QCheckBox
A checkbox widget for enabling hardware triggering
stop_button : QPushButton
A button widget for stopping the recording process
table_style_selector : QComboBox
A combo box widget for selecting the layout of the table
table : QTableWidget
A table widget for displaying the metadata
folder_path : Path
The path to the currently open data folder
folder_open : bool
A boolean indicating whether a data folder is currently open
recording_thread : threading.Thread
A thread for running the recording process
live_stream_process : subprocess.Popen
A process for running the live stream
info_panel : QWidget
A widget for displaying information about the currently open data folder
Methods
-------
create_table(metadata=None)
Create a new table widget using the provided metadata
update_table_style(index)
Update the table and metadata with the selected layout
detect_table_style(metadata)
Detect the layout of the table from the metadata
close_folder()
Close the currently open data folder
on_button_clicked()
Start the recording process
on_hardware_checkbox_state_changed
Enable or disable hardware triggering
record_images(folder, fps, duration)
Run the recording process with the selected settings
on_stop_button_clicked()
Terminate the recording thread
start_live_stream()
Start the live stream
stop_live_stream()
Stop the live stream
create_metadata(table=None)
Create a new metadata dictionary from the data in the table
check_data_access()
Check if the data folder can be accessed
create_data_folder(metadata=None)
Create a new data folder with the provided metadata
open_data_folder(folder_path=None)
Open an existing data folder
save_data()
Save the metadata to the currently open data folder
has_unsaved_changes()
Check if the metadata has unsaved changes
"""
# Create widgets
self.tab_widget = tab_widget
self.duration_spinbox = QSpinBox()
self.duration_spinbox.setRange(0, 10000)
self.duration_spinbox.setValue(3600)
self.fps_spinbox = QSpinBox()
self.fps_spinbox.setRange(0, 30)
self.fps_spinbox.setValue(30)
self.fps_label = QLabel()
self.experiment_type_selector = QComboBox()
self.experiment_type_selector.addItems(["Ball pushing", "Standard"])
self.experiment_type_selector.currentIndexChanged.connect(
self.on_experiment_type_changed
)
self.current_experiment_type = "Ball pushing"
self.folder_lineedit = QLineEdit()
self.record_button = QPushButton("Start Recording")
self.record_button.clicked.connect(self.on_button_clicked)
self.stop_button = QPushButton("Stop")
self.stop_button.clicked.connect(self.on_stop_button_clicked)
self.HardwareTrigger_checkbox = QCheckBox("Hardware Trigger")
self.HardwareTrigger_checkbox.stateChanged.connect(
self.on_hardware_checkbox_state_changed
)
self.table_style_selector = QComboBox()
self.table_style_selector.addItems(["arenas", "corridors"])
self.table_style_selector.currentIndexChanged.connect(self.update_table_style)
# Create layout
layout = QVBoxLayout()
layout.addWidget(QLabel("Experiment type:"))
layout.addWidget(self.experiment_type_selector)
layout.addWidget(QLabel("Duration:"))
layout.addWidget(self.duration_spinbox)
layout.addWidget(self.fps_label)
layout.addWidget(self.fps_spinbox)
layout.addWidget(QLabel("Folder:"))
layout.addWidget(self.folder_lineedit)
# Create a horizontal box layout for the record button and the checkbox
hbox = QHBoxLayout()
hbox.addWidget(self.record_button)
hbox.addWidget(self.HardwareTrigger_checkbox)
# Add the hbox layout to the main layout
layout.addLayout(hbox)
# layout.addWidget(self.stop_button)
layout.addWidget(QLabel("Table layout:"))
layout.addWidget(self.table_style_selector)
# Create an empty table
self.table = self.create_table()
# Add the table to the layout
layout.addWidget(self.table)
# Set the layout on the window
self.setLayout(layout)
# empty recording thread
self.recording_thread = None
# Intialize the updatable attributes to None
self.folder_path = None
# Initialize the folder_open attribute
self.folder_open = False
# Mac Datapath
if platform.system() == "Darwin":
self.DataPath = Path(
"/Volumes/Ramdya-Lab/DURRIEU_Matthias/Experimental_data/MultiMazeRecorder/Videos"
)
# Linux Datapath
if platform.system() == "Linux":
self.DataPath = Path(
"/mnt/labserver/DURRIEU_Matthias/Experimental_data/MultiMazeRecorder/Videos/"
)
self.local_path = Path("/home/matthias/Videos/")
# Check if Arduino is available and enable hardware triggering if so
if os.path.exists("/dev/ttyACM0"):
self.HardwareTrigger_checkbox.setEnabled(True)
self.HardwareTrigger_checkbox.setChecked(True)
else:
self.HardwareTrigger_checkbox.setEnabled(False)
def create_table(self, metadata=None, table_style="arenas"):
"""
Create a new table widget using the provided metadata
Parameters
----------
metadata : dict, optional
A dictionary containing the metadata for the table, by default None
table_style : str, optional
The layout style of the table, by default "arenas"
Returns
-------
QTableWidget
A table widget containing the metadata
"""
# Create a table widget to display the data
table = CustomTableWidget()
if table_style == "corridors":
column_count = 1 + 9 * 6
column_labels = ["Variable"]
for i in range(1, 10):
for j in range(1, 7):
column_labels.append(f"Arena{i}_Corridor{j}")
elif table_style == "arenas":
column_count = 1 + 9
column_labels = ["Variable"]
for i in range(1, 10):
column_labels.append(f"Arena{i}")
table.setColumnCount(column_count)
table.setHorizontalHeaderLabels(column_labels)
# Add empty rows and items to the table
for row in range(10):
table.insertRow(row)
for col in range(table.columnCount()):
item = QTableWidgetItem("")
table.setItem(row, col, item)
# Check if metadata was provided
if metadata:
# Fill the "Variable" column with the values from the "Variable" key in the metadata
for row, value in enumerate(metadata["Variable"]):
value_item = QTableWidgetItem(value)
table.setItem(row, 0, value_item)
# Fill the other columns with the values from the other keys in the metadata
col = 1
for variable, values in metadata.items():
if variable != "Variable":
for row, value in enumerate(values):
value_item = QTableWidgetItem(value)
table.setItem(row, col, value_item)
col += 1
# Check if the registry file exists and is not empty
registry_file = Path("variables_registry.json")
if registry_file.exists() and registry_file.stat().st_size > 0:
# Read the list of known variables from the registry file
with open(registry_file, "r") as f:
variables_registry = json.load(f)
else:
# Create a new list to store the known variables
variables_registry = []
# Check if any known variables are missing from the table and add them if necessary
row = len(metadata["Variable"])
for variable in variables_registry:
if variable not in metadata["Variable"]:
table.insertRow(row)
value_item = QTableWidgetItem(variable)
table.setItem(row, 0, value_item)
# Set the values of the other columns for this row
for col in range(1, table.columnCount()):
value_item = QTableWidgetItem("")
table.setItem(row, col, value_item)
row += 1
else:
# Check if the registry file exists and is not empty
registry_file = Path("variables_registry.json")
if registry_file.exists() and registry_file.stat().st_size > 0:
# Read the list of known variables from the registry file
with open(registry_file, "r") as f:
variables_registry = json.load(f)
else:
# Create a new list to store the known variables
variables_registry = []
# Fill the "Variable" column with the values from the registry
for row, value in enumerate(variables_registry):
value_item = QTableWidgetItem(value)
table.setItem(row, 0, value_item)
# Resize the rows and columns to fit their contents
table.resizeRowsToContents()
table.resizeColumnsToContents()
# Set a smaller font size for the table
font = table.font()
font.setPointSize(10)
table.setFont(font)
# Set a larger minimum size for the table widget
table.setMinimumSize(800, 600)
# Add empty rows to the table
table.add_empty_rows(10)
# Set the background color of the cells
table.set_cell_colors()
return table
def update_table_style(self, index):
# Get the selected layout from the combo box
table_style = self.table_style_selector.itemText(index)
# Update the table and metadata with the new layout
self.create_metadata(table_style=table_style)
self.create_table(table_style=table_style)
layout = self.layout()
if not self.folder_open:
table = self.create_table(table_style=table_style)
# Remove the existing table from the layout (if any)
if self.table:
layout.removeWidget(self.table)
self.table.deleteLater()
# Add the new table to the layout
layout.addWidget(table)
# Store a reference to the new table in an attribute
self.table = table
def detect_table_style(self, metadata):
# Check if the metadata contains keys for the "corridor" layout
if any(key.startswith("Arena1_Corridor") for key in metadata.keys()):
return "corridors"
# Check if the metadata contains keys for the "arena" layout
elif any(key.startswith("Arena") for key in metadata.keys()):
return "arenas"
# If neither layout is detected, return a default value
else:
return "arenas"
def close_folder(self):
if self.folder_open == False:
return
else:
# Reset the folder_open attribute
self.folder_open = False
# Get the layout of the central widget
layout = self.layout()
table = self.create_table()
# Remove the existing table from the layout (if any)
if self.table:
layout.removeWidget(self.table)
self.table.deleteLater()
# Store a reference to the new table in an attribute
self.table = table
layout.removeWidget(self.info_panel)
# Add the new table to the layout
layout.addWidget(table)
# Clear the folder line edit
self.folder_lineedit.clear()
# Reset the folder_path attribute
self.folder_path = None
self.folder_lineedit.setDisabled(False)
self.table_style_selector.setDisabled(False)
self.experiment_type_selector.setDisabled(False)
def on_hardware_checkbox_state_changed(self, state):
# If the checkbox is checked, enable hardware triggering
if state == 2:
self.recording_script = (
"/home/matthias/multimaze_recorder/Trigger_images.py"
)
self.fps_spinbox.setRange(16, 29)
self.fps_spinbox.setValue(29)
self.fps_label.setText("FPS (range: 16-29):")
print(
f"Hardware triggering enabled. Recording using {self.recording_script}"
)
# TODO : fix arduino not triggering when launching from GUI
else:
self.recording_script = "/home/matthias/multimaze_recorder/Snap_images.py"
self.fps_spinbox.setRange(1, 30)
self.fps_spinbox.setValue(30)
self.fps_label.setText("FPS (range: 1-30):")
print(
f"Hardware triggering disabled. Recording using {self.recording_script}"
)
def on_button_clicked(self):
duration = self.duration_spinbox.value()
fps = self.fps_spinbox.value()
folder = self.folder_lineedit.text()
# Check if a folder is open
if self.folder_open == False:
self.create_data_folder()
else:
self.save_data()
# Save the fps value to a npy file in the data folder
np.save(self.folder_path / "fps.npy", fps)
# Save the duration value to a npy file in the data folder
np.save(self.folder_path / "duration.npy", duration)
# Stop the live stream
self.stop_live_stream()
# Disable the record button and spinboxes
self.record_button.setEnabled(False)
self.duration_spinbox.setEnabled(False)
self.fps_spinbox.setEnabled(False)
# TODO: Fix this not properly disabling, and also apply it to situation where images already exist
print(f"Recording using {self.recording_script}")
# Start the recording in a separate thread
if platform.system() == "Linux":
self.recording_thread = threading.Thread(
target=self.record_images,
args=(self.recording_script, folder, fps, duration),
)
self.recording_thread.start()
elif platform.system() == "Darwin":
QMessageBox.information(
self, "Information", "Camera recording is not supported on laptop."
)
return
def record_images(self, script, folder, fps, duration):
subprocess.run(
[
"python",
script,
folder,
str(fps),
str(duration),
]
)
# Restart the live stream after recording is finished
self.start_live_stream()
# Re-enable the record button and spinboxes
self.record_button.setEnabled(True)
self.duration_spinbox.setEnabled(True)
self.fps_spinbox.setEnabled(True)
def on_stop_button_clicked(self):
# Terminate the recording thread
self.recording_thread.terminate()
# TODO: fix 'AttributeError: 'Thread' object has no attribute 'terminate'
def start_live_stream(self):
# Start the live stream in a separate process
if platform.system() == "Linux":
self.live_stream_process = subprocess.Popen(
["python", "/home/matthias/multimaze_recorder/LiveStream.py"]
)
elif platform.system() == "Darwin":
return
def stop_live_stream(self):
# Stop the live stream by terminating the process
if hasattr(self, "live_stream_process"):
self.live_stream_process.terminate()
def create_metadata(self, table=None, table_style="arenas"):
# Create a new metadata dictionary
metadata = {"Variable": []}
if table_style == "corridors":
for i in range(1, 10):
for j in range(1, 7):
metadata[f"Arena{i}_Corridor{j}"] = []
elif table_style == "arenas":
for i in range(1, 10):
metadata[f"Arena{i}"] = []
# If a table object is provided, use it to populate the metadata dictionary
if table:
# Update the metadata with the data from the table
variables = set()
for row in range(table.rowCount()):
variable_item = table.item(row, 0)
if variable_item:
variable = variable_item.text()
if variable and variable not in variables:
variables.add(variable)
metadata["Variable"].append(variable)
for col in range(1, table.columnCount()):
value_item = table.item(row, col)
column_label = table.horizontalHeaderItem(col).text()
if value_item:
value = value_item.text()
metadata[column_label].append(value)
else:
metadata[column_label].append("")
return metadata
def check_data_access(self):
if not self.DataPath.exists():
# Display an error message and return
QMessageBox.critical(
self,
"Error",
f"Cannot access the data folder. Check labserver connection.",
)
return False
def on_experiment_type_changed(self, index):
self.current_experiment_type = self.experiment_type_selector.itemText(index)
# If the experiment type is standard, set the table style to "arenas" and disable the table style selector
if self.current_experiment_type == "Standard":
self.table_style_selector.setCurrentIndex(0)
self.table_style_selector.setDisabled(True)
# If the experiment type is ball pushing, enable the table style selector
elif self.current_experiment_type == "Ball pushing":
self.table_style_selector.setDisabled(False)
def create_data_folder(self, metadata=None):
# Check if a folder is already open
if self.folder_open:
# Prompt the user to enter a new folder name
folder_name, ok = QInputDialog.getText(
self, "New Data Folder", "Enter new folder name:"
)
if not ok:
return
# Prompt the user to choose an experiment type
experiment_type, ok = QInputDialog.getItem(
self,
"Choose Experiment Type",
"Choose an experiment type for the new data folder:",
["Ball pushing", "Standard"],
0,
False,
)
if not ok:
return
self.current_experiment_type = experiment_type
index = self.experiment_type_selector.findText(experiment_type)
# If the experiment type is Standard, skip the table style selection and use the "arenas" layout
if experiment_type == "Standard":
table_style = "arenas"
elif experiment_type == "Ball pushing":
# Prompt the user to choose a table style
table_style, ok = QInputDialog.getItem(
self,
"Choose Table Style",
"Choose a table style for the new data folder:",
["arenas", "corridors"],
0,
False,
)
# Find the index of the item with the specified text
index = self.table_style_selector.findText(table_style)
# Set the current index of the table style selector combo box
self.table_style_selector.setCurrentIndex(index)
if not ok:
return
else:
# If there is a folder name in the lineedit, use it, else prompt the user to enter a folder name
if self.folder_lineedit.text():
folder_name = self.folder_lineedit.text()
ok = True
else:
folder_name, ok = QInputDialog.getText(
self, "New Data Folder", "Enter folder name:"
)
if not ok:
return
table_style = self.table_style_selector.currentText()
folder_path = self.DataPath / folder_name
# If the folder already exists, show a message box asking if the user wants to open the existing folder or choose a different name
while folder_path.exists():
reply = QMessageBox.question(
self,
"Folder Already Exists",
f"The folder {folder_name} already exists. Would you like to open the existing folder?",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.Yes,
)
# Open the existing folder if the user clicked "Yes"
if reply == QMessageBox.StandardButton.Yes:
self.open_data_folder(folder_path)
return
# Prompt the user to enter a new folder name if they clicked "No"
else:
return
if self.check_data_access() == False:
return
# Create the data folder with the specified name
if ok and folder_name:
folder_path = self.DataPath / folder_name
folder_path.mkdir(parents=True, exist_ok=True)
# Update the folder line edit with the full path to the new data folder
self.folder_lineedit.setText(str(folder_path))
# Create subdirectories for each arena
for i in range(1, 10):
arena_path = folder_path / f"arena{i}"
arena_path.mkdir(parents=True, exist_ok=True)
if self.current_experiment_type == "Ball pushing":
# Create subdirectories for each corridor
for j in range(1, 7):
corridor_path = arena_path / f"corridor{j}"
corridor_path.mkdir(parents=True, exist_ok=True)
if self.folder_open:
self.table.deleteLater()
table = self.create_table(table_style=table_style)
# Store a reference to the new table in an attribute
self.table = table
# Create experiment.json in the main folder
metadata = self.create_metadata(table=self.table, table_style=table_style)
if self.check_data_access() == False:
return
with open(folder_path / "metadata.json", "w") as f:
json.dump(metadata, f, indent=4)
# Open the new data folder
self.open_data_folder(folder_path)
def open_data_folder(self, folder_path=None):
if folder_path and str(self.DataPath) not in str(folder_path):
return
# Prompt the user to select a folder if no folder path was provided
if not folder_path:
folder_path = QFileDialog.getExistingDirectory(
self, "Open Data Folder", str(self.DataPath)
)
# Check if a valid folder was selected
if not folder_path:
return
# Convert the folder path to a Path object
folder_path = Path(folder_path)
# Check if the selected folder has a valid structure
valid_structure = True
for i in range(1, 10):
arena_path = folder_path / f"arena{i}"
if not arena_path.is_dir():
valid_structure = False
break
if self.current_experiment_type == "Ball pushing":
for j in range(1, 7):
corridor_path = arena_path / f"corridor{j}"
if not corridor_path.is_dir():
valid_structure = False
break
# TODO: Fix wrong instances of this happening and also change the result of the reply
if not valid_structure:
# Show a message box asking for confirmation to open the folder
reply = QMessageBox.question(
self,
"Invalid Folder Structure",
"This doesn't look like an experiment folder. Are you sure you want to open it?",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.Yes,
)
# Return if the user clicked "No"
if reply == QMessageBox.StandardButton.No:
return
# Check if the selected folder contains a metadata.json file
metadata_path = folder_path / "metadata.json"
if not metadata_path.is_file():
# Show a message box asking if the user wants to create a metadata.json file
reply = QMessageBox.question(
self,
"Missing Metadata File",
"This folder doesn't contain a metadata.json file. Would you like to create one?",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.Yes,
)
# Return if the user clicked "No"
if reply == QMessageBox.StandardButton.No:
return
# Create a new metadata.json file if the user clicked "Yes"
elif reply == QMessageBox.StandardButton.Yes:
# Prompt the user to choose a table style
table_style, ok = QInputDialog.getItem(
self,
"Choose Table Style",
"Choose a table style for the new data folder:",
["arenas", "corridors"],
0,
False,
)
# Find the index of the item with the specified text
index = self.table_style_selector.findText(table_style)
# Set the current index of the table style selector combo box
self.table_style_selector.setCurrentIndex(index)
if not ok:
return
# Create a new metadata.json file in the selected folder
metadata = self.create_metadata(table_style=table_style)
if self.check_data_access() == False:
return
with open(metadata_path, "w") as f:
json.dump(metadata, f, indent=4)
# Store the folder path in an attribute
self.folder_path = folder_path
# Load the metadata from the selected folder
with open(folder_path / "metadata.json", "r") as f:
metadata = json.load(f)
table_style = self.detect_table_style(metadata)
# Update the layout selector combo box with the detected layout
index = self.table_style_selector.findText(table_style)
self.table_style_selector.setCurrentIndex(index)
# Create a new table using the loaded metadata
table = self.create_table(
metadata, table_style=self.table_style_selector.currentText()
)
# Get the layout of the central widget
layout = self.layout()
# Remove the existing table from the layout (if any)
if self.table:
layout.removeWidget(self.table)
self.table.deleteLater()
# Add the new table to the layout
layout.addWidget(table)
# Store a reference to the new table in an attribute
self.table = table
# Remove the existing information panel from the layout (if any)
if hasattr(self, "info_panel"):
layout.removeWidget(self.info_panel)
self.info_panel.deleteLater()
# Create an information panel widget
info_panel = QWidget()
info_layout = QVBoxLayout()
info_panel.setLayout(info_layout)
# Add a label to display the folder path
folder_label = QLabel(f"Folder: {folder_path}")
info_layout.addWidget(folder_label)
# Check if the subfolders contain videos and .h5 files
full = True
processed = True
images = True
for i in range(1, 10):
arena_path = folder_path / f"arena{i}"
local_arena_path = self.local_path/ folder_path.name/ f"arena{i}"
if self.current_experiment_type == "Standard":
if not any(arena_path.glob("*.mp4")):
full = False
if not any(local_arena_path.glob("*.jpg")):
images = False
if not any(arena_path.glob("*.h5")):
processed = False
elif self.current_experiment_type == "Ball pushing":
for j in range(1, 7):
corridor_path = folder_path / f"arena{i}" / f"corridor{j}"
local_corridor_path = self.local_path/ folder_path.name/ f"arena{i}" / f"corridor{j}"
if not any(corridor_path.glob("*.mp4")):
full = False
if not any(local_corridor_path.glob("*.jpg")):
images = False
if not any(corridor_path.glob("*.h5")):
processed = False
if full or images:
# Disable the duration and fps spinboxes
self.duration_spinbox.setDisabled(True)
self.fps_spinbox.setDisabled(True)
self.record_button.setDisabled(True)
# if there are duration and fps files, apply their value to the spinboxes
if (folder_path / "duration.npy").exists():
self.duration_spinbox.setValue(np.load(folder_path / "duration.npy"))
if (folder_path / "fps.npy").exists():
self.fps_spinbox.setValue(np.load(folder_path / "fps.npy"))
# Add labels to display the status of the subfolders
full_label = QLabel(f"Full: {'Yes' if full else 'No'}")
processed_label = QLabel(f"Processed: {'Yes' if processed else 'No'}")
info_layout.addWidget(full_label)
info_layout.addWidget(processed_label)
if images:
# send a message to the user to inform that images are available for this experiment to be processed
QMessageBox.information(
self,
"Information",
f"Images are available for this experiment to be processed.",
)
self.info_panel = info_panel
# Add the information panel to the layout
layout.addWidget(info_panel)
# Set the folder_open attribute to True
self.folder_open = True
# Set the folder line edit to the selected folder
self.folder_lineedit.setText(str(folder_path.name))
self.folder_lineedit.setDisabled(True)
self.table_style_selector.setDisabled(True)
self.experiment_type_selector.setDisabled(True)
if self.tab_widget.currentIndex() != 0:
self.tab_widget.setCurrentIndex(0)
def save_data(self):
# Get the current folder path
folder_path = self.folder_path
# If no folder path has been entered, check if the folder line edit is empty
if not folder_path:
folder_name = self.folder_lineedit.text()
# If the folder line edit is empty, prompt the user to choose a folder name
if not folder_name:
metadata = self.create_metadata(table=self.table)
# Call the create_data_folder method to create a new data folder with the given metadata
self.create_data_folder(metadata)
# Get the new folder path from the line edit
folder_path = Path(self.folder_lineedit.text())
else:
# Use the text from the folder line edit as the folder name
folder_path = self.DataPath / folder_name
self.create_data_folder()
metadata = self.create_metadata(table=self.table)
# Save the updated metadata
if self.check_data_access() == False:
return
with open(folder_path / "metadata.json", "w") as f:
json.dump(metadata, f, indent=4)
else:
metadata = self.create_metadata(
table=self.table, table_style=self.table_style_selector.currentText()
)
# Save the updated metadata
if self.check_data_access() == False: