先日、新しいデスクトップマシンが職場に到着しました。
早速、Lin4Neuro-14.04をインストールしたところ、画面解像度が1280×1024までしかでなく、悲しいことに…。
グラフィックボードはNVIDIA GeForce GTX 750 Tiが搭載されています。
Ubuntuのnvidia-currentではまだ未対応であることから、本家のサイトからドライバーをインストールすることとしましたが、ひと手間だったので、その経過をまとめます。
先日、新しいデスクトップマシンが職場に到着しました。
早速、Lin4Neuro-14.04をインストールしたところ、画面解像度が1280×1024までしかでなく、悲しいことに…。
グラフィックボードはNVIDIA GeForce GTX 750 Tiが搭載されています。
Ubuntuのnvidia-currentではまだ未対応であることから、本家のサイトからドライバーをインストールすることとしましたが、ひと手間だったので、その経過をまとめます。
I managed to customize Xubuntu 14.04LTS to be Lin4Neuro-14.04.

For this customization I did the following;
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.
先日、デュアルブートのマシンでXubuntuのアップデートをした時にGrubがアップデートされ、質問に適当に答えていたら、Grubのエントリーが消えてしまうという事件がありました。
Grubの設定をいろいろするのはめんどくさい、何かいい方法がないかなと探していました。
そうしたら、ありました。めんどくさがりにぴったりなコマンドが。
$ sudo update-grub
これで、システムに入っているOSを探し出し、自動でメニューにエントリーしてくれます。
ということで、Grubで困ったら、焦らず、まずはupdate-grubをタイプしてみてはどうでしょうか。
私自身も遭遇しましたし、他の方からも問い合わせがありましたので、簡単に記載しておきます。
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.
これで、問題解決といって大丈夫そうです。
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
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の設定に関して、重要な変更が行われました。
今後は、spm_defaults.mは直接いじってはいけないことになりました。
これは、非常に理にかなっていることで、これまで、spm_defaults.mを修正しても、アップデートによってこれが上書きされてしまうことがありました。これを回避するために、新たな方法が導入されました。
「すぐできるVBM」のp.22 「8.SPMの設定」に関しての記載を以下のように訂正します。
最近、以下のようなことをやる必要がありました。
ファイル名が
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
としたいわけです。
SPMのメーリングリストでSPMの中の人が明らかにしていますが、SPM12の正式版がそろそろ公開されるようです。SPM12が正式に公開されました。(2014年10月1日)
SPM12はいろいろよい点も加わっていますが、まだ、プラグインで対応していないものも多々あります。
このため、しばらくは、SPM8とSPM12が同時に動かせる環境にあった方がよいですよね。
いい方法がありますので、ご紹介します。
2019.10.11: すべての回答ができましたので、アップデートしました。
何人かの方々から、「心理のためのMatlabチュートリアル」の練習の解答がないか問い合わせを受けていました。
回答の完全版ができましたので、公開させていただきます。
Lin4Neuro was updated. This time the change was limited to kernel update and security updates.
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.
研究でROC解析を行う必要があり、Rでどうやったらできるのか調べてみました。
そうしたところ、ROCRというパッケージが公開されており、比較的簡単にROC解析を行い、グラフを作成できることがわかりました。
既にRはインストールされているとします。Ubuntuの場合、ROCRパッケージはapt経由で簡単に入手できます。
$ sudo apt-get install r-cran-rocr
これでインストール完了です。
ROC解析に必要なものは、何らかの指標と、それが属するグループの一覧です。具体例を挙げると、以下のようになります。
第1列に指標、第2列に属するグループ(0か1)が記載されています。
これをroc_data.txtとという名前で保存することとします。保存したディレクトリをRのワーキングディレクトリとします。
0.9706 1 0.9572 1 0.4854 1 0.8003 1 0.1419 1 0.4218 1 0.9157 1 0.7922 1 0.9595 1 0.6557 1 0.0357 1 0.8491 1 0.934 1 0.6787 1 0.7577 1 0.7431 1 0.3922 1 0.6555 1 0.1712 1 0.706 1 0.4797 0 0.4551 0 0.0374 0 0.081 0 0.2984 0 0.7597 0 0.1404 0 0.3853 0 0.0238 0 0.5513 0 0.0551 0 0.306 0 0.4991 0 0.6909 0 0.7593 0 0.3472 0 0.0614 0 0.0507 0 0.0575 0 0.6407 0
ROCRはRを立ち上げた後に、library(ROCR)で起動できます。
$ R > library(ROCR)
先ほどのroc_data.txtをrocdataという変数に読み込みます。変数名は何でもいいのですが、ここではそうします。
read.tableという関数で表を読み込めるので、それを使います。
rocdata <- read.table("roc_data.txt")
ROCRのステップ1はpredictionで、値と属するグループを指定します。
今、rocdataは20行2列の行列になっています。1列目はrocdata[,1]で、2列目はrocdata[,2]であらわすことができますので、以下のように記載します。
pred <- prediction(rocdata[,1], rocdata[,2])
ここで、変数predはpredictionの頭文字4文字です。もちろん、別の名前でもかまいません。
私はよくカンマを忘れるので、カンマも忘れないようにしましょう。
ここで、何が行われているかというと、データを大きい順にソートし、真陽性(TP)、偽陽性(FP)、偽陰性(FN)、真陰性(TN) の数を算出します。
ROCRのステップ2はperformanceです。ここでは、感度、すなわち真陽性率 (TP/(TP+FN)で定義)と、1-特異度、すなわち偽陽性率(FP/(FP+TN)で定義)を求めます。以下のようにタイプします。
perf <- performance(pred, "tpr", "fpr")
ここでtprはtrue positive rateを、fprはfalse positive rateを意味します。
それでは、ROC曲線を描きます。非常に簡単です。
plot(perf)
そうすると、次のようなグラフが現れるはずです。

グラフをPNG形式で保存するには、次のように行うことで、roc-curve.pngという名前でワーキングディレクトリに保存されます。
png("roc-curve.png")
plot(perf)
dev.off()
先ほどのperformanceの際に”auc”と指定するとAUCも計算されます。ただ、1クッション入れる必要があります。具体的な方法はこちらのサイトに記載されていましたが、それを転載します。
auc.tmp <- performance(pred,"auc") auc <- as.numeric(auc.tmp@y.values)
performance(pred,”auc”)の結果をauc.tmpという変数に代入し、
auc.tmpの中からy.valuesの値を取り出して、その値を変数aucに代入します。
最後に変数aucを表示させてみます。
auc [1] 0.8
これでAUCが0.8だということがわかります。
ここからさらに一歩踏み込んで、正診率を求めたいと思います。
正診率、感度、特異度は以下で定義されます。
正診率=(TP+TN)/総数
感度(真陽性率)=TP/(TP+FN)
特異度=TN/(FP+TN)
今、総数は、rocdataの行数を求めればよいですから、nrow(rocdata)で求められます
カットオフ値を少しずつずらした時に、TP, FP, FN, TNは変わっていきますので、その一覧を表に出力しましょう。
表の列は以下のようにしたいと思います
Cutoff TP FP FN TN Sensitivity Specificity Accuracy
table <- data.frame(Cutoff=unlist(pred@cutoffs), TP=unlist(pred@tp), FP=unlist(pred@fp), FN=unlist(pred@fn), TN=unlist(pred@tn), Sensitivity=unlist(pred@tp)/(unlist(pred@tp)+unlist(pred@fn)), Specificity=unlist(pred@tn)/(unlist(pred@fp)+unlist(pred@tn)), Accuracy=((unlist(pred@tp)+unlist(pred@tn))/nrow(rocdata)) )
これで、tableを表示させると、以下のように表示されます。
> table Cutoff TP FP FN TN Sensitivity Specificity Accuracy 1 Inf 0 0 20 20 0.00 1.00 0.500 2 0.9706 1 0 19 20 0.05 1.00 0.525 3 0.9595 2 0 18 20 0.10 1.00 0.550 4 0.9572 3 0 17 20 0.15 1.00 0.575 5 0.9340 4 0 16 20 0.20 1.00 0.600 6 0.9157 5 0 15 20 0.25 1.00 0.625 7 0.8491 6 0 14 20 0.30 1.00 0.650 8 0.8003 7 0 13 20 0.35 1.00 0.675 9 0.7922 8 0 12 20 0.40 1.00 0.700 10 0.7597 8 1 12 19 0.40 0.95 0.675 11 0.7593 8 2 12 18 0.40 0.90 0.650 12 0.7577 9 2 11 18 0.45 0.90 0.675 13 0.7431 10 2 10 18 0.50 0.90 0.700 14 0.7060 11 2 9 18 0.55 0.90 0.725 15 0.6909 11 3 9 17 0.55 0.85 0.700 16 0.6787 12 3 8 17 0.60 0.85 0.725 17 0.6557 13 3 7 17 0.65 0.85 0.750 18 0.6555 14 3 6 17 0.70 0.85 0.775 19 0.6407 14 4 6 16 0.70 0.80 0.750 20 0.5513 14 5 6 15 0.70 0.75 0.725 21 0.4991 14 6 6 14 0.70 0.70 0.700 22 0.4854 15 6 5 14 0.75 0.70 0.725 23 0.4797 15 7 5 13 0.75 0.65 0.700 24 0.4551 15 8 5 12 0.75 0.60 0.675 25 0.4218 16 8 4 12 0.80 0.60 0.700 26 0.3922 17 8 3 12 0.85 0.60 0.725 27 0.3853 17 9 3 11 0.85 0.55 0.700 28 0.3472 17 10 3 10 0.85 0.50 0.675 29 0.3060 17 11 3 9 0.85 0.45 0.650 30 0.2984 17 12 3 8 0.85 0.40 0.625 31 0.1712 18 12 2 8 0.90 0.40 0.650 32 0.1419 19 12 1 8 0.95 0.40 0.675 33 0.1404 19 13 1 7 0.95 0.35 0.650 34 0.0810 19 14 1 6 0.95 0.30 0.625 35 0.0614 19 15 1 5 0.95 0.25 0.600 36 0.0575 19 16 1 4 0.95 0.20 0.575 37 0.0551 19 17 1 3 0.95 0.15 0.550 38 0.0507 19 18 1 2 0.95 0.10 0.525 39 0.0374 19 19 1 1 0.95 0.05 0.500 40 0.0357 20 19 0 1 1.00 0.05 0.525 41 0.0238 20 20 0 0 1.00 0.00 0.500
Accuracyがもっとも高いところを見つけるには、
max(table$Accuracy)
とします。そうすると、今は、
> max(table$Accuracy) [1] 0.775
となりますので、該当するところをみると、感度70%、特異度85%、正診率77.5%達成できるということがわかりました。
感度と特異度の曲線も簡単に書けます。predictionまで行った後に、次のようにします。
perf <- performance(pred, "sens", "spec")
png("sens-spec-curve.png")
plot(perf)
dev.off()
これで、下図のような感度、特異度の曲線がsens-spec-curve.pngという名前で保存されます。

比較的簡単に求められるので便利です。
Remastersys is a very useful remastering tool though developing has ceased now.
Many reported that they could make Remastersys work on Ubuntu 14.04, but I encountered an error with Xubuntu 14.04.
I found workaround for this error.
Short answer: Comment out the line 245 of “/usr/bin/remastersys” beginning with [ “grep lightdm /etc/X11/default-display-manager” != “” ]
Below is the long answer.
Installation is simple.
$ wget -O- http://www.remastersys.com/ubuntu/remastersys.gpg.key | sudo apt-key add -
Add the line to /etc/apt/sources.list
deb http://www.remastersys.com/ubuntu precise main
Please note that you don’t have to replace “precise” with “trusty.”
$ sudo apt-get update; sudo apt-get install remastersys
“sudo remastersys dist” terminated with error. The below is error message;
$ sudo remastersys dist
Distribution Mode Selected
Enabling remastersys-firstboot
Adding system startup for /etc/init.d/remastersys-firstboot …
/etc/rc0.d/K20remastersys-firstboot -> ../init.d/remastersys-firstboot
……
/etc/rc5.d/S20remastersys-firstboot -> ../init.d/remastersys-firstboot
Checking filesystem type of the Working Folder
/home/remastersys/remastersys is on a ext4 filesystem
Making sure popularity contest is not installed
Installing the Ubiquity GTK frontend
Lightdm not setup properly. You must set your default desktop with lightdm prior to remastering
I examined the script /usr/bin/remastersys with the keyword “Lightdm.” Then I found the description below in the line 245
[ “`grep lightdm /etc/X11/default-display-manager`” != “” ] && [ ! -f /etc/lightdm/lightdm.conf ] && [ ! -f /usr/share/xsessions/ubuntu.desktop ] && echo “Lightdm not setup properly. You must set your default desktop with lightdm prior to remastering” && echo “Lightdm not setup properly. You must set your default desktop with lightdm prior to remastering” >> $WORKDIR/remastersys.log && exit 1
Though this line checks if the file “/etc/lightdm/lightdm.conf” exists, The latest Xubuntu 14.04 doesn’t have that lightdm.conf file in /etc/lightdm.
So I commented out this line, just adding “#” to the beginning of the line.
Then I tried sudo remastersys dist again, with success!
If you want to use Remastersys in Xubuntu 14.04, it’s worth trying it.
Lin4Neuro was updated. The changes of this version were as follows;
Lin4Neuro is still based on Xubuntu 12.04. Now I’m working on Xubuntu 14.04, so hopefully the next version will be based on Xubuntu 14.04.
You can download the latest Lin4Neuro from here.
5人の方の申し込みをいただいたので、開催決定です!
岩手医大の山下典生先生も助っ人で参加してくださることになりました!
※場所が確定しました。秋葉原駅徒歩5分のAP秋葉原です。
※事前登録制です。参加希望の方はコメント欄からご連絡をお願いします。
すぐできるVBMが無事に出版に至りましたが、「本は買ったけれどもやる時間がない」という方もいらっしゃるのではないかと思います。また、「ここはどうなの?」と質問をしたいという方もいらっしゃると思います。
そこで、VBMのミニ勉強会を開催させていただきます。
日時:2014年6月28日(土)15:00-18:00
場所:AP秋葉原
参加費:3000円程度(会議室の実費代のみ。)
持参していただくもの:SPMがセットアップされている自分のパソコン
人数:最小5名〜最大20名
テキスト:「すぐできるVBM」(前もってご準備ください)
内容:実習形式
・VBMを実際に動かしてみる
・自分の解析でわからないことの質疑応答
参加希望の方は、下記コメントにお名前(ニックネーム可)とメールアドレスを書きこんでいただけますでしょうか。
VBMに関心のある方はご検討ください。
友人の山下典生先生が、2014年8月31日に『第1回 IMU脳画像解析セミナー「VBM/ROI解析を極める」』を開催します。
SPMでのVBMだけでなく、3D-SlicerやITK-SNAPも利用するとのこと。とても楽しみです。
早速申し込みしました。
関心のある方はこちらをごらんください。
「すぐできるVBM」に、全脳容積の求め方を記載していませんでした…。
以下の方法で、求めることができます。
必要なものは以下のとおりです。
容積を簡便に求めることができるスクリプトは、Ged Ridgway氏によるget_totals.mです。右クリックで「名前をつけて保存」とし、SPMのディレクトリに保存してください。
Segmentで最初に作られる画像であるc1*.niiとc2*.niiを準備します。ここで*はワイルドカードを意味し、「c1からはじまり、そのあとは何でもOKで、最後は.niiで終わる」という意味です。c1画像は厳密には確率画像なのですが、標準化前のこの画像のボクセル値は、ほぼ容積とイコールであると考えられています。
それでは実際にやっていきましょう。
まずは、灰白質の容積を求めます。Matlabのコマンドウィンドウから以下のようにタイプします。
gmv = get_totals
(gmvは変数名なので、gray_matterでも、gmでもかまいません)
そうすると、ファイルを選択するダイアログがあらわれます。ここで、容積を求めたいc1画像を指定してください。ここでは、ためしに5つのファイルを選びました。そうすると、以下のような表示になると思います。(もちろん、数字は選択した画像で変わります)
gmv =
640.9410
664.4302
617.7647
874.1366
670.1864
次に、白質の容積を求めます。Matlabのコマンドウィンドウから以下のようにタイプします。
wmv = get_totals
同様に、ファイルを選択するダイアログがあらわれますので、容積を求めたいc2画像を指定します。重要なのは、c1画像と同じ順番で選択してください。以下のような表示になると思います。
wmv =
431.9188
463.1192
418.5571
572.9889
488.4050
そうしたら、2つを足して全脳容積を求めましょう。
tbv = gmv + wmv
tbv =
1.0e+03 *
1.0729
1.1275
1.0363
1.4471
1.1586
これは、1.0×10^3(すなわち1000)にそれ以下の数字をかけたものということです。つまり、
tbv =
1072.9
1127.5
1036.3
1447.1
1158.6
ということになります。
これを選択してコピーしても使えますが、ファイルに書き出しましょう。
コンマ区切りファイルというものに出力することができます。
出力ファイル名をtotal_brain_volume.csvとします。
csvwrite('total_brain_volume.csv',tbv)
csvwriteの書式は csvwrite(出力ファイル名, 変数名) となります。
変数tbvの内容をtotal_brain_volume.csvに出力したいので、上記のようになります。
そうすると、ワーキングディレクトリにtotal_barin_volume.csvが生成され、Excelのような表計算ソフトでも、テキストエディタでも開くことができます。
最後に少し応用しましょう。灰白質容積(GMV), 白質容積(WMV), 全脳容積(TBV)をすべてCSVファイルに出力したいとしましょう。
その場合は、まず、すべての情報をひとつの変数volumesにまとめます。
Matlabのコマンドウィンドウから次のようにタイプしてください。
volumes = [gmv wmv tbv]
そうすると下記のようになります。
volumes =
1.0e+03 *
0.6409 0.4319 1.0729
0.6644 0.4631 1.1275
0.6178 0.4186 1.0363
0.8741 0.5730 1.4471
0.6702 0.4884 1.1586
左からgmv, wmv, tbvという順番になっています。
これを、先程と同じようにしてCSVファイルに保存します。ファイル名をvolumes.csvとします。
csvwrite('volumes.csv',volumes)
このようにして、脳容積の情報を保存できます。
臨床では、1人の症例で健常者と比較して容積低下部位を求めたいことがあります。
SPMでどのようにするかというと、シンプルです。
そうすると、Design matrixを作るための設定画面は下図のようになるはずです。

これを普通にEstimateして、Contrast managerで症例の容積が低下している領域を求めたかったら、[-1 1]とすれば大丈夫です。
Twitterで疑問を投げかけてくださった@silverjet_jpさん、ありがとうございました。
画像解析のためには、DICOM画像をNIFTI画像に変換する必要があります。
主に使われるソフトウェアとして、MRI Convertとdcm2niiがあります。
開発者からスクリーンショットを使ってよいという許可をいただきましたので、
MRI Convertおよびdcm2niiの使用方法をPDFで公開します。