def movement_reader(ifile, bsmap): """ An iterator to read personal daily data. """ assert isinstance(bsmap, BaseStationMap) buf_ts = [] buf_lc = [] buf_cr = [] last_uid = None last_day_start = None def check_time_alignment(): if len(buf_ts) == 0: return False if buf_ts[-1] != buf_ts[0] + 86400: buf_ts.append(buf_ts[0] + 86400) buf_lc.append(buf_lc[0]) buf_cr.append(buf_cr[0]) return True for line in ifile: if isinstance(line, str): if line.startswith('#'): continue line = line.strip('\r\n ') uid, ts, loc = line.split(',')[0:3] elif isinstance(line, tuple): uid, ts, loc = line uid = int(uid) ts = int(float(ts)) loc = int(loc) day_start, day_end = drange(ts) coord = bsmap.get_coordinates(loc) if not in_area(coord, HZ_LB, HZ_RT): continue if last_uid is None or uid == last_uid and day_start == last_day_start: buf_ts.append(ts) buf_lc.append(loc) buf_cr.append(coord) else: if check_time_alignment(): yield PersonMoveDay(last_uid, last_day_start, buf_ts, buf_lc, buf_cr) buf_ts = [ts] buf_lc = [loc] buf_cr = [coord] last_uid = uid last_day_start = day_start if check_time_alignment(): yield PersonMoveDay(last_uid, last_day_start, buf_ts, buf_lc, buf_cr)
def movement_reader(fname, bsmap): """ An iterator to read personal daily data. """ assert isinstance(bsmap, BaseStationMap) buf_ts = [] buf_lc = [] buf_cr = [] last_uid = None last_day_start = None def check_time_alignment(): if buf_ts[-1] != buf_ts[0] + 86400: buf_ts.append(buf_ts[0] + 86400) buf_lc.append(buf_lc[0]) buf_cr.append(buf_cr[0]) for line in open(fname, 'rb'): line = line.strip('\r\n ') if line.startswith('#'): continue uid, ts, loc = line.split(',')[0:3] uid = int(uid) ts = int(float(ts)) loc = int(loc) day_start, day_end = drange(ts) coord = bsmap.get_coordinates(loc) if not in_area(coord, HZ_LB, HZ_RT): continue if last_uid is None or uid == last_uid and day_start == last_day_start: buf_ts.append(ts) buf_lc.append(loc) buf_cr.append(coord) else: check_time_alignment() yield PersonMoveDay(last_uid, last_day_start, buf_ts, buf_lc, buf_cr) buf_ts = [ts] buf_lc = [loc] buf_cr = [coord] last_uid = uid last_day_start = day_start check_time_alignment() yield PersonMoveDay(last_uid, last_day_start, buf_ts, buf_lc, buf_cr)
def statistics(file_names, models, use_duration=True, window=semi_markov.WINDOW): results = classify_all(file_names, models, use_duration=use_duration, window=window) counter = defaultdict(lambda: defaultdict(Counter)) min_threshold = 1.0 / len(models) * len(results) for threshold in drange(start=min_threshold, stop=1.0, step=0.01): for term in results: for path, result in results[term]: max_prob = max(result[m][0] for m in result) predicts = {m: result[m][0] >= max_prob for m in result} if max_prob >= threshold: assert sum(map(int, predicts.values())) <= 1.0 for m in predicts: counter[threshold][term][m] += predicts[m] else: counter[threshold][term]['unrecognized'] += 1 return counter
def __init__(self, lower_bound: float, higher_bound: float, formula: str, computation_method: str, cluster_count: int = 5): self.cluster_ids_count = 0 self._clients_to_chunks_map = {} self._chunks = [] self.finished = False self.cluster_count = cluster_count self.computation_method = computation_method self._result = 0.0 # Определить шаг и разбить предел на локальные пределы и создать Чанки для них step = (higher_bound - lower_bound) / cluster_count for i, lbound in enumerate(drange(lower_bound, higher_bound, step)): self._chunks.append(Chunk(i, lbound, lbound + step)) self.formula = formula # Проверить формулу valid = validate_formula_code(formula) if not valid: raise ValueError('Получена некорректная формула')
from ROOT import * from rooFitBuilder import * gROOT.SetBatch() gROOT.SetMacroPath(".:../plugins/") gROOT.LoadMacro("HistManager.cc+") sys.path.append("../zgamma") import utils as u #from apzFitProducer import AutoVivification import ConfigParser as cp cf = cp.ConfigParser() cf.read('config.cfg') #massList0 = ['125','145'] #massList = ['125.0','145.0'] massList0 = ['120','125','130','135','140','145','150'] massList = ['%.1f'%(a) for a in u.drange(120,150,1.0)] myFunc = 'CBGM' #Crystall-Ball + Gauss (with same Mean) lumi = u.getLumi() # rounding function for interpolation def roundTo5(x, base=5): return int(base * round(float(x)/base)) def SignalNameParamFixer(year,lepton,cat,sig,mass,ws, sameMean=True): # print 'DBG SignalNameParamFixer' fitName = '_'.join(['CBG',year,lepton,'cat'+cat,sig,mass,'Interp']) newFitName = '_'.join(['sig',sig,lepton,year,'cat'+cat]) mean = '_'.join(['meanCBG', year,lepton,'cat'+cat,sig,mass, 'Interp']) meanG = '_'.join(['meanGCBG', year,lepton,'cat'+cat,sig,mass, 'Interp'])
plt.ylabel("error", size=16) plt.ion() # set interective mode plt.show() # Loop through time and evolve the wave-fields using ADER time-stepping scheme of N+1 order of accuracy start = timeit.default_timer() for j in range(0, nx): rate.g(V0, (5) - y[j, :]) V[j, :] = V0 A = np.linalg.norm(V) for t in utils.drange(0.0, tend, dt): n = n + 1 # compute numerical solution time_integrator.RK4advection(v, v, nx, dx, order, y, t, dt, tau) for j in range(0, nx): rate.g(V0, (t + dt) - y[j, :]) V[j, :] = V0 T.append(t) EV.append(np.linalg.norm(V - v)) # Updating plots
plt.xlabel('time[t]') ax4.set_ylim([10**-5, 1]) plt.title('relative error in stress') plt.tight_layout() plt.ion() plt.show() A = (np.linalg.norm(1/np.sqrt(2.0*np.pi*delta**2)*0.5*Zs*(np.exp(-(y+cs*(t+1*0.5)-x0)**2/(2.0*delta**2))\ - np.exp(-(y-cs*(t+1*0.5)-x0)**2/(2.0*delta**2))))) B = (np.linalg.norm(u)) # Loop through time and evolve the wave-fields using ADER time-stepping scheme of N+1 order of accuracy time_integrator = 'ADER' for t in utils.drange (0.0, Tend+dt,dt): n = n+1 # ADER time-integrator if time_integrator in ('ADER'): ADER_Wave_dG_return = timeintegrate.ADER_Wave_dG(u,v,D,NP,num_element,dx,w,psi,t,r0,rn,dt,rho,mu) u = ADER_Wave_dG_return['Hu'] v = ADER_Wave_dG_return['Hv'] # Runge-Kutta time-integrator if time_integrator in ('RK'): RK4_Wave_dG_return = timeintegrate.RK4_Wave_dG(u,v,D,NP,num_element,dx,w,psi,t,r0,rn,dt,rho,mu) u = RK4_Wave_dG_return['Hu'] v = RK4_Wave_dG_return['Hv'] # Analytical sine wave (use it when sine function is choosen above)
def get_roc(self, strt_thr, end_thr, step, files_test_normal, files_test_anomalous, best_features): print('[ + ] Computing ROC curve using :') print('\t[-->] Threshold from 0 to', end_thr) print('\t[-->] Step :', step) print('\t[-->] ROC curve has', int(end_thr / step), 'points') point_count = 1 # Init empty arrays for points coordinates a_vp = [] a_vn = [] a_fp = [] a_fn = [] # For every threshold... for thr in drange(strt_thr, end_thr, step): tot_vn = 0 tot_vp = 0 tot_fn = 0 tot_fp = 0 n_sample_norm = 0 n_sample_anom = 0 # Compute predictions for normal test set for file in files_test_normal: # Load data data = pd.read_csv(file) data = keep_best_features(data, best_features) # Prepare data for seq2seq processing data = get_seq2seq_batch(data, self.seq_length, self.batch_size) # Predict file with current threshold fp, vn = self.predict(data, thr) # Update true/false positives count n_sample_norm += data.shape[1] tot_vn += vn tot_fp += fp # Compute predictions for anomalous test set for file in files_test_anomalous: # Load data data = pd.read_csv(file) data = keep_best_features(data, best_features) # Prepare data for seq2seq processing data = get_seq2seq_batch(data, self.seq_length, self.batch_size) # Predict file with current threshold vp, fn = self.predict(data, thr) # Update n_sample_anom += data.shape[1] tot_vp += vp tot_fn += fn vp_rate = tot_vp / n_sample_anom # Append for ROC curve vn_rate = tot_vn / n_sample_norm fp_rate = tot_fp / n_sample_norm # Append for ROC curve fn_rate = tot_fn / n_sample_norm print( '\t[ + ] Point {}/{} \tTrue Positive Rate : {:.5f} \tFalse Positive Rate : {:.5f}' .format(point_count, int(end_thr / step), vp_rate, fp_rate)) point_count += 1 a_vp.append(vp_rate) a_vn.append(vn_rate) a_fp.append(fp_rate) a_fn.append(fn_rate) return a_fp, a_vp
def get_roc(self, strt_thr, end_thr, step, files_test_normal, files_test_anomalous, best_features): print('[ + ] Computing ROC curve using :') print('\t[-->] Threshold from 0 to', end_thr) print('\t[-->] Step :', step) print('\t[-->] ROC curve has', int(end_thr / step), 'points') point_count = 1 # Init empty arrays for points coordinates a_vp = [] a_vn = [] a_fp = [] a_fn = [] # For every threshold... for thr in drange(strt_thr, end_thr, step): tot_vn = 0 tot_vp = 0 tot_fn = 0 tot_fp = 0 n_sample_norm = 0 n_sample_anom = 0 # Compute predictions for normal test set for file in files_test_normal: # Read file data = read_csv(file, index_col=False) if best_features is not None: data = data[best_features].values else: data = data.values[:, 1:] # Predict file with current threshold fp, vn = self.predict(data, thr) # Update true/false positives count n_sample_norm += data.shape[0] tot_vn += vn tot_fp += fp # Compute predictions for anomalous test set for file in files_test_anomalous: # Read file data = read_csv(file, index_col=False) if best_features is not None: data = data[best_features].values else: data = data.values[:, 1:] # Predict file with current threshold vp, fn = self.predict(data, thr) # Update n_sample_anom += data.shape[0] tot_vp += vp tot_fn += fn vp_rate = tot_vp / n_sample_anom # Append for ROC curve vn_rate = tot_vn / n_sample_norm fp_rate = tot_fp / n_sample_norm # Append for ROC curve fn_rate = tot_fn / n_sample_norm print( '\t[ + ] Point {}/{} \tTrue Positive Rate : {:.2f} \tFalse Positive Rate : {:.2f}' .format(point_count, int(end_thr / step), vp_rate, fp_rate)) point_count += 1 a_vp.append(vp_rate) a_vn.append(vn_rate) a_fp.append(fp_rate) a_fn.append(fn_rate) return a_fp, a_vp
W = 0.4367 Vd = [0] Sd = [0] # Time stepping parameters cfl = 0.5 # CFL number dt = (cfl / (max(max(c1s), max(c2s)) * (2 * N + 1))) * min(min(dx_1), min(dx_2)) # time-step (s) t = 0.0 # initial time Tend = 2.5 # final time (s) n = 0 T = [t] for t in utils.drange(dt, Tend + dt, dt): n = n + 1 ADER_Wave_1D_GL_return = timeintegrate.ADER_Wave_1D_GL( u_1, v_1, u_2, v_2, S, W, D, NP, num_element1, num_element2, dx_1, dx_2, w, psi, t, r0, rn, dt, rho1, mu1, rho2, mu2, alpha, Tau_0, fric_law) u_1 = ADER_Wave_1D_GL_return['Hu_1'] v_1 = ADER_Wave_1D_GL_return['Hv_1'] u_2 = ADER_Wave_1D_GL_return['Hu_2'] v_2 = ADER_Wave_1D_GL_return['Hv_2'] S = ADER_Wave_1D_GL_return['H_d'] W = ADER_Wave_1D_GL_return['H_psi'] Vd.append(np.abs(u_1[-1] - u_2[0])) Sd.append(v_2[0] + 0.0 * Tau_0) T.append(t)