Esempio n. 1
0
 def special_load(self, obj):
     if 'data0' in self.__special__:
         import tramway.tessellation.base as tessellation
         if lazytype(obj) is tessellation.CellStats:
             obj = lazyvalue(obj, deep=True)
             try:
                 if obj._points is None:
                     raise AttributeError
             except AttributeError:
                 obj._points = self.__special__['data0']
     else:
         import pandas
         if lazytype(obj) is pandas.DataFrame:
             obj = lazyvalue(obj, deep=True)
             self.__special__['data0'] = obj
     return obj
Esempio n. 2
0
def load_rwa(path, verbose=None, lazy=False, force_load_spt_data=None):
    """
    Load a .rwa file.

    Note about laziness: the analysis tree uses an active handle to the opened file.
    As a consequence, the file should be read only once.
    It is safe to load, modify, save and then load again, but the first loaded data
    should be terminated before loading the data again.

    Arguments:

        path (str): path to .rwa file

        verbose (bool or int): verbosity level

        lazy (bool): reads the file lazily

        force_load_spt_data (bool): *new in 0.5*
            compatibility flag for pre-0.5 code;
            `None` currently defaults to `True`,
            but will default to `False` in the future

    Returns:

        tramway.core.analyses.base.Analyses:
            analysis tree;
            if `lazy` is ``True``, return type is
            :class:`tramway.core.analyses.lazy.Analyses` instead
    """
    try:
        hdf = RWAStore(path,
                       "r",
                       verbose=max(0,
                                   int(verbose) - 2) if verbose else False)
        # hdf._default_lazy = PermissivePeek
        hdf.lazy = lazy
        hdf.force_load_special = (True if force_load_spt_data is None else
                                  force_load_spt_data)
        try:
            analyses = lazyvalue(hdf.peek("analyses"))
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            if verbose is not False:
                print("cannot load file: {}".format(path))
            raise
        finally:
            if not lazy:
                hdf.close()
    except EnvironmentError as e:
        if hasattr(e, "errno") and e.errno == errno.ENOENT:
            raise
        elif e.args[1:]:
            if verbose:
                print(traceback.format_exc())
            raise OSError("HDF5 libraries may not be installed")
        else:
            raise
    return analyses
    return coerce_labels(analyses)
Esempio n. 3
0
    def special_unload(self, obj):
        # forced poke (calling lazyvalue) may not be necessary
        if "data0" in self.__special__:
            import tramway.tessellation.base as tessellation

            if issubclass(lazytype(obj), tessellation.Partition):
                obj = lazyvalue(obj, deep=True)
                if obj.points is self.__special__["data0"]:
                    import copy

                    obj = copy.copy(obj)
                    obj._points = None
        else:
            import pandas

            if issubclass(lazytype(obj), pandas.DataFrame):
                obj = lazyvalue(obj, deep=True)
                self.__special__["data0"] = obj
        return obj
Esempio n. 4
0
def load_rwa(path, verbose=None, lazy=False):
    """
    Load a .rwa file.

    Note about laziness: the analysis tree uses an active handle to the opened file.
    As a consequence, the file should be read only once.
    It is safe to load, modify, save and then load again, but the first loaded data
    should be terminated before loading the data again.

    Arguments:

        path (str): path to .rwa file

        verbose (bool or int): verbosity level

        lazy (bool): reads the file lazily

    Returns:

        tramway.core.analyses.base.Analyses:
            analysis tree;
            if `lazy` is ``True``, return type is
            :class:`tramway.core.analyses.lazy.Analyses` instead
    """
    try:
        hdf = RWAStore(path,
                       'r',
                       verbose=max(0,
                                   int(verbose) - 2) if verbose else False)
        #hdf._default_lazy = PermissivePeek
        hdf.lazy = lazy
        try:
            analyses = lazyvalue(hdf.peek('analyses'))
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            if verbose is not False:
                print('cannot load file: {}'.format(path))
            raise
        finally:
            hdf.close()
    except EnvironmentError as e:
        if hasattr(e, 'errno') and e.errno == errno.ENOENT:
            raise
        elif e.args[1:]:
            if verbose:
                print(traceback.format_exc())
            raise OSError('HDF5 libraries may not be installed')
        else:
            raise
    return coerce_labels(analyses)
Esempio n. 5
0
    def special_load(self, obj, objname, lazy=None):
        if objname != "_data":
            return obj
        if "data0" in self.__special__:
            import tramway.tessellation.base as tessellation

            if issubclass(lazytype(obj), tessellation.Partition):
                obj = lazyvalue(obj, deep=True)
                try:
                    if obj._points is None:
                        raise AttributeError
                except AttributeError:
                    obj._points = lazyvalue(self.__special__["data0"],
                                            deep=True)
        else:
            import pandas

            if issubclass(lazytype(obj), pandas.DataFrame):
                if self.force_load_special or not (self.lazy
                                                   if lazy is None else lazy):
                    obj = lazyvalue(obj, deep=True)
                self.__special__["data0"] = obj
        return obj
Esempio n. 6
0
 def get_base_grid(self, label, from_root=False):
     label = 'tramway.roi.RoiHelper.basegrid:' + label
     if from_root:
         try:
             artefact = self._extra_artefacts[label]
         except KeyError:
             artefact = None
             if self.collections.rwa_file:
                 from rwa import HDF5Store, lazyvalue
                 f = HDF5Store(self.collections.rwa_file, 'r')
                 try:
                     artefact = lazyvalue(f.peek(label))
                 except KeyError:
                     pass
                 else:
                     self._extra_artefacts[label] = artefact
                 finally:
                     f.close()
     else:
         artefact = self.analyses[label].data
     return artefact
Esempio n. 7
0
 def poke(self,
          objname,
          obj,
          container=None,
          visited=None,
          _stack=None,
          unload=None):
     if unload is not None:
         previous = self.unload
         self.unload = unload
     if visited is None:
         visited = {}
     if container is None:
         container = self.store
     try:
         obj = obj.lazy_unwrap()
     except AttributeError:
         pass
     obj = lazyvalue(obj,
                     deep=True)  # in the case it was stored not unloaded
     if self.unload and isinstance(obj, Lazy):
         # set lazy attributes to None (unset them so that memory is freed)
         #obj = copy(obj) # this causes a weird bug
         for name in obj.__lazy__:
             # `_lazy` should contain `name`;
             # if it is not, the object definition has changed
             if obj._lazy.get(name, True):
                 setattr(obj, obj.__fromlazy__(name), None)
     obj = self.special_unload(obj)
     HDF5Store.poke(self,
                    objname,
                    obj,
                    container,
                    visited=visited,
                    _stack=_stack)
     if unload is not None:
         self.unload = previous