Example #1
0
def test_Agglomerative_chose_pair():
    """ Find the next two particles to join.

    Return
    ----------
    row : int
        index of first of the pair of particles to next join.
    column : int
        index of second of the pair of particles to next join.
    """
    floats = SimpleClusterSamples.two_close["floats"]
    floats = np.concatenate((floats, floats[[0]]))
    ints = -np.ones((3, len(FormJets.Agglomerative.int_columns)))
    ints[:, 0] = [0, 1, 2]
    agg = FormJets.GeneralisedKT((ints, floats), memory_cap=10)
    agg.setup_internal()
    idx1, idx2 = agg.chose_pair()
    assert {0, 2} == {idx1, idx2}

    floats = SimpleClusterSamples.two_oposite["floats"]
    params = {"DeltaR": 100}
    ints = -np.ones((2, len(FormJets.Agglomerative.int_columns)))
    ints[:, 0] = [0, 1]
    agg = FormJets.GeneralisedKT((ints, floats),
                                 memory_cap=10,
                                 dict_jet_params=params)
    agg.setup_internal()
    idx1, idx2 = agg.chose_pair()
    assert {0, 1} == {idx1, idx2}
Example #2
0
    def _cluster(self, dR=0.8):
        self.dR = dR
        self.__jet_name = "MyJet"

        # calculate higher level (HL) data
        Components.add_all(self.__data_obj, inc_mass=True)

        # filter using the HL data
        filt_fin_pcl = FormJets.filter_ends  # pcls at end of shower
        filt_observe = FormJets.filter_pt_eta  # observable pt & eta range
        FormJets.create_jetInputs(  # apply the filters to edit data inplace
            self.__data_obj, [filt_fin_pcl, filt_observe],
            batch_length=np.inf)

        # jet cluster events with anti-kt
        FormJets.cluster_multiapply(self.__data_obj,
                                    cluster_algorithm=FormJets.Traditional,
                                    dict_jet_params={
                                        'DeltaR': dR,
                                        'ExpofPTMultiplier': -1
                                    },
                                    jet_name=self.__jet_name,
                                    batch_length=np.inf)

        self.clustered = True
Example #3
0
def test_normalised_laplacian():
    """Construct a laplacian with arbitary norm,
    Z=norm_by, L = Z^-1/2(D-A)Z^-1/2

    Parameters
    -------
    affinities : 2d array of float
        Square grid of affinities between points.
    norm_by : 2d or 1d array of floats
        
    Returns
    -------
    laplacian : 2d array of float
        Laplacian of the graph.
    """
    # shouldn't choke on empty input
    out = FormJets.normalised_laplacian(np.zeros((0, 0)), np.ones(0))
    assert len(out) == 0
    # or input lenght 1
    out = FormJets.normalised_laplacian(np.ones((1, 1)), np.ones(1))
    tst.assert_allclose(out, np.zeros((1, 1)))
    # should give good ansers otherwise
    out = FormJets.normalised_laplacian(np.array([[1, 2], [2, 1]]),
                                        np.array([1, 2]))
    tst.assert_allclose(out,
                        np.array([[2, -2 / np.sqrt(2)], [-2 / np.sqrt(2), 1]]))
Example #4
0
def plot_one(eventWise, jet_name=None):
    if isinstance(eventWise, str):
        eventWise = Components.EventWise.from_file(eventWise)
    if jet_name is None:
        jet_names = FormJets.get_jet_names(eventWise)
        jet_name = InputTools.list_complete("Chose a jet; ", jet_names).strip()
    jet_idxs = FormJets.filter_jets(eventWise, jet_name)
    [
        tag_mass2_in, all_mass2_in, bg_mass2_in, rapidity_in, phi_in, pt_in,
        mask, tag_rapidity_in, tag_phi_in, tag_pt_in, percent_found,
        seperate_jets
    ] = CompareClusters.per_event_detectables(eventWise, jet_name, jet_idxs)
    plot_rapidity_phi_offset(jet_name, eventWise, rapidity_in, phi_in,
                             tag_rapidity_in, tag_phi_in)
    plt.show()
    input()
    plot_PT(jet_name, eventWise, pt_in, tag_pt_in)
    plt.show()
    input()
    plot_Width(jet_name, eventWise)
    plt.show()
    input()
    plot_Multiplicity(jet_name, eventWise)
    plt.show()
    input()
    plot_MassPeaks(jet_name, eventWise)
    plt.show()
    input()
Example #5
0
def test_ratiokt_factor():
    """A ratio factor, which will be used to reduce affinity
    to particles with differing pt.

    Parameters
    ----------
    exponant : float
        power to raise each pt to.
    pt : array of float
        Row of pt values.
    pt_column : 2d array of float (optional)
        Column of pt values.
        If not given, taken as the transpose of the row.
        
    Returns
    -------
    factor : 2d array of float
        PT factors between points in the row and the column.
    """
    # shouldn't choke on empty input
    out = FormJets.ratiokt_factor(0., np.ones(0))
    assert len(out) == 0
    # or input lenght 1
    out = FormJets.ratiokt_factor(5., np.ones(1))
    tst.assert_allclose(out, np.ones((1, 1)))
    # should give good ansers otherwise
    out = FormJets.ratiokt_factor(2., np.array([1, 2]))
    tst.assert_allclose(out, np.array([[1., 0.25], [0.25, 1.]]))
    # also should be able to use columns
    out = FormJets.ratiokt_factor(2., np.array([1, 3]), np.array([[2]]))
    tst.assert_allclose(out, np.array([[0.25, 4. / 9.]]))
Example #6
0
def test_Agglomerative_setup_ints_floats():
    """ Create the _ints and _floats, along with 
    the _avaliable_mask and _avaliable_idxs

    Parameters
    ----------
    input_data : EventWise or (2d array of ints, 2d array of floats)
        data file for inputs
    """
    floats = SimpleClusterSamples.two_close["floats"]
    with TempTestDir("tst") as dir_name:
        ew = Components.EventWise(os.path.join(dir_name, "tmp.parquet"))
        ew.selected_event = 0
        set_JetInputs(ew, floats)
        agg = FormJets.GeneralisedKT(ew)
        # should make 3 rows so there is a row for the
        tst.assert_allclose(agg._avaliable_mask, [True, True, False])
        tst.assert_allclose(agg.PT[:2], np.ones(2))
        assert {0, 1} == set(agg._avaliable_idxs)
        assert set(agg.Label) == {-1, 0, 1}
        # should be able to achive the same by passing ints and floats
        ints = -np.ones_like(agg._ints)
        ints[[0, 1], 0] = [0, 1]
        agg2 = FormJets.GeneralisedKT((agg._ints[:2], floats))
        tst.assert_allclose(agg2._ints, agg._ints)
        tst.assert_allclose(agg2._floats, agg._floats)
Example #7
0
def make_new_cluster(eventWise):
    """
    

    Parameters
    ----------
    eventWise :
        

    Returns
    -------

    """
    cluster_name, cluster_function, chosen_parameters = pick_class_params()
    # now we have parameters, apply them
    jet_name = InputTools.list_complete("Name this cluster (empty for autoname); ", [''])
    if jet_name  == '':
        found = [name.split('_', 1)[0] for name in eventWise.hyperparameters
                 if name.startswith(cluster_name)]
        i = 0
        jet_name = cluster_name + "Jet" + str(i)
        while jet_name in found:
            i += 1
            jet_name = cluster_name + "Jet" + str(i)
        print(f"Naming this {jet_name}")
    FormJets.cluster_multiapply(eventWise, cluster_function, chosen_parameters, batch_length=BATCH_LENGTH, jet_name=jet_name)
    return jet_name
Example #8
0
def filter_with_mask(eventWise, mask_name, append=True):
    new_name, jet_name = get_filtered_name(mask_name)
    eventWise.selected_event = None

    # copy the hyper parameters
    hyperparameters = {}
    # not all jets have all input parameters,
    # so filter for what really exists
    for suffix in FormJets.get_jet_input_params():
        name = jet_name + '_' + suffix
        if name in eventWise.hyperparameter_columns:
            hyperparameters[new_name + '_' + suffix] = getattr(eventWise, name)

    # not to copy the parameters
    ps_mask = getattr(eventWise, mask_name)
    jet_ints = ['_' + name for name in FormJets.Clustering.int_columns]
    label_column = FormJets.Clustering.int_columns.index("Label")
    jet_floats = ['_' + name for name in FormJets.Clustering.float_columns]
    child_ps_mask = ps_mask * getattr(eventWise, jet_name + "_Child1") == -1
    # don't cut corners, make it using proper jets....
    content = {
        new_name + suffix: [[] for _ in child_ps_mask]
        for suffix in jet_ints + jet_floats
    }
    for event_n, event_mask in enumerate(child_ps_mask):
        eventWise.selected_event = event_n
        # thses are only the floats that have passed the mask
        event_floats = [
            ak.to_numpy(
                ak.flatten(getattr(eventWise, jet_name + suffix)[event_mask]))
            for suffix in jet_floats
        ]
        event_floats = np.vstack(event_floats).T
        # the labels need to be preserved
        event_ints = -np.ones(
            (len(event_floats), len(FormJets.Clustering.int_columns)))
        event_labels = getattr(eventWise, jet_name + "_Label")[event_mask]
        event_ints[:, label_column] = ak.flatten(event_labels)
        # now make a partitional jet of these values
        jets = FormJets.ManualPartitional((event_ints, event_floats))
        # and make it cluster like the real jets
        for jet_n, labels in enumerate(event_labels):
            jets.create_jet(ak.to_list(labels))
        # split the partitional object, and read out the created values
        for jet in jets.split():
            mask = jet.Label != -1
            for suffix in jet_ints + jet_floats:
                new_content = getattr(jet, suffix[1:])[mask]
                content[new_name + suffix][event_n].append(new_content)
    eventWise.selected_event = None

    if append:
        eventWise.append_hyperparameters(**hyperparameters)
        eventWise.append(**content)
    return hyperparameters, content
Example #9
0
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)
Example #10
0
 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])
Example #11
0
def compare_FastJet_FormJets(floats, deltaR, expofPTInput):
    """Helper function, that checks that clustering produced
    by fastjet match the GeneralisedKT answers"""
    # set distance to 0
    floats[:, -1] = 0.
    for i in range(len(floats)):
        fill_angular(floats[i], energy_boost_factor=10)
    # need to keep the eventwise file around
    with TempTestDir("tst") as dir_name:
        eventWise = Components.EventWise(os.path.join(dir_name, "tmp.parquet"))
        set_JetInputs(eventWise, floats)
        eventWise.selected_event = 0
        dict_jet_params = {
            "DeltaR": deltaR,
            "ExpofPTInput": expofPTInput,
            "ExpofPTFormatInput": "genkt"
        }
        genkt = FormJets.GeneralisedKT(eventWise,
                                       dict_jet_params=dict_jet_params,
                                       run=True)
        genkt_labels = [set(jet.Leaf_Label) for jet in genkt.split()]
        fastjet = FastJetPython.run_applyfastjet(eventWise, deltaR,
                                                 expofPTInput, "Jet")
        for jet in fastjet.split():
            labels = set(jet.Leaf_Label)
            assert labels in genkt_labels, f"{labels}, not in {genkt_labels}"
Example #12
0
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
Example #13
0
def test_Agglomerative_get_historic_2d_mask():
    """get a _2d_avaliable_indices mask for a previous step

    only works if debug_data is stored

    Parameters
    ----------
    step : int
        index of step starting from oldest retained step
        which may not be first step

    Returns
    -------
    : tuple of arrays
        tuple that will index the matrix minor
    """
    floats = SimpleClusterSamples.two_close["floats"]
    floats = np.concatenate((floats, floats))
    ints = -np.ones((4, len(FormJets.Agglomerative.int_columns)))
    ints[:, 0] = [0, 1, 2, 3]
    agg = FormJets.GeneralisedKT((ints, floats), memory_cap=10)
    agg.debug_run()
    first_mask = agg.get_historic_2d_mask(0)
    test = np.arange(100).reshape((10, 10))
    selected = test[first_mask]
    expected = np.array([[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23],
                         [30, 31, 32, 33]])
    tst.assert_allclose(expected, selected)
Example #14
0
def main():
    """ """
    from jet_tools import Components, InputTools, FormJets
    eventWise_path = InputTools.get_file_name("Name the eventWise: ", '.awkd').strip()
    if eventWise_path:
        eventWise = Components.EventWise.from_file(eventWise_path)
        jets = FormJets.get_jet_names(eventWise)
        repeat = True
        barrel_radius2 = np.max((eventWise.Track_OuterX**2 +
                                 eventWise.Track_OuterY**2)))
        barrel_radius = np.sqrt(barrel_radius2)
        half_barrel_length = np.max(np.abs(eventWise.Track_OuterZ.flatten()))
        while repeat:
            eventWise.selected_event = int(input("Event number: "))
            jet_name = InputTools.list_complete("Jet name (empty for none): ", jets).strip()
            if not jet_name:
                outer_pos, tower_pos, barrel_radius, halfbarrel_length\
                        = plot_tracks_towers(eventWise, barrel_radius=barrel_radius,
                                             half_barrel_length=half_barrel_length)
            else:
                jets_here = getattr(eventWise, jet_name + "_Label")
                print(f"Number of jets = {len(jets_here)}")
                source_idx = eventWise.JetInputs_SourceIdx
                n_source = len(source_idx)
                jets_here = [source_idx[jet[jet < n_source]] for jet in jets_here]
                all_jet_particles = set(np.concatenate(jets_here))
                assert all_jet_particles == set(source_idx)
                results = plot_tracks_towers(eventWise, particle_jets=jets_here,
                                             barrel_radius=barrel_radius,
                                             half_barrel_length=half_barrel_length)
                outer_pos, tower_pos, barrel_radius, halfbarrel_length = results
            plot_beamline(halfbarrel_length*3)
            print(f"Barrel_radius = {barrel_radius}, half barrel length = {halfbarrel_length}")
            mlab.show()
            repeat = InputTools.yesNo_question("Repeat? ")
Example #15
0
def test_get_jet_names_params():
    jet_class = FormJets.Spectral
    jet_paramsA = {'MaxMeanDist': 0.4, 'ExpofPTInput': 2.}
    floats = np.random.random((3, 8))
    # set distance to 0
    floats[:, -1] = 0.
    for i in range(3):
        fill_angular(floats[i])
    # need to keep the eventwise file around
    with TempTestDir("tst") as dir_name:
        eventWise = Components.EventWise(os.path.join(dir_name, "tmp.parquet"))
        set_JetInputs(eventWise, floats)
        eventWise.selected_event = 0
        jet_list = []
        with warnings.catch_warnings():
            warnings.simplefilter('ignore')
            eventWise.selected_event = 0
            jets = jet_class(eventWise,
                             dict_jet_params=jet_paramsA,
                             run=True,
                             jet_name="AAJet")
            FormJets.check_params(jets, eventWise)
            jet_list = [jets]
            content = FormJets.create_jet_contents(jet_list, {})
            eventWise.append(**content)
            jet_paramsB = {k: v for k, v in jet_paramsA.items()}
            jet_paramsB['ExpofPTFormatEmbedding'] = None
            eventWise.selected_event = 0
            jets = jet_class(eventWise,
                             dict_jet_params=jet_paramsB,
                             run=True,
                             jet_name="BBJet")
            jet_list = [jets]
            content = FormJets.create_jet_contents(jet_list, {})
            eventWise.append(**content)
            FormJets.check_params(jets, eventWise)
        jet_names = FormJets.get_jet_names(eventWise)
        assert "AAJet" in jet_names
        assert "BBJet" in jet_names
        assert len(jet_names) == 2
        found_paramsA = FormJets.get_jet_params(eventWise,
                                                "AAJet",
                                                add_defaults=True)
        for name in jet_paramsA:
            assert found_paramsA[name] == jet_paramsA[name]
        found_paramsB = FormJets.get_jet_params(eventWise,
                                                "BBJet",
                                                add_defaults=False)
        for name in jet_paramsB:
            assert found_paramsB[name] == jet_paramsB[name]
Example #16
0
def test_embedding_angular2():
    embedding = np.array([[0., 1.], [0., 2.], [1., 0.], [0., -1]])
    out = FormJets.embedding_angular2(embedding)
    tst.assert_allclose(
        out,
        np.array([[np.inf, 0., np.pi / 2, np.pi],
                  [0., np.inf, np.pi / 2, np.pi],
                  [np.pi / 2, np.pi / 2, np.inf, np.pi / 2],
                  [np.pi, np.pi, np.pi / 2, np.inf]])**2)
Example #17
0
def test_embedding_space():
    """Calculate eigenvectors of the laplacian to make the embedding space

    Parameters
    -------
    laplacian : 2d array of float
        Laplacian of the graph.
    num_dims : int (optional)
        Max number of dimensions in the embedding space.
        If None, no max dimensions.
        (Default; None)
    max_eigval: float (optional)
        Max value of eigenvalue assocated with dimensions' eigenvector
        in the embedding space.  If None, no max eigenvalue.
        (Default; None)
        
    Returns
    -------
    values : array of float
        Eigenvalue of the dimensions
    vectors : 2d array of float
        Eigenvectors of the dimensions
    """
    laplacian = np.arange(3 * 3).reshape((3, 3))
    laplacian += laplacian.T
    laplacian = np.diag(np.sum(laplacian, 0)) - laplacian
    vals, vectors = FormJets.embedding_space(laplacian, 2)
    assert len(vals) == 2
    rhs = np.sum(laplacian * vectors[:, 0], axis=1)
    lhs = vals[0] * vectors[:, 0]
    tst.assert_allclose(rhs, lhs)
    vals, vectors = FormJets.embedding_space(laplacian, max_eigval=0.1)
    assert np.all(vals < 0.1)
    # if the laplacien is two disconnected subgraphs,
    # then there shoudl be two 0 eigenvalues
    laplacian = np.zeros((4, 4))
    np.fill_diagonal(laplacian, 1)
    laplacian[[0, 1, 2, 3], [1, 0, 3, 2]] = -1
    vals, vectors = FormJets.embedding_space(laplacian, max_eigval=0.1)
    tst.assert_allclose(vals[0], 0., atol=1e-4)
    tst.assert_allclose(vectors[[0, 2], 0], vectors[[1, 3], 0])
    vals, vectors = FormJets.embedding_space(laplacian, num_dims=1)
    tst.assert_allclose(vals[0], 0., atol=1e-4)
    tst.assert_allclose(vectors[[0, 2], 0], vectors[[1, 3], 0])
Example #18
0
def test_GeneralisedKT_simple_cases():
    """Perform the clustering, without storing debug_data."""
    floats = SimpleClusterSamples.two_degenerate['floats']
    ints = SimpleClusterSamples.two_degenerate['ints']
    # for any deltaR, no matter how small, degenerate particles should join
    params = dict(DeltaR=0.001)
    jet1 = FormJets.GeneralisedKT((ints, floats),
                                  dict_jet_params=params,
                                  run=True)
    expected_floats = SimpleClusterSamples.degenerate_join['floats']
    expected_ints = SimpleClusterSamples.degenerate_join['ints']
    mask = jet1.Label != -1
    match_ints_floats(expected_ints, expected_floats, jet1._ints[mask],
                      jet1._floats[mask])
    # now try close
    floats = SimpleClusterSamples.two_close['floats']
    ints = SimpleClusterSamples.two_close['ints']
    # for a very small deltaR these don't join
    params = dict(DeltaR=0.001)
    jet1 = FormJets.GeneralisedKT((ints, floats),
                                  dict_jet_params=params,
                                  run=True)
    mask = jet1.Label != -1
    match_ints_floats(ints, floats, jet1._ints[mask], jet1._floats[mask])
    # with a more normal deltaR they do
    expected_floats = SimpleClusterSamples.close_join['floats']
    expected_ints = SimpleClusterSamples.close_join['ints']
    params = dict(DeltaR=0.5)
    jet1 = FormJets.GeneralisedKT((ints, floats),
                                  dict_jet_params=params,
                                  run=True)
    mask = jet1.Label != -1
    match_ints_floats(expected_ints, expected_floats, jet1._ints[mask],
                      jet1._floats[mask])
    # far appart objects don't tend to join
    floats = SimpleClusterSamples.two_oposite['floats']
    ints = SimpleClusterSamples.two_oposite['ints']
    params = dict(DeltaR=0.5)
    jet1 = FormJets.GeneralisedKT((ints, floats),
                                  dict_jet_params=params,
                                  run=True)
    mask = jet1.Label != -1
    match_ints_floats(ints, floats, jet1._ints[mask], jet1._floats[mask])
Example #19
0
def test_Agglomerative_get_decendants():
    """
    Get all decendants of a chosen particle
    within the structure of the jet.

    Parameters
    ----------
    last_only : bool
        Only return the end point decendants
        (Default value = True)
    start_label : int
        start_label used to identify the starting particle
        if not given idx required
        (Default value = None)
    start_idx : int
        Internal index to identify the particle
        if not given start_label required
        (Default value = None)

    Returns
    -------
    decendants : list of ints
        local indices of the decendants

    """
    ints = np.array([[0, -1, -1, -1, 0]])
    floats = np.zeros((1, len(FormJets.Agglomerative.float_columns)))
    agg = FormJets.GeneralisedKT((ints, floats), memory_cap=10)
    out = agg.get_decendants(True, start_label=0)
    tst.assert_allclose(out, [0])
    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)))
    agg = FormJets.GeneralisedKT((ints, floats), memory_cap=10)
    out = agg.get_decendants(True, start_label=0)
    assert set(out) == {0}
    out = agg.get_decendants(True, start_label=1)
    assert set(out) == {0, 1}
    out = agg.get_decendants(False, start_label=1)
    assert set(out) == {0, 1, 2}
    out = agg.get_decendants(True, start_idx=2)
    assert set(out) == {0, 1}
    out = agg.get_decendants(False, start_idx=2)
    assert set(out) == {0, 1, 2}
Example #20
0
def cluster_multiapply_with_ghosts(eventWise, cluster_algorithm,
                                   dict_jet_params={}, jet_name=None,
                                   batch_length=100, silent=False,
                                   variable=0.1, on_grid=True):
    """
    Apply a clustering algorithm to many events, 
    also incluse ghost particles to get jet areas.

    Parameters
    ----------
    eventWise : EventWise
        data file with inputs, results are also written here
    cluster_algorithm: callable
        function or class that will create the jets
    dict_jet_params : dict
        dictionary of input parameters for clustering settings
        (Default value = {})
    jet_name : string
        Prefix name for the jet in eventWise
        (Default value = None)
    batch_length : int
        numebr of events to process
        (Default value = 100)
    silent : bool
        should print statments indicating progrss be suppressed?
        useful for running in parallel
        (Default value = False)
    variable : float
        if on_grid is True; the spacing of the grid
        else; the number of particles
        (Default value = 0.1)
    on_grid : bool
        should the ghosts be on a grid?
        if this is False they will be randomly distributed
        (Default value = True)

    Returns
    -------
    : bool
        All events in the eventWise have been clustered

    """
    particle_labels, cluster_algorithm = \
        clustering_with_ghosts(cluster_algorithm, variable, on_grid)
    clustered = FormJets.cluster_multiapply(eventWise=eventWise,
                                            cluster_algorithm=cluster_algorithm,
                                            dict_jet_params=dict_jet_params,
                                            jet_name=jet_name,
                                            batch_length=batch_length,
                                            silent=silent)
    if not silent:
        print("Clustering finished, removing ghosts")
    remove_particles_from_jets(eventWise, particle_labels, silent=silent)
    return clustered
Example #21
0
def single_check(eventWise,
                 jet_class,
                 jet_name,
                 jet_params={},
                 num_particles=10,
                 compare_tree=None):
    if compare_tree is None:
        # make a guess baes on the jet name
        if "SKM" in jet_name or "KMeans" in jet_name:
            compare_tree = False
        elif "Indicator" or "SI" in jet_name:
            compare_tree = False
        else:
            compare_tree = True
    print("Clustering without ghosts")
    jet_name_no = "NoGhost" + jet_name
    FormJets.cluster_multiapply(eventWise,
                                jet_class,
                                dict_jet_params=jet_params,
                                jet_name=jet_name_no,
                                batch_length=np.inf,
                                silent=False)
    print("Clustering with ghosts")
    GhostParticles.cluster_multiapply_with_ghosts(eventWise,
                                                  jet_class,
                                                  dict_jet_params=jet_params,
                                                  jet_name=jet_name,
                                                  batch_length=np.inf,
                                                  silent=False,
                                                  variable=num_particles,
                                                  on_grid=False)
    print("Comparing")
    if compare_tree:
        changed = check_tree_changes(eventWise, jet_name)
    else:
        changed = check_leaf_changes(eventWise, jet_name)
    if np.any(changed):
        print("Not IR safe")
    else:
        print("IR safe")
    eventWise.append(**{jet_name + "_GhostChanges": changed})
Example #22
0
def get_eigenvalues(eventWise, jet_name):
    if "Spectral" in jet_name:
        jet_class = FormJets.Spectral
    else:
        raise NotImplementedError
    jet_params = FormJets.get_jet_params(eventWise, jet_name)
    n_inputs = len(eventWise.JetInputs_Energy)
    jet = jet_class(eventWise,
                    dict_jet_params=jet_params,
                    memory_cap=n_inputs + 1)
    jet.setup_internal()
    return jet._eigenvalues
Example #23
0
def check_joins(ew, deltaR, exp):
    genkt = FormJets.GeneralisedKT(ew,
                                   dict_jet_params={
                                       "DeltaR": deltaR,
                                       "ExpofPTInput": exp
                                   },
                                   run=True,
                                   memory_cap=len(ew.JetInputs_Energy) * 2)
    genkt_joins = np.sum((genkt.Child1 != -1) * (genkt.Label != -1))
    fastjet = FastJetPython.run_applyfastjet(ew, deltaR, exp, "Jet")
    fastjet_joints = np.sum((fastjet.Label != -1) * (fastjet.Child1 != -1))
    return fastjet_joints, genkt_joins
Example #24
0
def test_create_jet_contents():
    """Process the unsplit jets in jet_list to from a content dict for an
    eventWise.

    Parameters
    ----------
    jet_list : list of Agglomerative
        the unsplit agglomerative jets, one for each event.
    existing_contents : dict
        The contents dict for all previous events

    Return
    ------
    contents : dict
        A dict with the contents of previous and new events.
    """
    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)))
    floats[:, 0] = np.arange(3)
    agg1 = FormJets.GeneralisedKT((ints, floats),
                                  jet_name="DogJet",
                                  memory_cap=10)
    ints = np.array([[0, 1, -1, -1, 0], [2, 1, -1, -1, 0], [1, -1, 2, 0, 1],
                     [3, -1, -1, -1, 0]])
    floats = np.ones((4, len(FormJets.Agglomerative.float_columns)))
    agg2 = FormJets.GeneralisedKT((ints, floats),
                                  jet_name="DogJet",
                                  memory_cap=10)
    ints = np.empty((0, len(FormJets.Agglomerative.int_columns)))
    floats = np.empty((0, len(FormJets.Agglomerative.float_columns)))
    agg3 = FormJets.GeneralisedKT((ints, floats),
                                  jet_name="DogJet",
                                  memory_cap=10)
    jet_list = [agg1, agg2, agg3]
    contents = FormJets.create_jet_contents(jet_list, {})
    assert len(contents["DogJet_Label"]) == 3
    assert len(contents["DogJet_Label"][0]) == 1
    assert len(contents["DogJet_Label"][1]) == 2
    assert len(contents["DogJet_Label"][2]) == 0
    tst.assert_allclose(sorted(contents["DogJet_PT"][0][0]), np.arange(3))
Example #25
0
def test_symmetric_laplacian():
    """Construct a symmetric laplacian, L = D^-1/2(D-A)D^-1/2

    Parameters
    -------
    affinities : 2d array of float
        Square grid of affinities between points.
        
    Returns
    -------
    laplacian : 2d array of float
        Laplacian of the graph.
    """
    # shouldn't choke on empty input
    out = FormJets.symmetric_laplacian(np.zeros((0, 0)))
    assert len(out) == 0
    # or input lenght 1
    out = FormJets.symmetric_laplacian(np.ones((1, 1)))
    tst.assert_allclose(out, np.zeros((1, 1)))
    # should give good ansers otherwise
    out = FormJets.symmetric_laplacian(np.array([[1, 2], [2, 1]]))
    tst.assert_allclose(out, np.array([[2 / 3, -2 / 3], [-2 / 3, 2 / 3]]))
Example #26
0
def test_Agglomerative_split():
    """
    Split this jet into as many unconnected jets as it contains

    Returns
    -------
    jet_list : list of Clustering
        the indervidual jets found in here
    """
    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)))
    agg = FormJets.GeneralisedKT((ints, floats), memory_cap=10)
    jet_list = agg.split()
    assert len(jet_list) == 1
    assert len(jet_list[0].Label) == 3
    ints = np.array([[0, 1, -1, -1, 0], [2, 1, -1, -1, 0], [1, -1, 2, 0, 1],
                     [3, -1, -1, -1, 0]])
    floats = np.zeros((4, len(FormJets.Agglomerative.float_columns)))
    agg = FormJets.GeneralisedKT((ints, floats), memory_cap=10)
    jet_list = agg.split()
    assert len(jet_list) == 2
    assert {len(j.Label) for j in jet_list} == {1, 3}
Example #27
0
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)
Example #28
0
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)
Example #29
0
def solution_to_jet(eventWise, idealised_jet_clusters=None, jet_name=None):
    eventWise.selected_event = None
    if idealised_jet_clusters is None:
        idealised_jet_clusters = eventWise.Solution
    if jet_name is None:
        jet_name = "SolJet"
    # updated_dict will be replaced in the first batch
    updated_dict = None
    n_events = len(idealised_jet_clusters)
    jet_list = []
    for event_n, jets_gids in enumerate(idealised_jet_clusters):
        if event_n % 100 == 0:
            print(f"{event_n/n_events:.1%}", end='\r', flush=True)
        eventWise.selected_event = event_n
        jet = FormJets.Partitional(eventWise)
        source_idx = eventWise.JetInputs_SourceIdx.tolist()
        for gids in jets_gids:
            if len(gids):
                labels = [source_idx.index(i) for i in gids]
                jet.create_jet(labels)
        jet_list.append(jet)
    updated_dict = FormJets.create_jet_contents(jet_list)
    eventWise.append(**updated_dict)
Example #30
0
def test_Agglomerative_combine_ints_floats():
    """
    Caluclate the floats and ints created by combining two pseudojets.

    Parameters
    ----------
    idx1 : int
        index of the first pseudojet to input
    idx2 : int
        index of the second pseudojet to input
    distance2 : float
        distanc esquared between the pseudojets

    Returns
    -------
    ints : list of ints
        int columns of the combined pseudojet,
        order as per the column attributes
    floats : list of floats
        float columns of the combined pseudojet,
        order as per the column attributes
    """
    degenerate_ints = SimpleClusterSamples.degenerate_join["ints"]
    degenerate_floats = SimpleClusterSamples.degenerate_join["floats"]
    agg = FormJets.GeneralisedKT((degenerate_ints[:2], degenerate_floats[:2]),
                                 memory_cap=10)
    new_ints, new_floats = agg.combine_ints_floats(0, 1, 0.)
    tst.assert_allclose(new_ints, degenerate_ints[-1])
    tst.assert_allclose(new_floats, degenerate_floats[-1])

    close_ints = SimpleClusterSamples.close_join["ints"]
    close_floats = SimpleClusterSamples.close_join["floats"]
    agg = FormJets.GeneralisedKT((close_ints[:2], close_floats[:2]),
                                 memory_cap=10)
    new_ints, new_floats = agg.combine_ints_floats(0, 1, 0.1**2)
    tst.assert_allclose(new_ints, close_ints[-1])
    tst.assert_allclose(new_floats, close_floats[-1])