-
Notifications
You must be signed in to change notification settings - Fork 150
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
Ticket/PSB-207: #2726
Ticket/PSB-207: #2726
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import pathlib | ||
from typing import Union | ||
|
||
import numpy as np | ||
from allensdk.api.cloud_cache.cloud_cache import S3CloudCache | ||
from allensdk.api.cloud_cache.file_attributes import CacheFileAttributes | ||
from allensdk.brain_observatory.behavior.data_objects.stimuli.stimulus_templates import ( # noqa: E501 | ||
StimulusMovieTemplateFactory, | ||
) | ||
|
||
|
||
class NaturalMovieOneCache(S3CloudCache): | ||
def __init__(self, cache_dir: Union[str, pathlib.Path], bucket_name: str): | ||
super().__init__( | ||
cache_dir=cache_dir, | ||
bucket_name=bucket_name, | ||
project_name=None, | ||
ui_class_name=None, | ||
) | ||
|
||
# Set the file attributes by hand. This is used to get around needing | ||
# to run the data release tool and create/download a manifest file. | ||
# The hash has been pre-calculated from the file_hash_from_path | ||
# method in allensdk/api/cloud_cache/utils.py | ||
self._file_attributes = CacheFileAttributes( | ||
url="https://staging.visual-behavior-ophys-data.s3.us-west-2.amazonaws.com/visual-behavior-ophys/resources/Movie_TOE1.npy", # noqa E501 | ||
version_id="0y.DEg5ASDGaWA4Syls5MeC.S5Y6oIIS", | ||
file_hash="7e44cba154b29e1511ab8e5453b7aa5070f1ae456724b5b2541c97c052fbd80aebf159e5f933ab319bda8fdab7b863a096cdb44f129abd20a8c4cc791af4bc41", # noqa E501 | ||
local_path=pathlib.Path(cache_dir) / "Movie_TOE1.npy", | ||
) | ||
|
||
def _list_all_manifests(self) -> list: | ||
""" | ||
Return a list of all of the file names of the manifests associated | ||
with this dataset | ||
""" | ||
return None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this raise There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope. This is a function that gets called in the base class so some value needs to be there. I'll change up the comment. |
||
|
||
def get_file_attributes(self, file_id): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this returns |
||
""" | ||
Retrieve file attributes for a given file_id from the meatadata. | ||
|
||
Parameters | ||
---------- | ||
file_id: str or int | ||
The unique identifier of the file to be accessed (not used in this | ||
overwrite of the method) | ||
|
||
Returns | ||
------- | ||
CacheFileAttributes | ||
""" | ||
return self._file_attributes | ||
|
||
def get_raw_movie(self): | ||
"""Download the raw movie data from the cloud and return it as a numpy | ||
array. | ||
|
||
Returns | ||
------- | ||
raw_movie : np.ndarray | ||
""" | ||
return np.load(self.download_data(None)) | ||
|
||
def get_processed_template_movie(self, n_workers=None): | ||
"""Download the movie if needed and process it into warped and unwarped | ||
frames as presented to the mouse. The DataFrame is indexed with the | ||
same frame index as shown in the stimulus presentation table. | ||
|
||
The processing of the movie requires signicant processing and its | ||
return size is very large so take care in requesting this data. | ||
|
||
Parameters | ||
---------- | ||
n_workers : int | ||
Number of processes to use to transform the movie to what was shown | ||
on the monitor. Default=None (use all cores). | ||
|
||
Returns | ||
------- | ||
processed_movie : pd.DataFrame | ||
""" | ||
movie_data = self.get_raw_movie() | ||
movie_template = StimulusMovieTemplateFactory.from_unprocessed( | ||
movie_name="natural_movie_one", | ||
movie_frames=movie_data, | ||
n_workers=n_workers, | ||
) | ||
return movie_template.to_dataframe( | ||
index_name="movie_frame_index", index_type="int" | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from unittest.mock import patch | ||
|
||
import numpy as np | ||
from allensdk.brain_observatory.behavior.behavior_project_cache.project_apis.data_io.natural_movie_one_cache import ( # noqa: E501 | ||
NaturalMovieOneCache, | ||
) | ||
|
||
|
||
def test_natural_movie_cache(): | ||
""" | ||
Test that the natural movie is loaded and processed correctly | ||
""" | ||
rng = np.random.default_rng(1234) | ||
with patch( | ||
target="allensdk.brain_observatory.behavior." | ||
"behavior_project_cache.project_apis.data_io." | ||
"natural_movie_one_cache.NaturalMovieOneCache." | ||
"get_raw_movie", | ||
return_value=rng.integers( | ||
low=0, high=256, size=(1, 304, 608), dtype=np.uint8 | ||
), | ||
): | ||
cache = NaturalMovieOneCache( | ||
cache_dir="fake_dir", bucket_name="fake_bucket" | ||
) | ||
movie = cache.get_processed_template_movie(n_workers=1) | ||
assert movie.index.name == "movie_frame_index" | ||
assert movie.columns.to_list() == ["unwarped", "warped"] | ||
|
||
unwarped = movie.loc[0, "unwarped"] | ||
warped = movie.loc[0, "warped"] | ||
assert unwarped.shape == (1200, 1920) | ||
assert warped.shape == (1200, 1920) | ||
assert unwarped.dtype == "float64" | ||
assert warped.dtype == "uint8" |
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.
pretty sure it returns
CacheFileAttributes
and notdict
? Can you add return type?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.
Ah, was looking at my natural movie cache and saw what looked like a dict at first glace. Fixed.