Beispiel #1
0
def extract_super_pixels(mat_adj=None,
                         test_left=None,
                         test_right=None,
                         mat_cat=None,
                         num_neighbors=8,
                         cor_choice='mean',
                         connectivity=None,
                         min_pixels=50,
                         image=None,
                         plot=False,
                         use_mean_image=False):
    if image is None:
        if mat_cat is None:
            mat_cat = torch.cat([m[test_left:test_right] for m in mat_adj],
                                dim=0)
        cor_global = neighbor_cor(mat_cat,
                                  neighbors=num_neighbors,
                                  plot=plot,
                                  choice=cor_choice,
                                  title='correlation map')
        if use_mean_image:
            cor_global = mat_cat.mean(0) * cor_global
            cor_global = cor_global / cor_global.max()
        image = cor_global.detach().cpu().numpy()
    else:
        cor_global = image  # for backward compatibility
    if isinstance(image, torch.Tensor):
        image = image.detach().cpu().numpy()
    label_image, regions = get_label_image(image,
                                           min_pixels=min_pixels,
                                           connectivity=connectivity,
                                           plot=False)
    if plot:
        plot_image_label_overlay(image, label_image)
    return cor_global, label_image, regions
Beispiel #2
0
def detrend_high_magnification(mat, skip_segments=1, num_segments=6, period=500, train_size_left=0, train_size_right=350, 
                               linear_order=3, plot=False, signal_start=0, signal_end=100, filepath=None, size=(-1, 180, 300), 
                               device=torch.device('cuda'), start0=None, end0=None, return_mat=False, **kwargs):
    if mat is None:
        mat = load_file(filepath=filepath, size=size, dtype=np.uint16, device=device)
    L, nrow, ncol = mat.size()
    if period == 'unknown':
        period = L
    if signal_end == 'period':
        signal_end = period
    train_idx = ([range(skip_segments*period)] +
                 [range(i*period, train_size_left+i*period) for i in range(skip_segments, num_segments)] + 
                 [range((i+1)*period - train_size_right, (i+1)*period) for i in range(skip_segments, num_segments)])
    train_idx = functools.reduce(lambda x,y: list(x)+list(y), train_idx)
    input_aug = torch.linspace(-2, 2, L, device=device)
    beta, trend = linear_regression(X=input_aug[train_idx], Y=mat.reshape(L, -1)[train_idx], order=linear_order, X_test=input_aug)
    mat_adj = mat - trend.reshape(L, nrow, ncol)
    if plot:
        frame_mean = mat.mean(-1).mean(-1)
        plt.figure(figsize=(20, 10))
        plt.plot(frame_mean.cpu(), 'o--', linewidth=1, markersize=2)
        for i in range(num_segments):
            plt.axvline(i*period+signal_start, color='g', linestyle='-.')
            plt.axvline(i*period+signal_end, color='g', linestyle='-.')
        plt.title('Frame mean intensity')
        plt.show()
        imshow(mat.mean(0), title='Mean intensity')
        imshow(trend.mean(0).reshape(nrow, ncol), title='Trend')
        imshow(mat_adj.mean(0).reshape(nrow, ncol), title='Detrended')

        cor = neighbor_cor(mat, neighbors=8, plot=True, choice='max', title='cor mat')
        plot_hist(cor, show=True)
        cor = neighbor_cor(trend.reshape(-1, nrow, ncol), neighbors=8, plot=True, choice='max', title='cor trend')
        plot_hist(cor, show=True)
        cor = neighbor_cor(mat_adj.reshape(-1, nrow, ncol), neighbors=8, plot=True, choice='max', title='cor mat_adj')
        plot_hist(cor, show=True)
    
        plot_mean_intensity(mat, detrended=mat_adj, plot_detrended=True, plot_segments=True, num_frames=L, period=period, 
                            signal_length=signal_end-signal_start)
    if start0 is not None and end0 is not None:
        mat_adj = [mat_adj[s:e] for s, e in zip(start0, end0)]
    if return_mat:
        return mat, mat_adj
    else:
        return mat_adj
Beispiel #3
0
 def get_trace(mat=None,
               mat_adj=None,
               seg_idx=None,
               submat=None,
               weight_percentile=weight_percentile,
               plot_singular_values=plot_singular_values):
     if submat is None:
         if mat is None:
             mat = mat_adj[seg_idx]
         submat = mat[:, minr:maxr, minc:maxc]
     if cor is None:
         cor_ = neighbor_cor(submat,
                             neighbors=8,
                             plot=False,
                             choice='max',
                             title='cor',
                             return_adj_list=False)
     else:
         cor_ = cor[minr:maxr, minc:maxc]
     if weight_percentile is None:
         weight = cor_
     else:
         weight = nn.functional.threshold(cor_,
                                          np.percentile(
                                              cor_[label_mask.bool()].cpu(),
                                              weight_percentile),
                                          0,
                                          inplace=False)
     if weight.sum() == 0:
         weight = 1
     denominator = (
         label_mask *
         weight).sum() if weighted_denominator else label_mask.sum()
     trace = ((submat * label_mask * weight).sum(-1).sum(-1) / denominator)
     if plot_singular_values:
         u, s, v = torch.svd(submat.reshape(len(submat), -1))
         plot_tensor(s,
                     marker='o--',
                     linewidth=1,
                     figsize=(20, 10),
                     show=True)
     return submat, trace, weight