def figure1k1(info,path):
    if not os.path.exists(path):
        os.makedirs(path)
        
    sessions = info.groupby(level=['subject','session'])
    for (subject,session),sinfo in sessions:
        subjectpath = os.path.join(activitymovies.datafolder,subject)
        sact = activitytables.read_subjects(subjectpath,days=[session])
        scr = activitytables.read_subjects(subjectpath,days=[session],
                                           selector = activitytables.crossings)
        features = activitytables.stepfeature(sact,scr,4,3)
        leftwards = features.side == 'leftwards'
        features.xhead[leftwards] = max_width_cm - features.xhead[leftwards]
        median = np.sort(features.xhead)[len(features)/2+1]
        median = features[features.xhead == median].iloc[-1,:]
        
        vidpaths = activitymovies.getmoviepath(sinfo)
        timepaths = activitymovies.gettimepath(info)
        movie = [video.video(mpath,mtimepath)
                 for mpath,mtimepath in zip(vidpaths,timepaths)][0]
        medianframe = movie.frame(sact.index.get_loc(median.name))
        flip = median.side == 'rightwards'
        stepindex = 3 if flip else 4
        medianframe = activitytables.cropstep(medianframe,stepindex,flip=flip)
        
        fname = str.format("{0}_session_{1}_median_step_posture.png",
                           subject, session)
        fpath = os.path.join(path,subject)
        if not os.path.exists(fpath):
            os.makedirs(fpath)
        fpath = os.path.join(fpath,fname)
        cv2.imwrite(fpath,medianframe)
def slipframes(slipactivity,
               info,
               cropsize=(300, 200),
               subtractBackground=False):
    vidpaths = activitymovies.getmoviepath(info)
    timepaths = activitymovies.gettimepath(info)
    backpaths = activitymovies.getbackgroundpath(info)
    videos = [
        video.video(path, timepath)
        for path, timepath in zip(vidpaths, timepaths)
    ]

    frames = []
    for index, trial in slipactivity.iterrows():
        rightwards = trial.side == 'rightwards'
        frame = videos[0].frame(trial.frame)
        background = None
        if subtractBackground:
            timestamp = videos[0].timestamps[trial.frame]
            background = activitymovies.getbackground(backpaths[0], timestamp)
        if cropsize is not None:
            frame = cropslip(frame, trial.gapindex, cropsize, background,
                             rightwards)
        frames.append(frame)
    return frames
def roiframes(indices,
              side,
              info,
              leftroi,
              rightroi,
              croproi,
              cropsize=(300, 300),
              subtractBackground=False):
    # Tile step frames
    vidpaths = activitymovies.getmoviepath(info)
    timepaths = activitymovies.gettimepath(info)
    backpaths = activitymovies.getbackgroundpath(info)
    videos = [
        video.video(path, timepath)
        for path, timepath in zip(vidpaths, timepaths)
    ]

    frames = []
    for frameindex, side in zip(indices, side):
        leftwards = side == 'leftwards'
        roiindex = leftroi if leftwards else rightroi

        frame = videos[0].frame(frameindex)
        background = None
        if subtractBackground:
            timestamp = videos[0].timestamps[frameindex]
            background = activitymovies.getbackground(backpaths[0], timestamp)
        frame = croproi(frame, roiindex, cropsize, background,
                        roiindex == rightroi)
        frames.append(frame)
    return frames
def clusterstepframes(cr, info, leftstep, rightstep):
    # Compute step times
    stepactivity = cr.iloc[:, 16:24]
    stepdiff = stepactivity.diff()
    steppeaks = siphon.findpeaksMax(stepdiff, 1500)
    pksloc = [[stepdiff.index.get_loc(peak) for peak in step]
              for step in steppeaks]

    # Tile step frames
    vidpaths = activitymovies.getmoviepath(info)
    timepaths = activitymovies.gettimepath(info)
    backpaths = activitymovies.getbackgroundpath(info)
    videos = [
        video.video(path, timepath)
        for path, timepath in zip(vidpaths, timepaths)
    ]

    def getstepframes(stepindex, flip=False):
        stepcenterxcm = stepcenters[stepindex][1] * preprocess.width_pixel_to_cm
        framehead = [
            p for p in pksloc[stepindex]
            if (cr.xhead[p] < stepcenterxcm
                if not flip else cr.xhead[p] > stepcenterxcm)
        ]

        frames = [
            imgproc.croprect(stepcenters[stepindex], (200, 200),
                             videos[0].frame(p)) for p in framehead
        ]
        backgrounds = [
            imgproc.croprect(
                stepcenters[stepindex], (200, 200),
                activitymovies.getbackground(backpaths[0],
                                             videos[0].timestamps[p]))
            for p in framehead
        ]
        frames = [cv2.subtract(f, b) for f, b in zip(frames, backgrounds)]
        if flip:
            frames = [cv2.flip(f, 1) for f in frames]
        return frames, framehead

    leftframes, leftindices = getstepframes(leftstep, False)
    rightframes, rightindices = getstepframes(rightstep, True)
    frames = np.array(leftframes + rightframes)
    frameindices = np.array(leftindices + rightindices)
    sortindices = np.argsort(frameindices)
    frames = frames[sortindices]
    frameindices = frameindices[sortindices]

    R, labels, h = imgproc.cluster(frames, videos[0], frameindices)
    return frames, stepdiff, steppeaks, pksloc, R, labels
Exemplo n.º 5
0
def clusterroiframes(act, roiactivity, info, leftroi, rightroi, roicenter_cm,
                     cropframes):
    # Compute step times
    roidiff = roiactivity.diff()
    roipeaks = activitytables.findpeaks(roidiff, 1500)
    pksloc = [[roidiff.index.get_loc(peak) for peak in roi]
              for roi in roipeaks]

    # Tile step frames
    vidpaths = activitymovies.getmoviepath(info)
    timepaths = activitymovies.gettimepath(info)
    backpaths = activitymovies.getbackgroundpath(info)
    videos = [
        video.video(path, timepath)
        for path, timepath in zip(vidpaths, timepaths)
    ]

    def getroiframes(roiindex, flip=False):
        roicenterxcm = roicenter_cm[roiindex][1]
        headdistance = [act.xhead[p] - roicenterxcm for p in pksloc[roiindex]]
        print headdistance
        framehead = [
            p for i, p in enumerate(pksloc[roiindex])
            if (-25 < headdistance[i] < -5
                if not flip else 5 < headdistance[i] < 25)
        ]

        frames = [cropframes(videos[0].frame(p), roiindex) for p in framehead]
        backgrounds = [
            cropframes(
                activitymovies.getbackground(backpaths[0],
                                             videos[0].timestamps[p]),
                roiindex) for p in framehead
        ]
        frames = [cv2.subtract(f, b) for f, b in zip(frames, backgrounds)]
        if flip:
            frames = [cv2.flip(f, 1) for f in frames]
        return frames, framehead

    leftframes, leftindices = getroiframes(leftroi, False)
    rightframes, rightindices = getroiframes(rightroi, True)
    print "==========================="
    frames = np.array(leftframes + rightframes)
    frameindices = np.array(leftindices + rightindices)
    sortindices = np.argsort(frameindices)
    frames = frames[sortindices]
    frameindices = frameindices[sortindices]

    Z, R, labels, h = imgproc.cluster(frames, videos[0], frameindices)
    return frames, roidiff, roipeaks, pksloc, Z, R, labels
def clusterroiframes(act, roiactivity, info, leftroi, rightroi, roicenter_cm, cropframes):
    # Compute step times
    roidiff = roiactivity.diff()
    roipeaks = activitytables.findpeaks(roidiff, 1500)
    pksloc = [[roidiff.index.get_loc(peak) for peak in roi] for roi in roipeaks]

    # Tile step frames
    vidpaths = activitymovies.getmoviepath(info)
    timepaths = activitymovies.gettimepath(info)
    backpaths = activitymovies.getbackgroundpath(info)
    videos = [video.video(path, timepath) for path, timepath in zip(vidpaths, timepaths)]

    def getroiframes(roiindex, flip=False):
        roicenterxcm = roicenter_cm[roiindex][1]
        headdistance = [act.xhead[p] - roicenterxcm for p in pksloc[roiindex]]
        print headdistance
        framehead = [
            p
            for i, p in enumerate(pksloc[roiindex])
            if (-25 < headdistance[i] < -5 if not flip else 5 < headdistance[i] < 25)
        ]

        frames = [cropframes(videos[0].frame(p), roiindex) for p in framehead]
        backgrounds = [
            cropframes(activitymovies.getbackground(backpaths[0], videos[0].timestamps[p]), roiindex) for p in framehead
        ]
        frames = [cv2.subtract(f, b) for f, b in zip(frames, backgrounds)]
        if flip:
            frames = [cv2.flip(f, 1) for f in frames]
        return frames, framehead

    leftframes, leftindices = getroiframes(leftroi, False)
    rightframes, rightindices = getroiframes(rightroi, True)
    print "==========================="
    frames = np.array(leftframes + rightframes)
    frameindices = np.array(leftindices + rightindices)
    sortindices = np.argsort(frameindices)
    frames = frames[sortindices]
    frameindices = frameindices[sortindices]

    Z, R, labels, h = imgproc.cluster(frames, videos[0], frameindices)
    return frames, roidiff, roipeaks, pksloc, Z, R, labels
def roiframes(activity,crossings,info,leftroi,rightroi,roiframeindices,croproi,
               cropsize=(300,300),subtractBackground=False):
    # Tile step frames    
    vidpaths = activitymovies.getmoviepath(info)
    timepaths = activitymovies.gettimepath(info)
    backpaths = activitymovies.getbackgroundpath(info)
    videos = [video.video(path,timepath) for path,timepath in zip(vidpaths,timepaths)]
    
    frames = []
    indices,side = roiframeindices(activity,crossings,leftroi,rightroi)
    for frameindex,side in zip(indices,side):
        leftwards = side == 'leftwards'
        roiindex = leftroi if leftwards else rightroi
        
        frame = videos[0].frame(frameindex)
        background = None
        if subtractBackground:
            timestamp = videos[0].timestamps[frameindex]
            background = activitymovies.getbackground(backpaths[0],timestamp)
        frame = croproi(frame,roiindex,cropsize,background,roiindex == rightroi)
        frames.append(frame)
    return frames
def clusterstepframes(cr,info,leftstep,rightstep):
    # Compute step times
    stepactivity = cr.iloc[:,16:24]
    stepdiff = stepactivity.diff()
    steppeaks = siphon.findpeaksMax(stepdiff,1500)
    pksloc = [[stepdiff.index.get_loc(peak) for peak in step] for step in steppeaks]    
    
    # Tile step frames
    vidpaths = activitymovies.getmoviepath(info)
    timepaths = activitymovies.gettimepath(info)
    backpaths = activitymovies.getbackgroundpath(info)
    videos = [video.video(path,timepath) for path,timepath in zip(vidpaths,timepaths)]

    def getstepframes(stepindex,flip=False):
        stepcenterxcm = stepcenters[stepindex][1] * preprocess.width_pixel_to_cm
        framehead = [p for p in pksloc[stepindex]
                     if (cr.xhead[p] < stepcenterxcm if not flip else cr.xhead[p] > stepcenterxcm)]
        
        frames = [imgproc.croprect(stepcenters[stepindex],(200,200),videos[0].frame(p))
                  for p in framehead]
        backgrounds = [imgproc.croprect(stepcenters[stepindex],(200,200),activitymovies.getbackground(backpaths[0],videos[0].timestamps[p]))
                       for p in framehead]
        frames = [cv2.subtract(f,b) for f,b in zip(frames,backgrounds)]
        if flip:
            frames = [cv2.flip(f,1) for f in frames]
        return frames,framehead

    leftframes,leftindices = getstepframes(leftstep,False)
    rightframes,rightindices = getstepframes(rightstep,True)
    frames = np.array(leftframes + rightframes)
    frameindices = np.array(leftindices + rightindices)
    sortindices = np.argsort(frameindices)
    frames = frames[sortindices]
    frameindices = frameindices[sortindices]
    
    R,labels,h = imgproc.cluster(frames,videos[0],frameindices)
    return frames,stepdiff,steppeaks,pksloc,R,labels
Exemplo n.º 9
0
def figure1k1(info, path):
    if not os.path.exists(path):
        os.makedirs(path)

    sessions = info.groupby(level=['subject', 'session'])
    for (subject, session), sinfo in sessions:
        subjectpath = os.path.join(activitymovies.datafolder, subject)
        sact = activitytables.read_subjects(subjectpath, days=[session])
        scr = activitytables.read_subjects(subjectpath,
                                           days=[session],
                                           selector=activitytables.crossings)
        features = activitytables.stepfeature(sact, scr, 4, 3)
        leftwards = features.side == 'leftwards'
        features.xhead[leftwards] = max_width_cm - features.xhead[leftwards]
        median = np.sort(features.xhead)[len(features) / 2 + 1]
        median = features[features.xhead == median].iloc[-1, :]

        vidpaths = activitymovies.getmoviepath(sinfo)
        timepaths = activitymovies.gettimepath(info)
        movie = [
            video.video(mpath, mtimepath)
            for mpath, mtimepath in zip(vidpaths, timepaths)
        ][0]
        medianframe = movie.frame(sact.index.get_loc(median.name))
        flip = median.side == 'rightwards'
        stepindex = 3 if flip else 4
        medianframe = activitytables.cropstep(medianframe,
                                              stepindex,
                                              flip=flip)

        fname = str.format("{0}_session_{1}_median_step_posture.png", subject,
                           session)
        fpath = os.path.join(path, subject)
        if not os.path.exists(fpath):
            os.makedirs(fpath)
        fpath = os.path.join(fpath, fname)
        cv2.imwrite(fpath, medianframe)