tttrlib
A library for time-tagged time resolved data
Loading...
Searching...
No Matches
CLSMImage.h
Go to the documentation of this file.
1#ifndef TTTRLIB_CLSMIMAGE_H
2#define TTTRLIB_CLSMIMAGE_H
3
4#include <iostream> /* cout, clog */
5#include <vector>
6#include <utility>
7
8#include <cstring>
9#include <complex>
10#include <cmath>
11
12#include "pocketfft/pocketfft_hdronly.h"
13
14#include "TTTR.h" /* TTTR */
15
16#include "CLSMFrame.h"
17#include "CLSMLine.h"
18#include "CLSMPixel.h"
19#include "DecayPhasor.h"
20#include "Correlator.h"
21
22
29
30
31template<typename TS, typename TE>
32static std::pair<int, int> find_clsm_start_stop(
33 int &i_event,
34 int marker_start, int marker_stop, int marker_event,
35 unsigned long long* macro_time_arr,
36 TS start_stop_arr,
37 TE event_type_arr,
38 size_t n,
39 long duration = -1
40){
41 int start = -1;
42 int stop = -1;
43 for(; i_event < n; i_event++)
44 {
45 if(event_type_arr[i_event] != marker_event) continue;
46 if(start_stop_arr[i_event] == marker_start)
47 // start found -> search stop and break
48 {
49 start = i_event;
50 if(marker_stop > 0) // search for stop idx using stop event
51 {
52 for(;i_event < n; i_event++){
53 if(event_type_arr[i_event] != marker_event) continue;
54 if(start_stop_arr[i_event] == marker_stop){
55 stop = i_event;
56 break;
57 }
58 }
59 }
60 else if(duration > 0) // search for stop idx using duration
61 {
62 unsigned long stop_time = macro_time_arr[i_event] + duration;
63 for(;i_event < n; i_event++){
64 if(macro_time_arr[i_event] >= stop_time){
65 stop = i_event;
66 break;
67 }
68 }
69 }
70 else // do not search for stop idx
71 {
72 stop = 0;
73 }
74 break;
75 }
76 }
77 return {start, stop};
78}
79
80
81
83
84 friend class CLSMImage;
85
86protected:
87
91
93
96
99
101 std::vector<int> marker_frame_start = {};
102
105
107 int n_lines = 0;
108
109public:
110
134 explicit CLSMSettings(
136 bool skip_after_last_frame_marker = false,
138 int marker_line_start = 3,
139 int marker_line_stop = 2,
140 std::vector<int> marker_frame_start = std::vector<int>({1}),
141 int marker_event_type = 1,
142 int n_pixel_per_line = 1,
143 int n_lines = -1
144 // long long macro_time_shift = 0
145 ){
146 this->skip_before_first_frame_marker = skip_before_first_frame_marker;
147 this->skip_after_last_frame_marker = skip_after_last_frame_marker;
148 this->reading_routine = reading_routine;
149 this->n_pixel_per_line = n_pixel_per_line;
150 this->n_lines = n_lines;
151 this->marker_event_type = marker_event_type;
152 this->marker_line_stop = marker_line_stop;
153 this->marker_line_start = marker_line_start;
154 this->marker_frame_start = marker_frame_start;
155// this->macro_time_shift = macro_time_shift;
156 }
157
158};
159
160
162
163 friend class Correlator;
164 friend class CLSMFrame;
165 friend class CLSMLine;
166 friend class CLSMPixel;
167
168private:
169
170 CLSMSettings settings;
171
173 bool _is_filled_ = false;
174
175 std::vector<CLSMFrame *> frames;
176
177 void remove_incomplete_frames();
178
179 void create_pixels_in_lines();
180
181protected:
182
184 size_t n_frames = 0;
185
187 size_t n_lines = 0;
188
190 size_t n_pixel = 0;
191
193 std::shared_ptr<TTTR> tttr = nullptr;
194
195 void create_frames(bool clear_first = true);
196
198
200
201public:
202
203 std::shared_ptr<TTTR> get_tttr(){
204 return tttr;
205 }
206
207 void set_tttr(std::shared_ptr<TTTR> v){
208 tttr = v;
209 }
210
212 return &settings;
213 }
214
216 size_t size(){
217 return frames.size();
218 }
219
221 std::vector<int> marker_frame;
222
225
228
231
232 std::string reading_routine = "default";
233
238
257 void fill(
258 TTTR *tttr_data = nullptr,
259 std::vector<int> channels = std::vector<int>(),
260 bool clear = true,
261 const std::vector<std::pair<int,int>> &micro_time_ranges = std::vector<std::pair<int,int>>()
262 );
263
285 TTTR *tttr_data,
286 std::vector<int> channels,
287 bool clear_pixel = true,
288 std::vector<std::pair<int,int>> micro_time_ranges = std::vector<std::pair<int,int>>()
289 ){
290 std::clog << "WARNING: 'fill_pixels' deprecated. Use 'fill'." << std::endl;
291 fill(tttr_data, channels, clear_pixel, micro_time_ranges);
292 }
293
297 void clear();
298
307 std::clog << "WARNING: 'clear_pixels' deprecated. Use 'clear'." << std::endl;
308 clear();
309 }
310
321 void strip(const std::vector<int> &tttr_indices, int offset = 0);
322
353 float **output, int *dim1, int *dim2, int *dim3, int *dim4,
354 std::shared_ptr<TTTR> tttr,
355 CLSMImage *clsm_other = nullptr,
356 std::string correlation_method = "default",
357 int n_bins = 50,
358 int n_casc = 1,
359 bool stack_frames = false,
360 bool normalized_correlation = false,
361 int min_photons = 2
362 );
363
369 std::vector<CLSMFrame *> get_frames() {
370 return frames;
371 }
372
385 void get_intensity(unsigned short **output, int *dim1, int *dim2, int *dim3);
386
410 TTTR *tttr_data,
411 unsigned char **output, int *dim1, int *dim2, int *dim3, int *dim4,
412 int micro_time_coarsening = 1,
413 bool stack_frames = false
414 );
415
416
441 TTTR *tttr_data,
442 uint8_t* mask, int dmask1, int dmask2, int dmask3,
443 unsigned int **output, int *dim1, int *dim2,
444 int tac_coarsening,
445 bool stack_frames
446 );
447
448
472 TTTR *tttr_data,
473 double **output, int *dim1, int *dim2, int *dim3,
474 double microtime_resolution = -1.0,
475 int minimum_number_of_photons = 2,
476 bool stack_frames = false
477 );
478
479
507 float **output, int *dim1, int *dim2, int *dim3, int *dim4,
508 TTTR *tttr_data,
509 TTTR *tttr_irf = nullptr,
510 double frequency = -1,
511 int minimum_number_of_photons = 2,
512 bool stack_frames = false
513 );
514
515
549 TTTR *tttr_data,
550 double **output, int *dim1, int *dim2, int *dim3,
551 int minimum_number_of_photons = 3,
552 TTTR *tttr_irf = nullptr, double m0_irf = 1.0, double m1_irf = 1.0,
553 bool stack_frames = false,
554 std::vector<double> background = std::vector<double>(),
555 double m0_bg = 0.0, double m1_bg = 0.0,
556 double background_fraction = -1.0
557 );
558
570 inline int to1D(int frame, int line, int pixel) {
571 return (frame * n_lines * n_pixel) + (line * n_pixel) + pixel;
572 }
573
574
584 inline std::vector<int> to3D(int idx) {
585 int frame = idx / (n_lines * n_pixel);
586 idx -= (frame * n_lines * n_pixel);
587 int line = idx / n_pixel;
588 int pixel = idx % n_pixel;
589 return std::vector<int>{frame, line, pixel};
590 }
591
592
603 CLSMPixel* getPixel(unsigned int idx) {
604 int frame, line, pixel;
605
606 frame = idx / (n_lines * n_pixel);
607 idx -= (frame * n_lines * n_pixel);
608 line = idx / n_pixel;
609 pixel = idx % n_pixel;
610
611 CLSMFrame* s_frame = frames[frame];
612 CLSMLine* s_line = s_frame->lines[line];
613 CLSMPixel* s_pixel = &(s_line->pixels[pixel]);
614 return s_pixel;
615 }
616
617
625 int get_n_frames() const {
626 return n_frames;
627 }
628
629
637 int get_n_lines() const {
638 return n_lines;
639 }
640
641
649 int get_n_pixel() const {
650 return n_pixel;
651 }
652
663 void copy(const CLSMImage &p2, bool fill = false);
664
672 void append(CLSMFrame *frame);
673
683 void transform(unsigned int* input, int n_input);
684
695 void rebin(int bin_line, int bin_pixel);
696
709 unsigned int pixel_id,
710 CLSMImage* target,
711 std::vector<int>& target_pixel_ids,
712 std::vector<int>& target_probabilities
713 );
714
715
728 void crop(
729 int frame_start, int frame_stop,
730 int line_start, int line_stop,
731 int pixel_start, int pixel_stop
732 );
733
741 CLSMFrame* f0 = frames[0];
742 for (unsigned int i = 1; i < n_frames; i++) {
743 *f0 += *frames[i];
744 }
745 frames.resize(1);
746 n_frames = 1;
747 }
748
749
759 CLSMImage(const CLSMImage &p2, bool fill = false);
760
777 explicit CLSMImage(
778 std::shared_ptr<TTTR> tttr_data = nullptr,
779 CLSMSettings settings = CLSMSettings(),
780 CLSMImage *source = nullptr,
781 bool fill = true,
782 std::vector<int> channels = std::vector<int>(),
783 std::vector<std::pair<int, int>> micro_time_ranges =
784 std::vector<std::pair<int, int>>()
785 );
786
793 virtual ~CLSMImage() {
794 for (auto frame : frames) {
795 delete frame;
796 }
797 }
798
799
808 CLSMFrame* operator[](unsigned int i_frame) {
809 return frames[i_frame];
810 }
811
812
841 static void compute_ics(
842 double **output, int *dim1, int *dim2, int *dim3,
843 std::shared_ptr<TTTR> tttr_data = nullptr,
844 CLSMImage* clsm = nullptr,
845 double *images = nullptr, int input_frames = -1, int input_lines = -1, int input_pixel = 1,
846 std::vector<int> x_range = std::vector<int>({0, -1}),
847 std::vector<int> y_range = std::vector<int>({0, -1}),
848 std::vector<std::pair<int, int>> frames_index_pairs = std::vector<std::pair<int, int>>(),
849 std::string subtract_average = "",
850 uint8_t *mask = nullptr, int dmask1 = -1, int dmask2 = -1, int dmask3 = -1
851 );
852
893 static void get_roi(
894 double** output, int* dim1, int* dim2, int* dim3,
895 CLSMImage* clsm = nullptr,
896 std::vector<int> x_range=std::vector<int>({0,-1}),
897 std::vector<int> y_range=std::vector<int>({0,-1}),
898 std::string subtract_average = "",
899 double background = 0.0,
900 bool clip=false, double clip_max=1e6, double clip_min=-1e6,
901 double *images = nullptr, int n_frames=-1, int n_lines=-1, int n_pixels=1,
902 uint8_t *mask = nullptr, int dmask1 = -1, int dmask2 = -1, int dmask3 = -1,
903 std::vector<int> selected_frames = std::vector<int>()
904 );
905
906
933 static std::vector<int> get_frame_edges(
934 TTTR* tttr = nullptr,
935 int start_event = 0,
936 int stop_event = -1,
937 std::vector<int> marker_frame_start = std::vector<int>({4, 6}),
938 int marker_event_type = 15,
942 );
943
964 static std::vector<int> get_line_edges(
965 TTTR* tttr,
966 int start_event, int stop_event,
967 int marker_line_start = 1, int marker_line_stop = 2,
968 int marker_event_type = 15,
970 );
971
972
994 static std::vector<int> get_line_edges_by_duration(
995 TTTR* tttr,
996 int start_event, int stop_event,
997 int marker_line_start = 1,
998 int line_duration = 2,
999 int marker_event_type = 15,
1001 );
1002
1003
1016 double get_line_duration(int frame = 0, int line = 0){
1017 double re = -1.0;
1018 if(tttr != nullptr){
1019 auto header = tttr->get_header();
1020
1021 auto f = frames[frame];
1022 auto l = f->lines[line];
1023
1024 int start = l->get_start();
1025 int stop = l->get_stop();
1026
1027 unsigned long long t_stop = tttr->macro_times[stop];
1028 unsigned long long t_start = tttr->macro_times[start];
1029 unsigned long long dt = t_stop - t_start;
1030 double res = header->get_macro_time_resolution() * 1000.0;
1031 re = dt * res;
1032 }
1033 return re;
1034 }
1035
1049 double get_pixel_duration(int frame = 0, int line = 0){
1050 return get_line_duration(frame, line) / settings.n_pixel_per_line;
1051 }
1052
1053};
1054
1055
1056#endif //TTTRLIB_CLSMIMAGE_H
ReadingRoutine
Different types of distances between two accessible volumes.
Definition CLSMImage.h:24
@ CLSM_SP8
Leica SP5.
Definition CLSMImage.h:27
@ CLSM_DEFAULT
Definition CLSMImage.h:25
@ CLSM_SP5
Default reading compute_icsroutine.
Definition CLSMImage.h:26
Definition CLSMFrame.h:12
Definition CLSMImage.h:161
std::vector< int > to3D(int idx)
Convert a one-dimensional index to three-dimensional coordinates (frame, line, pixel).
Definition CLSMImage.h:584
void get_phasor(float **output, int *dim1, int *dim2, int *dim3, int *dim4, TTTR *tttr_data, TTTR *tttr_irf=nullptr, double frequency=-1, int minimum_number_of_photons=2, bool stack_frames=false)
Computes the phasor values for every pixel.
CLSMImage(std::shared_ptr< TTTR > tttr_data=nullptr, CLSMSettings settings=CLSMSettings(), CLSMImage *source=nullptr, bool fill=true, std::vector< int > channels=std::vector< int >(), std::vector< std::pair< int, int > > micro_time_ranges=std::vector< std::pair< int, int > >())
Constructs a CLSMImage object from TTTR data.
std::shared_ptr< TTTR > get_tttr()
Definition CLSMImage.h:203
static std::vector< int > get_line_edges(TTTR *tttr, int start_event, int stop_event, int marker_line_start=1, int marker_line_stop=2, int marker_event_type=15, int reading_routine=CLSM_SP8)
Identifies line edges by reading start and stop markers in a time-tagged time-resolved (TTTR) dataset...
int get_n_frames() const
Get the number of frames in the CLSM image.
Definition CLSMImage.h:625
int marker_event
The event type used for the marker.
Definition CLSMImage.h:230
int marker_line_start
Defines the marker for a line start.
Definition CLSMImage.h:224
static void get_roi(double **output, int *dim1, int *dim2, int *dim3, CLSMImage *clsm=nullptr, std::vector< int > x_range=std::vector< int >({0,-1}), std::vector< int > y_range=std::vector< int >({0,-1}), std::string subtract_average="", double background=0.0, bool clip=false, double clip_max=1e6, double clip_min=-1e6, double *images=nullptr, int n_frames=-1, int n_lines=-1, int n_pixels=1, uint8_t *mask=nullptr, int dmask1=-1, int dmask2=-1, int dmask3=-1, std::vector< int > selected_frames=std::vector< int >())
Copies a region of interest (ROI) from the input images, performs background correction,...
void get_intensity(unsigned short **output, int *dim1, int *dim2, int *dim3)
Computes an intensity image.
int get_n_lines() const
Get the number of lines per frame in the CLSMImage.
Definition CLSMImage.h:637
bool skip_after_last_frame_marker
Definition CLSMImage.h:237
CLSMFrame * operator[](unsigned int i_frame)
Accessor for CLSMFrame at the specified index in CLSMImage.
Definition CLSMImage.h:808
size_t n_pixel
The number if pixels per line.
Definition CLSMImage.h:190
void copy(const CLSMImage &p2, bool fill=false)
Copy information from another CLSMImage object.
static std::vector< int > get_frame_edges(TTTR *tttr=nullptr, int start_event=0, int stop_event=-1, std::vector< int > marker_frame_start=std::vector< int >({4, 6}), int marker_event_type=15, int reading_routine=CLSM_SP8, bool skip_before_first_frame_marker=false, bool skip_after_last_frame_marker=false)
Retrieves the time-tagged time-resolved (TTTR) indices of frame markers for a Leica SP8 microscope.
void distribute(unsigned int pixel_id, CLSMImage *target, std::vector< int > &target_pixel_ids, std::vector< int > &target_probabilities)
Distribute the photons of a pixel_id to a set of pixel ids in a target image according to provided pr...
bool skip_before_first_frame_marker
Definition CLSMImage.h:236
std::shared_ptr< TTTR > tttr
Pointer to tttr data that used to construct the Image.
Definition CLSMImage.h:193
void get_fluorescence_decay(TTTR *tttr_data, unsigned char **output, int *dim1, int *dim2, int *dim3, int *dim4, int micro_time_coarsening=1, bool stack_frames=false)
Computes an image stack where the value of each pixel corresponds to a histogram of micro times in ea...
static void compute_ics(double **output, int *dim1, int *dim2, int *dim3, std::shared_ptr< TTTR > tttr_data=nullptr, CLSMImage *clsm=nullptr, double *images=nullptr, int input_frames=-1, int input_lines=-1, int input_pixel=1, std::vector< int > x_range=std::vector< int >({0, -1}), std::vector< int > y_range=std::vector< int >({0, -1}), std::vector< std::pair< int, int > > frames_index_pairs=std::vector< std::pair< int, int > >(), std::string subtract_average="", uint8_t *mask=nullptr, int dmask1=-1, int dmask2=-1, int dmask3=-1)
Performs an image correlation spectroscopy analysis via FFTs for a set of frames.
void clear_pixels()
Clears the time-tagged time-resolved (TTTR) indices stored in the pixels.
Definition CLSMImage.h:306
std::vector< int > marker_frame
Vector containing the tttr indices of the frame markers.
Definition CLSMImage.h:221
void fill(TTTR *tttr_data=nullptr, std::vector< int > channels=std::vector< int >(), bool clear=true, const std::vector< std::pair< int, int > > &micro_time_ranges=std::vector< std::pair< int, int > >())
Fills the time-tagged time-resolved (TTTR) indices of the pixels with the indices of the photons that...
int get_n_pixel() const
Get the number of pixels per line in a frame of the CLSMImage.
Definition CLSMImage.h:649
void determine_number_of_lines()
static std::vector< int > get_line_edges_by_duration(TTTR *tttr, int start_event, int stop_event, int marker_line_start=1, int line_duration=2, int marker_event_type=15, int reading_routine=CLSM_SP8)
Identifies line edges by reading a start marker and using line duration as the stop criterion in a ti...
void crop(int frame_start, int frame_stop, int line_start, int line_stop, int pixel_start, int pixel_stop)
Crop the CLSMImage.
size_t size()
Get the number of frames in the CLSMImage.
Definition CLSMImage.h:216
void create_lines()
void transform(unsigned int *input, int n_input)
Move the content of the Pixels based on source and target pixel indices.
void fill_pixels(TTTR *tttr_data, std::vector< int > channels, bool clear_pixel=true, std::vector< std::pair< int, int > > micro_time_ranges=std::vector< std::pair< int, int > >())
Fills the time-tagged time-resolved (TTTR) indices of the pixels with the indices of the channels tha...
Definition CLSMImage.h:284
CLSMImage(const CLSMImage &p2, bool fill=false)
Copy constructor for CLSMImage.
void get_fcs_image(float **output, int *dim1, int *dim2, int *dim3, int *dim4, std::shared_ptr< TTTR > tttr, CLSMImage *clsm_other=nullptr, std::string correlation_method="default", int n_bins=50, int n_casc=1, bool stack_frames=false, bool normalized_correlation=false, int min_photons=2)
Computes an image where pixels represent correlation curves.
void get_mean_lifetime(TTTR *tttr_data, double **output, int *dim1, int *dim2, int *dim3, int minimum_number_of_photons=3, TTTR *tttr_irf=nullptr, double m0_irf=1.0, double m1_irf=1.0, bool stack_frames=false, std::vector< double > background=std::vector< double >(), double m0_bg=0.0, double m1_bg=0.0, double background_fraction=-1.0)
Computes an image of average lifetimes.
void clear()
Clears the time-tagged time-resolved (TTTR) indices stored in the pixels.
size_t n_frames
The number of frames in an CLSMImage.
Definition CLSMImage.h:184
CLSMPixel * getPixel(unsigned int idx)
Get a pointer to a CLSMPixel object based on a one-dimensional index.
Definition CLSMImage.h:603
double get_line_duration(int frame=0, int line=0)
Obtains the duration of a line (in milliseconds) for a specified frame and line.
Definition CLSMImage.h:1016
int to1D(int frame, int line, int pixel)
Convert three-dimensional coordinates (frame, line, pixel) to a one-dimensional index.
Definition CLSMImage.h:570
double get_pixel_duration(int frame=0, int line=0)
Obtains the duration of a pixel (in milliseconds) for a specified frame and line.
Definition CLSMImage.h:1049
void set_tttr(std::shared_ptr< TTTR > v)
Definition CLSMImage.h:207
std::vector< CLSMFrame * > get_frames()
Retrieves the frames stored in the CLSMImage.
Definition CLSMImage.h:369
const CLSMSettings * get_settings()
Definition CLSMImage.h:211
virtual ~CLSMImage()
Destructor for CLSMImage.
Definition CLSMImage.h:793
std::string reading_routine
Definition CLSMImage.h:232
void get_mean_micro_time(TTTR *tttr_data, double **output, int *dim1, int *dim2, int *dim3, double microtime_resolution=-1.0, int minimum_number_of_photons=2, bool stack_frames=false)
Calculates an image stack where the value of each pixel corresponds to the mean micro time (in units ...
void create_frames(bool clear_first=true)
size_t n_lines
The number of lines per frames.
Definition CLSMImage.h:187
void append(CLSMFrame *frame)
Append a CLSMFrame to the CLSM image.
void strip(const std::vector< int > &tttr_indices, int offset=0)
Strips time-tagged time-resolved (TTTR) indices from all pixels in the image.
void get_decay_of_pixels(TTTR *tttr_data, uint8_t *mask, int dmask1, int dmask2, int dmask3, unsigned int **output, int *dim1, int *dim2, int tac_coarsening, bool stack_frames)
Computes micro time histograms for the stacks of images and a selection of pixels....
int marker_line_stop
Defines the marker for a line stop.
Definition CLSMImage.h:227
void stack_frames()
Stack frames in the CLSMImage.
Definition CLSMImage.h:740
void rebin(int bin_line, int bin_pixel)
Rebin a CLSMImage.
Definition CLSMLine.h:9
Definition CLSMPixel.h:7
Definition CLSMImage.h:82
int reading_routine
Definition CLSMImage.h:92
std::vector< int > marker_frame_start
Vector containing the tttr indices of the frame markers.
Definition CLSMImage.h:101
int marker_line_stop
Defines the marker for a line stop.
Definition CLSMImage.h:98
CLSMSettings(bool skip_before_first_frame_marker=false, bool skip_after_last_frame_marker=false, int reading_routine=CLSM_DEFAULT, int marker_line_start=3, int marker_line_stop=2, std::vector< int > marker_frame_start=std::vector< int >({1}), int marker_event_type=1, int n_pixel_per_line=1, int n_lines=-1)
CLSMSettings Constructor.
Definition CLSMImage.h:134
bool skip_before_first_frame_marker
To skip incomplete frames.
Definition CLSMImage.h:89
int marker_event_type
The event type used for the marker.
Definition CLSMImage.h:104
int n_pixel_per_line
Definition CLSMImage.h:106
int marker_line_start
Defines the marker for a line start.
Definition CLSMImage.h:95
int n_lines
Definition CLSMImage.h:107
bool skip_after_last_frame_marker
Definition CLSMImage.h:90
Definition Correlator.h:21
Time-Tagged Time-Resolved (TTTR) data class.
Definition TTTR.h:195