Skip to content

Commit

Permalink
Initial sections of the write script component
Browse files Browse the repository at this point in the history
  • Loading branch information
jstout211 committed Jan 7, 2025
1 parent 68736ec commit b64b06f
Showing 1 changed file with 92 additions and 16 deletions.
108 changes: 92 additions & 16 deletions nih2mne/GUI/trigger_code_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,22 @@
TODO: Temporal coding on PPT -- a 1 followed by a 5 is a Y event
Set action to invert trigger for counts
Set all dig trigs to invert together
Create a parsemarks list and remove all after an erase command
detect_digital currently does not support downgoing triggers
"""

import glob
import mne
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout, \
QHBoxLayout, QVBoxLayout, QPushButton, QLabel, QComboBox, QLineEdit, QCheckBox
from functools import partial
# from functools import partial
import sys
# from nih2mne.dataQA.bids_project_interface import subject_bids_info, bids_project
import os, os.path as op
import numpy as np
# from nih2mne.utilities.montages import montages
# from nih2mne.dataQA.qa_config_reader import qa_dataset, read_yml
from nih2mne.utilities.trigger_utilities import (parse_marks, detect_digital,
check_analog_inverted, threshold_detect)

import glob
import mne


############## Setup Main Classes ################

Expand Down Expand Up @@ -206,6 +204,10 @@ def setup_full_layout(self):

self.keep_events_layout = QVBoxLayout()
main_layout.addLayout(self.keep_events_layout)

self.b_write_parser_file = QPushButton('Write Parser File')
self.b_write_parser_file.clicked.connect(self.write_parser_script)
main_layout.addWidget(self.b_write_parser_file)

return main_layout

Expand Down Expand Up @@ -276,13 +278,6 @@ def update_event_names(self):
#Empty the keep events layout list, so it doesn't append the previous
self.update_keep_events_list(flush=True)

#Add each event to the final keep events layout
#checkable allows for depressed pushbuttons
# for i in namelist:
# tmp=QPushButton(i)
# tmp.setCheckable(True)
# self.keep_events_layout.addWidget(tmp)
# del tmp

def update_keep_events_list(self, flush=False):
num_keep_buttons = self.keep_events_layout.count()
Expand Down Expand Up @@ -340,7 +335,8 @@ def select_meg_dset(self):
print(meg_fname)
if meg_fname != None:
self.meg_raw = mne.io.read_raw_ctf(meg_fname, clean_names=True,
system_clock='ignore', preload=False)
system_clock='ignore', preload=False,
verbose=False)
self.trig_ch_names = [i for i in self.meg_raw.ch_names if i.startswith('UADC') or i.startswith('UPPT')]
self.fill_trig_chan_layout()

Expand All @@ -351,6 +347,86 @@ def select_meg_dset(self):

self.keep_events_layout.addWidget(QLabel('Events To Write'))

def write_parser_script(self):
'''
Part 1: Write Threshold Detect Options
Part 2: Write Digital Detect components
Part 3: Write Parse_marks components
Part 4: Combine dataframes
Part 5: Select Keep options
Part 6: Write the markerfile to the input dataset
'''

##### Python Header Section #####
header=["#!/usr/bin/env python3\n",
"# -*- coding: utf-8 -*-\n",
"\n\n",
"'''\n"
"This code was generated by the nih2mne package: \n",
"https://github.com/nih-megcore/nih_to_mne.git \n",
"'''\n"
"\n\n",
"import sys\n"
"import mne\n",
"import nih2mne\n",
'''from nih2mne.utilities.trigger_utilities import (parse_marks, detect_digital,
check_analog_inverted, threshold_detect, append_conditions)\n''',
"\n\n",
"meg_fname = sys.argv[1]\n\n"
]
fname = "/tmp/testfile.py"
with open(fname, 'w') as f:
f.writelines(header)

##### Make a list of dataframes ####
init_code = []
init_code.append('dframe_list=[]')


##### Analogue Triggers #####
ana_trig_code = []
for i, tile in self.tile_dict.items():
if i.startswith('UADC'):
markname = tile.event_name.text()
if tile.b_downgoing_trigger.checkState()==2:
invert_val = True
else:
invert_val = False
tmp_code = f"tmp_dframe = threshold_detect(dsname=meg_fname, channel='{i}', mark='{markname}', invert={invert_val})"
ana_trig_code.append(tmp_code)
tmp_code = f"dframe_list.append(tmp_dframe)"
ana_trig_code.append(tmp_code)

##### Digital Triggers ##### #####!!!!!! IF NAME does not exist - dont write
dig_trig_code = []
for i, tile in self.tile_dict.items():
if i.startswith('UPPT'):
markname = tile.event_name.text()
# if self.b_downgoing_trigger.checkState()==2:
# invert_val = True
tmp_code = f"tmp_dframe = detect_digital(filename=meg_fname, channel='{i}', mark='{markname}')"
dig_trig_code.append(tmp_code)
tmp_code = f"dframe_list.append(tmp_dframe)"
dig_trig_code.append(tmp_code)

##### Combine Initial Trigger Processing #####
init_trig_code = ana_trig_code + dig_trig_code
fname = "/tmp/testfile.py"
with open(fname, 'a') as f:
for i in init_trig_code:
f.write(i+'\n')
f.write('\n')

# tmp_code =
# init_trig_code.ap










def window():
Expand Down

0 comments on commit b64b06f

Please sign in to comment.