Skip to content

Commit

Permalink
Fixing, and simplifying, the SWC reader. This commit fixes a serious …
Browse files Browse the repository at this point in the history
…bug.
  • Loading branch information
marwan-abdellah committed Oct 8, 2018
1 parent 1923364 commit 5dc855b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 77 deletions.
94 changes: 17 additions & 77 deletions neuromorphovis/file/readers/morphology/swc_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,12 @@ def build_connected_paths_from_samples(self):
# @build_sections_from_paths
################################################################################################
def build_sections_from_paths(self):
"""Builds a list of sections from the paths reconstructed during the reading of the
morphology.
"""

for path in self.paths:

print(path)

# A list of all the samples located along the path
samples_located_along_path = list()

Expand All @@ -166,8 +167,6 @@ def build_sections_from_paths(self):
# Order the list
samples_located_along_path = sorted(samples_located_along_path)

print(samples_located_along_path)

# Build the sections
for i in range(0, len(samples_located_along_path) - 1):

Expand All @@ -187,19 +186,8 @@ def build_sections_from_paths(self):

section_indices.append(path[j])

print(section_indices)

self.sections_samples_indices_list.append(section_indices)

print('')








################################################################################################
# @read_samples
################################################################################################
Expand Down Expand Up @@ -256,31 +244,14 @@ def read_samples(self):
# Get the Z-coordinate
z = float(data[nmv.consts.Arbors.SWC_SAMPLE_Z_COORDINATES_IDX])

# Handle samples that are located at the origin
if x < nmv.consts.Math.LITTLE_EPSILON and \
y < nmv.consts.Math.LITTLE_EPSILON and \
z < nmv.consts.Math.LITTLE_EPSILON:
x = 0.01
y = 0.01
z = 0.01

# Get the sample radius
radius = float(data[nmv.consts.Arbors.SWC_SAMPLE_RADIUS_IDX])

# Handle samples with Zero radii
if radius < nmv.consts.Math.LITTLE_EPSILON:

# Report the issue
nmv.logger.log('WARNING: Sample [%d] has a radius of [%f]' % (index, radius))

# Update the radius
radius = 0.1

# Get the sample parent index
parent_index = int(data[nmv.consts.Arbors.SWC_SAMPLE_PARENT_INDEX_IDX])

# If this is the soma sample, get the translation vector
if sample_type == 1 and parent_index == -1:
if parent_index == -1:

translation[0] = x
translation[1] = y
Expand Down Expand Up @@ -464,10 +435,12 @@ def build_soma(self,
################################################################################################
def get_sections_of_specific_type(self,
arbor_type):
"""
"""Returns a list of sections of specific type.
:param arbor_type:
The type of the requested sections.
:return:
A list of all the sections that have specific type.
"""

sections_list = list()
Expand All @@ -478,11 +451,11 @@ def get_sections_of_specific_type(self,
# For each section
for section_samples_indices in self.sections_samples_indices_list:

# Get the last sample along this
# Get the last sample along this section
last_sample = self.samples_list[section_samples_indices[-1]]

# If the type is matching
if last_sample[2] == arbor_type:
if str(last_sample[1]) == str(arbor_type):

# Append to the list
arbor_sections_samples_indices_list.append(section_samples_indices)
Expand All @@ -496,6 +469,10 @@ def get_sections_of_specific_type(self,
# For each sample in the section
for arbor_sample_index in arbor_section:

# Ignore the soma sample
if self.samples_list[arbor_sample_index][0] == 1:
continue

# Get the a nmv sample based on its index
nmv_sample = self.get_nmv_sample_from_samples_list(arbor_sample_index)

Expand All @@ -515,48 +492,11 @@ def get_sections_of_specific_type(self,
section.id = i

# Update the section type
section.type = section_type

# Copy the sections list
sections_list_clone = copy.deepcopy(sections_list)

# Now, link the sections together relying on thr indices of the initial and final samples
for i_section in sections_list:

# Traversal
for j_section in sections_list_clone:

# Ignore processing the same section twice
if i_section.id == j_section.id:

continue

# Root sections
if i_section.samples[0].parent_id == 1:

# Set the parent ID to None
i_section.parent_id = None

# Set the parent to None
i_section.parent = None

# If the index of the first sample of the section is the last of another section
if i_section.samples[0].id == j_section.samples[-1].id:

# Set the parent id to that of the parent section
i_section.parent_id = j_section.id

# Set the reference to the parent id to that of the parent section
i_section.parent = j_section

# If the index of the last sample of the section is that of the first sample of
# another section
if i_section.samples[-1].id == j_section.samples[0].id:
# Append the child section id to the children section IDs
i_section.children_ids.append(j_section.id)
section.type = arbor_type

# Append a reference to the child section as well
i_section.children.append(j_section)
# Updates the sections parenting
for section in sections_list:
nmv.skeleton.ops.update_section_parenting(section, sections_list)

# Return a list of all the disconnected sections
return sections_list
Expand Down
46 changes: 46 additions & 0 deletions neuromorphovis/skeleton/ops/skeleton_construction_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,52 @@
import neuromorphovis as nmv


####################################################################################################
# @update_section_parenting
####################################################################################################
def update_section_parenting(section,
sections_list):
"""Updates the section parents' and children references.
:param section:
A given section to update.
:param sections_list:
A list of all the sections in the morphology.
"""

# Detect if the section has no parent, then set it as a root
# Use the first sample to identify if this section is a root or not
if section.samples[0].parent_id == 1:

# This section is a root
section.parent = None
section.parent_id = None

for i_section in sections_list:

# If this is the same section
if i_section.id == section.id:

# Next section
continue

# If the last sample along the section has the same index of the first sample of the
# auxiliary section, then the auxiliary section is a child
if section.samples[-1].id == i_section.samples[0].id:

# Add the auxiliary section as a child to the parent section
section.children.append(i_section)
section.children_ids.append(i_section.id)

# If the first sample along the section has the same index of the last sample of the
# auxiliary section, then the auxiliary section is a parent
if section.samples[0].id == i_section.samples[-1].id:

# Set the auxiliary section to be a parent to this child section
section.parent = i_section
section.parent_id = i_section.id


####################################################################################################
# @build_arbors_from_sections
####################################################################################################
Expand Down
2 changes: 2 additions & 0 deletions neuromorphovis/skeleton/ops/skeleton_drawing_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ def draw_disconnected_segments(section,
poly_line_data=segment_data, name=segment_name, material=segment_material,
bevel_object=bevel_object)

"""
# Render frame for progressive rendering
if render_frame:
global progressive_frame_index
Expand All @@ -660,6 +661,7 @@ def draw_disconnected_segments(section,
# Increment the progressive frame index
progressive_frame_index += 1
"""

# Add the created segments to the list
segments_objects.append(created_segments)
Expand Down

0 comments on commit 5dc855b

Please sign in to comment.