*beamformer用のBEMを作る(ちょっとお手軽に)

背景

beamformerのBEM(-bem-sol.fif)はhead coordinateでないといけない。

mne_setup_forwad_modelで変換すると-bem-sol.fifが出力されるがMRI coordinateになっているので、

head coordinateを付加しないと使えません。

なので、-bem-sol.fifを一旦.meshに変換して(ここをスクリプトで作業します)、xfit上でhead coordinate情報を付加して-bem-sol.fifを作ります。

前提

1. -bem-sol.fif (-bem.fifでもok)があること。なかったらここを参照してください。

だいたい/neuro/databases/bemにあるかと思います。

2. mneのツールを使ってます => mneのパスが通っている必要があります

段取り

1. スクリプトを走らせる -> *4xfit.meshのファイルが

~/ (ホームディレクトリ)に出力される。

2. これ以後は、このページの段取り4と同じやり方で新しく-bem-sol.fifを作成

スクリプトのやっていることは

1. *-bem-sol.fif を *.meshに変換 (中身はただのテキストファイル)

ただこれはxfitで読み込めない。

2. 書式をxfitで読めるように調整して*4xfit.meshの名前で保存

注意点

最終的に出力する-bem-sol.fifは、どのMEGデータに対応する-bem-sol.fifかわかるようにファイル名をつけましょう。

%%^^^^^^^^^ スクリプト本体 (matlab用) コピペしてください^^^^^^^^^^^^^^%%

%%%%%%%%%%%%%% copy below %%%%%%%%%%

%% create .mesh file for xfit from -bem-sol.fif

% * It takes a few seconds.

% * It uses -bem-sol.fif (-bem.fif is still all right)

%

% *The created *4xfit.mesh contains brainstem and cerebellar hemispheres.

%

% *The created *4xfit.mesh file does not contain head coordinate, so you have to transfer head

% coordinate using xfit.

% => use estimates - prepare new BEM model

%

% *The created *4xfit.mesh is located at ~/ (home directory)

%

%% necessary information %%

% get -bem-sol.fif

cd('/neuro/databases/bem');

[sFnameIn sDir]=uigetfile('*-bem-sol.fif','choose -bem-sol.fif');

sFileNameBem=fullfile(sDir,sFnameIn);% bem.fif to process

%%%%%%%%%%% you do not need to edit the script below %%%%%%%%%%%%

[sDir sFnameIn sExt]=fileparts(sFileNameBem);

% convert bem-sol.fif into .mesh

sFileNameMesh=[sFnameIn,'.mesh'];

cd('~/');% go to home directory

eval(['!mne_list_bem --id 1 --xfit --bem ',sFileNameBem,' --out ',sFileNameMesh])

%% convert .mesh format

fid=fopen(sFileNameMesh,'r');

%% there are 2 blocks in the .mesh file

vecLines=zeros(1,2);

celTriColumns=[];

for kk=1:2

sLine=fgetl(fid);% line number

nLine=str2num(sLine);

matTriColumns=zeros(nLine,3);

for ii=1:nLine

sLine=fgetl(fid);%format:1 -0.100 -0.0930 -0.0070

idxBlank=strfind(sLine,' ');% search blanks

diffBlank=diff(idxBlank);

idxStart=[idxBlank(diffBlank>1)+1,idxBlank(end)+1];

nLengthNum=diffBlank(diffBlank>1)-1;

idxEnd=[idxStart(1:end-1)+nLengthNum-1,length(sLine)];

for jj=1:4

vecRow(jj)=str2num(sLine(idxStart(jj):idxEnd(jj)));

end

matTriColumns(ii,:)=vecRow(2:4);

end

celTriColumns{kk}=matTriColumns;

vecLines(kk)=nLine;

end

fclose(fid);

delete(sFileNameMesh);

%% write out .mesh

sFnameOut=[sFileNameMesh(1:end-5),'4xfit.mesh'];

fid=fopen(sFnameOut,'w');

% 1st part

kk=1;

matTriColumns=celTriColumns{kk};

fprintf(fid,'%u\n',vecLines(kk));

for ii=1:vecLines(kk)

fprintf(fid,'%1.4f %1.4f %1.4f\n',matTriColumns(ii,:));

end

% 2nd part

kk=2;

matTriColumns=celTriColumns{kk};

fprintf(fid,'%u\n',vecLines(kk));

for ii=1:vecLines(kk)

fprintf(fid,'%u %u %u\n',matTriColumns(ii,:));

end

fclose(fid)

hmsgLast1=msgbox([sFnameOut,' is created in home directory.'],'Done','none');

uiwait(hmsgLast1);

hmsgLast2=msgbox({['Now open xfit and use'],

['"estimates - prepare new BEM model"'],

['to create -bem-sol.fif with head coordinate']},'Open Xfit','none');