panelize 1.0.0-java

Sign up to get free protection for your applications and to get access to all the features.
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 panelize.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Colin J. Fuller
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Panelize
2
+
3
+ Panelize is a script for arranging microscopy images into a grid with constant leveling for making figures. Multiple channels are supported with up to three-channel RGB merge. An arbitrary number of treatments is supported; each channel is scaled the same across all treatments. Adds a scalebar to the lower left image in the grid.
4
+
5
+ ## Installation
6
+
7
+ Panelize requires jruby as it uses java libraries.
8
+
9
+ Execute:
10
+
11
+ $ gem install panelize
12
+
13
+ Or for an application using bundler, add this line to your application's Gemfile:
14
+
15
+ gem 'panelize'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+
22
+ ## Usage
23
+
24
+ Run `panelize` from the command prompt and answer the prompts it prints out (which will ask for input images, etc.).
25
+
26
+ When asked for the channels to display, these should be provided as numbers (with the first channel having index 0) where each number refers to the order the channels are stored in the image file. The order in which you list the channels will be the order they are displayed in the grid from left to right, and these will appear in the merge as red, green, and blue in that order. More than three channels can be displayed, but only the first three will appear in the merge.
27
+
28
+ Additional options are available at the command line; run `panelize --help` for a description.
29
+
30
+ Panelizer expects that the input images will be multi-channel images in a format that the [bio-formats library](http://www.openmicroscopy.org/site/products/bio-formats) can read and recognize as having multiple channels. If the images have multiple axial or time planes, only the final one will be used for display.
31
+
32
+ The final set of panels will be saved with a unique filename (which will be printed out) to the directory from which you ran the script.
33
+
34
+ If you want to tweak the scaling of the images, you should adjust the command line parameters scalesat and scaleundersat (run `panelize --help` to see the syntax for using these). If both these parameters are set to zero, in each channel, the images will be scaled between the min and max value appearing in that channel in any of the images. Setting these to a larger value will narrow the display range by the value supplied (in percent) from either the upper end (the scalesat parameter) or the lower end (scaleundersat), making the image appear brighter or the background dimmer, respectively.
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
43
+
44
+ ## License
45
+
46
+ Panelize is distributed under the MIT/X11 license (see LICENSE.txt for the full license).
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/panelize ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'panelize'
4
+
5
+ Panelize.go
6
+
@@ -0,0 +1,3 @@
1
+ module Panelize
2
+ VERSION = "1.0.0"
3
+ end
data/lib/panelize.rb ADDED
@@ -0,0 +1,238 @@
1
+ #--
2
+ # Copyright (c) 2013 Colin J. Fuller
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the Software), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ # SOFTWARE.
21
+ #++
22
+
23
+ require "panelize/version"
24
+ require 'rimageanalysistools'
25
+ require 'rimageanalysistools/get_image'
26
+ require 'rimageanalysistools/image_shortcuts'
27
+ require 'trollop'
28
+ require 'highline/import'
29
+
30
+ module Panelize
31
+
32
+ java_import Java::edu.stanford.cfuller.imageanalysistools.image.ImageCoordinate
33
+ java_import Java::edu.stanford.cfuller.imageanalysistools.image.ImageFactory
34
+ java_import Java::edu.stanford.cfuller.imageanalysistools.image.Histogram
35
+ java_import Java::ij.process.ColorProcessor
36
+ java_import Java::ij.ImagePlus
37
+ java_import Java::ij.io.FileSaver
38
+
39
+ class << self
40
+
41
+ def box_channel(im, ch)
42
+ lower = ImageCoordinate[0,0,0,0,0]
43
+ upper = ImageCoordinate.cloneCoord(im.getDimensionSizes)
44
+ lower[:c] = ch
45
+ upper[:c] = ch+1
46
+ im.setBoxOfInterest(lower, upper)
47
+ lower.recycle
48
+ upper.recycle
49
+ nil
50
+ end
51
+
52
+ def display(im, params)
53
+ box_channel(im, params[:col_chs][0])
54
+ max = Histogram.findMaxVal(im)
55
+ im.clearBoxOfInterest
56
+ imp = im.toImagePlus
57
+ imp.setOpenAsHyperStack true
58
+ imp.updatePosition(params[:col_chs][0]+1, 1, 1)
59
+ imp.setDisplayRange(0, max)
60
+ imp.setRoi 0,0, params[:size], params[:size]
61
+ imp.show
62
+ ask("Move the region of interest to where you want to crop and press enter when done.")
63
+ ul = [imp.getRoi.getPolygon.xpoints[0], imp.getRoi.getPolygon.ypoints[0]]
64
+ end
65
+
66
+ def crop(im, ul, params)
67
+ sizes = ImageCoordinate.cloneCoord(im.getDimensionSizes)
68
+ sizes[:x] = params[:size]
69
+ sizes[:y] = params[:size]
70
+ ul_coord = ImageCoordinate[ul[0], ul[1], 0,0,0]
71
+ im_crop = im.subImage(sizes, ul_coord)
72
+ im.toImagePlus.close
73
+ sizes.recycle
74
+ ul_coord.recycle
75
+ im_crop
76
+ end
77
+
78
+ def load_and_crop(fn, params)
79
+ im = RImageAnalysisTools.get_image(fn)
80
+ ul = display(im, params)
81
+ crop(im, ul, params)
82
+ end
83
+
84
+ def calculate_channel_scale(ims, params)
85
+ scale = {}
86
+
87
+ params[:col_chs].each do |ch|
88
+ min = Float::MAX
89
+ max = -1.0*Float::MAX
90
+
91
+ ims.each do |im|
92
+ box_channel(im, ch)
93
+
94
+ h = Histogram.new(im)
95
+ min = h.getMinValue if h.getMinValue < min
96
+ max = h.getMaxValue if h.getMaxValue > max
97
+ end
98
+
99
+ scale[ch] = [min + params[:scaleundersat]/100*(max-min), max - params[:scalesat]/100*(max-min)]
100
+ end
101
+
102
+ scale
103
+ end
104
+
105
+ def scale_to_8_bit(value, scale)
106
+ value = 255*(value - scale[0])/(scale[1]-scale[0])
107
+ value = 0 if value < 0
108
+ value = 255 if value > 255
109
+ value
110
+ end
111
+
112
+ def add_scalebar(panel_image, params)
113
+ scalebar_length_px = params[:scalebar]/params[:mpp]
114
+
115
+ y_start = calculate_height(params) - 2*params[:spacing]
116
+ x_start = params[:spacing]
117
+
118
+ x_start.upto(x_start + scalebar_length_px - 1) do |x|
119
+ y_start.upto(y_start + params[:spacing] - 1) do |y|
120
+ panel_image.putPixel(x, y, [255, 255, 255].to_java(:int))
121
+ end
122
+ end
123
+ end
124
+
125
+
126
+ def place_panel(panel_image, start_coord, image, channel, rgb_chs, scales)
127
+ box_channel(image, channel)
128
+
129
+ rgb_mult = [0, 0, 0]
130
+ rgb_mult[0] = 1 if rgb_chs.include? :r
131
+ rgb_mult[1] = 1 if rgb_chs.include? :g
132
+ rgb_mult[2] = 1 if rgb_chs.include? :b
133
+
134
+ image.each do |ic|
135
+ v = scale_to_8_bit(image[ic], scales[ic[:c]])
136
+ curr_val = panel_image.getPixel(ic[:x] + start_coord[:x], ic[:y] + start_coord[:y], nil)
137
+ panel_image.putPixel(ic[:x] + start_coord[:x], ic[:y] + start_coord[:y], rgb_mult.map.with_index { |e, i| e*v + (1-e)*curr_val[i]}.to_java(:int))
138
+ end
139
+
140
+ nil
141
+ end
142
+
143
+ def calculate_width(params)
144
+ (params[:col_chs].size + 1)*params[:size] + params[:col_chs].size*params[:spacing]
145
+ end
146
+
147
+ def calculate_height(params)
148
+ (params[:n_rows])*params[:size] + (params[:n_rows]-1)*params[:spacing]
149
+ end
150
+
151
+ def calculate_row_pos(n, params)
152
+ n*params[:size] + n*params[:spacing]
153
+ end
154
+
155
+ def calculate_col_pos(n, params)
156
+ n*params[:size] + n*params[:spacing]
157
+ end
158
+
159
+ def initialize_panel(params)
160
+ w = calculate_width(params)
161
+ h = calculate_height(params)
162
+
163
+ im = ColorProcessor.new(w, h)
164
+ w.times do |x|
165
+ h.times do |y|
166
+ im.putPixel(x, y, [255, 255, 255].to_java(:int))
167
+ end
168
+ end
169
+
170
+ im
171
+ end
172
+
173
+
174
+ def make_panels(ims, scale, params)
175
+ panels = initialize_panel(params)
176
+
177
+ params[:n_rows].times do |row|
178
+ params[:col_chs].size.times do |col|
179
+ place_panel(panels, {y: calculate_row_pos(row, params), x: calculate_col_pos(col, params)}, ims[row], params[:col_chs][col], [:r, :g, :b], scale)
180
+ end
181
+
182
+ zero_im = ImageFactory.createWritable(ims[0])
183
+ zero_im.each { |ic| zero_im[ic] = 0.0 }
184
+
185
+ [:r, :g, :b].each_with_index do |ch, i|
186
+ if params[:col_chs].size > i then
187
+ place_panel(panels, {y:calculate_row_pos(row, params), x: calculate_col_pos(params[:col_chs].size, params)}, ims[row], params[:col_chs][i], [ch], scale)
188
+ else
189
+ place_panel(panels, {y:calculate_row_pos(row, params), x: calculate_col_pos(params[:col_chs].size, params)}, zero_im, params[:col_chs][0], [ch], scale)
190
+ end
191
+ end
192
+ end
193
+
194
+ panels
195
+ end
196
+
197
+ def save_image(panels)
198
+ fs = FileSaver.new(ImagePlus.new("panels", panels))
199
+ fn = "panels_#{Time.now.to_i}.tif"
200
+ fs.saveAsTiff(fn)
201
+ puts "Panels saved as #{fn}"
202
+ end
203
+
204
+ def ask_for_params(params)
205
+ params[:n_rows] = ask("Enter the number of treatments (rows) to panelize: ", Integer)
206
+ params[:col_chs] = ask("Enter the channel numbers to display (comma-separated): ", lambda { |str| str.split(/,\s*/).map(&:to_i) })
207
+ params[:fns] = []
208
+ params[:n_rows].times do |i|
209
+ params[:fns] << ask("Enter the filename for treatment #{i+1}: ").gsub("'", "") #remove quotes if drag and drop fn insertion in the terminal puts them in
210
+ end
211
+ params[:mpp] = ask("Enter the number of microns per pixel (for calculating the length of a scalebar): ", Float)
212
+
213
+ end
214
+
215
+ def go
216
+ params = Trollop::options do
217
+ opt :spacing, "Spacing between image panels in pixels", type: :integer, default: 5
218
+ opt :size, "Size of the image in pixels", type: :integer, default: 256
219
+ opt :scalebar, "Size of the scalebar in microns", type: :float, default: 5.0
220
+ opt :scalesat, "Amount in percent by which to saturate images", type: :float, default: 15.0
221
+ opt :scaleundersat, "Amount in percent by which to undersaturate images", type: :float, default: 5.0
222
+ end
223
+
224
+ ask_for_params(params)
225
+
226
+ ims = []
227
+ params[:fns].each do |fn|
228
+ ims << load_and_crop(fn, params)
229
+ end
230
+
231
+ scale = calculate_channel_scale(ims, params)
232
+ panels = make_panels(ims, scale, params)
233
+ add_scalebar(panels, params)
234
+ save_image(panels)
235
+ end
236
+ end
237
+ end
238
+
data/panelize.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'panelize/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "panelize"
8
+ gem.version = Panelize::VERSION
9
+ gem.authors = ["Colin J. Fuller"]
10
+ gem.email = ["cjfuller@gmail.com"]
11
+ gem.description = %q{Script for arranging microscopy images into a grid with constant leveling for making figures.}
12
+ gem.summary = %q{Script for arranging microscopy images into a grid with constant leveling for making figures. Multiple channels are supported with up to three-channel merge. An arbitrary number of treatments is supported; each channel is scaled the same across all treatments. Adds a scalebar to the lower left image in the grid.}
13
+ gem.homepage = "https://github.com/cjfuller/panelize"
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
+
20
+ gem.add_dependency('rimageanalysistools', '>= 5.1.4.2')
21
+ gem.add_dependency('trollop')
22
+ gem.add_dependency('highline')
23
+
24
+ gem.platform = 'java'
25
+ gem.requirements = 'jruby'
26
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: panelize
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: java
7
+ authors:
8
+ - Colin J. Fuller
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rimageanalysistools
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 5.1.4.2
21
+ none: false
22
+ requirement: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.4.2
27
+ none: false
28
+ prerelease: false
29
+ type: :runtime
30
+ - !ruby/object:Gem::Dependency
31
+ name: trollop
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: !binary |-
37
+ MA==
38
+ none: false
39
+ requirement: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: !binary |-
44
+ MA==
45
+ none: false
46
+ prerelease: false
47
+ type: :runtime
48
+ - !ruby/object:Gem::Dependency
49
+ name: highline
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: !binary |-
55
+ MA==
56
+ none: false
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: !binary |-
62
+ MA==
63
+ none: false
64
+ prerelease: false
65
+ type: :runtime
66
+ description: Script for arranging microscopy images into a grid with constant leveling for making figures.
67
+ email:
68
+ - cjfuller@gmail.com
69
+ executables:
70
+ - panelize
71
+ extensions: []
72
+ extra_rdoc_files: []
73
+ files:
74
+ - ".gitignore"
75
+ - Gemfile
76
+ - LICENSE.txt
77
+ - README.md
78
+ - Rakefile
79
+ - bin/panelize
80
+ - lib/panelize.rb
81
+ - lib/panelize/version.rb
82
+ - panelize.gemspec
83
+ homepage: https://github.com/cjfuller/panelize
84
+ licenses: []
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: !binary |-
94
+ MA==
95
+ none: false
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: !binary |-
101
+ MA==
102
+ none: false
103
+ requirements:
104
+ - jruby
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.24
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Script for arranging microscopy images into a grid with constant leveling for making figures. Multiple channels are supported with up to three-channel merge. An arbitrary number of treatments is supported; each channel is scaled the same across all treatments. Adds a scalebar to the lower left image in the grid.
110
+ test_files: []
111
+ has_rdoc: