<commit_before>''' Learning algorithms for tractography'''

import numpy as np
from . import track_metrics as tm
import dipy.core.performance as pf
from scipy import ndimage as nd
import itertools
import time
import numpy.linalg as npla


def local_skeleton_clustering():
    '''
    tracks=np.array([[0,0,0,1,0,0,2,0,0],
                                [3,0,0,3.5,1,0,4,2,0],
                                [3.2,0,0,3.7,1,0,4.4,2,0],
                                [3.4,0,0,3.9,1,0,4.6,2,0],
                                [0,0.2,0,1,0.2,0,2,0.2,0],
                                [0,0,0,0,1,0,0,2,0]])
    '''
    tracks=[np.array([[0,0,0],[1,0,0,],[2,0,0]]),            
                np.array([[3,0,0],[3.5,1,0],[4,2,0]]),
                np.array([[3.2,0,0],[3.7,1,0],[4.4,2,0]]),
                np.array([[3.4,0,0],[3.9,1,0],[4.6,2,0]]),
                np.array([[0,0.2,0],[1,0.2,0],[2,0.2,0]]),
                np.array([[0,0,0],[0,1,0],[0,2,0]])]
                                
    from dipy.viz import fos
    r=fos.ren()
    for i in range(6):
        fos.add(r,fos.line(tracks[i].reshape((3,3)),fos.red))
    fos.show(r)

    #Network C
    C={0:{'indices':[0],'hidden':tracks[0],'N':1}}

    d_thr=0.3
            
    for (it,t) in enumerate(tracks[1:]):
        
        lenC=len(C.keys())
        print 'lenC',lenC
        alld=np.zeros(lenC)
        for k in range(lenC):
        
            h=C[k]['hidden']/C[k]['N']
            #d=((t-h)**2).sum()      
            print 't',t
            print 'h',h
            d=np.sum(np.sqrt(np.sum((t-h)**2,axis=1)))/3.0
                  
            alld[k]=d
        
        #print alld
        
        m_k=np.min(alld)
        i_k=np.argmin(alld)
        
        print 'm_k',m_k,'i_k',i_k
        
        if m_k<d_thr:
            C[i_k]['hidden']+=t
            C[i_k]['N']+=1
            C[i_k]['indices'].append(it)
        else:
            C[lenC]={}
            C[lenC]['hidden']=t
            C[lenC]['N']=1
            C[lenC]['indices']=[it]
        
                
    return C    

def local_skeleton(tracks):
    
    d_thr=5.
    #hidden or hypothetical track
    H=[tracks[0]]
    #Class
    C=[[0]]
    #tmp point
    ts=np.zeros((9,),dtype=np.float32)
    #print ts
    
    it=1
    for t in tracks[1:]:        

        print it
        k=0          
        dC=np.zeros(len(H))
        while k < len(H):
            
            #print t
            #swap
            ts[0:3]=t[6:9];ts[3:6]=t[3:6];ts[6:9]=t[0:3]
            
            h=H[k][0]/len(C[k])        
            d=((t-h)**2).sum()
            ds=((ts-h)**2).sum()
            
            if d > ds :
                t=ts; d=ds
                                
            dC[k]=d                        
            k+=1
        
        print dC        
        mdC=np.min(dC)
        iC=np.argmin(dC)
                
        if mdC < d_thr:
                       
            H[iC][0]+=t
            C[iC].append(it)
            
        else:
            
            H.append([t])
            C.append([it])
                           
        
        it+=1    

    
    return C,H

def detect_corresponding_tracks(indices,tracks1,tracks2):
    ''' Detect corresponding tracks from 1 to 2
    
    Parameters:
    ----------------
    indices: sequence
            of indices of tracks1 that are to be detected in tracks2
    
    tracks1: sequence <commit_msg>swapping trajs is not working yet in local_skeleton_clust<commit_after>''' Learning algorithms for tractography'''

import numpy as np
from . import track_metrics as tm
import dipy.core.performance as pf
from scipy import ndimage as nd
import itertools
import time
import numpy.linalg as npla


def local_skeleton_clustering( d_thr=0.5):
    '''
    
    '''
    tracks=[np.array([[0,0,0],[1,0,0,],[2,0,0]]),            
                np.array([[3,0,0],[3.5,1,0],[4,2,0]]),
                np.array([[3.2,0,0],[3.7,1,0],[4.4,2,0]]),
                np.array([[3.4,0,0],[3.9,1,0],[4.6,2,0]]),
                np.array([[0,0.2,0],[1,0.2,0],[2,0.2,0]]),
                #np.array([[2,0.2,0],[1,0.2,0],[0,0.2,0]]),
                np.array([[0,0,0],[0,1,0],[0,2,0]])]
                                    
    from dipy.viz import fos
    r=fos.ren()
    for i in range(6):
        fos.add(r,fos.line(tracks[i],fos.red))
    fos.show(r)

    #Network C
    C={0:{'indices':[0],'hidden':tracks[0].copy(),'N':1}}
    ts=np.zeros((3,3),dtype=np.float32)
    for (it,t) in enumerate(tracks[1:]):
        
        lenC=len(C.keys())
        alld=np.zeros(lenC)
        
        tc=t.copy()
        
        for k in range(lenC):
        
            h=C[k]['hidden']/C[k]['N']
            d=np.sum(np.sqrt(np.sum((tc-h)**2,axis=1)))/3.0
            '''
            ts[0]=tc[-1];ts[1]=tc[1];ts[-1]=tc[0]
            ds=np.sum(np.sqrt(np.sum((ts-h)**2,axis=1)))/3.0
            if ds<d:
                #print it+1,ts
                d=ds;tc=ts
            '''
        print
        m_k=np.min(alld)
        i_k=np.argmin(alld)
        
        if m_k<d_thr:
            C[i_k]['hidden']+=tc
            C[i_k]['N']+=1
            C[i_k]['indices'].append(it+1)
        else:
            C[lenC]={}
            C[lenC]['hidden']=tc
            C[lenC]['N']=1
            C[lenC]['indices']=[it+1]
       
    fos.clear(r)

    color=[fos.red,fos.green,fos.blue,fos.yellow]
    for c in C:
        for i in C[c]['indices']:
            fos.add(r,fos.line(tracks[i],color[c]))
                
    fos.show(r)

    return C,tracks    


def detect_corresponding_tracks(indices,tracks1,tracks2):
    ''' Detect corresponding tracks from 1 to 2
    
    Parameters:
    ----------------
    indices: sequence
            of indices of tracks1 that are to be detected in tracks2
    
    tracks1: sequence 
{'fraction_non_alphanumeric': 0.1874356333676622, 'fraction_numerical': 0.050291795399931345, 'mean_word_length': 1.3832310838445807, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4482725', 'n_tokens_mistral': 2305, 'n_tokens_neox': 2226, 'n_words': 316}