Passed
Push — main ( ddff4b...7b3fbc )
by Douglas
04:33
created

mandos.analysis.concordance   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A ConcordanceCalculator._calc() 0 2 1
A ConcordanceCalculator.generate() 0 5 2
A ConcordanceCalculator.__init__() 0 4 1
A TauConcordanceCalculator._n_z() 0 13 1
A TauConcordanceCalculator._calc() 0 5 1
A ConcordanceCalculator.calc() 0 9 2
1
"""
2
Calculations of concordance between annotations.
3
"""
4
import abc
5
import math
6
from typing import Sequence, Set, Collection, Tuple, Dict, Generator
0 ignored issues
show
Unused Code introduced by
Unused Set imported from typing
Loading history...
Unused Code introduced by
Unused Collection imported from typing
Loading history...
Unused Code introduced by
Unused Dict imported from typing
Loading history...
Unused Code introduced by
Unused Tuple imported from typing
Loading history...
7
8
import numpy as np
0 ignored issues
show
introduced by
Unable to import 'numpy'
Loading history...
9
import pandas as pd
0 ignored issues
show
introduced by
Unable to import 'pandas'
Loading history...
10
from typeddfs import TypedDfs
0 ignored issues
show
introduced by
Unable to import 'typeddfs'
Loading history...
11
12
from mandos.analysis import AnalysisUtils, SimilarityDf
0 ignored issues
show
Unused Code introduced by
Unused AnalysisUtils imported from mandos.analysis
Loading history...
13
14
ConcordanceDf = (
15
    TypedDfs.typed("ConcordanceDf").require("sample", dtype=int).require("score", dtype=float)
16
).build()
17
18
19
class ConcordanceCalculator(metaclass=abc.ABCMeta):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
20
    def __init__(self, n_samples: int, seed: int):
21
        self.n_samples = n_samples
22
        self.seed = seed
23
        self.rand = np.random.RandomState(seed)
24
25
    def calc(self, phi: SimilarityDf, psi: SimilarityDf) -> ConcordanceDf:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
26
        if phi.columns.tolist() != psi.columns.tolist():
27
            raise ValueError(
28
                f"Mismatched compounds: {phi.columns.tolist()} != {psi.columns.tolist()}"
29
            )
30
        df = pd.DataFrame(data=self.generate(phi, psi), columns=["score"])
0 ignored issues
show
Coding Style Naming introduced by
Variable name "df" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
31
        df = df.reset_index()
0 ignored issues
show
Coding Style Naming introduced by
Variable name "df" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
32
        df.columns = ["sample", "score"]
33
        return ConcordanceDf(df)
34
35
    def generate(self, phi: SimilarityDf, psi: SimilarityDf) -> Generator[float, None, None]:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
36
        for b in range(self.n_samples):
0 ignored issues
show
Unused Code introduced by
The variable b seems to be unused.
Loading history...
Coding Style Naming introduced by
Variable name "b" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
37
            phi_b = self.rand.choice(phi, replace=True)
38
            psi_b = self.rand.choice(psi, replace=True)
39
            yield self._calc(phi_b, psi_b)
40
41
    def _calc(self, phi: SimilarityDf, psi: SimilarityDf) -> float:
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
42
        raise NotImplemented()
0 ignored issues
show
Bug introduced by
NotImplemented does not seem to be callable.
Loading history...
Best Practice introduced by
NotImplemented raised - should raise NotImplementedError
Loading history...
43
44
45
class TauConcordanceCalculator(ConcordanceCalculator):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
46
    def _calc(self, phi: SimilarityDf, psi: SimilarityDf) -> float:
47
        n = len(phi)
0 ignored issues
show
Coding Style Naming introduced by
Variable name "n" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
48
        numerator = self._n_z(phi, psi, 1) - self._n_z(phi, psi, -1)
49
        denominator = math.factorial(n) / (2 * math.factorial(n - 2))
50
        return numerator / denominator
51
52
    def _n_z(self, a: Sequence[float], b: Sequence[float], z: int) -> int:
0 ignored issues
show
Coding Style Naming introduced by
Argument name "z" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
Argument name "b" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
Argument name "a" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
53
        return int(
54
            np.sum(
55
                [
56
                    int(
57
                        np.sum(
58
                            [
59
                                int(np.sign(a[i] - a[j]) == z * np.sign(b[i] - b[j]) != 0)
60
                                for j in range(i)
61
                            ]
62
                        )
63
                    )
64
                    for i in range(len(a))
65
                ]
66
            )
67
        )
68
69
70
__all__ = ["ConcordanceCalculator", "TauConcordanceCalculator"]
71