Giter Club home page Giter Club logo

pyhepmc's Introduction

scikit-hep: metapackage for Scikit-HEP

image

image

image

image

image

image

image

Project info

The Scikit-HEP project is a community-driven and community-oriented project with the aim of providing Particle Physics at large with an ecosystem for data analysis in Python embracing all major topics involved in a physicist's work. The project started in Autumn 2016 and its packages are actively developed and maintained.

It is not just about providing core and common tools for the community. It is also about improving the interoperability between HEP tools and the Big Data scientific ecosystem in Python, and about improving on discoverability of utility packages and projects.

For what concerns the project grand structure, it should be seen as a toolset rather than a toolkit.

Getting in touch

There are various ways to get in touch with project admins and/or users and developers.

scikit-hep package

scikit-hep is a metapackage for the Scikit-HEP project.

Installation

You can install this metapackage from PyPI with `pip`:

python -m pip install scikit-hep

or you can use Conda through conda-forge:

conda install -c conda-forge scikit-hep

All the normal best-practices for Python apply; you should be in a virtual environment, etc.

Package version and dependencies

Please check the setup.cfg and requirements.txt files for the list of Python versions supported and the list of Scikit-HEP project packages and dependencies included, respectively.

For any installed scikit-hep the following displays the actual versions of all Scikit-HEP dependent packages installed, for example:

>>> import skhep
>>> skhep.show_versions()

System:
    python: 3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:08:06) [GCC 11.3.0]
executable: /srv/conda/envs/notebook/bin/python
   machine: Linux-5.15.0-72-generic-x86_64-with-glibc2.27

Python dependencies:
       pip: 23.1.2
     numpy: 1.24.3
     scipy: 1.10.1
    pandas: 2.0.2
matplotlib: 3.7.1

Scikit-HEP package version and dependencies:
        awkward: 2.2.2
boost_histogram: 1.3.2
  decaylanguage: 0.15.3
       hepstats: 0.6.1
       hepunits: 2.3.2
           hist: 2.6.3
     histoprint: 2.4.0
        iminuit: 2.21.3
         mplhep: 0.3.28
       particle: 0.22.0
          pylhe: 0.6.0
       resample: 1.6.0
          skhep: 2023.06.09
         uproot: 5.0.8
         vector: 1.0.0

Note on the versioning system:

This package uses Calendar Versioning (CalVer).

pyhepmc's People

Contributors

afedynitch avatar amanmdesai avatar eduardo-rodrigues avatar hdembinski avatar henryiii avatar jncots avatar pre-commit-ci[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pyhepmc's Issues

Use `open` without `with`

I'd like to do something like this:

f = pyhepmc.open("../../tests/pythia6_simple.dat")
event = f.read()

(Reason being that I want to stick f into an array with other open file handles and hand them off to a different method).

But this crashes with

AttributeError: '_GeneratorContextManager' object has no attribute 'read'

Reading file using pyhepmc (pythia6_simple.dat)

I tried to run the following commands:

import pyhepmc

# pyhepmc.open can read most HepMC formats using auto-detection
with pyhepmc.open("pythia6_simple.dat") as f:
    print(f)
    event = f.read()
    print(event)

(using the "pythia6_simple.dat" as given in tests/ )

I get the following output:

<pyhepmc._core.ReaderAscii object at 0x7ff53fefbbb0>
None.

Simpler saving of visualizations

Currently, it is a bit awkward to save a visualized event as an image.

from pyhepmc.view import to_dot

g = to_dot(event)

with open("event.svg", "w") as f:
    f.write(g._repr_image_svg_xml())  # as svg (recommended)

Add a nicer API for this, e.g.

event.saveas("event.svg")

Is it deat or alive (or undead?)

Hi!

https://pypi.org/project/pyhepmc-ng/ says "pyhepmc-ng is actively developed" but in #14 stated:

Unfortunately, we couldn't turn this project into the default Python API for HepMC3, they have their own independent Python bindings. Therefore, I cannot offer much support.

Could you clarify the status of the project and if it is more on dead side, could you update pypi description to not to confuse new users.

Thanks!

Examples for writing data

Hi there! I see that there is good coverage in the docs for reading data from HepMC files, but I can't see much information on how to write data to one. I also notice the docs say the library is "numpy-friendly", but I can't really see much in the way of numpy integration.

I have my simulation data in numpy arrays, and would love a way of writing out to file objects in the HepMC format. I tried using the GenEvent.from_hepevt() method, since it seemed to take in the numpy data and spit out a GenEvent instance, but I kept getting confusing errors

TypeError                                 Traceback (most recent call last)
Cell In[17], line 1
----> 1 hepmc.GenEvent.from_hepevt(
      2     event_number=0,
      3     px=graph.pmu.x,
      4     py=graph.pmu.y,
      5     pz=graph.pmu.z,
      6     en=graph.pmu.energy,
      7     m=graph.pmu.mass,
      8     pid=graph.pdg.data.astype(np.int32),
      9     status=graph.status.data.astype(np.int32),
     10     parents=event.mothers,
     11     children=event.daughters,
     12     fortran=False,
     13 )

TypeError: from_hepevt(): incompatible function arguments. The following argument types are supported:
    1. (self: pyhepmc._core.GenEvent, event_number: int, px: numpy.ndarray[numpy.float64], py: numpy.ndarray[numpy.float64], pz: numpy.ndarray[numpy.float64], en: numpy.ndarray[numpy.float64], m: numpy.ndarray[numpy.float64], pid: numpy.ndarray[numpy.int32], status: numpy.ndarray[numpy.int32], parents: object = None, children: object = None, vx: object = None, vy: object = None, vz: object = None, vt: object = None, fortran: bool = True) -> None

Invoked with: kwargs: event_number=0, px=array([0.        , 0.        , 0.        , ..., 1.559276  , 3.2770876 ,
       5.43204363]), py=array([0.        , 0.        , 0.        , ..., 1.20976157, 2.72396556,
       4.74063254]), pz=array([ 6.49999993e+03, -6.49999993e+03,  9.98237131e+02, ...,
        4.51464275e+00,  9.52191620e+00,  1.60423752e+01]), en=array([6.50000000e+03, 6.50000000e+03, 9.98237131e+02, ...,
       4.92715576e+00, 1.04319787e+01, 1.75880214e+01]), m=array([9.38269999e-01, 9.38269999e-01, 0.00000000e+00, ...,
       9.35541920e-08, 1.92514502e-07, 0.00000000e+00]), pid=array([2212, 2212,   21, ...,   22,   22,   22], dtype=int32), status=array([-12, -12, -21, ...,  91,  91,  91], dtype=int32), parents=array([[   0,    0],
       [   0,    0],
       [   7,    7],
       ...,
       [1532,    0],
       [1533,    0],
       [1533,    0]], dtype=int32), children=array([[384,   0],
       [385,   0],
       [  5,   6],
       ...,
       [  0,   0],
       [  0,   0],
       [  0,   0]], dtype=int32), fortran=False

As far as I can tell I added everything correctly. I found this from the HepMC3 GitLab for converting directly from a pythia8.Pythia instance, but it's a bit hard to read, given there's no docs, type annotations, and the whole method for writing it is done in one monolithic chunk. I've managed to make it work, but will take a while unpacking what's going on to be able to just throw in numpy data.

Can you offer any help? Examples or advice greatly appreciated.

Make pyhepmc.open wrappable

In impy.writer:

        # TODO fix the following in pyhepmc, we should be able
        # to use the public API and not these secrets
        op = gzip.open if file.suffix == ".gz" else open
        self._file = op(file, "wb")
        self._ios = pyiostream(self._file)
        self._writer = _WrappedWriter(self._ios, None, WriterAscii)

ValueError: vector::_M_default_append

Hi there,
I was trying out this library in python using with a file I generated. This is the code I ran:

import pyhepmc_ng

adapter = pyhepmc_ng.ReaderAsciiHepMC2(filename="file.hepmc")

evt = pyhepmc_ng.GenEvent(
    momentum_unit=pyhepmc_ng.Units.GEV, length_unit=pyhepmc_ng.Units.MM
)

adapter.read_event(evt)

which returns the error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-9f5860f373ad> in <module>
      7 )
      8 
----> 9 adapter.read_event(evt)

ValueError: vector::_M_default_append

Disclaimer: I am not versed in hepmc, I was following the logic from here.

Cheers

fill_genevent_from_hepevt not adding particles

Hi there! Thanks for creating this easy-to-install alternative to building HepMC3.

I'm currently trying to use fill_genevent_from_hepevt to fill a blank GenEvent with some particles (I'm using numpythia to generate some events to be written to a HepMC3 file for Delphes usage, but I want to do some filtering before writing since HepMC3 files are very large and I'm not interested in most of the particles in each event).

However, I can't seem to get the filling of the GenEvent to work. Here's a minimal working example demonstrating the issue:

import numpy as np
import pyhepmc_ng as hep

evt = hep.GenEvent()
evt.clear()

print(evt) # Check how things look after calling "clear".

# Get some dummy data -- here, just a single particle.
event_number = 1
p = np.array([1.,1.,1.,4.]).reshape(1,-1)
m = np.array([1.])
v = np.array([1.,1.,1.,0.]).reshape(1,-1)
pid = np.array([5])
parents = np.array([1,1]).reshape(1,-1)
status = np.array([1])

hep.fill_genevent_from_hepevt(evt,
                              event_number = event_number,
                              p = p,
                              m = m,
                              v = v,
                              pid = pid,
                              parents = parents,
                              status = status
                             )

print(evt) # See if anything changed.

And this is what I get as output:

GenEvent: #0
 Momentum units: GEV Position units: MM
 Entries in this event: 0 vertices, 0 particles, 0 weights.
 Position offset: 0, 0, 0, 0
                                    GenParticle Legend
         ID    PDG ID   ( px,       py,       pz,     E )   Stat ProdVtx
________________________________________________________________________
________________________________________________________________________

________________________________________________________________________
GenEvent: #1
 Momentum units: GEV Position units: MM
 Entries in this event: 0 vertices, 0 particles, 0 weights.
 Position offset: 0, 0, 0, 0
                                    GenParticle Legend
         ID    PDG ID   ( px,       py,       pz,     E )   Stat ProdVtx
________________________________________________________________________
________________________________________________________________________

It looks like I am modifying the event_number (trying different numbers works as well), but there doesn't appear to be any information on the particle that I've tried to add.

I think the issue might lie with HEPEVT.to_genevent(), which is called at the end of fill_genevent_from_hepevt, as I tried calling this on a HEPEVT that I created (mimicking the global buffer) and similarly didn't see any particles added to the GenEvent I passed as an argument.

I see that GenEvent has methods for explicitly adding particles and vertices, which might let me circumvent this issue for now, but it would certainly be nice to have a vectorized approach instead of adding them on the Python side of things one-by-one (I also see that there is a 2nd unused version of fill_genevent_from_hepevt that does not call HEPEVT.to_genevent).

pyiostream improvements

  • pyiostream should be able to wrap sys.stdout
    - [ ] pyiostream should use the buffer of the Python object if possible instead of allocating a separate buffer

The second thing is not possible with the public API.

numpy is not listed as dependency

Hi,

finally I get to try out this packahe. When installing pyhepmc in a new venv, where numpy is not yet installed, importing pyhepmc_ng fails. Maybe it should be added to the list of dependencies insetup.py? Happy to add a PR.

Wrong visualization

Take the following hepmc2 file containing a single semileptonic ttH event: events.zip and visualize this event using pyhepmc version 2.13.2 to a pdf (I am piping to dot -Tpdf > event.pdf):

with pyhepmc.open("test.hepmc") as f:
    event = f.read()
    print(to_dot(event))

The resulting event contains two W bosons (which one can easily search for in the PDF) one of which contains into a single up quark and nothing else. However if I do

import pyhepmc
import numpy as np

with pyhepmc.open("test.hepmc") as f:
    event = f.read()
    for particle in event.particles:
        if np.abs(particle.pid) == 24:
            print(len(particle.children))

I can see that len(particle.children) is 2 for both W bosons as it should be. Seems like there is a bug in the to_dot function.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.