# LiberTEM/LiberTEM

src/libertem/io/corrections/detector.py

### Summary

D
1 day

#### Function `_correct_numba_inplace` has a Cognitive Complexity of 45 (exceeds 5 allowed). Consider refactoring. Open

``````def _correct_numba_inplace(buffer, dark_image, gain_map, exclude_pixels, repair_environments,
repair_counts):
'''
Numerical work horse to perform detector corrections

``````
Found in src/libertem/io/corrections/detector.py - About 6 hrs 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"

#### Cyclomatic complexity is too high in function _correct_numba_inplace. (16) Open

``````@numba.njit(cache=True, nogil=True)
def _correct_numba_inplace(buffer, dark_image, gain_map, exclude_pixels, repair_environments,
repair_counts):
'''
Numerical work horse to perform detector corrections``````

## 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.

#### Cyclomatic complexity is too high in function correct. (8) Open

``````def correct(
buffer, dark_image=None, gain_map=None, excluded_pixels=None, repair_descriptor=None,
inplace=False, sig_shape=None, allow_empty=False):
'''
Function to perform detector corrections``````

## 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.

#### Function `correct_dot_masks` has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring. Open

``````def correct_dot_masks(masks, gain_map, excluded_pixels=None, allow_empty=False):
sig_shape = gain_map.shape

``````
Found in src/libertem/io/corrections/detector.py - About 2 hrs 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"

#### Function `environments` has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring. Open

``````def environments(excluded_pixels, sigshape):
'''
Calculate a hypercube surface around a pixel, excluding frame boundaries

Returns``````
Found in src/libertem/io/corrections/detector.py - About 2 hrs 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"

#### Cyclomatic complexity is too high in function environments. (7) Open

``````@numba.njit(cache=True, nogil=True)
def environments(excluded_pixels, sigshape):
'''
Calculate a hypercube surface around a pixel, excluding frame boundaries

``````

## 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.

#### Cyclomatic complexity is too high in function correct_dot_masks. (7) Open

``````def correct_dot_masks(masks, gain_map, excluded_pixels=None, allow_empty=False):
sig_shape = gain_map.shape

``````

## 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.

#### Cyclomatic complexity is too high in function flatten_filter. (6) Open

``````@numba.njit(cache=True, nogil=True)
def flatten_filter(excluded_pixels, repairs, repair_counts, sig_shape):
'''
Flatten excluded pixels and repair environments and filter for collisions

``````

## 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.

#### Function `correct` has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open

``````def correct(
buffer, dark_image=None, gain_map=None, excluded_pixels=None, repair_descriptor=None,
inplace=False, sig_shape=None, allow_empty=False):
'''
Function to perform detector corrections``````
Found in src/libertem/io/corrections/detector.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"

#### Function `flatten_filter` has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring. Open

``````def flatten_filter(excluded_pixels, repairs, repair_counts, sig_shape):
'''
Flatten excluded pixels and repair environments and filter for collisions

Ravel indices to flattened signal dimension and``````
Found in src/libertem/io/corrections/detector.py - About 55 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"

#### Avoid deeply nested control flow statements. Open

``````                    for index in repair_environments[i, :repair_counts[i]]:
acc += buffer[nav, index]
buffer[nav, p] = acc / repair_counts[i]``````
Found in src/libertem/io/corrections/detector.py - About 45 mins to fix

#### Refactor this function to reduce its Cognitive Complexity from 45 to the 15 allowed. Open

``def _correct_numba_inplace(buffer, dark_image, gain_map, exclude_pixels, repair_environments,``

Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.

## See

#### Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed. Open

``def environments(excluded_pixels, sigshape):``

Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.

## See

#### Function "correct" has 8 parameters, which is greater than the 7 authorized. Open

``````        buffer, dark_image=None, gain_map=None, excluded_pixels=None, repair_descriptor=None,
inplace=False, sig_shape=None, allow_empty=False):``````

A long parameter list can indicate that a new structure should be created to wrap the numerous parameters or that the function is doing too many things.

## Noncompliant Code Example

With a maximum number of 4 parameters:

```def do_something(param1, param2, param3, param4, param5):
...
```

## Compliant Solution

```def do_something(param1, param2, param3, param4):
...
```

#### Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed. Open

``def correct_dot_masks(masks, gain_map, excluded_pixels=None, allow_empty=False):``

Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.