-
Notifications
You must be signed in to change notification settings - Fork 40
added functions to downsample images #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jdppthk
wants to merge
2
commits into
AllenInstitute:master
Choose a base branch
from
jdppthk:downsampleTool
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): | ||
| 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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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):