Skip to content

Commit

Permalink
add option to compute path length with types
Browse files Browse the repository at this point in the history
  • Loading branch information
xiuliren committed Nov 15, 2018
1 parent 1239b86 commit 6b5d414
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 20 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
julia 0.7
julia 1.0
LightGraphs
MetaGraphs
DataFrames
Expand Down
68 changes: 54 additions & 14 deletions src/Neurons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function Neuron(nodeNet::NodeNet)
seedNodeIdList::Vector = [rootNodeId]
# grow the main net
neuron = Neuron!(rootNodeId, nodeNet, collectedFlagVec)
println(sum(collectedFlagVec), " visited voxels in ", length(collectedFlagVec))
# println(sum(collectedFlagVec), " visited voxels in ", length(collectedFlagVec))
#return neuron # only use the main segment for evaluation

while !all(collectedFlagVec)
Expand Down Expand Up @@ -450,10 +450,14 @@ end


"""
get_segment_path_length_list(self::Neuron)
get_segment_path_length_list(self::Neuron;
segmentList::Vector{Segment}=get_segment_list(self),
class::{Nothing,UInt8}=nothing)
get euclidean path length of each segment
"""
function get_segment_path_length_list( self::Neuron; segmentList = get_segment_list(self) )
function get_segment_path_length_list( self::Neuron;
segmentList::Vector{Segment} = get_segment_list(self),
class::Union{Nothing,UInt8}=nothing )
ret = Vector{Float64}()
for (index, segment) in enumerate( segmentList )
segmentPathLength = Segments.get_path_length( segment )
Expand All @@ -464,8 +468,13 @@ function get_segment_path_length_list( self::Neuron; segmentList = get_segment_l
parentNode = parentSegment[ end ]
node = segment[1]
segmentPathLength += norm( [map((x,y)->x-y, node[1:3], parentNode[1:3])...] )
end
push!(ret, segmentPathLength)
end

if class == nothing || class==Segments.get_class(segment)
# include all segment
# the class matchs
push!(ret, segmentPathLength)
end
end
ret
end
Expand All @@ -482,8 +491,12 @@ function get_node_distance_list(neuron::Neuron)
nodeDistanceList
end

function get_total_path_length(self::Neuron)
get_segment_path_length_list(self) |> sum
"""
get_total_path_length(self::Neuron; class::Union{Nothing,UInt8}=nothing)
the default class=nothing will include all of the segments
"""
@inline function get_total_path_length(self::Neuron; class::Union{Nothing,UInt8}=nothing)
get_segment_path_length_list(self; class=class) |> sum
end

"""
Expand Down Expand Up @@ -1508,9 +1521,7 @@ function get_all_pre_synapse_list(self::Neuron)
for segment in self
preSynapseSparseVec = Segments.get_pre_synapse_sparse_vec(segment)
I, synapseList = findnz(preSynapseSparseVec)
if !isempty(I)
append!(ret, synapseList)
end
append!(ret, synapseList)
end
ret
end
Expand All @@ -1523,9 +1534,7 @@ function get_all_post_synapse_list(self::Neuron)
for segment in self
postSynapseSparseVec = Segments.get_post_synapse_sparse_vec(segment)
I, synapseList = findnz(postSynapseSparseVec)
if !isempty(I)
append!(ret, synapseList)
end
append!(ret, synapseList)
end
ret
end
Expand All @@ -1547,6 +1556,30 @@ function get_num_post_synapses(self::Neuron)
end

############################### manipulations ##########################################

"""
attach_pre_synapses!(self::Neuron, synapseList::Vector{Synapse})
"""
function attach_pre_synapses!(self::Neuron, synapseList::Vector{Synapse})
map(x->attach_pre_synapse!(self, x), synapseList)

for segment in self
Segments.adjust_class!(segment)
end
end

"""
attach_post_synapses!(self::Neuron, synapseList::Vector{Synapse})
"""
function attach_post_synapses!(self::Neuron, synapseList::Vector{Synapse})
map(x->attach_pre_synapse!(self, x), synapseList)

for segment in self
Segments.adjust_class!(segment)
end
end

"""
attach_pre_synapses!(self::Neuron, synapseTable::DataFrame)
make sure that the synapse table contains *only* the presynapses of this neuron
Expand Down Expand Up @@ -1683,6 +1716,8 @@ resample distance is 1000.0, then the resample distance is 1000 nm.
function resample(self::Neuron, resampleDistance::Float32)
newSegmentList = Vector{Segment}()
segmentList = get_segment_list(self)
preSynapseList = get_all_pre_synapse_list(self)
postSynapseList = get_all_post_synapse_list(self)
local nodeList::Vector
for (index, segment) in enumerate(segmentList)
parentSegmentId = get_parent_segment_id(self, index)
Expand All @@ -1700,7 +1735,12 @@ function resample(self::Neuron, resampleDistance::Float32)
push!(newSegmentList, newSegment)
end
end
Neuron(newSegmentList, get_connectivity_matrix(self))
neuron = Neuron(newSegmentList, get_connectivity_matrix(self))

# reattach synapses
attach_pre_synapses!(neuron, preSynapseList)
attach_post_synapses!(neuron, postSynapseList)
neuron
end

function resample(nodeList::Vector{NTuple{4,T}}, resampleDistance::T) where T
Expand Down
9 changes: 5 additions & 4 deletions src/Utils/PlotRecipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module PlotRecipes
using RealNeuralNetworks.Neurons
using Colors, ColorSchemes, Clustering
using Plots
using Statistics

import PyPlot
PyPlot.svg(true)


function plot_synapse_distributions( cellList;
synapseDistribution = map(get_synapse_to_soma_path_length_lists, cellList),
saveName = joinpath(homedir(), "synapse_distribution.svg"),
Expand Down Expand Up @@ -123,7 +123,7 @@ function hclustplot(hc::Hclust, useheight::Bool)

y1 = pos[hc.merge[i,1]][2]
y2 = pos[hc.merge[i,2]][2]
useheight ? h = hc.height[i] : h = 1
useheight ? h = hc.heights[i] : h = 1
newy = maximum([y1,y2]) + h
append!(ys, [y1,newy,newy,y2])
end
Expand All @@ -139,9 +139,9 @@ function treepositions(hc::Hclust, useheight::Bool)
for i in 1:size(hc.merge,1)
xpos = mean([positions[hc.merge[i,1]][1], positions[hc.merge[i,2]][1]])
if hc.merge[i,1] < 0 && hc.merge[i,2] < 0
useheight ? ypos = hc.height[i] : ypos = 1
useheight ? ypos = hc.heights[i] : ypos = 1
else
useheight ? h = hc.height[i] : h = 1
useheight ? h = hc.heights[i] : h = 1
ypos = maximum([positions[hc.merge[i,1]][2], positions[hc.merge[i,2]][2]]) + h
end

Expand All @@ -151,6 +151,7 @@ function treepositions(hc::Hclust, useheight::Bool)
end

function plot(clust::Hclust)
#Plots.gr()
Plots.plotly()
Plots.plot(hclustplot(clust, true), seriestype=:path, color=:black,
yaxis=nothing, grid=false, legend=false) #, xticks=classificationIdList[clust.order])
Expand Down
2 changes: 1 addition & 1 deletion test/NBLASTs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ NEURON_ID2 = 77641

println("test data structure transformation...")
NBLASTs.VectorCloud(Neuron(neuron1))
NodeNet(neuron1) |> NBLASTs.VectorCloud
NBLASTs.VectorCloud(neuron1)

# transform to micron
vectorCloud1[1:3, :] ./= 1000
Expand Down

0 comments on commit 6b5d414

Please sign in to comment.