def bicubic_interpolation(x, y, img): ix = int(math.floor(x)) iy = int(math.floor(y)) dx = x - math.floor(x) dy = y - math.floor(y) temp = 0 for m in range(-1, 3): for n in range(-1, 3): if ((x + m >= 0 and x + m < img.shape[1]) and (y + n >= 0 and y + n < img.shape[0])): temp = temp + img[iy + n, ix + m] * R(m - dx) * R(dy - n) point = temp return point
def lagrange_interpolation(x, y, img): dx = x - math.floor(x) dy = y - math.floor(y) L1 = L(1, dx, img, x, y) L2 = L(2, dx, img, x, y) L3 = L(3, dx, img, x, y) L4 = L(4, dx, img, x, y) point = ((-dy * (dy - 1) * (dy - 2) * L1) / 6) + ( ((dy + 1) * (dy - 1) * (dy - 2) * L2) / 2) + ((-dy * (dy + 1) * (dy - 2) * L3) / 2) + ((dy * (dy + 1) * (dy - 1) * L4) / 6) return point
def L(n, dx, img, x, y): l = 0 ix = int(math.floor(x)) iy = int(math.floor(y)) if ((x + 1 >= 0 and x + 1 < img.shape[1]) and (y + n - 2 >= 0 and y + n - 2 < img.shape[0])): l = ((-dx * (dx - 1) * (dx - 2) * img[iy + n - 2, ix + 1]) / 6) if ((x >= 0 and x < img.shape[1]) and (y + n - 2 >= 0 and y + n - 2 < img.shape[0])): l = l + (((dx + 1) * (dx - 1) * (dx - 2) * img[iy + n - 2, ix]) / 2) if ((x + 1 >= 0 and x + 1 < img.shape[1]) and (y + n - 2 >= 0 and y + n - 2 < img.shape[0])): l = l + ((-dx * (dx + 1) * (dx - 2) * img[iy + n - 2, ix + 1]) / 2) if ((x + 2 >= 0 and x + 2 < img.shape[1]) and (y + n - 2 >= 0 and y + n - 2 < img.shape[0])): l = l + ((dx * (dx + 1) * (dx - 1) * img[iy + n - 2, ix + 2]) / 6) return l
def bilinear_interpolation(x, y, img): ix = int(math.floor(x)) iy = int(math.floor(y)) dx = x - math.floor(x) dy = y - math.floor(y) point = 0 if ((x >= 0 and x < img.shape[1]) and (y >= 0 and y < img.shape[0])): point = ((1 - dx) * (1 - dy) * img[iy, ix]) if ((x + 1 >= 0 and x + 1 < img.shape[1]) and (y >= 0 and y < img.shape[0])): point = point + (dx * (1 - dy) * img[iy, ix + 1]) if ((x >= 0 and x < img.shape[1]) and (y + 1 >= 0 and y + 1 < img.shape[0])): point = point + ((1 - dx) * dy * img[iy + 1, ix]) if ((x + 1 >= 0 and x + 1 < img.shape[1]) and (y + 1 >= 0 and y + 1 < img.shape[0])): point = point + (dx * dy * img[iy + 1, ix + 1]) return point
def nn_interpolation(x, y, img): point = 0 dx = x - math.floor(x) dy = y - math.floor(y) ix = int(math.floor(x)) iy = int(math.floor(y)) if (dx < 0.5 and dy < 0.5): point = img[iy, ix] elif ((dx >= 0.5 and dy < 0.5) and ((x + 1 >= 0 and x + 1 < img.shape[1]) and (y >= 0 and y < img.shape[0]))): point = img[iy, ix + 1] elif ((dx < 0.5 and dy >= 0.5) and ((x >= 0 and x < img.shape[1]) and (y + 1 >= 0 and y + 1 < img.shape[0]))): point = img[iy + 1, ix] elif ((dx >= 0.5 and dy >= 0.5) and ((x + 1 >= 0 and x + 1 < img.shape[1]) and (y + 1 >= 0 and y + 1 < img.shape[0]))): point = img[iy + 1, ix + 1] return point
print "no calibration pipeline flags found" cal_flags = False #Loading data data = katdal.open(filename) if cal_flags: data._flags = h5_flags['flags'] #Nice name for pdf h5name = data.name.split('/')[-1] obs_details = h5name + '_AR1_observation_report' pp = PdfPages(obs_details + '.pdf') N = data.shape[0] ext = 930 step = max(int(math.floor(N / ext)), 1) #Loading flags data.select() M = 4 * np.shape(data)[1] / 4096 start_chan = 2200 * M // 4 end_chan = 2800 * M // 4 if data.receivers[data.receivers.keys()[0]][0] == 'u': start_chan = 0 #2200*M//4 end_chan = data.shape[1] #2800*M//4 data.select(channels=slice(start_chan, end_chan)) time = data.timestamps - data.timestamps[0] freqs = data.channel_freqs / 1e6 flags = data.flags[:] #Getting antenna activity
def process_training_data(train_page_names): """ Perform the training stage and return results in a dictionary. Params: train_page_names - list of training page names """ print('Reading data') images_train = [] labels_train = [] images_train_final = [] for page_name in train_page_names: images_train = utils.load_char_images(page_name, images_train) labels_train = utils.load_labels(page_name, labels_train) # for every image, increase contrast and store these new images as # the images to use for training # for image in images_train: # img_contr = increase_contrast_image(image, 150) # images_train_final.append(img_contr) # images_train_final = np.array(images_train_final) labels_train = np.array(labels_train) print('Extracting features from training data') bbox_size = get_bounding_box_size(images_train) fvectors_train_full = images_to_feature_vectors(images_train, bbox_size) # take first half of full training vectors fvectors_train_fhalf = fvectors_train_full[:( math.floor((fvectors_train_full.shape[0]) / 2)), :] # create random 1D array with n features to be used as noise np.random.seed(2) noise = (np.random.rand(2340) * 100).astype(int) # add noise to half of training data images fvectors_train_fhalf = np.subtract(fvectors_train_fhalf, noise) # any pixel below 0 (black) set to 0 for i in range(len(fvectors_train_fhalf) - 1): for j in range(len(fvectors_train_fhalf[0]) - 1): if fvectors_train_fhalf[i][j] < 0: fvectors_train_fhalf[i][j] = 0 # for every noisy training images, apply a median filter to image to reduce # noise and store these new images as the images to use for training # (same filters applied to test images) fvectors_train_fhalf_final = [] for vector in fvectors_train_fhalf: # img_contr = increase_contrast_vector(vector, 150) -- commented out as it reduces accuracy noise_red = ndimage.median_filter(vector, 3) fvectors_train_fhalf_final.append(noise_red) fvectors_train_fhalf_final = np.array(fvectors_train_fhalf_final) # recreate full training vectors by stacking noisy images with second half of original full training vectors fvectors_train_shalf = fvectors_train_full[( math.floor((fvectors_train_full.shape[0]) / 2)):, :] fvectors_train_full = np.vstack( (fvectors_train_fhalf_final, fvectors_train_shalf)) model_data = dict() model_data['train_mean'] = np.mean(fvectors_train_full).tolist() model_data['labels_train'] = labels_train.tolist() model_data['bbox_size'] = bbox_size print('Reducing to 10 dimensions') # use PCA to get 40 eigenvectors of covariance matrix of all training vectors covx = np.cov(fvectors_train_full, rowvar=False) N = covx.shape[0] w, v = linalg.eigh(covx, eigvals=(N - 40, N - 1)) v = np.fliplr(v) model_data['v'] = v.tolist() fvectors_train = reduce_dimensions_train(fvectors_train_full, model_data) model_data['fvectors_train'] = fvectors_train.tolist() return model_data