def likelihood_fit(time_diff, model, x0=None, bounds=None, title='', legend='', fit_min=-numpy.inf, range_hist=(0., 20), n_bins=100): plt.subplot(2, 1, 1) bins_center, n, dn = plot_functions.plot_histogram(time_diff, "time [$\mu$s]", "", n_bins=n_bins, range=range_hist, title=title, legend=legend, fmt='.b', as_scatter=True) mask = bins_center > fit_min bins_center = bins_center[mask] n = n[mask] minus_two_ll = functions.poisson_log_likelihood(bins_center, n, model) res = minimize(minus_two_ll, x0=x0, bounds=bounds) opt = res.x print('OPT_likelihood 2expo:', opt) plot_functions.scatter_plot(bins_center, model(bins_center, *opt), "time [$\mu$s]", "", fmt='-') plt.subplot(2, 1, 2) residuals = n - model(bins_center, *opt) plot_functions.scatter_plot(bins_center, residuals, "time [$\mu$s]", "res", fmt='.') return
def plot_channel_histogram(time_diff, channel_start, channel_stop, n_bins, fit_function=None, param_names=None, param_units=None, p0=None, bounds=(-numpy.inf, numpy.inf), save_fig=False, range_hist=None, x_min=0., label='', ex_int=(numpy.inf, -numpy.inf), title=''): print('\n') legend = '%s%d eventi' % (label, len(time_diff)) bin_width = int(1000 * (range_hist[1] - range_hist[0]) / n_bins) ylabel = 'ev./%d ns ' % (bin_width) if fit_function is not None: plt.subplot(2, 1, 1) bins, n, dn = plot_functions.plot_histogram(time_diff, "$\Delta t$ [$\mu$s]", ylabel, n_bins=n_bins, range=range_hist, title=title, legend=legend, fmt='.b', as_scatter=True) opt, pcov = plot_functions.do_fit(bins, n, dn, param_names, param_units, fit_function=fit_function, p0=p0, bounds=bounds, x_min=x_min, ex_int=ex_int) l_likelihood = functions.gauss_log_likelihood(bins, n, dn, fit_function, *opt) plt.subplot(2, 1, 2) residuals = n - fit_function(bins, *opt) plot_functions.scatter_plot(bins, residuals, "$\Delta t$ [$\mu$s]", 'Res.', dx=None, dy=dn, title='') #if save_fig == True: # figlabel = 'dt_%d_%d_%s.pdf' % (channel_start, channel_stop, label) # plt.savefig('%s' % figlabel , format = 'pdf') return l_likelihood else: bins, n, dn = plot_functions.plot_histogram(time_diff, "$\Delta t$ [$\mu$s]", ylabel, n_bins=n_bins, range=range_hist, title=title, legend=legend, fmt='.b', as_scatter=True) return bins, n, dn
dn, param_names_2exp, param_units_2exp, fit_function=functions.two_expo, p0=p0, bounds=bounds, x_min=fit_min, x_max=x_max, ex_int=ex_int) #opt_expo, pcov_expo = plot_functions.do_fit(bins, n, dn, param_names = param_names, param_units = param_units, fit_function = functions.exponential, p0 = None, bounds = (-numpy.inf, numpy.inf), x_min = x_min, x_max = x_max) plt.subplot(2, 1, 2) residuals_two_expo = n - functions.two_expo(bins, *opt_two_expo) plot_functions.scatter_plot(bins, residuals_two_expo, 'time [$\mu$s]', 'residuals', dx=None, dy=dn, title='') #residuals_expo = n - functions.exponential(bins, *opt_expo) #plot_functions.scatter_plot(bins, residuals_expo, 'time [$\mu$s]', 'residuals', dx = None, dy = dn/residuals_expo, title = '') """ #ALCUNI PLOT DI MONITORAGGIO plt.figure() n_bins = 20 plt.subplot(2, 3, 1) index, channel_diff, time_diff = utilities.mask_array(ch, time, ch_stop_down, ch_stop_up) plot_channel_histogram(time_diff, ch_stop_down, ch_stop_up, n_bins = n_bins, range_hist = (-0.1, 0.1), save_fig=save_fig) plt.subplot(2, 3, 2) index, channel_diff, time_diff = utilities.mask_array(ch, time, ch_stop_down, ch_stop_down) plot_channel_histogram(time_diff, ch_stop_down, ch_stop_down, n_bins = n_bins, save_fig=save_fig)
# repeat experiments and average running time for num_outlier in num_outliers: running_times[num_outlier] = [] for t in range(repeat): # score in 2d rank_matrix = [] construction_time, scoring_time = 0., 0. for i, features in enumerate(feature_pairs): # Generate Plot Y = features[0] X = features[1] rank_list, con_time, scor_time = plot_functions.scatter_plot( eval(X), eval(Y), IDs, discription[Y], discription[X], discription[Y] + ' vs ' + discription[X], compare_value[X]) # score all points in 2d rank_matrix.append(rank_list) construction_time += con_time scoring_time += scor_time features = combine_features([ eval(F) for F in identity_features + continuous_features + discrete_features ]) iForest(features) start_time = time.time() scaled_matrix, normal_matrix = ranklist.generate_graph( P_val, num_outlier, rank_matrix) # time_graph_gen = (time.time() - start_time) * num_outlier / (
epsilon_d3, epsilon_err_d3 = efficiency(N_sel, N_tot) #Misure 18 maggio N_sel = numpy.array([2502, 2140, 2904, 2083, 2114, 2450]) N_tot = numpy.array([9103, 2210, 3211, 2200, 2208, 3636]) epsilon_d4, epsilon_err_d4 = efficiency(N_sel, N_tot) days = numpy.array([1., 2., 3., 4.]) epsilon_matrix = numpy.vstack([epsilon_d1, epsilon_d2, epsilon_d3, epsilon_d4]) epsilon_err_matrix = numpy.vstack( [epsilon_err_d1, epsilon_err_d2, epsilon_err_d3, epsilon_err_d4]) plt.figure() for i in range(len(epsilon_d1)): legend = '%d' % (i + 1) plot_functions.scatter_plot(days, epsilon_matrix[:, i] / 100, 'days', 'epsilon', dx=None, dy=epsilon_err_matrix[:, i] / 100, title='', fmt='o-', legend=legend) epsilon_mean = numpy.mean(epsilon_matrix[:, i] / 100) #epsilon_stdev = print("epsilon_mean: ", epsilon_mean) plt.ion() plt.show()
"", n_bins=n_bins, range=range_hist, title=title, legend=legend, fmt='.b', as_scatter=True) #ASIMMETRIA asimmetry, asimmetry_err, bins_center = calculate_asimmetry( time_diff_up, time_diff_down, n_bins, range_hist, x_min) plt.figure() plt.subplot(3, 1, 1) plot_functions.scatter_plot(bins_center, asimmetry, 'dt [$\mu$s]', 'Asimmetry ', dy=asimmetry_err, title='') p0 = [0.1, 3., 0.0, 0.1] bounds = (0., 0., -numpy.inf, -numpy.inf), (0.3, numpy.inf, 2 * numpy.pi, +numpy.inf) param_names = ['Amplitude', '$\omega$', '$\phi$', 'costant'] param_units = ['', 'MHz', 'rad', ''] opt_wave, pcov_wave = plot_functions.do_fit(bins_center, asimmetry, asimmetry_err, param_names, param_units, functions.wave, p0=p0, bounds=bounds,
def likelihood_fit(model, param_names, param_units, time_diff=None, bin_center=None, n=None, jacobian=None, x0=None, bounds=None, title='', legend='', fit_min=-numpy.inf, range_hist=(0., 20), n_bins=100, output_file=''): plt.subplot(3, 1, (1, 2)) if time_diff is not None: bin_width = 100 #int(1000 * (range_hist[1]- range_hist[0])/n_bins) ylabel = 'ev./%d ns ' % (bin_width) bin_center, n, dn = plot_functions.plot_histogram( time_diff, "$\Delta t$ [$\mu$s]", ylabel, n_bins=n_bins, range=range_hist, title=title, legend=legend, fmt='.b', as_scatter=True) else: bin_width = int(1000 * (bin_center[2] - bin_center[1])) ylabel = 'ev./%d ns' % (bin_width) dn = numpy.sqrt(n) plot_functions.scatter_plot(bin_center, n, "$\Delta t$ [$\mu$s]", ylabel, dy=dn, title=title, fmt='.b') mask = bin_center > fit_min bin_center = bin_center[mask] n = n[mask] dn = dn[mask] minus_two_ll = functions.poisson_log_likelihood(bin_center, n, model) if jacobian is not None: jac = jacobian(bin_center, n) else: jac = None res = minimize(minus_two_ll, x0=x0, bounds=bounds, method='BFGS', jac=jac, options={'disp': True}) opt = res.x pcov = res.hess_inv L = minus_two_ll(opt) param_results = plot_functions.fit_legend(opt, numpy.sqrt(pcov.diagonal()), param_names, param_units) plot_functions.scatter_plot(bin_center, model(bin_center, *opt), "$\Delta t$ [$\mu$s]", ylabel, fmt='-', legend=param_results, title=title) plt.subplot(3, 1, 3) residuals = n - model(bin_center, *opt) plot_functions.scatter_plot(bin_center, residuals, "$\Delta t$ [$\mu$s]", "res", fmt='.') if output_file is not '': param_results = param_results + '\n\n' with open(output_file, 'a') as of: of.write(param_results) return L, bin_center, n, dn
TOF_true, res=None) T23, res23 = signal_propagation_functions.DT_23(x1[mask], x3[mask], y3[mask], delay_T23, TOF_true, res=None) plot_functions.multiple_histogram(x1[mask], x3[mask], "x1[mask]", "x3[mask]", bins=45) plot_functions.multiple_histogram(T13, T23, "T13", "T23", bins=45) plot_functions.scatter_plot(T12, T13, "T12 [ns]", "T13 [ns]") plot_functions.scatter_plot(T23, T13, "T23 [ns]", "T13 [ns]") plot_functions.histogram(TOF_true, "TOF_true [ns]", "dN/dTOF", bins=100, range=(6., 9.), f=False) plot_functions.hist2d(T23, T13, "T23", "T13", range_x=(15., 40.), range_y=(15., 40.), norm=LogNorm())
# get global list of outliers features = combine_features([eval(F) for F in identity_features + continuous_features + discrete_features]) iForest(features) feature_pairs = generate_pairs(continuous_features, continuous_features + discrete_features) rank_matrix = [] for i, features in enumerate(feature_pairs): # Generate Plot Y = features[0] X = features[1] rank_list = plot_functions.scatter_plot( eval(X), eval(Y), IDs, discription[Y], discription[X], discription[Y] + ' vs ' + discription[X], compare_value[X] ) rank_matrix.append(rank_list) # select plots scaled_matrix, normal_matrix = ranklist.generate_graph(P_val, num_outlier, rank_matrix) plots = plotSpot(budget, scaled_matrix, 'SpellOut') # frequencies = generate_frequency_list(plots, scaled_matrix) # end clock time_elapsed = time.time() - start_time running_times[num_edge].append(time_elapsed) print num_edge, t, time_elapsed
import functions import utilities N_1 = numpy.array([29592/291, 23514/60, 27370/377]) N_2 = numpy.array([383850/385, 299636/324, 221795/306]) N_3 = numpy.array([39937/152, 42673/120, 44482/166]) N_4 = numpy.array([701930/151, 615246/120, 527184/100, 62384/100, 98885/126]) N_5 = numpy.array([38041/151, 31289/122, 29685/130]) N_6 = numpy.array([18258/151, 14259/120, 18306/153]) dN_1 = numpy.sqrt(N_1) dN_2 = numpy.sqrt(N_2) dN_3 = numpy.sqrt(N_3) dN_4 = numpy.sqrt(N_4) dN_5 = numpy.sqrt(N_5) dN_6 = numpy.sqrt(N_6) days = numpy.array([1., 2., 3.]) days_4 = numpy.array([1., 2., 2.5, 2.6, 3.]) plt.figure() plot_functions.scatter_plot(days, N_1, 'days', ' Noise[Hz]', dx = None, dy = dN_1, title = '', fmt='o-', legend = 'S1') plot_functions.scatter_plot(days, N_2, 'days', ' Noise[Hz]', dx = None, dy = dN_2, title = '', fmt='o-', legend = 'S2') plot_functions.scatter_plot(days, N_3, 'days', ' Noise[Hz]', dx = None, dy = dN_3, title = '', fmt='o-', legend = 'S3') plot_functions.scatter_plot(days_4, N_4, 'days', ' Noise[Hz]', dx = None, dy = dN_4, title = '', fmt='o-', legend = 'S4') plot_functions.scatter_plot(days, N_5, 'days', ' Noise[Hz]', dx = None, dy = dN_5, title = '', fmt='o-', legend = 'S5') plot_functions.scatter_plot(days, N_6, 'days', ' Noise[Hz]', dx = None, dy = dN_6, title = '', fmt='o-', legend = 'S6') plt.ion() plt.show()