Skip to content

Commit

Permalink
✨ Introduce Numo::Linalg
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshoku committed Aug 24, 2019
1 parent 379a208 commit 7720a21
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 13 deletions.
20 changes: 12 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
sudo: false
os: linux
dist: trusty
dist: xenial
language: ruby
rvm:
- 2.3
- 2.4
- 2.5
- 2.6
before_install:
- travis_retry gem update --system || travis_retry gem update --system 2.7.8
- travis_retry gem install bundler --no-document || travis_retry gem install bundler --no-document -v 1.17.3
- '2.4'
- '2.5'
- '2.6'

addons:
apt:
packages:
- libopenblas-dev
- liblapacke-dev

before_install:
- gem install bundler -v 2.0.2
18 changes: 17 additions & 1 deletion lib/rumale/base/base_estimator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,24 @@ module BaseEstimator

private

def enable_linalg?
if defined?(Numo::Linalg).nil?
warn('If you want to use features that depend on Numo::Linalg, you should install and load Numo::Linalg in advance.')
return false
end
if Numo::Linalg::VERSION < '0.1.4'
warn('The loaded Numo::Linalg does not implement the methods required by Rumale. Please load Numo::Linalg version 0.1.4 or later.')
return false
end
true
end

def enable_parallel?
return false if @params[:n_jobs].nil? || defined?(Parallel).nil?
return false if @params[:n_jobs].nil?
if defined?(Parallel).nil?
warn('If you want to use parallel option, you should install and load Parallel in advance.')
return false
end
true
end

Expand Down
9 changes: 5 additions & 4 deletions rumale.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ Gem::Specification.new do |spec|

spec.add_runtime_dependency 'numo-narray', '>= 0.9.1'

spec.add_development_dependency 'bundler', '>= 1.16'
spec.add_development_dependency 'coveralls', '~> 0.8'
spec.add_development_dependency 'parallel'
spec.add_development_dependency 'bundler', '~> 2.0'
spec.add_development_dependency 'coveralls', '>= 0.8.23'
spec.add_development_dependency 'numo-linalg', '>= 0.1.4'
spec.add_development_dependency 'parallel', '>= 1.17.0'
spec.add_development_dependency 'rake', '~> 12.0'
spec.add_development_dependency 'rake-compiler'
spec.add_development_dependency 'rake-compiler', '~> 1.0'
spec.add_development_dependency 'rspec', '~> 3.0'
end
87 changes: 87 additions & 0 deletions spec/base/base_estimator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Rumale::Base::BaseEstimator do
let(:dummy) do
class Dummy
include Rumale::Base::BaseEstimator

def initialize
@params = {}
@params[:n_jobs] = 1
end

def linalg?
enable_linalg?
end

def parallel?
enable_parallel?
end
end
Dummy.new
end

describe '#enable_linalg?' do
context 'If Numo::Linalg is loaded' do
it 'returns true' do
expect(dummy.linalg?).to be_truthy
end
end

context 'If Numo::Linalg is not loaded' do
before do
@backup = Numo::Linalg
Numo.class_eval { remove_const(:Linalg) }
end

after { Numo::Linalg = @backup }

it 'returns false' do
expect { dummy.linalg? }.to output(/you should install and load Numo::Linalg in advance./).to_stderr
expect(dummy.linalg?).to be_falsy
end
end

context 'When the version of Numo::Linalg is 0.1.3 or lower' do
before do
@backup = Numo::Linalg::VERSION
Numo::Linalg.class_eval { remove_const(:VERSION) }
Numo::Linalg::VERSION = '0.1.3'
end

after do
Numo::Linalg.class_eval { remove_const(:VERSION) }
Numo::Linalg::VERSION = @backup
end

it 'returns false' do
expect { dummy.linalg? }.to output(/Please load Numo::Linalg version 0.1.4 or later./).to_stderr
expect(dummy.linalg?).to be_falsy
end
end
end

describe '#enable_parallel?' do
context 'If Parallel is loaded' do
it 'returns true' do
expect(dummy.parallel?).to be_truthy
end
end

context 'If Numo::Linalg is not loaded' do
before do
@backup = Parallel
Object.class_eval { remove_const(:Parallel) }
end

after { Parallel = @backup }

it 'returns false' do
expect { dummy.parallel? }.to output(/you should install and load Parallel in advance./).to_stderr
expect(dummy.parallel?).to be_falsy
end
end
end
end
6 changes: 6 additions & 0 deletions spec/kernel_machine/kernel_svc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@
end

it 'estimates class probabilities with three clusters dataset in parallel.' do
# FIXME: Remove Numo::Linalg temporarily for avoiding Parallel::DeadWorker error.
backup = Numo::Linalg
Numo.class_eval { remove_const(:Linalg) }

classes = y_mlt.to_a.uniq.sort
n_classes = classes.size
n_samples = x_mlt.shape[0]
Expand Down Expand Up @@ -118,6 +122,8 @@
expect(probs.shape[1]).to eq(n_classes)
expect(prob_predicted).to eq(y_mlt)
expect(estimator_parallel.score(kernel_mat_mlt, y_mlt)).to eq(1.0)

Numo::Linalg = backup
end

it 'dumps and restores itself using Marshal module.' do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
SimpleCov.start

require 'bundler/setup'
require 'numo/linalg/autoloader'
require 'rumale'
require 'parallel'

Expand Down

0 comments on commit 7720a21

Please sign in to comment.