def test_alpha_calc():
    frames = 5
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.linspace(0, frames, frames) + 5,
        'Y': np.linspace(0, frames, frames) + 3,
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)
    assert ft.alpha_calc(dframe) == (2.0000000000000004, 0.4999999999999998)

    frames = 10
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.sin(np.linspace(0, frames, frames) + 5),
        'Y': np.cos(np.linspace(0, frames, frames) + 3),
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)
    assert ft.alpha_calc(dframe) == (0.8201034110620524, 0.1494342948594476)
def test_efficiency():
    frames = 100
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.sin(np.linspace(0, frames, frames) + 3),
        'Y': np.cos(np.linspace(0, frames, frames) + 3),
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)

    assert ft.efficiency(dframe) ==\
        (0.003548421265914009, 0.0059620286331768385)

    frames = 10
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.linspace(0, frames, frames) + 5,
        'Y': np.linspace(0, frames, frames) + 3,
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)

    assert ft.efficiency(dframe) == (10.0, 1.0)
def test_kurtosis():
    frames = 5
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.linspace(0, frames, frames) + 5,
        'Y': np.linspace(0, frames, frames) + 3,
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)
    assert ft.kurtosis(dframe) == 4.079999999999999

    frames = 10
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.sin(np.linspace(0, frames, frames) + 3),
        'Y': np.cos(np.linspace(0, frames, frames) + 3),
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)
    assert ft.kurtosis(dframe) == 1.4759027695843443
Example #4
0
def test_partial_corr():
    dataf = msd.random_traj_dataset()
    msds = msd.all_msds2(dataf, frames=100)
    feat = ft.calculate_features(msds)
    pcorr = pca.partial_corr(feat)
    npt.assert_equal(24.0, np.round(np.sum(pcorr), 1))

    dataf = msd.random_traj_dataset(nparts=10)
    msds = msd.all_msds2(dataf, frames=100)
    feat = ft.calculate_features(msds)
    pcorr = pca.partial_corr(feat)
    npt.assert_equal(47.9, np.round(np.sum(pcorr), 1))

    dataf = msd.random_traj_dataset(nparts=10, seed=9)
    msds = msd.all_msds2(dataf, frames=100)
    feat = ft.calculate_features(msds)
    pcorr = pca.partial_corr(feat)
    npt.assert_equal(33.4, np.round(np.sum(pcorr), 1))

    dataf = msd.random_traj_dataset(nparts=10, nframes=40, seed=9)
    msds = msd.all_msds2(dataf, frames=40)
    feat = ft.calculate_features(msds)
    pcorr = pca.partial_corr(feat)
    npt.assert_equal(17.4, np.round(np.sum(pcorr), 1))

    dataf = msd.random_traj_dataset(nparts=10,
                                    nframes=40,
                                    ndist=(3, 5),
                                    seed=9)
    msds = msd.all_msds2(dataf, frames=40)
    feat = ft.calculate_features(msds)
    pcorr = pca.partial_corr(feat)
    npt.assert_equal(35.7, np.round(np.sum(pcorr), 1))
def test_msd_ratio():
    frames = 10
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.sin(np.linspace(0, frames, frames) + 3),
        'Y': np.cos(np.linspace(0, frames, frames) + 3),
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)

    assert ft.msd_ratio(dframe, 1, 9) == 0.09708430006771959

    frames = 10
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.linspace(0, frames, frames) + 5,
        'Y': np.linspace(0, frames, frames) + 3,
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)

    assert ft.msd_ratio(dframe, 1, 9) == -0.09876543209876543
def test_boundedness():
    frames = 100
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.sin(np.linspace(0, frames, frames) + 3),
        'Y': np.cos(np.linspace(0, frames, frames) + 3),
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)
    assert ft.boundedness(dframe) == (0.607673328076712, 5.674370543833708,
                                      -0.0535555587618044)

    frames = 10
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.linspace(0, frames, frames) + 5,
        'Y': np.linspace(0, frames, frames) + 3,
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)
    assert ft.boundedness(dframe) == (0.039999999999999994, 1.0,
                                      -0.21501108474766228)
def test_minboundrect():
    frames = 10
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.linspace(0, frames, frames) + 5,
        'Y': np.linspace(0, frames, frames) + 3,
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)

    d1, d2, d3, d4, d5, d6 = ft.minboundrect(dframe)
    o1, o2, o3, o4 = (-2.356194490192, 0, 14.142135623730, 0)
    o5 = np.array([10, 8])
    o6 = np.array([[5., 3.], [15., 13.], [15., 13.], [5., 3.]])

    # assert math.isclose(d1, o1, abs_tol=1e-10)
    assert math.isclose(d2, o2, abs_tol=1e-10)
    assert math.isclose(d3, o3, abs_tol=1e-10)
    assert math.isclose(d4, o4, abs_tol=1e-10)
    npt.assert_almost_equal(d5, o5)
    # npt.assert_almost_equal(d6, o6)

    frames = 100
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.sin(np.linspace(0, frames, frames) + 3),
        'Y': np.cos(np.linspace(0, frames, frames) + 3),
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)

    d1, d2, d3, d4, d5, d6 = ft.minboundrect(dframe)
    o1, o2, o3, o4 = (-2.7345175425633, 3.7067697307443, 1.899593160348,
                      1.951349272106)
    o5 = np.array([-0.00098312, 0.00228019])
    o6 = np.array([[-1.2594591, 0.52217706], [0.4849046, 1.27427376],
                   [1.25749286, -0.51761668], [-0.48687084, -1.26971339]])

    # assert math.isclose(d1, o1, abs_tol=1e-10)
    assert math.isclose(d2, o2, abs_tol=1e-10)
    assert math.isclose(d3, o3, abs_tol=1e-10)
    assert math.isclose(d4, o4, abs_tol=1e-10)
    npt.assert_almost_equal(d5, o5)
Example #8
0
def test_unmask_track():
    size = 10
    ID = np.ones(size)
    frame = np.linspace(5, size - 1 + 5, size)
    x = frame + 1
    y = frame + 3

    d = {'Frame': frame, 'Track_ID': ID, 'X': x, 'Y': y}
    di = pd.DataFrame(data=d)
    track = msd.all_msds2(di, frames=20)
    output = ft.unmask_track(track)

    d2 = {
        'Frame':
        frame - 5,
        'Track_ID':
        ID,
        'X':
        x,
        'Y':
        y,
        'MSDs':
        np.array((0, 2, 8, 18, 32, 50, 72, 98, 128, 162)).astype('float64'),
        'Gauss':
        np.array((0, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25))
    }
    check = pd.DataFrame(data=d2)

    pdt.assert_frame_equal(output, check)
def test_unmask_track():
    data = {
        'Frame': [0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
        'Track_ID': [1, 1, 1, 1, 1, 2, 2, 2, 2, 2],
        'X': [np.nan, 6, 7, 8, 9, 1, 2, 3, 4, np.nan],
        'Y': [np.nan, 7, 8, 9, 10, 2, 3, 4, 5, np.nan],
        'Quality': [np.nan, 10, 10, 10, 10, 10, 10, 10, 10, np.nan],
        'SN_Ratio': [np.nan, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, np.nan],
        'Mean_Intensity': [np.nan, 10, 10, 10, 10, 10, 10, 10, 10, np.nan]
    }
    dframe = pd.DataFrame(data=data)
    cols = [
        'Frame', 'Track_ID', 'X', 'Y', 'MSDs', 'Gauss', 'Quality', 'SN_Ratio',
        'Mean_Intensity'
    ]
    length = max(dframe['Frame']) + 1
    m_df = msd.all_msds2(dframe, frames=length)[cols]

    datat = {
        'Frame': [float(i) for i in [0, 1, 2, 3]],
        'Track_ID': [float(i) for i in [2, 2, 2, 2]],
        'X': [float(i) for i in [1, 2, 3, 4]],
        'Y': [float(i) for i in [2, 3, 4, 5]],
        'MSDs': [float(i) for i in [0, 2, 8, 18]],
        'Gauss': [float(i) for i in [0, 0.25, 0.25, 0.25]],
        'Quality': 4 * [10.0],
        'SN_Ratio': 4 * [0.1],
        'Mean_Intensity': 4 * [10.0]
    }
    dft = pd.DataFrame(data=datat)

    pdt.assert_frame_equal(ft.unmask_track(m_df[m_df['Track_ID'] == 2]), dft)
Example #10
0
def test_precision_weight():
    experiments = []
    geomean = {}
    geostder = {}
    for num in range(4):
        name = 'test_{}'.format(num)
        experiments.append(name)
        data1 = {
            'Frame': [1, 2, 3, 4, 5, 1, 2, 3, 4, 5],
            'Track_ID': [0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
            'X': [x * (num + 1) for x in [5, 6, 7, 8, 9, 2, 4, 6, 8, 10]],
            'Y': [x * (num + 1) for x in [6, 7, 8, 9, 10, 6, 8, 10, 12, 14]],
            'Quality': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
            'SN_Ratio': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
            'Mean_Intensity': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
        }
        df = pd.DataFrame(data=data1)
        msds = msd.all_msds2(df)
        msds.to_csv('msd_test_{}.csv'.format(num))
        geomean[name], geostder[name] = msd.geomean_msdisp(name,
                                                           umppx=1,
                                                           fps=1,
                                                           upload=False)

    slices, bins, bin_names = msd.binning(experiments, wells=1)
    weights, w_holder = msd.precision_weight(experiments, geostder)
    weights_t = np.array([8.3, 8.3, 8.3, 8.3])
    npt.assert_equal(np.round(weights[weights.mask == False].data, 1),
                     weights_t)
Example #11
0
def test_all_msds2():

    data1 = {
        'Frame': [0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
        'Track_ID': [1, 1, 1, 1, 1, 2, 2, 2, 2, 2],
        'X': [5, 6, 7, 8, 9, 1, 2, 3, 4, 5],
        'Y': [6, 7, 8, 9, 10, 2, 3, 4, 5, 6],
        'Quality': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
        'SN_Ratio': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
        'Mean_Intensity': [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
    }
    df = pd.DataFrame(data=data1)

    di = {
        'Frame': [float(i) for i in [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]],
        'Track_ID': [float(i) for i in [1, 1, 1, 1, 1, 2, 2, 2, 2, 2]],
        'X': [float(i) for i in [5, 6, 7, 8, 9, 1, 2, 3, 4, 5]],
        'Y': [float(i) for i in [6, 7, 8, 9, 10, 2, 3, 4, 5, 6]],
        'MSDs': [float(i) for i in [0, 2, 8, 18, 32, 0, 2, 8, 18, 32]],
        'Gauss': [0, 0.25, 0.25, 0.25, 0.25, 0, 0.25, 0.25, 0.25, 0.25],
        'Quality':
        [float(i) for i in [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]],
        'SN_Ratio': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
        'Mean_Intensity':
        [float(i) for i in [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]]
    }
    cols = [
        'Frame', 'Track_ID', 'X', 'Y', 'MSDs', 'Gauss', 'Quality', 'SN_Ratio',
        'Mean_Intensity'
    ]

    dfi = pd.DataFrame(data=di)[cols]

    length = max(df['Frame']) + 1
    pdt.assert_frame_equal(dfi, msd.all_msds2(df, frames=length)[cols])
Example #12
0
def test_kmo():
    dataf = msd.random_traj_dataset(nparts=10, ndist=(1, 1), seed=3)
    msds = msd.all_msds2(dataf, frames=100)
    feat = ft.calculate_features(msds)
    dataset = feat.drop(['frames', 'Track_ID'], axis=1)
    corrmatrix = np.corrcoef(dataset.transpose())
    npt.assert_equal(np.round(np.sum(corrmatrix), 1), 7.3)
Example #13
0
def test_calculate_features():
    d = {
        'Frame': [0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
        'Track_ID': [1, 1, 1, 1, 1, 2, 2, 2, 2, 2],
        'X': [0, 0, 1, 1, 2, 1, 1, 2, 2, 3],
        'Y': [0, 1, 1, 2, 2, 0, 1, 1, 2, 2]
    }
    df = pd.DataFrame(data=d)
    dfi = msd.all_msds2(df, frames=5)
    feat = ft.calculate_features(dfi)

    d = {
        'AR': np.ones(2) * 3.9999999999999996,
        'D_fit': np.ones(2) * 0.1705189932550273,
        'MSD_ratio': np.ones(2) * -0.2666666666666666,
        'X': [0.75, 1.75],
        'Y': [1.25, 1.25],
        'Track_ID': [1.0, 2.0],
        'alpha': np.ones(2) * 1.7793370720777268,
        'asymmetry1': np.ones(2) * 0.9440237239896903,
        'asymmetry2': np.ones(2) * 0.12,
        'asymmetry3': np.ones(2) * 0.3691430189107616,
        'boundedness': np.ones(2) * 0.25,
        'efficiency': np.ones(2) * 2.0,
        'elongation': np.ones(2) * 0.75,
        'fractal_dim': np.ones(2) * 1.333333333333333,
        'frames': [5.0, 5.0],
        'kurtosis': np.ones(2) * 1.166666666666667,
        'straightness': np.ones(2) * 0.7071067811865476,
        'trappedness': np.ones(2) * -0.15258529289428524
    }
    dfi = pd.DataFrame(data=d)

    pdt.assert_frame_equal(dfi, feat)
Example #14
0
def test_pca_analysis():
    dataf = msd.random_traj_dataset(nparts=10, ndist=(2, 6))
    msds = msd.all_msds2(dataf, frames=100)
    feat = ft.calculate_features(msds)
    pcadataset = pca.pca_analysis(feat,
                                  dropcols=['frames', 'Track_ID'],
                                  n_components=5)

    npt.assert_equal(np.round(np.sum(pcadataset.components.values), 3), 0.400)
def test_asymmetry():
    frames = 10
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.linspace(0, frames, frames) + 5,
        'Y': np.linspace(0, frames, frames) + 3,
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)

    o1, o2, o3, o4, o5 = (20.0, 0.0, 1.0, 0.0, 0.69314718)
    d1, d2, d3, d4, d5 = ft.asymmetry(dframe)
    assert math.isclose(o1, d1, abs_tol=1e-10)
    assert math.isclose(o2, d2, abs_tol=1e-10)
    assert math.isclose(o3, d3, abs_tol=1e-10)
    assert math.isclose(o4, d4, abs_tol=1e-10)
    assert math.isclose(o5, d5, abs_tol=1e-10)

    frames = 100
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.sin(np.linspace(0, frames, frames) + 3),
        'Y': np.cos(np.linspace(0, frames, frames) + 3),
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)

    o1, o2, o3, o4, o5 = (0.4254120816156, 0.42004967815488,
                          0.0001609000151811, 0.9873948021401,
                          2.0114322402896e-05)
    d1, d2, d3, d4, d5 = ft.asymmetry(dframe)
    assert math.isclose(o1, d1)
    assert math.isclose(o2, d2)
    assert math.isclose(o3, d3)
    assert math.isclose(o4, d4)
    assert math.isclose(o5, d5)
def test_gyration_tensor():
    frames = 6
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.linspace(0, frames, frames) + 5,
        'Y': np.linspace(0, frames, frames) + 3,
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)
    o1, o2, o3, o4 = (8.0, 0.0, np.array([0.70710678, -0.70710678]),
                      np.array([0.70710678, 0.70710678]))
    d1, d2, d3, d4 = ft.gyration_tensor(dframe)

    assert d1 == o1
    assert d2 == o2
    npt.assert_almost_equal(o3, d3)
    npt.assert_almost_equal(o4, d4)

    frames = 10
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.sin(np.linspace(0, frames, frames) + 5),
        'Y': np.cos(np.linspace(0, frames, frames) + 5),
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)
    o1, o2, o3, o4 = (0.47248734315843355, 0.3447097846562249,
                      np.array([0.83907153, 0.54402111]),
                      np.array([-0.54402111, 0.83907153]))
    d1, d2, d3, d4 = ft.gyration_tensor(dframe)

    assert d1 == o1
    assert d2 == o2
    npt.assert_almost_equal(o3, d3)
    npt.assert_almost_equal(o4, d4)
Example #17
0
def test_aspectratio():
    frames = 6
    d = {
        'Frame': np.linspace(0, frames, frames),
        'X': [0, 1, 1, 2, 2, 3],
        'Y': [0, 0, 1, 1, 2, 2],
        'Track_ID': np.ones(frames)
    }
    df = pd.DataFrame(data=d)
    df = msd.all_msds2(df, frames=frames + 1)
    assert ft.aspectratio(df)[0:2] == (3.9000000000000026, 0.7435897435897438)
    npt.assert_almost_equal(ft.aspectratio(df)[2], np.array([1.5, 1.]))
Example #18
0
def test_plot_particles_in_frame():
    prefix = 'test'
    msd_file = 'msd_{}.csv'.format(prefix)
    ft_file = 'features_{}.csv'.format(prefix)

    dataf = msd.random_traj_dataset(nparts=10, ndist=(1, 1), seed=3)
    msds = msd.all_msds2(dataf, frames=100)
    msds.to_csv(msd_file)
    feat = ft.calculate_features(msds)
    feat.to_csv(ft_file)

    hm.plot_particles_in_frame(prefix, x_range=100, y_range=20, upload=False)
    assert os.path.isfile('in_frame_{}.png'.format(prefix))
Example #19
0
def test_plot_trajectories():
    prefix = 'test'
    msd_file = 'msd_{}.csv'.format(prefix)
    ft_file = 'features_{}.csv'.format(prefix)

    dataf = msd.random_traj_dataset(nparts=30, ndist=(1, 1), seed=3)
    msds = msd.all_msds2(dataf, frames=100)
    msds.to_csv(msd_file)
    feat = ft.calculate_features(msds)
    feat.to_csv(ft_file)

    hm.plot_trajectories(prefix, resolution=520, rows=1, cols=1, upload=False)
    assert os.path.isfile('traj_{}.png'.format(prefix))
def test_plot_histogram():
    prefix = 'test'
    msd_file = 'msd_{}.csv'.format(prefix)
    ft_file = 'features_{}.csv'.format(prefix)

    dataf = msd.random_traj_dataset(nparts=30, ndist=(1, 1), seed=3)
    msds = msd.all_msds2(dataf, frames=100)
    msds.to_csv(msd_file)
    feat = ft.calculate_features(msds)
    feat.to_csv(ft_file)
    
    hm.plot_histogram(prefix, fps=1, umppx=1, frames=100, frame_interval=5, frame_range=5, y_range=10, upload=False)
    assert os.path.isfile('hist_{}.png'.format(prefix))
Example #21
0
def test_alpha_calc():
    frames = 5
    d = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.linspace(0, frames, frames) + 5,
        'Y': np.linspace(0, frames, frames) + 3,
        'Track_ID': np.ones(frames)
    }
    df = pd.DataFrame(data=d)
    df = msd.all_msds2(df, frames=frames + 1)
    assert ft.alpha_calc(df) == (2.0000000000000004, 0.4999999999999998)

    frames = 10
    d = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.sin(np.linspace(0, frames, frames) + 5),
        'Y': np.cos(np.linspace(0, frames, frames) + 3),
        'Track_ID': np.ones(frames)
    }
    df = pd.DataFrame(data=d)
    df = msd.all_msds2(df, frames=frames + 1)
    assert ft.alpha_calc(df) == (0.8201034110620524, 0.1494342948594476)
Example #22
0
def test_kurtosis():
    frames = 5
    d = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.linspace(0, frames, frames) + 5,
        'Y': np.linspace(0, frames, frames) + 3,
        'Track_ID': np.ones(frames)
    }
    df = pd.DataFrame(data=d)
    df = msd.all_msds2(df, frames=frames + 1)
    assert ft.kurtosis(df) == 4.079999999999999

    frames = 10
    d = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.sin(np.linspace(0, frames, frames) + 3),
        'Y': np.cos(np.linspace(0, frames, frames) + 3),
        'Track_ID': np.ones(frames)
    }
    df = pd.DataFrame(data=d)
    df = msd.all_msds2(df, frames=frames + 1)
    assert ft.kurtosis(df) == 1.4759027695843443
def test_plot_individual_msds():
    prefix = 'test'
    msd_file = 'msd_{}.csv'.format(prefix)
    ft_file = 'features_{}.csv'.format(prefix)

    dataf = msd.random_traj_dataset(nparts=30, ndist=(1, 1), seed=3)
    msds = msd.all_msds2(dataf, frames=100)
    msds.to_csv(msd_file)
    feat = ft.calculate_features(msds)
    feat.to_csv(ft_file)

    geomean, gSEM = hm.plot_individual_msds(prefix, umppx=1, fps=1, y_range=400, alpha=0.3, upload=False)
    npt.assert_equal(332.8, np.round(np.sum(geomean), 1))
    npt.assert_equal(33.1, np.round(np.sum(gSEM), 1))
Example #24
0
def test_msd_ratio():
    frames = 10
    d = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.sin(np.linspace(0, frames, frames) + 3),
        'Y': np.cos(np.linspace(0, frames, frames) + 3),
        'Track_ID': np.ones(frames)
    }
    df = pd.DataFrame(data=d)
    df = msd.all_msds2(df, frames=frames + 1)

    assert ft.msd_ratio(df, 1, 9) == 0.09708430006771959

    frames = 10
    d = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.linspace(0, frames, frames) + 5,
        'Y': np.linspace(0, frames, frames) + 3,
        'Track_ID': np.ones(frames)
    }
    df = pd.DataFrame(data=d)
    df = msd.all_msds2(df, frames=frames + 1)

    assert ft.msd_ratio(df, 1, 9) == -0.09876543209876543
Example #25
0
def test_efficiency():
    frames = 100
    d = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.sin(np.linspace(0, frames, frames) + 3),
        'Y': np.cos(np.linspace(0, frames, frames) + 3),
        'Track_ID': np.ones(frames)
    }
    df = pd.DataFrame(data=d)
    df = msd.all_msds2(df, frames=frames + 1)

    assert ft.efficiency(df) == (0.003548421265914009, 0.0059620286331768385)

    frames = 10
    d = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.linspace(0, frames, frames) + 5,
        'Y': np.linspace(0, frames, frames) + 3,
        'Track_ID': np.ones(frames)
    }
    df = pd.DataFrame(data=d)
    df = msd.all_msds2(df, frames=frames + 1)

    assert ft.efficiency(df) == (10.0, 1.0)
Example #26
0
def test_boundedness():
    frames = 100
    d = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.sin(np.linspace(0, frames, frames) + 3),
        'Y': np.cos(np.linspace(0, frames, frames) + 3),
        'Track_ID': np.ones(frames)
    }
    df = pd.DataFrame(data=d)
    df = msd.all_msds2(df, frames=frames + 1)
    assert ft.boundedness(df) == (0.607673328076712, 5.674370543833708,
                                  -0.0535555587618044)

    frames = 10
    d = {
        'Frame': np.linspace(0, frames, frames),
        'X': np.linspace(0, frames, frames) + 5,
        'Y': np.linspace(0, frames, frames) + 3,
        'Track_ID': np.ones(frames)
    }
    df = pd.DataFrame(data=d)
    df = msd.all_msds2(df, frames=frames + 1)
    assert ft.boundedness(df) == (0.039999999999999994, 1.0,
                                  -0.21501108474766228)
def test_aspectratio():
    frames = 6
    data = {
        'Frame': np.linspace(0, frames, frames),
        'X': [0, 1, 1, 2, 2, 3],
        'Y': [0, 0, 1, 1, 2, 2],
        'Track_ID': np.ones(frames),
        'Quality': 10.0 * np.ones(frames),
        'SN_Ratio': 0.1 * np.ones(frames),
        'Mean_Intensity': 10.0 * np.ones(frames)
    }
    dframe = pd.DataFrame(data=data)
    dframe = msd.all_msds2(dframe, frames=frames + 1)
    assert ft.aspectratio(dframe)[0:2] == (3.9000000000000026,
                                           0.7435897435897438)
    npt.assert_almost_equal(ft.aspectratio(dframe)[2], np.array([1.5, 1.]))
Example #28
0
def test_voronoi_finite_polygons_2d():
    prefix = 'test'
    msd_file = 'msd_{}.csv'.format(prefix)
    ft_file = 'features_{}.csv'.format(prefix)

    dataf = msd.random_traj_dataset(nparts=30, ndist=(1, 1), seed=3)
    msds = msd.all_msds2(dataf, frames=100)
    msds.to_csv(msd_file)
    feat = ft.calculate_features(msds)
    feat.to_csv(ft_file)

    xs = feat['X'].astype(int)
    ys = feat['Y'].astype(int)
    points = np.zeros((xs.shape[0], 2))
    points[:, 0] = xs
    points[:, 1] = ys

    vor = Voronoi(points)
    regions, vertices = hm.voronoi_finite_polygons_2d(vor)

    npt.assert_equal(243.8, np.round(np.mean(vertices), 1))
Example #29
0
def test_make_xyarray():
    d = {
        'Frame': [0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
        'Track_ID': [1, 1, 1, 1, 1, 2, 2, 2, 2, 2],
        'X': [np.nan, 6, 7, 8, 9, 1, 2, 3, 4, np.nan],
        'Y': [np.nan, 7, 8, 9, 10, 2, 3, 4, 5, np.nan]
    }
    df = pd.DataFrame(data=d)
    cols = ['Frame', 'Track_ID', 'X', 'Y', 'MSDs', 'Gauss']
    length = max(df['Frame']) + 1
    m_df = msd.all_msds2(df, frames=length)[cols]

    dt = {
        'Frame': [float(i) for i in [0, 1, 2, 3]],
        'Track_ID': [float(i) for i in [2, 2, 2, 2]],
        'X': [float(i) for i in [1, 2, 3, 4]],
        'Y': [float(i) for i in [2, 3, 4, 5]],
        'MSDs': [float(i) for i in [0, 2, 8, 18]],
        'Gauss': [float(i) for i in [0, 0.25, 0.25, 0.25]]
    }
    dft = pd.DataFrame(data=dt)

    pdt.assert_frame_equal(ft.unmask_track(m_df[m_df['Track_ID'] == 2]), dft)
def sensitivity_it(counter):

    import matplotlib as mpl
    mpl.use('Agg')
    import matplotlib.pyplot as plt
    import diff_classifier.aws as aws
    import diff_classifier.utils as ut
    import diff_classifier.msd as msd
    import diff_classifier.features as ft
    import diff_classifier.imagej as ij
    import diff_classifier.heatmaps as hm

    from scipy.spatial import Voronoi
    import scipy.stats as stats
    from shapely.geometry import Point
    from shapely.geometry.polygon import Polygon
    import matplotlib.cm as cm
    import os
    import os.path as op
    import numpy as np
    import numpy.ma as ma
    import pandas as pd
    import boto3
    import itertools

    #Sweep parameters
    #----------------------------------
    radius = [4.5, 6.0, 7.0]
    do_median_filtering = [True, False]
    quality = [1.5, 4.5, 8.5]
    linking_max_distance = [6.0, 10.0, 15.0]
    gap_closing_max_distance = [6.0, 10.0, 15.0]
    max_frame_gap = [1, 2, 5]
    track_displacement = [0.0, 10.0, 20.0]

    sweep = [
        radius, do_median_filtering, quality, linking_max_distance,
        gap_closing_max_distance, max_frame_gap, track_displacement
    ]
    all_params = list(itertools.product(*sweep))

    #Variable prep
    #----------------------------------
    s3 = boto3.client('s3')

    folder = '01_18_Experiment'
    s_folder = '{}/sensitivity'.format(folder)
    local_folder = '.'
    prefix = "P1_S1_R_0001_2_2"
    name = "{}.tif".format(prefix)
    local_im = op.join(local_folder, name)
    aws.download_s3(
        '{}/{}/{}.tif'.format(folder,
                              prefix.split('_')[0], prefix),
        '{}.tif'.format(prefix))

    outputs = np.zeros((len(all_params), len(all_params[0]) + 2))

    #Tracking and calculations
    #------------------------------------
    params = all_params[counter]
    outfile = 'Traj_{}_{}.csv'.format(name.split('.')[0], counter)
    msd_file = 'msd_{}_{}.csv'.format(name.split('.')[0], counter)
    geo_file = 'geomean_{}_{}.csv'.format(name.split('.')[0], counter)
    geoS_file = 'geoSEM_{}_{}.csv'.format(name.split('.')[0], counter)
    msd_image = 'msds_{}_{}.png'.format(name.split('.')[0], counter)
    iter_name = "{}_{}".format(prefix, counter)

    ij.track(local_im,
             outfile,
             template=None,
             fiji_bin=None,
             radius=params[0],
             threshold=0.,
             do_median_filtering=params[1],
             quality=params[2],
             x=511,
             y=511,
             ylo=1,
             median_intensity=300.0,
             snr=0.0,
             linking_max_distance=params[3],
             gap_closing_max_distance=params[4],
             max_frame_gap=params[5],
             track_displacement=params[6])

    traj = ut.csv_to_pd(outfile)
    msds = msd.all_msds2(traj, frames=651)
    msds.to_csv(msd_file)
    gmean1, gSEM1 = hm.plot_individual_msds(iter_name, alpha=0.05)
    np.savetxt(geo_file, gmean1, delimiter=",")
    np.savetxt(geoS_file, gSEM1, delimiter=",")

    aws.upload_s3(outfile, '{}/{}'.format(s_folder, outfile))
    aws.upload_s3(msd_file, '{}/{}'.format(s_folder, msd_file))
    aws.upload_s3(geo_file, '{}/{}'.format(s_folder, geo_file))
    aws.upload_s3(geoS_file, '{}/{}'.format(s_folder, geoS_file))
    aws.upload_s3(msd_image, '{}/{}'.format(s_folder, msd_image))

    print('Successful parameter calculations for {}'.format(iter_name))