Skip to content

Commit 35684e6

Browse files
committed
improvement: use manual centroids in plot_profiles_with_image and batch name from init file
1 parent 3c1ac75 commit 35684e6

File tree

1 file changed

+49
-11
lines changed

1 file changed

+49
-11
lines changed

chromrings/plot/plot_profiles_with_image.py

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
from tqdm import tqdm
66

7+
import pandas as pd
78
import numpy as np
89
import skimage.io
910
import skimage.measure
1011

1112
from chromrings import data_path, figures_path
1213
from chromrings import (
13-
data_info_json_path, utils, USE_ABSOLUTE_DIST
14+
data_info_json_path, utils, USE_ABSOLUTE_DIST, USE_MANUAL_NUCLEOID_CENTERS
1415
)
1516

1617
import matplotlib.pyplot as plt
@@ -41,7 +42,10 @@ def plot(batch_name):
4142
channel_names = batch_info['channel']
4243
batch_path = os.path.join(data_path, *batch_info['folder_path'].split('/')[1:])
4344
pdf_filepath = os.path.join(
44-
figures_path, f'{batch_name}_{EXP_TO_PLOT}_profiles_with_image.pdf'
45+
figures_path,
46+
f'{batch_name}_{EXP_TO_PLOT}_profiles_with_image'
47+
f'_manual_nucleolus_centers_{USE_MANUAL_NUCLEOID_CENTERS}'
48+
'.pdf'
4549
)
4650
pdf = PdfPages(pdf_filepath)
4751
exp_info = {}
@@ -60,12 +64,32 @@ def plot(batch_name):
6064
)
6165
for pos in tqdm(pos_foldernames, ncols=100):
6266
images_path = os.path.join(exp_path, pos, 'Images')
67+
nucleolus_centers_csv_filename = None
68+
lab = None
69+
img = None
6370
for file in utils.listdir(images_path):
6471
file_path = os.path.join(images_path, file)
6572
if file.endswith(f'{channel_name}.tif'):
6673
img = skimage.io.imread(file_path)
6774
elif file.endswith('segm.npz'):
6875
lab = np.load(file_path)['arr_0']
76+
elif file.endswith('nu.csv'):
77+
nucleolus_centers_csv_filename = file
78+
79+
load_manual_centr = (
80+
nucleolus_centers_csv_filename is not None
81+
and USE_MANUAL_NUCLEOID_CENTERS
82+
)
83+
nucleolus_centers_df = None
84+
if load_manual_centr:
85+
nucleolus_centers_csv_filepath = os.path.join(
86+
images_path, nucleolus_centers_csv_filename
87+
)
88+
nucleolus_centers_df = (
89+
pd.read_csv(nucleolus_centers_csv_filepath)
90+
.set_index('Cell_ID')
91+
.dropna(axis=1)
92+
)
6993

7094
img_data_max = img.max()
7195
intensity_image = -img + img_data_max
@@ -80,15 +104,32 @@ def plot(batch_name):
80104
ID = int(re.findall(r'ID_(\d+)_mean_radial_profile', col)[0])
81105
obj = rp[ID]
82106
yc_local, xc_local = obj.centroid_weighted_local[-2:]
83-
if len(obj.weighted_centroid) == 3:
107+
zc = None
108+
if nucleolus_centers_df is not None:
109+
# Center coordinates are provided as input
110+
center_df = nucleolus_centers_df.loc[obj.label]
111+
yc = center_df['y']
112+
xc = center_df['x']
113+
weighted_centroid = (yc, xc)
114+
try:
115+
zc = center_df['z']
116+
weighted_centroid = (zc, yc, xc)
117+
except KeyError as err:
118+
zc = None
119+
elif len(obj.weighted_centroid) == 3:
84120
zc, yc, xc = obj.weighted_centroid
121+
else:
122+
yc, xc = obj.weighted_centroid
123+
124+
if zc is None:
125+
lab_2D = lab_3D
126+
img_2D = img
127+
else:
85128
lab_3D[:] = 0
86129
lab_3D[obj.slice][obj.image] = ID
87130
lab_2D = lab_3D[round(zc)]
88131
img_2D = img[round(zc)]
89-
else:
90-
lab_2D = lab_3D
91-
img_2D = img
132+
92133
obj_2D = skimage.measure.regionprops(lab_2D)[0]
93134
obj_intens_img = img_2D[obj_2D.slice]
94135
if c > 11:
@@ -120,8 +161,5 @@ def plot(batch_name):
120161
pass
121162

122163
if __name__ == '__main__':
123-
batch_names = (
124-
'24h recovery',
125-
)
126-
for batch_name in batch_names:
127-
plot(batch_name)
164+
from chromrings import batch_name
165+
plot(batch_name)

0 commit comments

Comments
 (0)