found error in the estimation of the energy index for energy interpolation; added script to estimate likelihood for specified location

This commit is contained in:
2025-07-22 13:42:28 +03:00
parent 54febb8bbf
commit 23abdd884f
3 changed files with 252 additions and 24 deletions

View File

@@ -180,21 +180,6 @@ static PyObject * solve_for_locations(PyObject *self, PyObject *args)
double pval, eloc, p2, p3;
int idx1d, idx2d;
/*
pval = *psfvalfromptr(smatd, smat->dimensions, 1744, 50, 50);
printf("1744 50 50 %f\n", pval);
pval = *psfvalfromptr(smatd, smat->dimensions, 1744, 40, 48);
printf("1744 40 48 %f\n", pval);
pval = *psfvalfromptr(smatd, smat->dimensions, 1744, 20, 52);
printf("1744 20 52 %f\n", pval);
printf("bwd %f %f %f\n", xptr[0], xptr[10], xptr[20]);
printf("xptr %f %f %f\n",xcptr[0], xcptr[10], xcptr[20]);
printf("dimension %d\n", xc->dimensions[0]);
*/
for (loc=0; loc < xc->dimensions[0]; loc++) // loop over sky locations
{
msum = 0;
@@ -251,8 +236,6 @@ static PyObject * solve_for_locations(PyObject *self, PyObject *args)
if (pval > 1e-10)
{
bw[msum] = pval;
//printf("%d %d %d %f %f %f %f %f\n", k[ctr], idx1d, idx2d, inpixdx, inpixdy, dx, dy, pval);
//printf("%d %d %d %f %f %f %f %f\n", k[ctr], idx1d, idx2d, xptr[ctr], yptr[ctr], xcptr[loc], ycptr[loc], pval);
msum += 1;
};
@@ -347,7 +330,7 @@ static PyObject * solve_for_locations_eintp(PyObject *self, PyObject *args)
long ctr, msum=0;
double lkl, erf;
double pval, eloc, p2, p3, ptmp;
double pval, eloc, p2, p3;
int idx1d, idx2d;
//return psfdata + ((k*dims[1] + ei)*dims[2] + xi)*dims[3] + yi;
@@ -462,11 +445,153 @@ static PyObject * solve_for_locations_eintp(PyObject *self, PyObject *args)
static PyObject * solve_for_rates(PyObject *self, PyObject *args)
{
//xc, yc --- wcs locations, events has coordinates in the same locations, and psf have the same grid as well
// the only additional parameter to events are pk scale (rate scale in respect to psf) and rotation angle
PyArrayObject *psfi, *eidx, *x, *y, *rates, *roll, *pk, *smat;
double xc, yc;
int rid, ctr;
double x1, y1, dx, dy, eloc;
if (!PyArg_ParseTuple(args, "OOOOOOOdddO", &psfi, &eidx, &x, &y, &roll, &pk, &rates, &xc, &yc, &eloc, &smat)) return NULL;
// -------------------------- ===============
// those are events properties those for sky smat it array for psf matrices
npy_intp snew = {rates->dimensions[0]};
PyArrayObject * lkls = PyArray_SimpleNew(1, &snew, NPY_DOUBLE);
double * lklsd = (double*) lkls->data;
double * smatd = (double*) smat->data;
double *ca = (double*)malloc(sizeof(double)*x->dimensions[0]);
double *sa = (double*)malloc(sizeof(double)*x->dimensions[0]);
double* nparrptr = (double*) roll->data;
for (ctr=0; ctr < x->dimensions[0]; ctr++)
{
ca[ctr] = cos(nparrptr[ctr]);
sa[ctr] = sin(nparrptr[ctr]);
};
double * bw = (double*)malloc(sizeof(double)*psfi->dimensions[0]); //not more then thet will be used for each location
Py_BEGIN_ALLOW_THREADS;
double inpixdx, inpixdy, rate;
double * pkd = (double*) pk->data;
long * k = (long*)psfi->data;
double * ek = (double*)eidx->data;
int ei;
double* xptr = (double*) x->data;
double* yptr = (double*) y->data;
long msum=0;
double lkl, erf;
double pval, p2, p3;
int idx1d, idx2d;
for (rid=0; rid < rates->dimensions[0]; rid++) // loop over sky locations
{
msum = 0;
for (ctr=0; ctr < psfi->dimensions[0]; ctr++) // for each sky location loop over all provided events
{
x1 = (xc - xptr[ctr]);
y1 = (yc - yptr[ctr]);
//rotate by the event roll angle, dx dy centered at the psf center (central pixel of 101x101 map)
dx = x1*ca[ctr] - y1*sa[ctr]; //+ 50;
dy = y1*ca[ctr] + x1*sa[ctr]; // + 50.;
// temporary hardcode psf shape is 101x101
ei = (int)(ek[ctr]);
erf = ek[ctr] - (double)(ei);
//printf("evt %d ei %d erf %f ek %f dx %f dy %f\n", ctr, ei, erf, ek[ctr], dx, dy);
//current psf shape is 101:
if ((dx > -50) && (dx < 50))
{
if ((dy > -50) && (dy < 50))
{
idx1d = (int)((dx + 50.5)); // float dx from -0.5 to 0.5 should fell in the 50-th pixel
idx2d = (int)((dy + 50.5));
pval = (* eepsfvalfromptr(smatd, smat->dimensions, *(k + ctr), ei, idx1d, idx2d))*(1. - erf) + (* eepsfvalfromptr(smatd, smat->dimensions, *(k + ctr), ei + 1, idx1d, idx2d))*erf;
//naive interpolation block
//-------------------------------------------------------------------------------------------------------
inpixdx = dx - (idx1d - 50);
inpixdy = dy - (idx2d - 50);
if (inpixdx > 0.)
{
p2 = (* eepsfvalfromptr(smatd, smat->dimensions, *(k + ctr), ei, idx1d + 1, idx2d))*(1. - erf) + (* eepsfvalfromptr(smatd, smat->dimensions, *(k + ctr), ei + 1, idx1d + 1, idx2d))*erf;
if (inpixdy > 0.)
{
p3 = (* eepsfvalfromptr(smatd, smat->dimensions, *(k + ctr), ei, idx1d, idx2d + 1))*(1. - erf) + (* eepsfvalfromptr(smatd, smat->dimensions, *(k + ctr), ei + 1, idx1d, idx2d + 1))*erf;
}else{
inpixdy = -inpixdy;
p3 = (* eepsfvalfromptr(smatd, smat->dimensions, *(k + ctr), ei, idx1d, idx2d - 1))*(1. - erf) + (* eepsfvalfromptr(smatd, smat->dimensions, *(k + ctr), ei + 1, idx1d, idx2d - 1))*erf;
}
}else{
p2 = (* eepsfvalfromptr(smatd, smat->dimensions, *(k + ctr), ei, idx1d - 1, idx2d))*(1. - erf) + (* eepsfvalfromptr(smatd, smat->dimensions, *(k + ctr), ei + 1, idx1d - 1, idx2d))*erf;
inpixdx = -inpixdx;
if (inpixdy > 0.)
{
p3 = (* eepsfvalfromptr(smatd, smat->dimensions, *(k + ctr), ei, idx1d, idx2d + 1))*(1. - erf) + (* eepsfvalfromptr(smatd, smat->dimensions, *(k + ctr), ei + 1, idx1d, idx2d + 1))*erf;
}else{
inpixdy = -inpixdy;
p3 = (* eepsfvalfromptr(smatd, smat->dimensions, *(k + ctr), ei, idx1d, idx2d - 1))*(1. - erf) + (* eepsfvalfromptr(smatd, smat->dimensions, *(k + ctr), ei + 1, idx1d, idx2d - 1))*erf;
}
}
//printf("pval %f %f %f %f %f %d %d %d\n", pval, p2, p3, inpixdx, inpixdy, idx1d, idx2d, k[ctr]);
pval = (pval + inpixdx*(p2 - pval) + inpixdy*(p3 - pval))* (*(pkd + ctr));
// interpolation up to here
//-------------------------------------------------------------------------------------------------------
if (pval > 1e-10)
{
bw[msum] = pval;
msum += 1;
//printf("%f %d\n", pval, msum);
};
};
};
};
if (msum > 0)
{
rate = (double) *((double*) rates->data + rid);
lkl = 0.;
for (ctr=0; ctr < msum; ctr ++)
{
lkl = lkl + log(rate*bw[ctr] + 1.);
}
*(lklsd + rid) = lkl - eloc*rate;
}else{
*(lklsd + rid) = 0.;
};
};
//printf("loop done\n");
Py_END_ALLOW_THREADS;
free(bw);
PyObject *res = Py_BuildValue("O", lkls);
Py_DECREF(lkls);
return res;
}
static PyMethodDef PSFMethods[] = {
{"solve_for_locations", solve_for_locations, METH_VARARGS, "get coordinates within pixel based on its coordinates"},
{"solve_for_locations_eintp", solve_for_locations_eintp, METH_VARARGS, "compute likelihood using psf energy interpolation"},
{"put_psf_on_img", put_psf_on, METH_VARARGS, "put psf as is on img for all cooreindates "},
{"solve_for_rates", solve_for_rates, METH_VARARGS, "computed likelihood at specified position for a series of rates"},
{NULL, NULL, 0, NULL}
};