Skip to content

Commit

Permalink
support multi-position in per-stack and per-slice (#181)
Browse files Browse the repository at this point in the history
* support multi-position in per-stack and per-slice

* Change z-step mod

Co-authored-by: Kevin Dean <[email protected]>
  • Loading branch information
annie-xd-wang and zacsimile authored Aug 25, 2022
1 parent b671e56 commit a250768
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 30 deletions.
6 changes: 5 additions & 1 deletion src/aslm/model/aslm_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,9 @@ def run_single_acquisition(self):
channel_idx = int(channel_key[prefix_len:])
self.run_single_channel_acquisition_with_features(channel_idx)

if self.imaging_mode == 'z-stack':
break

def snap_image(self, channel_key):
r"""Acquire an image after updating the waveforms.
Expand Down Expand Up @@ -903,9 +906,10 @@ def run_single_channel_acquisition_with_features(self, target_channel=1):
return

self.signal_container.reset()
self.target_channel = target_channel

while not self.signal_container.end_flag and not self.stop_send_signal and not self.stop_acquisition:
self.run_single_channel_acquisition(target_channel)
self.run_single_channel_acquisition(self.target_channel)
if not hasattr(self, 'signal_container'):
return

Expand Down
95 changes: 66 additions & 29 deletions src/aslm/model/model_features/aslm_common_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ def __init__(self, model):

self.number_z_steps = 0
self.start_z_position = 0
self.end_z_position = 0
self.start_focus = 0
self.end_focus = 0
self.z_step_size = 0
self.focus_step_size = 0
self.timepoints = 0
Expand All @@ -94,6 +92,11 @@ def __init__(self, model):
self.current_z_position = 0
self.current_focus_position = 0
self.need_to_move_new_position = True
self.need_to_move_z_position = True
self.z_position_moved_time = 0

self.stack_cycling_mode = 'per_stack'
self.channels = [1]

self.config_table = {'signal': {'init': self.pre_signal_func,
'main': self.signal_func,
Expand All @@ -104,15 +107,21 @@ def __init__(self, model):
def pre_signal_func(self):
microscope_state = self.model.experiment.MicroscopeState

self.stack_cycling_mode = microscope_state['stack_cycling_mode']
# get available channels
prefix_len = len('channel_')
self.channels = [int(channel_key[prefix_len:]) for channel_key in microscope_state['channels']]
self.current_channel_in_list = 0

self.number_z_steps = int(microscope_state['number_z_steps'])

self.start_z_position = float(microscope_state['start_position'])
self.end_z_position = float(microscope_state['end_position'])
self.z_step_size = (self.end_z_position - self.start_z_position) / self.number_z_steps
end_z_position = float(microscope_state['end_position'])
self.z_step_size = float(microscope_state['step_size'])

self.start_focus = float(microscope_state['start_focus'])
self.end_focus = float(microscope_state['end_focus'])
self.focus_step_size = (self.end_focus - self.start_focus) / self.number_z_steps
end_focus = float(microscope_state['end_focus'])
self.focus_step_size = (end_focus - self.start_focus) / self.number_z_steps

self.timepoints = int(microscope_state['timepoints'])

Expand All @@ -129,10 +138,11 @@ def pre_signal_func(self):
}
})
self.current_position_idx = 0
self.current_z_position = 0
self.current_focus_position = 0
self.z_position_moved_time = 0
self.need_to_move_new_position = True
self.need_to_move_z_position = True
self.current_z_position = self.start_z_position + self.positions[self.current_position_idx]['z']
self.current_focus_position = self.start_focus + self.positions[self.current_position_idx]['f']

self.restore_z = -1

Expand All @@ -159,30 +169,36 @@ def signal_func(self):
self.model.pause_data_event.set()
self.model.pause_data_ready_lock.release()

self.z_position_moved_time = 0
# calculate first z, f position
self.current_z_position = self.start_z_position + self.positions[self.current_position_idx]['z']
self.current_focus_position = self.start_focus + self.positions[self.current_position_idx]['f']
# move to next position
self.current_position_idx += 1
# self.z_position_moved_time = 0
# # calculate first z, f position
# self.current_z_position = self.start_z_position + self.positions[self.current_position_idx]['z']
# self.current_focus_position = self.start_focus + self.positions[self.current_position_idx]['f']

# move z, f
self.model.pause_data_ready_lock.acquire()
self.model.ask_to_pause_data_thread = True
self.model.pause_data_ready_lock.acquire()
if self.need_to_move_z_position:
# move z, f
self.model.pause_data_ready_lock.acquire()
self.model.ask_to_pause_data_thread = True
self.model.pause_data_ready_lock.acquire()

self.model.move_stage({'z_abs': self.current_z_position, 'f_abs': self.current_focus_position}, wait_until_done=True)
self.model.move_stage({'z_abs': self.current_z_position, 'f_abs': self.current_focus_position}, wait_until_done=True)

self.model.ask_to_pause_data_thread = False
self.model.pause_data_event.set()
self.model.pause_data_ready_lock.release()
self.model.ask_to_pause_data_thread = False
self.model.pause_data_event.set()
self.model.pause_data_ready_lock.release()

if self.stack_cycling_mode != 'per_stack':
# update channel for each z position in 'per_slice'
self.update_channel()
self.need_to_move_z_position = (self.current_channel_in_list == 0)

# next z, f position
self.current_z_position += self.z_step_size
self.current_focus_position += self.focus_step_size
# in 'per_slice', move to next z position if all the channels have been acquired
if self.need_to_move_z_position:
# next z, f position
self.current_z_position += self.z_step_size
self.current_focus_position += self.focus_step_size

# update z position moved time
self.z_position_moved_time += 1
# update z position moved time
self.z_position_moved_time += 1

return True

Expand All @@ -193,8 +209,25 @@ def signal_end(self):

# decide whether to move X,Y,Theta
if self.z_position_moved_time >= self.number_z_steps:
self.need_to_move_new_position = True
if self.current_position_idx == len(self.positions):
self.z_position_moved_time = 0
# calculate first z, f position
self.current_z_position = self.start_z_position + self.positions[self.current_position_idx]['z']
self.current_focus_position = self.start_focus + self.positions[self.current_position_idx]['f']

# after running through a z-stack, update channel
if self.stack_cycling_mode == 'per_stack':
self.update_channel()
# if run through all the channels, move to next position
if self.current_channel_in_list == 0:
self.need_to_move_new_position = True
else:
self.need_to_move_new_position = True

if self.need_to_move_new_position:
# move to next position
self.current_position_idx += 1

if self.need_to_move_new_position and self.current_position_idx == len(self.positions):
self.timepoints -= 1
self.current_position_idx = 0

Expand All @@ -209,6 +242,10 @@ def generate_meta_data(self, *args):
# print('This frame: z stack', self.model.frame_id)
return True

def update_channel(self):
self.current_channel_in_list = (self.current_channel_in_list+1) % len(self.channels)
self.model.target_channel = self.channels[self.current_channel_in_list]


class FindTissueSimple2D:
def __init__(self, model, overlap=0.1, target_resolution='high'):
Expand Down

0 comments on commit a250768

Please sign in to comment.