示例#1
0
 def combine_sliced_values(self, values):
     if type(values) is tuple:
         combined_values = tuple(
             [sum(reshape(v, (self.slices, -1)), axis=0) for v in values])
     else:
         combined_values = sum(reshape(values, (self.slices, -1)), axis=0)
     return combined_values
示例#2
0
 def info(self):
     '''
     Prints information about the morphology
     '''
     print len([s for n, s in self._segments.iteritems() if s['n'] == n]), 'segments'
     print 'Branches:'
     for name, branch in self._branches.iteritems():
         area = sum([self._segments[s]['area'] for s in branch['segments']]) * meter ** 2
         length = sum([self._segments[s]['length'] for s in branch['segments']\
                     if 'length' in self._segments[s]]) * meter
         print name, ':', len(branch['segments']), 'segments ; area =', area, '; length =', length
示例#3
0
 def simplify(self):
     '''
     Replaces branches by equivalent cylinders.
     Preserves the total area and electrotonic length.
     '''
     for branch in self._branches.itervalues():
         if branch['name'] != 'soma':
             segs = [self._segments[n] for n in branch['segments']]
             a = sum([seg['area'] for seg in segs])
             l = sum([seg['length'] / sqrt(seg['radius']) for seg in segs])
             r = (a / (2 * pi * l)) ** (2. / 3.)
             l0 = (r ** .5) * l
             segs[0]['radius'] = r * meter
             #segs[0]['length']=l0 # length is the length of connecting segment
             segs[0]['area'] = a * meter ** 2
             self._segments[branch['end']] = segs[0]
             branch['segments'] = branch['segments'][0:1]
             # Destroy other segments
             for seg in segs[1:-1]:
                 del self._segments[seg['n']]
示例#4
0
    def load(self, name, Cm=1 * uF / (cm ** 2)):
        '''
        Reads a SWC file containing a neuronal morphology and returns
        a morphology object
        Cm is the specific membrane capacitance.
        
        segments is a dictionary of segments, each segment is a dictionary with keys:
        * parent: index of the parent segment
        * type: type of segment in 'undefined','soma','dendrite','axon','apical','fork','end','custom'
        * length
        * area
        * radius
        * location: (x,y,z) coordinates
        
        Large database at http://neuromorpho.org/neuroMorpho
        
        Information below from http://www.mssm.edu/cnic/swc.html
        
        SWC File Format
    
        The format of an SWC file is fairly simple. It is a text file consisting of a header with various fields beginning with a # character, and a series of three dimensional points containing an index, radius, type, and connectivity information. The lines in the text file representing points have the following layout. 
        n T x y z R P
    
        n is an integer label that identifies the current point and increments by one from one line to the next.
        
        T is an integer representing the type of neuronal segment, such as soma, axon, apical dendrite, etc. The standard accepted integer values are given below.
        
            * 0 = undefined
            * 1 = soma
            * 2 = axon
            * 3 = dendrite
            * 4 = apical dendrite
            * 5 = fork point
            * 6 = end point
            * 7 = custom
        
        x, y, z gives the cartesian coordinates of each node.
        
        R is the radius at that node.
        P indicates the parent (the integer label) of the current point or -1 to indicate an origin (soma). 
        '''
        # First create the list of segments
        lines = open(name).read().splitlines()
        segments = []
        branching_points = []
        types = ['undefined', 'soma', 'axon', 'dendrite', 'apical', 'fork', 'end', 'custom']
        for line in lines:
            if line[0] != '#': # comment
                numbers = line.split()
                n = int(numbers[0]) - 1
                T = types[int(numbers[1])]
                x = float(numbers[2]) * um
                y = float(numbers[3]) * um
                z = float(numbers[4]) * um
                R = float(numbers[5]) * um
                P = int(numbers[6]) - 1 # 0-based indexing
                segment = dict(n=n, type=T, location=(x, y, z), radius=R, parent=P)
                if T == 'soma':
                    segment['area'] = 4 * pi * segment['radius'] ** 2
                else: # dendrite
                    segment['length'] = (sum((array(segment['location']) - array(segments[P]['location'])) ** 2)) ** .5 * meter
                    segment['area'] = segment['length'] * 2 * pi * segment['radius']

                if (P != n - 1) and (P > -2): # P is a branching point
                    branching_points.append(P)

                segments.append(segment)

        # Create branches
        # branches = list of dict(begin,end,segments) where segments are segment indexes
        branches = []
        branch = dict(start=0, segments=[], children=0)
        for segment in segments:
            n = segment['n']
            if segment['n'] in branching_points: # end of branch
                branch['segments'].append(n)
                branch['end'] = n
                branches.append(branch)
                branch = dict(start=n + 1, segments=[], children=0)
            elif segment['parent'] != n - 1: # new branch
                branch['end'] = n - 1
                branches.append(branch)
                branch = dict(start=n, segments=[n], children=0)
            else:
                branch['segments'].append(n)
        # Last branch
        branch['end'] = n
        branches.append(branch)

        # Make segment dictionary
        self._segments = dict()
        for segment in segments:
            self._segments[segment['n']] = segment

        # Name branches and segments
        # The soma is 'soma'
        self._branches = dict()
        for branch in branches:
            #if branch['type']
            parent = self._segments[branch['start']]['parent']
            if parent in self._segments:
                b = [b for b in branches if parent in b['segments']][0] # parent branch
                if b['name'] == 'soma':
                    branch['name'] = str(b['children'])
                else:
                    branch['name'] = b['name'] + str(b['children'])
                b['children'] += 1
            else:
                branch['name'] = 'soma'
            self._branches[branch['name']] = branch

        self.build_equations(Cm)
示例#5
0
 def combine_sliced_values(self, values):
     if type(values) is tuple:
         combined_values = tuple([sum(reshape(v, (self.slices, -1)), axis=0) for v in values])
     else:
         combined_values = sum(reshape(values, (self.slices, -1)), axis=0)
     return combined_values