comparison msnc_tools.py @ 41:ef5c2f7b49ec draft

"planemo upload for repository https://github.com/rolfverberg/galaxytools commit 0d207c80e38a6019595ebe178f5678372b75f3e7"
author rv43
date Thu, 21 Apr 2022 14:21:38 +0000
parents 845270a96464
children
comparison
equal deleted inserted replaced
40:fa94fe25ca46 41:ef5c2f7b49ec
264 elif filetype == 'h5': 264 elif filetype == 'h5':
265 if not isinstance(files[0], str) and not os.path.isfile(files[0]): 265 if not isinstance(files[0], str) and not os.path.isfile(files[0]):
266 illegal_value('files[0]', files[0], 'loadImageStack') 266 illegal_value('files[0]', files[0], 'loadImageStack')
267 return img_stack 267 return img_stack
268 t0 = time() 268 t0 = time()
269 logging.info(f'Loading {files[0]}')
269 with h5py.File(files[0], 'r') as f: 270 with h5py.File(files[0], 'r') as f:
270 shape = f['entry/instrument/detector/data'].shape 271 shape = f['entry/instrument/detector/data'].shape
271 if len(shape) != 3: 272 if len(shape) != 3:
272 logging.error(f'inconsistent dimensions in {files[0]}') 273 logging.error(f'inconsistent dimensions in {files[0]}')
273 if not img_x_bounds: 274 if not img_x_bounds:
287 img_x_bounds[0]:img_x_bounds[1],img_y_bounds[0]:img_y_bounds[1]] 288 img_x_bounds[0]:img_x_bounds[1],img_y_bounds[0]:img_y_bounds[1]]
288 logging.info(f'... done in {time()-t0:.2f} seconds!') 289 logging.info(f'... done in {time()-t0:.2f} seconds!')
289 else: 290 else:
290 illegal_value('filetype', filetype, 'findImageRange') 291 illegal_value('filetype', filetype, 'findImageRange')
291 return img_stack 292 return img_stack
293
294 def combine_tiffs_in_h5(files, num_imgs, h5_filename):
295 img_stack = loadImageStack(files, 'tif', 0, num_imgs)
296 with h5py.File(h5_filename, 'w') as f:
297 f.create_dataset('entry/instrument/detector/data', data=img_stack)
298 del img_stack
299 return [h5_filename]
292 300
293 def clearFig(title): 301 def clearFig(title):
294 if not isinstance(title, str): 302 if not isinstance(title, str):
295 illegal_value('title', title, 'clearFig') 303 illegal_value('title', title, 'clearFig')
296 return 304 return
335 plt.figure(title) 343 plt.figure(title)
336 plt.imshow(a, extent=extent, **kwargs) 344 plt.imshow(a, extent=extent, **kwargs)
337 if show_grid: 345 if show_grid:
338 ax = plt.gca() 346 ax = plt.gca()
339 ax.grid(color=grid_color, linewidth=grid_linewidth) 347 ax.grid(color=grid_color, linewidth=grid_linewidth)
348 if title != 'quick_imshow':
349 plt.title = title
340 plt.savefig(path) 350 plt.savefig(path)
341 plt.close(fig=title) 351 plt.close(fig=title)
342 #plt.imsave(f'{title}.png', a, **kwargs) 352 #plt.imsave(f'{title}.png', a, **kwargs)
343 else: 353 else:
344 plt.ion() 354 plt.ion()
345 plt.figure(title) 355 plt.figure(title)
346 plt.imshow(a, extent=extent, **kwargs) 356 plt.imshow(a, extent=extent, **kwargs)
347 if show_grid: 357 if show_grid:
348 ax = plt.gca() 358 ax = plt.gca()
349 ax.grid(color=grid_color, linewidth=grid_linewidth) 359 ax.grid(color=grid_color, linewidth=grid_linewidth)
360 if title != 'quick_imshow':
361 plt.title = title
350 if save_fig: 362 if save_fig:
351 plt.savefig(path) 363 plt.savefig(path)
352 plt.pause(1) 364 plt.pause(1)
353 365
354 def quickPlot(*args, title=None, path=None, name=None, save_fig=False, save_only=False, 366 def quickPlot(*args, title=None, path=None, name=None, save_fig=False, save_only=False,
392 else: 404 else:
393 plt.plot(*args, **kwargs) 405 plt.plot(*args, **kwargs)
394 if show_grid: 406 if show_grid:
395 ax = plt.gca() 407 ax = plt.gca()
396 ax.grid(color='k')#, linewidth=1) 408 ax.grid(color='k')#, linewidth=1)
409 if title != 'quick_plot':
410 plt.title = title
397 plt.savefig(path) 411 plt.savefig(path)
398 plt.close(fig=title) 412 plt.close(fig=title)
399 else: 413 else:
400 plt.ion() 414 plt.ion()
401 plt.figure(title) 415 plt.figure(title)
405 else: 419 else:
406 plt.plot(*args, **kwargs) 420 plt.plot(*args, **kwargs)
407 if show_grid: 421 if show_grid:
408 ax = plt.gca() 422 ax = plt.gca()
409 ax.grid(color='k')#, linewidth=1) 423 ax.grid(color='k')#, linewidth=1)
424 if title != 'quick_plot':
425 plt.title = title
410 if save_fig: 426 if save_fig:
411 plt.savefig(path) 427 plt.savefig(path)
412 plt.pause(1) 428 plt.pause(1)
413 429
414 def selectArrayBounds(a, x_low=None, x_upp=None, num_x_min=None, 430 def selectArrayBounds(a, x_low=None, x_upp=None, num_x_min=None,
586 mod = StepModel(form=form) 602 mod = StepModel(form=form)
587 else: 603 else:
588 mod = RectangleModel(form=form) 604 mod = RectangleModel(form=form)
589 pars = mod.guess(y, x=x) 605 pars = mod.guess(y, x=x)
590 out = mod.fit(y, pars, x=x) 606 out = mod.fit(y, pars, x=x)
591 #print(out.fit_report()) 607 #print(f'fit_report:\n{out.fit_report()}')
592 #quickPlot((x,y),(x,out.best_fit)) 608 #quickPlot((x,y),(x,out.best_fit))
593 return out.best_values 609 return out.best_values
594 610
595 class Config: 611 class Config:
596 """Base class for processing a config file or dictionary. 612 """Base class for processing a config file or dictionary.