[hdf-compass] 192/295: Display string dataset in HDF5_model

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Sun May 8 10:35:43 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 0022d6dd61ca02138571ba314eb88d14fb063f2b
Author: giumas <giumas at yahoo.it>
Date:   Mon Oct 26 21:53:56 2015 -0400

    Display string dataset in HDF5_model
---
 hdf_compass/array_model/model.py   | 81 ++++++++++++++++++++++++++++++++++++--
 hdf_compass/hdf5_model/model.py    | 79 ++++++++++++++++++++++++++++++++-----
 hdf_compass/opendap_model/model.py | 20 +++-------
 3 files changed, 152 insertions(+), 28 deletions(-)

diff --git a/hdf_compass/array_model/model.py b/hdf_compass/array_model/model.py
index fbc56ac..ae43888 100644
--- a/hdf_compass/array_model/model.py
+++ b/hdf_compass/array_model/model.py
@@ -35,9 +35,14 @@ DATA = {'array://localhost/a_0d': np.array(1),
         'array://localhost/cmp_3d': np.ones((10, 10, 10), dtype=DT_CMP),
         'array://localhost/cmp_4d': np.ones((10, 10, 10, 10), dtype=DT_CMP),
         'array://localhost/S_0d': np.array(b"Hello"),
-        'array://localhost/S_1d': np.array((b"Hello",)),
-        'array://localhost/U_0d': np.array("Hello"),
-        'array://localhost/U_1d': np.array(("Hello",)),
+        'array://localhost/S_1d': np.array([b"Hello", b"Ciao"]),
+        'array://localhost/S_2d': np.array([[b"Hello", b"Ciao"], [b"Hello", b"Ciao"]]),
+        'array://localhost/S_3d': np.array([[[b"Hello", b"Ciao"], [b"Hello", b"Ciao"]],
+                                            [[b"Hello", b"Ciao"], [b"Hello", b"Ciao"]]]),
+        'array://localhost/U_1d': np.array(["Hello", "Ciao"]),
+        'array://localhost/U_2d': np.array([["Hello", "Ciao"], ["Hello", "Ciao"]]),
+        'array://localhost/U_3d': np.array([[["Hello", "Ciao"], ["Hello", "Ciao"]],
+                                            [["Hello", "Ciao"], ["Hello", "Ciao"]]]),
         'array://localhost/v_0d': np.array('\x01', dtype='|V1'),
         'array://localhost/non_square': np.arange(5 * 10).reshape((5, 10)),
         }
@@ -190,6 +195,73 @@ class Array(compass_model.Array):
         return True
 
 
+class ArrayText(compass_model.Text):
+    """ Represents a text array (both ASCII and UNICODE). """
+
+    class_kind = "TestArray [text]"
+
+    @staticmethod
+    def can_handle(store, key):
+        if key not in DATA:
+            return False
+
+        if DATA[key].dtype.kind == 'S':
+            # log.debug("ASCII String (characters: %d)" % DATA[key].dtype.itemsize)
+            return True
+        if DATA[key].dtype.kind == 'U':
+            # log.debug("Unicode String (characters: %d)" % DATA[key].dtype.itemsize)
+            return True
+        return False
+
+    def __init__(self, store, key):
+        self._store = store
+        self._key = key
+        self.data = DATA[key]
+
+    @property
+    def key(self):
+        return self._key
+
+    @property
+    def store(self):
+        return self._store
+
+    @property
+    def display_name(self):
+        return self.key.rsplit('/', 1)[-1]
+
+    @property
+    def description(self):
+        return 'Text "%s"' % (self.display_name,)
+
+    @property
+    def shape(self):
+        return self.data.shape
+
+    @property
+    def text(self):
+        txt = str()
+
+        if len(self.shape) == 0:
+            print(self.data)
+            txt += str(self.data)
+
+        elif len(self.shape) == 1:
+            for el in self.data:
+                txt += el + ",\n"
+
+        elif len(self.shape) == 2:
+            for i in range(self.shape[0]):
+                for j in range(self.shape[1]):
+                    txt += self.data[i, j] + ", "
+                txt += "\n"
+
+        else:
+            txt = ">> display of more than 2D string array not implemented <<"
+
+        return txt
+
+
 class ArrayKV(compass_model.KeyValue):
     class_kind = "Array Key/Value Attrs"
 
@@ -229,5 +301,6 @@ class ArrayKV(compass_model.KeyValue):
 ArrayStore.push(ArrayKV)
 ArrayStore.push(ArrayContainer)
 ArrayStore.push(Array)
+ArrayStore.push(ArrayText)
 
-compass_model.push(ArrayStore)
\ No newline at end of file
+compass_model.push(ArrayStore)
diff --git a/hdf_compass/hdf5_model/model.py b/hdf_compass/hdf5_model/model.py
index 0e5e5c6..6368265 100644
--- a/hdf_compass/hdf5_model/model.py
+++ b/hdf_compass/hdf5_model/model.py
@@ -100,9 +100,7 @@ class HDF5Store(compass_model.Store):
 
 
 class HDF5Group(compass_model.Container):
-    """
-    Represents an HDF5 group, to be displayed in the browser view.
-    """
+    """ Represents an HDF5 group, to be displayed in the browser view. """
 
     class_kind = "HDF5 Group"
 
@@ -166,9 +164,7 @@ class HDF5Group(compass_model.Container):
 
 
 class HDF5Dataset(compass_model.Array):
-    """
-    Represents an HDF5 dataset.
-    """
+    """ Represents an HDF5 dataset. """
 
     class_kind = "HDF5 Dataset"
 
@@ -218,10 +214,74 @@ class HDF5Dataset(compass_model.Array):
         return True
 
 
+class HDF5Text(compass_model.Text):
+    """ Represents a text array (both ASCII and UNICODE). """
+
+    class_kind = "HDF5 Dataset[text]"
+
+    @staticmethod
+    def can_handle(store, key):
+        if key in store and isinstance(store.f[key], h5py.Dataset):
+            if store.f[key].dtype.kind == 'S':
+                # log.debug("ASCII String (characters: %d)" % DATA[key].dtype.itemsize)
+                return True
+            if store.f[key].dtype.kind == 'U':
+                # log.debug("Unicode String (characters: %d)" % DATA[key].dtype.itemsize)
+                return True
+
+        return False
+
+    def __init__(self, store, key):
+        self._store = store
+        self._key = key
+        self.data = 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 'Text "%s"' % (self.display_name,)
+
+    @property
+    def shape(self):
+        return self.data.shape
+
+    @property
+    def text(self):
+        txt = str()
+
+        if len(self.shape) == 0:
+            # print(type(self.data))
+            txt += str(self.data[()])
+
+        elif len(self.shape) == 1:
+            for el in self.data:
+                txt += el + ", \n"
+
+        elif len(self.shape) == 2:
+            for i in range(self.shape[0]):
+                for j in range(self.shape[1]):
+                    txt += self.data[i, j] + ", "
+                txt += "\n"
+
+        else:
+            txt = ">> display of more than 2D string array not implemented <<"
+
+        return txt
+
+
 class HDF5KV(compass_model.KeyValue):
-    """
-    A KeyValue node used for HDF5 attributes.
-    """
+    """ A KeyValue node used for HDF5 attributes. """
 
     class_kind = "HDF5 Attributes"
 
@@ -322,6 +382,7 @@ class HDF5Image(compass_model.Image):
 # Register handlers    
 HDF5Store.push(HDF5KV)
 HDF5Store.push(HDF5Dataset)
+HDF5Store.push(HDF5Text)
 HDF5Store.push(HDF5Group)
 HDF5Store.push(HDF5Image)
 
diff --git a/hdf_compass/opendap_model/model.py b/hdf_compass/opendap_model/model.py
index e169237..2a99740 100644
--- a/hdf_compass/opendap_model/model.py
+++ b/hdf_compass/opendap_model/model.py
@@ -33,9 +33,7 @@ def check_key(key, dataset):
 
 
 class Server(compass_model.Store):
-    """
-        Represents the remote OpENDAP server to be accessed
-    """
+    """ Represents the remote OpENDAP server to be accessed """
 
     def __contains__(self, key):
         if '/' not in key:
@@ -97,9 +95,7 @@ class Server(compass_model.Store):
 
 
 class Dataset(compass_model.Container):
-    """
-        Represents Dataset/DatasetType Object in OpENDAP/Pydap.
-    """
+    """ Represents Dataset/DatasetType Object in OpENDAP/Pydap. """
 
     class_kind = "Dataset"
 
@@ -147,9 +143,7 @@ class Dataset(compass_model.Container):
 
 
 class Structure(compass_model.Container):
-    """
-        Represents Structure/StructureType Object in OpENDAP/Pydap.
-    """
+    """ Represents Structure/StructureType Object in OpENDAP/Pydap. """
 
     class_kind = "Structure/Grid/Sequence"
 
@@ -197,9 +191,7 @@ class Structure(compass_model.Container):
 
 
 class Base(compass_model.Array):
-    """
-        Represents Array/BaseType Object in OpENDAP/Pydap.
-    """
+    """ Represents Array/BaseType Object in OpENDAP/Pydap. """
 
     class_kind = "Array"
 
@@ -262,9 +254,7 @@ class Base(compass_model.Array):
 
 
 class Attributes(compass_model.KeyValue):
-    """
-        Represents the Attributes member of Pydap Objects.
-    """
+    """ Represents the Attributes member of Pydap Objects. """
 
     class_kind = "Attributes"
 

-- 
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