def advance_analysis(ols,ols_offset,objs,splits,objs_sizes,objs_fols,to_retire_objs,to_retire_objs_sizes,to_retire_objs_fols,last_frames,stream,frames,currsum,denom,seglen,cutoff,frames_offset,SHAPE,size_h,size_bins,fol_h,fol_bins,transform='',outline_engine='homebrew'):
    '''
    given current state objects, proceeds a single frame with all steps
    (advances frames, finds movement, extends object arcs)
    '''

    #shift frames
    frames_offset += 1
    last_frames.append(frames[0])
    mm = vidtools.shift_frames_return_diff(stream,frames,currsum,denom,seglen,transform=transform)

    #find blobs
    if outline_engine == 'homebrew':
        ol = vidtools.chain_outlines_from_mask(mm>cutoff,preshrink=1,debug=False,return_termini=False,order_points=True,sort_outlines=False)
    elif outline_engine == 'shapely':
        ol = vidtools.chain_outlines_from_mask_shapely(mm>cutoff,preshrink=1)
    else:
        print >> sys.stderr, 'outline_engine must be one of %s' % (['homebrew','shapely'])
        raise ValueError

    
    #shift blob outlines
    ols_offset += 1
    ols.pop(0)
    ols.append(ol)
    
    #advance object_arc tracking
    start_frame = ols_offset + len(ols) - 1
    ntr_objs, ntr_objs_sizes, ntr_objs_fols = vidtools.find_objs_progressive(ols, ols_offset, start_frame, ols_offset+1, SHAPE, objs, splits, \
                                                                             objs_sizes, objs_fols, size_h,size_bins,fol_h,fol_bins)
    to_retire_objs.update(ntr_objs)
    to_retire_objs_sizes.update(ntr_objs_sizes)
    to_retire_objs_fols.update(ntr_objs_fols)
    
    return ols_offset,frames_offset
def init_objects(stream,frames,currsum,denom,seglen,cutoff,frames_offset,SHAPE,size_h,size_bins,fol_h,fol_bins,transform='',outline_engine='homebrew'):
    hsl = seglen/2
    min_arc_score = (2*max(size_h))+max(fol_h) #score of a "perfect" object arc of length 2
    ols = []
    last_frames = []
    ols_offset = frames_offset + hsl
    c = itertools.cycle(['|','/','-','\\'])
    t = time.time()
    print >> sys.stderr, 'analyze %s frames:' % seglen
    for i in xrange(seglen):
        last_frames.append(frames[0])
	frames_offset += 1
	mm = vidtools.shift_frames_return_diff(stream,frames,currsum,denom,seglen,transform=transform)
        if outline_engine == 'homebrew':
            ol = vidtools.chain_outlines_from_mask(mm>cutoff,preshrink=1,debug=False,return_termini=False,order_points=True,sort_outlines=False) #order_points should be True
        elif outline_engine == 'shapely':
            ol = vidtools.chain_outlines_from_mask_shapely(mm>cutoff,preshrink=1)
        else:
            print >> sys.stderr, 'outline_engine must be one of %s' % (['homebrew','shapely'])
            raise ValueError
	ols.append(ol)
	print >> sys.stderr, '\r%s %s ' % (i,c.next()),

    print >> sys.stderr, 'done in', str(datetime.timedelta(seconds=int(time.time() - t)))
    objs = {}
    splits = defaultdict(list)
    objs_sizes = {}
    objs_fols = {}
    
    print >> sys.stderr, '\nperform initial object arc tracking on %s frames' % len(ols)
    #one round of object tracking; assumes next object tracking call will start a single frame from now
    to_retire_objs, to_retire_objs_sizes, to_retire_objs_fols = vidtools.find_objs_progressive(ols, ols_offset, ols_offset, ols_offset+1, SHAPE, objs, splits, \
                                                                                               objs_sizes, objs_fols, size_h,size_bins,fol_h,fol_bins)

    prelast_avg = vidtools.average_frames(last_frames[hsl:])
    prelast_mm = vidtools.mousemask_from_object_arcs(frames_offset-hsl,frames_offset,min_arc_score,ols,ols_offset, \
                                                     Util.merge_dictlist([objs,to_retire_objs]), \
                                                     Util.merge_dictlist([objs_sizes,to_retire_objs_sizes]), \
                                                     Util.merge_dictlist([objs_fols,to_retire_objs_fols]), \
                                                     size_h, size_bins, fol_h, fol_bins,SHAPE) 

    print >> sys.stderr, 'object initialization complete in', str(datetime.timedelta(seconds=int(time.time() - t)))
    return ols,ols_offset,frames_offset,objs,splits,objs_sizes,objs_fols,prelast_avg,prelast_mm,to_retire_objs,to_retire_objs_sizes,to_retire_objs_fols
        if opts.antfarm_config: #antfarm-specific analysis steps
            digdiffs.append(prelast_masked-this_masked)
            if len(digdiffs) > 3:
                nll = digdiffs.pop(0)
            if newactols and newactols[-1]:
                prevactmask += reduce(lambda x,y:x+y, [vidtools.shrink_mask(vidtools.mask_from_outline(ol,this_masked.shape),1) for ol in newactols[-1]])
            if len(digdiffs) > 1:
                m = digdiffs[-2] > 0.2 #ARBITRARY
                m[last_mm] = True #add mousemask to burrow area
                m[vidtools.grow_mask(vidtools.shrink_mask(prevactmask,1),1)] = False #MASK PREVIOUS DIGGING
                m[groundmask] = False #AND EVERYTHING ABOVE FIRST GROUND
                if opts.outline_engine == 'homebrew':
                    newactols.append(vidtools.chain_outlines_from_mask(m,preshrink=1,grow_by=1,debug=False,return_termini=False,order_points=True,sort_outlines=False))
                    prevol = vidtools.chain_outlines_from_mask(prevactmask,preshrink=1,grow_by=1,debug=False,return_termini=False,order_points=True,sort_outlines=False)
                elif opts.outline_engine == 'shapely':
                    newactols.append(vidtools.chain_outlines_from_mask_shapely(m,preshrink=1,grow_by=1))
                    prevol = vidtools.chain_outlines_from_mask_shapely(prevactmask,preshrink=1,grow_by=1)
                else:
                    print >> sys.stderr, 'outline_engine must be one of %s' % (['homebrew','shapely'])
                    raise ValueError
                digol = newactols[-1]
            else:
                digol = []
                prevol = []

            groundtrack_masked = last_masked.copy()
            for former_mm in mousemasks[-opts.former_mm:]:
                groundtrack_masked[former_mm] = numpy.mean(last_avg[:50,:50])
            last_ground = new_ground(grounds[-1],groundtrack_masked,config['hill_bounds'],improvement=opts.ground_improvement,window=10) #ARBITRARY
            if len(digdiffs) > 1:
                ddmat = digdiffs[-2] #NOTE -2 INDEX; aligns with this activity segment
def advance_analysis(ols,
                     ols_offset,
                     objs,
                     splits,
                     objs_sizes,
                     objs_fols,
                     to_retire_objs,
                     to_retire_objs_sizes,
                     to_retire_objs_fols,
                     last_frames,
                     stream,
                     frames,
                     currsum,
                     denom,
                     seglen,
                     cutoff,
                     frames_offset,
                     SHAPE,
                     size_h,
                     size_bins,
                     fol_h,
                     fol_bins,
                     transform='',
                     outline_engine='homebrew'):
    '''
    given current state objects, proceeds a single frame with all steps
    (advances frames, finds movement, extends object arcs)
    '''

    #shift frames
    frames_offset += 1
    last_frames.append(frames[0])
    mm = vidtools.shift_frames_return_diff(stream,
                                           frames,
                                           currsum,
                                           denom,
                                           seglen,
                                           transform=transform)

    #find blobs
    if outline_engine == 'homebrew':
        ol = vidtools.chain_outlines_from_mask(mm > cutoff,
                                               preshrink=1,
                                               debug=False,
                                               return_termini=False,
                                               order_points=True,
                                               sort_outlines=False)
    elif outline_engine == 'shapely':
        ol = vidtools.chain_outlines_from_mask_shapely(mm > cutoff,
                                                       preshrink=1)
    else:
        print >> sys.stderr, 'outline_engine must be one of %s' % (
            ['homebrew', 'shapely'])
        raise ValueError

    #shift blob outlines
    ols_offset += 1
    ols.pop(0)
    ols.append(ol)

    #advance object_arc tracking
    start_frame = ols_offset + len(ols) - 1
    ntr_objs, ntr_objs_sizes, ntr_objs_fols = vidtools.find_objs_progressive(ols, ols_offset, start_frame, ols_offset+1, SHAPE, objs, splits, \
                                                                             objs_sizes, objs_fols, size_h,size_bins,fol_h,fol_bins)
    to_retire_objs.update(ntr_objs)
    to_retire_objs_sizes.update(ntr_objs_sizes)
    to_retire_objs_fols.update(ntr_objs_fols)

    return ols_offset, frames_offset
def init_objects(stream,
                 frames,
                 currsum,
                 denom,
                 seglen,
                 cutoff,
                 frames_offset,
                 SHAPE,
                 size_h,
                 size_bins,
                 fol_h,
                 fol_bins,
                 transform='',
                 outline_engine='homebrew'):
    hsl = seglen / 2
    min_arc_score = (2 * max(size_h)) + max(
        fol_h)  #score of a "perfect" object arc of length 2
    ols = []
    last_frames = []
    ols_offset = frames_offset + hsl
    c = itertools.cycle(['|', '/', '-', '\\'])
    t = time.time()
    print >> sys.stderr, 'analyze %s frames:' % seglen
    for i in xrange(seglen):
        last_frames.append(frames[0])
        frames_offset += 1
        mm = vidtools.shift_frames_return_diff(stream,
                                               frames,
                                               currsum,
                                               denom,
                                               seglen,
                                               transform=transform)
        if outline_engine == 'homebrew':
            ol = vidtools.chain_outlines_from_mask(
                mm > cutoff,
                preshrink=1,
                debug=False,
                return_termini=False,
                order_points=True,
                sort_outlines=False)  #order_points should be True
        elif outline_engine == 'shapely':
            ol = vidtools.chain_outlines_from_mask_shapely(mm > cutoff,
                                                           preshrink=1)
        else:
            print >> sys.stderr, 'outline_engine must be one of %s' % (
                ['homebrew', 'shapely'])
            raise ValueError
        ols.append(ol)
        print >> sys.stderr, '\r%s %s ' % (i, c.next()),

    print >> sys.stderr, 'done in', str(
        datetime.timedelta(seconds=int(time.time() - t)))
    objs = {}
    splits = defaultdict(list)
    objs_sizes = {}
    objs_fols = {}

    print >> sys.stderr, '\nperform initial object arc tracking on %s frames' % len(
        ols)
    #one round of object tracking; assumes next object tracking call will start a single frame from now
    to_retire_objs, to_retire_objs_sizes, to_retire_objs_fols = vidtools.find_objs_progressive(ols, ols_offset, ols_offset, ols_offset+1, SHAPE, objs, splits, \
                                                                                               objs_sizes, objs_fols, size_h,size_bins,fol_h,fol_bins)

    prelast_avg = vidtools.average_frames(last_frames[hsl:])
    prelast_mm = vidtools.mousemask_from_object_arcs(frames_offset-hsl,frames_offset,min_arc_score,ols,ols_offset, \
                                                     Util.merge_dictlist([objs,to_retire_objs]), \
                                                     Util.merge_dictlist([objs_sizes,to_retire_objs_sizes]), \
                                                     Util.merge_dictlist([objs_fols,to_retire_objs_fols]), \
                                                     size_h, size_bins, fol_h, fol_bins,SHAPE)

    print >> sys.stderr, 'object initialization complete in', str(
        datetime.timedelta(seconds=int(time.time() - t)))
    return ols, ols_offset, frames_offset, objs, splits, objs_sizes, objs_fols, prelast_avg, prelast_mm, to_retire_objs, to_retire_objs_sizes, to_retire_objs_fols
                                                          debug=False,
                                                          return_termini=False,
                                                          order_points=True,
                                                          sort_outlines=False))
                    prevol = vidtools.chain_outlines_from_mask(
                        prevactmask,
                        preshrink=1,
                        grow_by=1,
                        debug=False,
                        return_termini=False,
                        order_points=True,
                        sort_outlines=False)
                elif opts.outline_engine == 'shapely':
                    newactols.append(
                        vidtools.chain_outlines_from_mask_shapely(m,
                                                                  preshrink=1,
                                                                  grow_by=1))
                    prevol = vidtools.chain_outlines_from_mask_shapely(
                        prevactmask, preshrink=1, grow_by=1)
                else:
                    print >> sys.stderr, 'outline_engine must be one of %s' % (
                        ['homebrew', 'shapely'])
                    raise ValueError
                digol = newactols[-1]
            else:
                digol = []
                prevol = []

            groundtrack_masked = last_masked.copy()
            for former_mm in mousemasks[-opts.former_mm:]:
                groundtrack_masked[former_mm] = numpy.mean(last_avg[:50, :50])