IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
plot_convolution_routines.py
1"""
2Fluorescence decay convolution
3==============================
4
5Fast convolution
6----------------
7
8In ``IMP.bff`` there are a set of different routines that can be used to compute
9fluorescence decays. Most fluorescence decays are linear combinations of exponential
10decays. Convolutions of such fluorescence decays with instrument response functions
11can be computed using the routines (fconv, fconv_per, fconv_per_cs, etc.). Here,
12fconv stands for fast convolution.
13
14``IMP.bff`` provides routines that make use of SIMD (Single Instruction Multiple Data).
15The SIMD routines use of the AVX extension (Advanced Vector Extension). The SIMD routines
16compute in parallel decays composed of more than a single fluorescence lifetime.
17The SIMD routines require CPUs with 'modern' instruction sets. Most x86 CPUs that were
18manufacture since 2012 are supported.
19
20"""
21import IMP.bff
22import sys
23try:
24 import scipy.stats
25except ImportError:
26 print("To run this example, please first install the 'scipy'")
27 print("Python module.")
28 sys.exit(0)
29import time
30import numpy as np
31import pylab as p
32
33period = 16
34n_channels = 2000
35irf_position = 4.0
36irf_width = 0.25
37time_axis = np.linspace(0, period, n_channels)
38irf = scipy.stats.norm.pdf(time_axis, loc=irf_position, scale=irf_width)
39
40dt = time_axis[1]-time_axis[0]
41lifetime_spectrum = np.array([1., 4, 1., 0.4] * 64)
42model = np.zeros_like(irf)
43stop = len(irf)
44start = 0
45n_runs = 50
46
47# benchmark
48################
49times = list()
50times_avx = list()
51names = ["fconv", "fconv_per"]
52t_start = time.perf_counter()
53for _ in range(n_runs):
54 IMP.bff.decay_fconv(fit=model, irf=irf, x=lifetime_spectrum, start=start, stop=stop, dt=dt)
55ex = time.perf_counter() - t_start
56times.append(ex)
57
58# If IMP.bff was built with AVX support, repeat the tests using AVX-accelerated
59# code
60if IMP.bff.IMP_BFF_HAS_AVX:
61 t_start = time.perf_counter()
62 for _ in range(n_runs):
63 IMP.bff.decay_fconv_avx(fit=model, irf=irf, x=lifetime_spectrum,
64 start=start, stop=stop, dt=dt)
65 ex = time.perf_counter() - t_start
66 times_avx.append(ex)
67
68t_start = time.perf_counter() # in seconds
69for _ in range(n_runs):
70 IMP.bff.decay_fconv_per(fit=model, irf=irf, x=lifetime_spectrum, period=period, start=start, stop=stop, dt=dt)
71ex = time.perf_counter() - t_start
72times.append(ex)
73
74if IMP.bff.IMP_BFF_HAS_AVX:
75 t_start = time.perf_counter()
76 for _ in range(n_runs):
77 IMP.bff.decay_fconv_per_avx(fit=model, irf=irf, x=lifetime_spectrum,
78 period=period, start=start, stop=stop,
79 dt=dt)
80 ex = time.perf_counter() - t_start
81 times_avx.append(ex)
82
83times = np.array(times) * 1000.0 / n_runs
84if IMP.bff.IMP_BFF_HAS_AVX:
85 times_avx = np.array(times_avx) * 1000.0 / n_runs
86
87
88# make plots
89##################
90fig, ax = p.subplots(nrows=1, ncols=2, sharex=False)
91
92ax[0].set_title('Convolution routines')
93ax[0].set_ylabel('counts')
94ax[0].set_xlabel('channels')
95
96model = np.zeros_like(irf)
97IMP.bff.decay_fconv(fit=model, irf=irf, x=lifetime_spectrum, start=start, stop=stop, dt=dt)
98ax[0].semilogy(model, label="fconv")
99
100if IMP.bff.IMP_BFF_HAS_AVX:
101 model_avx = np.zeros_like(irf)
102 IMP.bff.decay_fconv_avx(fit=model_avx, irf=irf, x=lifetime_spectrum,
103 start=start, stop=stop, dt=dt)
104 ax[0].semilogy(model, label="fconv_avx")
105
106model = np.zeros_like(irf)
107IMP.bff.decay_fconv_per(fit=model, irf=irf, x=lifetime_spectrum, period=period, start=start, stop=stop, dt=dt)
108ax[0].semilogy(model, label="fconv_per")
109
110if IMP.bff.IMP_BFF_HAS_AVX:
111 model = np.zeros_like(irf)
112 IMP.bff.decay_fconv_per_avx(fit=model, irf=irf, x=lifetime_spectrum,
113 period=period, start=start, stop=stop, dt=dt)
114 ax[0].semilogy(model, label="fconv_per_avx")
115ax[0].legend()
116
117# Benchmark
118ax[1].set_title('Benchmark')
119ax[1].set_ylabel('ms / evaluation')
120
121ind = np.arange(len(times)) # the x locations for the groups
122ax[1].set_title('Time per call')
123ax[1].set_xticks(ind)
124ax[1].set_xticklabels(names)
125
126width = 0.35
127ax[1].bar(ind, times, width, color='r', label='default')
128if IMP.bff.IMP_BFF_HAS_AVX:
129 ax[1].bar(ind + width, times_avx, width, color='y', label='AVX')
130ax[1].legend()
131
132p.savefig('plot.png')
133#p.show()
Bayesian Fluorescence Framework.
void decay_fconv(double *fit, double *x, double *lamp, int numexp, int start, int stop, double dt=0.05)
Convolve lifetime spectrum with instrument response (fast convolution, low repetition rate).
void decay_fconv_per_avx(double *fit, double *x, double *lamp, int numexp, int start, int stop, int n_points, double period, double dt=0.05)
Convolve lifetime spectrum with instrument response (fast convolution, high repetition rate),...
void decay_fconv_avx(double *fit, double *x, double *lamp, int numexp, int start, int stop, double dt=0.05)
Convolve lifetime spectrum with instrument response (fast convolution, AVX optimized for large lifeti...
void decay_fconv_per(double *fit, double *x, double *lamp, int numexp, int start, int stop, int n_points, double period, double dt=0.05)
Convolve lifetime spectrum with instrument response (fast convolution, high repetition rate).