Skip to content

Commit

Permalink
Merge pull request #1 from LorenzLamm/main
Browse files Browse the repository at this point in the history
Add MemBrain-seg functionality
  • Loading branch information
maurerv authored Apr 20, 2024
2 parents aaae8a1 + 7de8251 commit 5cb9deb
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 46 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,17 @@ Make sure to pick the correct environment as kernel to have access to the instal

When loading a new file it is advised to either restart the kernel and start from the top of the notebook. Or at least re-run the file loading cell. This will purge any existing data and avoid potential issues in the experimental stage.


## MemBrain-seg installation
If you would like to use [MemBrain-seg](https://github.com/teamtomo/membrain-seg/) to create initial membrane segmentations, you can install it to your environment via

```
git clone https://github.com/teamtomo/membrain-seg.git
cd membrain-seg
pip install .
```

or refer to MemBrain-seg's [installation instructions](https://github.com/teamtomo/membrain-seg/blob/main/docs/installation.md).

## Detailed User Guide:
Detailed tutorial and user guide can be found [here](https://kosinskilab.github.io/colabseg/).
93 changes: 93 additions & 0 deletions colabseg/membrainseg_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#
# ColabSeg - Interactive segmentation GUI
#
# Marc Siggel, December 2021
# Adapted by Lorenz Lamm for MemBrain-seg, July 2023

import os
from ipywidgets import widgets
from ipywidgets import Layout

from membrain_seg.segmentation.segment import segment as membrain_segment


def generate_membrainseg_gui():
"""Generates MemBrain-seg GUI"""
all_values = {}

def run_membrain_code(obj):
"""This is a wrapper for MemBrain-seg"""
print("MemBrain-seg running on {}".format(all_values["tomo_file"].value))
if os.path.isfile(all_values["tomo_file"].value) is False:
raise ValueError("Tomogram doesn't exist!")

print("Segmenting your tomogram using MemBrain-seg.")
out_file_seg = membrain_segment(
tomogram_path=all_values["tomo_file"].value,
ckpt_path=all_values["membrain_model"].value,
out_folder=os.getcwd(),
store_connected_components=all_values["compute_connected_components"].value,
connected_component_thres=(None if all_values["connected_component_thres"].value == "" else float(all_values["connected_component_thres"].value)),
test_time_augmentation=all_values["test_time_augmentation"].value
)

print("Your file is written to", out_file_seg)


box_layout = Layout(display='flex',
flex_flow='row',
align_items='stretch',
width='800px')

all_values["membrain_model"] = widgets.Text(
placeholder='PATH/TO/MEMBRAIN_MODEL',
description='MemBrain-seg model:',
style = {'description_width': 'initial'},
layout = box_layout,
disabled=False)

all_values["tomo_file"] = widgets.Text(
placeholder='input_tomo.mrc',
description='Input Filename:',
style = {'description_width': 'initial'},
layout = box_layout,
disabled=False)

all_values["compute_connected_components"] = widgets.Checkbox(
value=False,
description='Should connected components be computed to separate membranes?',
style = {'description_width': 'initial'},
layout = widgets.Layout(width='800px'))

all_values["connected_component_thres"] = widgets.Text(
placeholder='0',
description='Connected components smaller than this will be removed.',
style = {'description_width': 'initial'},
layout = box_layout,
disabled=False)

all_values["test_time_augmentation"] = widgets.Checkbox(
value=False,
description='Should 8-fold test-time augmentation be performed (takes longer)?',
style = {'description_width': 'initial'},
layout = widgets.Layout(width='800px'))

hbox_ckpt_path = all_values["membrain_model"]
hbox_tomo_file = all_values["tomo_file"]
conn_comp_pred = all_values["compute_connected_components"]
conn_comp_thres = all_values["connected_component_thres"]
test_time_aug = all_values["test_time_augmentation"]

vbox_all = widgets.VBox([
hbox_ckpt_path,
hbox_tomo_file,
conn_comp_pred,
conn_comp_thres,
test_time_aug
])
#hbox = widgets.HBox([all_values["cpus"], all_values["sspace"]])
display(vbox_all)

start_run = widgets.Button(description="Run MemBrain-seg")
start_run.on_click(run_membrain_code)
display(start_run)
105 changes: 59 additions & 46 deletions colabseg_demo_notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,31 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 1,
"id": "67516ef3",
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"from colabseg.tensorvoting_wrapper import *\n",
"from colabseg.segmentation_gui import *"
"from colabseg.segmentation_gui import *\n",
"from colabseg.membrainseg_wrapper import generate_membrainseg_gui"
]
},
{
"cell_type": "markdown",
"id": "366aa3c7",
"metadata": {},
"source": [
"# 1) Tensor Voting"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "16e58d80",
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"013_sq_df_sorted_binned.mrc 028_sq_df_sorted_binned_global2.mrc\r\n",
"013_sq_df_sorted_binned_global2.mrc 028_sq_df_sorted_binned_global2.mrc.gz\r\n",
"013_sq_df_sorted_binned_sspace.mrc 028_sq_df_sorted_global2.mrc\r\n",
"013_sq_df_sorted_binned_surf1.mrc 031_sq_df_sorted_binned.mrc\r\n",
"013_sq_df_sorted_binned_surf2.mrc 031_sq_df_sorted_binned_global2.mrc\r\n",
"013_sq_df_sorted_binned_thresh.mrc 031_sq_df_sorted_binned_thresh.mrc\r\n",
"013_sq_df_sorted_binned_tv1.mrc PP_04_sorted_binned.mrc\r\n",
"013_sq_df_sorted_binned_tv2.mrc PP_04_sorted_global2.mrc\r\n",
"013_sq_df_sorted_global2.mrc PP_09_sorted_binned.mrc\r\n",
"024_conservative.mrc PP_09_sorted_binned_global2.mrc\r\n",
"024_sq_df_sorted_binned.mrc PP_09_sorted_global2.mrc\r\n",
"024_sq_df_sorted_binned_global2.mrc raw_tomograms_manual.txt\r\n",
"024_sq_df_sorted_binned_surf2.mrc \u001b[1m\u001b[32mrun_tensorvoting_grotjahn.sh\u001b[m\u001b[m*\r\n",
"024_sq_df_sorted_binned_thresh.mrc seg_tomograms_manual.txt\r\n",
"028_sq_df_sorted_binned.mrc\r\n"
]
}
],
"source": [
"ls"
"# 1) Tensor Voting / MemBrain-seg\n",
"\n",
"Run one of the following cells to perform either TomoSegMemTV or MemBrain-seg to segment the membranes in your tomogram:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 3,
"id": "ee4163ad",
"metadata": {
"scrolled": false
Expand All @@ -74,12 +43,12 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d6c6a92fbeca43609a205314d6111f41",
"model_id": "0cc79c755f1348fa8653ece5c45de847",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(Text(value='', description='TV Path:', placeholder='PATH/TO/TOMOSEG', style=DescriptionStyle(de…"
"VBox(children=(Text(value='', description='TV Path:', placeholder='PATH/TO/TOMOSEG', style=TextStyle(descripti…"
]
},
"metadata": {},
Expand All @@ -88,7 +57,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1dc230a024f244cfa6c82a1151631ecc",
"model_id": "10002d462622410d9e05060633c5e138",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -102,7 +71,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "29d6c1b7c3e84fcfa2527b6f26f4cfa3",
"model_id": "a0eb0176898147b88921b069213691e9",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -115,7 +84,46 @@
}
],
"source": [
"generate_tensor_voting_gui()"
"generate_tensor_voting_gui() # run TomoSegMemTV"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a322d0a9",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "9413bb32da3546618ceb9af38209cbdf",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(Text(value='', description='MemBrain-seg model:', layout=Layout(align_items='stretch', display=…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f4ce70670b52475d8ed82bfd3821ed3e",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Button(description='Run MemBrain-seg', style=ButtonStyle())"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"generate_membrainseg_gui() # run MemBrain-seg"
]
},
{
Expand Down Expand Up @@ -443,9 +451,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "seg_test",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "seg_test"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -457,7 +465,12 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
"version": "3.8.0"
},
"vscode": {
"interpreter": {
"hash": "95c7c9545523fc50781ffbf487604435d61e4fb7ad5616715707e2a9e662aeb6"
}
}
},
"nbformat": 4,
Expand Down

0 comments on commit 5cb9deb

Please sign in to comment.