Skip to content
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

added read_avi notebook to 000559 #89

Merged
merged 2 commits into from
Aug 15, 2024
Merged
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
4 changes: 3 additions & 1 deletion 000559/dattalab/markowitz_gillis_nature_2023/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ Each notebook provides an example of how to access the critical data and metadat

Note: `reproduce_figure1d.ipynb` and `reproduce_figure_S1.ipynb` use the conda environment defined by the
`environment.yaml` file, but `reproduce_figure_S3.ipynb` uses a different environment defined by the
`environment_S3.yaml` due to dependency conflicts.
`environment_S3.yaml` due to dependency conflicts.

This submission also provides a notebook showcasing how to read the raw depth video (.avi) files: `read_avi.ipynb`
139 changes: 139 additions & 0 deletions 000559/dattalab/markowitz_gillis_nature_2023/read_avi.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Read AVI Files"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook showcases how to read the .avi depth video files in the dataset."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fsspec import filesystem\n",
"import cv2\n",
"import tempfile\n",
"from dandi.dandiapi import DandiAPIClient\n",
"from tqdm.notebook import tqdm\n",
"\n",
"def stream_avifile(DANDISET_ID, file_path, temp_file, num_chunks=None):\n",
" '''Stream .avi file from DANDI archive.\n",
" \n",
" Parameters\n",
" ----------\n",
" DANDISET_ID : str\n",
" Dandiset ID\n",
" file_path : str\n",
" Path to .avi file in DANDI archive\n",
" temp_file : file-like object\n",
" Temporary file to write the .avi file to\n",
" num_chunks : int, optional\n",
" Number of chunks to read from the file. If None, read the whole file.\n",
" '''\n",
" with DandiAPIClient() as client:\n",
" asset = client.get_dandiset(DANDISET_ID, 'draft').get_asset_by_path(file_path)\n",
" s3_url = asset.get_content_url(follow_redirects=1, strip_query=True)\n",
" fs = filesystem(\"http\")\n",
" file_system = fs.open(s3_url, \"rb\")\n",
" file_size = file_system.size\n",
" chunk_size = 10_000_000 # 10 MB\n",
" num_chunks = num_chunks or file_size // chunk_size\n",
" for _ in tqdm(range(num_chunks)):\n",
" chunk = file_system.read(length=chunk_size)\n",
" temp_file.write(chunk)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def show_video(file_path: str):\n",
" '''Show video in a window.\n",
"\n",
" Parameters\n",
" ----------\n",
" file_path : str\n",
" Path to video file\n",
" '''\n",
" raw_video = cv2.VideoCapture(file_path)\n",
"\n",
" while True:\n",
" # Read a frame from the video\n",
" ret, frame = raw_video.read()\n",
"\n",
" # If the frame is not read successfully, the video has ended\n",
" if not ret:\n",
" break\n",
"\n",
" frame = frame / frame.max() # Normalize the frame\n",
"\n",
" # Display the frame in a window\n",
" cv2.imshow('Frame', frame)\n",
"\n",
" # Wait for a key press to exit\n",
" if cv2.waitKey(25) & 0xFF == ord('q'):\n",
" break\n",
"\n",
" # Release the video capture and close the window\n",
" raw_video.release()\n",
" cv2.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"DANDISET_ID = '000559'\n",
"avi_file_path = 'sub-10/sub-10_ses-reinforcement-0e0aca4f-6a99-46eb-9eb8-439f36e66c40_behavior+image+ogen/f9af8a18-7cab-471c-802f-6937edeaec68_external_file_0.avi'\n",
"num_chunks = 10 # Use None to read the whole file\n",
"\n",
"with tempfile.NamedTemporaryFile() as temp:\n",
" stream_avifile(DANDISET_ID, avi_file_path, temp, num_chunks=num_chunks)\n",
" temp.flush()\n",
" show_video(temp.name)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "datta_env",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading