forked from IBM/CryoRL-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryoEM_object.py
204 lines (160 loc) · 6.76 KB
/
cryoEM_object.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
import numpy as np
from cryoEM_config import CryoEMConfig
class CryoEMObject:
def __init__(self, name='', idx=-1, parent_idx=-1, container=[]):
assert type(container) == list
self._name = name
self._idx = idx
self._parent_idx=parent_idx
self._container = container
def __getitem__(self, idx):
return self._container[idx]
def __len__(self):
return len(self._container)
@property
def name(self):
return self._name
@property
def idx(self):
return self._idx
@property
def parent_idx(self):
return self._parent_idx
@property
def container(self):
return self._container
class CTFValue:
def __init__(self, value=-100, confidence=0.0):
self._value = value
self._confidence = confidence
@property
def value(self):
return self._value
@property
def confidence(self):
return self._confidence
class CTFCategory:
def __init__(self, value=(0.0, 0.0), confidence=0.0):
self._value = value
self._confidence = confidence
@property
def value(self):
return self._value
@property
def confidence(self):
return self._confidence
'''
# Data structure for CryoEMHole
class CryoEMHole(CryoEMObject):
def __init__(self, name='', idx=-1, parent_idx=-1, ctf=CTF(), gt_ctf=CTF()):
super().__init__(name, idx, parent_idx)
self._visited = False
self._ctf = ctf
self._gt_ctf = gt_ctf
def __str__(self):
return ' HOLE {} ({} {}) ctf ({:5.2f} {:5.2f}) gt_ctf ({:5.2f} {:5.2f}) v {:}'.format(self.name, self.parent_idx, \
self.idx, self.ctf.value, self.ctf.confidence, self.gt_ctf.value, self.gt_ctf.confidence, self.status)
@property
def is_visited(self):
return self._visited
@property
def ctf(self):
return self._ctf
@property
def gt_ctf(self):
return self._gt_ctf
@property
def status(self):
return self._visited
def set_status(self, status=False):
self._visited = status
'''
# Data structure for CryoEMHole
class CryoEMHole(CryoEMObject):
def __init__(self, name='', idx=-1, parent_idx=-1, ctf=None, gt_ctf=CTFValue(), category_bins=(0, 6,CryoEMConfig.MAX_CTF_VALUE), ctf_category=None):
super().__init__(name, idx, parent_idx)
self._visited = False
self._ctf = ctf
self._gt_ctf = gt_ctf
self._category = ctf_category
self._gt_category = self._create_category(self._gt_ctf, category_bins)
def __str__(self):
hole_str =' HOLE ({} ({} {}) gt_ctf ({:5.2f} {:5.2f}) gt_category ({} {:5.2f}) v {:}'.format(self.name, self.parent_idx, \
self.idx, self.gt_ctf.value, self.gt_ctf.confidence, \
self.gt_category.value, self.gt_category.confidence, self.status)
if self._ctf:
hole_str += '\n ctf ({:5.2f} {:5.2f})'.format(self.ctf.value, self.ctf.confidence)
if self._gt_category:
hole_str += '\n category ({} {:5.2f})'.format(self.category.value, self.category.confidence)
return hole_str
def _create_category(self, ctf, category_bins):
hist, _ = np.histogram([ctf.value], category_bins)
return CTFCategory(tuple(hist), ctf.confidence)
@property
def is_visited(self):
return self._visited
@property
def ctf(self):
return self._ctf
@property
def gt_ctf(self):
return self._gt_ctf
@property
def status(self):
return self._visited
@property
def category(self):
return self._category
@property
def gt_category(self):
return self._gt_category
def set_status(self, status=False):
self._visited = status
# Data structure for CryoEMPatch (i.e hole-level image)
class CryoEMPatch(CryoEMObject):
def __init__(self, name='', idx=-1, parent_idx=-1, holeList=[]):
super().__init__(name, idx, parent_idx, holeList)
def __str__(self):
return ' PATCH {} ({} {}) #holes {} \n'.format(self.name, self.parent_idx, self.idx, len(self)) + ' \n'.join([str(item) for item in self.container])
# default setting returns all ctfs
def get_ctfs(self, threshold=CryoEMConfig.MAX_CTF_VALUE, prediction=True, visited=True):
if prediction:
return [hole.ctf for hole in self.container if hole.ctf.value <= threshold and hole.status == True] if visited else \
[hole.ctf for hole in self.container if hole.ctf.value <= threshold and hole.status == False]
else:
return [hole.gt_ctf for hole in self.container if hole.gt_ctf.value <= threshold and hole.status == True] if visited else \
[hole.gt_ctf for hole in self.container if hole.gt_ctf.value <= threshold and hole.status == False]
def get_categories(self, prediction=True, visited=True):
# print(f"prediciton type: {prediction}")
if prediction:
return [hole.category for hole in self.container if hole.status == True] if visited else \
[hole.category for hole in self.container if hole.status == False]
else:
return [hole.gt_category for hole in self.container if hole.status == True] if visited else \
[hole.gt_category for hole in self.container if hole.status == False]
'''
def ctf_histogram(self, bins=[0,3,5,7,9, CryoEMConfig.MAX_CTF_VALUE]): # computing features
hist, _= np.histogram(self.get_ctfs(), bins)
return hist
'''
# Data structure for CryoEMSquare
class CryoEMSquare(CryoEMObject):
def __init__(self, name='', idx=-1, parent_idx=-1, patchList=[]):
super().__init__(name, idx, parent_idx, patchList)
def __str__(self):
return ' SQUARE {} ({} {}) #patches {}\n'.format(self.name, self.parent_idx, self.idx, len(self)) + ' \n'.join([str(item) for item in self.container])
def hole_counts(self):
return sum([len(patch) for patch in self.container])
def visited_hole_counts(self):
return sum([patch.visited_hole_counts for patch in self.container])
'''
def ctf_histogram(self, bins=[6, CryoEMConfig.MAX_CTF_VALUE]): # computing features
hist, _= np.histogram(self.get_ctfs(), bins)
return hist
'''
# Data structure for CryoEMGrid
class CryoEMGrid(CryoEMObject):
def __init__(self, name='', idx=-1, squareList=[]):
super().__init__(name, idx, -1, squareList)
def __str__(self):
return 'GRID {} ({} {}) #squares {}\n'.format(self.name, self.parent_idx, self.idx, len(self)) + ' \n'.join([str(item) for item in self.container])