def context_factory(self): """ Return the type of context we are testing. """ data_context = DataContext() context = AdaptedDataContext(context=data_context) name = self.key_name() getitem_units = {name: meters / second} adapter = UnitConversionAdapter(getitem_units=getitem_units) context.push_adapter(adapter) return context
def context_factory(self): """ Return the type of context we are testing. """ data_context=DataContext() context = AdaptedDataContext(context=data_context) name = self.key_name() getitem_units = {name:meters/second} adapter = UnitConversionAdapter(getitem_units=getitem_units) context.push_adapter(adapter) return context
def setUp(self): unittest.TestCase.setUp(self) # Set up data for the contexts depth = linspace(0.0, 100.0, 11) lith = array(['sand'] * len(depth), dtype=object) # Create the contexts self.context = AdaptedDataContext(context=DataContext()) self.raw_context = self.context.context # Add data (before creating the adapter) self.context.update(depth=depth, lith=lith) # Add an adapter self.mask = (20.0 <= depth) & (depth <= 50.0) self.adapter = MaskingAdapter(mask=self.mask) self.context.push_adapter(self.adapter)
def setUp(self): unittest.TestCase.setUp(self) # Put unit adapters on either side of a masking adapter to see if they # cooperate. Store meters in the raw context, push fathoms through the # mask, and expose feet to the outside world. self.units = units = { 'in':meters, 'mid':fathom, 'out':feet } # Set up data for the contexts depth = Log(linspace(0.0, 100.0, 11), units=units['in']) lith = array(['sand']*len(depth), dtype=object) # Create the contexts self.context = AdaptedDataContext(context=DataContext()) self.raw_context = self.context.context # Add data (before creating the adapters) self.context.update(depth=depth, lith=lith) # (This simplifies creating UnitConversionAdapters) def always(value): class C(dict): def get(self, key, default=None): return value def __repr__(self): return '{*:%r}' % value return C() # Layer multiple adapters self.mask = (15.0 < depth) & (depth < 55.0) self.convert_out = lambda x: convert(x, units['in'], units['out']) self.convert_in = lambda x: convert(x, units['out'], units['in']) #self.context.push_adapter( # UnitConversionAdapter(setitem_units=always(units['in']), # getitem_units=always(units['mid']))) self.context.push_adapter(MaskingAdapter(mask=self.mask)) self.context.push_adapter( # UnitConversionAdapter(setitem_units=always(units['mid']), UnitConversionAdapter(setitem_units=always(units['in']), getitem_units=always(units['out'])))
def setUp(self): unittest.TestCase.setUp(self) # Set up data for the contexts depth = linspace(0.0, 100.0, 11) lith = array(["sand"] * len(depth), dtype=object) # Create the contexts self.context = AdaptedDataContext(context=DataContext()) self.raw_context = self.context.context # Add data (before creating the adapter) self.context.update(depth=depth, lith=lith) # Add an adapter self.mask = (20.0 <= depth) & (depth <= 50.0) self.adapter = MaskingAdapter(mask=self.mask) self.context.push_adapter(self.adapter)
class AdaptedDataContextTestCase(unittest.TestCase): ########################################################################### # TestCase interface ########################################################################### def setUp(self): unittest.TestCase.setUp(self) # Put unit adapters on either side of a masking adapter to see if they # cooperate. Store meters in the raw context, push fathoms through the # mask, and expose feet to the outside world. self.units = units = { 'in':meters, 'mid':fathom, 'out':feet } # Set up data for the contexts depth = Log(linspace(0.0, 100.0, 11), units=units['in']) lith = array(['sand']*len(depth), dtype=object) # Create the contexts self.context = AdaptedDataContext(context=DataContext()) self.raw_context = self.context.context # Add data (before creating the adapters) self.context.update(depth=depth, lith=lith) # (This simplifies creating UnitConversionAdapters) def always(value): class C(dict): def get(self, key, default=None): return value def __repr__(self): return '{*:%r}' % value return C() # Layer multiple adapters self.mask = (15.0 < depth) & (depth < 55.0) self.convert_out = lambda x: convert(x, units['in'], units['out']) self.convert_in = lambda x: convert(x, units['out'], units['in']) #self.context.push_adapter( # UnitConversionAdapter(setitem_units=always(units['in']), # getitem_units=always(units['mid']))) self.context.push_adapter(MaskingAdapter(mask=self.mask)) self.context.push_adapter( # UnitConversionAdapter(setitem_units=always(units['mid']), UnitConversionAdapter(setitem_units=always(units['in']), getitem_units=always(units['out']))) # TODO Nest masking adapters (broken) # TODO Allow unit adapters on either side of masking adapters (broken) ########################################################################### # AdaptedDataContextTestCase interface ########################################################################### def test_getitem(self): """ Is getitem adapted correctly? """ value = self.context['depth'] desired = self.convert_out(array((20., 30., 40., 50.))) self.assertEqual(len(value), 4) self.assertTrue(allclose(value, desired)) def test_setitem_existing_value(self): """ Is setitem adapted correctly for existing values? """ new_values = self.convert_out(array((30.0, 40.0, 50.0, 60.0))) desired = self.raw_context['depth'].copy() desired[self.mask] = self.convert_in(new_values) self.context['depth'] = Log(new_values, units=self.units['out']) value = self.raw_context['depth'] self.assertEqual(len(value), len(desired)) self.assertTrue(allclose(value, desired)) def test_setitem_non_existing_value(self): """ Is setitem adapted correctly for non-existing values? """ new_values = self.convert_out(array((30.0, 40.0, 50.0, 60.0))) dummy = 12345.67 desired = self.raw_context['depth'].copy() desired[self.mask] = self.convert_in(new_values) desired[~self.mask] = dummy self.context['foo'] = Log(new_values, units=self.units['out']) value = self.raw_context['foo'] value[isnan(value)] = dummy self.assertEqual(len(value), len(desired)) self.assertTrue(allclose(value, desired))
def setUp(self): self.context = AdaptedDataContext(context=DataContext())
class UnitConversionContextAdapterTestCase(unittest.TestCase): """ Other tests for UnitConversionContextAdapater """ ############################################################################ # TestCase interface ############################################################################ def setUp(self): self.context = AdaptedDataContext(context=DataContext()) ############################################################################ # UnitConversionContextAdapterTestCase interface ############################################################################ def test_getitem_converts_correctly(self): """ Does getitem convert units correctly? """ getitem_units = {"depth": feet} adapter = UnitConversionAdapter(getitem_units=getitem_units) old_log = Log((1, 2, 3), units=meters) self.context["depth"] = old_log self.context.push_adapter(adapter) new_log = self.context["depth"] # Did the values get converted correctly? self.assert_(all(new_log == old_log.as_units(feet))) # Are the units assigned correctly? self.assert_(new_log.units == feet) return def test_setitem_converts_correctly(self): """ Does setitem convert units correctly? """ old_log = Log((1, 2, 3), units=meters) getitem_units = {"depth": feet} adapter = UnitConversionAdapter(getitem_units=getitem_units) self.context.push_adapter(adapter) # pass the log into the conversion adapter as meters self.context["depth"] = old_log # Now retreive the log from the underlying context. new_log = self.context["depth"] # Did the values get converted correctly? self.assert_(all(new_log == old_log.as_units(feet))) # Are the units assigned correctly? self.assert_(new_log.units == feet) return # TODO Should this override DataContextTestCase.test_eval_is_not_slow? # TODO Re-enable when we start watching efficiency def HIDE_test_exec_is_not_slow(self): """ Compare exec with Adapter to the speed of a dict. (slowdown < 2.0) This test compares the costs of unit converting 1000 data points using pure python and then using our adapater code. A factor of 2.0 is pretty lousy I'd say, so we don't want to do this much. At the edge of function boundaries is OK. """ ### Parameters ######################################################## # Slowdown we will allow compared to standard python evaluation allowed_slowdown = 2.0 # Number of timer iterations. N = 1 ### Standard execution ################################################ setup = ( "from numpy import arange\n" "from enthought.units.length import meters, feet\n" "from enthought import units\n" "depth_meters = arange(1000)\n" ) code = ( "depth_feet = units.convert(depth_meters, meters, feet)\n" "depth2_feet = depth_feet + depth_feet\n" "depth2_meters = units.convert(depth2_feet, feet, meters)\n" ) std = timeit.Timer(code, setup) std_res = std.timeit(N) ### Adapter execution ################################################# # Adapter is set up to convert depth meters->feet and # depth2 feet->meters setup = ( "from numpy import arange\n" "from enthought.units.length import meters, feet\n" "from geo.context.api import DataContext, AdaptedDataContext\n" "from geo.log import Log\n" "from geo.context.api import UnitConversionAdapter\n" "data_context = DataContext()\n" "context = AdaptedDataContext(context=data_context)\n" "adapter = UnitConversionAdapter(getitem_units={'depth':feet, 'depth2':meters})\n" "context.push_adapter(adapter)\n" "context['depth'] = Log(arange(1000),units=meters)\n" "compiled = compile('depth2 = depth + depth','foo','exec')\n" ) code = "exec compiled in globals(), context\n" adp = timeit.Timer(code, setup) adp_res = adp.timeit(N) slowdown = adp_res / std_res msg = "actual slowdown, time: %f" % slowdown, adp_res / N print "[actual slowdown=%3.2f] " % slowdown, assert slowdown < allowed_slowdown, msg
class MaskingAdapterTestCase(unittest.TestCase): ########################################################################### # TestCase interface ########################################################################### def setUp(self): unittest.TestCase.setUp(self) # Set up data for the contexts depth = linspace(0.0, 100.0, 11) lith = array(['sand'] * len(depth), dtype=object) # Create the contexts self.context = AdaptedDataContext(context=DataContext()) self.raw_context = self.context.context # Add data (before creating the adapter) self.context.update(depth=depth, lith=lith) # Add an adapter self.mask = (20.0 <= depth) & (depth <= 50.0) self.adapter = MaskingAdapter(mask=self.mask) self.context.push_adapter(self.adapter) def tearDown(self): unittest.TestCase.tearDown(self) ########################################################################### # MaskingAdapterTestCase interface ########################################################################### def test_getitem(self): """ Does adapter mask values correctly? """ name, value = self.adapter.adapt_getitem(self.raw_context, 'depth', self.raw_context['depth']) self.assertEqual(len(value), 4) self.assertTrue(all(value == (20.0, 30.0, 40.0, 50.0))) def test_setitem_existing_value(self): """ Does setitem on existing data only change the masked values? """ new_values = (30.0, 40.0, 50.0, 60.0) desired = self.raw_context['depth'].copy() desired[self.mask] = new_values name, value = self.adapter.adapt_setitem(self.raw_context, 'depth', new_values) self.assertEqual(len(value), 11) self.assertTrue(all(value == desired)) def test_setitem_non_existing_value(self): """ Does setitem on non-existing data expand to depth's shape? """ new_values = (30.0, 40.0, 50.0, 60.0) desired = self.raw_context['depth'].copy() desired[self.mask] = new_values desired[~self.mask] = nan name, value = self.adapter.adapt_setitem(self.raw_context, 'foo', new_values) self.assertEqual(len(value), len(desired)) self.assertTrue( all((value == desired) | (isnan(value) == isnan(desired)))) def test_context_getitem(self): """ Are the returned values from context masked correctly? """ depth = self.context['depth'] self.assertEqual(len(depth), 4) self.assertTrue(all(depth == (20.0, 30.0, 40.0, 50.0))) def test_context_setitem_existing(self): """ Are the returned values from context masked correctly? """ new_values = (30.0, 40.0, 50.0, 60.0) self.context['depth'] = new_values # Grab the values from the underlying context, skipping the mask. depth = self.raw_context['depth'] desired = self.raw_context['depth'].copy() desired[self.mask] = new_values self.assertTrue(all(depth == desired))
class UnitConversionContextAdapterTestCase(unittest.TestCase): """ Other tests for UnitConversionContextAdapater """ ############################################################################ # TestCase interface ############################################################################ def setUp(self): self.context = AdaptedDataContext(context=DataContext()) ############################################################################ # UnitConversionContextAdapterTestCase interface ############################################################################ def test_getitem_converts_correctly(self): """ Does getitem convert units correctly? """ getitem_units = {'depth':feet} adapter = UnitConversionAdapter(getitem_units=getitem_units) old_log = Log((1,2,3),units=meters) self.context['depth'] = old_log self.context.push_adapter(adapter) new_log = self.context['depth'] # Did the values get converted correctly? self.assert_(all(new_log==old_log.as_units(feet))) # Are the units assigned correctly? self.assert_(new_log.units==feet) return def test_setitem_converts_correctly(self): """ Does setitem convert units correctly? """ old_log = Log((1,2,3),units=meters) getitem_units = {'depth':feet} adapter = UnitConversionAdapter(getitem_units=getitem_units) self.context.push_adapter(adapter) # pass the log into the conversion adapter as meters self.context['depth'] = old_log # Now retreive the log from the underlying context. new_log = self.context['depth'] # Did the values get converted correctly? self.assert_(all(new_log==old_log.as_units(feet))) # Are the units assigned correctly? self.assert_(new_log.units==feet) return # TODO Should this override DataContextTestCase.test_eval_is_not_slow? # TODO Re-enable when we start watching efficiency def HIDE_test_exec_is_not_slow(self): """ Compare exec with Adapter to the speed of a dict. (slowdown < 2.0) This test compares the costs of unit converting 1000 data points using pure python and then using our adapater code. A factor of 2.0 is pretty lousy I'd say, so we don't want to do this much. At the edge of function boundaries is OK. """ ### Parameters ######################################################## # Slowdown we will allow compared to standard python evaluation allowed_slowdown = 2.0 # Number of timer iterations. N = 1 ### Standard execution ################################################ setup = "from numpy import arange\n" \ "from scimath.units.length import meters, feet\n" \ "from scimath import units\n" \ "depth_meters = arange(1000)\n" code = "depth_feet = units.convert(depth_meters, meters, feet)\n" \ "depth2_feet = depth_feet + depth_feet\n" \ "depth2_meters = units.convert(depth2_feet, feet, meters)\n" std = timeit.Timer(code, setup) std_res = std.timeit(N) ### Adapter execution ################################################# # Adapter is set up to convert depth meters->feet and # depth2 feet->meters setup = "from numpy import arange\n" \ "from scimath.units.length import meters, feet\n" \ "from geo.context.api import DataContext, AdaptedDataContext\n" \ "from geo.log import Log\n" \ "from geo.context.api import UnitConversionAdapter\n" \ "data_context = DataContext()\n" \ "context = AdaptedDataContext(context=data_context)\n" \ "adapter = UnitConversionAdapter(getitem_units={'depth':feet, 'depth2':meters})\n" \ "context.push_adapter(adapter)\n" \ "context['depth'] = Log(arange(1000),units=meters)\n" \ "compiled = compile('depth2 = depth + depth','foo','exec')\n" code = "exec compiled in globals(), context\n" adp = timeit.Timer(code, setup) adp_res = adp.timeit(N) slowdown = adp_res/std_res msg = 'actual slowdown, time: %f' % slowdown, adp_res/N print "[actual slowdown=%3.2f] " % slowdown, assert slowdown < allowed_slowdown, msg
class MaskingAdapterTestCase(unittest.TestCase): ########################################################################### # TestCase interface ########################################################################### def setUp(self): unittest.TestCase.setUp(self) # Set up data for the contexts depth = linspace(0.0, 100.0, 11) lith = array(["sand"] * len(depth), dtype=object) # Create the contexts self.context = AdaptedDataContext(context=DataContext()) self.raw_context = self.context.context # Add data (before creating the adapter) self.context.update(depth=depth, lith=lith) # Add an adapter self.mask = (20.0 <= depth) & (depth <= 50.0) self.adapter = MaskingAdapter(mask=self.mask) self.context.push_adapter(self.adapter) def tearDown(self): unittest.TestCase.tearDown(self) ########################################################################### # MaskingAdapterTestCase interface ########################################################################### def test_getitem(self): """ Does adapter mask values correctly? """ name, value = self.adapter.adapt_getitem(self.raw_context, "depth", self.raw_context["depth"]) self.assertEqual(len(value), 4) self.assertTrue(all(value == (20.0, 30.0, 40.0, 50.0))) def test_setitem_existing_value(self): """ Does setitem on existing data only change the masked values? """ new_values = (30.0, 40.0, 50.0, 60.0) desired = self.raw_context["depth"].copy() desired[self.mask] = new_values name, value = self.adapter.adapt_setitem(self.raw_context, "depth", new_values) self.assertEqual(len(value), 11) self.assertTrue(all(value == desired)) def test_setitem_non_existing_value(self): """ Does setitem on non-existing data expand to depth's shape? """ new_values = (30.0, 40.0, 50.0, 60.0) desired = self.raw_context["depth"].copy() desired[self.mask] = new_values desired[~self.mask] = nan name, value = self.adapter.adapt_setitem(self.raw_context, "foo", new_values) self.assertEqual(len(value), len(desired)) self.assertTrue(all((value == desired) | (isnan(value) == isnan(desired)))) def test_context_getitem(self): """ Are the returned values from context masked correctly? """ depth = self.context["depth"] self.assertEqual(len(depth), 4) self.assertTrue(all(depth == (20.0, 30.0, 40.0, 50.0))) def test_context_setitem_existing(self): """ Are the returned values from context masked correctly? """ new_values = (30.0, 40.0, 50.0, 60.0) self.context["depth"] = new_values # Grab the values from the underlying context, skipping the mask. depth = self.raw_context["depth"] desired = self.raw_context["depth"].copy() desired[self.mask] = new_values self.assertTrue(all(depth == desired))