class Mask(object): """ Class that is going to provide the interface between the block and context when used with the 'with' keyword. """ mask = ndarray([]) context = None def __init__(self, object): """ Initialization method, should always contain a boolean array as a mask Parameters: ----------- object: Array([Bool]) """ self.mask = object def __enter__(self): """ Enter method. """ locals_val = sys._getframe().f_back.f_locals if locals_val.has_key('context') and isinstance( locals_val['context'], dict): locals_val = locals_val['context'] # Make a copy of this context. locals_val = adapt(locals_val, ICheckpointable).checkpoint() adapters = [WithMaskAdapter(mask=self.mask)] self.context = AdaptedDataContext(subcontext=locals_val, _adapters=adapters) return def __exit__(self, exc_type, exc_val, exc_tb): """ Exit method. """ locals_val = sys._getframe().f_back.f_locals if locals_val.has_key('context') and isinstance( locals_val['context'], dict): locals_val = locals_val['context'] for k in locals_val.keys(): if k in self.context.keys() and isinstance(self.context[k], ndarray): equal_values = self.context[k] == locals_val[k] if isinstance(equal_values, ndarray): equal_values = equal_values.all() if not equal_values: self.context[k] = locals_val[k] locals_val[k] = self.context[k] self.context.pop_adapter() return
def __enter__(self): """ Enter method. """ locals_val = sys._getframe().f_back.f_locals if 'context' in locals_val and isinstance(locals_val['context'], dict): locals_val = locals_val['context'] # Make a copy of this context. locals_val = adapt(locals_val, ICheckpointable).checkpoint() adapters = [WithMaskAdapter(mask=self.mask)] self.context = AdaptedDataContext(subcontext=locals_val, _adapters=adapters) return
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 = UnitArray(linspace(0.0, 100.0, 11), units=units['in']) lith = array(['sand'] * len(depth), dtype=object) # Create the contexts self.context = AdaptedDataContext(subcontext=DataContext()) self.raw_context = self.context.subcontext # 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 d = depth.view(ndarray) self.mask = (15.0 < d) & (d < 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 __enter__(self): """ Enter method. """ locals_val = sys._getframe().f_back.f_locals if locals_val.has_key('context') and isinstance(locals_val['context'], dict): locals_val = locals_val['context'] # Make a copy of this context. locals_val = adapt(locals_val, ICheckpointable).checkpoint() adapters = [WithMaskAdapter(mask=self.mask)] self.context = AdaptedDataContext(subcontext=locals_val, _adapters=adapters) return
def test_same_size_array(self): """ Same sized array assignments. """ dc = self.context depth = dc['depth'] dc['vp'] = zeros(depth.shape) dc['vs'] = zeros(depth.shape) mask = (depth < 4000.0) & (depth > 1000.0) adc = AdaptedDataContext(subcontext=self.context, _adapters=[WithMaskAdapter(mask=mask)]) adc['vp'] = arange(0., 10., 1.) adc['vs'] = arange(0., 100., 10.) desired_vp = arange(0., 10., 1.0) desired_vs = arange(0., 100., 10.) assert_array_equal(desired_vp, dc['vp']) assert_array_equal(desired_vs, dc['vs'])
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 = UnitArray(linspace(0.0, 100.0, 11), units=units['in']) lith = array(['sand']*len(depth), dtype=object) # Create the contexts self.context = AdaptedDataContext(subcontext=DataContext()) self.raw_context = self.context.subcontext # 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 d = depth.view(ndarray) self.mask = (15.0 < d) & (d < 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 test_floats_to_arrays(self): """ Assigning floats to arrays """ dc = self.context depth = dc['depth'] dc['vp'] = zeros(depth.shape) dc['vs'] = zeros(depth.shape) mask = (depth < 4000.0) & (depth > 1000.0) adc = AdaptedDataContext(subcontext=self.context, _adapters=[WithMaskAdapter(mask=mask)]) adc['vp'] = 1.0 adc['vs'] = 1.5 depth = arange(0., 10000., 1000.) desired_vp = zeros(depth.shape) desired_vs = zeros(depth.shape) desired_vp[(depth < 4000.0) & (depth > 1000.0)] = 1.0 desired_vs[(depth < 4000.0) & (depth > 1000.0)] = 1.5 assert_array_equal(desired_vp, dc['vp']) assert_array_equal(desired_vs, dc['vs'])
class Mask(object): """ Class that is going to provide the interface between the block and context when used with the 'with' keyword. """ mask = ndarray([]) context = None def __init__(self, object): """ Initialization method, should always contain a boolean array as a mask Parameters: ----------- object: Array([Bool]) """ self.mask = object def __enter__(self): """ Enter method. """ locals_val = sys._getframe().f_back.f_locals if locals_val.has_key('context') and isinstance(locals_val['context'], dict): locals_val = locals_val['context'] # Make a copy of this context. locals_val = adapt(locals_val, ICheckpointable).checkpoint() adapters = [WithMaskAdapter(mask=self.mask)] self.context = AdaptedDataContext(subcontext=locals_val, _adapters=adapters) return def __exit__(self, exc_type, exc_val, exc_tb): """ Exit method. """ locals_val = sys._getframe().f_back.f_locals if locals_val.has_key('context') and isinstance(locals_val['context'], dict): locals_val = locals_val['context'] for k in locals_val.keys(): if k in self.context.keys() and isinstance(self.context[k], ndarray): equal_values = self.context[k] == locals_val[k] if isinstance(equal_values, ndarray): equal_values = equal_values.all() if not equal_values: self.context[k] = locals_val[k] locals_val[k] = self.context[k] self.context.pop_adapter() return
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 = UnitArray(linspace(0.0, 100.0, 11), units=units['in']) lith = array(['sand']*len(depth), dtype=object) # Create the contexts self.context = AdaptedDataContext(subcontext=DataContext()) self.raw_context = self.context.subcontext # 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 d = depth.view(ndarray) self.mask = (15.0 < d) & (d < 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'] = UnitArray(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'] = UnitArray(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))