Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Ruby Association Grant 2019 work #56

Merged
merged 26 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ee12d9d
Removed draw function from ticks
alishdipani Nov 18, 2019
cd6a084
Added border width to Rectangle Object
alishdipani Nov 18, 2019
7e9153a
Added black border to legends
alishdipani Nov 18, 2019
2fe355f
Adding Image read, show and write
Mar 3, 2020
cedd20e
Improved Image read, show and write
Mar 12, 2020
19b77fe
Added image display option in testing
Mar 13, 2020
7af5c30
Improved tests for stacked bar and multi stacked bar plots
Mar 13, 2020
7dd907a
Improved tests for plot function
Mar 13, 2020
9a2d86c
Improved bubble plot and tests
Mar 13, 2020
1a42dc8
Added imclear function
Mar 13, 2020
d4ea909
Added opacity to area plot and improved tests
Mar 13, 2020
f6299ad
Improved tests for line plot
Mar 13, 2020
72f5da0
Improved tests for bar plot
Mar 13, 2020
6aa84fe
Improved tests for scatter plot
Mar 13, 2020
30e0f55
Improved histogram and its tests
Mar 14, 2020
1c08554
Improved tests for candle stick plot
Mar 14, 2020
667668f
Added width and color options to error bar plot and improved tests
Mar 14, 2020
1a3f4f3
Added median width to box plot and improved tests
Mar 14, 2020
5bae0ec
Added Image conversion to Ruby Array
Mar 14, 2020
2f03044
Added offset and support for Numo::Narray
Mar 15, 2020
8555f0f
fixed major tick label text for magick backend
Mar 15, 2020
d859e04
Skipping incomplete tests
Mar 15, 2020
b2b55f5
Added support for Numo arrays to plots
Mar 15, 2020
ce16841
Added reading QuantumRange
Mar 15, 2020
cb7e42c
Updated rubocop,TargetRubyVersion = 2.3+, gr path set as /usr/local/gr
alishdipani Mar 17, 2020
6348255
Added tutorial for Image class
alishdipani Mar 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ AllCops:
- 'tmp/*'
- '*.so'
DisplayCopNames: true
TargetRubyVersion: 2.2
TargetRubyVersion: 2.3+

# Preferred codebase style ---------------------------------------------
Layout/ExtraSpacing:
Expand All @@ -34,7 +34,7 @@ Layout/SpaceInsideBlockBraces:
Layout/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space

Layout/AlignParameters:
Layout/ParameterAlignment:
EnforcedStyle: with_fixed_indentation

Style/EmptyElse:
Expand Down Expand Up @@ -128,6 +128,3 @@ Style/MethodDefParentheses:

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



4 changes: 2 additions & 2 deletions ext/grruby/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'mkmf'

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

create_makefile('grruby/grruby')
10 changes: 8 additions & 2 deletions lib/rubyplot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'rubyplot/figure'
require 'rubyplot/subplot'
require 'rubyplot/spi'
require 'rubyplot/image'

module Rubyplot
LINE_TYPES = [
Expand All @@ -29,7 +30,7 @@ module Rubyplot
:double_dot,
:triple_dot,
].freeze

MARKER_TYPES = [
:dot,
:plus,
Expand Down Expand Up @@ -161,6 +162,12 @@ def stop_inline
@iruby_inline = false
end

def QuantumRange
# Setting Backend to Magick as Image is only implemented for Magick backend
@backend = Rubyplot::Backend::MagickWrapper.new
Rubyplot.backend.QuantumRange
end

def set_backend b
case b
when :magick
Expand All @@ -171,4 +178,3 @@ def set_backend b
end
end
end # module Rubyplot

1 change: 1 addition & 0 deletions lib/rubyplot/artist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
require_relative 'artist/circle'
require_relative 'artist/polygon'
require_relative 'artist/arrow'
require_relative 'artist/image'
51 changes: 51 additions & 0 deletions lib/rubyplot/artist/image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module Rubyplot
module Artist
class Image

attr_accessor :rows, :columns, :pixel_array
attr_reader :QuantumRange

def initialize(columns,rows)
@rows = rows
@columns = columns
@pixel_array = []
@image = Rubyplot.backend.init_image(columns,rows)
@QuantumRange = Rubyplot.backend.QuantumRange
end

def imread(path)
@image = Rubyplot.backend.imread(path)
@rows = @image.rows
@columns = @image.columns
end

def imshow
Rubyplot.backend.output_device = Rubyplot.iruby_inline ? :iruby : :window
Rubyplot.backend.imshow(@image, Rubyplot.backend.output_device)
end

def imwrite(path)
Rubyplot.backend.imwrite(@image, path)
end

def export_pixels(x, y, columns, rows, map)
@pixel_array = []
map.size.times do
@pixel_array.push([])
end
flat_pix_array = Rubyplot.backend.export_pixels(@image, x, y, columns, rows, map)
map.size.times do |channel|
rows.times do |row|
@pixel_array[channel].push(flat_pix_array[(channel*rows*columns)+(columns*row),columns])
end
end
@pixel_array.flatten!(1) if map.size==1
@pixel_array
end

def import_pixels(x, y, columns, rows, map, pixels)
Rubyplot.backend.import_pixels(@image, x, y, columns, rows, map, pixels.to_a.flatten)
end
end # class Image
end # module Artist
end # module Rubyplot
3 changes: 2 additions & 1 deletion lib/rubyplot/artist/legend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def configure_legend_color_box
y1: @abs_y + BOTTOM_MARGIN,
x2: @abs_x + @legend_box_size,
y2: @abs_y + @legend_box_size + BOTTOM_MARGIN,
border_color: @color,
border_color: :black,
border_width: 2,
fill_color: @color,
abs: true
)
Expand Down
4 changes: 2 additions & 2 deletions lib/rubyplot/artist/plot/area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Rubyplot
module Artist
module Plot
class Area < Artist::Plot::Base
attr_accessor :sort_data
attr_accessor :sort_data, :fill_opacity

def initialize(*)
super
Expand All @@ -15,7 +15,7 @@ def stacked(bool)
end

def data x_values, y_values
x_values, y_values = x_values.zip(y_values).sort.transpose if @sort_data
x_values, y_values = x_values.to_a.zip(y_values.to_a).sort.transpose if @sort_data
super(x_values, y_values)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rubyplot/artist/plot/bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def spacing_ratio=(s_r)

# Set Bar plot data.
def data y_values
super(Array.new(y_values.size) { |i| i }, y_values)
super(Array.new(y_values.to_a.size) { |i| i }, y_values.to_a)
end

# Number of bars in this Bar plot
Expand Down
4 changes: 2 additions & 2 deletions lib/rubyplot/artist/plot/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def color=(color)
end

def data(x_values, y_values)
@data[:x_values] = x_values
@data[:y_values] = y_values
@data[:x_values] = x_values.to_a
@data[:y_values] = y_values.to_a
end

def process_data
Expand Down
23 changes: 16 additions & 7 deletions lib/rubyplot/artist/plot/box_plot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ class BoxPlot < Artist::Plot::Base
attr_accessor :outlier_marker_type
attr_accessor :outlier_marker_color
attr_accessor :outlier_marker_size

attr_accessor :median_width

def initialize(*)
super
@whiskers = 1.5
@x_left_box = []
@median_color = :yellow
@median_color = :black
@outlier_marker_type = :plus
@outlier_marker_color = nil
@outlier_marker_size = 1.0
@median_width = 3.0
end

def process_data
Expand All @@ -37,15 +39,21 @@ def process_data
end

def data vectors
@vectors = vectors
# @vectors = vectors.to_a unless vectors.is_a? Array
if vectors.is_a? Array
vectors.each_with_index { |_, idx| vectors[idx] = vectors[idx].to_a}
@vectors = vectors
else
@vectors = vectors.to_a
end
end

def draw
@x_left_box.each_with_index do |x_left, i|
draw_box x_left, i
draw_whiskers x_left, i
draw_outliers x_left, i
draw_median x_left, i
draw_median x_left, i
end
end

Expand Down Expand Up @@ -111,7 +119,7 @@ def draw_outliers x_left, index

def first_max_outlier_index vector, max
return nil if vector.last >= max
vector.size.times do |i|
vector.size.times do |i|
if vector[i] > max
return i
end
Expand All @@ -131,7 +139,8 @@ def draw_median x_left, index
Rubyplot::Artist::Line2D.new(self,
x: [x_left, x_left + @box_width],
y: [@medians[index], @medians[index]],
color: @median_color
color: @median_color,
width: @median_width
).draw
end

Expand All @@ -141,7 +150,7 @@ def calculate_ranges!
@medians = []
@mins = []
@maxs = []

@vectors.each do |sorted_vec|
m = get_percentile 50, sorted_vec
q1 = get_percentile 25, sorted_vec
Expand Down
6 changes: 3 additions & 3 deletions lib/rubyplot/artist/plot/bubble.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Artist
module Plot
class Bubble < Artist::Plot::Base
# Width in pixels of the border of each bubble.
attr_reader :border_width
attr_accessor :border_width
attr_reader :z_max, :z_min
# Opacity of the circles
attr_accessor :fill_opacity
Expand All @@ -16,8 +16,8 @@ def initialize(*)
end

def data x_values, y_values, z_values
super(x_values, y_values)
@data[:z_values] = z_values
super(x_values.to_a, y_values.to_a)
@data[:z_values] = z_values.to_a
end

def draw
Expand Down
24 changes: 20 additions & 4 deletions lib/rubyplot/artist/plot/candle_stick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ module Rubyplot
module Artist
module Plot
class CandleStick < Artist::Plot::Base
attr_accessor :lows
attr_accessor :highs
attr_accessor :opens
attr_accessor :closes
attr_reader :lows
attr_reader :highs
attr_reader :opens
attr_reader :closes
attr_accessor :bar_width
attr_accessor :x_left_candle
attr_accessor :x_low_stick
Expand All @@ -24,6 +24,22 @@ def initialize(*)
@border_color = :black
end

def lows=(lows_arr)
@lows = lows_arr.to_a
end

def highs=(highs_arr)
@highs = highs_arr.to_a
end

def opens=(opens_arr)
@opens = opens_arr.to_a
end

def closes=(closes_arr)
@closes = closes_arr.to_a
end

def process_data
if @lows.size != @highs.size || @opens.size != @closes.size
raise Rubyplot::SizeError, "all given parameters must be of equal size."
Expand Down
Loading