def param_run(params): jet1 = FormJets.Spectral((ints, floats), dict_jet_params=params) jet1.setup_internal() idx1, idx2 = jet1.chose_pair() jet1.step(idx1, idx2) # make a jet out of the ints and floats ints2 = jet1._ints[jet1.Label != -1] floats2 = jet1._floats[jet1.Label != -1] jet2 = FormJets.Spectral((ints2, floats2), dict_jet_params=params) jet1.run() jet2.run() num_parts = np.sum(jet1.Label != -1) tst.assert_allclose(jet1._ints[:num_parts], jet2._ints[:num_parts]) tst.assert_allclose(jet1._floats[:num_parts], jet2._floats[:num_parts])
def test_Spectral_stopping_condition(): """ Will be called before taking another step. Parameters ---------- idx1 : int index of first of the pair of particles to next join. idx2 : int index of second of the pair of particles to next join. Returns ------- : bool True if the clustering should stop now, else False. """ ints = -np.ones((3, len(FormJets.Agglomerative.int_columns))) ints[:, 0] = np.arange(3) floats = np.zeros((3, len(FormJets.Agglomerative.float_columns))) params = {"MaxMeanDist": 10} spect = FormJets.Spectral((ints, floats), memory_cap=10, dict_jet_params=params) distances2 = np.array([[np.inf, 1., 4.], [1., np.inf, 16.], [4., 16., np.inf]]) spect._raw_distances2 = distances2 outcome = spect.stopping_condition(1, 1) assert not outcome distances2 = np.array([[np.inf, 110., 100.], [110., np.inf, 100.], [100., 100., np.inf]]) spect._raw_distances2 = distances2 outcome = spect.stopping_condition(1, 0) assert outcome
def test_Spectral_size(): ints = np.array([[0, -1, -1, -1, -1], [2, -1, -1, -1, -1]]) floats = np.zeros((2, len(FormJets.Agglomerative.float_columns))) params = {"ExpofPTFormatEmbedding": "genkt", "ExpofPTEmbedding": 2} spect = FormJets.Spectral((ints, floats), memory_cap=10, dict_jet_params=params) spect.setup_internal() expected_inital_size = spect._affinity[spect._2d_avaliable_indices][0, 1] tst.assert_allclose(spect.Available_Size, expected_inital_size) new_ints, new_floats = spect.combine_ints_floats(0, 1, 0.) tst.assert_allclose(new_floats[spect._col_num["Size"]], expected_inital_size * 2)
def test_Spectral_embedding_distance2(): ints = np.array([[0, 1, -1, -1, 0], [2, 1, -1, -1, 0], [1, -1, 2, 0, 1]]) floats = np.zeros((3, len(FormJets.Agglomerative.float_columns))) params = {"ExpofPTFormatEmbedding": "genkt", "ExpofPTEmbedding": 2} spect = FormJets.Spectral((ints, floats), memory_cap=10, dict_jet_params=params) spect.setup_internal() space = np.zeros((3, 2)) pt = np.arange(3) + 1 raw_distances, distances = spect._embedding_distance2(space, pt=pt) expected = np.zeros((3, 3)) np.fill_diagonal(expected, np.inf) tst.assert_allclose(raw_distances, expected) tst.assert_allclose(distances, expected + 1) # more complex space space = np.arange(3).reshape((3, 1)) raw_distances, distances = spect._embedding_distance2(space, pt=pt) expected = np.array([[np.inf, 1., 4.], [1., np.inf, 16.], [4., 16., np.inf]]) tst.assert_allclose(expected, raw_distances) tst.assert_allclose(expected + 1, distances) # try without the kt factor, but with a SingularitySuppression params = {"ExpofPTFormatEmbedding": None, "SingularitySuppression": 1} spect = FormJets.Spectral((ints, floats), memory_cap=10, dict_jet_params=params) spect.setup_internal() space = np.arange(3).reshape((3, 1)) pt = np.arange(3) + 0 singularity = np.array([[1, 0., 0.], [0., 1, 0.5], [0., 0.5, 1]]) raw_distances, distances = spect._embedding_distance2( space, singularity, pt) raw_expected = np.array([[np.inf, 1., 4.], [1., np.inf, 1.], [4., 1., np.inf]]) expected = np.array([[np.inf, 0., 0.], [0., np.inf, 1.], [0., 1., np.inf]]) tst.assert_allclose(raw_expected, raw_distances) tst.assert_allclose(expected, distances)
def get_normed_points(eventWise, jet_params, first_event, last_event): eventWise.selected_event = None allocated_tags, tag_masses = MassPeaks.descendants_masses(eventWise, [25]) allocated_tags, tag_masses = allocated_tags[25], tag_masses[25] events = [] for event_n in range(first_event, last_event): eventWise.selected_event = event_n tag_idxs = list(eventWise.TagIndex) tag_pairs = [[tag_idxs.index(i) for i in pair] for pair in allocated_tags[event_n]] tag_rapidity = eventWise.Rapidity[tag_idxs] tag_phi = eventWise.Phi[tag_idxs] jets = FormJets.Spectral(eventWise, dict_jet_params=jet_params) jets.setup_internal() distances, masses = get_jet_masses(jets, tag_rapidity, tag_phi, tag_pairs) masses -= list(tag_masses[event_n]) events.append((distances, masses)) return events
def create_eigenvectors(eventWise, jet_params): """ Create and return the initial embedding and eigenvectors. The jet params relevent to this are; PhyDistance, AffinityType, CutoffDistance, CutoffKNN, Laplacien, ExpOfPTMultiplier SpectralMean will be used. Parameters ---------- eventWise : EventWise Data set containing particle data. jet_params : dict Input parameter choices Returns ------- eigenvalues : list of numpy arrays of floats Non trivial eigenvalues for inital embedding eigenvectors : list of numpy arrays of floats Non trivial eigenvectors for inital embedding """ eventWise.selected_event = None eigenvectors = [] eigenvalues = [] for event_n in range(len(eventWise.X)): eventWise.selected_event = event_n jets = FormJets.Spectral(eventWise, run=False, dict_jet_params=jet_params) jets.debug_run() eigenvalues.append( np.array(jets.debug_data['eigenvalues'][0]).flatten()) eigenvectors.append(np.copy(jets.debug_data['embedding'][0])) del jets return eigenvalues, eigenvectors