Skip to content

Commit 5ceecfe

Browse files
committed
updating notebook structure
1 parent f47e294 commit 5ceecfe

15 files changed

Lines changed: 1012 additions & 52 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sigma1": 2.0,
3+
"sigma2": 60.0,
4+
"threshold": 100.0
5+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import sys\n",
10+
"\n",
11+
"sys.path.append(\"..\") # Adds higher directory to python modules path.\n",
12+
"import os\n",
13+
"from src import RASPRoutines\n",
14+
"\n",
15+
"RASP = RASPRoutines.RASP_Routines()"
16+
]
17+
},
18+
{
19+
"cell_type": "code",
20+
"execution_count": null,
21+
"metadata": {},
22+
"outputs": [],
23+
"source": [
24+
"\"\"\"This notebook tunes the two new parameters needed for the three-colour\n",
25+
"(oligomer + cell + bulk stain) pipeline, on a single test image, before\n",
26+
"running the full folder analysis in Step3_ImageFolderAnalysis.ipynb.\n",
27+
"\n",
28+
"Radiality (flatness/integratedGrad) is assumed already calibrated via\n",
29+
"Step1_RadialityCalibration.ipynb (also copied into this folder).\n",
30+
"\n",
31+
"Step A below tunes the bulk-stain threshold: RASP.bulk_threshold (and, if\n",
32+
"needed, RASP.bulk_sigma1/RASP.bulk_sigma2) controls how the bulk stain\n",
33+
"channel is thresholded into a binary mask (same difference-of-Gaussians\n",
34+
"detector already used for cell/large-object masks, just a single threshold).\n",
35+
"The plot below shows the raw bulk image with the resulting mask as a\n",
36+
"semi-transparent overlay -- adjust RASP.bulk_threshold and re-run until the\n",
37+
"mask tracks the bulk-stained regions you expect.\n",
38+
"\n",
39+
"These values persist as attributes on RASP, so whatever you set here is\n",
40+
"picked up automatically by RASP.analyse_images(..., bulk_string=...) in\n",
41+
"Step3 -- no need to pass them again.\n",
42+
"\"\"\"\n",
43+
"\n",
44+
"RASP.bulk_threshold = 100.0 # tune this\n",
45+
"# RASP.bulk_sigma1 = 2.0 # rarely needs changing\n",
46+
"# RASP.bulk_sigma2 = 60.0 # rarely needs changing\n",
47+
"\n",
48+
"protein_file = os.path.abspath(\n",
49+
" r\"../example_images/Example_01_ProteinImage_C1.tif\"\n",
50+
") # example file, replace as you wish. Always input as the absolute path of a raw string\n",
51+
"bulk_file = os.path.abspath(\n",
52+
" r\"../example_images/Example_01_CellImage_C0.tif\"\n",
53+
") # placeholder: this repo doesn't ship a real bulk-stain example image, so\n",
54+
"# the cell image stands in here purely to demonstrate the overlay plot --\n",
55+
"# replace with your actual bulk stain channel image.\n",
56+
"\n",
57+
"RASP.single_image_analysis(\n",
58+
" protein_file,\n",
59+
" thres=0.05,\n",
60+
" large_thres=250.0,\n",
61+
" gsigma=1.4,\n",
62+
" rwave=2.0,\n",
63+
" image_size=500,\n",
64+
" save_figure=False,\n",
65+
" cell_file=None,\n",
66+
" bulk_file=bulk_file,\n",
67+
")"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": null,
73+
"metadata": {},
74+
"outputs": [],
75+
"source": [
76+
"\"\"\"Step B: tune the cell-detection parameters, the same way as in the\n",
77+
"two-colour pipeline (example_twocolour_notebooks/Step2_SingleImageAnalysis.ipynb).\n",
78+
"\n",
79+
"RASP.cell_threshold1 / RASP.cell_threshold2 / RASP.cell_sigma1 /\n",
80+
"RASP.cell_sigma2 control the cell mask detector. The plot below shows the\n",
81+
"raw cell image with the resulting mask drawn as a contour border -- adjust\n",
82+
"and re-run until the outline matches the cells you expect.\n",
83+
"\n",
84+
"These also persist as attributes on RASP and are picked up automatically by\n",
85+
"RASP.analyse_images(..., cell_string=..., cell_analysis=True) in Step3.\n",
86+
"\"\"\"\n",
87+
"\n",
88+
"RASP.cell_threshold1 = 200.0 # tune this\n",
89+
"RASP.cell_threshold2 = 200.0 # tune this\n",
90+
"# RASP.cell_sigma1 = 2.0 # rarely needs changing\n",
91+
"# RASP.cell_sigma2 = 40.0 # rarely needs changing\n",
92+
"\n",
93+
"protein_file = os.path.abspath(\n",
94+
" r\"../example_images/Example_01_ProteinImage_C1.tif\"\n",
95+
") # example file, replace as you wish. Always input as the absolute path of a raw string\n",
96+
"cell_file = os.path.abspath(\n",
97+
" r\"../example_images/Example_01_CellImage_C0.tif\"\n",
98+
") # example file, replace as you wish. Always input as the absolute path of a raw string\n",
99+
"\n",
100+
"RASP.single_image_analysis(\n",
101+
" protein_file,\n",
102+
" thres=0.05,\n",
103+
" large_thres=250.0,\n",
104+
" gsigma=1.4,\n",
105+
" rwave=2.0,\n",
106+
" image_size=500,\n",
107+
" save_figure=False,\n",
108+
" cell_file=cell_file,\n",
109+
" bulk_file=None,\n",
110+
")"
111+
]
112+
}
113+
],
114+
"metadata": {
115+
"kernelspec": {
116+
"display_name": "Python 3 (ipykernel)",
117+
"language": "python",
118+
"name": "python3"
119+
},
120+
"language_info": {
121+
"codemirror_mode": {
122+
"name": "ipython",
123+
"version": 3
124+
},
125+
"file_extension": ".py",
126+
"mimetype": "text/x-python",
127+
"name": "python",
128+
"nbconvert_exporter": "python",
129+
"pygments_lexer": "ipython3",
130+
"version": "3.12.7"
131+
}
132+
},
133+
"nbformat": 4,
134+
"nbformat_minor": 5
135+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import sys\n",
10+
"\n",
11+
"sys.path.append(\"..\") # Adds higher directory to python modules path.\n",
12+
"import os\n",
13+
"from src import RASPRoutines\n",
14+
"\n",
15+
"RASP = RASPRoutines.RASP_Routines()"
16+
]
17+
},
18+
{
19+
"cell_type": "code",
20+
"execution_count": null,
21+
"metadata": {},
22+
"outputs": [],
23+
"source": [
24+
"RASP.flatness = 1.\n",
25+
"RASP.integratedGrad = 10.\n",
26+
"RASP.areathres = 42.\n",
27+
"\n",
28+
"# Cell and bulk-stain detection parameters tuned in Step2_SingleImageAnalysis.ipynb\n",
29+
"# persist as attributes on RASP -- set them here if you're running this notebook\n",
30+
"# standalone rather than continuing on from Step2 in the same session.\n",
31+
"# RASP.cell_threshold1, RASP.cell_threshold2 = 200.0, 200.0\n",
32+
"# RASP.bulk_threshold = 100.0"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"\"\"\" the command below will run the analyse_images analysis for the three-colour\n",
42+
"(oligomer + cell + bulk stain) pipeline:\n",
43+
"\n",
44+
" folder (string): Folder containing images\n",
45+
" imtype (string): Type of images being analysed. Default '.tif'\n",
46+
" thres (float): fraction of bright pixels accepted. Default 0.05.\n",
47+
" large_thres (float): large object intensity threshold. Default 100.\n",
48+
" gisgma (float): gaussian blurring parameter. Default 1.4.\n",
49+
" rwave (float): Ricker wavelent sigma. Default 2.\n",
50+
" protein_string (np.1darray): strings for protein-stained data. Default C1.\n",
51+
" cell_string (np.1darray): strings for cell-containing data. Default C0.\n",
52+
" bulk_string (np.1darray): strings for bulk-stain data. If given (not None),\n",
53+
" a bulk stain mask is computed for each image (using RASP.bulk_threshold /\n",
54+
" RASP.bulk_sigma1 / RASP.bulk_sigma2, tuned in Step2) and an\n",
55+
" \"overlap_with_bulk_stain\" boolean column is added to spot_analysis.csv,\n",
56+
" marking which oligomer spots fall inside the bulk stain mask. A\n",
57+
" \"*_bulkMask.tiff\" is also written alongside the existing \"*_cellMask.tiff\".\n",
58+
" if_filter (boolean): Filter images for focus. Default True.\n",
59+
" im_start (integer): Images to start from. Default 0.\n",
60+
" cell_analysis (boolean): Parameter where script also analyses cell\n",
61+
" images. Default True.\n",
62+
" one_savefile (boolean): Parameter that, if true, doesn't save a file. Default True.\n",
63+
" per image but amalgamates them into one file. Default True.\n",
64+
" disp (boolean): If true, prints when analysed an image stack. Default True.\n",
65+
" folder_recursion (boolean): If true, recursively finds folders and analyses each separately.\n",
66+
"\n",
67+
"All have defaults except folder, which is necessary.\n",
68+
"\"\"\"\n",
69+
"\n",
70+
"folder = os.path.abspath(\n",
71+
" r\"../example_images\"\n",
72+
") # example folder, replace as you wish. Always input as the absolute path of a raw string\n",
73+
"\n",
74+
"# This repo doesn't ship a real bulk-stain example image, so bulk_string is set to\n",
75+
"# \"C0\" below purely to exercise the three-channel code path end-to-end on the\n",
76+
"# existing two-colour example data (the cell image doubles as a bulk-stain stand-in).\n",
77+
"# On real three-channel data, use a string that uniquely identifies your bulk stain\n",
78+
"# channel files, distinct from protein_string and cell_string.\n",
79+
"RASP.analyse_images(\n",
80+
" folder,\n",
81+
" protein_string=\"C1\",\n",
82+
" cell_string=\"C0\",\n",
83+
" bulk_string=\"C0\",\n",
84+
" focus_images=\"C1\",\n",
85+
" thres=0.025,\n",
86+
" large_thres=1000.0,\n",
87+
" cell_analysis=True,\n",
88+
")"
89+
]
90+
}
91+
],
92+
"metadata": {
93+
"kernelspec": {
94+
"display_name": "Python 3 (ipykernel)",
95+
"language": "python",
96+
"name": "python3"
97+
},
98+
"language_info": {
99+
"codemirror_mode": {
100+
"name": "ipython",
101+
"version": 3
102+
},
103+
"file_extension": ".py",
104+
"mimetype": "text/x-python",
105+
"name": "python",
106+
"nbconvert_exporter": "python",
107+
"pygments_lexer": "ipython3",
108+
"version": "3.12.7"
109+
}
110+
},
111+
"nbformat": 4,
112+
"nbformat_minor": 5
113+
}

‎example_twocolour_notebooks/.ipynb_checkpoints/Step2_SingleImageAnalysis-checkpoint.ipynb‎

Lines changed: 489 additions & 0 deletions
Large diffs are not rendered by default.

‎src/IOFunctions.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ def save_analysis(
116116
lo_mask=[0],
117117
cell_mask=None,
118118
one_savefile=True,
119+
bulk_mask=None,
120+
bulk_string=None,
119121
):
120122
"""
121123
saves analysis.
@@ -132,6 +134,8 @@ def save_analysis(
132134
lo_mask (np.ndarray): mask of large objects
133135
cell_mask (np.ndarray): cell mask if cell mask saving
134136
one_savefile (boolean): if True, saving all analysis in one csv
137+
bulk_mask (np.ndarray): bulk stain mask if bulk mask saving
138+
bulk_string (np.1darray): strings for bulk-stained data
135139
"""
136140
A_F = _get_AF()
137141

@@ -172,6 +176,16 @@ def _write_dataframe(df, filepath, append=False):
172176
),
173177
bit=np.uint8,
174178
)
179+
180+
if bulk_mask is not None:
181+
self.write_tiff(
182+
bulk_mask,
183+
os.path.join(
184+
analysis_directory,
185+
f"{base_filename.split(protein_string)[0]}{bulk_string}_bulkMask.tiff",
186+
),
187+
bit=np.uint8,
188+
)
175189
return
176190

177191
# Handling single file saving
@@ -225,6 +239,17 @@ def _write_dataframe(df, filepath, append=False):
225239
bit=np.uint8,
226240
)
227241

242+
# Optional bulk stain mask
243+
if bulk_mask is not None:
244+
self.write_tiff(
245+
bulk_mask,
246+
os.path.join(
247+
analysis_directory,
248+
f"{_get_base_filename(files[i]).split(protein_string)[0]}{bulk_string}_bulkMask.tiff",
249+
),
250+
bit=np.uint8,
251+
)
252+
228253
# Save or append data
229254
dfs_to_save = [to_save, to_save_largeobjects, n_spots, n_largeobjects]
230255
for df, path in zip(dfs_to_save, save_paths):

0 commit comments

Comments
 (0)