[hdf-compass] 157/295: Add tests to bag_model + fix __init__ for hdf5_model

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Sun May 8 10:35:39 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/master
in repository hdf-compass.

commit bb8982eef8c526dd6bd38218ce5714313fda77c9
Author: giumas <giumas at yahoo.it>
Date:   Tue Oct 20 15:33:39 2015 +0200

    Add tests to bag_model + fix __init__ for hdf5_model
---
 hdf_compass/{hdf5_model => bag_model}/test.py    |  26 +-
 hdf_compass/hdf5_model/__init__.py               | 300 +----------------------
 hdf_compass/hdf5_model/{__init__.py => model.py} |   0
 hdf_compass/hdf5_model/test.py                   |  17 +-
 4 files changed, 9 insertions(+), 334 deletions(-)

diff --git a/hdf_compass/hdf5_model/test.py b/hdf_compass/bag_model/test.py
similarity index 59%
copy from hdf_compass/hdf5_model/test.py
copy to hdf_compass/bag_model/test.py
index 82a8c7e..26a5ce3 100644
--- a/hdf_compass/hdf5_model/test.py
+++ b/hdf_compass/bag_model/test.py
@@ -11,27 +11,13 @@
 ##############################################################################
 from __future__ import absolute_import, division, print_function
 
-from hdf_compass.compass_model.test import container, store
-from hdf_compass.hdf5_model import HDF5Group, HDF5Store
-from hdf_compass.utils import is_win
-
 import os
-import sys
-
-import hdf_compass
-
-# locate test file
-data_folder = os.path.abspath(os.path.join(os.path.dirname(hdf_compass.__file__), 'data'))
-test_file = os.path.join(data_folder, 'hdf5', 'tall.h5')
-if not os.path.exists(test_file):
-    raise RuntimeError("Unable to locate test file: %s" % test_file)
 
-# create url
-if is_win:
-    url = 'file:///' + os.path.abspath(test_file)
-else:
-    url = 'file://' + os.path.abspath(test_file)
+from hdf_compass.compass_model.test import store, container
+from hdf_compass.bag_model import BAGGroup, BAGStore
+from hdf_compass.utils import data_url
 
-s = store(HDF5Store, url)
-c = container(HDF5Store, url, HDF5Group, "/")
+url = os.path.join(data_url(), "bag", "bdb_00.bag")
 
+s = store(BAGStore, url)
+c = container(BAGStore, url, BAGGroup, "/")
\ No newline at end of file
diff --git a/hdf_compass/hdf5_model/__init__.py b/hdf_compass/hdf5_model/__init__.py
index b6f590c..75ee7e6 100644
--- a/hdf_compass/hdf5_model/__init__.py
+++ b/hdf_compass/hdf5_model/__init__.py
@@ -9,308 +9,10 @@
 # distribution tree.  If you do not have access to this file, you may        #
 # request a copy from help at hdfgroup.org.                                     #
 ##############################################################################
-
-"""
-Implementation of compass_model classes for HDF5 files.
-"""
 from __future__ import absolute_import, division, print_function, unicode_literals
 
-from itertools import groupby
-import sys
-import os.path as op
-import posixpath as pp
-
-import h5py
+from .model import HDF5Store, HDF5Group, HDF5Dataset, HDF5KV
 
 import logging
 log = logging.getLogger(__name__)
 log.addHandler(logging.NullHandler())
-
-# Py2App can't successfully import otherwise
-from hdf_compass import compass_model
-from hdf_compass.utils import url2path
-
-
-def sort_key(name):
-    """ Sorting key for names in an HDF5 group.
-
-    We provide "natural" sort order; e.g. "7" comes before "12".
-    """
-    return [(int(''.join(g)) if k else ''.join(g)) for k, g in groupby(name, key=unicode.isdigit)]
-
-
-class HDF5Store(compass_model.Store):
-    """
-    Data store implementation using an HDF5 file.
-
-    Keys are the full names of objects in the file.
-    """
-
-    file_extensions = {'HDF5 File': ['*.hdf5', '*.h5']}
-
-    def __contains__(self, key):
-        return key in self.f
-
-    @property
-    def url(self):
-        return self._url
-
-    @property
-    def display_name(self):
-        return op.basename(self.f.filename)
-
-    @property
-    def root(self):
-        return self['/']
-
-    @property
-    def valid(self):
-        return bool(self.f)
-
-    @staticmethod
-    def can_handle(url):
-        if not url.startswith('file://'):
-            return False
-        path = url2path(url)
-        if not h5py.is_hdf5(path):
-            return False
-        return True
-
-    def __init__(self, url):
-        if not self.can_handle(url):
-            raise ValueError(url)
-        self._url = url
-        path = url2path(url)
-        self.f = h5py.File(path, 'r')
-
-    def close(self):
-        self.f.close()
-
-    def get_parent(self, key):
-        # HDFCompass requires the parent of the root container be None
-        if key == "" or key == "/":
-            return None
-        pkey = pp.dirname(key)
-        if pkey == "":
-            pkey = "/"
-        return self[pkey]
-
-
-class HDF5Group(compass_model.Container):
-    """
-    Represents an HDF5 group, to be displayed in the browser view.
-    """
-
-    class_kind = "HDF5 Group"
-
-    @staticmethod
-    def can_handle(store, key):
-        return key in store and isinstance(store.f[key], h5py.Group)
-
-    @property
-    def _names(self):
-
-        # Lazily build the list of names; this helps when browsing big files
-        if self._xnames is None:
-
-            self._xnames = list(self._group)
-
-            # Natural sort is expensive
-            if len(self._xnames) < 1000:
-                self._xnames.sort(key=sort_key)
-
-        return self._xnames
-
-    def __init__(self, store, key):
-        self._store = store
-        self._key = key
-        self._group = store.f[key]
-        self._xnames = None
-
-    @property
-    def key(self):
-        return self._key
-
-    @property
-    def store(self):
-        return self._store
-
-    @property
-    def display_name(self):
-        name = pp.basename(self.key)
-        if name == "":
-            name = '/'
-        return name
-
-    @property
-    def display_title(self):
-        return "%s %s" % (self.store.display_name, self.key)
-
-    @property
-    def description(self):
-        return 'Group "%s" (%d members)' % (self.display_name, len(self))
-
-    def __len__(self):
-        return len(self._group)
-
-    def __iter__(self):
-        for name in self._names:
-            yield self.store[pp.join(self.key, name)]
-
-    def __getitem__(self, idx):
-        name = self._names[idx]
-        return self.store[pp.join(self.key, name)]
-
-
-class HDF5Dataset(compass_model.Array):
-    """
-    Represents an HDF5 dataset.
-    """
-
-    class_kind = "HDF5 Dataset"
-
-    @staticmethod
-    def can_handle(store, key):
-        return key in store and isinstance(store.f[key], h5py.Dataset)
-
-    def __init__(self, store, key):
-        self._store = store
-        self._key = key
-        self._dset = store.f[key]
-
-    @property
-    def key(self):
-        return self._key
-
-    @property
-    def store(self):
-        return self._store
-
-    @property
-    def display_name(self):
-        return pp.basename(self.key)
-
-    @property
-    def description(self):
-        return 'Dataset "%s"' % (self.display_name,)
-
-    @property
-    def shape(self):
-        return self._dset.shape
-
-    @property
-    def dtype(self):
-        return self._dset.dtype
-
-    def __getitem__(self, args):
-        return self._dset[args]
-
-
-class HDF5KV(compass_model.KeyValue):
-    """
-    A KeyValue node used for HDF5 attributes.
-    """
-
-    class_kind = "HDF5 Attributes"
-
-    @staticmethod
-    def can_handle(store, key):
-        return key in store.f
-
-    def __init__(self, store, key):
-        self._store = store
-        self._key = key
-        self._obj = store.f[key]
-        self._names = self._obj.attrs.keys()
-
-    @property
-    def key(self):
-        return self._key
-
-    @property
-    def store(self):
-        return self._store
-
-    @property
-    def display_name(self):
-        n = pp.basename(self.key)
-        return n if n != '' else '/'
-
-    @property
-    def description(self):
-        return self.display_name
-
-    @property
-    def keys(self):
-        return self._names[:]
-
-    def __getitem__(self, name):
-        return self._obj.attrs[name]
-
-
-class HDF5Image(compass_model.Image):
-    """
-    True-color images.
-    """
-
-    class_kind = "HDF5 Truecolor Image"
-
-    @staticmethod
-    def can_handle(store, key):
-        if key not in store:
-            return False
-        obj = store.f[key]
-        if obj.attrs.get('CLASS') != 'IMAGE':
-            return False
-        if obj.attrs.get('IMAGE_SUBCLASS') != 'IMAGE_TRUECOLOR':
-            return False
-        if obj.attrs.get('INTERLACE_MODE') != 'INTERLACE_PIXEL':
-            return False
-        return True
-
-    def __init__(self, store, key):
-        self._store = store
-        self._key = key
-        self._obj = store.f[key]
-
-    @property
-    def key(self):
-        return self._key
-
-    @property
-    def store(self):
-        return self._store
-
-    @property
-    def display_name(self):
-        n = pp.basename(self.key)
-        return n if n != '' else '/'
-
-    @property
-    def description(self):
-        return self.display_name
-
-    @property
-    def width(self):
-        return self._obj.shape[1]
-
-    @property
-    def height(self):
-        return self._obj.shape[0]
-
-    @property
-    def palette(self):
-        return None
-
-    @property
-    def data(self):
-        return self._obj[:]
-
-
-# Register handlers    
-HDF5Store.push(HDF5KV)
-HDF5Store.push(HDF5Dataset)
-HDF5Store.push(HDF5Group)
-HDF5Store.push(HDF5Image)
-
-compass_model.push(HDF5Store)
diff --git a/hdf_compass/hdf5_model/__init__.py b/hdf_compass/hdf5_model/model.py
similarity index 100%
copy from hdf_compass/hdf5_model/__init__.py
copy to hdf_compass/hdf5_model/model.py
diff --git a/hdf_compass/hdf5_model/test.py b/hdf_compass/hdf5_model/test.py
index 82a8c7e..dbe2977 100644
--- a/hdf_compass/hdf5_model/test.py
+++ b/hdf_compass/hdf5_model/test.py
@@ -13,24 +13,11 @@ from __future__ import absolute_import, division, print_function
 
 from hdf_compass.compass_model.test import container, store
 from hdf_compass.hdf5_model import HDF5Group, HDF5Store
-from hdf_compass.utils import is_win
+from hdf_compass.utils import data_url
 
 import os
-import sys
 
-import hdf_compass
-
-# locate test file
-data_folder = os.path.abspath(os.path.join(os.path.dirname(hdf_compass.__file__), 'data'))
-test_file = os.path.join(data_folder, 'hdf5', 'tall.h5')
-if not os.path.exists(test_file):
-    raise RuntimeError("Unable to locate test file: %s" % test_file)
-
-# create url
-if is_win:
-    url = 'file:///' + os.path.abspath(test_file)
-else:
-    url = 'file://' + os.path.abspath(test_file)
+url = os.path.join(data_url(), "hdf5", "tall.h5")
 
 s = store(HDF5Store, url)
 c = container(HDF5Store, url, HDF5Group, "/")

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/hdf-compass.git



More information about the debian-science-commits mailing list