idx int64 0 63k | question stringlengths 61 4.03k | target stringlengths 6 1.23k |
|---|---|---|
46,600 | def has_no_fat_ends ( neuron , multiple_of_mean = 2.0 , final_point_count = 5 ) : bad_ids = [ ] for leaf in _nf . iter_sections ( neuron . neurites , iterator_type = Tree . ileaf ) : mean_radius = np . mean ( leaf . points [ 1 : ] [ - final_point_count : , COLS . R ] ) if mean_radius * multiple_of_mean <= leaf . points [ - 1 , COLS . R ] : bad_ids . append ( ( leaf . id , leaf . points [ - 1 : ] ) ) return CheckResult ( len ( bad_ids ) == 0 , bad_ids ) | Check if leaf points are too large |
46,601 | def has_no_narrow_start ( neuron , frac = 0.9 ) : bad_ids = [ ( neurite . root_node . id , [ neurite . root_node . points [ 1 ] ] ) for neurite in neuron . neurites if neurite . root_node . points [ 1 ] [ COLS . R ] < frac * neurite . root_node . points [ 2 ] [ COLS . R ] ] return CheckResult ( len ( bad_ids ) == 0 , bad_ids ) | Check if neurites have a narrow start |
46,602 | def has_no_dangling_branch ( neuron ) : soma_center = neuron . soma . points [ : , COLS . XYZ ] . mean ( axis = 0 ) recentered_soma = neuron . soma . points [ : , COLS . XYZ ] - soma_center radius = np . linalg . norm ( recentered_soma , axis = 1 ) soma_max_radius = radius . max ( ) def is_dangling ( neurite ) : starting_point = neurite . points [ 1 ] [ COLS . XYZ ] if np . linalg . norm ( starting_point - soma_center ) - soma_max_radius <= 12. : return False if neurite . type != NeuriteType . axon : return True all_points = list ( chain . from_iterable ( n . points [ 1 : ] for n in iter_neurites ( neurite ) if n . type != NeuriteType . axon ) ) res = [ np . linalg . norm ( starting_point - p [ COLS . XYZ ] ) >= 2 * p [ COLS . R ] + 2 for p in all_points ] return all ( res ) bad_ids = [ ( n . root_node . id , [ n . root_node . points [ 1 ] ] ) for n in iter_neurites ( neuron ) if is_dangling ( n ) ] return CheckResult ( len ( bad_ids ) == 0 , bad_ids ) | Check if the neuron has dangling neurites |
46,603 | def has_no_narrow_neurite_section ( neuron , neurite_filter , radius_threshold = 0.05 , considered_section_min_length = 50 ) : considered_sections = ( sec for sec in iter_sections ( neuron , neurite_filter = neurite_filter ) if sec . length > considered_section_min_length ) def narrow_section ( section ) : return section . points [ : , COLS . R ] . mean ( ) < radius_threshold bad_ids = [ ( section . id , section . points [ 1 ] ) for section in considered_sections if narrow_section ( section ) ] return CheckResult ( len ( bad_ids ) == 0 , bad_ids ) | Check if the neuron has dendrites with narrow sections |
46,604 | def transform_header ( mtype_name ) : head_dict = OrderedDict ( ) head_dict [ "m-type" ] = mtype_name head_dict [ "components" ] = defaultdict ( OrderedDict ) return head_dict | Add header to json output to wrap around distribution data . |
46,605 | def draw ( obj , plane = '3d' , inline = False , ** kwargs ) : if plane . lower ( ) == '3d' : return _plot_neuron3d ( obj , inline , ** kwargs ) return _plot_neuron ( obj , plane , inline , ** kwargs ) | Draw the morphology using in the given plane |
46,606 | def _make_trace ( neuron , plane ) : for neurite in iter_neurites ( neuron ) : segments = list ( iter_segments ( neurite ) ) segs = [ ( s [ 0 ] [ COLS . XYZ ] , s [ 1 ] [ COLS . XYZ ] ) for s in segments ] coords = dict ( x = list ( chain . from_iterable ( ( p1 [ 0 ] , p2 [ 0 ] , None ) for p1 , p2 in segs ) ) , y = list ( chain . from_iterable ( ( p1 [ 1 ] , p2 [ 1 ] , None ) for p1 , p2 in segs ) ) , z = list ( chain . from_iterable ( ( p1 [ 2 ] , p2 [ 2 ] , None ) for p1 , p2 in segs ) ) ) color = TREE_COLOR . get ( neurite . root_node . type , 'black' ) if plane . lower ( ) == '3d' : plot_fun = go . Scatter3d else : plot_fun = go . Scatter coords = dict ( x = coords [ plane [ 0 ] ] , y = coords [ plane [ 1 ] ] ) yield plot_fun ( line = dict ( color = color , width = 2 ) , mode = 'lines' , ** coords ) | Create the trace to be plotted |
46,607 | def get_figure ( neuron , plane , title ) : data = list ( _make_trace ( neuron , plane ) ) axis = dict ( gridcolor = 'rgb(255, 255, 255)' , zerolinecolor = 'rgb(255, 255, 255)' , showbackground = True , backgroundcolor = 'rgb(230, 230,230)' ) if plane != '3d' : soma_2d = [ { 'type' : 'circle' , 'xref' : 'x' , 'yref' : 'y' , 'fillcolor' : 'rgba(50, 171, 96, 0.7)' , 'x0' : neuron . soma . center [ 0 ] - neuron . soma . radius , 'y0' : neuron . soma . center [ 1 ] - neuron . soma . radius , 'x1' : neuron . soma . center [ 0 ] + neuron . soma . radius , 'y1' : neuron . soma . center [ 1 ] + neuron . soma . radius , 'line' : { 'color' : 'rgba(50, 171, 96, 1)' , } , } , ] else : soma_2d = [ ] theta = np . linspace ( 0 , 2 * np . pi , 100 ) phi = np . linspace ( 0 , np . pi , 100 ) z = np . outer ( np . ones ( 100 ) , np . cos ( phi ) ) + neuron . soma . center [ 2 ] r = neuron . soma . radius data . append ( go . Surface ( x = ( np . outer ( np . cos ( theta ) , np . sin ( phi ) ) + neuron . soma . center [ 0 ] ) * r , y = ( np . outer ( np . sin ( theta ) , np . sin ( phi ) ) + neuron . soma . center [ 1 ] ) * r , z = z * r , cauto = False , surfacecolor = [ 'black' ] * len ( z ) , showscale = False , ) ) layout = dict ( autosize = True , title = title , scene = dict ( xaxis = axis , yaxis = axis , zaxis = axis , camera = dict ( up = dict ( x = 0 , y = 0 , z = 1 ) , eye = dict ( x = - 1.7428 , y = 1.0707 , z = 0.7100 , ) ) , aspectmode = 'data' ) , yaxis = dict ( scaleanchor = "x" ) , shapes = soma_2d , ) res = dict ( data = data , layout = layout ) return res | Returns the plotly figure containing the neuron |
46,608 | def rotate ( obj , axis , angle , origin = None ) : R = _rodrigues_to_dcm ( axis , angle ) try : return obj . transform ( PivotRotation ( R , origin ) ) except AttributeError : raise NotImplementedError | Rotation around unit vector following the right hand rule |
46,609 | def _sin ( x ) : return 0. if np . isclose ( np . mod ( x , np . pi ) , 0. ) else np . sin ( x ) | sine with case for pi multiples |
46,610 | def _rodrigues_to_dcm ( axis , angle ) : ux , uy , uz = axis / np . linalg . norm ( axis ) uxx = ux * ux uyy = uy * uy uzz = uz * uz uxy = ux * uy uxz = ux * uz uyz = uy * uz sn = _sin ( angle ) cs = _sin ( np . pi / 2. - angle ) cs1 = 1. - cs R = np . zeros ( [ 3 , 3 ] ) R [ 0 , 0 ] = cs + uxx * cs1 R [ 0 , 1 ] = uxy * cs1 - uz * sn R [ 0 , 2 ] = uxz * cs1 + uy * sn R [ 1 , 0 ] = uxy * cs1 + uz * sn R [ 1 , 1 ] = cs + uyy * cs1 R [ 1 , 2 ] = uyz * cs1 - ux * sn R [ 2 , 0 ] = uxz * cs1 - uy * sn R [ 2 , 1 ] = uyz * cs1 + ux * sn R [ 2 , 2 ] = cs + uzz * cs1 return R | Generates transformation matrix from unit vector and rotation angle . The rotation is applied in the direction of the axis which is a unit vector following the right hand rule . |
46,611 | def total_length ( nrn_pop , neurite_type = NeuriteType . all ) : nrns = _neuronfunc . neuron_population ( nrn_pop ) return list ( sum ( section_lengths ( n , neurite_type = neurite_type ) ) for n in nrns ) | Get the total length of all sections in the group of neurons or neurites |
46,612 | def n_segments ( neurites , neurite_type = NeuriteType . all ) : return sum ( len ( s . points ) - 1 for s in iter_sections ( neurites , neurite_filter = is_type ( neurite_type ) ) ) | Number of segments in a collection of neurites |
46,613 | def n_neurites ( neurites , neurite_type = NeuriteType . all ) : return sum ( 1 for _ in iter_neurites ( neurites , filt = is_type ( neurite_type ) ) ) | Number of neurites in a collection of neurites |
46,614 | def n_sections ( neurites , neurite_type = NeuriteType . all , iterator_type = Tree . ipreorder ) : return sum ( 1 for _ in iter_sections ( neurites , iterator_type = iterator_type , neurite_filter = is_type ( neurite_type ) ) ) | Number of sections in a collection of neurites |
46,615 | def n_bifurcation_points ( neurites , neurite_type = NeuriteType . all ) : return n_sections ( neurites , neurite_type = neurite_type , iterator_type = Tree . ibifurcation_point ) | number of bifurcation points in a collection of neurites |
46,616 | def n_forking_points ( neurites , neurite_type = NeuriteType . all ) : return n_sections ( neurites , neurite_type = neurite_type , iterator_type = Tree . iforking_point ) | number of forking points in a collection of neurites |
46,617 | def n_leaves ( neurites , neurite_type = NeuriteType . all ) : return n_sections ( neurites , neurite_type = neurite_type , iterator_type = Tree . ileaf ) | number of leaves points in a collection of neurites |
46,618 | def total_area_per_neurite ( neurites , neurite_type = NeuriteType . all ) : return [ neurite . area for neurite in iter_neurites ( neurites , filt = is_type ( neurite_type ) ) ] | Surface area in a collection of neurites . |
46,619 | def map_sections ( fun , neurites , neurite_type = NeuriteType . all , iterator_type = Tree . ipreorder ) : return map ( fun , iter_sections ( neurites , iterator_type = iterator_type , neurite_filter = is_type ( neurite_type ) ) ) | Map fun to all the sections in a collection of neurites |
46,620 | def section_lengths ( neurites , neurite_type = NeuriteType . all ) : return map_sections ( _section_length , neurites , neurite_type = neurite_type ) | section lengths in a collection of neurites |
46,621 | def section_term_lengths ( neurites , neurite_type = NeuriteType . all ) : return map_sections ( _section_length , neurites , neurite_type = neurite_type , iterator_type = Tree . ileaf ) | Termination section lengths in a collection of neurites |
46,622 | def section_bif_lengths ( neurites , neurite_type = NeuriteType . all ) : return map_sections ( _section_length , neurites , neurite_type = neurite_type , iterator_type = Tree . ibifurcation_point ) | Bifurcation section lengths in a collection of neurites |
46,623 | def section_branch_orders ( neurites , neurite_type = NeuriteType . all ) : return map_sections ( sectionfunc . branch_order , neurites , neurite_type = neurite_type ) | section branch orders in a collection of neurites |
46,624 | def section_bif_branch_orders ( neurites , neurite_type = NeuriteType . all ) : return map_sections ( sectionfunc . branch_order , neurites , neurite_type = neurite_type , iterator_type = Tree . ibifurcation_point ) | Bifurcation section branch orders in a collection of neurites |
46,625 | def section_term_branch_orders ( neurites , neurite_type = NeuriteType . all ) : return map_sections ( sectionfunc . branch_order , neurites , neurite_type = neurite_type , iterator_type = Tree . ileaf ) | Termination section branch orders in a collection of neurites |
46,626 | def section_path_lengths ( neurites , neurite_type = NeuriteType . all ) : dist = { } neurite_filter = is_type ( neurite_type ) for s in iter_sections ( neurites , neurite_filter = neurite_filter ) : dist [ s ] = s . length def pl2 ( node ) : return sum ( dist [ n ] for n in node . iupstream ( ) ) return map_sections ( pl2 , neurites , neurite_type = neurite_type ) | Path lengths of a collection of neurites |
46,627 | def map_neurons ( fun , neurites , neurite_type ) : nrns = _neuronfunc . neuron_population ( neurites ) return [ fun ( n , neurite_type = neurite_type ) for n in nrns ] | Map fun to all the neurites in a single or collection of neurons |
46,628 | def map_segments ( func , neurites , neurite_type ) : neurite_filter = is_type ( neurite_type ) return [ s for ss in iter_sections ( neurites , neurite_filter = neurite_filter ) for s in func ( ss ) ] | Map func to all the segments in a collection of neurites |
46,629 | def segment_volumes ( neurites , neurite_type = NeuriteType . all ) : def _func ( sec ) : return [ morphmath . segment_volume ( seg ) for seg in zip ( sec . points [ : - 1 ] , sec . points [ 1 : ] ) ] return map_segments ( _func , neurites , neurite_type ) | Volumes of the segments in a collection of neurites |
46,630 | def segment_radii ( neurites , neurite_type = NeuriteType . all ) : def _seg_radii ( sec ) : pts = sec . points [ : , COLS . R ] return np . divide ( np . add ( pts [ : - 1 ] , pts [ 1 : ] ) , 2.0 ) return map_segments ( _seg_radii , neurites , neurite_type ) | arithmetic mean of the radii of the points in segments in a collection of neurites |
46,631 | def segment_taper_rates ( neurites , neurite_type = NeuriteType . all ) : def _seg_taper_rates ( sec ) : pts = sec . points [ : , COLS . XYZR ] diff = np . diff ( pts , axis = 0 ) distance = np . linalg . norm ( diff [ : , COLS . XYZ ] , axis = 1 ) return np . divide ( 2 * np . abs ( diff [ : , COLS . R ] ) , distance ) return map_segments ( _seg_taper_rates , neurites , neurite_type ) | taper rates of the segments in a collection of neurites |
46,632 | def segment_midpoints ( neurites , neurite_type = NeuriteType . all ) : def _seg_midpoint ( sec ) : pts = sec . points [ : , COLS . XYZ ] return np . divide ( np . add ( pts [ : - 1 ] , pts [ 1 : ] ) , 2.0 ) return map_segments ( _seg_midpoint , neurites , neurite_type ) | Return a list of segment mid - points in a collection of neurites |
46,633 | def local_bifurcation_angles ( neurites , neurite_type = NeuriteType . all ) : return map_sections ( _bifurcationfunc . local_bifurcation_angle , neurites , neurite_type = neurite_type , iterator_type = Tree . ibifurcation_point ) | Get a list of local bifurcation angles in a collection of neurites |
46,634 | def remote_bifurcation_angles ( neurites , neurite_type = NeuriteType . all ) : return map_sections ( _bifurcationfunc . remote_bifurcation_angle , neurites , neurite_type = neurite_type , iterator_type = Tree . ibifurcation_point ) | Get a list of remote bifurcation angles in a collection of neurites |
46,635 | def bifurcation_partitions ( neurites , neurite_type = NeuriteType . all ) : return map ( _bifurcationfunc . bifurcation_partition , iter_sections ( neurites , iterator_type = Tree . ibifurcation_point , neurite_filter = is_type ( neurite_type ) ) ) | Partition at bifurcation points of a collection of neurites |
46,636 | def partition_asymmetries ( neurites , neurite_type = NeuriteType . all ) : return map ( _bifurcationfunc . partition_asymmetry , iter_sections ( neurites , iterator_type = Tree . ibifurcation_point , neurite_filter = is_type ( neurite_type ) ) ) | Partition asymmetry at bifurcation points of a collection of neurites |
46,637 | def partition_pairs ( neurites , neurite_type = NeuriteType . all ) : return map ( _bifurcationfunc . partition_pair , iter_sections ( neurites , iterator_type = Tree . ibifurcation_point , neurite_filter = is_type ( neurite_type ) ) ) | Partition pairs at bifurcation points of a collection of neurites . Partition pait is defined as the number of bifurcations at the two daughters of the bifurcating section |
46,638 | def section_term_radial_distances ( neurites , neurite_type = NeuriteType . all , origin = None ) : return section_radial_distances ( neurites , neurite_type = neurite_type , origin = origin , iterator_type = Tree . ileaf ) | Get the radial distances of the termination sections for a collection of neurites |
46,639 | def section_bif_radial_distances ( neurites , neurite_type = NeuriteType . all , origin = None ) : return section_radial_distances ( neurites , neurite_type = neurite_type , origin = origin , iterator_type = Tree . ibifurcation_point ) | Get the radial distances of the bifurcation sections for a collection of neurites |
46,640 | def number_of_sections_per_neurite ( neurites , neurite_type = NeuriteType . all ) : return list ( sum ( 1 for _ in n . iter_sections ( ) ) for n in iter_neurites ( neurites , filt = is_type ( neurite_type ) ) ) | Get the number of sections per neurite in a collection of neurites |
46,641 | def total_length_per_neurite ( neurites , neurite_type = NeuriteType . all ) : return list ( sum ( s . length for s in n . iter_sections ( ) ) for n in iter_neurites ( neurites , filt = is_type ( neurite_type ) ) ) | Get the path length per neurite in a collection |
46,642 | def terminal_path_lengths_per_neurite ( neurites , neurite_type = NeuriteType . all ) : return list ( sectionfunc . section_path_length ( s ) for n in iter_neurites ( neurites , filt = is_type ( neurite_type ) ) for s in iter_sections ( n , iterator_type = Tree . ileaf ) ) | Get the path lengths to each terminal point per neurite in a collection |
46,643 | def total_volume_per_neurite ( neurites , neurite_type = NeuriteType . all ) : return list ( sum ( s . volume for s in n . iter_sections ( ) ) for n in iter_neurites ( neurites , filt = is_type ( neurite_type ) ) ) | Get the volume per neurite in a collection |
46,644 | def neurite_volume_density ( neurites , neurite_type = NeuriteType . all ) : def vol_density ( neurite ) : return neurite . volume / convex_hull ( neurite ) . volume return list ( vol_density ( n ) for n in iter_neurites ( neurites , filt = is_type ( neurite_type ) ) ) | Get the volume density per neurite |
46,645 | def section_volumes ( neurites , neurite_type = NeuriteType . all ) : return map_sections ( sectionfunc . section_volume , neurites , neurite_type = neurite_type ) | section volumes in a collection of neurites |
46,646 | def section_areas ( neurites , neurite_type = NeuriteType . all ) : return map_sections ( sectionfunc . section_area , neurites , neurite_type = neurite_type ) | section areas in a collection of neurites |
46,647 | def section_tortuosity ( neurites , neurite_type = NeuriteType . all ) : return map_sections ( sectionfunc . section_tortuosity , neurites , neurite_type = neurite_type ) | section tortuosities in a collection of neurites |
46,648 | def section_end_distances ( neurites , neurite_type = NeuriteType . all ) : return map_sections ( sectionfunc . section_end_distance , neurites , neurite_type = neurite_type ) | section end to end distances in a collection of neurites |
46,649 | def principal_direction_extents ( neurites , neurite_type = NeuriteType . all , direction = 0 ) : def _pde ( neurite ) : points = neurite . points [ : , : 3 ] return morphmath . principal_direction_extent ( points ) [ direction ] return map ( _pde , iter_neurites ( neurites , filt = is_type ( neurite_type ) ) ) | Principal direction extent of neurites in neurons |
46,650 | def _get_linewidth ( tree , linewidth , diameter_scale ) : if diameter_scale is not None and tree : linewidth = [ 2 * segment_radius ( s ) * diameter_scale for s in iter_segments ( tree ) ] return linewidth | calculate the desired linewidth based on tree contents |
46,651 | def plot_tree ( ax , tree , plane = 'xy' , diameter_scale = _DIAMETER_SCALE , linewidth = _LINEWIDTH , color = None , alpha = _ALPHA ) : plane0 , plane1 = _plane2col ( plane ) segs = [ ( ( s [ 0 ] [ plane0 ] , s [ 0 ] [ plane1 ] ) , ( s [ 1 ] [ plane0 ] , s [ 1 ] [ plane1 ] ) ) for s in iter_segments ( tree ) ] linewidth = _get_linewidth ( tree , diameter_scale = diameter_scale , linewidth = linewidth ) color = _get_color ( color , tree . type ) collection = LineCollection ( segs , color = color , linewidth = linewidth , alpha = alpha ) ax . add_collection ( collection ) | Plots a 2d figure of the tree s segments |
46,652 | def plot_soma ( ax , soma , plane = 'xy' , soma_outline = True , linewidth = _LINEWIDTH , color = None , alpha = _ALPHA ) : plane0 , plane1 = _plane2col ( plane ) color = _get_color ( color , tree_type = NeuriteType . soma ) if isinstance ( soma , SomaCylinders ) : plane0 , plane1 = _plane2col ( plane ) for start , end in zip ( soma . points , soma . points [ 1 : ] ) : common . project_cylinder_onto_2d ( ax , ( plane0 , plane1 ) , start = start [ COLS . XYZ ] , end = end [ COLS . XYZ ] , start_radius = start [ COLS . R ] , end_radius = end [ COLS . R ] , color = color , alpha = alpha ) else : if soma_outline : ax . add_artist ( Circle ( soma . center [ [ plane0 , plane1 ] ] , soma . radius , color = color , alpha = alpha ) ) else : plane0 , plane1 = _plane2col ( plane ) points = [ ( p [ plane0 ] , p [ plane1 ] ) for p in soma . iter ( ) ] if points : points . append ( points [ 0 ] ) ax . plot ( points , color = color , alpha = alpha , linewidth = linewidth ) ax . set_xlabel ( plane [ 0 ] ) ax . set_ylabel ( plane [ 1 ] ) bounding_box = geom . bounding_box ( soma ) ax . dataLim . update_from_data_xy ( np . vstack ( ( [ bounding_box [ 0 ] [ plane0 ] , bounding_box [ 0 ] [ plane1 ] ] , [ bounding_box [ 1 ] [ plane0 ] , bounding_box [ 1 ] [ plane1 ] ] ) ) , ignore = False ) | Generates a 2d figure of the soma . |
46,653 | def plot_neuron ( ax , nrn , neurite_type = NeuriteType . all , plane = 'xy' , soma_outline = True , diameter_scale = _DIAMETER_SCALE , linewidth = _LINEWIDTH , color = None , alpha = _ALPHA ) : plot_soma ( ax , nrn . soma , plane = plane , soma_outline = soma_outline , linewidth = linewidth , color = color , alpha = alpha ) for neurite in iter_neurites ( nrn , filt = tree_type_checker ( neurite_type ) ) : plot_tree ( ax , neurite , plane = plane , diameter_scale = diameter_scale , linewidth = linewidth , color = color , alpha = alpha ) ax . set_title ( nrn . name ) ax . set_xlabel ( plane [ 0 ] ) ax . set_ylabel ( plane [ 1 ] ) | Plots a 2D figure of the neuron that contains a soma and the neurites |
46,654 | def plot_tree3d ( ax , tree , diameter_scale = _DIAMETER_SCALE , linewidth = _LINEWIDTH , color = None , alpha = _ALPHA ) : segs = [ ( s [ 0 ] [ COLS . XYZ ] , s [ 1 ] [ COLS . XYZ ] ) for s in iter_segments ( tree ) ] linewidth = _get_linewidth ( tree , diameter_scale = diameter_scale , linewidth = linewidth ) color = _get_color ( color , tree . type ) collection = Line3DCollection ( segs , color = color , linewidth = linewidth , alpha = alpha ) ax . add_collection3d ( collection ) _update_3d_datalim ( ax , tree ) | Generates a figure of the tree in 3d . |
46,655 | def plot_soma3d ( ax , soma , color = None , alpha = _ALPHA ) : color = _get_color ( color , tree_type = NeuriteType . soma ) if isinstance ( soma , SomaCylinders ) : for start , end in zip ( soma . points , soma . points [ 1 : ] ) : common . plot_cylinder ( ax , start = start [ COLS . XYZ ] , end = end [ COLS . XYZ ] , start_radius = start [ COLS . R ] , end_radius = end [ COLS . R ] , color = color , alpha = alpha ) else : common . plot_sphere ( ax , center = soma . center [ COLS . XYZ ] , radius = soma . radius , color = color , alpha = alpha ) _update_3d_datalim ( ax , soma ) | Generates a 3d figure of the soma . |
46,656 | def _generate_collection ( group , ax , ctype , colors ) : color = TREE_COLOR [ ctype ] collection = PolyCollection ( group , closed = False , antialiaseds = True , edgecolors = 'face' , facecolors = color ) ax . add_collection ( collection ) if color not in colors : label = str ( ctype ) . replace ( 'NeuriteType.' , '' ) . replace ( '_' , ' ' ) . capitalize ( ) ax . plot ( ( 0. , 0. ) , ( 0. , 0. ) , c = color , label = label ) colors . add ( color ) | Render rectangle collection |
46,657 | def plot_dendrogram ( ax , obj , show_diameters = True ) : dnd = Dendrogram ( obj , show_diameters = show_diameters ) dnd . generate ( ) _render_dendrogram ( dnd , ax , 0. ) ax . set_title ( 'Morphology Dendrogram' ) ax . set_xlabel ( 'micrometers (um)' ) ax . set_ylabel ( 'micrometers (um)' ) ax . set_aspect ( 'auto' ) ax . legend ( ) | Dendrogram of obj |
46,658 | def make_neurites ( rdw ) : post_action = _NEURITE_ACTION [ rdw . fmt ] trunks = rdw . neurite_root_section_ids ( ) if not trunks : return [ ] , [ ] nodes = tuple ( Section ( section_id = i , points = rdw . data_block [ sec . ids ] , section_type = _TREE_TYPES [ sec . ntype ] ) for i , sec in enumerate ( rdw . sections ) ) for i , node in enumerate ( nodes ) : parent_id = rdw . sections [ i ] . pid parent_type = nodes [ parent_id ] . type if parent_id != ROOT_ID and parent_type != NeuriteType . soma : nodes [ parent_id ] . add_child ( node ) neurites = tuple ( Neurite ( nodes [ i ] ) for i in trunks ) if post_action is not None : for n in neurites : post_action ( n . root_node ) return neurites , nodes | Build neurite trees from a raw data wrapper |
46,659 | def _remove_soma_initial_point ( tree ) : if tree . points [ 0 ] [ COLS . TYPE ] == POINT_TYPE . SOMA : tree . points = tree . points [ 1 : ] | Remove tree s initial point if soma |
46,660 | def _check_soma_topology_swc ( points ) : if len ( points ) == 3 : return parents = tuple ( p [ COLS . P ] for p in points if p [ COLS . P ] != ROOT_ID ) if len ( parents ) > len ( set ( parents ) ) : raise SomaError ( "Bifurcating soma" ) | check if points form valid soma |
46,661 | def points ( self ) : if self . _points is None : _points = self . soma . points . tolist ( ) for n in self . neurites : _points . extend ( n . points . tolist ( ) ) self . _points = np . array ( _points ) return self . _points | Return unordered array with all the points in this neuron |
46,662 | def transform ( self , trans ) : _data = deepcopy ( self . _data ) _data . data_block [ : , 0 : 3 ] = trans ( _data . data_block [ : , 0 : 3 ] ) return FstNeuron ( _data , self . name ) | Return a copy of this neuron with a 3D transformation applied |
46,663 | def vector ( p1 , p2 ) : return np . subtract ( p1 [ COLS . XYZ ] , p2 [ COLS . XYZ ] ) | compute vector between two 3D points |
46,664 | def interpolate_radius ( r1 , r2 , fraction ) : def f ( a , b , c ) : return a + c * ( b - a ) return f ( r2 , r1 , 1. - fraction ) if r1 > r2 else f ( r1 , r2 , fraction ) | Calculate the radius that corresponds to a point P that lies at a fraction of the length of a cut cone P1P2 where P1 P2 are the centers of the circles that bound the shape with radii r1 and r2 respectively . |
46,665 | def path_fraction_id_offset ( points , fraction , relative_offset = False ) : if not ( 0. <= fraction <= 1.0 ) : raise ValueError ( "Invalid fraction: %.3f" % fraction ) pts = np . array ( points ) [ : , COLS . XYZ ] lengths = np . linalg . norm ( np . diff ( pts , axis = 0 ) , axis = 1 ) cum_lengths = np . cumsum ( lengths ) offset = cum_lengths [ - 1 ] * fraction seg_id = np . argmin ( cum_lengths < offset ) if seg_id > 0 : offset -= cum_lengths [ seg_id - 1 ] if relative_offset : offset /= lengths [ seg_id ] return seg_id , offset | Find the segment which corresponds to the fraction of the path length along the piecewise linear curve which is constructed from the set of points . |
46,666 | def path_fraction_point ( points , fraction ) : seg_id , offset = path_fraction_id_offset ( points , fraction , relative_offset = True ) return linear_interpolate ( points [ seg_id ] , points [ seg_id + 1 ] , offset ) | Computes the point which corresponds to the fraction of the path length along the piecewise linear curve which is constructed from the set of points . |
46,667 | def scalar_projection ( v1 , v2 ) : return np . dot ( v1 , v2 ) / np . linalg . norm ( v2 ) | compute the scalar projection of v1 upon v2 |
46,668 | def vector_projection ( v1 , v2 ) : return scalar_projection ( v1 , v2 ) * v2 / np . linalg . norm ( v2 ) | compute the vector projection of v1 upon v2 |
46,669 | def dist_point_line ( p , l1 , l2 ) : cross_prod = np . cross ( l2 - l1 , p - l1 ) return np . linalg . norm ( cross_prod ) / np . linalg . norm ( l2 - l1 ) | compute the orthogonal distance between from the line that goes through the points l1 l2 and the point p |
46,670 | def point_dist2 ( p1 , p2 ) : v = vector ( p1 , p2 ) return np . dot ( v , v ) | compute the square of the euclidian distance between two 3D points |
46,671 | def angle_3points ( p0 , p1 , p2 ) : vec1 = vector ( p1 , p0 ) vec2 = vector ( p2 , p0 ) return math . atan2 ( np . linalg . norm ( np . cross ( vec1 , vec2 ) ) , np . dot ( vec1 , vec2 ) ) | compute the angle in radians between three 3D points |
46,672 | def angle_between_vectors ( p1 , p2 ) : v1 = p1 / np . linalg . norm ( p1 ) v2 = p2 / np . linalg . norm ( p2 ) return np . arccos ( np . clip ( np . dot ( v1 , v2 ) , - 1.0 , 1.0 ) ) | Computes the angle in radians between vectors p1 and p2 Normalizes the input vectors and computes the relative angle between them . |
46,673 | def polygon_diameter ( points ) : return max ( point_dist ( p0 , p1 ) for ( p0 , p1 ) in combinations ( points , 2 ) ) | Compute the maximun euclidian distance between any two points in a list of points |
46,674 | def average_points_dist ( p0 , p_list ) : return np . mean ( list ( point_dist ( p0 , p1 ) for p1 in p_list ) ) | Computes the average distance between a list of points and a given point p0 . |
46,675 | def path_distance ( points ) : vecs = np . diff ( points , axis = 0 ) [ : , : 3 ] d2 = [ np . dot ( p , p ) for p in vecs ] return np . sum ( np . sqrt ( d2 ) ) | Compute the path distance from given set of points |
46,676 | def segment_radial_dist ( seg , pos ) : return point_dist ( pos , np . divide ( np . add ( seg [ 0 ] , seg [ 1 ] ) , 2.0 ) ) | Return the radial distance of a tree segment to a given point |
46,677 | def segment_area ( seg ) : r0 = seg [ 0 ] [ COLS . R ] r1 = seg [ 1 ] [ COLS . R ] h2 = point_dist2 ( seg [ 0 ] , seg [ 1 ] ) return math . pi * ( r0 + r1 ) * math . sqrt ( ( r0 - r1 ) ** 2 + h2 ) | Compute the surface area of a segment . |
46,678 | def segment_volume ( seg ) : r0 = seg [ 0 ] [ COLS . R ] r1 = seg [ 1 ] [ COLS . R ] h = point_dist ( seg [ 0 ] , seg [ 1 ] ) return math . pi * h * ( ( r0 * r0 ) + ( r0 * r1 ) + ( r1 * r1 ) ) / 3.0 | Compute the volume of a segment . |
46,679 | def taper_rate ( p0 , p1 ) : return 2 * abs ( p0 [ COLS . R ] - p1 [ COLS . R ] ) / point_dist ( p0 , p1 ) | Compute the taper rate between points p0 and p1 |
46,680 | def principal_direction_extent ( points ) : points = np . copy ( points ) points -= np . mean ( points , axis = 0 ) _ , eigv = pca ( points ) extent = np . zeros ( 3 ) for i in range ( eigv . shape [ 1 ] ) : scalar_projs = np . sort ( np . array ( [ np . dot ( p , eigv [ : , i ] ) for p in points ] ) ) extent [ i ] = scalar_projs [ - 1 ] if scalar_projs [ 0 ] < 0. : extent -= scalar_projs [ 0 ] return extent | Calculate the extent of a set of 3D points . |
46,681 | def stylize ( ax , name , feature ) : ax . set_ylabel ( feature ) ax . set_title ( name , fontsize = 'small' ) | Stylization modifications to the plots |
46,682 | def plot_feature ( feature , cell ) : fig = pl . figure ( ) ax = fig . add_subplot ( 111 ) if cell is not None : try : histogram ( cell , feature , ax ) except ValueError : pass stylize ( ax , cell . name , feature ) return fig | Plot a feature |
46,683 | def path_end_to_end_distance ( neurite ) : trunk = neurite . root_node . points [ 0 ] return max ( morphmath . point_dist ( l . points [ - 1 ] , trunk ) for l in neurite . root_node . ileaf ( ) ) | Calculate and return end - to - end - distance of a given neurite . |
46,684 | def make_end_to_end_distance_plot ( nb_segments , end_to_end_distance , neurite_type ) : plt . figure ( ) plt . plot ( nb_segments , end_to_end_distance ) plt . title ( neurite_type ) plt . xlabel ( 'Number of segments' ) plt . ylabel ( 'End-to-end distance' ) plt . show ( ) | Plot end - to - end distance vs number of segments |
46,685 | def calculate_and_plot_end_to_end_distance ( neurite ) : def _dist ( seg ) : return morphmath . point_dist ( seg [ 1 ] , neurite . root_node . points [ 0 ] ) end_to_end_distance = [ _dist ( s ) for s in nm . iter_segments ( neurite ) ] make_end_to_end_distance_plot ( np . arange ( len ( end_to_end_distance ) ) + 1 , end_to_end_distance , neurite . type ) | Calculate and plot the end - to - end distance vs the number of segments for an increasingly larger part of a given neurite . |
46,686 | def tree_type_checker ( * ref ) : ref = tuple ( ref ) if NeuriteType . all in ref : def check_tree_type ( _ ) : return True else : def check_tree_type ( tree ) : return tree . type in ref return check_tree_type | Tree type checker functor |
46,687 | def dendrite_filter ( n ) : return n . type == NeuriteType . basal_dendrite or n . type == NeuriteType . apical_dendrite | Select only dendrites |
46,688 | def plot_somas ( somas ) : _ , ax = common . get_figure ( new_fig = True , subplot = 111 , params = { 'projection' : '3d' , 'aspect' : 'equal' } ) for s in somas : common . plot_sphere ( ax , s . center , s . radius , color = random_color ( ) , alpha = 1 ) plt . show ( ) | Plot set of somas on same figure as spheres each with different color |
46,689 | def _max_recursion_depth ( obj ) : neurites = obj . neurites if hasattr ( obj , 'neurites' ) else [ obj ] return max ( sum ( 1 for _ in neu . iter_sections ( ) ) for neu in neurites ) | Estimate recursion depth which is defined as the number of nodes in a tree |
46,690 | def _total_rectangles ( tree ) : return sum ( len ( sec . children ) + sec . points . shape [ 0 ] - 1 for sec in tree . iter_sections ( ) ) | Calculate the total number of segments that are required for the dendrogram . There is a vertical line for each segment and two horizontal line at each branching point |
46,691 | def _n_rectangles ( obj ) : return sum ( _total_rectangles ( neu ) for neu in obj . neurites ) if hasattr ( obj , 'neurites' ) else _total_rectangles ( obj ) | Calculate the total number of rectangles with respect to the type of the object |
46,692 | def _square_segment ( radius , origin ) : return np . array ( ( ( origin [ 0 ] - radius , origin [ 1 ] - radius ) , ( origin [ 0 ] - radius , origin [ 1 ] + radius ) , ( origin [ 0 ] + radius , origin [ 1 ] + radius ) , ( origin [ 0 ] + radius , origin [ 1 ] - radius ) ) ) | Vertices for a square |
46,693 | def _vertical_segment ( old_offs , new_offs , spacing , radii ) : return np . array ( ( ( new_offs [ 0 ] - radii [ 0 ] , old_offs [ 1 ] + spacing [ 1 ] ) , ( new_offs [ 0 ] - radii [ 1 ] , new_offs [ 1 ] ) , ( new_offs [ 0 ] + radii [ 1 ] , new_offs [ 1 ] ) , ( new_offs [ 0 ] + radii [ 0 ] , old_offs [ 1 ] + spacing [ 1 ] ) ) ) | Vertices for a vertical rectangle |
46,694 | def _horizontal_segment ( old_offs , new_offs , spacing , diameter ) : return np . array ( ( ( old_offs [ 0 ] , old_offs [ 1 ] + spacing [ 1 ] ) , ( new_offs [ 0 ] , old_offs [ 1 ] + spacing [ 1 ] ) , ( new_offs [ 0 ] , old_offs [ 1 ] + spacing [ 1 ] - diameter ) , ( old_offs [ 0 ] , old_offs [ 1 ] + spacing [ 1 ] - diameter ) ) ) | Vertices of a horizontal rectangle |
46,695 | def _spacingx ( node , max_dims , xoffset , xspace ) : x_spacing = _n_terminations ( node ) * xspace if x_spacing > max_dims [ 0 ] : max_dims [ 0 ] = x_spacing return xoffset - x_spacing / 2. | Determine the spacing of the current node depending on the number of the leaves of the tree |
46,696 | def _update_offsets ( start_x , spacing , terminations , offsets , length ) : return ( start_x + spacing [ 0 ] * terminations / 2. , offsets [ 1 ] + spacing [ 1 ] * 2. + length ) | Update the offsets |
46,697 | def _max_diameter ( tree ) : return 2. * max ( max ( node . points [ : , COLS . R ] ) for node in tree . ipreorder ( ) ) | Find max diameter in tree |
46,698 | def _generate_dendro ( self , current_section , spacing , offsets ) : max_dims = self . _max_dims start_x = _spacingx ( current_section , max_dims , offsets [ 0 ] , spacing [ 0 ] ) for child in current_section . children : segments = child . points terminations = _n_terminations ( child ) seg_lengths = np . linalg . norm ( np . subtract ( segments [ : - 1 , COLS . XYZ ] , segments [ 1 : , COLS . XYZ ] ) , axis = 1 ) radii = np . vstack ( ( segments [ : - 1 , COLS . R ] , segments [ 1 : , COLS . R ] ) ) . T if self . _show_diameters else np . zeros ( ( seg_lengths . shape [ 0 ] , 2 ) ) y_offset = offsets [ 1 ] for i , slen in enumerate ( seg_lengths ) : new_offsets = _update_offsets ( start_x , spacing , terminations , ( offsets [ 0 ] , y_offset ) , slen ) self . _rectangles [ self . _n ] = _vertical_segment ( ( offsets [ 0 ] , y_offset ) , new_offsets , spacing , radii [ i , : ] ) self . _n += 1 y_offset = new_offsets [ 1 ] if y_offset + spacing [ 1 ] * 2 + sum ( seg_lengths ) > max_dims [ 1 ] : max_dims [ 1 ] = y_offset + spacing [ 1 ] * 2. + sum ( seg_lengths ) self . _max_dims = max_dims self . _generate_dendro ( child , spacing , new_offsets ) start_x += terminations * spacing [ 0 ] if offsets [ 0 ] != new_offsets [ 0 ] : self . _rectangles [ self . _n ] = _horizontal_segment ( offsets , new_offsets , spacing , 0. ) self . _n += 1 | Recursive function for dendrogram line computations |
46,699 | def types ( self ) : neurites = self . _obj . neurites if hasattr ( self . _obj , 'neurites' ) else ( self . _obj , ) return ( neu . type for neu in neurites ) | Returns an iterator over the types of the neurites in the object . If the object is a tree then one value is returned . |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.