From 185ef717e4f33e05d50ee2c2e87733ea8393699c Mon Sep 17 00:00:00 2001 From: pauladkisson Date: Fri, 24 May 2024 17:01:38 -0700 Subject: [PATCH 1/2] added read_avi notebook --- .../markowitz_gillis_nature_2023/README.md | 4 +- .../read_avi.ipynb | 154 ++++++++++++++++++ 2 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 000559/dattalab/markowitz_gillis_nature_2023/read_avi.ipynb diff --git a/000559/dattalab/markowitz_gillis_nature_2023/README.md b/000559/dattalab/markowitz_gillis_nature_2023/README.md index 1394d40..8014522 100644 --- a/000559/dattalab/markowitz_gillis_nature_2023/README.md +++ b/000559/dattalab/markowitz_gillis_nature_2023/README.md @@ -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. \ No newline at end of file +`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` \ No newline at end of file diff --git a/000559/dattalab/markowitz_gillis_nature_2023/read_avi.ipynb b/000559/dattalab/markowitz_gillis_nature_2023/read_avi.ipynb new file mode 100644 index 0000000..2fa6416 --- /dev/null +++ b/000559/dattalab/markowitz_gillis_nature_2023/read_avi.ipynb @@ -0,0 +1,154 @@ +{ + "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": 2, + "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": 3, + "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": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "f1d5ce9eca83476187796f140c6c7759", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + " 0%| | 0/10 [00:00 Date: Fri, 24 May 2024 17:05:26 -0700 Subject: [PATCH 2/2] clear outputs --- .../read_avi.ipynb | 23 ++++--------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/000559/dattalab/markowitz_gillis_nature_2023/read_avi.ipynb b/000559/dattalab/markowitz_gillis_nature_2023/read_avi.ipynb index 2fa6416..6573eb4 100644 --- a/000559/dattalab/markowitz_gillis_nature_2023/read_avi.ipynb +++ b/000559/dattalab/markowitz_gillis_nature_2023/read_avi.ipynb @@ -16,7 +16,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -55,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -93,24 +93,9 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "f1d5ce9eca83476187796f140c6c7759", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - " 0%| | 0/10 [00:00