<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <H1>Bidirectional connections as a function of the distance </H1>
We will test whether bidirectionally connected inhibitory interneurons are over-represented as a function of the intersomatic distance.
Step2:  <H2> Load all distances from connected PV cells</H2>
Step4:  <H2>Distances in recurrently connected inhibitory neurons</H2>
<ASSISTANT_TASK:>
Python Code:
%pylab inline
import warnings
from inet import DataLoader, __version__
from inet.motifs import iicounter
from inet.utils import II_slice
print('Inet version {}'.format(__version__))
# use filenames in the dataset to read list of distances to be read
mydataset = DataLoader('../data/PV')
pvfiles = [ i for i in range(len(mydataset)) if int(mydataset.filename(i)[0])>1 ]
print('{} experiments with 2 or more PV-cells'.format(len(pvfiles)))
Explanation: <H1>Bidirectional connections as a function of the distance </H1>
We will test whether bidirectionally connected inhibitory interneurons are over-represented as a function of the intersomatic distance.
End of explanation
# read distances from between inhibitory neurons
def read_dist(fname):
    
    get distances between inhibitory pairs of neurons
    from a matrix of intersomatic distances.
    Argument:
    fname: string
        the matrix name to that contains the connected synapses (*.syn)
    
    # take all non-diagonal elements
    mypath = '../data/PV/' + fname[:-3] + 'dist'
    
    try:
        D = np.loadtxt(mypath)
        D = II_slice(D, int(fname[0]))
        idx = np.where(~np.eye(D.shape[0], dtype = bool))
        mydist = np.abs(D[idx]).tolist()
        return(mydist)
    
    except IOError:
        warnings.warn(mypath + ' not found!')
        return([])
    
dist_tested = list()
for i in pvfiles:
    dist_tested +=  read_dist(mydataset.filename(i))
print('{} total distances read'.format(len(dist_tested)))
mybins = arange(0,600, 50)
plt.hist(dist_tested, bins = mybins, facecolor='white', lw=2);
plt.ylim(ymax=50);
plt.ylabel('Inhbitory chemical synapses');
plt.xlabel('Intersomatic distance ($\mu$m)');
Explanation: <H2> Load all distances from connected PV cells</H2>
End of explanation
def read_rec_dist(fname):
    
    get distances between bidirectionally connected interneurons
    from a matrix of intersomatic distances.
    
    Argument:
    fname: string
        the matrix name to that contains the connected synapses (*.syn)
    
    # take all non-diagonal elements
    mydistpath = '../data/PV/' + fname[:-3] + 'dist'
    try:
        D = II_slice(np.loadtxt(mydistpath), int(fname[0]))
        
    except IOError:
        warnings.warn(mydistpath + ' not found!')
        return([])
    
    try:
        S = np.loadtxt('../data/PV/'+fname)
        
    except IOError:
        warnings.warn(fname + ' not found!')
        return([])
    
    S = II_slice(S, int(fname[0]))
    S[S==2] = 0 # remove gaps
    S[S==3] = 1 # remove gaps in chemical
        
    x,y = np.nonzero(S)
    ids = zip(x,y)
    mydist = list()
    if ids>0:
        for i,j in ids:
            if S[j,i] == 1:
                mydist.append(D[i,j])
     
    print(np.unique(np.abs(mydist)))  
    return( np.unique(np.abs(mydist)).tolist() )
# Number of bidirectionally connected interneurons
mydataset.motif['ii_c2']
# select experiments with bidirectional motifs
mybidirec = [i for i in range(len(mydataset)) if mydataset.motifs(i)['ii_c2']['found']>0 ]
for i in mybidirec:
    print('Experiment {:3d}, filename: {}'.format(i, mydataset.filename(i)))
dist_found = list()
for i in mybidirec:
    dist_found += read_rec_dist(mydataset.filename(i))
dist_found
mybins = arange(0,550, 50)
bid_tested = np.unique(dist_tested)
plt.hist(bid_tested, bins = mybins, facecolor='white', lw=2);
plt.ylim(ymax=20);
plt.ylabel('Inhbitory chemical synapses');
plt.xlabel('Intersomatic distance ($\mu$m)');
plt.hist(dist_found, bins = mybins, facecolor='gray', lw=2);
Explanation: <H2>Distances in recurrently connected inhibitory neurons</H2>
End of explanation
<END_TASK>