Esempio n. 1
0
    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
Esempio n. 2
0
    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
Esempio n. 3
0
    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
Esempio n. 4
0
    def get_annotation(self, uri, graph_uri=None):
        #---------------------------------------------
        """
    Get an Annotation from the repository.

    :param uri: The URI of an Annotation.
    :param graph_uri: An optional URI of the graph to query.
    :rtype: :class:`~biosignalml.Annotation`
    """
        # The following line works around a Virtuoso problem
        if graph_uri is None:
            graph_uri = self.get_graph_and_recording_uri(uri)[0]
        graph = self.get_resource_as_graph(uri, BSML.Annotation, graph_uri)

        ##### Put into Annotation/Segment.load_from_graph()   ????
        for sg in graph.get_objects(uri,
                                    DCT.subject):  ## This could be improved...
            graph.append_graph(
                self.get_resource_as_graph(sg.uri, BSML.Segment, graph_uri))
            for tm in graph.get_objects(
                    sg.uri, BSML.time):  ## This could be improved...
                graph.append_graph(
                    self.get_resource_as_graph(tm.uri, BSML.Instant,
                                               graph_uri))
                graph.append_graph(
                    self.get_resource_as_graph(tm.uri, BSML.Interval,
                                               graph_uri))

        return Annotation.create_from_graph(uri, graph)
Esempio n. 5
0
    def get_annotation(self, uri):
        # -----------------------------
        """
    Get an Annotation from its recording's graph.

    :param uri: The URI of an Annotation.
    :rtype: :class:`~biosignalml.Annotation`
    """
        return Annotation.create_from_graph(uri, self)
Esempio n. 6
0
    def get_annotation(self, uri):
        #-----------------------------
        """
    Get an Annotation from its recording's graph.

    :param uri: The URI of an Annotation.
    :rtype: :class:`~biosignalml.Annotation`
    """
        return Annotation.create_from_graph(uri, self)
Esempio n. 7
0
 def post(self):
     #--------------
     name = self.full_uri
     text = self.get_argument('annotation', '').strip()
     if self.get_argument('action') == 'Annotate' and text:
         repo = options.repository
         target = self.get_argument('target')
         recording = repo.get_recording(target,
                                        with_signals=False,
                                        open_dataset=False)
         ann = Annotation.Note(recording.make_uri(prefix='annotation'),
                               target,
                               text,
                               creator='%s/user/%s' %
                               (repo.uri, self.current_user)
                               if self.current_user is not None else None)
         repo.extend_graph(recording.graph.uri, ann.metadata_as_string())
     self._show_contents(name, False)
Esempio n. 8
0
    def get_annotation(self, uri, graph_uri=None):
        # ---------------------------------------------
        """
    Get an Annotation from the repository.

    :param uri: The URI of an Annotation.
    :param graph_uri: An optional URI of the graph to query.
    :rtype: :class:`~biosignalml.Annotation`
    """
        # The following line works around a Virtuoso problem
        if graph_uri is None:
            graph_uri = self.get_graph_and_recording_uri(uri)[0]
        graph = self.get_resource_as_graph(uri, BSML.Annotation, graph_uri)

        ##### Put into Annotation/Segment.load_from_graph()   ????
        for sg in graph.get_objects(uri, DCT.subject):  ## This could be improved...
            graph.append_graph(self.get_resource_as_graph(sg.uri, BSML.Segment, graph_uri))
            for tm in graph.get_objects(sg.uri, BSML.time):  ## This could be improved...
                graph.append_graph(self.get_resource_as_graph(tm.uri, BSML.Instant, graph_uri))
                graph.append_graph(self.get_resource_as_graph(tm.uri, BSML.Interval, graph_uri))

        return Annotation.create_from_graph(uri, graph)
Esempio n. 9
0
    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
Esempio n. 10
0
  r.save_metadata_to_graph(g)

  s = MyRecording.create_from_graph('http://example.org/uri1', g)
  assert(r.metadata_as_string(rdf.Format.TURTLE) == s.metadata_as_string(rdf.Format.TURTLE))
  s.comment='From graph'

  user = '******'

  a1 = 'http://example.org/annotation/1'
  a2 = 'http://example.org/annotation/2'
  a3 = 'http://example.org/annotation/3'

  t1 = 'http://example.org/onto#tag1'
  t2 = 'http://example.org/onto#tag2'
  t3 = 'http://example.org/onto#tag3'
  a = Annotation.Note(a1, s.uri, user, 'A test recording...')
  b = Annotation.Tag(a2, s.uri, user, t1)
  c = Annotation(a3, s.uri, user, tags=[t2, t3], text='Multiple tags')

  #print a.metadata_as_string(rdf.Format.TURTLE)
  #print b.metadata_as_string(rdf.Format.TURTLE)
  #print c.metadata_as_string(rdf.Format.TURTLE)

  c.save_metadata_to_graph(g)
  d = Annotation.create_from_graph(a3, g)
  assert(c.metadata_as_string(rdf.Format.TURTLE) == d.metadata_as_string(rdf.Format.TURTLE))

  s.metadata['zz'] = [ a, b, c ]
  print s.metadata_as_string(rdf.Format.TURTLE)