I am writing a python code that needs to calculate a lot of integrals really fast, so instead of using scipy.integrate
using a c
function, I am using ctypes
to calculate all of my integrals in c
.
I am importing the c function with this code:
ctypes.CDLL('/usr/lib/i386-linux-gnu/libgslcblas.so', mode=ctypes.RTLD_GLOBAL)
ctypes.CDLL('/usr/lib/i386-linux-gnu/libgsl.so', mode=ctypes.RTLD_GLOBAL)
lib = ctypes.CDLL('/home/aurelien/Desktop/Project/within_opt_model_1/integral_test.so')
In c, I am calculating the integral using:
gsl_integration_workspace *w = gsl_integration_workspace_alloc(200);
double result, err;
gsl_function F;
F.function = &integral;
F.params = ¶ms;
gsl_integration_qags(&F, z, 1, 1e-6, 1e-6, 200, w, &result, &err);
gsl_integration_workspace_free(w);
The problem is that sometimes (~5% of the time), since my integral is slowly convergent, it will kill my program and print:
gsl: qags.c:563: ERROR: interval is divergent, or slowly convergent
Default GSL error handler invoked
Aborted (core dumped)
This problem does not occur if I put 1e-10
instead of 1e-6
, but it makes the integral calculation go a lot slower.
I would like a way to do something like a try, catch
so that most of the time it uses 1e-6
and goes fast, and when it fails it uses 1e-10
.
I have tried doing:
int status = gsl_integration_qags(&F, z, 1, 1e-6, 1e-6, 200, w, &result, &err);
printf("%in", status);
But this only prints 0, since the error aborts before returning any value.
My guess is that I need create my own error handler method, but I don’t know how to do that.
Thank you very much!
(I can show you more code if that’s helpful, I tried to keep everything concise).
Hi,
I met the same problem when I read someother’s codes. But unfortunately, I can’t solve it.
The function is:
CvMat* ransac_xform( struct feature* features, int n, int mtype,
ransac_xform_fn xform_fn, int m, double p_badxform,
ransac_err_fn err_fn, double err_tol,
struct feature*** inliers, int* n_in )
{
struct feature** matched, ** sample, ** consensus, ** consensus_max = NULL;
struct ransac_data* rdata;
CvPoint2D64f* pts, * mpts;
CvMat* M = NULL;
gsl_rng* rng;
double p, in_frac = RANSAC_INLIER_FRAC_EST;
int i, nm, in, in_min, in_max = 0, k = 0;
nm = get_matched_features( features, n, mtype, &matched );
if( nm < m )
{
fprintf( stderr, "Warning: not enough matches to compute xform, %s"
" line %dn", __FILE__, __LINE__ );
goto end;
}
/* initialize random number generator */
rng = gsl_rng_alloc( gsl_rng_mt19937 );
gsl_rng_set( rng, time(NULL) );
in_min = calc_min_inliers( nm, m, RANSAC_PROB_BAD_SUPP, p_badxform );
p = pow( 1.0 - pow( in_frac, m ), k );
i = 0;
while( p > p_badxform )
{
sample = draw_ransac_sample( matched, nm, m, rng );
extract_corresp_pts( sample, m, mtype, &pts, &mpts );
M = xform_fn( pts, mpts, m );
if( ! M )
goto iteration_end;
in = find_consensus( matched, nm, mtype, M, err_fn, err_tol, &consensus);
if( in > in_max )
{
if( consensus_max )
free( consensus_max );
consensus_max = consensus;
in_max = in;
in_frac = (double)in_max / nm;
}
else
free( consensus );
cvReleaseMat( &M );
iteration_end:
release_mem( pts, mpts, sample );
p = pow( 1.0 — pow( in_frac, m ), ++k );
}
/* calculate final transform based on best consensus set */
if( in_max >= in_min )
{
extract_corresp_pts( consensus_max, in_max, mtype, &pts, &mpts );
M = xform_fn( pts, mpts, in_max );
in = find_consensus( matched, nm, mtype, M, err_fn, err_tol, &consensus);
cvReleaseMat( &M );
release_mem( pts, mpts, consensus_max );
extract_corresp_pts( consensus, in, mtype, &pts, &mpts );
M = xform_fn( pts, mpts, in );
if( inliers )
{
*inliers = consensus;
consensus = NULL;
}
if( n_in )
*n_in = in;
release_mem( pts, mpts, consensus );
}
else if( consensus_max )
{
if( inliers )
*inliers = NULL;
if( n_in )
*n_in = 0;
free( consensus_max );
}
gsl_rng_free( rng );
end:
for( i = 0; i < nm; i++ )
{
rdata = feat_ransac_data( matched[i] );
matched[i]->feature_data = rdata->orig_feat_data;
free( rdata );
}
free( matched );
return M;
}
I didn’t find the function gsl_rng_alloc that was repeatlly called, and the function ransac_xform is only called by the main function:
int main( int argc, char** argv )
{
IplImage* img1, * img2, * stacked;
struct feature* feat1, * feat2, * feat;
struct feature** nbrs;
struct kd_node* kd_root;
CvPoint pt1, pt2;
double d0, d1;
int n1, n2, k, i, m = 0;
img1 = cvLoadImage( img1_file, 1 );
if( ! img1 )
fatal_error( «unable to load image from %s», img1_file );
img2 = cvLoadImage( img2_file, 1 );
if( ! img2 )
fatal_error( «unable to load image from %s», img2_file );
stacked = stack_imgs( img1, img2 );
fprintf( stderr, "Finding features in %s...n", img1_file );
n1 = sift_features( img1, &feat1 );
fprintf( stderr, "Finding features in %s...n", img2_file );
n2 = sift_features( img2, &feat2 );
kd_root = kdtree_build( feat2, n2 );
for( i = 0; i < n1; i++ )
{
feat = feat1 + i;
k = kdtree_bbf_knn( kd_root, feat, 2, &nbrs, KDTREE_BBF_MAX_NN_CHKS );
if( k == 2 )
{
d0 = descr_dist_sq( feat, nbrs[0] );
d1 = descr_dist_sq( feat, nbrs[1] );
if( d0 < d1 * NN_SQ_DIST_RATIO_THR )
{
pt1 = cvPoint( cvRound( feat->x ), cvRound( feat->y ) );
pt2 = cvPoint( cvRound( nbrs[0]->x ), cvRound( nbrs[0]->y ) );
pt2.y += img1->height;
cvLine( stacked, pt1, pt2, CV_RGB(255,0,255), 1, 8, 0 );
m++;
feat1[i].fwd_match = nbrs[0];
}
}
free( nbrs );
}
fprintf( stderr, "Found %d total matchesn", m );
cvNamedWindow( "Matches", 1 );
cvShowImage( "Matches", stacked );
cvWaitKey( 0 );
/*
UNCOMMENT BELOW TO SEE HOW RANSAC FUNCTION WORKS
Note that this line above:
feat1[i].fwd_match = nbrs[0];
is important for the RANSAC function to work.
*/
{
CvMat* H;
H = ransac_xform( feat1, n1, FEATURE_FWD_MATCH, lsq_homog, 4, 0.01,
homog_xfer_err, 3.0, NULL, NULL );
if( H )
{
IplImage* xformed;
xformed = cvCreateImage( cvGetSize( img2 ), IPL_DEPTH_8U, 3 );
cvWarpPerspective( img1, xformed, H,
CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS,
cvScalarAll( 0 ) );
cvNamedWindow( "Xformed", 1 );
cvShowImage( "Xformed", xformed );
cvWaitKey( 0 );
cvReleaseImage( &xformed );
cvReleaseMat( &H );
}
}
cvReleaseImage( &stacked );
cvReleaseImage( &img1 );
cvReleaseImage( &img2 );
kdtree_release( kd_root );
free( feat1 );
free( feat2 );
return 0;
}
Any help very much appreciated.
Thanks!
FastQTL — GNU Scientific Library (GSL) Domain Error
Hi all,
I am using FastQTL (http://fastqtl.sourceforge.net/) for eQTL mapping in our genotyping (~20K SNPs) & RNA-Seq data. It basically uses a VCF file for the genotypes and a BED file for gene expression levels.
For genome-wide analysis it works very well so far; but I am missing information for some regions when it gives errors as it uses the GSL for maximum likelihood estimations during the permutation step. The error is as below:
»gsl: beta.c:44: ERROR: domain error.
Default GSL error handler invoked.
Aborted»
As I said, it doesn’t occur frequently and for all the genetic regions. I checked the values in VCF and BED files many times, and they all seem OK. I analyze the genome in chunks, and if this error occurs in one of the chunks, I lose all the information in that chunk.
Since it says »beta», I think it might be about the beta functions, but I couldn’t find more information to fix the problem. Any suggestions on this error?
eQTL
GSL
RNA-Seq
SNP
FastQTL
• 4.7k views
Can someone please explain in detail the meaning of the following section of the paper
2.2 Finding a candidate QTL per phenotype For simplicity, we will focus on a single molecular phenotype P quantified in a set of N
samples. Let G be the set of genotype dosages at L variant sites
located within a cis-window of 6 W Mb of the genomic location of P. To
discover the best candidate QTL for P, FastQTL measures Pearson
product-moment correlation coefficients between P and all L variants
in G, stores the most strongly correlated variant q [ G as candidate
QTL, and assesses its significance by calculating a nominal P-value pn
with standard significance tests for Pearson correlation.
How can this be affected by a test where only one SNP and its LD proxies (r=0.8) is tested.
Thanks!
Thank you very much for your answer mobius, I have a follow-up question. If I’m only testing a restricted number of SNPs going into the analysis e.g 240 will program computed the BETA p-value based on the number of SNPs in my vcf file (0.05/240= 0.0002083333 )? Or in some way compute a genome-wide BETA p-value?
Login before adding your answer.
I hope someone could say me where can i find the meaning of:
gsl: sinint.c:359: ERROR: domain error
Default GSL error handler invoked.
Aborted
or where i can find it.
I don’t understand the meaning. I tried to find the error but it seems
inside an «if ()», and it’s not a GSL instruction. The program was ok
since i addicted at the function:
if (fabs (j — j_esterno) == 1)
{
distanza_collinear = dist_adiacenti_collinear ;
}
else
{
distanza_collinear = (dist_adiacenti_collinear / fabs (j —
j_esterno)) + (lunghezza_dipolo / fabs (j_esterno — j — 1)) ;
}
Now the function is:
void onda_vert_piano_gp (gsl_matrix_complex *matrice_acc_dipoli,
gsl_vector *elementi_guasti_x, gsl_vector *elementi_guasti_y, int
guasti, int N, int M, int dist_adiacenti_side_by_side, int
dist_adiacenti_collinear, int lunghezza_dipolo, char *parassita)
{
int i, j, i_esterno, j_esterno, guasto = 0 ;
double distanza_collinear, distanza_side_by_side ;
gsl_complex accoppiamento, collinear_x, side_y ;
for (i_esterno = 0 ; i_esterno < M ; i_esterno++)
{
for (j_esterno = 0 ; j_esterno < N ; j_esterno++)
{
for (i = 0 ; i < M ; i++)
{
/* Lungo x sono collinear. */
// distanza_collinear = dist_adiacenti_collinear * fabs
(i_esterno — i) ;
distanza_side_by_side =
dist_adiacenti_side_by_side / fabs (i_esterno — i) ;
for (j = 0 ; j < N ; j++)
{
if ((i_esterno == i) && (j_esterno == j))
{
guasto = ricerca_guasto_piano
(elementi_guasti_x, elementi_guasti_y, guasti, i, j, i_esterno,
j_esterno) ;
if (guasto == 1)
{
if (strcmp (parassita, «ca») == 0)
{
GSL_SET_COMPLEX (&accoppiamento, 10e30,
0.0) ; /* Caso di ca. */
}
else
{
GSL_SET_COMPLEX (&accoppiamento, 0.0,
0.0) ; /* Caso di cc. */
}
guasto = 0 ;
}
else
{
auto_impedenza (&accoppiamento,
lunghezza_dipolo) ;
}
}
else
{
/* Lungo y sono side-by-side. */
if (fabs (j — j_esterno) == 1.0)
{
distanza_collinear = dist_adiacenti_collinear ;
}
else
{
distanza_collinear = (dist_adiacenti_collinear / fabs (j —
j_esterno)) + (lunghezza_dipolo / fabs (j_esterno — j — 1)) ;
}
guasto_piano_d_gp (distanza_collinear,
distanza_side_by_side, lunghezza_dipolo, &side_y, &collinear_x,
&accoppiamento) ;
}
gsl_matrix_complex_set (matrice_acc_dipoli, N *
i_esterno + j_esterno, N * i + j, accoppiamento) ;
}
}
}
}
}
Help Please!!!