m_log_weights_np = D["log_weights"] v_A = np.zeros(len(tmp)) for d_timestep in range(2*6,len(tmp)): d_row = int(tmp[d_timestep,0]*0.01/LAT_RESO) d_col = int((tmp[d_timestep,1]-300)*0.01/LON_RESO) d_ll_t = np.mean(m_log_weights_np[d_timestep,:,:]) d_qualtile = (d_ll_t - m_map_ll_mean[d_row,d_col])/m_map_ll_std[d_row,d_col] if (not np.isfinite(d_qualtile)): v_A[d_timestep] = 2 elif (d_qualtile <= -1.282): v_A[d_timestep] = 1 v_A = v_A[12:] v_anomalies = np.zeros(len(v_A)) for d_i_4h in range(0,len(v_A)+1-24): v_A_4h = v_A[d_i_4h:d_i_4h+24] v_anomalies_i = contrario_utils.contrario_detection(v_A_4h,CONTRARIO_EPS) v_anomalies[d_i_4h:d_i_4h+24][v_anomalies_i==1] = 1 ## Plotting tmp = tmp[12:] v_lat = (tmp[:,0]/300.0)*LAT_RANGE + LAT_MIN v_lon = (tmp[:,1]-300.0)/300.0*LON_RANGE + LON_MIN plt.plot(v_lon,v_lat,color='g',linewidth=0.8) if len(contrario_utils.nonzero_segments(v_anomalies)) > 0: D["anomaly_idx"] = v_anomalies l_dict_anomaly.append(D) # for l_segment in nonzero_segments(v_anomalies): # plt.plot(v_lon[l_segment],v_lat[l_segment],color='r',linewidth=0.8) ## #plt.close()
d_logprob_t = np.mean(m_log_weights_np[d_timestep, :]) # KDE l_local_log_prod = Map_logprob[str(d_row) + "," + str(d_col)] if len(l_local_log_prod) < 2: v_A[d_timestep] = 2 else: kernel = stats.gaussian_kde(l_local_log_prod) cdf = kernel.integrate_box_1d(-np.inf, d_logprob_t) if cdf < 0.1: v_A[d_timestep] = 1 v_A = v_A[12:] v_anomalies = np.zeros(len(v_A)) for d_i_4h in range(0, len(v_A) + 1 - 24): v_A_4h = v_A[d_i_4h:d_i_4h + 24] v_anomalies_i = contrario_utils.contrario_detection( v_A_4h, config.contrario_eps) v_anomalies[d_i_4h:d_i_4h + 24][v_anomalies_i == 1] = 1 if len(contrario_utils.nonzero_segments(v_anomalies)) > 0: D["anomaly_idx"] = v_anomalies l_dict_anomaly.append(D) except: n_error += 1 print("Number of processed tracks: ", len(l_dict)) print("Number of abnormal tracks: ", len(l_dict_anomaly)) print("Number of errors: ", n_error) # Save to disk n_anomalies = len(l_dict_anomaly) save_filename = os.path.basename(config.trainingset_name)\ +"-" + os.path.basename(config.trainingset_name)\