Mercurial > repos > bgruening > cellpose
annotate cp_segmentation.py @ 3:c793edde4284 draft default tip
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 3f2ba60f101c923896ca95ed62981fcbb0a5ced3
| author | bgruening |
|---|---|
| date | Fri, 12 Dec 2025 12:36:21 +0000 |
| parents | e5370bb71633 |
| children |
| rev | line source |
|---|---|
|
0
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
1 import argparse |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
2 import json |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
3 import os |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
4 import warnings |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
5 |
|
2
e5370bb71633
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 5846f23282167df28e6cc00e1bda31523b66b5b4
bgruening
parents:
1
diff
changeset
|
6 # Make sure, that `MKL_NUM_THREADS` is set to 1, to ensure reproducibility: |
|
e5370bb71633
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 5846f23282167df28e6cc00e1bda31523b66b5b4
bgruening
parents:
1
diff
changeset
|
7 # https://forum.image.sc/t/reproducibility-how-we-spent-years-building-containers-and-then-mkl-decided-to-screw-our-results/109599 |
|
e5370bb71633
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 5846f23282167df28e6cc00e1bda31523b66b5b4
bgruening
parents:
1
diff
changeset
|
8 if str(os.environ['MKL_NUM_THREADS']) != '1': |
|
e5370bb71633
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 5846f23282167df28e6cc00e1bda31523b66b5b4
bgruening
parents:
1
diff
changeset
|
9 warnings.warn('MKL_NUM_THREADS must be set to 1 to ensure reproducibility, and will be adjusted accordingly for now.') |
|
e5370bb71633
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 5846f23282167df28e6cc00e1bda31523b66b5b4
bgruening
parents:
1
diff
changeset
|
10 os.environ['MKL_NUM_THREADS'] = '1' |
|
e5370bb71633
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 5846f23282167df28e6cc00e1bda31523b66b5b4
bgruening
parents:
1
diff
changeset
|
11 |
|
e5370bb71633
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 5846f23282167df28e6cc00e1bda31523b66b5b4
bgruening
parents:
1
diff
changeset
|
12 # Load the remaining packages *after* adjusting `MKL_NUM_THREADS` (this likely necessary for it to take effect) |
|
0
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
13 import matplotlib.pyplot as plt |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
14 import numpy as np |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
15 import skimage.io |
|
2
e5370bb71633
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 5846f23282167df28e6cc00e1bda31523b66b5b4
bgruening
parents:
1
diff
changeset
|
16 import torch |
|
3
c793edde4284
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 3f2ba60f101c923896ca95ed62981fcbb0a5ced3
bgruening
parents:
2
diff
changeset
|
17 from cellpose import models, plot |
|
0
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
18 |
|
2
e5370bb71633
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 5846f23282167df28e6cc00e1bda31523b66b5b4
bgruening
parents:
1
diff
changeset
|
19 # Apply PyTorch guidelines for reproducibility |
|
e5370bb71633
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 5846f23282167df28e6cc00e1bda31523b66b5b4
bgruening
parents:
1
diff
changeset
|
20 torch.backends.cudnn.benchmark = True |
|
e5370bb71633
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 5846f23282167df28e6cc00e1bda31523b66b5b4
bgruening
parents:
1
diff
changeset
|
21 torch.backends.cudnn.deterministic = True |
|
e5370bb71633
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 5846f23282167df28e6cc00e1bda31523b66b5b4
bgruening
parents:
1
diff
changeset
|
22 torch.manual_seed(0) |
|
e5370bb71633
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 5846f23282167df28e6cc00e1bda31523b66b5b4
bgruening
parents:
1
diff
changeset
|
23 |
|
0
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
24 |
|
3
c793edde4284
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 3f2ba60f101c923896ca95ed62981fcbb0a5ced3
bgruening
parents:
2
diff
changeset
|
25 def main(inputs, img_path, output_dir): |
|
0
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
26 """ |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
27 Parameter |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
28 --------- |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
29 inputs : str |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
30 File path to galaxy tool parameter |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
31 img_path : str |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
32 File path for the input image |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
33 output_dir : str |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
34 Folder to save the outputs. |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
35 """ |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
36 warnings.simplefilter('ignore') |
|
1
1857781df226
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 81c6be8cc2502367ae52bf093b838e9764b22c00
bgruening
parents:
0
diff
changeset
|
37 np.random.seed(42) |
|
0
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
38 with open(inputs, 'r') as param_handler: |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
39 params = json.load(param_handler) |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
40 |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
41 gpu = params['use_gpu'] |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
42 model_type = params['model_type'] |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
43 options = params['options'] |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
44 img = skimage.io.imread(img_path) |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
45 |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
46 print(f"Image shape: {img.shape}") |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
47 |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
48 model = models.Cellpose(gpu=gpu, model_type=model_type) |
|
3
c793edde4284
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 3f2ba60f101c923896ca95ed62981fcbb0a5ced3
bgruening
parents:
2
diff
changeset
|
49 masks, flows, styles, diams = model.eval(img, channels=[0, 0], **options) |
|
0
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
50 |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
51 # save masks to tiff |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
52 with warnings.catch_warnings(): |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
53 warnings.simplefilter("ignore") |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
54 skimage.io.imsave(os.path.join(output_dir, 'cp_masks.tif'), |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
55 masks.astype(np.uint16)) |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
56 |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
57 # make segmentation show # |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
58 if params['show_segmentation']: |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
59 img = skimage.io.imread(img_path) |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
60 |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
61 maski = masks |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
62 flowi = flows[0] |
|
3
c793edde4284
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 3f2ba60f101c923896ca95ed62981fcbb0a5ced3
bgruening
parents:
2
diff
changeset
|
63 fig = plt.figure(figsize=(8, 2)) |
|
0
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
64 # can save images (set save_dir=None if not) |
|
3
c793edde4284
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 3f2ba60f101c923896ca95ed62981fcbb0a5ced3
bgruening
parents:
2
diff
changeset
|
65 plot.show_segmentation(fig, img, maski, flowi, channels=[0, 0]) |
|
0
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
66 fig.savefig(os.path.join(output_dir, 'segm_show.png'), dpi=300) |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
67 plt.close(fig) |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
68 |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
69 |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
70 if __name__ == '__main__': |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
71 aparser = argparse.ArgumentParser() |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
72 aparser.add_argument("-i", "--inputs", dest="inputs", required=True) |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
73 aparser.add_argument("-p", "--img_path", dest="img_path") |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
74 aparser.add_argument("-O", "--output_dir", dest="output_dir") |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
75 args = aparser.parse_args() |
|
4ddb0af5a806
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit e80ca9b0e2e6f7ae94371170d0a672f46f2d9c3c
bgruening
parents:
diff
changeset
|
76 |
|
3
c793edde4284
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 3f2ba60f101c923896ca95ed62981fcbb0a5ced3
bgruening
parents:
2
diff
changeset
|
77 main(args.inputs, args.img_path, args.output_dir) |
