forked from CAVEconnectome/EMAnnotationSchemas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_sqalchemy_script.py
59 lines (52 loc) · 1.7 KB
/
example_sqalchemy_script.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
from emannotationschemas.models import make_annotation_model, Base
from emannotationschemas.flatten import flatten_dict
from emannotationschemas import get_schema
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
# example of initializing mapping of database
DATABASE_URI = "postgres://postgres:synapsedb@localhost:5432/minnie"
engine = create_engine(DATABASE_URI, echo=True)
model_dict = make_annotation_model('test', 'synapse')
# assures that all the tables are created
# would be done as a db management task in general
Base.metadata.create_all(engine)
# create a session class
# this will produce session objects to manage a single transaction
Session = sessionmaker(bind=engine)
# some example test data as a raw json compatible blob
synapse_d = {
"type": "synapse",
"pre_pt":
{
"position": [5, 5, 10],
"root_id": 9223372036854775899,
"supervoxel_id": 89851029364932800
},
"post_pt":
{
"position": [10, 5, 10],
"root_id": 9223372036854775898,
"supervoxel_id": 106205165316472881
},
"ctr_pt": {
"position": [7, 5, 10]
},
"size": 40.5
}
# get the schema to deserialize the test data
SynapseSchema = get_schema('synapse')
schema = SynapseSchema(context={'postgis': True})
# use the schema to deserialize the schema
d = schema.load(synapse_d)
d = flatten_dict(d)
# get the appropriate sqlalchemy model
# for the annotation type and dataset
SynapseModel = model_dict['test']['synapse']
# # create a new model instance with data
synapse = SynapseModel(**d)
# # create a new db session
session = Session()
# add this synapse to database
session.add(synapse)
# commit this transaction to database
session.commit()