def analyze(self, methods=['GRs']): self.log.info('Analyzing %d files.' % self.background.nsets) self.log.debug('Producing search template.') search = { m: templates.Signal(self.detector, self.psr, m, 0, self.t) for m in methods } self.log.debug('Setting up results') # NEEDS CORRECTION! self.results = Results(self.detector, self.psr, methods=methods, hinj=self.hinj, kind=self.injkind, pdif=self.pdif) self.log.debug('Looping over files') for n in range(self.background.nsets): self.log.debug('File %i.' % n) try: back_file = pd.HDFStore(self.background.path + str(n), 'r') data = back_file[self.psr] finally: back_file.close() self.log.debug('Looping over instantiations.') for inst in data.columns: inst_number = int(n * self.background.filesize + inst) self.log.info('%i/%i ' % (inst_number, len(self.hinj) - 1)) self.log.debug('Selecting psi, iota, phi0.') psi = random.uniform(self.pol_range[0], self.pol_range[1]) iota = random.uniform(self.inc_range[0], self.inc_range[1]) psi_inj = random.uniform(self.pol_range[0], self.pol_range[1]) iota_inj = random.uniform(self.inc_range[0], self.inc_range[1]) phi0 = random.uniform(self.phi0_range[0], self.phi0_range[1]) self.log.debug('Search with POL: %f, INC: %f' % (psi, iota)) self.log.debug('Loop over search methods.') # note: important that this follows inst loop to get same psi and iota for m in methods: d = data[inst] # inject if necessary h = self.hinj[inst_number] self.log.debug('I! %(psi_inj)f %(iota_inj)f %(phi0)f' % locals()) d += h * self.injection.simulate( psi_inj, iota_inj, phase=phi0) self.log.debug('Get design matrix.') designMatrix = search[m].design_matrix(psi, iota) A = designMatrix.div(self.sg, axis=0) b = d / self.sg self.log.debug('SVD decomposition.') svd = np.linalg.svd(A, full_matrices=False) U = pd.DataFrame(svd[0], columns=A.columns, index=A.index) W = pd.DataFrame(np.diag(1. / svd[1]), index=A.columns, columns=A.columns) V = pd.DataFrame(svd[2], index=A.columns, columns=A.columns) cov = V.T.dot(W**2).dot(V) # covariance matrix VtW = V.T.dot(W) # need to make U complex before dotting with b Utb = (U + 0j).mul(b, axis=0).sum(axis=0) a = VtW.dot(Utb.T) # results # NEEDS MODIFYICATION FROM HERE ON: self.log.debug('Average h0') self.results.h[m][inst_number] = (abs(a).sum()) / len(a) self.log.debug('Significance') self.results.s[m][inst_number] = abs( np.dot(a.conj(), np.linalg.solve(cov, a))) ## Save self.results.save()
def __init__(self, nh0, nhs, detector='H1', psr='J0534+2200', pdif='p', pdif_s='p', frange=[1.0e-7, 1.0e-5], hinjrange=[1.0E-27, 1.0E-23], range=[], filesize=100): # system info self.detector = detector self.psr = psr self.log = logging.getLogger('Frequentist') self.log.debug('Initializing frequentist ST sensitivity analysis.') # data info self.freq = np.linspace(frange[0], frange[1], nh0) self.background = Background(detector, psr, self.freq, filesize) self.background.get() self.log.debug('Obtaining time.') self.t = self.background.seed.finehet.index self.log.debug('Obtaining sigma.') sigma = Sigma(self.detector, self.psr, self.background.seed.finehet) self.sg = sigma.std self.log.debug('Preparing injections.') inj = np.linspace(hinjrange[0], hinjrange[1], ninj) self.pdif = pdif self.pdif_s = pdif_s self.injkind = 'GRs' self.injection = templates.Signal( detector, psr, self.t, pdif=pdif, pdif_s=pdif_s, kind=injkind, ) self.log.debug('Preparing parameter ranges.') src = self.injection.response.src if 'psi' in rangeparam or rangeparam == 'all': self.pol_range = [ src.param['POL'] - src.param['POL error'], src.param['POL'] + src.param['POL error'] ] else: self.pol_range = [src.param['POL'], src.param['POL']] if 'iota' in rangeparam or rangeparam == 'all': self.inc_range = [ src.param['INC'] - src.param['INC error'], src.param['INC'] + src.param['INC error'] ] else: self.inc_range = [src.param['INC'], src.param['INC']] if 'phi0' in rangeparam or rangeparam == 'all': self.phi0_range = [0., np.pi / 2] else: self.phi0_range = [0., 0.]
def __init__(self, detector, psr, nfreq, injkind, pdif, ninj, rangeparam=[], frange=[1.0e-7, 1.0e-5], hinjrange=[1.0E-27, 1.0E-23], filesize=100): # system info self.detector = detector self.psr = psr self.log = logging.getLogger('Frequentist') self.log.debug('Initializing frequentist sensitivity analysis.') # data info self.freq = np.linspace(frange[0], frange[1], nfreq) self.background = Background(detector, psr, self.freq, filesize) self.background.get() self.log.debug('Obtaining time.') self.t = self.background.seed.finehet.index self.log.debug('Obtaining sigma.') sigma = Sigma(self.detector, self.psr, self.background.seed.finehet) self.sg = sigma.std self.log.debug('Preparing injection strengths.') inj = np.linspace(hinjrange[0], hinjrange[1], ninj) injLocations = [ int(x) for x in np.linspace(0, nfreq, ninj, endpoint=False) ] self.hinj = np.zeros(nfreq) self.hinj[injLocations] = inj self.pdif = pdif self.injkind = injkind self.injection = templates.Signal(detector, psr, injkind, pdif, self.t) src = self.injection.response.src self.log.debug('Preparing parameter ranges.') if 'psi' in rangeparam or rangeparam == 'all': self.pol_range = [ src.param['POL'] - src.param['POL error'], src.param['POL'] + src.param['POL error'] ] else: self.pol_range = [src.param['POL'], src.param['POL']] if 'iota' in rangeparam or rangeparam == 'all': self.inc_range = [ src.param['INC'] - src.param['INC error'], src.param['INC'] + src.param['INC error'] ] else: self.inc_range = [src.param['INC'], src.param['INC']] if 'phi0' in rangeparam or rangeparam == 'all': self.phi0_range = [0., np.pi / 2] else: self.phi0_range = [0., 0.]
def analyze(self, methods): print 'Analyzing %d files.' % self.background.nsets # search info search = { m: templates.Signal(self.detector, self.psr, m, 0, self.t) for m in methods } # results self.results = Results(self.detector, self.psr, methods=methods, hinj=self.hinj, kind=self.injkind, pdif=self.pdif) # loop over files for n in range(self.background.nsets): try: back_file = pd.HDFStore(self.background.path + str(n), 'r') data = back_file[self.psr] finally: back_file.close() # loop over instantiations for inst in data.columns: inst_number = int(n * self.background.filesize + inst) print '%i/%i ' % (inst_number, len(self.hinj) - 1), # select psi, iota and phi0 psi = random.uniform(self.pol_range[0], self.pol_range[1]) iota = random.uniform(self.inc_range[0], self.inc_range[1]) psi_inj = random.uniform(self.pol_range[0], self.pol_range[1]) iota_inj = random.uniform(self.inc_range[0], self.inc_range[1]) phi0 = random.uniform(self.phi0_range[0], self.phi0_range[1]) print psi, iota, phi0 # loop over search methods # note: important that this follows inst loop to get same psi and iota for m in methods: d = data[inst] # inject if necessary h = self.hinj[inst_number] if h != 0: print self.injection.kind + str(self.injection.pdif), print 'I! %(psi_inj)f %(iota_inj)f %(phi0)f' % locals() d += h * self.injection.simulate( psi_inj, iota_inj, phase=phi0) # get design matrix designMatrix = search[m].design_matrix(psi, iota) A = designMatrix.div(self.sg, axis=0) b = d / self.sg # SVD DECOMPOSITION svd = np.linalg.svd(A, full_matrices=False) U = pd.DataFrame(svd[0], columns=A.columns, index=A.index) W = pd.DataFrame(np.diag(1. / svd[1]), index=A.columns, columns=A.columns) V = pd.DataFrame(svd[2], index=A.columns, columns=A.columns) cov = V.T.dot(W**2).dot(V) # covariance matrix VtW = V.T.dot(W) # need to make U complex before dotting with b Utb = (U + 0j).mul(b, axis=0).sum(axis=0) a = VtW.dot(Utb.T) # results # average h0 self.results.h[m][inst_number] = (abs(a).sum()) / len(a) # significance self.results.s[m][inst_number] = abs( np.dot(a.conj(), np.linalg.solve(cov, a))) ## Save self.results.save()
def __init__(self, detector, psr, nfreq, injkind, pdif, ninj, rangeparam=[], frange=[1.0e-7, 1.0e-5], hinjrange=[1.0E-27, 1.0E-23], filesize=100): # system info self.detector = detector self.psr = psr # data info self.freq = np.linspace(frange[0], frange[1], nfreq) print 'Getting background.' self.background = Background(detector, psr, self.freq, filesize) self.background.get() self.t = self.background.seed.finehet.index sigma = Sigma(self.detector, self.psr, self.background.seed.finehet) self.sg = 2 * sigma.std # injection info inj = np.linspace(hinjrange[0], hinjrange[1], ninj) injLocations = [ int(x) for x in np.linspace(0, nfreq, ninj, endpoint=False) ] self.hinj = np.zeros(nfreq) self.hinj[injLocations] = inj self.pdif = pdif self.injkind = injkind self.injection = templates.Signal(detector, psr, injkind, pdif, self.t) src = self.injection.response.src # range info if 'psi' in rangeparam or rangeparam == 'all': self.pol_range = [ src.param['POL'] - src.param['POL error'], src.param['POL'] + src.param['POL error'] ] else: self.pol_range = [src.param['POL'], src.param['POL']] if 'iota' in rangeparam or rangeparam == 'all': self.inc_range = [ src.param['INC'] - src.param['INC error'], src.param['INC'] + src.param['INC error'] ] else: self.inc_range = [src.param['INC'], src.param['INC']] if 'phi0' in rangeparam or rangeparam == 'all': self.phi0_range = [0., np.pi / 2] else: self.phi0_range = [0., 0.]
days = 1 t = np.array([x + 630720013 for x in range(0, int(days * sd.ss), 60)]) det = 'H1' kind = 'GR' pdifs = ['0', 'p', 'm'] phi0 = 5. plt.figure() for p in pdifs: sig = templates.Signal('H1', 'J0534+2200', 'GR', p, t) s = sig.simulate(sig.response.src.param['POL'], sig.response.src.param['INC'], phase=phi0) s.real.plot(label=p) plt.title('Crab LHO GR signal (Re) $\phi_0=$' + str(phi0)) plt.legend(numpoints=1, loc=4) plt.figure() for p in pdifs: sig = templates.Signal('H1', 'J0534+2200', 'GR', p, t)