slider_app.py 6.76 KB
Newer Older
1
#!/usr/bin/env python
Sean Fitzgibbon's avatar
Sean Fitzgibbon committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 
# Copyright 2021 Sean Fitzgibbon, University of Oxford
# 
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
# 
#        http://www.apache.org/licenses/LICENSE-2.0
# 
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
# 
17
18
19
import argparse
import sys

20
from slider.slide_reg import register_slide_to_slide, apply_slide_xfm
21
from slider.chart_reg import register_chart_to_slide
22
from slider.batch import run_batch
Sean Fitzgibbon's avatar
Sean Fitzgibbon committed
23
from slider.report import report_reg
24
25
26
27
28
29
30
31
32
33
34
35

def add_slide_cli(subparsers):
    """
    Set up slide-to-slide subparser instance.
    """
    parser = subparsers.add_parser(
        'SLIDE',
        description='Register 2D slide to 2D slide',
        formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=55, width=100)
    )

    parser.add_argument("moving", metavar="<moving>",
36
37
38
                        help="Moving slide image", type=str)
    parser.add_argument("moving_res", metavar="<moving-resolution>",
                        help="Moving image resolution (mm)", type=float)
39

40
    parser.add_argument("fixed", metavar="<fixed>",
41
42
43
                        help="Fixed slide image", type=str)
    parser.add_argument("fixed_res", metavar="<fixed-resolution>",
                        help="Fixed image resolution (mm)", type=float)
44

45
    parser.add_argument("--out", metavar="<dir>",
46
                        help="Output directory", default='./slide-to-slide.reg', type=str,
47
                        required=False)
48
49
    parser.add_argument("--config", metavar="<config.json>",
                        help="configuration file", default=None, type=str,
50
51
52
53
54
55
56
57
58
59
60
61
62
                        required=False)
    parser.set_defaults(method='slide')


def add_chart_cli(subparsers):
    """
    Set up chart-to-slide subparser instance.
    """
    parser = subparsers.add_parser(
        'CHART',
        description='Register charting to 2D slide',
        formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=55, width=100)
    )
63
    parser.add_argument("chart", metavar="<chart>",
Sean Fitzgibbon's avatar
Sean Fitzgibbon committed
64
                        help="Neurolucida chart file", type=str)
65
66
    parser.add_argument("slide", metavar="<slide>",
                        help="Fixed slide", type=str)
67
68
    parser.add_argument("slide_res", metavar="<slide-resolution>",
                        help="Slide image resolution (mm)", type=float)
69
    parser.add_argument("--out", metavar="<dir>",
70
                        help="Output directory", default='./chart-to-slide.reg', type=str,
71
                        required=False)
Sean Fitzgibbon's avatar
Sean Fitzgibbon committed
72
73
74
    parser.add_argument("--config", metavar="<config.yaml>",
                        help="configuration file", default=None, type=str,
                        required=False)
75
76
77
    parser.set_defaults(method='chart')


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def add_applyxfm_cli(subparsers):
    """
    Set up applyxfm subparser instance.
    """
    parser = subparsers.add_parser(
        'APPLYXFM',
        description='Apply transform to image.',
        formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=55, width=100)
    )

    parser.add_argument("moving", metavar="<moving>",
                        help="Moving slide image", type=str)
    parser.add_argument("moving_res", metavar="<moving-resolution>",
                        help="Moving image resolution (mm)", type=float)

    parser.add_argument("fixed", metavar="<fixed>",
                        help="Fixed slide image", type=str)
    parser.add_argument("fixed_res", metavar="<fixed-resolution>",
                        help="Fixed image resolution (mm)", type=float)

Sean Fitzgibbon's avatar
Sean Fitzgibbon committed
98
99
100
101
    parser.add_argument("moving_reg", metavar="<reg-moving>",
                        help="Moving timg from registration directory", type=str)     
    parser.add_argument("fixed_reg", metavar="<reg-fixed>",
                        help="Fixed timg from registration directory (e.g. fixed4_nonlinear.timg)", type=str)                
102

Sean Fitzgibbon's avatar
Sean Fitzgibbon committed
103
    parser.add_argument("out", metavar="<resampled-img>",
104
                        help="Name for resampled output image", type=str)
105

106
107
108
109
    parser.add_argument("--rlevel", metavar="<rlevel>",
                        help="Resolution level for output image (subsample by 2^rlevel)", default=0, type=int,
                        required=False)

Sean Fitzgibbon's avatar
Sean Fitzgibbon committed
110
111
    parser.add_argument('--inverse', dest='inverse', action='store_true',
                        help='Invert the transformation chain')
112
113
114
    parser.set_defaults(method='applyxfm')


115
116
117
118
119
120
121
122
123
124
125
126
127
128
def add_batch_cli(subparsers):
    """
    Set up batch subparser instance.
    """
    parser = subparsers.add_parser(
        'BATCH',
        description='Batch processing SlideR jobs',
        formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=55, width=100)
    )
    parser.add_argument("csv", metavar="<csv>",
                        help="Spreadsheet of job info", type=str)
    parser.set_defaults(method='batch')


Sean Fitzgibbon's avatar
Sean Fitzgibbon committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def add_report_cli(subparsers):
    """
    Set up report subparser instance.
    """
    parser = subparsers.add_parser(
        'REPORT',
        description='Generate report of SlideR registrations',
        formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=55, width=100)
    )
    parser.add_argument("out", metavar="<dir>",
                        help="Output directory", default=None, type=str)
    parser.add_argument("dirs", metavar="<reg-dir>", nargs='+', 
                        help="Registration directories to include in report")
    parser.add_argument('--embed', dest='embed', action='store_true',
                        help='Embed images into report to make it transportable as a single file (report will be much larger).')
    parser.set_defaults(method='report')


147

148
149
150
151
152
153
154
155
if __name__ == "__main__":
    """ Main program code. """

    parser = argparse.ArgumentParser()

    subparsers = parser.add_subparsers()
    add_slide_cli(subparsers)
    add_chart_cli(subparsers)
156
    add_applyxfm_cli(subparsers)
157
    add_batch_cli(subparsers)
Sean Fitzgibbon's avatar
Sean Fitzgibbon committed
158
    add_report_cli(subparsers)
159
160
161
162
163
164
165
166
167
168
169
170
171

    # ---

    if len(sys.argv) == 1:
        parser.parse_args(['-h'])
    else:
        args = vars(parser.parse_args())

    method = args.pop('method')

    if method == 'slide':
        register_slide_to_slide(**args)
    elif method == 'chart':
172
        register_chart_to_slide(**args)
173
174
    elif method == 'applyxfm':
        apply_slide_xfm(**args)
175
176
    elif method == 'batch':
        run_batch(**args)
Sean Fitzgibbon's avatar
Sean Fitzgibbon committed
177
178
    elif method == 'report':
        report_reg(**args)
179
180
    else:
        raise RuntimeError(f'Unknown method: {method}')