Changeset: efd113d8c4b0 for MonetDB URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=efd113d8c4b0 Modified Files: monetdb5/extras/pyapi/bytearray.c monetdb5/extras/pyapi/type_conversion.h sql/backends/monet5/Tests/pyapi08.sql Branch: pyapi Log Message:
Added a faster implementation of pyapi08. diffs (193 lines): diff --git a/monetdb5/extras/pyapi/bytearray.c b/monetdb5/extras/pyapi/bytearray.c --- a/monetdb5/extras/pyapi/bytearray.c +++ b/monetdb5/extras/pyapi/bytearray.c @@ -106,7 +106,7 @@ int replace_method(char *name, PyCFuncti } if (i >= 0) { - fprintf(stderr, "ERROR, trying to replace function %s in ByteArrayObject with a new object but the function was not where we expected it to be.", name); + fprintf(stderr, "WARNING: Trying to replace function %s in ByteArrayObject with a new object but the function could not be found. Maybe this is because of a different/newer Python version?", name); } return index; } diff --git a/monetdb5/extras/pyapi/type_conversion.h b/monetdb5/extras/pyapi/type_conversion.h --- a/monetdb5/extras/pyapi/type_conversion.h +++ b/monetdb5/extras/pyapi/type_conversion.h @@ -64,9 +64,6 @@ bool py_to_hge(PyObject *ptr, hge *value PyObject *PyLong_FromHge(hge h); #endif -//! Create a PyByteArray from a reference to a string. NOTE: The string is not actually copied -PyObject *PyByteArray_FromString(char *str); -void PyByteArray_Override(void); //using macros, create a number of str_to_<type>, unicode_to_<type> and pyobject_to_<type> functions (we are Java now) #define CONVERSION_FUNCTION_HEADER_FACTORY(tpe) \ diff --git a/sql/backends/monet5/Tests/pyapi08.sql b/sql/backends/monet5/Tests/pyapi08.sql --- a/sql/backends/monet5/Tests/pyapi08.sql +++ b/sql/backends/monet5/Tests/pyapi08.sql @@ -1014,60 +1014,116 @@ 2015-06-03 15:11:40.000000|1|"244650760" CREATE FUNCTION numpy_distance(stt string, tss bigint, lat double, lon double, alt double) returns table (s1 string, s2 string, timestamp int, mindist int) language P { - import numpy as np - import math - timelimit = 10 - distlimit = 20000 - rets1 = np.array([], dtype='|S10') - rets2 = np.array([], dtype='|S10') - retts = np.array([], dtype=np.int64) - retmd = np.array([], dtype=np.uint32) - it = np.nditer(tss, flags=['f_index']) - while not it.finished: - if (it.index+1 >= tss.size) : break - lat1 = lat[it.index] - lon1 = lon[it.index] - alt1 = alt[it.index] - mindist = distlimit - otheridx = -1 - it2 = np.nditer(tss[it.index+1:], flags=['f_index']) - while not it2.finished: - timediff = it2[0] - it[0] - if (timediff > timelimit): - break - if (stt[it.index] == stt[it2.index]): - it2.iternext() - continue - altdiff = abs(alt[it2.index] - alt1) - if (altdiff > distlimit): - it2.iternext() - continue - lat2 = lat[it2.index] - lon2 = lon[it2.index] - distdiff = (6371000 * math.sqrt(math.pow((lon1 * 0.01745 - lon2 * 0.01745) * - math.cos(0.5 * (lat1 * 0.01745 + lat2 * 0.01745)),2) + - math.pow((lat1 * 0.01745 - lat2 * 0.01745),2))) - if (distdiff > distlimit) : - it2.iternext() - continue - if (distdiff < mindist): - mindist = distdiff - otheridx = it2.index - it2.iternext() - if mindist < distlimit: - rets1 = numpy.append(rets1, str(stt[it.index])) - rets2 = numpy.append(rets2, str(stt[otheridx])) - retts = numpy.append(retts, int(it[0]/1000)) - retmd = numpy.append(retmd, int(mindist)) - it.iternext() - return([rets1, rets2, retts, retmd]) + import numpy as np + import math + timelimit = 10 + distlimit = 20000 + rets1 = np.array([], dtype='|S10') + rets2 = np.array([], dtype='|S10') + retts = np.array([], dtype=np.int64) + retmd = np.array([], dtype=np.uint32) + it = np.nditer(tss, flags=['f_index']) + while not it.finished: + if (it.index+1 >= tss.size) : break + lat1 = lat[it.index] + lon1 = lon[it.index] + alt1 = alt[it.index] + mindist = distlimit + otheridx = -1 + it2 = np.nditer(tss[it.index+1:], flags=['f_index']) + while not it2.finished: + timediff = it2[0] - it[0] + if (timediff > timelimit): + break + if (stt[it.index] == stt[it2.index]): + it2.iternext() + continue + altdiff = abs(alt[it2.index] - alt1) + if (altdiff > distlimit): + it2.iternext() + continue + lat2 = lat[it2.index] + lon2 = lon[it2.index] + distdiff = (6371000 * math.sqrt(math.pow((lon1 * 0.01745 - lon2 * 0.01745) * + math.cos(0.5 * (lat1 * 0.01745 + lat2 * 0.01745)),2) + + math.pow((lat1 * 0.01745 - lat2 * 0.01745),2))) + if (distdiff > distlimit) : + it2.iternext() + continue + if (distdiff < mindist): + mindist = distdiff + otheridx = it2.index + it2.iternext() + if mindist < distlimit: + rets1 = numpy.append(rets1, str(stt[it.index])) + rets2 = numpy.append(rets2, str(stt[otheridx])) + retts = numpy.append(retts, int(it[0]/1000)) + retmd = numpy.append(retmd, int(mindist)) + it.iternext() + return([rets1, rets2, retts, retmd]) +}; + +CREATE FUNCTION numpy_distance_fast(stt_in string, tss_in bigint, lat_in double, lon_in double, alt_in double) returns table (s1 string, s2 string, timestamp int, mindist int) language P { + import numpy as np + import math + stt = np.array(stt_in, dtype=object); + tss = np.array(tss_in, dtype=object); + lat = np.array(lat_in, dtype=object); + lon = np.array(lon_in, dtype=object); + alt = np.array(alt_in, dtype=object); + timelimit = 10 + distlimit = 20000 + rets1 = np.array([], dtype='|S10') + rets2 = np.array([], dtype='|S10') + retts = np.array([], dtype=np.int64) + retmd = np.array([], dtype=np.uint32) + it = np.nditer(tss, flags=['f_index', 'refs_ok']) + while not it.finished: + if (it.index+1 >= tss.size) : break + lat1 = lat[it.index] + lon1 = lon[it.index] + alt1 = alt[it.index] + mindist = distlimit + otheridx = -1 + it2 = np.nditer(tss[it.index+1:], flags=['f_index', 'refs_ok']) + while not it2.finished: + timediff = it2[0] - it[0] + if (timediff > timelimit): + break + if (stt[it.index] == stt[it2.index]): + it2.iternext() + continue + altdiff = abs(alt[it2.index] - alt1) + if (altdiff > distlimit): + it2.iternext() + continue + lat2 = lat[it2.index] + lon2 = lon[it2.index] + distdiff = (6371000 * math.sqrt(math.pow((lon1 * 0.01745 - lon2 * 0.01745) * + math.cos(0.5 * (lat1 * 0.01745 + lat2 * 0.01745)),2) + + math.pow((lat1 * 0.01745 - lat2 * 0.01745),2))) + if (distdiff > distlimit) : + it2.iternext() + continue + if (distdiff < mindist): + mindist = distdiff + otheridx = it2.index + it2.iternext() + if mindist < distlimit: + rets1 = numpy.append(rets1, str(stt[it.index])) + rets2 = numpy.append(rets2, str(stt[otheridx])) + retts = numpy.append(retts, int(it[0]/1000)) + retmd = numpy.append(retmd, int(mindist)) + it.iternext() + return([rets1, rets2, retts, retmd]) }; create temporary table planes as SELECT station, (ts-CAST('1970-01-01' AS timestamp)), lat, lon, alt*0.3048 FROM streams WHERE type = 2 and alt > 0 with data; -select * from numpy_distance( (SELECT * FROM planes AS p) ); +select * from numpy_distance_fast( (SELECT * FROM planes AS p) ); drop table planes; drop function numpy_distance; +drop function numpy_distance_fast; rollback; _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list