Mercurial > repos > bgruening > cellpose
annotate cp_segmentation.py @ 2:eda6dfae9617 draft
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 7e5c9f013821c8905f2c9c452a1f00891e3a0cfd
author | bgruening |
---|---|
date | Thu, 16 May 2024 22:05:24 +0000 |
parents | 1e7334a51725 |
children |
rev | line source |
---|---|
0
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
1 import argparse |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
2 import json |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
3 import os |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
4 import warnings |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
5 |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
6 import matplotlib.pyplot as plt |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
7 import numpy as np |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
8 import skimage.io |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
9 from cellpose import models, plot, transforms |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
10 |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
11 |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
12 def main(inputs, img_path, img_format, output_dir): |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
13 """ |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
14 Parameter |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
15 --------- |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
16 inputs : str |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
17 File path to galaxy tool parameter |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
18 img_path : str |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
19 File path for the input image |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
20 img_format : str |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
21 One of the ['ome.tiff', 'tiff', 'png', 'jpg'] |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
22 output_dir : str |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
23 Folder to save the outputs. |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
24 """ |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
25 warnings.simplefilter('ignore') |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
26 |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
27 with open(inputs, 'r') as param_handler: |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
28 params = json.load(param_handler) |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
29 |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
30 gpu = params['use_gpu'] |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
31 model_type = params['model_type'] |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
32 chan = params['chan'] |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
33 chan2 = params['chan2'] |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
34 chan_first = params['chan_first'] |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
35 if chan is None: |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
36 channels = None |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
37 else: |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
38 channels = [int(chan), int(chan2) if chan2 is not None else None] |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
39 |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
40 options = params['options'] |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
41 |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
42 img = skimage.io.imread(img_path) |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
43 |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
44 print(f"Image shape: {img.shape}") |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
45 # transpose to Ly x Lx x nchann and reshape based on channels |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
46 if img_format.endswith('tiff'): |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
47 img = np.transpose(img, (1, 2, 0)) |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
48 img = transforms.reshape(img, channels=channels, chan_first=chan_first) |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
49 |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
50 print(f"Image shape: {img.shape}") |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
51 model = models.Cellpose(gpu=gpu, model_type=model_type) |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
52 masks, flows, styles, diams = model.eval(img, channels=channels, **options) |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
53 |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
54 # save masks to tiff |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
55 with warnings.catch_warnings(): |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
56 warnings.simplefilter("ignore") |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
57 skimage.io.imsave(os.path.join(output_dir, 'cp_masks.tif'), |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
58 masks.astype(np.uint16)) |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
59 |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
60 # make segmentation show # |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
61 if params['show_segmentation']: |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
62 img = skimage.io.imread(img_path) |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
63 # uniform image |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
64 if img_format.endswith('tiff'): |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
65 img = np.transpose(img, (1, 2, 0)) |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
66 img = transforms.reshape(img, channels=channels, chan_first=chan_first) |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
67 |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
68 maski = masks |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
69 flowi = flows[0] |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
70 fig = plt.figure(figsize=(12, 3)) |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
71 # can save images (set save_dir=None if not) |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
72 plot.show_segmentation(fig, img, maski, flowi, channels=channels) |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
73 fig.savefig(os.path.join(output_dir, 'segm_show.png'), dpi=300) |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
74 plt.close(fig) |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
75 |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
76 |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
77 if __name__ == '__main__': |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
78 aparser = argparse.ArgumentParser() |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
79 aparser.add_argument("-i", "--inputs", dest="inputs", required=True) |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
80 aparser.add_argument("-p", "--img_path", dest="img_path") |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
81 aparser.add_argument("-f", "--img_format", dest="img_format") |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
82 aparser.add_argument("-O", "--output_dir", dest="output_dir") |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
83 args = aparser.parse_args() |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
84 |
1e7334a51725
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/cellpose commit 06dd9637975e3b9d6d27a3d5a773c85e9a52baf2
bgruening
parents:
diff
changeset
|
85 main(args.inputs, args.img_path, args.img_format, args.output_dir) |