fsl-ruby 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fsl-ruby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Simon Rascovsky
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Fsl::Ruby
2
+
3
+ A Ruby wrapper for the FSL neuroimaging suite
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'fsl-ruby'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install fsl-ruby
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/fsl-ruby.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fsl-ruby/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "fsl-ruby"
8
+ gem.version = FSL::VERSION
9
+ gem.authors = ["Simon Rascovsky"]
10
+ gem.email = ["simonmd@gmail.com"]
11
+ gem.description = %q{Ruby Wrapper for the FSL Neuroimaging suite}
12
+ gem.summary = %q{Ruby Wrapper for the FSL Neuroimaging suite}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
data/lib/.DS_Store ADDED
Binary file
data/lib/fsl-ruby.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "fsl-ruby/version"
2
+ require "fsl-ruby/bet"
3
+ require "fsl-ruby/first"
4
+ require "fsl-ruby/stats"
5
+ require "fsl-ruby/utils"
6
+
7
+ module FSL
8
+
9
+ end
@@ -0,0 +1,88 @@
1
+ module FSL
2
+ class BET # bet <input> <output> [options]
3
+
4
+ # Main bet2 options:
5
+ # -o generate brain surface outline overlaid onto original image
6
+ # -m generate binary brain mask
7
+ # -s generate rough skull image (not as clean as what betsurf generates)
8
+ # -n don't generate the default brain image output
9
+ # -f <f> fractional intensity threshold (0->1); default=0.5; smaller values give larger brain outline estimates
10
+ # -g <g> vertical gradient in fractional intensity threshold (-1->1); default=0; positive values give larger brain outline at bottom, smaller at top
11
+ # -r <r> head radius (mm not voxels); initial surface sphere is set to half of this
12
+ # -c < x y z> centre-of-gravity (voxels not mm) of initial mesh surface.
13
+ # -t apply thresholding to segmented brain image and mask
14
+ # -e generates brain surface as mesh in .vtk format.
15
+ @@command_path = '/usr/local/fsl/bin/bet'
16
+
17
+ def self.command_path=(path)
18
+ @@command_path = path
19
+ end
20
+
21
+ def self.command_path
22
+ @@command_path
23
+ end
24
+
25
+
26
+ @@options_map = { outline: '-o',
27
+ mask: '-m',
28
+ skull: '-s',
29
+ no_output: '-n',
30
+ fi_threshold: '-f',
31
+ v_gradient: '-g',
32
+ radius: '-r',
33
+ centre: '-c',
34
+ thresholding: '-t',
35
+ mesh: '-e'
36
+ }
37
+
38
+ def initialize(input_file, output_dir, opt = {})
39
+ @input_file = input_file
40
+ @basename = File.basename(input_file, '.nii.gz')
41
+ @output_dir = output_dir
42
+ @opt = opt
43
+ end
44
+
45
+ def self.options_map
46
+ @@options_map
47
+ end
48
+
49
+ def map_vals(val)
50
+ if val == true || val == false
51
+ ''
52
+ else
53
+ val.to_s
54
+ end
55
+ end
56
+
57
+ def map_options(opt ={})
58
+ opt.inject({}) { |h, (k, v)| h[k] = (self.class.options_map[k] + ' ' + map_vals(v)); h }
59
+ end
60
+
61
+ def argument_list
62
+ map_options(@opt).collect {|k,v| v}.join(' ')
63
+ end
64
+
65
+ def command
66
+ command_str = "#{self.class.command_path} #{@input_file} #{@output_dir}/#{@basename}_brain #{argument_list}"
67
+ puts "Running BET with command: #{command_str}..."
68
+ result = `#{command_str}`
69
+ exit_code = $?
70
+ case exit_code
71
+ when 0
72
+ puts "Done running BET."
73
+ return result
74
+ else
75
+ puts "An error ocurred while running BET"
76
+ # exit_error = Dcm2nii::Runner::UnexpectedExitError.new
77
+ # exit_error.exit_code = exit_code
78
+ # raise exit_error
79
+ # end
80
+ end
81
+ end
82
+
83
+ def get_result
84
+ return `find #{@output_dir} -name *_brain.nii*`.chomp
85
+ end
86
+
87
+ end
88
+ end
@@ -0,0 +1,102 @@
1
+ module FSL
2
+ class FIRST
3
+
4
+ # def first(bet_file, id, tmp_dir)
5
+ # `run_first_all -i #{bet_file} -b -s L_Hipp,R_Hipp -o #{tmp_dir}/#{id}`
6
+ # first_files = {}
7
+ # first_files[:origsegs] = `find #{tmp_dir} -name *origsegs.nii*`.chomp
8
+ # first_files[:firstseg] = `find #{tmp_dir} -name *firstseg.nii*`.chomp
9
+ # return first_files
10
+ # end
11
+
12
+ # Usage: run_first_all [options] -i <input_image> -o <output_image>
13
+
14
+ # Optional arguments:
15
+ # -m <method> : method must be one of auto, fast, none or a (numerical) threshold value
16
+ # -b : input is already brain extracted
17
+ # -s <name> : run only on one specified structure (e.g. L_Hipp) or a comma separated list (no spaces)
18
+ # -a <img2std.mat> : use affine matrix (do not re-run registration)
19
+ # -3 : use 3-stage affine registration (only currently for hippocampus)
20
+ # -d : do not cleanup image output files (useful for debugging)
21
+ # -v : verbose output
22
+ # -h : display this help message
23
+
24
+ # e.g.: run_first_all -i im1 -o output_name
25
+
26
+ @@command_path = '/usr/local/fsl/bin/run_first_all'
27
+
28
+ def self.command_path=(path)
29
+ @@command_path = path
30
+ end
31
+
32
+ def self.command_path
33
+ @@command_path
34
+ end
35
+
36
+
37
+ @@options_map = {
38
+ method: '-m',
39
+ already_bet: '-b',
40
+ structure: '-s',
41
+ affine: '-a',
42
+ threestage: '-3',
43
+ no_cleanup: '-d',
44
+ verbose: '-v',
45
+ help: '-h'
46
+ }
47
+
48
+ def initialize(input_file, output_file, opt = {})
49
+ @input_file = input_file
50
+ @basename = File.basename(input_file, '.nii.gz')
51
+ @output_file = output_file
52
+ @output_dir = File.dirname(output_file)
53
+ @opt = opt
54
+ end
55
+
56
+ def self.options_map
57
+ @@options_map
58
+ end
59
+
60
+ def map_vals(val)
61
+ if val == true || val == false
62
+ ''
63
+ else
64
+ val.to_s
65
+ end
66
+ end
67
+
68
+ def map_options(opt ={})
69
+ opt.inject({}) { |h, (k, v)| h[k] = (self.class.options_map[k] + ' ' + map_vals(v)); h }
70
+ end
71
+
72
+ def argument_list
73
+ map_options(@opt).collect {|k,v| v}.join(' ')
74
+ end
75
+
76
+ def command
77
+ command_str = "#{self.class.command_path} #{argument_list} -i #{@input_file} -o #{@output_file}"
78
+ puts "Running FIRST with command: #{command_str}..."
79
+ result = `#{command_str}`
80
+ exit_code = $?
81
+ case exit_code
82
+ when 0
83
+ puts "Done running FIRST."
84
+ return result
85
+ else
86
+ puts "An error ocurred while running FIRST"
87
+ # exit_error = Dcm2nii::Runner::UnexpectedExitError.new
88
+ # exit_error.exit_code = exit_code
89
+ # raise exit_error
90
+ # end
91
+ end
92
+ end
93
+
94
+ def get_result
95
+ first_files = {}
96
+ first_files[:origsegs] = `find #{@output_dir} -name *origsegs.nii*`.chomp
97
+ first_files[:firstseg] = `find #{@output_dir} -name *firstseg.nii*`.chomp
98
+ return first_files
99
+ end
100
+
101
+ end
102
+ end
@@ -0,0 +1,124 @@
1
+ module FSL
2
+ class Stats
3
+
4
+ # Usage: fslstats [-t] <input> [options]
5
+
6
+ # -t will give a separate output line for each 3D volume of a 4D timeseries
7
+ # Note - options are applied in order, e.g. -M -l 10 -M will report the non-zero mean, apply a threshold and then report the new nonzero mean
8
+
9
+ # -l <lthresh> : set lower threshold
10
+ # -u <uthresh> : set upper threshold
11
+ # -r : output <robust min intensity> <robust max intensity>
12
+ # -R : output <min intensity> <max intensity>
13
+ # -e : output mean entropy ; mean(-i*ln(i))
14
+ # -E : output mean entropy (of nonzero voxels)
15
+ # -v : output <voxels> <volume>
16
+ # -V : output <voxels> <volume> (for nonzero voxels)
17
+ # -m : output mean
18
+ # -M : output mean (for nonzero voxels)
19
+ # -s : output standard deviation
20
+ # -S : output standard deviation (for nonzero voxels)
21
+ # -w : output smallest ROI <xmin> <xsize> <ymin> <ysize> <zmin> <zsize> <tmin> <tsize> containing nonzero voxels
22
+ # -x : output co-ordinates of maximum voxel
23
+ # -X : output co-ordinates of minimum voxel
24
+ # -c : output centre-of-gravity (cog) in mm coordinates
25
+ # -C : output centre-of-gravity (cog) in voxel coordinates
26
+ # -p <n> : output nth percentile (n between 0 and 100)
27
+ # -P <n> : output nth percentile (for nonzero voxels)
28
+ # -a : use absolute values of all image intensities
29
+ # -n : treat NaN or Inf as zero for subsequent stats
30
+ # -k <mask> : use the specified image (filename) for masking - overrides lower and upper thresholds
31
+ # -h <nbins> : output a histogram (for the thresholded/masked voxels only) with nbins
32
+ # -H <nbins> <min> <max> : output a histogram (for the thresholded/masked voxels only) with nbins and histogram limits of min and max
33
+
34
+ # Note - thresholds are not inclusive ie lthresh<allowed<uthresh
35
+
36
+ @@command_path = '/usr/local/fsl/bin/fslstats'
37
+
38
+ def self.command_path=(path)
39
+ @@command_path = path
40
+ end
41
+
42
+ def self.command_path
43
+ @@command_path
44
+ end
45
+
46
+ @@options_map = {
47
+ low_threshold: '-l', # <lthresh> : set lower threshold
48
+ up_threshold: '-u', # <uthresh> : set upper threshold
49
+ robust_intensity: '-r', # output <robust min intensity> <robust max intensity>
50
+ intensity: '-R', # output <min intensity> <max intensity>
51
+ entropy: '-e', # output mean entropy ; mean(-i*ln(i))
52
+ entropy_nonzero: '-E',# output mean entropy (of nonzero voxels)
53
+ voxels: '-v', # output <voxels> <volume>
54
+ voxels_nonzero: '-V', # output <voxels> <volume> (for nonzero voxels)
55
+ mean: '-m', # output mean
56
+ mean_nonzero: '-M',# output mean (for nonzero voxels)
57
+ stdev: '-s', # output standard deviation
58
+ stdev_nonzero: '-S', # output standard deviation (for nonzero voxels)
59
+ smallest_roi: '-w', # output smallest ROI <xmin> <xsize> <ymin> <ysize> <zmin> <zsize> <tmin> <tsize> containing nonzero voxels
60
+ coord_maxvoxel: '-x', # output co-ordinates of maximum voxel
61
+ coord_minvoxel: '-X', # output co-ordinates of minimum voxel
62
+ cog_mm: '-c', # output centre-of-gravity (cog) in mm coordinates
63
+ cog_voxel: '-C', # output centre-of-gravity (cog) in voxel coordinates
64
+ nth_percentile: '-p', # <n> output nth percentile (n between 0 and 100)
65
+ nth_percentage_nonzero: '-P', # <n> output nth percentile (for nonzero voxels)
66
+ abs: '-a', # use absolute values of all image intensities
67
+ nan_as_zero: '-n', # treat NaN or Inf as zero for subsequent stats
68
+ mask: '-k', #<mask> use the specified image (filename) for masking - overrides lower and upper thresholds
69
+ hist: '-h', #<nbins> output a histogram (for the thresholded/masked voxels only) with nbins
70
+ hist_minmax: '-H' # <nbins> <min> <max> : output a histogram (for the thresholded/masked voxels only) with nbins and histogram limits of min and max
71
+ }
72
+
73
+ def initialize(input_file, separate=false, opt = {})
74
+ @input_file = input_file
75
+ @basename = File.basename(input_file, '.nii.gz')
76
+ @separate = separate
77
+ @opt = opt
78
+ end
79
+
80
+ def self.options_map
81
+ @@options_map
82
+ end
83
+
84
+ def map_vals(val)
85
+ if val == true || val == false
86
+ ''
87
+ else
88
+ val.to_s
89
+ end
90
+ end
91
+
92
+ def map_options(opt ={})
93
+ opt.inject({}) { |h, (k, v)| h[k] = (self.class.options_map[k] + ' ' + map_vals(v)); h }
94
+ end
95
+
96
+ def argument_list
97
+ map_options(@opt).collect {|k,v| v}.join(' ')
98
+ end
99
+
100
+ def command
101
+ sf = @separate ? '-t' : ''
102
+ command_str = "#{self.class.command_path} #{sf} #{@input_file} #{argument_list}"
103
+ puts "Running FSLSTATS with command: #{command_str}..."
104
+ result = `#{command_str}`
105
+ exit_code = $?
106
+ case exit_code
107
+ when 0
108
+ puts "Done running FSLSTATS."
109
+ return result
110
+ else
111
+ puts "An error ocurred while running FSLSTATS"
112
+ # exit_error = Dcm2nii::Runner::UnexpectedExitError.new
113
+ # exit_error.exit_code = exit_code
114
+ # raise exit_error
115
+ # end
116
+ end
117
+ end
118
+
119
+ def get_result
120
+ # return `find #{@output_dir} -name *_brain.nii*`.chomp
121
+ end
122
+
123
+ end
124
+ end
@@ -0,0 +1,30 @@
1
+ module FSL
2
+ class UTILS # Misc FSL utilities grouped under FSLUTILS
3
+
4
+ @@FSLHOME= '/usr/local/fsl/bin/'
5
+
6
+ def self.roi(input, output, xmin, xsize, ymin, ysize, zmin, zsize)
7
+ # Usage: fslroi <input> <output> <xmin> <xsize> <ymin> <ysize> <zmin> <zsize>
8
+ # fslroi <input> <output> <tmin> <tsize>
9
+ # fslroi <input> <output> <xmin> <xsize> <ymin> <ysize> <zmin> <zsize> <tmin> <tsize>
10
+ # Note: indexing (in both time and space) starts with 0 not 1! Inputting -1 for a size will set it to the full image extent for that dimension.
11
+ command_str = "#{@@FSLHOME}/fslroi #{input} #{output} #{xmin.to_s} #{xsize.to_s} #{ymin.to_s} #{ysize.to_s} #{zmin.to_s} #{zsize.to_s}"
12
+ puts "Running FSLROI with command #{command_str}..."
13
+ result = `#{command_str}`
14
+ puts "Done running FSLROI."
15
+ end
16
+
17
+ def self.swapdim(input,axes,output)
18
+ # Usage: fslswapdim <input> <a> <b> <c> [output]
19
+
20
+ # where a,b,c represent the new x,y,z axes in terms of the old axes. They can take values of -x,x,y,-y,z,-z
21
+ # or RL,LR,AP,PA,SI,IS (in the case of nifti inputs) e.g. fslswapdim invol y x -z outvol
22
+ # or fslswapdim invol RL PA IS outvol where the latter will convert to axial slicing (to match the avg152 images)
23
+ command_str = "#{@@FSLHOME}/fslswapdim #{input} #{axes} #{output}"
24
+ puts "Running FSLSWAPDIM with command #{command_str}..."
25
+ result = `#{command_str}`
26
+ puts "Done running FSLSWAPDIM."
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module FSL
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fsl-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Simon Rascovsky
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-21 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Ruby Wrapper for the FSL Neuroimaging suite
15
+ email:
16
+ - simonmd@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .DS_Store
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - fsl-ruby.gemspec
28
+ - lib/.DS_Store
29
+ - lib/fsl-ruby.rb
30
+ - lib/fsl-ruby/bet.rb
31
+ - lib/fsl-ruby/first.rb
32
+ - lib/fsl-ruby/stats.rb
33
+ - lib/fsl-ruby/utils.rb
34
+ - lib/fsl-ruby/version.rb
35
+ homepage: ''
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.23
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Ruby Wrapper for the FSL Neuroimaging suite
59
+ test_files: []