Skip to content

Commit 0d01459

Browse files
authored
Merge pull request #107 from zoq/timeout_decorator
Use python timeout decorator instead of process queues.
2 parents 2729fd4 + 7cc0460 commit 0d01459

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+675
-578
lines changed

libraries/dtimeout_install.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
#
3+
# Wrapper script to unpack and build dtimeout.
4+
#
5+
# Include files will be installed to ../include/.
6+
# Library files will be installed to ../lib/.
7+
#
8+
# One dtimeout.tar.gz file should be located in this directory.
9+
tars=`ls dtimeout.tar.gz | wc -l`;
10+
if [ "$tars" -eq "0" ];
11+
then
12+
echo "No source dtimeout.tar.gz found in libraries/!"
13+
exit 1
14+
fi
15+
16+
# Remove any old directory.
17+
rm -rf dtimeout/
18+
mkdir dtimeout/
19+
tar -xzpf dtimeout.tar.gz --strip-components=1 -C dtimeout/
20+
21+
cd dtimeout/
22+
python3 setup.py build
23+
PYVER=`python3 -c 'import sys; print("python" + sys.version[0:3])'`;
24+
mkdir -p ../lib/$PYVER/site-packages/
25+
PYTHONPATH=../lib/$PYVER/site-packages/ python3 setup.py install --prefix=../ -O2

libraries/install_all.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,9 @@ if [ "$?" -ne "0" ]; then
8585
echo "Error installing R!";
8686
exit 1;
8787
fi
88+
89+
./dtimeout_install.sh $1
90+
if [ "$?" -ne "0" ]; then
91+
echo "Error installing R!";
92+
exit 1;
93+
fi

libraries/package-urls.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
dtimeout https://github.com/pnpnpn/timeout-decorator/archive/master.tar.gz
12
ann https://www.cs.umd.edu/~mount/ANN/Files/1.1.2/ann_1.1.2.tar.gz
23
flann https://github.com/mariusmuja/flann/archive/1.9.1.tar.gz
34
HLearn https://github.com/mikeizbicki/HLearn/archive/2.0.0.0.tar.gz

methods/annoy/ann.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import sys
99
import inspect
10+
import timeout_decorator
1011

1112
# Import the util path, this method even works if the path contains symlinks to
1213
# modules.
@@ -46,7 +47,8 @@ def __init__(self, dataset, timeout=0, verbose=True):
4647
successful.
4748
'''
4849
def AnnAnnoy(self, options):
49-
def RunAnnAnnoy(q):
50+
@timeout_decorator.timeout(self.timeout)
51+
def RunAnnAnnoy():
5052
totalTimer = Timer()
5153

5254
# Load input dataset.
@@ -58,18 +60,15 @@ def RunAnnAnnoy(q):
5860
# Parse options.
5961
if not "k" in options:
6062
Log.Fatal("Required option: Number of furthest neighbors to find.")
61-
q.put(-1)
6263
return -1
6364
else:
6465
k = int(options.pop("k"))
6566
if (k < 1 or k > referenceData.shape[0]):
6667
Log.Fatal("Invalid k: " + k.group(1) + "; must be greater than 0"
6768
+ " and less or equal than " + str(referenceData.shape[0]))
68-
q.put(-1)
6969
return -1
7070
if not "num_trees" in options:
7171
Log.Fatal("Required option: Number of trees to build")
72-
q.put(-1)
7372
return -1
7473
else:
7574
n = int(options.pop("num_trees"))
@@ -91,13 +90,14 @@ def RunAnnAnnoy(q):
9190
v = t.get_nns_by_vector(queryData[i],k)
9291
except Exception as e:
9392
Log.Info(e)
94-
q.put(e)
9593
return -1
9694
time = totalTimer.ElapsedTime()
97-
q.put(time)
9895
return time
9996

100-
return timeout(RunAnnAnnoy, self.timeout)
97+
try:
98+
return RunAnnAnnoy()
99+
except timeout_decorator.TimeoutError:
100+
return -1
101101

102102
'''
103103
Perform All K-Nearest-Neighbors. If the method has been successfully completed

methods/matlab/dtc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def parseTimer(self, data):
126126
pattern = re.compile(br"""
127127
.*?total_time: (?P<total_time>.*?)s.*?
128128
""", re.VERBOSE|re.MULTILINE|re.DOTALL)
129-
129+
130130
match = pattern.match(data)
131131
if not match:
132132
Log.Fatal("Can't parse the data: wrong format")

methods/milk/adaboost.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import sys
77
import inspect
8+
import timeout_decorator
89

910
# Import the util path, this method even works if the path contains symlinks to
1011
# modules.
@@ -65,7 +66,8 @@ def BuildModel(self):
6566
successful.
6667
'''
6768
def ADABOOSTMilk(self, options):
68-
def RunADABOOSTMilk(q):
69+
@timeout_decorator.timeout(self.timeout)
70+
def RunADABOOSTMilk():
6971
totalTimer = Timer()
7072

7173
Log.Info("Loading dataset", self.verbose)
@@ -75,19 +77,18 @@ def RunADABOOSTMilk(q):
7577
try:
7678
self.model = self.BuildModel()
7779
with totalTimer:
78-
self.model = self.model.train(trainData, labels)
79-
80+
self.model = self.model.train(trainData, labels)
81+
8082
except Exception as e:
81-
Log.Debug(str(e))
82-
q.put(-1)
8383
return -1
8484

8585
time = totalTimer.ElapsedTime()
86-
q.put(time)
87-
8886
return time
8987

90-
return timeout(RunADABOOSTMilk, self.timeout)
88+
try:
89+
return RunADABOOSTMilk()
90+
except timeout_decorator.TimeoutError:
91+
return -1
9192

9293
'''
9394
Perform the AdaBoost classifier. If the method has been

methods/milk/dtc.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import sys
88
import inspect
9+
import timeout_decorator
910

1011
# Import the util path, this method even works if the path contains symlinks to
1112
# modules.
@@ -66,7 +67,8 @@ def BuildModel(self):
6667
successful.
6768
'''
6869
def DTCMilk(self, options):
69-
def RunDTCMilk(q):
70+
@timeout_decorator.timeout(self.timeout)
71+
def RunDTCMilk():
7072
totalTimer = Timer()
7173

7274
Log.Info("Loading dataset", self.verbose)
@@ -85,15 +87,15 @@ def RunDTCMilk(q):
8587
with totalTimer:
8688
self.model = self.model.train(trainData, labels)
8789
except Exception as e:
88-
Log.Debug(e)
89-
q.put(-1)
9090
return -1
9191

9292
time = totalTimer.ElapsedTime()
93-
q.put(time)
9493
return time
9594

96-
return timeout(RunDTCMilk, self.timeout)
95+
try:
96+
return RunDTCMilk()
97+
except timeout_decorator.TimeoutError:
98+
return -1
9799

98100
'''
99101
Perform the Decision Tree Classifier. If the method has been

methods/milk/kmeans.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import sys
99
import inspect
10+
import timeout_decorator
1011

1112
# Import the util path, this method even works if the path contains symlinks to
1213
# modules.
@@ -44,7 +45,8 @@ def __init__(self, dataset, timeout=0, verbose=True):
4445
successful.
4546
'''
4647
def KMeansMilk(self, options):
47-
def RunKMeansMilk(q):
48+
@timeout_decorator.timeout(self.timeout)
49+
def RunKMeansMilk():
4850
totalTimer = Timer()
4951

5052
# Load input dataset.
@@ -71,12 +73,10 @@ def RunKMeansMilk(q):
7173
# Now do validation of options.
7274
if not clusters and len(self.dataset) != 2:
7375
Log.Fatal("Required option: Number of clusters or cluster locations.")
74-
q.put(-1)
7576
return -1
7677
elif (not clusters or int(clusters) < 1) and len(self.dataset) != 2:
7778
Log.Fatal("Invalid number of clusters requested! Must be greater than"
7879
+ " or equal to 1.")
79-
q.put(-1)
8080
return -1
8181

8282
m = 1000 if not maxIterations else int(maxIterations)
@@ -97,14 +97,15 @@ def RunKMeansMilk(q):
9797

9898
except Exception as e:
9999
Log.Fatal("Exception: " + str(e))
100-
q.put(-1)
101100
return -1
102101

103102
time = totalTimer.ElapsedTime()
104-
q.put(time)
105103
return time
106104

107-
return timeout(RunKMeansMilk, self.timeout)
105+
try:
106+
return RunKMeansMilk()
107+
except timeout_decorator.TimeoutError:
108+
return -1
108109

109110
'''
110111
Perform K-Means Clustering. If the method has been successfully completed

methods/milk/logistic_regression.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import sys
88
import inspect
9+
import timeout_decorator
910

1011
# Import the util path, this method even works if the path contains symlinks to
1112
# modules.
@@ -62,7 +63,8 @@ def BuildModel(self):
6263
successful.
6364
'''
6465
def LogisticRegressionMilk(self, options):
65-
def RunLogisticRegressionMilk(q):
66+
@timeout_decorator.timeout(self.timeout)
67+
def RunLogisticRegressionMilk():
6668
totalTimer = Timer()
6769

6870
Log.Info("Loading dataset", self.verbose)
@@ -79,14 +81,15 @@ def RunLogisticRegressionMilk(q):
7981
with totalTimer:
8082
self.model = self.model.train(trainData, labels)
8183
except Exception as e:
82-
q.put(-1)
8384
return -1
8485

8586
time = totalTimer.ElapsedTime()
86-
q.put(time)
8787
return time
8888

89-
return timeout(RunLogisticRegressionMilk, self.timeout)
89+
try:
90+
return RunLogisticRegressionMilk()
91+
except timeout_decorator.TimeoutError:
92+
return -1
9093

9194
'''
9295
Perform the Logistic Regression Classifier. If the method has been

methods/milk/random_forest.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import sys
88
import inspect
9+
import timeout_decorator
910

1011
# Import the util path, this method even works if the path contains symlinks to
1112
# modules.
@@ -66,7 +67,8 @@ def BuildModel(self):
6667
successful.
6768
'''
6869
def RANDOMFORESTMilk(self, options):
69-
def RunRANDOMFORESTMilk(q):
70+
@timeout_decorator.timeout(self.timeout)
71+
def RunRANDOMFORESTMilk():
7072
totalTimer = Timer()
7173

7274
Log.Info("Loading dataset", self.verbose)
@@ -85,14 +87,15 @@ def RunRANDOMFORESTMilk(q):
8587
with totalTimer:
8688
self.model = self.model.train(trainData, labels)
8789
except Exception as e:
88-
q.put(-1)
8990
return -1
9091

9192
time = totalTimer.ElapsedTime()
92-
q.put(time)
9393
return time
9494

95-
return timeout(RunRANDOMFORESTMilk, self.timeout)
95+
try:
96+
return RunRANDOMFORESTMilk()
97+
except timeout_decorator.TimeoutError:
98+
return -1
9699

97100
'''
98101
Perform the Random Forest Classifier. If the method has been

0 commit comments

Comments
 (0)