def test_find_nearest(self): # test that find_nearest works for positive and negative values array = np.linspace(-10, 10, 21) neg_val = -5 pos_val = 5 idx_neg = ORC.find_nearest(array, neg_val) idx_pos = ORC.find_nearest(array, pos_val) self.assertEqual(array[idx_neg], neg_val) self.assertEqual(array[idx_pos], pos_val)
def fieldmap_bin(field_map, bin): ''' Bins a given field map given a binning value Parameters ---------- field_map : numpy.ndarray Field map matrix in Hz bin : int Binning value in Hz Returns ------- binned_field_map : numpy.ndarray Binned field map matrix ''' fmax = field_map.max() bins = np.arange(-fmax, fmax + bin, bin) binned_field_map = np.zeros(field_map.shape) for x in range(field_map.shape[0]): for y in range(field_map.shape[1]): idx = ORC.find_nearest(bins, field_map[x, y]) binned_field_map[x, y] = bins[idx] return binned_field_map