Skip to content

Commit

Permalink
WIP - Added brainsight target file to bstags
Browse files Browse the repository at this point in the history
Need to integrate this into bids pipeline
  • Loading branch information
jstout211 committed Jul 25, 2024
1 parent 7fa1d3d commit c0b3499
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
57 changes: 57 additions & 0 deletions nih2mne/bstags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
''''''

Expand Down
13 changes: 12 additions & 1 deletion nih2mne/tests/test_bstags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# =============================================================================
Expand Down Expand Up @@ -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



Expand Down

0 comments on commit c0b3499

Please sign in to comment.