Completed
Push — appveyor ( 280314...2c0e2c )
by Konstantinos
02:09
created

so_magic.data.backend.panda_handling.df_discretization   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A PDBinnerFactory.equal_length_binner() 0 2 1
A DFSameLengthBinning.bin() 0 2 1
A PDBinnerFactory.create_binner() 0 2 1
A BasePDBinner.register_as_subclass() 0 6 1
A DFQBinning.bin() 0 2 1
A BasePDBinner.create() 0 5 2
A PDBinnerFactory.quantisized_binner() 0 2 1
1
from abc import ABC
2
import pandas as pd
3
4
from data.discretization import BaseBinner, BinnerFactory
5
6
7
class BasePDBinner(BaseBinner, ABC):
8
9
    @classmethod
10
    def register_as_subclass(cls, binner_type):
11
        def wrapper(subclass):
12
            cls.subclasses[binner_type] = subclass
13
            return subclass
14
        return wrapper
15
16
    @classmethod
17
    def create(cls, binner_type, *args, **kwargs):
18
        if binner_type not in cls.subclasses:
19
            raise ValueError('Bad "BinnerFactory Backend type" type \'{}\''.format(binner_type))
20
        return cls.subclasses[binner_type](*args, **kwargs)
21
22
23
@BaseDFBinner.register_as_subclass('same-length')
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable BaseDFBinner does not seem to be defined.
Loading history...
24
class DFSameLengthBinning(BasePDBinner):
25
26
    def bin(self, values, nb_bins):
27
        return pd.cut(values, nb_bins)
28
29
@BaseDFBinner.register_as_subclass('quantisized')
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable BaseDFBinner does not seem to be defined.
Loading history...
30
class DFQBinning(BasePDBinner):
31
32
    def bin(self, values, nb_bins):
33
        return pd.qcut(values, nb_bins)
34
35
36
@BinnerFactory.register_as_subclass('pandas')
37
class PDBinnerFactory(BinnerFactory):
38
39
    def equal_length_binner(self, *args, **kwargs) -> DFSameLengthBinning:
40
        return DFSameLengthBinning()
41
42
    def quantisized_binner(self, *args, **kwargs) -> DFQBinning:
43
        return DFQBinning()
44
45
    def create_binner(self, *args, **kwargs) -> BasePDBinner:
46
        BasePDBinner.create(*args, **kwargs)
47
48