From 1da66de3a866a2f98aad041a58e30a5b3230c66d Mon Sep 17 00:00:00 2001 From: Jennifer Eng Date: Wed, 24 Nov 2021 12:53:30 -0800 Subject: [PATCH] remove old python files --- 20200812_Segment_Quenching.py | 189 ---------------------------------- 20200902_Annotate_napari.py | 121 ---------------------- 20200911_AF_napari.py | 48 --------- 3 files changed, 358 deletions(-) delete mode 100755 20200812_Segment_Quenching.py delete mode 100755 20200902_Annotate_napari.py delete mode 100755 20200911_AF_napari.py diff --git a/20200812_Segment_Quenching.py b/20200812_Segment_Quenching.py deleted file mode 100755 index 79686b3..0000000 --- a/20200812_Segment_Quenching.py +++ /dev/null @@ -1,189 +0,0 @@ -# image processing for with mlpex_image -# date: 2020-07-21 -# author: engje -# language Python 3.7 -# license: GPL>=v3 - -#libraries - -import os -import sys -import numpy as np -import pandas as pd -import shutil -import matplotlib.pyplot as plt - -# cd /home/groups/graylab_share/OMERO.rdsStore/engje/Data/cycIF_ValidationStudies/cycIF_Validation/Images/tiff/4165NPanc - -#### Paths #### - -codedir = os.getcwd() -#czidir = codedir.replace('_Workflow','_Images') -#tiffdir = f'{codedir}/RawImages' -#qcdir = f'{codedir}/QC' -regdir = f'{codedir}/Images/tiff/4165NPanc' -subdir = f'{regdir}/SubtractedRegisteredImages' -segdir = f'{regdir}/Segmentation' -qcdir = f'{regdir}/QC' -cropdir = f'{regdir}/Cropped' - -# Start Preprocessing - -from mplex_image import preprocess, mpimage, cmif -preprocess.cmif_mkdir([segdir,subdir,qcdir,cropdir]) #tiffdir,,regdir, - -ls_sample = [ - #'4165NPanc-73', - #'4165NPanc-75', - #'4165NPanc-77', - #'4165NPanc-74', - #'4165NPanc-76', - '4165NPanc-78', - ] - -#### 2 Tiffs: check/change names #### -''' -for s_sample in ls_sample: - os.chdir(f'{subdir}/{s_sample}') - #Example: change file name and change back - d_rename = {'R1_blank.blank.blank.blank':'R1_R1c2.R1c3.R1c4.R1c5', - 'R2_blank.blank.blank.blank':'R2_R2c2.R2c3.R2c4.R2c5', - 'R3_blank.blank.blank.blank':'R3_R3c2.R3c3.R3c4.R3c5', - 'R4_blank.blank.blank.blank':'R4_R4c2.R4c3.R4c4.R4c5', - 'R5_blank.blank.blank.blank':'R5_R5c2.R5c3.R5c4.R5c5', - 'R6_blank.blank.blank.blank':'R6_R6c2.R6c3.R6c4.R6c5'} - preprocess.dchange_fname(d_rename)#,b_test=False) - #sort and count images - df_img = mpimage.parse_org() - #cmif.count_images(df_img) -''' - -### 7 move images and prep for seg ### -''' -for s_sample in ls_sample: - preprocess.cmif_mkdir([f'{subdir}/{s_sample}']) - -''' -#### 8 cell pose segmentation & feat extraction -''' -from mplex_image import segment -import time - -nuc_diam = 30 -cell_diam = 30 - -s_seg_markers= "['CK7-Vimentin']"#"['Ecad']" #"['blank']" ### -s_type = 'cell'#'nuclei'# - - -print(f'Predicting {s_type}') -for s_sample in ls_sample: - preprocess.cmif_mkdir([f'{segdir}/{s_sample}Cellpose_Segmentation']) - #os.chdir(f'{subdir}') - #os.chdir(f'{subdir}') - #for s_file in os.listdir(): - # if s_file.find(s_sample) > -1: - # os.chdir(f'{subdir}/{s_file}') - # print(f'Processing {s_file}') - os.chdir(regdir) - df_img = segment.parse_org() - for s_scene in sorted(set(df_img.scene)): - s_slide_scene= f'{s_sample}-Scene-{s_scene}' - s_find = df_img[(df_img.rounds=='R1') & (df_img.color=='c1') & (df_img.scene==s_scene)].index[0] - segment.cellpose_segment_job(s_sample,s_slide_scene, - s_find,f'{segdir}/{s_sample}Cellpose_Segmentation', - f'{regdir}',nuc_diam,cell_diam, - s_type,s_seg_markers,b_long=True)#,b_match=True) #b_long=True) os.chdir(f'{segdir}/{s_sample}Cellpose_Segmentation') - os.system(f'sbatch cellpose_{s_type}_{s_slide_scene}.sh') - time.sleep(9) - print('Next') -''' - -#### Extract Cellpose Features ####### -''' -from mplex_image import features - -nuc_diam = 30 -cell_diam = 30 -ls_seg_markers = ['Ecad']#['blank']# - -for s_sample in ls_sample: - df_sample, df_thresh = features.extract_cellpose_features(s_sample, segdir, subdir, ls_seg_markers, nuc_diam, cell_diam) - df_sample.to_csv(f'{segdir}/features_{s_sample}_MeanIntensity_Centroid_Shape.csv') - df_thresh.to_csv(f'{segdir}thresh_{s_sample}_ThresholdLi.csv') -''' -#### 9 filter cellpose data -''' -from mplex_image import process -#parameters -ls_filter = ['DAPI2_nuclei','DAPI6_nuclei'] -ls_marker_cyto = [] -ls_custom = [] -s_thresh= 'Ecad' - -for s_sample in ls_sample: - #filtering - os.chdir(segdir) - df_img_all = process.load_li([s_sample]) - #dapi filter too high - #df_img_all.loc[df_img_all[df_img_all.marker.str.contains('DAPI')].index,'threshold_li'] = 500 - df_mi_full = process.load_cellpose_df([s_sample], segdir) - df_xy = process.filter_cellpose_xy(df_mi_full) - df_mi_filled = process.fill_cellpose_nas(df_img_all,df_mi_full,ls_marker_cyto,s_thresh=s_thresh,ls_celline=[],ls_shrunk = [],qcdir=qcdir) - df_mi = process.filter_loc_cellpose(df_mi_filled, ls_marker_cyto, ls_custom) - df_pos_auto,d_thresh_record = process.auto_threshold(df_mi,df_img_all) - ls_color = df_mi.columns[df_mi.columns.str.contains('DAPI')].tolist() - process.positive_scatterplots(df_pos_auto,d_thresh_record,df_xy,ls_color,qcdir) #+ [f'{s_thresh}_cytoplasm'] - df_mi_filter = process.filter_dapi_cellpose(df_pos_auto,ls_color,df_mi,ls_filter,df_img_all,qcdir) - df_mi_filter.to_csv(f'{segdir}/features_{s_sample}_FilteredMeanIntensity_{"_".join([item.split("_")[0] for item in ls_filter])}.csv') - df_xy.to_csv(f'{segdir}/features_{s_sample}_CentroidXY.csv') - -#Expand nuclei without matching cell labels for cells' touching analysis -#for s_sample in ls_sample: -# se_neg = df_mi_full[df_mi_full.slide==s_sample].Ecad_negative -# labels,combine,dd_result = features.combine_labels(s_sample, segdir, subdir, ls_seg_markers, nuc_diam, cell_diam, se_neg) - -''' - -d_crop = { - '4165NPanc-74-Scene-001': (6000,3000), - '4165NPanc-76-Scene-001': (6000,3000), - '4165NPanc-78-Scene-001': (6000,3000), - } - -d_crop = { - #'4165NPanc-74-Scene-001': (2000,10000), - #'4165NPanc-76-Scene-001': (2000,10000), - '4165NPanc-78-Scene-001': (2000,10000), - } -tu_dim=(2000,2000) - -#10 -2 ome-tiff -import tifffile -#ome-tiff parameters -s_dapi = 'DAPI1' -d_combos = {#'AF':{'R0c2','R0c3','R0c4','R0c5'}, - 'Background':{'R1c2','R2c2','R3c2','R4c2','R5c2','R1c3','R1c4'}, - } -for s_sample in ls_sample: - os.chdir(f'{subdir}/{s_sample}') - df_img = mpimage.parse_org() - for s_index in df_img.index: - s_marker = df_img.loc[s_index,'marker'] - if s_marker == 'DAPI': - s_marker = s_marker + f'{df_img.loc[s_index,"rounds"].split("R")[1]}' - dd_result = mpimage.overlay_crop(d_combos,d_crop,df_img,s_dapi,tu_dim) - for s_crop, d_result in dd_result.items(): - for s_type, (ls_marker, array) in d_result.items(): - new_array = array[np.newaxis,np.newaxis,:] - s_xml = mpimage.gen_xml(new_array, ls_marker) - with tifffile.TiffWriter(f'{cropdir}/{s_crop}_{s_type}.ome.tif') as tif: - tif.save(new_array, photometric = "minisblack", description=s_xml, metadata = None) - -#10-3 crop basins to match cropped overlays - -#cmif.crop_labels(d_crop,tu_dim,segdir,cropdir,s_find='exp5_CellSegmentationBasins') -#cmif.crop_labels(d_crop,tu_dim,segdir,cropdir,s_find='Nuclei Segmentation Basins') - -os.chdir(codedir) - diff --git a/20200902_Annotate_napari.py b/20200902_Annotate_napari.py deleted file mode 100755 index c71b267..0000000 --- a/20200902_Annotate_napari.py +++ /dev/null @@ -1,121 +0,0 @@ -# 2020-05-05 Need to threshold with Napari! -# author engje -# ipython --gui=qt -# %run 20200902_Annotate_napari.pyrun 20200504_JPTMAs_napari.py -import napari -import os -import skimage -from skimage import io -import numpy as np -import copy -import pandas as pd -import tifffile -import json -from mplex_image import visualize as viz -from mplex_image import process - -#paths -codedir = 'C:\\Users\\engje\\Documents\\Data\\PipelineExample' -s_slide = 'BC44290-146-Scene-1' -regdir = f'{codedir}\\44290-146_Cropped' - - -#load positive and intensity data -os.chdir(codedir) -df_mi = pd.read_csv(f'features_BC44290-146_FilteredMeanIntensity_DAPI8Q_DAPI2.csv',index_col=0) -df_pos = pd.read_csv(f'20200911_BC44290-146_PositiveNegative.csv',index_col=0) -df_af =df_pos.loc[:,df_pos.columns.str.contains('FalseNegative') | df_pos.columns.str.contains('AF')] -df_af.rename({'AF':'AF_'},axis=1,inplace=True) - -#load images -os.chdir(regdir) -ls_crop = ['x1800y9000'] -ls_crop = ['x2000y5000'] -ls_crop = ['x2000y7000'] - -viewer = napari.Viewer() -label_image = viz.load_crops(viewer,ls_crop) - - -#show positive results -ls_cell = df_af.columns.tolist() - -for s_cell in ls_cell: - label_image_cell = viz.pos_label(viewer,df_af,label_image,s_cell) - - -#true negatives (R8Q false positives) - -d_result = { #['x2000y7000'] - 'CD4R':[[1381.18178064, 1167.52534939], - [ 571.87259166, 14.32131056]], - 'CD68':[[1619.57331007, 1051.80155136], - [1447.56806507, 1819.63925294], - [1283.27001693, 1564.30065155], - [ 369.84348738, 1597.61376619]], - 'CD8R':[[ 951.82290682, 90.3360093 ], - [1196.48355621, 1855.65557084]], - 'CD31':[[ 82.14010145, 1845.35591055]], - 'CD4':[[1563.24442559, 10.30673509]], - } - -d_result = { #['x2000y5000'] - 'CD68':[[1407.54191671, 1983.84245846]], - #'PDPN':[[]], - #'aSMA':[[]], - #'PCNA':[[]], - 'ER':[[619.82496889, 40.60634881]], - #'CD8':[[]], - 'PD1':[[1033.48474961, 31.36584275], - [1107.61847063, 27.48110627], - [1325.4254353 , 1828.14697367], - [1594.8071995 , 1885.21599927]], - 'CD4':[[1249.57500705, 46.35869932], - [1965.13887629, 14.71219476]], - } - -#drop lamB1 and Vim -d_result = { #['x1800y9000'] 2000X2000 pixels - #'AR':[[]], - #'CD4R': [[]], - 'CD8R':[[1097.51591281, 1464.731752], - [1726.59810008, 1198.07145173], - [1633.68815278, 1331.44740395], - [1783.57423501, 1476.80129363]], - 'CD31':[[1073.83394211, 1584.78914659]], - #'PDPN':[[]], - #'CK17':[[]], - #'Ki67':[[]], - 'CD68':[[ 601.54176882, 1898.32020524], - [ 628.38177528, 1898.95925302], - [1588.01111769, 1228.85081276], - [1477.62764205, 1489.64693652]], - #'aSMA':[[]], - #'ER':[[]], - #'HER2':[[]], - #'PCNA':[[]], - 'CD4':[[924.8928818 , 307.04451231]], - #'PD1':[[]], - #'CK7':[[]], - #'CD44':[[]], - #'CK14':[[]],'pHH3':[[]], - 'CD8':[[1865.69698867, 1258.66083733], - [1950.51373498, 1197.15250985]], - } - -with open(f'TrueNegatives_44290-146_scaledAF_{ls_crop[0]}.json','w') as f: - json.dump(d_result, f) - -d_cell_result = {} -for s_marker, points in d_result.items(): - ls_points = [] - for point in points: - point = np.array(point).astype('int') - i_cell = label_image[point[0]][point[1]] - ls_points.append(str(i_cell)) - d_cell_result.update({s_marker:ls_points}) - -with open(f'TrueNegatives_CellIDs_44290-146_scaledAF_{ls_crop[0]}.json','w') as f: - json.dump(d_cell_result, f) - -os.chdir(codedir) \ No newline at end of file diff --git a/20200911_AF_napari.py b/20200911_AF_napari.py deleted file mode 100755 index 4df46e4..0000000 --- a/20200911_AF_napari.py +++ /dev/null @@ -1,48 +0,0 @@ -# 2020-05-05 Need to threshold with Napari! -# author engje -# ipython --gui=qt -#%run 20200504_JPTMAs_napari.py -import napari -import os -import skimage -from skimage import io -import numpy as np -import copy -import pandas as pd -import tifffile - -#paths -codedir = os.getcwd() -s_slide = '4165NPanc-74-Scene-001' -regdir = f'{codedir}\\Images\\tiff\\4165NPanc\\Cropped' - -#os.chdir('..') -from mplex_image import visualize as viz -from mplex_image import process, analyze - -#load positive and intensity data -os.chdir(f'{codedir}\\Data') -df_pos = pd.read_csv(f'4165NPanc_AF_norm_leiden.csv',index_col=0) -df_pos = analyze.celltype_to_bool(df_pos, 'leiden') -df_pos.columns = [str(item) for item in df_pos.columns] - -#load images -os.chdir(regdir) -s_crop = 'x2000y10000' # -#s_crop = 'x2000y7000' -#s_crop = 'x6000y3000' -viewer = napari.Viewer() -label_image = viz.load_crops(viewer,s_crop,s_slide) - -#show positive results -ls_cell = df_pos.columns.tolist() -# positive in scene -df_pos['tissue'] = [item.split('_')[0] for item in df_pos.index] -df_scene = df_pos[df_pos.tissue==s_slide.split('-Scene')[0]] - -for s_cell in ls_cell: - label_image_cell = viz.pos_label(viewer,df_scene,label_image,s_cell) - -os.chdir(codedir) - -