Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: snazzy-test-env
name: snazzy-env
channels:
- conda-forge
- defaults
Expand Down
56 changes: 19 additions & 37 deletions snazzy_processing/notebooks/snazzy-processing-pipeline.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
"\n",
"Integrates all of the modules from the package, to go from raw data to csv output.\n",
"More information about each module can be found in the other Jupyter Notebooks.\n",
"You should change the `experiment name` and the `img_path` at the fist cell."
"You should change the `experiment name` and the `img_path` at the fist cell.\n",
"\n",
"If the raw data is in nd2 format, it can be converted directly using the pipeline.\n",
"For other formats, the image must me converted to tif using another application, for example ImageJ.\n",
"\n",
"Adjust the image paths to where the nd2 file is, and where the tif file will be saved."
]
},
{
Expand All @@ -27,38 +32,14 @@
"\n",
"from snazzy_processing import pipeline, slice_img\n",
"\n",
"experiment_name = '20240611_25C'\n",
"experiment_name = '20240611'\n",
"root_dir = Path.cwd().parent\n",
"project_dir = root_dir.joinpath('data', experiment_name)\n",
"res_dir = root_dir.joinpath('results', experiment_name)\n",
"res_dir.mkdir(parents=True, exist_ok=True)\n",
"\n",
"# Provide an absolute path for the raw tif image\n",
"# If you are starting with another image format, ignore this line and convert the\n",
"# image in the next cell\n",
"img_path = ''"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If the raw data is in nd2 format, it must first be converted to a tif file, and that tif file should be used in the other cells of this jupyter notebook.\n",
"\n",
"Adjust the image paths to where the nd2 file is, and where the tif file will be saved."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"base_path = Path.home()\n",
"# Path where the raw image is located:\n",
"nd2_path = base_path.joinpath(\"Documents\", \"raw_data\", f\"{experiment_name}.nd2\")\n",
"# Path where the new tiff file will be saved:\n",
"img_path = base_path.joinpath(\"Documents\", \"raw_data\", f\"{experiment_name}.tif\")"
"# Provide an absolute path for the raw tif or nd2 image\n",
"img_path = Path.home().joinpath(\"Documents\", \"raw_data\", f\"{experiment_name}.nd2\")"
]
},
{
Expand All @@ -74,13 +55,12 @@
"metadata": {},
"outputs": [],
"source": [
"first_frames = f\"first_frames.tif\"\n",
"first_frames = \"first_frames.tif\"\n",
"first_frames_path = root_dir.joinpath(\"results\", experiment_name, first_frames)\n",
"if first_frames_path.exists():\n",
" print(f\"{first_frames_path.stem} already exists.\\nPlease choose another path.\")\n",
"else:\n",
" file_path = nd2_path if nd2_path else img_path\n",
" slice_img.save_first_frames_as_tiff(file_path, first_frames_path, 10)"
" slice_img.save_first_frames_as_tiff(img_path, first_frames_path, 10)"
]
},
{
Expand All @@ -103,11 +83,11 @@
"img = slice_img.get_first_image(first_frames_path)\n",
"\n",
"coords = slice_img.calculate_slice_coordinates(\n",
" first_frames_path, n_cols=4, thres_adjust=-5\n",
" first_frames_path, n_cols=2, thres_adjust=-10\n",
")\n",
"\n",
"# If necessary, manually change the bboxes size by changing w and h:\n",
"boundaries = slice_img.increase_bbox(coords, w=150, h=40, shape=img.shape)\n",
"boundaries = slice_img.increase_bbox(coords, w=190, h=40, shape=img.shape)\n",
"\n",
"rect_coords = [slice_img.boundary_to_rect_coords(b) for b in boundaries.values()]\n",
"recs = [Rectangle((y, x), w, h) for (x, y, w, h) in rect_coords]\n",
Expand Down Expand Up @@ -159,7 +139,7 @@
"clean_up_data = False\n",
"# List of the ids of the embryos that should be processed\n",
"# The ids are the bbox numbers from the previous cell output\n",
"embryos = [1, 2, 3, 4, 5]\n",
"embryos = []\n",
"# Interval (number of frames) used to calculate VNC length\n",
"vnc_length_interval = 10\n",
"# Window (number of frames) to calculate VNC ROI (which is then used to calculate activity)\n",
Expand All @@ -170,9 +150,11 @@
"embs_dest.mkdir(parents=True, exist_ok=True)\n",
"\n",
"# nd2 to tif\n",
"if nd2_path:\n",
"if img_path.suffix == \".nd2\":\n",
" print(\"Converting from nd2 to tif\")\n",
" slice_img.save_as_tiff(nd2_path, img_path)\n",
" output_path = img_path.parent.joinpath(f\"{img_path.stem}.tif\")\n",
" slice_img.save_as_tiff(img_path, output_path)\n",
" img_path = output_path\n",
"\n",
"# tif to individual movies\n",
"print(\"Cropping individual movies\")\n",
Expand Down Expand Up @@ -212,7 +194,7 @@
"# write params used\n",
"output_path = root_dir.joinpath(\"results\", experiment_name, \"params.txt\")\n",
"pipeline.log_params(\n",
" path=output_path,\n",
" output_path=output_path,\n",
" experiment_name=experiment_name,\n",
" embryos=embryos,\n",
" window=window,\n",
Expand Down
7 changes: 6 additions & 1 deletion snazzy_processing/snazzy_processing/slice_img.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ def save_first_frames_as_tiff(file: Path, dest_path: Path, n: int):
print(f"File '{dest.name}' already exists.")
return
if file.suffix == ".tif" or file.suffix == ".tiff":
initial_frames = imread(file, key=slice(0, n))
# tiffile will slice over pages, so we need to reshape
initial_frames = imread(file, key=slice(0, 2 * n))
_, y, x = initial_frames.shape
initial_frames = np.reshape(initial_frames, (n, 2, y, x))

imwrite(dest_path, initial_frames)
else:
with ND2File(file) as f:
Expand Down Expand Up @@ -426,6 +430,7 @@ def get_first_image(img_path: Path):

The channel 2 frames are averaged and equalized, for better visualization."""
img = imread(img_path)
print(img.shape)
first_frame = np.average(img[:, 1, :, :], axis=0)
return equalize_hist(first_frame)

Expand Down
Loading