From c0b349973445383cfc4bb630aff17b8a56d6c1e0 Mon Sep 17 00:00:00 2001 From: Jeff Stout Date: Thu, 25 Jul 2024 14:45:31 -0400 Subject: [PATCH] WIP - Added brainsight target file to bstags Need to integrate this into bids pipeline --- nih2mne/bstags.py | 57 ++++++++++++++++++++++++++++++++++++ nih2mne/tests/test_bstags.py | 13 +++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/nih2mne/bstags.py b/nih2mne/bstags.py index dfbd68d..e2cd845 100755 --- a/nih2mne/bstags.py +++ b/nih2mne/bstags.py @@ -63,6 +63,63 @@ def txt_to_tag_pd(txtname): return tags +def tags_from_bsight_targetfile(fname, tag_template=['NAS','LPA','RPA']): + ''' + If brainsight files are exported from the target menu, the headers are + inconsistent the electrodes.txt style export file. This strips out the + non-matching column widths. + + Parameters + ---------- + fname : TYPE + DESCRIPTION. + tag_template : TYPE, optional + DESCRIPTION. The default is ['NAS','LPA','RPA']. + + Raises + ------ + ValueError + If start or stopping conditions in the target file can not be found + this will result in an error. + + Returns + ------- + tags : dict + {"NAS": float, "LPA": float, "RPA": float} + + ''' + import pandas as pd + with open(fname) as f: + tmp = f.readlines() + + #Identify start and end of target section + idx=0; start_idx=0; end_idx=0 + for i in tmp: + if i[0:8]=='# Sample': + start_idx=idx + if i[0:9]=='# Planned': + end_idx=idx + idx+=1 + + if (start_idx==0) or (end_idx==0): + raise ValueError('Cannot find the start and end of localizer section') + + #Format text and create list + tmp_ = tmp[start_idx:end_idx] + output = [] + for i in tmp_: + output.append(i.strip('\n').split('\t')) + output[0]=['Sample Name']+output[0][1:] + + #Create dataframe and extract tags + dframe = pd.DataFrame(output[1:], columns=output[0]) + tags ={} + for tag in tag_template: + tmp = dframe[dframe['Sample Name']==tag][['Loc. X','Loc. Y','Loc. Z']].values[0] + tags[tag] = [float(i) for i in tmp] + return tags + + def write_tagfile(tags, out_fname=None): '''''' diff --git a/nih2mne/tests/test_bstags.py b/nih2mne/tests/test_bstags.py index dfc128c..4491e40 100755 --- a/nih2mne/tests/test_bstags.py +++ b/nih2mne/tests/test_bstags.py @@ -6,10 +6,13 @@ @author: jstout """ -from ..bstags import txt_to_tag, txt_to_tag_pd +from ..bstags import txt_to_tag, txt_to_tag_pd, tags_from_bsight_targetfile from ..bstags import write_tagfile import pytest import os +import logging +# logger = logging.get_logger() +# err_logger = logging.get_logger() # ============================================================================= # Tests # ============================================================================= @@ -63,7 +66,15 @@ def test_alt_exported(): assert tags['Nasion']=="'Nasion' -6.0344 -114.7126 -2.6041" assert tags['Right Ear']=="'Right Ear' -67.2147 -18.6125 -36.5009" assert tags['Left Ear']=="'Left Ear' 64.1748 -28.3103 -32.4693" + +def test_tags_from_bsight_targetfile(): + fname = os.path.join(test_dir, 'bsight_target_file.txt') + tmp = tags_from_bsight_targetfile(fname, tag_template=['NAS','LPA','RPA']) + testvals = {'NAS': [9.1961, 125.8807, 5.5038], + 'LPA': [-62.6752, 52.695, -39.5915], + 'RPA': [71.4232, 39.4116, -31.5812]} + assert tmp == testvals