def get_make_tags(eventWise, jet_name): """ Parameters ---------- eventWise : jet_name : Returns ------- """ tag_name = jet_name + '_' if InputTools.yesNo_question("Apply pt cut to jets before tagging? "): jet_pt_cut = InputTools.get_literal("Jet pt cut; ", float) jet_name += str(int(jet_pt_cut)) else: jet_pt_cut = None tag_name += "Tags" if hasattr(eventWise, tag_name): if InputTools.yesNo_question(f"{tag_name} already exists, use this? "): # just return here return jet_pt_cut dr = InputTools.get_literal("What is the maximum angle between the tag and the jet (dr)? ", float) min_tracks = InputTools.get_literal("Min tracks to tag jet; ", int) TrueTag.add_tags(eventWise, jet_name, dr, batch_length=BATCH_LENGTH, jet_pt_cut=jet_pt_cut, min_tracks=min_tracks, overwrite=True) return jet_pt_cut
def test_get_literal(): inputs = ['1', '1.', 'True', '[1,2, 3]', '(3,4)'] expecteds = [1, 1., True, [1, 2, 3], (3, 4)] for inp, exp in zip(inputs, expecteds): with replace_stdin(io.StringIO(inp)): found = InputTools.get_literal("Msg ") assert type(found) == type(exp) tst.assert_allclose(found, exp) inputs = ['1', '1.', 'True', '[1,2, 3]', '(3,4)'] expecteds = [1., 1, 1, (1, 2, 3), [3, 4]] converters = [float, int, int, tuple, list] for inp, exp, conv in zip(inputs, expecteds, converters): with replace_stdin(io.StringIO(inp)): found = InputTools.get_literal("Msg ", conv) assert type(found) == type(exp) tst.assert_allclose(found, exp)
def define_inputs(eventWise): """ Parameters ---------- eventWise : Returns ------- """ if 'JetInputs_Energy' in eventWise.columns: use_existing = InputTools.yesNo_question("There are already JetInputs, use these? ") if use_existing: return eventWise else: # make a copy of the eventWise with no clusters or JetInputs path = eventWise.path_name print("Will make a copy of the eventWise without existing clusters to change JetInputs") print(f"Current file name is {eventWise.file_name}.") new_name = InputTools.list_complete("Name the copy; ", ['']) if not new_name.endswith('.awkd'): new_name += '.awkd' new_path = os.path.join(eventWise.dir_name, new_name) shutil.copy(path, new_path) del eventWise eventWise = Components.EventWise.from_file(new_path) # remove any clusters clusters = {name.split('_', 1)[0] for name in eventWise.columns if name.endswith('Parent')} for name in clusters: eventWise.remove_prefix(name) # remove the jet inputs eventWise.remove_prefix('JetInputs') # if we get here there are no current JetInputs if InputTools.yesNo_question("Filter the tracks on pT or eta? "): pt_cut = InputTools.get_literal("What is the minimum pT of the tracks? ", float) eta_cut = InputTools.get_literal("What is the absolute maximum of the tracks? ", float) pt_eta_cut = lambda *args: FormJetInputs.filter_pt_eta(*args, min_pt=pt_cut, max_eta=eta_cut) filter_functions = [FormJetInputs.filter_ends, pt_eta_cut] else: filter_functions = [FormJetInputs.filter_ends] FormJetInputs.create_jetInputs(eventWise, filter_functions=filter_functions, batch_length=BATCH_LENGTH) return eventWise
def check_problem(): """ """ eventWise = get_data_file() BATCH_LENGTH = InputTools.get_literal("How long should the batch be (-1 for all events)? ", int) if BATCH_LENGTH == -1: BATCH_LENGTH = np.inf eventWise = define_inputs(eventWise) params = {'DeltaR': 0.4, 'ExponentMultiplier': 0.1, 'NumEigenvectors': 2, 'Laplacien': 'symmetric', "AffinityType": 'linear', "WithLaplacienScaling": False, "CutoffDistance": 5.2, "Invarient": 'normed'} jet_name = "ProblemJet" params["jet_name"] = jet_name FormJets.cluster_multiapply(eventWise, FormJets.Spectral, params, batch_length=BATCH_LENGTH)
def plot_results(eventWise, jet_name, pretag_jet_pt_cut, img_base): """ Parameters ---------- eventWise : jet_name : pretag_jet_pt_cut : img_base : Returns ------- """ tag_before_pt_cut = pretag_jet_pt_cut is None jet_pt_cut = pretag_jet_pt_cut if not tag_before_pt_cut: if InputTools.yesNo_question("Apply pt cut to jets after tagging? "): jet_pt_cut = InputTools.get_literal("Jet pt cut; ", float) if InputTools.yesNo_question("Plot shape variables? "): ShapeVariables.append_tagshapes(eventWise, batch_length=BATCH_LENGTH, jet_pt_cut=jet_pt_cut, tag_before_pt_cut=tag_before_pt_cut) ShapeVariables.append_jetshapes(eventWise, jet_name, batch_length=BATCH_LENGTH, jet_pt_cut=jet_pt_cut, tag_before_pt_cut=tag_before_pt_cut) if tag_before_pt_cut: ShapeVariables.plot_shapevars(eventWise, jet_name) else: ShapeVariables.plot_shapevars(eventWise, jet_name, jet_pt_cut) if img_base: plt.tight_layout() fig = plt.gcf() fig.set_size_inches(10, 10) plt.savefig(img_base + '_shape.png') else: plt.show() # mass peaks if not hasattr(eventWise, jet_name + "_Tags"): raise Exception #st() MassPeaks.plot_PT_pairs(eventWise, jet_name, jet_pt_cut=jet_pt_cut, show=not img_base) if img_base: plt.tight_layout() fig = plt.gcf() fig.set_size_inches(10, 10) plt.savefig(img_base + '_PTpairs.png') else: plt.show()
def check_problem(): """ """ eventWise = get_data_file() BATCH_LENGTH = InputTools.get_literal("How long should the batch be (-1 for all events)? ", int) if BATCH_LENGTH == -1: BATCH_LENGTH = np.inf eventWise = define_inputs(eventWise) params = {'DeltaR': 0.4, 'ExponentMultiplier': 0.1, 'NumEigenvectors': 2, 'Laplacien': 'symmetric', "AffinityType": 'linear', "WithLaplacienScaling": False, "CutoffDistance": 5.2, "Invarient": 'normed'} jet_name = "ProblemJet" params["jet_name"] = jet_name FormJets.cluster_multiapply(eventWise, FormJets.Spectral, params, batch_length=BATCH_LENGTH) if __name__ == '__main__': eventWise = get_data_file() BATCH_LENGTH = InputTools.get_literal("How long should the batch be (-1 for all events)? ", int) if BATCH_LENGTH == -1: BATCH_LENGTH = np.inf eventWise = define_inputs(eventWise) jet_name = get_existing_clusters(eventWise) if not jet_name: jet_name = make_new_cluster(eventWise) # now do tagging pretag_jet_pt_cut = get_make_tags(eventWise, jet_name) # now calculate shapes and mass peaks img_base = InputTools.get_file_name("Give a name to save the plots (empty for no save); ") plot_results(eventWise, jet_name, pretag_jet_pt_cut, img_base)
source = eventWise.JetInputs_SourceIdx truth = np.fromiter((next(j for j, jet in enumerate(solution) if i in jet) for i in source), dtype=int) colours = make_colours(len(set(truth)), [truth]) draw_event(eventWise.JetInputs_Px, eventWise.JetInputs_Py, eventWise.JetInputs_Pz, eventWise.JetInputs_Energy, colours) plt.show() input("Hit enter") eventWise_path = "../megaIgnore/basis_2k.parquet" eventWise = Components.EventWise.from_file(eventWise_path) event_num = InputTools.get_literal("Event number? ", int) #plot_barrel(event_num, eventWise) try: data = plot_event(event_num, eventWise, slice(0, 17), True) except (IndexError, TypeError): plt.close() #plt.close() #fig, ax1 = plt.subplots() #plot_realspace(ax1, data, 0, False) num_showers = data['num_showers'] num_steps = len(data['shower']) num_correct = sum(data['join_type']) num_incorrect = len(data['join_type']) - sum(data['join_type']) print(f"Event num: {event_num}") print(f"\tnum showers: {num_showers}") print(f"\tnum steps: {num_steps}")