1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
""" |
5
|
|
|
A base vectorizer for The Cannon. |
6
|
|
|
""" |
7
|
|
|
|
8
|
|
|
from __future__ import (division, print_function, absolute_import, |
9
|
|
|
unicode_literals) |
10
|
|
|
|
11
|
|
|
__all__ = ["BaseVectorizer"] |
12
|
|
|
|
13
|
|
|
import numpy as np |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class BaseVectorizer(object): |
17
|
|
|
""" |
18
|
|
|
A vectorizer class that models spectral fluxes and its derivatives. |
19
|
|
|
""" |
20
|
|
|
|
21
|
|
|
def __init__(self, label_names, terms, **kwargs): |
22
|
|
|
self._terms = terms |
23
|
|
|
self._label_names = tuple(label_names) |
24
|
|
|
self.metadata = kwargs.get("metadata", {}) |
25
|
|
|
return None |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
# These can be over-written by sub-classes, but it is useful to have some |
29
|
|
|
# basic information if the sub-classes do not overwrite it. |
30
|
|
|
def __str__(self): |
31
|
|
|
return "<{module}.{name} object consisting of {K} labels and {D} terms>"\ |
32
|
|
|
.format(module=self.__module__, name=type(self).__name__, |
33
|
|
|
D=len(self.terms), K=len()) |
34
|
|
|
|
35
|
|
|
def __repr__(self): |
36
|
|
|
return "<{0}.{1} object at {2}>".format( |
37
|
|
|
self.__module__, type(self).__name__, hex(id(self))) |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
# I/O (Serializable) functionality. |
41
|
|
|
def __getstate__(self): |
42
|
|
|
""" Return the state of the vectorizer. """ |
43
|
|
|
return (type(self).__name__, dict( |
44
|
|
|
label_names=self.label_names, |
45
|
|
|
terms=self.terms, |
46
|
|
|
metadata=self.metadata)) |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
def __setstate__(self, state): |
50
|
|
|
""" Set the state of the vectorizer. """ |
51
|
|
|
model_name, kwds = kwds |
52
|
|
|
self._label_names = kwds["label_names"] |
53
|
|
|
self._terms = kwds["terms"] |
54
|
|
|
self.metadata = kwds["metadata"] |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
@property |
58
|
|
|
def terms(self): |
59
|
|
|
""" Return the terms provided for this vectorizer. """ |
60
|
|
|
return self._terms |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
@property |
64
|
|
|
def label_names(self): |
65
|
|
|
""" |
66
|
|
|
Return the label names that are used in this vectorizer. |
67
|
|
|
""" |
68
|
|
|
return self._label_names |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def __call__(self, *args, **kwargs): |
72
|
|
|
""" |
73
|
|
|
An alias to the get_label_vector method. |
74
|
|
|
""" |
75
|
|
|
return self.get_label_vector(*args, **kwargs) |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
def get_label_vector(self, labels, *args, **kwargs): |
79
|
|
|
""" |
80
|
|
|
Return the label vector based on the labels provided. |
81
|
|
|
|
82
|
|
|
:param labels: |
83
|
|
|
The values of the labels. These should match the length and order of |
84
|
|
|
the `label_names` attribute. |
85
|
|
|
""" |
86
|
|
|
raise NotImplementedError("the get_label_vector method " |
87
|
|
|
"must be specified by the sub-classes") |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
def get_label_vector_derivative(self, labels, *args, **kwargs): |
91
|
|
|
""" |
92
|
|
|
Return the derivative of the label vector with respect to the given |
93
|
|
|
label. |
94
|
|
|
|
95
|
|
|
:param labels: |
96
|
|
|
The values of the labels to calculate the label vector for. |
97
|
|
|
""" |
98
|
|
|
raise NotImplementedError("the get_label_vector_derivative method " |
99
|
|
|
"must be specified by the sub-classes") |