/tomo/pyhst

To get this branch, use:
bzr branch http://darksoft.org/webbzr/tomo/pyhst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include"Python.h"
#include"structmember.h"
#include<string.h>

#include "numpy/arrayobject.h"

#ifdef LINUX
 #define _FILE_OFFSET_BITS  64
#endif


#define DEBUG(a)


 /* The error object to expose 
 */

static PyObject *ErrorObject;
#define onError(message)\
  { PyErr_SetString(ErrorObject, message); return NULL;}


#define  __SINO_Filter_name__  "SINO_Filter"
#define __init__Filter__      initSINO_Filter


    int hst_ffa8__(int* , int* , int* , float *, int* , float *, int *) ;
    int hst_ffs8__(int* , int* , int* , float *, int* , float *, int *) ;





void C_HST_EXPNT(int dim_exponts,int dim_fft2,int radix,float *EXPONENTS,
		 int *status)
{
  int hst_expnt__(int *maxdat,int * numdat,int * jump,float  *twiddle, int *status);
  
  hst_expnt__(&dim_exponts,&dim_fft2,&radix,EXPONENTS,status);
}


static char Filter_doc[]=
"/* "
"   Filter( item ,   SINO_FILTER_PARA     )"
"                     ^"
"                     dictionary"
" */"
;

static PyObject *
Filter(PyObject *self_a, PyObject *args)
{
  PyObject * A         ;

  PyObject * para_dict ;



  PyObject * dic_key;
  PyObject * dic_value;
  /* double threshold ; */

  int i;
  int dim_3,dim2,dim_1;

  float * a_data;

  float * sum_line;
  float  *EXPONENTS ;
  int status;
  int True=1;
  int dim_exponts;
  int dim_fft, two_power;
  


  float * FILTER;

  int slice_count, proj_count;


 if(!PyArg_ParseTuple(args,"OO:CDD_Filter",&A, &para_dict  ))
   return NULL;

 /* check the Objects */

  if(!PyArray_Check(A ))    onError("not a PyArray, argument 1");
  if(!PyDict_Check(para_dict ))        onError("not a PyDict, argument 2");

  /* check the types */

  if( ((PyArrayObject *) A )  ->descr->type_num != PyArray_FLOAT ) onError(" arg 1 is not an array of float " ) ;

  /* check n of dimensione */

  if( ((PyArrayObject *) A)->nd !=3)  onError(" arg 1 is not a 3D array " ) ;
 

  /* check contiguity */

  if(  ((PyArrayObject *) A) ->flags %2 == 0) onError(" array has to be contiguous");

  a_data = (float *) ((PyArrayObject *) A)->data;
 
  /* straighforward addressing scheme in case of contiguous memory. Or you'll need to use strides */
		      
  dim_3 = ((PyArrayObject *) A)->dimensions[0] ;
  dim2 = ((PyArrayObject *) A)->dimensions[1] ;
  dim_1 = ((PyArrayObject *) A)->dimensions[2] ;

  /* getting parameter(s) contained in the dictionary */
  
  dic_key = PyString_FromString("FILTER");
  dic_value =  PyDict_GetItem(para_dict , dic_key);
  Py_DECREF(dic_key); 
  if( dic_value == NULL) 
    onError(" unable to find the FILTER  key in the passed dictionary");
  
  if( !PyArray_Check(dic_value) ) 
    onError("supplied value for FILTER is not an ARRAY ");

  if( ((PyArrayObject *) dic_value )  ->descr->type_num != PyArray_FLOAT ) onError(" Filter is not an array of float " ) ;  

  if( ((PyArrayObject *)dic_value )->nd !=1)  onError(" Filter is not a 1D array " ) ;

  if(  ((PyArrayObject *)dic_value  ) ->flags %2 == 0) onError("Filter  has to be contiguous");



  two_power = (int)(log((2. * dim_1 - 1)) / log(2.0) + 0.9999);
  dim_fft = 1; for(i=1;i<two_power; i++) dim_fft*=2;

  if(  ((PyArrayObject *)dic_value )->dimensions[0] != dim_fft )  {
    printf("FILTER dimension should be = %d \n", dim_fft);
    onError("Filter  has wrong dimension, must correspond to the padded slice lenght ( power of 2 lenght)");
  }
  



  /* trusting passed value */
  FILTER  = (float*) ((PyArrayObject *)dic_value ) ->data ;
  
  

  
  
#define AA(i,j,k)  a_data[(k)+dim_1*((j)+dim2*(i))  ]
  

  sum_line = (float*) malloc( dim_fft*sizeof(float)  );
  dim_exponts= 2 * dim_fft;
  EXPONENTS  = malloc((dim_exponts) * sizeof(float));
  C_HST_EXPNT(dim_exponts, dim_fft*2, 8, EXPONENTS, &status);
  

  
  memset(sum_line,0,dim_fft*sizeof(float) );
  
  
  
  
  for(slice_count=0; slice_count< dim_3; slice_count++) {
    
    memset(sum_line,0,dim_fft*sizeof(float) );
    for( proj_count=0 ; proj_count< dim2; proj_count++) {
      
      for(i=0; i< dim_1; i++) {
	
	sum_line[i] += AA(  slice_count, proj_count, i) ;
	
      }
      
    }
    
    for(i=0; i< dim_1; i++) {
      sum_line[i] /= dim2 ;
    }
      

    
    /* Filtering */
    hst_ffa8__(&dim_fft, &dim_fft, &dim_exponts, EXPONENTS, &True  ,sum_line , &status);
    /******************************************************
     * Multiply the Fourier transformed array with filter *
     ******************************************************/    
    for(i=0;i<dim_fft;i++) {
      sum_line[i] = sum_line[i] * FILTER[i]/dim_fft;
    }
    
    /***************************************************
     * Inverse Fourier transform to give convolution   *
     ***************************************************/
    
    hst_ffs8__(&dim_fft,&dim_fft, &dim_exponts, EXPONENTS, &True, sum_line, &status);
    
    
    /* Subtraction */
    for( proj_count=0 ; proj_count< dim2; proj_count++) {
      
      for(i=0; i< dim_1; i++) {
	
	AA(  slice_count, proj_count, i) -=  sum_line[i]  ;
	
      }
      
    }    
    
  }
  
  free(sum_line);
  
  Py_INCREF(Py_None);
  return Py_None;
  
  
}



static PyMethodDef SINO_Filter_functions [] = {
  {"Filter", Filter,  METH_VARARGS, Filter_doc},
  { NULL, NULL}
};






void 
__init__Filter__   (void)
{
  PyObject *m, *d;
  m = Py_InitModule(  __SINO_Filter_name__    ,  SINO_Filter_functions  );
  d = PyModule_GetDict(m);
  ErrorObject = Py_BuildValue("s",  __SINO_Filter_name__  ".error");
  PyDict_SetItemString(d,"error", ErrorObject);
  if(PyErr_Occurred())
    Py_FatalError("can't initialize module " __SINO_Filter_name__  );
  
#ifdef import_array
  import_array();
#endif
}