From 345ceea61c688b6e0a36ec4d467411619b221fad Mon Sep 17 00:00:00 2001 From: Paul Doucet Date: Tue, 5 Nov 2024 11:59:33 +0100 Subject: [PATCH] optimize imports --- src/hest/io/seg_readers.py | 5 ++--- src/hest/readers.py | 2 +- src/hest/segmentation/TissueMask.py | 3 ++- src/hest/segmentation/cell_segmenters.py | 7 ++----- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/hest/io/seg_readers.py b/src/hest/io/seg_readers.py index dcf4094..34119c2 100644 --- a/src/hest/io/seg_readers.py +++ b/src/hest/io/seg_readers.py @@ -1,7 +1,6 @@ from collections import defaultdict -from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed +from concurrent.futures import ProcessPoolExecutor, as_completed import json -import time import warnings from abc import abstractmethod @@ -9,7 +8,6 @@ from loguru import logger import numpy as np import pandas as pd -from matplotlib import pyplot as plt from shapely.geometry.polygon import Point, Polygon from tqdm import tqdm @@ -240,6 +238,7 @@ def write_geojson(gdf: gpd.GeoDataFrame, path: str, category_key: str, extra_pro def generate_colors(names): + from matplotlib import pyplot as plt colors = plt.get_cmap('hsv', len(names)) color_dict = {} for i in range(len(names)): diff --git a/src/hest/readers.py b/src/hest/readers.py index 4ba09e8..9bea5e4 100644 --- a/src/hest/readers.py +++ b/src/hest/readers.py @@ -13,7 +13,6 @@ from hestcore.segmentation import get_path_relative from loguru import logger -from hest.autoalign import autoalign_visium from hest.custom_readers import ADT_to_adata, GSE144239_to_adata, colon_atlas_to_adata, heart_atlas_to_adata from hest.HESTData import (HESTData, STHESTData, VisiumHDHESTData, VisiumHESTData, XeniumHESTData) @@ -457,6 +456,7 @@ def read(self, if (not tissue_position_exists and alignment_file_path is None): print('no tissue_positions_list.csv/tissue_positions.csv or alignment file found') print('attempt fiducial auto alignment...') + from hest.autoalign import autoalign_visium os.makedirs(os.path.join(os.path.dirname(img_path), 'spatial'), exist_ok=True) autoalign_save_dir = None diff --git a/src/hest/segmentation/TissueMask.py b/src/hest/segmentation/TissueMask.py index a9b60d6..8bb54d6 100644 --- a/src/hest/segmentation/TissueMask.py +++ b/src/hest/segmentation/TissueMask.py @@ -1,7 +1,6 @@ import pickle from typing import List -import cv2 import numpy as np from PIL import Image @@ -27,6 +26,8 @@ def load_tissue_mask(pkl_path: str, jpg_path: str, width: int, height: int) -> T with Image.open(jpg_path) as img: tissue_mask = np.array(img).copy() + + import cv2 tissue_mask = cv2.resize(tissue_mask, (width, height)) mask = TissueMask(tissue_mask, contours_tissue, contours_holes) diff --git a/src/hest/segmentation/cell_segmenters.py b/src/hest/segmentation/cell_segmenters.py index 2736798..ca79263 100644 --- a/src/hest/segmentation/cell_segmenters.py +++ b/src/hest/segmentation/cell_segmenters.py @@ -2,17 +2,13 @@ import os import sys -import time import traceback import warnings from abc import abstractmethod -from concurrent.futures import ThreadPoolExecutor, as_completed -from multiprocessing import Pool, cpu_count -from time import sleep +from concurrent.futures import ThreadPoolExecutor from typing import Tuple, Union import geopandas as gpd -import matplotlib.pyplot as plt import numpy as np import openslide import pandas as pd @@ -410,6 +406,7 @@ def expand_nuclei(gdf: gpd.GeoDataFrame, pixel_size: float, exp_um=5, plot=False gdf_cell.geometry = gdf_cell.union(gdf.loc[~invalid_mask]) if plot: + import matplotlib.pyplot as plt logger.info('Plotting...') _, ax = plt.subplots(figsize=(50, 50))