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.

RでのROC解析:ROCRパッケージを使ったROC曲線とAUCの求め方

研究でROC解析を行う必要があり、Rでどうやったらできるのか調べてみました。
そうしたところ、ROCRというパッケージが公開されており、比較的簡単にROC解析を行い、グラフを作成できることがわかりました。

  • ROCRパッケージのインストール(Ubuntu)
  • 既にRはインストールされているとします。Ubuntuの場合、ROCRパッケージはapt経由で簡単に入手できます。

    $ sudo apt-get install r-cran-rocr
    

    これでインストール完了です。

  • 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の起動
  • ROCRはRを立ち上げた後に、library(ROCR)で起動できます。

    $ R
    > library(ROCR)
    
  • データの読み込み
  • 先ほどのroc_data.txtをrocdataという変数に読み込みます。変数名は何でもいいのですが、ここではそうします。
    read.tableという関数で表を読み込めるので、それを使います。

    rocdata <- read.table("roc_data.txt")
    
  • ROCR ステップ1: prediction
  • 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
  • ROCRのステップ2はperformanceです。ここでは、感度、すなわち真陽性率 (TP/(TP+FN)で定義)と、1-特異度、すなわち偽陽性率(FP/(FP+TN)で定義)を求めます。以下のようにタイプします。

    perf <- performance(pred, "tpr", "fpr")
    

    ここでtprはtrue positive rateを、fprはfalse positive rateを意味します。

  • ROCR ステップ3: グラフの描画
  • それでは、ROC曲線を描きます。非常に簡単です。

    plot(perf)
    

    そうすると、次のようなグラフが現れるはずです。

    roc-curve

  • グラフの保存
  • グラフをPNG形式で保存するには、次のように行うことで、roc-curve.pngという名前でワーキングディレクトリに保存されます。

    png("roc-curve.png")
    plot(perf)
    dev.off()
    
  • AUCの算出
  • 先ほどの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という名前で保存されます。

    sens-spec-curve

比較的簡単に求められるので便利です。

Workaround for Remastersys on Xubuntu 14.04

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
  • Installation is simple.

    1. Import GPG key
    2. $ wget -O- http://www.remastersys.com/ubuntu/remastersys.gpg.key | sudo apt-key add -
      
    3. Add sources
    4. 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.”

    5. Installation
    6. $ sudo apt-get update; sudo apt-get install remastersys
      
  • Error with “remastersys dist”
  • “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.

Update of Lin4Neuro (12 Jun 2014)

Lin4Neuro was updated. The changes of this version were as follows;

  • Kernel was updated to 3.13.0-29-generic.
  • ITK-SNAP was updated to 3.0.0.
  • MITK was updated to 2014.03.00.
  • MITK Diffusion was updated to 2014.03.
  • All security updates were applied.

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.

VBMミニ勉強会@秋葉原 6月28日15:00-18:00

5人の方の申し込みをいただいたので、開催決定です!
岩手医大の山下典生先生も助っ人で参加してくださることになりました!

※場所が確定しました。秋葉原駅徒歩5分のAP秋葉原です。
事前登録制です。参加希望の方はコメント欄からご連絡をお願いします。

すぐできるVBMが無事に出版に至りましたが、「本は買ったけれどもやる時間がない」という方もいらっしゃるのではないかと思います。また、「ここはどうなの?」と質問をしたいという方もいらっしゃると思います。

そこで、VBMのミニ勉強会を開催させていただきます。

日時:2014年6月28日(土)15:00-18:00
場所:AP秋葉原
参加費:3000円程度(会議室の実費代のみ。)
持参していただくもの:SPMがセットアップされている自分のパソコン
人数:最小5名〜最大20名
テキスト:「すぐできるVBM」(前もってご準備ください)
内容:実習形式
 ・VBMを実際に動かしてみる
 ・自分の解析でわからないことの質疑応答

参加希望の方は、下記コメントにお名前(ニックネーム可)とメールアドレスを書きこんでいただけますでしょうか。
VBMに関心のある方はご検討ください。