KEOClustering benchmark

← Back to KEO

Clustering a Particle Detector: e-Means and o-Means on CaloChallenge 2022 Dataset 1 (ATLAS Photon and Pion Showers)

· 52 min read

What happens when e-means and o-means are tested on detector data instead of embeddings? We benchmark them on 121,000 simulated photon showers (368 voxels each) and 120,800 pion showers (533 voxels each) from CaloChallenge 2022, Dataset 1.

Contents

In the first KEO post, we tested whether e-means and o-means could deliver the speed improvements described in their project materials when they were used on datasets that were not part of the original evaluations. We tested four embedding datasets and one physics dataset, HIGGS. The results were encouraging for e-means and more mixed for o-means.

This article takes the same question to a very different type of data: the energy a particle leaves behind as it travels through a detector.

We use Dataset 1 from the CaloChallenge 2022. It contains simulated showers produced by two types of particles:

These are not ordinary machine-learning embeddings. Most voxel values are zero, the non-zero values can differ by several orders of magnitude, and the energy of the incoming particle strongly influences the entire shower.

Here is the main result. Across both particle types and all three cluster counts we tested (\(k = 256\), \(1,024\), and \(4,096\)), e-means reached the test-error level of 25 iterations of Lloyd’s k-means algorithm (Lloyd@25) after roughly 1.1 to 1.9 passes over the training data. Our reconstruction of o-means reached a similar error level using 0.37 to 1.0 full scans of distance calculations, compared with 1.2 to 1.8 scans for e-means.

The benefit of o-means became larger as we increased the number of clusters. Its index also found the exact nearest centroid in almost every test case. For example, at \(k = 4,096\), it missed the exact nearest centroid for only about 2 out of every 100,000 photon showers.

There are important qualifications, though:

Before looking at the results, let’s first understand what a calorimeter measures, what one row in this dataset represents, and why preparing the voxel values is such an important part of this experiment.

1. Why this dataset, and what we are not doing

The CaloChallenge is a community challenge focused on fast simulation of particle detectors.

A detailed physics simulation of how a particle moves through a detector and deposits its energy can take a long time. To make this process faster, researchers develop generative machine-learning models, such as GANs, normalising flows, and diffusion models. These models learn to generate detector showers that resemble the results of detailed simulations. The challenge provides simulated showers that researchers can use to train and evaluate their models.

Dataset 1 comes from the ATLAS detector simulation. It contains showers created by photons and charged pions.

We are not training a generative model, and we are not evaluating the CaloChallenge itself. We use the detector showers as input to a clustering benchmark. This makes the dataset a useful test of the claims made by the e-means and o-means work for several reasons:

As in the rest of KEO, we report these results separately. We do not combine them with the embedding datasets. The reason is simple: the amount of work needed to reach Lloyd@25 depends on the shape and structure of the data. Detector measurements have a different structure from text or image embeddings, so their results should be read on their own.

2. Prerequisites in five minutes

This section introduces the algorithms and measurements needed to understand the experiment. The first KEO post contains the longer mathematical explanations, so you can skip this section if you have already read it.

2.1 k-means and Lloyd’s algorithm

Imagine that we have \(n\) data points and want to divide them into \(k\) groups. k-means does this by placing \(k\) representative points, called centroids, among the data.

Each data point is assigned to the closest centroid. The algorithm tries to position the centroids so that data points are, on average, as close as possible to the centroid assigned to them.

The objective is to minimise the mean squared distance (MSE):

$$ \mathrm{MSE}(C)=\frac{1}{n}\sum_{i=1}^{n}\min_{j}\|x_i-c_j\|_2^2 $$

Here:

The standard version of k-means is commonly implemented using Lloyd’s algorithm. It repeatedly performs two operations:

  1. Assignment: Find the nearest centroid for every data point.
  2. Update: Move each centroid to the average position of the points assigned to it.

These two operations make up one iteration.

The expensive part is the assignment step. Every one of the \(n\) data points must be compared with all \(k\) centroids. That means \(n \times k\) distance comparisons in one full assignment step. As the number of clusters grows, this becomes costly.

2.2 e-means: updating centroids without waiting for a full pass

e-means changes how centroids are updated.

Lloyd’s algorithm normally processes the complete dataset before updating the centroids. e-means instead works with smaller batches of data, known as mini-batches.

For every centroid, it keeps track of two things:

These values are updated using an exponential moving average. In simple terms, the algorithm gradually adjusts its estimate of each centroid as it sees more batches.

It also gradually reduces the update step size using a schedule. If a cluster stops receiving data points, the algorithm can revive that cluster rather than leaving it unused.

The main idea is that centroids do not need to wait for 25 complete passes through the dataset to become useful. e-means aims to reach a similar clustering error in roughly two passes.

2.3 o-means: finding the nearest centroid more efficiently

o-means keeps the centroid-update approach used by e-means, but changes how it searches for the nearest centroid.

An exact search compares a data point against all \(k\) centroids. o-means uses a two-level index to avoid checking every centroid.

First, it divides the centroids into groups called coarse cells. For each data point, it finds the closest few cells. It then searches only the centroids inside those selected cells.

Two settings control this search:

If the cell containing the true nearest centroid is among the selected cells, o-means returns the same answer as an exact search. If that cell is not selected, the algorithm can miss the true nearest centroid.

This is the main trade-off: searching fewer centroids reduces computation, but it introduces the possibility of an incorrect nearest-centroid assignment.

One important detail: the o-means paper has not been released. We could not run an official implementation, so all o-means results in this article use our own reconstruction, based on the authors’ project page. The exact implementation therefore remains unverified.

2.4 How we compare: Lloyd@25, passes and scans

We need a common reference point to compare the algorithms.

3. The physics in one page

You do not need a background in particle physics to follow the experiment. These five terms are enough to understand the dataset.

That final point is particularly useful. We can compare the clusters with the incident energy after clustering, even though the algorithms did not use that information to create the clusters.

Incident energy is not a ground-truth cluster label: two showers produced by particles with the same incident energy can still look different. However, it is a meaningful physical quantity that helps us understand what the clusters have learned.

4. The dataset

4.1 What one row contains

Each row in the dataset represents one simulated particle shower.

The shower is stored as a vector. Each position in that vector corresponds to one voxel, and the values are arranged layer by layer. The challenge provides a binning file that tells us how many voxels belong to each layer.

LayerPhotons: radial × angular bins = voxelsPions: radial × angular bins = voxels
08 × 1 = 88 × 1 = 8
116 × 10 = 16010 × 10 = 100
219 × 10 = 19010 × 10 = 100
35 × 1 = 55 × 1 = 5
125 × 1 = 515 × 10 = 150
13not present16 × 10 = 160
14not present10 × 1 = 10
Total \(d\)368533

This table shows that the two particle types are represented differently.

For photons, most of the features come from the middle of the electromagnetic calorimeter: layers 1 and 2 contain 350 of the 368 voxels.

Pions also have features from three hadronic layers. Those layers account for 320 of their 533 voxels. This reflects the fact that pion showers can extend further into the detector.

4.2 Dataset statistics

We use the files exactly as provided by the challenge: one training file and one separate evaluation file for each particle type. The files come from Zenodo record 8099322 (DOI: 10.5281/zenodo.8099322), and we verified their checksums. We did not create a new train/evaluation split.

Photons (calo1photons368)Pions (calo1pions533)
Training showers121,000120,800
Features \(d\) (voxels)368533
Evaluation showers121,000120,800
Held-out validation / test (50/50 split of the evaluation file, seed 0)60,500 / 60,50060,400 / 60,400
Eval showers removed as duplicates of training rows00
Non-zero voxel entries (train)67.7 %41.3 %
All-zero showers (train / eval)4 / 6942 / 973
Unique rows (train)120,997119,858
Largest single voxel value (train)about 1.8 × 10⁶ MeVabout 2.0 × 10⁶ MeV
Median deposited / incident energy0.9750.78
Converted file (float32, raw MeV)357 MB516 MB
\(n/k\) at k = 256 / 1,024 / 4,096473 / 118 / 29.5472 / 118 / 29.5

A few details are worth highlighting:

4.3 The incident energies

The incoming particles have 15 possible incident-energy values. These values double each time, starting at 256 MeV and reaching 4,194,304 MeV, or about 4 TeV.

For photons, there are 10,000 training showers at each energy up to 262,144 MeV. The number of training showers is smaller at the four highest energy values: 5,000, 3,000, 2,000, and 1,000.

Pions follow the same sampling scheme, except that there are 9,800 training showers at 262,144 MeV.

This matters when interpreting the plots later in the article. At the highest energy, the test set contains only about 500 showers. Results at those energy values can therefore be noisier than results in the middle of the range.

4.4 Four properties that make this dataset challenging

There are four important characteristics of this data that affect clustering.

  1. Sparsity. Many voxel values are exactly zero. Around one third of photon entries and nearly 60% of pion entries are zero. A low-energy shower may activate only a small number of detector cells.

  2. Very different value scales. Voxel energies range from fractions of a MeV to around \(2 \times 10^6\) MeV. The total deposited energy also changes substantially with the incoming particle’s energy. If we applied k-means directly to the raw values, a few very large voxel values would dominate the distance calculation. The algorithm would mostly separate showers by their total energy rather than comparing their detailed shapes.

  3. All-zero showers. Some showers contain no recorded energy at all. These are observations in the supplied dataset, so we keep them. There are 4 such photon showers and 942 pion showers in training; the corresponding evaluation files contain 6 and 973. Keeping them required a small adjustment in our data pipeline. Otherwise, the normal step that removes evaluation rows duplicated in the training set would remove every all-zero evaluation shower, because all-zero rows also appear in training.

  4. Photons and pions behave differently. Pion showers have much larger fluctuations. Their deposited energy can fall to around 20% of the incident energy, and they contain a sizeable group of all-zero showers. We therefore analyse photons and pions separately instead of assuming that a result for one particle type will also hold for the other.

4.5 What does a centroid look like?

A centroid is the average shower represented by one cluster. Looking at centroids helps us understand what the clustering algorithms are grouping together.

The first figure shows the energy deposited in each detector layer for six photon centroids from the \(k = 1,024\) k-means solution. The centroids are ordered from lower to higher energy. We also show the nearest e-means centroid after two passes over the data.

Energy per detector layer for six k-means centroids, from low to high energy, with the nearest e-means centroid after 2 passes overlaid, on a log axis.

Figure 1. Photon centroids’ energy per layer (k = 1,024, seed 0). The numbers in each panel title are the centroid’s mean log₂ incident energy in MeV (8.5 is about 360 MeV, 20 is about 1 TeV). Low-energy showers peak in layer 1; from about 8 GeV upward they peak in layer 2 and leak into layers 3 and 12. The e-means centroid follows the k-means one closely except where the energy in a layer is tiny.

The figure shows how the shower changes as the incoming particle becomes more energetic. Low-energy photon showers deposit most of their energy in layer 1. At higher energies, the largest deposit moves to layer 2, and some energy also reaches layers 3 and 12.

The e-means centroids follow the k-means centroids quite closely. The differences are easiest to see in layers where the deposited energy is very small.

The next figure shows the same idea in a detector-like view. Instead of combining all voxels in a layer into one value, it displays the energy in each voxel. The horizontal direction represents radial bins, and the vertical direction represents angular bins.

Voxel energy of a low-energy and a high-energy photon centroid in each of the five layers, for k-means and for e-means after 2 passes.

Figure 2. Photon centroids as detector images (k = 1,024, seed 0), one shared colour scale, brighter is more energy. The low-energy centroid is a small blob in the middle of layer 1; the high-energy centroid fills every layer, strongest near the shower axis (left) in layer 2.

The low-energy centroid appears as a small, concentrated region in layer 1. The high-energy centroid spreads across more of the detector and has its strongest energy deposit near the shower axis in layer 2.

Finally, we can look at the overall structure of the dataset using a two-dimensional projection.

Two-dimensional projection of 20,000 photon test showers coloured by incident energy, with k-means, e-means and o-means centroids at several budgets drawn on top.

Figure 3. Photon showers (grey-to-yellow, by log₂ incident energy) projected on two of 368 directions, with centroids (k = 1,024, seed 0) of each method on top. The picture shows only two directions, so it does not show that distances in the full space are preserved, and showers are coloured by energy, not by cluster. What it does show is that the data is a thin band ordered by energy, and that the centroids of all three methods follow it.

We projected 20,000 test showers onto their two leading principal components. These two directions retain 80.8% of the spread in the data.

The colours represent incident energy, not cluster assignments. The points form a relatively narrow band, with their position strongly related to incident energy. The centroids from all three methods follow this band.

Keep in mind that this is only a two-dimensional view of a 368-dimensional dataset. It helps us see the broad structure, but it cannot show whether all distances in the original feature space are preserved.

5. How we prepare the features

Before running the benchmarks, we selected and documented a primary preprocessing method. This was done before looking at the benchmark results, so we could not choose the method simply because it produced the most favourable outcome.

Our primary method has two steps:

  1. Apply \(\log(1+x)\) to every voxel’s energy value, where \(x\) is the energy in MeV.
  2. Standardise each voxel using a z-score calculated from the training data only.

Why do this?

The logarithm reduces the difference between very small and very large values. For example, it prevents a voxel containing millions of MeV from dominating every distance calculation simply because its raw value is so large. It also keeps zero values at zero and retains information about energy.

The z-score then puts each voxel on a comparable scale. After these two steps, Euclidean distance compares differences in the shower’s shape and size, voxel by voxel, on a logarithmic scale.

This choice also has a drawback. A voxel that is rarely active can receive the same standardised weight as a voxel that is active in most showers. The rarest voxel is non-zero in only 8.0% of photon training rows and 14.3% of pion training rows.

No voxel is constant or always inactive. The largest standardised value is 9.9 for photons and 6.6 for pions, so we do not have extreme standardised values that need clipping.

We are not claiming that this preprocessing method is the best possible choice. We chose it as a reasonable, clearly defined starting point. In Section 13, we compare it with three alternatives:

6. The experiment

The table below summarises the benchmark setup.

ParameterConfiguration
Datasetscalo1photons368 and calo1pions533, one benchmark each
Preprocessing\(\log(1+x)\), then z-score with training statistics (primary)
Cluster counts256; 1,024; 4,096
Random seeds0, 1, 2
AlgorithmsLloyd (1, 2, 3, 5, 10, 25 iterations); e-means and o-means (1, 1.5, 2, 3, 5, 8, 12.8 passes)
e-means recipeofficial (the constants of the authors’ code)
Implementationsour reference code for all three; the authors’ code for Lloyd@25 and e-means
Initialisationk randomly chosen training rows, seeded, the same for every method
Train-error rowsall training rows (no subsample)
Experiment cells252 per dataset, 0 failed
HardwareApple M4, CPU only, 4 torch threads
Packagekeo 0.1.5

We use the same initial centroids for every method within a run. This makes the comparison fairer because the algorithms start from the same positions.

The authors’ o-means implementation requires CUDA and was not available in our environment. We therefore skip it, as we have done in the other KEO benchmarks. Every o-means result in this article comes from our reconstruction, not the authors’ official implementation.

6.1 Choosing the o-means index settings

We had to set the o-means index parameters manually.

With the library’s default settings, the search at \(k = 256\) and \(k = 1,024\) would compare each shower with more than 100% of the centroids. That is effectively an exact search, so it would not provide a meaningful test of the index.

We therefore selected one configuration for all three values of \(k\) and for both particle types:

kCells (nlist)Cells probed (nprobe)Centroids per cellNominal comparisons per showerShare of exact search
2563212812850.0 %
1,02432123241640.6 %
4,09632121281,56838.3 %

This setting means that o-means searches roughly 38% to 50% of the centroids during each pass. As a result, one o-means pass costs approximately 0.38 to 0.50 of an exact full scan.

That is important when reading the results. The index already reduces the amount of distance work by design. Our main question is whether o-means can keep clustering quality while doing less work, and how many passes it needs to reach the reference error.

7. Results: how many passes are needed?

We first look at how quickly each method reaches the test error of Lloyd@25.

The horizontal axis in the following plots shows the number of passes over the data. The vertical axis shows the relative test MSE compared with Lloyd@25. A value of zero means the method has reached the reference error. A negative value means its test error is lower.

7.1 Photons

Gap to k-means after 25 iterations against passes over the data for photons, in three panels for k = 256, 1,024 and 4,096.

Figure 4. Photons: relative test MSE against Lloyd@25 as a function of passes. Blue is Lloyd, orange e-means, green our reconstructed o-means; filled markers are our implementation, hollow markers the authors’ (there are no hollow o-means markers). The shaded band is the range over 3 seeds. The title baked into the image uses a looser rule (within 0.5 % of Lloyd@25) than the table below (crossing 0).

The plot compares how quickly the methods approach the error of Lloyd@25 for photon showers. The shaded region shows how much the result varies across our three random seeds.

7.2 Pions

Gap to k-means after 25 iterations against passes over the data for pions, in three panels for k = 256, 1,024 and 4,096.

Figure 5. Pions, same layout. The curves start higher (k-means is further from its own answer after 1 pass) and the seed bands are wider at k = 256.

The pion curves start further above the reference error after one pass. The wider shaded region at \(k = 256\) also shows that the results vary more between random seeds in this setting.

7.3 How much work is needed to match Lloyd@25?

The table gives the estimated number of passes needed to reach the Lloyd@25 test error. It also shows the equivalent amount of distance-computation work, measured in full scans.

Datasetke-means (ours), passese-means (authors), passeso-means, passese-means (ours), scanso-means, scans
Photons2561.23†1.09†≤ 1.00‡1.210.56‡
Photons1,0241.831.561.601.790.62
Photons4,0961.861.41≤ 1.00‡1.820.37‡
Pions2561.471.391.511.430.98
Pions1,0241.871.491.721.830.76
Pions4,0961.791.32≤ 1.00‡1.750.45‡

Median over 3 seeds. † One of the three seeds was already below Lloyd@25 at 1 pass and counts as 1 pass. ‡ All three seeds were already below Lloyd@25 at 1 pass, so the passes value is an upper bound, and the scans value is the cost of that first pass (an upper bound too).

The table reports the median across three random seeds. In some cases, a method had already reached the target after the first pass. We mark those results as upper bounds because we did not test budgets smaller than one pass.

For completeness, the e-means results across individual seeds were:

The o-means results varied most for pions at \(k = 256\), where the range was 1.06 to 2.04 passes.

There are several things to notice:

7.4 A caveat about the largest pass budget

There is an unexpected result at \(k = 4,096\).

At 12.8 passes, both our e-means and our o-means implementations become worse relative to Lloyd@25. Their test error rises to about 0.7% above the reference:

The authors’ e-means implementation does not show the same increase. It remains about 0.4% to 0.7% below Lloyd@25.

We have a possible explanation, but we have not tested it yet.

At the 12.8-pass budget, the batch-size schedule selects a batch size of 32,768. Each dataset contains about 121,000 training rows, so this produces only three complete batches per epoch. Our implementation drops the remaining rows at the end of each epoch. At this setting, that means 18.8% of the training data is dropped in every epoch. At the smaller batch sizes used by the other budgets, the dropped portion is only 1.8% to 5.2%.

There is another difference: our implementation performs 38 centroid updates, while the authors’ code performs 51.

This looks similar to the fractional-pass scheduling issue we observed in the first KEO post. Because this dataset is relatively small, the effect may be more visible here.

However, this is still only a hypothesis. We need to change how leftover rows are handled and repeat the experiment before we can confirm the cause.

8. Computational work: where o-means earns its place

The number of passes tells us how many times an algorithm processes the dataset. It does not tell us the full amount of distance-computation work.

This distinction matters because e-means performs an exact nearest-centroid search, while o-means searches only a subset of centroids.

8.1 Comparing work in full-scan equivalents

We use full-scan equivalents to make the computational cost easier to compare.

One full scan means \(n \times k\) distance calculations: every training row compared with every centroid. An exact-search pass costs one full scan. An o-means pass costs less because it searches only the centroids in its selected cells.

The following figures show test error against this measure of computational work.

Gap to k-means after 25 iterations against work in full scans of the data for photons, in three panels.

Figure 6. Photons: relative test MSE against work in full scans (distance computations divided by n × k). An exact pass costs 1 scan, so the e-means curve sits at nearly the same positions as in Figure 4, and the o-means curve is shifted left by the cost of its smaller search. The title baked into the image quotes the looser 0.5 % rule.

Gap to k-means after 25 iterations against work in full scans of the data for pions, in three panels.

Figure 7. Pions, same layout.

From Section 7.3, we can compare the number of scans required by o-means with the number required by our e-means implementation.

Datasetko-means scans ÷ e-means scans
Photons2560.47 (upper bound)
Photons1,0240.35
Photons4,0960.20 (upper bound)
Pions2560.69
Pions1,0240.41
Pions4,0960.26 (upper bound)

The main pattern is clear: the computational saving from o-means grows as the number of clusters increases.

At \(k = 256\), o-means uses around half to two thirds of the distance-computation work required by e-means. At \(k = 4,096\), it uses around one fifth to one quarter.

This is the pattern described on the o-means project page. It is also different from the NYTimes-256 result in our first post, where the saving was relatively small.

Cost to match Lloyd’s best answer against the number of clusters, in passes and in full scans, for photons.

Figure 8. Photons: cost to match Lloyd@25 as k grows (median line, min to max band over 3 seeds). Where all seeds of o-means were already below Lloyd@25 at 1 pass (k = 256 and 4,096), the passes are an upper bound. The x axis labels are crowded because k is on a log scale; the three points are k = 256, 1,024 and 4,096.

There is one important detail behind these savings. Our index configuration searches only around 38% to 50% of the centroids. So some of the reduction in work is expected from the way we configured the search.

The more interesting question is whether this smaller search still finds good clusters. In other words, does o-means save computation by accepting many incorrect nearest-centroid assignments, or does it preserve the answer well? The next experiment checks that directly.

8.2 Does the index find the true nearest centroid?

To test the index, we rebuilt it using the centroids obtained after two passes of training with seed 0.

For every test shower, we compared two results:

We then counted how often the index failed to include the exact nearest centroid in the cells it searched.

Missed-nearest rate against cells probed, cell-size distribution and nominal versus actual comparisons per shower, for photons at three values of k.

Figure 9. Photons: left, the share of test showers whose true nearest centroid was missed, against the number of cells probed (the circled point, 12, is the benchmark setting); middle, how unevenly the centroids fall into the 32 cells; right, centroids actually compared per shower as a share of k, solid, against the nominal figure, dashed.

At our benchmark setting of 12 probed cells, the missed-nearest rates were:

So the index almost always included the exact nearest centroid.

We also measured how many centroids were actually compared. For photons, the index compared 51%, 42%, and 38% of all centroids at \(k = 256\), \(1,024\), and \(4,096\), respectively. The nominal settings were 50%, 41%, and 38%. The actual numbers are slightly higher because the centroids are not distributed perfectly evenly across the index cells.

For context, in the first KEO post, the same index missed the exact nearest centroid for about 3% of GloVe-100 points and about 8% of NYTimes-256 points. The miss rates on this calorimeter data are much lower.

It may be possible to reduce the search further. For example, probing only 4 cells would compare around 13% to 15% of the centroids at \(k = 4,096\). In this diagnostic test, the missed-nearest rate at that setting was 0.035% for photons and 0.9% for pions.

We did not run the full benchmark with 4 probed cells, so these numbers are only an indication of what might be possible. They are not a benchmark result.

We also do not yet know why the index works so accurately on this detector data. One possible explanation is that the showers form a narrow band ordered largely by incident energy, as shown in Figure 3. Nearby centroids may therefore tend to fall into the same index cell. We have not tested this explanation.

Missed-nearest rate against cells probed for pions at three values of k.

Figure 10. Pions: the same diagnostics. The miss rate falls more slowly with cells probed than for photons (at 4 cells it is still about 0.9 %), then reaches almost zero at the benchmark setting.

The pion index needs more cells to reach the same very low miss rate as the photon index. At 4 probed cells, its miss rate is still around 0.9%. At our benchmark setting of 12 cells, it falls to almost zero.

9. Does our e-means agree with the authors’ e-means?

So far, we have seen that our e-means implementation reaches the Lloyd@25 error level quickly. But we also want to check whether our implementation behaves like the authors’ implementation.

This is important because a reproduction should not only produce a good result; it should also agree with the reference implementation within a reasonable tolerance.

Our target, carried over from the first KEO post, is a difference of no more than 0.5% in test MSE.

Difference in test error between our e-means and the authors’ e-means for each pass setting, for photons.

Figure 11. Photons: difference in held-out error between our e-means and the authors’ e-means at each budget.

The largest absolute difference between the two implementations was:

Datasetk = 256k = 1,024k = 4,096
Photons0.22 %0.36 %1.15 %
Pions0.32 %0.45 %1.47 %

The result is straightforward:

At \(k = 4,096\), the differences are concentrated in two budgets:

At the other five budgets, the two implementations agree within 0.18%.

The Lloyd runs from the two implementations agree within 0.012% in every case. This suggests that the mismatch is specific to the e-means implementation or its update schedule, rather than a general difference in the underlying distance calculations.

The two budgets that fail are also the budgets where the implementations differ most in the number of centroid updates and the number of rows they process:

The number of dropped rows also differs, as discussed in Section 7.4.

We saw a similar pattern on GloVe-100, NYTimes-256, and Fashion-MNIST in the earlier KEO experiments. However, we have not yet isolated the exact cause with a controlled experiment.

10. Does matching Lloyd’s error mean finding the same clusters?

This is an important question that goes beyond the original speed comparison.

So far, we have compared methods using MSE. If two methods have almost the same MSE, it means they compress the data with a similar amount of error.

But it does not necessarily mean that they assign the same data points to the same clusters.

For example, two methods might divide a dataset into different groups but still place their centroids in locations that give almost identical average distances. Their error would be similar even though their cluster assignments would be different.

We wanted to understand whether that was happening here.

Using seed 0, we saved the centroids and test-set assignments produced at each budget. We then compared the assignments from each method with those produced by Lloyd@25 using the same starting point.

Adjusted Rand index and Hungarian-matched share of test showers between each method at each budget and Lloyd after 25 iterations, in three panels for k = 256, 1,024 and 4,096.

Figure 12. Photons: agreement of each method’s test-set partition with Lloyd@25 (seed 0). Top, the adjusted Rand index (1 means identical); bottom, the share of test showers that sit in the one-to-one best-matched cluster. Lloyd’s own earlier iterations are drawn for scale.

We use two measures:

The photon results after two passes are:

Photons, after 2 passes (or iterations)k = 256k = 1,024k = 4,096
Lloyd, adjusted Rand index vs Lloyd@250.420.410.48
e-means, adjusted Rand index vs Lloyd@250.390.250.13
o-means, adjusted Rand index vs Lloyd@250.410.250.13
e-means, share in the matched cluster0.490.340.23

Pions show a similar overall trend. The e-means ARI values are 0.57, 0.47, and 0.35 at \(k = 256\), \(1,024\), and \(4,096\). Lloyd’s own values are 0.47, 0.48, and 0.59.

Three observations stand out:

Our interpretation is that many different clusterings can achieve almost the same error on this dataset, especially when \(k\) is large and there are only about 30 training showers per cluster.

The faster methods are finding a different partition with a similar error, rather than necessarily finding a worse one.

This is not automatically a weakness of e-means or o-means. It does, however, change what we can claim. The results do not support the statement that e-means produces the same clusters as standard k-means.

That distinction matters in applications where the identity of each cluster is important. For example, if we build a codebook or an index using the clusters, a different partition can lead to different downstream behaviour even when the clustering error is almost unchanged.

There is one more limitation: this comparison uses only seed 0. We therefore treat the pattern as indicative, not as a result established across multiple seeds.

10.1 Cluster sizes and empty clusters

The next question is whether the methods create clusters of similar sizes or leave many clusters unused.

Cluster sizes, largest first, on a log axis for k-means after 25 iterations and for e-means and o-means after 2 passes, in three panels.

Figure 13. Photons: cluster sizes on the training showers for Lloyd@25 and for each cheap method after 2 passes (seed 0). A curve that falls off a cliff on the right has many tiny or empty clusters.

At \(k = 4,096\) for photons, the cluster sizes are:

MethodEmptyOne showerFewer than 10 showersLargest cluster ÷ average
Lloyd, 25 iterations03331,5867.2
e-means, 2 passes16138895.5
o-means, 2 passes21149125.2
e-means, 1 pass3521991,5307.6

Even Lloyd@25 produces many small clusters: 39% of its clusters contain fewer than 10 showers. That is a large fraction when the average cluster contains only about 30 showers.

After two passes, e-means and o-means distribute the showers somewhat more evenly. However, they still have a small number of empty clusters.

The first pass is more challenging. e-means starts with 352 empty clusters. Its cluster-revival step reduces this number to 48 after 1.5 passes and 16 after two passes.

So when we say that e-means reaches Lloyd@25’s error in about two passes, it is worth remembering that 16 clusters are still empty at \(k = 4,096\) in this photon experiment. We include those clusters in the results rather than removing them.

10.2 What happens to all-zero showers?

The all-zero showers form a particularly interesting group.

For photons, all 4 all-zero training showers are assigned to the same cluster by every method and at every value of \(k\). The corresponding cluster is small. At \(k = 4,096\), it contains between 89 and 122 showers, of which only 3% to 4.5% are all-zero.

For pions, all 942 all-zero training showers also end up together. But the resulting cluster is much larger:

The largest pion cluster is extremely large compared with the average cluster. Its size is 72 times the average for Lloyd@25 and 44 times the average for e-means after two passes. For photons, the corresponding ratios are only around 5 to 7.

This tells us something important about the pion results: the largest cluster is largely a group of showers with an almost empty detector, not a typical physical shower category.

Because of this, we should be careful about interpreting the pion results at \(k = 256\) to \(1,024\) as meaningful physical shower classes.

11. Do the clusters mean something physically?

The clustering algorithms never receive the incident energy of a particle. They only see the voxel values.

We can therefore use incident energy as an independent variable to understand what the clusters represent after training.

The following heatmaps show the incident-energy composition of each cluster. Each row represents one cluster, and each column represents one of the 15 possible incident energies. The colour shows what fraction of the showers in that cluster came from each energy.

Heatmaps of cluster composition by incident energy for k-means after 25 iterations and e-means after 2 passes at three values of k, for photons.

Figure 14. Photons: composition of the clusters by incident energy (seed 0, training showers). A bright diagonal band means clusters are made of one energy; a smear means they mix energies. Left, Lloyd after 25 iterations; right, e-means after 2 passes.

For photons, the pattern is quite clear. From around 1 GeV upward, almost every cluster is made mostly of showers with one incident energy. The lowest two or three energy values are the main exception, because their showers are mixed across neighbouring energies.

We also measure the relationship between cluster assignment and incident energy using normalised mutual information (NMI). This score measures how much knowing the cluster tells us about the incident energy, and vice versa. Higher values indicate a stronger relationship.

For Lloyd@25, the NMI values are:

The e-means and o-means values after two passes are within 0.01 of these numbers.

The score decreases as \(k\) grows because additional clusters can split showers from the same incident energy into several smaller groups.

Heatmaps of cluster composition by incident energy for k-means after 25 iterations and e-means after 2 passes at three values of k, for pions.

Figure 15. Pions, same layout. The diagonal is much more smeared: a pion cluster mixes three or four neighbouring energies even at k = 4,096.

The pion heatmap looks different. The diagonal pattern is much less clear. Even at \(k = 4,096\), a cluster often contains showers from three or four neighbouring incident energies.

The NMI values are also lower:

This is consistent with the physics described earlier. Hadronic showers fluctuate more than electromagnetic showers. Two pions with the same incident energy can produce noticeably different shower shapes, while showers with different incident energies can sometimes look similar.

Incident energy is useful for understanding the clusters, but it is not a label that every cluster should perfectly recover. A lower NMI value for pions is therefore not automatically a failure.

11.1 Does the match look the same at every energy?

An average result can hide differences between parts of the dataset.

For example, a method might perform well for most incident energies but perform poorly at the lowest or highest energies. To check this, we divided the held-out test showers into groups according to their incident energy.

For each energy group, we measured the test-error difference between the method and Lloyd@25. We used the smallest budget at which the method matched Lloyd overall.

Test error of e-means and o-means relative to Lloyd after 25 iterations, for each of the 15 incident energies, at three values of k, for photons.

Figure 16. Photons: gap to Lloyd@25 per incident energy (seed 0, test showers). Below 0 means the method is better at that energy. The budget used for each curve is in the CSV. About 500 test showers per energy sit at the top, so the right-hand end is noisy.

For photons, the difference is within roughly ±1% across the middle of the energy range. At the two ends, the cheaper methods sometimes have lower error than Lloyd@25.

At the highest energy, 4 TeV, the difference reaches about -12% for \(k = 256\). At \(k = 4,096\), it is around -2% to -3% at both ends of the energy range.

Test error of e-means and o-means relative to Lloyd after 25 iterations, for each of the 15 incident energies, at three values of k, for pions.

Figure 17. Pions, same layout.

For pions, the low-energy end is more noticeable. The difference reaches about -7% at 256 MeV for \(k = 1,024\), and -10% at \(k = 4,096\).

In this seed, the cheaper methods are not worse than Lloyd@25 at any of the incident energies. At some energy values, they have lower test error.

We do not yet know why. One possible explanation is that Lloyd allocates clusters according to the density of the data and may not represent the less common ends of the energy range as well. Another possibility is that the results are affected by noise because some energy groups contain only a few hundred test showers.

We have not tested either explanation. Since this analysis uses only one seed, we would also avoid making strong claims about the size of these differences.

The final detector image shows how pion centroids differ from photon centroids.

Voxel energy of a low-energy and a high-energy pion centroid in each of the seven layers, for k-means and for e-means after 2 passes.

Figure 18. Pion centroids as detector images (k = 1,024, seed 0). Compare with Figure 2: the high-energy centroid now fills the three hadronic layers (12 to 14) too, and the low-energy centroid is again a small blob in layer 1.

Compared with photons, the high-energy pion centroid also spreads into the three hadronic layers, 12 to 14. The low-energy centroid remains a small region concentrated in layer 1.

12. What the two particle types do differently

The table brings together the main differences between the photon and pion experiments.

PhotonsPions
Voxels368533
Non-zero entries67.7 %41.3 %
All-zero training showers4942
Lloyd after 1 iteration, above Lloyd@25 at k = 256+3.5 %+5.3 %
e-means, passes to match at k = 256 / 1,024 / 4,0961.23 / 1.83 / 1.861.47 / 1.87 / 1.79
o-means, scans to match at k = 256 / 1,024 / 4,0960.56 / 0.62 / 0.370.98 / 0.76 / 0.45
Parity (largest difference) at k = 4,0961.15 %1.47 %
Mutual information with incident energy at k = 1,024 (Lloyd@25)0.530.42
Largest cluster ÷ average, Lloyd@25 at k = 4,0967.272

The two datasets show the same broad ranking of methods. e-means reaches the Lloyd@25 error level in a small number of passes, and the computational saving from o-means becomes larger as \(k\) increases.

However, pions are the more difficult case in several ways. Their showers are sparser, they contain many more all-zero observations, their clusters are more uneven, and their incident energy is less clearly reflected in the cluster assignments.

The conclusion should remain modest: the behaviour of e-means and o-means is similar across two different types of particle showers. That makes it less likely that our result is a quirk of only one of the two datasets.

It is still one detector, one simulation, and one operating point. We need other datasets before generalising further.

13. Does the cost depend on how we scale the showers?

So far, we have used the same primary preprocessing method: apply \(\log(1+x)\), then z-score each voxel.

But preprocessing changes the geometry of the data. Since k-means is based on distances, changing the scale of the features can change both the clusters and how quickly an algorithm reaches a particular error.

We therefore repeated the experiment using three alternative ways of preparing the voxel energies. We tested all four preprocessing methods at \(k = 1,024\) and \(k = 4,096\), using three seeds and the same pass budgets.

One detail is important when comparing these results: each preprocessing method is compared with its own Lloyd@25 baseline.

For example, the MSE calculated on raw MeV values is not directly comparable with the MSE calculated on log-transformed and standardised values. They are measured in different feature spaces. What we can compare is how much work each algorithm needs to reach the Lloyd@25 result within that same feature space.

Cost to match Lloyd after 25 iterations under four ways of preparing the voxel energies, in passes and full scans, at k = 1,024 and 4,096, for photons.

Figure 19. Photons: cost for e-means and o-means to match their own recipe’s Lloyd@25 under four preprocessing recipes. One dot per seed, the line is the median; a hollow dot is censored (already at or below Lloyd at 1 pass); an × means that seed never reached Lloyd@25 at any budget.

The same figure for pions.

Figure 20. Pions, same layout.

The table shows the median number of passes needed to match the corresponding Lloyd@25 result. Each entry lists our e-means result first and o-means second.

RecipePhotons, k = 1,024Photons, k = 4,096Pions, k = 1,024Pions, k = 4,096
log1p + z-score (primary)1.83 / 1.601.86 / ≤ 1.001.87 / 1.721.79 / ≤ 1.00
raw MeV1.06 / 1.30≤ 1.00 / ≤ 1.005.04 / 4.141.27 / 1.01
raw MeV, z-scored1.29 / 1.05§1.92 / 1.921.65 / 1.281.73 / 1.51
energy fractionnever / never1.88 / 1.942.79 / 3.091.56 / 1.23

§ One of the three seeds never reached Lloyd@25; the median is over the other two.

There are four important observations:

This experiment makes one point especially clear: when benchmarking clustering algorithms, preprocessing cannot be treated as an unimportant detail. It changes the problem the algorithm is solving.

14. What this experiment establishes, and what it does not

14.1 What the results support

Based on the experiments in this article, we can support the following conclusions:

  1. With our primary preprocessing method, e-means reaches the test-error level of Lloyd@25 in about 1.1 to 1.9 passes on both CaloChallenge Dataset 1 particle types, at \(k = 256\), \(1,024\), and \(4,096\).
  2. Our o-means reconstruction reaches a similar error level in about the same number of passes or fewer. Its distance-computation work falls from around half to two thirds of e-means’ work at \(k = 256\), to around one fifth to one quarter at \(k = 4,096\).
  3. At the benchmark index setting, o-means very rarely misses the exact nearest centroid. The missed-nearest rate is at most 0.02% of test showers, so the reduction in search work is not accompanied by a large number of incorrect nearest-centroid results.
  4. Our e-means implementation agrees with the authors’ implementation within 0.5% at \(k = 256\) and \(k = 1,024\). At \(k = 4,096\), it also meets this target at most budgets, but not at 1.5 or 12.8 passes.
  5. The result depends strongly on preprocessing. The number of passes can change by a factor of five, and one setting does not reach the reference error at all.
  6. Similar MSE does not mean identical clusters. At larger values of \(k\), the partitions produced by e-means and o-means agree only weakly with Lloyd@25.

14.2 What remains unverified

There are several limitations to keep in mind.

LimitationWhy it matters
Three seedsNo significance tests; seed ranges are given, not confidence intervals
One machine, CPU onlyScans are hardware-independent; timings are not reported here
o-means is a reconstructionThe authors’ o-means needs CUDA and is not available
n is small (about 121,000)n/k is 29.5 at k = 4,096; the remainder-row effect (section 7.4) is stronger than on the larger datasets
Partition-agreement, energy and index figures use seed 0One seed, so they are indicative
One detector, one simulationDatasets 2 and 3 of the challenge (other detectors) are not tested
The 12.8-pass spike and the parity failures are not isolatedThe dropped-rows explanation is a hypothesis
o-means index set onceOne setting (32 cells, 12 probed) for all k and both particles; not tuned
Primary recipe not shown to be bestIt is the pre-registered, defensible choice
The 12.8-pass budget at k = 4,096Reference e-means and o-means end above Lloyd@25 there

These limitations do not make the measurements useless. They define how far we can reasonably take the conclusions. In particular, we should treat the results as preliminary evidence from one detector dataset, rather than as proof that the same performance will hold for every kind of data.

14.3 A note about the baseline

Lloyd@25 is our comparison point, but it is not necessarily the best clustering solution that can be achieved.

At \(k = 4,096\), Lloyd is already within 0.02% to 0.03% of its 25-iteration error after ten iterations. Meanwhile, e-means and o-means reach errors about 0.3% to 0.6% below Lloyd@25 at eight passes.

So matching Lloyd@25 is a practical reference threshold, chosen to stay consistent with the common 25-iteration convention. It should not be interpreted as reaching the globally optimal k-means solution.

k-means is a non-convex optimisation problem. Different starting points and update paths can lead to different clusterings. As Section 10 showed, even substantially different partitions can have almost the same error.

15. Experiments that would make the results stronger

There are several follow-up experiments that would help us understand these results more completely.

  1. Run more random seeds. We should use at least 10 seeds and repeat the cluster-level analyses across all of them, rather than relying on seed 0 for those figures. This would help us understand how stable the observed patterns are.
  2. Fix and test the leftover-row handling. We should update our reference implementation so that it processes the rows left over at the end of each epoch. Then we should repeat the 1.5- and 12.8-pass experiments at \(k = 4,096\). If the performance spike and implementation mismatch disappear, that would support the explanation proposed in Section 7.4.
  3. Test a smaller o-means search. We should try probing 4 cells instead of 12 and measure the missed-nearest rate again. If the rate remains close to 0.04%, the scan saving at \(k = 4,096\) could roughly double.
  4. Try other feature representations. Possible options include scaling each detector layer separately, using a square-root transformation, or clustering shower shape and total energy separately. Section 13 suggests that feature representation can matter more than the choice between algorithms.
  5. Evaluate Datasets 2 and 3 of the CaloChallenge. These datasets use other detectors and have much larger readouts. They would help us test whether the results transfer to more complex detector data. Larger values of \(k\) would also be useful, because the o-means search saving may become more important as the number of centroids grows.
  6. Run GPU benchmarks. All experiments in this article were run on the CPU. GPU experiments would provide a more complete picture of real execution time.
  7. Understand why the partitions differ. We should compare e-means partitions across different random seeds, not only against Lloyd. This would tell us whether e-means consistently finds its own type of partition or whether its results also vary substantially between runs.

16. Reproduce the experiment

The two CaloChallenge datasets are not downloaded by keo download.

To reproduce the experiment, start from the repository root. Download the four dataset files from Zenodo record 8099322 and the two binning files from the CaloChallenge homepage.

Then convert the photon data using the following command. For pions, use pions and 533 in place of the photon dataset name and feature count.

python scripts/convert_physics_dataset.py data/calo1photons368/raw/dataset_1_photons_1.hdf5 \
  --eval-source data/calo1photons368/raw/dataset_1_photons_2.hdf5 \
  --key showers --energy-key incident_energies \
  --out data/calo1photons368/calo1photons368.hdf5

The converted file stores the original voxel energies in MeV as float32, together with the incident energies.

The logarithm and z-score transformations are applied later, when the data is prepared for each experiment. This allows all four preprocessing experiments to use the same converted source file.

Each converted file has a SHA-256 checksum recorded in its dataset configuration. Rebuilding the file from the verified source files produces the same checksum.

Once the data is ready, run the benchmarks and generate the figures:

uv run keo run bench_calo1photons368        # 3 k values x 3 seeds, resumable
uv run keo run bench_calo1pions533
uv run keo figures bench_calo1photons368 --theme both
uv run keo run ablate_calo1photons368_raw   # and _rawz, _frac; the same for pions

The cluster-level figures used in Sections 8.2, 10, 11, and the preprocessing ablation analysis are generated by a separate script. It re-fits seed 0 and checks that the test MSE exactly matches the value stored by the benchmark. For both datasets, the difference was 0.0.

Run the following commands:

uv run python scripts/calo_cluster_eval.py capture photons
uv run python scripts/calo_cluster_eval.py analyze photons
uv run python scripts/calo_cluster_eval.py ablation photons

Every figure has a corresponding CSV file containing the exact values used to generate it. These files are stored in:

The experiment configurations include seeds 0, 1, and 2. Python dependencies are pinned in uv.lock.

All benchmarks were run on an Apple M4, using the CPU only, with keo 0.1.5.

17. Conclusion: the same two ideas, tested on a different kind of data

In the first KEO post, we tested two ideas using embedding datasets:

  1. e-means: Update centroids more efficiently, using mini-batches rather than waiting for a complete pass over the data.
  2. o-means: Reduce the cost of finding the nearest centroid by searching only selected groups of centroids.

This article tests the same ideas on calorimeter showers. These are very different from text or image embeddings: they are sparse, their values cover a wide range, and they have a clear physical meaning.

e-means carried over well. On both photon and pion showers, it reached the error level of Lloyd@25 in about 1.1 to 1.9 passes. This happened at all three cluster counts we tested, and the results were particularly consistent across random seeds at larger \(k\).

o-means performed better than we expected. Its computational saving increased with the number of clusters, and its index almost never missed the exact nearest centroid at our benchmark setting. This is consistent with the behaviour described on the project page and is the opposite of what we saw on NYTimes-256. Part of the saving comes from the index configuration we selected, but the measured miss rate suggests that this configuration was not too aggressive.

However, the qualifications matter just as much as the headline results.

First, similar clustering error does not mean that two algorithms find the same clusters. At larger values of \(k\), e-means and o-means produced partitions that agreed only weakly with Lloyd@25. So we should not describe the result as “e-means finds the same clusters as k-means.”

Second, the cost of reaching the reference error depends strongly on how we represent the voxel energies. Changing the preprocessing method moved the required number of passes substantially. For photon showers represented as energy fractions at \(k = 1,024\), neither e-means nor o-means reached the Lloyd@25 error within the tested budgets.

Finally, our e-means implementation still differs from the authors’ implementation at some fractional-pass budgets when \(k = 4,096\). We have a likely explanation related to dropped training rows, but it remains untested.

These results are preliminary. We used three random seeds, one machine, one detector simulation, and a reconstruction of o-means rather than the authors’ implementation. We also have not run significance tests.

The next steps are to investigate the implementation difference, test whether the o-means index can search even fewer cells, and evaluate the other detector datasets in the CaloChallenge.

The broader lesson is the same one we took from the first KEO post, but this experiment makes it even clearer: an algorithm’s performance depends not only on the algorithm itself, but also on the structure of the data and how we represent it.

For detector data, a seemingly simple decision such as how to scale voxel energies can change the amount of work needed to reach a target error by several times. That is why benchmarking clustering methods on different kinds of data, with clearly documented preprocessing, is important.

References and further reading

  1. CaloChallenge 2022: the Fast Calorimeter Simulation Challenge — the community challenge that produced the data.

    CaloChallenge homepage.

  2. CaloChallenge 2022 Dataset 1 (ATLAS photons and pions) — the files used here, with the binning definition of the voxels.

    Zenodo record 8099322.

  3. e-means: outperforming k-means (in one-tenth the epochs) — Swann Bessa et al. (2026).

    Read the e-means paper (arXiv).

  4. FAISS — the library the authors’ e-means is part of.

    FAISS GitHub repository.

  5. Rethinking K-Means: How e-Means and o-Means Reduce Clustering Computation — the first KEO post, with the derivations, the embedding datasets and HIGGS.

    Read the first post.

  6. o-means implementation details: The authors’ o-means needs CUDA, so we could not run it, and we found no public paper or repository for it. Our reconstruction is based on the project page; the exact algorithm remains unverified.