IMP logo
IMP Reference Guide  2.22.0
The Integrative Modeling Platform
DecayCurve.h
Go to the documentation of this file.
1 /**
2  * \file IMP/bff/DecayCurve.h
3  * \brief Class for fluorescence decay curves
4  *
5  * \authors Thomas-Otavio Peulen
6  * Copyright 2007-2022 IMP Inventors. All rights reserved.
7  */
8 #ifndef IMPBFF_DECAYCURVE_H
9 #define IMPBFF_DECAYCURVE_H
10 #include <IMP/bff/bff_config.h>
11 #include <cmath>
12 #include <vector>
13 #include <numeric>
14 #include <algorithm>
15 #include <iostream> /* std::cerr */
16 #include <string>
17 #include <cstring> /* strcmp */
18 #include <limits> /* std::numeric_limits */
19 #include <IMP/bff/internal/MovingAverage.h>
20 #include <IMP/bff/internal/json.h>
21 #include <IMP/bff/DecayRoutines.h>
22 
23 IMPBFF_BEGIN_NAMESPACE
24 
25 enum NoiseModelTypes{
26  NOISE_NA,
27  NOISE_POISSON
28 };
29 
30 /**
31  * \class DecayCurve
32  * \brief Class for fluorescence decay curves
33  *
34  * This class represents fluorescence decay curves. It stores the x-values,
35  * y-values, and error values of the curve. It also provides various operations
36  * and transformations on the curve.
37  */
38 class IMPBFFEXPORT DecayCurve {
39  friend class DecayConvolution;
40  friend class DecayPileup;
41  friend class DecayLinearization;
42  friend class DecayScale;
43  friend class DecayScore;
44  friend class DecayPattern;
45 
46 private:
47 
48  int noise_model = NOISE_NA; /**< The noise model type */
49  double current_shift = 0.0; /**< The current shift applied to the curve */
50  double acquisition_time = std::numeric_limits<double>::max(); /**< The acquisition time of the curve */
51  std::vector<double> dx = {1.0}; /**< The x-value differences */
52  std::vector<double> x = std::vector<double>(); /**< The x-values of the curve */
53  std::vector<double> _y; /**< The initial y-values of the curve */
54  std::vector<double> y = std::vector<double>(); /**< The transformed y-values of the curve */
55  std::vector<double> ey = std::vector<double>(); /**< The error values of the curve */
56 
57  /**
58  * \brief Compute the noise of the curve
59  *
60  * This function computes the noise of the curve based on the noise model type.
61  *
62  * \param noise_model The noise model type
63  */
64  void compute_noise(int noise_model = NOISE_POISSON);
65 
66 public:
67 
68  /**
69  * \brief Shift an array by a given value
70  *
71  * This static function shifts the elements of an array by a given value.
72  *
73  * \param input The input array
74  * \param n_input The size of the input array
75  * \param shift The shift value
76  * \return The shifted array
77  */
78  static std::vector<double> shift_array(
79  double *input, int n_input,
80  double shift
81  );
82 
83  /**
84  * \brief Get the size of the curve
85  *
86  * This function returns the size of the curve, i.e., the number of data points.
87  *
88  * \return The size of the curve
89  */
90  size_t size() const;
91 
92  /**
93  * \brief Check if the curve is empty
94  *
95  * This function checks if the curve is empty, i.e., it has no data points.
96  *
97  * \return True if the curve is empty, false otherwise
98  */
99  bool empty();
100 
101  /**
102  * \brief Get the x-value differences
103  *
104  * This function returns the differences between consecutive x-values.
105  *
106  * \return The x-value differences
107  */
108  std::vector<double> get_dx();
109 
110  /**
111  * \brief Resize the curve
112  *
113  * This function resizes the curve to a given size. If the new size is larger
114  * than the current size, the new elements are initialized with a given value.
115  *
116  * \param n The new size of the curve
117  * \param v The value to initialize the new elements with (default: 0.0)
118  * \param dx The x-value difference (default: 1.0)
119  */
120  void resize(size_t n, double v=0.0, double dx=1.0);
121 
122  /**
123  * \brief Get the average x-value difference
124  *
125  * This function returns the average difference between consecutive x-values.
126  *
127  * \return The average x-value difference
128  */
129  double get_average_dx();
130 
131  /**
132  * \brief Get the x-values of the curve
133  *
134  * This function returns the x-values of the curve.
135  *
136  * \return The x-values of the curve
137  */
138  std::vector<double>& get_x();
139 
140  void set_x(const std::vector<double>& v);
141 
142  /**
143  * @brief Sets the x values of the decay curve.
144  * @param input The array of x values.
145  * @param n_input The number of x values.
146  */
147  void set_x(double* input, int n_input);
148 
149  /**
150  * @brief Returns the y values of the decay curve.
151  * @return The y values.
152  */
153  std::vector<double>& get_y();
154 
155  /**
156  * @brief Sets the y values of the decay curve.
157  * @param v The y values.
158  */
159  void set_y(std::vector<double>& v);
160 
161  /**
162  * @brief Sets the y values of the decay curve.
163  * @param input The array of y values.
164  * @param n_input The number of y values.
165  */
166  void set_y(double* input, int n_input);
167 
168  /**
169  * @brief Returns the error values of the decay curve.
170  * @return The error values.
171  */
172  std::vector<double>& get_ey();
173 
174  /**
175  * @brief Sets the error values of the decay curve.
176  * @param v The error values.
177  */
178  void set_ey(std::vector<double>& v);
179 
180  /**
181  * @brief Sets the error values of the decay curve.
182  * @param input The array of error values.
183  * @param n_input The number of error values.
184  */
185  void set_ey(double* input, int n_input);
186 
187  /**
188  * @brief Sets the acquisition time of the decay curve.
189  * @param v The acquisition time.
190  */
191  void set_acquisition_time(double v);
192 
193  /**
194  * @brief Returns the acquisition time of the decay curve.
195  * @return The acquisition time.
196  */
197  double get_acquisition_time() const;
198 
199  /**
200  * @brief Sets the time shift of the decay curve.
201  * @param v The time shift.
202  */
203  void set_shift(double v);
204 
205  /**
206  * @brief Returns the time shift of the decay curve.
207  * @return The time shift.
208  */
209  double get_shift();
210 
211  /**
212  * @brief Returns the JSON representation of the decay curve.
213  * @return The JSON string.
214  */
215  std::string get_json() const;
216 
217  /**
218  * @brief Reads the decay curve from a JSON string.
219  * @param json_string The JSON string.
220  * @return 0 if successful, -1 otherwise.
221  */
222  int read_json(std::string json_string);
223 
224  DecayCurve(
225  std::vector<double> x = std::vector<double>(),
226  std::vector<double> y = std::vector<double>(),
227  std::vector<double> ey = std::vector<double>(),
228  double acquisition_time = std::numeric_limits<double>::max(),
229  int noise_model = NOISE_POISSON,
230  int size = -1
231  );
232 
233  /**
234  * @brief Calculates the sum of y values within a given range.
235  * @param start The start index.
236  * @param stop The stop index.
237  * @return The sum of y values.
238  */
239  double sum(int start=0, int stop=-1);
240 
241  /**
242  * @brief Applies a simple moving average (SMA) filter to the data.
243  * @param start The start index.
244  * @param stop The stop index.
245  * @param n_window The window size for the SMA filter.
246  * @param normalize Flag indicating whether to normalize the filtered data.
247  */
248  void apply_simple_moving_average(int start, int stop, int n_window=5, bool normalize=false);
249 
250  /**
251  * @brief Overloads the addition operator to add a constant value to the decay curve.
252  * @param v The constant value to be added.
253  * @return A new DecayCurve object representing the result of the addition.
254  */
255  DecayCurve& operator+(double v) const;
256 
257  /**
258  * @brief Overloads the subtraction operator to subtract a constant value from the decay curve.
259  * @param v The constant value to be subtracted.
260  * @return A new DecayCurve object representing the result of the subtraction.
261  */
262  DecayCurve& operator-(double v) const;
263 
264  /**
265  * @brief Overloads the multiplication operator to multiply the decay curve by a constant value.
266  * @param v The constant value to be multiplied by.
267  * @return A new DecayCurve object representing the result of the multiplication.
268  */
269  DecayCurve& operator*(double v) const;
270 
271  /**
272  * @brief Overloads the division operator to divide the decay curve by a constant value.
273  * @param v The constant value to be divided by.
274  * @return A new DecayCurve object representing the result of the division.
275  */
276  DecayCurve& operator/(double v) const;
277 
278  /**
279  * @brief Adds a value to the decay curve.
280  * @param v The value to be added.
281  * @return A reference to the modified DecayCurve object.
282  */
283  DecayCurve& operator+=(double v);
284 
285  /**
286  * @brief Subtracts a value from the decay curve.
287  * @param v The value to be subtracted.
288  * @return A reference to the modified DecayCurve object.
289  */
290  DecayCurve& operator-=(double v);
291 
292  /**
293  * @brief Multiplies the decay curve by a value.
294  * @param v The value to be multiplied by.
295  * @return A reference to the modified DecayCurve object.
296  */
297  DecayCurve& operator*=(double v);
298 
299  /**
300  * @brief Divides the decay curve by a value.
301  * @param v The value to be divided by.
302  * @return A reference to the modified DecayCurve object.
303  */
304  DecayCurve& operator/=(double v);
305 
306  /**
307  * @brief Overloads the '+' operator to add two DecayCurve objects.
308  * @param other The DecayCurve object to be added.
309  * @return A new DecayCurve object that is the result of the addition.
310  */
311  DecayCurve& operator+(const DecayCurve& other) const;
312 
313  /**
314  * @brief Overloads the '-' operator to subtract two DecayCurve objects.
315  * @param other The DecayCurve object to be subtracted.
316  * @return A new DecayCurve object that is the result of the subtraction.
317  */
318  DecayCurve& operator-(const DecayCurve& other) const;
319 
320  /**
321  * @brief Overloads the '*' operator to multiply two DecayCurve objects.
322  * @param other The DecayCurve object to be multiplied.
323  * @return A new DecayCurve object that is the result of the multiplication.
324  */
325  DecayCurve& operator*(const DecayCurve& other) const;
326 
327  /**
328  * @brief Overloads the '/' operator to divide two DecayCurve objects.
329  * @param other The DecayCurve object to be divided.
330  * @return A new DecayCurve object that is the result of the division.
331  */
332  DecayCurve& operator/(const DecayCurve& other) const;
333 
334  /**
335  * @brief Assignment operator.
336  * @param other The DecayCurve object to be assigned.
337  * @return A reference to the assigned DecayCurve object.
338  */
339  DecayCurve& operator=(const DecayCurve& other);
340 
341  /**
342  * @brief Shifts the curve by a float value.
343  * @param v The value to shift the curve by.
344  * @return A reference to the modified DecayCurve object.
345  */
346  DecayCurve& operator<<(double v);
347 
348 };
349 
350 IMPBFF_END_NAMESPACE
351 
352 
353 #endif //IMPBFF_DECAYCURVE_H
A decorator that adds pile-up effects to a DecayCurve object.
Definition: DecayPileup.h:34
Decay routines (e.g. convolution, scaling, and lamp shift routines)
Compute convolved decay.
A decay modifier to apply linearization to a DecayCurve.
A class for scaling a DecayCurve by a constant factor and subtracting a constant background value...
Definition: DecayScale.h:34
Class for scoring model fluorescence decay.
Definition: DecayScore.h:34
Class for fluorescence decay curves.
Definition: DecayCurve.h:38
VectorD< D > operator*(double s, VectorD< D > o)
Definition: VectorD.h:180
The DecayPattern class represents a decay pattern with a constant offset and a background pattern...
Definition: DecayPattern.h:26