[docs]defSNR_fix(aid):ifaid.hint("trust_SNR"):returnwithaid.log_heading("SNR Fix Test"):W=aid.fitter.WSNR_max=aid.hint("SNR_max")ifSNR_maxisnotNone:Wclip=np.minimum(W,SNR_max)ifnp.any(Wclip<W):W=Wclipaid.fitter.W=Wclipaid.invalidate_fitters()aid.log_warn(3,""" Applying 'SNR_max'={} to clip the weights """.format(SNR_max),)# TODO, this validate=False is for relative_degree checks# since this is so early. Need a better mehtodaid.fitter_update(representative=False,validate=False,)effective_data_points=(np.sum(W**2))**2/np.sum(W**4)ratio=effective_data_points/len(W)Wmax=max(W)pref_ratio=aid.hint("SNR_regularize_ratio")pref_scale=aid.hint("SNR_regularize_scale")did_warn=Falseifratio<1-pref_scale/Wmax:aid.log_warn(3,""" The number of effective data points N=(ΣW^2)^2/(ΣW^4)={:.2e}*len(W) [where W=SNR] is below the configured 'SNR_regularize_scale'={}, given the maximum SNR={}. Now Finding an SNR ceiling that balances the ratio with max SNR. """.format(ratio,pref_scale,Wmax),)did_warn=Trueelifratio<pref_ratio:aid.log_warn(3,""" The number of effective data points N=(ΣW^2)^2/(ΣW^4)={:.2e}*len(W) [where W=SNR] is below the configured 'SNR_regularize_ratio'={}. Now Finding an SNR ceiling to adjust to that ratio. """.format(ratio,pref_ratio),)did_warn=Trueifdid_warn:aid.log_rationale(3,""" Disable this adjustment by using a smaller value of 'SNR_regularize_ratio', setting 'fix_SNR'=False, or setting 'trust_SNR'=True. """.format(pref_ratio),)else:returnW=np.sort(aid.fitter.W)idx_min=0idx_max=len(W)# binary searchwhileidx_min+1<idx_max:idx_mid=(idx_max-idx_min)//2+idx_minW_ceiling=W[idx_mid]ifW_ceiling<=0:idx_min=idx_midcontinueW_new=np.minimum(W_ceiling,W)effective_data_points=(np.sum(W_new**2))**2/np.sum(W_new**4)ratio=effective_data_points/len(W)pref_scale_ratio=1-pref_scale/W_ceilingifratio<pref_ratioorratio<pref_scale_ratio:idx_max=idx_midelse:idx_min=idx_midaid.log_debug(9,"W:ratio",pref_ratio,pref_scale_ratio,ratio,idx_min,idx_max,W_ceiling,)W_ceiling=W[idx_min]aid.log_warn(3,""" Using SNR<{} ceiling. """.format(W_ceiling),)aid.fitter.W=np.minimum(aid.fitter.W,W_ceiling)aid.invalidate_fitters()# TODO, this validate=False is for relative_degree checks# since this is so early. Need a better mehtodaid.fitter_update(representative=False,validate=False,)