Exemplo n.º 1
0
 def __init__(self, uri, dataset=None, **kwds):
     #---------------------------------------------
     newfile = kwds.pop('create', False)
     if uri is None and (newfile or dataset is None):
         raise TypeError("No URI given for HDF5 recording")
     if uri is not None:
         BSMLRecording.__init__(self, uri, dataset, **kwds)
     if dataset:
         if newfile:
             self._h5 = H5Recording.create(uri, str(dataset), **kwds)
             self.graph = RecordingGraph(uri, rec_class=HDF5Recording)
         else:
             self._h5 = H5Recording.open(dataset, **kwds)
             if uri is None:
                 BSMLRecording.__init__(self, self._h5.uri, dataset, **kwds)
             elif str(uri) != str(self._h5.uri):
                 raise TypeError("Wrong URI in HDF5 recording")
             self._load_metadata()
             for n, s in enumerate(self._h5.signals()):
                 signal = HDF5Signal.create_from_H5Signal(n, s)
                 signal.add_metadata(self.graph)
                 signal.clock.add_metadata(
                     self.graph)  ## We need to cache clocks in recording...
                 self.add_signal(signal)
     else:
         self._h5 = None
Exemplo n.º 2
0
 def __init__(self, uri, dataset=None, **kwds):
     # ---------------------------------------------
     newfile = kwds.pop("create", False)
     if uri is None and (newfile or dataset is None):
         raise TypeError("No URI given for HDF5 recording")
     if uri is not None:
         BSMLRecording.__init__(self, uri, dataset, **kwds)
     if dataset:
         if newfile:
             self._h5 = H5Recording.create(uri, str(dataset), **kwds)
             self.graph = RecordingGraph(uri, rec_class=HDF5Recording)
         else:
             self._h5 = H5Recording.open(dataset, **kwds)
             if uri is None:
                 BSMLRecording.__init__(self, self._h5.uri, dataset, **kwds)
             elif str(uri) != str(self._h5.uri):
                 raise TypeError("Wrong URI in HDF5 recording")
             self._load_metadata()
             for n, s in enumerate(self._h5.signals()):
                 signal = HDF5Signal.create_from_H5Signal(n, s)
                 signal.add_metadata(self.graph)
                 signal.clock.add_metadata(self.graph)  ## We need to cache clocks in recording...
                 self.add_signal(signal)
     else:
         self._h5 = None
Exemplo n.º 3
0
 def _load_metadata(self):
     # ------------------------
     """
 Set metadata attributes of the recording from its associated
 BioSignalML HDF5 file.
 """
     string, format = self._h5.get_metadata()
     if string:
         self.graph = RecordingGraph.create_from_string(self.uri, string, format=format, rec_class=HDF5Recording)
         self.add_metadata(self.graph)
     else:
         raise TypeError("No metadata in BioSignalML file")
Exemplo n.º 4
0
 def _load_metadata(self):
     #------------------------
     """
 Set metadata attributes of the recording from its associated
 BioSignalML HDF5 file.
 """
     string, format = self._h5.get_metadata()
     if string:
         self.graph = RecordingGraph.create_from_string(
             self.uri, string, format=format, rec_class=HDF5Recording)
         self.add_metadata(self.graph)
     else:
         raise TypeError("No metadata in BioSignalML file")
Exemplo n.º 5
0
class HDF5Recording(BSMLRecording):
    #==================================
    """
  A HDF5 :class:`~biosignalml.Recording`.

  :param uri: The recording's URI.
  :param dataset: The file path or URI of the BioSignalML HDF5 file for the recording.
  :param kwds: :class:`~biosignalml.Recording` attributes to set.
  """

    MIMETYPE = MIMETYPES.HDF5  #: The mimetype for BioSignalML HDF5 files
    EXTENSIONS = ['h5', 'hdf',
                  'hdf5']  #: File extensions to try when opening a file
    SignalClass = HDF5Signal  #: The class of Signals HDF5Recording

    def __init__(self, uri, dataset=None, **kwds):
        #---------------------------------------------
        newfile = kwds.pop('create', False)
        if uri is None and (newfile or dataset is None):
            raise TypeError("No URI given for HDF5 recording")
        if uri is not None:
            BSMLRecording.__init__(self, uri, dataset, **kwds)
        if dataset:
            if newfile:
                self._h5 = H5Recording.create(uri, str(dataset), **kwds)
                self.graph = RecordingGraph(uri, rec_class=HDF5Recording)
            else:
                self._h5 = H5Recording.open(dataset, **kwds)
                if uri is None:
                    BSMLRecording.__init__(self, self._h5.uri, dataset, **kwds)
                elif str(uri) != str(self._h5.uri):
                    raise TypeError("Wrong URI in HDF5 recording")
                self._load_metadata()
                for n, s in enumerate(self._h5.signals()):
                    signal = HDF5Signal.create_from_H5Signal(n, s)
                    signal.add_metadata(self.graph)
                    signal.clock.add_metadata(
                        self.graph)  ## We need to cache clocks in recording...
                    self.add_signal(signal)
        else:
            self._h5 = None

    @classmethod
    def open(cls, dataset, **kwds):
        #------------------------------
        """
    Open a BioSignalML HDF5 file to obtain a HDF5 Recording.

    :param dataset: The file path or URI of the BioSignalML HDF5 file.
    :param kwds: Other :class:`~biosignalml.Recording` attributes to set.
    """
        return cls(None, dataset, **kwds)

    @classmethod
    def create(cls, uri, dataset, **kwds):
        #-------------------------------------
        """
    Create a new recording and its BioSignalML HDF5 file.

    :param uri: The recording's URI.
    :param dataset: The file path or URI of the BioSignalML HDF5 file.
    :param kwds: Other :class:`~biosignalml.Recording` attributes to set.
    """
        return cls(uri, dataset, create=True, **kwds)


###    self.dataset = dataset    ### Only set dataset in metadata when storing into a repository??

    def close(self, prefixes=None):
        #------------------------------
        """ Close a recording`. """
        if self._h5:
            self._save_metadata(prefixes=prefixes)
            self._h5.close()
            self._h5 = None

    def new_signal(self, uri, units, id=None, **kwds):
        #-------------------------------------------------
        """
    Create a new signal and add it to the recording.

    :param uri: The URI for the signal.
    :param units: The physical units of the signal's data.
    :rtype: :class:`HDF5Signal`
    :param kwds: Other :class:`~biosignalml.Signal` attributes to set.
    """
        sig = BSMLRecording.new_signal(self, uri, units, id=id, **kwds)
        if self._h5:
            if sig.clock:
                self._h5.create_clock(sig.clock.uri,
                                      sig.clock.units,
                                      times=sig.clock.times)
                kwds['clock'] = sig.clock.uri
            sig._h5 = self._h5.create_signal(sig.uri, units, **kwds)
        return sig

    def _save_metadata(self, format=rdf.Format.TURTLE, prefixes=None):
        #-----------------------------------------------------------------
        """
    Save all metadata associated with the recording in its
    BioSignalML HDF5 file.

    :param format: The :class:`~biosignalml.rdf.Format` in which to serialise RDF.
    :param prefixes: An optional dictionary of namespace abbreviations and URIs.
    """
        self.save_metadata_to_graph(self.graph)
        self._h5.store_metadata(
            self.graph.serialise(format=format,
                                 prefixes=prefixes,
                                 base=self.uri), rdf.Format.mimetype(format))

    def _load_metadata(self):
        #------------------------
        """
    Set metadata attributes of the recording from its associated
    BioSignalML HDF5 file.
    """
        string, format = self._h5.get_metadata()
        if string:
            self.graph = RecordingGraph.create_from_string(
                self.uri, string, format=format, rec_class=HDF5Recording)
            self.add_metadata(self.graph)
        else:
            raise TypeError("No metadata in BioSignalML file")

    def initialise(self, **kwds):
        #----------------------------
        """
    Set recording and associated signal attributes
    once the recording's dataset is known.
    """
        if self.dataset is not None and kwds.pop('open_dataset', True):
            dataset = str(self.dataset)
            creating = kwds.pop('create', False)
            try:
                self._h5 = H5Recording.open(dataset, **kwds)
            except IOError:
                if not creating: raise
                H5Recording.create(self.uri, dataset, **kwds)
                self._h5 = H5Recording.open(dataset, **kwds)
            if kwds.pop('create_signals', False): kwds['create'] = True
            for s in self.signals():
                HDF5Signal.initialise_class(s, **kwds)
Exemplo n.º 6
0
 def get_recording(self, uri, graph_uri=None, **kwds):
     # ----------------------------------------------------
     rgraph = RecordingGraph.create_from_store(self, uri, rec_class=self.RecordingClass)
     return rgraph.get_recording(repository=self, **kwds)
Exemplo n.º 7
0
 def get_recording(self, uri, graph_uri=None, **kwds):
     #----------------------------------------------------
     rgraph = RecordingGraph.create_from_store(
         self, uri, rec_class=self.RecordingClass)
     return rgraph.get_recording(repository=self, **kwds)
Exemplo n.º 8
0
class HDF5Recording(BSMLRecording):
    # ==================================
    """
  A HDF5 :class:`~biosignalml.Recording`.

  :param uri: The recording's URI.
  :param dataset: The file path or URI of the BioSignalML HDF5 file for the recording.
  :param kwds: :class:`~biosignalml.Recording` attributes to set.
  """

    MIMETYPE = MIMETYPES.HDF5  #: The mimetype for BioSignalML HDF5 files
    EXTENSIONS = ["h5", "hdf", "hdf5"]  #: File extensions to try when opening a file
    SignalClass = HDF5Signal  #: The class of Signals HDF5Recording

    def __init__(self, uri, dataset=None, **kwds):
        # ---------------------------------------------
        newfile = kwds.pop("create", False)
        if uri is None and (newfile or dataset is None):
            raise TypeError("No URI given for HDF5 recording")
        if uri is not None:
            BSMLRecording.__init__(self, uri, dataset, **kwds)
        if dataset:
            if newfile:
                self._h5 = H5Recording.create(uri, str(dataset), **kwds)
                self.graph = RecordingGraph(uri, rec_class=HDF5Recording)
            else:
                self._h5 = H5Recording.open(dataset, **kwds)
                if uri is None:
                    BSMLRecording.__init__(self, self._h5.uri, dataset, **kwds)
                elif str(uri) != str(self._h5.uri):
                    raise TypeError("Wrong URI in HDF5 recording")
                self._load_metadata()
                for n, s in enumerate(self._h5.signals()):
                    signal = HDF5Signal.create_from_H5Signal(n, s)
                    signal.add_metadata(self.graph)
                    signal.clock.add_metadata(self.graph)  ## We need to cache clocks in recording...
                    self.add_signal(signal)
        else:
            self._h5 = None

    @classmethod
    def open(cls, dataset, **kwds):
        # ------------------------------
        """
    Open a BioSignalML HDF5 file to obtain a HDF5 Recording.

    :param dataset: The file path or URI of the BioSignalML HDF5 file.
    :param kwds: Other :class:`~biosignalml.Recording` attributes to set.
    """
        return cls(None, dataset, **kwds)

    @classmethod
    def create(cls, uri, dataset, **kwds):
        # -------------------------------------
        """
    Create a new recording and its BioSignalML HDF5 file.

    :param uri: The recording's URI.
    :param dataset: The file path or URI of the BioSignalML HDF5 file.
    :param kwds: Other :class:`~biosignalml.Recording` attributes to set.
    """
        return cls(uri, dataset, create=True, **kwds)

    ###    self.dataset = dataset    ### Only set dataset in metadata when storing into a repository??

    def close(self, prefixes=None):
        # ------------------------------
        """ Close a recording`. """
        if self._h5:
            self._save_metadata(prefixes=prefixes)
            self._h5.close()
            self._h5 = None

    def new_signal(self, uri, units, id=None, **kwds):
        # -------------------------------------------------
        """
    Create a new signal and add it to the recording.

    :param uri: The URI for the signal.
    :param units: The physical units of the signal's data.
    :rtype: :class:`HDF5Signal`
    :param kwds: Other :class:`~biosignalml.Signal` attributes to set.
    """
        sig = BSMLRecording.new_signal(self, uri, units, id=id, **kwds)
        if self._h5:
            if sig.clock:
                self._h5.create_clock(sig.clock.uri, sig.clock.units, times=sig.clock.times)
                kwds["clock"] = sig.clock.uri
            sig._h5 = self._h5.create_signal(sig.uri, units, **kwds)
        return sig

    def _save_metadata(self, format=rdf.Format.TURTLE, prefixes=None):
        # -----------------------------------------------------------------
        """
    Save all metadata associated with the recording in its
    BioSignalML HDF5 file.

    :param format: The :class:`~biosignalml.rdf.Format` in which to serialise RDF.
    :param prefixes: An optional dictionary of namespace abbreviations and URIs.
    """
        self.save_metadata_to_graph(self.graph)
        self._h5.store_metadata(
            self.graph.serialise(format=format, prefixes=prefixes, base=self.uri), rdf.Format.mimetype(format)
        )

    def _load_metadata(self):
        # ------------------------
        """
    Set metadata attributes of the recording from its associated
    BioSignalML HDF5 file.
    """
        string, format = self._h5.get_metadata()
        if string:
            self.graph = RecordingGraph.create_from_string(self.uri, string, format=format, rec_class=HDF5Recording)
            self.add_metadata(self.graph)
        else:
            raise TypeError("No metadata in BioSignalML file")

    def initialise(self, **kwds):
        # ----------------------------
        """
    Set recording and associated signal attributes
    once the recording's dataset is known.
    """
        if self.dataset is not None and kwds.pop("open_dataset", True):
            dataset = str(self.dataset)
            creating = kwds.pop("create", False)
            try:
                self._h5 = H5Recording.open(dataset, **kwds)
            except IOError:
                if not creating:
                    raise
                H5Recording.create(self.uri, dataset, **kwds)
                self._h5 = H5Recording.open(dataset, **kwds)
            if kwds.pop("create_signals", False):
                kwds["create"] = True
            for s in self.signals():
                HDF5Signal.initialise_class(s, **kwds)