-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
184 lines (155 loc) · 7.35 KB
/
app.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
184
import streamlit as st
import numpy as np
import os
import matplotlib.cm as cm
from utils import load_tiff, adjust_brightness, get_slice, save_tiff_sequence, apply_colormap
# Set page config at top level
st.set_page_config(page_title="axonAtlas", layout="wide")
class OptimizedTiffViewer:
def __init__(self):
self.volumes = {}
self.views = ['XZ', 'XY', 'YZ']
self.colormaps = {
'bone': cm.bone,
'turbo': cm.turbo,
'viridis': cm.viridis,
'plasma': cm.plasma,
'hot': cm.hot,
'cool': cm.cool,
'gray': cm.gray
}
if 'current_view' not in st.session_state:
st.session_state.current_view = 0
if 'rotation_angle' not in st.session_state:
st.session_state.rotation_angle = 0
def SetupPage(self):
st.title("axonAtlas")
def GetOverlaidSlice(self, view_type, index):
combined_slice = None
for vol_id, vol_data in self.volumes.items():
slice_data = get_slice(vol_data['data'], view_type, index)
for _ in range(st.session_state.rotation_angle):
slice_data = np.rot90(slice_data)
adjusted = adjust_brightness(
slice_data,
vol_data['brightness_range'][0],
vol_data['brightness_range'][1],
vol_data['opacity']
)
colored = apply_colormap(adjusted, self.colormaps[vol_data['colormap']])
if combined_slice is None:
combined_slice = colored
else:
combined_slice = np.maximum(combined_slice, colored)
return combined_slice if combined_slice is not None else np.zeros((1,1))
def Run(self):
self.SetupPage()
# Create output area for terminal logs
terminal_output = st.empty()
uploaded_files = st.file_uploader("upload tiffStacks",
type=['tif', 'tiff'],
accept_multiple_files=True
)
if uploaded_files:
for uploaded_file in uploaded_files:
vol_id = uploaded_file.name
if vol_id not in self.volumes:
self.volumes[vol_id] = {
'data': load_tiff(uploaded_file),
'opacity': 1.0,
'colormap': list(self.colormaps.keys())[0]
}
max_z = max(vol['data'].shape[0] for vol in self.volumes.values())
max_y = max(vol['data'].shape[1] for vol in self.volumes.values())
max_x = max(vol['data'].shape[2] for vol in self.volumes.values())
st.sidebar.subheader("view controls")
col1, col2 = st.sidebar.columns(2)
with col1:
if st.button("switch view"):
st.session_state.current_view = (st.session_state.current_view + 1) % 3
with col2:
if st.button("rotate 90°"):
st.session_state.rotation_angle = (st.session_state.rotation_angle + 1) % 4
st.sidebar.subheader("volume controls")
for vol_id in self.volumes:
with st.sidebar.expander(vol_id):
vol_data = self.volumes[vol_id]
vol_data['colormap'] = st.selectbox(
"colormap",
list(self.colormaps.keys()),
key=f"colormap_{vol_id}"
)
data_min = float(vol_data['data'].min())
data_max = float(vol_data['data'].max())
vol_data['brightness_range'] = st.slider(
"brightness",
min_value=data_min,
max_value=data_max,
value=(data_min, data_max),
key=f"brightness_{vol_id}"
)
vol_data['opacity'] = st.slider(
"opacity",
min_value=0.0,
max_value=1.0,
value=1.0,
key=f"opacity_{vol_id}"
)
if st.button("Prepare for TrailMap", key=f"trailmap_{vol_id}"):
output_dir = os.path.join(
os.path.dirname(__file__),
f'trailmap_input_{vol_id.replace(".tif", "")}'
)
save_tiff_sequence(vol_data['data'], output_dir)
st.success(f"saved frames to: {output_dir}")
# TrailMap batch processing button
st.sidebar.subheader("TrailMap processing")
if st.sidebar.button("process All TrailMap Inputs"):
try:
import sys
import subprocess
input_folders = [d for d in os.listdir(os.path.dirname(__file__))
if os.path.isdir(d) and d.startswith('trailmap_input')]
if input_folders:
cmd = [sys.executable, "TRAILMAP/segment_brain_batch.py"] + input_folders
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1,
universal_newlines=True
)
output_text = ""
while True:
output = process.stdout.readline()
if output == '' and process.poll() is not None:
break
if output:
output_text += output
terminal_output.text(output_text)
if process.poll() == 0:
st.sidebar.success("TrailMap processing completed!")
else:
st.sidebar.error("TrailMap processing failed!")
else:
st.sidebar.warning("no TrailMap input folders found")
except Exception as e:
st.sidebar.error(f"failed to run TrailMap: {str(e)}")
current_view = self.views[st.session_state.current_view]
st.subheader(f"current view: {current_view}")
if current_view == 'XY':
slice_idx = st.slider("Z", 0, max_z-1, max_z//2)
elif current_view == 'YZ':
slice_idx = st.slider("X", 0, max_x-1, max_x//2)
else: # XZ
slice_idx = st.slider("Y", 0, max_y-1, max_y//2)
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
st.image(
self.GetOverlaidSlice(current_view, slice_idx),
use_column_width=True
)
if __name__ == "__main__":
viewer = OptimizedTiffViewer()
viewer.Run()