|
26 | 26 | Match, Maybe, Optional, Range, Required, Schema |
27 | 27 | from CPAC import docs_prefix |
28 | 28 | from CPAC.utils.datatypes import ListFromItem |
29 | | -from CPAC.utils.utils import delete_nested_value, lookup_nested_value, \ |
30 | | - set_nested_value |
31 | 29 |
|
32 | 30 | # 1 or more digits, optional decimal, 'e', optional '-', 1 or more digits |
33 | 31 | scientific_notation_str_regex = r'^([0-9]+(\.[0-9]*)*(e)-{0,1}[0-9]+)*$' |
@@ -204,166 +202,6 @@ def permutation_message(key, options): |
204 | 202 | ''' |
205 | 203 |
|
206 | 204 |
|
207 | | -def _combine_labels(config_dict, list_to_combine, new_key): |
208 | | - ''' |
209 | | - Helper function to combine formerly separate keys into a |
210 | | - combined key. |
211 | | -
|
212 | | - Parameters |
213 | | - ---------- |
214 | | - config_dict: dict |
215 | | -
|
216 | | - key_sequence: iterable of lists or tuples |
217 | | -
|
218 | | - new_key: list or tuple |
219 | | -
|
220 | | - Returns |
221 | | - ------- |
222 | | - updated_config_dict: dict |
223 | | - ''' |
224 | | - new_value = [] |
225 | | - any_old_values = False |
226 | | - for _to_combine in list_to_combine: |
227 | | - try: |
228 | | - old_value = lookup_nested_value(config_dict, _to_combine) |
229 | | - except KeyError: |
230 | | - old_value = None |
231 | | - if old_value is not None: |
232 | | - any_old_values = True |
233 | | - if isinstance(old_value, (list, set, tuple)): |
234 | | - for value in old_value: |
235 | | - new_value.append(value) |
236 | | - else: |
237 | | - new_value.append(old_value) |
238 | | - config_dict = delete_nested_value(config_dict, _to_combine) |
239 | | - if any_old_values: |
240 | | - return set_nested_value(config_dict, new_key, new_value) |
241 | | - return config_dict |
242 | | - |
243 | | - |
244 | | -def _now_runswitch(config_dict, key_sequence): |
245 | | - ''' |
246 | | - Helper function to convert a formerly forkable value to a |
247 | | - runswitch. |
248 | | -
|
249 | | - Parameters |
250 | | - ---------- |
251 | | - config_dict: dict |
252 | | -
|
253 | | - key_sequence: list or tuple |
254 | | -
|
255 | | - Returns |
256 | | - ------- |
257 | | - updated_config_dict: dict |
258 | | - ''' |
259 | | - try: |
260 | | - old_forkable = lookup_nested_value(config_dict, key_sequence) |
261 | | - except KeyError: |
262 | | - return config_dict |
263 | | - if isinstance(old_forkable, bool) or isinstance(old_forkable, list): |
264 | | - return set_nested_value( |
265 | | - config_dict, key_sequence, {'run': old_forkable}) |
266 | | - return config_dict |
267 | | - |
268 | | - |
269 | | -def _changes_1_8_0_to_1_8_1(config_dict): |
270 | | - ''' |
271 | | - Examples |
272 | | - -------- |
273 | | - Starting with 1.8.0 |
274 | | - >>> zero = {'anatomical_preproc': { |
275 | | - ... 'non_local_means_filtering': True, |
276 | | - ... 'n4_bias_field_correction': True |
277 | | - ... }, 'functional_preproc': { |
278 | | - ... 'motion_estimates_and_correction': { |
279 | | - ... 'calculate_motion_first': False |
280 | | - ... } |
281 | | - ... }, 'segmentation': { |
282 | | - ... 'tissue_segmentation': { |
283 | | - ... 'ANTs_Prior_Based': { |
284 | | - ... 'CSF_label': 0, |
285 | | - ... 'left_GM_label': 1, |
286 | | - ... 'right_GM_label': 2, |
287 | | - ... 'left_WM_label': 3, |
288 | | - ... 'right_WM_label': 4}}}} |
289 | | - >>> updated_apb = _changes_1_8_0_to_1_8_1(zero)[ |
290 | | - ... 'segmentation']['tissue_segmentation']['ANTs_Prior_Based'] |
291 | | - >>> updated_apb['CSF_label'] |
292 | | - [0] |
293 | | - >>> updated_apb['GM_label'] |
294 | | - [1, 2] |
295 | | - >>> updated_apb['WM_label'] |
296 | | - [3, 4] |
297 | | -
|
298 | | - Starting with 1.8.1 |
299 | | - >>> one = {'anatomical_preproc': { |
300 | | - ... 'non_local_means_filtering': True, |
301 | | - ... 'n4_bias_field_correction': True |
302 | | - ... }, 'functional_preproc': { |
303 | | - ... 'motion_estimates_and_correction': { |
304 | | - ... 'calculate_motion_first': False |
305 | | - ... } |
306 | | - ... }, 'segmentation': { |
307 | | - ... 'tissue_segmentation': { |
308 | | - ... 'ANTs_Prior_Based': { |
309 | | - ... 'CSF_label': [0], |
310 | | - ... 'GM_label': [1, 2], |
311 | | - ... 'WM_label': [3, 4]}}}} |
312 | | - >>> updated_apb = _changes_1_8_0_to_1_8_1(one)[ |
313 | | - ... 'segmentation']['tissue_segmentation']['ANTs_Prior_Based'] |
314 | | - >>> updated_apb['CSF_label'] |
315 | | - [0] |
316 | | - >>> updated_apb['GM_label'] |
317 | | - [1, 2] |
318 | | - >>> updated_apb['WM_label'] |
319 | | - [3, 4] |
320 | | - ''' |
321 | | - for key_sequence in { |
322 | | - ('anatomical_preproc', 'non_local_means_filtering'), |
323 | | - ('anatomical_preproc', 'n4_bias_field_correction') |
324 | | - }: |
325 | | - config_dict = _now_runswitch(config_dict, key_sequence) |
326 | | - for combiners in { |
327 | | - (( |
328 | | - ('segmentation', 'tissue_segmentation', 'ANTs_Prior_Based', |
329 | | - 'CSF_label'), |
330 | | - ), ('segmentation', 'tissue_segmentation', 'ANTs_Prior_Based', |
331 | | - 'CSF_label')), |
332 | | - (( |
333 | | - ('segmentation', 'tissue_segmentation', 'ANTs_Prior_Based', |
334 | | - 'left_GM_label'), |
335 | | - ('segmentation', 'tissue_segmentation', 'ANTs_Prior_Based', |
336 | | - 'right_GM_label') |
337 | | - ), ('segmentation', 'tissue_segmentation', 'ANTs_Prior_Based', |
338 | | - 'GM_label')), |
339 | | - (( |
340 | | - ('segmentation', 'tissue_segmentation', 'ANTs_Prior_Based', |
341 | | - 'left_WM_label'), |
342 | | - ('segmentation', 'tissue_segmentation', 'ANTs_Prior_Based', |
343 | | - 'right_WM_label') |
344 | | - ), ('segmentation', 'tissue_segmentation', 'ANTs_Prior_Based', |
345 | | - 'WM_label')) |
346 | | - }: |
347 | | - config_dict = _combine_labels(config_dict, *combiners) |
348 | | - try: |
349 | | - calculate_motion_first = lookup_nested_value( |
350 | | - config_dict, |
351 | | - ['functional_preproc', 'motion_estimates_and_correction', |
352 | | - 'calculate_motion_first'] |
353 | | - ) |
354 | | - except KeyError: |
355 | | - calculate_motion_first = None |
356 | | - if calculate_motion_first is not None: |
357 | | - del config_dict['functional_preproc'][ |
358 | | - 'motion_estimates_and_correction']['calculate_motion_first'] |
359 | | - config_dict = set_nested_value(config_dict, [ |
360 | | - 'functional_preproc', 'motion_estimates_and_correction', |
361 | | - 'motion_estimates', 'calculate_motion_first' |
362 | | - ], calculate_motion_first) |
363 | | - |
364 | | - return config_dict |
365 | | - |
366 | | - |
367 | 205 | def sanitize(filename): |
368 | 206 | '''Sanitize a filename and replace whitespaces with underscores''' |
369 | 207 | return re.sub(r'\s+', '_', sanitize_filename(filename)) |
@@ -1111,6 +949,7 @@ def schema(config_dict): |
1111 | 949 | ------- |
1112 | 950 | dict |
1113 | 951 | ''' |
| 952 | + from CPAC.utils.utils import _changes_1_8_0_to_1_8_1 |
1114 | 953 | partially_validated = latest_schema(_changes_1_8_0_to_1_8_1(config_dict)) |
1115 | 954 | try: |
1116 | 955 | if (partially_validated['registration_workflows'][ |
|
0 commit comments