climate-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From good...@apache.org
Subject svn commit: r1507011 - in /incubator/climate/trunk/rcmet/src/main/python: rcmes/toolkit/metrics.py tests/test_metrics.py
Date Thu, 25 Jul 2013 15:21:30 GMT
Author: goodale
Date: Thu Jul 25 15:21:30 2013
New Revision: 1507011

URL: http://svn.apache.org/r1507011
Log:
CLIMATE-110: Pull the raw_inputs out of calcPdf()

Added:
    incubator/climate/trunk/rcmet/src/main/python/tests/test_metrics.py
Modified:
    incubator/climate/trunk/rcmet/src/main/python/rcmes/toolkit/metrics.py

Modified: incubator/climate/trunk/rcmet/src/main/python/rcmes/toolkit/metrics.py
URL: http://svn.apache.org/viewvc/incubator/climate/trunk/rcmet/src/main/python/rcmes/toolkit/metrics.py?rev=1507011&r1=1507010&r2=1507011&view=diff
==============================================================================
--- incubator/climate/trunk/rcmet/src/main/python/rcmes/toolkit/metrics.py (original)
+++ incubator/climate/trunk/rcmet/src/main/python/rcmes/toolkit/metrics.py Thu Jul 25 15:21:30
2013
@@ -424,8 +424,16 @@ def calcNashSutcliff(evaluationData, ref
                (((data2 - meanData2) ** 2).sum(axis = 1)))
     return nashcor
 
+def getPdfInputValues():
+    print '****PDF input values from user required **** \n'
+    nbins = int (raw_input('Please enter the number of bins to use. \n'))
+    minEdge = float(raw_input('Please enter the minimum value to use for the edge. \n'))
+    maxEdge = float(raw_input('Please enter the maximum value to use for the edge. \n'))
+    
+    return nbins, minEdge, maxEdge
+
 
-def calcPdf(evaluationData, referenceData):
+def calcPdf(evaluationData, referenceData, settings=None):
     '''
     Routine to calculate a normalized Probability Distribution Function with 
     bins set according to data range.
@@ -435,16 +443,22 @@ def calcPdf(evaluationData, referenceDat
         called in do_rcmes_processing_sub.py
          
     Inputs::
-        2 arrays of data
-        t1 is the modelData and t2 is 3D obsdata - time,lat, lon NB, time here 
-        is the number of time values eg for time period 199001010000 - 199201010000 
+        evaluationData (3D numpy array): array shape (time, lat, lon)
+        referenceData (3D numpy array): array shape (time, lat, lon)
+        settings (tuple): [optional] format (binCount, minEdge, maxEdge)
+            binCount (int): number of bins to use
+            minEdge (int|float): minimum edge
+            maxEdge (int|float): maximum edge
         
-        if annual means-opt 1, was chosen, then t2.shape = (2,lat,lon)
+        NB, time here is the number of time values eg for time period
+         199001010000 - 199201010000 
         
-        if monthly means - opt 2, was choosen, then t2.shape = (24,lat,lon)
+    Assumptions::
+        If annual means-opt 1, was chosen, then referenceData.shape = (YearsCount,lat,lon)
         
-    User inputs: number of bins to use and edges (min and max)
-    Output:
+        If monthly means - opt 2, was choosen, then referenceData.shape = (MonthsCount,lat,lon)
+        
+    Output::
 
         one float which represents the PDF for the year
 
@@ -459,20 +473,17 @@ def calcPdf(evaluationData, referenceDat
         PDF for the year
 
     '''
-    # float to store the final PDF similarity score
     similarityScore = 0.0
-
     print 'min modelData', evaluationData[:, :, :].min()
     print 'max modelData', evaluationData[:, :, :].max()
     print 'min obsData', referenceData[:, :, :].min()
     print 'max obsData', referenceData[:, :, :].max()
-    # find a distribution for the entire dataset
-    #prompt the user to enter the min, max and number of bin values. 
-    # The max, min info above is to help guide the user with these choises
-    print '****PDF input values from user required **** \n'
-    nbins = int (raw_input('Please enter the number of bins to use. \n'))
-    minEdge = float(raw_input('Please enter the minimum value to use for the edge. \n'))
-    maxEdge = float(raw_input('Please enter the maximum value to use for the edge. \n'))
+
+    if settings == None:
+        nbins, minEdge, maxEdge = getPdfInputValues()
+    else:
+        nbins, minEdge, maxEdge = settings
+
     
     mybins = np.linspace(minEdge, maxEdge, nbins)
     print 'nbins is', nbins, 'mybins are', mybins

Added: incubator/climate/trunk/rcmet/src/main/python/tests/test_metrics.py
URL: http://svn.apache.org/viewvc/incubator/climate/trunk/rcmet/src/main/python/tests/test_metrics.py?rev=1507011&view=auto
==============================================================================
--- incubator/climate/trunk/rcmet/src/main/python/tests/test_metrics.py (added)
+++ incubator/climate/trunk/rcmet/src/main/python/tests/test_metrics.py Thu Jul 25 15:21:30
2013
@@ -0,0 +1,52 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+import unittest
+
+import toolkit.metrics
+
+import numpy as np
+import numpy.ma as ma
+
+
+class TestCalcPdf(unittest.TestCase):
+    
+    def testUsingSettingsKwarg(self):
+
+        # declare and initialize 1d arrays (mimick timeseries)
+        good_pdf = '0.091'
+      
+        # declare and initialize  3d(time,lat,lon) type array
+        evaluationDataset = np.arange(0, 12, 0.5)
+        evaluationDataset = evaluationDataset.reshape(4, 2, 3)
+        evaluationDataset = ma.array(evaluationDataset)
+    
+        referenceDataset = np.arange(10, 34)
+        referenceDataset = referenceDataset.reshape(4, 2, 3)
+        referenceDataset = ma.array(referenceDataset)
+
+        settings = (3, 10, 20)
+
+        pdf =  '%.3f' % toolkit.metrics.calcPdf(evaluationDataset, referenceDataset, settings)
+        # Checking accuracy to 3 decimal places using a simple string comparison
+        self.assertEqual(pdf, good_pdf)
+        
+
+if __name__ == '__main__':
+    unittest.main()



Mime
View raw message