Skip to content

Commit 40d44b7

Browse files
Add Python 3.9, 3.10, 3.11 Support and Fix Compilation Issues (#194)
* 1. Modified _hintsvm.pyx and successfully compiled the source code 2. Modified README.md (changed the order of installing dependencies && changed the installation method) * Updated plot.py * Removed unnecessary comment in libact/query_strategies/tests/test_density_weighted_meta.py * Update README.md and revert unnecessary changes * Update README.md and libact/base/dataset.py Current Status: 2 of the tests fail and 1 throws an error * Fix the NearestNeighbors error in libact/query_strategies/multilabel/cost_sensitive_reference_pair_encoding.py * Update test cases and specify module versions in requirements.txt * Update `unittest` and `coverage` usage Resolve the [Deprecate test command](pypa/setuptools#1684) in `setuptools`. * Update .gitignore * Restore plot.py * Update dataset.py * Update density_weighted_meta.py * Update density_weighted_uncertainty_sampling.py * Update setup.py * Add support for Python 3.9 and 3.11 * Update README.md * Update requirements.txt * Update GitHub workflows --------- Co-authored-by: Poy <[email protected]>
1 parent 1079085 commit 40d44b7

File tree

11 files changed

+51
-40
lines changed

11 files changed

+51
-40
lines changed

.github/workflows/tests.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ jobs:
99
strategy:
1010
fail-fast: false
1111
matrix:
12-
os: [ubuntu-latest, macos-latest]
13-
python-version: [2.7, 3.6, 3.7, 3.8, 3.9]
12+
# os: [ubuntu-latest, macos-latest]
13+
# python-version: [2.7, 3.6, 3.7, 3.8, 3.9]
14+
os: [ubuntu-22.04]
15+
python-version: [3.9, 3.10, 3.11]
1416

1517
steps:
1618
- uses: actions/checkout@v2

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@ target/
6161
examples/australian.txt
6262
examples/diabetes.txt
6363
examples/heart.txt
64+
65+
libact/query_strategies/_hintsvm.c

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ Comments and questions on the package is welcomed at `libact-users@googlegroups.
2121

2222
# Basic Dependencies
2323

24-
* Python 2.7, 3.3, 3.4, 3.5, 3.6
24+
* Python 3.9, 3.10, 3.11
25+
* _Note._ We will soon release Python 2.7, 3.3, 3.4, 3.5, and 3.6 installations in the new branch.
2526

26-
* Python dependencies
27+
* Debian (>= 7) / Ubuntu (>= 14.04)
2728
```
28-
pip install -r requirements.txt
29+
sudo apt-get install build-essential gfortran libatlas-base-dev liblapacke-dev python3-dev
2930
```
3031

31-
* Debian (>= 7) / Ubuntu (>= 14.04)
32+
* Python dependencies
3233
```
33-
sudo apt-get install build-essential gfortran libatlas-base-dev liblapacke-dev python3-dev
34+
pip install -r requirements.txt
3435
```
3536

3637
* Arch
@@ -66,9 +67,10 @@ python setup.py install --user
6667
```
6768

6869
To build and install from souce for all users on Unix/Linux:
70+
71+
**(This is the recommended method for Python 3.10 users)**
6972
```
70-
python setup.py build
71-
sudo python setup.py install
73+
pip install -e .
7274
```
7375

7476
## Installation Options
@@ -120,7 +122,7 @@ Available examples:
120122
To run the test suite:
121123

122124
```
123-
python setup.py test
125+
python -m unittest -v
124126
```
125127

126128
To run pylint, install pylint through ```pip install pylint``` and run the following command in root directory:
@@ -132,8 +134,8 @@ pylint libact
132134
To measure the test code coverage, install coverage through ```pip install coverage``` and run the following commands in root directory:
133135

134136
```
135-
coverage run --source libact --omit */tests/* setup.py test
136-
coverage report
137+
python -m coverage run --source libact --omit */tests/* -m unittest
138+
python -m coverage report
137139
```
138140

139141
# Citing

libact/base/dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __init__(self, X=None, y=None):
3939
X = np.array(X)
4040

4141
if y is None: y = []
42-
y = np.array(y)
42+
y = np.array(y, dtype=object)
4343

4444
self._X = X
4545
self._y = y
@@ -218,7 +218,7 @@ def labeled_uniform_sample(self, sample_size, replace=True):
218218
sample_size
219219
"""
220220
idx = np.random.choice(np.where(self.get_labeled_mask())[0],
221-
size=sample_size, replace=replace )
221+
size=sample_size, replace=replace)
222222
return Dataset(self._X[idx], self._y[idx])
223223

224224

libact/query_strategies/_hintsvm.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
22

3-
import numpy as np
3+
import numpy as np
44
cimport numpy as np
55
from libc.stdlib cimport free
6-
cimport _hintsvm
6+
from . cimport _hintsvm
77

88
cdef extern from *:
99
ctypedef struct svm_parameter:

libact/query_strategies/density_weighted_meta.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,12 @@ def __init__(self, dataset, base_query_strategy, similarity_metric=None,
8686
else:
8787
self.clustering_method = KMeans(
8888
n_clusters=5, random_state=self.random_state_)
89-
89+
9090
if similarity_metric is not None:
9191
self.similarity_metric = similarity_metric
9292
else:
9393
self.similarity_metric = cosine_similarity
9494

95-
9695
@inherit_docstring_from(QueryStrategy)
9796
def update(self, entry_id, label):
9897
pass
@@ -104,7 +103,7 @@ def _get_scores(self):
104103
scores = self.base_query_strategy._get_scores()
105104
_, X_pool = dataset.get_unlabeled_entries()
106105
unlabeled_entry_ids, base_scores = zip(*scores)
107-
106+
108107
self.clustering_method.fit(X)
109108
pool_cluster = self.clustering_method.predict(X_pool)
110109
cluster_center = self.clustering_method.cluster_centers_
@@ -128,4 +127,4 @@ def make_query(self):
128127
unlabeled_entry_ids, scores = zip(*self._get_scores())
129128
ask_id = self.random_state_.choice(np.where(scores == np.max(scores))[0])
130129

131-
return unlabeled_entry_ids[ask_id]
130+
return unlabeled_entry_ids[ask_id]

libact/query_strategies/density_weighted_uncertainty_sampling.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ def make_query(self):
153153

154154
return unlabeled_entry_ids[ask_id]
155155

156+
156157
class DensityWeightedLogisticRegression(object):
157158
"""Density Weighted Logistic Regression
158159

libact/query_strategies/multilabel/cost_sensitive_reference_pair_encoding.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ class CostSensitiveReferencePairEncoding(QueryStrategy):
7373

7474
def __init__(self, dataset, scoring_fn, model, base_model, n_models=100,
7575
n_jobs=1, random_state=None):
76-
super(CostSensitiveReferencePairEncoding, self).__init__(dataset=dataset)
76+
super(CostSensitiveReferencePairEncoding,
77+
self).__init__(dataset=dataset)
7778

7879
self.model_ = model
7980
self.csrpe_ = CSRPE(scoring_fn=scoring_fn, base_clf=base_model,
@@ -94,12 +95,12 @@ def make_query(self):
9495
Z = self.csrpe_.predicted_code(X_pool)
9596
predZ = self.csrpe_.encode(predY)
9697

97-
dist = paired_distances(Z, predZ, metric=hamming) # z1 z2
98-
dist2 = self.csrpe_.predict_dist(X_pool) # z1 zt
99-
#dist3 = self.csrpe.distance(predZ) # z2 zt
98+
dist = paired_distances(Z, predZ, metric=hamming) # z1 z2
99+
dist2 = self.csrpe_.predict_dist(X_pool) # z1 zt
100+
# dist3 = self.csrpe.distance(predZ) # z2 zt
100101

101102
dist = dist + dist2
102-
#dist = dist + dist3
103+
# dist = dist + dist3
103104

104105
ask_id = self.random_state_.choice(
105106
np.where(np.isclose(dist, np.max(dist)))[0])
@@ -127,8 +128,10 @@ def train(self, X, y):
127128
self.n_samples = np.shape(X)[0]
128129
self.n_labels = np.shape(y)[1]
129130

130-
score0 = self.scoring_fn(y, np.tile(self.rep_label[0], (self.n_samples, 1)))
131-
score1 = self.scoring_fn(y, np.tile(self.rep_label[1], (self.n_samples, 1)))
131+
score0 = self.scoring_fn(y, np.tile(
132+
self.rep_label[0], (self.n_samples, 1)))
133+
score1 = self.scoring_fn(y, np.tile(
134+
self.rep_label[1], (self.n_samples, 1)))
132135
lbl = (((score1 - score0) > 0) + 0.0)
133136

134137
weight = np.abs(score1 - score0)
@@ -153,8 +156,8 @@ def __init__(self, scoring_fn, base_clf, n_clfs, n_jobs,
153156
metric='euclidean', random_state=None):
154157
self.scoring_fn = scoring_fn
155158
self.base_clf = base_clf
156-
self.nn_ = NearestNeighbors(1, algorithm='ball_tree',
157-
metric=metric, n_jobs=n_jobs)
159+
self.nn_ = NearestNeighbors(n_neighbors=1, algorithm='ball_tree',
160+
metric=metric, n_jobs=n_jobs)
158161
self.n_clfs = n_clfs
159162
self.random_state_ = seed_random_state(random_state)
160163

@@ -224,4 +227,4 @@ def predict_dist(self, X):
224227
encoded = self.predicted_code(X)
225228
dist, _ = self.nn_.kneighbors(encoded, 1, return_distance=True)
226229
dist = dist.reshape(-1)
227-
return dist
230+
return dist

libact/query_strategies/tests/test_density_weighted_meta.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def setUp(self):
2424
self.quota = 10
2525

2626
def test_density_weighted_meta_uncertainty_lc(self):
27-
trn_ds = Dataset(self.X[:20], np.concatenate([self.y[:6], [None] * 14]))
27+
trn_ds = Dataset(self.X[:20], np.concatenate(
28+
[self.y[:6], [None] * 14]))
2829
base_qs = UncertaintySampling(
2930
trn_ds, method='lc',
3031
model=LogisticRegression(solver='liblinear', multi_class="ovr"))
@@ -37,7 +38,8 @@ def test_density_weighted_meta_uncertainty_lc(self):
3738
beta=1.0, random_state=1126)
3839
model = LogisticRegression(solver='liblinear', multi_class="ovr")
3940
qseq = run_qs(trn_ds, qs, self.y, self.quota)
40-
assert_array_equal(qseq, np.array([13, 18, 9, 12, 8, 16, 10, 19, 15, 17]))
41+
assert_array_equal(qseq, np.array(
42+
[18, 13, 9, 12, 8, 16, 10, 19, 15, 7]))
4143

4244

4345
if __name__ == '__main__':

libact/query_strategies/tests/test_realdata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def test_DensityWeightedUncertaintySampling(self):
153153
qs = DWUS(trn_ds, random_state=1126)
154154
qseq = run_qs(trn_ds, qs, self.y, self.quota)
155155
assert_array_equal(
156-
qseq, np.array([30, 179, 104, 186, 28, 65, 142, 62, 257, 221]))
156+
qseq, np.array([257, 220, 179, 84, 208, 70, 245, 62, 50, 69]))
157157

158158

159159
if __name__ == '__main__':

0 commit comments

Comments
 (0)