cohere-core 4.0b0__tar.gz → 4.2.0__tar.gz

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.
Files changed (49) hide show
  1. {cohere_core-4.0b0 → cohere_core-4.2.0}/PKG-INFO +8 -1
  2. {cohere_core-4.0b0 → cohere_core-4.2.0}/README.md +2 -2
  3. cohere_core-4.2.0/cohere_core/__init__.py +1 -0
  4. {cohere_core-4.0b0 → cohere_core-4.2.0}/cohere_core/controller/AI_guess.py +7 -93
  5. cohere_core-4.2.0/cohere_core/controller/__init__.py +3 -0
  6. {cohere_core-4.0b0 → cohere_core-4.2.0}/cohere_core/controller/features.py +165 -107
  7. cohere_core-4.2.0/cohere_core/controller/op_flow.py +314 -0
  8. cohere_core-4.2.0/cohere_core/controller/phasing.py +1105 -0
  9. {cohere_core-4.0b0 → cohere_core-4.2.0}/cohere_core/controller/reconstruction_coupled.py +8 -43
  10. {cohere_core-4.0b0 → cohere_core-4.2.0}/cohere_core/controller/reconstruction_single.py +22 -39
  11. {cohere_core-4.0b0 → cohere_core-4.2.0}/cohere_core/data/alien_tools.py +12 -34
  12. {cohere_core-4.0b0 → cohere_core-4.2.0}/cohere_core/data/standard_preprocess.py +66 -25
  13. {cohere_core-4.0b0 → cohere_core-4.2.0}/cohere_core/lib/aflib.py +52 -0
  14. cohere_core-4.2.0/cohere_core/lib/check_libs.py +44 -0
  15. {cohere_core-4.0b0 → cohere_core-4.2.0}/cohere_core/lib/cohlib.py +267 -13
  16. cohere_core-4.2.0/cohere_core/lib/cplib.py +369 -0
  17. cohere_core-4.2.0/cohere_core/lib/nplib.py +342 -0
  18. cohere_core-4.2.0/cohere_core/lib/torchlib.py +461 -0
  19. cohere_core-4.2.0/cohere_core/utilities/__init__.py +4 -0
  20. {cohere_core-4.0b0 → cohere_core-4.2.0}/cohere_core/utilities/config_errors_dict.py +7 -48
  21. {cohere_core-4.0b0 → cohere_core-4.2.0}/cohere_core/utilities/config_verifier.py +64 -382
  22. cohere_core-4.2.0/cohere_core/utilities/dvc_utils.py +821 -0
  23. cohere_core-4.2.0/cohere_core/utilities/ga_utils.py +183 -0
  24. cohere_core-4.2.0/cohere_core/utilities/host_utils.py +36 -0
  25. cohere_core-4.2.0/cohere_core/utilities/utils.py +988 -0
  26. {cohere_core-4.0b0 → cohere_core-4.2.0}/cohere_core.egg-info/PKG-INFO +9 -2
  27. {cohere_core-4.0b0 → cohere_core-4.2.0}/cohere_core.egg-info/SOURCES.txt +3 -3
  28. cohere_core-4.2.0/cohere_core.egg-info/requires.txt +7 -0
  29. {cohere_core-4.0b0 → cohere_core-4.2.0}/setup.py +7 -2
  30. cohere_core-4.0b0/cohere_core/__init__.py +0 -3
  31. cohere_core-4.0b0/cohere_core/controller/__init__.py +0 -6
  32. cohere_core-4.0b0/cohere_core/controller/op_flow.py +0 -232
  33. cohere_core-4.0b0/cohere_core/controller/phasing.py +0 -729
  34. cohere_core-4.0b0/cohere_core/controller/reconstruction_GA.py +0 -581
  35. cohere_core-4.0b0/cohere_core/controller/reconstruction_multi.py +0 -228
  36. cohere_core-4.0b0/cohere_core/data/auto_prep.py +0 -0
  37. cohere_core-4.0b0/cohere_core/lib/cplib.py +0 -188
  38. cohere_core-4.0b0/cohere_core/lib/nplib.py +0 -194
  39. cohere_core-4.0b0/cohere_core/lib/torchlib.py +0 -263
  40. cohere_core-4.0b0/cohere_core/utilities/__init__.py +0 -2
  41. cohere_core-4.0b0/cohere_core/utilities/dvc_utils.py +0 -527
  42. cohere_core-4.0b0/cohere_core/utilities/utils.py +0 -559
  43. cohere_core-4.0b0/cohere_core.egg-info/requires.txt +0 -3
  44. {cohere_core-4.0b0 → cohere_core-4.2.0}/LICENSE +0 -0
  45. {cohere_core-4.0b0 → cohere_core-4.2.0}/cohere_core/data/__init__.py +0 -0
  46. {cohere_core-4.0b0 → cohere_core-4.2.0}/cohere_core/lib/__init__.py +0 -0
  47. {cohere_core-4.0b0 → cohere_core-4.2.0}/cohere_core.egg-info/dependency_links.txt +0 -0
  48. {cohere_core-4.0b0 → cohere_core-4.2.0}/cohere_core.egg-info/top_level.txt +0 -0
  49. {cohere_core-4.0b0 → cohere_core-4.2.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cohere_core
3
- Version: 4.0b0
3
+ Version: 4.2.0
4
4
  Home-page: https://github.com/advancedPhotonSource/cohere
5
5
  Author: Barbara Frosik, Ross Harder
6
6
  Author-email: bfrosik@anl.gov
@@ -12,3 +12,10 @@ Classifier: Programming Language :: Python :: 3.9
12
12
  Classifier: Programming Language :: Python :: 3.10
13
13
  Classifier: Programming Language :: Python :: 3.11
14
14
  License-File: LICENSE
15
+ Requires-Dist: numpy<2
16
+ Requires-Dist: scikit-learn
17
+ Requires-Dist: tifffile
18
+ Requires-Dist: gputil
19
+ Requires-Dist: psutil
20
+ Requires-Dist: tqdm
21
+ Requires-Dist: matplotlib
@@ -1,6 +1,6 @@
1
- [![Documentation Status](https://readthedocs.org/projects/cohere/badge/?version=latest)](http://cohere.readthedocs.io/en/latest/?badge=latest)
1
+ [![Documentation Status](https://readthedocs.org/projects/cohere-dev/badge/?version=latest)](http://cohere-dev.readthedocs.io/en/latest/?badge=latest)
2
2
 
3
- Project home page: https://cohere.readthedocs.io/
3
+ Project home page: [https://cohere-dev.readthedocs.io/](https://cohere-dev.readthedocs.io/)
4
4
 
5
5
  The cohere package provides tools for reconstruction of image of a nanoscale structures from data obtained using Bragg Coherent Diffraction Imaging technique.
6
6
 
@@ -0,0 +1 @@
1
+
@@ -3,10 +3,6 @@ import os
3
3
  import cohere_core.utilities.utils as ut
4
4
  import math
5
5
  from typing import Union
6
-
7
- # tensorflow will use cpu
8
- os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
9
- # import tensorflow for trained model
10
6
  import tensorflow as tf
11
7
  from tensorflow.keras import backend as K
12
8
  from tensorflow.keras.models import Model, load_model
@@ -51,91 +47,13 @@ class Mymodel:
51
47
  outputs=model.get_layer('phi').output)
52
48
 
53
49
 
54
- def threshold_by_edge(fp: np.ndarray) -> np.ndarray:
55
- # threshold by left edge value
56
- mask = np.ones_like(fp, dtype=bool)
57
- mask[tuple([slice(1, None)] * fp.ndim)] = 0
58
- zero = 1e-6
59
- cut = np.max(fp[mask])
60
- binary = np.zeros_like(fp)
61
- binary[(np.abs(fp) > zero) & (fp > cut)] = 1
62
- return binary
63
-
64
-
65
- def select_central_object(fp: np.ndarray) -> np.ndarray:
66
- import scipy.ndimage as ndimage
67
- zero = 1e-6
68
- binary = np.abs(fp)
69
- binary[binary > zero] = 1
70
- binary[binary <= zero] = 0
71
-
72
- # cluster by connectivity
73
- struct = ndimage.morphology.generate_binary_structure(fp.ndim,
74
- 1).astype("uint8")
75
- label, nlabel = ndimage.label(binary, structure=struct)
76
-
77
- # select largest cluster
78
- select = np.argmax(np.bincount(np.ravel(label))[1:]) + 1
79
-
80
- binary[label != select] = 0
81
-
82
- fp[binary == 0] = 0
83
- return fp
84
-
85
-
86
- def get_central_object_extent(fp: np.ndarray) -> list:
87
- fp_cut = threshold_by_edge(np.abs(fp))
88
- need = select_central_object(fp_cut)
89
-
90
- # get extend of cluster
91
- extent = [np.max(s) + 1 - np.min(s) for s in np.nonzero(need)]
92
- return extent
93
-
94
-
95
- def get_oversample_ratio(fp: np.ndarray) -> np.ndarray:
96
- """ get oversample ratio
97
- fp = diffraction pattern
98
- """
99
- # autocorrelation
100
- acp = np.fft.fftshift(np.fft.ifftn(np.abs(fp)**2.))
101
- aacp = np.abs(acp)
102
-
103
- # get extent
104
- blob = get_central_object_extent(aacp)
105
-
106
- # correct for underestimation due to thresholding
107
- correction = [0.025, 0.025, 0.0729][:fp.ndim]
108
-
109
- extent = [
110
- min(m, s + int(round(f * aacp.shape[i], 1)))
111
- for i, (s, f, m) in enumerate(zip(blob, correction, aacp.shape))
112
- ]
113
-
114
- # oversample ratio
115
- oversample = [
116
- 2. * s / (e + (1 - s % 2)) for s, e in zip(aacp.shape, extent)
117
- ]
118
- return np.round(oversample, 3)
119
-
120
-
121
- def Resize(IN, dim):
122
- ft = np.fft.fftshift(np.fft.fftn(IN)) / np.prod(IN.shape)
123
-
124
- pad_value = np.array(dim) // 2 - np.array(ft.shape) // 2
125
- pad = [[pad_value[0], pad_value[0]], [pad_value[1], pad_value[1]],
126
- [pad_value[2], pad_value[2]]]
127
- ft_resize = ut.adjust_dimensions(ft, pad)
128
- output = np.fft.ifftn(np.fft.ifftshift(ft_resize)) * np.prod(dim)
129
- return output
130
-
131
-
132
50
  def match_oversample_diff(
133
51
  diff: np.ndarray,
134
52
  fr: Union[list, np.ndarray, None] = None,
135
53
  to: Union[list, np.ndarray, None] = None,
136
54
  shape: Union[list, np.ndarray, None] = [64, 64, 64],
137
55
  ):
138
- """ resize diff to match oversample ratios
56
+ """ resize diff to match oversample ratios
139
57
  diff = diffraction pattern
140
58
  fr = from oversample ratio
141
59
  to = to oversample ratio
@@ -144,7 +62,6 @@ def match_oversample_diff(
144
62
  # adjustment needed to match oversample ratio
145
63
  change = [np.round(f / t).astype('int32') for f, t in zip(fr, to)]
146
64
  change = [np.max([1, c]) for c in change]
147
-
148
65
  diff = ut.binning(diff, change)
149
66
  # crop diff to match output shape
150
67
  shape_arr = np.array(shape)
@@ -245,7 +162,7 @@ def ff_propagation(data):
245
162
  '''
246
163
  diffraction. Assume same x and y lengthss and uniform sampling
247
164
  data: source plane field
248
-
165
+
249
166
  '''
250
167
  diff = _fourier_transform(data)
251
168
 
@@ -293,7 +210,7 @@ def run_AI(data, model_file, dir):
293
210
 
294
211
  # prepare data to make the oversampling ratio ~3
295
212
  wos = 3.0
296
- orig_os = get_oversample_ratio(data)
213
+ orig_os = ut.get_oversample_ratio(data)
297
214
  # match oversampling to wos
298
215
  wanted_os = [wos, wos, wos]
299
216
  # match diff os
@@ -314,17 +231,14 @@ def run_AI(data, model_file, dir):
314
231
  pred_obj = preds_amp * np.exp(1j * preds_phi)
315
232
 
316
233
  # match object size with the input data
317
- pred_obj = Resize(pred_obj, inshape)
234
+ pred_obj = ut.Resize(pred_obj, inshape)
318
235
 
319
236
  pad_value = np.array(data.shape) // 2 - np.array(pred_obj.shape) // 2
320
237
  pad = [[pad_value[0], pad_value[0]], [pad_value[1], pad_value[1]],
321
238
  [pad_value[2], pad_value[2]]]
322
239
  guess = ut.adjust_dimensions(pred_obj, pad)
323
240
 
324
- np.save(dir + '/image.npy', guess)
325
-
326
- if "CUDA_VISIBLE_DEVICES" in os.environ:
327
- del os.environ["CUDA_VISIBLE_DEVICES"]
241
+ np.save(ut.join(dir, 'image.npy'), guess)
328
242
 
329
243
 
330
244
  def start_AI(pars, datafile, dir):
@@ -352,7 +266,7 @@ def start_AI(pars, datafile, dir):
352
266
  print ('no AI_trained_model in config')
353
267
  return None
354
268
  if not os.path.isfile(pars['AI_trained_model']):
355
- print('there is no file', pars['AI_trained_model'])
269
+ print(f'there is no file {pars["AI_trained_model"]}')
356
270
  return None
357
271
 
358
272
  if datafile.endswith('tif') or datafile.endswith('tiff'):
@@ -372,7 +286,7 @@ def start_AI(pars, datafile, dir):
372
286
  return None
373
287
 
374
288
  # The results will be stored in the directory <experiment_dir>/AI_guess
375
- ai_dir = dir + '/results_AI'
289
+ ai_dir = ut.join(dir, 'results_AI')
376
290
  if os.path.exists(ai_dir):
377
291
  pass
378
292
  else:
@@ -0,0 +1,3 @@
1
+ from .reconstruction_single import *
2
+ from .reconstruction_coupled import *
3
+ from .phasing import *
@@ -1,3 +1,4 @@
1
+ import cohere_core.utilities.utils as ut
1
2
  import cohere_core.utilities.dvc_utils as dvut
2
3
  from abc import ABC, abstractmethod
3
4
 
@@ -5,53 +6,48 @@ __author__ = "Barbara Frosik"
5
6
  __copyright__ = "Copyright (c) 2016, UChicago Argonne, LLC."
6
7
  __docformat__ = 'restructuredtext en'
7
8
  __all__ = ['Pcdi',
8
- 'Feature',
9
+ 'TriggeredOp',
9
10
  'ShrinkWrap',
10
- 'PhaseMod',
11
+ 'PhaseConstrain',
11
12
  'LowPassFilter']
12
13
 
13
14
 
14
15
  def set_lib(dlib):
15
16
  global devlib
16
17
  devlib = dlib
17
- dvut.set_lib(devlib)
18
18
 
19
19
 
20
20
  class Pcdi:
21
21
  def __init__(self, params, data, dir=None):
22
- if 'pc_type' in params:
23
- self.type = params['pc_type']
24
- else:
25
- self.type = "LUCY"
22
+ self.type = params.get('pc_type', 'LUCY')
26
23
  if 'pc_LUCY_iterations' in params:
27
24
  self.iterations = params['pc_LUCY_iterations']
28
25
  else:
29
- self.iterations = 20
30
- if 'pc_normalize' in params:
31
- self.normalize = params['pc_normalize']
32
- else:
33
- self.normalize = True
26
+ print('pc_LUCY_iterations parameter not defined')
27
+ raise
28
+ self.normalize = params.get('pc_normalize', True)
34
29
  if 'pc_LUCY_kernel' in params:
35
30
  self.kernel_area = params['pc_LUCY_kernel']
36
31
  else:
37
- self.kernel_area = (16, 16, 16)
32
+ print('pc_LUCY_kernel parameter not defined')
33
+ raise
38
34
  if dir is None:
39
35
  self.kernel = None
40
36
  else:
41
37
  try:
42
- self.kernel = devlib.load(dir + '/coherence.npy')
38
+ self.kernel = devlib.load(ut.join(dir, 'coherence.npy'))
43
39
  except:
44
40
  self.kernel = None
45
41
 
46
42
  self.dims = devlib.dims(data)
47
- self.roi_data = dvut.crop_center(devlib.fftshift(data), self.kernel_area)
43
+ self.roi_data = ut.crop_center(devlib.fftshift(data), self.kernel_area)
48
44
  if self.normalize:
49
45
  self.sum_roi_data = devlib.sum(devlib.square(self.roi_data))
50
46
  if self.kernel is None:
51
47
  self.kernel = devlib.full(self.kernel_area, 0.5, dtype=devlib.dtype(data))
52
48
 
53
49
  def set_previous(self, abs_amplitudes):
54
- self.roi_amplitudes_prev = dvut.crop_center(devlib.fftshift(abs_amplitudes), self.kernel_area)
50
+ self.roi_amplitudes_prev = ut.crop_center(devlib.fftshift(abs_amplitudes), self.kernel_area)
55
51
 
56
52
  def apply_partial_coherence(self, abs_amplitudes):
57
53
  abs_amplitudes_2 = devlib.square(abs_amplitudes)
@@ -60,7 +56,7 @@ class Pcdi:
60
56
  return converged
61
57
 
62
58
  def update_partial_coherence(self, abs_amplitudes):
63
- roi_amplitudes = dvut.crop_center(devlib.fftshift(abs_amplitudes), self.kernel_area)
59
+ roi_amplitudes = ut.crop_center(devlib.fftshift(abs_amplitudes), self.kernel_area)
64
60
  roi_combined_amp = 2 * roi_amplitudes - self.roi_amplitudes_prev
65
61
  if self.normalize:
66
62
  amplitudes_2 = devlib.square(roi_combined_amp)
@@ -86,12 +82,12 @@ class Pcdi:
86
82
  self.kernel = devlib.absolute(self.kernel) / coh_sum
87
83
 
88
84
 
89
- class Feature(ABC):
85
+ class TriggeredOp(ABC):
90
86
  """
91
- Base class for features. This class creates feature objects and manages the trigger or subtrigger depending on
87
+ Base class for Triggered operation. This class creates feature objects and manages the trigger or subtrigger depending on
92
88
  configuration.
93
89
  """
94
- def __init__(self, sub_rows_trigs, params):
90
+ def __init__(self, trig_op_name):
95
91
  # the self.objs will hold either one concrete feature object if configuration is for general trigger,
96
92
  # i.e. spanning across all iterations,
97
93
  # or it will hold a list of objects, each being a concrete sub-feature, appearing in order that will be
@@ -102,12 +98,8 @@ class Feature(ABC):
102
98
  # or to self.apply_trigger_seq if sub-triggers are configured
103
99
  self.f = None
104
100
 
105
- # this function sets self.objs and self.f and creates all objects
106
- self.create_objs(sub_rows_trigs, params)
101
+ self.trig_op_name = trig_op_name # ex: 'shrink_wrap_trigger'
107
102
 
108
- self.key = None # ex: 'SW'
109
-
110
- self.trig_name = None # ex: 'shrink_wrap_trigger'
111
103
 
112
104
  def apply_trigger_obj(self, *args):
113
105
  """
@@ -149,7 +141,7 @@ class Feature(ABC):
149
141
  """
150
142
  pass
151
143
 
152
- def create_objs(self, sub_rows_trigs, params):
144
+ def create_objs(self, params, sub_rows_trigs):
153
145
  """
154
146
  The params map contains value of the feature trigger. If the trigger is configured as a general one,
155
147
  i.e. a single trigger, the self.objs is set to the created instance of the feature, and the self.f
@@ -162,14 +154,17 @@ class Feature(ABC):
162
154
  :param params: configuration parameters
163
155
  :return:
164
156
  """
165
- if self.key in sub_rows_trigs.keys():
166
- row = sub_rows_trigs[self.key][0]
167
- sub_trigs = sub_rows_trigs[self.key][1]
157
+ trigger_name = f'{self.trig_op_name}_trigger'
158
+
159
+ if trigger_name in sub_rows_trigs.keys():
160
+ row = sub_rows_trigs[trigger_name][0]
161
+ sub_trigs = sub_rows_trigs[trigger_name][1]
168
162
  sub_objs = {}
169
163
  for sub_t in sub_trigs:
170
164
  (beg, end, idx) = sub_t
171
165
  index = int(idx)
172
- sub_objs[index] = self.create_obj(params, index=index, beg=beg, end=end)
166
+ if index not in sub_objs.keys():
167
+ sub_objs[index] = self.create_obj(params, index=index, beg=beg, end=end)
173
168
  trigs = [i-1 for i in row.tolist() if i != 0]
174
169
  # the operation of creating object might fail
175
170
  # if conditions are not met, ex: the sw_type is not supported
@@ -177,16 +172,14 @@ class Feature(ABC):
177
172
  self.objs = [sub_objs[idx] for idx in trigs]
178
173
  self.f = self.apply_trigger_seq
179
174
  else:
180
- if self.trig_name in params:
175
+ if trigger_name in params:
181
176
  self.objs = self.create_obj(params)
182
177
  self.f = self.apply_trigger_obj
183
178
 
184
179
 
185
- class ShrinkWrap(Feature):
186
- def __init__(self, params, sub_rows_trigs):
187
- self.key = 'SW'
188
- self.trig_name = 'shrink_wrap_trigger'
189
- super().__init__(sub_rows_trigs, params)
180
+ class ShrinkWrap(TriggeredOp):
181
+ def __init__(self, trig_op):
182
+ super().__init__(trig_op)
190
183
 
191
184
  class GaussSW:
192
185
  def __init__(self, gauss_sigma, threshold):
@@ -194,88 +187,131 @@ class ShrinkWrap(Feature):
194
187
  self.threshold = threshold
195
188
 
196
189
  def apply_trigger(self, *args):
197
- # print('shrink wrap, sigma', self.gauss_sigma)
198
190
  ds_image = args[0]
199
191
  return dvut.shrink_wrap(ds_image, self.threshold, self.gauss_sigma)
200
192
 
201
193
 
194
+ class Gauss1SW:
195
+ def __init__(self, gauss_sigma, threshold):
196
+ self.gauss_sigma = gauss_sigma
197
+ self.threshold = threshold
198
+
199
+ def apply_trigger(self, *args):
200
+ ds_image = args[0]
201
+ return dvut.shrink_wrap(ds_image, self.threshold + .01, self.gauss_sigma)
202
+
203
+
202
204
  def create_obj(self, params, index=None, beg=None, end=None):
205
+ def check_Gauss_type():
206
+ # for now cohere supports only Gauss type, so the following parameters are mandatory
207
+ if 'shrink_wrap_gauss_sigma' not in params:
208
+ print('shrink_wrap_gauss_sigma parameter not defined')
209
+ raise
210
+ if 'shrink_wrap_threshold' not in params:
211
+ print('shrink_wrap_threshold parameter not defined')
212
+ raise
213
+
214
+ def check_Gauss1_type():
215
+ # for now cohere supports only Gauss type, so the following parameters are mandatory
216
+ if 'shrink_wrap_gauss_sigma' not in params:
217
+ print('shrink_wrap_gauss_sigma parameter not defined')
218
+ raise
219
+ if 'shrink_wrap_threshold' not in params:
220
+ print('shrink_wrap_threshold parameter not defined')
221
+ raise
222
+
223
+ if 'shrink_wrap_type' not in params:
224
+ print('shrink_wrap_type parameter not defined')
225
+ raise
226
+
203
227
  if index is None:
204
- if 'shrink_wrap_type' not in params:
205
- sw_type = 'GAUSS'
206
- else:
207
- sw_type = params['shrink_wrap_type']
228
+ sw_type = params['shrink_wrap_type']
208
229
  if sw_type == 'GAUSS':
209
- if 'shrink_wrap_gauss_sigma' not in params:
210
- sigma = 1.0
211
- else:
212
- sigma = params['shrink_wrap_gauss_sigma']
213
- if 'shrink_wrap_threshold' not in params:
214
- threshold = 0.1
215
- else:
216
- threshold = params['shrink_wrap_threshold']
217
- else:
218
- if 'shrink_wrap_type' not in params or len(params['shrink_wrap_type']) - 1 < index:
219
- sw_type = 'GAUSS'
230
+ check_Gauss_type()
231
+ sigma = params['shrink_wrap_gauss_sigma']
232
+ threshold = params['shrink_wrap_threshold']
233
+ return self.GaussSW(sigma, threshold)
234
+ elif sw_type == 'GAUSS1':
235
+ check_Gauss1_type()
236
+ sigma = params['shrink_wrap_gauss_sigma']
237
+ threshold = params['shrink_wrap_threshold']
238
+ return self.Gauss1SW(sigma, threshold)
220
239
  else:
221
- sw_type = params['shrink_wrap_type'][index]
240
+ print(f'{sw_type} shrink wrap type is not supported')
241
+ raise
242
+ else:
243
+ if len(params['shrink_wrap_type']) - 1 < index:
244
+ print(f'shrink_wrap_type not defined for sub-trigger {index}')
245
+ raise
246
+ sw_type = params['shrink_wrap_type'][index]
222
247
  if sw_type == 'GAUSS':
223
- if 'shrink_wrap_gauss_sigma' not in params or len(params['shrink_wrap_gauss_sigma']) - 1 < index:
224
- sigma = 1.0
225
- else:
226
- sigma = params['shrink_wrap_gauss_sigma'][index]
227
- if 'shrink_wrap_threshold' not in params or len(params['shrink_wrap_threshold']) - 1 < index:
228
- threshold = 0.1
229
- else:
230
- threshold = params['shrink_wrap_threshold'][index]
231
- return self.GaussSW(sigma, threshold)
232
-
233
-
234
- class PhaseMod(Feature):
235
- def __init__(self, params, sub_rows_trigs):
236
- self.key = 'PHM'
237
- self.trig_name = 'phm_trigger'
238
- super().__init__(sub_rows_trigs, params)
239
-
240
- class PhasePHM:
241
- def __init__(self, phm_phase_min, phm_phase_max):
242
- self.phm_phase_min = phm_phase_min
243
- self.phm_phase_max = phm_phase_max
248
+ check_Gauss_type()
249
+ if len(params['shrink_wrap_gauss_sigma']) - 1 < index:
250
+ print(f'shrink_wrap_gauss_sigma not defined for sub-trigger {index}')
251
+ raise
252
+ sigma = params['shrink_wrap_gauss_sigma'][index]
253
+ if len(params['shrink_wrap_threshold']) - 1 < index:
254
+ print(f'shrink_wrap_threshold not defined for sub-trigger {index}')
255
+ raise
256
+ threshold = params['shrink_wrap_threshold'][index]
257
+ return self.GaussSW(sigma, threshold)
258
+ elif sw_type == 'GAUSS1':
259
+ check_Gauss1_type()
260
+ if len(params['shrink_wrap_gauss_sigma']) - 1 < index:
261
+ print(f'shrink_wrap_gauss_sigma not defined for sub-trigger {index}')
262
+ raise
263
+ sigma = params['shrink_wrap_gauss_sigma'][index]
264
+ if len(params['shrink_wrap_threshold']) - 1 < index:
265
+ print(f'shrink_wrap_threshold not defined for sub-trigger {index}')
266
+ raise
267
+ threshold = params['shrink_wrap_threshold'][index]
268
+ return self.Gauss1SW(sigma, threshold)
269
+ else:
270
+ print(f'{sw_type} shrink wrap type is not supported')
271
+ raise
272
+
273
+
274
+
275
+ class PhaseConstrain(TriggeredOp):
276
+ def __init__(self, trig_op):
277
+ super().__init__(trig_op)
278
+
279
+ class PhasePHC:
280
+ def __init__(self, phc_phase_min, phc_phase_max):
281
+ self.phc_phase_min = phc_phase_min
282
+ self.phc_phase_max = phc_phase_max
244
283
 
245
284
  def apply_trigger(self, *args):
246
- # print('phm trig, phase min', self.phm_phase_min)
247
285
  ds_image = args[0]
248
286
  phase = devlib.angle(ds_image)
249
- return (phase > self.phm_phase_min) & (phase < self.phm_phase_max)
287
+ return (phase > self.phc_phase_min) & (phase < self.phc_phase_max)
250
288
 
251
289
 
252
290
  def create_obj(self, params, index=None, beg=None, end=None):
291
+ if 'phc_phase_min' not in params:
292
+ print('phc_phase_min parameter not defined')
293
+ raise
294
+ if 'phc_phase_max' not in params:
295
+ print('phc_phase_max parameter not defined')
296
+ raise
253
297
  if index is None:
254
- if 'phm_phase_min' not in params:
255
- phase_min = -1.57
256
- else:
257
- phase_min = params['phm_phase_min']
258
- if 'phm_phase_max' not in params:
259
- phase_max = 1.57
260
- else:
261
- phase_max = params['phm_phase_max']
298
+ phase_min = params['phc_phase_min']
299
+ phase_max = params['phc_phase_max']
262
300
  else:
263
- if 'phm_phase_min' not in params or len(params['phm_phase_min']) - 1 < index:
264
- phase_min = -1.57
265
- else:
266
- phase_min = params['phm_phase_min'][index]
267
- if 'phm_phase_max' not in params or len(params['phm_phase_max']) - 1 < index:
268
- phase_max = 1.57
269
- else:
270
- phase_max = params['phm_phase_max'][index]
271
- return self.PhasePHM(phase_min, phase_max)
301
+ if len(params['phc_phase_min']) - 1 < index:
302
+ print(f'phc_phase_min not defined for sub-trigger {index}')
303
+ raise
304
+ phase_min = params['phc_phase_min'][index]
305
+ if len(params['phc_phase_max']) - 1 < index:
306
+ print(f'phc_phase_max not defined for sub-trigger {index}')
307
+ raise
308
+ phase_max = params['phc_phase_max'][index]
309
+ return self.PhasePHC(phase_min, phase_max)
272
310
 
273
311
 
274
- class LowPassFilter(Feature):
275
- def __init__(self, params, sub_rows_trigs):
276
- self.key = 'LPF'
277
- self.trig_name = 'lowpass_filter_trigger'
278
- super().__init__(sub_rows_trigs, params)
312
+ class LowPassFilter(TriggeredOp):
313
+ def __init__(self, trig_op):
314
+ super().__init__(trig_op)
279
315
 
280
316
  class Filter:
281
317
  def __init__(self, filter_range, iter_range, threshold):
@@ -292,30 +328,52 @@ class LowPassFilter(Feature):
292
328
  iter = args[1]
293
329
  ds_image = args[2]
294
330
  filter_sigma = self.filter_sigmas[iter - self.iter_offset]
295
- # print('in apply_low_filter, filter_sigma', filter_sigma)
296
331
  return (devlib.gaussian_filter(data, filter_sigma), dvut.shrink_wrap(ds_image, self.threshold, 1/filter_sigma))
297
332
 
298
333
 
299
334
  def create_obj(self, params, index=None, beg=None, end=None):
335
+ if 'lowpass_filter_sw_threshold' not in params:
336
+ print('lowpass_filter_sw_threshold parameter not defined')
337
+ raise
338
+ if 'lowpass_filter_range' not in params:
339
+ print('lowpass_filter_range parameter not defined')
340
+ raise
300
341
  # in case of a all_iteration configuration the lowpass filter
301
342
  # iteration can be read from lowpass_filter_trigger
302
343
  if index is None:
303
344
  iter_range = (params['lowpass_filter_trigger'][0], params['lowpass_filter_trigger'][2])
304
345
  filter_range = params['lowpass_filter_range']
305
- if 'lowpass_filter_threshold' in params:
306
- threshold = params['lowpass_filter_threshold']
307
- else:
308
- threshold = .1
346
+ threshold = params['lowpass_filter_sw_threshold']
309
347
  # otherwise the iterations to apply this instance are given as arguments
310
348
  else:
311
349
  iter_range = (beg, end)
350
+ if len(params['lowpass_filter_range']) - 1 < index:
351
+ print(f'lowpass_filter_range not defined for sub-trigger {index}')
352
+ raise
312
353
  filter_range = params['lowpass_filter_range'][index]
313
- if 'lowpass_filter_threshold' not in params or len(params['lowpass_filter_threshold']) - 1 < index:
314
- threshold = .1
315
- else:
316
- threshold = params['lowpass_filter_threshold'][index]
354
+ if len(params['lowpass_filter_sw_threshold']) - 1 < index:
355
+ print(f'lowpass_filter_sw_threshold not defined for sub-trigger {index}')
356
+ raise
357
+ threshold = params['lowpass_filter_sw_threshold'][index]
317
358
  if len(filter_range) == 1:
318
359
  filter_range.append(1.0)
319
360
  return self.Filter(filter_range, iter_range, threshold)
320
361
 
362
+ def create(trig_op, params, trig_op_info):
363
+ if trig_op == 'shrink_wrap':
364
+ to = ShrinkWrap(trig_op)
365
+ if trig_op == 'phc':
366
+ to = PhaseConstrain(trig_op)
367
+ if trig_op == 'lowpass_filter':
368
+ to = LowPassFilter(trig_op)
369
+
370
+ # this function sets self.objs and self.f and creates all objects
371
+ try:
372
+ to.create_objs(params, trig_op_info)
373
+ return to
374
+ except:
375
+ return None
376
+
377
+
378
+
321
379