Skip to content

Commit

Permalink
Support non zero origin (#94)
Browse files Browse the repository at this point in the history
Fix GH-93
  • Loading branch information
mrkn authored Aug 19, 2022
1 parent 3b92c44 commit 6f1fb90
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ jobs:

- run: sudo apt install build-essential libsqlite3-dev

- run: npm install playwright@latest

- run: bundle install --jobs 4 --retry 3 --without "nmatrix python"

- run: bundle exec rake
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/nmatrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ jobs:

- run: sudo apt install build-essential libsqlite3-dev

- run: npm install playwright@latest

- run: bundle install --jobs 4 --retry 3 --without "numo python"

- run: bundle exec rake
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/pycall.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ jobs:

- run: sudo apt install build-essential libsqlite3-dev

- run: npm install playwright@latest

- run: pip install --user matplotlib pandas

- run: bundle install --jobs 4 --retry 3 --without "nmatrix numo"
Expand Down
4 changes: 2 additions & 2 deletions lib/charty/backends/plotly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ def scatter(x, y, variables, color:, color_mapper:,

groups = (0 ... x.length).group_by do |i|
key = {}
key[:color] = color[i] unless color.nil?
key[:style] = style[i] unless style.nil?
key[:color] = color.iloc(i) unless color.nil?
key[:style] = style.iloc(i) unless style.nil?
key
end

Expand Down
16 changes: 10 additions & 6 deletions lib/charty/linspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ def initialize(range, num_step)
end

def each(&block)
step = (@range.end - @range.begin).to_r / (@num_step - 1)
(@num_step - 1).times do |i|
block.call(@range.begin + i * step)
end
if @num_step == 1
block.call(@range.begin)
else
step = (@range.end - @range.begin).to_r / (@num_step - 1)
(@num_step - 1).times do |i|
block.call(@range.begin + i * step)
end

unless @range.exclude_end?
block.call(@range.end)
unless @range.exclude_end?
block.call(@range.end)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/charty/plotters/abstract_plotter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def title=(val)
elsif vector.categorical?
:categorical
else
case vector[0]
case vector.iloc(0)
when true, false
boolean_type
else
Expand Down
1 change: 1 addition & 0 deletions lib/charty/vector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def initialize(data, index: nil, name: nil)

alias size length

def_delegators :adapter, :iloc
def_delegators :adapter, :to_a
def_delegators :adapter, :each
def_delegators :adapter, :empty?
Expand Down
4 changes: 3 additions & 1 deletion lib/charty/vector_adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def ==(other)
end
end

def_delegator :data, :[], :iloc

def_delegators :data, :[], :[]=
def_delegators :data, :each, :to_a, :empty?

Expand Down Expand Up @@ -148,7 +150,7 @@ def [](key)
when Charty::Vector
where(key)
else
super(key_to_loc(key))
iloc(key_to_loc(key))
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/charty/vector_adapters/pandas_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def compare_data_equality(other)
data.equals(Pandas::Series.new(other, index: data.index))
end

def iloc(i)
data.iloc[i]
end

def [](key)
case key
when Charty::Vector
Expand Down
3 changes: 2 additions & 1 deletion lib/charty/vector_adapters/vector_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def initialize(data, index: nil)
:uniq,
:unique_values,
:values_at,
:where
:where,
:iloc

def compare_data_equality(other)
if other.is_a?(self.class)
Expand Down
40 changes: 40 additions & 0 deletions test/plot_methods/bar_plot_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,44 @@ def test_bar_plot_sd(data)
end
end
end

sub_test_case("with nonzero origin index") do # [GH-93]
def setup
numpy_required
pandas_required

data = Numpy.arange(300).reshape([100, 3])
@df = Pandas::DataFrame.new(data, index: (300...400).to_a, columns: %w[a b c])

Charty::Backends.use(:plotly)
end

def test_bar_plot_with_single_category
@df[:categorical_column] = "category_name"

plot = Charty.bar_plot(
data: @df,
x: :a,
y: :b,
color: :categorical_column
)
assert_nothing_raised do
plot.render()
end
end

def test_bar_plot_with_multiple_categories
@df[:categorical_column] = ["A", "B"] * 50

plot = Charty.bar_plot(
data: @df,
x: :a,
y: :b,
color: :categorical_column
)
assert_nothing_raised do
plot.render()
end
end
end
end
42 changes: 42 additions & 0 deletions test/plot_methods/scatter_plot_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,46 @@ def setup_pandas_dataframe_data
@data = Pandas::DataFrame.new(data: @data)
end
end

sub_test_case("with nonzero origin index") do # [GH-93]
def setup
numpy_required
pandas_required

data = Numpy.arange(300).reshape([100, 3])
@df = Pandas::DataFrame.new(data, index: (300...400).to_a, columns: %w[a b c])

Charty::Backends.use(:plotly)
end

def test_scatter_plot_with_single_category
@df[:categorical_column] = "category_name"

plot = Charty.scatter_plot(
data: @df,
x: :a,
y: :b,
color: :categorical_column
)

assert_nothing_raised do
plot.render()
end
end

def test_scatter_plot_with_multiple_categories
@df[:categorical_column] = ["A", "B"] * 50

Charty::Backends.use(:plotly)
plot = Charty.scatter_plot(
data: @df,
x: :a,
y: :b,
color: :categorical_column
)
assert_nothing_raised do
plot.render()
end
end
end
end
52 changes: 52 additions & 0 deletions test/vector/iloc_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class VectorIlocTest < Test::Unit::TestCase
include Charty::TestHelpers

sub_test_case("Charty::Vector#iloc") do
data(:adapter_type, [:array, :daru, :narray, :numpy, :pandas], keep: true)

test("with the default index") do |data|
vector = setup_vector(data[:adapter_type], [10, 20, 30])
assert_equal([20, 10, 30],
[vector.iloc(1), vector.iloc(0), vector.iloc(2)])
end

test("with non-zero origin index") do |data|
vector = setup_vector(data[:adapter_type], [10, 20, 30], index: [5, 10, 15])
assert_equal([20, 10, 30],
[vector.iloc(1), vector.iloc(0), vector.iloc(2)])
end

test("with string index") do |data|
vector = setup_vector(data[:adapter_type], [10, 20, 30], index: ["a", "b", "c"])
assert_equal([20, 10, 30],
[vector.iloc(1), vector.iloc(0), vector.iloc(2)])
end
end

def setup_vector(adapter_type, data, index: nil)
send("setup_vector_with_#{adapter_type}", data, index)
end

def setup_vector_with_array(data, index)
Charty::Vector.new(data, index: index)
end

def setup_vector_with_daru(data, index)
Charty::Vector.new(Daru::Vector.new(data), index: index)
end

def setup_vector_with_narray(data, index)
numo_required
Charty::Vector.new(Numo::Int64[*data], index: index)
end

def setup_vector_with_numpy(data, index)
numpy_required
Charty::Vector.new(Numpy.asarray(data, dtype: "int64"), index: index)
end

def setup_vector_with_pandas(data, index)
pandas_required
Charty::Vector.new(Pandas::Series.new(data), index: index)
end
end

0 comments on commit 6f1fb90

Please sign in to comment.