## 3 Analysis of Contrast Extraction Algorithms
### Contrast Extraction Procedure  
First, we provide the general procedure of deducing contrasts from the photon probabilities of the scatterings given by the algorithms. In the low count rate regime (mean photon occupancy \(\bar{k}\ll 1\)), shot noise contributes to the fluctuations in the measured speckle intensity. The photon occupancy probabilities in discrete speckle patterns with mean count rate \(\bar{k}\) and contrast \(\beta=1/M\) (\(M\) is known as the mode number of a speckle pattern) follow a negative binomial distribution [20]:  
\[P(k|M,\bar{k})=\frac{\Gamma(k+M)}{\Gamma(M)\Gamma(k+1)}[(\frac{\bar{k}}{\bar{ k}+M})^{k}(\frac{M}{\bar{k}+M})^{M}]. \tag{1}\]  
Here, \(P(k|M,\bar{k})\) denotes the probability of \(k\) photons per pixel events, which will be referred to as \(k\)-photon events in the context. The equation yields an iterative function  
\[\frac{P(k+1)}{P(k)}=\frac{\Gamma(k+M+1)\Gamma(k+1)}{\Gamma(k+M)\Gamma(k+2)}( \frac{\bar{k}}{\bar{k}+M})=\frac{\bar{k}(k+M)}{(k+1)(\bar{k}+M)}. \tag{2}\]  
Specifically for \(k=1\), we have  
\[\beta=\frac{P(1)-2P(2)/\bar{k}}{2P(2)-P(1)}. \tag{3}\]  
For low-intensity speckle patterns, each frame is dominated by the Poisson noise. Contrast can only be estimated with a large number of speckle patterns. At the same time, instead of having a stable count rate, the monochromatized self-amplified spontaneous emitted (SASE) X-ray FEL pulses have shot-to-shot intensity fluctuations following the Gamma distribution. We therefore evaluated the intensity dependence of the algorithms by grouping speckle patterns with similar mean scattering intensities. For each intensity  
Figure 1: (**a**) Average small-angle coherent scattering from silica nanopowders over 100 consecutive frames. Focused beam size at the sample plane was measured to be 5 \(\upmu\)m. Between the two orange dashed lines is the region of interest (ROI) for contrast extraction. It has a center Q = 0.013 Å\({}^{-1}\) and in total 35035 pixels. (**b**) Enlarged view of the speckle pattern in the area outlined in magenta in (**a**). (**c**) Averaged speckle pattern with focused beam size of 10 \(\upmu\)m showing the same ROI in the scattering as in (**b**). (**d**) The contrast reduction factor as a function of detector sampling. The gray line is from the theoretical calculation and the red dots are from simulations. group, we calculated the averaged count rate as well as one-/two-photon probabilities, from which we deduced the speckle contrast following Equation (3). The error \(\sigma_{\beta}\) for each value was estimated from counting statistics by:  
\[\frac{\sigma_{\beta}}{\beta}\approx\frac{1}{k\beta}\sqrt{\frac{2(1+\beta)}{N_{ \text{pixel}}N_{\text{frame}}}}. \tag{4}\]  
Here, \(N_{\text{frame}}\) and \(N_{\text{pixel}}\) denote the total number of frames and the number of pixels in the chosen ROI for each frame separately. To extract the overall effective contrast \(\beta_{e}\), a weighted average of the individual contrasts was performed with the weight, i.e., \(\hat{k}^{2}N_{\text{frame}}\), which is inversely proportional to the variance of the contrast, in order to optimize the log likelihood of the measurement [21]. The error bar for \(\beta_{e}\) is then estimated as the weighted standard deviation of the contrasts from these individual intensity groups. Following this procedure, the contrast values extracted from the photon assignment of the three algorithms are displayed in Figure 2 for all nine measurement conditions. Within each intensity group, all three algorithms show sufficient sensitivity to distinguish the trend of contrast change, e.g., the speckles from 250 \(\upmu\)m slit beam and beam with slits fully open with well below 1% level of contrast difference. However, the three algorithms gave distinct values for the same data set. This confirms our simulation studies [17] and suggests that further calibration is required. One other important observation relates to realistic measurements using monochromatized SASE X-ray FEL pulses having shot-to-shot intensity fluctuations.