Skip to content

Commit 1571672

Browse files
authored
Merge pull request #56 from alishdipani/master
[WIP] Ruby Association Grant 2019 work
2 parents a81b2c4 + 6348255 commit 1571672

35 files changed

+1174
-147
lines changed

.rubocop.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ AllCops:
1313
- 'tmp/*'
1414
- '*.so'
1515
DisplayCopNames: true
16-
TargetRubyVersion: 2.2
16+
TargetRubyVersion: 2.3+
1717

1818
# Preferred codebase style ---------------------------------------------
1919
Layout/ExtraSpacing:
@@ -34,7 +34,7 @@ Layout/SpaceInsideBlockBraces:
3434
Layout/SpaceInsideHashLiteralBraces:
3535
EnforcedStyle: no_space
3636

37-
Layout/AlignParameters:
37+
Layout/ParameterAlignment:
3838
EnforcedStyle: with_fixed_indentation
3939

4040
Style/EmptyElse:
@@ -128,6 +128,3 @@ Style/MethodDefParentheses:
128128

129129
# Bans methods like `has_missing_data?`, `is_number?` and so on - started
130130
# with unnecessary has_ or is_.
131-
132-
133-

ext/grruby/extconf.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'mkmf'
22

3-
$CFLAGS << ' -I/home/sameer/Downloads/gr/include '
4-
$LDFLAGS << ' -L/home/sameer/Downloads/gr/lib -lGR -lm -Wl,-rpath,/home/sameer/Downloads/gr/lib '
3+
$CFLAGS << ' -I/usr/local/gr/include '
4+
$LDFLAGS << ' -L/usr/local/gr/lib -lGR -lm -Wl,-rpath,/usr/local/gr/lib '
55

66
create_makefile('grruby/grruby')

lib/rubyplot.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
require 'rubyplot/figure'
1414
require 'rubyplot/subplot'
1515
require 'rubyplot/spi'
16+
require 'rubyplot/image'
1617

1718
module Rubyplot
1819
LINE_TYPES = [
@@ -29,7 +30,7 @@ module Rubyplot
2930
:double_dot,
3031
:triple_dot,
3132
].freeze
32-
33+
3334
MARKER_TYPES = [
3435
:dot,
3536
:plus,
@@ -161,6 +162,12 @@ def stop_inline
161162
@iruby_inline = false
162163
end
163164

165+
def QuantumRange
166+
# Setting Backend to Magick as Image is only implemented for Magick backend
167+
@backend = Rubyplot::Backend::MagickWrapper.new
168+
Rubyplot.backend.QuantumRange
169+
end
170+
164171
def set_backend b
165172
case b
166173
when :magick
@@ -171,4 +178,3 @@ def set_backend b
171178
end
172179
end
173180
end # module Rubyplot
174-

lib/rubyplot/artist.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
require_relative 'artist/circle'
1313
require_relative 'artist/polygon'
1414
require_relative 'artist/arrow'
15+
require_relative 'artist/image'

lib/rubyplot/artist/image.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module Rubyplot
2+
module Artist
3+
class Image
4+
5+
attr_accessor :rows, :columns, :pixel_array
6+
attr_reader :QuantumRange
7+
8+
def initialize(columns,rows)
9+
@rows = rows
10+
@columns = columns
11+
@pixel_array = []
12+
@image = Rubyplot.backend.init_image(columns,rows)
13+
@QuantumRange = Rubyplot.backend.QuantumRange
14+
end
15+
16+
def imread(path)
17+
@image = Rubyplot.backend.imread(path)
18+
@rows = @image.rows
19+
@columns = @image.columns
20+
end
21+
22+
def imshow
23+
Rubyplot.backend.output_device = Rubyplot.iruby_inline ? :iruby : :window
24+
Rubyplot.backend.imshow(@image, Rubyplot.backend.output_device)
25+
end
26+
27+
def imwrite(path)
28+
Rubyplot.backend.imwrite(@image, path)
29+
end
30+
31+
def export_pixels(x, y, columns, rows, map)
32+
@pixel_array = []
33+
map.size.times do
34+
@pixel_array.push([])
35+
end
36+
flat_pix_array = Rubyplot.backend.export_pixels(@image, x, y, columns, rows, map)
37+
map.size.times do |channel|
38+
rows.times do |row|
39+
@pixel_array[channel].push(flat_pix_array[(channel*rows*columns)+(columns*row),columns])
40+
end
41+
end
42+
@pixel_array.flatten!(1) if map.size==1
43+
@pixel_array
44+
end
45+
46+
def import_pixels(x, y, columns, rows, map, pixels)
47+
Rubyplot.backend.import_pixels(@image, x, y, columns, rows, map, pixels.to_a.flatten)
48+
end
49+
end # class Image
50+
end # module Artist
51+
end # module Rubyplot

lib/rubyplot/artist/legend.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def configure_legend_color_box
3838
y1: @abs_y + BOTTOM_MARGIN,
3939
x2: @abs_x + @legend_box_size,
4040
y2: @abs_y + @legend_box_size + BOTTOM_MARGIN,
41-
border_color: @color,
41+
border_color: :black,
42+
border_width: 2,
4243
fill_color: @color,
4344
abs: true
4445
)

lib/rubyplot/artist/plot/area.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Rubyplot
22
module Artist
33
module Plot
44
class Area < Artist::Plot::Base
5-
attr_accessor :sort_data
5+
attr_accessor :sort_data, :fill_opacity
66

77
def initialize(*)
88
super
@@ -15,7 +15,7 @@ def stacked(bool)
1515
end
1616

1717
def data x_values, y_values
18-
x_values, y_values = x_values.zip(y_values).sort.transpose if @sort_data
18+
x_values, y_values = x_values.to_a.zip(y_values.to_a).sort.transpose if @sort_data
1919
super(x_values, y_values)
2020
end
2121

lib/rubyplot/artist/plot/bar.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def spacing_ratio=(s_r)
3434

3535
# Set Bar plot data.
3636
def data y_values
37-
super(Array.new(y_values.size) { |i| i }, y_values)
37+
super(Array.new(y_values.to_a.size) { |i| i }, y_values.to_a)
3838
end
3939

4040
# Number of bars in this Bar plot

lib/rubyplot/artist/plot/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def color=(color)
3939
end
4040

4141
def data(x_values, y_values)
42-
@data[:x_values] = x_values
43-
@data[:y_values] = y_values
42+
@data[:x_values] = x_values.to_a
43+
@data[:y_values] = y_values.to_a
4444
end
4545

4646
def process_data

lib/rubyplot/artist/plot/box_plot.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ class BoxPlot < Artist::Plot::Base
1414
attr_accessor :outlier_marker_type
1515
attr_accessor :outlier_marker_color
1616
attr_accessor :outlier_marker_size
17-
17+
attr_accessor :median_width
18+
1819
def initialize(*)
1920
super
2021
@whiskers = 1.5
2122
@x_left_box = []
22-
@median_color = :yellow
23+
@median_color = :black
2324
@outlier_marker_type = :plus
2425
@outlier_marker_color = nil
2526
@outlier_marker_size = 1.0
27+
@median_width = 3.0
2628
end
2729

2830
def process_data
@@ -37,15 +39,21 @@ def process_data
3739
end
3840

3941
def data vectors
40-
@vectors = vectors
42+
# @vectors = vectors.to_a unless vectors.is_a? Array
43+
if vectors.is_a? Array
44+
vectors.each_with_index { |_, idx| vectors[idx] = vectors[idx].to_a}
45+
@vectors = vectors
46+
else
47+
@vectors = vectors.to_a
48+
end
4149
end
4250

4351
def draw
4452
@x_left_box.each_with_index do |x_left, i|
4553
draw_box x_left, i
4654
draw_whiskers x_left, i
4755
draw_outliers x_left, i
48-
draw_median x_left, i
56+
draw_median x_left, i
4957
end
5058
end
5159

@@ -111,7 +119,7 @@ def draw_outliers x_left, index
111119

112120
def first_max_outlier_index vector, max
113121
return nil if vector.last >= max
114-
vector.size.times do |i|
122+
vector.size.times do |i|
115123
if vector[i] > max
116124
return i
117125
end
@@ -131,7 +139,8 @@ def draw_median x_left, index
131139
Rubyplot::Artist::Line2D.new(self,
132140
x: [x_left, x_left + @box_width],
133141
y: [@medians[index], @medians[index]],
134-
color: @median_color
142+
color: @median_color,
143+
width: @median_width
135144
).draw
136145
end
137146

@@ -141,7 +150,7 @@ def calculate_ranges!
141150
@medians = []
142151
@mins = []
143152
@maxs = []
144-
153+
145154
@vectors.each do |sorted_vec|
146155
m = get_percentile 50, sorted_vec
147156
q1 = get_percentile 25, sorted_vec

lib/rubyplot/artist/plot/bubble.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Artist
33
module Plot
44
class Bubble < Artist::Plot::Base
55
# Width in pixels of the border of each bubble.
6-
attr_reader :border_width
6+
attr_accessor :border_width
77
attr_reader :z_max, :z_min
88
# Opacity of the circles
99
attr_accessor :fill_opacity
@@ -16,8 +16,8 @@ def initialize(*)
1616
end
1717

1818
def data x_values, y_values, z_values
19-
super(x_values, y_values)
20-
@data[:z_values] = z_values
19+
super(x_values.to_a, y_values.to_a)
20+
@data[:z_values] = z_values.to_a
2121
end
2222

2323
def draw

lib/rubyplot/artist/plot/candle_stick.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ module Rubyplot
22
module Artist
33
module Plot
44
class CandleStick < Artist::Plot::Base
5-
attr_accessor :lows
6-
attr_accessor :highs
7-
attr_accessor :opens
8-
attr_accessor :closes
5+
attr_reader :lows
6+
attr_reader :highs
7+
attr_reader :opens
8+
attr_reader :closes
99
attr_accessor :bar_width
1010
attr_accessor :x_left_candle
1111
attr_accessor :x_low_stick
@@ -24,6 +24,22 @@ def initialize(*)
2424
@border_color = :black
2525
end
2626

27+
def lows=(lows_arr)
28+
@lows = lows_arr.to_a
29+
end
30+
31+
def highs=(highs_arr)
32+
@highs = highs_arr.to_a
33+
end
34+
35+
def opens=(opens_arr)
36+
@opens = opens_arr.to_a
37+
end
38+
39+
def closes=(closes_arr)
40+
@closes = closes_arr.to_a
41+
end
42+
2743
def process_data
2844
if @lows.size != @highs.size || @opens.size != @closes.size
2945
raise Rubyplot::SizeError, "all given parameters must be of equal size."

0 commit comments

Comments
 (0)