Skip to content

Commit ae61797

Browse files
authored
sorting in C++ side
1 parent 613672c commit ae61797

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

FPSim2/FPSim2.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import numpy as np
33
import tables as tb
44
from .io import S_INDEXS, load_fps, load_query, get_bounds_range
5-
from .FPSim2lib import _similarity_search, _substructure_search
5+
from .FPSim2lib import _similarity_search, _substructure_search, sort_results
66

77

88
def on_disk_search(query, fp_filename, chunk_indexes, threshold, s_index):
@@ -172,8 +172,6 @@ def _base_search(self, query_string, threshold, search_func, chunk_size, s_index
172172
if fp_range:
173173
if n_workers == 1 and not on_disk:
174174
np_res = search_func(query, self.fps.fps, threshold, fp_range[0], fp_range[1])
175-
if s_index != 'substructure':
176-
np_res[::-1].sort(order='coeff')
177175
else:
178176
results = self._parallel_run(query,
179177
search_func,
@@ -187,7 +185,7 @@ def _base_search(self, query_string, threshold, search_func, chunk_size, s_index
187185
if results:
188186
np_res = np.concatenate(results)
189187
if s_index != 'substructure':
190-
np_res[::-1].sort(order='coeff')
188+
sort_results(np_res)
191189
else:
192190
np_res = empty_np
193191
else:

FPSim2/src/sim.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ py::array_t<uint32_t> _substructure_search(py::array_t<unsigned long long> pyque
108108
return py::array_t<uint32_t>(results->size(), results->data(), capsule);
109109
}
110110

111+
bool cmp(const Result &l, const Result &r) { return l.coeff > r.coeff; }
112+
113+
void sort_results(py::array_t<Result> pyres)
114+
{
115+
py::buffer_info buf = pyres.request();
116+
Result *ptr = (Result *) buf.ptr;
117+
std::sort(&ptr[0], &ptr[buf.shape[0]], cmp);
118+
}
119+
111120
py::array_t<Result> _similarity_search(py::array_t<unsigned long long> pyquery,
112121
py::array_t<unsigned long long> pydb,
113122
float threshold,
@@ -153,7 +162,9 @@ py::array_t<Result> _similarity_search(py::array_t<unsigned long long> pyquery,
153162
int_count = 0;
154163
i++;
155164
}
165+
// set final size and sort
156166
results->resize(total_sims);
167+
std::sort(results->begin(), results->end(), cmp);
157168

158169
// acquire the GIL
159170
py::gil_scoped_acquire acquire;

FPSim2/src/sim.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ __inline float tanimoto_coeff(uint32_t int_count,
1717

1818
uint32_t py_popcount(py::array_t<unsigned long long> pyquery);
1919

20+
void sort_results(py::array_t<Result> pyres);
21+
2022
py::array_t<uint32_t> _substructure_search(py::array_t<unsigned long long> pyquery,
2123
py::array_t<unsigned long long> pydb,
2224
float threshold,

FPSim2/src/wraps.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ namespace py = pybind11;
77
PYBIND11_MODULE(FPSim2lib, m)
88
{
99
PYBIND11_NUMPY_DTYPE(Result, mol_id, coeff);
10+
m.def("py_popcount", &py_popcount, py::call_guard<py::gil_scoped_release>());
11+
m.def("sort_results", &sort_results);
1012
m.def("_substructure_search", &_substructure_search);
1113
m.def("_similarity_search", &_similarity_search);
12-
m.def("py_popcount", &py_popcount, py::call_guard<py::gil_scoped_release>());
1314
}

0 commit comments

Comments
 (0)