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 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'])