-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_thresholds.py
183 lines (154 loc) · 7.06 KB
/
check_thresholds.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import napari
from qtpy.QtWidgets import QPushButton, QSlider, QVBoxLayout, QWidget, QLineEdit, QLabel, QComboBox
from qtpy.QtCore import Qt
import easygui
import numpy as np
from skimage.io import imread
import os
import pandas as pd
global global_multiplier
global ch_mean
global ch_sd
global df
global_multiplier = 0
ch_mean = 0
ch_sd = 0
df = pd.DataFrame()
def on_dropdown_change(index):
print(f"Selected channel: {dropdown.itemText(index)}")
global viewer
global df
global ch_mean
global ch_sd
layer_map = {0: 'ch_2', 1: 'ch_3', 2: 'ch_4'}
selected_layer_name = layer_map[index]
for layer in viewer.layers:
layer.visible = (layer.name == selected_layer_name)
if layer.name == selected_layer_name:
# Clear the current selection
viewer.layers.selection.clear()
# Add the selected layer to the selection
viewer.layers.selection.add(layer)
ch_mean = df['bg_mean_' + selected_layer_name][0]
ch_sd = df['bg_sd_' + selected_layer_name][0]
def toggle_positive_nuclei_visibility():
layer = next((layer for layer in viewer.layers if layer.name == "positive_nuclei"), None)
if layer:
layer.visible = not layer.visible
# Dummy function to be connected to the button and slider
def on_load_button_click():
global df
print("Load Button was clicked!")
viewer.layers.clear()
folder_path = easygui.diropenbox(title="Select Folder")
if folder_path is not None:
#get the last child in the path
filename = os.path.basename(os.path.normpath(folder_path))
print(filename)
print(f"Selected folder: {folder_path}")
ch2 = imread(folder_path + os.path.sep + 'channel_2.tif')
ch3 = imread(folder_path + os.path.sep + 'channel_3.tif')
ch4 = imread(folder_path + os.path.sep + 'channel_4.tif')
ch4_layer = viewer.add_image(ch4, name='ch_4', colormap='gray', blending='additive', visible=False, contrast_limits=(np.amin(ch4), np.amax(ch4)))
ch3_layer = viewer.add_image(ch3, name='ch_3', colormap='gray', blending='additive', visible=False, contrast_limits=(np.amin(ch3), np.amax(ch3)))
ch2_layer = viewer.add_image(ch2, name='ch_2', colormap='gray', blending='additive', visible=True, contrast_limits=(np.amin(ch2), np.amax(ch2)))
#get everything except the last two folders from the folder_path
normalized_path = os.path.normpath(folder_path)
path_components = normalized_path.split(os.sep)
path_without_last_two = os.sep.join(path_components[:-2])
print('path without last 2: ' + str(path_without_last_two))
df = pd.read_csv(path_without_last_two + os.path.sep + 'quantification' + os.path.sep + filename + '_thresh.csv')
#load the intensity images from the intensity_images folder
ch2_intensity = imread(path_without_last_two + os.path.sep + 'intensity_images' + os.path.sep + filename + '_ch2.tif')
ch3_intensity = imread(path_without_last_two + os.path.sep + 'intensity_images' + os.path.sep + filename + '_ch3.tif')
ch4_intensity = imread(path_without_last_two + os.path.sep + 'intensity_images' + os.path.sep + filename + '_ch4.tif')
viewer.add_labels(ch4_intensity, name='ch_4_intensity', blending='additive', visible=False)
viewer.add_labels(ch3_intensity, name='ch_3_intensity', blending='additive', visible=False)
viewer.add_labels(ch2_intensity, name='ch_2_intensity', blending='additive', visible=False)
masked_data = np.zeros_like(ch2)
temp_layer_name = "positive_nuclei"
temp_layer = viewer.add_labels(masked_data, name=temp_layer_name, visible=True)
viewer.layers.selection.clear() # Clear any existing selection
viewer.layers.selection.add(ch2_layer) # Select ch2_layer
on_dropdown_change(0)
else:
print("No folder was selected.")
def on_segment_button_click():
global viewer
global global_multiplier
global ch_mean
global ch_sd
# Get the currently active layer
threshold = ch_mean + (global_multiplier * ch_sd)
print('using mean: ' + str(ch_mean))
print('using sd: ' + str(ch_sd))
print('using threshold: ' + str(threshold))
current_layer = viewer.layers.selection.active
if current_layer and current_layer._type_string == 'image':
# Apply threshold
temp_layer_name = "positive_nuclei"
temp_layer = next((layer for layer in viewer.layers if layer.name == temp_layer_name), None)
intensity_layer_name = current_layer.name + "_intensity"
intensity_layer = next((layer for layer in viewer.layers if layer.name == intensity_layer_name), None)
if intensity_layer:
data = intensity_layer.data
masked_data = np.where(data > threshold, 1, 0) # Replace values below threshold with np.nan for transparency
# Update the layer with the masked data
if temp_layer:
# Update the existing 'temp' layer with the new masked data
temp_layer.data = masked_data
temp_layer.visible = True
else:
# Add a new layer called 'temp' with the masked data
temp_layer = viewer.add_labels(masked_data, name=temp_layer_name, visible=True)
else:
print(f"No intensity layer found for {current_layer.name}.")
else:
print("No active image layer selected.")
def on_slider_change(value):
global global_multiplier
real_value = value / 20.0
text_box.setText(f"{real_value:.2f}")
global_multiplier = real_value
# Create a napari viewer
viewer = napari.Viewer()
# Create a QWidget to hold buttons and sliders
widget = QWidget()
layout = QVBoxLayout()
# Create a button and connect it to dummy_function
button = QPushButton("Load an Image")
button.clicked.connect(on_load_button_click)
layout.addWidget(button)
dropdown = QComboBox()
dropdown.addItem("channel 2")
dropdown.addItem("channel 3")
dropdown.addItem("channel 4")
dropdown.currentIndexChanged.connect(on_dropdown_change)
layout.addWidget(dropdown)
label = QLabel("Current SD Multiplier")
layout.addWidget(label) # Add the label to the layout
text_box = QLineEdit()
text_box.setReadOnly(True) # Make the text box read-only
layout.addWidget(text_box)
# Create a slider and connect its value changed signal to on_slider_change function
slider = QSlider(Qt.Horizontal)
slider.setMinimum(0)
slider.setMinimum(0) # 0 * 20 = 0
slider.setMaximum(400) # 10 * 20 = 200
slider.setSingleStep(1) # 0.05 * 20 = 1
slider.setValue(20)
on_slider_change(20)
slider.valueChanged.connect(on_slider_change)
layout.addWidget(slider)
# Create a button and connect it to dummy_function
button = QPushButton("Apply new threshold")
button.clicked.connect(on_segment_button_click)
layout.addWidget(button)
toggle_button = QPushButton("Toggle Positive Nuclei")
toggle_button.clicked.connect(toggle_positive_nuclei_visibility)
layout.addWidget(toggle_button)
# Set the layout on the widget and add it to the viewer
widget.setLayout(layout)
viewer.window.add_dock_widget(widget)
# Start the napari event loop
napari.run()