Sha256: bf2b30c63abfba544fbc868cfe7858c70ead24c22862e56e3dcc0d08561bcf31

Contents?: true

Size: 1.54 KB

Versions: 7

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require 'numo/narray'

module Rumale
  # This module consists of basic mix-in classes.
  module Base
    # Base class for all estimators in Rumale.
    class Estimator
      # Return parameters about an estimator.
      # @return [Hash]
      attr_reader :params

      private

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

      def enable_parallel?(warning: true)
        return false if @params[:n_jobs].nil?

        unless defined?(Parallel)
          if warning
            warn('If you want to use parallel option, ' \
                 'you should install and load Parallel in advance.')
          end
          return false
        end
        true
      end

      def n_processes
        return 1 unless enable_parallel?(warning: false)

        @params[:n_jobs] <= 0 ? Parallel.processor_count : @params[:n_jobs]
      end

      def parallel_map(n_outputs, &block)
        Parallel.map(Array.new(n_outputs) { |v| v }, in_processes: n_processes, &block)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rumale-core-0.29.0 lib/rumale/base/estimator.rb
rumale-core-0.28.1 lib/rumale/base/estimator.rb
rumale-core-0.28.0 lib/rumale/base/estimator.rb
rumale-core-0.27.0 lib/rumale/base/estimator.rb
rumale-core-0.26.0 lib/rumale/base/estimator.rb
rumale-core-0.25.0 lib/rumale/base/estimator.rb
rumale-core-0.24.0 lib/rumale/base/estimator.rb