Difficulty reading a matrix as a node in rdflib -
i created rdf file images stored in nodes matrices. when try read them cannot matrix form: example
from rflib import literal mm = np.random.normal(0,1,(3,3)) l = literal(mm)
it easy matrix l.value
in [494]: l out[494]: rdflib.term.literal(u'[[-1.39304728 0.39093531 0.88042378]\n [ 0.22605682 0.56064787 -0.75176713]\n [ 0.57021203 0.31796492 -0.53303191]]') in [495]: l.value out[495]: array([[-1.39304728, 0.39093531, 0.88042378], [ 0.22605682, 0.56064787, -0.75176713], [ 0.57021203, 0.31796492, -0.53303191]])
however when execute sparql stored in image_nodes get:
in [501]: res = [q q in image_nodes] in [502]: res[0][0] out[502]: rdflib.term.literal(u'[[ 0. 0. 0. ..., 0. 0. 0.]\n [ 0. 0. 0. ..., 0. 0. 0.]\n [ 0. 0. 0. ..., 0. 0. 0.]\n ..., \n [ 0. 0. 0. ..., 0. 0. 0.]\n [ 0. 0. 0. ..., 0. 0. 0.]\n [ 0. 0. 0. ..., 0. 0. 0.]]') in [503]: (res[0][0]).value out[503]: u'[[ 0. 0. 0. ..., 0. 0. 0.]\n [ 0. 0. 0. ..., 0. 0. 0.]\n [ 0. 0. 0. ..., 0. 0. 0.]\n ..., \n [ 0. 0. 0. ..., 0. 0. 0.]\n [ 0. 0. 0. ..., 0. 0. 0.]\n [ 0. 0. 0. ..., 0. 0. 0.]]'
why cant matrix format time? in unicode , resistant transformation. thanks
as didn't provide sparql query parts of answer guess-work...
if create rdflib.literal(obj)
rdflib try convert given object suitable rdf (xsd) representation. python standard types mapped here. if given object doesn't have of types (like np.array
) _castpythontoliteral(obj)
method fall return obj
. l.value
observe here untouched obj
passed literal(obj)
, can seen in constructor code, arguably confusing. actual returned inst
of literal
unicode
object, can "content" of literal
unicode(l)
. important it's stored in stores / sparql endpoint.
now if retrieve literal sparql endpoint , if obj
none of standard types, endpoint ever knew string representation of obj
, returns l = literal(str_rep_of_obj)
. sparql endpoint's standpoint string. rdflib see string , why unicode string in res[0][0].value
.
in order fix need de-serialize string content of res[0][0]
np.array
, e.g. json this:
l = literal(json.dumps(mm.tolist()))
and later
imagenode = np.array(json.loads(unicode(res_literal)))
one additional remark though... databases saving binary blobs such images in database not idea. it's better put binary data somewhere , save link in database. isn't cool rdf uses uris ;)
Comments
Post a Comment