Exemplo n.º 1
0
def center_spline(traceback, distances, smoothing=None):
    '''Generate spline corresponding to the centerline of the worm
    
    Parameters:
    ------------
    traceback: list of 2-d tuples
        List of indices associated with the centerline, starting with
        one of the endpoints of the centerline and ending with the ending
        index of the centerline. 
    distances: ndarray shape(n,m)
        Distance transform from the medial axis transform of the worm mask

    Returns:
    -----------
    tck: parametric spline tuple
        spline tuple (see documentation for zplib.interpolate for more info)
    '''

    #NOTE: we will extrapolate the first/last few pixels to get the full length of the worm,
    #since medial axis transforms/skeltons don't always go to the edge of the mask
    #get the x,y positions for the centerline that can be
    #inputted into the fit spline function
    #NOTE: need to use traceback since order matters for spline creation
    if len(traceback) == 0:
        return (0, 0, 0)

    points = np.array(list(np.transpose(traceback))).T
    #print(points.shape)
    widths = distances[list(np.transpose(traceback))]

    if smoothing is None:
        smoothing = 0.2 * len(widths)

    #create splines for the first and last few points
    begin_tck = interpolate.fit_spline(points[:10],
                                       smoothing=smoothing,
                                       order=1)
    begin_xys = interpolate.spline_evaluate(
        begin_tck, np.linspace(-widths[0], 0, int(widths[0]), endpoint=False))
    #print(begin_xys.shape)
    #print(points[-10:])
    end_tck = interpolate.fit_spline(points[-10:],
                                     smoothing=smoothing,
                                     order=1)
    tmax = end_tck[0][-1]
    #print(tmax)
    #print(widths[-1])
    #print(tmax+tmax/widths[-1])
    #print(tmax+widths[-1])
    #remove the first point to prevent duplicate points in the end tck
    end_xys = interpolate.spline_evaluate(
        end_tck, np.linspace(tmax, tmax + widths[-1], int(widths[-1] + 1)))[1:]
    #print(end_xys)
    new_points = np.concatenate((begin_xys, points, end_xys))
    #print(new_points)
    #print("new_points: "+str(new_points.shape))
    tck = interpolate.fit_spline(new_points, smoothing=smoothing)
    return tck
Exemplo n.º 2
0
def extrapolate_head(tck, width_tck, smoothing=None):
    '''Since the width/length splines don't get to the end of the worm,
    try to extrapolate the widths and such to the end of the worm mask

    NOTE: Not really used in the spline-fitting pipeline
    '''
    #get first and last points from the width_tck
    width_ends = interpolate.spline_interpolate(width_tck, 2)
    print(width_ends)
    #calculate new x's and y's that go to the end of the mask
    tmax = tck[0][-1]
    print("tmax: " + str(tmax))
    xys = interpolate.spline_evaluate(
        tck, np.linspace(-width_ends[0], tmax + width_ends[1], 600))
    #print(xys.shape)

    #interpolate the widths so that we can add on the end widths
    #NOTE: end widths will be zero since they are at the end of the mask
    widths = interpolate.spline_interpolate(width_tck, 600)
    new_widths = np.concatenate([[0], widths, [0]])
    #need to generate new x's to use to re-make the width splines with new_widths
    #new endpoint xs need to be reflective of where they are on the centerline
    new_xs = np.concatenate([[-widths[0] / tmax],
                             np.linspace(0, 1, 600), [1 + widths[-1] / tmax]])

    #re-make the splines
    if smoothing is None:
        smoothing = 0.2 * len(new_widths)

    new_tck = interpolate.fit_spline(xys, smoothing=smoothing)

    new_width_tck = interpolate.fit_nonparametric_spline(new_xs,
                                                         new_widths,
                                                         smoothing=smoothing)
    return new_tck, new_width_tck
Exemplo n.º 3
0
def save_landmarks(metadata, landmark_file):
    spine_tck = metadata['spine_tck']
    vulva_t = metadata['vulva_t']
    tail_t = spine_tck[0][-1]
    t_values = [0, vulva_t, tail_t]
    positions = interpolate.spline_evaluate(spine_tck, t_values)
    write_xy_positions(landmark_file, positions)
Exemplo n.º 4
0
    def pca_smooth_widths(self, width_tck, mean_widths):
        """Return PCA-smoothed worm widths.

        Parameters:
            width_tck: spline to be smoothed
            mean_widths: mean width profile (not tck) for the worm in question
                (can be obtained using width_profile_for_age method, e.g.)
        """
        if self.pca_basis is None:
            return None
        basis_shape = self.pca_basis.shape[1]
        x = numpy.linspace(0, 1, basis_shape)
        mean_shape = mean_widths.shape[0]
        if mean_shape != basis_shape:
            mean_widths = numpy.interp(x, numpy.linspace(0, 1, mean_shape),
                                       mean_widths)
        widths = interpolate.spline_evaluate(width_tck, x)
        pca_projection = numpy.dot(widths - mean_widths, self.pca_basis.T)
        pca_reconstruction = mean_widths + numpy.dot(pca_projection,
                                                     self.pca_basis)
        return self._to_tck(pca_reconstruction)
Exemplo n.º 5
0
 def evaluate_tck(self, x=None):
     if x is None:
         x = self._tck_x
     return interpolate.spline_evaluate(self._tck, x)