def get_all_img_ROI(img, frame_data, roi_size=-1):
    #more generic function that tolerates different ROI size
    worms_in_frame = {}
    for irow, row in frame_data.iterrows():
        worm_roi = roi_size if roi_size > 0 else row['roi_size']

        worms_in_frame[irow] = getWormROI(img, row['coord_x'], row['coord_y'],
                                          worm_roi)
    return worms_in_frame
Пример #2
0
 def _get_roi_cnts(img, x, y, roi_size, thresh, max_area):
     
     worm_img, roi_corner = getWormROI(img, x, y, roi_size)
     worm_mask, worm_cnt, _ = getWormMask(worm_img, 
                                          thresh, 
                                          min_blob_area=max_area
                                          )
     if worm_cnt.size > 0:
         worm_cnt += roi_corner   
     return worm_cnt 
Пример #3
0
    def updateROIcanvas(self, wormCanvas, worm_index_roi, comboBox_ROI,
                        isDrawSkel):

        if self.frame_data is None:
            # no trajectories data presented, nothing to do here
            wormCanvas.clear()
            return

        # update valid index for the comboBox
        comboBox_ROI.clear()
        comboBox_ROI.addItem(str(worm_index_roi))

        for ind in self.frame_data[self.worm_index_type].data:
            comboBox_ROI.addItem(str(ind))

        # extract individual worm ROI
        good = self.frame_data[self.worm_index_type] == worm_index_roi
        row_data = self.frame_data.loc[good].squeeze()

        if row_data.size == 0 or np.isnan(row_data['coord_x']) or np.isnan(
                row_data['coord_y']):
            # invalid data nothing to do here
            wormCanvas.clear()
            return

        worm_img, roi_corner = getWormROI(self.frame_img, row_data['coord_x'],
                                          row_data['coord_y'],
                                          row_data['roi_size'])

        roi_ori_size = worm_img.shape

        worm_img = np.ascontiguousarray(worm_img)
        worm_qimg = self._convert2Qimg(worm_img)

        canvas_size = min(wormCanvas.height(), wormCanvas.width())
        worm_qimg = worm_qimg.scaled(canvas_size, canvas_size,
                                     Qt.KeepAspectRatio)

        worm_qimg = self.drawSkelResult(worm_img,
                                        worm_qimg,
                                        row_data,
                                        isDrawSkel,
                                        roi_corner,
                                        read_center=False)

        pixmap = QPixmap.fromImage(worm_qimg)
        wormCanvas.setPixmap(pixmap)
Пример #4
0
    def _updateROI(self, roi):

        if self.frame_data is None or not self.worm_index_type:
            # no trajectories data presented, nothing to do here
            roi.wormCanvas.clear()
            return

        self.updateROIcomboBox(roi)

        # extract individual worm ROI
        good = self.frame_data[self.worm_index_type] == roi.worm_index
        row_data = self.frame_data.loc[good].squeeze()

        if row_data.size == 0 or \
        np.isnan(row_data['coord_x']) or \
        np.isnan(row_data['coord_y']):
            # invalid data nothing to do here
            roi.wormCanvas.clear()
            return

        worm_img, roi_corner = getWormROI(self.frame_img, row_data['coord_x'],
                                          row_data['coord_y'],
                                          row_data['roi_size'])

        roi_ori_size = worm_img.shape
        worm_img = np.ascontiguousarray(worm_img)
        worm_qimg = self._convert2Qimg(worm_img)

        canvas_size = min(roi.wormCanvas.height(), roi.wormCanvas.width())
        worm_qimg = worm_qimg.scaled(canvas_size, canvas_size,
                                     Qt.KeepAspectRatio)

        worm_qimg = self.drawSkelResult(worm_img,
                                        worm_qimg,
                                        row_data,
                                        roi.isDrawSkel,
                                        roi_corner,
                                        read_center=False)

        pixmap = QPixmap.fromImage(worm_qimg)
        roi.wormCanvas.setPixmap(pixmap)
Пример #5
0
    frame_data = traj_g.get_group(tt)

    ts_g = timeseries_data.groupby('timestamp')
    ts_frame_data = ts_g.get_group(tt)

    with tables.File(mask_file, 'r') as fid:
        img = fid.get_node('/mask')[tt]

        save_full = fid.get_node('/full_data')._v_attrs['save_interval']
        img_full = fid.get_node('/full_data')[tt // save_full]

    row = frame_data[frame_data['worm_index_joined'] == w_index].iloc[0]

    fig, axs = plt.subplots(1, 3, sharex=True, sharey=True)
    #### ROI RAW ####
    worm_full, roi_corner = getWormROI(img_full, row['coord_x'],
                                       row['coord_y'], row['roi_size'])

    axs[0].imshow(worm_full, interpolation='none', cmap='gray')
    axs[0].axis('off')

    #### ROI + SKELETON ####
    worm_img, roi_corner = getWormROI(img, row['coord_x'], row['coord_y'],
                                      row['roi_size'])

    axs[1].imshow(worm_img, interpolation='none', cmap='gray')
    axs[1].axis('off')

    r_ind = ts_frame_data[ts_frame_data['worm_index'] == w_index].index[0]
    with tables.File(mask_file, 'r') as fid:
        stage_position_pix = fid.get_node('/stage_position_pix')[:]
def getIndCnt(img, x, y, roi_size, thresh, max_area):
    worm_img, roi_corner = getWormROI(img, x, y, roi_size)
    worm_mask, worm_cnt, _ = getWormMask(worm_img, thresh)
    if worm_cnt.size > 0:
        worm_cnt += roi_corner   
    return worm_cnt                 
def getIntensityProfile(masked_image_file,
                        skeletons_file,
                        intensities_file,
                        width_resampling=15,
                        length_resampling=131,
                        min_num_skel=100,
                        smooth_win=11,
                        pol_degree=3,
                        width_percentage=0.5,
                        save_maps=False):

    min_num_skel = min_num_skel_defaults(skeletons_file,
                                         min_num_skel=min_num_skel)

    assert smooth_win > pol_degree
    assert min_num_skel > 0
    assert 0 < width_percentage < 1

    # we want to use symetrical distance centered in the skeleton
    if length_resampling % 2 == 0:
        length_resampling += 1
    if width_resampling % 2 == 0:
        width_resampling += 1

    # get the limits to be averaged from the intensity map
    if save_maps:
        width_win_ind = getWidthWinLimits(width_resampling, width_percentage)
    else:
        width_win_ind = (0, width_resampling)

    # filters for the tables structures
    table_filters = tables.Filters(complevel=5,
                                   complib='zlib',
                                   shuffle=True,
                                   fletcher32=True)

    # Get a reduced version of the trajectories_data table with only the valid skeletons.
    # The rows of this new table are going to be saved into skeletons_file
    trajectories_data_valid = setIntMapIndexes(skeletons_file, min_num_skel)

    # let's save this new table into the intensities file
    with tables.File(intensities_file, 'w') as fid:
        fid.create_table('/',
                         'trajectories_data_valid',
                         obj=trajectories_data_valid.to_records(index=False),
                         filters=table_filters)

    tot_rows = len(trajectories_data_valid)
    if tot_rows == 0:
        with tables.File(intensities_file, "r+") as int_file_id:
            # nothing to do here let's save empty data and go out
            worm_int_avg_tab = int_file_id.create_array(
                "/", "straighten_worm_intensity_median", obj=np.zeros(0))
            worm_int_avg_tab._v_attrs['has_finished'] = 1
        return

    with tables.File(masked_image_file, 'r')  as mask_fid, \
            tables.File(skeletons_file, 'r') as ske_file_id, \
            tables.File(intensities_file, "r+") as int_file_id:

        # pointer to the compressed videos
        mask_dataset = mask_fid.get_node("/mask")

        # pointer to skeletons
        skel_tab = ske_file_id.get_node('/skeleton')
        skel_width_tab = ske_file_id.get_node('/width_midbody')

        filters = tables.Filters(complevel=5, complib='zlib', shuffle=True)

        # we are using Float16 to save space, I am assuing the intensities are
        # between uint8
        worm_int_avg_tab = int_file_id.create_carray(
            "/",
            "straighten_worm_intensity_median",
            tables.Float16Atom(dflt=np.nan), (tot_rows, length_resampling),
            chunkshape=(1, length_resampling),
            filters=table_filters)

        worm_int_avg_tab._v_attrs['has_finished'] = 0
        worm_int_avg_tab.attrs['width_win_ind'] = width_win_ind

        if save_maps:
            worm_int_tab = int_file_id.create_carray(
                "/",
                "straighten_worm_intensity",
                tables.Float16Atom(dflt=np.nan),
                (tot_rows, length_resampling, width_resampling),
                chunkshape=(1, length_resampling, width_resampling),
                filters=table_filters)

        grouped_frames = trajectories_data_valid.groupby('frame_number')
        # variables used to report progress
        base_name = skeletons_file.rpartition('.')[0].rpartition(
            os.sep)[-1].rpartition('_')[0]
        progressTime = TimeCounter('Obtaining intensity maps.',
                                   len(grouped_frames))

        for frame, frame_data in grouped_frames:
            img = mask_dataset[frame, :, :]
            for ii, row_data in frame_data.iterrows():
                skeleton_id = int(row_data['skeleton_id'])
                worm_index = int(row_data['worm_index_joined'])
                int_map_id = int(row_data['int_map_id'])

                # read ROI and skeleton, and put them in the same coordinates
                # map
                worm_img, roi_corner = getWormROI(img, row_data['coord_x'],
                                                  row_data['coord_y'],
                                                  row_data['roi_size'])
                skeleton = skel_tab[skeleton_id, :, :] - roi_corner

                half_width = skel_width_tab[skeleton_id] / 2
                assert not np.isnan(skeleton[0, 0])

                skel_smooth = smoothSkeletons(
                    skeleton,
                    length_resampling=length_resampling,
                    smooth_win=smooth_win,
                    pol_degree=pol_degree)
                straighten_worm, grid_x, grid_y = getStraightenWormInt(
                    worm_img,
                    skel_smooth,
                    half_width=half_width,
                    width_resampling=width_resampling)

                # if you use the mean it is better to do not use float16
                int_avg = np.median(
                    straighten_worm[width_win_ind[0]:width_win_ind[1], :],
                    axis=0)

                worm_int_avg_tab[int_map_id] = int_avg

                # only save the full map if it is specified by the user
                if save_maps:
                    worm_int_tab[int_map_id] = straighten_worm.T

            if frame % 500 == 0:
                progress_str = progressTime.get_str(frame)
                print_flush(base_name + ' ' + progress_str)

        worm_int_avg_tab._v_attrs['has_finished'] = 1