def binned_to_binned(self, key, new_binning): ''' resample a binned key into a different binning Parameters ---------- key : str new_binning : MultiDimBinning the new binning ''' logging.debug('Resampling %s' % (key)) old_binning, hist = self.binned_data[key] sample = [ self.get_binned_data(name, old_binning) for name in old_binning.names ] new_sample = [ SmartArray(self.unroll_binning(name, new_binning)) for name in new_binning.names ] hist = resample(hist, sample, old_binning, new_sample, new_binning) self.add_binned_data(key, (new_binning, hist))
def get_binned_data(self, key, out_binning=None): """Get data array from binned data: if the key is a binning dimensions, then unroll the binning otherwise return the corresponding flattened array """ if out_binning is not None: # check if key is binning dimension if key in out_binning.names: return self.unroll_binning(key, out_binning) binning, data = self.binned_data[key] if out_binning is not None: if not binning == out_binning: logging.warning('Automatically re-beinning data %s'%key) sample = [SmartArray(self.unroll_binning(name, binning)) for name in binning.names] new_sample = [SmartArray(self.unroll_binning(name, out_binning)) for name in out_binning.names] return resample(data, sample, binning, new_sample, out_binning) return data
def resample(self, key, src_representation, dest_representation): """Resample a binned key into a different binning Parameters ---------- key : str src_representation : MultiDimBinning dest_representation : MultiDimBinning """ logging.debug('Resampling %s' % (key)) self.representation = src_representation sample = [self[name] for name in src_representation.names] weights = self[key] self.representation = dest_representation new_sample = [self[name] for name in dest_representation.names] new_hist = resample(weights, sample, src_representation, new_sample, dest_representation) return new_hist