batch.py 2.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env python
# 
# 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.
# 

import pandas as pd
19
from slider.slide_reg import register_slide_to_slide, apply_slide_xfm
Sean Fitzgibbon's avatar
Sean Fitzgibbon committed
20
from slider.chart_reg import register_chart_to_slide
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
REQUIRED_ARGS = {
    'chart': ['chart', 'slide', 'slide_res'],
    'slide': ['moving', 'moving_res', 'fixed', 'fixed_res'],
    'applyxfm': ['moving', 'moving_res', 'fixed', 'fixed_res', 'moving_reg', 'fixed_reg', 'resampled-img'],
}

def looks_like(df):

    col_names = list(df)

    def has_required_args(ftype):
        return all([arg in col_names for arg in REQUIRED_ARGS[ftype]])

    if has_required_args('chart'):
        print('Looks like a CHART csv')
        return 'CHART'
    elif has_required_args('applyxfm'):
        print('Looks like an APPLYXFM csv')
        return 'APPLYXFM'
    elif has_required_args('slide') and not has_required_args('applyxfm'):
        print('Looks like an SLIDE csv')
        return 'SLIDE'
    else:
        raise RuntimeError('Unknown CSV column names')


48
49
def run_batch(csv):
    
Sean Fitzgibbon's avatar
Sean Fitzgibbon committed
50
    # TODO: Add support for CHART and APPLYXFM csv
51
52

    df = pd.read_csv(csv)
53
    file_type = looks_like(df)
54

55
    if file_type == 'SLIDE':
56
57

        # csv is SLIDE
Sean Fitzgibbon's avatar
Sean Fitzgibbon committed
58
59
        print('Batch slider_app.py SLIDE')
        
60
61
        for idx, row in df.iterrows():
            register_slide_to_slide(**row)
Sean Fitzgibbon's avatar
Sean Fitzgibbon committed
62

63
    elif file_type == 'CHART':
Sean Fitzgibbon's avatar
Sean Fitzgibbon committed
64
65
66
67
68
69
70

        # csv is CHART
        print('Batch slider_app.py CHART')
        
        for idx, row in df.iterrows():
            register_chart_to_slide(**row)

71
72
    elif file_type == 'APPLYXFM':
        
73
74
75
76
77
78
        # csv is APPLYXFM
        print('Batch slider_app.py APPLYXFM')
        
        for idx, row in df.iterrows():
            apply_slide_xfm(**row)