/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
#include"Python.h"
#include"structmember.h"
#include<string.h>

//#include "/scisoft/ESRF_sw/linux_i386/include/python2.2/numpy/arrayobject.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_Smooth"
#define __init__Filter__      initSINO_Filter_Smooth

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 dim_3,dim_2,dim_1;

  float * a_data;

  float * sum_line;
  int slice_count, proj_count;

  int width;
  int i;
  int x;

  int span;
  float* smooth_sum_line;

  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] ;
  dim_2 = ((PyArrayObject *) A)->dimensions[1] ;
  dim_1 = ((PyArrayObject *) A)->dimensions[2] ;

  /* getting parameter(s) contained in the dictionary */
  dic_key = PyString_FromString("SmoothFilter");
  dic_value =  PyDict_GetItem(para_dict, dic_key);
  Py_DECREF(dic_key); 
  if( dic_value == NULL) 
    onError(" SmoothFilter key is not integer");
  
  /*
  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");
  */

  if( !PyInt_Check(dic_value) )
    onError(" unable to find the FILTER key in the passed dictionary");    
   
  /* trusting passed value */
  span = (int) (PyInt_AsLong(dic_value));
  printf("SmoothFilter: span = %i\n", span);
  if ( dim_1 < span )
    span = dim_1;

  if (span % 2)
    span += 1;
  
#define AA(i,j,k)  a_data[(k)+dim_1*((j)+dim_2*(i))  ]

  sum_line = (float*) malloc( dim_1*sizeof(float)  );
  smooth_sum_line = (float*) calloc(dim_1*dim_2, sizeof(float)); 

  for(slice_count=0; slice_count< dim_3; slice_count++) {
    
    memset(sum_line,0,dim_1*sizeof(float) );
    for( proj_count=0 ; proj_count< dim_2; proj_count++) {
      
      for(i=0; i< dim_1; i++) {
	sum_line[i] += AA(  slice_count, proj_count, i) ;
      }
    }
    
  /* smoothing with moving window (span) */
  memset(smooth_sum_line,0,dim_1*sizeof(float) );

  for (x=0; x<span-1; x++) {
    width = x;
    for(i=-width; i<width+1; i++) {
      smooth_sum_line[x] += sum_line[x+i]; 
    }
    smooth_sum_line[x] = smooth_sum_line[x] / (2*x+1);
  }
 
   width = (int) (span-1)/2;
   for (x=span-1; x<dim_1; x++) {
     for(i=-width; i<width+1; i++) {
       smooth_sum_line[x] += sum_line[x+i]; 
     }
     smooth_sum_line[x] = smooth_sum_line[x] / span;
   }

    /* write back to sino */
    for( proj_count=0 ; proj_count< dim_2; proj_count++) {
      
      for(i=0; i< dim_1; i++) {
	AA(slice_count,proj_count,i) = AA(slice_count,proj_count,i) / sum_line[i] * smooth_sum_line[i];
      }
    }
    /*
    for(i=0; i< dim_1; i++)
      printf("%i  %f   %f\n", i, sum_line[i], smooth_sum_line[i] );
    */
  }
  free(sum_line);
  free(smooth_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__()
{
  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
}