kengz/SLM-Lab

View on GitHub

Showing 37 of 37 total issues

Cyclomatic complexity is too high in function update_online_stats. (11)
Open

def update_online_stats(body, state):
    '''
    Method to calculate the running mean and standard deviation of the state space.
    See https://www.johndcook.com/blog/standard_deviation/ for more details
    for n >= 1

Cyclomatic Complexity

Cyclomatic Complexity corresponds to the number of decisions a block of code contains plus 1. This number (also called McCabe number) is equal to the number of linearly independent paths through the code. This number can be used as a guide when testing conditional logic in blocks.

Radon analyzes the AST tree of a Python program to compute Cyclomatic Complexity. Statements have the following effects on Cyclomatic Complexity:

Construct Effect on CC Reasoning
if +1 An if statement is a single decision.
elif +1 The elif statement adds another decision.
else +0 The else statement does not cause a new decision. The decision is at the if.
for +1 There is a decision at the start of the loop.
while +1 There is a decision at the while statement.
except +1 Each except branch adds a new conditional path of execution.
finally +0 The finally block is unconditionally executed.
with +1 The with statement roughly corresponds to a try/except block (see PEP 343 for details).
assert +1 The assert statement internally roughly equals a conditional statement.
Comprehension +1 A list/set/dict comprehension of generator expression is equivalent to a for loop.
Boolean Operator +1 Every boolean operator (and, or) adds a decision point.

Source: http://radon.readthedocs.org/en/latest/intro.html

Function flatten_dict has a Cognitive Complexity of 18 (exceeds 10 allowed). Consider refactoring.
Open

def flatten_dict(obj, delim='.'):
    '''Missing pydash method to flatten dict'''
    nobj = {}
    for key, val in obj.items():
        if ps.is_dict(val) and not ps.is_empty(val):
Severity: Minor
Found in slm_lab/lib/util.py - About 1 hr to fix

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

Function plot_go has 17 arguments (exceeds 10 allowed). Consider refactoring.
Open

def plot_go(
Severity: Major
Found in slm_lab/lib/viz.py - About 1 hr to fix

    Function run has a Cognitive Complexity of 13 (exceeds 10 allowed). Consider refactoring.
    Open

        def run(self):
            run_trial = create_remote_fn(self.experiment)
            meta_spec = self.experiment.spec['meta']
            ray.init(**meta_spec.get('resources', {}))
            register_ray_serializer()
    Severity: Minor
    Found in slm_lab/experiment/search.py - About 45 mins to fix

    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

    Function retro_eval has a Cognitive Complexity of 13 (exceeds 10 allowed). Consider refactoring.
    Open

    def retro_eval(predir, session_index=None):
        '''
        Method to run eval sessions by scanning a predir for ckpt files. Used to rerun failed eval sessions.
        @example
    
    
    Severity: Minor
    Found in slm_lab/experiment/retro_analysis.py - About 45 mins to fix

    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

    Function gen_assert_trained has a Cognitive Complexity of 13 (exceeds 10 allowed). Consider refactoring.
    Open

    def gen_assert_trained(pre_model):
        '''
        Generate assert_trained function used to check weight updates
        @example
    
    
    Severity: Minor
    Found in slm_lab/agent/net/net_util.py - About 45 mins to fix

    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

    Function try_ckpt has a Cognitive Complexity of 13 (exceeds 10 allowed). Consider refactoring.
    Open

        def try_ckpt(self, agent, env):
            '''Try to checkpoint agent at the start, save_freq, and the end'''
            tick = env.clock.get(env.max_tick_unit)
            to_ckpt = False
            if not util.in_eval_lab_modes() and tick <= env.max_tick:
    Severity: Minor
    Found in slm_lab/experiment/control.py - About 45 mins to fix

    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

    Function normalize_state has a Cognitive Complexity of 12 (exceeds 10 allowed). Consider refactoring.
    Open

    def normalize_state(body, state):
        '''
        Normalizes one or more states using a running mean and standard deviation
        Details of the normalization from Deep RL Bootcamp, L6
        https://www.youtube.com/watch?v=8EcdaCk9KaQ&feature=youtu.be
    Severity: Minor
    Found in slm_lab/agent/algorithm/policy_util.py - About 35 mins to fix

    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

    Function get_policy_out_dim has a Cognitive Complexity of 12 (exceeds 10 allowed). Consider refactoring.
    Open

    def get_policy_out_dim(body):
        '''Helper method to construct the policy network out_dim for a body according to is_discrete, action_type'''
        action_dim = body.action_dim
        if body.is_discrete:
            if body.action_type == 'multi_discrete':
    Severity: Minor
    Found in slm_lab/agent/net/net_util.py - About 35 mins to fix

    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

    Avoid too many return statements within this function.
    Open

            return 'multi_binary'
    Severity: Major
    Found in slm_lab/experiment/monitor.py - About 30 mins to fix

      Avoid too many return statements within this function.
      Open

                  return np.clip((state - body.state_mean) / body.state_std_dev, -10, 10)
      Severity: Major
      Found in slm_lab/agent/algorithm/policy_util.py - About 30 mins to fix

        Avoid too many return statements within this function.
        Open

                    return np.clip(state - body.state_mean, -10, 10)
        Severity: Major
        Found in slm_lab/agent/algorithm/policy_util.py - About 30 mins to fix

          Function space_train has a Cognitive Complexity of 11 (exceeds 10 allowed). Consider refactoring.
          Open

              def space_train(self):
                  '''
                  Completes one training step for the agent if it is time to train.
                  i.e. the environment timestep is greater than the minimum training timestep and a multiple of the training_frequency.
                  Each training step consists of sampling n batches from the agent's memory.
          Severity: Minor
          Found in slm_lab/agent/algorithm/hydra_dqn.py - About 25 mins to fix

          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

          Ambiguous variable name 'l'
          Open

                  margin=go.layout.Margin(l=60, r=60, t=60, b=60),
          Severity: Minor
          Found in slm_lab/lib/viz.py by pep8

          Never use the characters 'l', 'O', or 'I' as variable names.

          In some fonts, these characters are indistinguishable from the numerals
          one and zero. When tempted to use 'l', use 'L' instead.
          
          Okay: L = 0
          Okay: o = 123
          Okay: i = 42
          E741: l = 0
          E741: O = 123
          E741: I = 42
          
          Variables can be bound in several other contexts, including class and
          function definitions, 'global' and 'nonlocal' statements, exception
          handlers, and 'with' statements.
          
          Okay: except AttributeError as o:
          Okay: with lock as L:
          E741: except AttributeError as O:
          E741: with lock as l:
          E741: global I
          E741: nonlocal l
          E742: class I(object):
          E743: def l(x):

          Module level import not at top of file
          Open

          import sys
          Severity: Minor
          Found in run_lab.py by pep8

          Place imports at the top of the file.

          Always put imports at the top of the file, just after any module comments
          and docstrings, and before module globals and constants.
          
          Okay: import os
          Okay: # this is a comment\nimport os
          Okay: '''this is a module docstring'''\nimport os
          Okay: r'''this is a module docstring'''\nimport os
          Okay:
          try:\n\timport x\nexcept ImportError:\n\tpass\nelse:\n\tpass\nimport y
          Okay:
          try:\n\timport x\nexcept ImportError:\n\tpass\nfinally:\n\tpass\nimport y
          E402: a=1\nimport os
          E402: 'One string'\n"Two string"\nimport os
          E402: a=1\nfrom sys import x
          
          Okay: if x:\n    import os

          Module level import not at top of file
          Open

          import torch.multiprocessing as mp
          Severity: Minor
          Found in run_lab.py by pep8

          Place imports at the top of the file.

          Always put imports at the top of the file, just after any module comments
          and docstrings, and before module globals and constants.
          
          Okay: import os
          Okay: # this is a comment\nimport os
          Okay: '''this is a module docstring'''\nimport os
          Okay: r'''this is a module docstring'''\nimport os
          Okay:
          try:\n\timport x\nexcept ImportError:\n\tpass\nelse:\n\tpass\nimport y
          Okay:
          try:\n\timport x\nexcept ImportError:\n\tpass\nfinally:\n\tpass\nimport y
          E402: a=1\nimport os
          E402: 'One string'\n"Two string"\nimport os
          E402: a=1\nfrom sys import x
          
          Okay: if x:\n    import os

          Module level import not at top of file
          Open

          from slm_lab.spec import spec_util
          Severity: Minor
          Found in run_lab.py by pep8

          Place imports at the top of the file.

          Always put imports at the top of the file, just after any module comments
          and docstrings, and before module globals and constants.
          
          Okay: import os
          Okay: # this is a comment\nimport os
          Okay: '''this is a module docstring'''\nimport os
          Okay: r'''this is a module docstring'''\nimport os
          Okay:
          try:\n\timport x\nexcept ImportError:\n\tpass\nelse:\n\tpass\nimport y
          Okay:
          try:\n\timport x\nexcept ImportError:\n\tpass\nfinally:\n\tpass\nimport y
          E402: a=1\nimport os
          E402: 'One string'\n"Two string"\nimport os
          E402: a=1\nfrom sys import x
          
          Okay: if x:\n    import os

          Module level import not at top of file
          Open

          from slm_lab import EVAL_MODES, TRAIN_MODES
          Severity: Minor
          Found in run_lab.py by pep8

          Place imports at the top of the file.

          Always put imports at the top of the file, just after any module comments
          and docstrings, and before module globals and constants.
          
          Okay: import os
          Okay: # this is a comment\nimport os
          Okay: '''this is a module docstring'''\nimport os
          Okay: r'''this is a module docstring'''\nimport os
          Okay:
          try:\n\timport x\nexcept ImportError:\n\tpass\nelse:\n\tpass\nimport y
          Okay:
          try:\n\timport x\nexcept ImportError:\n\tpass\nfinally:\n\tpass\nimport y
          E402: a=1\nimport os
          E402: 'One string'\n"Two string"\nimport os
          E402: a=1\nfrom sys import x
          
          Okay: if x:\n    import os

          Module level import not at top of file
          Open

          from xvfbwrapper import Xvfb
          Severity: Minor
          Found in run_lab.py by pep8

          Place imports at the top of the file.

          Always put imports at the top of the file, just after any module comments
          and docstrings, and before module globals and constants.
          
          Okay: import os
          Okay: # this is a comment\nimport os
          Okay: '''this is a module docstring'''\nimport os
          Okay: r'''this is a module docstring'''\nimport os
          Okay:
          try:\n\timport x\nexcept ImportError:\n\tpass\nelse:\n\tpass\nimport y
          Okay:
          try:\n\timport x\nexcept ImportError:\n\tpass\nfinally:\n\tpass\nimport y
          E402: a=1\nimport os
          E402: 'One string'\n"Two string"\nimport os
          E402: a=1\nfrom sys import x
          
          Okay: if x:\n    import os

          Module level import not at top of file
          Open

          from slm_lab.experiment.control import Session, Trial, Experiment
          Severity: Minor
          Found in run_lab.py by pep8

          Place imports at the top of the file.

          Always put imports at the top of the file, just after any module comments
          and docstrings, and before module globals and constants.
          
          Okay: import os
          Okay: # this is a comment\nimport os
          Okay: '''this is a module docstring'''\nimport os
          Okay: r'''this is a module docstring'''\nimport os
          Okay:
          try:\n\timport x\nexcept ImportError:\n\tpass\nelse:\n\tpass\nimport y
          Okay:
          try:\n\timport x\nexcept ImportError:\n\tpass\nfinally:\n\tpass\nimport y
          E402: a=1\nimport os
          E402: 'One string'\n"Two string"\nimport os
          E402: a=1\nfrom sys import x
          
          Okay: if x:\n    import os
          Severity
          Category
          Status
          Source
          Language