ms-msrun 0.2.1 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.rdoc +8 -4
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/lib/ms/msrun.rb +2 -2
- data/lib/ms/msrun/index.rb +2 -2
- data/lib/ms/msrun/nokogiri/mzxml.rb +4 -3
- data/spec/ms/msrun_spec.rb +1 -1
- data/spec/ms/spectrum/compare_spec.rb +4 -5
- data/spec/ms/spectrum/filter_spec.rb +2 -3
- metadata +47 -33
- data/lib/lmat.rb +0 -267
- data/spec/lmat_spec.rb +0 -105
data/.gitignore
CHANGED
data/README.rdoc
CHANGED
@@ -4,8 +4,7 @@ A library for working with LC/MS runs.
|
|
4
4
|
|
5
5
|
== Examples
|
6
6
|
|
7
|
-
The following example
|
8
|
-
in the future)
|
7
|
+
The following example works on any version of *mzXML* (doesn't support mzXML 3.1 compression just yet).
|
9
8
|
|
10
9
|
require "ms/msrun"
|
11
10
|
|
@@ -61,10 +60,11 @@ in the future)
|
|
61
60
|
# Quicker way to get at scans:
|
62
61
|
Ms::Msrun.foreach("file.mzXML") {|scan| scan <do something> }
|
63
62
|
|
63
|
+
mzData support to be re-included in the future and mzML support planned.
|
64
|
+
|
64
65
|
== Features
|
65
66
|
|
66
|
-
[<b>Fast</b>] uses Nokogiri and a dash of regular expressions to achieve very
|
67
|
-
fast random access of scans and full access.
|
67
|
+
[<b>Fast</b>] uses Nokogiri and a dash of regular expressions to achieve very fast random access of scans and full access.
|
68
68
|
[<b>Unified</b>] one interface for all formats.
|
69
69
|
[<b>Lazy evaluation at scan and spectrum level</b>] Scans are only read from IO when requested. Spectra are also decoded only when explicitly accessed.
|
70
70
|
|
@@ -75,3 +75,7 @@ fast random access of scans and full access.
|
|
75
75
|
== Copying
|
76
76
|
|
77
77
|
See LICENSE
|
78
|
+
|
79
|
+
== See also
|
80
|
+
|
81
|
+
mzml[http://github.com/delagoya/mzml]
|
data/Rakefile
CHANGED
@@ -12,11 +12,11 @@ gemspec = Gem::Specification.new do |s|
|
|
12
12
|
s.name = NAME
|
13
13
|
s.authors = ["John T. Prince"]
|
14
14
|
s.email = "jtprince@gmail.com"
|
15
|
-
s.homepage = "http://jtprince.github.com/" + NAME + "/"
|
15
|
+
s.homepage = "http://jtprince.github.com/" + NAME + "/rdoc"
|
16
16
|
s.summary = "an mspire library for working with LC/MS runs (mzxml, mzData, mzML)"
|
17
17
|
s.description = 'A library for working with LC/MS runs. Part of mspire. Has parsers for mzXML v1, 2, and 3, mzData (currently broken) and mzML (planned). Can convert to commonly desired search output (such as mgf). Fast random access of scans, and fast reading of the entire file.'
|
18
18
|
s.rubyforge_project = 'mspire'
|
19
|
-
s.add_dependency 'ms-core'
|
19
|
+
s.add_dependency 'ms-core', ">= 0.0.3"
|
20
20
|
s.add_dependency 'nokogiri'
|
21
21
|
s.add_dependency 'narray'
|
22
22
|
s.add_development_dependency("spec-more")
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.4
|
data/lib/ms/msrun.rb
CHANGED
data/lib/ms/msrun/index.rb
CHANGED
@@ -35,8 +35,8 @@ class Ms::Msrun::Index < Array
|
|
35
35
|
def initialize(filename_or_io)
|
36
36
|
(ft, version) = Ms::Msrun.filetype_and_version(filename_or_io)
|
37
37
|
tag = case ft
|
38
|
-
when :mzml
|
39
|
-
when :mzxml
|
38
|
+
when :mzml ; MZML_INDEX_TAG
|
39
|
+
when :mzxml ; MZXML_INDEX_TAG
|
40
40
|
end
|
41
41
|
fn =
|
42
42
|
if filename_or_io.is_a? String
|
@@ -7,7 +7,7 @@ require 'ms/data'
|
|
7
7
|
require 'ms/data/lazy_io'
|
8
8
|
require 'ms/precursor'
|
9
9
|
require 'ms/mzxml'
|
10
|
-
|
10
|
+
require 'narray'
|
11
11
|
|
12
12
|
class Ms::Msrun::Nokogiri::Mzxml
|
13
13
|
NetworkOrder = true
|
@@ -75,7 +75,6 @@ class Ms::Msrun::Nokogiri::Mzxml
|
|
75
75
|
opts = {:spectrum => true, :precursor => true}.merge(options)
|
76
76
|
start_io_pos = @io.pos
|
77
77
|
@io.pos = start_byte
|
78
|
-
|
79
78
|
# read in the data keeping track of peaks start and stop
|
80
79
|
string = ""
|
81
80
|
if opts[:spectrum]
|
@@ -132,7 +131,9 @@ class Ms::Msrun::Nokogiri::Mzxml
|
|
132
131
|
# data is stored as the base64 string until we actually try to access
|
133
132
|
# it! At that point the string is decoded and knows it is interleaved
|
134
133
|
# data. So, no spectrum is actually decoded unless it is accessed!
|
135
|
-
|
134
|
+
compression_type = peaks_n['compressionType']
|
135
|
+
lazy_string = Ms::Data::LazyString.new(peaks_n.text, Ms::Data::LazyIO.unpack_code(peaks_n['precision'].to_i, NetworkOrder), compression_type == 'zlib')
|
136
|
+
peaks_data = Ms::Data.new_interleaved(lazy_string)
|
136
137
|
spec = Ms::Spectrum.new(peaks_data)
|
137
138
|
scan[8] = Ms::Spectrum.new(peaks_data)
|
138
139
|
end
|
data/spec/ms/msrun_spec.rb
CHANGED
@@ -10,16 +10,15 @@ class Array
|
|
10
10
|
end
|
11
11
|
|
12
12
|
module CompareSpec
|
13
|
-
include Ms
|
14
13
|
|
15
14
|
describe 'comparison of spectra' do
|
16
15
|
|
17
16
|
before do
|
18
|
-
@a = Spectrum.new([[0,2,3,4], [5,6,7,8]])
|
19
|
-
@b = Spectrum.new([[0, 1.5, 3.5, 5.5], [9,10,11,12]])
|
17
|
+
@a = Ms::Spectrum.new([[0,2,3,4], [5,6,7,8]])
|
18
|
+
@b = Ms::Spectrum.new([[0, 1.5, 3.5, 5.5], [9,10,11,12]])
|
20
19
|
|
21
|
-
@c = Spectrum.new([[0, 1], [8,9]])
|
22
|
-
@d = Spectrum.new([[0.6, 0.75], [10,11]])
|
20
|
+
@c = Ms::Spectrum.new([[0, 1], [8,9]])
|
21
|
+
@d = Ms::Spectrum.new([[0.6, 0.75], [10,11]])
|
23
22
|
end
|
24
23
|
|
25
24
|
it 'compares spectra' do
|
@@ -11,12 +11,11 @@ class Array
|
|
11
11
|
end
|
12
12
|
|
13
13
|
class FilterSpec
|
14
|
-
include Ms
|
15
14
|
|
16
15
|
describe 'filtering spectra' do
|
17
16
|
before do
|
18
|
-
@a = Spectrum.new([[0,5,10, 15,16,17,18, 20.1], [0,1,2, 3,8,10,4, 0]])
|
19
|
-
@null = Spectrum.new([[],[]])
|
17
|
+
@a = Ms::Spectrum.new([[0,5,10, 15,16,17,18, 20.1], [0,1,2, 3,8,10,4, 0]])
|
18
|
+
@null = Ms::Spectrum.new([[],[]])
|
20
19
|
end
|
21
20
|
|
22
21
|
it 'filters spectra' do
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ms-msrun
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 4
|
9
|
+
version: 0.2.4
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- John T. Prince
|
@@ -9,55 +14,65 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-06-23 00:00:00 -06:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: ms-core
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
25
|
- - ">="
|
22
26
|
- !ruby/object:Gem::Version
|
23
|
-
|
24
|
-
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 0
|
30
|
+
- 3
|
31
|
+
version: 0.0.3
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
25
34
|
- !ruby/object:Gem::Dependency
|
26
35
|
name: nokogiri
|
27
|
-
|
28
|
-
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
38
|
requirements:
|
31
39
|
- - ">="
|
32
40
|
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
33
43
|
version: "0"
|
34
|
-
|
44
|
+
type: :runtime
|
45
|
+
version_requirements: *id002
|
35
46
|
- !ruby/object:Gem::Dependency
|
36
47
|
name: narray
|
37
|
-
|
38
|
-
|
39
|
-
version_requirements: !ruby/object:Gem::Requirement
|
48
|
+
prerelease: false
|
49
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
40
50
|
requirements:
|
41
51
|
- - ">="
|
42
52
|
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
43
55
|
version: "0"
|
44
|
-
|
56
|
+
type: :runtime
|
57
|
+
version_requirements: *id003
|
45
58
|
- !ruby/object:Gem::Dependency
|
46
59
|
name: spec-more
|
47
|
-
|
48
|
-
|
49
|
-
version_requirements: !ruby/object:Gem::Requirement
|
60
|
+
prerelease: false
|
61
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
50
62
|
requirements:
|
51
63
|
- - ">="
|
52
64
|
- !ruby/object:Gem::Version
|
65
|
+
segments:
|
66
|
+
- 0
|
53
67
|
version: "0"
|
54
|
-
|
68
|
+
type: :development
|
69
|
+
version_requirements: *id004
|
55
70
|
description: A library for working with LC/MS runs. Part of mspire. Has parsers for mzXML v1, 2, and 3, mzData (currently broken) and mzML (planned). Can convert to commonly desired search output (such as mgf). Fast random access of scans, and fast reading of the entire file.
|
56
71
|
email: jtprince@gmail.com
|
57
72
|
executables:
|
58
73
|
- base64_to_array.rb
|
59
|
-
- ms_to_search.rb
|
60
74
|
- ms_to_obiwarp.rb
|
75
|
+
- ms_to_search.rb
|
61
76
|
extensions: []
|
62
77
|
|
63
78
|
extra_rdoc_files:
|
@@ -74,7 +89,6 @@ files:
|
|
74
89
|
- bin/base64_to_array.rb
|
75
90
|
- bin/ms_to_obiwarp.rb
|
76
91
|
- bin/ms_to_search.rb
|
77
|
-
- lib/lmat.rb
|
78
92
|
- lib/ms/msrun.rb
|
79
93
|
- lib/ms/msrun/axml/mzxml.rb
|
80
94
|
- lib/ms/msrun/index.rb
|
@@ -91,7 +105,6 @@ files:
|
|
91
105
|
- lib/ms/scan.rb
|
92
106
|
- lib/ms/spectrum/compare.rb
|
93
107
|
- lib/ms/spectrum/filter.rb
|
94
|
-
- spec/lmat_spec.rb
|
95
108
|
- spec/metadata/opd1/000.v1.mzXML.yml
|
96
109
|
- spec/metadata/opd1/000.v2.1.mzXML.yml
|
97
110
|
- spec/metadata/opd1/020.mzData.xml.yml
|
@@ -125,7 +138,7 @@ files:
|
|
125
138
|
- spec/testfiles/opd1/020.v2.0.readw.mzXML
|
126
139
|
- spec/testfiles/opd1/020.v2.0.readw.mzXML.key.yml
|
127
140
|
has_rdoc: true
|
128
|
-
homepage: http://jtprince.github.com/ms-msrun/
|
141
|
+
homepage: http://jtprince.github.com/ms-msrun/rdoc
|
129
142
|
licenses: []
|
130
143
|
|
131
144
|
post_install_message:
|
@@ -137,33 +150,34 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
137
150
|
requirements:
|
138
151
|
- - ">="
|
139
152
|
- !ruby/object:Gem::Version
|
153
|
+
segments:
|
154
|
+
- 0
|
140
155
|
version: "0"
|
141
|
-
version:
|
142
156
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
157
|
requirements:
|
144
158
|
- - ">="
|
145
159
|
- !ruby/object:Gem::Version
|
160
|
+
segments:
|
161
|
+
- 0
|
146
162
|
version: "0"
|
147
|
-
version:
|
148
163
|
requirements: []
|
149
164
|
|
150
165
|
rubyforge_project: mspire
|
151
|
-
rubygems_version: 1.3.
|
166
|
+
rubygems_version: 1.3.6
|
152
167
|
signing_key:
|
153
168
|
specification_version: 3
|
154
169
|
summary: an mspire library for working with LC/MS runs (mzxml, mzData, mzML)
|
155
170
|
test_files:
|
156
|
-
- spec/ms/scan_spec.rb
|
157
|
-
- spec/ms/msrun_spec.rb
|
158
171
|
- spec/ms/msrun_bm.rb
|
172
|
+
- spec/ms/msrun/test_parsing_xml_frags/test_failures.rb
|
173
|
+
- spec/ms/msrun/test_parsing_xml_frags/parse_test.rb
|
159
174
|
- spec/ms/msrun/search_spec.rb
|
160
175
|
- spec/ms/msrun/index_spec.rb
|
161
176
|
- spec/ms/msrun/hpricot.rb
|
162
|
-
- spec/ms/msrun/scan_spec.rb
|
163
177
|
- spec/ms/msrun/sha1_spec.rb
|
164
|
-
- spec/ms/msrun/
|
165
|
-
- spec/ms/msrun/test_parsing_xml_frags/test_failures.rb
|
166
|
-
- spec/ms/spectrum/compare_spec.rb
|
178
|
+
- spec/ms/msrun/scan_spec.rb
|
167
179
|
- spec/ms/spectrum/filter_spec.rb
|
168
|
-
- spec/
|
180
|
+
- spec/ms/spectrum/compare_spec.rb
|
181
|
+
- spec/ms/msrun_spec.rb
|
182
|
+
- spec/ms/scan_spec.rb
|
169
183
|
- spec/spec_helper.rb
|
data/lib/lmat.rb
DELETED
@@ -1,267 +0,0 @@
|
|
1
|
-
|
2
|
-
require 'gsl'
|
3
|
-
require 'narray'
|
4
|
-
#include Runarray
|
5
|
-
|
6
|
-
include Math
|
7
|
-
include GSL
|
8
|
-
|
9
|
-
## Labeled matrix
|
10
|
-
|
11
|
-
class Lmat
|
12
|
-
|
13
|
-
NUM_BYTE_SIZE = 4
|
14
|
-
|
15
|
-
# an narray object numerically labelling the m-axis
|
16
|
-
attr_accessor :mvec
|
17
|
-
# an narray object numerically labelling the n-axis
|
18
|
-
attr_accessor :nvec
|
19
|
-
# an mvec.size X nvec.size narray
|
20
|
-
attr_accessor :mat
|
21
|
-
|
22
|
-
## Takes an array of narray objects
|
23
|
-
def initialize(mat=nil, mvec=nil, nvec=nil)
|
24
|
-
@mat = mat
|
25
|
-
@mvec = mvec
|
26
|
-
@nvec = nvec
|
27
|
-
end
|
28
|
-
|
29
|
-
class << self
|
30
|
-
def [](*args)
|
31
|
-
mat = NArray[*args]
|
32
|
-
(nlen, mlen) = mat.shape
|
33
|
-
obj = new(mat)
|
34
|
-
obj.mvec = NArray[0...mlen]
|
35
|
-
obj.nvec = NArray[0...nlen]
|
36
|
-
obj
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def [](*args)
|
41
|
-
@mat[*args]
|
42
|
-
end
|
43
|
-
|
44
|
-
def []=(*args)
|
45
|
-
@mat.send('[]=', *args)
|
46
|
-
end
|
47
|
-
|
48
|
-
def slice=(*args)
|
49
|
-
@mat.send(:slice, *args)
|
50
|
-
end
|
51
|
-
|
52
|
-
def slice(*args)
|
53
|
-
@mat.slice(*args)
|
54
|
-
end
|
55
|
-
|
56
|
-
def inspect
|
57
|
-
# TODO: needs work (see ruport pivotted table output)
|
58
|
-
["nvec=#{@nvec.inspect}", "mvec=#{@mvec.inspect}", "mat=#{@mat.inspect}"].join("\n")
|
59
|
-
|
60
|
-
start = ' ' << nvec.to_a.join(", ") << "\n"
|
61
|
-
start << (" " + ("-" * (start.size - 4))) << "\n"
|
62
|
-
mvec[].indgen!.each do |i|
|
63
|
-
start << "#{mvec[i]} | " << @mat[true, i].to_a.join(" ") << "\n"
|
64
|
-
end
|
65
|
-
start
|
66
|
-
end
|
67
|
-
|
68
|
-
def max
|
69
|
-
@mat.max
|
70
|
-
end
|
71
|
-
|
72
|
-
def dup
|
73
|
-
a = Lmat.new
|
74
|
-
a.mvec = self.mvec[]
|
75
|
-
a.nvec = self.nvec[]
|
76
|
-
a.mat = self.mat[]
|
77
|
-
a
|
78
|
-
end
|
79
|
-
|
80
|
-
# returns self
|
81
|
-
def from_lmat(file)
|
82
|
-
File.open(file) do |io|
|
83
|
-
(@mvec, @nvec) = [true, true].map do |iv|
|
84
|
-
_len = io.read(4).unpack('I').first
|
85
|
-
NArray.to_na( io.read(_len*NUM_BYTE_SIZE), 'sfloat' )
|
86
|
-
end
|
87
|
-
@mat = NArray.to_na(io.read, 'sfloat', @nvec.size, @mvec.size)
|
88
|
-
end
|
89
|
-
self
|
90
|
-
end
|
91
|
-
|
92
|
-
# returns self
|
93
|
-
def from_lmata(file)
|
94
|
-
# this can probably be made faster
|
95
|
-
File.open(file) do |io|
|
96
|
-
num_m = io.readline.to_i
|
97
|
-
mline = io.readline.chomp
|
98
|
-
@mvec = NArray.to_na( mline.split(' ').map {|v| v.to_f } )
|
99
|
-
raise RuntimeError, "bad m vec size" if mvec.size != num_m
|
100
|
-
num_n = io.readline.to_i
|
101
|
-
nline = io.readline.chomp
|
102
|
-
@nvec = NArray.to_na( nline.split(' ').map {|v| v.to_f } )
|
103
|
-
raise RuntimeError, "bad n vec size" if nvec.size != num_n
|
104
|
-
@mat = NArray.float(num_n, num_m)
|
105
|
-
num_m.times do |m|
|
106
|
-
line = io.readline.chomp!
|
107
|
-
@mat[true, m] = line.split(' ').map {|v| v.to_f }
|
108
|
-
end
|
109
|
-
end
|
110
|
-
self
|
111
|
-
end
|
112
|
-
|
113
|
-
# converts msrun object to a labeled matrix
|
114
|
-
# takes hash with symbols as keys
|
115
|
-
def from_msrun(msrun, args)
|
116
|
-
opt = {
|
117
|
-
:inc_mz => 1.0,
|
118
|
-
:behave_mz => 'sum',
|
119
|
-
:baseline=> 0.0,
|
120
|
-
|
121
|
-
#:start_tm => 0.0,
|
122
|
-
#:end_tm => 3600.0,
|
123
|
-
#:inc_tm => nil,
|
124
|
-
|
125
|
-
#:start_mz => 400.0,
|
126
|
-
#:end_mz => 1500.0,
|
127
|
-
}
|
128
|
-
opt.merge!(args)
|
129
|
-
|
130
|
-
(st, en) = msrun.start_and_end_mz
|
131
|
-
unless st && en
|
132
|
-
msg = ["scanning spectrum for start and end m/z values"]
|
133
|
-
msg << "(use :start_mz and :end_mz options to avoid this)"
|
134
|
-
warn msg
|
135
|
-
(st, en) = msrun.start_and_end_mz_brute_force
|
136
|
-
end
|
137
|
-
opt[:start_mz] ||= st
|
138
|
-
opt[:end_mz] ||= en
|
139
|
-
|
140
|
-
#unless opt[:start_tm] then opt[:start_tm] = times.first end
|
141
|
-
#unless opt[:end_tm] then opt[:end_tm] = times.last end
|
142
|
-
|
143
|
-
if opt[:inc_tm]
|
144
|
-
raise NotImplementedError, "haven't implemented interpolation in ruby yet! (#{File.basename(__FILE__)}: #{__LINE__})"
|
145
|
-
else ## No interpolation
|
146
|
-
times = []
|
147
|
-
@nvec = nil
|
148
|
-
vecs = []
|
149
|
-
num_scans = msrun.scan_count
|
150
|
-
printf "Reading #{num_scans} spectra [.=100]" if $VERBOSE
|
151
|
-
spectrum_cnt = 0
|
152
|
-
msrun.each(:ms_level => 1) do |scan|
|
153
|
-
spectrum = scan.spectrum
|
154
|
-
times << scan.time
|
155
|
-
#(mz,inten) = spectrum_to_mz_and_inten(spectrum, VecD)
|
156
|
-
# TODO: Figure out a shallow copy here:
|
157
|
-
# perhaps we'll make spectra Vec objects by default in future and
|
158
|
-
# then we'd be set...
|
159
|
-
mzs = NArray.new(spectrum.mzs)
|
160
|
-
intens = NArray.new(spectrum.intensities)
|
161
|
-
(x,y) = mzs.inc_x(intens, opt[:start_mz], opt[:end_mz], opt[:inc_mz], opt[:baseline], opt[:behave_mz])
|
162
|
-
spectrum_cnt += 1
|
163
|
-
if spectrum_cnt % 100 == 0
|
164
|
-
printf "." if $VERBOSE ; $stdout.flush
|
165
|
-
end
|
166
|
-
@nvec ||= x # just need the first one for the x values
|
167
|
-
vecs << y
|
168
|
-
end
|
169
|
-
puts "DONE!" if $VERBOSE
|
170
|
-
@mvec = NArray.new(times)
|
171
|
-
@mat = vecs
|
172
|
-
end
|
173
|
-
self
|
174
|
-
end
|
175
|
-
|
176
|
-
# outputs vec lengths if set to true
|
177
|
-
def to_s(with_vec_lengths=false)
|
178
|
-
arr = []
|
179
|
-
arr.push(@mvec.size) if with_vec_lengths
|
180
|
-
arr.push(@mvec.to_a.join(" "))
|
181
|
-
arr.push(@nvec.size) if with_vec_lengths
|
182
|
-
arr.push(@nvec.to_a.join(" "))
|
183
|
-
(0...@mvec.size).each do |m_index|
|
184
|
-
arr.push(@mat[true, m_index].to_a.join(" "))
|
185
|
-
end
|
186
|
-
arr.join("\n")
|
187
|
-
end
|
188
|
-
|
189
|
-
def ==(other)
|
190
|
-
other != nil && self.class == other.class && @nvec == other.nvec && @mvec == other.mvec && @mat == other.mat
|
191
|
-
end
|
192
|
-
|
193
|
-
# returns a fresh lmat object
|
194
|
-
def warp_cols(new_m_values, deep_copy=false)
|
195
|
-
new_guy = self.dup
|
196
|
-
new_guy.warp_cols!(new_m_values, deep_copy)
|
197
|
-
new_guy
|
198
|
-
end
|
199
|
-
|
200
|
-
# warps the data in self based on interpolation of the cols. Evaluates the
|
201
|
-
# new_m_values for each column and returns a new lmat object with the m
|
202
|
-
# values set to new_m_values. nvec will be the same is in self.
|
203
|
-
def warp_cols!(new_m_values, deep_copy=false)
|
204
|
-
nvec[].indgen.each do |n|
|
205
|
-
self[n,true] = Spline.alloc(Interp::AKIMA, mvec, self[n, true]).eval(new_m_values)
|
206
|
-
end
|
207
|
-
self.nvec = deep_copy ? self.nvec[] : self.nvec
|
208
|
-
self.mvec = deep_copy ? new_m_values[] : new_m_values
|
209
|
-
self
|
210
|
-
end
|
211
|
-
|
212
|
-
def write(file=nil, int_format_string='i')
|
213
|
-
handle = $>
|
214
|
-
if file; handle = File.open(file, "wb") end
|
215
|
-
bin_string = ""
|
216
|
-
bin_string << [@mvec.size].pack(int_format_string)
|
217
|
-
bin_string << @mvec.to_s
|
218
|
-
bin_string << [@nvec.size].pack(int_format_string)
|
219
|
-
bin_string << @nvec.to_s
|
220
|
-
bin_string << @mat.to_s
|
221
|
-
handle.print bin_string
|
222
|
-
if file; handle.close end
|
223
|
-
end
|
224
|
-
|
225
|
-
def print(file=nil)
|
226
|
-
handle = $>
|
227
|
-
handle = File.new(file, "w") if file
|
228
|
-
handle.print( self.to_s(true) )
|
229
|
-
handle.close if file
|
230
|
-
end
|
231
|
-
end
|
232
|
-
|
233
|
-
class Lmat
|
234
|
-
module Gnuplot
|
235
|
-
|
236
|
-
# png output only right now, given no outfile, plot to X11
|
237
|
-
def plot(outfile=nil)
|
238
|
-
# modified from Hornet's eye
|
239
|
-
require 'gnuplot'
|
240
|
-
::Gnuplot.open do |gp|
|
241
|
-
::Gnuplot::SPlot.new(gp) do |plot|
|
242
|
-
if outfile
|
243
|
-
plot.terminal 'png'
|
244
|
-
plot.output outfile
|
245
|
-
end
|
246
|
-
plot.pm3d
|
247
|
-
plot.hidden3d
|
248
|
-
plot.palette 'defined ( 0 "black", 51 "blue", 102 "green", ' +
|
249
|
-
'153 "yellow", 204 "red", 255 "white" )'
|
250
|
-
plot.xlabel 'n'
|
251
|
-
plot.ylabel 'm'
|
252
|
-
plot.data << ::Gnuplot::DataSet.new( self ) do |ds|
|
253
|
-
ds.with = 'pm3d'
|
254
|
-
ds.matrix = true
|
255
|
-
end
|
256
|
-
end
|
257
|
-
end
|
258
|
-
end
|
259
|
-
|
260
|
-
def to_gsplot
|
261
|
-
require 'gnuplot'
|
262
|
-
[@mvec.to_a, @nvec.to_a, @mat.to_a].to_gsplot
|
263
|
-
end
|
264
|
-
end
|
265
|
-
include Gnuplot
|
266
|
-
end
|
267
|
-
|
data/spec/lmat_spec.rb
DELETED
@@ -1,105 +0,0 @@
|
|
1
|
-
require File.expand_path( File.dirname(__FILE__) + '/spec_helper' )
|
2
|
-
|
3
|
-
require 'lmat'
|
4
|
-
|
5
|
-
describe 'an lmat' do
|
6
|
-
|
7
|
-
@klass = Lmat
|
8
|
-
@lmatfile = TESTFILES + "/lmat/tmp1.lmat"
|
9
|
-
@lmatafile = TESTFILES + "/lmat/tmp1.lmata"
|
10
|
-
@lmatafile_small = TESTFILES + "/lmat/tmp2.lmata"
|
11
|
-
|
12
|
-
before do
|
13
|
-
@lmat = Lmat.new
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'can be created with no arguments' do
|
17
|
-
obj1 = @klass.new
|
18
|
-
obj1.class.is @klass
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'can be created with arrays' do
|
22
|
-
obj = @klass[[1,2,3],[4,5,6]]
|
23
|
-
obj[0,0].is 1
|
24
|
-
obj[2,1].is 6
|
25
|
-
obj[1,0].is 2
|
26
|
-
obj.mvec.enums [0,1]
|
27
|
-
obj.nvec.enums [0,1,2]
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'can find the max value' do
|
31
|
-
obj = @klass[[1,2,3],[1,8,3]]
|
32
|
-
obj.max.is 8
|
33
|
-
end
|
34
|
-
|
35
|
-
it 'can be read from lmat file' do
|
36
|
-
x = Lmat.new
|
37
|
-
x.from_lmat(@lmatfile)
|
38
|
-
x.nvec.size.is 30
|
39
|
-
x.mvec.size.is 40
|
40
|
-
x.mat.size.is 1200
|
41
|
-
x.mat.shape.is [30,40]
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'can write an lmat file' do
|
45
|
-
begin
|
46
|
-
output = @lmatfile + ".TMP"
|
47
|
-
@lmat.from_lmat(@lmatfile)
|
48
|
-
@lmat.write(output)
|
49
|
-
IO.read(output).is IO.read(@lmatfile)
|
50
|
-
ensure
|
51
|
-
File.unlink(output) if File.exist?(output)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'can be read from an lmata file' do
|
56
|
-
x = Lmat.new.from_lmata(@lmatafile)
|
57
|
-
x.nvec.size.is 30
|
58
|
-
x.mvec.size.is 40
|
59
|
-
x.mat.size.is 1200
|
60
|
-
x.mat.shape.is [30,40]
|
61
|
-
end
|
62
|
-
|
63
|
-
it 'can print an lmata file' do
|
64
|
-
begin
|
65
|
-
output = @lmatafile_small + ".TMP"
|
66
|
-
@lmat.from_lmata(@lmatafile_small)
|
67
|
-
@lmat.print(output)
|
68
|
-
ars = [output, @lmatafile_small].map do |file|
|
69
|
-
IO.read(file).chomp.gsub("\n", " ").split(/\s+/).map {|v| v.to_f }
|
70
|
-
end
|
71
|
-
ars.first.enums ars.last
|
72
|
-
ensure
|
73
|
-
File.unlink(output) if File.exist?(output)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
xit 'can warp data columns' do
|
78
|
-
@lmat.from_lmata(@lmatafile_small)
|
79
|
-
puts "Warp before"
|
80
|
-
p @lmat
|
81
|
-
deep_copy = true
|
82
|
-
@lmat.plot("before.png")
|
83
|
-
new_lmat = @lmat.warp_cols(NArray.float(7).indgen(12).collect {|v| v + 2.5 }, deep_copy)
|
84
|
-
new_lmat.isa Lmat
|
85
|
-
new_lmat.plot("after.png")
|
86
|
-
puts "Warp after"
|
87
|
-
p new_lmat
|
88
|
-
## TODO: NEEEED tests HERE
|
89
|
-
end
|
90
|
-
|
91
|
-
begin
|
92
|
-
require 'gnuplot'
|
93
|
-
it 'can plot' do
|
94
|
-
file = "mypng.png"
|
95
|
-
@lmat.from_lmata(@lmatafile_small)
|
96
|
-
@lmat.plot(file)
|
97
|
-
@lmat.isa Lmat
|
98
|
-
ok File.exist?(file)
|
99
|
-
File.unlink(file) if File.exist?(file)
|
100
|
-
end
|
101
|
-
rescue
|
102
|
-
puts "SKIPPING: plotting (since gnuplot gem not found)"
|
103
|
-
end
|
104
|
-
|
105
|
-
end
|