From 7540686e72017af652554059b7c6b8f263773f9b Mon Sep 17 00:00:00 2001 From: alex-l-kong Date: Thu, 29 Aug 2024 00:06:45 -0700 Subject: [PATCH] Map to closest core in case centroid in mapping dict was "missed" by MALDI scan --- src/maldi_tools/extraction.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/maldi_tools/extraction.py b/src/maldi_tools/extraction.py index 561a8a7..99cfb6c 100644 --- a/src/maldi_tools/extraction.py +++ b/src/maldi_tools/extraction.py @@ -8,6 +8,7 @@ import json import os +import warnings from functools import partial from operator import itemgetter from pathlib import Path @@ -431,10 +432,19 @@ def map_coordinates_to_core_name( "Region", ] if region_match.shape[0] == 0: - raise ValueError( + core_centroid_distances: pd.Series = np.sqrt( + (region_core_info["x"] - center_point["x"]) ** 2 + + (region_core_info["y"] - center_point["y"]) ** 2 + ) + region_match = region_core_info.iloc[core_centroid_distances.idxmin(), 0] + + # TODO: add an error threshold of a few pixels, shouldn't naively map everything + warnings.warn( f"Could not find mapping of core {core['name']} to any location on the slide, " "please verify that you positioned the central point of the core correctly " - "using the TSAI tiler, or that you've set the right poslog file." + "using the TSAI tiler, or that you've set the right poslog file.\n\n" + f"The closest region to the core's centroid is {region_match.values[0]}, " + "using this as finalized mapping." ) core_region_mapping[region_match.values[0]] = core["name"]