tracking_policy_agendas.classifiers.meta_sklearn_clf   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 50
Duplicated Lines 66 %

Test Coverage

Coverage 68.18%

Importance

Changes 0
Metric Value
wmc 11
eloc 23
dl 33
loc 50
ccs 15
cts 22
cp 0.6818
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A MetaSkLearnClf.load_model() 13 13 4
A MetaSkLearnClf.__init__() 3 3 3
A MetaSkLearnClf.save_model() 14 14 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
"""
2
Abstract class for SKLearn Classifiers
3
4
....................................................................................................
5
MIT License
6
Copyright (c) 2021-2023 AUT Iran, Mohammad H Forouhesh
7
Copyright (c) 2021-2022 MetoData.ai, Mohammad H Forouhesh
8
....................................................................................................
9
This module abstracts sklearn classifier.
10
"""
11
12 1
import os
13 1
import pickle
14 1
from .meta_clf import MetaClf
15
16
17 1 View Code Duplication
class MetaSkLearnClf(MetaClf):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
introduced by
Missing class docstring
Loading history...
18 1
    def __init__(self, classifier_instance, **kwargs):
19 1
        super().__init__(classifier_instance=classifier_instance, **kwargs)
20 1
        if 'load_path' in kwargs and kwargs['load_path'] is not None: self.load_model(kwargs['load_path'])
0 ignored issues
show
Coding Style introduced by
More than one statement on a single line
Loading history...
Coding Style introduced by
This line is too long as per the coding-style (106/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
21
22 1
    def load_model(self, load_path: str):
23
        """
24
        A tool to load model from disk.
25
        :param load_path:   Model path.
26
        :return:            None
27
        """
28
29 1
        loading_prep = lambda string: f'model_dir/{load_path}/{string}'
30 1
        self.emb.load(loading_prep('emb.pkl'))
31 1
        with open(loading_prep('model.pkl'), 'rb') as f:
0 ignored issues
show
Coding Style Naming introduced by
Variable name "f" 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 1
            self.clf = pickle.load(f)
33 1
        with open(loading_prep('scaler.pkl'), 'rb') as f:
0 ignored issues
show
Coding Style Naming introduced by
Variable name "f" 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...
34 1
            self.scaler = pickle.load(f)
35
36 1
    def save_model(self, save_path: str):
37
        """
38
        A tool to save model to disk
39
        :param save_path:   Saving path.
40
        :return:            None.
41
        """
42
43
        os.makedirs(f'model_dir/{save_path}', exist_ok=True)
44
        saving_prep = lambda string: f'model_dir/{save_path}/{string}'
45
        self.emb.save(saving_prep('emb.pkl'))
46
        with open(saving_prep('scaler.pkl'), 'wb') as f:
0 ignored issues
show
Coding Style Naming introduced by
Variable name "f" 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...
47
            pickle.dump(self.scaler, f, pickle.HIGHEST_PROTOCOL)
48
        with open(saving_prep('model.pkl'), 'wb') as f:
0 ignored issues
show
Coding Style Naming introduced by
Variable name "f" 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...
49
            pickle.dump(self.clf, f, pickle.HIGHEST_PROTOCOL)
50