A Matlab script to generate ROI masks using an Atlas in SPM12

SPM12 introduces some useful functions such as spm_atlas or new atlas “labels_Neuromorphometrics.” We find the description about labels_Neuromorphometrics in SPM12 Release note.

Maximum probability tissue labels derived from the “MICCAI 2012 Grand Challenge and Workshop on Multi-Atlas Labeling” are available in files tpm/labels Neuromorphometrics.{nii,xml}. These
data were released under the Creative Commons Attribution-NonCommercial (CC BY-NC) with no end date. Users should credit the MRI scans as originating from the OASIS project and the labeled
data as “provided by Neuromorphometrics, Inc. under academic subscription”. These references should be included in all workshop and final publications. See spm templates.man for more details about the generation of this file.

I wanted to generate masks of some regions using this labels_Neuromorphometrics.

Below is the tiny script which generates masks from your preferred atlas.
Running script brings up a file selector. You can choose any atlas you want.
Then it brings up another dialog which lists the region within the atlas. You can choose as many regions as you want, and the scripts generates masks whose file name is the name of the regions.


%generate_masks_from_atlas.m
%This script generate mask files from any atlases you prefer.
%K. Nemoto 25 April 2015

xA=spm_atlas('load');
S=spm_atlas('select',xA);

for i = 1:size(S,2)
    fname=strcat(S{i},'.nii');
    VM=spm_atlas('mask',xA,S{i});
    VM.fname=fname;
    spm_write_vol(VM,spm_read_vols(VM));
end

Download generate_masks_from_atlas.m (right click and save as)

A Matlab script to set origin to the center of the image using SPM

Sometimes we need to handle images whose origin is far from the center of the image.
My friend, Fumio Yamashita, from Iwate Medical University wrote a script which set the origin to the center of the image.

Usage is very simple.

    Download

  1. setorigin_center.m (Right click and save as…) and save in SPM directory or any directories under MATLAB path.
  2. Type the following in Matlab command window.
  3. >>setorigin_center
  4. This will pop up the file selector, so choose the images you want to set the origin to the center.

That’s it.

Below is the example of the script.

Left is before applying the script. You can see that the origin is far from the center.
Right is after applying the script. Now origin is set to the center.

setorigin_screenshot

(筑波大関係者専用)Ubuntuで OpenVPNを使用して筑波大のVPNサービスに接続する方法

筑波大の学術情報メディアセンターはVPNサービスを運用してくださっています。
非常にわかりやすいマニュアルを提供してくださっていますが、残念ながら、Linuxのマニュアルはまだ提供されていません。

そこで、私の環境(Xubuntu 14.04)でOpenVPNを使ってうまくVPN接続に成功しましたので、備忘録を兼ねて書いておきます。

続きを読む

Ubuntu12.04以降に本家TeX Liveの最新版を簡単にインストールする方法

先日、私のシステムをXubuntu 12.04からXubuntu 14.04にアップグレードしました。
私はLaTeXに関しては、本家のTeX Liveを使っています。インストーラーは十分に使いやすいのですが、怠け者なので、より簡単にできないかと方法を探してみました。

なぜかというと、本家を使う場合、リンクにあるように、少し設定が必要だからです。

そのリンクをひとつずつ確認していたところ、ある方が、Ubuntu用の本家TeX Liveのインストールスクリプトを公開しているということを知りました。最新版にリンクされています。

早速トライしてみたら便利だったので、紹介します。

続きを読む

Ubuntu 14.04 へのFreeSurferのインストール

2017.10.04 本記事は古い物です。最新版はこちらになりますので、そちらをご参照ください。

以前、FreeSurferのインストールの方法を記載しましたが、しばらく時間が経ちました。最近、改めてFreeSurferをインストールする機会がありましたので、改めてまとめてみます。2014年12月現在、FreeSurferのバージョンは、5.3.0です。コピペでもいけるぐらいにまとめてみます。

英語でのFreesurferをLinuxにインストールするときの具体的な方法は、
http://surfer.nmr.mgh.harvard.edu/fswiki/LinuxInstall
に書いてあります。以下は英語を読むのがつらい方のためです。

続きを読む

2014年12月包括脳VBMチュートリアル配布資料(VBMの実践)

2014年12月13日に包括脳MRI脳画像解析チュートリアルが開催されました。
私はVBMチュートリアルを担当しましたが、その時の配布資料を公開します。

このPDFは、「すぐできるVBM」の補足のような位置づけにあります。
すぐできるVBMのサンプルデータを使っていますし、チュートリアルを受講していない方々でも、このPDFを参考にしていただければ、さらなる知識が得られるのではないかと思います。

ダウンロードはこちらからどうぞ。

SPM8とSPM12をよりスマートに同一のMatlabのライセンスで使用する方法

しばらく前に、SPM8とSPM12を同一のMatlabのライセンスで使用する方法という記事を書きましたが、先日の包括脳チュートリアルのアンケートで、「SPM8とSPM12を併存する方法がわかりにくい」というご指摘をいただきました。

ということで、ひとつのスクリプトで全部解決できる方法を考えてみました。
奇しくも、SPM-MLで同じような話題があり、私もその話題に加わったもので、その結果を受けています。

続きを読む

Linuxにおけるハイフンの意味

ときどき、下記のような表現を見ます。

$ wget -O- http://www.example.com/example.gpg.key | \
  sudo apt-key add -

この -Oのあとについているハイフンや、apt-key addのあとについているハイフンの意味をきちんと理解していませんでした。

改めて、調べたら、いろいろな方が解説してくれていました。

結論からいうと、

Linuxにおけるハイフンは、標準入出力をさす

のですね。

上記の例の場合、wget -O は本当はファイル名を引数にもつはずで、

wget -O file となるはずなんです。

でも、今の場合、-を指定することで、

wget -O- はその内容を、標準出力に表示するんですね。
今の場合は、example.gpg.keyが表示されるわけです。

で、上記の場合、さらにパイプがつながっていて、apt-key add -と続きます。
今回のハイフンは、今度は標準入力になるわけですね。

つまり、example.gpg.keyの内容が標準出力に出力され、
それがパイプで今度は標準入力になって、apt-key addの引数として使われるわけです。

となると、次と同じということですね。

$ wget -O tmpfile http://www.example.com/example.gpg.key
$ sudo apt-key add tmpfile
$ rm tmpfile

ハイフンを上手に使うことで一時ファイルを作ることなくパイプを使うことができるわけです。
スマートですね。

Xubuntu 14.04にNVIDIAの最新ドライバーをインストールする方法(Xubuntuをテキストモードで起動させる方法)

先日、新しいデスクトップマシンが職場に到着しました。
早速、Lin4Neuro-14.04をインストールしたところ、画面解像度が1280×1024までしかでなく、悲しいことに…。

グラフィックボードはNVIDIA GeForce GTX 750 Tiが搭載されています。

Ubuntuのnvidia-currentではまだ未対応であることから、本家のサイトからドライバーをインストールすることとしましたが、ひと手間だったので、その経過をまとめます。

続きを読む

Lin4Neuro based on Xubuntu 14.04LTS was released

I managed to customize Xubuntu 14.04LTS to be Lin4Neuro-14.04.

L4N-trusty-screenshot

For this customization I did the following;

  • .desktop files for neuroimaging analysis packages are stored in .local/share/applications
  • Neuroimaging.directory is stored in .local/share/desktop-directories.
  • Custom icons are stored in ~/.icons

If you look at these files, you will see how you can add your entries to menu.

I keep maintaining Xubuntu 12.04 because I am not certain Connectome mapper works in Xubuntu 14.04. If you want to use Connectome mapper, I recommend to use Lin4Neuro 12.04.

Grubで困った時にまずすること

先日、デュアルブートのマシンでXubuntuのアップデートをした時にGrubがアップデートされ、質問に適当に答えていたら、Grubのエントリーが消えてしまうという事件がありました。

Grubの設定をいろいろするのはめんどくさい、何かいい方法がないかなと探していました。
そうしたら、ありました。めんどくさがりにぴったりなコマンドが。

$ sudo update-grub

これで、システムに入っているOSを探し出し、自動でメニューにエントリーしてくれます。

ということで、Grubで困ったら、焦らず、まずはupdate-grubをタイプしてみてはどうでしょうか。

Mac OSX YosemiteにアップグレードしたらFSLが動かなくなった場合の対処法

私自身も遭遇しましたし、他の方からも問い合わせがありましたので、簡単に記載しておきます。

MacをYosemiteにアップグレードした際に、FSLが動作しなくなることがあります。
その際には、バタバタする前に、まず、XQuartzを再インストールしてみてください。
それだけで問題が解決することがあります。

本家にも情報がありました。

Note for Yosemite (10.10) users: even if you have previously installed X11, you will need to reinstall it from the XQuartz web page to get FSL to work.

これで、問題解決といって大丈夫そうです。

Making 2-up pdf files using pdfnup included in Tex Live 2014

I needed to make a 2-up pdf out of slide pdfs. That is, original slide pdf is landscape, and I wanted to put 2 slides in one page.

Googling led me “pdfnup”. Since I installed Tex Live 2014 on my machine, pdfnup was already installed.

So, I tried the following

$ pdfnup --orient portrait --nup 1x2 input.pdf

This resulted in error, which said

  pdfnup ERROR: the --orient option is not allowed,
  use --landscape or --no-landscape to specify
  the output page orientation

so I used –no-landscape option

$ pdfnup --no-landscape --nup 1x2 input.pdf

This made input-nup.pdf and that is 2-up pdf!

This tool is so useful for preparing handouts for presentation.

In addition to the above example, the author shows a nice example of how to produce a handout from a file of presentation slides.
For slides made with the standard 4:3 aspect ratio a nice 6-up handout on A4 paper can be made by

$ pdfjam --nup 2x3 --frame true --noautoscale false \
  --delta "0.2cm 0.3cm" --scale 0.95 myslides.pdf \
  --outfile myhandout.pdf

Scripts to convert hdr/img format into nii format

Nifti file havs two kinds of extensions, hdr/img or nii.
Sometimes hdr/file nifti files can be confusing because the extension is the same as ANALYZE format.
There are some ways to convert hdr/img into nifti.
One is to use fslchfiletype included in FSL. This is the easiest, but it requires FSL.
The other is to use SPM. However, some scripting is needed.
So I wrote tiny scripts for converting hdr/img format into nii format.

If you prefer SPM, use img2nii_spm.m. Save the script into a folder under MATLAB path and type “img2nii_spm” from MATLAB command window.

%%img2nii.m--------------------------------------------
%Script to convert hdr/img files to nii.
%This script uses SPM function, so you need to install SPM5 or later.
%Kiyotaka Nemoto 05-Nov-2014

%select files
f = spm_select(Inf,'img$','Select img files to be converted');

%convert img files to nii
for i=1:size(f,1)
	input = deblank(f(i,:));
	[pathstr,fname,ext] = fileparts(input);
    output = strcat(fname,'.nii');
    V=spm_vol(input);
    ima=spm_read_vols(V);
    V.fname=output;
    spm_write_vol(V,ima);
end

If you prefer FSL, use img2nii_fsl.sh. Save the script into a folder, add executable attribute using “chmod 755 img2nii_fsl.sh”.
Wild card can be used for selecting files.

#!/bin/sh

#Image file type converter using fslchfiletype
#This script converts various image files into NIFTI format (.nii) files.
#K. Nemoto 19 Jan 2013

if [ $# -lt 1 ] ; then
	echo "Please specify the files you want to convert!"
	echo "Usage: $0 filename"
	exit 1
fi

for file in "$@" ; do
	if [ -f $file ] ; then
		fslchfiletype NIFTI $file
	else
		echo "$file: No such file"
	fi
done

Download img2nii_spm.m (Right-click and save).
Download img2nii_fsl.sh (Right-click and save).

SPM12におけるspm_defaults.mの設定 【重要】

SPM12の正式版が出て、気づいたのですが、spm_defaults.mの設定に関して、重要な変更が行われました。

今後は、spm_defaults.mは直接いじってはいけないことになりました。
これは、非常に理にかなっていることで、これまで、spm_defaults.mを修正しても、アップデートによってこれが上書きされてしまうことがありました。これを回避するために、新たな方法が導入されました。

「すぐできるVBM」のp.22 「8.SPMの設定」に関しての記載を以下のように訂正します。

続きを読む

包括脳MRIチュートリアルが12月13日に開催されます

毎年開催している包括脳MRIチュートリアルですが、
今年は12月13日(土)に開催されることになりました。

詳しい案内はこちらをご覧ください。

VBMチュートリアルに関しては、席はあと20席程度残っているとのことです。

今回は、

・画像のQC(岩手医大山下先生)
・多重比較補正についての理解(京大川口先生)
・SPMの結果を論文に載せるまでの具体的手順

といった内容を計画しています。

関心のある方は、こちらにある申し込み方法をご覧のうえ、ご応募ください。

Linuxでfind, sedを使いながら文字列を再利用してファイル名の途中に文字列を挿入する方法

最近、以下のようなことをやる必要がありました。

ファイル名が

201105_ID.jpg
20120608_ID.jpg
20130704_ID.jpg
201409_ID.jpg

となっているファイル(年月が必ずあり、日が時についている)に対して、
年の後にアンダースコアを入れたい、つまり、

2011_05_ID.jpg
2012_0608_ID.jpg
2013_0704_ID.jpg
2014_09_ID.jpg

としたいわけです。

続きを読む

SPM8とSPM12を同一のMatlabのライセンスで使用する方法

SPMのメーリングリストでSPMの中の人が明らかにしていますが、SPM12の正式版がそろそろ公開されるようです。SPM12が正式に公開されました。(2014年10月1日)

SPM12はいろいろよい点も加わっていますが、まだ、プラグインで対応していないものも多々あります。
このため、しばらくは、SPM8とSPM12が同時に動かせる環境にあった方がよいですよね。

いい方法がありますので、ご紹介します。

続きを読む

心理のためのMatlabチュートリアル:解答例と解説

2019.10.11: すべての回答ができましたので、アップデートしました。

何人かの方々から、「心理のためのMatlabチュートリアル」の練習の解答がないか問い合わせを受けていました。
回答の完全版ができましたので、公開させていただきます。

心理のためのMatlabチュートリアルの解答例をダウンロード

Update of Lin4Neuro (10 Aug 2014)

Lin4Neuro was updated. This time the change was limited to kernel update and security updates.

  • Kernel was updated to 3.13.0-32-generic.
  • All security updates were applied.

I’m working on Xubuntu 14.04, but it seems to have several problems with neuroimaging software packages. So for now I keep updating Lin4Neuro based on Xubuntu 12.04.

You can download the latest Lin4Neuro from here.