From 2307d37e4e05d430713497d9a0c08af65cd23b36 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Sat, 25 Aug 2018 20:45:44 +0000 Subject: [PATCH 1/2] added functions to downsample images --- downsampling_module.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 downsampling_module.py diff --git a/downsampling_module.py b/downsampling_module.py new file mode 100644 index 0000000..e430e0c --- /dev/null +++ b/downsampling_module.py @@ -0,0 +1,25 @@ +import numpy as np + +def downsample(arr, new_shape): + shape = (new_shape[0], arr.shape[0] // new_shape[0], + new_shape[1], arr.shape[1] // new_shape[1]) + return arr.reshape(shape).mean(-1).mean(1) + +def downsample_images(images, x_frac, y_frac): + """ Takes the 3d array of images from natural scenes and downsaples it by your chosen fractions + + If the fractions do not divide the original image dimensions, this function removes pixels until + the fractions divide the original image dimensions + + """ + (current_image_size_x, current_image_size_y) = np.shape(images[0]) + (new_image_size_x, new_image_size_y) = (current_image_size_x//x_frac, current_image_size_y//y_frac) + rem0 = current_image_size_x % new_image_size_x + rem1 = current_image_size_y % new_image_size_y + new_image_size = (new_image_size_x, new_image_size_y) + downsampled_image_list = [] + + for idx in np.arange(images.shape[0]): + downsampled_image_list.append(downsample(images[idx,0:current_image_size_x-rem0,0:current_image_size_y-rem1], new_image_size)) + + return(downsampled_image_list) \ No newline at end of file From 3eaffda51fe9722bb6df45538348754d530ba35f Mon Sep 17 00:00:00 2001 From: jdppthk Date: Sat, 25 Aug 2018 22:23:50 +0000 Subject: [PATCH 2/2] added a function for creating a stimulus design matrix that contains all the stimuli at different times in a given time window with a given sampling rate --- design_matrix_creation_module.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 design_matrix_creation_module.py diff --git a/design_matrix_creation_module.py b/design_matrix_creation_module.py new file mode 100644 index 0000000..a54d6e6 --- /dev/null +++ b/design_matrix_creation_module.py @@ -0,0 +1,19 @@ +import numpy as np +import pandas as pd + + +def get_frame_at_time(time, stim_table): + starts = stim_table.start.values + idx = np.searchsorted(starts, time)-1 + return(stim_table.iloc[idx].values[2]) + + +def get_sampled_stimulus_time_array(stim_table, sampling_time, flattened_image_list): + ns_time_start = stim_table.iloc[0].values[0] + ns_time_end = stim_table.iloc[stim_table.last_valid_index()][1] + time_array = np.linspace(ns_time_start,ns_time_end,int((ns_time_end-ns_time_start)/sampling_time)) + stim_array = [] + for idx, time_point in enumerate(time_array): + stim_index = get_frame_at_time(time_point, stim_table) + stim_array.append(flattened_image_list[int(stim_index)]) + return(stim_array) \ No newline at end of file