def compFeatures(data, length, who, ctrlStatus, percentile, *args):
    how_many=5
    outputFolder='../prediction/annotations'
    plwList=defaultdict(int)
    whoList={}
    extractors=[]
    for feature_name in args:
        extractor = cellExtractor(feature_name)
        extractors.append(extractor)
        bestMovies = extractor.dataToBestMovies(data, length, who, ctrlStatus, percentile, how_many=how_many)
        #plwList.extend(bestMovies.keys())
        whoList[feature_name]=bestMovies
        for el in bestMovies:
            plwList[el]+=len(bestMovies[el])
    plwList={el:plwList[el] for el in filter(lambda x: np.all([x in whoList[feature_name] for feature_name in args]), plwList.keys())}
    sort= sorted(plwList, key=operator.itemgetter(1))
    chosen=sort[-how_many:]
    num=1
    for el in chosen:
        plate, well=el
        filename = "PL{}___P{}___T00000.xml".format(plate, well)
        file_ = open(os.path.join(outputFolder, filename), 'w')
        file_.write(initXml())
        for i,feature_name in enumerate(args):
            resultCour = whoList[feature_name][el]
            print len(resultCour)#donc attention ici on n'a que les index des trajectoires dans le fichier coord
            file_.write(extractors[i].wellToXml(length,who, plate, well, resultCour, outputFolder, num)) # _writeXml(plate, well, resultCour, num))
            num+=1
        file_.write(finirXml())
        file_.close()
    return chosen
 def writeTracks(self):
     '''
     To produce annotated tracklets, either all tracklets (option all_=True) or only those used for feature extraction (option all=False)
     '''
     intensity_qc_dict = self._getIntensityQC()
     
     if 'tracking_annotations' not in os.listdir(self.settings.outputFolder):
         os.mkdir(os.path.join(self.settings.outputFolder, 'tracking_annotations'))
     if 'annotations' not in os.listdir(os.path.join(self.settings.outputFolder, 'tracking_annotations')):
         os.mkdir(os.path.join(os.path.join(self.settings.outputFolder, 'tracking_annotations', 'annotations')))
     
     if self.settings.all_trajectory_xml:
         well_files = filter(lambda x: self.settings.traj_filename.split('{}')[0] in x, os.listdir(os.path.join(self.settings.outputFolder, self.plate)))
     else:
         well_files = filter(lambda x: self.settings.feature_filename.split('{}')[0] in x, os.listdir(os.path.join(self.settings.outputFolder, self.plate)))
     for file_ in well_files:
         well_num=int(file_.split('_')[2][1:])
         well = '{:>05}_01'.format(well_num)
         
         nomFichier = "PL"+self.plate+"___P"+well+"___T00001.xml"
         nomFichier = os.path.join(self.settings.outputFolder,'tracking_annotations', 'annotations', nomFichier)
 
         if self.settings.all_trajectory_xml:
             f=open(os.path.join(self.settings.outputFolder, self.plate, file_), 'r')
             d=pickle.load(f); f.close()
             ensTraj = d['tracklets dictionary'][self.plate][well]
             _ = sortiesAll(nomFichier, ensTraj)
         else:
             f=open(os.path.join(self.settings.outputFolder, self.plate, file_), 'r')
             _,coord,_=pickle.load(f); f.close()
             
             compteur =1
             fichierX = open(nomFichier, "w")
             fichierX.write(initXml())
             if intensity_qc_dict is not None and well_num in intensity_qc_dict:
                 frames_to_skip = intensity_qc_dict[well_num]
             else:
                 frames_to_skip=None
 
             for lstPoints in coord:
                 frameL=lstPoints[0]; X=lstPoints[1]; Y=lstPoints[2]
                 d={(frameL[k]+len(np.where(frames_to_skip<=frameL[k])[0]),0):(X[k], Y[k]) for k in range(len(frameL))}
                 txt, coord = ecrireXml(compteur,d, True)
                 fichierX.write(txt)
                 compteur+=1
                 if compteur>1600:
                     break
         
             fichierX.write(finirXml())
             fichierX.close()
             print 'finally ', compteur, 'trajectories'
         
     return
Ejemplo n.º 3
0
def findingSharpMovements(setting_file='bgeig/settings/settings_bgeig.py', measure='turningangle', 
                          speed_filter=10, nocontact=True):
    settings=Settings(setting_file, globals())
    
    f=open(settings.dict_corresp_nuclei_cyto)
    dict_corresp=pickle.load(f); f.close()
    
    plates=os.listdir(settings.dataFolder)
    sharp={}; contact={}
    
    for plate in plates:
        print '-----', plate, '<br>'
        wells=['{:>05}_01'.format(k) for k in range(21, 25)]
        sharp[plate]={}; contact[plate]={}
        
        for well in wells:
            print well, '<br>'
            sharp[plate][well]=defaultdict(list)
            contact[plate][well]=defaultdict(list)
            
            contact_count=0; not_contact=0
            
            f=open(os.path.join(settings.outputFolder, plate, settings.feature_filename.format(well)))
            _, coord, hist, id_list=pickle.load(f); f.close()
            
            nomFichier = "PL"+plate+"___P"+well+"___T00001.xml"
            nomFichier = os.path.join(settings.outputFolder,'sharp_mov', 'annotations', nomFichier)
            compteur =1
            fichierX = open(nomFichier, "w")
            fichierX.write(initXml())
            
            for i,el in enumerate(hist[measure]):
                if np.any(np.array(el)>0.70):
                    wh_=np.where((np.array(el)>0.70)&(np.array(hist['displacements'][i][:-1])>speed_filter))[0]
                    if wh_.shape[0]==0:
                        continue
                    d={}
                    id_=id_list[i]
                    currcoord=np.array(zip(*coord[i]))
                    sharp[plate][well][id_].append(currcoord[0][0][0])
                    
                    assert(len(el)==len(currcoord)-2)
                    
                    for index in range(len(el)):
                        el=currcoord[index]
                        im, cell_id=el[0]
                        corresp_cytoplasm = dict_corresp[plate][well][im][cell_id]
                        if Counter(dict_corresp[plate][well][im].values())[corresp_cytoplasm]>1:
                            contact[plate][well][id_].append(im)
                            
                        if index in wh_:                        
                            sharp[plate][well][id_].append(im)
                            
                            if nocontact:
                                try:
                                    previous_im, previous_id = currcoord[index-1][0]
                                except IndexError:
                                    previous_im=im
                                    previous_id=cell_id
                                try:
                                    next_im, next_id = currcoord[index+1][0]
                                except IndexError:
                                    next_im=im
                                    next_id=cell_id
                                
                                previous_cytoplasm = dict_corresp[plate][well][previous_im][previous_id]
                                next_cytoplasm=dict_corresp[plate][well][next_im][next_id]
                                
                                test = Counter(dict_corresp[plate][well][im].values())[corresp_cytoplasm]==1\
                                and Counter(dict_corresp[plate][well][previous_im].values())[previous_cytoplasm]==1\
                                and Counter(dict_corresp[plate][well][next_im].values())[next_cytoplasm]==1
                            else:
                                test=True
                            
                            if test:
                                d[tuple(el[0])]=(el[1], el[2])
                                not_contact+=1
                            else:
                                contact_count+=1
                            
                    txt, _ = ecrireXml(compteur,d, True)
                    fichierX.write(txt)
                    compteur+=1
            
            fichierX.write(finirXml())
            fichierX.close()
            print 'Cells with sharp movements + contact', contact_count#, '<br>'
            print 'Cells with sharp movements + alone', not_contact#, '<br>'
#So if I'm looking for the track ids that contain at least one sharp movement 
#I should be able to find it in the dictionary sharp which is saved in projects/Geiger/results/dict_nuclei_sharpmov_10pixelmin.pkl
    return sharp, contact