From 7039f981921be25a403e939f6654838d76a4c0b8 Mon Sep 17 00:00:00 2001 From: Alex Kong Date: Thu, 21 Nov 2024 11:02:25 -0800 Subject: [PATCH 1/3] Ensure incomplete_fov_check works with first 5 channels irrespective of if Au was found --- src/toffy/bin_extraction.py | 10 ++++---- templates/3a_monitor_MIBI_run.ipynb | 14 ++++++++++- templates/3b_extract_images_from_bin.ipynb | 28 +++++++++++++++++++--- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/toffy/bin_extraction.py b/src/toffy/bin_extraction.py index 9ece159a..4ba092ca 100644 --- a/src/toffy/bin_extraction.py +++ b/src/toffy/bin_extraction.py @@ -99,12 +99,14 @@ def incomplete_fov_check( run_file_path = os.path.join(bin_file_dir, run_name + ".json") run_metadata = read_json_file(run_file_path, encoding="utf-8") - # get fov and channel info - fovs = io_utils.list_folders(extraction_dir, "fov") + # TODO: list_folders does not handle cases such as "fov0" correctly + # need to add a fix in to alpineer to deal with this + fovs = [f for f in os.listdir(extraction_dir) if "fov" in f] + + # NOTE: the specifically-named "Au" channel is no longer be assumed to be present + # simply check the first 5 channels found channels = io_utils.list_files(os.path.join(extraction_dir, fovs[0]), ".tiff") channels_subset = channels[:num_channels] - if "Au.tiff" not in channels_subset: - channels_subset = channels_subset[:-1] + ["Au.tiff"] incomplete_fovs = {} for fov in fovs: diff --git a/templates/3a_monitor_MIBI_run.ipynb b/templates/3a_monitor_MIBI_run.ipynb index ba24a348..4fc93c3d 100644 --- a/templates/3a_monitor_MIBI_run.ipynb +++ b/templates/3a_monitor_MIBI_run.ipynb @@ -76,6 +76,17 @@ "log_path = os.path.join('C:\\\\Users\\\\Customer.ION\\\\Documents\\\\watcher_logs', run_name)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# define the specific channels you want to extract as intensity images, leave list empty if there are none\n", + "# NOTE: these must match names found in panel_path\n", + "intensity_channels = [\"Au\", \"chan_39\"]" + ] + }, { "cell_type": "markdown", "metadata": { @@ -125,6 +136,7 @@ " save_dir=metrics_plot_dir,\n", " panel=panel,\n", " warn_overwrite=False,\n", + " intensities=intensity_channels,\n", ")" ] }, @@ -154,7 +166,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.16" + "version": "3.11.9" }, "vscode": { "interpreter": { diff --git a/templates/3b_extract_images_from_bin.ipynb b/templates/3b_extract_images_from_bin.ipynb index fd65c96a..17a20104 100644 --- a/templates/3b_extract_images_from_bin.ipynb +++ b/templates/3b_extract_images_from_bin.ipynb @@ -98,6 +98,18 @@ "The function below will skip any previously extracted FOVs and print \"Extraction completed!\" when done. If you receive a warning message stating \"no viable bin files were found\", then all of the necessary FOVs have already been extracted." ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "27887a2a-3dc7-4c7a-ac44-e404c29ff112", + "metadata": {}, + "outputs": [], + "source": [ + "# define the specific channels you want to extract as intensity images, leave list empty if there are none\n", + "# NOTE: these must match names found in panel_path\n", + "intensity_channels = [\"Au\", \"chan_39\"]" + ] + }, { "cell_type": "code", "execution_count": null, @@ -106,11 +118,21 @@ "outputs": [], "source": [ "# base deficient extraction\n", - "extract_missing_fovs(base_dir, extraction_dir, panel)\n", + "extract_missing_fovs(\n", + " base_dir,\n", + " extraction_dir,\n", + " panel,\n", + " extract_intensities=intensity_channels,\n", + ")\n", "\n", "# mass proficient extraction (for long-term storage)\n", "if extract_prof:\n", - " extract_missing_fovs(base_dir, extraction_prof_dir, modify_panel_ranges(panel, start_offset=0.3, stop_offset=0.3))" + " extract_missing_fovs(\n", + " base_dir,\n", + " extraction_prof_dir,\n", + " modify_panel_ranges(panel, start_offset=0.3, stop_offset=0.3),\n", + " extract_intensities=intensity_channels,\n", + " )" ] }, { @@ -149,7 +171,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.16" + "version": "3.11.9" } }, "nbformat": 4, From eb7268bbdf7cca7ff3937ab7d890ef363302e891 Mon Sep 17 00:00:00 2001 From: Alex Kong Date: Thu, 21 Nov 2024 11:54:55 -0800 Subject: [PATCH 2/3] Fix test to ensure the first two tests work for all channels partially imaged, and last test for only one --- tests/bin_extraction_test.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/bin_extraction_test.py b/tests/bin_extraction_test.py index 834af1b0..a8b52218 100644 --- a/tests/bin_extraction_test.py +++ b/tests/bin_extraction_test.py @@ -153,10 +153,15 @@ def test_incomplete_fov_check(): # change fov-2 to have zero values in the bottom of image fov2_data = load_utils.load_imgs_from_tree(extraction_dir, fovs=["fov-2-scan-1"]) - fov2_data[:, 10:, :, 0] = 0 + fov2_data_partial = fov2_data.copy() + fov2_data_partial[:, 10:, :, :] = 0 image_utils.save_image( os.path.join(extraction_dir, "fov-2-scan-1", "Au.tiff"), - fov2_data.loc["fov-2-scan-1", :, :, "Au"], + fov2_data_partial.loc["fov-2-scan-1", :, :, "Au"], + ) + image_utils.save_image( + os.path.join(extraction_dir, "fov-2-scan-1", "chan2.tiff"), + fov2_data_partial.loc["fov-2-scan-1", :, :, "chan2"], ) # test warning for partial fovs (checking 1 channel img) @@ -173,6 +178,11 @@ def test_incomplete_fov_check(): ) # test that increasing the number of channels to check causes no warning + # NOTE: this case specifically tests if only a subset of channels are partially imaged + image_utils.save_image( + os.path.join(extraction_dir, "fov-2-scan-1", "chan2.tiff"), + fov2_data.loc["fov-2-scan-1", :, :, "chan2"], + ) with warnings.catch_warnings(): warnings.simplefilter("error") bin_extraction.incomplete_fov_check( From 15dd93cffecea61e61e1fe4535caa69a58938f54 Mon Sep 17 00:00:00 2001 From: Alex Kong Date: Thu, 21 Nov 2024 12:33:00 -0800 Subject: [PATCH 3/3] Clarify comment --- src/toffy/bin_extraction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/toffy/bin_extraction.py b/src/toffy/bin_extraction.py index 4ba092ca..559f271f 100644 --- a/src/toffy/bin_extraction.py +++ b/src/toffy/bin_extraction.py @@ -104,7 +104,7 @@ def incomplete_fov_check( fovs = [f for f in os.listdir(extraction_dir) if "fov" in f] # NOTE: the specifically-named "Au" channel is no longer be assumed to be present - # simply check the first 5 channels found + # simply check the first "num_channels" channels found channels = io_utils.list_files(os.path.join(extraction_dir, fovs[0]), ".tiff") channels_subset = channels[:num_channels]