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 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