int rlIniFile::read(const char *filename) { FILE *fp; char line[rl_PRINTF_LENGTH], name_section[rl_PRINTF_LENGTH], name_name[rl_PRINTF_LENGTH], name_param[rl_PRINTF_LENGTH], *cptr; rlSection *s, *s_old; // delete old content s = _firstSection; while(s != NULL) { deleteSectionNames(s); s_old = s; s = s->nextSection; delete s_old; } // read the file fname.setText(filename); _firstSection = new rlSection; // first section holds names with section name = null_string _firstSection->nextSection = NULL; _firstSection->firstName = NULL; _firstSection->name = new char[1]; _firstSection->name[0] = '\0'; fp = fopen(filename,"r"); if(fp == NULL) return -1; name_section[0] = name_name[0] = name_param[0] = '\0'; while(fgets(line,sizeof(line)-1,fp) != NULL) { cptr = strchr(line,0x0d); if(cptr != NULL) *cptr = '\0'; cptr = strchr(line,0x0a); if(cptr != NULL) *cptr = '\0'; if(line[0] == '[') // section identifier { copyIdentifier(name_section,line); setText(name_section, NULL, NULL); } else if(line[0] > ' ' && line[0] != '\t' && line[0] != '#') // name identifier { copyName(name_name,line); copyParam(name_param,line); setText(name_section, name_name, name_param); } else // it must be a comment line { setText(name_section, line, NULL); } } fclose(fp); return 0; }
Param& Param::operator=(const Param &other) { this->m_childNodes.clear(); // Loop through other and recreate the param tree (Depth first) std::function<void(Param&, const Param&)> copyParam = [&](Param &parent, const Param &oParam) { for(const auto &child : oParam.m_childNodes) { Param& newNode = parent.push(child.second.get()->getName()); copyParam(newNode, *child.second.get()); } parent.m_data = oParam.asStdString(); parent.m_name = oParam.getName(); }; copyParam(*this, other); return *this; }
Param::Param(const Param &other) : m_childNodes() , m_parentNode(nullptr) , m_name(other.getName()) , m_data(other.asStdString("")) { // Loop through other and recreate the param tree (Depth first) std::function<void(Param&, const Param&)> copyParam = [&](Param &parent, const Param &oParam) { for(const auto &child : oParam.m_childNodes) { Param& newNode = parent.push(child.second.get()->getName()); copyParam(newNode, *child.second.get()); } parent.m_data = oParam.asStdString(); parent.m_name = oParam.getName(); }; copyParam(*this, other); }
/* Accept the current trial params by moving them from * psr[0] to psr[1]. */ void reset_params(pulsar *p) { copyPSR(p, 1, 0); for (unsigned ip=0; ip<MAX_PARAMS; ip++) copyParam(p[1].param[ip], &(p[0].param[ip])); }
/* Accept the current trial params by moving them from * psr[0] to psr[1]. */ void store_params(pulsar *p) { copyPSR(p, 0, 1); for (unsigned ip=0; ip<MAX_PARAMS; ip++) copyParam(p[0].param[ip], &(p[1].param[ip])); }
void ConfigParam::copyConfigParam(Param param) { copyParam(param); }
int bootstrap(pulsar *psr,int p,int npsr) { longdouble param[MAX_PARAMS],err[MAX_PARAMS],psq[MAX_PARAMS],xmean[MAX_PARAMS]; longdouble result[MAX_PARAMS][MAX_ITER]; longdouble fac,x1,x2,xmid,sum,sumwt,wgt,x,dt,mean,meansq,sdev; int nFit=0,nFit2,npts,okay; int i,j,k,ii,nboot,iter,l; int il1,il2,ih1,ih2; double globalParam; long idum = -999; /* Should be set be clock, or user */ const char *CVS_verNum = "$Id: 6b34ebfda6648825ae3d3f51fb5041e0763c9ec1 $"; if (displayCVSversion == 1) CVSdisplayVersion("bootstrap.C","bootstrap()",CVS_verNum); printf("Bootstrap1 = %d\n",psr[0].bootStrap); copyPSR(psr,p,npsr); /* Have a copy of the pulsar */ for (i=0;i<MAX_PARAMS;i++) copyParam(psr[0].param[i],&(psr[npsr].param[i])); printf("Bootstrap = %d %d\n",psr[0].bootStrap,psr[1].bootStrap); nboot = (int)pow(2,psr[p].bootStrap); for (i=0;i<psr[p].nobs;i++) psr[p].obsn[i].residual = psr[p].obsn[i].prefitResidual; /* Store the current post-fit parameters and their errors */ for (i=0;i<MAX_PARAMS;i++) { for (k=0;k<psr[p].param[i].aSize;k++) { if (psr[p].param[i].fitFlag[k] == 1) { param[nFit] = psr[p].param[i].val[k]; /* - psr[p].param[i].prefit[k]; */ ld_printf("Initial param = %s %Lf %Lf\n",psr[p].param[i].label[0], psr[p].param[i].val[k], psr[p].param[i].prefit[k]); err[nFit] = psr[p].param[i].err[k]; psq[nFit] = 0.0; nFit++; psr[p].param[i].val[k] = psr[p].param[i].prefit[k]; } } } /* Determine number of TOAs */ npts=0; okay=0; for (i=0;i<psr[p].nobs;i++) { if (psr[p].obsn[i].deleted==0) okay=1; /* Check for START and FINISH flags */ if (psr[p].param[param_start].paramSet[0]==1 && psr[p].param[param_start].fitFlag[0]==1 && psr[p].param[param_start].val[0] > psr[p].obsn[i].bat) okay=0; if (psr[p].param[param_finish].paramSet[0]==1 && psr[p].param[param_finish].fitFlag[0]==1 && psr[p].param[param_finish].val[0] < psr[p].obsn[i].bat) okay=0; if (okay==1) npts++; } /* Do the bootstrap monte-carlo */ fac = sqrt((double)npts); x1 = 0.342*nboot; x2 = 0.477*nboot; xmid = 0.5*(nboot+1); il1 = (int)((xmid-x1)+0.5); il2 = (int)((xmid-x2)+0.5); ih1 = (int)((xmid+x1)+0.5); ih2 = (int)((xmid+x2)+0.5); for (iter=0;iter<nboot;iter++) { sum = 0.0; sumwt = 0.0; for (j=0;j<nFit;j++) xmean[j] = 0.0; for (i=0;i<npts;i++) { if (psr[npsr].fitMode==1) wgt = 1.0 / (1.0e-6*psr[npsr].obsn[i].toaErr*psr[npsr].param[param_f].val[0]* 1.0e-6*psr[npsr].obsn[i].toaErr*psr[npsr].param[param_f].val[0]); else wgt=1.0/(1.0e-6*psr[npsr].param[param_f].val[0]*1.0e-6*psr[npsr].param[param_f].val[0]); dt = psr[npsr].obsn[i].residual; ii = (int)(npts*random(&idum)); /* Randomise the data index */ for (j=0;j<nFit;j++) xmean[j]+=wgt; /* *fctn[j]; --- NEEDS TO BE IN -- WHAT IS THIS FOR ANYWAY?? */ sum+=wgt*dt; sumwt+=wgt; /* */ /* Randomly change around the observation order */ /* */ psr[p].obsn[i].prefitResidual = psr[npsr].obsn[ii].prefitResidual; psr[p].obsn[i].residual = psr[npsr].obsn[ii].residual; psr[p].obsn[i].sat = psr[npsr].obsn[ii].sat; psr[p].obsn[i].bat = psr[npsr].obsn[ii].bat; psr[p].obsn[i].deleted = psr[npsr].obsn[ii].deleted; psr[p].obsn[i].freq = psr[npsr].obsn[ii].freq; psr[p].obsn[i].freqSSB = psr[npsr].obsn[ii].freqSSB; psr[p].obsn[i].toaErr = psr[npsr].obsn[ii].toaErr; strcpy(psr[p].obsn[i].fname,psr[npsr].obsn[ii].fname); strcpy(psr[p].obsn[i].telID,psr[npsr].obsn[ii].telID); for (l=0;l<3;l++) { psr[p].obsn[i].earth_ssb[l] = psr[npsr].obsn[ii].earth_ssb[l]; psr[p].obsn[i].observatory_earth[l] = psr[npsr].obsn[ii].observatory_earth[l]; } } writeTim("testout.tim",psr,"fred"); psr[p].bootStrap = 0; doFit(&psr[p],1,0); /* textOutput(psr,npsr,globalParam,0,0,0,""); */ /* Output results to the screen */ j=0; for (i=0;i<MAX_PARAMS;i++) { for (k=0;k<psr[p].param[i].aSize;k++) { if (psr[p].param[i].fitFlag[k] == 1) { /* x = fac*((psr[p].param[i].val[k] - psr[p].param[i].prefit[k])-param[j])/err[j]; */ /* WHY IS FACTOR USED HERE? */ x = ((psr[p].param[i].val[k] - psr[p].param[i].prefit[k])-param[j]); result[j][iter] = psr[p].param[i].val[k]-param[j]; psq[j]+=x*x; j++; } } } /* Store the current post-fit parameters and their errors */ for (i=0;i<psr[p].nobs;i++) psr[p].obsn[i].residual = psr[npsr].obsn[i].prefitResidual; for (i=0;i<MAX_PARAMS;i++) { for (k=0;k<psr[p].param[i].aSize;k++) { if (psr[p].param[i].fitFlag[k] == 1) { psr[p].param[i].prefit[k] = psr[npsr].param[i].prefit[k]; psr[p].param[i].val[k] = psr[npsr].param[i].prefit[k]; } } } printf("Finished iteration %d of %d, so %g percent done.\n", (int)(iter+1.5),(int)(nboot+0.5),(double)((double)(iter+1.5)*100/nboot)); } /* Restore the post-fit parameters, but use the Monte-carlo error estimates */ nFit2=0; for (i=0;i<MAX_PARAMS;i++) { for (k=0;k<psr[p].param[i].aSize;k++) { if (psr[p].param[i].fitFlag[k] == 1) { mean=longdouble(0.0); meansq=longdouble(0.0); for (l=0;l<nboot;l++) { mean += (result[nFit2][l])/nboot; meansq+= (result[nFit2][l]*result[nFit2][l])/nboot; ld_printf("bootstrap parameters [%s] = %.14Lg\n",psr[p].param[i].shortlabel[k], result[nFit2][l]+param[nFit2]); } /* mean/=(longdouble)nboot; meansq/=(longdouble)nboot; */ sdev = (longdouble)sqrt(meansq-mean*mean); ld_printf("Bootstrap mean difference: %Lf mean squared: %.14Lf rms: %.14Lf; sigma: %.14Lf, mean value: %.14Lf\n", mean,(mean*mean),meansq,sdev,(mean+param[nFit2])); /* psr[p].param[i].val[k] = psr[npsr].param[i].val[k]; */ psr[p].param[i].val[k] = mean+param[nFit2]; psr[p].param[i].err[k] = sdev; /* psr[p].param[i].err[k] = err[nFit2]*sqrt(psq[nFit2]/nboot); */ /* psr[p].param[i].err[k] = sqrt(psq[nFit2]/(nboot-1)); */ /* sort(nboot,a[1][j]); fl1[nFit2] = a[il1][j]; fl2[nFit2] = a[il2][j]; fh1[nFit2] = a[ih1][j]; fh2[nFit2] = a[ih2][j]; */ nFit2++; } } } return 0; }