1+ import os
2+ import numpy as np
3+ import cupy as cp
4+ import scipy
5+ import pyfftw
6+ import pyfftw .interfaces .numpy_fft as fft
7+ from cupyx .scipy .fft import rfft2 , fft2 , fftshift
8+ import httomolibgpu
9+ from httomolibgpu .cuda_kernels import load_cuda_module
10+ from httomolibgpu .prep .normalize import normalize
11+ import httomolibgpu .recon .rotation
12+
13+ import matplotlib .pyplot as plt
14+ import math
15+ import time
16+
17+ import httomolibgpu .recon .rotation as rotation
18+ import importlib .util
19+ import sys
20+
21+ def _create_mask_numpy (nrow , ncol , radius , drop ):
22+ du = 1.0 / ncol
23+ dv = (nrow - 1.0 ) / (nrow * 2.0 * np .pi )
24+ cen_row = np .int16 (np .ceil (nrow / 2.0 ) - 1 )
25+ cen_col = np .int16 (np .ceil (ncol / 2.0 ) - 1 )
26+ drop = min (drop , np .int16 (np .ceil (0.05 * nrow )))
27+ mask = np .zeros ((nrow , ncol ), dtype = "float32" )
28+ for i in range (nrow ):
29+ pos = np .int16 (np .round (((i - cen_row ) * dv / radius ) / du ))
30+ (pos1 , pos2 ) = np .clip (np .sort ((- pos + cen_col , pos + cen_col )), 0 , ncol - 1 )
31+ mask [i , pos1 : pos2 + 1 ] = 1.0
32+ mask [cen_row - drop : cen_row + drop + 1 , :] = 0.0
33+ mask [:, cen_col - 1 : cen_col + 2 ] = 0.0
34+ return mask
35+
36+ def _create_mask (nrow , ncol , radius , drop ):
37+ du = 1.0 / ncol
38+ dv = (nrow - 1.0 ) / (nrow * 2.0 * np .pi )
39+ cen_row = int (math .ceil (nrow / 2.0 ) - 1 )
40+ cen_col = int (math .ceil (ncol / 2.0 ) - 1 )
41+ drop = min ([drop , int (math .ceil (0.05 * nrow ))])
42+
43+ block_x = 128
44+ block_y = 1
45+ block_dims = (block_x , block_y )
46+ grid_x = (ncol // 2 + 1 + block_x - 1 ) // block_x
47+ grid_y = nrow
48+ grid_dims = (grid_x , grid_y )
49+ mask = cp .empty ((nrow , ncol // 2 + 1 ), dtype = "uint16" )
50+ params = (
51+ ncol ,
52+ nrow ,
53+ cen_col ,
54+ cen_row ,
55+ cp .float32 (du ),
56+ cp .float32 (dv ),
57+ cp .float32 (radius ),
58+ cp .float32 (drop ),
59+ mask ,
60+ )
61+ module = load_cuda_module ("generate_mask" )
62+ kernel = module .get_function ("generate_mask" )
63+ kernel (grid_dims , block_dims , params )
64+ return mask
65+
66+ def _create_mask_new (nrow , ncol , radius , drop ):
67+ du = 1.0 / ncol
68+ dv = (nrow - 1.0 ) / (nrow * 2.0 * np .pi )
69+ cen_row = int (math .ceil (nrow / 2.0 ) - 1 )
70+ cen_col = int (math .ceil (ncol / 2.0 ) - 1 )
71+ drop = min ([drop , int (math .ceil (0.05 * nrow ))])
72+
73+ block_x = 128
74+ block_y = 1
75+ block_dims = (block_x , block_y )
76+ grid_x = (ncol + block_x - 1 ) // block_x
77+ grid_y = nrow
78+ grid_dims = (grid_x , grid_y )
79+ mask = cp .empty ((nrow , ncol ), dtype = "float32" )
80+ params = (
81+ ncol ,
82+ nrow ,
83+ cen_col ,
84+ cen_row ,
85+ cp .float32 (du ),
86+ cp .float32 (dv ),
87+ cp .float32 (radius ),
88+ cp .float32 (drop ),
89+ mask ,
90+ )
91+ module = load_cuda_module ("generate_mask" )
92+ kernel = module .get_function ("generate_mask" )
93+ kernel (grid_dims , block_dims , params )
94+ return mask
95+
96+ # Load the sinogram data
97+ path_lib = os .path .dirname (httomolibgpu .__file__ )
98+ in_file = os .path .abspath (
99+ os .path .join (path_lib , ".." , "tests/test_data/" , "i12LFOV.npz" )
100+ )
101+ l_infile = np .load (in_file )
102+
103+ projdata = cp .asarray (l_infile ["projdata" ])
104+ flats = cp .asarray (l_infile ["flats" ])
105+ darks = cp .asarray (l_infile ["darks" ])
106+ del l_infile
107+
108+ data_normalised = normalize (projdata , flats , darks , minus_log = False )
109+ del flats , darks , projdata
110+
111+ spec = importlib .util .spec_from_file_location ("rotation_new" , "C:/Work/DiamondLightSource/httomolibgpu/docs/source/rotation_new.py" )
112+ rotation_new = importlib .util .module_from_spec (spec )
113+ sys .modules ["rotation_new" ] = rotation_new
114+ spec .loader .exec_module (rotation_new )
115+
116+ # --- Running the centre of rotation algorithm ---#
117+ mid_slice = data_normalised .shape [1 ] // 2
118+
119+ rotation_value = rotation .find_center_vo (data_normalised [:, mid_slice , :]);
120+ new_rotation_value = rotation_new .find_center_vo (data_normalised [:, mid_slice , :]);
121+
122+ print (rotation_value )
123+ print (new_rotation_value )
124+
125+ #subplot(r,c) provide the no. of rows and columns
126+ # f, axarr = plt.subplots(2,2)
127+
128+ # mask_2 = _create_mask(2 * nrow, ncol, 0.5 * ratio * ncol, drop)
129+
130+ # use the created array to output your multiple images. In this case I have stacked 4 images vertically
131+ # axarr[0, 0].imshow(mask.get())
132+ # axarr[0, 0].set_title('Original mask')
133+ # axarr[0, 1].imshow(mask_2.get())
134+ # axarr[0, 1].set_title('GPU mask')
135+ # axarr[1, 0].imshow(mask.get() - mask_2.get())
136+ # axarr[1, 0].set_title('Difference of masks')
137+ # axarr[1, 1].imshow(mask.get() - mask_2.get())
138+ # axarr[1, 1].set_title('Difference of masks')
139+
140+ # plt.show()
141+
142+
143+
0 commit comments