-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest_synapse_schema.py
205 lines (163 loc) · 6.31 KB
/
test_synapse_schema.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
import marshmallow as mm
import pytest
from emannotationschemas import get_flat_schema
from emannotationschemas.flatten import flatten_dict
from emannotationschemas.schemas.synapse import (
BaseSynapseSchema,
BuhmannEcksteinSynapseSchema,
BuhmannSynapseSchema,
NoCleftSynapse,
PlasticSynapse,
SynapseSchema,
)
good_base_synapse = {
"pre_pt": {"position": [31, 31, 0], "supervoxel_id": 95, "root_id": 4},
"post_pt": {"position": [33, 33, 0], "supervoxel_id": 101, "root_id": 5},
}
good_no_cleft_synapse = {
'pre_pt': {'position': [31, 31, 0], 'supervoxel_id': 95, 'root_id': 4},
'post_pt': {'position': [33, 33, 0], 'supervoxel_id': 101, 'root_id': 5},
'score': 50
}
good_synapse = {
"pre_pt": {"position": [31, 31, 0]},
"ctr_pt": {"position": [32, 32, 0]},
"post_pt": {"position": [33, 33, 0]},
}
incomplete_type = {
"pre_pt": {"position": [31, 31, 0]},
"ctr_pt": {"position": [32, 32, 0]},
"post_pt": {"position": [33, 33, 0]},
}
supervoxel_synapse = {
"pre_pt": {"position": [31, 31, 0], "supervoxel_id": 95},
"ctr_pt": {"position": [32, 32, 0]},
"post_pt": {"position": [33, 33, 0], "supervoxel_id": 101},
}
supervoxel_rootId_synapse = {
"pre_pt": {"position": [31, 31, 0], "supervoxel_id": 95, "root_id": 4},
"ctr_pt": {"position": [32, 32, 0]},
"post_pt": {"position": [33, 33, 0], "supervoxel_id": 101, "root_id": 5},
}
supervoxel_rootId_invalid_synapse = {
"pre_pt": {"position": [31, 31, 0], "supervoxel_id": 95, "root_id": 5},
"ctr_pt": {"position": [32, 32, 0], "supervoxel_id": 105, "root_id": 5},
"post_pt": {"position": [33, 33, 0], "supervoxel_id": 101, "root_id": 5},
}
good_buhmann_synapse = {
"pre_pt": {"position": [31, 31, 0], "supervoxel_id": 95, "root_id": 4},
"post_pt": {"position": [33, 33, 0], "supervoxel_id": 101, "root_id": 5},
"connection_score": 1.32,
"cleft_score": 1.0,
}
good_buhmann_eckstein_synapse = {
"pre_pt": {"position": [31, 31, 0], "supervoxel_id": 95, "root_id": 4},
"post_pt": {"position": [33, 33, 0], "supervoxel_id": 101, "root_id": 5},
"connection_score": 1.32,
"cleft_score": 1.0,
"gaba": 0.8,
"ach": 0.0,
"glut": 0.1,
"oct": 0.1,
"ser": 0.0,
"da": 0.2,
"valid_nt": True,
}
good_plastic_synapse = {
"pre_pt": {"position": [31, 31, 0]},
"ctr_pt": {"position": [32, 32, 0]},
"post_pt": {"position": [33, 33, 0]},
"plasticity": 1,
}
# class PlasticSynapse(SynapseSchema):
# plasticity = mm.fields.Int(required=True,
# validate=mm.validate.OneOf([0, 1, 2, 3, 4]),
# description="plasticity state 0:not synapse 1:normal 2:mild 3:strong 4:not rated")
def annotation_import(item):
item["supervoxel_id"] = 5
item.pop("rootId", None)
def test_base_synapse_schema():
schema = BaseSynapseSchema()
result = schema.load(good_base_synapse)
assert result["pre_pt"]["position"] == [31, 31, 0]
assert result["pre_pt"]["supervoxel_id"] == 95
assert result["pre_pt"]["root_id"] == 4
assert result["post_pt"]["position"] == [33, 33, 0]
assert result["post_pt"]["supervoxel_id"] == 101
assert result["post_pt"]["root_id"] == 5
assert "rootId" not in result["pre_pt"].keys()
def test_no_cleft_synapse_schema():
schema = NoCleftSynapse()
result = schema.load(good_no_cleft_synapse)
assert result["pre_pt"]["position"] == [31, 31, 0]
assert result["pre_pt"]["supervoxel_id"] == 95
assert result["pre_pt"]["root_id"] == 4
assert result["post_pt"]["position"] == [33, 33, 0]
assert result["post_pt"]["supervoxel_id"] == 101
assert result["post_pt"]["root_id"] == 5
assert result["score"] == 50.0
assert "rootId" not in result["pre_pt"].keys()
def test_buhmann_synapse_schema():
schema = BuhmannSynapseSchema()
result = schema.load(good_buhmann_synapse)
assert result["pre_pt"]["position"] == [31, 31, 0]
assert result["pre_pt"]["supervoxel_id"] == 95
assert result["cleft_score"] == 1.0
assert result["connection_score"] == 1.32
assert "rootId" not in result["pre_pt"].keys()
def test_buhmann_eckstein_synapse_schema():
schema = BuhmannEcksteinSynapseSchema()
result = schema.load(good_buhmann_eckstein_synapse)
assert result["pre_pt"]["position"] == [31, 31, 0]
assert result["pre_pt"]["supervoxel_id"] == 95
assert result["cleft_score"] == 1.0
assert result["gaba"] == 0.8
assert result["ach"] == 0.0
assert result["glut"] == 0.1
assert result["oct"] == 0.1
assert result["ser"] == 0.0
assert result["da"] == 0.2
assert result["valid_nt"] == True
assert "rootId" not in result["pre_pt"].keys()
def test_synapse_validation():
schema = SynapseSchema(context={"bsp_fn": annotation_import})
result = schema.load(good_synapse)
assert result["pre_pt"]["supervoxel_id"] == 5
schema.validate(result)
result = schema.load(supervoxel_synapse)
assert result["pre_pt"]["supervoxel_id"] == 5
result = schema.load(supervoxel_rootId_synapse)
assert result["pre_pt"]["supervoxel_id"] == 5
assert "rootId" not in result["pre_pt"].keys()
def test_plastic_synapse_schema():
schema = PlasticSynapse()
result = schema.load(good_plastic_synapse)
assert result["plasticity"] == 1
def test_synapse_flatten():
schema = SynapseSchema()
result = schema.load(good_synapse)
d = flatten_dict(result)
assert d["pre_pt_position"] == [31, 31, 0]
result = schema.load(supervoxel_synapse)
assert d["pre_pt_position"] == [31, 31, 0]
result = schema.load(supervoxel_rootId_synapse)
assert d["pre_pt_position"] == [31, 31, 0]
FlatSynapseSchema = get_flat_schema("synapse")
schema = FlatSynapseSchema()
result = schema.load(d)
assert len(result) == 8
def test_synapse_postgis():
schema = SynapseSchema(context={"postgis": True})
result = schema.load(good_synapse)
d = flatten_dict(result)
assert d["pre_pt_position"] == "POINTZ(31 31 0)"
def test_synapse_validity():
schema = SynapseSchema()
result = schema.load(supervoxel_rootId_synapse)
print("valid test", result)
assert result["valid"]
result = schema.load(good_synapse)
def test_synapse_invalid():
schema = SynapseSchema()
with pytest.raises(mm.ValidationError):
result = schema.load(supervoxel_rootId_invalid_synapse)