forked from eisfabian/SPACEtomo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPACEtomo_postAction.py
139 lines (117 loc) · 5.29 KB
/
SPACEtomo_postAction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!Python
# ===================================================================
#ScriptName SPACEtomo_postAction
# Purpose: Post action script to setup targets from SPACEtomo runs.
# More information at http://github.com/eisfabian/SPACEtomo
# Author: Fabian Eisenstein
# Created: 2023/07/13
# Revision: v1.0beta
# Last Change: 2023/11/12: fixes after Krios test
# ===================================================================
import serialem as sem
import os
import sys
CUR_DIR = sem.ReportDirectory()
# Read settings written by main SPACE script
if os.path.exists(os.path.join(CUR_DIR, "SPACEtargets_settings.txt")):
exec(open(os.path.join(CUR_DIR, "SPACEtargets_settings.txt")).read())
else:
sem.Echo("ERROR: No SPACE settings file was found. No targets were set up.")
sem.Exit()
# Check if external processing directory is valid
MM_external = False
if external_map_dir == "":
MAP_DIR = CUR_DIR
else:
if os.path.exists(external_map_dir):
MAP_DIR = external_map_dir
MM_external = True
else:
sem.Echo("ERROR: External map directory does not exist!")
sem.Exit()
# Import SPACE functions (needs SPACE folder from settings file)
sys.path.insert(len(sys.path), SPACE_DIR)
import SPACEtomo_functions as space
# Instantiate mic params from settings
mic_params = space.MicParams(WG_image_state, IM_mag_index, MM_image_state)
# Load model
MM_model = space.MMModel()
# Update SPACE runs and queue
if not MM_external:
space.queueSpaceRun(MM_model)
# Make list of finished SPACE runs
if os.path.exists(os.path.join(MAP_DIR, "SPACE_runs.txt")):
with open(os.path.join(MAP_DIR, "SPACE_runs.txt"), "r") as f:
space_lines = f.readlines()
else:
sem.Echo("ERROR: No SPACE runs file was found. No targets were set up.")
sem.Exit()
space_maps = []
active_runs = []
for line in space_lines:
map_name = os.path.splitext(os.path.basename(line))[0]
map_seg = os.path.join(MAP_DIR, map_name + "_seg.png")
map_tgt = os.path.join(CUR_DIR, map_name + "_tgts.txt")
if not os.path.exists(map_seg):
active_runs.append(map_name)
elif not os.path.exists(map_tgt):
sem.Echo("SPACEtomo [" + line + "] run finished!")
space_maps.append(map_name)
else:
sem.Echo("Targets file for " + line + " already exists. Skipping...")
# Check if any targets are remaining and wait for next segmentation if not
if len(space_maps) > 0:
sem.Echo("Setting up targets for " + str(len(space_maps)) + " lamellae...")
else:
num_acq, *_ = sem.ReportNumNavAcquire()
while num_acq == 0 and len(space_maps) == 0 and len(active_runs) > 0:
sem.Echo("Waiting for next prediction before setting up targets...")
sem.Delay(60, "s")
if not MM_external:
space.queueSpaceRun(MM_model)
for map_name in active_runs:
map_seg = os.path.join(MAP_DIR, map_name + "_seg.png")
if os.path.exists(map_seg):
space_maps.append(map_name)
# Check again in case results were added
if len(space_maps) == 0:
if len(active_runs) > 0:
sem.Echo("Target setup for remaining " + str(len(active_runs)) + " lamellae postponed until predictions are available.")
else:
sem.Echo("All lamellae have been set up and no more predictions are running.")
sem.Exit()
# Get microscope parameters
sem.GoToLowDoseArea("V")
mic_params.getViewParams()
sem.GoToLowDoseArea("R")
mic_params.getRecParams()
MM_model.setDimensions(mic_params)
# Set up common parameters
weight_mask, edge_weight_masks = space.makeScoreWeights(MM_model, target_edge)
grid_vecs = space.findGridVecs(MM_model, max_tilt, mic_params)
# Loop over all MM maps
for m, map_name in enumerate(space_maps): # adjusted from main script
map_id = int(sem.NavIndexWithNote(map_name + ".mrc"))
sem.LoadOtherMap(map_id)
buffer, *_ = sem.ReportCurrentBuffer()
sem.Echo("")
sem.Echo("Setting up targets for " + map_name + "...")
# Instantiate lamella and find points
lamella = space.Lamella(map_name, MAP_DIR, target_list, avoid_list, MM_model, weight_mask, edge_weight_masks, grid_vecs, mic_params, max_tilt)
lamella.findPoints(sparse_targets, penalty_weight, target_score_threshold, max_iterations)
if len(lamella.points) == 0:
sem.Echo("WARNING: No targets found!")
sem.Echo("If you visually identified targets, please adjust your settings or add them manually!")
# Write empty PACE target file
lamella.saveAsTargets(buffer, penalty_weight)
continue
sem.Echo("Final targets: " + str(len(lamella.points)))
sem.Echo("Saving overview image...")
lamella.plotTargets(tracking_id=0, overlay=lamella.target_mask, save=os.path.join(CUR_DIR, map_name + "_" + target_list[0] +"_targets.png"))
sem.Echo("Saved at " + os.path.join(CUR_DIR, map_name + "_targets_" + target_list[0] + ".png"))
# Find geo points for sample geometry measurement
lamella.findGeoPoints()
# Save targets for PACEtomo
lamella.saveAsTargets(buffer, penalty_weight)
num_acq, *_ = sem.ReportNumNavAcquire()
sem.Echo("Continuing PACEtomo acquisition of " + str(int(num_acq)) + " areas!")