Commits

Chris Mutel  committed a979e8e

Added conversion utilities so one can pass tuples or dictionaries instead of structured arrays

  • Participants
  • Parent commits 83e93cd

Comments (0)

Files changed (3)

File stats_toolkit/distributions/base.py

 from stats_toolkit.errors import InvalidParamsError,\
     ImproperBoundsError, MultipleRowParamsArrayError, \
     UnreasonableBoundsError, MaximumIterationsError
-from numpy import isnan, array, ndarray
+from numpy import isnan, array, ndarray, zeros, NaN
 from functools import wraps
 
 
     default_number_points_in_pdf = 200
     standard_deviations_in_default_range = 2.2
 
+    ### Conversion utilities ###
+    @classmethod
+    def from_tuples(cls, *data):
+        params = cls._params_array(len(data))
+        for index, row in enumerate(data):
+            if len(row) < 5:
+                row = row + (0,) * (5 - len(row))
+            params[index] = row
+        return params
+
+    @classmethod
+    def from_dicts(cls, *dicts):
+        params = cls._params_array(len(dicts))
+        for key in set.union(*(set(x.keys()) for x in dicts)):
+            params[key] = tuple([x[key] for x in dicts])
+        return params
+
+    @classmethod
+    def _params_array(cls, length=1):
+        params = zeros((length,), dtype=[
+            ('amount', 'f4'), ('sigma', 'f4'),
+            ('minimum', 'f4'), ('maximum', 'f4'),
+            ('negative', 'b1')])
+        params['minimum'] = params['maximum'] = params['sigma'] = NaN
+        return params
+
     ### Utility methods ###
     @classmethod
     def validate(cls, params):

File stats_toolkit/tests/__init__.py

 from uncertainty import UncertaintyTestCase, LognormalTestCase, \
     NormalTestCase, UniformTestCase, TriangularTestCase, BernoulliTestCase
 from lhc import LatinHypercubeMCTestCase
-from distributions.beta import BetaTestCase
+from distributions.beta import BetaTestCase
+from conversions import ConversionTestCase

File stats_toolkit/tests/conversions.py

+import unittest
+from numpy import zeros, NaN, allclose, isnan, any
+from stats_toolkit.distributions import UncertaintyBase
+
+
+class ConversionTestCase(unittest.TestCase):
+    def get_right_1d_array(self):
+    	params = UncertaintyBase._params_array()
+    	params['amount'] = 1
+    	params['maximum'] = 2
+    	params['minimum'] = 0
+    	params['sigma'] = 3
+    	return params
+
+    def get_right_2d_array(self):
+    	params = UncertaintyBase._params_array(2)
+    	params['amount'] = (1, 2)
+    	params['sigma'] = (3, 4)
+    	params['maximum'] = 10
+    	params['minimum'] = 0
+    	return params
+
+    def test_1d_dict(self):
+    	values = {"amount": 1.0, 
+    		"sigma": 3.0,
+    		"maximum": 2.0,
+    		"minimum": 0.0,
+    		"negative": False,
+    		}
+    	self.sa_allclose(
+    		UncertaintyBase.from_dicts(values), 
+    		self.get_right_1d_array(),
+    		)
+
+    def test_1d_tuple(self):
+    	values = (1, 3, 0, 2)
+    	self.sa_allclose(
+    		UncertaintyBase.from_tuples(values), 
+    		self.get_right_1d_array(),
+    		)
+
+    def test_2d_dict(self):
+    	values = (
+    		{"amount": 1.0, 
+    		"sigma": 3.0,
+    		"maximum": 10.0,
+    		"minimum": 0.0,
+    		"negative": False}, 
+    		{"amount": 2.0, 
+    		"sigma": 4.0,
+    		"maximum": 10.0,
+    		"minimum": 0.0,
+    		"negative": False}
+    		)
+    	self.sa_allclose(
+    		UncertaintyBase.from_dicts(*values), 
+    		self.get_right_2d_array(),
+    		)
+
+    def test_2d_tuple(self):
+    	values = (
+    		(1, 3, 0, 10),
+    		(2, 4, 0, 10),
+    		)
+    	self.sa_allclose(
+    		UncertaintyBase.from_tuples(*values), 
+    		self.get_right_2d_array(),
+    		)
+
+    def sa_allclose(self, a, b):
+    	"""allclose for structured arrays"""
+    	for name in a.dtype.names:
+    		print name, a[name], b[name]
+    		self.assertTrue(allclose(a[name], b[name]))
+    	return True