Browse Source

Add quick optimization

Matthias Vogelgesang 7 years ago
parent
commit
23efc124bd
1 changed files with 88 additions and 49 deletions
  1. 88 49
      cockpit

+ 88 - 49
cockpit

@@ -23,6 +23,7 @@ def read_info_file(path):
 
     return result
 
+
 def read_edf_id19_header(filename):
     result = {}
 
@@ -206,6 +207,7 @@ class StateMachine(object):
     CLEAN = 3
     FLATCORRECT = 4
     OPTIMIZE = 5
+    QUICK_OPTIMIZE = 6
 
     def __init__(self):
         self.current = StateMachine.START
@@ -217,6 +219,7 @@ class StateMachine(object):
             StateMachine.QUIT: collections.OrderedDict(),
             StateMachine.SYNC: collections.OrderedDict(),
             StateMachine.OPTIMIZE: collections.OrderedDict(),
+            StateMachine.QUICK_OPTIMIZE: collections.OrderedDict(),
         }
 
     def add_action(self, from_state, action):
@@ -269,41 +272,7 @@ class Application(object):
         info_file = os.path.join(self.config.destination, '{}.info'.format(self.prefix))
         return read_info_file(info_file)
 
-    def on_quit(self):
-        self.running = False
-        return True
-
-    def on_sync(self):
-        self.log.highlight("Syncing data ...")
-        cmd = 'bash sync.sh {} {}'.format(self.config.source, self.config.destination)
-        return self.run_command(cmd)
-
-    def on_clean(self):
-        self.log.highlight("Cleaning {}".format(self.config.destination))
-        return True
-
-    def on_flat_correct(self):
-        self.log.highlight("Flat field correction ...")
-        info = self.read_info()
-        data = dict(path=self.config.destination, prefix=self.prefix, num=info['TOMO_N'], step=1)
-
-        cmd = ('tofu flatcorrect --verbose'
-               ' --reduction-mode median'
-               ' --projections "{path}/{prefix}*.edf"'
-               ' --darks {path}/darkend0000.edf'
-               ' --flats {path}/ref*_0000.edf'
-               ' --flats2 {path}/ref*_{num}.edf'
-               ' --output {path}/fc/fc-%04i.tif'
-               ' --number {num}'
-               ' --step {step}'
-               ' --absorptivity'
-               ' --fix-nan-and-inf'.format(**data))
-
-        return self.run_command(cmd)
-
-    def on_optimize(self):
-        self.log.highlight("Optimizing ...")
-
+    def optimize(self, resize):
         slices_per_device = 100
         half_range = 1.0
 
@@ -323,9 +292,19 @@ class Application(object):
         angle_start = theta - half_range
         angle_stop = theta + half_range
 
+        x_region = 960
+        y_region = 960
+        fc_path = '{prefix}/fc'.format(prefix=self.prefix)
+
+        if resize:
+            x_region /= 2
+            y_region /= 2
+            axis /= 2
+            fc_path = '{prefix}/fc-small'.format(prefix=self.prefix)
+
         self.log.info(" Using theta = {}, inclination angle = {}".format(theta, inclination_angle))
-        self.log.info(" Scanning angle within [{}:{}:{}]".format(angle_start, angle_stop, angle_step))
-        self.log.info(" Scanning axis within [{}:{}:{}]".format(axis_start, axis_stop, axis_step))
+        self.log.info(" Scanning lamino angle within [{}:{}:{}]".format(angle_start, angle_stop, angle_step))
+        self.log.info(" Scanning x axis within [{}:{}:{}]".format(axis_start, axis_stop, axis_step))
 
         opt_params = ('--num-iterations 2'
                       ' --axis-range={ax_start},{ax_stop},{ax_step}'
@@ -334,30 +313,87 @@ class Application(object):
                       .format(ax_start=axis_start, ax_stop=axis_stop, ax_step=axis_step,
                               an_start=angle_start, an_stop=angle_stop, an_step=angle_step))
 
-        params = ('--x-region=-960,960,1'
-                  ' --y-region=-960,960,1'
+        params = ('--x-region="-{x_region},{x_region},1"'
+                  ' --y-region="-{y_region},{y_region},1"'
                   ' --overall-angle -360'
                   ' --pixel-size {pixel_size}e-6'
                   ' --roll-angle 0'
                   ' --slices-per-device 100'
-                  .format(pixel_size=info['PixelSize']))
+                  .format(pixel_size=info['PixelSize'], x_region=x_region, y_region=y_region))
 
         cmd = ('optimize-parameters --verbose'
-               ' {prefix}/fc'
+               ' {fc_path}'
                ' {opt_params}'
                ' --reco-params "{params}"'
                ' --params-filename params.json'
-               .format(opt_params=opt_params, params=params, prefix=self.prefix))
+               .format(opt_params=opt_params, params=params, fc_path=fc_path))
+
+        return self.run_command(cmd)
+
+    def on_quit(self):
+        self.running = False
+        return True
+
+    def on_sync(self):
+        self.log.highlight("Syncing data ...")
+        cmd = 'bash sync.sh {} {}'.format(self.config.source, self.config.destination)
+        return self.run_command(cmd)
+
+    def on_clean(self):
+        self.log.highlight("Cleaning {}".format(self.config.destination))
+        return True
+
+    def on_flat_correct(self):
+        self.log.highlight("Flat field correction ...")
+        info = self.read_info()
+        path = self.config.destination
+        data = dict(path=path, prefix=self.prefix, num=info['TOMO_N'], step=1)
+
+        cmd = ('tofu flatcorrect --verbose'
+               ' --reduction-mode median'
+               ' --projections "{path}/{prefix}*.edf"'
+               ' --darks {path}/darkend0000.edf'
+               ' --flats {path}/ref*_0000.edf'
+               ' --flats2 {path}/ref*_{num}.edf'
+               ' --number {num}'
+               ' --step {step}'
+               ' --absorptivity'
+               ' --fix-nan-and-inf'.format(**data))
+
+        large_cmd = cmd + ' --output {path}/fc/fc-%04i.tif'.format(path=path)
+        small_cmd = cmd + ' --resize 2 --output {path}/fc-small/fc-%04i.tif'.format(path=path)
+
+        if not self.run_command(small_cmd):
+            self.log.error("Could not create small flat field corrected projections")
+            return result
+
+        return self.run_command(large_cmd)
+
+    def on_quick_optimize(self):
+        self.log.highlight("Quick optimization ...")
 
-        result = self.run_command(cmd)
+        if self.optimize(True):
+            with open('params.json') as f:
+                opt = json.load(f)
+                self.log.highlight(" Optimal axis: {}".format(opt['lamino-angle']['value']))
+                self.log.highlight(" Optimal center: {}".format(opt['x-center']['value'] * 2.0))
+
+            return True
+
+        return False
 
-        if result == 0:
+    def on_optimize(self):
+        self.log.highlight("Optimizing ...")
+
+        if self.optimize(False):
             with open('params.json') as f:
                 opt = json.load(f)
                 self.log.highlight(" Optimal axis: {}".format(opt['lamino-angle']['value']))
                 self.log.highlight(" Optimal center: {}".format(opt['x-center']['value']))
 
-        return result
+            return True
+
+        return False
 
     def do_nothing(self):
         return True
@@ -381,27 +417,30 @@ class Application(object):
 
         machine = StateMachine()
 
-        quit = Action('q', 'Quit', self.on_quit, machine.QUIT)
+        quit = Action('e', 'Exit', self.on_quit, machine.QUIT)
         sync = Action('s', 'Sync', self.on_sync, machine.SYNC)
         flatcorrect = Action('f', 'Flat correct', self.on_flat_correct, machine.FLATCORRECT)
         clean = Action('c', 'Clean', self.on_clean, machine.CLEAN)
+        quick_optimize = Action('q', 'Quick', self.on_quick_optimize, machine.QUICK_OPTIMIZE)
         optimize = Action('o', 'Optimize', self.on_optimize, machine.OPTIMIZE)
 
         machine.add_action(machine.START, sync)
         machine.add_action(machine.START, flatcorrect)
+        machine.add_action(machine.START, quick_optimize)
         machine.add_action(machine.START, optimize)
-        machine.add_action(machine.START, clean)
         machine.add_action(machine.START, quit)
 
         machine.add_action(machine.SYNC, flatcorrect)
-        machine.add_action(machine.SYNC, clean)
         machine.add_action(machine.SYNC, quit)
 
+        machine.add_action(machine.FLATCORRECT, quick_optimize)
         machine.add_action(machine.FLATCORRECT, optimize)
         machine.add_action(machine.FLATCORRECT, quit)
 
+        machine.add_action(machine.QUICK_OPTIMIZE, sync)
+        machine.add_action(machine.QUICK_OPTIMIZE, quit)
+
         machine.add_action(machine.OPTIMIZE, sync)
-        machine.add_action(machine.OPTIMIZE, clean)
         machine.add_action(machine.OPTIMIZE, quit)
 
         machine.add_action(machine.CLEAN, sync)