def getpoints(input_dir, num_points=5, num_pixels=10.0):
    """
    Evaluate spline at discrete points that will be compared to establish 
    continuous time trajectories.

    args:
        df (DataFrame): contains requested coordinates and trajectories

    kwargs:
        num_points (int): number of points to compare
        num_pixels (float): number of pixels along midline to compare

    """

    # Read the midline and any previous trajectories in from file
    df = read.makedataframe(input_dir, ('MidSpline', 'Length', 'Trace'))
    df = df.reindex(sorted(df.index))
    if 'Trace' not in df:
        df['Trace'] = [v for v in df.index]

    # Evaluate the midline at discrete points starting at the old pole
    u0 = np.linspace(0., 1., num_points)
    vs = []
    for i in df.index:
        u = u0 * min(1, num_pixels / df['Length'][i])
        try:
            v = splev(u, df['MidSpline'][i])
        except:
            # traceback.print_exc()
            v = [np.ones(num_points) * np.nan] * 2
        vs.append(v)
    df['Points'] = vs
    return df.ix[:, ('Points', 'Trace')]
Beispiel #2
0
def get_fft_coefs(input_dir, num_points=500, num_coefs=20):
    """
    Calculate the Fourier shape descriptors from the saved cell contours.

    args:
        df (DataFrame): contains requested coordinates and trajectories

    """

    # Read the necessary variables in from file
    df = read.makedataframe(input_dir, ('EdgeSpline', ))
    df['FourierCoef'] = None

    # Evaluate each frame independently
    u = np.arange(0., 1., 1. / num_points)
    for i, v in enumerate(df.index):
        tck = df['EdgeSpline'].ix[v]
        try:
            xs = splineprops(u, tck, 'Coordinates')['Coordinates']
            d = fftcoefs(xs, n=num_coefs)
        except Exception:
            d = {}
        df['FourierCoef'].ix[v] = d

    # Save new Series to file
    df['FourierCoef'].to_pickle(os.path.join(input_dir, 'FourierCoef.pickle'))
Beispiel #3
0
def get_width_profiles(input_dir, num_points=500):
    """
    Calculate the width profiles from the saved cell midlines.

    args:
        df (DataFrame): contains requested coordinates and trajectories

    """

    # Read the necessary variables in from file
    old_keys = ('Area', 'EdgeSpline', 'Length', 'MidSpline', 'Perimeter',
                'StalkedPole', 'SwarmerPole', 'Trace')
    df = read.makedataframe(input_dir, old_keys)

    # Evaluate each frame independently
    for i, v in enumerate(df.index):
        d = get_width_profile_new(df.ix[v], num_points)
        if i == 0:
            new_keys = d.keys()
            for k in new_keys:
                df[k] = None
        for k in new_keys:
            df[k].ix[v] = d[k]

    # Save each Series to file
    for k in new_keys:
        df[k].to_pickle(os.path.join(input_dir, k + '.pickle'))
def get_fft_coefs(input_dir, num_points=500, num_coefs=20):
    """
    Calculate the Fourier shape descriptors from the saved cell contours.

    args:
        df (DataFrame): contains requested coordinates and trajectories

    """

    # Read the necessary variables in from file
    df = read.makedataframe(input_dir, ("EdgeSpline",))
    df["FourierCoef"] = None

    # Evaluate each frame independently
    u = np.arange(0.0, 1.0, 1.0 / num_points)
    for i, v in enumerate(df.index):
        tck = df["EdgeSpline"].ix[v]
        try:
            xs = splineprops(u, tck, "Coordinates")["Coordinates"]
            d = fftcoefs(xs, n=num_coefs)
        except Exception:
            d = {}
        df["FourierCoef"].ix[v] = d

    # Save new Series to file
    df["FourierCoef"].to_pickle(os.path.join(input_dir, "FourierCoef.pickle"))
def get_width_profiles(input_dir, num_points=500):
    """
    Calculate the width profiles from the saved cell midlines.

    args:
        df (DataFrame): contains requested coordinates and trajectories

    """

    # Read the necessary variables in from file
    old_keys = ("Area", "EdgeSpline", "Length", "MidSpline", "Perimeter", "StalkedPole", "SwarmerPole", "Trace")
    df = read.makedataframe(input_dir, old_keys)

    # Evaluate each frame independently
    for i, v in enumerate(df.index):
        d = get_width_profile_new(df.ix[v], num_points)
        if i == 0:
            new_keys = d.keys()
            for k in new_keys:
                df[k] = None
        for k in new_keys:
            df[k].ix[v] = d[k]

    # Save each Series to file
    for k in new_keys:
        df[k].to_pickle(os.path.join(input_dir, k + ".pickle"))
Beispiel #6
0
def getpoints(input_dir, num_points=5, num_pixels=10.0):
    """
    Evaluate spline at discrete points that will be compared to establish 
    continuous time trajectories.

    args:
        df (DataFrame): contains requested coordinates and trajectories

    kwargs:
        num_points (int): number of points to compare
        num_pixels (float): number of pixels along midline to compare

    """

    # Read the midline and any previous trajectories in from file
    df = read.makedataframe(input_dir, ('MidSpline', 'Length', 'Trace'))
    df = df.reindex(sorted(df.index))
    if 'Trace' not in df:
        df['Trace'] = [v for v in df.index]

    # Evaluate the midline at discrete points starting at the old pole
    u0 = np.linspace(0., 1., num_points)
    vs = []
    for i in df.index:
        u = u0 * min(1, num_pixels / df['Length'][i])
        try:
            v = splev(u, df['MidSpline'][i])
        except:
            # traceback.print_exc()
            v = [np.ones(num_points) * np.nan] * 2
        vs.append(v)
    df['Points'] = vs
    return df.ix[:, ('Points', 'Trace')]
Beispiel #7
0
def smooth_widths(input_dir, sigma=2, cutoff=0.02):
    """
    Smooth the widths and recalculate the shape parameters.

    args:
        df (DataFrame): contains requested coordinates and trajectories

    """

    # Read the necessary variables in from file
    df = read.makedataframe(input_dir,
                            ('Area', 'EdgeSpline', 'Length', 'MidSpline',
                             'StalkedPole', 'SwarmerPole', 'Widths'))

    # Save new and overwrite existing shape variables
    new_keys = ('AreaStalked', 'LengthStalkedMaxPole', 'LengthStalkedMin',
                'LengthStalkedMaxCenter', 'LengthMin',
                'LengthSwarmerMaxCenter', 'LengthSwarmerMin',
                'LengthSwarmerMaxPole', 'WidthStalkedMaxPole',
                'WidthStalkedMin', 'WidthStalkedMaxCenter', 'WidthMin',
                'WidthSwarmerMaxCenter', 'WidthSwarmerMin',
                'WidthSwarmerMaxPole', 'WidthStalkedMax', 'WidthSwarmerMax',
                'WidthMax', 'LengthStalkedMax', 'LengthSwarmerMax',
                'LengthMax', 'WidthsSmoothed')
    for k in new_keys:
        df[k] = None

    # Evaluate each frame independently
    for v in df.index:
        # Assign default values
        y = np.ndarray(0)
        ast = np.nan
        lstmaxp = np.nan
        lstmin = np.nan
        lstmaxc = np.nan
        lst = np.nan
        lswmaxc = np.nan
        lswmin = np.nan
        lswmaxp = np.nan
        wstmaxp = np.nan
        wstmin = np.nan
        wstmaxc = np.nan
        wmin = np.nan
        wswmaxc = np.nan
        wswmin = np.nan
        wswmaxp = np.nan
        wstmax = np.nan
        wswmax = np.nan
        wmax = np.nan
        lstmax = np.nan
        lswmax = np.nan
        lmax = np.nan

        try:
            s = df.ix[v]

            # Load variables from file
            a = s['Area']
            l = s['Length']
            ps = (s['StalkedPole'], s['SwarmerPole'])
            w = s['Widths']
            tcke = s['EdgeSpline']
            tckm = s['MidSpline']

            # Reconstruct cell contour and midline
            n = len(w)
            u = np.linspace(0., 1., n)
            es = np.asarray(zip(*splev(u, tcke)))
            ms = np.asarray(zip(*splev(u, tckm)))
            dms = np.asarray(zip(*splev(u, tckm, der=1)))

            # Smooth the width profile
            y = gaussian_filter1d(w, sigma=sigma)

            # Truncate the width profile near the poles
            #y = y[m:-m]

            # Find all relative minima and maxima
            m = int(n * cutoff)
            imin = np.hstack(argrelmin(y, order=m))
            imax = np.hstack(argrelmax(y, order=m))

            if np.any(imin):
                # Find the smallest (primary) minimum
                jmin = imin[np.argmin(y[imin])]

                # Assign the minimum width
                wmin = y[jmin]

                # Find the normal line that split the cell at the minimum width
                esmin = get_divn_plane(es, ms[jmin], dms[jmin])

                # Split into stalked and swarmer parts
                ast, _ = split_areas(u, tcke, esmin, es, ps, a)
                lst = get_length(u[:jmin + 1], tckm)

                # Sort the other minima to the stalked or swarmer part
                istmin = imin[imin < jmin]
                iswmin = imin[imin > jmin]

                # Sort the maxima to the stalked or swarmer part
                istmax = imax[imax < jmin]
                iswmax = imax[imax > jmin]

                if np.any(istmin):
                    # Find the smallest of the stalked minima
                    jstmin = istmin[np.argmin(y[istmin])]

                    # Assign the minimum stalked width
                    wstmin = y[jstmin]

                    # Find the length at the minimum stalked width
                    lstmin = get_length(u[:jstmin + 1], tckm)

                    # Sort the stalked maxima to the pole or center
                    istmaxp = istmax[istmax < jstmin]
                    istmaxc = istmax[istmax > jstmin]

                    # Find the largest of the stalked maxima
                    jstmaxp = istmaxp[np.argmax(y[istmaxp])]
                    jstmaxc = istmaxc[np.argmax(y[istmaxc])]

                    # Assign the stalked pole width and length maxima
                    wstmaxp = y[jstmaxp]
                    lstmaxp = get_length(u[:jstmaxp + 1], tckm)

                    # Assign the stalked center width and length maxima
                    wstmaxc = y[jstmaxc]
                    lstmaxc = get_length(u[:jstmaxc + 1], tckm)
                else:
                    # Find the largest of the stalked maxima
                    jstmax = istmax[np.argmax(y[istmax])]

                    # Assign the stalked pole width and length maxima
                    wstmax = y[jstmax]
                    lstmax = get_length(u[:jstmax + 1], tckm)
                if np.any(iswmin):
                    # Find the smallest of the swarmer minima
                    jswmin = iswmin[np.argmin(y[iswmin])]

                    # Assign the minimum swarmer width
                    wswmin = y[jswmin]

                    # Find the length at the minimum swarmer width
                    lswmin = get_length(u[:jswmin + 1], tckm)

                    # Sort the swarmer maxima to the pole or center
                    iswmaxp = iswmax[iswmax > jswmin]
                    iswmaxc = iswmax[iswmax < jswmin]

                    # Find the largest of the swarmer maxima
                    jswmaxp = iswmaxp[np.argmax(y[iswmaxp])]
                    jswmaxc = iswmaxc[np.argmax(y[iswmaxc])]

                    # Assign the swarmer pole width and length maxima
                    wswmaxp = y[jswmaxp]
                    lswmaxp = get_length(u[:jswmaxp + 1], tckm)

                    # Assign the swarmer center width and length maxima
                    wswmaxc = y[jswmaxc]
                    lswmaxc = get_length(u[:jswmaxc + 1], tckm)
                else:
                    # Find the largest of the swarmer maxima
                    jswmax = iswmax[np.argmax(y[iswmax])]

                    # Assign the swarmer pole width and length maxima
                    wswmax = y[jswmax]
                    lswmax = get_length(u[:jswmax + 1], tckm)
            else:
                # Find the largest of the maxima
                jmax = imax[np.argmax(y[imax])]

                # Assign the width and length maxima
                wmax = y[jmax]
                lmax = get_length(u[:jmax + 1], tckm)
        except Exception as e:
            pass

        s['WidthsSmoothed'] = y
        s['AreaStalked'] = ast
        s['LengthStalkedMaxPole'] = lstmaxp
        s['LengthStalkedMin'] = lstmin
        s['LengthStalkedMaxCenter'] = lstmaxc
        s['LengthMin'] = lst
        s['LengthSwarmerMaxCenter'] = lswmaxc
        s['LengthSwarmerMin'] = lswmin
        s['LengthSwarmerMaxPole'] = lswmaxp
        s['WidthStalkedMaxPole'] = wstmaxp
        s['WidthStalkedMin'] = wstmin
        s['WidthStalkedMaxCenter'] = wstmaxc
        s['WidthMin'] = wmin
        s['WidthSwarmerMaxCenter'] = wswmaxc
        s['WidthSwarmerMin'] = wswmin
        s['WidthSwarmerMaxPole'] = wswmaxp
        s['WidthStalkedMax'] = wstmax
        s['WidthSwarmerMax'] = wswmax
        s['WidthMax'] = wmax
        s['LengthStalkedMax'] = lstmax
        s['LengthSwarmerMax'] = lswmax
        s['LengthMax'] = lmax

        df.ix[v] = s

    # Save new Series to file
    for k in new_keys:
        df[k].to_pickle(os.path.join(input_dir, k + '.pickle'))
def smooth_widths(input_dir, sigma=2, cutoff=0.02):
    """
    Smooth the widths and recalculate the shape parameters.

    args:
        df (DataFrame): contains requested coordinates and trajectories

    """

    # Read the necessary variables in from file
    df = read.makedataframe(
        input_dir, ("Area", "EdgeSpline", "Length", "MidSpline", "StalkedPole", "SwarmerPole", "Widths")
    )

    # Save new and overwrite existing shape variables
    new_keys = (
        "AreaStalked",
        "LengthStalkedMaxPole",
        "LengthStalkedMin",
        "LengthStalkedMaxCenter",
        "LengthMin",
        "LengthSwarmerMaxCenter",
        "LengthSwarmerMin",
        "LengthSwarmerMaxPole",
        "WidthStalkedMaxPole",
        "WidthStalkedMin",
        "WidthStalkedMaxCenter",
        "WidthMin",
        "WidthSwarmerMaxCenter",
        "WidthSwarmerMin",
        "WidthSwarmerMaxPole",
        "WidthStalkedMax",
        "WidthSwarmerMax",
        "WidthMax",
        "LengthStalkedMax",
        "LengthSwarmerMax",
        "LengthMax",
        "WidthsSmoothed",
    )
    for k in new_keys:
        df[k] = None

    # Evaluate each frame independently
    for v in df.index:
        # Assign default values
        y = np.ndarray(0)
        ast = np.nan
        lstmaxp = np.nan
        lstmin = np.nan
        lstmaxc = np.nan
        lst = np.nan
        lswmaxc = np.nan
        lswmin = np.nan
        lswmaxp = np.nan
        wstmaxp = np.nan
        wstmin = np.nan
        wstmaxc = np.nan
        wmin = np.nan
        wswmaxc = np.nan
        wswmin = np.nan
        wswmaxp = np.nan
        wstmax = np.nan
        wswmax = np.nan
        wmax = np.nan
        lstmax = np.nan
        lswmax = np.nan
        lmax = np.nan

        try:
            s = df.ix[v]

            # Load variables from file
            a = s["Area"]
            l = s["Length"]
            ps = (s["StalkedPole"], s["SwarmerPole"])
            w = s["Widths"]
            tcke = s["EdgeSpline"]
            tckm = s["MidSpline"]

            # Reconstruct cell contour and midline
            n = len(w)
            u = np.linspace(0.0, 1.0, n)
            es = np.asarray(zip(*splev(u, tcke)))
            ms = np.asarray(zip(*splev(u, tckm)))
            dms = np.asarray(zip(*splev(u, tckm, der=1)))

            # Smooth the width profile
            y = gaussian_filter1d(w, sigma=sigma)

            # Truncate the width profile near the poles
            # y = y[m:-m]

            # Find all relative minima and maxima
            m = int(n * cutoff)
            imin = np.hstack(argrelmin(y, order=m))
            imax = np.hstack(argrelmax(y, order=m))

            if np.any(imin):
                # Find the smallest (primary) minimum
                jmin = imin[np.argmin(y[imin])]

                # Assign the minimum width
                wmin = y[jmin]

                # Find the normal line that split the cell at the minimum width
                esmin = get_divn_plane(es, ms[jmin], dms[jmin])

                # Split into stalked and swarmer parts
                ast, _ = split_areas(u, tcke, esmin, es, ps, a)
                lst = get_length(u[: jmin + 1], tckm)

                # Sort the other minima to the stalked or swarmer part
                istmin = imin[imin < jmin]
                iswmin = imin[imin > jmin]

                # Sort the maxima to the stalked or swarmer part
                istmax = imax[imax < jmin]
                iswmax = imax[imax > jmin]

                if np.any(istmin):
                    # Find the smallest of the stalked minima
                    jstmin = istmin[np.argmin(y[istmin])]

                    # Assign the minimum stalked width
                    wstmin = y[jstmin]

                    # Find the length at the minimum stalked width
                    lstmin = get_length(u[: jstmin + 1], tckm)

                    # Sort the stalked maxima to the pole or center
                    istmaxp = istmax[istmax < jstmin]
                    istmaxc = istmax[istmax > jstmin]

                    # Find the largest of the stalked maxima
                    jstmaxp = istmaxp[np.argmax(y[istmaxp])]
                    jstmaxc = istmaxc[np.argmax(y[istmaxc])]

                    # Assign the stalked pole width and length maxima
                    wstmaxp = y[jstmaxp]
                    lstmaxp = get_length(u[: jstmaxp + 1], tckm)

                    # Assign the stalked center width and length maxima
                    wstmaxc = y[jstmaxc]
                    lstmaxc = get_length(u[: jstmaxc + 1], tckm)
                else:
                    # Find the largest of the stalked maxima
                    jstmax = istmax[np.argmax(y[istmax])]

                    # Assign the stalked pole width and length maxima
                    wstmax = y[jstmax]
                    lstmax = get_length(u[: jstmax + 1], tckm)
                if np.any(iswmin):
                    # Find the smallest of the swarmer minima
                    jswmin = iswmin[np.argmin(y[iswmin])]

                    # Assign the minimum swarmer width
                    wswmin = y[jswmin]

                    # Find the length at the minimum swarmer width
                    lswmin = get_length(u[: jswmin + 1], tckm)

                    # Sort the swarmer maxima to the pole or center
                    iswmaxp = iswmax[iswmax > jswmin]
                    iswmaxc = iswmax[iswmax < jswmin]

                    # Find the largest of the swarmer maxima
                    jswmaxp = iswmaxp[np.argmax(y[iswmaxp])]
                    jswmaxc = iswmaxc[np.argmax(y[iswmaxc])]

                    # Assign the swarmer pole width and length maxima
                    wswmaxp = y[jswmaxp]
                    lswmaxp = get_length(u[: jswmaxp + 1], tckm)

                    # Assign the swarmer center width and length maxima
                    wswmaxc = y[jswmaxc]
                    lswmaxc = get_length(u[: jswmaxc + 1], tckm)
                else:
                    # Find the largest of the swarmer maxima
                    jswmax = iswmax[np.argmax(y[iswmax])]

                    # Assign the swarmer pole width and length maxima
                    wswmax = y[jswmax]
                    lswmax = get_length(u[: jswmax + 1], tckm)
            else:
                # Find the largest of the maxima
                jmax = imax[np.argmax(y[imax])]

                # Assign the width and length maxima
                wmax = y[jmax]
                lmax = get_length(u[: jmax + 1], tckm)
        except Exception as e:
            pass

        s["WidthsSmoothed"] = y
        s["AreaStalked"] = ast
        s["LengthStalkedMaxPole"] = lstmaxp
        s["LengthStalkedMin"] = lstmin
        s["LengthStalkedMaxCenter"] = lstmaxc
        s["LengthMin"] = lst
        s["LengthSwarmerMaxCenter"] = lswmaxc
        s["LengthSwarmerMin"] = lswmin
        s["LengthSwarmerMaxPole"] = lswmaxp
        s["WidthStalkedMaxPole"] = wstmaxp
        s["WidthStalkedMin"] = wstmin
        s["WidthStalkedMaxCenter"] = wstmaxc
        s["WidthMin"] = wmin
        s["WidthSwarmerMaxCenter"] = wswmaxc
        s["WidthSwarmerMin"] = wswmin
        s["WidthSwarmerMaxPole"] = wswmaxp
        s["WidthStalkedMax"] = wstmax
        s["WidthSwarmerMax"] = wswmax
        s["WidthMax"] = wmax
        s["LengthStalkedMax"] = lstmax
        s["LengthSwarmerMax"] = lswmax
        s["LengthMax"] = lmax

        df.ix[v] = s

    # Save new Series to file
    for k in new_keys:
        df[k].to_pickle(os.path.join(input_dir, k + ".pickle"))