graph-segment 0.0.1__tar.gz → 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. graph_segment-0.1.0/.github/workflows/wheels.yml +53 -0
  2. {graph_segment-0.0.1 → graph_segment-0.1.0}/.gitignore +4 -0
  3. {graph_segment-0.0.1 → graph_segment-0.1.0}/.pre-commit-config.yaml +1 -23
  4. graph_segment-0.1.0/CMakeLists.txt +11 -0
  5. {graph_segment-0.0.1 → graph_segment-0.1.0}/Dockerfile +1 -1
  6. graph_segment-0.1.0/PKG-INFO +128 -0
  7. graph_segment-0.1.0/README.md +105 -0
  8. graph_segment-0.1.0/benchmarks/muon_collider.py +117 -0
  9. {graph_segment-0.0.1 → graph_segment-0.1.0}/pyproject.toml +20 -4
  10. {graph_segment-0.0.1 → graph_segment-0.1.0}/src/CMakeLists.txt +0 -4
  11. graph_segment-0.1.0/src/cpp_core/CMakeLists.txt +7 -0
  12. graph_segment-0.1.0/src/cpp_core/bestpred_walk.cxx +289 -0
  13. graph_segment-0.1.0/src/cpp_core/include/graph_segment/bestpred_walk.h +60 -0
  14. graph_segment-0.1.0/src/cpp_pybind11/CMakeLists.txt +4 -0
  15. graph_segment-0.1.0/src/cpp_pybind11/walk_through_binding.cxx +75 -0
  16. graph_segment-0.1.0/src/graph_segment/__init__.py +89 -0
  17. graph_segment-0.1.0/tests/test_bestpred_walk.py +66 -0
  18. graph_segment-0.1.0/tests/test_get_tracks.py +91 -0
  19. graph_segment-0.0.1/.vscode/settings.json +0 -71
  20. graph_segment-0.0.1/CMakeLists.txt +0 -5
  21. graph_segment-0.0.1/PKG-INFO +0 -52
  22. graph_segment-0.0.1/README.md +0 -37
  23. graph_segment-0.0.1/data/debug_graph.dot +0 -4
  24. graph_segment-0.0.1/dist/graph_segment-0.0.1-cp310-cp310-linux_x86_64.whl +0 -0
  25. graph_segment-0.0.1/download_data.py +0 -29
  26. graph_segment-0.0.1/src/cpp_core/CMakeLists.txt +0 -20
  27. graph_segment-0.0.1/src/cpp_core/cc_and_walk.cxx +0 -332
  28. graph_segment-0.0.1/src/cpp_core/include/graph_segment/cc_and_walk.h +0 -64
  29. graph_segment-0.0.1/src/cpp_pybind11/CMakeLists.txt +0 -12
  30. graph_segment-0.0.1/src/cpp_pybind11/walk_through_binding.cxx +0 -39
  31. graph_segment-0.0.1/src/graph_segment/__init__.py +0 -5
  32. graph_segment-0.0.1/src/graph_segment/load.py +0 -21
  33. graph_segment-0.0.1/tracks.txt +0 -1
  34. graph_segment-0.0.1/tracks_v1.txt +0 -1
  35. {graph_segment-0.0.1 → graph_segment-0.1.0}/LICENSE +0 -0
  36. {graph_segment-0.0.1 → graph_segment-0.1.0}/data/.keep +0 -0
  37. {graph_segment-0.0.1 → graph_segment-0.1.0}/eval_pybinding.py +0 -0
@@ -0,0 +1,53 @@
1
+ name: build wheels
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ tags: ['v*']
7
+
8
+ jobs:
9
+ build_wheels:
10
+ name: Build wheels on ${{ matrix.os }}
11
+ runs-on: ${{ matrix.os }}
12
+ strategy:
13
+ matrix:
14
+ os: [ubuntu-latest]
15
+
16
+ steps:
17
+ - uses: actions/checkout@v5
18
+
19
+ - name: Set up Python
20
+ uses: actions/setup-python@v5
21
+ with:
22
+ python-version: "3.11"
23
+
24
+ - name: Build wheels
25
+ uses: pypa/cibuildwheel@v3.2.0
26
+ with:
27
+ output-dir: dist
28
+
29
+ - name: Build sdist
30
+ run: pip install build twine && python -m build --sdist
31
+
32
+ - uses: actions/upload-artifact@v4
33
+ with:
34
+ name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }}
35
+ path: dist/*
36
+
37
+ publish_pypi:
38
+ name: Publish to PyPI
39
+ needs: build_wheels
40
+ runs-on: ubuntu-latest
41
+ if: startsWith(github.ref, 'refs/tags/v')
42
+ steps:
43
+ - uses: actions/download-artifact@v4
44
+ with:
45
+ pattern: cibw-wheels-*
46
+ path: dist
47
+ merge-multiple: true
48
+
49
+ - name: Publish to PyPI
50
+ uses: pypa/gh-action-pypi-publish@release/v1
51
+ with:
52
+ user: __token__
53
+ password: ${{ secrets.PYPI_API_TOKEN }}
@@ -32,3 +32,7 @@
32
32
  *.app
33
33
 
34
34
  build
35
+ .venv
36
+ venv
37
+ *wheelhouse*
38
+ *dist*
@@ -19,7 +19,7 @@ ci:
19
19
  repos:
20
20
  # Standard hooks
21
21
  - repo: https://github.com/pre-commit/pre-commit-hooks
22
- rev: v5.0.0
22
+ rev: v6.0.0
23
23
  hooks:
24
24
  - id: check-added-large-files
25
25
  - id: check-case-conflict
@@ -33,22 +33,6 @@ repos:
33
33
  - id: requirements-txt-fixer
34
34
  - id: trailing-whitespace
35
35
 
36
- # Check linting and style issues
37
- - repo: https://github.com/astral-sh/ruff-pre-commit
38
- rev: "v0.7.3"
39
- hooks:
40
- - id: ruff
41
- args: ["--fix", "--show-fixes"]
42
- - id: ruff-format
43
- exclude: ^(docs)
44
-
45
- # Changes tabs to spaces
46
- - repo: https://github.com/Lucas-C/pre-commit-hooks
47
- rev: v1.5.5
48
- hooks:
49
- - id: remove-tabs
50
- exclude: ^(docs)
51
-
52
36
  # CMake formatting
53
37
  - repo: https://github.com/cheshirekow/cmake-format-precommit
54
38
  rev: v0.6.13
@@ -57,9 +41,3 @@ repos:
57
41
  additional_dependencies: [pyyaml]
58
42
  types: [file]
59
43
  files: (\.cmake|CMakeLists.txt)(.in)?$
60
-
61
- # Suggested hook if you add a .clang-format file
62
- # - repo: https://github.com/pre-commit/mirrors-clang-format
63
- # rev: v13.0.0
64
- # hooks:
65
- # - id: clang-format
@@ -0,0 +1,11 @@
1
+ cmake_minimum_required(VERSION 3.16...3.31)
2
+
3
+ project(
4
+ ${SKBUILD_PROJECT_NAME}
5
+ VERSION ${SKBUILD_PROJECT_VERSION}
6
+ LANGUAGES CXX)
7
+
8
+ set(PYBIND11_FINDPYTHON ON)
9
+ find_package(pybind11 CONFIG REQUIRED)
10
+
11
+ add_subdirectory(src)
@@ -4,7 +4,7 @@ FROM nvidia/cuda:12.4.1-cudnn-devel-ubuntu22.04
4
4
 
5
5
  RUN apt-get update -y && DEBIAN_FRONTEND="noninteractive" TZ="America/Los_Angeles" apt-get -y install \
6
6
  dpkg-dev g++ gcc python3-dev python3-pip \
7
- libboost-all-dev vim wget rsync \
7
+ vim wget rsync \
8
8
  libtbb2 libtbb-dev cmake \
9
9
  && rm -rf /var/lib/apt/lists/*
10
10
 
@@ -0,0 +1,128 @@
1
+ Metadata-Version: 2.4
2
+ Name: graph_segment
3
+ Version: 0.1.0
4
+ Summary: Graph-based image segmentation
5
+ Keywords: graph,segmentation,tracking
6
+ Author-Email: Xiangyang Ju <xju@lbl.gov>
7
+ License-Expression: MIT
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Topic :: Scientific/Engineering :: Physics
10
+ Classifier: Intended Audience :: Science/Research
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: C++
13
+ Project-URL: Homepage, https://github.com/xju2/graph_segment_for_gnn_tracking
14
+ Provides-Extra: dev
15
+ Requires-Dist: pre-commit; extra == "dev"
16
+ Requires-Dist: ruff>=0.9.7; extra == "dev"
17
+ Requires-Dist: pytest; extra == "dev"
18
+ Requires-Dist: pytest-cov; extra == "dev"
19
+ Requires-Dist: cibuildwheel; extra == "dev"
20
+ Requires-Dist: twine; extra == "dev"
21
+ Requires-Dist: wheel; extra == "dev"
22
+ Description-Content-Type: text/markdown
23
+
24
+ # Graph Segment Algorithms for Graph Neural Network-based Tracking
25
+
26
+ ### Installation
27
+ ```bash
28
+ pip install graph_segment
29
+ ```
30
+
31
+ ### Usage: `get_tracks`
32
+ After a GNN has scored the edges of a hit graph, graph segmentation cuts the graph into
33
+ tracks. All algorithms ("backends") sit behind one function, so switching algorithm means
34
+ changing one string:
35
+ ```python
36
+ import graph_segment
37
+
38
+ # edge_index: (2, E) int array, each edge pointing outward (from the hit nearer the
39
+ # interaction point), so the graph is acyclic; scores: (E,) edge scores
40
+ tracks = graph_segment.get_tracks(num_nodes, edge_index, scores, "cc_bestpred_walk",
41
+ cc_cut=0.01, th_min=0.3, th_add=0.8,
42
+ min_track_score=0.5, min_track_hits=7)
43
+ # {"cc": [[node, ...], ...], "walk": [...]} node indices into the graph
44
+ print(list(graph_segment.BACKENDS))
45
+ ```
46
+
47
+ | backend | what it does |
48
+ |---|---|
49
+ | `cc` | drop edges with score <= `cc_cut`; the connected components that are simple paths (no hit with two incoming or two outgoing edges, >= 3 hits) are tracks |
50
+ | `cc_walk` | `cc`, then walk through the branching components. From each unused hit in topological order, follow the best edge above `th_min` and branch on every edge above `th_add`, then keep the longest path |
51
+ | `cc_bestpred_walk` | `cc_walk` with the best-predecessor rule: step u -> v only if u -> v is v's best edge from an unused hit, so a branch that joins another track's chain cannot take its hits |
52
+ | `cc_junction_removal` | simple chains of >= `min_chain_length` hits after `cc_cut`. On the other hits, keep each edge above `junction_cut` that is the best in-edge of its target and the best out-edge of its source, and take their components as tracks |
53
+
54
+ - **Cuts:** `get_tracks` takes the cuts of every backend. Each backend uses only its own cuts, so one set of cuts works for all of them, and a misspelled cut name raises an error.
55
+ - **Filters:** every backend takes `min_track_score` (drop tracks whose mean internal edge score is lower) and `min_track_hits`.
56
+ - **Track length:** no backend returns a track with fewer than two hits.
57
+ - **Implementation:** the backends are C++ on plain edge arrays (`src/cpp_core/bestpred_walk.cxx`), with no Boost and no networkx.
58
+ - **Adding a backend:** write `fn(num_nodes, edge_index, scores, cut_a=..., ...) -> {method: tracks}` and add it to `BACKENDS` in `src/graph_segment/__init__.py`. Name each cut as a keyword parameter, not `**kwargs`: `get_tracks` passes a backend only the cuts it names.
59
+
60
+ `cc_and_bestpred_walk(...)` is still available and returns `(cc_tracks, walk_tracks)`.
61
+
62
+ **Validation:** every backend gives the same tracks as the Python implementation in
63
+ acorn (`acorn.stages.track_building.segmentation`). The one exception is that acorn's
64
+ plain walk also returns dead-end hits as 1-hit "tracks". `tests/test_get_tracks.py` makes
65
+ this comparison on random graphs when acorn is installed. Run the tests with `pytest tests/`.
66
+
67
+ ### Benchmark: muon collider with BIB
68
+ Setup:
69
+ - **Events:** 993 validation events, each with one muon plus ~120k beam-induced-background
70
+ hits (~72k GNN edges per event). The GNN is acorn's v3 muon-collider model (epoch 154).
71
+ - **Script:** `benchmarks/muon_collider.py`. It needs acorn to read the graphs and match tracks.
72
+ - **Matching:** acorn's matching with 50% of hits and tracks of >= 5 hits.
73
+ - **Metrics:** *eff* uses ATLAS matching and *eff 2-way* two-way matching. *fake* is the
74
+ fraction of tracks matched to no particle. *dup* is the number of extra matched tracks per
75
+ reconstructed muon.
76
+ - **Filtered:** `min_track_score=0.5`, `min_track_hits=7`.
77
+ - **Time:** `get_tracks` wall time per event with one thread on a Perlmutter CPU node.
78
+ Loading the graph and matching are excluded.
79
+
80
+ | backend | cuts | eff | fake | dup | eff 2-way | eff / fake / dup (filtered) | time / event |
81
+ |---|---|---|---|---|---|---|---|
82
+ | `cc` | 0.01 | 0.000 | 1.000 | - | 0.000 | 0.000 / - / - | 9.6 ms |
83
+ | `cc` | 0.9 | 0.189 | 0.041 | 0.000 | 0.178 | 0.183 / 0.005 / 0.000 | 10.2 ms |
84
+ | `cc_walk` | 0.01 / 0.3 / 0.8 | 0.989 | 0.306 | 0.017 | 0.983 | 0.985 / 0.027 / 0.005 | 10.7 ms |
85
+ | **`cc_bestpred_walk`** | 0.01 / 0.3 / 0.8 | **0.993** | 0.303 | 0.012 | **0.988** | **0.990 / 0.019 / 0.003** | **10.2 ms** |
86
+ | `cc_junction_removal` | 0.7 / 0.5 / 3 hits | 0.991 | 0.034 | 0.138 | 0.928 | 0.979 / 0.003 / 0.052 | 16.4 ms |
87
+
88
+ The physics numbers are identical to acorn's Python backends on the same events. The speed
89
+ is not: acorn takes 1.15 s per event for `cc` and `cc_walk`, which use networkx, and about
90
+ 22 ms for its numpy `cc_bestpred_walk` and `cc_junction_removal`.
91
+
92
+ What to take from it:
93
+ - **Use `cc_bestpred_walk` with the filter.** It has the best efficiency with a 2% fake rate.
94
+ - **Plain CC fails with this background.** At a loose cut every muon component touches
95
+ background hits and branches. At a tight cut the muon breaks into pieces.
96
+ - **Without the filter every walk makes ~30% fakes.** These are mostly short 5–6 hit
97
+ background segments. The length and score filter removes them for < 0.5% efficiency.
98
+ - **Junction removal splits the muon.** Its ATLAS efficiency and fake rate look good, but
99
+ its dup rate is high and its 2-way efficiency is low. Compare backends on the 2-way
100
+ efficiency and dup columns too.
101
+
102
+ Per-event data: https://portal.nersc.gov/cfs/m3443/xju/muon_collider/segmentation_benchmark/graph_segment/
103
+
104
+ ### Test
105
+ `pytest tests/`
106
+
107
+
108
+ ### Developer Guide
109
+ Install a Python virtual environment and activate it:
110
+ ```bash
111
+ python3 -m venv .venv
112
+ source .venv/bin/activate
113
+ pip install --upgrade pip
114
+ pip install cibuildwheel build twine
115
+ ```
116
+
117
+ Then you can run
118
+ ```bash
119
+ pipx run cibuildwheel --platform linux
120
+ ```
121
+
122
+ ### Packing and Uploading
123
+ ```bash
124
+ python3 -m build --sdist
125
+ twine upload dist/* wheelhouse/*
126
+ ```
127
+ #### Note
128
+ The code was improved by `OpenAI o1Pro` model. The execution time was reduced from 403 ms to 190 ms.
@@ -0,0 +1,105 @@
1
+ # Graph Segment Algorithms for Graph Neural Network-based Tracking
2
+
3
+ ### Installation
4
+ ```bash
5
+ pip install graph_segment
6
+ ```
7
+
8
+ ### Usage: `get_tracks`
9
+ After a GNN has scored the edges of a hit graph, graph segmentation cuts the graph into
10
+ tracks. All algorithms ("backends") sit behind one function, so switching algorithm means
11
+ changing one string:
12
+ ```python
13
+ import graph_segment
14
+
15
+ # edge_index: (2, E) int array, each edge pointing outward (from the hit nearer the
16
+ # interaction point), so the graph is acyclic; scores: (E,) edge scores
17
+ tracks = graph_segment.get_tracks(num_nodes, edge_index, scores, "cc_bestpred_walk",
18
+ cc_cut=0.01, th_min=0.3, th_add=0.8,
19
+ min_track_score=0.5, min_track_hits=7)
20
+ # {"cc": [[node, ...], ...], "walk": [...]} node indices into the graph
21
+ print(list(graph_segment.BACKENDS))
22
+ ```
23
+
24
+ | backend | what it does |
25
+ |---|---|
26
+ | `cc` | drop edges with score <= `cc_cut`; the connected components that are simple paths (no hit with two incoming or two outgoing edges, >= 3 hits) are tracks |
27
+ | `cc_walk` | `cc`, then walk through the branching components. From each unused hit in topological order, follow the best edge above `th_min` and branch on every edge above `th_add`, then keep the longest path |
28
+ | `cc_bestpred_walk` | `cc_walk` with the best-predecessor rule: step u -> v only if u -> v is v's best edge from an unused hit, so a branch that joins another track's chain cannot take its hits |
29
+ | `cc_junction_removal` | simple chains of >= `min_chain_length` hits after `cc_cut`. On the other hits, keep each edge above `junction_cut` that is the best in-edge of its target and the best out-edge of its source, and take their components as tracks |
30
+
31
+ - **Cuts:** `get_tracks` takes the cuts of every backend. Each backend uses only its own cuts, so one set of cuts works for all of them, and a misspelled cut name raises an error.
32
+ - **Filters:** every backend takes `min_track_score` (drop tracks whose mean internal edge score is lower) and `min_track_hits`.
33
+ - **Track length:** no backend returns a track with fewer than two hits.
34
+ - **Implementation:** the backends are C++ on plain edge arrays (`src/cpp_core/bestpred_walk.cxx`), with no Boost and no networkx.
35
+ - **Adding a backend:** write `fn(num_nodes, edge_index, scores, cut_a=..., ...) -> {method: tracks}` and add it to `BACKENDS` in `src/graph_segment/__init__.py`. Name each cut as a keyword parameter, not `**kwargs`: `get_tracks` passes a backend only the cuts it names.
36
+
37
+ `cc_and_bestpred_walk(...)` is still available and returns `(cc_tracks, walk_tracks)`.
38
+
39
+ **Validation:** every backend gives the same tracks as the Python implementation in
40
+ acorn (`acorn.stages.track_building.segmentation`). The one exception is that acorn's
41
+ plain walk also returns dead-end hits as 1-hit "tracks". `tests/test_get_tracks.py` makes
42
+ this comparison on random graphs when acorn is installed. Run the tests with `pytest tests/`.
43
+
44
+ ### Benchmark: muon collider with BIB
45
+ Setup:
46
+ - **Events:** 993 validation events, each with one muon plus ~120k beam-induced-background
47
+ hits (~72k GNN edges per event). The GNN is acorn's v3 muon-collider model (epoch 154).
48
+ - **Script:** `benchmarks/muon_collider.py`. It needs acorn to read the graphs and match tracks.
49
+ - **Matching:** acorn's matching with 50% of hits and tracks of >= 5 hits.
50
+ - **Metrics:** *eff* uses ATLAS matching and *eff 2-way* two-way matching. *fake* is the
51
+ fraction of tracks matched to no particle. *dup* is the number of extra matched tracks per
52
+ reconstructed muon.
53
+ - **Filtered:** `min_track_score=0.5`, `min_track_hits=7`.
54
+ - **Time:** `get_tracks` wall time per event with one thread on a Perlmutter CPU node.
55
+ Loading the graph and matching are excluded.
56
+
57
+ | backend | cuts | eff | fake | dup | eff 2-way | eff / fake / dup (filtered) | time / event |
58
+ |---|---|---|---|---|---|---|---|
59
+ | `cc` | 0.01 | 0.000 | 1.000 | - | 0.000 | 0.000 / - / - | 9.6 ms |
60
+ | `cc` | 0.9 | 0.189 | 0.041 | 0.000 | 0.178 | 0.183 / 0.005 / 0.000 | 10.2 ms |
61
+ | `cc_walk` | 0.01 / 0.3 / 0.8 | 0.989 | 0.306 | 0.017 | 0.983 | 0.985 / 0.027 / 0.005 | 10.7 ms |
62
+ | **`cc_bestpred_walk`** | 0.01 / 0.3 / 0.8 | **0.993** | 0.303 | 0.012 | **0.988** | **0.990 / 0.019 / 0.003** | **10.2 ms** |
63
+ | `cc_junction_removal` | 0.7 / 0.5 / 3 hits | 0.991 | 0.034 | 0.138 | 0.928 | 0.979 / 0.003 / 0.052 | 16.4 ms |
64
+
65
+ The physics numbers are identical to acorn's Python backends on the same events. The speed
66
+ is not: acorn takes 1.15 s per event for `cc` and `cc_walk`, which use networkx, and about
67
+ 22 ms for its numpy `cc_bestpred_walk` and `cc_junction_removal`.
68
+
69
+ What to take from it:
70
+ - **Use `cc_bestpred_walk` with the filter.** It has the best efficiency with a 2% fake rate.
71
+ - **Plain CC fails with this background.** At a loose cut every muon component touches
72
+ background hits and branches. At a tight cut the muon breaks into pieces.
73
+ - **Without the filter every walk makes ~30% fakes.** These are mostly short 5–6 hit
74
+ background segments. The length and score filter removes them for < 0.5% efficiency.
75
+ - **Junction removal splits the muon.** Its ATLAS efficiency and fake rate look good, but
76
+ its dup rate is high and its 2-way efficiency is low. Compare backends on the 2-way
77
+ efficiency and dup columns too.
78
+
79
+ Per-event data: https://portal.nersc.gov/cfs/m3443/xju/muon_collider/segmentation_benchmark/graph_segment/
80
+
81
+ ### Test
82
+ `pytest tests/`
83
+
84
+
85
+ ### Developer Guide
86
+ Install a Python virtual environment and activate it:
87
+ ```bash
88
+ python3 -m venv .venv
89
+ source .venv/bin/activate
90
+ pip install --upgrade pip
91
+ pip install cibuildwheel build twine
92
+ ```
93
+
94
+ Then you can run
95
+ ```bash
96
+ pipx run cibuildwheel --platform linux
97
+ ```
98
+
99
+ ### Packing and Uploading
100
+ ```bash
101
+ python3 -m build --sdist
102
+ twine upload dist/* wheelhouse/*
103
+ ```
104
+ #### Note
105
+ The code was improved by `OpenAI o1Pro` model. The execution time was reduced from 403 ms to 190 ms.
@@ -0,0 +1,117 @@
1
+ """Benchmark every graph_segment backend on scored muon-collider graphs: physics and time.
2
+
3
+ Needs acorn (https://gitlab.cern.ch/gnn4itkteam/acorn) to read the graphs and match tracks.
4
+
5
+ python benchmarks/muon_collider.py <scored dir>/valset -o out_dir -j 64
6
+ python benchmarks/muon_collider.py --report out_dir # table from out_dir/raw.pkl
7
+
8
+ Per event and config: get_tracks wall time (1 thread; loading, pointing the edges outward
9
+ and matching excluded), then acorn matching (ATLAS and two_way, 50% of hits, tracks of
10
+ >= 5 hits), with and without the built-in filter (min_track_score 0.5, min_track_hits 7).
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ import argparse
16
+ import glob
17
+ import os
18
+ import pickle
19
+ import time
20
+ from multiprocessing import Pool
21
+
22
+ import numpy as np
23
+
24
+ CONFIGS = { # name: (backend, cuts)
25
+ "cc@0.01": ("cc", {"cc_cut": 0.01}),
26
+ "cc@0.9": ("cc", {"cc_cut": 0.9}),
27
+ "cc_walk": ("cc_walk", {"cc_cut": 0.01, "th_min": 0.3, "th_add": 0.8}),
28
+ "cc_bestpred_walk": ("cc_bestpred_walk", {"cc_cut": 0.01, "th_min": 0.3, "th_add": 0.8}),
29
+ "cc_junction_removal": ("cc_junction_removal",
30
+ {"cc_cut": 0.7, "junction_cut": 0.5, "min_chain_length": 3}),
31
+ }
32
+ FILTER = {"min_track_score": 0.5, "min_track_hits": 7}
33
+
34
+
35
+ def counts(g, trks, style):
36
+ from acorn.stages.track_building import utils as tbu
37
+ from acorn.stages.track_building.models.cc_and_walk_utils import add_track_labels
38
+
39
+ g = g.clone()
40
+ hid = g.hit_id.numpy()
41
+ add_track_labels(g, {m: [hid[t].tolist() for t in ts] for m, ts in trks.items()})
42
+ d = tbu.evaluate_labelled_graph(g, sel_conf={"pt": [0, float("inf")]}, matching_fraction=0.5,
43
+ matching_style=style, min_track_length=5)
44
+ real = d[d.particle_id != 0] # hits on no true track form a fake "particle 0"
45
+ parts = real[real.is_reconstructable]
46
+ reco = parts[(parts.track_id != -1) & parts.is_reconstructed & parts.is_matchable]
47
+ trk = d[(d.track_id != -1) & d.is_matchable]
48
+ mt = real[(real.track_id != -1) & real.is_matched & real.is_matchable]
49
+ return np.array([len(parts.drop_duplicates(["event_id", "particle_id"])),
50
+ len(reco.drop_duplicates(["event_id", "particle_id"])), len(reco),
51
+ len(trk.drop_duplicates(["event_id", "track_id"])),
52
+ len(mt.drop_duplicates(["event_id", "track_id"]))])
53
+
54
+
55
+ def run_event(f):
56
+ import torch
57
+
58
+ torch.set_num_threads(1)
59
+ from acorn.stages.track_building.models.cc_and_walk_utils import remove_cycles
60
+ from acorn.utils.loading_utils import add_variable_name_prefix_in_pyg, load_pyg
61
+
62
+ from graph_segment import get_tracks
63
+
64
+ g = add_variable_name_prefix_in_pyg(load_pyg(f.removesuffix(".gz"), weights_only=False))
65
+ g.edge_scores = g.edge_scores.float()
66
+ g = remove_cycles(g)
67
+ n, ei, s = g.hit_id.shape[0], g.edge_index.numpy(), g.edge_scores.numpy()
68
+ out = {"event": os.path.basename(f), "n_hits": n, "n_edges": ei.shape[1]}
69
+ for name, (backend, cuts) in CONFIGS.items():
70
+ res = {}
71
+ for tag, extra in [("raw", {}), ("filt", FILTER)]:
72
+ t0 = time.perf_counter()
73
+ trks = get_tracks(n, ei, s, backend, **cuts, **extra)
74
+ res[f"time_{tag}"] = time.perf_counter() - t0
75
+ res[tag] = np.concatenate([counts(g, trks, "ATLAS"), counts(g, trks, "two_way")])
76
+ out[name] = res
77
+ return out
78
+
79
+
80
+ def report(rows, fh=None):
81
+ p = lambda *a: print(*a, file=fh) # noqa: E731
82
+ p(f"{len(rows)} events; filt = {FILTER}\n")
83
+ p(f"{'config':20s} {'filter':6s} {'eff':>6s} {'fake':>6s} {'dup':>6s} {'eff2way':>7s} "
84
+ f"{'trk/evt':>7s} | {'t_mean_ms':>9s} {'t_med_ms':>8s} {'t_p99_ms':>8s} {'t_max_ms':>8s}")
85
+ for n in CONFIGS:
86
+ for tag in ["raw", "filt"]:
87
+ c = sum(r[n][tag] for r in rows)
88
+ t = 1e3 * np.array([r[n][f"time_{tag}"] for r in rows])
89
+ p(f"{n:20s} {tag:6s} {c[1] / c[0]:6.3f} {1 - c[4] / c[3]:6.3f} "
90
+ f"{(c[2] - c[1]) / max(c[1], 1):6.3f} {c[6] / c[5]:7.3f} {c[3] / len(rows):7.2f} | "
91
+ f"{t.mean():9.2f} {np.median(t):8.2f} {np.percentile(t, 99):8.2f} {t.max():8.2f}")
92
+ p("\ncuts: " + "; ".join(f"{n} {c}" for n, (_, c) in CONFIGS.items()))
93
+ p("time = graph_segment.get_tracks wall time per event, 1 thread")
94
+
95
+
96
+ if __name__ == "__main__":
97
+ ap = argparse.ArgumentParser()
98
+ ap.add_argument("input_dir", nargs="?")
99
+ ap.add_argument("-o", "--out")
100
+ ap.add_argument("-n", type=int, default=None)
101
+ ap.add_argument("-j", type=int, default=32)
102
+ ap.add_argument("--report", help="print the table from <dir>/raw.pkl")
103
+ a = ap.parse_args()
104
+ if a.report:
105
+ with open(f"{a.report}/raw.pkl", "rb") as fh:
106
+ report(pickle.load(fh))
107
+ raise SystemExit
108
+ os.makedirs(a.out, exist_ok=True)
109
+ files = sorted(glob.glob(f"{a.input_dir}/*.pyg*"))[: a.n]
110
+ with Pool(a.j, maxtasksperchild=20) as pool:
111
+ rows = list(pool.imap_unordered(run_event, files))
112
+ with open(f"{a.out}/raw.pkl", "wb") as fh:
113
+ pickle.dump(rows, fh)
114
+ with open(f"{a.out}/table.txt", "w") as fh:
115
+ report(rows, fh)
116
+ with open(f"{a.out}/table.txt") as fh:
117
+ print(fh.read())
@@ -1,11 +1,11 @@
1
1
  [build-system]
2
- requires = ["scikit-build-core>=0.11.0", "pybind11", "cmake"]
2
+ requires = ["scikit-build-core>=0.11.0", "pybind11"]
3
3
  build-backend = "scikit_build_core.build"
4
4
 
5
5
 
6
6
  [project]
7
7
  name = "graph_segment"
8
- version = "0.0.1"
8
+ version = "0.1.0"
9
9
  description = "Graph-based image segmentation"
10
10
  readme = "README.md"
11
11
  authors = [
@@ -24,12 +24,28 @@ classifiers = [
24
24
  [project.urls]
25
25
  Homepage = "https://github.com/xju2/graph_segment_for_gnn_tracking"
26
26
 
27
+ [project.optional-dependencies]
28
+ dev = [
29
+ "pre-commit",
30
+ "ruff >= 0.9.7",
31
+ "pytest",
32
+ "pytest-cov",
33
+ "cibuildwheel",
34
+ "twine",
35
+ "wheel",
36
+ ]
37
+
38
+
39
+ [tool.cibuildwheel]
40
+ before-all = "uname -a"
41
+
42
+ manylinux-x86_64-image = "manylinux_2_28"
43
+
27
44
 
28
45
  [tool.scikit-build]
29
46
  build.verbose = true
30
47
  logging.level = "INFO"
31
- sdist.exclude = ["src/cpp_test"]
32
- wheel.exclude = ["src/cpp_test"]
48
+ cmake.version = ">=3.16,<4.0"
33
49
 
34
50
  [tool.ruff.lint]
35
51
  extend-select = [
@@ -16,7 +16,3 @@ add_compile_options(-pedantic)
16
16
 
17
17
  add_subdirectory(cpp_core)
18
18
  add_subdirectory(cpp_pybind11)
19
-
20
- if(CMAKE_BUILD_TYPE MATCHES Debug)
21
- add_subdirectory(cpp_test)
22
- endif()
@@ -0,0 +1,7 @@
1
+ add_library(gs_core STATIC bestpred_walk.cxx)
2
+ set_target_properties(
3
+ gs_core
4
+ PROPERTIES LINKER_LANGUAGE CXX
5
+ CXX_STANDARD 20
6
+ PUBLIC_HEADER "include/graph_segment/bestpred_walk.h")
7
+ target_include_directories(gs_core PUBLIC include)