BICs = [] for degree in degreeRange: realModel = cosineFit(x, np.real(data), realErrors, degree) imagModel = cosineFit(x, np.imag(data), imagErrors, degree) BICs.append(BIC(data, realModel, imagModel, realErrors, imagErrors, DoF=2*degree+2)) bestDegree = (degreeRange[BICs==np.min(BICs)][0]) bestModel = cosineFit(x, np.real(data), realErrors, bestDegree) + 1.0j * cosineFit(x, np.imag(data), imagErrors, bestDegree) fullModel = cosineFit(x, np.real(data), realErrors, bestDegree, fullx=fullx) + 1.0j * cosineFit(x, np.imag(data), imagErrors, bestDegree, fullx=fullx) bestChiSqPerDoF = (np.sum(((np.real(data) - np.real(bestModel))/realErrors)**2) + np.sum(((np.imag(data) - np.imag(bestModel))/imagErrors)**2))/(len(data)*2) return bestModel, fullModel, BICs, bestDegree, bestChiSqPerDoF degreeRange = np.arange(60,120,2) if verbose: print 'Now exploring between degree = ' + str(degreeRange[0]) + ' and degree = ' + str(degreeRange[-1]) + ' for the minimum BIC...' observedRealErrors = np.std(np.real(np.asarray(bootstrapBetas).T),axis=1) observedImagErrors = np.std(np.imag(np.asarray(bootstrapBetas).T),axis=1) modelRealErrors = np.abs(betas)*((np.diag(np.linalg.pinv(uCal.AtNinvA))[0:2*uCal.nChans:2]))**.5 modelImagErrors = np.abs(betas)*((np.diag(np.linalg.pinv(uCal.AtNinvA))[1:2*uCal.nChans:2]))**.5 model, fullModel, BICs, bestDegree, bestChiSqPerDoF = findBestModel(freqs[uCal.chans] - .1, betas*freqs[uCal.chans]**spectralIndex, modelRealErrors*freqs[uCal.chans]**spectralIndex, modelImagErrors*freqs[uCal.chans]**spectralIndex, degreeRange, freqs-.1) if verbose: print 'The fourier mode limit with the lowest BIC is ' + str(bestDegree) + ' with a fit chi^2 per DoF of ' + str(bestChiSqPerDoF) #%%########################################## # Save Results ############################################# uc.save2npz(dataFiles[0].replace('.npz', '.uCalResults.npz'), dataFiles, chans, uCal.chans, betas*freqs[uCal.chans]**spectralIndex, fullModel) diagnosticResults = {var: eval(var) for var in ['dataFiles', 'uCal', 'betas', 'Sigmas', 'Ds', 'bootstrapBetas', 'bootstrapSigmas', 'bootstrapDs', 'freqs', 'observedRealErrors', 'observedImagErrors', 'model', 'noiseCovDiag', 'BICs', 'degreeRange']} pickle.dump(diagnosticResults, open(dataFiles[0].replace('.npz', '.diagnosticResults.p'),'wb')) if verbose: print '\nuCal complete! Results saved to ' + dataFiles[0].replace('.npz', '.diagnosticResults.p') + ' and ' + dataFiles[0].replace('.npz', '.uCalResults.p')
def setUp(self): uc.save2npz('uCal_unittesting_savetest', 1, 2, 3, 4, 5, 6, 7, 8)
modelImagErrors = np.abs(betas) * ( (np.diag(np.linalg.pinv(uCal.AtNinvA))[1:2 * uCal.nChans:2]))**.5 model, fullModel, BICs, bestDegree, bestChiSqPerDoF = findBestModel( freqs[uCal.chans] - .1, betas * freqs[uCal.chans]**spectralIndex, modelRealErrors * freqs[uCal.chans]**spectralIndex, modelImagErrors * freqs[uCal.chans]**spectralIndex, degreeRange, freqs - .1) if verbose: print 'The fourier mode limit with the lowest BIC is ' + str( bestDegree) + ' with a fit chi^2 per DoF of ' + str(bestChiSqPerDoF) #%%########################################## # Save Results ############################################# uc.save2npz(dataFiles[0].replace('.npz', '.uCalResults.npz'), dataFiles, chans, uCal.chans, betas * freqs[uCal.chans]**spectralIndex, fullModel) diagnosticResults = { var: eval(var) for var in [ 'dataFiles', 'uCal', 'betas', 'Sigmas', 'Ds', 'bootstrapBetas', 'bootstrapSigmas', 'bootstrapDs', 'freqs', 'observedRealErrors', 'observedImagErrors', 'model', 'noiseCovDiag', 'BICs', 'degreeRange' ] } pickle.dump(diagnosticResults, open(dataFiles[0].replace('.npz', '.diagnosticResults.p'), 'wb')) if verbose: print '\nuCal complete! Results saved to ' + dataFiles[0].replace( '.npz', '.diagnosticResults.p') + ' and ' + dataFiles[0].replace( '.npz', '.uCalResults.p')
def setUp(self): uc.save2npz("uCal_unittesting_savetest", 1, 2, 3, 4, 5, 6, 7, 8)