-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnectivity_tool.py
256 lines (192 loc) · 8.71 KB
/
connectivity_tool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
"""
This file contains the classes for Connectivity Tool
"""
# blender imports
import bpy
from bpy.props import BoolProperty, CollectionProperty, EnumProperty, \
FloatProperty, FloatVectorProperty, IntProperty, \
IntVectorProperty, PointerProperty, StringProperty
import mathutils
# python imports
import re
import neuropil_tools
import cellblender
# Spine Head Analyzer Operators:
class CONNECTIVITY_OT_show_synaptic_partner(bpy.types.Operator):
bl_idname = "connectivity_tool.show_synaptic_partner"
bl_label = "Show Axon and Dendrite for Selected PSD or AZ"
bl_description = "Show Axon and Dendrite for Selected PSD or AZ"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
context.object.connectivity.show_synaptic_partner(context)
return {'FINISHED'}
class CONNECTIVITY_OT_output_pre_to_post(bpy.types.Operator):
bl_idname = "connectivity_tool.output_pre_to_post"
bl_label = "Output Connectivity Data"
bl_description = "Output Connectivity Data"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
outFile = open('pre_to_post_connectivity.txt', 'wt')
# outFile.write('# axon sy dendrite\n')
axon_filter = 'a[0-9]{3}$'
axons = [obj.name for obj in context.scene.objects if re.match(axon_filter,obj.name) != None]
axons.sort()
for axon in axons:
obj = context.scene.objects[axon]
obj.connectivity.output_pre_to_post(context,obj,outFile)
outFile.close()
return {'FINISHED'}
class CONNECTIVITY_OT_output_post_to_pre(bpy.types.Operator):
bl_idname = "connectivity_tool.output_post_to_pre"
bl_label = "Output Connectivity Data"
bl_description = "Output Connectivity Data"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
outFile = open('post_to_pre_connectivity.txt', 'wt')
# outFile.write('# dendrite sy axon\n')
dend_filter = 'd[0-9]{3}$'
dends = [obj.name for obj in context.scene.objects if re.match(dend_filter,obj.name) != None]
dends.sort()
for dend in dends:
obj = context.scene.objects[dend]
obj.connectivity.output_post_to_pre(context,obj,outFile)
outFile.close()
return {'FINISHED'}
# Connectivity Tool Panel:
class CONNECTIVITY_UL_psd_draw_item(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data,
active_propname, index):
self.filter_name = "*sy*"
active_obj = context.active_object
layout.label(text=item.name)
class CONNECTIVITY_PT_ConnectivityTool(bpy.types.Panel):
bl_label = "Connectivity Tool"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_options = {'DEFAULT_CLOSED'}
bl_category = "Neuropil Tools"
def draw(self, context):
if context.object != None:
context.object.connectivity.draw_panel(context, panel=self)
# Connectivity Tool Properties:
class ConnectivityToolObjectProperty(bpy.types.PropertyGroup):
active_psd_region_index: IntProperty(name="Active PSD Index", default=0)
def get_active_psd(self,context):
active_obj = context.active_object
reg_list = active_obj.mcell.regions.region_list
sy_list = [reg.name for reg in reg_list if reg.name.rfind('sy') > -1]
psd_region_name = None
if len(sy_list) > 0:
psd_region_name = active_obj.mcell.regions.region_list[self.active_psd_region_index].name
return(psd_region_name)
def get_dendrite(self,context,psd_region_name):
scn = context.scene
dend_filter = 'd[0-9]{3}'
m = re.match(dend_filter,psd_region_name)
if m != None:
dend_name = m.group(0)
return (scn.objects[dend_name])
def get_axon(self,context,psd_region_name):
scn = context.scene
axon_filter = 'a[0-9]{3}$'
axon_objs = [obj for obj in scn.objects if re.match(axon_filter,obj.name) != None]
for obj in axon_objs:
reg_list = obj.mcell.regions.region_list
if reg_list.get(psd_region_name) != None:
return(obj)
def show_synaptic_partner(self,context):
psd_region_name = self.get_active_psd(context)
if self.is_dendrite(context):
self.show_axon(context,psd_region_name)
else:
self.show_dendrite(context,psd_region_name)
def show_dendrite(self,context,psd_region_name):
dend_obj = self.get_dendrite(context,psd_region_name)
if (dend_obj != None):
dend_obj.hide = False
def show_axon(self,context,psd_region_name):
axon_obj = self.get_axon(context,psd_region_name)
if (axon_obj != None):
axon_obj.hide = False
def is_dendrite(self,context):
active_obj = context.active_object
dend_filter = 'd[0-9]{3}$'
if re.match(dend_filter,active_obj.name) != None:
return (True)
else:
return (False)
def is_axon(self,context):
active_obj = context.active_object
axon_filter = 'a[0-9]{3}$'
if re.match(axon_filter,active_obj.name) != None:
return (True)
else:
return (False)
def output_pre_to_post(self,context,axon_obj,file):
reg_list = axon_obj.mcell.regions.region_list
sy_list = [reg.name for reg in reg_list if reg.name.rfind('sy') > -1]
for sy in sy_list:
dend_obj = self.get_dendrite(context,sy)
file.write('%s %s %s\n' % (axon_obj.name, sy, dend_obj.name))
def output_post_to_pre(self,context,dend_obj,file):
reg_list = dend_obj.mcell.regions.region_list
sy_list = [reg.name for reg in reg_list if reg.name.rfind('sy') > -1]
for sy in sy_list:
axon_obj = self.get_axon(context,sy)
file.write('%s %s %s\n' % (dend_obj.name, sy, axon_obj.name))
def draw_panel(self, context, panel):
layout = panel.layout
active_obj = context.active_object
if (active_obj != None) and (active_obj.type == 'MESH'):
sy_text = 'Synaptic Regions:'
if self.is_dendrite(context):
sy_text = 'Synaptic PSD Regions:'
elif self.is_axon(context):
sy_text = 'Synaptic Active Zone Regions:'
row = layout.row()
row.label(text=sy_text, icon='MESH_ICOSPHERE')
row = layout.row()
# Layout active_object.mcell.regions.region_list with filter "*sy*" and active_psd_region_index
row.template_list("CONNECTIVITY_UL_psd_draw_item","spine_psd_list",
active_obj.mcell.regions, "region_list",
self, "active_psd_region_index",
rows=2)
row = layout.row()
row.operator("connectivity_tool.output_pre_to_post", text="Output Pre to Post")
row = layout.row()
row.operator("connectivity_tool.output_post_to_pre", text="Output Post to Pre")
psd_region_name = self.get_active_psd(context)
if psd_region_name != None:
row = layout.row()
row.operator("connectivity_tool.show_synaptic_partner", text="Show Synaptic Partner")
classes = (
CONNECTIVITY_OT_show_synaptic_partner,
CONNECTIVITY_OT_output_pre_to_post,
CONNECTIVITY_OT_output_post_to_pre,
CONNECTIVITY_UL_psd_draw_item,
CONNECTIVITY_PT_ConnectivityTool,
ConnectivityToolObjectProperty,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)