annotate workflow/run_tomo.py @ 71:1cf15b61cd83 draft

planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
author rv43
date Mon, 20 Mar 2023 13:56:57 +0000
parents fba792d5f83b
children d5e1d4ea2b7e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1 #!/usr/bin/env python3
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
2
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
3 import logging
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
4 logger = logging.getLogger(__name__)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
5
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
6 import numpy as np
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
7 try:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
8 import numexpr as ne
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
9 except:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
10 pass
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
11 try:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
12 import scipy.ndimage as spi
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
13 except:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
14 pass
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
15
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
16 from multiprocessing import cpu_count
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
17 from nexusformat.nexus import *
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
18 from os import mkdir
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
19 from os import path as os_path
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
20 try:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
21 from skimage.transform import iradon
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
22 except:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
23 pass
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
24 try:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
25 from skimage.restoration import denoise_tv_chambolle
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
26 except:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
27 pass
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
28 from time import time
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
29 try:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
30 import tomopy
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
31 except:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
32 pass
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
33 from yaml import safe_load, safe_dump
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
34
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
35 try:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
36 from msnctools.fit import Fit
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
37 except:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
38 from fit import Fit
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
39 try:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
40 from msnctools.general import illegal_value, is_int, is_int_pair, is_num, is_index_range, \
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
41 input_int, input_num, input_yesno, input_menu, draw_mask_1d, select_image_bounds, \
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
42 select_one_image_bound, clear_imshow, quick_imshow, clear_plot, quick_plot
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
43 except:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
44 from general import illegal_value, is_int, is_int_pair, is_num, is_index_range, \
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
45 input_int, input_num, input_yesno, input_menu, draw_mask_1d, select_image_bounds, \
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
46 select_one_image_bound, clear_imshow, quick_imshow, clear_plot, quick_plot
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
47
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
48 try:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
49 from workflow.models import import_scanparser, FlatField, TomoField, TomoWorkflow
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
50 from workflow.__version__ import __version__
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
51 except:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
52 pass
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
53
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
54 num_core_tomopy_limit = 24
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
55
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
56 def nxcopy(nxobject:NXobject, exclude_nxpaths:list[str]=[], nxpath_prefix:str='') -> NXobject:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
57 '''Function that returns a copy of a nexus object, optionally exluding certain child items.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
58
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
59 :param nxobject: the original nexus object to return a "copy" of
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
60 :type nxobject: nexusformat.nexus.NXobject
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
61 :param exlude_nxpaths: a list of paths to child nexus objects that
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
62 should be exluded from the returned "copy", defaults to `[]`
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
63 :type exclude_nxpaths: list[str], optional
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
64 :param nxpath_prefix: For use in recursive calls from inside this
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
65 function only!
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
66 :type nxpath_prefix: str
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
67 :return: a copy of `nxobject` with some children optionally exluded.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
68 :rtype: NXobject
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
69 '''
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
70
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
71 nxobject_copy = nxobject.__class__()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
72 if not len(nxpath_prefix):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
73 if 'default' in nxobject.attrs:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
74 nxobject_copy.attrs['default'] = nxobject.attrs['default']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
75 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
76 for k, v in nxobject.attrs.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
77 nxobject_copy.attrs[k] = v
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
78
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
79 for k, v in nxobject.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
80 nxpath = os_path.join(nxpath_prefix, k)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
81
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
82 if nxpath in exclude_nxpaths:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
83 continue
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
84
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
85 if isinstance(v, NXgroup):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
86 nxobject_copy[k] = nxcopy(v, exclude_nxpaths=exclude_nxpaths,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
87 nxpath_prefix=os_path.join(nxpath_prefix, k))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
88 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
89 nxobject_copy[k] = v
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
90
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
91 return(nxobject_copy)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
92
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
93 class set_numexpr_threads:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
94
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
95 def __init__(self, num_core):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
96 if num_core is None or num_core < 1 or num_core > cpu_count():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
97 self.num_core = cpu_count()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
98 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
99 self.num_core = num_core
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
100
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
101 def __enter__(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
102 self.num_core_org = ne.set_num_threads(self.num_core)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
103
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
104 def __exit__(self, exc_type, exc_value, traceback):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
105 ne.set_num_threads(self.num_core_org)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
106
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
107 class Tomo:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
108 """Processing tomography data with misalignment.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
109 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
110 def __init__(self, galaxy_flag=False, num_core=-1, output_folder='.', save_figs=None,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
111 test_mode=False):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
112 """Initialize with optional config input file or dictionary
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
113 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
114 if not isinstance(galaxy_flag, bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
115 raise ValueError(f'Invalid parameter galaxy_flag ({galaxy_flag})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
116 self.galaxy_flag = galaxy_flag
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
117 self.num_core = num_core
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
118 if self.galaxy_flag:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
119 if output_folder != '.':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
120 logger.warning('Ignoring output_folder in galaxy mode')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
121 self.output_folder = '.'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
122 if test_mode != False:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
123 logger.warning('Ignoring test_mode in galaxy mode')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
124 self.test_mode = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
125 if save_figs is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
126 logger.warning('Ignoring save_figs in galaxy mode')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
127 save_figs = 'only'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
128 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
129 self.output_folder = os_path.abspath(output_folder)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
130 if not os_path.isdir(output_folder):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
131 mkdir(os_path.abspath(output_folder))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
132 if not isinstance(test_mode, bool):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
133 raise ValueError(f'Invalid parameter test_mode ({test_mode})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
134 self.test_mode = test_mode
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
135 if save_figs is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
136 save_figs = 'no'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
137 self.test_config = {}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
138 if self.test_mode:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
139 if save_figs != 'only':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
140 logger.warning('Ignoring save_figs in test mode')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
141 save_figs = 'only'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
142 if save_figs == 'only':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
143 self.save_only = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
144 self.save_figs = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
145 elif save_figs == 'yes':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
146 self.save_only = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
147 self.save_figs = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
148 elif save_figs == 'no':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
149 self.save_only = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
150 self.save_figs = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
151 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
152 raise ValueError(f'Invalid parameter save_figs ({save_figs})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
153 if self.save_only:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
154 self.block = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
155 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
156 self.block = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
157 if self.num_core == -1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
158 self.num_core = cpu_count()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
159 if not is_int(self.num_core, gt=0, log=False):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
160 raise ValueError(f'Invalid parameter num_core ({num_core})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
161 if self.num_core > cpu_count():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
162 logger.warning(f'num_core = {self.num_core} is larger than the number of available '
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
163 f'processors and reduced to {cpu_count()}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
164 self.num_core= cpu_count()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
165
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
166 def read(self, filename):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
167 extension = os_path.splitext(filename)[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
168 if extension == '.yml' or extension == '.yaml':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
169 with open(filename, 'r') as f:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
170 config = safe_load(f)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
171 # if len(config) > 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
172 # raise ValueError(f'Multiple root entries in {filename} not yet implemented')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
173 # if len(list(config.values())[0]) > 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
174 # raise ValueError(f'Multiple sample maps in {filename} not yet implemented')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
175 return(config)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
176 elif extension == '.nxs':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
177 with NXFile(filename, mode='r') as nxfile:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
178 nxroot = nxfile.readfile()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
179 return(nxroot)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
180 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
181 raise ValueError(f'Invalid filename extension ({extension})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
182
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
183 def write(self, data, filename):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
184 extension = os_path.splitext(filename)[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
185 if extension == '.yml' or extension == '.yaml':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
186 with open(filename, 'w') as f:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
187 safe_dump(data, f)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
188 elif extension == '.nxs':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
189 data.save(filename, mode='w')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
190 elif extension == '.nc':
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
191 data.to_netcdf(os_path=filename)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
192 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
193 raise ValueError(f'Invalid filename extension ({extension})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
194
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
195 def gen_reduced_data(self, data, img_x_bounds=None):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
196 """Generate the reduced tomography images.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
197 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
198 logger.info('Generate the reduced tomography images')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
199
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
200 # Create plot galaxy path directory if needed
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
201 if self.galaxy_flag and not os_path.exists('tomo_reduce_plots'):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
202 mkdir('tomo_reduce_plots')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
203
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
204 if isinstance(data, dict):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
205 # Create Nexus format object from input dictionary
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
206 wf = TomoWorkflow(**data)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
207 if len(wf.sample_maps) > 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
208 raise ValueError(f'Multiple sample maps not yet implemented')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
209 # print(f'\nwf:\n{wf}\n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
210 nxroot = NXroot()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
211 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
212 for sample_map in wf.sample_maps:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
213 logger.info(f'Start constructing the {sample_map.title} map.')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
214 import_scanparser(sample_map.station)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
215 sample_map.construct_nxentry(nxroot, include_raw_data=False)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
216 logger.info(f'Constructed all sample maps in {time()-t0:.2f} seconds.')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
217 nxentry = nxroot[nxroot.attrs['default']]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
218 # Get test mode configuration info
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
219 if self.test_mode:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
220 self.test_config = data['sample_maps'][0]['test_mode']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
221 elif isinstance(data, NXroot):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
222 nxentry = data[data.attrs['default']]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
223 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
224 raise ValueError(f'Invalid parameter data ({data})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
225
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
226 # Create an NXprocess to store data reduction (meta)data
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
227 reduced_data = NXprocess()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
228
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
229 # Generate dark field
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
230 if 'dark_field' in nxentry['spec_scans']:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
231 reduced_data = self._gen_dark(nxentry, reduced_data)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
232
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
233 # Generate bright field
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
234 reduced_data = self._gen_bright(nxentry, reduced_data)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
235
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
236 # Set vertical detector bounds for image stack
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
237 img_x_bounds = self._set_detector_bounds(nxentry, reduced_data, img_x_bounds=img_x_bounds)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
238 logger.info(f'img_x_bounds = {img_x_bounds}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
239 reduced_data['img_x_bounds'] = img_x_bounds
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
240
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
241 # Set zoom and/or theta skip to reduce memory the requirement
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
242 zoom_perc, num_theta_skip = self._set_zoom_or_skip()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
243 if zoom_perc is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
244 reduced_data.attrs['zoom_perc'] = zoom_perc
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
245 if num_theta_skip is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
246 reduced_data.attrs['num_theta_skip'] = num_theta_skip
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
247
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
248 # Generate reduced tomography fields
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
249 reduced_data = self._gen_tomo(nxentry, reduced_data)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
250
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
251 # Create a copy of the input Nexus object and remove raw and any existing reduced data
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
252 if isinstance(data, NXroot):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
253 exclude_items = [f'{nxentry._name}/reduced_data/data',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
254 f'{nxentry._name}/instrument/detector/data',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
255 f'{nxentry._name}/instrument/detector/image_key',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
256 f'{nxentry._name}/instrument/detector/sequence_number',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
257 f'{nxentry._name}/sample/rotation_angle',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
258 f'{nxentry._name}/sample/x_translation',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
259 f'{nxentry._name}/sample/z_translation',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
260 f'{nxentry._name}/data/data',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
261 f'{nxentry._name}/data/image_key',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
262 f'{nxentry._name}/data/rotation_angle',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
263 f'{nxentry._name}/data/x_translation',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
264 f'{nxentry._name}/data/z_translation']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
265 nxroot = nxcopy(data, exclude_nxpaths=exclude_items)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
266 nxentry = nxroot[nxroot.attrs['default']]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
267
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
268 # Add the reduced data NXprocess
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
269 nxentry.reduced_data = reduced_data
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
270
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
271 if 'data' not in nxentry:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
272 nxentry.data = NXdata()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
273 nxentry.attrs['default'] = 'data'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
274 nxentry.data.makelink(nxentry.reduced_data.data.tomo_fields, name='reduced_data')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
275 nxentry.data.makelink(nxentry.reduced_data.rotation_angle, name='rotation_angle')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
276 nxentry.data.attrs['signal'] = 'reduced_data'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
277
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
278 return(nxroot)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
279
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
280 def find_centers(self, nxroot, center_rows=None, center_stack_index=None):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
281 """Find the calibrated center axis info
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
282 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
283 logger.info('Find the calibrated center axis info')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
284
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
285 if not isinstance(nxroot, NXroot):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
286 raise ValueError(f'Invalid parameter nxroot ({nxroot})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
287 nxentry = nxroot[nxroot.attrs['default']]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
288 if not isinstance(nxentry, NXentry):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
289 raise ValueError(f'Invalid nxentry ({nxentry})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
290 if self.galaxy_flag:
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
291 if center_rows is not None:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
292 center_rows = tuple(center_rows)
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
293 if not is_int_pair(center_rows):
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
294 raise ValueError(f'Invalid parameter center_rows ({center_rows})')
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
295 elif center_rows is not None:
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
296 logger.warning(f'Ignoring parameter center_rows ({center_rows})')
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
297 center_rows = None
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
298 if self.galaxy_flag:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
299 if center_stack_index is not None and not is_int(center_stack_index, ge=0):
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
300 raise ValueError(f'Invalid parameter center_stack_index ({center_stack_index})')
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
301 elif center_stack_index is not None:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
302 logger.warning(f'Ignoring parameter center_stack_index ({center_stack_index})')
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
303 center_stack_index = None
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
304
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
305 # Create plot galaxy path directory and path if needed
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
306 if self.galaxy_flag:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
307 if not os_path.exists('tomo_find_centers_plots'):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
308 mkdir('tomo_find_centers_plots')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
309 path = 'tomo_find_centers_plots'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
310 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
311 path = self.output_folder
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
312
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
313 # Check if reduced data is available
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
314 if ('reduced_data' not in nxentry or 'reduced_data' not in nxentry.data):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
315 raise KeyError(f'Unable to find valid reduced data in {nxentry}.')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
316
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
317 # Select the image stack to calibrate the center axis
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
318 # reduced data axes order: stack,theta,row,column
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
319 # Note: Nexus cannot follow a link if the data it points to is too big,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
320 # so get the data from the actual place, not from nxentry.data
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
321 tomo_fields_shape = nxentry.reduced_data.data.tomo_fields.shape
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
322 if len(tomo_fields_shape) != 4 or any(True for dim in tomo_fields_shape if not dim):
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
323 raise KeyError('Unable to load the required reduced tomography stack')
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
324 num_tomo_stacks = tomo_fields_shape[0]
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
325 if num_tomo_stacks == 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
326 center_stack_index = 0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
327 default = 'n'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
328 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
329 if self.test_mode:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
330 center_stack_index = self.test_config['center_stack_index']-1 # make offset 0
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
331 elif self.galaxy_flag:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
332 if center_stack_index is None:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
333 center_stack_index = int(num_tomo_stacks/2)
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
334 if center_stack_index >= num_tomo_stacks:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
335 raise ValueError(f'Invalid parameter center_stack_index ({center_stack_index})')
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
336 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
337 center_stack_index = input_int('\nEnter tomography stack index to calibrate the '
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
338 'center axis', ge=1, le=num_tomo_stacks, default=int(1+num_tomo_stacks/2))
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
339 center_stack_index -= 1
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
340 default = 'y'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
341
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
342 # Get thetas (in degrees)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
343 thetas = np.asarray(nxentry.reduced_data.rotation_angle)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
344
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
345 # Get effective pixel_size
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
346 if 'zoom_perc' in nxentry.reduced_data:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
347 eff_pixel_size = 100.*(nxentry.instrument.detector.x_pixel_size/
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
348 nxentry.reduced_data.attrs['zoom_perc'])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
349 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
350 eff_pixel_size = nxentry.instrument.detector.x_pixel_size
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
351
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
352 # Get cross sectional diameter
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
353 cross_sectional_dim = tomo_fields_shape[3]*eff_pixel_size
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
354 logger.debug(f'cross_sectional_dim = {cross_sectional_dim}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
355
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
356 # Determine center offset at sample row boundaries
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
357 logger.info('Determine center offset at sample row boundaries')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
358
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
359 # Lower row center
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
360 if self.test_mode:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
361 lower_row = self.test_config['lower_row']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
362 elif self.galaxy_flag:
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
363 if center_rows is None:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
364 lower_row = 0
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
365 else:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
366 lower_row = min(center_rows)
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
367 if not 0 <= lower_row < tomo_fields_shape[2]-1:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
368 raise ValueError(f'Invalid parameter center_rows ({center_rows})')
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
369 else:
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
370 lower_row = select_one_image_bound(
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
371 nxentry.reduced_data.data.tomo_fields[center_stack_index,0,:,:], 0, bound=0,
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
372 title=f'theta={round(thetas[0], 2)+0}',
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
373 bound_name='row index to find lower center', default=default, raise_error=True)
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
374 logger.debug('Finding center...')
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
375 t0 = time()
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
376 lower_center_offset = self._find_center_one_plane(
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
377 #np.asarray(nxentry.reduced_data.data.tomo_fields[center_stack_index,:,lower_row,:]),
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
378 nxentry.reduced_data.data.tomo_fields[center_stack_index,:,lower_row,:],
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
379 lower_row, thetas, eff_pixel_size, cross_sectional_dim, path=path,
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
380 num_core=self.num_core)
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
381 logger.debug(f'... done in {time()-t0:.2f} seconds')
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
382 logger.debug(f'lower_row = {lower_row:.2f}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
383 logger.debug(f'lower_center_offset = {lower_center_offset:.2f}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
384
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
385 # Upper row center
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
386 if self.test_mode:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
387 upper_row = self.test_config['upper_row']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
388 elif self.galaxy_flag:
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
389 if center_rows is None:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
390 upper_row = tomo_fields_shape[2]-1
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
391 else:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
392 upper_row = max(center_rows)
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
393 if not lower_row < upper_row < tomo_fields_shape[2]:
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
394 raise ValueError(f'Invalid parameter center_rows ({center_rows})')
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
395 else:
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
396 upper_row = select_one_image_bound(
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
397 nxentry.reduced_data.data.tomo_fields[center_stack_index,0,:,:], 0,
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
398 bound=tomo_fields_shape[2]-1, title=f'theta={round(thetas[0], 2)+0}',
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
399 bound_name='row index to find upper center', default=default, raise_error=True)
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
400 logger.debug('Finding center...')
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
401 t0 = time()
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
402 upper_center_offset = self._find_center_one_plane(
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
403 #np.asarray(nxentry.reduced_data.data.tomo_fields[center_stack_index,:,upper_row,:]),
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
404 nxentry.reduced_data.data.tomo_fields[center_stack_index,:,upper_row,:],
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
405 upper_row, thetas, eff_pixel_size, cross_sectional_dim, path=path,
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
406 num_core=self.num_core)
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
407 logger.debug(f'... done in {time()-t0:.2f} seconds')
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
408 logger.debug(f'upper_row = {upper_row:.2f}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
409 logger.debug(f'upper_center_offset = {upper_center_offset:.2f}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
410
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
411 center_config = {'lower_row': lower_row, 'lower_center_offset': lower_center_offset,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
412 'upper_row': upper_row, 'upper_center_offset': upper_center_offset}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
413 if num_tomo_stacks > 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
414 center_config['center_stack_index'] = center_stack_index+1 # save as offset 1
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
415
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
416 # Save test data to file
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
417 if self.test_mode:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
418 with open(f'{self.output_folder}/center_config.yaml', 'w') as f:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
419 safe_dump(center_config, f)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
420
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
421 return(center_config)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
422
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
423 def reconstruct_data(self, nxroot, center_info, x_bounds=None, y_bounds=None):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
424 """Reconstruct the tomography data.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
425 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
426 logger.info('Reconstruct the tomography data')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
427
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
428 if not isinstance(nxroot, NXroot):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
429 raise ValueError(f'Invalid parameter nxroot ({nxroot})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
430 nxentry = nxroot[nxroot.attrs['default']]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
431 if not isinstance(nxentry, NXentry):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
432 raise ValueError(f'Invalid nxentry ({nxentry})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
433 if not isinstance(center_info, dict):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
434 raise ValueError(f'Invalid parameter center_info ({center_info})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
435
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
436 # Create plot galaxy path directory and path if needed
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
437 if self.galaxy_flag:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
438 if not os_path.exists('tomo_reconstruct_plots'):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
439 mkdir('tomo_reconstruct_plots')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
440 path = 'tomo_reconstruct_plots'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
441 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
442 path = self.output_folder
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
443
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
444 # Check if reduced data is available
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
445 if ('reduced_data' not in nxentry or 'reduced_data' not in nxentry.data):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
446 raise KeyError(f'Unable to find valid reduced data in {nxentry}.')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
447
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
448 # Create an NXprocess to store image reconstruction (meta)data
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
449 nxprocess = NXprocess()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
450
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
451 # Get rotation axis rows and centers
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
452 lower_row = center_info.get('lower_row')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
453 lower_center_offset = center_info.get('lower_center_offset')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
454 upper_row = center_info.get('upper_row')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
455 upper_center_offset = center_info.get('upper_center_offset')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
456 if (lower_row is None or lower_center_offset is None or upper_row is None or
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
457 upper_center_offset is None):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
458 raise KeyError(f'Unable to find valid calibrated center axis info in {center_info}.')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
459 center_slope = (upper_center_offset-lower_center_offset)/(upper_row-lower_row)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
460
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
461 # Get thetas (in degrees)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
462 thetas = np.asarray(nxentry.reduced_data.rotation_angle)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
463
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
464 # Reconstruct tomography data
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
465 # reduced data axes order: stack,theta,row,column
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
466 # reconstructed data order in each stack: row/z,x,y
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
467 # Note: Nexus cannot follow a link if the data it points to is too big,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
468 # so get the data from the actual place, not from nxentry.data
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
469 if 'zoom_perc' in nxentry.reduced_data:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
470 res_title = f'{nxentry.reduced_data.attrs["zoom_perc"]}p'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
471 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
472 res_title = 'fullres'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
473 load_error = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
474 num_tomo_stacks = nxentry.reduced_data.data.tomo_fields.shape[0]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
475 tomo_recon_stacks = num_tomo_stacks*[np.array([])]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
476 for i in range(num_tomo_stacks):
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
477 # Convert reduced data stack from theta,row,column to row,theta,column
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
478 logger.debug(f'Reading reduced data stack {i+1}...')
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
479 t0 = time()
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
480 tomo_stack = np.asarray(nxentry.reduced_data.data.tomo_fields[i])
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
481 logger.debug(f'... done in {time()-t0:.2f} seconds')
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
482 if len(tomo_stack.shape) != 3 or any(True for dim in tomo_stack.shape if not dim):
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
483 raise ValueError(f'Unable to load tomography stack {i+1} for reconstruction')
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
484 tomo_stack = np.swapaxes(tomo_stack, 0, 1)
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
485 assert(len(thetas) == tomo_stack.shape[1])
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
486 assert(0 <= lower_row < upper_row < tomo_stack.shape[0])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
487 center_offsets = [lower_center_offset-lower_row*center_slope,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
488 upper_center_offset+(tomo_stack.shape[0]-1-upper_row)*center_slope]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
489 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
490 logger.debug(f'Running _reconstruct_one_tomo_stack on {self.num_core} cores ...')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
491 tomo_recon_stack = self._reconstruct_one_tomo_stack(tomo_stack, thetas,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
492 center_offsets=center_offsets, num_core=self.num_core, algorithm='gridrec')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
493 logger.debug(f'... done in {time()-t0:.2f} seconds')
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
494 logger.info(f'Reconstruction of stack {i+1} took {time()-t0:.2f} seconds')
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
495
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
496 # Combine stacks
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
497 tomo_recon_stacks[i] = tomo_recon_stack
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
498
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
499 # Resize the reconstructed tomography data
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
500 # reconstructed data order in each stack: row/z,x,y
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
501 if self.test_mode:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
502 x_bounds = self.test_config.get('x_bounds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
503 y_bounds = self.test_config.get('y_bounds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
504 z_bounds = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
505 elif self.galaxy_flag:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
506 if x_bounds is not None and not is_int_pair(x_bounds, ge=0,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
507 lt=tomo_recon_stacks[0].shape[1]):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
508 raise ValueError(f'Invalid parameter x_bounds ({x_bounds})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
509 if y_bounds is not None and not is_int_pair(y_bounds, ge=0,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
510 lt=tomo_recon_stacks[0].shape[1]):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
511 raise ValueError(f'Invalid parameter y_bounds ({y_bounds})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
512 z_bounds = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
513 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
514 x_bounds, y_bounds, z_bounds = self._resize_reconstructed_data(tomo_recon_stacks)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
515 if x_bounds is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
516 x_range = (0, tomo_recon_stacks[0].shape[1])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
517 x_slice = int(x_range[1]/2)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
518 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
519 x_range = (min(x_bounds), max(x_bounds))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
520 x_slice = int((x_bounds[0]+x_bounds[1])/2)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
521 if y_bounds is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
522 y_range = (0, tomo_recon_stacks[0].shape[2])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
523 y_slice = int(y_range[1]/2)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
524 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
525 y_range = (min(y_bounds), max(y_bounds))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
526 y_slice = int((y_bounds[0]+y_bounds[1])/2)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
527 if z_bounds is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
528 z_range = (0, tomo_recon_stacks[0].shape[0])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
529 z_slice = int(z_range[1]/2)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
530 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
531 z_range = (min(z_bounds), max(z_bounds))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
532 z_slice = int((z_bounds[0]+z_bounds[1])/2)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
533
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
534 # Plot a few reconstructed image slices
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
535 if num_tomo_stacks == 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
536 basetitle = 'recon'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
537 else:
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
538 basetitle = f'recon stack {i+1}'
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
539 for i, stack in enumerate(tomo_recon_stacks):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
540 title = f'{basetitle} {res_title} xslice{x_slice}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
541 quick_imshow(stack[z_range[0]:z_range[1],x_slice,y_range[0]:y_range[1]],
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
542 title=title, path=path, save_fig=self.save_figs, save_only=self.save_only,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
543 block=self.block)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
544 title = f'{basetitle} {res_title} yslice{y_slice}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
545 quick_imshow(stack[z_range[0]:z_range[1],x_range[0]:x_range[1],y_slice],
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
546 title=title, path=path, save_fig=self.save_figs, save_only=self.save_only,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
547 block=self.block)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
548 title = f'{basetitle} {res_title} zslice{z_slice}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
549 quick_imshow(stack[z_slice,x_range[0]:x_range[1],y_range[0]:y_range[1]],
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
550 title=title, path=path, save_fig=self.save_figs, save_only=self.save_only,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
551 block=self.block)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
552
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
553 # Save test data to file
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
554 # reconstructed data order in each stack: row/z,x,y
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
555 if self.test_mode:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
556 for i, stack in enumerate(tomo_recon_stacks):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
557 np.savetxt(f'{self.output_folder}/recon_stack_{i+1}.txt',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
558 stack[z_slice,x_range[0]:x_range[1],y_range[0]:y_range[1]], fmt='%.6e')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
559
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
560 # Add image reconstruction to reconstructed data NXprocess
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
561 # reconstructed data order in each stack: row/z,x,y
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
562 nxprocess.data = NXdata()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
563 nxprocess.attrs['default'] = 'data'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
564 for k, v in center_info.items():
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
565 nxprocess[k] = v
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
566 if x_bounds is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
567 nxprocess.x_bounds = x_bounds
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
568 if y_bounds is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
569 nxprocess.y_bounds = y_bounds
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
570 if z_bounds is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
571 nxprocess.z_bounds = z_bounds
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
572 nxprocess.data['reconstructed_data'] = np.asarray([stack[z_range[0]:z_range[1],
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
573 x_range[0]:x_range[1],y_range[0]:y_range[1]] for stack in tomo_recon_stacks])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
574 nxprocess.data.attrs['signal'] = 'reconstructed_data'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
575
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
576 # Create a copy of the input Nexus object and remove reduced data
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
577 exclude_items = [f'{nxentry._name}/reduced_data/data', f'{nxentry._name}/data/reduced_data']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
578 nxroot_copy = nxcopy(nxroot, exclude_nxpaths=exclude_items)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
579
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
580 # Add the reconstructed data NXprocess to the new Nexus object
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
581 nxentry_copy = nxroot_copy[nxroot_copy.attrs['default']]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
582 nxentry_copy.reconstructed_data = nxprocess
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
583 if 'data' not in nxentry_copy:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
584 nxentry_copy.data = NXdata()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
585 nxentry_copy.attrs['default'] = 'data'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
586 nxentry_copy.data.makelink(nxprocess.data.reconstructed_data, name='reconstructed_data')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
587 nxentry_copy.data.attrs['signal'] = 'reconstructed_data'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
588
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
589 return(nxroot_copy)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
590
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
591 def combine_data(self, nxroot, x_bounds=None, y_bounds=None):
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
592 """Combine the reconstructed tomography stacks.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
593 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
594 logger.info('Combine the reconstructed tomography stacks')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
595
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
596 if not isinstance(nxroot, NXroot):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
597 raise ValueError(f'Invalid parameter nxroot ({nxroot})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
598 nxentry = nxroot[nxroot.attrs['default']]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
599 if not isinstance(nxentry, NXentry):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
600 raise ValueError(f'Invalid nxentry ({nxentry})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
601
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
602 # Create plot galaxy path directory and path if needed
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
603 if self.galaxy_flag:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
604 if not os_path.exists('tomo_combine_plots'):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
605 mkdir('tomo_combine_plots')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
606 path = 'tomo_combine_plots'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
607 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
608 path = self.output_folder
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
609
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
610 # Check if reconstructed image data is available
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
611 if ('reconstructed_data' not in nxentry or 'reconstructed_data' not in nxentry.data):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
612 raise KeyError(f'Unable to find valid reconstructed image data in {nxentry}.')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
613
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
614 # Create an NXprocess to store combined image reconstruction (meta)data
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
615 nxprocess = NXprocess()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
616
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
617 # Get the reconstructed data
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
618 # reconstructed data order: stack,row(z),x,y
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
619 # Note: Nexus cannot follow a link if the data it points to is too big,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
620 # so get the data from the actual place, not from nxentry.data
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
621 num_tomo_stacks = nxentry.reconstructed_data.data.reconstructed_data.shape[0]
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
622 if num_tomo_stacks == 1:
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
623 logger.info('Only one stack available: leaving combine_data')
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
624 return(None)
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
625
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
626 # Combine the reconstructed stacks
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
627 # (load one stack at a time to reduce risk of hitting Nexus data access limit)
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
628 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
629 logger.debug(f'Combining the reconstructed stacks ...')
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
630 tomo_recon_combined = np.asarray(nxentry.reconstructed_data.data.reconstructed_data[0])
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
631 if num_tomo_stacks > 2:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
632 tomo_recon_combined = np.concatenate([tomo_recon_combined]+
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
633 [nxentry.reconstructed_data.data.reconstructed_data[i]
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
634 for i in range(1, num_tomo_stacks-1)])
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
635 if num_tomo_stacks > 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
636 tomo_recon_combined = np.concatenate([tomo_recon_combined]+
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
637 [nxentry.reconstructed_data.data.reconstructed_data[num_tomo_stacks-1]])
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
638 logger.debug(f'... done in {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
639 logger.info(f'Combining the reconstructed stacks took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
640
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
641 # Resize the combined tomography data stacks
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
642 # combined data order: row/z,x,y
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
643 if self.test_mode:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
644 x_bounds = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
645 y_bounds = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
646 z_bounds = self.test_config.get('z_bounds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
647 elif self.galaxy_flag:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
648 if x_bounds is not None and not is_int_pair(x_bounds, ge=0,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
649 lt=tomo_recon_stacks[0].shape[1]):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
650 raise ValueError(f'Invalid parameter x_bounds ({x_bounds})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
651 if y_bounds is not None and not is_int_pair(y_bounds, ge=0,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
652 lt=tomo_recon_stacks[0].shape[1]):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
653 raise ValueError(f'Invalid parameter y_bounds ({y_bounds})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
654 z_bounds = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
655 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
656 x_bounds, y_bounds, z_bounds = self._resize_reconstructed_data(tomo_recon_combined,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
657 z_only=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
658 if x_bounds is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
659 x_range = (0, tomo_recon_combined.shape[1])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
660 x_slice = int(x_range[1]/2)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
661 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
662 x_range = x_bounds
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
663 x_slice = int((x_bounds[0]+x_bounds[1])/2)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
664 if y_bounds is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
665 y_range = (0, tomo_recon_combined.shape[2])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
666 y_slice = int(y_range[1]/2)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
667 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
668 y_range = y_bounds
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
669 y_slice = int((y_bounds[0]+y_bounds[1])/2)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
670 if z_bounds is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
671 z_range = (0, tomo_recon_combined.shape[0])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
672 z_slice = int(z_range[1]/2)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
673 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
674 z_range = z_bounds
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
675 z_slice = int((z_bounds[0]+z_bounds[1])/2)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
676
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
677 # Plot a few combined image slices
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
678 quick_imshow(tomo_recon_combined[z_range[0]:z_range[1],x_slice,y_range[0]:y_range[1]],
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
679 title=f'recon combined xslice{x_slice}', path=path,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
680 save_fig=self.save_figs, save_only=self.save_only, block=self.block)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
681 quick_imshow(tomo_recon_combined[z_range[0]:z_range[1],x_range[0]:x_range[1],y_slice],
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
682 title=f'recon combined yslice{y_slice}', path=path,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
683 save_fig=self.save_figs, save_only=self.save_only, block=self.block)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
684 quick_imshow(tomo_recon_combined[z_slice,x_range[0]:x_range[1],y_range[0]:y_range[1]],
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
685 title=f'recon combined zslice{z_slice}', path=path,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
686 save_fig=self.save_figs, save_only=self.save_only, block=self.block)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
687
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
688 # Save test data to file
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
689 # combined data order: row/z,x,y
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
690 if self.test_mode:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
691 np.savetxt(f'{self.output_folder}/recon_combined.txt', tomo_recon_combined[
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
692 z_slice,x_range[0]:x_range[1],y_range[0]:y_range[1]], fmt='%.6e')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
693
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
694 # Add image reconstruction to reconstructed data NXprocess
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
695 # combined data order: row/z,x,y
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
696 nxprocess.data = NXdata()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
697 nxprocess.attrs['default'] = 'data'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
698 if x_bounds is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
699 nxprocess.x_bounds = x_bounds
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
700 if y_bounds is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
701 nxprocess.y_bounds = y_bounds
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
702 if z_bounds is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
703 nxprocess.z_bounds = z_bounds
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
704 nxprocess.data['combined_data'] = tomo_recon_combined
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
705 nxprocess.data.attrs['signal'] = 'combined_data'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
706
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
707 # Create a copy of the input Nexus object and remove reconstructed data
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
708 exclude_items = [f'{nxentry._name}/reconstructed_data/data',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
709 f'{nxentry._name}/data/reconstructed_data']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
710 nxroot_copy = nxcopy(nxroot, exclude_nxpaths=exclude_items)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
711
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
712 # Add the combined data NXprocess to the new Nexus object
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
713 nxentry_copy = nxroot_copy[nxroot_copy.attrs['default']]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
714 nxentry_copy.combined_data = nxprocess
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
715 if 'data' not in nxentry_copy:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
716 nxentry_copy.data = NXdata()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
717 nxentry_copy.attrs['default'] = 'data'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
718 nxentry_copy.data.makelink(nxprocess.data.combined_data, name='combined_data')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
719 nxentry_copy.data.attrs['signal'] = 'combined_data'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
720
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
721 return(nxroot_copy)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
722
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
723 def _gen_dark(self, nxentry, reduced_data):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
724 """Generate dark field.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
725 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
726 # Get the dark field images
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
727 image_key = nxentry.instrument.detector.get('image_key', None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
728 if image_key and 'data' in nxentry.instrument.detector:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
729 field_indices = [index for index, key in enumerate(image_key) if key == 2]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
730 tdf_stack = nxentry.instrument.detector.data[field_indices,:,:]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
731 # RV the default NXtomo form does not accomodate bright or dark field stacks
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
732 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
733 dark_field_scans = nxentry.spec_scans.dark_field
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
734 dark_field = FlatField.construct_from_nxcollection(dark_field_scans)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
735 prefix = str(nxentry.instrument.detector.local_name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
736 tdf_stack = dark_field.get_detector_data(prefix)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
737 if isinstance(tdf_stack, list):
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
738 assert(len(tdf_stack) == 1) # TODO
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
739 tdf_stack = tdf_stack[0]
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
740
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
741 # Take median
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
742 if tdf_stack.ndim == 2:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
743 tdf = tdf_stack
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
744 elif tdf_stack.ndim == 3:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
745 tdf = np.median(tdf_stack, axis=0)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
746 del tdf_stack
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
747 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
748 raise ValueError(f'Invalid tdf_stack shape ({tdf_stack.shape})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
749
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
750 # Remove dark field intensities above the cutoff
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
751 #RV tdf_cutoff = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
752 tdf_cutoff = tdf.min()+2*(np.median(tdf)-tdf.min())
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
753 logger.debug(f'tdf_cutoff = {tdf_cutoff}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
754 if tdf_cutoff is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
755 if not is_num(tdf_cutoff, ge=0):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
756 logger.warning(f'Ignoring illegal value of tdf_cutoff {tdf_cutoff}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
757 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
758 tdf[tdf > tdf_cutoff] = np.nan
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
759 logger.debug(f'tdf_cutoff = {tdf_cutoff}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
760
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
761 # Remove nans
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
762 tdf_mean = np.nanmean(tdf)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
763 logger.debug(f'tdf_mean = {tdf_mean}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
764 np.nan_to_num(tdf, copy=False, nan=tdf_mean, posinf=tdf_mean, neginf=0.)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
765
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
766 # Plot dark field
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
767 if self.galaxy_flag:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
768 quick_imshow(tdf, title='dark field', path='tomo_reduce_plots', save_fig=self.save_figs,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
769 save_only=self.save_only)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
770 elif not self.test_mode:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
771 quick_imshow(tdf, title='dark field', path=self.output_folder, save_fig=self.save_figs,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
772 save_only=self.save_only)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
773 clear_imshow('dark field')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
774 # quick_imshow(tdf, title='dark field', block=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
775
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
776 # Add dark field to reduced data NXprocess
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
777 reduced_data.data = NXdata()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
778 reduced_data.data['dark_field'] = tdf
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
779
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
780 return(reduced_data)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
781
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
782 def _gen_bright(self, nxentry, reduced_data):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
783 """Generate bright field.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
784 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
785 # Get the bright field images
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
786 image_key = nxentry.instrument.detector.get('image_key', None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
787 if image_key and 'data' in nxentry.instrument.detector:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
788 field_indices = [index for index, key in enumerate(image_key) if key == 1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
789 tbf_stack = nxentry.instrument.detector.data[field_indices,:,:]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
790 # RV the default NXtomo form does not accomodate bright or dark field stacks
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
791 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
792 bright_field_scans = nxentry.spec_scans.bright_field
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
793 bright_field = FlatField.construct_from_nxcollection(bright_field_scans)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
794 prefix = str(nxentry.instrument.detector.local_name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
795 tbf_stack = bright_field.get_detector_data(prefix)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
796 if isinstance(tbf_stack, list):
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
797 assert(len(tbf_stack) == 1) # TODO
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
798 tbf_stack = tbf_stack[0]
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
799
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
800 # Take median if more than one image
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
801 """Median or mean: It may be best to try the median because of some image
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
802 artifacts that arise due to crinkles in the upstream kapton tape windows
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
803 causing some phase contrast images to appear on the detector.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
804 One thing that also may be useful in a future implementation is to do a
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
805 brightfield adjustment on EACH frame of the tomo based on a ROI in the
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
806 corner of the frame where there is no sample but there is the direct X-ray
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
807 beam because there is frame to frame fluctuations from the incoming beam.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
808 We don’t typically account for them but potentially could.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
809 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
810 if tbf_stack.ndim == 2:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
811 tbf = tbf_stack
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
812 elif tbf_stack.ndim == 3:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
813 tbf = np.median(tbf_stack, axis=0)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
814 del tbf_stack
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
815 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
816 raise ValueError(f'Invalid tbf_stack shape ({tbf_stacks.shape})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
817
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
818 # Subtract dark field
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
819 if 'data' in reduced_data and 'dark_field' in reduced_data.data:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
820 tbf -= reduced_data.data.dark_field
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
821 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
822 logger.warning('Dark field unavailable')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
823
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
824 # Set any non-positive values to one
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
825 # (avoid negative bright field values for spikes in dark field)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
826 tbf[tbf < 1] = 1
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
827
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
828 # Plot bright field
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
829 if self.galaxy_flag:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
830 quick_imshow(tbf, title='bright field', path='tomo_reduce_plots',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
831 save_fig=self.save_figs, save_only=self.save_only)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
832 elif not self.test_mode:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
833 quick_imshow(tbf, title='bright field', path=self.output_folder,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
834 save_fig=self.save_figs, save_only=self.save_only)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
835 clear_imshow('bright field')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
836 # quick_imshow(tbf, title='bright field', block=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
837
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
838 # Add bright field to reduced data NXprocess
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
839 if 'data' not in reduced_data:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
840 reduced_data.data = NXdata()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
841 reduced_data.data['bright_field'] = tbf
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
842
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
843 return(reduced_data)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
844
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
845 def _set_detector_bounds(self, nxentry, reduced_data, img_x_bounds=None):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
846 """Set vertical detector bounds for each image stack.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
847 Right now the range is the same for each set in the image stack.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
848 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
849 if self.test_mode:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
850 return(tuple(self.test_config['img_x_bounds']))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
851
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
852 # Get the first tomography image and the reference heights
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
853 image_key = nxentry.instrument.detector.get('image_key', None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
854 if image_key and 'data' in nxentry.instrument.detector:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
855 field_indices = [index for index, key in enumerate(image_key) if key == 0]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
856 first_image = np.asarray(nxentry.instrument.detector.data[field_indices[0],:,:])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
857 theta = float(nxentry.sample.rotation_angle[field_indices[0]])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
858 z_translation_all = nxentry.sample.z_translation[field_indices]
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
859 vertical_shifts = sorted(list(set(z_translation_all)))
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
860 num_tomo_stacks = len(vertical_shifts)
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
861 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
862 tomo_field_scans = nxentry.spec_scans.tomo_fields
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
863 tomo_fields = TomoField.construct_from_nxcollection(tomo_field_scans)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
864 vertical_shifts = tomo_fields.get_vertical_shifts()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
865 if not isinstance(vertical_shifts, list):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
866 vertical_shifts = [vertical_shifts]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
867 prefix = str(nxentry.instrument.detector.local_name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
868 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
869 first_image = tomo_fields.get_detector_data(prefix, tomo_fields.scan_numbers[0], 0)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
870 logger.debug(f'Getting first image took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
871 num_tomo_stacks = len(tomo_fields.scan_numbers)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
872 theta = tomo_fields.theta_range['start']
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
873
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
874 # Select image bounds
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
875 title = f'tomography image at theta={round(theta, 2)+0}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
876 if (img_x_bounds is not None and not is_index_range(img_x_bounds, ge=0,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
877 le=first_image.shape[0])):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
878 raise ValueError(f'Invalid parameter img_x_bounds ({img_x_bounds})')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
879 if nxentry.instrument.source.attrs['station'] in ('id1a3', 'id3a'):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
880 pixel_size = nxentry.instrument.detector.x_pixel_size
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
881 # Try to get a fit from the bright field
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
882 tbf = np.asarray(reduced_data.data.bright_field)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
883 tbf_shape = tbf.shape
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
884 x_sum = np.sum(tbf, 1)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
885 x_sum_min = x_sum.min()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
886 x_sum_max = x_sum.max()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
887 fit = Fit.fit_data(x_sum, 'rectangle', x=np.array(range(len(x_sum))), form='atan',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
888 guess=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
889 parameters = fit.best_values
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
890 x_low_fit = parameters.get('center1', None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
891 x_upp_fit = parameters.get('center2', None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
892 sig_low = parameters.get('sigma1', None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
893 sig_upp = parameters.get('sigma2', None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
894 have_fit = fit.success and x_low_fit is not None and x_upp_fit is not None and \
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
895 sig_low is not None and sig_upp is not None and \
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
896 0 <= x_low_fit < x_upp_fit <= x_sum.size and \
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
897 (sig_low+sig_upp)/(x_upp_fit-x_low_fit) < 0.1
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
898 if have_fit:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
899 # Set a 5% margin on each side
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
900 margin = 0.05*(x_upp_fit-x_low_fit)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
901 x_low_fit = max(0, x_low_fit-margin)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
902 x_upp_fit = min(tbf_shape[0], x_upp_fit+margin)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
903 if num_tomo_stacks == 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
904 if have_fit:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
905 # Set the default range to enclose the full fitted window
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
906 x_low = int(x_low_fit)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
907 x_upp = int(x_upp_fit)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
908 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
909 # Center a default range of 1 mm (RV: can we get this from the slits?)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
910 num_x_min = int((1.0-0.5*pixel_size)/pixel_size)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
911 x_low = int(0.5*(tbf_shape[0]-num_x_min))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
912 x_upp = x_low+num_x_min
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
913 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
914 # Get the default range from the reference heights
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
915 delta_z = vertical_shifts[1]-vertical_shifts[0]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
916 for i in range(2, num_tomo_stacks):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
917 delta_z = min(delta_z, vertical_shifts[i]-vertical_shifts[i-1])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
918 logger.debug(f'delta_z = {delta_z}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
919 num_x_min = int((delta_z-0.5*pixel_size)/pixel_size)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
920 logger.debug(f'num_x_min = {num_x_min}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
921 if num_x_min > tbf_shape[0]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
922 logger.warning('Image bounds and pixel size prevent seamless stacking')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
923 if have_fit:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
924 # Center the default range relative to the fitted window
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
925 x_low = int(0.5*(x_low_fit+x_upp_fit-num_x_min))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
926 x_upp = x_low+num_x_min
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
927 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
928 # Center the default range
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
929 x_low = int(0.5*(tbf_shape[0]-num_x_min))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
930 x_upp = x_low+num_x_min
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
931 if self.galaxy_flag:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
932 img_x_bounds = (x_low, x_upp)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
933 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
934 tmp = np.copy(tbf)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
935 tmp_max = tmp.max()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
936 tmp[x_low,:] = tmp_max
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
937 tmp[x_upp-1,:] = tmp_max
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
938 quick_imshow(tmp, title='bright field')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
939 tmp = np.copy(first_image)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
940 tmp_max = tmp.max()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
941 tmp[x_low,:] = tmp_max
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
942 tmp[x_upp-1,:] = tmp_max
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
943 quick_imshow(tmp, title=title)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
944 del tmp
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
945 quick_plot((range(x_sum.size), x_sum),
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
946 ([x_low, x_low], [x_sum_min, x_sum_max], 'r-'),
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
947 ([x_upp, x_upp], [x_sum_min, x_sum_max], 'r-'),
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
948 title='sum over theta and y')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
949 print(f'lower bound = {x_low} (inclusive)')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
950 print(f'upper bound = {x_upp} (exclusive)]')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
951 accept = input_yesno('Accept these bounds (y/n)?', 'y')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
952 clear_imshow('bright field')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
953 clear_imshow(title)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
954 clear_plot('sum over theta and y')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
955 if accept:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
956 img_x_bounds = (x_low, x_upp)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
957 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
958 while True:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
959 mask, img_x_bounds = draw_mask_1d(x_sum, title='select x data range',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
960 legend='sum over theta and y')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
961 if len(img_x_bounds) == 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
962 break
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
963 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
964 print(f'Choose a single connected data range')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
965 img_x_bounds = tuple(img_x_bounds[0])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
966 if (num_tomo_stacks > 1 and img_x_bounds[1]-img_x_bounds[0]+1 <
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
967 int((delta_z-0.5*pixel_size)/pixel_size)):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
968 logger.warning('Image bounds and pixel size prevent seamless stacking')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
969 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
970 if num_tomo_stacks > 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
971 raise NotImplementedError('Selecting image bounds for multiple stacks on FMB')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
972 # For FMB: use the first tomography image to select range
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
973 # RV: revisit if they do tomography with multiple stacks
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
974 x_sum = np.sum(first_image, 1)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
975 x_sum_min = x_sum.min()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
976 x_sum_max = x_sum.max()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
977 if self.galaxy_flag:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
978 if img_x_bounds is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
979 img_x_bounds = (0, first_image.shape[0])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
980 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
981 quick_imshow(first_image, title=title)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
982 print('Select vertical data reduction range from first tomography image')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
983 img_x_bounds = select_image_bounds(first_image, 0, title=title)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
984 clear_imshow(title)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
985 if img_x_bounds is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
986 raise ValueError('Unable to select image bounds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
987
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
988 # Plot results
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
989 if self.galaxy_flag:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
990 path = 'tomo_reduce_plots'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
991 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
992 path = self.output_folder
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
993 x_low = img_x_bounds[0]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
994 x_upp = img_x_bounds[1]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
995 tmp = np.copy(first_image)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
996 tmp_max = tmp.max()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
997 tmp[x_low,:] = tmp_max
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
998 tmp[x_upp-1,:] = tmp_max
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
999 quick_imshow(tmp, title=title, path=path, save_fig=self.save_figs, save_only=self.save_only,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1000 block=self.block)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1001 del tmp
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1002 quick_plot((range(x_sum.size), x_sum),
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1003 ([x_low, x_low], [x_sum_min, x_sum_max], 'r-'),
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1004 ([x_upp, x_upp], [x_sum_min, x_sum_max], 'r-'),
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1005 title='sum over theta and y', path=path, save_fig=self.save_figs,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1006 save_only=self.save_only, block=self.block)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1007
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1008 return(img_x_bounds)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1009
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1010 def _set_zoom_or_skip(self):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1011 """Set zoom and/or theta skip to reduce memory the requirement for the analysis.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1012 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1013 # if input_yesno('\nDo you want to zoom in to reduce memory requirement (y/n)?', 'n'):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1014 # zoom_perc = input_int(' Enter zoom percentage', ge=1, le=100)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1015 # else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1016 # zoom_perc = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1017 zoom_perc = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1018 # if input_yesno('Do you want to skip thetas to reduce memory requirement (y/n)?', 'n'):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1019 # num_theta_skip = input_int(' Enter the number skip theta interval', ge=0,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1020 # lt=num_theta)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1021 # else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1022 # num_theta_skip = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1023 num_theta_skip = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1024 logger.debug(f'zoom_perc = {zoom_perc}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1025 logger.debug(f'num_theta_skip = {num_theta_skip}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1026
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1027 return(zoom_perc, num_theta_skip)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1028
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1029 def _gen_tomo(self, nxentry, reduced_data):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1030 """Generate tomography fields.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1031 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1032 # Get full bright field
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1033 tbf = np.asarray(reduced_data.data.bright_field)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1034 tbf_shape = tbf.shape
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1035
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1036 # Get image bounds
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1037 img_x_bounds = tuple(reduced_data.get('img_x_bounds', (0, tbf_shape[0])))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1038 img_y_bounds = tuple(reduced_data.get('img_y_bounds', (0, tbf_shape[1])))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1039
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1040 # Get resized dark field
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1041 # if 'dark_field' in data:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1042 # tbf = np.asarray(reduced_data.data.dark_field[
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1043 # img_x_bounds[0]:img_x_bounds[1],img_y_bounds[0]:img_y_bounds[1]])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1044 # else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1045 # logger.warning('Dark field unavailable')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1046 # tdf = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1047 tdf = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1048
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1049 # Resize bright field
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1050 if img_x_bounds != (0, tbf.shape[0]) or img_y_bounds != (0, tbf.shape[1]):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1051 tbf = tbf[img_x_bounds[0]:img_x_bounds[1],img_y_bounds[0]:img_y_bounds[1]]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1052
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1053 # Get the tomography images
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1054 image_key = nxentry.instrument.detector.get('image_key', None)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1055 if image_key and 'data' in nxentry.instrument.detector:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1056 field_indices_all = [index for index, key in enumerate(image_key) if key == 0]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1057 z_translation_all = nxentry.sample.z_translation[field_indices_all]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1058 z_translation_levels = sorted(list(set(z_translation_all)))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1059 num_tomo_stacks = len(z_translation_levels)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1060 tomo_stacks = num_tomo_stacks*[np.array([])]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1061 horizontal_shifts = []
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1062 vertical_shifts = []
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1063 thetas = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1064 tomo_stacks = []
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1065 for i, z_translation in enumerate(z_translation_levels):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1066 field_indices = [field_indices_all[index]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1067 for index, z in enumerate(z_translation_all) if z == z_translation]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1068 horizontal_shift = list(set(nxentry.sample.x_translation[field_indices]))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1069 assert(len(horizontal_shift) == 1)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1070 horizontal_shifts += horizontal_shift
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1071 vertical_shift = list(set(nxentry.sample.z_translation[field_indices]))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1072 assert(len(vertical_shift) == 1)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1073 vertical_shifts += vertical_shift
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1074 sequence_numbers = nxentry.instrument.detector.sequence_number[field_indices]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1075 if thetas is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1076 thetas = np.asarray(nxentry.sample.rotation_angle[field_indices]) \
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1077 [sequence_numbers]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1078 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1079 assert(all(thetas[i] == nxentry.sample.rotation_angle[field_indices[index]]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1080 for i, index in enumerate(sequence_numbers)))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1081 assert(list(set(sequence_numbers)) == [i for i in range(len(sequence_numbers))])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1082 if list(sequence_numbers) == [i for i in range(len(sequence_numbers))]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1083 tomo_stack = np.asarray(nxentry.instrument.detector.data[field_indices])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1084 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1085 raise ValueError('Unable to load the tomography images')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1086 tomo_stacks.append(tomo_stack)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1087 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1088 tomo_field_scans = nxentry.spec_scans.tomo_fields
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1089 tomo_fields = TomoField.construct_from_nxcollection(tomo_field_scans)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1090 horizontal_shifts = tomo_fields.get_horizontal_shifts()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1091 vertical_shifts = tomo_fields.get_vertical_shifts()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1092 prefix = str(nxentry.instrument.detector.local_name)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1093 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1094 tomo_stacks = tomo_fields.get_detector_data(prefix)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1095 logger.debug(f'Getting tomography images took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1096 logger.debug(f'Getting all images took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1097 thetas = np.linspace(tomo_fields.theta_range['start'], tomo_fields.theta_range['end'],
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1098 tomo_fields.theta_range['num'])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1099 if not isinstance(tomo_stacks, list):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1100 horizontal_shifts = [horizontal_shifts]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1101 vertical_shifts = [vertical_shifts]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1102 tomo_stacks = [tomo_stacks]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1103
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1104 reduced_tomo_stacks = []
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1105 if self.galaxy_flag:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1106 path = 'tomo_reduce_plots'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1107 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1108 path = self.output_folder
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1109 for i, tomo_stack in enumerate(tomo_stacks):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1110 # Resize the tomography images
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1111 # Right now the range is the same for each set in the image stack.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1112 if img_x_bounds != (0, tbf.shape[0]) or img_y_bounds != (0, tbf.shape[1]):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1113 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1114 tomo_stack = tomo_stack[:,img_x_bounds[0]:img_x_bounds[1],
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1115 img_y_bounds[0]:img_y_bounds[1]].astype('float64')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1116 logger.debug(f'Resizing tomography images took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1117
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1118 # Subtract dark field
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1119 if tdf is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1120 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1121 with set_numexpr_threads(self.num_core):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1122 ne.evaluate('tomo_stack-tdf', out=tomo_stack)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1123 logger.debug(f'Subtracting dark field took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1124
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1125 # Normalize
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1126 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1127 with set_numexpr_threads(self.num_core):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1128 ne.evaluate('tomo_stack/tbf', out=tomo_stack, truediv=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1129 logger.debug(f'Normalizing took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1130
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1131 # Remove non-positive values and linearize data
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1132 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1133 cutoff = 1.e-6
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1134 with set_numexpr_threads(self.num_core):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1135 ne.evaluate('where(tomo_stack<cutoff, cutoff, tomo_stack)', out=tomo_stack)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1136 with set_numexpr_threads(self.num_core):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1137 ne.evaluate('-log(tomo_stack)', out=tomo_stack)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1138 logger.debug('Removing non-positive values and linearizing data took '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1139 f'{time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1140
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1141 # Get rid of nans/infs that may be introduced by normalization
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1142 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1143 np.where(np.isfinite(tomo_stack), tomo_stack, 0.)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1144 logger.debug(f'Remove nans/infs took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1145
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1146 # Downsize tomography stack to smaller size
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1147 # TODO use theta_skip as well
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1148 tomo_stack = tomo_stack.astype('float32')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1149 if not self.test_mode:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1150 if len(tomo_stacks) == 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1151 title = f'red fullres theta {round(thetas[0], 2)+0}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1152 else:
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
1153 title = f'red stack {i+1} fullres theta {round(thetas[0], 2)+0}'
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1154 quick_imshow(tomo_stack[0,:,:], title=title, path=path, save_fig=self.save_figs,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1155 save_only=self.save_only, block=self.block)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1156 # if not self.block:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1157 # clear_imshow(title)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1158 if False and zoom_perc != 100:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1159 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1160 logger.debug(f'Zooming in ...')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1161 tomo_zoom_list = []
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1162 for j in range(tomo_stack.shape[0]):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1163 tomo_zoom = spi.zoom(tomo_stack[j,:,:], 0.01*zoom_perc)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1164 tomo_zoom_list.append(tomo_zoom)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1165 tomo_stack = np.stack([tomo_zoom for tomo_zoom in tomo_zoom_list])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1166 logger.debug(f'... done in {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1167 logger.info(f'Zooming in took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1168 del tomo_zoom_list
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1169 if not self.test_mode:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1170 title = f'red stack {zoom_perc}p theta {round(thetas[0], 2)+0}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1171 quick_imshow(tomo_stack[0,:,:], title=title, path=path, save_fig=self.save_figs,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1172 save_only=self.save_only, block=self.block)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1173 # if not self.block:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1174 # clear_imshow(title)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1175
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1176 # Save test data to file
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1177 if self.test_mode:
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
1178 # row_index = int(tomo_stack.shape[0]/2)
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
1179 # np.savetxt(f'{self.output_folder}/red_stack_{i+1}.txt', tomo_stack[row_index,:,:],
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
1180 # fmt='%.6e')
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
1181 row_index = int(tomo_stack.shape[1]/2)
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
1182 np.savetxt(f'{self.output_folder}/red_stack_{i+1}.txt', tomo_stack[:,row_index,:],
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1183 fmt='%.6e')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1184
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1185 # Combine resized stacks
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1186 reduced_tomo_stacks.append(tomo_stack)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1187
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1188 # Add tomo field info to reduced data NXprocess
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1189 reduced_data['rotation_angle'] = thetas
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1190 reduced_data['x_translation'] = np.asarray(horizontal_shifts)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1191 reduced_data['z_translation'] = np.asarray(vertical_shifts)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1192 reduced_data.data['tomo_fields'] = np.asarray(reduced_tomo_stacks)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1193
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1194 if tdf is not None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1195 del tdf
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1196 del tbf
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1197
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1198 return(reduced_data)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1199
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1200 def _find_center_one_plane(self, sinogram, row, thetas, eff_pixel_size, cross_sectional_dim,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1201 path=None, tol=0.1, num_core=1):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1202 """Find center for a single tomography plane.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1203 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1204 # Try automatic center finding routines for initial value
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1205 # sinogram index order: theta,column
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1206 # need column,theta for iradon, so take transpose
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
1207 sinogram = np.asarray(sinogram)
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1208 sinogram_T = sinogram.T
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1209 center = sinogram.shape[1]/2
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1210
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1211 # Try using Nghia Vo’s method
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1212 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1213 if num_core > num_core_tomopy_limit:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1214 logger.debug(f'Running find_center_vo on {num_core_tomopy_limit} cores ...')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1215 tomo_center = tomopy.find_center_vo(sinogram, ncore=num_core_tomopy_limit)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1216 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1217 logger.debug(f'Running find_center_vo on {num_core} cores ...')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1218 tomo_center = tomopy.find_center_vo(sinogram, ncore=num_core)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1219 logger.debug(f'... done in {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1220 logger.info(f'Finding the center using Nghia Vo’s method took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1221 center_offset_vo = tomo_center-center
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1222 logger.info(f'Center at row {row} using Nghia Vo’s method = {center_offset_vo:.2f}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1223 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1224 logger.debug(f'Running _reconstruct_one_plane on {self.num_core} cores ...')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1225 recon_plane = self._reconstruct_one_plane(sinogram_T, tomo_center, thetas,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1226 eff_pixel_size, cross_sectional_dim, False, num_core)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1227 logger.debug(f'... done in {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1228 logger.info(f'Reconstructing row {row} took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1229
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1230 title = f'edges row{row} center offset{center_offset_vo:.2f} Vo'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1231 self._plot_edges_one_plane(recon_plane, title, path=path)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1232
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1233 # Try using phase correlation method
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1234 # if input_yesno('Try finding center using phase correlation (y/n)?', 'n'):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1235 # t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1236 # logger.debug(f'Running find_center_pc ...')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1237 # tomo_center = tomopy.find_center_pc(sinogram, sinogram, tol=0.1, rotc_guess=tomo_center)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1238 # error = 1.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1239 # while error > tol:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1240 # prev = tomo_center
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1241 # tomo_center = tomopy.find_center_pc(sinogram, sinogram, tol=tol,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1242 # rotc_guess=tomo_center)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1243 # error = np.abs(tomo_center-prev)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1244 # logger.debug(f'... done in {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1245 # logger.info('Finding the center using the phase correlation method took '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1246 # f'{time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1247 # center_offset = tomo_center-center
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1248 # print(f'Center at row {row} using phase correlation = {center_offset:.2f}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1249 # t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1250 # logger.debug(f'Running _reconstruct_one_plane on {self.num_core} cores ...')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1251 # recon_plane = self._reconstruct_one_plane(sinogram_T, tomo_center, thetas,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1252 # eff_pixel_size, cross_sectional_dim, False, num_core)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1253 # logger.debug(f'... done in {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1254 # logger.info(f'Reconstructing row {row} took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1255 #
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1256 # title = f'edges row{row} center_offset{center_offset:.2f} PC'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1257 # self._plot_edges_one_plane(recon_plane, title, path=path)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1258
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1259 # Select center location
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1260 # if input_yesno('Accept a center location (y) or continue search (n)?', 'y'):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1261 if True:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1262 # center_offset = input_num(' Enter chosen center offset', ge=-center, le=center,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1263 # default=center_offset_vo)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1264 center_offset = center_offset_vo
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1265 del sinogram_T
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1266 del recon_plane
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1267 return float(center_offset)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1268
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1269 # perform center finding search
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1270 while True:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1271 center_offset_low = input_int('\nEnter lower bound for center offset', ge=-center,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1272 le=center)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1273 center_offset_upp = input_int('Enter upper bound for center offset',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1274 ge=center_offset_low, le=center)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1275 if center_offset_upp == center_offset_low:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1276 center_offset_step = 1
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1277 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1278 center_offset_step = input_int('Enter step size for center offset search', ge=1,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1279 le=center_offset_upp-center_offset_low)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1280 num_center_offset = 1+int((center_offset_upp-center_offset_low)/center_offset_step)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1281 center_offsets = np.linspace(center_offset_low, center_offset_upp, num_center_offset)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1282 for center_offset in center_offsets:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1283 if center_offset == center_offset_vo:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1284 continue
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1285 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1286 logger.debug(f'Running _reconstruct_one_plane on {num_core} cores ...')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1287 recon_plane = self._reconstruct_one_plane(sinogram_T, center_offset+center, thetas,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1288 eff_pixel_size, cross_sectional_dim, False, num_core)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1289 logger.debug(f'... done in {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1290 logger.info(f'Reconstructing center_offset {center_offset} took '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1291 f'{time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1292 title = f'edges row{row} center_offset{center_offset:.2f}'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1293 self._plot_edges_one_plane(recon_plane, title, path=path)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1294 if input_int('\nContinue (0) or end the search (1)', ge=0, le=1):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1295 break
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1296
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1297 del sinogram_T
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1298 del recon_plane
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1299 center_offset = input_num(' Enter chosen center offset', ge=-center, le=center)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1300 return float(center_offset)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1301
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1302 def _reconstruct_one_plane(self, tomo_plane_T, center, thetas, eff_pixel_size,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1303 cross_sectional_dim, plot_sinogram=True, num_core=1):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1304 """Invert the sinogram for a single tomography plane.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1305 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1306 # tomo_plane_T index order: column,theta
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1307 assert(0 <= center < tomo_plane_T.shape[0])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1308 center_offset = center-tomo_plane_T.shape[0]/2
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1309 two_offset = 2*int(np.round(center_offset))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1310 two_offset_abs = np.abs(two_offset)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1311 max_rad = int(0.55*(cross_sectional_dim/eff_pixel_size)) # 10% slack to avoid edge effects
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1312 if max_rad > 0.5*tomo_plane_T.shape[0]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1313 max_rad = 0.5*tomo_plane_T.shape[0]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1314 dist_from_edge = max(1, int(np.floor((tomo_plane_T.shape[0]-two_offset_abs)/2.)-max_rad))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1315 if two_offset >= 0:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1316 logger.debug(f'sinogram range = [{two_offset+dist_from_edge}, {-dist_from_edge}]')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1317 sinogram = tomo_plane_T[two_offset+dist_from_edge:-dist_from_edge,:]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1318 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1319 logger.debug(f'sinogram range = [{dist_from_edge}, {two_offset-dist_from_edge}]')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1320 sinogram = tomo_plane_T[dist_from_edge:two_offset-dist_from_edge,:]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1321 if not self.galaxy_flag and plot_sinogram:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1322 quick_imshow(sinogram.T, f'sinogram center offset{center_offset:.2f}', aspect='auto',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1323 path=self.output_folder, save_fig=self.save_figs, save_only=self.save_only,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1324 block=self.block)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1325
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1326 # Inverting sinogram
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1327 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1328 recon_sinogram = iradon(sinogram, theta=thetas, circle=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1329 logger.debug(f'Inverting sinogram took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1330 del sinogram
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1331
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1332 # Performing Gaussian filtering and removing ring artifacts
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1333 recon_parameters = None#self.config.get('recon_parameters')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1334 if recon_parameters is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1335 sigma = 1.0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1336 ring_width = 15
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1337 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1338 sigma = recon_parameters.get('gaussian_sigma', 1.0)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1339 if not is_num(sigma, ge=0.0):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1340 logger.warning(f'Invalid gaussian_sigma ({sigma}) in _reconstruct_one_plane, '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1341 'set to a default value of 1.0')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1342 sigma = 1.0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1343 ring_width = recon_parameters.get('ring_width', 15)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1344 if not is_int(ring_width, ge=0):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1345 logger.warning(f'Invalid ring_width ({ring_width}) in _reconstruct_one_plane, '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1346 'set to a default value of 15')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1347 ring_width = 15
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1348 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1349 recon_sinogram = spi.gaussian_filter(recon_sinogram, sigma, mode='nearest')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1350 recon_clean = np.expand_dims(recon_sinogram, axis=0)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1351 del recon_sinogram
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1352 recon_clean = tomopy.misc.corr.remove_ring(recon_clean, rwidth=ring_width, ncore=num_core)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1353 logger.debug(f'Filtering and removing ring artifacts took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1354
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1355 return recon_clean
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1356
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1357 def _plot_edges_one_plane(self, recon_plane, title, path=None):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1358 vis_parameters = None#self.config.get('vis_parameters')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1359 if vis_parameters is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1360 weight = 0.1
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1361 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1362 weight = vis_parameters.get('denoise_weight', 0.1)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1363 if not is_num(weight, ge=0.0):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1364 logger.warning(f'Invalid weight ({weight}) in _plot_edges_one_plane, '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1365 'set to a default value of 0.1')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1366 weight = 0.1
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1367 edges = denoise_tv_chambolle(recon_plane, weight=weight)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1368 vmax = np.max(edges[0,:,:])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1369 vmin = -vmax
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1370 if path is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1371 path = self.output_folder
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1372 quick_imshow(edges[0,:,:], f'{title} coolwarm', path=path, cmap='coolwarm',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1373 save_fig=self.save_figs, save_only=self.save_only, block=self.block)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1374 quick_imshow(edges[0,:,:], f'{title} gray', path=path, cmap='gray', vmin=vmin, vmax=vmax,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1375 save_fig=self.save_figs, save_only=self.save_only, block=self.block)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1376 del edges
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1377
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1378 def _reconstruct_one_tomo_stack(self, tomo_stack, thetas, center_offsets=[], num_core=1,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1379 algorithm='gridrec'):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1380 """Reconstruct a single tomography stack.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1381 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1382 # tomo_stack order: row,theta,column
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1383 # input thetas must be in degrees
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1384 # centers_offset: tomography axis shift in pixels relative to column center
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1385 # RV should we remove stripes?
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1386 # https://tomopy.readthedocs.io/en/latest/api/tomopy.prep.stripe.html
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1387 # RV should we remove rings?
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1388 # https://tomopy.readthedocs.io/en/latest/api/tomopy.misc.corr.html
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1389 # RV: Add an option to do (extra) secondary iterations later or to do some sort of convergence test?
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1390 if not len(center_offsets):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1391 centers = np.zeros((tomo_stack.shape[0]))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1392 elif len(center_offsets) == 2:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1393 centers = np.linspace(center_offsets[0], center_offsets[1], tomo_stack.shape[0])
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1394 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1395 if center_offsets.size != tomo_stack.shape[0]:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1396 raise ValueError('center_offsets dimension mismatch in reconstruct_one_tomo_stack')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1397 centers = center_offsets
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1398 centers += tomo_stack.shape[2]/2
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1399
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1400 # Get reconstruction parameters
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1401 recon_parameters = None#self.config.get('recon_parameters')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1402 if recon_parameters is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1403 sigma = 2.0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1404 secondary_iters = 0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1405 ring_width = 15
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1406 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1407 sigma = recon_parameters.get('stripe_fw_sigma', 2.0)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1408 if not is_num(sigma, ge=0):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1409 logger.warning(f'Invalid stripe_fw_sigma ({sigma}) in '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1410 '_reconstruct_one_tomo_stack, set to a default value of 2.0')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1411 ring_width = 15
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1412 secondary_iters = recon_parameters.get('secondary_iters', 0)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1413 if not is_int(secondary_iters, ge=0):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1414 logger.warning(f'Invalid secondary_iters ({secondary_iters}) in '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1415 '_reconstruct_one_tomo_stack, set to a default value of 0 (skip them)')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1416 ring_width = 0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1417 ring_width = recon_parameters.get('ring_width', 15)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1418 if not is_int(ring_width, ge=0):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1419 logger.warning(f'Invalid ring_width ({ring_width}) in _reconstruct_one_plane, '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1420 'set to a default value of 15')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1421 ring_width = 15
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1422
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1423 # Remove horizontal stripe
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1424 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1425 if num_core > num_core_tomopy_limit:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1426 logger.debug('Running remove_stripe_fw on {num_core_tomopy_limit} cores ...')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1427 tomo_stack = tomopy.prep.stripe.remove_stripe_fw(tomo_stack, sigma=sigma,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1428 ncore=num_core_tomopy_limit)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1429 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1430 logger.debug(f'Running remove_stripe_fw on {num_core} cores ...')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1431 tomo_stack = tomopy.prep.stripe.remove_stripe_fw(tomo_stack, sigma=sigma,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1432 ncore=num_core)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1433 logger.debug(f'... tomopy.prep.stripe.remove_stripe_fw took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1434
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1435 # Perform initial image reconstruction
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1436 logger.debug('Performing initial image reconstruction')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1437 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1438 logger.debug(f'Running recon on {num_core} cores ...')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1439 tomo_recon_stack = tomopy.recon(tomo_stack, np.radians(thetas), centers,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1440 sinogram_order=True, algorithm=algorithm, ncore=num_core)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1441 logger.debug(f'... done in {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1442 logger.info(f'Performing initial image reconstruction took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1443
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1444 # Run optional secondary iterations
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1445 if secondary_iters > 0:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1446 logger.debug(f'Running {secondary_iters} secondary iterations')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1447 #options = {'method':'SIRT_CUDA', 'proj_type':'cuda', 'num_iter':secondary_iters}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1448 #RV: doesn't work for me:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1449 #"Error: CUDA error 803: system has unsupported display driver/cuda driver combination."
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1450 #options = {'method':'SIRT', 'proj_type':'linear', 'MinConstraint': 0, 'num_iter':secondary_iters}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1451 #SIRT did not finish while running overnight
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1452 #options = {'method':'SART', 'proj_type':'linear', 'num_iter':secondary_iters}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1453 options = {'method':'SART', 'proj_type':'linear', 'MinConstraint': 0,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1454 'num_iter':secondary_iters}
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1455 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1456 logger.debug(f'Running recon on {num_core} cores ...')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1457 tomo_recon_stack = tomopy.recon(tomo_stack, np.radians(thetas), centers,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1458 init_recon=tomo_recon_stack, options=options, sinogram_order=True,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1459 algorithm=tomopy.astra, ncore=num_core)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1460 logger.debug(f'... done in {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1461 logger.info(f'Performing secondary iterations took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1462
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1463 # Remove ring artifacts
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1464 t0 = time()
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1465 tomopy.misc.corr.remove_ring(tomo_recon_stack, rwidth=ring_width, out=tomo_recon_stack,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1466 ncore=num_core)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1467 logger.debug(f'Removing ring artifacts took {time()-t0:.2f} seconds')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1468
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1469 return tomo_recon_stack
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1470
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1471 def _resize_reconstructed_data(self, data, z_only=False):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1472 """Resize the reconstructed tomography data.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1473 """
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1474 # Data order: row(z),x,y or stack,row(z),x,y
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1475 if isinstance(data, list):
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1476 for stack in data:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1477 assert(stack.ndim == 3)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1478 num_tomo_stacks = len(data)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1479 tomo_recon_stacks = data
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1480 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1481 assert(data.ndim == 3)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1482 num_tomo_stacks = 1
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1483 tomo_recon_stacks = [data]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1484
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1485 if z_only:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1486 x_bounds = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1487 y_bounds = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1488 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1489 # Selecting x bounds (in yz-plane)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1490 tomosum = 0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1491 [tomosum := tomosum+np.sum(tomo_recon_stacks[i], axis=(0,2))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1492 for i in range(num_tomo_stacks)]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1493 select_x_bounds = input_yesno('\nDo you want to change the image x-bounds (y/n)?', 'y')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1494 if not select_x_bounds:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1495 x_bounds = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1496 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1497 accept = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1498 index_ranges = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1499 while not accept:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1500 mask, x_bounds = draw_mask_1d(tomosum, current_index_ranges=index_ranges,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1501 title='select x data range', legend='recon stack sum yz')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1502 while len(x_bounds) != 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1503 print('Please select exactly one continuous range')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1504 mask, x_bounds = draw_mask_1d(tomosum, title='select x data range',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1505 legend='recon stack sum yz')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1506 x_bounds = x_bounds[0]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1507 # quick_plot(tomosum, vlines=x_bounds, title='recon stack sum yz')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1508 # print(f'x_bounds = {x_bounds} (lower bound inclusive, upper bound '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1509 # 'exclusive)')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1510 # accept = input_yesno('Accept these bounds (y/n)?', 'y')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1511 accept = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1512 logger.debug(f'x_bounds = {x_bounds}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1513
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1514 # Selecting y bounds (in xz-plane)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1515 tomosum = 0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1516 [tomosum := tomosum+np.sum(tomo_recon_stacks[i], axis=(0,1))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1517 for i in range(num_tomo_stacks)]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1518 select_y_bounds = input_yesno('\nDo you want to change the image y-bounds (y/n)?', 'y')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1519 if not select_y_bounds:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1520 y_bounds = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1521 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1522 accept = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1523 index_ranges = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1524 while not accept:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1525 mask, y_bounds = draw_mask_1d(tomosum, current_index_ranges=index_ranges,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1526 title='select x data range', legend='recon stack sum xz')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1527 while len(y_bounds) != 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1528 print('Please select exactly one continuous range')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1529 mask, y_bounds = draw_mask_1d(tomosum, title='select x data range',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1530 legend='recon stack sum xz')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1531 y_bounds = y_bounds[0]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1532 # quick_plot(tomosum, vlines=y_bounds, title='recon stack sum xz')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1533 # print(f'y_bounds = {y_bounds} (lower bound inclusive, upper bound '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1534 # 'exclusive)')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1535 # accept = input_yesno('Accept these bounds (y/n)?', 'y')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1536 accept = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1537 logger.debug(f'y_bounds = {y_bounds}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1538
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
1539 # Selecting z bounds (in xy-plane) (only valid for a single image stack)
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1540 if num_tomo_stacks != 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1541 z_bounds = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1542 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1543 tomosum = 0
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1544 [tomosum := tomosum+np.sum(tomo_recon_stacks[i], axis=(1,2))
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1545 for i in range(num_tomo_stacks)]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1546 select_z_bounds = input_yesno('Do you want to change the image z-bounds (y/n)?', 'n')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1547 if not select_z_bounds:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1548 z_bounds = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1549 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1550 accept = False
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1551 index_ranges = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1552 while not accept:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1553 mask, z_bounds = draw_mask_1d(tomosum, current_index_ranges=index_ranges,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1554 title='select x data range', legend='recon stack sum xy')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1555 while len(z_bounds) != 1:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1556 print('Please select exactly one continuous range')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1557 mask, z_bounds = draw_mask_1d(tomosum, title='select x data range',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1558 legend='recon stack sum xy')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1559 z_bounds = z_bounds[0]
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1560 # quick_plot(tomosum, vlines=z_bounds, title='recon stack sum xy')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1561 # print(f'z_bounds = {z_bounds} (lower bound inclusive, upper bound '+
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1562 # 'exclusive)')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1563 # accept = input_yesno('Accept these bounds (y/n)?', 'y')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1564 accept = True
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1565 logger.debug(f'z_bounds = {z_bounds}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1566
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1567 return(x_bounds, y_bounds, z_bounds)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1568
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1569
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1570 def run_tomo(input_file:str, output_file:str, modes:list[str], center_file=None, num_core=-1,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1571 output_folder='.', save_figs='no', test_mode=False) -> None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1572
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1573 if test_mode:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1574 logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s'
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1575 level = logging.getLevelName('INFO')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1576 logging.basicConfig(filename=f'{output_folder}/tomo.log', filemode='w',
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1577 format=logging_format, level=level, force=True)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1578 logger.info(f'input_file = {input_file}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1579 logger.info(f'center_file = {center_file}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1580 logger.info(f'output_file = {output_file}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1581 logger.debug(f'modes= {modes}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1582 logger.debug(f'num_core= {num_core}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1583 logger.info(f'output_folder = {output_folder}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1584 logger.info(f'save_figs = {save_figs}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1585 logger.info(f'test_mode = {test_mode}')
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1586
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1587 # Check for correction modes
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
1588 legal_modes = ['reduce_data', 'find_center', 'reconstruct_data', 'combine_data', 'all']
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1589 if modes is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1590 modes = ['all']
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
1591 if not all(True if mode in legal_modes else False for mode in modes):
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
1592 raise ValueError(f'Invalid parameter modes ({modes})')
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1593
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1594 # Instantiate Tomo object
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1595 tomo = Tomo(num_core=num_core, output_folder=output_folder, save_figs=save_figs,
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1596 test_mode=test_mode)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1597
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1598 # Read input file
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1599 data = tomo.read(input_file)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1600
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1601 # Generate reduced tomography images
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1602 if 'reduce_data' in modes or 'all' in modes:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1603 data = tomo.gen_reduced_data(data)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1604
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1605 # Find rotation axis centers for the tomography stacks.
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1606 center_data = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1607 if 'find_center' in modes or 'all' in modes:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1608 center_data = tomo.find_centers(data)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1609
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1610 # Reconstruct tomography stacks
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1611 if 'reconstruct_data' in modes or 'all' in modes:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1612 if center_data is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1613 # Read input file
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1614 center_data = tomo.read(center_file)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1615 data = tomo.reconstruct_data(data, center_data)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1616 center_data = None
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1617
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1618 # Combine reconstructed tomography stacks
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1619 if 'combine_data' in modes or 'all' in modes:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1620 data = tomo.combine_data(data)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1621
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1622 # Write output file
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
1623 if data is not None and not test_mode:
69
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1624 if center_data is None:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1625 data = tomo.write(data, output_file)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1626 else:
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1627 data = tomo.write(center_data, output_file)
fba792d5f83b planemo upload for repository https://github.com/rolfverberg/galaxytools commit ab9f412c362a4ab986d00e21d5185cfcf82485c1
rv43
parents:
diff changeset
1628
71
1cf15b61cd83 planemo upload for repository https://github.com/rolfverberg/galaxytools commit 366e516aef0735af2998c6ff3af037181c8d5213
rv43
parents: 69
diff changeset
1629 logger.info(f'Completed modes: {modes}')