Skip to content
Open
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
360 changes: 360 additions & 0 deletions notebooks/simple.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,360 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "0400cc7f-2670-4055-a4a4-bf2e48724eb9",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from mesmerize_core import *\n",
"from mesmerize_viz import *\n",
"import numpy as np\n",
"from copy import deepcopy\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "79593a5e-e018-48ea-a0c9-7be1816d619b",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"pd.options.display.max_colwidth = 120"
]
},
{
"cell_type": "markdown",
"id": "007f8e68-71d8-4cea-987a-9c7b945dfdd7",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"# Paths\n",
"\n",
"`set_parent_raw_data_path()` - sets the **top level raw data directory**\n",
"\n",
"For example, if you have your experimental data in the following dir:\n",
"\n",
"`/data/my_name/exp_top_level/....`\n",
"\n",
"Set `/data/my_name` as the \"parent raw data path\". This allows you to move `exp_top_level/...` between computers.\n",
"\n",
"On windows:\n",
"\n",
"`D:/my_name/exp_top_level/...`\n",
"\n",
"Set `D:/my_name` as the parent raw data path, and you can then move `exp_top_level/...` between computers.\n",
"\n",
"**Note: Even on windows just use `/`, you do not have to worry about the issue of `\\\\` and `\\` on windows if you use `pathlib` :D**"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9611e9a9-1ebd-4a46-8667-83706f937806",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# for this demo set this dir as the path to your `caiman_data` dir\n",
"set_parent_raw_data_path(\"/home/kushal/caiman_data/\")"
]
},
{
"cell_type": "markdown",
"id": "ffe64010-ff54-42e0-9781-fe94c00caffa",
"metadata": {},
"source": [
"### Batch path, this is where caiman outputs will be organized\n",
"\n",
"This can be anywhere, it does not need to be under the parent raw data path."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a66c0c1d-47f9-4b68-8840-e035b8a6a160",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"batch_path = get_parent_raw_data_path().joinpath(\"mesmerize-batch5/batch.pickle\")"
]
},
{
"cell_type": "markdown",
"id": "7e70fe53-3207-4f78-acf4-1d5f5a3c8809",
"metadata": {
"tags": []
},
"source": [
"**Note: We recommend using [pathlib](https://docs.python.org/3/library/pathlib.html) instead of manually managing paths as strings. `pathlib` is just a part of the Python standard library, it makes it much easier to deal with paths and saves a lot of time in the long-run! It also makes your paths compatible across operating systems.**"
]
},
{
"cell_type": "markdown",
"id": "e78ae2f3-ccab-4728-a34f-7068d8569cac",
"metadata": {},
"source": [
"### Create a new batch\n",
"\n",
"This creates a new pandas `DataFrame` with the columns that are necessary for mesmerize. In mesmerize we call this the **batch DataFrame**. You can add additional columns relevant to your experiment, but do not modify columns used by mesmerize.\n",
"\n",
"Note that when you create a DataFrame you will need to use `load_batch()` to load it later. You cannot use `create_batch()` to overwrite an existing batch DataFrame"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a555bf21-514f-4e1e-8413-5bfde7714d69",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# create a new batch\n",
"# df = create_batch(batch_path)\n",
"# to load existing batches use `load_batch()`\n",
"df = load_batch(batch_path)"
]
},
{
"cell_type": "markdown",
"id": "e5383552-2dd6-4d93-a7e0-4ff1978da139",
"metadata": {},
"source": [
"### View the dataframe\n",
"\n",
"It is empty with the appropriate columns for mesmerize"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "381f777d-5f52-46fe-96b6-7e058d3aafff",
"metadata": {},
"outputs": [],
"source": [
"df"
]
},
{
"cell_type": "markdown",
"id": "48a744f5-5158-45db-b0ca-5603fe749696",
"metadata": {},
"source": [
"# Motion Correction\n",
"\n",
"### Get raw movie\n",
"\n",
"An input movie must be anywhere within `raw data path` or `batch path`. We will use the Sue 2p example."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb782ed2-6320-49c5-ac33-a7ec50adaabd",
"metadata": {},
"outputs": [],
"source": [
"# We'll use the Sue movie from caiman\n",
"# download it if you don't have it\n",
"from caiman.utils.utils import download_demo\n",
"download_demo()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ff288722-22b2-4b6f-9845-e504800d211d",
"metadata": {},
"outputs": [],
"source": [
"movie_path = get_parent_raw_data_path().joinpath(\"example_movies/Sue_2x_3000_40_-46.tif\")"
]
},
{
"cell_type": "markdown",
"id": "37b436ac-512d-438c-8d5c-1668930e65e6",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Motion correction parameters\n",
"\n",
"Parameters for all algos have the following structure:\n",
"\n",
"```python\n",
"{\"main\": {... params directly passed to caiman}}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e549707-d7d7-4703-986f-4e8795cfb3b6",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# We will start with one version of parameters\n",
"mcorr_params1 =\\\n",
"{\n",
" 'main': # this key is necessary for specifying that these are the \"main\" params for the algorithm\n",
" {\n",
" 'max_shifts': [24, 24],\n",
" 'strides': [48, 48],\n",
" 'overlaps': [24, 24],\n",
" 'max_deviation_rigid': 3,\n",
" 'border_nan': 'copy',\n",
" 'pw_rigid': True,\n",
" 'gSig_filt': None\n",
" },\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "605944e3-5555-4262-8031-42a4bec24c71",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"### Add a \"batch item\", this is the combination of:\n",
"* algorithm to run, `algo`\n",
"* input movie to run the algorithm on, `input_movie_path`\n",
"* parameters for the specified algorithm, `params`\n",
"* a name for you to keep track of things, usually the same as the movie filename, `item_name`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6d9cee81-d6c7-4e2d-b67e-7cba84d6ec27",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# add an item to the batch\n",
"df.caiman.add_item(\n",
" algo='mcorr',\n",
" input_movie_path=movie_path,\n",
" params=mcorr_params1,\n",
" item_name=movie_path.stem, # filename of the movie, but can be anything\n",
")\n",
"\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "53be38ee-b991-48ff-90ad-e91bdae39444",
"metadata": {},
"source": [
"### Run item :D "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "beb610a1-850c-4746-9487-16b5e07d81f0",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# iloc is used to index rows in the dataframe\n",
"df.iloc[0].caiman.run()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ebe6b769-dfdd-4488-96dd-0e686fb59575",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"df = df.caiman.reload_from_disk()\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "202958b1-0fee-45ac-b3da-f3664b57d4d2",
"metadata": {},
"source": [
"# Visualize :D "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "efc19e8d-4af4-4130-9bd0-b9c9f77a1ada",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"mcorr_widget = df.mcorr.viz(data=[\"input\", \"mcorr\"])\n",
"mcorr_widget.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fff4cb5d-a857-445d-8f60-7b36675d20a1",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}