Skip to content

Commit

Permalink
Merge pull request #397 from Hjorthmedh/region_mesh_redux
Browse files Browse the repository at this point in the history
Added parameters to config file
  • Loading branch information
Hjorthmedh authored Oct 26, 2023
2 parents 26b96df + 03ed349 commit 4f9e989
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 224 deletions.
231 changes: 18 additions & 213 deletions examples/notebooks/bend_morphologies.ipynb

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions snudda/init/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,10 @@ def add_neurons(self, name,
model_type="neuron",
volume_id=None,
rotation_mode="random",
stay_inside=False):
stay_inside=False,
k_dist=30e-6,
n_random=5,
max_angle=0.1):

if num_neurons <= 0:
return
Expand Down Expand Up @@ -571,7 +574,7 @@ def add_neurons(self, name,
cell_data["axonConfig"] = axon_config

if stay_inside:
cell_data["stayInsideMesh"] = True
cell_data["stayInsideMesh"] = {"k_dist": k_dist, "n_random": n_random, "max_angle": max_angle}

self.network_data["Neurons"][unique_name] = cell_data

Expand Down
17 changes: 12 additions & 5 deletions snudda/place/bend_morphologies.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def check_if_inside(self, morphology: NeuronMorphologyExtended):

return inside_flag

def bend_morphology(self, morphology: NeuronMorphologyExtended, k=30e-6, n_random=5, random_seed=None):
def bend_morphology(self, morphology: NeuronMorphologyExtended,
k_dist=30e-6, max_angle=0.1, # angle in radians
n_random=5, random_seed=None):

# k -- how early will the neuron start bending when it approaches the border

Expand Down Expand Up @@ -93,7 +95,7 @@ def bend_morphology(self, morphology: NeuronMorphologyExtended, k=30e-6, n_rando
# Parent has not moved, so use stored original distance
dist = section_dist[idx]

P_move = 1 / (1 + np.exp(-dist/k))
P_move = 1 / (1 + np.exp(-dist/k_dist))

# Cache the random numbers for segments in the section...
if dist > parent_dist and rng.uniform() < P_move:
Expand All @@ -103,7 +105,7 @@ def bend_morphology(self, morphology: NeuronMorphologyExtended, k=30e-6, n_rando

# We need to randomize new rotation matrix
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.transform.Rotation.html
angles = rng.uniform(size=(n_random, 3), low=-0.1, high=0.1) # Angles in radians
angles = rng.uniform(size=(n_random, 3), low=-max_angle, high=max_angle) # Angles in radians
avoidance_rotations = Rotation.from_euler(seq="XYZ", angles=angles)

for idx2, av_rot in enumerate(avoidance_rotations):
Expand Down Expand Up @@ -342,11 +344,16 @@ def write_swc(self, morphology: MorphologyData, output_file, comment=None):

print(f"Wrote {output_file}")

def edge_avoiding_morphology(self, swc_file, new_file, original_position, original_rotation, random_seed=None):
def edge_avoiding_morphology(self, swc_file, new_file, original_position, original_rotation,
k_dist=30e-6, max_angle=0.1, n_random=5,
random_seed=None):

md = MorphologyData(swc_file=swc_file)
md.place(rotation=original_rotation, position=original_position)
rot_rep, morphology_changed = self.bend_morphology(md, random_seed=random_seed)
rot_rep, morphology_changed = self.bend_morphology(md,
k_dist=k_dist, max_angle=max_angle,
n_random=n_random,
random_seed=random_seed)

if morphology_changed:
new_coord = self.apply_rotation(md, rot_rep)
Expand Down
32 changes: 28 additions & 4 deletions snudda/place/place.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,31 @@ def avoid_edges_parallel(self):
if "stayInsideMesh" in config and config["stayInsideMesh"]:
volume_id = config["volumeID"]
mesh_file = self.config["Volume"][volume_id]["meshFile"]

if type(config["stayInsideMesh"]) in (dict, OrderedDict):
if "k_dist" in config["stayInsideMesh"]:
k_dist = config["stayInsideMesh"]["k_dist"]
else:
k_dist = 30e-6

if "n_random" in config["stayInsideMesh"]:
n_random = config["stayInsideMesh"]["n_random"]
else:
n_random = 5

if "max_angle" in config["stayInsideMesh"]:
max_angle = config["stayInsideMesh"]["max_angle"]
else:
max_angle = 0.1 # radians
else:
k_dist = 30e-6
n_random = 5
max_angle = 0.1 # radians

bend_neuron_info.append((neuron.neuron_id, neuron.name, neuron.swc_filename,
neuron.position, neuron.rotation,
neuron_random_seed[neuron.neuron_id],
volume_id, mesh_file))
volume_id, mesh_file, k_dist, n_random, max_angle))

bend_morph_path = os.path.join(self.network_path, "modified_morphologies")

Expand Down Expand Up @@ -625,8 +646,8 @@ def avoid_edges_helper(bend_neuron_info, network_path):

modified_morphologies = []

for neuron_id, neuron_name, swc_filename, position, rotation, random_seed, volume_id, mesh_file \
in bend_neuron_info:
for neuron_id, neuron_name, swc_filename, position, rotation, random_seed, volume_id, mesh_file,\
k_dist, n_random, max_angle in bend_neuron_info:

if volume_id not in bend_morph:
bend_morph[volume_id] = BendMorphologies(region_mesh=mesh_file, rng=None)
Expand All @@ -637,7 +658,10 @@ def avoid_edges_helper(bend_neuron_info, network_path):
new_file=new_morph_name,
original_position=position,
original_rotation=rotation,
random_seed=random_seed)
random_seed=random_seed,
k_dist=k_dist,
n_random=n_random,
max_angle=max_angle)

if new_morphology:
modified_morphologies.append((neuron_id, new_morphology))
Expand Down

0 comments on commit 4f9e989

Please sign in to comment.