-
Notifications
You must be signed in to change notification settings - Fork 1
/
static_view.py
161 lines (130 loc) · 6.11 KB
/
static_view.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import updated_napari_ui
import pandas as pd
import numpy as np
import imageio
import matplotlib.cm as cm
import sys
csv_path = sys.argv[1]
img_path = sys.argv[2]
p_number = sys.argv[3]
class tissueData():
def __init__(self, cells_data_csv, cells_data_image_path, mapper):
self.cell_data = cells_data_csv
self.image = imageio.imread(cells_data_image_path)
self.mapper_dict = mapper
def create_tagged_image(self, patinet_number, fov = None):
# Load the CSV data
if fov is None:
cell_data_specific_p = self.cell_data[self.cell_data[self.mapper_dict['patints_col']] == patinet_number]
else:
cell_data_specific_p = self.cell_data[(self.cell_data[self.mapper_dict['patints_col']] == patinet_number) \
& (self.cell_data[self.mapper_dict['fov_col']] == fov)]
segmented_image_array = np.array(self.image)
# Create a mapping from cell type to integer
cell_types = cell_data_specific_p[self.mapper_dict['cell_types_col']].unique()
cell_type_to_int = {cell_type: idx + 1 for idx, cell_type in enumerate(cell_types)}
# Create a new image array for cell types
cell_type_image_array = np.zeros_like(segmented_image_array)
# Populate the new image array based on cell type mapping
for _, row in cell_data_specific_p.iterrows():
cell_label = row[self.mapper_dict['cell_index_col']]
cell_type_int = cell_type_to_int[row[self.mapper_dict['cell_types_col']]]
cell_type_image_array[segmented_image_array == cell_label] = cell_type_int
return cell_type_image_array.astype(np.uint8)
def generate_cell_type_structure(self):
"""
Generate a dictionary structure similar to tnbc_cells_type for the provided cell types.
"""
cell_types = self.cell_data[self.mapper_dict['cell_types_col']]
unique_cell_types = np.unique(cell_types)
num_unique_types = len(unique_cell_types)
# Generate a wide range of unique colors using a colormap
colormap = cm.get_cmap('tab20c', num_unique_types)
colors = [colormap(i) for i in range(num_unique_types)]
# Background and Unknown are hardcoded
cell_type_structure = {
#0: {'name': 'Background', 'color': 'black'},
#1: {'name': 'Unknown', 'color': 'black'}
}
# Populate the dictionary with unique cell types and their colors
for i, cell_type in enumerate(unique_cell_types):#, start=2):
cell_type_structure[i] = {'name': cell_type, 'color': colors[i]}
return cell_type_structure
def generate_cell_type_structure_from_tagged(self, mapper):
num_unique_types = len(mapper)
colormap = cm.get_cmap('tab20c', num_unique_types)
colors = [colormap(i) for i in range(num_unique_types)]
# Background and Unknown are hardcoded
cell_type_structure = {
#0: {'name': 'Background', 'color': 'black'},
#1: {'name': 'Unknown', 'color': 'black'}
}
# Populate the dictionary with unique cell types and their colors
for i, cell_type in enumerate(mapper.keys()):#, start=2):
cell_type_structure[cell_type] = {'name': mapper[cell_type], 'color': colors[i]}
return cell_type_structure
def main(self,
patinet_number,
fov,
motif,
is_tagged = False,
list_of_cells_to_exclude = []):
if is_tagged == False:
self.image = self.create_tagged_image(patinet_number, fov)
cell_types_dict = self.generate_cell_type_structure()
else:
cell_types_dict = self.generate_cell_type_structure_from_tagged(is_tagged)
print(cell_types_dict)
self.image_original, self.G_full, self.coords_full, self.point2cell_full, p2c = updated_napari_ui.build_graph(self.image, list_of_cells_to_exclude)
motifs = updated_napari_ui.find_motifs(self.G_full, motif = motif)
fig = updated_napari_ui.vis_graph_and_motifs(coords_full = self.coords_full,
subgraphs_8_list = motifs,
image_original = self.image_original,
G_full = self.G_full,
point2cell_full = self.point2cell_full,
cell_types=cell_types_dict)
#########################################################################################################
#### params specific for the dataset
# mapping col names
mapper = {'patints_col' : 'SampleID',
'cell_types_col' : 'CellType',
'cell_index_col' : 'cellLabelInImage',
'fov_col' : 'FOV'}
# the motif we want to find
motif = {
('A', 'B'): {},
('A', 'C'): {},
('B', 'C'): {},
'A': {'type': 9},
'B': {'type': 10},
'C': {'type': 10}
}
# if the image is tagged, provide the index:cell_type mapper
mapper_cells = {
1: 'Unidentified',
2: 'Endothelial',
3: 'Mesenchyme',
4: 'Tumor',
5: 'Tregs',
6: 'CD4 t cells',
7: 'CD8 T cells',
8: 'CD3 T cells',
9: 'NK cells',
10: 'B cells',
11: 'Neutrophils',
12: 'Macrophages',
13: 'DC',
14: 'DC/Mono',
15: 'Mono/Neu',
16: 'Immune other'
}
cells_data = pd.read_csv(csv_path)
visualizer = tissueData(cells_data,
img_path,
mapper)
visualizer.main(patinet_number=p_number,
fov=None,
motif=motif,
is_tagged=mapper_cells,
list_of_cells_to_exclude=[i for i in range(1,17)]
)