-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfselector.py
More file actions
50 lines (38 loc) · 1.36 KB
/
fselector.py
File metadata and controls
50 lines (38 loc) · 1.36 KB
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
"""
Features Selector
"""
from chisquared import ChiSquared
from mutualinfo import MutualInfo
import thread
import threading
class FeatureSelector(object):
_supported = {'chi_squared': ChiSquared,
'mutual_inf': MutualInfo}
def __init__(self, technique, id, threadNum): # pylint: disable=E1002
self._id = id
self._threadNum = threadNum
#fail if wrong argument
if technique not in self._supported.keys():
raise ValueError("The technique must be one of" + str(self._supported.keys()))
self._metric = self._supported[technique]()
self._dataSet = set
# run selection
def select(self, k):
featureSet = self._metric.select()
featureSet.sort(key = lambda x : x[1])
featureSet.reverse()
#keep k best
return featureSet[0:k]
def process(self, line):
self._metric.process(line)
def startProcessing(self, fname):
#prepare the data set itself
event = threading.Event()
try:
thread.start_new_thread( self._metric._readFile, (fname, event, self._id, self._threadNum, ) )
except Exception, e:
print "Error: unable to start thread" + str(e)
return event
def combine(self, other):
self._metric.combine(other._metric)
return self