You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@climate.apache.org by go...@apache.org on 2013/08/26 18:30:09 UTC

svn commit: r1517592 - in /incubator/climate/trunk/ocw: dataset.py tests/test_dataset.py

Author: goodale
Date: Mon Aug 26 16:30:08 2013
New Revision: 1517592

URL: http://svn.apache.org/r1517592
Log:
CLIMATE-274 Improve the Exceptions thrown by Dataset class

Now the Dataset class will throw an error with information about
why the error was thrown.  These are usually related to incoming
data being the wrong shape, or multi-dimensional data given in
the wrong order.


Modified:
    incubator/climate/trunk/ocw/dataset.py
    incubator/climate/trunk/ocw/tests/test_dataset.py

Modified: incubator/climate/trunk/ocw/dataset.py
URL: http://svn.apache.org/viewvc/incubator/climate/trunk/ocw/dataset.py?rev=1517592&r1=1517591&r2=1517592&view=diff
==============================================================================
--- incubator/climate/trunk/ocw/dataset.py (original)
+++ incubator/climate/trunk/ocw/dataset.py Mon Aug 26 16:30:08 2013
@@ -49,9 +49,7 @@ class Dataset:
 
         :raises: ValueError
         '''
-        if self._inputs_are_invalid(lats, lons, times, values):
-            err = "Dataset given improperly shaped array during initialization."
-            raise ValueError(err)
+        self._validate_inputs(lats, lons, times, values)
 
         self.lats = lats
         self.lons = lons
@@ -137,21 +135,40 @@ class Dataset:
 
         return time_resolution
 
-    def _inputs_are_invalid(self, lats, lons, times, values):
-        '''Check if Dataset input values are expected shape.
+    def _validate_inputs(self, lats, lons, times, values):
+        """Check that Dataset inputs are valid.
         
-        :returns: True if the values are invalid, False otherwise.
-        '''
-        lats_shape = lats.shape
-        lons_shape = lons.shape
-        times_shape = times.shape
-        values_shape = values.shape
-
-        return (
-            len(lats_shape) != 1 or len(lons_shape) != 1 or 
-            len(times_shape) != 1 or len(values_shape) != 3 or
-            values_shape != (times_shape[0], lats_shape[0], lons_shape[0])
-        )
+        :raises: ValueError
+        """
+        err_msg = None
+        #Setup and Check parameter dimensionality is correct
+        lat_dim = len(lats.shape)
+        lon_dim = len(lons.shape)
+        time_dim = len(times.shape)
+        value_dim = len(values.shape)
+        lat_count = lats.shape[0]
+        lon_count = lons.shape[0]
+        time_count = times.shape[0]
+        
+        if lat_dim != 1:
+            err_msg = "Latitude Array should be 1 dimensional.  %s dimensions found." % lat_dim
+        elif lon_dim != 1:
+            err_msg = "Longitude Array should be 1 dimensional. %s dimensions found." % lon_dim
+        elif time_dim != 1:
+            err_msg = "Time Array should be 1 dimensional.  %s dimensions found." % time_dim
+        elif value_dim != 3:
+            err_msg = "Value Array should be 3 dimensional.  %s dimensions found." % value_dim
+        # Finally check that the Values array conforms to the proper shape
+        elif values.shape != (time_count, lat_count, lon_count):
+            err_msg = """Value Array must be of shape (times, lats, lons).
+Expected shape (%s, %s, %s) but received (%s, %s, %s)""" % (time_count,
+                                                            lat_count,
+                                                            lon_count,
+                                                            values.shape[0],
+                                                            values.shape[1],
+                                                            values.shape[2])
+        if err_msg:
+            raise ValueError(err_msg)
 
 
 class Bounds(object):

Modified: incubator/climate/trunk/ocw/tests/test_dataset.py
URL: http://svn.apache.org/viewvc/incubator/climate/trunk/ocw/tests/test_dataset.py?rev=1517592&r1=1517591&r2=1517592&view=diff
==============================================================================
--- incubator/climate/trunk/ocw/tests/test_dataset.py (original)
+++ incubator/climate/trunk/ocw/tests/test_dataset.py Mon Aug 26 16:30:08 2013
@@ -55,6 +55,7 @@ class TestInvalidDatasetInit(unittest.Te
         self.time = np.array([dt.datetime(2000, x, 1) for x in range(1, 13)])
         flat_array = np.array(range(300))
         self.value = flat_array.reshape(12, 5, 5)
+        self.values_in_wrong_order = flat_array.reshape(5, 5, 12)
 
     def test_bad_lat_shape(self):
         self.lat = np.array([[1, 2], [3, 4]])
@@ -82,6 +83,10 @@ class TestInvalidDatasetInit(unittest.Te
         self.lat = self.lat[:-2]
         with self.assertRaises(ValueError):
             Dataset(self.lat, self.lon, self.time, self.value, 'prec')
+    
+    def test_values_given_in_wrong_order(self):
+#         with self.assertRaises(ValueError):
+            Dataset(self.lat, self.lon, self.time, self.values_in_wrong_order)