SelfOrganizingMapFactory.create()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nop 5
dl 0
loc 10
rs 9.95
c 0
b 0
f 0
1
import logging
2
import attr
3
from so_magic.utils import Subject
4
from .self_organising_map import SomTrainer, SelfOrganizingMap, NoFeatureVectorsError
5
6
7
logger = logging.getLogger(__name__)
8
9
10
@attr.s
11
class SelfOrganizingMapFactory:
12
    trainer = attr.ib(init=True, default=attr.Factory(SomTrainer.from_callable))
13
    subject = attr.ib(init=True, default=attr.Factory(lambda: Subject([])))
14
15
    def create(self, dataset, nb_cols, nb_rows, **kwargs):
16
        try:
17
            # run a backend algorithm and get a self-organising map representation object
18
            somoclu_map = self.trainer.infer_map(nb_cols, nb_rows, dataset, **kwargs)
19
            self.subject.state = somoclu_map
20
            self.subject.notify()
21
            return SelfOrganizingMap(somoclu_map, dataset.name)
22
        except NoFeatureVectorsError as exception:
23
            logger.info("%s Fire up an 'encode' command.", str(exception))
24
            raise exception
25