comparison msnc_tools.py @ 1:e4778148df6b draft

"planemo upload for repository https://github.com/rolfverberg/galaxytools commit 7bc44bd6a5a7d503fc4d6f6fb67b3a04a631430b"
author rv43
date Thu, 31 Mar 2022 18:24:16 +0000
parents cb1b0d757704
children f9c52762c32c
comparison
equal deleted inserted replaced
0:cb1b0d757704 1:e4778148df6b
34 def is_int(v, v_min=None, v_max=None): 34 def is_int(v, v_min=None, v_max=None):
35 """Value is an integer in range v_min <= v <= v_max. 35 """Value is an integer in range v_min <= v <= v_max.
36 """ 36 """
37 if not isinstance(v, int): 37 if not isinstance(v, int):
38 return False 38 return False
39 if (v_min != None and v < v_min) or (v_max != None and v > v_max): 39 if (v_min is not None and v < v_min) or (v_max is not None and v > v_max):
40 return False 40 return False
41 return True 41 return True
42 42
43 def is_num(v, v_min=None, v_max=None): 43 def is_num(v, v_min=None, v_max=None):
44 """Value is a number in range v_min <= v <= v_max. 44 """Value is a number in range v_min <= v <= v_max.
45 """ 45 """
46 if not isinstance(v, (int,float)): 46 if not isinstance(v, (int,float)):
47 return False 47 return False
48 if (v_min != None and v < v_min) or (v_max != None and v > v_max): 48 if (v_min is not None and v < v_min) or (v_max is not None and v > v_max):
49 return False 49 return False
50 return True 50 return True
51 51
52 def is_index(v, v_min=0, v_max=None): 52 def is_index(v, v_min=0, v_max=None):
53 """Value is an array index in range v_min <= v < v_max. 53 """Value is an array index in range v_min <= v < v_max.
54 """ 54 """
55 if not isinstance(v, int): 55 if not isinstance(v, int):
56 return False 56 return False
57 if v < v_min or (v_max != None and v >= v_max): 57 if v < v_min or (v_max is not None and v >= v_max):
58 return False 58 return False
59 return True 59 return True
60 60
61 def is_index_range(v, v_min=0, v_max=None): 61 def is_index_range(v, v_min=0, v_max=None):
62 """Value is an array index range in range v_min <= v[0] <= v[1] < v_max. 62 """Value is an array index range in range v_min <= v[0] <= v[1] <= v_max.
63 """ 63 """
64 if not (isinstance(v, list) and len(v) == 2 and isinstance(v[0], int) and 64 if not (isinstance(v, list) and len(v) == 2 and isinstance(v[0], int) and
65 isinstance(v[1], int)): 65 isinstance(v[1], int)):
66 return False 66 return False
67 if not 0 <= v[0] < v[1] or (v_max != None and v[1] >= v_max): 67 if not 0 <= v[0] <= v[1] or (v_max is not None and v[1] > v_max):
68 return False 68 return False
69 return True 69 return True
70 70
71 def illegal_value(name, value, location=None, exit_flag=False): 71 def illegal_value(name, value, location=None, exit_flag=False):
72 if not isinstance(location, str): 72 if not isinstance(location, str):
81 exit(1) 81 exit(1)
82 82
83 def get_trailing_int(string): 83 def get_trailing_int(string):
84 indexRegex = re.compile(r'\d+$') 84 indexRegex = re.compile(r'\d+$')
85 mo = indexRegex.search(string) 85 mo = indexRegex.search(string)
86 if mo == None: 86 if mo is None:
87 return None 87 return None
88 else: 88 else:
89 return int(mo.group()) 89 return int(mo.group())
90 90
91 def findImageFiles(path, filetype, name=None): 91 def findImageFiles(path, filetype, name=None):
106 if num_imgs < 1: 106 if num_imgs < 1:
107 logging.warning('No available'+name+'files') 107 logging.warning('No available'+name+'files')
108 return -1, 0, [] 108 return -1, 0, []
109 first_index = indexRegex.search(files[0]).group() 109 first_index = indexRegex.search(files[0]).group()
110 last_index = indexRegex.search(files[-1]).group() 110 last_index = indexRegex.search(files[-1]).group()
111 if first_index == None or last_index == None: 111 if first_index is None or last_index is None:
112 logging.error('Unable to find correctly indexed'+name+'images') 112 logging.error('Unable to find correctly indexed'+name+'images')
113 return -1, 0, [] 113 return -1, 0, []
114 first_index = int(first_index) 114 first_index = int(first_index)
115 last_index = int(last_index) 115 last_index = int(last_index)
116 if num_imgs != last_index-first_index+1: 116 if num_imgs != last_index-first_index+1:
149 'use this value ([y]/n)? ', blank=True) 149 'use this value ([y]/n)? ', blank=True)
150 else: 150 else:
151 use_input = pyip.inputYesNo('\nCurrent'+name+'first index/offset = '+ 151 use_input = pyip.inputYesNo('\nCurrent'+name+'first index/offset = '+
152 f'{first_index}/{offset}, use these values ([y]/n)? ', 152 f'{first_index}/{offset}, use these values ([y]/n)? ',
153 blank=True) 153 blank=True)
154 if use_input != 'no': 154 if num_required is None:
155 use_input = pyip.inputYesNo('Current number of'+name+'images = '+ 155 if use_input != 'no':
156 f'{num_imgs}, use this value ([y]/n)? ', 156 use_input = pyip.inputYesNo('Current number of'+name+'images = '+
157 blank=True) 157 f'{num_imgs}, use this value ([y]/n)? ',
158 if use_input == 'yes': 158 blank=True)
159 if use_input != 'no':
159 return first_index, offset, num_imgs 160 return first_index, offset, num_imgs
160 161
161 # Check range against requirements 162 # Check range against requirements
162 if num_imgs < 1: 163 if num_imgs < 1:
163 logging.warning('No available'+name+'images') 164 logging.warning('No available'+name+'images')
164 return -1, -1, 0 165 return -1, -1, 0
165 if num_required == None: 166 if num_required is None:
166 if num_imgs == 1: 167 if num_imgs == 1:
167 return first_index, 0, 1 168 return first_index, 0, 1
168 else: 169 else:
169 if not is_int(num_required, 1): 170 if not is_int(num_required, 1):
170 illegal_value('num_required', num_required, 'selectImageRange') 171 illegal_value('num_required', num_required, 'selectImageRange')
173 logging.error('Unable to find the required'+name+ 174 logging.error('Unable to find the required'+name+
174 f'images ({num_imgs} out of {num_required})') 175 f'images ({num_imgs} out of {num_required})')
175 return -1, -1, 0 176 return -1, -1, 0
176 177
177 # Select index range 178 # Select index range
178 if num_required == None: 179 print('\nThe number of available'+name+f'images is {num_imgs}')
180 if num_required is None:
179 last_index = first_index+num_imgs 181 last_index = first_index+num_imgs
180 use_all = f'Use all ([{first_index}, {last_index}])' 182 use_all = f'Use all ([{first_index}, {last_index}])'
181 pick_offset = 'Pick a first index offset and a number of images' 183 pick_offset = 'Pick a first index offset and a number of images'
182 pick_bounds = 'Pick the first and last index' 184 pick_bounds = 'Pick the first and last index'
183 menuchoice = pyip.inputMenu([use_all, pick_offset, pick_bounds], numbered=True) 185 menuchoice = pyip.inputMenu([use_all, pick_offset, pick_bounds], numbered=True)
227 return None 229 return None
228 if not img_y_bounds: 230 if not img_y_bounds:
229 img_y_bounds = [0, img_read.shape[1]] 231 img_y_bounds = [0, img_read.shape[1]]
230 else: 232 else:
231 if (not isinstance(img_y_bounds, list) or len(img_y_bounds) != 2 or 233 if (not isinstance(img_y_bounds, list) or len(img_y_bounds) != 2 or
232 not (0 <= img_y_bounds[0] < img_y_bounds[1] <= img_read.shape[0])): 234 not (0 <= img_y_bounds[0] < img_y_bounds[1] <= img_read.shape[1])):
233 logging.error(f'inconsistent column dimension in {f}') 235 logging.error(f'inconsistent column dimension in {f}')
234 return None 236 return None
235 return img_read[img_x_bounds[0]:img_x_bounds[1],img_y_bounds[0]:img_y_bounds[1]] 237 return img_read[img_x_bounds[0]:img_x_bounds[1],img_y_bounds[0]:img_y_bounds[1]]
236 238
237 def loadImageStack(files, filetype, img_offset, num_imgs, num_img_skip=0, 239 def loadImageStack(files, filetype, img_offset, num_imgs, num_img_skip=0,
294 return 296 return
295 plt.close(fig=re.sub(r"\s+", '_', title)) 297 plt.close(fig=re.sub(r"\s+", '_', title))
296 298
297 def quickImshow(a, title=None, path=None, name=None, save_fig=False, save_only=False, 299 def quickImshow(a, title=None, path=None, name=None, save_fig=False, save_only=False,
298 clear=True, **kwargs): 300 clear=True, **kwargs):
299 if title != None and not isinstance(title, str): 301 if title is not None and not isinstance(title, str):
300 illegal_value('title', title, 'quickImshow') 302 illegal_value('title', title, 'quickImshow')
301 return 303 return
302 if path is not None and not isinstance(path, str): 304 if path is not None and not isinstance(path, str):
303 illegal_value('path', path, 'quickImshow') 305 illegal_value('path', path, 'quickImshow')
304 return 306 return
341 plt.savefig(path) 343 plt.savefig(path)
342 plt.pause(1) 344 plt.pause(1)
343 345
344 def quickPlot(*args, title=None, path=None, name=None, save_fig=False, save_only=False, 346 def quickPlot(*args, title=None, path=None, name=None, save_fig=False, save_only=False,
345 clear=True, **kwargs): 347 clear=True, **kwargs):
346 if title != None and not isinstance(title, str): 348 if title is not None and not isinstance(title, str):
347 illegal_value('title', title, 'quickPlot') 349 illegal_value('title', title, 'quickPlot')
348 return 350 return
349 if path is not None and not isinstance(path, str): 351 if path is not None and not isinstance(path, str):
350 illegal_value('path', path, 'quickPlot') 352 illegal_value('path', path, 'quickPlot')
351 return 353 return
400 """Interactively select the lower and upper data bounds for a numpy array. 402 """Interactively select the lower and upper data bounds for a numpy array.
401 """ 403 """
402 if not isinstance(a, np.ndarray) or a.ndim != 1: 404 if not isinstance(a, np.ndarray) or a.ndim != 1:
403 logging.error('Illegal array type or dimension in selectArrayBounds') 405 logging.error('Illegal array type or dimension in selectArrayBounds')
404 return None 406 return None
405 if num_x_min == None: 407 x_low_save = x_low
408 x_upp_save = x_upp
409 num_x_min_save = num_x_min
410 if num_x_min is None:
406 num_x_min = 1 411 num_x_min = 1
407 else: 412 else:
408 if num_x_min < 2 or num_x_min > a.size: 413 if num_x_min < 2 or num_x_min > a.size:
409 logging.warning('Illegal input for num_x_min in selectArrayBounds, input ignored') 414 logging.warning('Illegal input for num_x_min in selectArrayBounds, input ignored')
410 num_x_min = 1 415 num_x_min = 1
411 if x_low == None: 416 if x_low is None:
412 x_min = 0 417 x_min = 0
413 x_max = a.size 418 x_max = a.size
414 x_low_max = a.size-num_x_min 419 x_low_max = a.size-num_x_min
415 while True: 420 while True:
416 quickPlot(range(x_min, x_max), a[x_min:x_max], title=title) 421 quickPlot(range(x_min, x_max), a[x_min:x_max], title=title)
427 break 432 break
428 else: 433 else:
429 if not is_int(x_low, 0, a.size-num_x_min): 434 if not is_int(x_low, 0, a.size-num_x_min):
430 illegal_value('x_low', x_low, 'selectArrayBounds') 435 illegal_value('x_low', x_low, 'selectArrayBounds')
431 return None 436 return None
432 if x_upp == None: 437 if x_upp is None:
433 x_min = x_low+num_x_min 438 x_min = x_low+num_x_min
434 x_max = a.size 439 x_max = a.size
435 x_upp_min = x_min 440 x_upp_min = x_min
436 while True: 441 while True:
437 quickPlot(range(x_min, x_max), a[x_min:x_max], title=title) 442 quickPlot(range(x_min, x_max), a[x_min:x_max], title=title)
454 print(f'lower bound = {x_low} (inclusive)\nupper bound = {x_upp} (exclusive)]') 459 print(f'lower bound = {x_low} (inclusive)\nupper bound = {x_upp} (exclusive)]')
455 #quickPlot(range(bounds[0], bounds[1]), a[bounds[0]:bounds[1]], title=title) 460 #quickPlot(range(bounds[0], bounds[1]), a[bounds[0]:bounds[1]], title=title)
456 quickPlot((range(a.size), a), ([bounds[0], bounds[0]], [a.min(), a.max()], 'r-'), 461 quickPlot((range(a.size), a), ([bounds[0], bounds[0]], [a.min(), a.max()], 'r-'),
457 ([bounds[1], bounds[1]], [a.min(), a.max()], 'r-'), title=title) 462 ([bounds[1], bounds[1]], [a.min(), a.max()], 'r-'), title=title)
458 if pyip.inputYesNo('Accept these bounds ([y]/n)?: ', blank=True) == 'no': 463 if pyip.inputYesNo('Accept these bounds ([y]/n)?: ', blank=True) == 'no':
459 bounds = selectArrayBounds(a, title=title) 464 bounds = selectArrayBounds(a, x_low_save, x_upp_save, num_x_min_save, title=title)
460 return bounds 465 return bounds
461 466
462 def selectImageBounds(a, axis, low=None, upp=None, num_min=None, 467 def selectImageBounds(a, axis, low=None, upp=None, num_min=None,
463 title='select array bounds'): 468 title='select array bounds'):
464 """Interactively select the lower and upper data bounds for a 2D numpy array. 469 """Interactively select the lower and upper data bounds for a 2D numpy array.
467 logging.error('Illegal array type or dimension in selectImageBounds') 472 logging.error('Illegal array type or dimension in selectImageBounds')
468 return None 473 return None
469 if axis < 0 or axis >= a.ndim: 474 if axis < 0 or axis >= a.ndim:
470 illegal_value('axis', axis, 'selectImageBounds') 475 illegal_value('axis', axis, 'selectImageBounds')
471 return None 476 return None
472 if num_min == None: 477 low_save = low
478 upp_save = upp
479 num_min_save = num_min
480 if num_min is None:
473 num_min = 1 481 num_min = 1
474 else: 482 else:
475 if num_min < 2 or num_min > a.shape[axis]: 483 if num_min < 2 or num_min > a.shape[axis]:
476 logging.warning('Illegal input for num_min in selectImageBounds, input ignored') 484 logging.warning('Illegal input for num_min in selectImageBounds, input ignored')
477 num_min = 1 485 num_min = 1
478 if low == None: 486 if low is None:
479 min_ = 0 487 min_ = 0
480 max_ = a.shape[axis] 488 max_ = a.shape[axis]
481 low_max = a.shape[axis]-num_min 489 low_max = a.shape[axis]-num_min
482 while True: 490 while True:
483 if axis: 491 if axis:
499 break 507 break
500 else: 508 else:
501 if not is_int(low, 0, a.shape[axis]-num_min): 509 if not is_int(low, 0, a.shape[axis]-num_min):
502 illegal_value('low', low, 'selectImageBounds') 510 illegal_value('low', low, 'selectImageBounds')
503 return None 511 return None
504 if upp == None: 512 if upp is None:
505 min_ = low+num_min 513 min_ = low+num_min
506 max_ = a.shape[axis] 514 max_ = a.shape[axis]
507 upp_min = min_ 515 upp_min = min_
508 while True: 516 while True:
509 if axis: 517 if axis:
536 a_tmp[bounds[0],:] = a.max() 544 a_tmp[bounds[0],:] = a.max()
537 a_tmp[bounds[1],:] = a.max() 545 a_tmp[bounds[1],:] = a.max()
538 print(f'lower bound = {low} (inclusive)\nupper bound = {upp} (exclusive)') 546 print(f'lower bound = {low} (inclusive)\nupper bound = {upp} (exclusive)')
539 quickImshow(a_tmp, title=title) 547 quickImshow(a_tmp, title=title)
540 if pyip.inputYesNo('Accept these bounds ([y]/n)?: ', blank=True) == 'no': 548 if pyip.inputYesNo('Accept these bounds ([y]/n)?: ', blank=True) == 'no':
541 bounds = selectImageBounds(a, title=title) 549 bounds = selectImageBounds(a, axis, low=low_save, upp=upp_save, num_min=num_min_save,
550 title=title)
542 return bounds 551 return bounds
543 552
544 def fitStep(x=None, y=None, model='step', form='arctan'): 553 def fitStep(x=None, y=None, model='step', form='arctan'):
545 if not isinstance(y, np.ndarray) or y.ndim != 1: 554 if not isinstance(y, np.ndarray) or y.ndim != 1:
546 logging.error('Illegal array type or dimension for y in fitStep') 555 logging.error('Illegal array type or dimension for y in fitStep')
688 # if item[0].strip() == key: 697 # if item[0].strip() == key:
689 # lines[index] = newline 698 # lines[index] = newline
690 # break 699 # break
691 # else: 700 # else:
692 # # Insert new key/value pair 701 # # Insert new key/value pair
693 # if search_string != None: 702 # if search_string is not None:
694 # if isinstance(search_string, str): 703 # if isinstance(search_string, str):
695 # search_string = [search_string] 704 # search_string = [search_string]
696 # elif not isinstance(search_string, (tuple, list)): 705 # elif not isinstance(search_string, (tuple, list)):
697 # illegal_value('search_string', search_string, 'Config.update') 706 # illegal_value('search_string', search_string, 'Config.update')
698 # search_string = None 707 # search_string = None
699 # update_flag = False 708 # update_flag = False
700 # if search_string != None: 709 # if search_string is not None:
701 # indices = [[index for index,line in enumerate(lines) if item in line] 710 # indices = [[index for index,line in enumerate(lines) if item in line]
702 # for item in search_string] 711 # for item in search_string]
703 # for i,index in enumerate(indices): 712 # for i,index in enumerate(indices):
704 # if index: 713 # if index:
705 # if len(search_string) > 1 and key < search_string[i]: 714 # if len(search_string) > 1 and key < search_string[i]: