nifti 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,6 +1,21 @@
1
- pkg/*
2
1
  *.gem
2
+ *.rbc
3
3
  .bundle
4
- ._*
5
- doc/*
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
18
+ .DS_Store
19
+ .ruby-version
20
+ .ruby-gemset
6
21
 
data/CHANGELOG CHANGED
@@ -1,3 +1,13 @@
1
+ = 0.0.2
2
+
3
+ === (Updates from Rafael Reggiani Manzo) March, 2014
4
+
5
+ * Add Gzip Read/Write Support
6
+ * Ruby 2.0 Compatibility
7
+ * New NImage convenience class to index into NArray Image Data
8
+ * Cleaned up the gemspec
9
+
10
+
1
11
  = 0.0.1
2
12
 
3
13
  === (In Development) February, 2011
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -3,10 +3,10 @@ RUBY NIfTI
3
3
 
4
4
  Ruby NIfTI is a pure-ruby library for handling NIfTI data in Ruby. NIfTI
5
5
  [(Neuroimaging Informatics Technology Initiative)](http://nifti.nimh.nih.gov/)
6
- is an image format designed primarily for storing and analyzing MRI & PET
6
+ is an image format designed primarily for the storage and analysis of MRI & PET
7
7
  imaging data.
8
8
 
9
- Ruby NIfTI currently only supports basic access to NIfTI files, including
9
+ Ruby NIfTI currently supports basic access to NIfTI files, including
10
10
  basic and extended header information, as well as image information. It
11
11
  doesn't attempt to touch the image data (rotate it, etc.), but it does provide
12
12
  access to qform and sform orientation matrices. Nonetheless it does provide a
@@ -24,21 +24,36 @@ INSTALLATION
24
24
  BASIC USAGE
25
25
  -----------
26
26
 
27
+ Initialize the library:
28
+
27
29
  require "nifti"
28
- # Read file:
30
+
31
+ Read file:
32
+
29
33
  obj = NIFTI::NObject.new("some_file.nii")
30
- # Display some key information about the file:
34
+
35
+ Display some key information about the file:
36
+
31
37
  puts obj.header['sform_code_descr']
32
38
  => "NIFTI_XFORM_SCANNER_ANAT"
33
- # Retrieve the pixel data in a Ruby Array:
39
+
40
+ Retrieve the pixel data in a Ruby Array:
41
+
34
42
  image = obj.get_image
35
- # Load the pixel data to an NArray image object and display it on the screen:
43
+
44
+ Load the pixel data to an NArray image object and display it on the screen:
45
+
36
46
  image = obj.get_image_narray
37
47
 
48
+ Or load the pixel data into an NImage (for easier indexing):
49
+
50
+ image = obj.get_nimage
51
+
52
+
38
53
  LIMITATIONS
39
54
  -----------
40
55
 
41
- There are plenty of NIfTI libraries around (the canonical
56
+ There are several good NIfTI libraries around (the canonical
42
57
  [nifticlib](http://niftilib.sourceforge.net/), which includes c, python and
43
58
  matlab interfaces, and Matlab interfaces in the Mathworks image processing
44
59
  toolbox (IPT) and [Toolbox for
@@ -48,12 +63,13 @@ by Jimmy Shen.
48
63
  This library does not intend to replace them, but rather to provide a few ruby
49
64
  convenience methods for quickly accessing imaging data from ruby without
50
65
  having to call out to external programs, as well as write custom extensions to
51
- the NIfTI header. At current, it doesn't do much more than byte parsing and
52
- providing access to the header, and its quite slow when compared to existing
53
- libraries. However, [narray](http://narray.rubyforge.org/) is picking up and
54
- its possible that people would want to have some easy way to access NIfTI info
55
- using custom ruby libraries.
66
+ the NIfTI header.
67
+
68
+ GETTING HELP
69
+ ------------
56
70
 
71
+ Feel free to use the [github issue tracker](https://github.com/brainmap/nifti/issues)
72
+ to post any questions, or email me directly.
57
73
 
58
74
  CREDIT
59
75
  ------
@@ -67,9 +83,8 @@ wouldn't exist without his examples.
67
83
  RESOURCES
68
84
  ---------
69
85
 
70
- * [Official home page](http://brainmap.wisc.edu/pages/10-RUBY-NIFTI)
71
- * [Development / Source code repository](https://github.com/brainmap/ruby-nifti)
72
- * [Documentation](http://rdoc.info/github/brainmap/ruby-nifti/master/frames)
86
+ * [Development / Source code](https://github.com/brainmap/nifti)
87
+ * [Documentation](http://rdoc.info/github/brainmap/nifti/master/frames)
73
88
 
74
89
 
75
90
  Examples
@@ -105,22 +120,3 @@ want to take find the mean and std dev of the entire image.
105
120
 
106
121
  If you don't have narray installed, you could still use obj.image as a ruby
107
122
  array, but you'd have to collect the summary stats yourself.
108
-
109
-
110
- COPYRIGHT
111
- ---------
112
-
113
- Copyright 2011 Erik Kastman
114
-
115
- This program is free software: you can redistribute it and/or modify
116
- it under the terms of the GNU General Public License as published by
117
- the Free Software Foundation, either version 3 of the License, or
118
- (at your option) any later version.
119
-
120
- This program is distributed in the hope that it will be useful,
121
- but WITHOUT ANY WARRANTY; without even the implied warranty of
122
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
123
- GNU General Public License for more details.
124
-
125
- You should have received a copy of the GNU General Public License
126
- along with this program. If not, see http://www.gnu.org/licenses/ .
data/Rakefile CHANGED
@@ -1,2 +1,18 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+
4
+ begin
5
+
6
+ require 'rspec'
7
+ require 'rspec/core/rake_task'
8
+
9
+
10
+ desc "Run RSpec code examples"
11
+ RSpec::Core::RakeTask.new(:spec) do |t|
12
+ t.rcov = true
13
+ t.rcov_opts = %w{--exclude osx\/objc,gems\/,spec\/,features\/}
14
+ end
15
+
16
+ rescue LoadError => e
17
+ puts e
18
+ end
@@ -0,0 +1,18 @@
1
+ Feature: Image data interface with NImage
2
+ In Order to able manipulate the image data
3
+ As a developer
4
+ I want to access it as a multidimensional array
5
+
6
+ @clean_saved_files
7
+ Scenario: reading and writing
8
+ Given I have a NObject loaded
9
+ And I have the NImage representation of the image
10
+ When I get the value at ("64", "64", "35", "3")
11
+ # I know the dataset, so we know this value
12
+ Then the value should be equals to "2.4997341632843018"
13
+ When I set the value at ("64", "64", "35", "3") to "2.0"
14
+ And I save the NImage as new file
15
+ And I load the new file
16
+ And I have the NImage representation of the image
17
+ And I get the value at ("64", "64", "35", "3")
18
+ Then the value should be equals to "2.0"
@@ -0,0 +1,33 @@
1
+ Given(/^I have a NObject loaded$/) do
2
+ @n_object = NIFTI::NObject.new(File.join(File.dirname(__FILE__), '..', 'support', 'fixtures', 'brain_dti.nii.gz'))
3
+ end
4
+
5
+ Given(/^I have the NImage representation of the image$/) do
6
+ @n_image = @n_object.get_nimage
7
+ end
8
+
9
+ When(/^I get the value at \("(.*?)", "(.*?)", "(.*?)", "(.*?)"\)$/) do |x, y, z, t|
10
+ @value = @n_image[x.to_i][y.to_i][z.to_i][t.to_i]
11
+ end
12
+
13
+ When(/^I set the value at \("(.*?)", "(.*?)", "(.*?)", "(.*?)"\) to "(.*?)"$/) do |x, y, z, t, val|
14
+ @n_image[x.to_i][y.to_i][z.to_i][t.to_i] = val.to_f
15
+ end
16
+
17
+ When(/^I save the NImage as new file$/) do
18
+ obj = NIFTI::NObject.new
19
+ obj.header = @n_object.header
20
+ obj.extended_header = @n_object.extended_header
21
+ obj.image = @n_image.array_image
22
+
23
+ writer = NIFTI::NWrite.new(obj, "test.nii.gz")
24
+ writer.write
25
+ end
26
+
27
+ When(/^I load the new file$/) do
28
+ @n_object = NIFTI::NObject.new('test.nii.gz')
29
+ end
30
+
31
+ Then(/^the value should be equals to "(.*?)"$/) do |val|
32
+ @value.should eq(val.to_f)
33
+ end
@@ -0,0 +1,11 @@
1
+ # SimpleCov for test coverage report
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter "/spec/"
5
+ add_filter "/features/"
6
+
7
+ coverage_dir 'coverage/cucumber'
8
+ end
9
+
10
+ # The gem itself
11
+ require 'nifti'
@@ -0,0 +1,3 @@
1
+ After('@clean_saved_files') do
2
+ File.delete('test.nii.gz') if File.exist?('test.nii.gz')
3
+ end
@@ -12,6 +12,7 @@ module NIFTI; end
12
12
  require 'nifti/n_object'
13
13
  require 'nifti/n_read'
14
14
  require 'nifti/n_write'
15
+ require 'nifti/n_image'
15
16
  require 'nifti/stream'
16
17
  require 'nifti/constants'
17
18
 
@@ -0,0 +1,110 @@
1
+ module NIFTI
2
+ # The NImage class is a container for the "raw" NIFTI image data making easier to deal with them.
3
+
4
+ class NImage
5
+ attr_reader :array_image, :dim, :previous_indexes
6
+
7
+ # Creates an NImage instance.
8
+ #
9
+ # The NImages instance provides a user friendly interface to the NIFTI Image
10
+ # A NImage is typically built by NObject instance
11
+ #
12
+ # === Parameters
13
+ #
14
+ # * <tt>array_image</tt> -- The NIFTI image contained on and one dimensional array
15
+ # * <tt>dim</tt> -- The dimensions array from the NIFTI header.
16
+ #
17
+ # === Examples
18
+ #
19
+ # # Creates an NImage to deal with an 9 position array that represents a 3x3 matrix
20
+ # img = Nimage.new(Array.new(9,0.0), [2,3,3])
21
+ #
22
+ def initialize(array_image, dim, previous_indexes=[])
23
+ @array_image = array_image
24
+ @dim = dim
25
+ @previous_indexes = previous_indexes
26
+ end
27
+
28
+ # Retrieves an element or partition of the dataset
29
+ #
30
+ # === Parameters
31
+ #
32
+ # * <tt>index</tt> -- The desired index on the dataset
33
+ #
34
+ # === Options
35
+ #
36
+ # === Examples
37
+ #
38
+ # img[0][0]
39
+ # img[0][0..1]
40
+ #
41
+ def [](index)
42
+ # Dealing with Ranges is useful when the image represents a tensor
43
+ if (index.is_a?(Fixnum) && index >= self.shape[0]) || (index.is_a?(Range) && index.last >= self.shape[0])
44
+ raise IndexError.new("Index over bounds")
45
+ elsif self.shape.count == 1
46
+ if index.is_a?(Range)
47
+ value = []
48
+ index.each { |i| value << self.array_image[get_index_value(i)] }
49
+ value
50
+ else
51
+ self.array_image[get_index_value(index)]
52
+ end
53
+ else
54
+ NImage.new(self.array_image, self.dim, self.previous_indexes.clone << index)
55
+ end
56
+ end
57
+
58
+ # Set the value for an element of the dataset
59
+ #
60
+ # === Parameters
61
+ #
62
+ # * <tt>index</tt> -- The desired index on the dataset
63
+ # * <tt>value</tt> -- The value that the will be set
64
+ #
65
+ # === Options
66
+ #
67
+ # === Examples
68
+ #
69
+ # img[0][0] = 1.0
70
+ #
71
+ def []=(index,value)
72
+ if self.shape.count != 1 or index >= self.shape[0]
73
+ raise IndexError.new("You can only set values for array values")
74
+ else
75
+ @array_image[get_index_value(index)] = value
76
+ end
77
+ end
78
+
79
+ # Dataset shape
80
+ #
81
+ # === Examples
82
+ #
83
+ # img.shape
84
+ #
85
+ def shape
86
+ start_index = 1
87
+ self.previous_indexes.each {start_index += 1}
88
+ self.dim[start_index..self.dim[0]]
89
+ end
90
+
91
+ private
92
+
93
+ def get_index_value(current_index)
94
+ reverse_dim = self.dim.take(self.dim[0] + 1).reverse
95
+ step = (reverse_dim.inject(:*)/self.dim[0])/reverse_dim[0]
96
+
97
+ index_value = current_index*step
98
+ step /= reverse_dim[1]
99
+ dim_index = 1
100
+
101
+ self.previous_indexes.reverse_each do |previous_index|
102
+ index_value += step*previous_index
103
+ dim_index += 1
104
+ step /= reverse_dim[dim_index] if dim_index < (reverse_dim.count - 1)
105
+ end
106
+
107
+ index_value
108
+ end
109
+ end
110
+ end