Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions design_matrix_creation_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import numpy as np
import pandas as pd


def get_frame_at_time(time, stim_table):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple of oddities about this function. One is that it selects the output column by integer index rather than name, which will fail if you run into a stim table with differently-ordered or inserted columns. The other is that you are looking at start times and not end times, so if you supply a time after the bounds of the experiment, this function will happily report the last value from the dataframe (rather than throwing an error or otherwise notifying the caller).

Maybe something like this (sans error handling):

def get_frame_at_time(time, stim_table):
    return stim_table.loc[(stim_table['start'] <= time) & (stim_table['end'] > time), 'frame'].values[0]

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)
25 changes: 25 additions & 0 deletions downsampling_module.py
Original file line number Diff line number Diff line change
@@ -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)