{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# ET MCR Analysis subsection selection within stimulation regimen\n", "\n", "#### Author: Youngbin Kim\n", "#### Last Edited: June 2021" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import Packages" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-05-12T17:54:45.171322Z", "start_time": "2021-05-12T17:54:31.971442Z" } }, "outputs": [], "source": [ "# you might need to install libraries if you don't already have them\n", "\n", "import millipillar as mp\n", "import numpy as np\n", "import pandas as pd\n", "import scipy as sp\n", "\n", "import matplotlib.pyplot as plt\n", "import plotly.express as px \n", "import os.path\n", "import glob\n", "import tkinter as tk\n", "from tkinter import filedialog" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import Stimulation Regimen" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-05-12T17:54:45.631173Z", "start_time": "2021-05-12T17:54:45.177320Z" }, "scrolled": false }, "outputs": [], "source": [ "regimen = pd.read_excel('new regimen.xlsx')\n", "regimen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Select folder containing videos to be analyzed" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### For ND2 files" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-05-10T19:25:42.190157Z", "start_time": "2021-05-10T19:25:35.659733Z" } }, "outputs": [], "source": [ "# asks for file path of your video\n", "root = tk.Tk()\n", "root.withdraw()\n", "root.call('wm', 'attributes', '.', '-topmost', True)\n", "folder_path = filedialog.askdirectory()\n", "file_paths = glob.glob(os.path.join(folder_path, \"**/*.nd2\"), recursive=True)\n", "print(len(file_paths), \"files detected\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### For tif files" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-04-29T14:46:36.211484Z", "start_time": "2021-04-29T14:46:31.350743Z" }, "scrolled": true }, "outputs": [], "source": [ "# asks for file path of your video\n", "root = tk.Tk()\n", "root.withdraw()\n", "root.call('wm', 'attributes', '.', '-topmost', True)\n", "folder_path = filedialog.askdirectory()\n", "file_paths = glob.glob(os.path.join(folder_path, \"**/*.tif\"), recursive=True)\n", "print(len(file_paths), \"files detected\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Batch analysis for 1Hz portion of regimen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### for nd2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-05-10T19:25:59.992248Z", "start_time": "2021-05-10T19:25:46.935537Z" }, "scrolled": true }, "outputs": [], "source": [ "#%%timeit\n", "total_summary = pd.DataFrame()\n", "traces = []\n", "for file_path in file_paths:\n", " video = mp.Video(file_path, filetype=\"nd2\")\n", " name = os.path.splitext(file_path.replace(folder_path+\"\\\\\", \"\"))[0]\n", " # modify the ranges below in \"[1801:2200]\" to get the desired subframes \n", " trace = mp.Trace(data=video.trace[1801:2200], sampling_rate=video.frame_rate, name=name) \n", " trace.analyze(baseline_fit=\"exp\")\n", " traces.append(trace)\n", " trace_summary = trace.summary()\n", " trace_summary[\"trace\"] = trace\n", " total_summary = total_summary.append(trace_summary)\n", " print(\"Processed\", len(traces), \"out of\", len(file_paths), \"videos.\")\n", "total_summary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### for tif videos" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from skimage import io\n", "\n", "#%%timeit\n", "total_summary = pd.DataFrame()\n", "traces = []\n", "for file_path in tif_directories:\n", " video = mp.Video(file_path, filetype=\"tif\", frame_rate = 20)\n", " name = os.path.splitext(file_path.replace(folder_path+\"\\\\\", \"\"))[0]\n", " # modify the ranges below in \"[1801:2200]\" to get the desired subframes \n", " trace = mp.Trace(data=video.trace[1801:2200], sampling_rate=video.frame_rate, name=name)\n", " trace.analyze(baseline_fit=\"exp\")\n", " traces.append(trace)\n", " trace_summary = trace.summary()\n", " trace_summary[\"trace\"] = trace\n", " total_summary = total_summary.append(trace_summary)\n", " print(\"Processed\", len(traces), \"out of\", len(file_paths), \"videos.\")\n", "total_summary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Analysis Visualization" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-05-12T17:59:57.347866Z", "start_time": "2021-05-12T17:59:57.221939Z" }, "scrolled": false }, "outputs": [], "source": [ "#looking at a sample peak_summary for one of the traces\n", "df_f0 = pd.DataFrame([t.df_f0 for t in total_summary[\"trace\"]])\n", "df_f0.index = total_summary.index" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-05-12T18:00:06.263774Z", "start_time": "2021-05-12T17:59:58.088444Z" }, "scrolled": true }, "outputs": [], "source": [ "for d in df_f0.index:\n", " print(d)\n", " plt.plot(df_f0.loc[d])\n", " plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Save output\n", "### saves in folder selected in the beginning" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-05-12T18:01:27.753830Z", "start_time": "2021-05-12T18:01:27.286816Z" } }, "outputs": [], "source": [ "total_summary.to_csv(os.path.join(folder_path, \"1Hz summary.csv\"))\n", "df_f0.to_csv(os.path.join(folder_path, \"1Hz df_f0 trace.csv\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Save individual peak data for each video\n", "#### more comprehensive data about each peak is saved in file location" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T23:13:16.784042Z", "start_time": "2020-12-04T23:13:16.117435Z" }, "scrolled": false }, "outputs": [], "source": [ "def save_row_peak_summary(row):\n", " file_name = os.path.join(folder_path,\"analysis\", row.name+\"_batch_1Hz_stim.csv\")\n", " if not os.path.exists(os.path.dirname(file_name)):\n", " os.makedirs(os.path.dirname(file_name))\n", " row.peak_summary().to_csv(file_name)\n", "\n", "total_summary['trace'].apply(save_row_peak_summary);" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }