def ecrireTraining(ensembleTraj, fileToSave, LENGTH):
    solutions = {}
    fileS=open(fileToSave, "w")
    fileSHDF5 = fileToSave[:-3]+"hdf5"

    going = {}
    coming = {}
    appearCandidates = {}
    disappearCandidates = {}

    for trajectoire in ensembleTraj.lstTraj:
        frameLabels = sorted(trajectoire.lstPoints.iterkeys())
        lastLabels = []
        lastFrame = -1
        lastMoveSplit = [False for x in range(100)]

        for f in frameLabels:
            frame = int(f[0])
            nextFrame = frame+1

            if nextFrame==LENGTH:
                break

            if lastLabels == []:
                lastLabels=trajectoire.findFrame(frame)

            if frame == lastFrame:
                continue

            nextLabels = trajectoire.findFrame(nextFrame)

            if not frame in solutions:
                solutions[frame]={}

            l = len(lastLabels)
            nextL = len(nextLabels)
            #print "FRAME :"+str(frame),l, nextL, lastLabels, nextLabels, trajectoire.numCellule


            if int(l)==int(nextL):
                if int(l)==1 and int(lastLabels[0])==-1:
                    appearing = nextLabels[0]
                    if not nextFrame in appearCandidates:
                        appearCandidates[nextFrame]=[]
                    appearCandidates[nextFrame].append(appearing)

                elif int(l)==1 and int(nextLabels[0])==-2:
                    disappearing = lastLabels[0]
                    if not frame in disappearCandidates:
                        disappearCandidates[frame]=[]
                    disappearCandidates[frame].append(disappearing)
                    #continue

                elif int(l)==1:
                    #print "MOVE"
                                    
                    if not "move" in solutions[frame]:
                        solutions[frame]["move"]=[[], []]

                    solutions[frame]["move"][0].append(lastLabels)
                    solutions[frame]["move"][1].append(nextLabels)
                    if not nextFrame in coming:
                        coming[nextFrame]=[]
                    for label in nextLabels: 
                        coming[nextFrame].append(label)
                        
                    if not frame in going:
                        going[frame]=[]
                    for label in lastLabels:
                        going[frame].append(label)

                else:
                    print "problem", lastLabels, nextLabels, frame
                    raise

            else:
                print "problem at frame "+str(frame), lastLabels, nextLabels
                raise

            lastFrame = frame
            if not lastMoveSplit[frame]:
                 lastLabels = nextLabels
            if int(lastLabels[0])==-2:
                break
            
#looking at merges : if two cells have the same target, it means it is not a move but a merge

    for f in solutions:
        if "move" not in solutions[f]:
            continue
        
        listeLabelsTargetMove = solutions[f]["move"][1]
        listeLabelsSourceMove = solutions[f]["move"][0]
        nextFrame = int(f+1)
        #print listeLabelsSourceMove, listeLabelsTargetMove
        
        newSourceMove = []
        newTargetMove = []
        
        merges = []
        moves = []
        for t_el in listeLabelsTargetMove:
            if listeLabelsTargetMove.count(t_el) > 1:
                merges.append(t_el)
            else:
                moves.append(t_el)

        indexToDel = []
        for label1 in merges:
          #  print label1
            nextLabels=label1
            lastLabels=[]
            for i in range(len(listeLabelsTargetMove)):
                if listeLabelsTargetMove[i]==label1:
                    lastLabels.append(listeLabelsSourceMove[i][0])
                    indexToDel.append(i)
#                    

            if not "merge" in solutions[f]:
                solutions[f]["merge"]=[[], []]
            if nextLabels in solutions[f]["merge"][1]:
                continue
            solutions[f]["merge"][0].append(lastLabels)
            solutions[f]["merge"][1].append(nextLabels)

            if not f in going:
                going[f]=[]
            if not nextFrame in coming:
                coming[nextFrame]=[]
            for label in nextLabels:
                coming[nextFrame].append(label)
            for label in lastLabels:
                going[f].append(label)
        
#looking at splits : if two cells have the same source, it means it is not a move but a split
        
        splits = []
        moves = []
        for s_el in listeLabelsSourceMove:
            if listeLabelsSourceMove.count(s_el) > 1:
                splits.append(s_el)
            else:
                moves.append(s_el)
        
#        indexToDel = []
        for label1 in splits:
            #print label1
            lastLabels=label1
            nextLabels=[]
            for i in range(len(listeLabelsSourceMove)):
                if listeLabelsSourceMove[i]==label1:
                    indexToDel.append(i)
                    nextLabels.append(listeLabelsTargetMove[i][0])
#                    
            if not "split" in solutions[f]:
                solutions[f]["split"]=[[], []]
            if lastLabels in solutions[f]["split"][0]:
                continue
            solutions[f]["split"][0].append(lastLabels)
            solutions[f]["split"][1].append(nextLabels)

            if not f in going:
                going[f]=[]
            if not nextFrame in coming:
                coming[nextFrame]=[]
            for label in nextLabels:
                coming[nextFrame].append(label)
            for label in lastLabels:
                going[f].append(label)
                
        for i in range(len(listeLabelsSourceMove)):
            if i not in indexToDel:
                newSourceMove.append(listeLabelsSourceMove[i])
                newTargetMove.append(listeLabelsTargetMove[i])
 #       print listeLabelsSourceMove, listeLabelsTargetMove
        #print newSourceMove, newTargetMove
        solutions[f]["move"][1] = newTargetMove
        solutions[f]["move"][0] = newSourceMove
        
                                           

#appear candidates 
    for frame in appearCandidates.keys():
        appearlist = appearCandidates[frame]
        for label in appearlist:
            if frame not in coming.keys() or (frame in coming.keys() and label not in coming[frame]):
                print "APPEAR "+str(label)+" on frame "+str(frame)
                if not "appear" in solutions[frame-1]:
                    solutions[frame-1]["appear"]=[[], []]

                solutions[frame-1]["appear"][0].append([-1])
                solutions[frame-1]["appear"][1].append(label)

#disappear candidates
    for frame in disappearCandidates.keys():
        disappearlist = disappearCandidates[frame]
        for label in disappearlist:
            if frame not in going.keys() or (frame in going.keys() and label not in going[frame]):
                print "DISAPPEAR "+str(label)+" on frame "+str(frame)

                if not "disappear" in solutions[frame]:
                    solutions[frame]["disappear"]=[[], []]

                solutions[frame]["disappear"][0].append(label)
                solutions[frame]["disappear"][1].append([])

    count={}
    out_merge = "\n MERGE \n"
    out_split = "\n SPLIT \n"
    for e in EVENTS:
        count[e]=0

    for f in solutions:
        fileS.write("\n --------------------------FRAME "+str(f)+"------------------------------------")
        for e in solutions[f]:
            s = len(solutions[f][e][0])
#attention a l'ordre des axes : ici cx=nombre d'evenements, "taille" de l'evenement marche bien (de toutes facons lorsqu'on appelle la fonction writeHDF5 elle retablit l'ordre numpy)
            shapeS = (s,EVENTSSIZE[e][0],)
            shapeT = (s,EVENTSSIZE[e][1],)

            tabSource = v.VigraArray(shapeS,n.int32, axistags = v.VigraArray.defaultAxistags('cx'), init=True)
            tabTarget = v.VigraArray(shapeT,n.int32, axistags = v.VigraArray.defaultAxistags('cx'), init=True)
            tabSource = tabSource-1
            tabTarget = tabTarget-1
            path = "Training/{:0>6}/{}/".format(f, e)
            pathSource = path+"Source"
            pathTarget = path+"Target"
            
            fileS.write("\n EVENT "+str(e)+"*******\n SOURCES :\n")
            j=0
            for label in solutions[f][e][0]:
                if isinstance(label, int) or isinstance(label, n.uint16) :
                    length = 1
                else:
                    length=int(len(label))
                fileS.write("\n label"+str(label))

                if label == []:
                    continue

                diff = EVENTSSIZE[e][0] - length
                if diff==0:
                    tabSource[j]=label
                else:
                    while diff>0:
                        try:
                            label.append(-1)
                        except AttributeError:
                            print "evenement "+str(e)+" probleme de taille entre evenement"+str(EVENTSSIZE[e][0])+" et le training set "+str(length)
                        else:
                            diff-=1
                            
                    tabSource[j]=label

#                if e == "split":print e, f, label, j, tabSource
                j+=1
                count[e]+=1
            i=0
            fileS.write("\n TARGETS :\n")
            for label in solutions[f][e][1]:
                if isinstance(label, int) or isinstance(label, n.uint16) :
                    length = 1
                else:
                    length=int(len(label))
                fileS.write("\n label"+str(label))
                
                diff = EVENTSSIZE[e][1] - length
                if diff==0:
                    tabTarget[i]=label
                else:
                    while diff>0:
                        try:
                            label.append(-1)
                        except AttributeError:
                            print "evenement "+str(e)+" probleme de taille entre evenement"+str(EVENTSSIZE[e][1])+" et le training set "+str(length)
                        else:
                            diff-=1
                    tabTarget[i]=label

                i+=1

            if i<>j: print "probleme : difference de longueurs entre Source et Target at fr "+str(f)+" pour l'evenement "+e

            vi.writeHDF5(tabSource, fileSHDF5, pathSource)
            vi.writeHDF5(tabTarget, fileSHDF5, pathTarget)
            
            if e =="merge":
                out_merge +="\n"+str(solutions[f][e][0])+" on frame "+str(f)+" to "+str(solutions[f][e][1])
            if e=="split":
                out_split +="\n"+str(solutions[f][e][0])+" on frame "+str(f)+" to "+str(solutions[f][e][1])
    print count, out_merge, out_split
    fileS.close()
    return solutions
def ecrireTrainingSansHDF5(ensembleTraj, fileToSave, LENGTH):
    solutions = {}
    #fileS=open(fileToSave, "w")

    going = {}
    coming = {}
    appearCandidates = {}
    disappearCandidates = {}

    for trajectoire in ensembleTraj.lstTraj:
        frameLabels = sorted(trajectoire.lstPoints.iterkeys())
        lastLabels = []
        lastFrame = -1
        lastMoveSplit = [False for x in range(100)]

        for f in frameLabels:
            frame = int(f[0])
            nextFrame = frame+1

            if nextFrame==LENGTH:
                break

            if lastLabels == []:
                lastLabels=trajectoire.findFrame(frame)

            if frame == lastFrame:
                continue

            nextLabels = trajectoire.findFrame(nextFrame)

            if not frame in solutions:
                solutions[frame]={}

            l = len(lastLabels)
            nextL = len(nextLabels)
            #print "FRAME :"+str(frame),l, nextL, lastLabels, nextLabels, trajectoire.numCellule


            if int(l)==int(nextL):
                if int(l)==1 and int(lastLabels[0])==-1:
                    appearing = nextLabels[0]
                    if not nextFrame in appearCandidates:
                        appearCandidates[nextFrame]=[]
                    appearCandidates[nextFrame].append(appearing)

                elif int(l)==1 and int(nextLabels[0])==-2:
                    disappearing = lastLabels[0]
                    if not frame in disappearCandidates:
                        disappearCandidates[frame]=[]
                    disappearCandidates[frame].append(disappearing)
                    #continue

                elif int(l)==1:
                    #print "MOVE"
                                    
                    if not "move" in solutions[frame]:
                        solutions[frame]["move"]=[[], []]

                    solutions[frame]["move"][0].append(lastLabels)
                    solutions[frame]["move"][1].append(nextLabels)
                    if not nextFrame in coming:
                        coming[nextFrame]=[]
                    for label in nextLabels: 
                        coming[nextFrame].append(label)
                        
                    if not frame in going:
                        going[frame]=[]
                    for label in lastLabels:
                        going[frame].append(label)

                else:
                    print "problem", lastLabels, nextLabels, frame
                    raise

            else:
                print "problem at frame "+str(frame), lastLabels, nextLabels
                raise

            lastFrame = frame
            if not lastMoveSplit[frame]:
                 lastLabels = nextLabels
            if int(lastLabels[0])==-2:
                break
            
#looking at merges : if two cells have the same target, it means it is not a move but a merge

    for f in solutions:
        if "move" not in solutions[f]:
            continue
        
        listeLabelsTargetMove = solutions[f]["move"][1]
        listeLabelsSourceMove = solutions[f]["move"][0]
        nextFrame = int(f+1)
         #print listeLabelsSourceMove, listeLabelsTargetMove
        
        newSourceMove = []
        newTargetMove = []
        
        merges = []
        moves = []
        for t_el in listeLabelsTargetMove:
            if listeLabelsTargetMove.count(t_el) > 1:
                merges.append(t_el)
            else:
                moves.append(t_el)

        indexToDel = []
        for label1 in merges:
            #print label1
            nextLabels=label1
            lastLabels=[]
            for i in range(len(listeLabelsTargetMove)):
                if listeLabelsTargetMove[i]==label1:
                    lastLabels.append(listeLabelsSourceMove[i][0])
                    indexToDel.append(i)
#                    
            for l in lastLabels:
                if lastLabels.count(l)>1:
                    print f, l
                    raise SameLabelException

            if not "merge" in solutions[f]:
                solutions[f]["merge"]=[[], []]
            if nextLabels in solutions[f]["merge"][1]:
                continue
            solutions[f]["merge"][0].append(lastLabels)
            solutions[f]["merge"][1].append(nextLabels)

            if not f in going:
                going[f]=[]
            if not nextFrame in coming:
                coming[nextFrame]=[]
            for label in nextLabels:
                coming[nextFrame].append(label)
            for label in lastLabels:
                going[f].append(label)
        
#looking at splits : if two cells have the same source, it means it is not a move but a split
        
        splits = []
        moves = []
        for s_el in listeLabelsSourceMove:
            if listeLabelsSourceMove.count(s_el) > 1:
                splits.append(s_el)
            else:
                moves.append(s_el)
        
#        indexToDel = []
        for label1 in splits:
            #print label1
            lastLabels=label1
            nextLabels=[]
            for i in range(len(listeLabelsSourceMove)):
                if listeLabelsSourceMove[i]==label1:
                    indexToDel.append(i)
                    nextLabels.append(listeLabelsTargetMove[i][0])
                    
            for l in nextLabels:
                if nextLabels.count(l)>1:
                    print f, l
                    raise SameLabelException
#                    
            if not "split" in solutions[f]:
                solutions[f]["split"]=[[], []]
            if lastLabels in solutions[f]["split"][0]:
                continue
#            if maxTwos and len(nextLabels)>2:
#                continue
            solutions[f]["split"][0].append(lastLabels)
            solutions[f]["split"][1].append(nextLabels)

            if not f in going:
                going[f]=[]
            if not nextFrame in coming:
                coming[nextFrame]=[]
            for label in nextLabels:
                coming[nextFrame].append(label)
            for label in lastLabels:
                going[f].append(label)
                
        for i in range(len(listeLabelsSourceMove)):
            if i not in indexToDel:
                newSourceMove.append(listeLabelsSourceMove[i])
                newTargetMove.append(listeLabelsTargetMove[i])
 #       print listeLabelsSourceMove, listeLabelsTargetMove
        #print newSourceMove, newTargetMove
        solutions[f]["move"][1] = newTargetMove
        solutions[f]["move"][0] = newSourceMove
        
 
#appear candidates 
    for frame in appearCandidates.keys():
        appearlist = appearCandidates[frame]
        for label in appearlist:
            if frame not in coming.keys() or (frame in coming.keys() and label not in coming[frame]):
                if not "appear" in solutions[frame-1]:
                    solutions[frame-1]["appear"]=[[], []]
                if [label] in solutions[frame-1]["appear"][1]:
                    continue
                #print "APPEAR "+str(label)+" on frame "+str(frame)
                solutions[frame-1]["appear"][0].append([-1])
                solutions[frame-1]["appear"][1].append([label])

#disappear candidates
    for frame in disappearCandidates.keys():
        disappearlist = disappearCandidates[frame]
        for label in disappearlist:
            if frame not in going.keys() or (frame in going.keys() and label not in going[frame]):
                if not "disappear" in solutions[frame]:
                    solutions[frame]["disappear"]=[[], []]
                if [label] in solutions[frame]["disappear"][0]:
                    continue
                #print "DISAPPEAR "+str(label)+" on frame "+str(frame)
                solutions[frame]["disappear"][0].append([label])
                solutions[frame]["disappear"][1].append([-1])

    count={}
    out_merge = "\n MERGE \n"
    out_split = "\n SPLIT \n"
    for e in EVENTS:
        count[e]=0

    for f in solutions:
        #fileS.write("\n --------------------------FRAME "+str(f)+"------------------------------------")
        for e in solutions[f]:
         #   fileS.write("\n EVENT "+str(e)+"*******\n SOURCES :\n")
            j=0
            for label in solutions[f][e][0]:
          #      fileS.write("\n label"+str(label))
                if label == []:
                    continue
                j+=1
                count[e]+=1
            i=0
           # fileS.write("\n TARGETS :\n")
            for label in solutions[f][e][1]:
            #    fileS.write("\n label"+str(label))
                i+=1

            if i<>j: print "probleme : difference de longueurs entre Source et Target at fr "+str(f)+" pour l'evenement "+e

            if e =="merge":
                out_merge +="\n"+str(solutions[f][e][0])+" on frame "+str(f)+" to "+str(solutions[f][e][1])
            if e=="split":
                out_split +="\n"+str(solutions[f][e][0])+" on frame "+str(f)+" to "+str(solutions[f][e][1])
    #fileS.write(str(count))
    print count#, out_merge, out_split
    #fileS.close()
    return solutions
def ecrireTraining(ensembleTraj, fileToSave):
    solutions = {}
#    solutions[0]={}
    fileS=open(fileToSave, "w")
    going = {}
    coming = {}
    appearCandidates = {}
    disappearCandidates = {}

    for trajectoire in ensembleTraj.lstTraj:
        frameLabels = sorted(trajectoire.lstPoints.iterkeys())
        lastLabels = []
        lastFrame = -1
        lastMoveSplit = [False for x in range(100)]

        for f in frameLabels:
            frame = int(f[0])
            nextFrame = frame+1

            if nextFrame==LENGTH:
                #if not nextFrame in coming:
                 #   coming[nextFrame]=[]
                # for label in lastLabels: 
                 #   coming[nextFrame].append(label)
                break

            if lastLabels == []:
                lastLabels=trajectoire.findFrame(frame)

            if frame == lastFrame:
                continue

 #           if frame > lastFrame+1:
  #             appearCandidates[lastLabels[0]]=frame

            nextLabels = trajectoire.findFrame(nextFrame)

            if not frame in solutions:
                solutions[frame]={}

            l = len(lastLabels)
            nextL = len(nextLabels)
            #print "FRAME :"+str(frame),l, nextL, lastLabels, nextLabels, trajectoire.numCellule


            if int(l)==int(nextL):
                if int(l)==1 and int(lastLabels[0])==-1:
                    appearing = nextLabels[0]
                    if not nextFrame in appearCandidates:
                        appearCandidates[nextFrame]=[]
                    appearCandidates[nextFrame].append(appearing)

                elif int(l)==1 and int(nextLabels[0])==-2:
                    disappearing = lastLabels[0]
                    if not frame in disappearCandidates:
                        disappearCandidates[frame]=[]
                    disappearCandidates[frame].append(disappearing)
                    #continue

                elif int(l)==1:
                    #print "MOVE"
                #lastMoveSplit = False
                
                    if not "move" in solutions[frame]:
                        solutions[frame]["move"]=[[], []]

                    solutions[frame]["move"][0].append(lastLabels)
                    solutions[frame]["move"][1].append(nextLabels)
                    if not nextFrame in coming:
                        coming[nextFrame]=[]
                    for label in nextLabels: 
                        coming[nextFrame].append(label)
                        
                    if not frame in going:
                        going[frame]=[]
                    for label in lastLabels:
                        going[frame].append(label)

                else:
                    print "problem", lastLabels, nextLabels, frame
                    raise

            else:
#             if int(nextL)==0:
                print "problem at frame "+str(frame), lastLabels, nextLabels
                raise

#                elif  int(nextL)>=2:
#                    lastMoveSplit[nextFrame] = True
#                    #print "SPLIT", lastMoveSplit[nextFrame]
#
#                    if not "split" in solutions[frame]:
#                        solutions[frame]["split"]=[[], []]
#
#                    solutions[frame]["split"][0].append(lastLabels)
#                    solutions[frame]["split"][1].append(nextLabels)
#                    if not nextFrame in coming:
#                        coming[nextFrame]=[]
#                    for label in nextLabels: 
#                        coming[nextFrame].append(label)
#                    if not frame in going:
#                        going[frame]=[]
#                    for label in lastLabels:
#                        going[frame].append(label)
#
#                else:
#                   print "one cell PBL at t="+str(frame)
#                  raise

#            elif int(l)>=2:
#                if int(nextL)==1:
#                    print "FRAME :"+str(frame), lastLabels, nextLabels, trajectoire.numCellule, lastMoveSplit[frame]
#                    if lastMoveSplit[frame]:
#                        xmin = 100
#                        ymin = 100
#                        labelmin = 0
#                        xf = trajectoire.lstPoints[(nextFrame, nextLabels[0])][0]
#                        yf = trajectoire.lstPoints[(nextFrame, nextLabels[0])][1]
#                        for label in lastLabels:
#                            x = abs(int(xf) - int(trajectoire.lstPoints[(frame, label)][0]))
#                            y = abs(int(yf) - int(trajectoire.lstPoints[(frame, label)][1]))
#                            if int(x)<int(xmin) and int(y)<int(ymin):
#                                labelmin = label
#                                xmin = x
#                                ymin = y
#
#                        if labelmin == 0:
#                            print "lastMoveSplit recognition problem at t="+str(frame)
#                            raise
#
#                        #print "MOVE"
#                        # lastMoveSplit = False
#                        if not "move" in solutions[frame]:
#                            solutions[frame]["move"]=[[], []]
#
#                        lastLabels = [labelmin]
#                        solutions[frame]["move"][0].append(lastLabels)
#                        solutions[frame]["move"][1].append(nextLabels)
#                       
#                        if not nextFrame in coming:
#                            coming[nextFrame]=[]
#                        for label in nextLabels: 
#                            coming[nextFrame].append(label)
#                        if not frame in going:
#                            going[frame]=[]
#                        for label in lastLabels:
#                            going[frame].append(label)
#
#                    else:
#                        print "problem"
#                        raise
#                        
                       # if not "merge" in solutions[frame]:
                        #    solutions[frame]["merge"]=[[], []]

                       # solutions[frame]["merge"][0].append(lastLabels)
                        #solutions[frame]["merge"][1].append(nextLabels)
   #                     if not frame in mergedLabels:
    #                        mergedLabels[frame]=[]
     #                   if not nextFrame in mergedLabels:
      #                      mergedLabels[nextFrame]=[]
       #                 for label in nextLabels:
        #                    mergedLabels[nextFrame].append(label)
         #               for label in lastLabels:
          #                  mergedLabels[frame].append(label)
                        #lastMoveSplit =False
#                else:
#                    print "two cells PBL at t="+str(frame)
#                    raise
#
#            elif int(l)==0:
#                print "problem at frame "+str(frame)
#                raise
#            elif int(nextL)==0:
#                print "last pbl at frame "+str(frame), lastLabels, nextLabels
#                raise
#
            lastFrame = frame
            if not lastMoveSplit[frame]:
                 lastLabels = nextLabels
            if int(lastLabels[0])==-2:
                break

    for f in solutions:
        if "move" not in solutions[f]:
            continue
        listeLabelsTargetMove = solutions[f]["move"][1]
        listeLabelsSourceMove = solutions[f]["move"][0]
        nextFrame = int(f+1)

        for label1 in listeLabelsTargetMove:
            label1=label1[0]
            count = 0
            for label2 in listeLabelsTargetMove:
                label2=label2[0]
                if int(label1)==int(label2):
                    count+=1

            if count>1:
                #print "label1 "+str(label1), count, f
                #print "MERGE ", label1, f
                lastLabels=[]
                nextLabels=[label1]

                while [label1] in listeLabelsTargetMove:
                    index = listeLabelsTargetMove.index([label1])
                    #print index, label1, listeLabelsSourceMove[index]
                    lastLabels.append(listeLabelsSourceMove[index][0])
                    
                    del listeLabelsTargetMove[index]
                    del listeLabelsSourceMove[index]
                    

                if not "merge" in solutions[f]:
                    solutions[f]["merge"]=[[], []]

                solutions[f]["merge"][0].append(lastLabels)
                solutions[f]["merge"][1].append(nextLabels)

                if not f in going:
                    going[f]=[]
                if not nextFrame in coming:
                    coming[nextFrame]=[]
                for label in nextLabels:
                    coming[nextFrame].append(label)
                for label in lastLabels:
                    going[f].append(label)

        for label1 in listeLabelsSourceMove:
            label1=label1[0]
            count = 0
            for label2 in listeLabelsSourceMove:
                label2=label2[0]
                if int(label1)==int(label2):
                    count+=1

            if count>1:
                #print "label1 "+str(label1), count, f
                #print "SPLIT ", label1, f
                lastLabels=[label1]
                nextLabels=[]

                while [label1] in listeLabelsSourceMove:
                    index = listeLabelsSourceMove.index([label1])
                    #print index, label1, listeLabelsSourceMove[index]
                    nextLabels.append(listeLabelsTargetMove[index][0])
                    
                    del listeLabelsTargetMove[index]
                    del listeLabelsSourceMove[index]
                    

                if not "split" in solutions[f]:
                    solutions[f]["split"]=[[], []]

                solutions[f]["split"][0].append(lastLabels)
                solutions[f]["split"][1].append(nextLabels)

                if not f in going:
                    going[f]=[]
                if not nextFrame in coming:
                    coming[nextFrame]=[]
                for label in nextLabels:
                    coming[nextFrame].append(label)
                for label in lastLabels:
                    going[f].append(label)
                                           

#appear candidates 
    for frame in appearCandidates.keys():
        appearlist = appearCandidates[frame]
        for label in appearlist:
            if frame not in coming.keys() or (frame in coming.keys() and label not in coming[frame]):
                print "APPEAR "+str(label)+" on frame "+str(frame)
                if not "appear" in solutions[frame-1]:
                    solutions[frame-1]["appear"]=[[], []]

                solutions[frame-1]["appear"][0].append([])
                solutions[frame-1]["appear"][1].append(label)

#disappear candidates
    for frame in disappearCandidates.keys():
        disappearlist = disappearCandidates[frame]
        for label in disappearlist:
            if frame not in going.keys() or (frame in going.keys() and label not in going[frame]):
                print "DISAPPEAR "+str(label)+" on frame "+str(frame)

                if not "disappear" in solutions[frame]:
                    solutions[frame]["disappear"]=[[], []]

                solutions[frame]["disappear"][0].append(label)
                solutions[frame]["disappear"][1].append([])

    count={}
    for e in EVENTS:
        count[e]=0

    for f in solutions:
        fileS.write("\n --------------------------FRAME "+str(f)+"------------------------------------")
        for e in solutions[f]:
            fileS.write("\n EVENT "+str(e)+"*******\n SOURCES :\n")
            for label in solutions[f][e][0]:
                fileS.write("\n label"+str(label))
                count[e]+=1
            fileS.write("\n TARGETS :\n")
            for label in solutions[f][e][1]:
                fileS.write("\n label"+str(label))
            if e <> "move":
                print e+str(solutions[f][e][0])+" on frame "+str(f)+" to "+str(solutions[f][e][1])
    print count
    fileS.close()
    return