-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_features.py
238 lines (183 loc) · 6.11 KB
/
group_features.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
import numpy as np
import json
import copy
dataset = '20210722'
def filter_synapses(features, feature_name):
'''Filter out all synapses that do not have the requested feature.'''
if feature_name.startswith('vesicle'):
features_filtered = [
synapse
for synapse in features
if synapse[feature_name]
]
else:
features_filtered = [
synapse
for synapse in features
if synapse[feature_name] is not None
]
return features_filtered
def get_duplicate_synapse_ids(features):
'''Return a set of synapse IDs that have been annotated more than once.'''
duplicate_sets = {}
duplicate_synapse_ids = set({})
for i, synapse in enumerate(features):
synapse_id = synapse['synapse_id']
if synapse_id in duplicate_sets:
duplicate_sets[synapse_id].append(i)
else:
duplicate_sets[synapse_id] = [i]
for synapse_id in duplicate_sets.keys():
if len(duplicate_sets[synapse_id]) > 1:
duplicate_synapse_ids.add(synapse_id)
return duplicate_synapse_ids
def group_features(features, feature_name, group_condition):
'''Group features with a given name by the given condition.'''
# make sure to not use undefined features
features = filter_synapses(features, feature_name)
grouping_keys = [
{
'by_annotators': 'annotator',
'by_nt_types': 'neurotransmitter'
}[c]
for c in group_condition
]
# list of features for vesicles, instead of a single number
if feature_name.startswith('vesicle'):
feature_values = [
value
for synapse in features
for value in synapse[feature_name]
]
conditions = [
condition
for synapse in features
for condition in list(
((tuple([synapse[key] for key in grouping_keys]),)
*len(synapse[feature_name]) )
)
]
else:
feature_values = [synapse[feature_name] for synapse in features]
conditions = []
for synapse in features:
condition = []
for key in grouping_keys:
condition.append(synapse[key])
conditions.append(tuple(condition))
#for key in grouping_keys:
# for synapse in features:
# condition = tuple([synapse[key]])
# conditions.append(tuple(
# [synapse[key]]
# ))
#conditions = [
# tuple([synapse[key]
# for synapse in features
# for key in grouping_keys])
#]
grouped_features = {}
for condition, feature_value in zip(conditions, feature_values):
if condition not in grouped_features:
grouped_features[condition] = []
grouped_features[condition].append(feature_value)
# print('grouped_features: ', '\n', f'{grouped_features}')
return grouped_features
def group_features_by_conditions(condition, filter='unique'):
'''
Group synapse features by different conditions.
Args:
condition (tuple of strings):
Possible values are "by_nt_types" and "by_annotators".
filter (string, optional):
Possible values are "unique", "same", "all":
"unique": All synapses with duplicate_number 1 (default).
"same": Only synapses that have been annotated by at least two
annotators. Results will be grouped by pairs of annotators.
"all": All synpases (including duplicates).
Returns a dictionary that looks like:
```
{
<feature_name>: {
<condidation_1>: ...,
<condidation_2>: ...,
<condidation_3>: ...
...
},
# ... more features
}
```
Examples:
```
group_features_by_conditions(('by_annotators',))
```
Returns:
```
{
'vesicle_sizes': {
('c0',): [....],
('c1',): [....],
('c2',): [....]
}
}
```
```
group_features_by_conditions(('by_annotators', 'by_nt_types'))
```
Returns:
```
... {
'vesicle_sizes': {
('c0', 'glutamate'): [....],
('c0', 'gaba'): [....],
...
('c1', 'glutamate'): [....],
('c1', 'gaba'): [....]
...
}
}
```
Features should have been extracted earlier with `./extract_features.py`,
which puts them into <synapse_features_{dataset}.json>.
This function can be used in a jupyter notebook:
```
from group_features import group_features_by_conditions
features = group_features_by_conditions(('by_nt_types',))
```
'''
# handle full features
with open(f"synapse_features_{dataset}.json", 'r') as f:
features = json.load(f)
feature_names = ['cleft_mean_intensity', 't-bars_mean_intensity',
't-bars_mean_intensity', 'cleft_median_intensity',
't-bars_median_intensity', 't-bars_mean_normalized_intensity',
'cleft_mean_normalized_intensity',
't-bars_median_normalized_intensity',
'cleft_median_normalized_intensity',
'post_count', 'num_vesicles',
'vesicle_sizes', 'vesicle_eccentricities']
# filter based on duplicate numbers
if filter == 'unique':
features = [
f
for f in features
if f['duplicate_number'] == 1
]
elif filter == 'same':
duplicate_synapse_ids = get_duplicate_synapse_ids(features)
features = [
f
for f in features
if f['synapse_id'] in duplicate_synapse_ids
]
elif filter == 'all':
pass
else:
raise RuntimeError("'filter' should be 'unique', 'same', or 'all'")
for c in condition:
assert c in ['by_nt_types', 'by_annotators']
grouped_features = {
feature_name: group_features(features, feature_name, condition)
for feature_name in feature_names
}
return grouped_features