Alternative ways of masking - KDTree#

In addition to convolutional techniques, GRIDGEN supports masks created using KDTree-based neighbour counting and Self-Organizing Maps (SOM).

We will test derive cancer stroma with both SOM and KDTree and find identify γδ T cell regions using KDtree approaches in Xenium CRC data.

%load_ext autoreload

import os 
import sys
import time
import logging
import re
from tqdm import tqdm
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import json
import anndata as ad
import scanpy as sc
import squidpy as sq
from natsort import natsorted
import matplotlib.colors as mcolors
np.random.seed(42)
import cv2
import copy
import seaborn as sns
from skimage.measure import label
from matplotlib import patches
sys.path.append(os.path.dirname(os.getcwd()))
from gridgene import get_arrays as ga
from gridgene import contours, get_masks 
from gridgene.mask_properties import MaskAnalysisPipeline, MaskDefinition,MorphologyExtractor
from gridgene.binsom import GetBins, GetContour
from helper_plot import parse_dapi, draw_categorical_json,plot_TRGC_TRDC_points_polygons, plot_TRGC_TRDC_points_contours,draw_categorical_mask_from_tiff,plot_TRGC_TRDC_points_mask
/home/martinha/miniconda3/envs/GRIDGEN/lib/python3.11/site-packages/dask/dataframe/__init__.py:31: FutureWarning: The legacy Dask DataFrame implementation is deprecated and will be removed in a future version. Set the configuration option `dataframe.query-planning` to `True` or None to enable the new Dask Dataframe implementation and silence this warning.
  warnings.warn(
/home/martinha/miniconda3/envs/GRIDGEN/lib/python3.11/site-packages/anndata/utils.py:434: FutureWarning: Importing read_text from `anndata` is deprecated. Import anndata.io.read_text instead.
  warnings.warn(msg, FutureWarning)

define the logger : can be None, and is set to INFO

# define the logger :  can be None, and is set to INFO
# Custom logger setup
logger = logging.getLogger('contour_logger')
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)

Get the files

xenium_path =  '../../xenium_data/HLA/GD_TMA1_S3/fov_filtered'

to_exclude = [
    'TMA1_Selection14_filtered.csv' , # little tumour
    'TMA1_Selection15_filtered.csv', # tonsil
    'TMA1_Selection18_filtered.csv' , # normal
    'TMA1_Selection24_filtered.csv', # tonsl 
    'TMA1_Selection27_filtered.csv', # low quality
    'TMA1_Selection32_filtered.csv', # low quality
    'TMA1_Selection33_filtered.csv', # low quality
             ]
files_tma1 = os.listdir(xenium_path)
files = [os.path.join(xenium_path, file) for file in files_tma1 if file not in to_exclude]
print(len(files))
20

Neighbour counting based cancer stroma identification

The KD-tree algorithm, a space-partitioning structure, efficiently organizes points in multidimensional space and is commonly used to accelerate nearest-neighbour searches in local neighbourhood analysis. To exemplify this approach, we derived tumour and stromal regions (Supplementary Figure 2D) by calculating, for each point, the number of neighbouring transcripts within a 10 μm radius — both across the full set of genes and a cancer-specific gene set. Points with a high number of cancer gene neighbours were classified as cancer regions, while areas lacking nearby transcripts were designated as empty or stromal.

Because this computation is performed on a per-point basis, the KD-tree-based method is generally slower than convolutional approaches, particularly for large datasets. However, it can be effective for smaller regions or targeted analyses, where its higher resolution and point-level granularity may be advantageous.

Use an example file

# we will do for a single file 
file_csv = files[7]
filename = os.path.basename(file_csv).split('.')[0] # Extract the filename without extension
name_file = '_'.join(filename.split('_')[:2])# Extract the desired part

df_total = pd.read_csv(file_csv)
df_total = df_total[['x_location', 'y_location', 'feature_name']]
df_total = df_total.rename(columns={'feature_name': 'target'})
df_total = df_total[~df_total['target'].str.contains('System|egative')]
df_total['X'] = df_total['x_location'] - min(df_total['x_location'])
df_total['Y'] = df_total['y_location'] - min(df_total['y_location'])

n_genes = len(df_total['target'].unique())
height = int(max(df_total['X'])) + 1
width = int(max(df_total['Y'])) + 1

print(f'n genes: {n_genes}')
print(f'shape: {height}, {width}')
print(f'n hits {len(df_total)}')
n genes: 480
shape: 1809, 1769
n hits 4433228

Plot the transcript points for cancer and stroma

target_tum = ['EPCAM', 'SMIM22','CLDN3', 'KRT18','LGALS4', 'KRT8', 'ELF3','TSPAN8', 'STMN1', 'CD47', 'MYC', 'LGALS3'] 
df_subset_tum = df_total[df_total['target'].isin(target_tum)]
fig, axes = plt.subplots(1, 3, figsize=(18, 6))

# Plot 1: All points
axes[0].scatter(df_total['X'], df_total['Y'], c='blue', alpha=0.6, s=0.01)
axes[0].set_title('All Points')
axes[0].set_xlabel('X Coordinate')
axes[0].set_ylabel('Y Coordinate')

# Plot 2: Tumour subset
axes[1].scatter(df_subset_tum['X'], df_subset_tum['Y'], c='blue', alpha=0.6, s=0.1)
axes[1].set_title('Cancer Targets')
axes[1].set_xlabel('X Coordinate')
axes[1].set_ylabel('Y Coordinate')

# Plot 3: Overlay
axes[2].scatter(df_total['X'], df_total['Y'], c='blue', alpha=0.6, s=0.1, label='All Points')
axes[2].scatter(df_subset_tum['X'], df_subset_tum['Y'], c='red', alpha=0.6, s=0.1, label='Tumour Points')
axes[2].set_title('All Points + Cancer Targets')
axes[2].set_xlabel('X Coordinate')
axes[2].set_ylabel('Y Coordinate')
axes[2].legend()

# Adjust layout
plt.tight_layout()
plt.show()
/tmp/ipykernel_4069310/3341716541.py:24: UserWarning: Creating legend with loc="best" can be slow with large amounts of data.
  plt.tight_layout()
/home/martinha/miniconda3/envs/GRIDGEN/lib/python3.11/site-packages/IPython/core/pylabtools.py:170: UserWarning: Creating legend with loc="best" can be slow with large amounts of data.
  fig.canvas.print_figure(bytes_io, **kw)
../_images/6dd80e89f6cbceb9880706e3342d693df6e041d73117b2638fef29c9061a1e9d.png

First we will get all neighborhood points to be able to identify tissue/empty regions. We will inquiry all points in a max distance of 10 pixels

CEmpty = contours.KDTreeContours(df_total[['target', 'X', 'Y']], contour_name='empty', 
                                height=height, width=width)

CEmpty.get_kdt_dist(radius = 10) # max distance of 10 
array_total_nei = CEmpty.get_neighbour_array() # array 2D of the neighboors per point

plt.imshow(array_total_nei, cmap="hot", origin="lower")
plt.colorbar()
plt.show()
2025-06-18 14:47:50,271 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:48:37,998 - gridgen.contours.empty - INFO - get_kdt_dist took 47.7267 seconds
2025-06-18 14:48:38,044 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0453 seconds
../_images/8a02c50be9d8c35d40d73dbf812f784b71906ebca105926b9a564d70ba559304.png

we can use this values to derive the contours, using instead of the local sum from convolutional sum the neighborhood points

density_th_empty = 100
min_area_th_empty = 700
CEmpty.contours_from_neighbors(density_threshold = density_th_empty,
                       min_area_threshold = min_area_th_empty, 
                               directionality = 'lower') # attention that directionality is lower here 

fig, axs = plt.subplots(1, 2, figsize=(20, 10))

CEmpty.plot_contours_scatter(path=None, show=False, s=0.05, alpha=0.5, linewidth=1,
                           c_points= 'blue',c_contours= 'red', ax=axs[0])
axs[0].set_title('total points and contours for empty')  
CEmpty.plot_conv_sum(cmap='plasma', c_countour='white', ax=axs[1])
axs[1].set_title('total points and contours for empty')

plt.show()
    
2025-06-18 14:48:38,751 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 42
2025-06-18 14:48:38,752 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 42
2025-06-18 14:48:38,759 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.3561 seconds
../_images/d8dbc53abd5ee6bdd238bffc4bae8bb53c4fc937179c583c3f0db5305faf1ea5.png

Define cancer the same way

subset_condition = df_total['target'].isin(target_tum) 
df_subset = df_total[subset_condition]

Ctum = contours.KDTreeContours(df_subset[['target', 'X', 'Y']],
                               contour_name='cancer',
                               height=height, width=width)

Ctum.get_kdt_dist(radius = 10) # max distance of 10 
array_tum_nei = Ctum.get_neighbour_array() # array 2D of the neighboors per point

plt.imshow(array_tum_nei, cmap="hot", origin="lower")
plt.colorbar()
plt.show()
2025-06-18 14:48:40,352 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:48:43,810 - gridgen.contours.cancer - INFO - get_kdt_dist took 3.4577 seconds
2025-06-18 14:48:43,819 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0082 seconds
../_images/34b268680a3d84854c77888c9085c013e6c0ff345e3179b2c885a11533894c90.png

Because we are using a smaller set of targets, the points get sparse and makes difficult to derive contours.

We can interpolate the array and derive the contours .

array_tum_nei = Ctum.interpolate_array()

plt.imshow(array_tum_nei, cmap="hot", origin="lower")
plt.colorbar()
plt.show()
../_images/12e5511e6ccbf2e56a97293e5bfd0a4527e6b5db7365d218c1ff72042ad0aff2.png
density_th_tum = 50
min_area_th_tum = 700
Ctum.contours_from_neighbors(density_threshold = density_th_tum,
                       min_area_threshold = min_area_th_tum,
                             directionality = 'higher') 

fig, axs = plt.subplots(1, 2, figsize=(20, 10))

Ctum.plot_contours_scatter(path=None, show=False, s=0.05, alpha=0.5, linewidth=1,
                           c_points= 'blue',c_contours= 'red', ax=axs[0])
axs[0].set_title('total points and contours for empty')  
Ctum.plot_conv_sum(cmap='plasma', c_countour='white', ax=axs[1])
axs[1].set_title('total points and contours for empty')

plt.show()
2025-06-18 14:48:46,011 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 35
2025-06-18 14:48:46,011 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 35
2025-06-18 14:48:46,012 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0227 seconds
../_images/9dd290a19dbfe778dba19d80185caecc3008a391b3e8aad0939a597b37e00710.png

With the contours, we can build the masks

#### obtain masks
GM = get_masks.GetMasks(image_shape = (height, width))
  
mask_empty = GM.create_mask(CEmpty.contours)
mask_tum = GM.create_mask(Ctum.contours)
mask_stroma = GM.subtract_masks(np.ones((height, width), dtype=np.uint8), mask_tum, mask_empty)          
mask_stroma = GM.filter_binary_mask_by_area(mask_stroma, min_area=700)

GM.plot_masks(masks=[mask_stroma, mask_tum], mask_names=['Stroma', 'Tumour'],
                  background_color=(1, 1, 1), mask_colors={'Stroma': (65, 105, 225), 'Tumour': (255, 165, 0)},
                  path=None, show=True, ax=None, figsize=(8, 6))
2025-06-18 14:48:46,683 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:48:46,690 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/3224032b64657780bd54afd830f9e3f0c56b90dd8d4bcec767c13bef7438fe00.png

Do full cohort

radius = max_dist = 10  # 100 px 
density_th_empty = 100
min_area_th_empty = 700
density_th_tum = 50
min_area_th_tum = 700

for file_csv in tqdm(files, desc="Processing Folders", unit="folder"):
    start_time = time.time()
    filename = os.path.basename(file_csv).split('.')[0] # Extract the filename without extension
    name_file = '_'.join(filename.split('_')[:2])# Extract the desired part
    path_save = f"results/kdtree/{name_file}/"
    if not os.path.exists(path_save):
        os.makedirs(path_save)
        
    df_total = pd.read_csv(file_csv)

    df_total = df_total[['x_location', 'y_location', 'feature_name']]
    df_total = df_total.rename(columns={'feature_name': 'target'})
    df_total = df_total[~df_total['target'].str.contains('System|egative')]
    df_total['X'] = df_total['x_location'] - min(df_total['x_location'])
    df_total['Y'] = df_total['y_location'] - min(df_total['y_location'])
    
    n_genes = len(df_total['target'].unique())
    height = int(max(df_total['X'])) + 1
    width = int(max(df_total['Y'])) + 1
    
    
    CEmpty = contours.KDTreeContours(df_total[['target', 'X', 'Y']], contour_name='empty', 
                                height=height, width=width)

    CEmpty.get_kdt_dist(radius = 10) # max distance of 10 
    array_total_nei = CEmpty.get_neighbour_array() # array 2D of the neighboors per point
    CEmpty.contours_from_neighbors(density_threshold = density_th_empty,
                           min_area_threshold = min_area_th_empty, 
                                   directionality = 'lower') # attention that directionality is lower here 
    
    subset_condition = df_total['target'].isin(target_tum) 
    df_subset = df_total[subset_condition]

    Ctum = contours.KDTreeContours(df_subset[['target', 'X', 'Y']],
                                   contour_name='cancer',
                                   height=height, width=width)

    Ctum.get_kdt_dist(radius = 10) # max distance of 10 
    array_tum_nei = Ctum.get_neighbour_array() # array 2D of the neighboors per point
    array_tum_nei = Ctum.interpolate_array()
    Ctum.contours_from_neighbors(density_threshold = density_th_tum,
                           min_area_threshold = min_area_th_tum,
                                 directionality = 'higher') 

    #### obtain masks
    GM = get_masks.GetMasks(image_shape = (height, width))
  
    mask_empty = GM.create_mask(CEmpty.contours)
    mask_tum = GM.create_mask(Ctum.contours)
    mask_stroma = GM.subtract_masks(np.ones((height, width), dtype=np.uint8), mask_tum, mask_empty)          
    mask_stroma = GM.filter_binary_mask_by_area(mask_stroma, min_area=700)

    GM.plot_masks(masks=[mask_stroma, mask_tum], mask_names=['Stroma', 'Tumour'],
                      background_color=(1, 1, 1), mask_colors={'Stroma': (65, 105, 225), 'Tumour': (255, 165, 0)},
                      path=None, show=True, ax=None, figsize=(8, 6))
Processing Folders:   0%|                                                                                                                                | 0/20 [00:00<?, ?folder/s]2025-06-18 14:48:51,032 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:49:18,195 - gridgen.contours.empty - INFO - get_kdt_dist took 27.1587 seconds
2025-06-18 14:49:18,229 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0331 seconds
2025-06-18 14:49:18,794 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 57
2025-06-18 14:49:18,795 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 57
2025-06-18 14:49:18,806 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.5766 seconds
2025-06-18 14:49:18,946 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:49:21,413 - gridgen.contours.cancer - INFO - get_kdt_dist took 2.4657 seconds
2025-06-18 14:49:21,420 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0066 seconds
2025-06-18 14:49:23,065 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 31
2025-06-18 14:49:23,065 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 31
2025-06-18 14:49:23,065 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0253 seconds
2025-06-18 14:49:23,066 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:49:23,076 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/9f56a2a68f385b3818c0eddb175a30f7bca965b372db536f77448d2fb38697d2.png
Processing Folders:   5%|██████                                                                                                                  | 1/20 [00:36<11:31, 36.39s/folder]2025-06-18 14:49:27,891 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:50:02,787 - gridgen.contours.empty - INFO - get_kdt_dist took 34.8923 seconds
2025-06-18 14:50:02,825 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0375 seconds
2025-06-18 14:50:03,308 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 49
2025-06-18 14:50:03,309 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 49
2025-06-18 14:50:03,317 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.4917 seconds
2025-06-18 14:50:03,476 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:50:05,036 - gridgen.contours.cancer - INFO - get_kdt_dist took 1.5580 seconds
2025-06-18 14:50:05,041 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0050 seconds
2025-06-18 14:50:06,654 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 39
2025-06-18 14:50:06,654 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 39
2025-06-18 14:50:06,654 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0198 seconds
2025-06-18 14:50:06,655 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:50:06,662 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/8481cd644401b01b755ab715eb978c6b78a4e0e58be227e0d527e9635b14d22d.png
Processing Folders:  10%|████████████                                                                                                            | 2/20 [01:19<12:10, 40.60s/folder]2025-06-18 14:50:09,625 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:50:25,886 - gridgen.contours.empty - INFO - get_kdt_dist took 16.2566 seconds
2025-06-18 14:50:25,909 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0229 seconds
2025-06-18 14:50:26,644 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 217
2025-06-18 14:50:26,645 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 217
2025-06-18 14:50:26,657 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.7470 seconds
2025-06-18 14:50:26,754 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:50:27,139 - gridgen.contours.cancer - INFO - get_kdt_dist took 0.3839 seconds
2025-06-18 14:50:27,142 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0021 seconds
2025-06-18 14:50:28,528 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 8
2025-06-18 14:50:28,529 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 8
2025-06-18 14:50:28,529 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0081 seconds
2025-06-18 14:50:28,529 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:50:28,548 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/2a5897772b3f5d68a6e8f600979e221fd7998d4cbfa523091e98696707074605.png
Processing Folders:  15%|██████████████████                                                                                                      | 3/20 [01:41<09:05, 32.06s/folder]2025-06-18 14:50:30,800 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:50:46,593 - gridgen.contours.empty - INFO - get_kdt_dist took 15.7895 seconds
2025-06-18 14:50:46,610 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0172 seconds
2025-06-18 14:50:46,773 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 20
2025-06-18 14:50:46,773 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 20
2025-06-18 14:50:46,777 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.1659 seconds
2025-06-18 14:50:46,851 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:50:47,458 - gridgen.contours.cancer - INFO - get_kdt_dist took 0.6071 seconds
2025-06-18 14:50:47,461 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0022 seconds
2025-06-18 14:50:48,616 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 13
2025-06-18 14:50:48,617 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 13
2025-06-18 14:50:48,617 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0097 seconds
2025-06-18 14:50:48,618 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:50:48,621 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/8ceccc38b688102d7c6e8dff25175da67308e55df43bb219e43e75eea4211fdf.png
Processing Folders:  20%|████████████████████████                                                                                                | 4/20 [02:01<07:16, 27.30s/folder]2025-06-18 14:50:51,832 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:51:13,253 - gridgen.contours.empty - INFO - get_kdt_dist took 21.4182 seconds
2025-06-18 14:51:13,279 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0260 seconds
2025-06-18 14:51:14,055 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 123
2025-06-18 14:51:14,056 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 123
2025-06-18 14:51:14,068 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.7888 seconds
2025-06-18 14:51:14,173 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:51:14,744 - gridgen.contours.cancer - INFO - get_kdt_dist took 0.5707 seconds
2025-06-18 14:51:14,747 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0024 seconds
2025-06-18 14:51:16,278 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 10
2025-06-18 14:51:16,279 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 10
2025-06-18 14:51:16,279 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0090 seconds
2025-06-18 14:51:16,279 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:51:16,291 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/e6bf86aea077d2c0fa359ba3bcc7b7f824adf3f90d9a77a6f318e4116dec2a25.png
Processing Folders:  25%|██████████████████████████████                                                                                          | 5/20 [02:29<06:51, 27.46s/folder]2025-06-18 14:51:22,638 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:52:17,210 - gridgen.contours.empty - INFO - get_kdt_dist took 54.5686 seconds
2025-06-18 14:52:17,261 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0497 seconds
2025-06-18 14:52:17,646 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 19
2025-06-18 14:52:17,647 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 19
2025-06-18 14:52:17,656 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.3948 seconds
2025-06-18 14:52:17,871 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:52:19,524 - gridgen.contours.cancer - INFO - get_kdt_dist took 1.6526 seconds
2025-06-18 14:52:19,530 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0056 seconds
2025-06-18 14:52:21,190 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 54
2025-06-18 14:52:21,190 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 54
2025-06-18 14:52:21,191 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0339 seconds
2025-06-18 14:52:21,192 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:52:21,197 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/f65571d3b07da6ece49ee873aa57d7a4627c82bd55b15af67edfff1298083d1f.png
Processing Folders:  30%|████████████████████████████████████                                                                                    | 6/20 [03:34<09:22, 40.19s/folder]2025-06-18 14:52:26,550 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:53:07,380 - gridgen.contours.empty - INFO - get_kdt_dist took 40.6816 seconds
2025-06-18 14:53:07,423 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0418 seconds
2025-06-18 14:53:07,725 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 11
2025-06-18 14:53:07,725 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 11
2025-06-18 14:53:07,733 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.3094 seconds
2025-06-18 14:53:07,926 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:53:11,074 - gridgen.contours.cancer - INFO - get_kdt_dist took 3.1464 seconds
2025-06-18 14:53:11,082 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0079 seconds
2025-06-18 14:53:12,719 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 33
2025-06-18 14:53:12,720 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 33
2025-06-18 14:53:12,720 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0248 seconds
2025-06-18 14:53:12,721 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:53:12,726 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/fc107aa236671bfe86ca5c8e0ac97e2ecf6f924bcee2fc95e9a2475fba8cfaae.png
Processing Folders:  35%|██████████████████████████████████████████                                                                              | 7/20 [04:26<09:30, 43.90s/folder]2025-06-18 14:53:17,924 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:54:01,922 - gridgen.contours.empty - INFO - get_kdt_dist took 43.9770 seconds
2025-06-18 14:54:01,966 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0436 seconds
2025-06-18 14:54:02,294 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 42
2025-06-18 14:54:02,295 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 42
2025-06-18 14:54:02,302 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.3353 seconds
2025-06-18 14:54:02,487 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:54:05,924 - gridgen.contours.cancer - INFO - get_kdt_dist took 3.4354 seconds
2025-06-18 14:54:05,933 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0081 seconds
2025-06-18 14:54:07,414 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 35
2025-06-18 14:54:07,414 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 35
2025-06-18 14:54:07,415 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0226 seconds
2025-06-18 14:54:07,415 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:54:07,421 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/3224032b64657780bd54afd830f9e3f0c56b90dd8d4bcec767c13bef7438fe00.png
Processing Folders:  40%|████████████████████████████████████████████████                                                                        | 8/20 [05:20<09:27, 47.33s/folder]2025-06-18 14:54:12,087 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:54:43,993 - gridgen.contours.empty - INFO - get_kdt_dist took 31.8657 seconds
2025-06-18 14:54:44,033 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0385 seconds
2025-06-18 14:54:44,540 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 57
2025-06-18 14:54:44,541 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 57
2025-06-18 14:54:44,552 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.5186 seconds
2025-06-18 14:54:44,717 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:54:49,102 - gridgen.contours.cancer - INFO - get_kdt_dist took 4.3843 seconds
2025-06-18 14:54:49,112 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0092 seconds
2025-06-18 14:54:50,612 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 13
2025-06-18 14:54:50,613 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 13
2025-06-18 14:54:50,613 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0145 seconds
2025-06-18 14:54:50,613 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:54:50,624 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/11d1cffda0cdb5fe5cf305382b62ed645371ac1fddeaf711ca1c4bd2bd255f56.png
Processing Folders:  45%|██████████████████████████████████████████████████████                                                                  | 9/20 [06:03<08:26, 46.04s/folder]2025-06-18 14:54:53,264 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:55:07,798 - gridgen.contours.empty - INFO - get_kdt_dist took 14.5301 seconds
2025-06-18 14:55:07,821 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0218 seconds
2025-06-18 14:55:08,680 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 167
2025-06-18 14:55:08,681 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 167
2025-06-18 14:55:08,694 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.8733 seconds
2025-06-18 14:55:08,758 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:55:09,407 - gridgen.contours.cancer - INFO - get_kdt_dist took 0.6475 seconds
2025-06-18 14:55:09,410 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0024 seconds
2025-06-18 14:55:10,965 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 7
2025-06-18 14:55:10,965 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 7
2025-06-18 14:55:10,965 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0088 seconds
2025-06-18 14:55:10,966 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:55:10,988 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/781bbd7a3a47897545cb4846e65f144f9ea0680abd2e4902ccad231d0d00409f.png
Processing Folders:  50%|███████████████████████████████████████████████████████████▌                                                           | 10/20 [06:24<06:21, 38.13s/folder]2025-06-18 14:55:14,543 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:55:35,929 - gridgen.contours.empty - INFO - get_kdt_dist took 21.3828 seconds
2025-06-18 14:55:35,958 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0278 seconds
2025-06-18 14:55:36,383 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 61
2025-06-18 14:55:36,383 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 61
2025-06-18 14:55:36,392 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.4334 seconds
2025-06-18 14:55:36,515 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:55:37,095 - gridgen.contours.cancer - INFO - get_kdt_dist took 0.5792 seconds
2025-06-18 14:55:37,098 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0023 seconds
2025-06-18 14:55:38,234 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 19
2025-06-18 14:55:38,234 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 19
2025-06-18 14:55:38,234 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0108 seconds
2025-06-18 14:55:38,235 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:55:38,244 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/8ba97c12ab0884bb9ed3bbde01a382b19150d286c0ecb1c721ef1860e683c106.png
Processing Folders:  55%|█████████████████████████████████████████████████████████████████▍                                                     | 11/20 [06:51<05:12, 34.77s/folder]2025-06-18 14:55:42,486 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:56:14,776 - gridgen.contours.empty - INFO - get_kdt_dist took 32.2867 seconds
2025-06-18 14:56:14,811 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0344 seconds
2025-06-18 14:56:15,086 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 39
2025-06-18 14:56:15,087 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 39
2025-06-18 14:56:15,094 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.2821 seconds
2025-06-18 14:56:15,243 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:56:18,173 - gridgen.contours.cancer - INFO - get_kdt_dist took 2.9287 seconds
2025-06-18 14:56:18,180 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0068 seconds
2025-06-18 14:56:19,256 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 39
2025-06-18 14:56:19,256 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 39
2025-06-18 14:56:19,257 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0198 seconds
2025-06-18 14:56:19,257 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:56:19,263 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/697d913a5e8937bc59f7d9dc8791632521a0dccb5866ed5cd89ff03e757eaf2f.png
Processing Folders:  60%|███████████████████████████████████████████████████████████████████████▍                                               | 12/20 [07:32<04:53, 36.67s/folder]2025-06-18 14:56:22,891 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:56:44,031 - gridgen.contours.empty - INFO - get_kdt_dist took 21.1362 seconds
2025-06-18 14:56:44,061 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0289 seconds
2025-06-18 14:56:44,690 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 111
2025-06-18 14:56:44,691 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 111
2025-06-18 14:56:44,703 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.6414 seconds
2025-06-18 14:56:44,829 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:56:45,189 - gridgen.contours.cancer - INFO - get_kdt_dist took 0.3586 seconds
2025-06-18 14:56:45,192 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0020 seconds
2025-06-18 14:56:46,431 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 5
2025-06-18 14:56:46,431 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 5
2025-06-18 14:56:46,431 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0072 seconds
2025-06-18 14:56:46,432 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:56:46,448 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/a31d655a325e19262ee4ee92d9d40b1aae5db36a46453d2a753c343ae7179d23.png
Processing Folders:  65%|█████████████████████████████████████████████████████████████████████████████▎                                         | 13/20 [07:59<03:56, 33.80s/folder]2025-06-18 14:56:49,127 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:57:04,575 - gridgen.contours.empty - INFO - get_kdt_dist took 15.4449 seconds
2025-06-18 14:57:04,597 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0211 seconds
2025-06-18 14:57:05,141 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 89
2025-06-18 14:57:05,142 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 89
2025-06-18 14:57:05,152 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.5545 seconds
2025-06-18 14:57:05,243 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:57:06,602 - gridgen.contours.cancer - INFO - get_kdt_dist took 1.3583 seconds
2025-06-18 14:57:06,608 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0047 seconds
2025-06-18 14:57:07,947 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 49
2025-06-18 14:57:07,948 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 49
2025-06-18 14:57:07,948 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0287 seconds
2025-06-18 14:57:07,949 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:57:07,961 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/dcf615a5a6ea1ddb359574037facf876e3d1c2793ea8afbf609a1304d0d2f5b7.png
Processing Folders:  70%|███████████████████████████████████████████████████████████████████████████████████▎                                   | 14/20 [08:21<03:00, 30.11s/folder]2025-06-18 14:57:13,100 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:57:53,138 - gridgen.contours.empty - INFO - get_kdt_dist took 40.0348 seconds
2025-06-18 14:57:53,178 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0401 seconds
2025-06-18 14:57:53,502 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 39
2025-06-18 14:57:53,503 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 39
2025-06-18 14:57:53,510 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.3316 seconds
2025-06-18 14:57:53,693 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:57:57,776 - gridgen.contours.cancer - INFO - get_kdt_dist took 4.0821 seconds
2025-06-18 14:57:57,785 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0085 seconds
2025-06-18 14:57:59,282 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 36
2025-06-18 14:57:59,282 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 36
2025-06-18 14:57:59,283 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0208 seconds
2025-06-18 14:57:59,283 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:57:59,290 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/132a34cde83d2e2bafff0359c5be8742ab05d3f0fee570c9ef6dc9fe502242e2.png
Processing Folders:  75%|█████████████████████████████████████████████████████████████████████████████████████████▎                             | 15/20 [09:12<03:02, 36.49s/folder]2025-06-18 14:58:04,376 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:58:44,278 - gridgen.contours.empty - INFO - get_kdt_dist took 39.8965 seconds
2025-06-18 14:58:44,318 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0394 seconds
2025-06-18 14:58:44,644 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 41
2025-06-18 14:58:44,644 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 41
2025-06-18 14:58:44,652 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.3331 seconds
2025-06-18 14:58:44,824 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:58:47,233 - gridgen.contours.cancer - INFO - get_kdt_dist took 2.4079 seconds
2025-06-18 14:58:47,239 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0056 seconds
2025-06-18 14:58:48,611 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 25
2025-06-18 14:58:48,612 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 25
2025-06-18 14:58:48,612 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0195 seconds
2025-06-18 14:58:48,613 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:58:48,620 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/926368a9e236c1b11803c58c45a8c745abe17e0a29e8e0fa3478e1c071a0187e.png
Processing Folders:  80%|███████████████████████████████████████████████████████████████████████████████████████████████▏                       | 16/20 [10:01<02:41, 40.36s/folder]2025-06-18 14:58:52,328 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:59:14,345 - gridgen.contours.empty - INFO - get_kdt_dist took 22.0118 seconds
2025-06-18 14:59:14,374 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0284 seconds
2025-06-18 14:59:14,963 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 93
2025-06-18 14:59:14,964 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 93
2025-06-18 14:59:14,975 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.6003 seconds
2025-06-18 14:59:15,103 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:59:16,219 - gridgen.contours.cancer - INFO - get_kdt_dist took 1.1147 seconds
2025-06-18 14:59:16,223 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0036 seconds
2025-06-18 14:59:17,815 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 31
2025-06-18 14:59:17,816 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 31
2025-06-18 14:59:17,816 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0173 seconds
2025-06-18 14:59:17,816 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:59:17,829 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/afb1a0e23285c9ca0ce80d09cbfb3bba47bee3d60bbb5ac5a85a02b6810d5727.png
Processing Folders:  85%|█████████████████████████████████████████████████████████████████████████████████████████████████████▏                 | 17/20 [10:31<01:51, 37.01s/folder]2025-06-18 14:59:21,848 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 14:59:49,271 - gridgen.contours.empty - INFO - get_kdt_dist took 27.4191 seconds
2025-06-18 14:59:49,304 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0322 seconds
2025-06-18 14:59:49,691 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 42
2025-06-18 14:59:49,692 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 42
2025-06-18 14:59:49,700 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.3950 seconds
2025-06-18 14:59:49,839 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 14:59:52,088 - gridgen.contours.cancer - INFO - get_kdt_dist took 2.2480 seconds
2025-06-18 14:59:52,095 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0060 seconds
2025-06-18 14:59:53,895 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 29
2025-06-18 14:59:53,896 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 29
2025-06-18 14:59:53,896 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0232 seconds
2025-06-18 14:59:53,896 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 14:59:53,906 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/f8413ee93aa85898bc83532815b737128f3597183f8a67f2d71f9b70b39a5fad.png
Processing Folders:  90%|███████████████████████████████████████████████████████████████████████████████████████████████████████████            | 18/20 [11:07<01:13, 36.74s/folder]/tmp/ipykernel_4069310/2892387316.py:15: DtypeWarning: Columns (10) have mixed types. Specify dtype option on import or set low_memory=False.
  df_total = pd.read_csv(file_csv)
2025-06-18 14:59:57,466 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 15:00:17,691 - gridgen.contours.empty - INFO - get_kdt_dist took 20.2213 seconds
2025-06-18 15:00:17,719 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0274 seconds
2025-06-18 15:00:18,233 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 73
2025-06-18 15:00:18,234 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 73
2025-06-18 15:00:18,243 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.5239 seconds
2025-06-18 15:00:18,358 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 15:00:20,502 - gridgen.contours.cancer - INFO - get_kdt_dist took 2.1423 seconds
2025-06-18 15:00:20,508 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0057 seconds
2025-06-18 15:00:21,862 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 44
2025-06-18 15:00:21,863 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 44
2025-06-18 15:00:21,863 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0259 seconds
2025-06-18 15:00:21,864 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 15:00:21,875 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/375241dfd9d25a62c1114ad4fcfd31b436088dfd1de09eeb809a8755c4188f63.png
Processing Folders:  95%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████      | 19/20 [11:35<00:34, 34.09s/folder]2025-06-18 15:00:25,110 - gridgen.contours.empty - INFO - Initialized GetContour
2025-06-18 15:00:44,082 - gridgen.contours.empty - INFO - get_kdt_dist took 18.9689 seconds
2025-06-18 15:00:44,110 - gridgen.contours.empty - INFO - get_neighbour_array took 0.0263 seconds
2025-06-18 15:00:44,621 - gridgen.contours.empty - INFO - Number of contours after filtering no counts: 84
2025-06-18 15:00:44,621 - gridgen.contours.empty - INFO - Contours extracted from neighboor counts after checks: 84
2025-06-18 15:00:44,631 - gridgen.contours.empty - INFO - contours_from_neighbors took 0.5206 seconds
2025-06-18 15:00:44,747 - gridgen.contours.cancer - INFO - Initialized GetContour
2025-06-18 15:00:46,993 - gridgen.contours.cancer - INFO - get_kdt_dist took 2.2454 seconds
2025-06-18 15:00:47,000 - gridgen.contours.cancer - INFO - get_neighbour_array took 0.0058 seconds
2025-06-18 15:00:48,341 - gridgen.contours.cancer - INFO - Number of contours after filtering no counts: 35
2025-06-18 15:00:48,342 - gridgen.contours.cancer - INFO - Contours extracted from neighboor counts after checks: 35
2025-06-18 15:00:48,342 - gridgen.contours.cancer - INFO - contours_from_neighbors took 0.0279 seconds
2025-06-18 15:00:48,343 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 15:00:48,354 - gridgen.get_masks.GetMasks - INFO - Subtracted masks from base mask.
../_images/1800085b4b5e1330222f7b8c7c050ef3c7fcd2319786338623d12cba3f92af25.png
Processing Folders: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [12:01<00:00, 36.08s/folder]

KDTree approach to identify γδ enriched regions

We also applied the KD-tree method to identify spatial associations between TRGC and TRDC transcripts in order to identify γδ T cell regions. Specifically, we searched for transcript pairs located within a 7 μm radius. Due to the sparsity of these transcripts and the lack of sufficient density for contour generation, we defined regions by drawing circular masks centered on the identified transcript.

CosMx

cosmx_path_s3 =  '../../cosmx_data/S3/S3/20230628_151317_S3/AnalysisResults/yxyz3r7ufm'
dapi_folder = '/home/martinha/PycharmProjects/phd/spatial_transcriptomics/cosmx_data/S3/S3/20230628_151317_S3/CellStatsDir/Morphology2D/'
folder_names_s3 = [folder_name for folder_name in os.listdir(cosmx_path_s3) if
                os.path.isdir(os.path.join(cosmx_path_s3, folder_name))]


target_files_s3 = [
    os.path.join(cosmx_path_s3, folder, file)
    for folder in os.listdir(cosmx_path_s3)
    if os.path.isdir(os.path.join(cosmx_path_s3, folder))
    for file in os.listdir(os.path.join(cosmx_path_s3, folder))
    if '__target_call_coord.csv' in file
]

files_cosmx = natsorted(target_files_s3)
len(files_cosmx)
20
target_gd = ['TRGC1/TRGC2', 'TRDC']
target_ab = ['TRBC1/TRBC2', 'TRAC']
target_tum = ['EPCAM', 'SMIM22','CLDN3', 'KRT18','LGALS4', 'KRT8', 'ELF3','TSPAN8', 'STMN1', 'CD47', 'MYC', 'LGALS3'] 


target_cd8 = ['CD8A','CD8B', 'TRBC1/TRBC2', 'TRAC']
target_cd4 = ['CD4']
fov = 'FOV007'
file_csv = [file for file in files_cosmx if fov in file][0]  
    
df_total = pd.read_csv(file_csv)
df_total = df_total.rename(columns={'x': 'X', 'y': 'Y'})
df_total = df_total[~df_total['target'].str.contains('System|egative')]

n_genes = len(df_total['target'].unique())
height = int(max(df_total['X'])) + 1
width = int(max(df_total['Y'])) + 1

print(f'n genes: {n_genes}')
print(f'shape: {height}, {width}')
print(f'n hits {len(df_total)}')
n genes: 999
shape: 4246, 4245
n hits 2838781
# this makes the sparse df to an array with the spatial information 
target_dict_total = {target: index for index, target in enumerate(df_total['target'].unique())}
array_total = ga.transform_df_to_array(df = df_total, target_dict=target_dict_total, array_shape = (height, width,len(target_dict_total))).astype(np.int8)

# creating subsets 
df_subset_gd, array_subset_gd, target_indices_subset_gd = ga.get_subset_arrays(df_total, array_total,
                                                                                   target_dict_total,
                                                                                   target_list=target_gd,
                                                                           target_col='target')

df_subset_ab, array_subset_ab, target_indices_subset_ab = ga.get_subset_arrays(df_total, array_total,
                                                                                       target_dict_total,
                                                                                       target_list=target_ab,
                                                                                       target_col='target')
    
    
df_subset_g, array_subset_g, target_indices_subset_g = ga.get_subset_arrays(df_total, array_total,
                                                                                       target_dict_total,
                                                                                       target_list=['TRGC1/TRGC2'],
                                                                                       target_col='target')
    
df_subset_d, array_subset_d, target_indices_subset_d = ga.get_subset_arrays(df_total, array_total,
                                                                                       target_dict_total,
                                                                                       target_list=['TRDC'],
                                                                                       target_col='target')

T cells are usually between 5-10 um diameter, in CosMx this will be between 21px and 43 px. Use KDtrees to find points of TRDC and TRGC that dist less than 43px between each other. We will find all points that have at least 1 neighbouring point in a radius of 43 px. The points that are closed together are then enclosed in a simple circle contour.

The fact that 2 points of TRDC or TRGC are closed together does not mean that we are in the presence of a GD area. We will filter the contours and only consider the ones where there are at least 1 TRDC and 1 TRGC and the sum of counts of TRGC and TRDC are higher than TRAC and TRBC.

As the points for GD are very sparse we take a different approach here.

Instead of counting neighboor points and select contours around it, we will query and find all the points in a 25 radius that have at least one neighboor. We will use it to draw contours around the points as simple circles.

The filtering follows the same strategy as before

%%time 

df_subset_gd = df_total[df_total['target'].isin(target_gd)]
CGD = contours.KDTreeContours(df_subset_gd, contour_name = 'gd', height=height, width = width)
CGD.find_points_with_neighoors(radius = 25, min_neighbours = 1)
CGD.get_contours_around_points_with_neighboors(type_contouring='simple_circle')

CGD.plot_point_clusters_with_contours(show=True, figsize=(5, 5))


# ### Filtering 
CGD.filter_contours_by_gene_comparison(gene_array1 = array_subset_gd, gene_array2 = array_subset_ab,
                                       gene_name1 = "gd", gene_name2 = "ab") # gene 1 > gene2 --> valid contour 
# G>0 D>0
CGD.filter_contours_by_gene_threshold(gene_array = array_subset_d.squeeze(), threshold = 1, gene_name = 'TRDC')# >= 1
CGD.filter_contours_by_gene_threshold(gene_array = np.sum(array_subset_g, axis=-1), threshold = 1, gene_name = 'TRGC1_2')# >= 1

    #### obtain masks
2025-06-18 15:01:00,211 - gridgen.contours.gd - INFO - Initialized GetContour
2025-06-18 15:01:00,221 - gridgen.contours.gd - INFO - Points with more than 1 neighbors: 614
2025-06-18 15:01:00,231 - gridgen.contours.gd - INFO - Points w/ neig agglomerated in DBSCAN labels: 614
2025-06-18 15:01:01,235 - gridgen.contours.gd - INFO - N contours: 242
../_images/bd690c521318a268fd225496500fc5053cbecf43e725c2f35f2e9d0d22325c0b.png
2025-06-18 15:01:04,721 - gridgen.contours.gd - INFO - Excluding contour 31. gd count 1.00 ≤ ab count 2.00
2025-06-18 15:01:07,784 - gridgen.contours.gd - INFO - Excluding contour 85. gd count 1.00 ≤ ab count 2.00
2025-06-18 15:01:09,477 - gridgen.contours.gd - INFO - Excluding contour 115. gd count 1.00 ≤ ab count 2.00
2025-06-18 15:01:10,560 - gridgen.contours.gd - INFO - Excluding contour 134. gd count 1.00 ≤ ab count 1.00
2025-06-18 15:01:10,901 - gridgen.contours.gd - INFO - Excluding contour 140. gd count 0.00 ≤ ab count 0.00
2025-06-18 15:01:11,402 - gridgen.contours.gd - INFO - Excluding contour 149. gd count 2.00 ≤ ab count 2.00
2025-06-18 15:01:14,632 - gridgen.contours.gd - INFO - Excluding contour 206. gd count 1.00 ≤ ab count 1.00
2025-06-18 15:01:14,920 - gridgen.contours.gd - INFO - Excluding contour 211. gd count 1.00 ≤ ab count 1.00
2025-06-18 15:01:16,629 - gridgen.contours.gd - INFO - Contours remaining after gene comparison: 234
2025-06-18 15:01:16,685 - gridgen.contours.gd - INFO - Excluding contour 1. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:16,850 - gridgen.contours.gd - INFO - Excluding contour 7. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:16,906 - gridgen.contours.gd - INFO - Excluding contour 9. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:16,961 - gridgen.contours.gd - INFO - Excluding contour 11. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:16,989 - gridgen.contours.gd - INFO - Excluding contour 12. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:17,018 - gridgen.contours.gd - INFO - Excluding contour 13. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:17,128 - gridgen.contours.gd - INFO - Excluding contour 17. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:17,265 - gridgen.contours.gd - INFO - Excluding contour 22. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:17,293 - gridgen.contours.gd - INFO - Excluding contour 23. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:17,321 - gridgen.contours.gd - INFO - Excluding contour 24. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:17,349 - gridgen.contours.gd - INFO - Excluding contour 25. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:17,514 - gridgen.contours.gd - INFO - Excluding contour 31. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:17,625 - gridgen.contours.gd - INFO - Excluding contour 35. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:17,766 - gridgen.contours.gd - INFO - Excluding contour 40. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:17,796 - gridgen.contours.gd - INFO - Excluding contour 41. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:17,825 - gridgen.contours.gd - INFO - Excluding contour 42. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:17,881 - gridgen.contours.gd - INFO - Excluding contour 44. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:17,994 - gridgen.contours.gd - INFO - Excluding contour 48. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:18,050 - gridgen.contours.gd - INFO - Excluding contour 50. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:18,219 - gridgen.contours.gd - INFO - Excluding contour 56. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:18,303 - gridgen.contours.gd - INFO - Excluding contour 59. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:18,614 - gridgen.contours.gd - INFO - Excluding contour 70. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:18,642 - gridgen.contours.gd - INFO - Excluding contour 71. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:18,728 - gridgen.contours.gd - INFO - Excluding contour 74. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:18,756 - gridgen.contours.gd - INFO - Excluding contour 75. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:18,896 - gridgen.contours.gd - INFO - Excluding contour 80. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:18,925 - gridgen.contours.gd - INFO - Excluding contour 81. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:18,982 - gridgen.contours.gd - INFO - Excluding contour 83. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:19,010 - gridgen.contours.gd - INFO - Excluding contour 84. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:19,038 - gridgen.contours.gd - INFO - Excluding contour 85. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:19,207 - gridgen.contours.gd - INFO - Excluding contour 91. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:19,263 - gridgen.contours.gd - INFO - Excluding contour 93. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:19,318 - gridgen.contours.gd - INFO - Excluding contour 95. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:19,375 - gridgen.contours.gd - INFO - Excluding contour 97. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:19,489 - gridgen.contours.gd - INFO - Excluding contour 101. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:19,546 - gridgen.contours.gd - INFO - Excluding contour 103. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:19,603 - gridgen.contours.gd - INFO - Excluding contour 105. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:19,632 - gridgen.contours.gd - INFO - Excluding contour 106. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:19,661 - gridgen.contours.gd - INFO - Excluding contour 107. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:19,857 - gridgen.contours.gd - INFO - Excluding contour 114. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:19,940 - gridgen.contours.gd - INFO - Excluding contour 117. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:20,023 - gridgen.contours.gd - INFO - Excluding contour 120. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:20,051 - gridgen.contours.gd - INFO - Excluding contour 121. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:20,244 - gridgen.contours.gd - INFO - Excluding contour 128. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:20,327 - gridgen.contours.gd - INFO - Excluding contour 131. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:20,382 - gridgen.contours.gd - INFO - Excluding contour 133. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:20,438 - gridgen.contours.gd - INFO - Excluding contour 135. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:20,521 - gridgen.contours.gd - INFO - Excluding contour 138. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:20,549 - gridgen.contours.gd - INFO - Excluding contour 139. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:20,577 - gridgen.contours.gd - INFO - Excluding contour 140. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:20,632 - gridgen.contours.gd - INFO - Excluding contour 142. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:20,660 - gridgen.contours.gd - INFO - Excluding contour 143. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:20,688 - gridgen.contours.gd - INFO - Excluding contour 144. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:20,716 - gridgen.contours.gd - INFO - Excluding contour 145. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:20,804 - gridgen.contours.gd - INFO - Excluding contour 148. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:20,945 - gridgen.contours.gd - INFO - Excluding contour 153. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:21,170 - gridgen.contours.gd - INFO - Excluding contour 161. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:21,198 - gridgen.contours.gd - INFO - Excluding contour 162. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:21,282 - gridgen.contours.gd - INFO - Excluding contour 165. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:21,425 - gridgen.contours.gd - INFO - Excluding contour 170. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:21,482 - gridgen.contours.gd - INFO - Excluding contour 172. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:21,510 - gridgen.contours.gd - INFO - Excluding contour 173. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:21,539 - gridgen.contours.gd - INFO - Excluding contour 174. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:21,595 - gridgen.contours.gd - INFO - Excluding contour 176. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:21,624 - gridgen.contours.gd - INFO - Excluding contour 177. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:21,681 - gridgen.contours.gd - INFO - Excluding contour 179. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:21,765 - gridgen.contours.gd - INFO - Excluding contour 182. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:21,794 - gridgen.contours.gd - INFO - Excluding contour 183. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:21,851 - gridgen.contours.gd - INFO - Excluding contour 185. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:21,907 - gridgen.contours.gd - INFO - Excluding contour 187. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,020 - gridgen.contours.gd - INFO - Excluding contour 191. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,048 - gridgen.contours.gd - INFO - Excluding contour 192. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,077 - gridgen.contours.gd - INFO - Excluding contour 193. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,217 - gridgen.contours.gd - INFO - Excluding contour 198. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,245 - gridgen.contours.gd - INFO - Excluding contour 199. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,274 - gridgen.contours.gd - INFO - Excluding contour 200. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,302 - gridgen.contours.gd - INFO - Excluding contour 201. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,358 - gridgen.contours.gd - INFO - Excluding contour 203. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,386 - gridgen.contours.gd - INFO - Excluding contour 204. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,530 - gridgen.contours.gd - INFO - Excluding contour 209. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,588 - gridgen.contours.gd - INFO - Excluding contour 211. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,617 - gridgen.contours.gd - INFO - Excluding contour 212. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,674 - gridgen.contours.gd - INFO - Excluding contour 214. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,703 - gridgen.contours.gd - INFO - Excluding contour 215. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,761 - gridgen.contours.gd - INFO - Excluding contour 217. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,845 - gridgen.contours.gd - INFO - Excluding contour 220. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,873 - gridgen.contours.gd - INFO - Excluding contour 221. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,901 - gridgen.contours.gd - INFO - Excluding contour 222. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,956 - gridgen.contours.gd - INFO - Excluding contour 224. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:22,985 - gridgen.contours.gd - INFO - Excluding contour 225. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:23,067 - gridgen.contours.gd - INFO - Excluding contour 228. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:23,095 - gridgen.contours.gd - INFO - Excluding contour 229. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:23,151 - gridgen.contours.gd - INFO - Excluding contour 231. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:23,179 - gridgen.contours.gd - INFO - Excluding contour 232. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:23,207 - gridgen.contours.gd - INFO - Excluding contour 233. Gene TRDC count  0.0 is below threshold 1
2025-06-18 15:01:23,207 - gridgen.contours.gd - INFO - Number of contours remaining: 139
2025-06-18 15:01:23,259 - gridgen.contours.gd - INFO - Excluding contour 0. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,287 - gridgen.contours.gd - INFO - Excluding contour 1. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,315 - gridgen.contours.gd - INFO - Excluding contour 2. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,371 - gridgen.contours.gd - INFO - Excluding contour 4. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,399 - gridgen.contours.gd - INFO - Excluding contour 5. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,483 - gridgen.contours.gd - INFO - Excluding contour 8. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,511 - gridgen.contours.gd - INFO - Excluding contour 9. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,567 - gridgen.contours.gd - INFO - Excluding contour 11. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,596 - gridgen.contours.gd - INFO - Excluding contour 12. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,652 - gridgen.contours.gd - INFO - Excluding contour 14. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,680 - gridgen.contours.gd - INFO - Excluding contour 15. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,708 - gridgen.contours.gd - INFO - Excluding contour 16. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,764 - gridgen.contours.gd - INFO - Excluding contour 18. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,820 - gridgen.contours.gd - INFO - Excluding contour 20. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,848 - gridgen.contours.gd - INFO - Excluding contour 21. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,877 - gridgen.contours.gd - INFO - Excluding contour 22. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,932 - gridgen.contours.gd - INFO - Excluding contour 24. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,961 - gridgen.contours.gd - INFO - Excluding contour 25. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:23,991 - gridgen.contours.gd - INFO - Excluding contour 26. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,050 - gridgen.contours.gd - INFO - Excluding contour 28. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,079 - gridgen.contours.gd - INFO - Excluding contour 29. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,108 - gridgen.contours.gd - INFO - Excluding contour 30. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,137 - gridgen.contours.gd - INFO - Excluding contour 31. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,194 - gridgen.contours.gd - INFO - Excluding contour 33. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,251 - gridgen.contours.gd - INFO - Excluding contour 35. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,280 - gridgen.contours.gd - INFO - Excluding contour 36. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,309 - gridgen.contours.gd - INFO - Excluding contour 37. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,337 - gridgen.contours.gd - INFO - Excluding contour 38. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,366 - gridgen.contours.gd - INFO - Excluding contour 39. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,395 - gridgen.contours.gd - INFO - Excluding contour 40. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,509 - gridgen.contours.gd - INFO - Excluding contour 44. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,537 - gridgen.contours.gd - INFO - Excluding contour 45. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,566 - gridgen.contours.gd - INFO - Excluding contour 46. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,623 - gridgen.contours.gd - INFO - Excluding contour 48. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,652 - gridgen.contours.gd - INFO - Excluding contour 49. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,681 - gridgen.contours.gd - INFO - Excluding contour 50. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,710 - gridgen.contours.gd - INFO - Excluding contour 51. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,739 - gridgen.contours.gd - INFO - Excluding contour 52. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,767 - gridgen.contours.gd - INFO - Excluding contour 53. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,796 - gridgen.contours.gd - INFO - Excluding contour 54. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,826 - gridgen.contours.gd - INFO - Excluding contour 55. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,856 - gridgen.contours.gd - INFO - Excluding contour 56. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,885 - gridgen.contours.gd - INFO - Excluding contour 57. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,914 - gridgen.contours.gd - INFO - Excluding contour 58. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,943 - gridgen.contours.gd - INFO - Excluding contour 59. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:24,972 - gridgen.contours.gd - INFO - Excluding contour 60. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,001 - gridgen.contours.gd - INFO - Excluding contour 61. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,030 - gridgen.contours.gd - INFO - Excluding contour 62. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,058 - gridgen.contours.gd - INFO - Excluding contour 63. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,087 - gridgen.contours.gd - INFO - Excluding contour 64. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,144 - gridgen.contours.gd - INFO - Excluding contour 66. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,173 - gridgen.contours.gd - INFO - Excluding contour 67. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,202 - gridgen.contours.gd - INFO - Excluding contour 68. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,231 - gridgen.contours.gd - INFO - Excluding contour 69. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,259 - gridgen.contours.gd - INFO - Excluding contour 70. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,288 - gridgen.contours.gd - INFO - Excluding contour 71. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,317 - gridgen.contours.gd - INFO - Excluding contour 72. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,346 - gridgen.contours.gd - INFO - Excluding contour 73. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,374 - gridgen.contours.gd - INFO - Excluding contour 74. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,403 - gridgen.contours.gd - INFO - Excluding contour 75. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,432 - gridgen.contours.gd - INFO - Excluding contour 76. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,489 - gridgen.contours.gd - INFO - Excluding contour 78. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,518 - gridgen.contours.gd - INFO - Excluding contour 79. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,546 - gridgen.contours.gd - INFO - Excluding contour 80. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,575 - gridgen.contours.gd - INFO - Excluding contour 81. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,604 - gridgen.contours.gd - INFO - Excluding contour 82. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,632 - gridgen.contours.gd - INFO - Excluding contour 83. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,692 - gridgen.contours.gd - INFO - Excluding contour 85. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,749 - gridgen.contours.gd - INFO - Excluding contour 87. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,778 - gridgen.contours.gd - INFO - Excluding contour 88. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,807 - gridgen.contours.gd - INFO - Excluding contour 89. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,865 - gridgen.contours.gd - INFO - Excluding contour 91. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,894 - gridgen.contours.gd - INFO - Excluding contour 92. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,924 - gridgen.contours.gd - INFO - Excluding contour 93. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,952 - gridgen.contours.gd - INFO - Excluding contour 94. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:25,981 - gridgen.contours.gd - INFO - Excluding contour 95. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,037 - gridgen.contours.gd - INFO - Excluding contour 97. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,066 - gridgen.contours.gd - INFO - Excluding contour 98. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,094 - gridgen.contours.gd - INFO - Excluding contour 99. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,123 - gridgen.contours.gd - INFO - Excluding contour 100. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,151 - gridgen.contours.gd - INFO - Excluding contour 101. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,179 - gridgen.contours.gd - INFO - Excluding contour 102. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,208 - gridgen.contours.gd - INFO - Excluding contour 103. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,236 - gridgen.contours.gd - INFO - Excluding contour 104. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,264 - gridgen.contours.gd - INFO - Excluding contour 105. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,293 - gridgen.contours.gd - INFO - Excluding contour 106. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,348 - gridgen.contours.gd - INFO - Excluding contour 108. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,377 - gridgen.contours.gd - INFO - Excluding contour 109. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,405 - gridgen.contours.gd - INFO - Excluding contour 110. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,433 - gridgen.contours.gd - INFO - Excluding contour 111. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,462 - gridgen.contours.gd - INFO - Excluding contour 112. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,490 - gridgen.contours.gd - INFO - Excluding contour 113. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,518 - gridgen.contours.gd - INFO - Excluding contour 114. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,546 - gridgen.contours.gd - INFO - Excluding contour 115. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,575 - gridgen.contours.gd - INFO - Excluding contour 116. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,603 - gridgen.contours.gd - INFO - Excluding contour 117. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,632 - gridgen.contours.gd - INFO - Excluding contour 118. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,715 - gridgen.contours.gd - INFO - Excluding contour 121. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,744 - gridgen.contours.gd - INFO - Excluding contour 122. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,772 - gridgen.contours.gd - INFO - Excluding contour 123. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,800 - gridgen.contours.gd - INFO - Excluding contour 124. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,829 - gridgen.contours.gd - INFO - Excluding contour 125. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,857 - gridgen.contours.gd - INFO - Excluding contour 126. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,885 - gridgen.contours.gd - INFO - Excluding contour 127. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,914 - gridgen.contours.gd - INFO - Excluding contour 128. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:26,942 - gridgen.contours.gd - INFO - Excluding contour 129. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:27,001 - gridgen.contours.gd - INFO - Excluding contour 131. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:27,031 - gridgen.contours.gd - INFO - Excluding contour 132. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:27,060 - gridgen.contours.gd - INFO - Excluding contour 133. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:27,090 - gridgen.contours.gd - INFO - Excluding contour 134. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:27,118 - gridgen.contours.gd - INFO - Excluding contour 135. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:27,147 - gridgen.contours.gd - INFO - Excluding contour 136. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:27,176 - gridgen.contours.gd - INFO - Excluding contour 137. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:27,205 - gridgen.contours.gd - INFO - Excluding contour 138. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 15:01:27,205 - gridgen.contours.gd - INFO - Number of contours remaining: 25
CPU times: user 33.1 s, sys: 20.1 ms, total: 33.2 s
Wall time: 27.1 s
CGD.plot_contours_scatter(path=None, show=False, s=1, alpha=0.5, linewidth=1,
                           c_points= 'blue',c_contours= 'red',figsize=(8,8))
<Axes: title={'center': 'Scatter with contours and genes gd'}>
../_images/b12651cc3e81a08d4586756f823d2e91a2461c5efb9d637f8f969b572ad02bfd.png

Plot overlay with DAPI image

Lets visualize with DAPI images and the trancripts

dapi_file = [file for file in os.listdir(dapi_folder) if str(fov[3:]) in file][0]
dapi_file_path = os.path.join(dapi_folder, dapi_file)

cell_counts = f"../../cosmx_data/S3/S3/20230628_151317_S3/AnalysisResults/yxyz3r7ufm/{fov}/Run_69de8227-1a7b-40ee-ba46-0ea99a7c59f4_{fov}__complete_code_cell_target_call_coord.csv"
cell_counts_df = pd.read_csv(cell_counts)
target_list = ['TRGC1/TRGC2', 'TRDC','TRBC1/TRBC2', 'TRAC']
filtered_data = cell_counts_df[cell_counts_df['target'].isin(target_list)]
print(filtered_data['target'].value_counts())

img_gd_contour = plot_TRGC_TRDC_points_contours(CGD.contours, filtered_data, dapi_file_path)
target
TRBC1/TRBC2    1598
TRAC            886
TRDC            706
TRGC1/TRGC2     537
Name: count, dtype: int64
<built-in method max of numpy.ndarray object at 0x7facba371bf0>

build the masks and obtain the centroids to plot zoom in sections

GM = get_masks.GetMasks(image_shape=(height, width))

SA = get_masks.SingleClassObjectAnalysis(GM, contours_object=CGD.contours)
SA.get_mask_objects(exclude_masks=None)  # Not excluding any tum or empty 

mask_GD = SA.mask_object_SA

morpho_properties= MorphologyExtractor().extract_per_object_features(labeled_mask= label(mask_GD))
centroids = [(obj['centroid_x'], obj['centroid_y']) for obj in morpho_properties]
centroids = [(float(y), float(x)) for (x, y) in centroids]
2025-06-18 15:11:40,280 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 15:11:40,282 - gridgen.get_masks.GetMasks - INFO - Mask for objects created.

Plot zoomed in

def plot_zoom_boxes_on_image(
    img,
    centroids,
    box_size = 500,
    num_cols = 4,
    show_titles = True,
    draw_boxes = True
):
    half_box = box_size // 2
    img_w, img_h = img.size

    # Show original image with optional boxes
    fig, ax = plt.subplots(figsize=(12, 12))
    ax.imshow(img)
    ax.set_title("Image with Boxes")
    ax.axis('off')

    zoom_coords = []

    for (cx, cy) in centroids:
        left = max(0, int(cx - half_box))
        upper = max(0, int(cy - half_box))
        right = min(left + box_size, img_w)
        lower = min(upper + box_size, img_h)

        # Adjust left and upper if right/lower were clamped
        left = max(0, right - box_size)
        upper = max(0, lower - box_size)

        zoom_coords.append((left, upper, right, lower))

        if draw_boxes:
            rect = patches.Rectangle(
                (left, upper), box_size, box_size,
                linewidth=2, edgecolor='red', facecolor='none', linestyle='dotted'
            )
            ax.add_patch(rect)

    plt.show()

    # Plot zoomed-in crops
    num_coords = len(zoom_coords)
    num_rows = (num_coords + num_cols - 1) // num_cols

    fig, axes = plt.subplots(num_rows, num_cols, figsize=(5 * num_cols, 5 * num_rows))

    # Normalize axes for different shapes
    axes = axes.flatten() if num_coords > 1 else [axes]

    for idx, (left, upper, right, lower) in enumerate(zoom_coords):
        ax = axes[idx]
        cropped_img = img.crop((left, upper, right, lower))
        ax.imshow(cropped_img, origin='lower')
        ax.axis('off')
        if show_titles:
            cx, cy = centroids[idx]
            ax.set_title(f"Zoom {idx + 1} ({cx:.1f}, {cy:.1f})")

    # Hide unused subplots
    for idx in range(num_coords, len(axes)):
        axes[idx].axis('off')

    plt.tight_layout()
    plt.show()
    
  plot_zoom_boxes_on_image(img_gd_contour, centroids, box_size = 500,num_cols = 4,
    show_titles = True, draw_boxes = True)
../_images/f58b6bf894d912edd7346b4231d59e1c66c5df9346c52ab70c7727cfb68c9220.png ../_images/70fb8a8a9073d85cf1f14dc591d81339d5f3be20ba9ee3d2d8c229e97237e1ff.png

Check other FOV

fov = 'FOV002'
file_csv = [file for file in files_cosmx if fov in file][0]  
    
df_total = pd.read_csv(file_csv)
df_total = df_total.rename(columns={'x': 'X', 'y': 'Y'})
df_total = df_total[~df_total['target'].str.contains('System|egative')]

n_genes = len(df_total['target'].unique())
height = int(max(df_total['X'])) + 1
width = int(max(df_total['Y'])) + 1


# this makes the sparse df to an array with the spatial information 
target_dict_total = {target: index for index, target in enumerate(df_total['target'].unique())}
array_total = ga.transform_df_to_array(df = df_total, target_dict=target_dict_total, array_shape = (height, width,len(target_dict_total))).astype(np.int8)

# creating subsets 
df_subset_gd, array_subset_gd, target_indices_subset_gd = ga.get_subset_arrays(df_total, array_total,
                                                                                   target_dict_total,
                                                                                   target_list=target_gd,
                                                                           target_col='target')

df_subset_ab, array_subset_ab, target_indices_subset_ab = ga.get_subset_arrays(df_total, array_total,
                                                                                       target_dict_total,
                                                                                       target_list=target_ab,
                                                                                       target_col='target')
    
    
df_subset_g, array_subset_g, target_indices_subset_g = ga.get_subset_arrays(df_total, array_total,
                                                                                       target_dict_total,
                                                                                       target_list=['TRGC1/TRGC2'],
                                                                                       target_col='target')
    
df_subset_d, array_subset_d, target_indices_subset_d = ga.get_subset_arrays(df_total, array_total,
                                                                                       target_dict_total,
                                                                                       target_list=['TRDC'],
                                                                                       target_col='target')



df_subset_gd = df_total[df_total['target'].isin(target_gd)]
CGD = contours.KDTreeContours(df_subset_gd, contour_name = 'gd', height=height, width = width)
CGD.find_points_with_neighoors(radius = 25, min_neighbours = 1)
CGD.get_contours_around_points_with_neighboors(type_contouring='simple_circle')

# ### Filtering 
CGD.filter_contours_by_gene_comparison(gene_array1 = array_subset_gd, gene_array2 = array_subset_ab,
                                       gene_name1 = "gd", gene_name2 = "ab") # gene 1 > gene2 --> valid contour 
# G>0 D>0
CGD.filter_contours_by_gene_threshold(gene_array = array_subset_d.squeeze(), threshold = 1, gene_name = 'TRDC')# >= 1
CGD.filter_contours_by_gene_threshold(gene_array = np.sum(array_subset_g, axis=-1), threshold = 1, gene_name = 'TRGC1_2')# >= 1



# centroids for GD
GM = get_masks.GetMasks(image_shape=(height, width))

SA = get_masks.SingleClassObjectAnalysis(GM, contours_object=CGD.contours)
SA.get_mask_objects(exclude_masks=None)  # Not excluding any tum or empty 

mask_GD = SA.mask_object_SA

morpho_properties= MorphologyExtractor().extract_per_object_features(labeled_mask= label(mask_GD))
centroids = [(obj['centroid_x'], obj['centroid_y']) for obj in morpho_properties]
centroids = [(float(y), float(x)) for (x, y) in centroids]


# image 


dapi_file = [file for file in os.listdir(dapi_folder) if str(fov[3:]) in file][0]
dapi_file_path = os.path.join(dapi_folder, dapi_file)
segmentation_file_path = f"../../cosmx_data/S3/S3/20230628_151317_S3/CellStatsDir/{fov}/CellLabels_F{fov[3:]}.tif"

target_list = ['TRGC1/TRGC2', 'TRDC','TRBC1/TRBC2', 'TRAC',] #  'CD8A','CD8B','CD4'
dapi_file = [file for file in os.listdir(dapi_folder) if str(fov[3:]) in file][0]
dapi_file_path = os.path.join(dapi_folder, dapi_file)
cell_counts = f"../../cosmx_data/S3/S3/20230628_151317_S3/AnalysisResults/yxyz3r7ufm/{fov}/Run_69de8227-1a7b-40ee-ba46-0ea99a7c59f4_{fov}__complete_code_cell_target_call_coord.csv"
cell_counts_df = pd.read_csv(cell_counts)
filtered_data = cell_counts_df[cell_counts_df['target'].isin(target_list)]
print(filtered_data['target'].value_counts())

img_gd_contour = plot_TRGC_TRDC_points_contours(CGD.contours, filtered_data, dapi_file_path)

    
plot_zoom_boxes_on_image(img_gd_contour, centroids, box_size = 500,num_cols = 4,
    show_titles = True, draw_boxes = True)
2025-06-18 16:43:09,251 - gridgen.contours.gd - INFO - Initialized GetContour
2025-06-18 16:43:09,266 - gridgen.contours.gd - INFO - Points with more than 1 neighbors: 591
2025-06-18 16:43:09,268 - gridgen.contours.gd - INFO - Points w/ neig agglomerated in DBSCAN labels: 591
2025-06-18 16:43:10,321 - gridgen.contours.gd - INFO - N contours: 208
2025-06-18 16:43:11,444 - gridgen.contours.gd - INFO - Excluding contour 10. gd count 2.00 ≤ ab count 2.00
2025-06-18 16:43:11,910 - gridgen.contours.gd - INFO - Excluding contour 17. gd count 1.00 ≤ ab count 1.00
2025-06-18 16:43:14,609 - gridgen.contours.gd - INFO - Excluding contour 58. gd count 2.00 ≤ ab count 2.00
2025-06-18 16:43:16,517 - gridgen.contours.gd - INFO - Excluding contour 87. gd count 0.00 ≤ ab count 0.00
2025-06-18 16:43:16,970 - gridgen.contours.gd - INFO - Excluding contour 94. gd count 2.00 ≤ ab count 2.00
2025-06-18 16:43:17,106 - gridgen.contours.gd - INFO - Excluding contour 96. gd count 0.00 ≤ ab count 0.00
2025-06-18 16:43:18,632 - gridgen.contours.gd - INFO - Excluding contour 119. gd count 1.00 ≤ ab count 1.00
2025-06-18 16:43:18,902 - gridgen.contours.gd - INFO - Excluding contour 123. gd count 2.00 ≤ ab count 2.00
2025-06-18 16:43:20,142 - gridgen.contours.gd - INFO - Excluding contour 142. gd count 2.00 ≤ ab count 4.00
2025-06-18 16:43:20,673 - gridgen.contours.gd - INFO - Excluding contour 150. gd count 2.00 ≤ ab count 2.00
2025-06-18 16:43:22,132 - gridgen.contours.gd - INFO - Excluding contour 172. gd count 0.00 ≤ ab count 0.00
2025-06-18 16:43:23,373 - gridgen.contours.gd - INFO - Excluding contour 191. gd count 1.00 ≤ ab count 2.00
2025-06-18 16:43:24,435 - gridgen.contours.gd - INFO - Contours remaining after gene comparison: 196
2025-06-18 16:43:24,503 - gridgen.contours.gd - INFO - Excluding contour 1. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:24,569 - gridgen.contours.gd - INFO - Excluding contour 3. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:24,739 - gridgen.contours.gd - INFO - Excluding contour 8. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:24,810 - gridgen.contours.gd - INFO - Excluding contour 10. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:24,845 - gridgen.contours.gd - INFO - Excluding contour 11. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:24,879 - gridgen.contours.gd - INFO - Excluding contour 12. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:24,915 - gridgen.contours.gd - INFO - Excluding contour 13. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:24,983 - gridgen.contours.gd - INFO - Excluding contour 15. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:25,017 - gridgen.contours.gd - INFO - Excluding contour 16. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:25,252 - gridgen.contours.gd - INFO - Excluding contour 23. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:25,318 - gridgen.contours.gd - INFO - Excluding contour 25. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:25,651 - gridgen.contours.gd - INFO - Excluding contour 35. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:25,718 - gridgen.contours.gd - INFO - Excluding contour 37. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:25,818 - gridgen.contours.gd - INFO - Excluding contour 40. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:25,885 - gridgen.contours.gd - INFO - Excluding contour 42. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:25,987 - gridgen.contours.gd - INFO - Excluding contour 45. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:26,023 - gridgen.contours.gd - INFO - Excluding contour 46. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:26,229 - gridgen.contours.gd - INFO - Excluding contour 52. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:26,297 - gridgen.contours.gd - INFO - Excluding contour 54. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:26,332 - gridgen.contours.gd - INFO - Excluding contour 55. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:26,366 - gridgen.contours.gd - INFO - Excluding contour 56. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:26,435 - gridgen.contours.gd - INFO - Excluding contour 58. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:26,538 - gridgen.contours.gd - INFO - Excluding contour 61. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:26,607 - gridgen.contours.gd - INFO - Excluding contour 63. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:26,710 - gridgen.contours.gd - INFO - Excluding contour 66. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:27,054 - gridgen.contours.gd - INFO - Excluding contour 76. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:27,088 - gridgen.contours.gd - INFO - Excluding contour 77. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:27,123 - gridgen.contours.gd - INFO - Excluding contour 78. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:27,397 - gridgen.contours.gd - INFO - Excluding contour 86. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:27,499 - gridgen.contours.gd - INFO - Excluding contour 89. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:27,600 - gridgen.contours.gd - INFO - Excluding contour 92. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:27,634 - gridgen.contours.gd - INFO - Excluding contour 93. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:27,670 - gridgen.contours.gd - INFO - Excluding contour 94. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:27,809 - gridgen.contours.gd - INFO - Excluding contour 98. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:27,878 - gridgen.contours.gd - INFO - Excluding contour 100. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:28,218 - gridgen.contours.gd - INFO - Excluding contour 110. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:28,318 - gridgen.contours.gd - INFO - Excluding contour 113. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:28,452 - gridgen.contours.gd - INFO - Excluding contour 117. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:28,618 - gridgen.contours.gd - INFO - Excluding contour 122. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:28,652 - gridgen.contours.gd - INFO - Excluding contour 123. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:28,686 - gridgen.contours.gd - INFO - Excluding contour 124. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:28,853 - gridgen.contours.gd - INFO - Excluding contour 129. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:29,086 - gridgen.contours.gd - INFO - Excluding contour 136. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:29,120 - gridgen.contours.gd - INFO - Excluding contour 137. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:29,220 - gridgen.contours.gd - INFO - Excluding contour 140. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:29,256 - gridgen.contours.gd - INFO - Excluding contour 141. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:29,360 - gridgen.contours.gd - INFO - Excluding contour 144. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:29,430 - gridgen.contours.gd - INFO - Excluding contour 146. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:29,465 - gridgen.contours.gd - INFO - Excluding contour 147. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:29,499 - gridgen.contours.gd - INFO - Excluding contour 148. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:29,534 - gridgen.contours.gd - INFO - Excluding contour 149. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:29,603 - gridgen.contours.gd - INFO - Excluding contour 151. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:29,672 - gridgen.contours.gd - INFO - Excluding contour 153. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:29,741 - gridgen.contours.gd - INFO - Excluding contour 155. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:29,878 - gridgen.contours.gd - INFO - Excluding contour 159. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:29,912 - gridgen.contours.gd - INFO - Excluding contour 160. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:29,947 - gridgen.contours.gd - INFO - Excluding contour 161. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:29,981 - gridgen.contours.gd - INFO - Excluding contour 162. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,085 - gridgen.contours.gd - INFO - Excluding contour 165. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,120 - gridgen.contours.gd - INFO - Excluding contour 166. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,156 - gridgen.contours.gd - INFO - Excluding contour 167. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,293 - gridgen.contours.gd - INFO - Excluding contour 171. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,327 - gridgen.contours.gd - INFO - Excluding contour 172. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,362 - gridgen.contours.gd - INFO - Excluding contour 173. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,396 - gridgen.contours.gd - INFO - Excluding contour 174. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,431 - gridgen.contours.gd - INFO - Excluding contour 175. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,466 - gridgen.contours.gd - INFO - Excluding contour 176. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,534 - gridgen.contours.gd - INFO - Excluding contour 178. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,569 - gridgen.contours.gd - INFO - Excluding contour 179. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,604 - gridgen.contours.gd - INFO - Excluding contour 180. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,639 - gridgen.contours.gd - INFO - Excluding contour 181. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,673 - gridgen.contours.gd - INFO - Excluding contour 182. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,708 - gridgen.contours.gd - INFO - Excluding contour 183. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,742 - gridgen.contours.gd - INFO - Excluding contour 184. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,810 - gridgen.contours.gd - INFO - Excluding contour 186. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,914 - gridgen.contours.gd - INFO - Excluding contour 189. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:30,983 - gridgen.contours.gd - INFO - Excluding contour 191. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:31,052 - gridgen.contours.gd - INFO - Excluding contour 193. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:31,087 - gridgen.contours.gd - INFO - Excluding contour 194. Gene TRDC count  0.0 is below threshold 1
2025-06-18 16:43:31,122 - gridgen.contours.gd - INFO - Number of contours remaining: 117
2025-06-18 16:43:31,253 - gridgen.contours.gd - INFO - Excluding contour 2. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:31,321 - gridgen.contours.gd - INFO - Excluding contour 4. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:31,355 - gridgen.contours.gd - INFO - Excluding contour 5. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:31,388 - gridgen.contours.gd - INFO - Excluding contour 6. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:31,422 - gridgen.contours.gd - INFO - Excluding contour 7. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:31,489 - gridgen.contours.gd - INFO - Excluding contour 9. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:31,522 - gridgen.contours.gd - INFO - Excluding contour 10. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:31,556 - gridgen.contours.gd - INFO - Excluding contour 11. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:31,656 - gridgen.contours.gd - INFO - Excluding contour 14. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:31,689 - gridgen.contours.gd - INFO - Excluding contour 15. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:31,756 - gridgen.contours.gd - INFO - Excluding contour 17. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:31,789 - gridgen.contours.gd - INFO - Excluding contour 18. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:31,823 - gridgen.contours.gd - INFO - Excluding contour 19. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:31,857 - gridgen.contours.gd - INFO - Excluding contour 20. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:31,957 - gridgen.contours.gd - INFO - Excluding contour 23. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,123 - gridgen.contours.gd - INFO - Excluding contour 28. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,189 - gridgen.contours.gd - INFO - Excluding contour 30. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,259 - gridgen.contours.gd - INFO - Excluding contour 32. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,295 - gridgen.contours.gd - INFO - Excluding contour 33. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,329 - gridgen.contours.gd - INFO - Excluding contour 34. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,364 - gridgen.contours.gd - INFO - Excluding contour 35. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,399 - gridgen.contours.gd - INFO - Excluding contour 36. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,435 - gridgen.contours.gd - INFO - Excluding contour 37. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,469 - gridgen.contours.gd - INFO - Excluding contour 38. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,504 - gridgen.contours.gd - INFO - Excluding contour 39. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,572 - gridgen.contours.gd - INFO - Excluding contour 41. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,607 - gridgen.contours.gd - INFO - Excluding contour 42. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,675 - gridgen.contours.gd - INFO - Excluding contour 44. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,744 - gridgen.contours.gd - INFO - Excluding contour 46. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,778 - gridgen.contours.gd - INFO - Excluding contour 47. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,881 - gridgen.contours.gd - INFO - Excluding contour 50. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:32,949 - gridgen.contours.gd - INFO - Excluding contour 52. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,052 - gridgen.contours.gd - INFO - Excluding contour 55. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,155 - gridgen.contours.gd - INFO - Excluding contour 58. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,190 - gridgen.contours.gd - INFO - Excluding contour 59. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,224 - gridgen.contours.gd - INFO - Excluding contour 60. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,259 - gridgen.contours.gd - INFO - Excluding contour 61. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,429 - gridgen.contours.gd - INFO - Excluding contour 66. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,464 - gridgen.contours.gd - INFO - Excluding contour 67. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,532 - gridgen.contours.gd - INFO - Excluding contour 69. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,601 - gridgen.contours.gd - INFO - Excluding contour 71. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,635 - gridgen.contours.gd - INFO - Excluding contour 72. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,669 - gridgen.contours.gd - INFO - Excluding contour 73. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,703 - gridgen.contours.gd - INFO - Excluding contour 74. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,771 - gridgen.contours.gd - INFO - Excluding contour 76. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,805 - gridgen.contours.gd - INFO - Excluding contour 77. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,840 - gridgen.contours.gd - INFO - Excluding contour 78. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,909 - gridgen.contours.gd - INFO - Excluding contour 80. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,944 - gridgen.contours.gd - INFO - Excluding contour 81. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:33,978 - gridgen.contours.gd - INFO - Excluding contour 82. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,047 - gridgen.contours.gd - INFO - Excluding contour 84. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,150 - gridgen.contours.gd - INFO - Excluding contour 87. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,185 - gridgen.contours.gd - INFO - Excluding contour 88. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,219 - gridgen.contours.gd - INFO - Excluding contour 89. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,254 - gridgen.contours.gd - INFO - Excluding contour 90. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,288 - gridgen.contours.gd - INFO - Excluding contour 91. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,321 - gridgen.contours.gd - INFO - Excluding contour 92. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,356 - gridgen.contours.gd - INFO - Excluding contour 93. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,455 - gridgen.contours.gd - INFO - Excluding contour 96. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,588 - gridgen.contours.gd - INFO - Excluding contour 100. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,622 - gridgen.contours.gd - INFO - Excluding contour 101. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,722 - gridgen.contours.gd - INFO - Excluding contour 104. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,756 - gridgen.contours.gd - INFO - Excluding contour 105. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,789 - gridgen.contours.gd - INFO - Excluding contour 106. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,823 - gridgen.contours.gd - INFO - Excluding contour 107. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,856 - gridgen.contours.gd - INFO - Excluding contour 108. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,890 - gridgen.contours.gd - INFO - Excluding contour 109. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,924 - gridgen.contours.gd - INFO - Excluding contour 110. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,958 - gridgen.contours.gd - INFO - Excluding contour 111. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:34,991 - gridgen.contours.gd - INFO - Excluding contour 112. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:35,025 - gridgen.contours.gd - INFO - Excluding contour 113. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:35,058 - gridgen.contours.gd - INFO - Excluding contour 114. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:35,092 - gridgen.contours.gd - INFO - Excluding contour 115. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:35,126 - gridgen.contours.gd - INFO - Excluding contour 116. Gene TRGC1_2 count  0.0 is below threshold 1
2025-06-18 16:43:35,126 - gridgen.contours.gd - INFO - Number of contours remaining: 43
2025-06-18 16:43:35,127 - gridgen.get_masks.GetMasks - INFO - Initialized GetMasks
2025-06-18 16:43:35,128 - gridgen.get_masks.GetMasks - INFO - Mask for objects created.
target
TRBC1/TRBC2    2250
TRAC           1195
TRDC            740
TRGC1/TRGC2     546
Name: count, dtype: int64
<built-in method max of numpy.ndarray object at 0x7fac016f0090>
../_images/d5c3b6cde481591523365ba4e8376a452a55da7379764974dfe8d0309a37532c.png ../_images/5bbb3763efc4a8dde1cfe36c6d485ff0ce414b1b92af23844e1fb85a206338a4.png
# saving
# output_dir = "results/zoom_outputs"
# os.makedirs(output_dir, exist_ok=True)

# for idx, (left, upper, right, lower) in enumerate(zoom_coords):
#     cropped_img = img_gd_contour.crop((left, upper, right, lower))
    
#     # Save as PNG
#     output_path = os.path.join(output_dir, f"6_zoom_{idx + 1}.png")
#     cropped_img.save(output_path)
    
#     # Optionally display with annotation
#     cx, cy = centroids[idx]
#     plt.figure(figsize=(5, 5))
#     plt.imshow(cropped_img, origin='lower')
#     plt.axis('off')
#     plt.tight_layout()
#     plt.close()