tilepairs.py 6.93 KB
Newer Older
Martin Schorb's avatar
Martin Schorb committed
1
2
3
4
5
6
7
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 12 16:15:27 2020

@author: schorb
"""
Martin Schorb's avatar
Martin Schorb committed
8
import dash
Martin Schorb's avatar
Martin Schorb committed
9
10
from dash import dcc
from dash import html
Martin Schorb's avatar
Martin Schorb committed
11
12
from dash.dependencies import Input,Output,State
import params
Martin Schorb's avatar
Martin Schorb committed
13
14
15
import json
import os
import importlib
Martin Schorb's avatar
Martin Schorb committed
16
17


Martin Schorb's avatar
Martin Schorb committed
18
19
from app import app
from utils import launch_jobs, pages
Martin Schorb's avatar
Martin Schorb committed
20
from utils import helper_functions as hf
Martin Schorb's avatar
Martin Schorb committed
21

Martin Schorb's avatar
Martin Schorb committed
22
from callbacks import runstate,render_selector,substack_sel
Martin Schorb's avatar
Martin Schorb committed
23
24


Martin Schorb's avatar
Martin Schorb committed
25
module='tilepairs'
Martin Schorb's avatar
Martin Schorb committed
26
27


Martin Schorb's avatar
Martin Schorb committed
28
29
storeinit = {}            
store = pages.init_store(storeinit, module)
Martin Schorb's avatar
Martin Schorb committed
30

Martin Schorb's avatar
Martin Schorb committed
31
store.append(dcc.Store(id={'component':'runstep','module':module},data='generate'))
Martin Schorb's avatar
Martin Schorb committed
32
33


Martin Schorb's avatar
Martin Schorb committed
34
main=html.Div(id={'component': 'main', 'module': module},children=html.H3("Generate TilePairs for Render stack"))
Martin Schorb's avatar
Martin Schorb committed
35

Martin Schorb's avatar
Martin Schorb committed
36

Martin Schorb's avatar
Martin Schorb committed
37
page = [main]
Martin Schorb's avatar
Martin Schorb committed
38
39
40



Martin Schorb's avatar
Martin Schorb committed
41
# # ===============================================
Martin Schorb's avatar
Martin Schorb committed
42
#  RENDER STACK SELECTOR
Martin Schorb's avatar
Martin Schorb committed
43

Martin Schorb's avatar
Martin Schorb committed
44
# Pre-fill render stack selection from previous module
Martin Schorb's avatar
Martin Schorb committed
45

Martin Schorb's avatar
Martin Schorb committed
46
us_out,us_in,us_state = render_selector.init_update_store(module,'convert')
Martin Schorb's avatar
Martin Schorb committed
47

Martin Schorb's avatar
Martin Schorb committed
48
49
@app.callback(us_out,us_in,us_state,
              prevent_initial_call=True)
50
def tilepairs_update_store(*args): 
Martin Schorb's avatar
Martin Schorb committed
51
    return render_selector.update_store(*args)
Martin Schorb's avatar
Martin Schorb committed
52

Martin Schorb's avatar
Martin Schorb committed
53
page1 = pages.render_selector(module)
Martin Schorb's avatar
Martin Schorb committed
54

Martin Schorb's avatar
Martin Schorb committed
55
page.append(page1)
Martin Schorb's avatar
Martin Schorb committed
56

Martin Schorb's avatar
Martin Schorb committed
57
58


Martin Schorb's avatar
Martin Schorb committed
59
# # # ===============================================
Martin Schorb's avatar
Martin Schorb committed
60
61


Martin Schorb's avatar
Martin Schorb committed
62
63
64
page2 = html.Div(id={'component':'page2', 'module': module},
                 children=[html.H4('Pair assignment mode'),
                           html.Div([dcc.RadioItems(options=[
Martin Schorb's avatar
Martin Schorb committed
65
                                                          {'label': '2D (tiles in montage/mosaic)', 'value': '2D'},
Martin Schorb's avatar
Martin Schorb committed
66
                                                          {'label': '3D (across sections)', 'value': '3D'}],
Martin Schorb's avatar
Martin Schorb committed
67
                                                      value='2D',                                                
Martin Schorb's avatar
Martin Schorb committed
68
                                                      id={'component':'pairmode', 'module': module}),
69
                               ]
Martin Schorb's avatar
Martin Schorb committed
70
71
72
                               ,style={'display':'inline,block'}),
                           html.Br()  
                           ])
Martin Schorb's avatar
Martin Schorb committed
73
74
75
                                                                           
page.append(page2)

Martin Schorb's avatar
Martin Schorb committed
76
page.append(pages.substack_sel(module))
Martin Schorb's avatar
Martin Schorb committed
77
78


Martin Schorb's avatar
Martin Schorb committed
79
# # ===============================================
Martin Schorb's avatar
Martin Schorb committed
80

Martin Schorb's avatar
Martin Schorb committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
@app.callback([Output({'component':'3Dslices','module' : module},'style'),
                Output({'component':'sec_input1','module' : module},'value')],
              Input({'component':'pairmode','module' : module},'value'))
def tilepairs_3D_status(pairmode):

    if pairmode == '2D':
        style = {'display':'none'}
        val = 0
    elif pairmode == '3D':
        style = {'display':'block'}
        val = 1
    return style, val





# =============================================
# Start Button



gobutton = html.Div(children=[html.Br(),
                              html.Button('Start TilePair generation',
                                          id={'component': 'go', 'module': module}),
                              html.Br(),
                              pages.compute_loc(module),
                              html.Br(),
                              html.Div(id={'component': 'run_state', 'module': module}, style={'display': 'none'},children='wait')])


page.append(gobutton)


@app.callback([Output({'component': 'go', 'module': module}, 'disabled'),
Martin Schorb's avatar
Martin Schorb committed
116
               Output({'component': 'store_launch_status', 'module': module},'data'),
Martin Schorb's avatar
Martin Schorb committed
117
               Output({'component': 'store_render_launch', 'module': module},'data')],
118
              [Input({'component': 'go', 'module': module}, 'n_clicks'),
Martin Schorb's avatar
Martin Schorb committed
119
120
               Input({'component':'pairmode','module' : module},'value'),
               Input({'component':'stack_dd','module' : module},'value')],
Martin Schorb's avatar
Martin Schorb committed
121
122
123
124
125
              [State({'component':'sec_input1','module' : module},'value'),
                State({'component':'compute_sel','module' : module},'value'),
                State({'component':'startsection','module' : module},'value'),
                State({'component':'endsection','module' : module},'value'),
                State({'component':'store_owner','module' : module},'data'),
Martin Schorb's avatar
Martin Schorb committed
126
                State({'component':'store_project','module' : module},'data')]
Martin Schorb's avatar
Martin Schorb committed
127
              ,prevent_initial_call=True)                 
Martin Schorb's avatar
Martin Schorb committed
128
def tilepairs_execute_gobutton(click,pairmode,stack,slicedepth,comp_sel,startsection,endsection,owner,project):
Martin Schorb's avatar
Martin Schorb committed
129
    if click is None: return dash.no_update
130
131
132
133
134
135
136

    trigger = hf.trigger()

    if not 'go' in trigger:
        return False, dash.no_update, dash.no_update


Martin Schorb's avatar
Martin Schorb committed
137
    # prepare parameters:
Martin Schorb's avatar
Martin Schorb committed
138
    run_prefix = launch_jobs.run_prefix()
Martin Schorb's avatar
Martin Schorb committed
139
140
141
142

    run_params = params.render_json.copy()
    run_params['render']['owner'] = owner
    run_params['render']['project'] = project
Martin Schorb's avatar
Martin Schorb committed
143
    
Martin Schorb's avatar
Martin Schorb committed
144
    run_params_generate = run_params.copy()
Martin Schorb's avatar
Martin Schorb committed
145
146
    
    
Martin Schorb's avatar
Martin Schorb committed
147
    #generate script call...
Martin Schorb's avatar
Martin Schorb committed
148
    
149
    tilepairdir = params.json_run_dir + '/tilepairs_' + run_prefix + '_'+ stack + '_' + pairmode
Martin Schorb's avatar
Martin Schorb committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    
    if not os.path.exists(tilepairdir): os.makedirs(tilepairdir)
    
    run_params_generate['output_dir'] = tilepairdir
    
    with open(os.path.join(params.json_template_dir,'tilepairs.json'),'r') as f:
            run_params_generate.update(json.load(f))
    
    run_params_generate['output_json'] = tilepairdir + '/tiles_'+ stack + '_' + pairmode
    
    run_params_generate['minZ'] = startsection
    run_params_generate['maxZ'] = endsection
    
    run_params_generate['stack'] = stack
    
    if pairmode == '2D':
        run_params_generate['zNeighborDistance'] = 0
        run_params_generate['excludeSameLayerNeighbors'] = 'False'
Martin Schorb's avatar
Martin Schorb committed
168
        
Martin Schorb's avatar
Martin Schorb committed
169
170
171
172
    elif pairmode == '3D':
        run_params_generate['zNeighborDistance'] = slicedepth
        run_params_generate['excludeSameLayerNeighbors'] = 'True'

173
        if owner == 'SBEM':
Martin Schorb's avatar
Martin Schorb committed
174
            run_params_generate["xyNeighborFactor"] = 0.2
Martin Schorb's avatar
Martin Schorb committed
175

176
    param_file = params.json_run_dir + '/' + module + '_' + run_prefix + '_' + pairmode + '.json' 
Martin Schorb's avatar
Martin Schorb committed
177
178
179
180
181

           
    with open(param_file,'w') as f:
        json.dump(run_params_generate,f,indent=4)

182
    log_file = params.render_log_dir + '/' + module + '_' + run_prefix + '_' + pairmode
Martin Schorb's avatar
Martin Schorb committed
183
184
185
    err_file = log_file + '.err'
    log_file += '.log'
    
Martin Schorb's avatar
Martin Schorb committed
186
    tilepairs_generate_p = launch_jobs.run(target=comp_sel,pyscript=params.rendermodules_dir+'rendermodules/pointmatch/create_tilepairs.py',
187
                        jsonfile=param_file,run_args=None,target_args=None,logfile=log_file,errfile=err_file)
Martin Schorb's avatar
Martin Schorb committed
188

Martin Schorb's avatar
Martin Schorb committed
189
190
191
    
    launch_store=dict()
    launch_store['logfile'] = log_file
Martin Schorb's avatar
launch    
Martin Schorb committed
192
    launch_store['status'] = 'launch'
Martin Schorb's avatar
Martin Schorb committed
193
194
    launch_store['id'] = tilepairs_generate_p
    launch_store['type'] = comp_sel
Martin Schorb's avatar
launch    
Martin Schorb committed
195

Martin Schorb's avatar
Martin Schorb committed
196

Martin Schorb's avatar
Martin Schorb committed
197
198
199
200
201
    outstore = dict()
    outstore['owner'] = owner
    outstore['project'] = project
    outstore['stack'] = stack
    outstore['tilepairdir'] = tilepairdir
Martin Schorb's avatar
Martin Schorb committed
202
    
Martin Schorb's avatar
Martin Schorb committed
203
    return True, launch_store, outstore
Martin Schorb's avatar
Martin Schorb committed
204
205
206
207
208


# =============================================
# Processing status

Martin Schorb's avatar
Martin Schorb committed
209
210
# initialized with store
# embedded from callbacks import runstate
Martin Schorb's avatar
Martin Schorb committed
211

Martin Schorb's avatar
Martin Schorb committed
212
213
# # =============================================
# # PROGRESS OUTPUT
Martin Schorb's avatar
Martin Schorb committed
214
215


Martin Schorb's avatar
Martin Schorb committed
216
collapse_stdout = pages.log_output(module)
Martin Schorb's avatar
Martin Schorb committed
217

Martin Schorb's avatar
Martin Schorb committed
218
# ----------------
Martin Schorb's avatar
Martin Schorb committed
219
220
221

# Full page layout:
    
Martin Schorb's avatar
Martin Schorb committed
222

Martin Schorb's avatar
Martin Schorb committed
223
page.append(collapse_stdout)