giuse/machine_learning_workbench

View on GitHub
lib/machine_learning_workbench/optimizer/natural_evolution_strategies/xnes.rb

Summary

Maintainability
B
4 hrs
Test Coverage

Assignment Branch Condition size for initialize_distribution is too high. [60.04/15]
Open

    def initialize_distribution mu_init: 0, sigma_init: 1
      @mu = case mu_init
        when Range # initialize with random in range
          raise ArgumentError, "mu_init: `Range` start/end in `Float`s" \
            unless mu_init.first.kind_of?(Float) && mu_init.last.kind_of?(Float)

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

Method has too many lines. [30/10]
Open

    def initialize_distribution mu_init: 0, sigma_init: 1
      @mu = case mu_init
        when Range # initialize with random in range
          raise ArgumentError, "mu_init: `Range` start/end in `Float`s" \
            unless mu_init.first.kind_of?(Float) && mu_init.last.kind_of?(Float)

This cop checks if the length of a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.

Method initialize_distribution has a Cognitive Complexity of 24 (exceeds 5 allowed). Consider refactoring.
Open

    def initialize_distribution mu_init: 0, sigma_init: 1
      @mu = case mu_init
        when Range # initialize with random in range
          raise ArgumentError, "mu_init: `Range` start/end in `Float`s" \
            unless mu_init.first.kind_of?(Float) && mu_init.last.kind_of?(Float)

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Assignment Branch Condition size for train is too high. [28.79/15]
Open

    def train picks: sorted_inds
      g_mu = utils.dot(picks)
      g_log_sigma = popsize.times.inject(NArray.zeros sigma.shape) do |sum, i|
        u = utils[i]
        ind = picks[i, true]

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

Cyclomatic complexity for initialize_distribution is too high. [16/6]
Open

    def initialize_distribution mu_init: 0, sigma_init: 1
      @mu = case mu_init
        when Range # initialize with random in range
          raise ArgumentError, "mu_init: `Range` start/end in `Float`s" \
            unless mu_init.first.kind_of?(Float) && mu_init.last.kind_of?(Float)

This cop checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.

An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one.

Perceived complexity for initialize_distribution is too high. [12/7]
Open

    def initialize_distribution mu_init: 0, sigma_init: 1
      @mu = case mu_init
        when Range # initialize with random in range
          raise ArgumentError, "mu_init: `Range` start/end in `Float`s" \
            unless mu_init.first.kind_of?(Float) && mu_init.last.kind_of?(Float)

This cop tries to produce a complexity score that's a measure of the complexity the reader experiences when looking at a method. For that reason it considers when nodes as something that doesn't add as much complexity as an if or a &&. Except if it's one of those special case/when constructs where there's no expression after case. Then the cop treats it as an if/elsif/elsif... and lets all the when nodes count. In contrast to the CyclomaticComplexity cop, this cop considers else nodes as adding complexity.

Example:

def my_method                   # 1
  if cond                       # 1
    case var                    # 2 (0.8 + 4 * 0.2, rounded)
    when 1 then func_one
    when 2 then func_two
    when 3 then func_three
    when 4..10 then func_other
    end
  else                          # 1
    do_something until a && b   # 2
  end                           # ===
end                             # 7 complexity points

Method initialize_distribution has 30 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    def initialize_distribution mu_init: 0, sigma_init: 1
      @mu = case mu_init
        when Range # initialize with random in range
          raise ArgumentError, "mu_init: `Range` start/end in `Float`s" \
            unless mu_init.first.kind_of?(Float) && mu_init.last.kind_of?(Float)

    Use Array.new(ndims) with a block instead of .times.map only if ndims is always 0 or more.
    Open

              NArray[*ndims.times.map { mu_rng.rand mu_init }]

    This cop checks for .times.map calls. In most cases such calls can be replaced with an explicit array creation.

    Example:

    # bad
    9.times.map do |i|
      i.to_s
    end
    
    # good
    Array.new(9) do |i|
      i.to_s
    end

    end at 25, 6 is not aligned with case at 9, 12.
    Open

          end

    This cop checks whether the end keywords are aligned properly.

    Three modes are supported through the EnforcedStyleAlignWith configuration parameter:

    If it's set to keyword (which is the default), the end shall be aligned with the start of the keyword (if, class, etc.).

    If it's set to variable the end shall be aligned with the left-hand-side of the variable assignment, if there is one.

    If it's set to start_of_line, the end shall be aligned with the start of the line where the matching keyword appears.

    Example: EnforcedStyleAlignWith: keyword (default)

    # bad
    
    variable = if true
        end
    
    # good
    
    variable = if true
               end

    Example: EnforcedStyleAlignWith: variable

    # bad
    
    variable = if true
        end
    
    # good
    
    variable = if true
    end

    Example: EnforcedStyleAlignWith: startofline

    # bad
    
    variable = if true
        end
    
    # good
    
    puts(if true
    end)

    Surrounding space missing for operator /.
    Open

          @log_sigma += g_log_sigma * (lrate/2)

    Checks that operators have space around them, except for ** which should not have surrounding space.

    Example:

    # bad
    total = 3*4
    "apple"+"juice"
    my_number = 38/4
    a ** b
    
    # good
    total = 3 * 4
    "apple" + "juice"
    my_number = 38 / 4
    a**b

    end at 37, 6 is not aligned with case at 26, 15.
    Open

          end

    This cop checks whether the end keywords are aligned properly.

    Three modes are supported through the EnforcedStyleAlignWith configuration parameter:

    If it's set to keyword (which is the default), the end shall be aligned with the start of the keyword (if, class, etc.).

    If it's set to variable the end shall be aligned with the left-hand-side of the variable assignment, if there is one.

    If it's set to start_of_line, the end shall be aligned with the start of the line where the matching keyword appears.

    Example: EnforcedStyleAlignWith: keyword (default)

    # bad
    
    variable = if true
        end
    
    # good
    
    variable = if true
               end

    Example: EnforcedStyleAlignWith: variable

    # bad
    
    variable = if true
        end
    
    # good
    
    variable = if true
    end

    Example: EnforcedStyleAlignWith: startofline

    # bad
    
    variable = if true
        end
    
    # good
    
    puts(if true
    end)

    Add parentheses to nested method call NArray.zeros sigma.shape.
    Open

          g_log_sigma = popsize.times.inject(NArray.zeros sigma.shape) do |sum, i|

    This cop checks for unparenthesized method calls in the argument list of a parenthesized method call.

    Example:

    # good
    method1(method2(arg), method3(arg))
    
    # bad
    method1(method2 arg, method3, arg)

    Indent when as deep as case.
    Open

            when Numeric

    This cop checks how the whens of a case expression are indented in relation to its case or end keyword.

    It will register a separate offense for each misaligned when.

    Example:

    # If Layout/EndAlignment is set to keyword style (default)
    # *case* and *end* should always be aligned to same depth,
    # and therefore *when* should always be aligned to both -
    # regardless of configuration.
    
    # bad for all styles
    case n
      when 0
        x * 2
      else
        y / 3
    end
    
    # good for all styles
    case n
    when 0
      x * 2
    else
      y / 3
    end

    Example: EnforcedStyle: case (default)

    # if EndAlignment is set to other style such as
    # start_of_line (as shown below), then *when* alignment
    # configuration does have an effect.
    
    # bad
    a = case n
    when 0
      x * 2
    else
      y / 3
    end
    
    # good
    a = case n
        when 0
          x * 2
        else
          y / 3
    end

    Example: EnforcedStyle: end

    # bad
    a = case n
        when 0
          x * 2
        else
          y / 3
    end
    
    # good
    a = case n
    when 0
      x * 2
    else
      y / 3
    end

    Indent when as deep as case.
    Open

            when Array

    This cop checks how the whens of a case expression are indented in relation to its case or end keyword.

    It will register a separate offense for each misaligned when.

    Example:

    # If Layout/EndAlignment is set to keyword style (default)
    # *case* and *end* should always be aligned to same depth,
    # and therefore *when* should always be aligned to both -
    # regardless of configuration.
    
    # bad for all styles
    case n
      when 0
        x * 2
      else
        y / 3
    end
    
    # good for all styles
    case n
    when 0
      x * 2
    else
      y / 3
    end

    Example: EnforcedStyle: case (default)

    # if EndAlignment is set to other style such as
    # start_of_line (as shown below), then *when* alignment
    # configuration does have an effect.
    
    # bad
    a = case n
    when 0
      x * 2
    else
      y / 3
    end
    
    # good
    a = case n
        when 0
          x * 2
        else
          y / 3
    end

    Example: EnforcedStyle: end

    # bad
    a = case n
        when 0
          x * 2
        else
          y / 3
    end
    
    # good
    a = case n
    when 0
      x * 2
    else
      y / 3
    end

    Use nested module/class definitions instead of compact style.
    Open

    module MachineLearningWorkbench::Optimizer::NaturalEvolutionStrategies

    This cop checks the style of children definitions at classes and modules. Basically there are two different styles:

    Example: EnforcedStyle: nested (default)

    # good
    # have each child on its own line
    class Foo
      class Bar
      end
    end

    Example: EnforcedStyle: compact

    # good
    # combine definitions as much as possible
    class Foo::Bar
    end

    The compact style is only forced for classes/modules with one child.

    Use def with parentheses when there are parameters.
    Open

        def train picks: sorted_inds

    This cops checks for parentheses around the arguments in method definitions. Both instance and class/singleton methods are checked.

    Example: EnforcedStyle: require_parentheses (default)

    # The `require_parentheses` style requires method definitions
    # to always use parentheses
    
    # bad
    def bar num1, num2
      num1 + num2
    end
    
    def foo descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name
      do_something
    end
    
    # good
    def bar(num1, num2)
      num1 + num2
    end
    
    def foo(descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name)
      do_something
    end

    Example: EnforcedStyle: requirenoparentheses

    # The `require_no_parentheses` style requires method definitions
    # to never use parentheses
    
    # bad
    def bar(num1, num2)
      num1 + num2
    end
    
    def foo(descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name)
      do_something
    end
    
    # good
    def bar num1, num2
      num1 + num2
    end
    
    def foo descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name
      do_something
    end

    Example: EnforcedStyle: requirenoparenthesesexceptmultiline

    # The `require_no_parentheses_except_multiline` style prefers no
    # parantheses when method definition arguments fit on single line,
    # but prefers parantheses when arguments span multiple lines.
    
    # bad
    def bar(num1, num2)
      num1 + num2
    end
    
    def foo descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name
      do_something
    end
    
    # good
    def bar num1, num2
      num1 + num2
    end
    
    def foo(descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name)
      do_something
    end

    Indent when as deep as case.
    Open

            when NArray

    This cop checks how the whens of a case expression are indented in relation to its case or end keyword.

    It will register a separate offense for each misaligned when.

    Example:

    # If Layout/EndAlignment is set to keyword style (default)
    # *case* and *end* should always be aligned to same depth,
    # and therefore *when* should always be aligned to both -
    # regardless of configuration.
    
    # bad for all styles
    case n
      when 0
        x * 2
      else
        y / 3
    end
    
    # good for all styles
    case n
    when 0
      x * 2
    else
      y / 3
    end

    Example: EnforcedStyle: case (default)

    # if EndAlignment is set to other style such as
    # start_of_line (as shown below), then *when* alignment
    # configuration does have an effect.
    
    # bad
    a = case n
    when 0
      x * 2
    else
      y / 3
    end
    
    # good
    a = case n
        when 0
          x * 2
        else
          y / 3
    end

    Example: EnforcedStyle: end

    # bad
    a = case n
        when 0
          x * 2
        else
          y / 3
    end
    
    # good
    a = case n
    when 0
      x * 2
    else
      y / 3
    end

    Indent when as deep as case.
    Open

            when Range # initialize with random in range

    This cop checks how the whens of a case expression are indented in relation to its case or end keyword.

    It will register a separate offense for each misaligned when.

    Example:

    # If Layout/EndAlignment is set to keyword style (default)
    # *case* and *end* should always be aligned to same depth,
    # and therefore *when* should always be aligned to both -
    # regardless of configuration.
    
    # bad for all styles
    case n
      when 0
        x * 2
      else
        y / 3
    end
    
    # good for all styles
    case n
    when 0
      x * 2
    else
      y / 3
    end

    Example: EnforcedStyle: case (default)

    # if EndAlignment is set to other style such as
    # start_of_line (as shown below), then *when* alignment
    # configuration does have an effect.
    
    # bad
    a = case n
    when 0
      x * 2
    else
      y / 3
    end
    
    # good
    a = case n
        when 0
          x * 2
        else
          y / 3
    end

    Example: EnforcedStyle: end

    # bad
    a = case n
        when 0
          x * 2
        else
          y / 3
    end
    
    # good
    a = case n
    when 0
      x * 2
    else
      y / 3
    end

    Prefer Object#is_a? over Object#kind_of?.
    Open

                unless mu_init.first.kind_of?(Float) && mu_init.last.kind_of?(Float)

    This cop enforces consistent use of Object#is_a? or Object#kind_of?.

    Example: EnforcedStyle: is_a? (default)

    # bad
    var.kind_of?(Date)
    var.kind_of?(Integer)
    
    # good
    var.is_a?(Date)
    var.is_a?(Integer)

    Example: EnforcedStyle: kind_of?

    # bad
    var.is_a?(Time)
    var.is_a?(String)
    
    # good
    var.kind_of?(Time)
    var.kind_of?(String)

    Indent when as deep as case.
    Open

            when NArray

    This cop checks how the whens of a case expression are indented in relation to its case or end keyword.

    It will register a separate offense for each misaligned when.

    Example:

    # If Layout/EndAlignment is set to keyword style (default)
    # *case* and *end* should always be aligned to same depth,
    # and therefore *when* should always be aligned to both -
    # regardless of configuration.
    
    # bad for all styles
    case n
      when 0
        x * 2
      else
        y / 3
    end
    
    # good for all styles
    case n
    when 0
      x * 2
    else
      y / 3
    end

    Example: EnforcedStyle: case (default)

    # if EndAlignment is set to other style such as
    # start_of_line (as shown below), then *when* alignment
    # configuration does have an effect.
    
    # bad
    a = case n
    when 0
      x * 2
    else
      y / 3
    end
    
    # good
    a = case n
        when 0
          x * 2
        else
          y / 3
    end

    Example: EnforcedStyle: end

    # bad
    a = case n
        when 0
          x * 2
        else
          y / 3
    end
    
    # good
    a = case n
    when 0
      x * 2
    else
      y / 3
    end

    Prefer single-quoted strings when you don't need string interpolation or special symbols.
    Open

              raise ArgumentError, "mu_init: `Range` start/end in `Float`s" \

    Checks if uses of quotes match the configured preference.

    Example: EnforcedStyle: single_quotes (default)

    # bad
    "No special symbols"
    "No string interpolation"
    "Just text"
    
    # good
    'No special symbols'
    'No string interpolation'
    'Just text'
    "Wait! What's #{this}!"

    Example: EnforcedStyle: double_quotes

    # bad
    'Just some text'
    'No special chars or interpolation'
    
    # good
    "Just some text"
    "No special chars or interpolation"
    "Every string in #{project} uses double_quotes"

    Indent when as deep as case.
    Open

            when Array

    This cop checks how the whens of a case expression are indented in relation to its case or end keyword.

    It will register a separate offense for each misaligned when.

    Example:

    # If Layout/EndAlignment is set to keyword style (default)
    # *case* and *end* should always be aligned to same depth,
    # and therefore *when* should always be aligned to both -
    # regardless of configuration.
    
    # bad for all styles
    case n
      when 0
        x * 2
      else
        y / 3
    end
    
    # good for all styles
    case n
    when 0
      x * 2
    else
      y / 3
    end

    Example: EnforcedStyle: case (default)

    # if EndAlignment is set to other style such as
    # start_of_line (as shown below), then *when* alignment
    # configuration does have an effect.
    
    # bad
    a = case n
    when 0
      x * 2
    else
      y / 3
    end
    
    # good
    a = case n
        when 0
          x * 2
        else
          y / 3
    end

    Example: EnforcedStyle: end

    # bad
    a = case n
        when 0
          x * 2
        else
          y / 3
    end
    
    # good
    a = case n
    when 0
      x * 2
    else
      y / 3
    end

    Indent when as deep as case.
    Open

            when Numeric

    This cop checks how the whens of a case expression are indented in relation to its case or end keyword.

    It will register a separate offense for each misaligned when.

    Example:

    # If Layout/EndAlignment is set to keyword style (default)
    # *case* and *end* should always be aligned to same depth,
    # and therefore *when* should always be aligned to both -
    # regardless of configuration.
    
    # bad for all styles
    case n
      when 0
        x * 2
      else
        y / 3
    end
    
    # good for all styles
    case n
    when 0
      x * 2
    else
      y / 3
    end

    Example: EnforcedStyle: case (default)

    # if EndAlignment is set to other style such as
    # start_of_line (as shown below), then *when* alignment
    # configuration does have an effect.
    
    # bad
    a = case n
    when 0
      x * 2
    else
      y / 3
    end
    
    # good
    a = case n
        when 0
          x * 2
        else
          y / 3
    end

    Example: EnforcedStyle: end

    # bad
    a = case n
        when 0
          x * 2
        else
          y / 3
    end
    
    # good
    a = case n
    when 0
      x * 2
    else
      y / 3
    end

    Ambiguous block operator. Parenthesize the method arguments if it's surely a block operator, or add a whitespace to the right of the & if it should be a binary AND.
    Open

          @mu, @log_sigma = data.map &:to_na

    This cop checks for ambiguous operators in the first argument of a method invocation without parentheses.

    Example:

    # bad
    
    # The `*` is interpreted as a splat operator but it could possibly be
    # a `*` method invocation (i.e. `do_something.*(some_array)`).
    do_something *some_array

    Example:

    # good
    
    # With parentheses, there's no ambiguity.
    do_something(*some_array)

    Space missing after comma.
    Open

              NArray.new([1,ndims]).fill mu_init

    Checks for comma (,) not followed by some kind of space.

    Example:

    # bad
    [1,2]
    { foo:bar,}
    
    # good
    [1, 2]
    { foo:bar, }

    Prefer Object#is_a? over Object#kind_of?.
    Open

                unless mu_init.first.kind_of?(Float) && mu_init.last.kind_of?(Float)

    This cop enforces consistent use of Object#is_a? or Object#kind_of?.

    Example: EnforcedStyle: is_a? (default)

    # bad
    var.kind_of?(Date)
    var.kind_of?(Integer)
    
    # good
    var.is_a?(Date)
    var.is_a?(Integer)

    Example: EnforcedStyle: kind_of?

    # bad
    var.is_a?(Time)
    var.is_a?(String)
    
    # good
    var.kind_of?(Time)
    var.kind_of?(String)

    Use def with parentheses when there are parameters.
    Open

        def load data

    This cops checks for parentheses around the arguments in method definitions. Both instance and class/singleton methods are checked.

    Example: EnforcedStyle: require_parentheses (default)

    # The `require_parentheses` style requires method definitions
    # to always use parentheses
    
    # bad
    def bar num1, num2
      num1 + num2
    end
    
    def foo descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name
      do_something
    end
    
    # good
    def bar(num1, num2)
      num1 + num2
    end
    
    def foo(descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name)
      do_something
    end

    Example: EnforcedStyle: requirenoparentheses

    # The `require_no_parentheses` style requires method definitions
    # to never use parentheses
    
    # bad
    def bar(num1, num2)
      num1 + num2
    end
    
    def foo(descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name)
      do_something
    end
    
    # good
    def bar num1, num2
      num1 + num2
    end
    
    def foo descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name
      do_something
    end

    Example: EnforcedStyle: requirenoparenthesesexceptmultiline

    # The `require_no_parentheses_except_multiline` style prefers no
    # parantheses when method definition arguments fit on single line,
    # but prefers parantheses when arguments span multiple lines.
    
    # bad
    def bar(num1, num2)
      num1 + num2
    end
    
    def foo descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name
      do_something
    end
    
    # good
    def bar num1, num2
      num1 + num2
    end
    
    def foo(descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name)
      do_something
    end

    Line is too long. [82/80]
    Open

              raise ArgumentError, "Something is wrong with sigma_init: #{sigma_init}"

    Line is too long. [86/80]
    Open

          # Works with the log of sigma to avoid continuous decompositions (thanks Sun Yi)

    Use def with parentheses when there are parameters.
    Open

        def initialize_distribution mu_init: 0, sigma_init: 1

    This cops checks for parentheses around the arguments in method definitions. Both instance and class/singleton methods are checked.

    Example: EnforcedStyle: require_parentheses (default)

    # The `require_parentheses` style requires method definitions
    # to always use parentheses
    
    # bad
    def bar num1, num2
      num1 + num2
    end
    
    def foo descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name
      do_something
    end
    
    # good
    def bar(num1, num2)
      num1 + num2
    end
    
    def foo(descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name)
      do_something
    end

    Example: EnforcedStyle: requirenoparentheses

    # The `require_no_parentheses` style requires method definitions
    # to never use parentheses
    
    # bad
    def bar(num1, num2)
      num1 + num2
    end
    
    def foo(descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name)
      do_something
    end
    
    # good
    def bar num1, num2
      num1 + num2
    end
    
    def foo descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name
      do_something
    end

    Example: EnforcedStyle: requirenoparenthesesexceptmultiline

    # The `require_no_parentheses_except_multiline` style prefers no
    # parantheses when method definition arguments fit on single line,
    # but prefers parantheses when arguments span multiple lines.
    
    # bad
    def bar(num1, num2)
      num1 + num2
    end
    
    def foo descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name
      do_something
    end
    
    # good
    def bar num1, num2
      num1 + num2
    end
    
    def foo(descriptive_var_name,
            another_descriptive_var_name,
            last_descriptive_var_name)
      do_something
    end

    There are no issues that match your filters.

    Category
    Status