{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# ET MCR Analysis with regimen (Manual one by one workflow)\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-04-30T15:45:47.409401Z", "start_time": "2021-04-30T15:45:47.328156Z" } }, "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\n", "import warnings" ] }, { "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": [ "## Analysis" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-04-30T15:54:46.335494Z", "start_time": "2021-04-30T15:54:46.313514Z" } }, "outputs": [], "source": [ "def automated_analysis(file_paths, regimen, filetype, frame_shift = 0):\n", " etmcr = pd.DataFrame(columns = [\"full_trace\", \"trace\", \"ET (V)\", \"MCR (Hz)\"])\n", " for f in file_paths:\n", " name = os.path.splitext(f.replace(folder_path+\"\\\\\", \"\"))[0]\n", " print(\"Processing\", name)\n", " full_trace, traces, table, et, mcr = mp.stimulation_analysis(f, regimen, filetype, frame_shift, baseline_fit=\"linear\")\n", " etmcr.loc[name] = [full_trace, traces, et, mcr]\n", " return etmcr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Automated: assumes synchronized acquisition" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-04-30T17:10:16.665993Z", "start_time": "2021-04-30T16:00:23.132385Z" }, "scrolled": true }, "outputs": [], "source": [ "# change filetype depending on whether it is tif or nd2\n", "\n", "with warnings.catch_warnings():\n", " warnings.simplefilter(\"ignore\")\n", " etmcr = automated_analysis(file_paths, regimen, filetype=\"nd2\", frame_shift = 0)\n", "etmcr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Saves automated ETMCR analysis to csv file" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-04-30T18:50:58.934317Z", "start_time": "2021-04-30T18:50:58.823310Z" }, "scrolled": false }, "outputs": [], "source": [ "etmcr.to_csv(os.path.join(folder_path, \"etmcr.csv\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Manual visualization to ensure analysis was computed properly\n", "#### What looks like calcium alternans could sometimes be spiral waves" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "for tissue in etmcr.index:\n", " print(etmcr.loc[tissue].name)\n", " plt.plot(etmcr.loc[tissue, \"full_trace\"].df_f0)\n", " plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-04-30T17:20:13.836323Z", "start_time": "2021-04-30T17:10:16.698973Z" }, "scrolled": true }, "outputs": [], "source": [ "for tissue in etmcr.index:\n", " for i in range(len(etmcr.loc[tissue, \"trace\"])):\n", " print(etmcr.loc[tissue].name)\n", " print(regimen.iloc[i, 2:4])\n", " print(\"numpeaks: \", len(etmcr.loc[tissue, \"trace\"][i].peaks))\n", " plt.plot(etmcr.loc[tissue, \"trace\"][i].data)\n", " plt.scatter(etmcr.loc[tissue, \"trace\"][i].peaks, etmcr.loc[tissue, \"trace\"][i].data[etmcr.loc[tissue, \"trace\"][i].peaks])\n", " plt.show()" ] } ], "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 }