예제 #1
0
def events_to_pandas(events: pwa.EventCollection,
                     model: str = None) -> pd.DataFrame:
    """Convert an `~.EventCollection` to a  `~pandas.DataFrame`.

    Create a PWA formatted `~pandas.DataFrame` from an
    `~.EventCollection`.

    Parameters:
        events: The `~.EventCollection` that you want to convert.
        model: Name of the XML file containing the kinematic info or a
            `.Kinematics` instance. **Required if you want to rename the
            PIDs!**
    Raises:
        ConfigurationConflict: Number of final state particles in XML does not
            agree with number of final state particles in `~.PwaAccessor`.
    """
    pids = events.pids
    if model:
        id_to_name = pwa.get_final_state_id_to_name_mapping(model)
        if len(id_to_name) != len(pids):
            raise exception.ConfigurationConflict(
                f"XML file {model} has {len(id_to_name)} final state"
                f"particles, the event collection has {len(pids)}")
        particle_list = pwa.read_particles(model)
        pids = [
            particle_list.name_to_pid(name) for name in id_to_name.values()
        ]
    particles = naming.make_values_unique(pids)
    frame = create.pwa_frame(events.to_table(), particle_names=particles)
    if model:
        particle_list = pwa.read_particles(model)
        naming.pid_to_name(frame, particle_list)
    if events.has_weights():
        frame[_labels.WEIGHT] = events.weights()
    return frame
예제 #2
0
def generate_data_samples(model_filename, number_of_events):
    ParticleList = ui.read_particles(model_filename)

    kin = ui.create_helicity_kinematics(model_filename, ParticleList)

    gen = ui.EvtGenGenerator(
        kin.get_particle_state_transition_kinematics_info())

    rand_gen = ui.StdUniformRealGenerator(123)

    # Generate phase space sample
    phsp_sample = ui.generate_phsp(number_of_events, gen, rand_gen)

    builder = ui.IntensityBuilderXML(model_filename, ParticleList, kin,
                                     phsp_sample)
    intens = builder.create_intensity()

    # Generate Data
    sample = ui.generate(number_of_events, kin, gen, intens, rand_gen)

    # Plotting
    kin.create_all_subsystems()
    # create dataset
    dataset = kin.convert(sample)

    # use the direct data point access
    from pycompwa.plotting import (PlotData, create_nprecord)
    var_names, dataarray = ui.create_data_array(dataset)
    data_record = create_nprecord(var_names, dataarray)

    return PlotData(data_record=data_record)
예제 #3
0
def test_renames(kinematics, ids, pids, particles, selection):
    """Test rename functions."""
    frame = import_pandas(weights=True)

    # Prepare selection
    frame = frame[selection].copy(deep=True)
    three_particles = len(ids) == 3
    if three_particles:
        with pytest.raises(exception.ConfigurationConflict):
            naming.id_to_particle(frame, kinematics, make_unique=False)
    else:
        frame.rename(columns={"pi0-1": "pi0"}, inplace=True)
    assert frame.pwa.particles == particles

    particle_list = pwa.read_particles(kinematics)

    naming.name_to_pid(frame, particle_list)
    assert frame.pwa.particles == pids

    naming.pid_to_name(frame, particle_list)
    assert frame.pwa.particles == particles

    naming.particle_to_id(frame, kinematics)
    assert frame.pwa.particles == ids

    naming.id_to_particle(frame, kinematics, make_unique=True)
    assert frame.pwa.particles == particles
예제 #4
0
def pandas_to_events(frame: pd.DataFrame, model: str) -> pwa.EventCollection:
    """Convert :class:`~pandas.DataFrame` to an :class:`~.EventCollection`.

    Create a :class:`PWA formatted DataFrame <.PwaAccessor>` from an
    :class:`~.EventCollection` object.
    """
    particle_list = pwa.read_particles(model)
    id_to_name = pwa.get_final_state_id_to_name_mapping(model)
    ids = list(id_to_name.keys())
    pids = [particle_list.name_to_pid(name) for name in id_to_name.values()]
    if not set(ids) <= set(frame.pwa.particles):
        raise exception.DataException(
            "\n  You first have to convert the columns names:\n"
            f"    {frame.pwa.particles}\n"
            "  to the final state IDs:\n"
            f"    {ids}\n"
            "  as defined in XML file\n"
            f'    "{model}"\n'
            "  with e.g. naming.particle_to_id or pandas.DataFrame.rename")
    numpy_arrays = [
        frame[particle].to_numpy() for particle in frame.pwa.particles
    ]
    if frame.pwa.has_weights:
        events = [
            pwa.Event(
                pwa.FourMomentumList(
                    [pwa.FourMomentum(momentum) for momentum in event[:-1]]),
                event[-1],
            ) for event in zip(*numpy_arrays, frame.pwa.weights)
        ]
    else:  # default constructor if no weights
        events = [
            pwa.Event(
                pwa.FourMomentumList(
                    [pwa.FourMomentum(momentum) for momentum in event]))
            for event in zip(*numpy_arrays)
        ]
    return pwa.EventCollection(pids, pwa.EventList(events))