-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
160 lines (137 loc) · 4.23 KB
/
utils.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
'''
Utility python functions for pathology analysis with connection to PostgreSQL
'''
import os
import os.path as op
import pandas as pd
from pgclasses import *
from time import gmtime, strftime
def add_tables_to_sql(path):
List_of_data = []
for roots, dirs, files in os.walk(path):
for f in files:
if f.endswith('_all.txt'):
List_of_data.append((roots, f))
for r, f in List_of_data:
ct = rawtable(op.join(r, f))
try:
ct.add_cell_postgresql()
except:
print("{}: {} commitment failed".format(strftime("%Y-%m-%d %H:%M:%S", gmtime()), f))
try:
ct.close_connection()
except:
pass
def generate_number_of_neighbour(path, save_path, ngh_range=(10,101,10)):
List_of_data = []
for roots, dirs, files in os.walk(path):
for f in files:
if f.endswith('_all.txt'):
List_of_data.append((roots, f))
for r, f in List_of_data:
ct = rawtable(op.join(r, f))
df = ct.calculate_neighbours(ngh_range)
df.to_csv(op.join(save_path, '{}_number_neighbours.csv'.format(ct.sid)),
index=False)
try:
ct.close_connection()
except:
pass
def generate_neighbour_features(path, save_path, max_neighbour=5, radius_list=[10, 20]):
List_of_data = []
for roots, dirs, files in os.walk(path):
for f in files:
if f.endswith('_all.txt'):
List_of_data.append((roots, f))
for r, f in List_of_data:
ct = rawtable(op.join(r, f))
nd = ct.calculate_neighbour_features(max_neighbour, radius_list)
for nns, dfs in nd.items():
dfs.to_csv(op.join(save_path, '{}_{}_neighbour_features.csv'.format(
ct.sid, nns)), index=False)
try:
ct.close_connection()
except:
pass
def add_cortical_ribbons(path):
List_of_data = []
for roots, dirs, files in os.walk(path):
for f in files:
if f.endswith('_GM_anns.json'):
List_of_data.append((roots, f))
for r, f in List_of_data:
ct = regiontable.cortical_ribbons(op.join(r, f))
ct.add_annote()
try:
ct.close_connection()
except:
pass
def add_white_matter(path):
List_of_data = []
for roots, dirs, files in os.walk(path):
for f in files:
if f.endswith('_WM_anns.json'):
List_of_data.append((roots, f))
for r, f in List_of_data:
ct = regiontable.white_matter(op.join(r, f))
ct.add_annote()
try:
ct.close_connection()
except:
pass
def drop_slice_cell_record(sid):
s = sqltable(sid)
s.drop_cell_records()
try:
s.close_connection()
except:
pass
def retrieve_cell_details(sid):
s = sqltable(sid)
df = s.select_cell_records()
try:
s.close_connection()
except:
pass
return df
def gnenerate_ribbon_cells(sid, save_path):
s = sqltable(sid)
df = s.extract_ribbon_cells()
df.to_csv(op.join(save_path, 'ribbon_cells_{}.csv'.format(sid)), index=False)
try:
s.close_connection()
except:
pass
def generate_all_positive_cell(save_path):
l = sqltable.get_list_of_slices()
L = [] # list of results
for sid in l:
s = sqltable(sid)
L.append(s.extract_positive_cell_counts())
pd.concat(L).to_csv(op.join(save_path, 'positive_cells.csv'), index=False)
try:
s.close_connection()
except:
pass
def generate_all_positive_cell_threshold(save_path, threshold=0.20):
l = sqltable.get_list_of_slices()
L = [] # list of results
for sid in l:
s = sqltable(sid)
L.append(s.extract_positive_cell_counts_threshold(threshold))
pd.concat(L).to_csv(op.join(save_path, 'positive_cells_{:.2f}.csv'.format(threshold)), index=False)
try:
s.close_connection()
except:
pass
def generate_all_ribbon_distance(save_path):
l = sqltable.get_list_of_slices()
L = [] # list of results
for sid in l:
s = sqltable(sid)
df = s.cortical_ribbon_distance()
df.to_csv(op.join(save_path, 'cortical_layer_{}.csv'.format(sid)), index=False)
try:
s.close_connection()
except:
pass