cohere-core 4.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cohere_core/controller/AI_guess.py +303 -0
- cohere_core/controller/__init__.py +1 -0
- cohere_core/controller/features.py +372 -0
- cohere_core/controller/op_flow.py +318 -0
- cohere_core/controller/phasing.py +1290 -0
- cohere_core/data/__init__.py +1 -0
- cohere_core/data/alien_tools.py +417 -0
- cohere_core/data/standard_preprocess.py +187 -0
- cohere_core/lib/__init__.py +0 -0
- cohere_core/lib/aflib.py +393 -0
- cohere_core/lib/check_libs.py +50 -0
- cohere_core/lib/cohlib.py +594 -0
- cohere_core/lib/cplib.py +388 -0
- cohere_core/lib/nplib.py +361 -0
- cohere_core/lib/torchlib.py +478 -0
- cohere_core/utilities/__init__.py +4 -0
- cohere_core/utilities/config_errors_dict.py +155 -0
- cohere_core/utilities/config_verifier.py +1088 -0
- cohere_core/utilities/dvc_utils.py +780 -0
- cohere_core/utilities/ga_utils.py +217 -0
- cohere_core/utilities/utils.py +502 -0
- cohere_core-4.3.dist-info/METADATA +68 -0
- cohere_core-4.3.dist-info/RECORD +26 -0
- cohere_core-4.3.dist-info/WHEEL +5 -0
- cohere_core-4.3.dist-info/licenses/LICENSE +18 -0
- cohere_core-4.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
# #########################################################################
|
|
2
|
+
# Copyright (c) , UChicago Argonne, LLC. All rights reserved. #
|
|
3
|
+
# #
|
|
4
|
+
# See LICENSE file. #
|
|
5
|
+
# #########################################################################
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
import re
|
|
9
|
+
|
|
10
|
+
# This dict maps the mnemonic used when defining algorithm sequence parameter to the four steps of
|
|
11
|
+
# phase retrieval algorithm functions.
|
|
12
|
+
|
|
13
|
+
algs = {'ER': ('to_reciprocal_space', 'modulus', 'to_direct_space', 'er'),
|
|
14
|
+
'HIO': ('to_reciprocal_space', 'modulus', 'to_direct_space', 'hio'),
|
|
15
|
+
'ERpc': ('to_reciprocal_space', 'pc_modulus', 'to_direct_space', 'er'),
|
|
16
|
+
'HIOpc': ('to_reciprocal_space', 'pc_modulus', 'to_direct_space', 'hio'),
|
|
17
|
+
'SF': ('to_reciprocal_space', 'modulus', 'to_direct_space', 'sf'),
|
|
18
|
+
'RAAR': ('to_reciprocal_space', 'modulus', 'to_direct_space', 'raar')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
# This map keeps the names of triggers that can be configured as sub-trigger, i.e. be a trigger for the iteration span
|
|
22
|
+
# defined by preceding algorithm. The key is the trigger name and value is the mnemonic. The mnemonic is used in the
|
|
23
|
+
# configuration.
|
|
24
|
+
sub_triggers = {'SW' : 'shrink_wrap_trigger',
|
|
25
|
+
'PHC' : 'phc_trigger',
|
|
26
|
+
'LPF' : 'lowpass_filter_trigger'}
|
|
27
|
+
|
|
28
|
+
# This list contains triggers that will be active at the last iteration defined by trigger, despite
|
|
29
|
+
# not being a trigger calculated by the step formula.
|
|
30
|
+
# It applies to sub-triggers, setting the last iteration to that of sub-trigger.
|
|
31
|
+
last_iter_op_triggers = ['progress_trigger',
|
|
32
|
+
'switch_peaks_trigger',
|
|
33
|
+
'switch_resampling_trigger']
|
|
34
|
+
|
|
35
|
+
def get_alg_rows(s, pc_conf_start):
|
|
36
|
+
"""
|
|
37
|
+
Parses algorithm sequence string into structures being: algorithm rows, and sub-trigger operations info.
|
|
38
|
+
|
|
39
|
+
:param s: str
|
|
40
|
+
algorithm sequence
|
|
41
|
+
:param pc_conf_start: boolean or None
|
|
42
|
+
if None, no partial coherence is scheduled
|
|
43
|
+
if True, the configured partial coherence will be scheduled
|
|
44
|
+
if False, the partial coherence started ago (in GA case) and will continue here
|
|
45
|
+
:return: tuple
|
|
46
|
+
rows : ndarry
|
|
47
|
+
ndarray that depicts algorithms (modulus, pc_modulus, hio, er) operations
|
|
48
|
+
sub_rows : dict
|
|
49
|
+
dictionary with entries of k : v, where
|
|
50
|
+
k is the trigger name that is being configured as sub-triggers
|
|
51
|
+
v is a list of sub-trigger operations
|
|
52
|
+
iter_no : int
|
|
53
|
+
number of iterations
|
|
54
|
+
pc_start : None or int
|
|
55
|
+
starting iteration of partial coherence if any
|
|
56
|
+
"""
|
|
57
|
+
seq = []
|
|
58
|
+
accum_iter = 0
|
|
59
|
+
|
|
60
|
+
def parse_entry(ent, accum_iter):
|
|
61
|
+
# parses elementary part of the algorithm sequence
|
|
62
|
+
r_e = ent.split('*')
|
|
63
|
+
seq.append([int(r_e[0]), r_e[1], accum_iter])
|
|
64
|
+
accum_iter += int(r_e[0])
|
|
65
|
+
return accum_iter
|
|
66
|
+
|
|
67
|
+
s = s.replace(' ', '')
|
|
68
|
+
entries = s.split('+')
|
|
69
|
+
i = 0
|
|
70
|
+
while i < len(entries):
|
|
71
|
+
entry = entries[i]
|
|
72
|
+
if '(' in entry:
|
|
73
|
+
group = []
|
|
74
|
+
rep_entry = entry.split('(')
|
|
75
|
+
repeat = int(rep_entry[0][:-1])
|
|
76
|
+
group.append(rep_entry[1])
|
|
77
|
+
i += 1
|
|
78
|
+
group_entry = entries[i]
|
|
79
|
+
while ')' not in group_entry:
|
|
80
|
+
group.append(group_entry)
|
|
81
|
+
i += 1
|
|
82
|
+
group_entry = entries[i]
|
|
83
|
+
group.append(group_entry[:-1])
|
|
84
|
+
for _ in range(repeat):
|
|
85
|
+
for group_entry in group:
|
|
86
|
+
accum_iter = parse_entry(group_entry, accum_iter)
|
|
87
|
+
i += 1
|
|
88
|
+
else:
|
|
89
|
+
accum_iter = parse_entry(entry, accum_iter)
|
|
90
|
+
i += 1
|
|
91
|
+
iter_no = sum([e[0] for e in seq])
|
|
92
|
+
alg_rows = {}
|
|
93
|
+
sub_rows = {}
|
|
94
|
+
row = np.zeros(iter_no, dtype=int)
|
|
95
|
+
fs = set([i for sub in algs.values() for i in sub])
|
|
96
|
+
for f in fs:
|
|
97
|
+
alg_rows[f] = row.copy()
|
|
98
|
+
i = 0
|
|
99
|
+
pc_start = None
|
|
100
|
+
for entry in seq:
|
|
101
|
+
repeat = entry[0]
|
|
102
|
+
funs = entry[1].split('.')
|
|
103
|
+
if funs[0] not in algs:
|
|
104
|
+
msg = f'algorithm {funs[0]} is not defined in op_flow.py file, algs dict.'
|
|
105
|
+
raise NameError(msg)
|
|
106
|
+
# the pc will not be executed if pc_conf_start is None
|
|
107
|
+
# this code will be revised after each generation has separate config
|
|
108
|
+
if pc_conf_start is None:
|
|
109
|
+
if funs[0].endswith('pc'):
|
|
110
|
+
funs[0] = funs[0][:-2]
|
|
111
|
+
elif not pc_conf_start:
|
|
112
|
+
if not funs[0].endswith('pc'):
|
|
113
|
+
funs[0] = funs[0] + 'pc'
|
|
114
|
+
|
|
115
|
+
row_keys = algs[funs[0]]
|
|
116
|
+
for row_key in row_keys:
|
|
117
|
+
alg_rows[row_key][i:i+repeat] = 1
|
|
118
|
+
if 'pc' in row_key and pc_start == None:
|
|
119
|
+
if pc_conf_start == True:
|
|
120
|
+
pc_start = i
|
|
121
|
+
else:
|
|
122
|
+
pc_start = 1
|
|
123
|
+
# find sub-triggers
|
|
124
|
+
for row_key in funs[1:]:
|
|
125
|
+
match = re.match(r"([A-Z]+)([0-9]+)", row_key, re.I)
|
|
126
|
+
if match:
|
|
127
|
+
(trig_op, idx) = match.groups(0)
|
|
128
|
+
sub_t = sub_triggers[trig_op]
|
|
129
|
+
if trig_op not in sub_triggers.keys():
|
|
130
|
+
msg = f'the sub-trigger {trig_op} must be defined in op_flow.py file, sub_triggers dict.'
|
|
131
|
+
raise NameError(msg)
|
|
132
|
+
if sub_t not in sub_rows:
|
|
133
|
+
sub_rows[sub_t] = []
|
|
134
|
+
sub_rows[sub_t].append((entry[2], entry[0] + entry[2], idx))
|
|
135
|
+
i += repeat
|
|
136
|
+
|
|
137
|
+
return alg_rows, sub_rows, iter_no, pc_start
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def fill_trigger_row(trig, iter_no, last_trig, row=None):
|
|
141
|
+
"""
|
|
142
|
+
This functions creates ndarray that depicts triggered operations for a given trigger.
|
|
143
|
+
|
|
144
|
+
:param trig: list
|
|
145
|
+
a list with 1, 2, or 3 elements defining trigger
|
|
146
|
+
:param iter_no: int
|
|
147
|
+
total number of iterations
|
|
148
|
+
:param row: ndarray
|
|
149
|
+
if given, the row will be used to fill the trigger
|
|
150
|
+
:return:
|
|
151
|
+
"""
|
|
152
|
+
if row is None:
|
|
153
|
+
row = np.zeros(iter_no, dtype=int)
|
|
154
|
+
if len(trig) ==1:
|
|
155
|
+
trig_iter = trig[0]
|
|
156
|
+
if trig_iter < 0:
|
|
157
|
+
trig_iter += iter_no
|
|
158
|
+
row[trig_iter] = 1
|
|
159
|
+
else: # trig is list
|
|
160
|
+
trig_start = trig[0]
|
|
161
|
+
if trig_start < 0:
|
|
162
|
+
trig_start += iter_no
|
|
163
|
+
step = trig[1]
|
|
164
|
+
if len(trig) == 3:
|
|
165
|
+
trig_stop = trig[2]
|
|
166
|
+
if trig_stop < 0:
|
|
167
|
+
trig_stop += iter_no
|
|
168
|
+
else:
|
|
169
|
+
trig_stop = iter_no
|
|
170
|
+
for it in range(trig_start, trig_stop, step):
|
|
171
|
+
row[it] = 1
|
|
172
|
+
if last_trig:
|
|
173
|
+
row[trig_stop - 1] = 1
|
|
174
|
+
return row
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def fill_sub_trigger_row(sub_iters, sub_trigs, iter_no, last_trig):
|
|
178
|
+
"""
|
|
179
|
+
Based on iterations allocated to sub-triggers and sub-triggers definitions this functions
|
|
180
|
+
creates ndarray that depicts triggered operations.
|
|
181
|
+
|
|
182
|
+
:param sub_iters: list
|
|
183
|
+
contains entry for each sub-trigger
|
|
184
|
+
the entry consisting of starting and ending iterations where the sub-trigger
|
|
185
|
+
is active and index value (+1) that specifies sub-trigger.
|
|
186
|
+
:param sub_trigs: list
|
|
187
|
+
list of sub-trigger, defined in configuration
|
|
188
|
+
:param iter_no: int
|
|
189
|
+
total number of iterations
|
|
190
|
+
:return: ndarray
|
|
191
|
+
array of int, value of zero meaning no trigger operation in this iteration
|
|
192
|
+
value greater than zero meaning the sub-trigger operation related to the value
|
|
193
|
+
will be triggered in this iteration
|
|
194
|
+
"""
|
|
195
|
+
# create array indicating triggered operation (1) or no action (0) along iterations
|
|
196
|
+
sub_trig_row = np.zeros(iter_no, dtype=int)
|
|
197
|
+
# create array indicating with index which sub-triggered operation may happen in the iterations
|
|
198
|
+
sub_trig_idx_row = np.zeros(iter_no, dtype=int)
|
|
199
|
+
# for each defined sub iteration chunk apply corresponding sub-trigger
|
|
200
|
+
for (b, e, idx) in sub_iters:
|
|
201
|
+
index = int(idx)
|
|
202
|
+
sub_trig_idx_row[b:e] = index + 1
|
|
203
|
+
if len(sub_trigs) - 1 < index:
|
|
204
|
+
msg = 'not enough entries in sub-trigger'
|
|
205
|
+
raise RuntimeError(msg)
|
|
206
|
+
trigger = sub_trigs[index].copy()
|
|
207
|
+
trigger[0] += b
|
|
208
|
+
if len(trigger) == 2:
|
|
209
|
+
trigger.append(e)
|
|
210
|
+
elif len(trigger) == 3:
|
|
211
|
+
trigger[2] = min(e, trigger[0] + trigger[2])
|
|
212
|
+
sub_trig_row = fill_trigger_row(trigger, iter_no, last_trig, sub_trig_row)
|
|
213
|
+
|
|
214
|
+
return sub_trig_row * sub_trig_idx_row
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def get_flow_arr(params, flow_items_list, curr_gen=None):
|
|
218
|
+
# get information about GA and partial coherence from config_map
|
|
219
|
+
# pc_conf_start is None if partial coherence is inactive in this reconstruction
|
|
220
|
+
# it is True if the partial coherence starts in this generation
|
|
221
|
+
# and False if partial coherence started in previous generation and is continued
|
|
222
|
+
if 'pc_interval' in params:
|
|
223
|
+
if curr_gen is None:
|
|
224
|
+
pc_conf_start = True
|
|
225
|
+
else:
|
|
226
|
+
if curr_gen < params['ga_gen_pc_start']:
|
|
227
|
+
pc_conf_start = None
|
|
228
|
+
elif curr_gen == params['ga_gen_pc_start']:
|
|
229
|
+
pc_conf_start = True
|
|
230
|
+
else:
|
|
231
|
+
pc_conf_start = False
|
|
232
|
+
else:
|
|
233
|
+
pc_conf_start = None
|
|
234
|
+
if pc_conf_start is None:
|
|
235
|
+
params.pop('pc_interval', None)
|
|
236
|
+
|
|
237
|
+
# parse algorithm sequence to get the algorithm rows and sub-triggers rows, number iterations,
|
|
238
|
+
# and partial coherence starting iteration
|
|
239
|
+
try:
|
|
240
|
+
(alg_rows, sub_iters, iter_no, pc_start) = get_alg_rows(params['algorithm_sequence'], pc_conf_start)
|
|
241
|
+
except:
|
|
242
|
+
return False, None, None
|
|
243
|
+
|
|
244
|
+
# do some checks to find if the sequence and configuration are runnable
|
|
245
|
+
# and special cases
|
|
246
|
+
|
|
247
|
+
# this array is a mask blocking shrink wrap when low pass filter is active
|
|
248
|
+
apply_sw_row = np.ones(iter_no, dtype=int)
|
|
249
|
+
last_lpf = None
|
|
250
|
+
if 'lowpass_filter_trigger' in params:
|
|
251
|
+
if 'lowpass_filter_trigger' in sub_iters:
|
|
252
|
+
for b, e, i in sub_iters['lowpass_filter_trigger']:
|
|
253
|
+
apply_sw_row[b:e] = 0
|
|
254
|
+
last_lpf = e
|
|
255
|
+
elif len(params['lowpass_filter_trigger']) < 2:
|
|
256
|
+
print('Low pass trigger misconfiguration error. This trigger should have upper bound.')
|
|
257
|
+
raise
|
|
258
|
+
elif params['lowpass_filter_trigger'][2] >= iter_no:
|
|
259
|
+
print('Low pass trigger misconfiguration error. The upper bound should be less than total iterations.')
|
|
260
|
+
raise
|
|
261
|
+
else:
|
|
262
|
+
apply_sw_row[params['lowpass_filter_trigger'][0]:params['lowpass_filter_trigger'][2]] = 0
|
|
263
|
+
last_lpf = params['lowpass_filter_trigger'][2]
|
|
264
|
+
|
|
265
|
+
if pc_start is not None:
|
|
266
|
+
if pc_start == 0:
|
|
267
|
+
raise ValueError('partial coherence is configured in first iteration, allow several ER before.')
|
|
268
|
+
else:
|
|
269
|
+
pc_interval = params['pc_interval']
|
|
270
|
+
params['pc_trigger'] = [pc_start, pc_interval]
|
|
271
|
+
|
|
272
|
+
# initialize
|
|
273
|
+
sub_trig_op = {}
|
|
274
|
+
|
|
275
|
+
# create empty array with the size of number of all functions by number of all iterations
|
|
276
|
+
flow_arr = np.zeros((len(flow_items_list), iter_no), dtype=int)
|
|
277
|
+
|
|
278
|
+
# fill the flow array with ones if function should execute in iteration
|
|
279
|
+
for i, flow_item in enumerate(flow_items_list):
|
|
280
|
+
if flow_item == 'next':
|
|
281
|
+
# these functions are executed in each iteration
|
|
282
|
+
flow_arr[i, :] = 1
|
|
283
|
+
elif flow_item in alg_rows.keys():
|
|
284
|
+
# fill out the algorithm rows
|
|
285
|
+
flow_arr[i] = alg_rows[flow_item]
|
|
286
|
+
elif flow_item.endswith('operation'):
|
|
287
|
+
# fill out trigger/sub-trigger operations rows
|
|
288
|
+
# The function name and associated trigger differ in prefix.
|
|
289
|
+
# the function name ends with 'operation', and trigger ends with 'trigger'
|
|
290
|
+
trigger_name = flow_item.replace('operation', 'trigger')
|
|
291
|
+
if trigger_name in params:
|
|
292
|
+
# set the switch last_trig if the trigger should end with operation
|
|
293
|
+
last_trig = trigger_name in last_iter_op_triggers
|
|
294
|
+
|
|
295
|
+
# determined in algorithm sequence parsing if the triggered operation is configured
|
|
296
|
+
# with sub-triggers or trigger
|
|
297
|
+
if trigger_name in sub_iters.keys():
|
|
298
|
+
# may throw exception
|
|
299
|
+
flow_arr[i] = fill_sub_trigger_row(sub_iters[trigger_name], params[trigger_name], iter_no, last_trig)
|
|
300
|
+
# add entry to sub trigger operation dict with key of the trigger mnemonic
|
|
301
|
+
# and the value of a list with the row and sub triggers iterations chunks
|
|
302
|
+
sub_trig_op[trigger_name] = (flow_arr[i], sub_iters[trigger_name])
|
|
303
|
+
else:
|
|
304
|
+
flow_arr[i] = fill_trigger_row(params[trigger_name], iter_no, last_trig)
|
|
305
|
+
|
|
306
|
+
# handle special cases
|
|
307
|
+
if flow_item == 'shrink_wrap_operation':
|
|
308
|
+
# shrink wrap is blocked by low pass filter
|
|
309
|
+
flow_arr[i] = flow_arr[i] * apply_sw_row
|
|
310
|
+
elif flow_item == 'set_prev_pc' and pc_start is not None:
|
|
311
|
+
# set_prev_pc is executed one iteration before pc_trigger
|
|
312
|
+
pc_row = flow_items_list.index('pc_operation')
|
|
313
|
+
flow_arr[i, : -1] = flow_arr[pc_row, 1:]
|
|
314
|
+
elif flow_item == 'reset_resolution' and last_lpf is not None:
|
|
315
|
+
# reset low pass filter (i.e. data set to original) after the last LPF operation
|
|
316
|
+
flow_arr[i][last_lpf] = 1
|
|
317
|
+
|
|
318
|
+
return pc_start is not None, flow_arr, sub_trig_op
|