-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_info.py
148 lines (109 loc) · 4.96 KB
/
db_info.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 23 16:59:45 2017
@author: ajaver
"""
import os
import json
import tables
import numpy as np
import pymysql.cursors
import pytz
from collections import OrderedDict
from tierpsy.helper.params import copy_unit_conversions, read_microns_per_pixel
from tierpsy.analysis.stage_aligment.alignStageMotion import _h_add_stage_position_pix, isGoodStageAligment
def db_row2dict(row):
experiment_info = OrderedDict()
experiment_info['base_name'] = row['base_name']
if not 'experimenter' in row:
experiment_info['who'] = 'Laura Grundy'
else:
experiment_info['who'] = row['experimenter']
if not 'lab' in row:
#I hard code the lab for the moment since all this data base is for the single worm case, and all the data was taken from the schafer lab.
#if at certain point we add another lab we need to add an extra table in the main database
experiment_info['lab'] = {'name' : 'William R Schafer',
'location':'MRC Laboratory of Molecular Biology, Hills Road, Cambridge, CB2 0QH, UK'}
else:
experiment_info['lab'] = row['lab']
#add timestamp with timezone
local = pytz.timezone ('Europe/London')
local_dt = local.localize(row['date'], is_dst=True)
experiment_info['timestamp'] = local_dt.isoformat()
experiment_info['arena'] = {
"style":'petri',
"size":35,
"orientation":"away"
}
if 'NGM liquid drop' in row['arena']:
media = "NGM liquid drop + NGM agar low peptone"
else:
media = "NGM agar low peptone"
experiment_info['media'] = media
experiment_info['food'] = row['food']
experiment_info['strain'] = row['strain']
experiment_info['gene'] = row['gene']
experiment_info['allele'] = row['allele']
experiment_info['chromosome'] = row['chromosome']
experiment_info['strain_description'] = row['strain_description']
experiment_info['sex'] = row['sex']
experiment_info['stage'] = row['developmental_stage']
if experiment_info['stage'] == "young adult":
experiment_info['stage'] = 'adult'
experiment_info['ventral_side'] = row['ventral_side']
if row['habituation'] == 'NONE':
hab = "no wait before recording starts."
else:
hab = "worm transferred to arena 30 minutes before recording starts."
experiment_info['protocol'] = [
"method in E. Yemini et al. doi:10.1038/nmeth.2560",
hab
]
experiment_info['habituation'] = row['habituation']
experiment_info['tracker'] = row['tracker']
experiment_info['original_video_name'] = row['original_video']
return experiment_info
def get_exp_info_str(cur, base_name):
cur.execute("SELECT * FROM experiments_full WHERE base_name='{}';".format(base_name))
row = cur.fetchone()
experiment_info = db_row2dict(row)
experiment_info_str = bytes(json.dumps(experiment_info), 'utf-8')
return experiment_info_str
def add_extra_info(cur, base_name, results_dir):
valid_fields = ['/mask', '/trajectories_data', '/features_timeseries']
mask_file = os.path.join(results_dir, base_name + '.hdf5')
skeletons_file = os.path.join(results_dir, base_name + '_skeletons.hdf5')
features_file = os.path.join(results_dir, base_name + '_features.hdf5')
def _add_exp_data(fname, experiment_info_str):
with tables.File(fname, 'r+') as fid:
if os.path.exists(skeletons_file):
group_to_save = fid.get_node(field)
copy_unit_conversions(group_to_save, skeletons_file)
if '/experiment_info' in fid:
fid.remove_node('/', 'experiment_info')
fid.create_array('/', 'experiment_info', obj = experiment_info_str)
experiment_info_str = get_exp_info_str(cur, base_name)
fnames = [mask_file, skeletons_file, features_file]
for fname, field in zip(fnames, valid_fields):
if os.path.exists(fname):
_add_exp_data(fname, experiment_info_str)
#finally if the stage was aligned correctly add the information into the mask file
if os.path.exists(mask_file) and isGoodStageAligment(skeletons_file):
_h_add_stage_position_pix(mask_file, skeletons_file)
if __name__ == '__main__':
video_dir_root = '/Volumes/behavgenom_archive$/single_worm/MaskedVideos'
ONLY_UNFINSHED = True
conn = pymysql.connect(host='localhost')
cur = conn.cursor(pymysql.cursors.DictCursor)
cur.execute('USE `single_worm_db`;')
sql = '''
SELECT base_name, results_dir
FROM experiments
WHERE exit_flag_id > (SELECT f.id FROM exit_flags as f WHERE name="COMPRESS")
'''
cur.execute(sql)
valid_files = cur.fetchall()
for ii, row in enumerate(valid_files):
print('{} of {} : {}'.format(ii+1, len(valid_files), row['base_name']))
add_extra_info(cur, **row)