{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# milliPillar calcium tissue batch analysis\n", "#### Author: Youngbin Kim\n", "#### Last updated: 06/23/21" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import packages" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-05-10T19:15:08.410199Z", "start_time": "2021-05-10T19:15:01.833601Z" } }, "outputs": [], "source": [ "import millipillar as mp\n", "import matplotlib.pyplot as plt\n", "import plotly.express as px\n", "import pandas as pd\n", "import numpy as np\n", "import os.path\n", "import glob\n", "import tkinter as tk\n", "from tkinter import filedialog" ] }, { "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": [ "### Analyze all the videos in the folder and output a summary" ] }, { "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", " trace = mp.Trace(data=video.trace, 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": { "ExecuteTime": { "end_time": "2021-04-29T14:46:58.769891Z", "start_time": "2021-04-29T14:46:56.219619Z" } }, "outputs": [], "source": [ "#%%timeit\n", "total_summary = pd.DataFrame()\n", "traces = []\n", "for file_path in file_paths:\n", " video = mp.Video(file_path, filetype=\"tif\", frame_rate=100)\n", " name = os.path.splitext(file_path.replace(folder_path+\"\\\\\", \"\"))[0]\n", " trace = mp.Trace(data=video.trace, 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": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-04-29T15:12:12.111171Z", "start_time": "2021-04-29T15:12:11.835286Z" } }, "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\n", "total_summary[\"max df_f0\"] = df_f0.max(axis=1)\n", "total_summary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Save these results to csv files" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-05-10T19:22:04.799599Z", "start_time": "2021-05-10T19:22:04.782607Z" } }, "outputs": [], "source": [ "file_name = \"summary.csv\"" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2021-02-16T04:05:48.484548Z", "start_time": "2021-02-16T04:05:48.481619Z" } }, "source": [ "#### save general summary" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-05-10T19:22:05.840437Z", "start_time": "2021-05-10T19:22:05.780472Z" }, "scrolled": false }, "outputs": [], "source": [ "total_summary.to_csv(os.path.join(folder_path, file_name))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### save df/f0" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-05-10T19:22:07.881867Z", "start_time": "2021-05-10T19:22:07.525435Z" } }, "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\n", "df_f0.to_csv(os.path.join(folder_path, \"df_f0 trace.csv\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### save summary for each individual video" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-02-16T04:06:20.303898Z", "start_time": "2021-02-16T04:06:20.299991Z" } }, "outputs": [], "source": [ "total_summary['trace'].apply(lambda row: row.peak_summary().to_csv(os.path.join(folder_path, row.name+\".csv\")));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More detailed information for each video" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-02-16T15:19:17.937266Z", "start_time": "2021-02-16T15:19:17.922617Z" }, "scrolled": false }, "outputs": [], "source": [ "trace = total_summary.iloc[0]['trace']\n", "trace.peak_summary()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-02-16T13:42:52.196331Z", "start_time": "2021-02-16T13:42:52.049843Z" } }, "outputs": [], "source": [ "plt.scatter(trace.peaks, trace.df_f0[trace.peaks], label=\"peaks\")\n", "plt.plot(trace.df_f0, label=\"df_f0\")\n", "plt.legend()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Supplementary Code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### calculating df/f0" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-02-16T07:35:45.222953Z", "start_time": "2021-02-16T07:35:45.074476Z" }, "scrolled": true }, "outputs": [], "source": [ "plt.plot(trace.data, label=\"raw\")\n", "plt.plot(trace.baseline, label='f0')\n", "plt.scatter(trace.peaks, trace.data[trace.peaks], label='peaks')\n", "plt.legend()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### accounting for fluorescent decay" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-02-16T07:31:51.680431Z", "start_time": "2021-02-16T07:31:51.633458Z" }, "scrolled": false }, "outputs": [], "source": [ "plt.plot(trace.df_f0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-02-16T07:33:01.650178Z", "start_time": "2021-02-16T07:33:01.632094Z" } }, "outputs": [], "source": [ "trace.summary().iloc[[0],15:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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 }