示例#1
0
    def setUp(self):
        self.stage  = pos.StagePosition(x=0)
        self.stage2 = pos.StagePosition(x=3, y=4, z=0)
        self.stage3 = pos.StagePosition(x=0, y=0, z=0)
        self.stage4 = pos.StagePosition(x=100, y=30, z=15)
        self.stage5 = pos.StagePosition(x=0, y=0, z=0)

        self.stagelist = [self.stage, self.stage2, self.stage3, self.stage4]
        self.stagelist2 = [self.stage2, self.stage]

        print (self.stage)
        print (self.stage2)
        print (self.stage3)
        print (self.stage4)
示例#2
0
def get_center(mmc, center, frame_to_pixel_ratio, camera_pixel_width,
               camera_pixel_height):
    cur_position = pos.current(mmc)
    x_change = (center[0] -
                (float(camera_pixel_width) / 2)) * frame_to_pixel_ratio
    y_change = (center[1] -
                (float(camera_pixel_height) / 2)) * frame_to_pixel_ratio
    new_x = cur_position.x - x_change
    new_y = cur_position.y - y_change
    return pos.StagePosition(x=new_x, y=new_y)
示例#3
0
    def get_position_list(self, focused_pl):
        ''' Calculates the position list for imaging

        args:
            focused_pl: The position list of the focused interior points.
            
        returns: PositionList()
        '''
        # get the focus model from the focused points
        focus_func = focus.predict_z_height(focused_pl)

        x_steps = int(
            np.ceil(self.chip['number_of_streets'] /
                    self.number_of_apartments_in_frame_x))
        y_steps = int(
            np.ceil(self.chip['number_of_apartments'] /
                    self.number_of_apartments_in_frame_y))
        x_step_size = self.number_of_apartments_in_frame_x * self.chip[
            'street_spacing']
        y_step_size = self.number_of_apartments_in_frame_y * self.chip[
            'apartment_spacing']

        # Get 2D rotation matix for origin point
        origin = np.matmul(np.linalg.inv(
            self.R), [self.corner_poslist[0].x, self.corner_poslist[0].y])

        poslist = pos.PositionList()
        x_list = range(x_steps)
        for y_ctr in range(y_steps):
            for x_ctr in x_list:
                rotation = origin + [
                    self.first_position[0] + x_step_size * x_ctr,
                    self.first_position[1] - y_step_size * y_ctr
                ]
                posit = np.matmul(self.R, rotation)
                name = ("_ST_" + str(
                    x_ctr * self.number_of_apartments_in_frame_x).zfill(3) +
                        "_APT_" +
                        str(y_ctr *
                            self.number_of_apartments_in_frame_y).zfill(3) +
                        "_")
                s = pos.StagePosition(x=posit[0],
                                      y=posit[1],
                                      z=focus_func(posit[0], posit[1])[0],
                                      name=name)
                poslist.append(s)
            # Each time though reverse the order to snake through chip
            x_list = x_list[::-1]
        return poslist
示例#4
0
def focus_from_image_stack(xy_points, mmc, delta_z=5, total_z=150, exposure=1):
    ''' Brute force focus algorthim 
    
    args: 
        xy_points: a PositionList() of the locations to 
            focus at
        mmc: Micromanger instance
        delta_z: distacne between images (um)
        total_z: total imaging distance (all focused points on 
            chip should be in this range)
    
    returns:
        focused PositionList()
    '''
    # Get focus model
    focus_model = miq.get_classifier()
    pos_list = pos.PositionList()
    # make z position array
    cur_pos = pos.current(mmc)
    z = get_z_list(cur_pos.z, delta_z, total_z)
    cam = sc_utils.start_cam()
    for posit in xy_points:
        # Go to the x,y position 
        pos.set_pos(mmc, x=posit.x, y=posit.y)
        preds = []
        for curr_z in z:
            mmc.setPosition(curr_z)
            mmc.waitForSystem()
            frame = cam.get_frame(exp_time=exposure).reshape(cam.sensor_size[::-1])
            preds.append(focus_model.score(sc_utils.bytescale(frame, high=65535)))
        # find the index of the min focus prediction
        best_focus_index = np.argmin(preds)
        # append to the PositionList 
        sp = pos.StagePosition(x=posit.x, y=posit.y,
                               z=z[best_focus_index])
        pos_list.append(sp)
    
    sc_utils.close_cam(cam)
    return pos_list
示例#5
0
    def get_focus_position_list(self, fp_x, fp_y):
        ''' Gets the xy stage positions for the 
        interior focus points 
        args:
            fp_x: number of focus points in the x direction
            fp_y: number of focus points in the y direction

        returns: PositionList()
        '''
        print("--------------")
        print(self.first_position[0])
        print(self.first_position[1])

        delta_x = (self.total_x - np.abs(self.first_position[0]) * 2) / (fp_x -
                                                                         1)
        delta_y = (self.total_y - np.abs(self.first_position[1]) * 2) / (fp_y -
                                                                         1)
        origin = np.matmul(np.linalg.inv(
            self.R), [self.corner_poslist[0].x, self.corner_poslist[0].y])

        print(delta_x)
        print(delta_y)

        fp_positions = pos.PositionList()
        fp_x_list = range(fp_x)
        for y_ctr in range(fp_y):
            for x_ctr in fp_x_list:
                rotation = origin + [
                    self.first_position[0] + delta_x * x_ctr,
                    (self.first_position[1] - delta_y * y_ctr)
                ]
                fp = np.matmul(self.R, rotation)
                s = pos.StagePosition(x=fp[0], y=fp[1])
                fp_positions.append(s)
            fp_x_list = fp_x_list[::-1]
        return fp_positions
示例#6
0
def focus_from_last_point(xy_points, mmc, model_path, delta_z=10, total_z=150, next_point_range=35, exposure=1):
    ''' Gets a focused position list using a brute force method to find the 
    first focus point, then after that, used the last focused point as the 
    center of the new, shorter focus range. 

    args: 
        xy_points: a PositionList() of the locations to 
            focus at
        mmc: Micromanger instance
        delta_z: distacne between images (um)
        total_z: total imaging distance (all focused points on 
            chip should be in this range)
        nex_point_range: range to look (um) for point other than the 
            first point
    
    returns:
        focused PositionList()

    '''
    focus_model = miq.get_classifier(model_path)
    pos_list = pos.PositionList()

    # Focus the first point 
    pos.set_pos(mmc, x=xy_points[0].x, y=xy_points[0].y)
    last_z = focus_point(mmc, focus_model, delta_z=delta_z, total_z=total_z, exposure=exposure)
    sp = pos.StagePosition(x=xy_points[0].x, y=xy_points[0].y,
                            z=last_z)
    pos_list.append(sp)

    z_range = np.arange(-next_point_range/2, next_point_range/2, delta_z)
    cam = sc_utils.start_cam()
    for i, posit in enumerate(xy_points):
        # We already did the first point
        if i == 0:
            best_focus_index = 0
            continue
        
        preds = []
        # Go to the next x,y position with the previous best focus 
        pos.set_pos(mmc, x=posit.x, y=posit.y, z=last_z)
        
        if best_focus_index > (len(z_range) / 2):
            # Reverse the order of the list
            z_range = z_range[::-1]

        # Build list from last position
        # in order that makes sense 
        z_list = [(last_z+i) for i in z_range]

        for j, curr_z in enumerate(z_list):
            pos.set_pos(mmc, z=curr_z)
            frame = sc_utils.get_live_frame(cam, exposure)
            preds.append(focus_model.score(sc_utils.bytescale(frame, high=65535)))

            if j > 1:
                if ((preds[j] > preds[j-1]) and (preds[j] > preds[j-2]) and 
                    (np.abs(preds[j] - preds[j-1]) > 2 or np.abs(preds[j] - preds[j-2]) > 2)):
                    # Focus got worse
                    break
        # find the index of the min focus prediction
        best_focus_index = np.argmin(preds)
        # append to the PositionList 
        last_z = z_list[best_focus_index]
        sp = pos.StagePosition(x=posit.x, y=posit.y,
                                z=last_z)
        pos_list.append(sp)

        if len(preds) < len(z_list):
            sc_utils.print_info ('('+ str(posit.x) + ',' +  str(posit.y) +  ') - Score: ' + str(np.min(preds)) +  ' - Good focus')
        elif preds[best_focus_index] > 5:
            sc_utils.print_info ('('+ str(posit.x) + ',' +  str(posit.y) +  ') - Score: ' + str(np.min(preds)) +  ' - BAD FOCUS')
        else:
            sc_utils.print_info ('('+ str(posit.x) + ',' +  str(posit.y) +  ') - Score: ' + str(np.min(preds)) +  ' - OK focus')
    
    sc_utils.close_cam(cam)
    return pos_list
示例#7
0
def auto_image_chip(cur_chip, mmc, save_dir, chip_number, alignment_model_path,
                    focus_model_path, naming_scheme, focus_delta_z,
                    focus_total_z, focus_next_point_range,
                    number_of_focus_points_x, number_of_focus_points_y,
                    focus_exposure, image_rotation, frame_to_pixel_ratio,
                    camera_pixels, exposure, first_position,
                    number_of_apartments_in_frame_x,
                    number_of_apartments_in_frame_y, output_pixels):
    ''' Aligns, focuses, and images given chip

    args:
        chip: an instance of a Chip class (found in chip.py)
        mmc: Micro-Manager instance (can get from sc_utils.get_stage_controller())
        save_dir: directory to save images to 
        chip_number: string to identify chip
        alignment_model_name: name of the trained model for alignment
        alignment_model_path: path to the trained model for alignment
        naming_scheme: Beginning of image file names
        focus_delta_z: amount to change in the z direction between getting 
                       focus prediction
        focus_total_z: total z in each direction to look for an individual 
                       focus point or the first focus point in a set
        focus_next_point_range: total z in each direction to look for each 
                       focus point that directly follows a nearby point that 
                       we have already focused (this will need to be increased
                       for chips that are not relatively flat)
        number_of_focus_points_x: Number of positions to focus at in the x 
                       direction (must be greater than 3 for interpolation to 
                       work propertly)
        number_of_focus_points_y: Number of positions to focus at in the y 
                       direction (must be greater than 3 for interpolation to 
                       work propertly)
        save_jpg: Saves images as both tiff files and jpg files if True
    '''
    start = time.time()
    sc_utils.print_info("Starting: Alignment, Focus, and Imaging")

    model = alignment.get_inference_model(alignment_model_path)
    p1 = pos.current(mmc)
    p2 = pos.StagePosition(x=p1.x + cur_chip['chip_width'], y=p1.y)
    p3 = pos.StagePosition(x=p1.x + cur_chip['chip_width'],
                           y=p1.y - cur_chip['chip_height'])

    # Create a temporay chip for focusing
    temp_corners = pos.PositionList(positions=[p1, p2, p3])
    print('Corners: ', str(temp_corners))
    temp_chip = chip.Chip(temp_corners, first_position, cur_chip,
                          number_of_apartments_in_frame_x,
                          number_of_apartments_in_frame_y)

    focus_pl = temp_chip.get_focus_position_list(number_of_focus_points_x,
                                                 number_of_focus_points_y)
    print('Focus PL: ', str(focus_pl))
    # return
    focused_pl = focus.focus_from_last_point(
        focus_pl,
        mmc,
        focus_model_path,
        delta_z=focus_delta_z,
        total_z=focus_total_z,
        next_point_range=focus_next_point_range,
        exposure=focus_exposure)
    focused_pl.save('focused_pl', save_dir)

    p1.z = focus.predict_z_height(focused_pl, xy_location=(p1.x, p1.y))[0][0]
    p2.z = focus.predict_z_height(focused_pl, xy_location=(p2.x, p2.y))[0][0]
    p3.z = focus.predict_z_height(focused_pl, xy_location=(p3.x, p3.y))[0][0]

    print(p1)

    p1 = alignment.search_and_find_center(mmc, p1, model, exposure,
                                          frame_to_pixel_ratio,
                                          camera_pixels[0], camera_pixels[1])
    p2 = alignment.search_and_find_center(mmc, p2, model, exposure,
                                          frame_to_pixel_ratio,
                                          camera_pixels[0], camera_pixels[1])
    p3 = alignment.search_and_find_center(mmc, p3, model, exposure,
                                          frame_to_pixel_ratio,
                                          camera_pixels[0], camera_pixels[1])

    align_time = time.time()
    sc_utils.print_info('Time for alignment:' + str(align_time - start))

    # Create a Position List of the corners and save it
    corners = pos.PositionList(positions=[p1, p2, p3])
    corners.save('corners_pl', save_dir)
    # # Create a chip instance
    imaging_chip = chip.Chip(corners, first_position, cur_chip,
                             number_of_apartments_in_frame_x,
                             number_of_apartments_in_frame_y)
    imaging_pl = imaging_chip.get_position_list(focused_pl)
    imaging_pl.image(mmc,
                     save_dir,
                     naming_scheme,
                     rotation=image_rotation,
                     exposure=exposure,
                     output_pixels=output_pixels)

    end = time.time()
    sc_utils.print_info('Total time:' + str(end - start))