def get_annotations(self): #------------------------- """ Return all Annotations about the recording. :rtype: list of bsml:Annotations """ sparql = """PREFIX bsml: <%(bsml)s> PREFIX rdfs: <%(rdfs)s> PREFIX dct: <%(dct)s> PREFIX prv: <%(prv)s> PREFIX tl: <%(tl)s> select ?ann ?about ?stype ?comment ?tag ?created ?source ?tm ?start ?duration ?timeline where { ?ann a bsml:Annotation minus { [] prv:precededBy ?ann } { ?ann dct:subject ?about . ?about a ?stype . optional { ?ann rdfs:comment ?comment } optional { ?ann dct:created ?created } optional { ?ann bsml:tag ?tag } { ?ann dct:subject <%(subject)s> } union { ?about a bsml:Segment ; dct:source <%(subject)s> ; dct:source ?source ; bsml:time ?tm . ?tm tl:timeline ?timeline . { ?tm a bsml:Interval ; tl:start ?start ; tl:duration ?duration } union { ?tm a bsml:Instant ; tl:at ?start } } } }""" % dict(subject=self._rec_uri, bsml=BSML.prefix, rdfs=RDFS.prefix, dct=DCT.prefix, prv=PRV.prefix, tl=TL.prefix) anns = [[ str(a[0].uri), str(a[1].uri), str(a[2]), a[3].value, a[5].value, str(a[6].uri), str(a[7].uri), a[8].value, None if a[9] in ['', None] else a[9].value, str(a[10].uri) ] for a in self.query(sparql)] anns.sort(key=operator.itemgetter(7, 1, 0)) # start, about, uri annotations = [] for a in anns: ann = Annotation(a[0], about=a[1], comment=a[3], created=a[4]) if a[2] == str(BSML.Segment): time = TemporalEntity.create(a[6], a[7], a[8], timeline=a[9]) ann.about = Segment(a[1], source=a[5], time=time) annotations.append(ann) return annotations
def get_annotations(self, subject, graph_uri=None): #-------------------------------------------------- """ Return all Annotations about a subject. This is significantly faster (approx 15x) then getting a list of URIs (fast) and then creating Annotations (slow). Code could be used as a template for general abstract object creation based on PropertyMap and bypassing internal use of RDF -- instead map directly to and from SPARQL. :param subject: The URI of the subject. :param graph_uri: An optional URI of the graph to query. :rtype: list of bsml:Annotations """ anns = self.get_resources( BSML.Annotation, rvars= '?ann ?about ?stype ?comment ?tag ?created ?source ?tm ?start ?duration ?timeline', condition='''minus { [] prv:precededBy ?ann } { ?ann dct:subject ?about . ?about a ?stype . optional { ?ann rdfs:comment ?comment } optional { ?ann dct:created ?created } optional { ?ann bsml:tag ?tag } { ?ann dct:subject <%(subject)s> } union { ?about a bsml:Segment ; dct:source <%(subject)s> ; dct:source ?source ; bsml:time ?tm . ?tm tl:timeline ?timeline . { ?tm a bsml:Interval ; tl:start ?start ; tl:duration ?duration } union { ?tm a bsml:Instant ; tl:at ?start } } }''' % dict(subject=subject), prefixes=dict(bsml=BSML.prefix, dct=DCT.prefix, prv=PRV.prefix, tl=TL.prefix), graph=graph_uri, # order = '?start', ## Virtuoso doesn't correctly sort xsd:dayTimeDuration ) anns.sort(key=operator.itemgetter(9, 2, 1)) # start, about, uri annotations = [] for a in anns: ann = Annotation(a[1], about=a[2], comment=a[4], created=a[6]) if str(a[3]) == str(BSML.Segment): time = TemporalEntity.create( a[8], float(a[9]), duration=None if a[10] in ['', None] else float(a[10]), timeline=a[11]) ann.about = Segment(a[2], source=a[7], time=time) annotations.append(ann) return annotations
def create_from_graph(cls, uri, graph, **kwds): #---------------------------------------------- from biosignalml.data.time import TemporalEntity # Prevent a circular import self = cls(uri, None, **kwds) self.add_metadata(graph) if self.time is not None: self.time = TemporalEntity.create_from_graph(self.time, graph) return self
def get_annotations(self, subject, graph_uri=None): # -------------------------------------------------- """ Return all Annotations about a subject. This is significantly faster (approx 15x) then getting a list of URIs (fast) and then creating Annotations (slow). Code could be used as a template for general abstract object creation based on PropertyMap and bypassing internal use of RDF -- instead map directly to and from SPARQL. :param subject: The URI of the subject. :param graph_uri: An optional URI of the graph to query. :rtype: list of bsml:Annotations """ anns = self.get_resources( BSML.Annotation, rvars="?ann ?about ?stype ?comment ?tag ?created ?source ?tm ?start ?duration ?timeline", condition="""minus { [] prv:precededBy ?ann } { ?ann dct:subject ?about . ?about a ?stype . optional { ?ann rdfs:comment ?comment } optional { ?ann dct:created ?created } optional { ?ann bsml:tag ?tag } { ?ann dct:subject <%(subject)s> } union { ?about a bsml:Segment ; dct:source <%(subject)s> ; dct:source ?source ; bsml:time ?tm . ?tm tl:timeline ?timeline . { ?tm a bsml:Interval ; tl:start ?start ; tl:duration ?duration } union { ?tm a bsml:Instant ; tl:at ?start } } }""" % dict(subject=subject), prefixes=dict(bsml=BSML.prefix, dct=DCT.prefix, prv=PRV.prefix, tl=TL.prefix), graph=graph_uri, # order = '?start', ## Virtuoso doesn't correctly sort xsd:dayTimeDuration ) anns.sort(key=operator.itemgetter(9, 2, 1)) # start, about, uri annotations = [] for a in anns: ann = Annotation(a[1], about=a[2], comment=a[4], created=a[6]) if str(a[3]) == str(BSML.Segment): time = TemporalEntity.create( a[8], float(a[9]), duration=None if a[10] in ["", None] else float(a[10]), timeline=a[11] ) ann.about = Segment(a[2], source=a[7], time=time) annotations.append(ann) return annotations
def create_from_graph(cls, uri, graph, **kwds): #---------------------------------------------- ''' Create a new instance of a Segment, setting attributes from RDF triples in a graph. :param uri: The URI of the Segment. :param graph: A RDF graph. :type graph: :class:`~biosignalml.rdf.Graph` :rtype: :class:`Segment` ''' from biosignalml.data.time import TemporalEntity # Prevent a circular import self = super(Segment, cls).create_from_graph(cls, uri, graph, **kwds) if self.time is not None: ## Could add_metadata automatically do this for sub-elements?? ## PropertyMap would need to know class (TemporalEntity) self.time = TemporalEntity.create_from_graph(self.time, graph) return self
def get_annotations(self): # ------------------------- """ Return all Annotations about the recording. :rtype: list of bsml:Annotations """ sparql = """PREFIX bsml: <%(bsml)s> PREFIX rdfs: <%(rdfs)s> PREFIX dct: <%(dct)s> PREFIX prv: <%(prv)s> PREFIX tl: <%(tl)s> select ?ann ?about ?stype ?comment ?tag ?created ?source ?tm ?start ?duration ?timeline where { ?ann a bsml:Annotation minus { [] prv:precededBy ?ann } { ?ann dct:subject ?about . ?about a ?stype . optional { ?ann rdfs:comment ?comment } optional { ?ann dct:created ?created } optional { ?ann bsml:tag ?tag } { ?ann dct:subject <%(subject)s> } union { ?about a bsml:Segment ; dct:source <%(subject)s> ; dct:source ?source ; bsml:time ?tm . ?tm tl:timeline ?timeline . { ?tm a bsml:Interval ; tl:start ?start ; tl:duration ?duration } union { ?tm a bsml:Instant ; tl:at ?start } } } }""" % dict( subject=self._rec_uri, bsml=BSML.prefix, rdfs=RDFS.prefix, dct=DCT.prefix, prv=PRV.prefix, tl=TL.prefix ) anns = [ [ str(a[0].uri), str(a[1].uri), str(a[2]), a[3].value, a[5].value, str(a[6].uri), str(a[7].uri), a[8].value, None if a[9] in ["", None] else a[9].value, str(a[10].uri), ] for a in self.query(sparql) ] anns.sort(key=operator.itemgetter(7, 1, 0)) # start, about, uri annotations = [] for a in anns: ann = Annotation(a[0], about=a[1], comment=a[3], created=a[4]) if a[2] == str(BSML.Segment): time = TemporalEntity.create(a[6], a[7], a[8], timeline=a[9]) ann.about = Segment(a[1], source=a[5], time=time) annotations.append(ann) return annotations