-
Notifications
You must be signed in to change notification settings - Fork 0
/
method.py
62 lines (47 loc) · 1.39 KB
/
method.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
A container class for all the algorithms to provide common io methods and other utilities.
"""
import os
import utils
import logging
logger = logging.getLogger(__name__)
class Method:
def __init__(self):
self.threshold = None
def get_name(self):
return self.__class__.__name__
def save(self, filepath=None):
if filepath is None:
filepath = self.workdir
utils.mkdir(filepath)
filename = os.path.join(filepath, "%s.pkl" % self.get_name())
utils.writepickle(self, filename)
logger.info("Saved method %s to %s" % (self.get_name(), filename))
def set_workdir(self, workdir):
"""
sets the workdir
"""
self.workdir = workdir
utils.mkdir(self.workdir)
def info(self):
"""
Prints out all the variables
"""
import inspect
message = "All variables available for method %s" % self.__str__()
print message
print '-'*len(message)
attributes = inspect.getmembers(self, lambda a:not(inspect.isroutine(a)))
for a in attributes:
if (a[0].startswith('__') and a[0].endswith('__')): continue
print a[0], "=", a[1]
def _set_default(self, dico, name, default, warn=None):
if not name in dico.keys():
dico[name] = default
if not warn is None:
logger.warn(warn)
def set_threshold(self, thr):
self.threshold = thr
def predict(self, features, **kwargs):
proba = self.predict_proba(features, **kwargs)
return utils.classify(proba, self.threshold)