Spaces:
Sleeping
Sleeping
Updated app.py
Browse files
app.py
CHANGED
|
@@ -184,16 +184,32 @@ def run_3D_registration(user_section, ):
|
|
| 184 |
moving=affined_fixed_atlas,
|
| 185 |
transformlist=mytx_non_rigid['fwdtransforms'],
|
| 186 |
interpolator='nearestNeighbor')
|
| 187 |
-
gallery_images = load_gallery_images()
|
| 188 |
transformed_images = []
|
| 189 |
if not(os.path.exists("Overlaped_registered")):
|
| 190 |
os.mkdir("Overlaped_registered")
|
| 191 |
-
registered = nonrigid_fixed_atlas.numpy()/255
|
| 192 |
-
for id in list(range((registered.shape[0]//2)-15, (registered.shape[0]//2)+15, 2)):
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
return transformed_images
|
| 198 |
|
| 199 |
|
|
@@ -219,9 +235,11 @@ def run_2D_registration(user_section, slice_idx):
|
|
| 219 |
mytx = affine_reg(fixed_image,moving_image)
|
| 220 |
mytx_non_rigid = nonrigid_reg(fixed_image,mytx)
|
| 221 |
gallery_imgs = natsorted(load_gallery_images())
|
| 222 |
-
|
|
|
|
|
|
|
| 223 |
affined_fixed_atlas = ants.apply_transforms(fixed=fixed_image,
|
| 224 |
-
moving=
|
| 225 |
transformlist=mytx['fwdtransforms'],
|
| 226 |
interpolator='nearestNeighbor')
|
| 227 |
nonrigid_fixed_atlas = ants.apply_transforms(fixed=fixed_image,
|
|
@@ -232,10 +250,29 @@ def run_2D_registration(user_section, slice_idx):
|
|
| 232 |
transformed_images = []
|
| 233 |
if not(os.path.exists("Overlaped_registered")):
|
| 234 |
os.mkdir("Overlaped_registered")
|
| 235 |
-
|
|
|
|
|
|
|
| 236 |
|
| 237 |
-
return ['Overlaped_registered/
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
def embeddings_classifier(user_section, atlas_embeddings,atlas_labels):
|
| 241 |
class SliceEncoder(nn.Module):
|
|
@@ -307,6 +344,8 @@ def atlas_slice_prediction(user_section, axis = 'coronal'):
|
|
| 307 |
|
| 308 |
|
| 309 |
|
|
|
|
|
|
|
| 310 |
example_files = [
|
| 311 |
["./resampled_green_25.nii.gz", "CCF registered Sample", "3D"],
|
| 312 |
["./Brain_1.png", "Custom Sample", "2D"],
|
|
@@ -467,6 +506,9 @@ def handle_data_type_change(dt):
|
|
| 467 |
return gr.update(visible=True, minimum=0, maximum=len(actual_ids)-1, value=len(actual_ids)//2)
|
| 468 |
|
| 469 |
def on_select(evt: gr.SelectData):
|
|
|
|
|
|
|
|
|
|
| 470 |
gallery_selected_data = evt.index
|
| 471 |
|
| 472 |
gallery_images = natsorted(load_gallery_images())
|
|
@@ -474,7 +516,7 @@ with gr.Blocks() as demo:
|
|
| 474 |
gr.Markdown("# Map My Sections\n### This GUI is part of the submission to the Allen Institute's Map My Sections tool by Tibbling Technologies.")
|
| 475 |
|
| 476 |
with gr.Row():
|
| 477 |
-
gr.Markdown("### Step 1: Upload your sample, currently only .nii.gz and .png supported")
|
| 478 |
gr.Markdown("### Step 2: Select your sample and data type.")
|
| 479 |
with gr.Row():
|
| 480 |
nifti_file = gr.File(label="File Upload")
|
|
@@ -492,10 +534,10 @@ with gr.Blocks() as demo:
|
|
| 492 |
with gr.Column(scale=1):
|
| 493 |
gr.Markdown("### Step 4: Visualizing Allen Brain Cell Types Atlas")
|
| 494 |
gallery = gr.Gallery(label="ABC Atlas", value = gallery_images,columns = 5, height = 450)
|
| 495 |
-
gr.Markdown("### Step 5: Run cell type
|
| 496 |
with gr.Row():
|
| 497 |
-
run_button = gr.Button("
|
| 498 |
-
reg_button = gr.Button("Run Registration", visible=False)
|
| 499 |
|
| 500 |
with gr.Column(visible=False) as plot_row:
|
| 501 |
gr.Markdown("### Step 6: Quantitative results of the mapping model.")
|
|
@@ -522,7 +564,7 @@ with gr.Blocks() as demo:
|
|
| 522 |
outputs=slice_slider
|
| 523 |
)
|
| 524 |
|
| 525 |
-
gallery.select(on_select, None, None)
|
| 526 |
|
| 527 |
slice_slider.change(update_slice, inputs=slice_slider, outputs=[image_display, prob_plot, gallery])
|
| 528 |
run_button.click(run_mapping, outputs=[prob_plot, plot_row, download_button, download_step])
|
|
|
|
| 184 |
moving=affined_fixed_atlas,
|
| 185 |
transformlist=mytx_non_rigid['fwdtransforms'],
|
| 186 |
interpolator='nearestNeighbor')
|
| 187 |
+
gallery_images = natsorted(load_gallery_images())
|
| 188 |
transformed_images = []
|
| 189 |
if not(os.path.exists("Overlaped_registered")):
|
| 190 |
os.mkdir("Overlaped_registered")
|
| 191 |
+
# registered = nonrigid_fixed_atlas.numpy()/255
|
| 192 |
+
# for id in list(range((registered.shape[0]//2)-15, (registered.shape[0]//2)+15, 2)):
|
| 193 |
+
# print(id)
|
| 194 |
+
# plt.imsave(f'Overlaped_registered/{id}.png',registered[id, :, :], cmap = 'gray' )
|
| 195 |
+
# transformed_images.append(f'Overlaped_registered/{id}.png')
|
| 196 |
+
for i in range(len(gallery_images)-10):
|
| 197 |
+
im = plt.imread(gallery_images[i])
|
| 198 |
+
fname = os.path.split(gallery_images[i])[-1]
|
| 199 |
+
moving_image_slice = ants.from_numpy(square_padding(gray_scale(im)))
|
| 200 |
+
affined_fixed_atlas = ants.apply_transforms(fixed=fixed_image,
|
| 201 |
+
moving=moving_image,
|
| 202 |
+
transformlist=mytx['fwdtransforms'],
|
| 203 |
+
interpolator='nearestNeighbor')
|
| 204 |
+
nonrigid_fixed_atlas = ants.apply_transforms(fixed=fixed_image,
|
| 205 |
+
moving=affined_fixed_atlas,
|
| 206 |
+
transformlist=mytx_non_rigid['fwdtransforms'],
|
| 207 |
+
interpolator='nearestNeighbor')
|
| 208 |
+
# print(im.shape, nonrigid_fixed_atlas.numpy().shape)
|
| 209 |
+
reconverted_img = reconvert_to_rgb(im[:,:,:3], nonrigid_fixed_atlas.numpy()[i,:,:])
|
| 210 |
+
plt.imsave(f'Overlaped_registered/{fname}',(reconverted_img * 255).astype(np.uint8))
|
| 211 |
+
transformed_images.append(f'Overlaped_registered/{fname}')
|
| 212 |
+
transformed_images = natsorted(load_gallery_images())
|
| 213 |
return transformed_images
|
| 214 |
|
| 215 |
|
|
|
|
| 235 |
mytx = affine_reg(fixed_image,moving_image)
|
| 236 |
mytx_non_rigid = nonrigid_reg(fixed_image,mytx)
|
| 237 |
gallery_imgs = natsorted(load_gallery_images())
|
| 238 |
+
im = plt.imread(gallery_imgs[gallery_selected_data])
|
| 239 |
+
print(im.shape)
|
| 240 |
+
moving_gallery_img = ants.from_numpy(square_padding(gray_scale(im)))
|
| 241 |
affined_fixed_atlas = ants.apply_transforms(fixed=fixed_image,
|
| 242 |
+
moving=moving_gallery_img,
|
| 243 |
transformlist=mytx['fwdtransforms'],
|
| 244 |
interpolator='nearestNeighbor')
|
| 245 |
nonrigid_fixed_atlas = ants.apply_transforms(fixed=fixed_image,
|
|
|
|
| 250 |
transformed_images = []
|
| 251 |
if not(os.path.exists("Overlaped_registered")):
|
| 252 |
os.mkdir("Overlaped_registered")
|
| 253 |
+
print("Reconverting Image")
|
| 254 |
+
reconverted_img = reconvert_to_rgb(im[:,:,:3], nonrigid_fixed_atlas.numpy())
|
| 255 |
+
plt.imsave(f'Overlaped_registered/registered_slice_reconverted_1.png',(reconverted_img * 255).astype(np.uint8))
|
| 256 |
|
| 257 |
+
return ['Overlaped_registered/registered_slice_reconverted_1.png']
|
| 258 |
|
| 259 |
+
def reconvert_to_rgb(img_rgb, img_gray_processed):
|
| 260 |
+
|
| 261 |
+
# 3. Resize original RGB to match processed grayscale shape
|
| 262 |
+
original_shape = img_gray_processed.shape
|
| 263 |
+
img_rgb_resized = resize(img_rgb, (original_shape[0], original_shape[1]), preserve_range=True)
|
| 264 |
+
|
| 265 |
+
# 4. Convert resized RGB to grayscale
|
| 266 |
+
gray_resized = np.mean(img_rgb_resized, axis=2) + 1e-8 # avoid divide-by-zero
|
| 267 |
+
|
| 268 |
+
# 5. Compute ratio of new_gray / old_gray, apply to RGB channels
|
| 269 |
+
ratio = img_gray_processed / gray_resized
|
| 270 |
+
img_recolored = img_rgb_resized * ratio[..., np.newaxis]
|
| 271 |
+
|
| 272 |
+
# 6. Clip values to [0, 1] if image is in float format (common for imread)
|
| 273 |
+
# img_recolored = np.clip(img_recolored, 0, 1)
|
| 274 |
+
|
| 275 |
+
return img_recolored
|
| 276 |
|
| 277 |
def embeddings_classifier(user_section, atlas_embeddings,atlas_labels):
|
| 278 |
class SliceEncoder(nn.Module):
|
|
|
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
+
|
| 348 |
+
|
| 349 |
example_files = [
|
| 350 |
["./resampled_green_25.nii.gz", "CCF registered Sample", "3D"],
|
| 351 |
["./Brain_1.png", "Custom Sample", "2D"],
|
|
|
|
| 506 |
return gr.update(visible=True, minimum=0, maximum=len(actual_ids)-1, value=len(actual_ids)//2)
|
| 507 |
|
| 508 |
def on_select(evt: gr.SelectData):
|
| 509 |
+
print("Selected index:", evt)
|
| 510 |
+
print("Selected value:", evt.value)
|
| 511 |
+
print("Selected coordinates:", evt.selected)
|
| 512 |
gallery_selected_data = evt.index
|
| 513 |
|
| 514 |
gallery_images = natsorted(load_gallery_images())
|
|
|
|
| 516 |
gr.Markdown("# Map My Sections\n### This GUI is part of the submission to the Allen Institute's Map My Sections tool by Tibbling Technologies.")
|
| 517 |
|
| 518 |
with gr.Row():
|
| 519 |
+
gr.Markdown("### Step 1: Upload your sample, currently only .nii.gz (3D) and .png (2D) supported")
|
| 520 |
gr.Markdown("### Step 2: Select your sample and data type.")
|
| 521 |
with gr.Row():
|
| 522 |
nifti_file = gr.File(label="File Upload")
|
|
|
|
| 534 |
with gr.Column(scale=1):
|
| 535 |
gr.Markdown("### Step 4: Visualizing Allen Brain Cell Types Atlas")
|
| 536 |
gallery = gr.Gallery(label="ABC Atlas", value = gallery_images,columns = 5, height = 450)
|
| 537 |
+
gr.Markdown("### Step 5: Run cell type mappinp and/or registeration. ")
|
| 538 |
with gr.Row():
|
| 539 |
+
run_button = gr.Button("Map My Sections")
|
| 540 |
+
reg_button = gr.Button("Run Registration (Optional)", visible=False)
|
| 541 |
|
| 542 |
with gr.Column(visible=False) as plot_row:
|
| 543 |
gr.Markdown("### Step 6: Quantitative results of the mapping model.")
|
|
|
|
| 564 |
outputs=slice_slider
|
| 565 |
)
|
| 566 |
|
| 567 |
+
gallery.select(on_select, inputs=None, outputs=None)
|
| 568 |
|
| 569 |
slice_slider.change(update_slice, inputs=slice_slider, outputs=[image_display, prob_plot, gallery])
|
| 570 |
run_button.click(run_mapping, outputs=[prob_plot, plot_row, download_button, download_step])
|