annotate orientationpy-cli.py @ 1:df211f6b5cf2 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit c045f067a57e8308308cf6329060c7ccd3fc372f
author imgteam
date Thu, 04 Apr 2024 15:25:49 +0000
parents 214f548481eb
children cc8fe984ec3e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
1 import argparse
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
2 import csv
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
3
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
4 import numpy as np
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
5 import orientationpy
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
6 import skimage.io
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
7 import skimage.util
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
8
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
9
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
10 if __name__ == '__main__':
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
11
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
12 parser = argparse.ArgumentParser()
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
13 parser.add_argument('input', type=str)
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
14 parser.add_argument('--mode', type=str, required=True)
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
15 parser.add_argument('--sigma', type=float, required=True)
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
16 parser.add_argument('--min_coherency', type=float, required=True)
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
17 parser.add_argument('--min_energy', type=float, required=True)
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
18 parser.add_argument('--max_precision', type=int, required=True)
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
19 parser.add_argument('--output_angle_tsv', type=str, default=None)
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
20 args = parser.parse_args()
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
21
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
22 im = skimage.io.imread(args.input)
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
23 im = skimage.util.img_as_float(im)
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
24 im = np.squeeze(im)
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
25 assert im.ndim == 2
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
26
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
27 Gy, Gx = orientationpy.computeGradient(im, mode=args.mode)
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
28 structureTensor = orientationpy.computeStructureTensor([Gy, Gx], sigma=args.sigma)
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
29 orientations = orientationpy.computeOrientation(structureTensor, computeEnergy=True, computeCoherency=True)
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
30
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
31 # Compute angle according to https://bigwww.epfl.ch/demo/orientationj/#dist:
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
32 mask = np.logical_and(
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
33 orientations['coherency'] >= args.min_coherency,
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
34 orientations['energy'] >= args.min_energy * orientations['energy'].max(),
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
35 )
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
36 angles = orientations['theta'][mask]
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
37 weights = orientations['coherency'][mask]
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
38 bin_size = 1 if args.max_precision == 0 else pow(10, -args.max_precision)
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
39 hist, bin_edges = np.histogram(
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
40 angles,
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
41 range=(-90, +90),
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
42 weights=weights,
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
43 bins=round(180 / bin_size),
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
44 )
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
45 hidx = np.argmax(hist)
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
46 angle = (bin_edges[hidx] + bin_edges[hidx + 1]) / 2
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
47 angle = round(angle, args.max_precision)
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
48
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
49 # Write results
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
50 if args.output_angle_tsv:
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
51 with open(args.output_angle_tsv, 'w') as fp:
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
52 writer = csv.writer(fp, delimiter='\t', lineterminator='\n')
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
53 writer.writerow(['Angle'])
214f548481eb planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
imgteam
parents:
diff changeset
54 writer.writerow([angle])