Skip to content

Commit

Permalink
some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaPandeyCN committed Aug 21, 2024
1 parent 6d98ec2 commit ad92502
Showing 1 changed file with 67 additions and 50 deletions.
117 changes: 67 additions & 50 deletions pyneuroml/swc/ExportNML.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
import os
import sys
import tempfile
from typing import Dict, List

Expand All @@ -16,13 +18,21 @@

from .LoadSWC import SWCGraph, SWCNode, load_swc

logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s",
filename="neuroml_conversion.log",
filemode="w",
)
current_dir = os.path.dirname(os.path.abspath(__file__))

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)
fh = logging.FileHandler("neuroml_conversion.log")
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)


class NeuroMLWriter:
Expand Down Expand Up @@ -172,7 +182,10 @@ def parse_tree(
:param this_point: The current point being processed.
:type this_point: SWCNode
"""
if this_point.id in self.processed_nodes:
if (
this_point.id in self.processed_nodes
and this_point.id not in self.second_points_of_new_types
):
logger.debug(f"Point {this_point.id} already processed, skipping")
return

Expand All @@ -185,7 +198,7 @@ def parse_tree(
self.handle_soma(this_point, parent_point)
else:
if this_point.id not in self.second_points_of_new_types:
logger.debug(f"Creating segment for point {this_point.id}")
logger.debug(f"Processing non-soma point: {this_point.id}")
self.create_segment(this_point, parent_point, new_branch or type_change)
self.processed_nodes.add(this_point.id)
else:
Expand Down Expand Up @@ -290,61 +303,63 @@ def handle_soma(
self.add_segment_to_groups(self.next_segment_id, SWCNode.SOMA)
self.next_segment_id += 1

else:
elif len(soma_points) > 3:
logger.debug(f"Processing multi-point soma with {len(soma_points)} points")
sorted_soma_points = sorted(soma_points, key=lambda p: p.x)

if this_point == sorted_soma_points[0]:
logger.debug("Processing multi-point soma")

for i, current_point in enumerate(sorted_soma_points):
if current_point.id not in self.processed_nodes:
segment = Segment(
id=self.next_segment_id,
name=f"soma_Seg_{self.next_segment_id}",
segment = Segment(
id=self.next_segment_id,
name=f"soma_Seg_{self.next_segment_id}",
)

if i == 0:
segment.proximal = Point3DWithDiam(
x=current_point.x,
y=current_point.y,
z=current_point.z,
diameter=2 * current_point.radius,
)
else:
segment.parent = SegmentParent(
segments=self.next_segment_id - 1
)

if i == 0:
segment.proximal = Point3DWithDiam(
x=current_point.x,
y=current_point.y,
z=current_point.z,
diameter=2 * current_point.radius,
)
else:
segment.parent = SegmentParent(
segments=self.next_segment_id - 1
)

if i < len(sorted_soma_points) - 1:
next_point = sorted_soma_points[i + 1]
segment.distal = Point3DWithDiam(
x=next_point.x,
y=next_point.y,
z=next_point.z,
diameter=2 * next_point.radius,
)
else:
segment.distal = Point3DWithDiam(
x=current_point.x,
y=current_point.y,
z=current_point.z,
diameter=2 * current_point.radius,
)

self.cell.morphology.segments.append(segment)
self.point_indices_vs_seg_ids[current_point.id] = (
self.next_segment_id
if i < len(sorted_soma_points) - 1:
next_point = sorted_soma_points[i + 1]
segment.distal = Point3DWithDiam(
x=next_point.x,
y=next_point.y,
z=next_point.z,
diameter=2 * next_point.radius,
)
else:
segment.distal = Point3DWithDiam(
x=current_point.x,
y=current_point.y,
z=current_point.z,
diameter=2 * current_point.radius,
)
self.segment_types[self.next_segment_id] = SWCNode.SOMA
self.add_segment_to_groups(self.next_segment_id, SWCNode.SOMA)
self.processed_nodes.add(current_point.id)

self.next_segment_id += 1
self.cell.morphology.segments.append(segment)
self.point_indices_vs_seg_ids[current_point.id] = (
self.next_segment_id
)
self.segment_types[self.next_segment_id] = SWCNode.SOMA
self.add_segment_to_groups(self.next_segment_id, SWCNode.SOMA)
self.processed_nodes.add(current_point.id)

elif this_point != sorted_soma_points[0]:
self.next_segment_id += 1

else:
logger.debug(f"Soma point {this_point.id} not the first, skipping")

self.processed_nodes.add(this_point.id)
logger.debug(f"Finished handling soma point: {this_point.id}")
logger.debug(f"Processed nodes after soma: {self.processed_nodes}")
logger.debug(f"Total segments created so far: {self.next_segment_id}")

def create_segment(
self,
Expand Down Expand Up @@ -388,6 +403,7 @@ def create_segment(
)
self.second_points_of_new_types.add(second_point.id)

self.point_indices_vs_seg_ids[second_point.id] = seg_id
if parent_point.id in self.point_indices_vs_seg_ids:
parent_seg_id = self.point_indices_vs_seg_ids[parent_point.id]
segment.parent = SegmentParent(segments=parent_seg_id)
Expand Down Expand Up @@ -417,6 +433,7 @@ def create_segment(
)

elif is_branch_point:
logger.debug("Setting proximal and distal for branch point")
segment.proximal = Point3DWithDiam(
x=parent_point.x,
y=parent_point.y,
Expand Down

0 comments on commit ad92502

Please sign in to comment.