def tracksSpeedsPerSpike(exp_files):
    sns.set()
    sns.set_context('paper')

    for i, exp_tuple in enumerate(exp_files):
        dir_name = path.dirname(exp_tuple[0])

        cur_artifacts = Artifacts(expLocation=dir_name)

        tracks_by_pulse = cur_artifacts.getArtifact('tracks_by_pulse')

        for key in tracks_by_pulse.keys():
            pulse_df = pd.DataFrame()

            current_tracks = tracks_by_pulse[key]
            spikes_speeds = np.array([t._tracksSpeeds for t in current_tracks])
            # Getting rid start/end time artifacts.
            spikes_speeds = spikes_speeds[:, 2:-2]

            df = pd.DataFrame({
                'Time':
                tuple(range(0, spikes_speeds.shape[1])) * 0.5 *
                spikes_speeds.shape[0],
                'Speed':
                np.reshape(spikes_speeds, np.size(spikes_speeds))
            })

            fig = sns.lineplot(x='time', y='speed', data=df, ci=None)
            fig.set_axes_label('Speed [au]', 'Time [s]')
            pass
def main():
    folder = '/home/itskov/Temp/Chris'

    #folder = '/mnt/storageNASRe/ChristianData/ChrisNewTracks'

    conds = ['LTAP.', 'LTAV.', 'STAP.', 'STAV.', 'NAIVE']

    df = None
    for cond in conds:
        print('Cond: ' + cond)
        for i, filename in enumerate(Path(folder).rglob(cond + "*")):
            print('Working on: %s' % (filename, ))

            currentArt = Artifacts(expLocation=filename)
            current_roi = currentArt.getArtifact('roi')

            if current_roi is None:
                continue

            if current_roi['wormCount'] < 20:
                print('Not enough worms: %d. Continuing.' %
                      (current_roi['wormCount'], ))
                continue

            TIME = 2160
            frames = range(1, len(current_roi['arrivedFrac']) + 1)
            arrived_frac = np.maximum(0, current_roi['arrivedFrac'][0:TIME])
            current_df = pd.DataFrame({
                'frame': range(1, TIME + 1),
                'arrived_frac': arrived_frac,
                'cond': cond
            })

            df = current_df if df is None else pd.concat(
                (df, current_df), ignore_index=True)

    sns.set()
    #sns.lineplot(x='frame',y='arrived_frac', hue='cond', data=df, estimator=np.median, ci=68, n_boot=1000)
    df.to_csv('/home/itskov/workspace/fraction_in_time.csv')

    #plt.ylim([-0.001, 0.15])
    #plt.xlabel('Frame [2hz]')
    #plt.ylabel('Arrived Fraction')
    #plt.show()

    pass
def tracksSpeedsForAllSpikes(exp_files, paper=False, show=True):
    #sns.set()
    if not paper:
        sns.set_context('talk')
        plt.style.use('dark_background')
    else:
        sns.set_context('paper')

    df = pd.DataFrame({'Frame': [], 'Speed': [], 'Activation Duration': []})

    for i, exp_tuple in enumerate(exp_files):
        dir_name = path.dirname(exp_tuple[0])

        cur_artifacts = Artifacts(expLocation=dir_name)

        tracks_for_all_pulses = cur_artifacts.getArtifact(
            'tracks_for_all_spikes')

        spikes_speeds = np.array(
            [t._tracksSpeeds for t in tracks_for_all_pulses])
        # Getting rid start/end time artifacts.
        spikes_speeds = spikes_speeds[:, 2:-2]
        norm_factor = np.reshape(np.mean(spikes_speeds[:, 0:10], axis=1),
                                 (spikes_speeds.shape[0], 1))
        spikes_speeds = spikes_speeds - norm_factor

        current_df = pd.DataFrame({
            'Time':
            tuple(np.array(range(0, spikes_speeds.shape[1])) * 0.5) *
            spikes_speeds.shape[0],
            'Speed':
            np.reshape(spikes_speeds, np.size(spikes_speeds)),
            'Activation Duration':
            exp_tuple[1]
        })

        cp = sns.cubehelix_palette(8)
        ax = sns.lineplot(x='Time',
                          y='Speed',
                          data=current_df,
                          ci=95,
                          color=cp[5],
                          linewidth=2)
        plt.gca().grid(alpha=0.2)
        ax.set(xlabel='Time [s]', ylabel='Normalized Speed [au]')
        plt.gca().grid(alpha=0.2)

        df = pd.concat((df, current_df))
        pass

    ax = sns.lineplot(x='Time',
                      y='Speed',
                      hue='Activation Duration',
                      data=df,
                      ci=None,
                      linewidth=2,
                      alpha=0.75)
    plt.gca().grid(alpha=0.2)
    ax.set(xlabel='Time [s]', ylabel='Speed [au]')
    plt.gca().grid(alpha=0.2)

    if show:
        plt.show()
    pass
示例#4
0
def day_summary_plots(exp_pairs,
                      titles,
                      legends,
                      paper=False,
                      show=True,
                      output_file=None):
    number_of_exps = len(exp_pairs)

    fig, axs = plt.subplots(2,
                            int(np.ceil(number_of_exps / 2)),
                            figsize=(16, 4))
    plt.subplots_adjust(wspace=0.3, top=0.93, hspace=0.51)

    if not paper:
        plt.style.use("dark_background")
        sns.set_context("talk")
    else:
        sns.set_context("paper")

    axs = np.atleast_2d(axs)

    df = pd.DataFrame({'Time': [], 'ATR+': [], 'ATR-': [], 'Type': []})

    for i, exp_pair in enumerate(exp_pairs):

        # ROI plot
        plt.sca(axs[int(i >= 3)][int(i % 3)])
        first_exp_art = Artifacts(expLocation=exp_pair[0])
        second_exp_art = Artifacts(expLocation=exp_pair[1])

        PairWiseRoi_(['ATR+', 'ATR- (Ctrl)'], [
            first_exp_art.getArtifact('roi'),
            second_exp_art.getArtifact('roi')
        ],
                     showShow=False,
                     paper=paper,
                     show_count=False,
                     freq=120)
        #plt.title(titles[i])
        #plt.legend(fontsize='small', title_fontsize='40')

        first_mean_speed = np.mean(first_exp_art.getArtifact('speed')['speed'])
        second_mean_speed = np.mean(
            second_exp_art.getArtifact('speed')['speed'])

        first_mean_proj = np.mean(first_exp_art.getArtifact('proj')['proj'])
        second_mean_proj = np.mean(second_exp_art.getArtifact('proj')['proj'])

        first_max_roi = np.mean(
            first_exp_art.getArtifact('roi')['arrivedFrac'])
        second_max_roi = np.mean(
            second_exp_art.getArtifact('roi')['arrivedFrac'])

        cur_df = pd.DataFrame({
            'Time':
            i,
            'ATR+': [first_mean_speed, first_mean_proj, first_max_roi],
            'ATR-': [second_mean_speed, second_mean_proj, second_max_roi],
            'Type': ['Speed', 'Projection', 'Roi']
        })

        df = pd.concat((df, cur_df), ignore_index=True)

    #fig.tight_layout()
    #fig.savefig('/home/itskov/Dropbox/dayfigs.png')

    if show:
        plt.show()

    return df
示例#5
0
from Behavior.Tools.Artifacts import Artifacts
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

art_120a = Artifacts(expLocation='/mnt/storageNASRe/tph1/Results/22-Jan-2020/TPH_1_ATR_TRAIN_65M_D120_NO_IAA3x5.avi_12.57.03/')
art_120na = Artifacts(expLocation='/mnt/storageNASRe/tph1/Results/22-Jan-2020/TPH_1_NO_ATR_TRAIN_65M_D120_NO_IAA3x5.avi_12.56.12/')

art_180a = Artifacts(expLocation='/mnt/storageNASRe/tph1/Results/22-Jan-2020/TPH_1_ATR_TRAIN_65M_D180_NO_IAA3x5.avi_15.29.04/')
art_180na = Artifacts(expLocation='/mnt/storageNASRe/tph1/Results/22-Jan-2020/TPH_1_NO_ATR_TRAIN_65M_D180_NO_IAA3x5.avi_15.28.03/')

art_240a = Artifacts(expLocation='/mnt/storageNASRe/tph1/Results/22-Jan-2020/TPH_1_ATR_TRAIN_65M_D240_NO_IAA3x5.avi_17.28.52/')
art_240na = Artifacts(expLocation='/mnt/storageNASRe/tph1/Results/22-Jan-2020/TPH_1_NO_ATR_TRAIN_65M_D240_NO_IAA3x5.avi_17.28.04/')

art_300a = Artifacts(expLocation='/mnt/storageNASRe/tph1/Results/22-Jan-2020/TPH_1_ATR_TRAIN_65M_D300_NO_IAA3x5.avi_20.12.12/')
art_300na = Artifacts(expLocation='/mnt/storageNASRe/tph1/Results/22-Jan-2020/TPH_1_NO_ATR_TRAIN_65M_D300_NO_IAA3x5.avi_20.11.34/')


art_120a_frac = art_120a.getArtifact('roi')['arrivedFrac'][4499]
art_120na_frac = art_120na.getArtifact('roi')['arrivedFrac'][4499]

art_180a_frac = art_180a.getArtifact('roi')['arrivedFrac'][4499]
art_180na_frac = art_180na.getArtifact('roi')['arrivedFrac'][4499]

art_240a_frac = art_240a.getArtifact('roi')['arrivedFrac'][4499]
art_240na_frac = art_240na.getArtifact('roi')['arrivedFrac'][4499]

art_300a_frac = art_300a.getArtifact('roi')['arrivedFrac'][4499]
art_300na_frac = art_300na.getArtifact('roi')['arrivedFrac'][4499]

示例#6
0
def scatterEnhacnment(pairsDf, paper=False, figNames=None):
    MAX_TIME = 3750

    plotDfArrivedFrac = pd.DataFrame({'Strain': [], 'ATR+': [], 'ATR-': []})
    plotDfProjection = pd.DataFrame({'Strain': [], 'ATR+': [], 'ATR-': []})
    plotDfSpeed = pd.DataFrame({'Strain': [], 'ATR+': [], 'ATR-': []})

    linePlotDf = pd.DataFrame({
        'Strain': [],
        'time': [],
        'FrationArrived': [],
        'Exp': []
    })

    print(pairsDf.shape)
    for i in range(pairsDf.shape[0]):
        try:
            current_row = pairsDf.iloc[i]
            files = np.array(current_row['files'])

            noAtrInd = np.array([file.find('NO_ATR') >= 0 for file in files])

            if np.sum(noAtrInd) != 1:
                print('*** Error: Invalid pair ***')
            else:
                atrFile = files[np.logical_not(noAtrInd)][0]
                noAtrFile = files[noAtrInd][0]

                print(atrFile)
                print(noAtrFile)

                atrRoi = Artifacts(expLocation=atrFile).getArtifact('roi')
                noAtrRoi = Artifacts(expLocation=noAtrFile).getArtifact('roi')

                if atrRoi['wormCount'] < 30 or noAtrRoi['wormCount'] < 30:
                    print('Not enough worms. Continuing.')
                    continue

                plotDfArrivedFrac = plotDfArrivedFrac.append(
                    {
                        'Strain': current_row['Strain'],
                        'ATR+': np.minimum(atrRoi['arrivedFrac'][MAX_TIME], 1),
                        'ATR-': np.minimum(noAtrRoi['arrivedFrac'][MAX_TIME],
                                           1),
                        'ExpId': i
                    },
                    ignore_index=True)

                atrProj = Artifacts(
                    expLocation=atrFile).getArtifact('proj')['proj']
                noAtrProj = Artifacts(
                    expLocation=noAtrFile).getArtifact('proj')['proj']

                plotDfProjection = plotDfProjection.append(
                    {
                        'Strain': current_row['Strain'],
                        'ATR+': np.mean(atrProj),
                        'ATR-': np.mean(noAtrProj),
                        'ExpId': i
                    },
                    ignore_index=True)

                atrSpeed = Artifacts(
                    expLocation=atrFile).getArtifact('speed')['speed']
                noAtrSpeed = Artifacts(
                    expLocation=noAtrFile).getArtifact('speed')['speed']

                plotDfSpeed = plotDfSpeed.append(
                    {
                        'Strain': current_row['Strain'],
                        'ATR+': np.mean(atrSpeed),
                        'ATR-': np.mean(noAtrSpeed),
                        'ExpId': i
                    },
                    ignore_index=True)

                print(
                    "Arrival vals: ATR+:%f, ATR-:%f, Projection mean: ATR+:%f, ATR-:%f, Speed mean ATR+:%f, ATR-:%f"
                    % (atrRoi['arrivedFrac'][MAX_TIME],
                       noAtrRoi['arrivedFrac'][MAX_TIME], np.mean(atrProj),
                       np.mean(noAtrProj), np.mean(atrSpeed),
                       np.mean(noAtrSpeed)))

                print(atrRoi['arrivedFrac'][0:MAX_TIME].shape)
                print(np.array(list(range(MAX_TIME))).shape)

                currentLinePlot = pd.DataFrame({
                    'Strain':
                    current_row['Strain'],
                    'time':
                    np.array(list(range(MAX_TIME))) * 0.5,
                    'FractionArrived':
                    atrRoi['arrivedFrac'][0:MAX_TIME],
                    'Exp':
                    'ATR'
                })

                linePlotDf = pd.concat((linePlotDf, currentLinePlot))

                currentLinePlot = pd.DataFrame({
                    'Strain':
                    current_row['Strain'],
                    'time':
                    np.array(list(range(MAX_TIME))) * 0.5,
                    'FractionArrived':
                    noAtrRoi['arrivedFrac'][0:MAX_TIME],
                    'Exp':
                    'NO ATR'
                })

                linePlotDf = pd.concat((linePlotDf, currentLinePlot))
        except Exception as e:
            raise e

    #print(plotDf)
    #DEBUG
    #plotDfArrivedFrac.to_pickle('/home/itskov/Dropbox/tempBundle_arrival.pkl')
    #plotDfProjection.to_pickle('/home/itskov/Dropbox/tempBundle_proj.pkl')
    #plotDfSpeed.to_pickle('/home/itskov/Dropbox/tempBundle_speed.pkl')
    #linePlotDf.to_pickle('/home/itskov/Dropbox/tempBundle2.pkl')
    #DEBUG

    if paper == False:
        plt.style.use("dark_background")
        sns.set_context('talk')
        cp = sns.dark_palette("purple", 7)
    else:
        cp = sns.cubehelix_palette(8, start=.5, rot=-.75)

    if paper == False:
        ax = sns.scatterplot(x='ATR-',
                             y='ATR+',
                             data=plotDfArrivedFrac,
                             linewidth=0,
                             alpha=0.85,
                             color=cp[6])
        ax.plot([0, 1.1], [0, 1.1], ":")
    else:
        ax = sns.scatterplot(x='ATR-',
                             y='ATR+',
                             data=plotDfArrivedFrac,
                             linewidth=0,
                             alpha=0.85,
                             color=cp[6])
        ax.plot([0, 1.1], [0, 1.1], ":", color='k')

    plt.xlim(0, 1.1)
    plt.ylim(0, 1.1)
    plt.gca().grid(alpha=0.2)
    plt.title('Arrival Fracion, n = %d' % (plotDfArrivedFrac.shape[0], ),
              loc='left')
    if figNames is None:
        plt.show()
    else:
        plt.gcf().savefig(figNames[0], format='svg')

    # Plotting the projection plot.
    #plt.style.use("dark_background")
    #sns.set_context('talk')
    #cp = sns.dark_palette("purple", 7)

    ax = sns.scatterplot(x='ATR-',
                         y='ATR+',
                         data=plotDfProjection,
                         linewidth=0,
                         alpha=0.85,
                         color=cp[6])

    xmin = np.min(plotDfProjection['ATR-'])
    xmax = np.max(plotDfProjection['ATR-'])
    ymin = np.min(plotDfProjection['ATR-'])
    ymax = np.max(plotDfProjection['ATR+'])

    if paper == False:
        ax.plot([0, 1], [0, 1], ":")
    else:
        ax.plot([0, 1], [0, 1], ":", color='k')

    plt.xlim(0, 0.35)
    plt.ylim(0, 0.35)
    plt.gca().grid(alpha=0.2)
    plt.locator_params(axis='y', nbins=6)
    plt.locator_params(axis='x', nbins=6)
    plt.title('Mean Projection, n = %d' % (plotDfArrivedFrac.shape[0], ),
              loc='left')
    if figNames is None:
        plt.show()
    else:
        plt.gcf().savefig(figNames[1], format='svg')

    # Plotting the speed plot.
    #plt.style.use("dark_background")
    #sns.set_context('talk')
    #cp = sns.dark_palette("purple", 7)
    ax = sns.scatterplot(x='ATR-',
                         y='ATR+',
                         data=plotDfSpeed,
                         linewidth=0,
                         alpha=0.85,
                         color=cp[6])

    xmin = np.min(plotDfSpeed['ATR-'])
    xmax = np.max(plotDfSpeed['ATR-'])
    ymin = np.min(plotDfSpeed['ATR+'])
    ymax = np.max(plotDfSpeed['ATR+'])

    if paper == False:
        ax.plot([0, 0.0018], [0, 0.0018], ":")
    else:
        ax.plot([0, 0.0018], [0, 0.0018], ":", color='k')

    plt.xlim(0.0008, 0.0018)
    plt.ylim(0.0008, 0.0018)
    plt.gca().grid(alpha=0.2)
    plt.locator_params(axis='y', nbins=6)
    plt.locator_params(axis='x', nbins=6)
    plt.title('Mean Speed, n = %d' % (plotDfSpeed.shape[0], ), loc='left')
    if figNames is None:
        plt.show()
    else:
        plt.gcf().savefig(figNames[2], format='svg')

    #ax = sns.lineplot(x='time', y='FractionArrived', hue='Exp', data=linePlotDf, ci=68, estimator=np.median)
    #plt.gca().grid(alpha=0.2)
    #ax.set(xlabel='Time [s]')

    #plt.show()
    return (plotDfArrivedFrac, plotDfProjection, plotDfSpeed)
def day_summary_plots(exp_pairs,
                      titles,
                      legends,
                      paper=False,
                      show=True,
                      output_file=None):
    number_of_exps = len(exp_pairs)

    fig, axs = plt.subplots(number_of_exps, 3, figsize=(10, 14))
    plt.subplots_adjust(wspace=0.36,
                        left=0.09,
                        bottom=0.11,
                        right=0.98,
                        top=0.96,
                        hspace=0.36)

    if not paper:
        plt.style.use("dark_background")
        sns.set_context("talk")
    else:
        sns.set_context("paper")

    axs = np.atleast_2d(axs)

    df = pd.DataFrame({'Value': [], 'Type': [], 'Cond': [], 'Exp': []})

    for i, exp_pair in enumerate(exp_pairs):

        first_exp_art = Artifacts(expLocation=exp_pair[0])
        second_exp_art = Artifacts(expLocation=exp_pair[1])

        plt.sca(axs[int(i)][0])
        PairWiseRoi_(['ATR+', 'ATR- (Ctrl)'], [
            first_exp_art.getArtifact('roi'),
            second_exp_art.getArtifact('roi')
        ],
                     showShow=False,
                     paper=paper,
                     show_count=False,
                     freq=120)
        df = df.append(
            {
                'Value': np.max(
                    first_exp_art.getArtifact('roi')['arrivedFrac']),
                'Type': 'Max Roi',
                'Cond': 'ATR+',
                'Exp': i
            },
            ignore_index=True)

        df = df.append(
            {
                'Value': np.max(
                    second_exp_art.getArtifact('roi')['arrivedFrac']),
                'Type': 'Max Roi',
                'Cond': 'ATR-',
                'Exp': i
            },
            ignore_index=True)

        plt.sca(axs[int(i)][1])
        first_speed = (first_exp_art.getArtifact('speed')['speed'])
        second_speed = (second_exp_art.getArtifact('speed')['speed'])
        sns.kdeplot(first_speed, shade=True, label='ATR+')
        ax = sns.kdeplot(second_speed, shade=True, label='ATR- (Control)')
        plt.gca().grid(alpha=0.2)
        ax.set(xlabel="Speed [au / sec]", ylabel="Density")
        plt.locator_params(nbins=5)
        df_cur = pd.DataFrame(
            {
                'Value': np.mean(first_speed),
                'Type': 'Speed [au]',
                'Cond': 'ATR+',
                'Exp': i
            },
            index=[0])

        df = pd.concat((df, df_cur), ignore_index=True)

        df_cur = pd.DataFrame(
            {
                'Value': np.mean(second_speed),
                'Type': 'Speed [au]',
                'Cond': 'ATR-',
                'Exp': i
            },
            index=[0])

        df = pd.concat((df, df_cur), ignore_index=True)

        plt.sca(axs[int(i)][2])
        first_proj = (first_exp_art.getArtifact('proj')['proj'])
        second_proj = (second_exp_art.getArtifact('proj')['proj'])
        sns.kdeplot(first_proj, shade=True, label='ATR+')
        ax = sns.kdeplot(second_proj, shade=True, label='ATR- (Control)')
        plt.gca().grid(alpha=0.2)
        ax.set(xlabel="Projection", ylabel="Density")
        plt.locator_params(nbins=5)

        df_cur = pd.DataFrame(
            {
                'Value': np.mean(first_proj),
                'Type': 'Projection',
                'Cond': 'ATR+',
                'Exp': i
            },
            index=[0])

        df = pd.concat((df, df_cur), ignore_index=True)

        df_cur = pd.DataFrame(
            {
                'Value': np.mean(second_proj),
                'Type': 'Projection',
                'Cond': 'ATR-',
                'Exp': i
            },
            index=[0])

        df = pd.concat((df, df_cur), ignore_index=True)

    if show:
        plt.show()

    return df
def meanProjectionMeanWithIaa(firstGroupName, secondGroupName, rootPath):
    firstExperimentsFiles = list(
        Path(rootPath).rglob(firstGroupName + '*/exp.npy'))
    secondExperimentsFiles = list(
        Path(rootPath).rglob(secondGroupName + '*/exp.npy'))

    interestingTimePoints = [0, 1000, 2000, 3000, 4000]
    firstGroupResults = np.zeros(
        (len(firstExperimentsFiles), len(interestingTimePoints)))
    secoundGroupResults = np.zeros(
        (len(secondExperimentsFiles), len(interestingTimePoints)))

    print('First Group:')
    for i, filename in enumerate(firstExperimentsFiles):
        if 'Nov' not in str(filename):
            continue

        if '03-Nov' in str(filename):
            continue

        # Getting the dirname of the experiment.
        expDirName = path.dirname(filename)
        arts = Artifacts(expLocation=expDirName)

        roiResults = arts.getArtifact('roi')

        if roiResults == None:
            continue

        print('Going over experiment: %s' % (filename, ))

        currentExpResults = roiResults['arrivedFrac']
        timePointResults = np.array(currentExpResults)[interestingTimePoints]
        firstGroupResults[i, :] = timePointResults

    print('Second Group:')
    for i, filename in enumerate(secondExperimentsFiles):
        if 'Nov' not in str(filename):
            continue

        if '03-Nov' in str(filename):
            continue

        # Getting the dirname of the experiment.
        expDirName = path.dirname(filename)
        arts = Artifacts(expLocation=expDirName)

        roiResults = arts.getArtifact('roi')

        if roiResults == None:
            continue

        print('Going over experiment: %s' % (filename, ))

        currentExpResults = roiResults['arrivedFrac']
        timePointResults = np.array(currentExpResults)[interestingTimePoints]
        secoundGroupResults[i, :] = timePointResults

    firstGroupResults = firstGroupResults[
        np.logical_not(np.all(firstGroupResults == 0, axis=1)), :]
    secoundGroupResults = secoundGroupResults[
        np.logical_not(np.all(secoundGroupResults == 0, axis=1)), :]

    flattenFirstGroup = firstGroupResults.flatten(order='C')
    flattenSecondGroup = secoundGroupResults.flatten(order='C')

    fracitons = tuple(flattenFirstGroup) + tuple(flattenSecondGroup)
    conds = (firstGroupName, ) * flattenFirstGroup.size + (
        secondGroupName, ) * flattenSecondGroup.size

    timePoints = int((len(fracitons)) /
                     len(interestingTimePoints)) * tuple(interestingTimePoints)
    df = pd.DataFrame({
        'Fraction Arrived': fracitons,
        'Cond': conds,
        'time': timePoints
    })

    sns.set(style='darkgrid')
    plt.style.use("dark_background")
    ax = sns.pointplot(x='time', y='Fraction Arrived', data=df, hue='Cond')
    ax.set(xlabel='Frames [2Hz]')
    plt.show()

    pairedDf_2 = pd.DataFrame({
        'ATR+':
        firstGroupResults[[1, 2, 3, 4, 5, 6, 7, 9], 2],
        'ATR-':
        secoundGroupResults[[2, 3, 4, 5, 6, 7, 8, 9], 2]
    })
    pairedDf_3 = pd.DataFrame({
        'ATR+':
        firstGroupResults[[1, 2, 3, 4, 5, 6, 7, 9], 3],
        'ATR-':
        secoundGroupResults[[2, 3, 4, 5, 6, 7, 8, 9], 3]
    })
    pairedDf_4 = pd.DataFrame({
        'ATR+':
        firstGroupResults[[1, 2, 3, 4, 5, 6, 7, 9], 4],
        'ATR-':
        secoundGroupResults[[2, 3, 4, 5, 6, 7, 8, 9], 4]
    })
    pairedDf = pd.concat((pairedDf_2, pairedDf_3, pairedDf_4),
                         ignore_index=True)

    ax = sns.scatterplot(x='ATR-', y='ATR+', data=pairedDf)

    x0, x1 = ax.get_xlim()
    y0, y1 = ax.get_ylim()
    lims = [max(x0, y0), min(x1, y1)]
    ax.plot(lims, lims, ':k', color='white')

    print('Done.')
def createDemMovie(exp_dir):
    expDir = ExpDir(exp_dir)
    artifacts = Artifacts(expLocation=exp_dir)

    # Getting the frame intensities values
    frame_intensities = artifacts.getArtifact('frame_intensities')
    frame_intensities /= np.max(frame_intensities)

    frame_range = range(1200, 3000)

    #cap = cv2.VideoCapture(expDir.getVidFile())
    cap = cv2.VideoCapture(expDir.getExpSegVid())
    cap.set(cv2.CAP_PROP_POS_FRAMES, np.min(frame_range))
    frame_intensities = frame_intensities[frame_range]

    plt.style.use('dark_background')
    plt.tight_layout()
    sns.set_context('paper')

    # Setting a 3 to 1 ratio between video and figure.
    fig, axs = \
        plt.subplots(2, 3, gridspec_kw={'height_ratios':[4, 1], 'width_ratios':[1 ,3, 1]})

    ax_vid = axs[0][1]
    ax_fig = axs[1][1]

    ax_vid.axis('off')
    axs[0][0].axis('off')
    axs[0][2].axis('off')
    axs[1][0].axis('off')
    axs[1][2].axis('off')

    # Global variables
    im = None
    vidAlign = 0
    vertLine = None

    def updateMovie(frameNumber):
        nonlocal ax_vid
        nonlocal ax_fig
        nonlocal cap
        nonlocal im
        nonlocal vidAlign
        nonlocal frame_range
        nonlocal vertLine

        print('Frame number: %d' % frameNumber)

        #DEBUG
        cap.set(cv2.CAP_PROP_POS_FRAMES, frameNumber)
        #DEBUG

        # Updating the video
        fig.sca(ax_vid)
        _, frame = cap.read()
        frame = frame[480:1440, 1100:2300]
        if frameNumber == -1:
            vidAlign = alignImage(frame)
            frame = np.roll(frame, vidAlign, axis=1)
            im = plt.imshow(frame)
            cap.set(cv2.CAP_PROP_POS_FRAMES, np.min(frame_range))
        else:
            frame = np.roll(frame, vidAlign, axis=1)
            im.set_data(frame)

        # Updating the figure
        fig.sca(ax_fig)
        if frameNumber == -1:
            plt.gca().grid(alpha=0.2)
            plt.plot(np.array(frame_range) * 0.5,
                     frame_intensities,
                     linewidth=1.5,
                     color=sns.xkcd_rgb['windows blue'],
                     alpha=0.75)
            plt.xlabel('Time [s]')
            h = plt.ylabel('Power [au]')
            #h.set_rotation(0)
            plt.xlim(np.min(frame_range) * 0.5, np.max(frame_range) * 0.5)
        else:
            if vertLine is not None:
                vertLine.remove()

            vertLine = ax_fig.axvline(x=frameNumber * 0.5, ymin=0, ymax=1)

        return (ax_vid, ax_fig)

    anim = FuncAnimation(fig,
                         updateMovie,
                         frames=[1905],
                         init_func=lambda: updateMovie(-1))
    anim.save('/home/itskov/Dropbox/DemMovie2.mp4',
              fps=50,
              extra_args=['-vcodec', 'libx264'],
              dpi=250)

    cap.release()
def gatherTracksForSpikes(exp_file, show_plot=False):
    RANGE_BEFORE = 100
    RANGE_AFTER = 260
    MAX_FRAMES = 3500

    speed_up_times = []

    exp = np.load(exp_file)[0]

    # Filtering the tracks.
    exp._tracks = filterTracksForAnalyses(exp._tracks,
                                          minSteps=370,
                                          minDistance=150)

    # Get pulses in time
    dir_name = path.dirname(exp._videoFilename)
    #pulses_filename = path.join(dir_name, 'pulses.npy')
    #print(pulses_filename)
    artifacts = Artifacts(expLocation=dir_name)
    artifacts.checkForArtifactsDir()
    frame_intensities = artifacts.getArtifact('frame_intensities')
    if frame_intensities is not None:
        print('Found file.')
    else:
        frame_intensities = np.zeros((MAX_FRAMES, ))

        for i in range(MAX_FRAMES - 1):
            _, cur_frame = exp._cap.read()
            frame_intensities[i] = np.mean(cur_frame)
            print('Went over frame: %d' % i)

        artifacts.addArtifact('frame_intensities', frame_intensities)

    # Looking for spike points
    spike_points = np.where(np.diff(frame_intensities) > 10)[0]
    spike_points = spike_points[1:]

    spike_ranges = []
    for spike_point in spike_points:
        spike_ranges.append(
            list(range(spike_point - RANGE_BEFORE, spike_point + RANGE_AFTER)))

    tracks_for_spikes = []
    tracks_for_single_spikes = {}

    df = pd.DataFrame({'frame': [], 'speed': []})
    # Going over the relevant tacks.
    for track in exp._tracks:
        current_df = pd.DataFrame({
            'frame': track._trackFrames,
            'speed': track._tracksSpeeds
        })
        df = pd.concat((df, current_df))
        for i, spike_range in enumerate(spike_ranges):
            # DEBUG
            # if i != 3:
            #    continue
            # DEBUG
            if spike_range[0] in track._trackFrames and spike_range[
                    -1] in track._trackFrames:
                new_track = track.trimTrack(spike_range[-1], spike_range[0])
                tracks_for_spikes.append(new_track)
                tracks_for_single_spikes[i] = tracks_for_single_spikes[i] + [new_track]\
                    if i in tracks_for_single_spikes.keys() else [new_track]

        pass

    # Pick spike light represenetitive
    spike_rep = frame_intensities[spike_ranges[1]]

    spikes_speeds = np.array([t._tracksSpeeds for t in tracks_for_spikes])
    spikes_angles = np.array([
        t.getAngles(exp._regionsOfInterest['endReg']['pos'])
        for t in tracks_for_spikes
    ])
    spikes_reversals = np.array(
        [t._tracksReversals for t in tracks_for_spikes])

    spikes_angles[:, 0] = spikes_angles[:, 1]
    spikes_angles[:, -2] = spikes_angles[:, -3]
    spikes_angles[:, -1] = spikes_angles[:, -3]

    mean_spikes_speeds = np.median(spikes_speeds, axis=0)
    mean_spikes_angles = np.median(spikes_angles, axis=0)

    mean_spikes_speeds = mean_spikes_speeds[2:-2]
    spike_rep = spike_rep[2:-2]

    # Gathering the speeding up time.
    '''smoothed_speeds = savgol_filter(mean_spikes_speeds, 11, 3)
    spike_decay_point = np.max(np.where(np.diff(spike_rep) < -5)) + 3
    speed_up_time = \
        np.min(np.where(smoothed_speeds[spike_decay_point:] >= np.mean(smoothed_speeds[0:RANGE_BEFORE])))

    speed_up_times.append(speed_up_time)'''

    artifacts.addArtifact('tracks_by_pulse', tracks_for_single_spikes)
    artifacts.addArtifact('tracks_for_all_spikes', tracks_for_spikes)
示例#11
0
def demonstrateLightingSegmentation(exp_dir):
    expDir = ExpDir(exp_dir)
    artifacts = Artifacts(expLocation=exp_dir)

    # Getting the light intensity file
    light_intensities = artifacts.getArtifact('frame_intensities')

    base_line = np.mean(light_intensities[1:10])

    #vid_cap = cv2.VideoCapture(expDir.getVidFile())
    vid_cap = cv2.VideoCapture(
        '/mnt/storageNASRe/tph1/12.03.20/12-Mar-2020-21.16.44-MIC2-TPH_1_ATR_ONLINE[NO_IAA]_5S.avi.mj2'
    )
    seg_cap = cv2.VideoCapture(expDir.getExpSegVid())

    spike_count = 0
    in_spike = False
    in_spike_count = 0

    full_vid_handle = FFmpegWriter('/home/itskov/Dropbox/dem.mp4',
                                   outputdict={'-crf': '0'})

    # Reading first frame for shape.
    _, first_frame = vid_cap.read()
    _, second_frame = seg_cap.read()

    # Alignining only the segmented version.
    seg_frame_align = alignImage(second_frame)

    start_frame = 700
    vid_cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
    seg_cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)

    for i in range(start_frame, 2000):
        _, vid_frame = vid_cap.read()
        _, seg_frame = seg_cap.read()

        vid_frame = cv2.cvtColor(vid_frame, cv2.COLOR_BGR2GRAY)
        vid_frame = cv2.cvtColor(vid_frame, cv2.COLOR_GRAY2BGR)

        #vid_frame = np.reshape(vid_frame, (vid_frame.shape))

        vid_frame = np.roll(vid_frame, seg_frame_align, axis=1)
        seg_frame = np.roll(seg_frame, seg_frame_align, axis=1)

        if light_intensities[i] > (base_line + 7):
            in_spike = True
            in_spike_count += 1

            if in_spike_count == 6 and spike_count % 2 == 0 and spike_count > 0:
                splits_num = 80
                width = vid_frame.shape[1]
                segments = [
                    tuple(s)
                    for s in np.split(np.array(range(width)), splits_num)
                ]

                for i in range(2 * splits_num - 1):
                    bar_step = i % (splits_num * 2)
                    if bar_step > splits_num:
                        bar_step = splits_num - (bar_step - splits_num)

                    if bar_step > 0 and bar_step < splits_num:
                        print(bar_step)
                        #print(segments)
                        #print(segments[bar_step:])

                        first_part = vid_frame[:,
                                               np.ravel(segments[bar_step:]
                                                        ), :]
                        second_part = seg_frame[:,
                                                np.ravel(segments[0:bar_step]
                                                         ), :]

                        new_frame = np.concatenate((second_part, first_part),
                                                   axis=1)
                        cur_img = Image.fromarray(new_frame)
                        cur_imgdraw = ImageDraw.Draw(cur_img)

                        cur_imgdraw.ellipse((40, 40, 180, 180),
                                            fill='red',
                                            outline='red')

                        full_vid_handle.writeFrame(np.array(cur_img))

            else:
                # Do Spike stuff here.
                cur_img = Image.fromarray(vid_frame)
                cur_imgdraw = ImageDraw.Draw(cur_img)

                cur_imgdraw.ellipse((40, 40, 180, 180),
                                    fill='red',
                                    outline='red')
                full_vid_handle.writeFrame(np.array(cur_img))
        else:
            if in_spike == True:
                in_spike = False
                spike_count += 1

            in_spike_count = 0
            full_vid_handle.writeFrame(vid_frame)

        print('Frame %d. Spike Count; %d' % (i, spike_count))

    vid_cap.release()
    seg_cap.release()
    full_vid_handle.close()
示例#12
0
    from Behavior.Visualizers.OccupVisualizer import OccupVisualizer

    import numpy as np

    rootDir = '/home/itskov/Temp/Chris/'

    for fileName in Path(rootDir).rglob('*/exp.npy'):
        print(fileName)

        try:
            # load experiment.
            exp = np.load(fileName)[0]
            exp.trimExperiment(4500)

            # Create an artifact folder.
            art = Artifacts(exp)

            # Checking for artifact dirs.
            art.checkForArtifactsDir()

            roiAnalyses = RoiAnalysis(exp)
            projectionAnalyses = ProjectionAnalyses(exp)
            occupAnalyses = OccupVisualizer(exp)

            roiAnalyses.execute()
            projectionAnalyses.execute()
            occupAnalyses.execute(showPlot=False)

            #DEBUG Chris
            exp._regionsOfInterest['startReg']['rad'] = exp._scale / 4
            exp._regionsOfInterest['endReg']['rad'] = exp._scale / 3
示例#13
0
def analyzeSpeedUps(exps, paper=False, fitLinear=True, show=True):

    delays = []
    speed_ups = []

    for expDir, delay in exps:
        artifacts = Artifacts(expLocation=expDir)
        frame_intensities = artifacts.getArtifact('frame_intensities')
        tracks_for_all_spikes = artifacts.getArtifact('tracks_for_all_spikes')

        speeds = [t._tracksSpeeds for t in tracks_for_all_spikes]

        signal = np.mean(speeds, axis=0)
        signal = signal[2:-2]
        min_decay_ind = np.argmin(signal)
        signal = signal[min_decay_ind:]
        signal = signal / signal[-1]

        xdata = np.array(range(signal.shape[0])) * 0.5

        popt, pcov = curve_fit(func, xdata, signal)

        plt.plot(xdata, signal)
        plt.plot(xdata, func(xdata, *popt), 'r-')
        print(popt)

        delays.append(delay)
        speed_ups.append(popt[1])

        pass

    regressor = LinearRegression()
    delays = np.array(delays).reshape(-1, 1)
    speed_ups = np.array(speed_ups).reshape(-1, 1)
    regressor.fit(delays, speed_ups)
    predicted = regressor.predict(delays)

    linearDf = pd.DataFrame({
        'Activation Duration [s]': np.ravel(delays),
        'Time Coefficient [s]': np.ravel(speed_ups),
        'Predicted': np.ravel(predicted)
    })

    if not fitLinear:
        print(np.ravel(delays))
        print(np.ravel(speed_ups))
        popt, pcov = curve_fit(func, np.ravel(delays), np.ravel(speed_ups))

    plt.close()
    if not paper:
        sns.set_context('talk')
        plt.style.use("dark_background")
        cp = sns.dark_palette("purple", 7)

        if fitLinear:
            sns.lineplot(data=linearDf,
                         x='Activation Duration [s]',
                         y='Predicted',
                         ci=None,
                         color=cp[2])
        else:
            xvals = np.linspace(np.min(delays), np.max(delays), 1000)
            plt.plot(xvals, func(xvals, *popt), color=cp[2])

        sns.scatterplot(data=linearDf,
                        x='Activation Duration [s]',
                        y='Time Coefficient [s]',
                        linewidth=0,
                        alpha=0.98,
                        color=cp[6])

    else:
        sns.set_context('paper')

        if fitLinear:
            sns.lineplot(data=linearDf,
                         x='Activation Duration [s]',
                         y='Predicted',
                         ci=None)
        else:
            xvals = np.linspace(np.min(delays), np.max(delays), 1000)
            plt.plot(xvals, func(xvals, *popt))

        sns.scatterplot(data=linearDf,
                        x='Activation Duration [s]',
                        y='Time Coefficient [s]',
                        linewidth=0,
                        alpha=0.98)

    plt.title('$R^2 = %.2f$' % (r2_score(speed_ups, predicted), ), loc='left')
    plt.gca().grid(alpha=0.2)

    if show:
        plt.show()
    pass