一般我们在进行圈细胞之前需要将基因在空间上的表达情况绘制出来,同时与拍照的TIF图片进行对应,在我的上篇博客里面用了一位师兄的代码,但是却发现其执行效率过于低下,经常一张芯片要运行四五个小时。故自己写了新的架构,将速度提升了数百倍。代码如下所示:


# import spateo as st
import pandas as pd
from PIL import Image
import matplotlib.pyplot as plt
from scipy.sparse import csr_matrix
import numpy as np
import skimage
import cv2

'''
log setting
'''
import logging
log = logging.getLogger('Console')
log.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler() 
console_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
log.addHandler(console_handler)

def generate_geneExpression_png(
    gem_file_path: str, 
    save_path: str):
    '''
    这里的 gem_file_path 指的是你的 gem 文件地址 如 ~/gem/***.gem.gz
    save_path 指的是保存灰度图的文件地址,如 ~/geneExpression/***.png
    '''
    log.info('start read gem file')
    gem_file = pd.read_csv(gem_file_path, sep='\t',comment='#')
    
    x, y = gem_file["x"].values, gem_file["y"].values
    shape = (x.max() + 1,y.max() + 1)
    
    log.info('matrix generating')
    X = csr_matrix((gem_file["MIDCount"].values, (x, y)), shape=shape, dtype=np.uint16)
    mtx = X.todense().astype(np.uint8)
    mtx_ = skimage.color.gray2rgb(np.array(mtx))
    
    log.info('matrix ploting')
    plt.imsave(save_path, mtx_)
    return mtx_